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