Simplify and update functions storeRegToStackSlot and loadRegFromStackSlot.
[oota-llvm.git] / lib / Target / Mips / MipsInstrInfo.cpp
1 //===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
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 Mips implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsInstrInfo.h"
15 #include "MipsTargetMachine.h"
16 #include "MipsMachineFunction.h"
17 #include "InstPrinter/MipsInstPrinter.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/ADT/STLExtras.h"
23
24 #define GET_INSTRINFO_CTOR
25 #include "MipsGenInstrInfo.inc"
26
27 using namespace llvm;
28
29 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
30   : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
31     TM(tm), IsN64(TM.getSubtarget<MipsSubtarget>().isABI_N64()),
32     RI(*TM.getSubtargetImpl(), *this) {}
33
34
35 const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const { 
36   return RI;
37 }
38
39 static bool isZeroImm(const MachineOperand &op) {
40   return op.isImm() && op.getImm() == 0;
41 }
42
43 /// isLoadFromStackSlot - If the specified machine instruction is a direct
44 /// load from a stack slot, return the virtual or physical register number of
45 /// the destination along with the FrameIndex of the loaded stack slot.  If
46 /// not, return 0.  This predicate must return 0 if the instruction has
47 /// any side effects other than loading from the stack slot.
48 unsigned MipsInstrInfo::
49 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
50 {
51   if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
52       (MI->getOpcode() == Mips::LDC1)) {
53     if ((MI->getOperand(1).isFI()) && // is a stack slot
54         (MI->getOperand(2).isImm()) &&  // the imm is zero
55         (isZeroImm(MI->getOperand(2)))) {
56       FrameIndex = MI->getOperand(1).getIndex();
57       return MI->getOperand(0).getReg();
58     }
59   }
60
61   return 0;
62 }
63
64 /// isStoreToStackSlot - If the specified machine instruction is a direct
65 /// store to a stack slot, return the virtual or physical register number of
66 /// the source reg along with the FrameIndex of the loaded stack slot.  If
67 /// not, return 0.  This predicate must return 0 if the instruction has
68 /// any side effects other than storing to the stack slot.
69 unsigned MipsInstrInfo::
70 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
71 {
72   if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
73       (MI->getOpcode() == Mips::SDC1)) {
74     if ((MI->getOperand(1).isFI()) && // is a stack slot
75         (MI->getOperand(2).isImm()) &&  // the imm is zero
76         (isZeroImm(MI->getOperand(2)))) {
77       FrameIndex = MI->getOperand(1).getIndex();
78       return MI->getOperand(0).getReg();
79     }
80   }
81   return 0;
82 }
83
84 /// insertNoop - If data hazard condition is found insert the target nop
85 /// instruction.
86 void MipsInstrInfo::
87 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
88 {
89   DebugLoc DL;
90   BuildMI(MBB, MI, DL, get(Mips::NOP));
91 }
92
93 void MipsInstrInfo::
94 copyPhysReg(MachineBasicBlock &MBB,
95             MachineBasicBlock::iterator I, DebugLoc DL,
96             unsigned DestReg, unsigned SrcReg,
97             bool KillSrc) const {
98   unsigned Opc = 0, ZeroReg = 0;
99
100   if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg.
101     if (Mips::CPURegsRegClass.contains(SrcReg))
102       Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
103     else if (Mips::CCRRegClass.contains(SrcReg))
104       Opc = Mips::CFC1;
105     else if (Mips::FGR32RegClass.contains(SrcReg))
106       Opc = Mips::MFC1;
107     else if (SrcReg == Mips::HI)
108       Opc = Mips::MFHI, SrcReg = 0;
109     else if (SrcReg == Mips::LO)
110       Opc = Mips::MFLO, SrcReg = 0;
111   }
112   else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg.
113     if (Mips::CCRRegClass.contains(DestReg))
114       Opc = Mips::CTC1;
115     else if (Mips::FGR32RegClass.contains(DestReg))
116       Opc = Mips::MTC1;
117     else if (DestReg == Mips::HI)
118       Opc = Mips::MTHI, DestReg = 0;
119     else if (DestReg == Mips::LO)
120       Opc = Mips::MTLO, DestReg = 0;
121   }
122   else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
123     Opc = Mips::FMOV_S;
124   else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
125     Opc = Mips::FMOV_D32;
126   else if (Mips::CCRRegClass.contains(DestReg, SrcReg))
127     Opc = Mips::MOVCCRToCCR;
128   else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg.
129     if (Mips::CPU64RegsRegClass.contains(SrcReg))
130       Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
131     else if (SrcReg == Mips::HI64)
132       Opc = Mips::MFHI64, SrcReg = 0;
133     else if (SrcReg == Mips::LO64)
134       Opc = Mips::MFLO64, SrcReg = 0;
135   }
136   else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
137     if (DestReg == Mips::HI64)
138       Opc = Mips::MTHI64, DestReg = 0;
139     else if (DestReg == Mips::LO64)
140       Opc = Mips::MTLO64, DestReg = 0;
141   }
142
143   assert(Opc && "Cannot copy registers");
144
145   MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
146   
147   if (DestReg)
148     MIB.addReg(DestReg, RegState::Define);
149
150   if (ZeroReg)
151     MIB.addReg(ZeroReg);
152
153   if (SrcReg)
154     MIB.addReg(SrcReg, getKillRegState(KillSrc));
155 }
156
157 void MipsInstrInfo::
158 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
159                     unsigned SrcReg, bool isKill, int FI,
160                     const TargetRegisterClass *RC,
161                     const TargetRegisterInfo *TRI) const {
162   DebugLoc DL;
163   if (I != MBB.end()) DL = I->getDebugLoc();
164   unsigned Opc = 0;
165
166   if (RC == Mips::CPURegsRegisterClass)
167     Opc = IsN64 ? Mips::SW_P8 : Mips::SW;
168   else if (RC == Mips::CPU64RegsRegisterClass)
169     Opc = IsN64 ? Mips::SD_P8 : Mips::SD;
170   else if (RC == Mips::FGR32RegisterClass)
171     Opc = Mips::SWC1;
172   else if (RC == Mips::AFGR64RegisterClass)
173     Opc = Mips::SDC1;
174
175   assert(Opc && "Register class not handled!");
176   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
177     .addFrameIndex(FI).addImm(0);
178 }
179
180 void MipsInstrInfo::
181 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
182                      unsigned DestReg, int FI,
183                      const TargetRegisterClass *RC,
184                      const TargetRegisterInfo *TRI) const
185 {
186   DebugLoc DL;
187   if (I != MBB.end()) DL = I->getDebugLoc();
188   unsigned Opc = 0;
189
190   if (RC == Mips::CPURegsRegisterClass)
191     Opc = IsN64 ? Mips::LW_P8 : Mips::LW;
192   else if (RC == Mips::CPU64RegsRegisterClass)
193     Opc = IsN64 ? Mips::LD_P8 : Mips::LD;
194   else if (RC == Mips::FGR32RegisterClass)
195     Opc = Mips::LWC1;
196   else if (RC == Mips::AFGR64RegisterClass)
197     Opc = Mips::LDC1;
198
199   assert(Opc && "Register class not handled!");
200   BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0);
201 }
202
203 MachineInstr*
204 MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
205                                         uint64_t Offset, const MDNode *MDPtr,
206                                         DebugLoc DL) const {
207   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
208     .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
209   return &*MIB;
210 }
211
212 //===----------------------------------------------------------------------===//
213 // Branch Analysis
214 //===----------------------------------------------------------------------===//
215
216 static unsigned GetAnalyzableBrOpc(unsigned Opc) {
217   return (Opc == Mips::BEQ  || Opc == Mips::BNE  || Opc == Mips::BGTZ ||
218           Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
219           Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
220 }
221
222 /// GetOppositeBranchOpc - Return the inverse of the specified
223 /// opcode, e.g. turning BEQ to BNE.
224 unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
225 {
226   switch (Opc) {
227   default: llvm_unreachable("Illegal opcode!");
228   case Mips::BEQ  : return Mips::BNE;
229   case Mips::BNE  : return Mips::BEQ;
230   case Mips::BGTZ : return Mips::BLEZ;
231   case Mips::BGEZ : return Mips::BLTZ;
232   case Mips::BLTZ : return Mips::BGEZ;
233   case Mips::BLEZ : return Mips::BGTZ;
234   case Mips::BC1T : return Mips::BC1F;
235   case Mips::BC1F : return Mips::BC1T;
236   }
237 }
238
239 static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
240                           MachineBasicBlock *&BB,
241                           SmallVectorImpl<MachineOperand>& Cond) {
242   assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
243   int NumOp = Inst->getNumExplicitOperands();
244   
245   // for both int and fp branches, the last explicit operand is the
246   // MBB.
247   BB = Inst->getOperand(NumOp-1).getMBB();
248   Cond.push_back(MachineOperand::CreateImm(Opc));
249
250   for (int i=0; i<NumOp-1; i++)
251     Cond.push_back(Inst->getOperand(i));
252 }
253
254 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
255                                   MachineBasicBlock *&TBB,
256                                   MachineBasicBlock *&FBB,
257                                   SmallVectorImpl<MachineOperand> &Cond,
258                                   bool AllowModify) const
259 {
260   MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
261
262   // Skip all the debug instructions.
263   while (I != REnd && I->isDebugValue())
264     ++I;
265
266   if (I == REnd || !isUnpredicatedTerminator(&*I)) {
267     // If this block ends with no branches (it just falls through to its succ)
268     // just return false, leaving TBB/FBB null.
269     TBB = FBB = NULL;
270     return false;
271   }
272
273   MachineInstr *LastInst = &*I;
274   unsigned LastOpc = LastInst->getOpcode();
275
276   // Not an analyzable branch (must be an indirect jump).
277   if (!GetAnalyzableBrOpc(LastOpc))
278     return true;
279
280   // Get the second to last instruction in the block.
281   unsigned SecondLastOpc = 0;
282   MachineInstr *SecondLastInst = NULL;
283
284   if (++I != REnd) {
285     SecondLastInst = &*I;
286     SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
287
288     // Not an analyzable branch (must be an indirect jump).
289     if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
290       return true;
291   }
292
293   // If there is only one terminator instruction, process it.
294   if (!SecondLastOpc) {
295     // Unconditional branch
296     if (LastOpc == Mips::J) {
297       TBB = LastInst->getOperand(0).getMBB();
298       return false;
299     }
300
301     // Conditional branch
302     AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
303     return false;
304   }
305
306   // If we reached here, there are two branches.
307   // If there are three terminators, we don't know what sort of block this is.
308   if (++I != REnd && isUnpredicatedTerminator(&*I))
309     return true;
310
311   // If second to last instruction is an unconditional branch,
312   // analyze it and remove the last instruction.
313   if (SecondLastOpc == Mips::J) {
314     // Return if the last instruction cannot be removed.
315     if (!AllowModify)
316       return true;
317
318     TBB = SecondLastInst->getOperand(0).getMBB();
319     LastInst->eraseFromParent();
320     return false;
321   }
322
323   // Conditional branch followed by an unconditional branch.
324   // The last one must be unconditional.
325   if (LastOpc != Mips::J)
326     return true;
327
328   AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
329   FBB = LastInst->getOperand(0).getMBB();
330
331   return false;
332
333   
334 void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
335                                 MachineBasicBlock *TBB, DebugLoc DL,
336                                 const SmallVectorImpl<MachineOperand>& Cond)
337   const {
338   unsigned Opc = Cond[0].getImm();
339   const MCInstrDesc &MCID = get(Opc);
340   MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
341
342   for (unsigned i = 1; i < Cond.size(); ++i)
343     MIB.addReg(Cond[i].getReg());
344
345   MIB.addMBB(TBB);
346 }
347
348 unsigned MipsInstrInfo::
349 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
350              MachineBasicBlock *FBB,
351              const SmallVectorImpl<MachineOperand> &Cond,
352              DebugLoc DL) const {
353   // Shouldn't be a fall through.
354   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
355
356   // # of condition operands:
357   //  Unconditional branches: 0
358   //  Floating point branches: 1 (opc)
359   //  Int BranchZero: 2 (opc, reg)
360   //  Int Branch: 3 (opc, reg0, reg1)
361   assert((Cond.size() <= 3) &&
362          "# of Mips branch conditions must be <= 3!");
363
364   // Two-way Conditional branch.
365   if (FBB) {
366     BuildCondBr(MBB, TBB, DL, Cond);
367     BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
368     return 2;
369   }
370
371   // One way branch.
372   // Unconditional branch.
373   if (Cond.empty())
374     BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
375   else // Conditional branch.
376     BuildCondBr(MBB, TBB, DL, Cond);
377   return 1;
378 }
379
380 unsigned MipsInstrInfo::
381 RemoveBranch(MachineBasicBlock &MBB) const
382 {
383   MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
384   MachineBasicBlock::reverse_iterator FirstBr;
385   unsigned removed;
386
387   // Skip all the debug instructions.
388   while (I != REnd && I->isDebugValue())
389     ++I;
390
391   FirstBr = I;
392
393   // Up to 2 branches are removed.
394   // Note that indirect branches are not removed.
395   for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
396     if (!GetAnalyzableBrOpc(I->getOpcode()))
397       break;
398
399   MBB.erase(I.base(), FirstBr.base());
400
401   return removed;
402 }
403
404 /// ReverseBranchCondition - Return the inverse opcode of the
405 /// specified Branch instruction.
406 bool MipsInstrInfo::
407 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
408 {
409   assert( (Cond.size() && Cond.size() <= 3) &&
410           "Invalid Mips branch condition!");
411   Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
412   return false;
413 }
414
415 /// getGlobalBaseReg - Return a virtual register initialized with the
416 /// the global base register value. Output instructions required to
417 /// initialize the register in the function entry block, if necessary.
418 ///
419 unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
420   MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
421   unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
422   if (GlobalBaseReg != 0)
423     return GlobalBaseReg;
424
425   // Insert the set of GlobalBaseReg into the first MBB of the function
426   MachineBasicBlock &FirstMBB = MF->front();
427   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
428   MachineRegisterInfo &RegInfo = MF->getRegInfo();
429   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
430
431   GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
432   BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
433           GlobalBaseReg).addReg(Mips::GP);
434   RegInfo.addLiveIn(Mips::GP);
435
436   MipsFI->setGlobalBaseReg(GlobalBaseReg);
437   return GlobalBaseReg;
438 }