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