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