[AVX512] Fix VSQRT packed instructions internal names.
[oota-llvm.git] / lib / Target / Mips / Mips16InstrInfo.cpp
1
2 //===-- Mips16InstrInfo.cpp - Mips16 Instruction Information --------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file contains the Mips16 implementation of the TargetInstrInfo class.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "Mips16InstrInfo.h"
15 #include "InstPrinter/MipsInstPrinter.h"
16 #include "MipsMachineFunction.h"
17 #include "MipsTargetMachine.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/RegisterScavenging.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include <cctype>
29
30 using namespace llvm;
31
32 #define DEBUG_TYPE "mips16-instrinfo"
33
34 Mips16InstrInfo::Mips16InstrInfo(const MipsSubtarget &STI)
35     : MipsInstrInfo(STI, Mips::Bimm16), RI(STI) {}
36
37 const MipsRegisterInfo &Mips16InstrInfo::getRegisterInfo() const {
38   return RI;
39 }
40
41 /// isLoadFromStackSlot - If the specified machine instruction is a direct
42 /// load from a stack slot, return the virtual or physical register number of
43 /// the destination along with the FrameIndex of the loaded stack slot.  If
44 /// not, return 0.  This predicate must return 0 if the instruction has
45 /// any side effects other than loading from the stack slot.
46 unsigned Mips16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
47                                               int &FrameIndex) const {
48   return 0;
49 }
50
51 /// isStoreToStackSlot - If the specified machine instruction is a direct
52 /// store to a stack slot, return the virtual or physical register number of
53 /// the source reg along with the FrameIndex of the loaded stack slot.  If
54 /// not, return 0.  This predicate must return 0 if the instruction has
55 /// any side effects other than storing to the stack slot.
56 unsigned Mips16InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
57                                              int &FrameIndex) const {
58   return 0;
59 }
60
61 void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
62                                   MachineBasicBlock::iterator I, DebugLoc DL,
63                                   unsigned DestReg, unsigned SrcReg,
64                                   bool KillSrc) const {
65   unsigned Opc = 0;
66
67   if (Mips::CPU16RegsRegClass.contains(DestReg) &&
68       Mips::GPR32RegClass.contains(SrcReg))
69     Opc = Mips::MoveR3216;
70   else if (Mips::GPR32RegClass.contains(DestReg) &&
71            Mips::CPU16RegsRegClass.contains(SrcReg))
72     Opc = Mips::Move32R16;
73   else if ((SrcReg == Mips::HI0) &&
74            (Mips::CPU16RegsRegClass.contains(DestReg)))
75     Opc = Mips::Mfhi16, SrcReg = 0;
76
77   else if ((SrcReg == Mips::LO0) &&
78            (Mips::CPU16RegsRegClass.contains(DestReg)))
79     Opc = Mips::Mflo16, SrcReg = 0;
80
81
82   assert(Opc && "Cannot copy registers");
83
84   MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
85
86   if (DestReg)
87     MIB.addReg(DestReg, RegState::Define);
88
89   if (SrcReg)
90     MIB.addReg(SrcReg, getKillRegState(KillSrc));
91 }
92
93 void Mips16InstrInfo::storeRegToStack(MachineBasicBlock &MBB,
94                                       MachineBasicBlock::iterator I,
95                                       unsigned SrcReg, bool isKill, int FI,
96                                       const TargetRegisterClass *RC,
97                                       const TargetRegisterInfo *TRI,
98                                       int64_t Offset) const {
99   DebugLoc DL;
100   if (I != MBB.end()) DL = I->getDebugLoc();
101   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
102   unsigned Opc = 0;
103   if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
104     Opc = Mips::SwRxSpImmX16;
105   assert(Opc && "Register class not handled!");
106   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)).
107       addFrameIndex(FI).addImm(Offset)
108       .addMemOperand(MMO);
109 }
110
111 void Mips16InstrInfo::loadRegFromStack(MachineBasicBlock &MBB,
112                                        MachineBasicBlock::iterator I,
113                                        unsigned DestReg, int FI,
114                                        const TargetRegisterClass *RC,
115                                        const TargetRegisterInfo *TRI,
116                                        int64_t Offset) const {
117   DebugLoc DL;
118   if (I != MBB.end()) DL = I->getDebugLoc();
119   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
120   unsigned Opc = 0;
121
122   if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
123     Opc = Mips::LwRxSpImmX16;
124   assert(Opc && "Register class not handled!");
125   BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
126     .addMemOperand(MMO);
127 }
128
129 bool Mips16InstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
130   MachineBasicBlock &MBB = *MI->getParent();
131   switch(MI->getDesc().getOpcode()) {
132   default:
133     return false;
134   case Mips::RetRA16:
135     ExpandRetRA16(MBB, MI, Mips::JrcRa16);
136     break;
137   }
138
139   MBB.erase(MI);
140   return true;
141 }
142
143 /// GetOppositeBranchOpc - Return the inverse of the specified
144 /// opcode, e.g. turning BEQ to BNE.
145 unsigned Mips16InstrInfo::getOppositeBranchOpc(unsigned Opc) const {
146   switch (Opc) {
147   default:  llvm_unreachable("Illegal opcode!");
148   case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
149   case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
150   case Mips::BeqzRxImm16: return Mips::BnezRxImm16;
151   case Mips::BnezRxImm16: return Mips::BeqzRxImm16;
152   case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
153   case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
154   case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
155   case Mips::Btnez16: return Mips::Bteqz16;
156   case Mips::BtnezX16: return Mips::BteqzX16;
157   case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
158   case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
159   case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
160   case Mips::Bteqz16: return Mips::Btnez16;
161   case Mips::BteqzX16: return Mips::BtnezX16;
162   case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
163   case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
164   case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
165   case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
166   case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
167   case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
168   }
169   assert(false && "Implement this function.");
170   return 0;
171 }
172
173 static void addSaveRestoreRegs(MachineInstrBuilder &MIB,
174                                const std::vector<CalleeSavedInfo> &CSI,
175                                unsigned Flags = 0) {
176   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
177     // Add the callee-saved register as live-in. Do not add if the register is
178     // RA and return address is taken, because it has already been added in
179     // method MipsTargetLowering::LowerRETURNADDR.
180     // It's killed at the spill, unless the register is RA and return address
181     // is taken.
182     unsigned Reg = CSI[e-i-1].getReg();
183     switch (Reg) {
184     case Mips::RA:
185     case Mips::S0:
186     case Mips::S1:
187       MIB.addReg(Reg, Flags);
188       break;
189     case Mips::S2:
190       break;
191     default:
192       llvm_unreachable("unexpected mips16 callee saved register");
193
194     }
195   }
196 }
197 // Adjust SP by FrameSize bytes. Save RA, S0, S1
198 void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
199                                 MachineBasicBlock &MBB,
200                                 MachineBasicBlock::iterator I) const {
201   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
202   MachineFunction &MF = *MBB.getParent();
203   MachineFrameInfo *MFI    = MF.getFrameInfo();
204   const BitVector Reserved = RI.getReservedRegs(MF);
205   bool SaveS2 = Reserved[Mips::S2];
206   MachineInstrBuilder MIB;
207   unsigned Opc = ((FrameSize <= 128) && !SaveS2)? Mips::Save16:Mips::SaveX16;
208   MIB = BuildMI(MBB, I, DL, get(Opc));
209   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
210   addSaveRestoreRegs(MIB, CSI);
211   if (SaveS2)
212     MIB.addReg(Mips::S2);
213   if (isUInt<11>(FrameSize))
214     MIB.addImm(FrameSize);
215   else {
216     int Base = 2040; // should create template function like isUInt that
217                      // returns largest possible n bit unsigned integer
218     int64_t Remainder = FrameSize - Base;
219     MIB.addImm(Base);
220     if (isInt<16>(-Remainder))
221       BuildAddiuSpImm(MBB, I, -Remainder);
222     else
223       adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
224   }
225 }
226
227 // Adjust SP by FrameSize bytes. Restore RA, S0, S1
228 void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
229                                    MachineBasicBlock &MBB,
230                                    MachineBasicBlock::iterator I) const {
231   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
232   MachineFunction *MF = MBB.getParent();
233   MachineFrameInfo *MFI    = MF->getFrameInfo();
234   const BitVector Reserved = RI.getReservedRegs(*MF);
235   bool SaveS2 = Reserved[Mips::S2];
236   MachineInstrBuilder MIB;
237   unsigned Opc = ((FrameSize <= 128) && !SaveS2)?
238     Mips::Restore16:Mips::RestoreX16;
239
240   if (!isUInt<11>(FrameSize)) {
241     unsigned Base = 2040;
242     int64_t Remainder = FrameSize - Base;
243     FrameSize = Base; // should create template function like isUInt that
244                      // returns largest possible n bit unsigned integer
245
246     if (isInt<16>(Remainder))
247       BuildAddiuSpImm(MBB, I, Remainder);
248     else
249       adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
250   }
251   MIB = BuildMI(MBB, I, DL, get(Opc));
252   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
253   addSaveRestoreRegs(MIB, CSI, RegState::Define);
254   if (SaveS2)
255     MIB.addReg(Mips::S2, RegState::Define);
256   MIB.addImm(FrameSize);
257 }
258
259 // Adjust SP by Amount bytes where bytes can be up to 32bit number.
260 // This can only be called at times that we know that there is at least one free
261 // register.
262 // This is clearly safe at prologue and epilogue.
263 //
264 void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
265                                         MachineBasicBlock &MBB,
266                                         MachineBasicBlock::iterator I,
267                                         unsigned Reg1, unsigned Reg2) const {
268   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
269   //
270   // li reg1, constant
271   // move reg2, sp
272   // add reg1, reg1, reg2
273   // move sp, reg1
274   //
275   //
276   MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
277   MIB1.addImm(Amount).addImm(-1);
278   MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
279   MIB2.addReg(Mips::SP, RegState::Kill);
280   MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
281   MIB3.addReg(Reg1);
282   MIB3.addReg(Reg2, RegState::Kill);
283   MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
284                                                      Mips::SP);
285   MIB4.addReg(Reg1, RegState::Kill);
286 }
287
288 void Mips16InstrInfo::adjustStackPtrBigUnrestricted(
289     unsigned SP, int64_t Amount, MachineBasicBlock &MBB,
290     MachineBasicBlock::iterator I) const {
291    assert(false && "adjust stack pointer amount exceeded");
292 }
293
294 /// Adjust SP by Amount bytes.
295 void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
296                                      MachineBasicBlock &MBB,
297                                      MachineBasicBlock::iterator I) const {
298   if (isInt<16>(Amount))  // need to change to addiu sp, ....and isInt<16>
299     BuildAddiuSpImm(MBB, I, Amount);
300   else
301     adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
302 }
303
304 /// This function generates the sequence of instructions needed to get the
305 /// result of adding register REG and immediate IMM.
306 unsigned Mips16InstrInfo::loadImmediate(unsigned FrameReg, int64_t Imm,
307                                         MachineBasicBlock &MBB,
308                                         MachineBasicBlock::iterator II,
309                                         DebugLoc DL, unsigned &NewImm) const {
310   //
311   // given original instruction is:
312   // Instr rx, T[offset] where offset is too big.
313   //
314   // lo = offset & 0xFFFF
315   // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
316   //
317   // let T = temporary register
318   // li T, hi
319   // shl T, 16
320   // add T, Rx, T
321   //
322   RegScavenger rs;
323   int32_t lo = Imm & 0xFFFF;
324   NewImm = lo;
325   int Reg =0;
326   int SpReg = 0;
327
328   rs.enterBasicBlock(&MBB);
329   rs.forward(II);
330   //
331   // We need to know which registers can be used, in the case where there
332   // are not enough free registers. We exclude all registers that
333   // are used in the instruction that we are helping.
334   //  // Consider all allocatable registers in the register class initially
335   BitVector Candidates =
336       RI.getAllocatableSet
337       (*II->getParent()->getParent(), &Mips::CPU16RegsRegClass);
338   // Exclude all the registers being used by the instruction.
339   for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
340     MachineOperand &MO = II->getOperand(i);
341     if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
342         !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
343       Candidates.reset(MO.getReg());
344   }
345
346   // If the same register was used and defined in an instruction, then
347   // it will not be in the list of candidates.
348   //
349   // we need to analyze the instruction that we are helping.
350   // we need to know if it defines register x but register x is not
351   // present as an operand of the instruction. this tells
352   // whether the register is live before the instruction. if it's not
353   // then we don't need to save it in case there are no free registers.
354   int DefReg = 0;
355   for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
356     MachineOperand &MO = II->getOperand(i);
357     if (MO.isReg() && MO.isDef()) {
358       DefReg = MO.getReg();
359       break;
360     }
361   }
362
363   BitVector Available = rs.getRegsAvailable(&Mips::CPU16RegsRegClass);
364   Available &= Candidates;
365   //
366   // we use T0 for the first register, if we need to save something away.
367   // we use T1 for the second register, if we need to save something away.
368   //
369   unsigned FirstRegSaved =0, SecondRegSaved=0;
370   unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
371
372   Reg = Available.find_first();
373
374   if (Reg == -1) {
375     Reg = Candidates.find_first();
376     Candidates.reset(Reg);
377     if (DefReg != Reg) {
378       FirstRegSaved = Reg;
379       FirstRegSavedTo = Mips::T0;
380       copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
381     }
382   }
383   else
384     Available.reset(Reg);
385   BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm).addImm(-1);
386   NewImm = 0;
387   if (FrameReg == Mips::SP) {
388     SpReg = Available.find_first();
389     if (SpReg == -1) {
390       SpReg = Candidates.find_first();
391       // Candidates.reset(SpReg); // not really needed
392       if (DefReg!= SpReg) {
393         SecondRegSaved = SpReg;
394         SecondRegSavedTo = Mips::T1;
395       }
396       if (SecondRegSaved)
397         copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
398     }
399    else
400      Available.reset(SpReg);
401     copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
402     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(SpReg, RegState::Kill)
403       .addReg(Reg);
404   }
405   else
406     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(FrameReg)
407       .addReg(Reg, RegState::Kill);
408   if (FirstRegSaved || SecondRegSaved) {
409     II = std::next(II);
410     if (FirstRegSaved)
411       copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
412     if (SecondRegSaved)
413       copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
414   }
415   return Reg;
416 }
417
418 unsigned Mips16InstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
419   return (Opc == Mips::BeqzRxImmX16   || Opc == Mips::BimmX16  ||
420           Opc == Mips::Bimm16  ||
421           Opc == Mips::Bteqz16        || Opc == Mips::Btnez16 ||
422           Opc == Mips::BeqzRxImm16    || Opc == Mips::BnezRxImm16   ||
423           Opc == Mips::BnezRxImmX16   || Opc == Mips::BteqzX16 ||
424           Opc == Mips::BteqzT8CmpX16  || Opc == Mips::BteqzT8CmpiX16 ||
425           Opc == Mips::BteqzT8SltX16  || Opc == Mips::BteqzT8SltuX16  ||
426           Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
427           Opc == Mips::BtnezX16       || Opc == Mips::BtnezT8CmpX16 ||
428           Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
429           Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
430           Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
431 }
432
433 void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
434                                   MachineBasicBlock::iterator I,
435                                   unsigned Opc) const {
436   BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
437 }
438
439 const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
440   if (validSpImm8(Imm))
441     return get(Mips::AddiuSpImm16);
442   else
443     return get(Mips::AddiuSpImmX16);
444 }
445
446 void Mips16InstrInfo::BuildAddiuSpImm
447   (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
448   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
449   BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
450 }
451
452 const MipsInstrInfo *llvm::createMips16InstrInfo(const MipsSubtarget &STI) {
453   return new Mips16InstrInfo(STI);
454 }
455
456 bool Mips16InstrInfo::validImmediate(unsigned Opcode, unsigned Reg,
457                                      int64_t Amount) {
458   switch (Opcode) {
459   case Mips::LbRxRyOffMemX16:
460   case Mips::LbuRxRyOffMemX16:
461   case Mips::LhRxRyOffMemX16:
462   case Mips::LhuRxRyOffMemX16:
463   case Mips::SbRxRyOffMemX16:
464   case Mips::ShRxRyOffMemX16:
465   case Mips::LwRxRyOffMemX16:
466   case Mips::SwRxRyOffMemX16:
467   case Mips::SwRxSpImmX16:
468   case Mips::LwRxSpImmX16:
469     return isInt<16>(Amount);
470   case Mips::AddiuRxRyOffMemX16:
471     if ((Reg == Mips::PC) || (Reg == Mips::SP))
472       return isInt<16>(Amount);
473     return isInt<15>(Amount);
474   }
475   llvm_unreachable("unexpected Opcode in validImmediate");
476 }
477
478 /// Measure the specified inline asm to determine an approximation of its
479 /// length.
480 /// Comments (which run till the next SeparatorString or newline) do not
481 /// count as an instruction.
482 /// Any other non-whitespace text is considered an instruction, with
483 /// multiple instructions separated by SeparatorString or newlines.
484 /// Variable-length instructions are not handled here; this function
485 /// may be overloaded in the target code to do that.
486 /// We implement the special case of the .space directive taking only an
487 /// integer argument, which is the size in bytes. This is used for creating
488 /// inline code spacing for testing purposes using inline assembly.
489 ///
490 unsigned Mips16InstrInfo::getInlineAsmLength(const char *Str,
491                                              const MCAsmInfo &MAI) const {
492
493   // Count the number of instructions in the asm.
494   bool atInsnStart = true;
495   unsigned Length = 0;
496   for (; *Str; ++Str) {
497     if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
498                                 strlen(MAI.getSeparatorString())) == 0)
499       atInsnStart = true;
500     if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
501       if (strncmp(Str, ".space", 6)==0) {
502         char *EStr; int Sz;
503         Sz = strtol(Str+6, &EStr, 10);
504         while (isspace(*EStr)) ++EStr;
505         if (*EStr=='\0') {
506           DEBUG(dbgs() << "parsed .space " << Sz << '\n');
507           return Sz;
508         }
509       }
510       Length += MAI.getMaxInstLength();
511       atInsnStart = false;
512     }
513     if (atInsnStart && strncmp(Str, MAI.getCommentString(),
514                                strlen(MAI.getCommentString())) == 0)
515       atInsnStart = false;
516   }
517
518   return Length;
519 }