Replace copyRegToReg with copyPhysReg for Mips.
[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 void MipsInstrInfo::
131 copyPhysReg(MachineBasicBlock &MBB,
132             MachineBasicBlock::iterator I, DebugLoc DL,
133             unsigned DestReg, unsigned SrcReg,
134             bool KillSrc) const {
135   bool DestCPU = Mips::CPURegsRegClass.contains(DestReg);
136   bool SrcCPU  = Mips::CPURegsRegClass.contains(SrcReg);
137
138   // CPU-CPU is the most common.
139   if (DestCPU && SrcCPU) {
140     BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
141       .addReg(SrcReg, getKillRegState(KillSrc));
142     return;
143   }
144
145   // Copy to CPU from other registers.
146   if (DestCPU) {
147     if (Mips::CCRRegClass.contains(SrcReg))
148       BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg)
149         .addReg(SrcReg, getKillRegState(KillSrc));
150     else if (Mips::FGR32RegClass.contains(SrcReg))
151       BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg)
152         .addReg(SrcReg, getKillRegState(KillSrc));
153     else if (SrcReg == Mips::HI)
154       BuildMI(MBB, I, DL, get(Mips::MFHI), DestReg);
155     else if (SrcReg == Mips::LO)
156       BuildMI(MBB, I, DL, get(Mips::MFLO), DestReg);
157     else
158       llvm_unreachable("Copy to CPU from invalid register");
159     return;
160   }
161
162   // Copy to other registers from CPU.
163   if (SrcCPU) {
164     if (Mips::CCRRegClass.contains(DestReg))
165       BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg)
166         .addReg(SrcReg, getKillRegState(KillSrc));
167     else if (Mips::FGR32RegClass.contains(DestReg))
168       BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg)
169         .addReg(SrcReg, getKillRegState(KillSrc));
170     else if (DestReg == Mips::HI)
171       BuildMI(MBB, I, DL, get(Mips::MTHI))
172         .addReg(SrcReg, getKillRegState(KillSrc));
173     else if (DestReg == Mips::LO)
174       BuildMI(MBB, I, DL, get(Mips::MTLO))
175         .addReg(SrcReg, getKillRegState(KillSrc));
176     else
177       llvm_unreachable("Copy from CPU to invalid register");
178     return;
179   }
180
181   if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) {
182     BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg)
183       .addReg(SrcReg, getKillRegState(KillSrc));
184     return;
185   }
186   
187   if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) {
188     BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg)
189       .addReg(SrcReg, getKillRegState(KillSrc));
190     return;
191   }
192
193   if (Mips::CCRRegClass.contains(DestReg, SrcReg)) {
194     BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg)
195       .addReg(SrcReg, getKillRegState(KillSrc));
196     return;
197   }
198   llvm_unreachable("Cannot copy registers");
199 }
200
201 void MipsInstrInfo::
202 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
203                     unsigned SrcReg, bool isKill, int FI, 
204                     const TargetRegisterClass *RC,
205                     const TargetRegisterInfo *TRI) const {
206   DebugLoc DL;
207   if (I != MBB.end()) DL = I->getDebugLoc();
208
209   if (RC == Mips::CPURegsRegisterClass) 
210     BuildMI(MBB, I, DL, get(Mips::SW)).addReg(SrcReg, getKillRegState(isKill))
211           .addImm(0).addFrameIndex(FI);
212   else if (RC == Mips::FGR32RegisterClass)
213     BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
214           .addImm(0).addFrameIndex(FI);
215   else if (RC == Mips::AFGR64RegisterClass) {
216     if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
217       BuildMI(MBB, I, DL, get(Mips::SDC1))
218         .addReg(SrcReg, getKillRegState(isKill))
219         .addImm(0).addFrameIndex(FI);
220     } else {
221       const TargetRegisterInfo *TRI = 
222         MBB.getParent()->getTarget().getRegisterInfo();
223       const unsigned *SubSet = TRI->getSubRegisters(SrcReg);
224       BuildMI(MBB, I, DL, get(Mips::SWC1))
225         .addReg(SubSet[0], getKillRegState(isKill))
226         .addImm(0).addFrameIndex(FI);
227       BuildMI(MBB, I, DL, get(Mips::SWC1))
228         .addReg(SubSet[1], getKillRegState(isKill))
229         .addImm(4).addFrameIndex(FI);
230     }
231   } else
232     llvm_unreachable("Register class not handled!");
233 }
234
235 void MipsInstrInfo::
236 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
237                      unsigned DestReg, int FI,
238                      const TargetRegisterClass *RC,
239                      const TargetRegisterInfo *TRI) const 
240 {
241   DebugLoc DL;
242   if (I != MBB.end()) DL = I->getDebugLoc();
243
244   if (RC == Mips::CPURegsRegisterClass) 
245     BuildMI(MBB, I, DL, get(Mips::LW), DestReg).addImm(0).addFrameIndex(FI);
246   else if (RC == Mips::FGR32RegisterClass)
247     BuildMI(MBB, I, DL, get(Mips::LWC1), DestReg).addImm(0).addFrameIndex(FI);
248   else if (RC == Mips::AFGR64RegisterClass) {
249     if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
250       BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addImm(0).addFrameIndex(FI);
251     } else {
252       const TargetRegisterInfo *TRI = 
253         MBB.getParent()->getTarget().getRegisterInfo();
254       const unsigned *SubSet = TRI->getSubRegisters(DestReg);
255       BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[0])
256         .addImm(0).addFrameIndex(FI);
257       BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[1])
258         .addImm(4).addFrameIndex(FI);
259     }
260   } else
261     llvm_unreachable("Register class not handled!");
262 }
263
264 MachineInstr *MipsInstrInfo::
265 foldMemoryOperandImpl(MachineFunction &MF,
266                       MachineInstr* MI,
267                       const SmallVectorImpl<unsigned> &Ops, int FI) const 
268 {
269   if (Ops.size() != 1) return NULL;
270
271   MachineInstr *NewMI = NULL;
272
273   switch (MI->getOpcode()) {
274   case Mips::ADDu:
275     if ((MI->getOperand(0).isReg()) &&
276         (MI->getOperand(1).isReg()) &&
277         (MI->getOperand(1).getReg() == Mips::ZERO) &&
278         (MI->getOperand(2).isReg())) {
279       if (Ops[0] == 0) {    // COPY -> STORE
280         unsigned SrcReg = MI->getOperand(2).getReg();
281         bool isKill = MI->getOperand(2).isKill();
282         bool isUndef = MI->getOperand(2).isUndef();
283         NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW))
284           .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
285           .addImm(0).addFrameIndex(FI);
286       } else {              // COPY -> LOAD
287         unsigned DstReg = MI->getOperand(0).getReg();
288         bool isDead = MI->getOperand(0).isDead();
289         bool isUndef = MI->getOperand(0).isUndef();
290         NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW))
291           .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
292                   getUndefRegState(isUndef))
293           .addImm(0).addFrameIndex(FI);
294       }
295     }
296     break;
297   case Mips::FMOV_S32:
298   case Mips::FMOV_D32:
299     if ((MI->getOperand(0).isReg()) &&
300         (MI->getOperand(1).isReg())) {
301       const TargetRegisterClass 
302         *RC = RI.getRegClass(MI->getOperand(0).getReg());
303       unsigned StoreOpc, LoadOpc;
304       bool IsMips1 = TM.getSubtarget<MipsSubtarget>().isMips1();
305
306       if (RC == Mips::FGR32RegisterClass) {
307         LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
308       } else {
309         assert(RC == Mips::AFGR64RegisterClass);
310         // Mips1 doesn't have ldc/sdc instructions.
311         if (IsMips1) break;
312         LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
313       }
314
315       if (Ops[0] == 0) {    // COPY -> STORE
316         unsigned SrcReg = MI->getOperand(1).getReg();
317         bool isKill = MI->getOperand(1).isKill();
318         bool isUndef = MI->getOperand(2).isUndef();
319         NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc))
320           .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
321           .addImm(0).addFrameIndex(FI) ;
322       } else {              // COPY -> LOAD
323         unsigned DstReg = MI->getOperand(0).getReg();
324         bool isDead = MI->getOperand(0).isDead();
325         bool isUndef = MI->getOperand(0).isUndef();
326         NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc))
327           .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
328                   getUndefRegState(isUndef))
329           .addImm(0).addFrameIndex(FI);
330       }
331     }
332     break;
333   }
334
335   return NewMI;
336 }
337
338 //===----------------------------------------------------------------------===//
339 // Branch Analysis
340 //===----------------------------------------------------------------------===//
341
342 /// GetCondFromBranchOpc - Return the Mips CC that matches 
343 /// the correspondent Branch instruction opcode.
344 static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc) 
345 {
346   switch (BrOpc) {
347   default: return Mips::COND_INVALID;
348   case Mips::BEQ  : return Mips::COND_E;
349   case Mips::BNE  : return Mips::COND_NE;
350   case Mips::BGTZ : return Mips::COND_GZ;
351   case Mips::BGEZ : return Mips::COND_GEZ;
352   case Mips::BLTZ : return Mips::COND_LZ;
353   case Mips::BLEZ : return Mips::COND_LEZ;
354
355   // We dont do fp branch analysis yet!  
356   case Mips::BC1T : 
357   case Mips::BC1F : return Mips::COND_INVALID;
358   }
359 }
360
361 /// GetCondBranchFromCond - Return the Branch instruction
362 /// opcode that matches the cc.
363 unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC) 
364 {
365   switch (CC) {
366   default: llvm_unreachable("Illegal condition code!");
367   case Mips::COND_E   : return Mips::BEQ;
368   case Mips::COND_NE  : return Mips::BNE;
369   case Mips::COND_GZ  : return Mips::BGTZ;
370   case Mips::COND_GEZ : return Mips::BGEZ;
371   case Mips::COND_LZ  : return Mips::BLTZ;
372   case Mips::COND_LEZ : return Mips::BLEZ;
373
374   case Mips::FCOND_F:
375   case Mips::FCOND_UN:
376   case Mips::FCOND_EQ:
377   case Mips::FCOND_UEQ:
378   case Mips::FCOND_OLT:
379   case Mips::FCOND_ULT:
380   case Mips::FCOND_OLE:
381   case Mips::FCOND_ULE:
382   case Mips::FCOND_SF:
383   case Mips::FCOND_NGLE:
384   case Mips::FCOND_SEQ:
385   case Mips::FCOND_NGL:
386   case Mips::FCOND_LT:
387   case Mips::FCOND_NGE:
388   case Mips::FCOND_LE:
389   case Mips::FCOND_NGT: return Mips::BC1T;
390
391   case Mips::FCOND_T:
392   case Mips::FCOND_OR:
393   case Mips::FCOND_NEQ:
394   case Mips::FCOND_OGL:
395   case Mips::FCOND_UGE:
396   case Mips::FCOND_OGE:
397   case Mips::FCOND_UGT:
398   case Mips::FCOND_OGT:
399   case Mips::FCOND_ST:
400   case Mips::FCOND_GLE:
401   case Mips::FCOND_SNE:
402   case Mips::FCOND_GL:
403   case Mips::FCOND_NLT:
404   case Mips::FCOND_GE:
405   case Mips::FCOND_NLE:
406   case Mips::FCOND_GT: return Mips::BC1F;
407   }
408 }
409
410 /// GetOppositeBranchCondition - Return the inverse of the specified 
411 /// condition, e.g. turning COND_E to COND_NE.
412 Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC) 
413 {
414   switch (CC) {
415   default: llvm_unreachable("Illegal condition code!");
416   case Mips::COND_E   : return Mips::COND_NE;
417   case Mips::COND_NE  : return Mips::COND_E;
418   case Mips::COND_GZ  : return Mips::COND_LEZ;
419   case Mips::COND_GEZ : return Mips::COND_LZ;
420   case Mips::COND_LZ  : return Mips::COND_GEZ;
421   case Mips::COND_LEZ : return Mips::COND_GZ;
422   case Mips::FCOND_F  : return Mips::FCOND_T;
423   case Mips::FCOND_UN : return Mips::FCOND_OR;
424   case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
425   case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
426   case Mips::FCOND_OLT: return Mips::FCOND_UGE;
427   case Mips::FCOND_ULT: return Mips::FCOND_OGE;
428   case Mips::FCOND_OLE: return Mips::FCOND_UGT;
429   case Mips::FCOND_ULE: return Mips::FCOND_OGT;
430   case Mips::FCOND_SF:  return Mips::FCOND_ST;
431   case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
432   case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
433   case Mips::FCOND_NGL: return Mips::FCOND_GL;
434   case Mips::FCOND_LT:  return Mips::FCOND_NLT;
435   case Mips::FCOND_NGE: return Mips::FCOND_GE;
436   case Mips::FCOND_LE:  return Mips::FCOND_NLE;
437   case Mips::FCOND_NGT: return Mips::FCOND_GT;
438   }
439 }
440
441 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, 
442                                   MachineBasicBlock *&TBB,
443                                   MachineBasicBlock *&FBB,
444                                   SmallVectorImpl<MachineOperand> &Cond,
445                                   bool AllowModify) const 
446 {
447   // If the block has no terminators, it just falls into the block after it.
448   MachineBasicBlock::iterator I = MBB.end();
449   if (I == MBB.begin())
450     return false;
451   --I;
452   while (I->isDebugValue()) {
453     if (I == MBB.begin())
454       return false;
455     --I;
456   }
457   if (!isUnpredicatedTerminator(I))
458     return false;
459   
460   // Get the last instruction in the block.
461   MachineInstr *LastInst = I;
462   
463   // If there is only one terminator instruction, process it.
464   unsigned LastOpc = LastInst->getOpcode();
465   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
466     if (!LastInst->getDesc().isBranch())
467       return true;
468
469     // Unconditional branch
470     if (LastOpc == Mips::J) {
471       TBB = LastInst->getOperand(0).getMBB();
472       return false;
473     }
474
475     Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
476     if (BranchCode == Mips::COND_INVALID)
477       return true;  // Can't handle indirect branch.
478
479     // Conditional branch
480     // Block ends with fall-through condbranch.
481     if (LastOpc != Mips::COND_INVALID) {
482       int LastNumOp = LastInst->getNumOperands();
483
484       TBB = LastInst->getOperand(LastNumOp-1).getMBB();
485       Cond.push_back(MachineOperand::CreateImm(BranchCode));
486
487       for (int i=0; i<LastNumOp-1; i++) {
488         Cond.push_back(LastInst->getOperand(i));
489       }
490
491       return false;
492     }
493   }
494   
495   // Get the instruction before it if it is a terminator.
496   MachineInstr *SecondLastInst = I;
497   
498   // If there are three terminators, we don't know what sort of block this is.
499   if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
500     return true;
501
502   // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
503   unsigned SecondLastOpc    = SecondLastInst->getOpcode();
504   Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
505
506   if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
507     int SecondNumOp = SecondLastInst->getNumOperands();
508
509     TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
510     Cond.push_back(MachineOperand::CreateImm(BranchCode));
511
512     for (int i=0; i<SecondNumOp-1; i++) {
513       Cond.push_back(SecondLastInst->getOperand(i));
514     }
515
516     FBB = LastInst->getOperand(0).getMBB();
517     return false;
518   }
519   
520   // If the block ends with two unconditional branches, handle it. The last 
521   // one is not executed, so remove it.
522   if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
523     TBB = SecondLastInst->getOperand(0).getMBB();
524     I = LastInst;
525     if (AllowModify)
526       I->eraseFromParent();
527     return false;
528   }
529
530   // Otherwise, can't handle this.
531   return true;
532 }
533
534 unsigned MipsInstrInfo::
535 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, 
536              MachineBasicBlock *FBB,
537              const SmallVectorImpl<MachineOperand> &Cond,
538              DebugLoc DL) const {
539   // Shouldn't be a fall through.
540   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
541   assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
542          "Mips branch conditions can have two|three components!");
543
544   if (FBB == 0) { // One way branch.
545     if (Cond.empty()) {
546       // Unconditional branch?
547       BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
548     } else {
549       // Conditional branch.
550       unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
551       const TargetInstrDesc &TID = get(Opc);
552
553       if (TID.getNumOperands() == 3)
554         BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg())
555                           .addReg(Cond[2].getReg())
556                           .addMBB(TBB);
557       else
558         BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg())
559                           .addMBB(TBB);
560
561     }                             
562     return 1;
563   }
564   
565   // Two-way Conditional branch.
566   unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
567   const TargetInstrDesc &TID = get(Opc);
568
569   if (TID.getNumOperands() == 3)
570     BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
571                       .addMBB(TBB);
572   else
573     BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg()).addMBB(TBB);
574
575   BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
576   return 2;
577 }
578
579 unsigned MipsInstrInfo::
580 RemoveBranch(MachineBasicBlock &MBB) const 
581 {
582   MachineBasicBlock::iterator I = MBB.end();
583   if (I == MBB.begin()) return 0;
584   --I;
585   while (I->isDebugValue()) {
586     if (I == MBB.begin())
587       return 0;
588     --I;
589   }
590   if (I->getOpcode() != Mips::J && 
591       GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
592     return 0;
593   
594   // Remove the branch.
595   I->eraseFromParent();
596   
597   I = MBB.end();
598   
599   if (I == MBB.begin()) return 1;
600   --I;
601   if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
602     return 1;
603   
604   // Remove the branch.
605   I->eraseFromParent();
606   return 2;
607 }
608
609 /// ReverseBranchCondition - Return the inverse opcode of the 
610 /// specified Branch instruction.
611 bool MipsInstrInfo::
612 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const 
613 {
614   assert( (Cond.size() == 3 || Cond.size() == 2) && 
615           "Invalid Mips branch condition!");
616   Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
617   return false;
618 }
619
620 /// getGlobalBaseReg - Return a virtual register initialized with the
621 /// the global base register value. Output instructions required to
622 /// initialize the register in the function entry block, if necessary.
623 ///
624 unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
625   MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
626   unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
627   if (GlobalBaseReg != 0)
628     return GlobalBaseReg;
629
630   // Insert the set of GlobalBaseReg into the first MBB of the function
631   MachineBasicBlock &FirstMBB = MF->front();
632   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
633   MachineRegisterInfo &RegInfo = MF->getRegInfo();
634   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
635
636   GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
637   BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
638           GlobalBaseReg).addReg(Mips::GP);
639   RegInfo.addLiveIn(Mips::GP);
640
641   MipsFI->setGlobalBaseReg(GlobalBaseReg);
642   return GlobalBaseReg;
643 }