Expand macro/pseudo instructions BtnezT8SltX16 and BtnezT8SltuX16.
[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::BtnezT8SltX16:
154     ExpandFEXT_T8I816_ins(MBB, MI, Mips::BtnezX16, Mips::SltRxRy16);
155     break;
156   case Mips::BtnezT8SltuX16:
157     // TBD: figure out a way to get this or remove the instruction
158     // altogether.
159     ExpandFEXT_T8I816_ins(MBB, MI, Mips::BtnezX16, Mips::SltuRxRy16);
160     break;
161   case Mips::RetRA16:
162     ExpandRetRA16(MBB, MI, Mips::JrcRa16);
163     break;
164   }
165
166   MBB.erase(MI);
167   return true;
168 }
169
170 /// GetOppositeBranchOpc - Return the inverse of the specified
171 /// opcode, e.g. turning BEQ to BNE.
172 unsigned Mips16InstrInfo::GetOppositeBranchOpc(unsigned Opc) const {
173   switch (Opc) {
174   default:  llvm_unreachable("Illegal opcode!");
175   case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
176   case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
177   case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
178   case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
179   case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
180   case Mips::BtnezX16: return Mips::BteqzX16;
181   case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
182   case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
183   case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
184   case Mips::BteqzX16: return Mips::BtnezX16;
185   case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
186   case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
187   case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
188   case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
189   case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
190   case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
191   }
192   assert(false && "Implement this function.");
193   return 0;
194 }
195
196 // Adjust SP by FrameSize bytes. Save RA, S0, S1
197 void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
198                     MachineBasicBlock &MBB,
199                     MachineBasicBlock::iterator I) const {
200   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
201   if (!NeverUseSaveRestore) {
202     if (isUInt<11>(FrameSize))
203       BuildMI(MBB, I, DL, get(Mips::SaveRaF16)).addImm(FrameSize);
204     else {
205       int Base = 2040; // should create template function like isUInt that
206                        // returns largest possible n bit unsigned integer
207       int64_t Remainder = FrameSize - Base;
208       BuildMI(MBB, I, DL, get(Mips::SaveRaF16)). addImm(Base);
209       if (isInt<16>(-Remainder))
210         BuildAddiuSpImm(MBB, I, -Remainder);
211       else
212         adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
213     }
214
215   }
216   else {
217     //
218     // sw ra, -4[sp]
219     // sw s1, -8[sp]
220     // sw s0, -12[sp]
221
222     MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
223                                        Mips::RA);
224     MIB1.addReg(Mips::SP);
225     MIB1.addImm(-4);
226     MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
227                                        Mips::S1);
228     MIB2.addReg(Mips::SP);
229     MIB2.addImm(-8);
230     MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
231                                        Mips::S0);
232     MIB3.addReg(Mips::SP);
233     MIB3.addImm(-12);
234     adjustStackPtrBig(SP, -FrameSize, MBB, I, Mips::V0, Mips::V1);
235   }
236 }
237
238 // Adjust SP by FrameSize bytes. Restore RA, S0, S1
239 void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
240                                    MachineBasicBlock &MBB,
241                                    MachineBasicBlock::iterator I) const {
242   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
243   if (!NeverUseSaveRestore) {
244     if (isUInt<11>(FrameSize))
245       BuildMI(MBB, I, DL, get(Mips::RestoreRaF16)).addImm(FrameSize);
246     else {
247       int Base = 2040; // should create template function like isUInt that
248                        // returns largest possible n bit unsigned integer
249       int64_t Remainder = FrameSize - Base;
250       if (isInt<16>(Remainder))
251         BuildAddiuSpImm(MBB, I, Remainder);
252       else
253         adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
254       BuildMI(MBB, I, DL, get(Mips::RestoreRaF16)). addImm(Base);
255     }
256   }
257   else {
258     adjustStackPtrBig(SP, FrameSize, MBB, I, Mips::A0, Mips::A1);
259     // lw ra, -4[sp]
260     // lw s1, -8[sp]
261     // lw s0, -12[sp]
262     MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
263                                        Mips::A0);
264     MIB1.addReg(Mips::SP);
265     MIB1.addImm(-4);
266     MachineInstrBuilder MIB0 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
267                                        Mips::RA);
268      MIB0.addReg(Mips::A0);
269     MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
270                                        Mips::S1);
271     MIB2.addReg(Mips::SP);
272     MIB2.addImm(-8);
273     MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
274                                        Mips::S0);
275     MIB3.addReg(Mips::SP);
276     MIB3.addImm(-12);
277   }
278
279 }
280
281 // Adjust SP by Amount bytes where bytes can be up to 32bit number.
282 // This can only be called at times that we know that there is at least one free
283 // register.
284 // This is clearly safe at prologue and epilogue.
285 //
286 void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
287                                         MachineBasicBlock &MBB,
288                                         MachineBasicBlock::iterator I,
289                                         unsigned Reg1, unsigned Reg2) const {
290   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
291 //  MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
292 //  unsigned Reg1 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
293 //  unsigned Reg2 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
294   //
295   // li reg1, constant
296   // move reg2, sp
297   // add reg1, reg1, reg2
298   // move sp, reg1
299   //
300   //
301   MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
302   MIB1.addImm(Amount);
303   MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
304   MIB2.addReg(Mips::SP, RegState::Kill);
305   MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
306   MIB3.addReg(Reg1);
307   MIB3.addReg(Reg2, RegState::Kill);
308   MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
309                                                      Mips::SP);
310   MIB4.addReg(Reg1, RegState::Kill);
311 }
312
313 void Mips16InstrInfo::adjustStackPtrBigUnrestricted(unsigned SP, int64_t Amount,
314                     MachineBasicBlock &MBB,
315                     MachineBasicBlock::iterator I) const {
316    assert(false && "adjust stack pointer amount exceeded");
317 }
318
319 /// Adjust SP by Amount bytes.
320 void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
321                                      MachineBasicBlock &MBB,
322                                      MachineBasicBlock::iterator I) const {
323   if (isInt<16>(Amount))  // need to change to addiu sp, ....and isInt<16>
324     BuildAddiuSpImm(MBB, I, Amount);
325   else
326     adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
327 }
328
329 /// This function generates the sequence of instructions needed to get the
330 /// result of adding register REG and immediate IMM.
331 unsigned
332 Mips16InstrInfo::loadImmediate(unsigned FrameReg,
333                                int64_t Imm, MachineBasicBlock &MBB,
334                                MachineBasicBlock::iterator II, DebugLoc DL,
335                                unsigned &NewImm) const {
336   //
337   // given original instruction is:
338   // Instr rx, T[offset] where offset is too big.
339   //
340   // lo = offset & 0xFFFF
341   // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
342   //
343   // let T = temporary register
344   // li T, hi
345   // shl T, 16
346   // add T, Rx, T
347   //
348   RegScavenger rs;
349   int32_t lo = Imm & 0xFFFF;
350   int32_t hi = ((Imm >> 16) + (lo >> 15)) & 0xFFFF;
351   NewImm = lo;
352   unsigned Reg =0;
353   unsigned SpReg = 0;
354   rs.enterBasicBlock(&MBB);
355   rs.forward(II);
356   //
357   // we use T0 for the first register, if we need to save something away.
358   // we use T1 for the second register, if we need to save something away.
359   //
360   unsigned FirstRegSaved =0, SecondRegSaved=0;
361   unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
362
363   Reg = rs.FindUnusedReg(&Mips::CPU16RegsRegClass);
364   if (Reg == 0) {
365     FirstRegSaved = Reg = Mips::V0;
366     FirstRegSavedTo = Mips::T0;
367     copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
368   }
369   else
370     rs.setUsed(Reg);
371   BuildMI(MBB, II, DL, get(Mips::LiRxImmX16), Reg).addImm(hi);
372   BuildMI(MBB, II, DL, get(Mips::SllX16), Reg).addReg(Reg).
373     addImm(16);
374   if (FrameReg == Mips::SP) {
375     SpReg = rs.FindUnusedReg(&Mips::CPU16RegsRegClass);
376     if (SpReg == 0) {
377       if (Reg != Mips::V1) {
378         SecondRegSaved = SpReg = Mips::V1;
379         SecondRegSavedTo = Mips::T1;
380       }
381       else {
382         SecondRegSaved = SpReg = Mips::V0;
383         SecondRegSavedTo = Mips::T0;
384       }
385       copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
386     }
387     else
388       rs.setUsed(SpReg);
389
390     copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
391     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(SpReg)
392       .addReg(Reg);
393   }
394   else
395     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(FrameReg)
396       .addReg(Reg, RegState::Kill);
397   if (FirstRegSaved || SecondRegSaved) {
398     II = llvm::next(II);
399     if (FirstRegSaved)
400       copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
401     if (SecondRegSaved)
402       copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
403   }
404   return Reg;
405 }
406
407 unsigned Mips16InstrInfo::GetAnalyzableBrOpc(unsigned Opc) const {
408   return (Opc == Mips::BeqzRxImmX16   || Opc == Mips::BimmX16  ||
409           Opc == Mips::BnezRxImmX16   || Opc == Mips::BteqzX16 ||
410           Opc == Mips::BteqzT8CmpX16  || Opc == Mips::BteqzT8CmpiX16 ||
411           Opc == Mips::BteqzT8SltX16  || Opc == Mips::BteqzT8SltuX16  ||
412           Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
413           Opc == Mips::BtnezX16       || Opc == Mips::BtnezT8CmpX16 ||
414           Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
415           Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
416           Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
417 }
418
419 void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
420                                   MachineBasicBlock::iterator I,
421                                   unsigned Opc) const {
422   BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
423 }
424
425
426 void Mips16InstrInfo::ExpandFEXT_T8I816_ins(
427   MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
428   unsigned BtOpc, unsigned CmpOpc) const {
429   unsigned regX = I->getOperand(0).getReg();
430   unsigned regY = I->getOperand(1).getReg();
431   MachineBasicBlock *target = I->getOperand(2).getMBB();
432   BuildMI(MBB, I, I->getDebugLoc(), get(CmpOpc)).addReg(regX).addReg(regY);
433   BuildMI(MBB, I, I->getDebugLoc(), get(BtOpc)).addMBB(target);
434
435 }
436 const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
437   if (validSpImm8(Imm))
438     return get(Mips::AddiuSpImm16);
439   else
440     return get(Mips::AddiuSpImmX16);
441 }
442
443 void Mips16InstrInfo::BuildAddiuSpImm
444   (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
445   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
446   BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
447 }
448
449 const MipsInstrInfo *llvm::createMips16InstrInfo(MipsTargetMachine &TM) {
450   return new Mips16InstrInfo(TM);
451 }