e0277ff5a00d19d2d2111a0b7debb3b4893b7b7c
[oota-llvm.git] / lib / Target / Mips / Mips16InstrInfo.cpp
1 //===-- Mips16InstrInfo.cpp - Mips16 Instruction Information --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the Mips16 implementation of the TargetInstrInfo class.
11 //
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/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/TargetRegistry.h"
27
28 using namespace llvm;
29
30 static cl::opt<bool> NeverUseSaveRestore(
31   "mips16-never-use-save-restore",
32   cl::init(false),
33   cl::desc("For testing ability to adjust stack pointer "
34            "without save/restore instruction"),
35   cl::Hidden);
36
37
38 Mips16InstrInfo::Mips16InstrInfo(MipsTargetMachine &tm)
39   : MipsInstrInfo(tm, Mips::BimmX16),
40     RI(*tm.getSubtargetImpl(), *this) {}
41
42 const MipsRegisterInfo &Mips16InstrInfo::getRegisterInfo() const {
43   return RI;
44 }
45
46 /// isLoadFromStackSlot - If the specified machine instruction is a direct
47 /// load from a stack slot, return the virtual or physical register number of
48 /// the destination along with the FrameIndex of the loaded stack slot.  If
49 /// not, return 0.  This predicate must return 0 if the instruction has
50 /// any side effects other than loading from the stack slot.
51 unsigned Mips16InstrInfo::
52 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
53 {
54   return 0;
55 }
56
57 /// isStoreToStackSlot - If the specified machine instruction is a direct
58 /// store to a stack slot, return the virtual or physical register number of
59 /// the source reg along with the FrameIndex of the loaded stack slot.  If
60 /// not, return 0.  This predicate must return 0 if the instruction has
61 /// any side effects other than storing to the stack slot.
62 unsigned Mips16InstrInfo::
63 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
64 {
65   return 0;
66 }
67
68 void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
69                                   MachineBasicBlock::iterator I, DebugLoc DL,
70                                   unsigned DestReg, unsigned SrcReg,
71                                   bool KillSrc) const {
72   unsigned Opc = 0;
73
74   if (Mips::CPU16RegsRegClass.contains(DestReg) &&
75       Mips::CPURegsRegClass.contains(SrcReg))
76     Opc = Mips::MoveR3216;
77   else if (Mips::CPURegsRegClass.contains(DestReg) &&
78            Mips::CPU16RegsRegClass.contains(SrcReg))
79     Opc = Mips::Move32R16;
80   else if ((SrcReg == Mips::HI) &&
81            (Mips::CPU16RegsRegClass.contains(DestReg)))
82     Opc = Mips::Mfhi16, SrcReg = 0;
83
84   else if ((SrcReg == Mips::LO) &&
85            (Mips::CPU16RegsRegClass.contains(DestReg)))
86     Opc = Mips::Mflo16, SrcReg = 0;
87
88
89   assert(Opc && "Cannot copy registers");
90
91   MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
92
93   if (DestReg)
94     MIB.addReg(DestReg, RegState::Define);
95
96   if (SrcReg)
97     MIB.addReg(SrcReg, getKillRegState(KillSrc));
98 }
99
100 void Mips16InstrInfo::
101 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
102                     unsigned SrcReg, bool isKill, int FI,
103                     const TargetRegisterClass *RC,
104                     const TargetRegisterInfo *TRI) const {
105   DebugLoc DL;
106   if (I != MBB.end()) DL = I->getDebugLoc();
107   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
108   unsigned Opc = 0;
109   if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
110     Opc = Mips::SwRxSpImmX16;
111   assert(Opc && "Register class not handled!");
112   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
113     .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
114 }
115
116 void Mips16InstrInfo::
117 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
118                      unsigned DestReg, int FI,
119                      const TargetRegisterClass *RC,
120                      const TargetRegisterInfo *TRI) const {
121   DebugLoc DL;
122   if (I != MBB.end()) DL = I->getDebugLoc();
123   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
124   unsigned Opc = 0;
125
126   if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
127     Opc = Mips::LwRxSpImmX16;
128   assert(Opc && "Register class not handled!");
129   BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0)
130     .addMemOperand(MMO);
131 }
132
133 bool Mips16InstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
134   MachineBasicBlock &MBB = *MI->getParent();
135
136   switch(MI->getDesc().getOpcode()) {
137   default:
138     return false;
139   case Mips::BteqzT8CmpX16:
140     ExpandFEXT_T8I816_ins(MBB, MI, Mips::BteqzX16, Mips::CmpRxRy16);
141     break;
142   case Mips::BteqzT8SltX16:
143     ExpandFEXT_T8I816_ins(MBB, MI, Mips::BteqzX16, Mips::SltRxRy16);
144     break;
145   case Mips::BteqzT8SltuX16:
146     // TBD: figure out a way to get this or remove the instruction
147     // altogether.
148     ExpandFEXT_T8I816_ins(MBB, MI, Mips::BteqzX16, Mips::SltuRxRy16);
149     break;
150   case Mips::BtnezT8CmpX16:
151     ExpandFEXT_T8I816_ins(MBB, MI, Mips::BtnezX16, Mips::CmpRxRy16);
152     break;
153   case Mips::RetRA16:
154     ExpandRetRA16(MBB, MI, Mips::JrcRa16);
155     break;
156   }
157
158   MBB.erase(MI);
159   return true;
160 }
161
162 /// GetOppositeBranchOpc - Return the inverse of the specified
163 /// opcode, e.g. turning BEQ to BNE.
164 unsigned Mips16InstrInfo::GetOppositeBranchOpc(unsigned Opc) const {
165   switch (Opc) {
166   default:  llvm_unreachable("Illegal opcode!");
167   case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
168   case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
169   case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
170   case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
171   case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
172   case Mips::BtnezX16: return Mips::BteqzX16;
173   case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
174   case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
175   case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
176   case Mips::BteqzX16: return Mips::BtnezX16;
177   case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
178   case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
179   case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
180   case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
181   case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
182   case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
183   }
184   assert(false && "Implement this function.");
185   return 0;
186 }
187
188 // Adjust SP by FrameSize bytes. Save RA, S0, S1
189 void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
190                     MachineBasicBlock &MBB,
191                     MachineBasicBlock::iterator I) const {
192   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
193   if (!NeverUseSaveRestore) {
194     if (isUInt<11>(FrameSize))
195       BuildMI(MBB, I, DL, get(Mips::SaveRaF16)).addImm(FrameSize);
196     else {
197       int Base = 2040; // should create template function like isUInt that
198                        // returns largest possible n bit unsigned integer
199       int64_t Remainder = FrameSize - Base;
200       BuildMI(MBB, I, DL, get(Mips::SaveRaF16)). addImm(Base);
201       if (isInt<16>(-Remainder))
202         BuildAddiuSpImm(MBB, I, -Remainder);
203       else
204         adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
205     }
206
207   }
208   else {
209     //
210     // sw ra, -4[sp]
211     // sw s1, -8[sp]
212     // sw s0, -12[sp]
213
214     MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
215                                        Mips::RA);
216     MIB1.addReg(Mips::SP);
217     MIB1.addImm(-4);
218     MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
219                                        Mips::S1);
220     MIB2.addReg(Mips::SP);
221     MIB2.addImm(-8);
222     MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
223                                        Mips::S0);
224     MIB3.addReg(Mips::SP);
225     MIB3.addImm(-12);
226     adjustStackPtrBig(SP, -FrameSize, MBB, I, Mips::V0, Mips::V1);
227   }
228 }
229
230 // Adjust SP by FrameSize bytes. Restore RA, S0, S1
231 void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
232                                    MachineBasicBlock &MBB,
233                                    MachineBasicBlock::iterator I) const {
234   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
235   if (!NeverUseSaveRestore) {
236     if (isUInt<11>(FrameSize))
237       BuildMI(MBB, I, DL, get(Mips::RestoreRaF16)).addImm(FrameSize);
238     else {
239       int Base = 2040; // should create template function like isUInt that
240                        // returns largest possible n bit unsigned integer
241       int64_t Remainder = FrameSize - Base;
242       if (isInt<16>(Remainder))
243         BuildAddiuSpImm(MBB, I, Remainder);
244       else
245         adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
246       BuildMI(MBB, I, DL, get(Mips::RestoreRaF16)). addImm(Base);
247     }
248   }
249   else {
250     adjustStackPtrBig(SP, FrameSize, MBB, I, Mips::A0, Mips::A1);
251     // lw ra, -4[sp]
252     // lw s1, -8[sp]
253     // lw s0, -12[sp]
254     MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
255                                        Mips::A0);
256     MIB1.addReg(Mips::SP);
257     MIB1.addImm(-4);
258     MachineInstrBuilder MIB0 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
259                                        Mips::RA);
260      MIB0.addReg(Mips::A0);
261     MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
262                                        Mips::S1);
263     MIB2.addReg(Mips::SP);
264     MIB2.addImm(-8);
265     MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
266                                        Mips::S0);
267     MIB3.addReg(Mips::SP);
268     MIB3.addImm(-12);
269   }
270
271 }
272
273 // Adjust SP by Amount bytes where bytes can be up to 32bit number.
274 // This can only be called at times that we know that there is at least one free
275 // register.
276 // This is clearly safe at prologue and epilogue.
277 //
278 void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
279                                         MachineBasicBlock &MBB,
280                                         MachineBasicBlock::iterator I,
281                                         unsigned Reg1, unsigned Reg2) const {
282   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
283 //  MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
284 //  unsigned Reg1 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
285 //  unsigned Reg2 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
286   //
287   // li reg1, constant
288   // move reg2, sp
289   // add reg1, reg1, reg2
290   // move sp, reg1
291   //
292   //
293   MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
294   MIB1.addImm(Amount);
295   MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
296   MIB2.addReg(Mips::SP, RegState::Kill);
297   MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
298   MIB3.addReg(Reg1);
299   MIB3.addReg(Reg2, RegState::Kill);
300   MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
301                                                      Mips::SP);
302   MIB4.addReg(Reg1, RegState::Kill);
303 }
304
305 void Mips16InstrInfo::adjustStackPtrBigUnrestricted(unsigned SP, int64_t Amount,
306                     MachineBasicBlock &MBB,
307                     MachineBasicBlock::iterator I) const {
308    assert(false && "adjust stack pointer amount exceeded");
309 }
310
311 /// Adjust SP by Amount bytes.
312 void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
313                                      MachineBasicBlock &MBB,
314                                      MachineBasicBlock::iterator I) const {
315   if (isInt<16>(Amount))  // need to change to addiu sp, ....and isInt<16>
316     BuildAddiuSpImm(MBB, I, Amount);
317   else
318     adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
319 }
320
321 /// This function generates the sequence of instructions needed to get the
322 /// result of adding register REG and immediate IMM.
323 unsigned
324 Mips16InstrInfo::loadImmediate(unsigned FrameReg,
325                                int64_t Imm, MachineBasicBlock &MBB,
326                                MachineBasicBlock::iterator II, DebugLoc DL,
327                                unsigned &NewImm) const {
328   //
329   // given original instruction is:
330   // Instr rx, T[offset] where offset is too big.
331   //
332   // lo = offset & 0xFFFF
333   // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
334   //
335   // let T = temporary register
336   // li T, hi
337   // shl T, 16
338   // add T, Rx, T
339   //
340   RegScavenger rs;
341   int32_t lo = Imm & 0xFFFF;
342   int32_t hi = ((Imm >> 16) + (lo >> 15)) & 0xFFFF;
343   NewImm = lo;
344   unsigned Reg =0;
345   unsigned SpReg = 0;
346   rs.enterBasicBlock(&MBB);
347   rs.forward(II);
348   //
349   // we use T0 for the first register, if we need to save something away.
350   // we use T1 for the second register, if we need to save something away.
351   //
352   unsigned FirstRegSaved =0, SecondRegSaved=0;
353   unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
354
355   Reg = rs.FindUnusedReg(&Mips::CPU16RegsRegClass);
356   if (Reg == 0) {
357     FirstRegSaved = Reg = Mips::V0;
358     FirstRegSavedTo = Mips::T0;
359     copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
360   }
361   else
362     rs.setUsed(Reg);
363   BuildMI(MBB, II, DL, get(Mips::LiRxImmX16), Reg).addImm(hi);
364   BuildMI(MBB, II, DL, get(Mips::SllX16), Reg).addReg(Reg).
365     addImm(16);
366   if (FrameReg == Mips::SP) {
367     SpReg = rs.FindUnusedReg(&Mips::CPU16RegsRegClass);
368     if (SpReg == 0) {
369       if (Reg != Mips::V1) {
370         SecondRegSaved = SpReg = Mips::V1;
371         SecondRegSavedTo = Mips::T1;
372       }
373       else {
374         SecondRegSaved = SpReg = Mips::V0;
375         SecondRegSavedTo = Mips::T0;
376       }
377       copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
378     }
379     else
380       rs.setUsed(SpReg);
381
382     copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
383     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(SpReg)
384       .addReg(Reg);
385   }
386   else
387     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(FrameReg)
388       .addReg(Reg, RegState::Kill);
389   if (FirstRegSaved || SecondRegSaved) {
390     II = llvm::next(II);
391     if (FirstRegSaved)
392       copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
393     if (SecondRegSaved)
394       copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
395   }
396   return Reg;
397 }
398
399 unsigned Mips16InstrInfo::GetAnalyzableBrOpc(unsigned Opc) const {
400   return (Opc == Mips::BeqzRxImmX16   || Opc == Mips::BimmX16  ||
401           Opc == Mips::BnezRxImmX16   || Opc == Mips::BteqzX16 ||
402           Opc == Mips::BteqzT8CmpX16  || Opc == Mips::BteqzT8CmpiX16 ||
403           Opc == Mips::BteqzT8SltX16  || Opc == Mips::BteqzT8SltuX16  ||
404           Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
405           Opc == Mips::BtnezX16       || Opc == Mips::BtnezT8CmpX16 ||
406           Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
407           Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
408           Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
409 }
410
411 void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
412                                   MachineBasicBlock::iterator I,
413                                   unsigned Opc) const {
414   BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
415 }
416
417
418 void Mips16InstrInfo::ExpandFEXT_T8I816_ins(
419   MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
420   unsigned BtOpc, unsigned CmpOpc) const {
421   unsigned regX = I->getOperand(0).getReg();
422   unsigned regY = I->getOperand(1).getReg();
423   MachineBasicBlock *target = I->getOperand(2).getMBB();
424   BuildMI(MBB, I, I->getDebugLoc(), get(CmpOpc)).addReg(regX).addReg(regY);
425   BuildMI(MBB, I, I->getDebugLoc(), get(BtOpc)).addMBB(target);
426
427 }
428 const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
429   if (validSpImm8(Imm))
430     return get(Mips::AddiuSpImm16);
431   else
432     return get(Mips::AddiuSpImmX16);
433 }
434
435 void Mips16InstrInfo::BuildAddiuSpImm
436   (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
437   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
438   BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
439 }
440
441 const MipsInstrInfo *llvm::createMips16InstrInfo(MipsTargetMachine &TM) {
442   return new Mips16InstrInfo(TM);
443 }