Add argument TargetRegisterInfo to loadRegFromStackSlot and storeRegToStackSlot.
[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 "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "MipsGenInstrInfo.inc"
22
23 using namespace llvm;
24
25 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
26   : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
27     TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
28
29 static bool isZeroImm(const MachineOperand &op) {
30   return op.isImm() && op.getImm() == 0;
31 }
32
33 /// Return true if the instruction is a register to register move and
34 /// leave the source and dest operands in the passed parameters.
35 bool MipsInstrInfo::
36 isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg,
37             unsigned &SrcSubIdx, unsigned &DstSubIdx) const 
38 {
39   SrcSubIdx = DstSubIdx = 0; // No sub-registers.
40
41   // addu $dst, $src, $zero || addu $dst, $zero, $src
42   // or   $dst, $src, $zero || or   $dst, $zero, $src
43   if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
44     if (MI.getOperand(1).getReg() == Mips::ZERO) {
45       DstReg = MI.getOperand(0).getReg();
46       SrcReg = MI.getOperand(2).getReg();
47       return true;
48     } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
49       DstReg = MI.getOperand(0).getReg();
50       SrcReg = MI.getOperand(1).getReg();
51       return true;
52     }
53   }
54
55   // mov $fpDst, $fpSrc
56   // mfc $gpDst, $fpSrc
57   // mtc $fpDst, $gpSrc
58   if (MI.getOpcode() == Mips::FMOV_S32 || 
59       MI.getOpcode() == Mips::FMOV_D32 || 
60       MI.getOpcode() == Mips::MFC1 || 
61       MI.getOpcode() == Mips::MTC1 ||
62       MI.getOpcode() == Mips::MOVCCRToCCR) {
63     DstReg = MI.getOperand(0).getReg();
64     SrcReg = MI.getOperand(1).getReg();
65     return true;
66   }
67
68   // addiu $dst, $src, 0
69   if (MI.getOpcode() == Mips::ADDiu) {
70     if ((MI.getOperand(1).isReg()) && (isZeroImm(MI.getOperand(2)))) {
71       DstReg = MI.getOperand(0).getReg();
72       SrcReg = MI.getOperand(1).getReg();
73       return true;
74     }
75   }
76
77   return false;
78 }
79
80 /// isLoadFromStackSlot - If the specified machine instruction is a direct
81 /// load from a stack slot, return the virtual or physical register number of
82 /// the destination along with the FrameIndex of the loaded stack slot.  If
83 /// not, return 0.  This predicate must return 0 if the instruction has
84 /// any side effects other than loading from the stack slot.
85 unsigned MipsInstrInfo::
86 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const 
87 {
88   if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
89       (MI->getOpcode() == Mips::LDC1)) {
90     if ((MI->getOperand(2).isFI()) && // is a stack slot
91         (MI->getOperand(1).isImm()) &&  // the imm is zero
92         (isZeroImm(MI->getOperand(1)))) {
93       FrameIndex = MI->getOperand(2).getIndex();
94       return MI->getOperand(0).getReg();
95     }
96   }
97
98   return 0;
99 }
100
101 /// isStoreToStackSlot - If the specified machine instruction is a direct
102 /// store to a stack slot, return the virtual or physical register number of
103 /// the source reg along with the FrameIndex of the loaded stack slot.  If
104 /// not, return 0.  This predicate must return 0 if the instruction has
105 /// any side effects other than storing to the stack slot.
106 unsigned MipsInstrInfo::
107 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const 
108 {
109   if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
110       (MI->getOpcode() == Mips::SDC1)) {
111     if ((MI->getOperand(2).isFI()) && // is a stack slot
112         (MI->getOperand(1).isImm()) &&  // the imm is zero
113         (isZeroImm(MI->getOperand(1)))) {
114       FrameIndex = MI->getOperand(2).getIndex();
115       return MI->getOperand(0).getReg();
116     }
117   }
118   return 0;
119 }
120
121 /// insertNoop - If data hazard condition is found insert the target nop
122 /// instruction.
123 void MipsInstrInfo::
124 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const 
125 {
126   DebugLoc DL;
127   BuildMI(MBB, MI, DL, get(Mips::NOP));
128 }
129
130 bool MipsInstrInfo::
131 copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
132              unsigned DestReg, unsigned SrcReg,
133              const TargetRegisterClass *DestRC,
134              const TargetRegisterClass *SrcRC) const {
135   DebugLoc DL;
136   
137   if (I != MBB.end()) DL = I->getDebugLoc();
138
139   if (DestRC != SrcRC) {
140
141     // Copy to/from FCR31 condition register
142     if ((DestRC == Mips::CPURegsRegisterClass) && 
143         (SrcRC == Mips::CCRRegisterClass))
144       BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg).addReg(SrcReg);
145     else if ((DestRC == Mips::CCRRegisterClass) && 
146         (SrcRC == Mips::CPURegsRegisterClass))
147       BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg).addReg(SrcReg);
148
149     // Moves between coprocessors and cpu
150     else if ((DestRC == Mips::CPURegsRegisterClass) && 
151         (SrcRC == Mips::FGR32RegisterClass))
152       BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg).addReg(SrcReg);
153     else if ((DestRC == Mips::FGR32RegisterClass) &&
154              (SrcRC == Mips::CPURegsRegisterClass))
155       BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg).addReg(SrcReg);
156
157     // Move from/to Hi/Lo registers
158     else if ((DestRC == Mips::HILORegisterClass) &&
159              (SrcRC == Mips::CPURegsRegisterClass)) {
160       unsigned Opc = (DestReg == Mips::HI) ? Mips::MTHI : Mips::MTLO;
161       BuildMI(MBB, I, DL, get(Opc), DestReg);
162     } else if ((SrcRC == Mips::HILORegisterClass) &&
163                (DestRC == Mips::CPURegsRegisterClass)) {
164       unsigned Opc = (SrcReg == Mips::HI) ? Mips::MFHI : Mips::MFLO;
165       BuildMI(MBB, I, DL, get(Opc), DestReg);
166     } else 
167       // Can't copy this register
168       return false; 
169
170     return true;
171   }
172
173   if (DestRC == Mips::CPURegsRegisterClass)
174     BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
175       .addReg(SrcReg);
176   else if (DestRC == Mips::FGR32RegisterClass) 
177     BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg).addReg(SrcReg);
178   else if (DestRC == Mips::AFGR64RegisterClass)
179     BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
180   else if (DestRC == Mips::CCRRegisterClass)
181     BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg).addReg(SrcReg);
182   else
183     // Can't copy this register
184     return false;
185   
186   return true;
187 }
188
189 void MipsInstrInfo::
190 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
191                     unsigned SrcReg, bool isKill, int FI, 
192                     const TargetRegisterClass *RC,
193                     const TargetRegisterInfo *TRI) const {
194   DebugLoc DL;
195   if (I != MBB.end()) DL = I->getDebugLoc();
196
197   if (RC == Mips::CPURegsRegisterClass) 
198     BuildMI(MBB, I, DL, get(Mips::SW)).addReg(SrcReg, getKillRegState(isKill))
199           .addImm(0).addFrameIndex(FI);
200   else if (RC == Mips::FGR32RegisterClass)
201     BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
202           .addImm(0).addFrameIndex(FI);
203   else if (RC == Mips::AFGR64RegisterClass) {
204     if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
205       BuildMI(MBB, I, DL, get(Mips::SDC1))
206         .addReg(SrcReg, getKillRegState(isKill))
207         .addImm(0).addFrameIndex(FI);
208     } else {
209       const TargetRegisterInfo *TRI = 
210         MBB.getParent()->getTarget().getRegisterInfo();
211       const unsigned *SubSet = TRI->getSubRegisters(SrcReg);
212       BuildMI(MBB, I, DL, get(Mips::SWC1))
213         .addReg(SubSet[0], getKillRegState(isKill))
214         .addImm(0).addFrameIndex(FI);
215       BuildMI(MBB, I, DL, get(Mips::SWC1))
216         .addReg(SubSet[1], getKillRegState(isKill))
217         .addImm(4).addFrameIndex(FI);
218     }
219   } else
220     llvm_unreachable("Register class not handled!");
221 }
222
223 void MipsInstrInfo::
224 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
225                      unsigned DestReg, int FI,
226                      const TargetRegisterClass *RC,
227                      const TargetRegisterInfo *TRI) const 
228 {
229   DebugLoc DL;
230   if (I != MBB.end()) DL = I->getDebugLoc();
231
232   if (RC == Mips::CPURegsRegisterClass) 
233     BuildMI(MBB, I, DL, get(Mips::LW), DestReg).addImm(0).addFrameIndex(FI);
234   else if (RC == Mips::FGR32RegisterClass)
235     BuildMI(MBB, I, DL, get(Mips::LWC1), DestReg).addImm(0).addFrameIndex(FI);
236   else if (RC == Mips::AFGR64RegisterClass) {
237     if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
238       BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addImm(0).addFrameIndex(FI);
239     } else {
240       const TargetRegisterInfo *TRI = 
241         MBB.getParent()->getTarget().getRegisterInfo();
242       const unsigned *SubSet = TRI->getSubRegisters(DestReg);
243       BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[0])
244         .addImm(0).addFrameIndex(FI);
245       BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[1])
246         .addImm(4).addFrameIndex(FI);
247     }
248   } else
249     llvm_unreachable("Register class not handled!");
250 }
251
252 MachineInstr *MipsInstrInfo::
253 foldMemoryOperandImpl(MachineFunction &MF,
254                       MachineInstr* MI,
255                       const SmallVectorImpl<unsigned> &Ops, int FI) const 
256 {
257   if (Ops.size() != 1) return NULL;
258
259   MachineInstr *NewMI = NULL;
260
261   switch (MI->getOpcode()) {
262   case Mips::ADDu:
263     if ((MI->getOperand(0).isReg()) &&
264         (MI->getOperand(1).isReg()) &&
265         (MI->getOperand(1).getReg() == Mips::ZERO) &&
266         (MI->getOperand(2).isReg())) {
267       if (Ops[0] == 0) {    // COPY -> STORE
268         unsigned SrcReg = MI->getOperand(2).getReg();
269         bool isKill = MI->getOperand(2).isKill();
270         bool isUndef = MI->getOperand(2).isUndef();
271         NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW))
272           .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
273           .addImm(0).addFrameIndex(FI);
274       } else {              // COPY -> LOAD
275         unsigned DstReg = MI->getOperand(0).getReg();
276         bool isDead = MI->getOperand(0).isDead();
277         bool isUndef = MI->getOperand(0).isUndef();
278         NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW))
279           .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
280                   getUndefRegState(isUndef))
281           .addImm(0).addFrameIndex(FI);
282       }
283     }
284     break;
285   case Mips::FMOV_S32:
286   case Mips::FMOV_D32:
287     if ((MI->getOperand(0).isReg()) &&
288         (MI->getOperand(1).isReg())) {
289       const TargetRegisterClass 
290         *RC = RI.getRegClass(MI->getOperand(0).getReg());
291       unsigned StoreOpc, LoadOpc;
292       bool IsMips1 = TM.getSubtarget<MipsSubtarget>().isMips1();
293
294       if (RC == Mips::FGR32RegisterClass) {
295         LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
296       } else {
297         assert(RC == Mips::AFGR64RegisterClass);
298         // Mips1 doesn't have ldc/sdc instructions.
299         if (IsMips1) break;
300         LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
301       }
302
303       if (Ops[0] == 0) {    // COPY -> STORE
304         unsigned SrcReg = MI->getOperand(1).getReg();
305         bool isKill = MI->getOperand(1).isKill();
306         bool isUndef = MI->getOperand(2).isUndef();
307         NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc))
308           .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
309           .addImm(0).addFrameIndex(FI) ;
310       } else {              // COPY -> LOAD
311         unsigned DstReg = MI->getOperand(0).getReg();
312         bool isDead = MI->getOperand(0).isDead();
313         bool isUndef = MI->getOperand(0).isUndef();
314         NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc))
315           .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
316                   getUndefRegState(isUndef))
317           .addImm(0).addFrameIndex(FI);
318       }
319     }
320     break;
321   }
322
323   return NewMI;
324 }
325
326 //===----------------------------------------------------------------------===//
327 // Branch Analysis
328 //===----------------------------------------------------------------------===//
329
330 /// GetCondFromBranchOpc - Return the Mips CC that matches 
331 /// the correspondent Branch instruction opcode.
332 static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc) 
333 {
334   switch (BrOpc) {
335   default: return Mips::COND_INVALID;
336   case Mips::BEQ  : return Mips::COND_E;
337   case Mips::BNE  : return Mips::COND_NE;
338   case Mips::BGTZ : return Mips::COND_GZ;
339   case Mips::BGEZ : return Mips::COND_GEZ;
340   case Mips::BLTZ : return Mips::COND_LZ;
341   case Mips::BLEZ : return Mips::COND_LEZ;
342
343   // We dont do fp branch analysis yet!  
344   case Mips::BC1T : 
345   case Mips::BC1F : return Mips::COND_INVALID;
346   }
347 }
348
349 /// GetCondBranchFromCond - Return the Branch instruction
350 /// opcode that matches the cc.
351 unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC) 
352 {
353   switch (CC) {
354   default: llvm_unreachable("Illegal condition code!");
355   case Mips::COND_E   : return Mips::BEQ;
356   case Mips::COND_NE  : return Mips::BNE;
357   case Mips::COND_GZ  : return Mips::BGTZ;
358   case Mips::COND_GEZ : return Mips::BGEZ;
359   case Mips::COND_LZ  : return Mips::BLTZ;
360   case Mips::COND_LEZ : return Mips::BLEZ;
361
362   case Mips::FCOND_F:
363   case Mips::FCOND_UN:
364   case Mips::FCOND_EQ:
365   case Mips::FCOND_UEQ:
366   case Mips::FCOND_OLT:
367   case Mips::FCOND_ULT:
368   case Mips::FCOND_OLE:
369   case Mips::FCOND_ULE:
370   case Mips::FCOND_SF:
371   case Mips::FCOND_NGLE:
372   case Mips::FCOND_SEQ:
373   case Mips::FCOND_NGL:
374   case Mips::FCOND_LT:
375   case Mips::FCOND_NGE:
376   case Mips::FCOND_LE:
377   case Mips::FCOND_NGT: return Mips::BC1T;
378
379   case Mips::FCOND_T:
380   case Mips::FCOND_OR:
381   case Mips::FCOND_NEQ:
382   case Mips::FCOND_OGL:
383   case Mips::FCOND_UGE:
384   case Mips::FCOND_OGE:
385   case Mips::FCOND_UGT:
386   case Mips::FCOND_OGT:
387   case Mips::FCOND_ST:
388   case Mips::FCOND_GLE:
389   case Mips::FCOND_SNE:
390   case Mips::FCOND_GL:
391   case Mips::FCOND_NLT:
392   case Mips::FCOND_GE:
393   case Mips::FCOND_NLE:
394   case Mips::FCOND_GT: return Mips::BC1F;
395   }
396 }
397
398 /// GetOppositeBranchCondition - Return the inverse of the specified 
399 /// condition, e.g. turning COND_E to COND_NE.
400 Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC) 
401 {
402   switch (CC) {
403   default: llvm_unreachable("Illegal condition code!");
404   case Mips::COND_E   : return Mips::COND_NE;
405   case Mips::COND_NE  : return Mips::COND_E;
406   case Mips::COND_GZ  : return Mips::COND_LEZ;
407   case Mips::COND_GEZ : return Mips::COND_LZ;
408   case Mips::COND_LZ  : return Mips::COND_GEZ;
409   case Mips::COND_LEZ : return Mips::COND_GZ;
410   case Mips::FCOND_F  : return Mips::FCOND_T;
411   case Mips::FCOND_UN : return Mips::FCOND_OR;
412   case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
413   case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
414   case Mips::FCOND_OLT: return Mips::FCOND_UGE;
415   case Mips::FCOND_ULT: return Mips::FCOND_OGE;
416   case Mips::FCOND_OLE: return Mips::FCOND_UGT;
417   case Mips::FCOND_ULE: return Mips::FCOND_OGT;
418   case Mips::FCOND_SF:  return Mips::FCOND_ST;
419   case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
420   case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
421   case Mips::FCOND_NGL: return Mips::FCOND_GL;
422   case Mips::FCOND_LT:  return Mips::FCOND_NLT;
423   case Mips::FCOND_NGE: return Mips::FCOND_GE;
424   case Mips::FCOND_LE:  return Mips::FCOND_NLE;
425   case Mips::FCOND_NGT: return Mips::FCOND_GT;
426   }
427 }
428
429 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, 
430                                   MachineBasicBlock *&TBB,
431                                   MachineBasicBlock *&FBB,
432                                   SmallVectorImpl<MachineOperand> &Cond,
433                                   bool AllowModify) const 
434 {
435   // If the block has no terminators, it just falls into the block after it.
436   MachineBasicBlock::iterator I = MBB.end();
437   if (I == MBB.begin())
438     return false;
439   --I;
440   while (I->isDebugValue()) {
441     if (I == MBB.begin())
442       return false;
443     --I;
444   }
445   if (!isUnpredicatedTerminator(I))
446     return false;
447   
448   // Get the last instruction in the block.
449   MachineInstr *LastInst = I;
450   
451   // If there is only one terminator instruction, process it.
452   unsigned LastOpc = LastInst->getOpcode();
453   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
454     if (!LastInst->getDesc().isBranch())
455       return true;
456
457     // Unconditional branch
458     if (LastOpc == Mips::J) {
459       TBB = LastInst->getOperand(0).getMBB();
460       return false;
461     }
462
463     Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
464     if (BranchCode == Mips::COND_INVALID)
465       return true;  // Can't handle indirect branch.
466
467     // Conditional branch
468     // Block ends with fall-through condbranch.
469     if (LastOpc != Mips::COND_INVALID) {
470       int LastNumOp = LastInst->getNumOperands();
471
472       TBB = LastInst->getOperand(LastNumOp-1).getMBB();
473       Cond.push_back(MachineOperand::CreateImm(BranchCode));
474
475       for (int i=0; i<LastNumOp-1; i++) {
476         Cond.push_back(LastInst->getOperand(i));
477       }
478
479       return false;
480     }
481   }
482   
483   // Get the instruction before it if it is a terminator.
484   MachineInstr *SecondLastInst = I;
485   
486   // If there are three terminators, we don't know what sort of block this is.
487   if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
488     return true;
489
490   // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
491   unsigned SecondLastOpc    = SecondLastInst->getOpcode();
492   Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
493
494   if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
495     int SecondNumOp = SecondLastInst->getNumOperands();
496
497     TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
498     Cond.push_back(MachineOperand::CreateImm(BranchCode));
499
500     for (int i=0; i<SecondNumOp-1; i++) {
501       Cond.push_back(SecondLastInst->getOperand(i));
502     }
503
504     FBB = LastInst->getOperand(0).getMBB();
505     return false;
506   }
507   
508   // If the block ends with two unconditional branches, handle it. The last 
509   // one is not executed, so remove it.
510   if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
511     TBB = SecondLastInst->getOperand(0).getMBB();
512     I = LastInst;
513     if (AllowModify)
514       I->eraseFromParent();
515     return false;
516   }
517
518   // Otherwise, can't handle this.
519   return true;
520 }
521
522 unsigned MipsInstrInfo::
523 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, 
524              MachineBasicBlock *FBB,
525              const SmallVectorImpl<MachineOperand> &Cond) const {
526   // FIXME this should probably have a DebugLoc argument
527   DebugLoc dl;
528   // Shouldn't be a fall through.
529   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
530   assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
531          "Mips branch conditions can have two|three components!");
532
533   if (FBB == 0) { // One way branch.
534     if (Cond.empty()) {
535       // Unconditional branch?
536       BuildMI(&MBB, dl, get(Mips::J)).addMBB(TBB);
537     } else {
538       // Conditional branch.
539       unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
540       const TargetInstrDesc &TID = get(Opc);
541
542       if (TID.getNumOperands() == 3)
543         BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
544                           .addReg(Cond[2].getReg())
545                           .addMBB(TBB);
546       else
547         BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
548                           .addMBB(TBB);
549
550     }                             
551     return 1;
552   }
553   
554   // Two-way Conditional branch.
555   unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
556   const TargetInstrDesc &TID = get(Opc);
557
558   if (TID.getNumOperands() == 3)
559     BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
560                       .addMBB(TBB);
561   else
562     BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addMBB(TBB);
563
564   BuildMI(&MBB, dl, get(Mips::J)).addMBB(FBB);
565   return 2;
566 }
567
568 unsigned MipsInstrInfo::
569 RemoveBranch(MachineBasicBlock &MBB) const 
570 {
571   MachineBasicBlock::iterator I = MBB.end();
572   if (I == MBB.begin()) return 0;
573   --I;
574   while (I->isDebugValue()) {
575     if (I == MBB.begin())
576       return 0;
577     --I;
578   }
579   if (I->getOpcode() != Mips::J && 
580       GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
581     return 0;
582   
583   // Remove the branch.
584   I->eraseFromParent();
585   
586   I = MBB.end();
587   
588   if (I == MBB.begin()) return 1;
589   --I;
590   if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
591     return 1;
592   
593   // Remove the branch.
594   I->eraseFromParent();
595   return 2;
596 }
597
598 /// ReverseBranchCondition - Return the inverse opcode of the 
599 /// specified Branch instruction.
600 bool MipsInstrInfo::
601 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const 
602 {
603   assert( (Cond.size() == 3 || Cond.size() == 2) && 
604           "Invalid Mips branch condition!");
605   Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
606   return false;
607 }
608
609 /// getGlobalBaseReg - Return a virtual register initialized with the
610 /// the global base register value. Output instructions required to
611 /// initialize the register in the function entry block, if necessary.
612 ///
613 unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
614   MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
615   unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
616   if (GlobalBaseReg != 0)
617     return GlobalBaseReg;
618
619   // Insert the set of GlobalBaseReg into the first MBB of the function
620   MachineBasicBlock &FirstMBB = MF->front();
621   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
622   MachineRegisterInfo &RegInfo = MF->getRegInfo();
623   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
624
625   GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
626   bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalBaseReg, Mips::GP,
627                               Mips::CPURegsRegisterClass,
628                               Mips::CPURegsRegisterClass);
629   assert(Ok && "Couldn't assign to global base register!");
630   Ok = Ok; // Silence warning when assertions are turned off.
631   RegInfo.addLiveIn(Mips::GP);
632
633   MipsFI->setGlobalBaseReg(GlobalBaseReg);
634   return GlobalBaseReg;
635 }