LLVM API Documentation

MipsInstrInfo.cpp

Go to the documentation of this file.
00001 //===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the Mips implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MipsInstrInfo.h"
00015 #include "MipsTargetMachine.h"
00016 #include "MipsMachineFunction.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/CodeGen/MachineInstrBuilder.h"
00019 #include "llvm/CodeGen/MachineRegisterInfo.h"
00020 #include "llvm/Support/ErrorHandling.h"
00021 #include "MipsGenInstrInfo.inc"
00022 
00023 using namespace llvm;
00024 
00025 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
00026   : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
00027     TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
00028 
00029 static bool isZeroImm(const MachineOperand &op) {
00030   return op.isImm() && op.getImm() == 0;
00031 }
00032 
00033 /// Return true if the instruction is a register to register move and
00034 /// leave the source and dest operands in the passed parameters.
00035 bool MipsInstrInfo::
00036 isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg,
00037             unsigned &SrcSubIdx, unsigned &DstSubIdx) const 
00038 {
00039   SrcSubIdx = DstSubIdx = 0; // No sub-registers.
00040 
00041   // addu $dst, $src, $zero || addu $dst, $zero, $src
00042   // or   $dst, $src, $zero || or   $dst, $zero, $src
00043   if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
00044     if (MI.getOperand(1).getReg() == Mips::ZERO) {
00045       DstReg = MI.getOperand(0).getReg();
00046       SrcReg = MI.getOperand(2).getReg();
00047       return true;
00048     } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
00049       DstReg = MI.getOperand(0).getReg();
00050       SrcReg = MI.getOperand(1).getReg();
00051       return true;
00052     }
00053   }
00054 
00055   // mov $fpDst, $fpSrc
00056   // mfc $gpDst, $fpSrc
00057   // mtc $fpDst, $gpSrc
00058   if (MI.getOpcode() == Mips::FMOV_S32 || 
00059       MI.getOpcode() == Mips::FMOV_D32 || 
00060       MI.getOpcode() == Mips::MFC1 || 
00061       MI.getOpcode() == Mips::MTC1 ||
00062       MI.getOpcode() == Mips::MOVCCRToCCR) {
00063     DstReg = MI.getOperand(0).getReg();
00064     SrcReg = MI.getOperand(1).getReg();
00065     return true;
00066   }
00067 
00068   // addiu $dst, $src, 0
00069   if (MI.getOpcode() == Mips::ADDiu) {
00070     if ((MI.getOperand(1).isReg()) && (isZeroImm(MI.getOperand(2)))) {
00071       DstReg = MI.getOperand(0).getReg();
00072       SrcReg = MI.getOperand(1).getReg();
00073       return true;
00074     }
00075   }
00076 
00077   return false;
00078 }
00079 
00080 /// isLoadFromStackSlot - If the specified machine instruction is a direct
00081 /// load from a stack slot, return the virtual or physical register number of
00082 /// the destination along with the FrameIndex of the loaded stack slot.  If
00083 /// not, return 0.  This predicate must return 0 if the instruction has
00084 /// any side effects other than loading from the stack slot.
00085 unsigned MipsInstrInfo::
00086 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const 
00087 {
00088   if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
00089       (MI->getOpcode() == Mips::LDC1)) {
00090     if ((MI->getOperand(2).isFI()) && // is a stack slot
00091         (MI->getOperand(1).isImm()) &&  // the imm is zero
00092         (isZeroImm(MI->getOperand(1)))) {
00093       FrameIndex = MI->getOperand(2).getIndex();
00094       return MI->getOperand(0).getReg();
00095     }
00096   }
00097 
00098   return 0;
00099 }
00100 
00101 /// isStoreToStackSlot - If the specified machine instruction is a direct
00102 /// store to a stack slot, return the virtual or physical register number of
00103 /// the source reg along with the FrameIndex of the loaded stack slot.  If
00104 /// not, return 0.  This predicate must return 0 if the instruction has
00105 /// any side effects other than storing to the stack slot.
00106 unsigned MipsInstrInfo::
00107 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const 
00108 {
00109   if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
00110       (MI->getOpcode() == Mips::SDC1)) {
00111     if ((MI->getOperand(2).isFI()) && // is a stack slot
00112         (MI->getOperand(1).isImm()) &&  // the imm is zero
00113         (isZeroImm(MI->getOperand(1)))) {
00114       FrameIndex = MI->getOperand(2).getIndex();
00115       return MI->getOperand(0).getReg();
00116     }
00117   }
00118   return 0;
00119 }
00120 
00121 /// insertNoop - If data hazard condition is found insert the target nop
00122 /// instruction.
00123 void MipsInstrInfo::
00124 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const 
00125 {
00126   DebugLoc DL = DebugLoc::getUnknownLoc();
00127   if (MI != MBB.end()) DL = MI->getDebugLoc();
00128   BuildMI(MBB, MI, DL, get(Mips::NOP));
00129 }
00130 
00131 bool MipsInstrInfo::
00132 copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00133              unsigned DestReg, unsigned SrcReg,
00134              const TargetRegisterClass *DestRC,
00135              const TargetRegisterClass *SrcRC) const {
00136   DebugLoc DL = DebugLoc::getUnknownLoc();
00137   if (I != MBB.end()) DL = I->getDebugLoc();
00138 
00139   if (DestRC != SrcRC) {
00140 
00141     // Copy to/from FCR31 condition register
00142     if ((DestRC == Mips::CPURegsRegisterClass) && 
00143         (SrcRC == Mips::CCRRegisterClass))
00144       BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg).addReg(SrcReg);
00145     else if ((DestRC == Mips::CCRRegisterClass) && 
00146         (SrcRC == Mips::CPURegsRegisterClass))
00147       BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg).addReg(SrcReg);
00148 
00149     // Moves between coprocessors and cpu
00150     else if ((DestRC == Mips::CPURegsRegisterClass) && 
00151         (SrcRC == Mips::FGR32RegisterClass))
00152       BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg).addReg(SrcReg);
00153     else if ((DestRC == Mips::FGR32RegisterClass) &&
00154              (SrcRC == Mips::CPURegsRegisterClass))
00155       BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg).addReg(SrcReg);
00156 
00157     // Move from/to Hi/Lo registers
00158     else if ((DestRC == Mips::HILORegisterClass) &&
00159              (SrcRC == Mips::CPURegsRegisterClass)) {
00160       unsigned Opc = (DestReg == Mips::HI) ? Mips::MTHI : Mips::MTLO;
00161       BuildMI(MBB, I, DL, get(Opc), DestReg);
00162     } else if ((SrcRC == Mips::HILORegisterClass) &&
00163                (DestRC == Mips::CPURegsRegisterClass)) {
00164       unsigned Opc = (SrcReg == Mips::HI) ? Mips::MFHI : Mips::MFLO;
00165       BuildMI(MBB, I, DL, get(Opc), DestReg);
00166 
00167     // Can't copy this register
00168     } else
00169       return false; 
00170 
00171     return true;
00172   }
00173 
00174   if (DestRC == Mips::CPURegsRegisterClass)
00175     BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
00176       .addReg(SrcReg);
00177   else if (DestRC == Mips::FGR32RegisterClass) 
00178     BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg).addReg(SrcReg);
00179   else if (DestRC == Mips::AFGR64RegisterClass)
00180     BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
00181   else if (DestRC == Mips::CCRRegisterClass)
00182     BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg).addReg(SrcReg);
00183   else
00184     // Can't copy this register
00185     return false;
00186   
00187   return true;
00188 }
00189 
00190 void MipsInstrInfo::
00191 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00192                     unsigned SrcReg, bool isKill, int FI, 
00193                     const TargetRegisterClass *RC) const {
00194   unsigned Opc;
00195 
00196   DebugLoc DL = DebugLoc::getUnknownLoc();
00197   if (I != MBB.end()) DL = I->getDebugLoc();
00198 
00199   if (RC == Mips::CPURegsRegisterClass) 
00200     Opc = Mips::SW;
00201   else if (RC == Mips::FGR32RegisterClass)
00202     Opc = Mips::SWC1;
00203   else {
00204     assert(RC == Mips::AFGR64RegisterClass);
00205     Opc = Mips::SDC1;
00206   }
00207   
00208   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
00209           .addImm(0).addFrameIndex(FI);
00210 }
00211 
00212 void MipsInstrInfo::
00213 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00214                      unsigned DestReg, int FI,
00215                      const TargetRegisterClass *RC) const 
00216 {
00217   unsigned Opc;
00218   if (RC == Mips::CPURegsRegisterClass) 
00219     Opc = Mips::LW;
00220   else if (RC == Mips::FGR32RegisterClass)
00221     Opc = Mips::LWC1;
00222   else {
00223     assert(RC == Mips::AFGR64RegisterClass);
00224     Opc = Mips::LDC1;
00225   }
00226   
00227   DebugLoc DL = DebugLoc::getUnknownLoc();
00228   if (I != MBB.end()) DL = I->getDebugLoc();
00229   BuildMI(MBB, I, DL, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
00230 }
00231 
00232 MachineInstr *MipsInstrInfo::
00233 foldMemoryOperandImpl(MachineFunction &MF,
00234                       MachineInstr* MI,
00235                       const SmallVectorImpl<unsigned> &Ops, int FI) const 
00236 {
00237   if (Ops.size() != 1) return NULL;
00238 
00239   MachineInstr *NewMI = NULL;
00240 
00241   switch (MI->getOpcode()) {
00242   case Mips::ADDu:
00243     if ((MI->getOperand(0).isReg()) &&
00244         (MI->getOperand(1).isReg()) &&
00245         (MI->getOperand(1).getReg() == Mips::ZERO) &&
00246         (MI->getOperand(2).isReg())) {
00247       if (Ops[0] == 0) {    // COPY -> STORE
00248         unsigned SrcReg = MI->getOperand(2).getReg();
00249         bool isKill = MI->getOperand(2).isKill();
00250         bool isUndef = MI->getOperand(2).isUndef();
00251         NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW))
00252           .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
00253           .addImm(0).addFrameIndex(FI);
00254       } else {              // COPY -> LOAD
00255         unsigned DstReg = MI->getOperand(0).getReg();
00256         bool isDead = MI->getOperand(0).isDead();
00257         bool isUndef = MI->getOperand(0).isUndef();
00258         NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW))
00259           .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
00260                   getUndefRegState(isUndef))
00261           .addImm(0).addFrameIndex(FI);
00262       }
00263     }
00264     break;
00265   case Mips::FMOV_S32:
00266   case Mips::FMOV_D32:
00267     if ((MI->getOperand(0).isReg()) &&
00268         (MI->getOperand(1).isReg())) {
00269       const TargetRegisterClass 
00270         *RC = RI.getRegClass(MI->getOperand(0).getReg());
00271       unsigned StoreOpc, LoadOpc;
00272 
00273       if (RC == Mips::FGR32RegisterClass) {
00274         LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
00275       } else {
00276         assert(RC == Mips::AFGR64RegisterClass);
00277         LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
00278       }
00279 
00280       if (Ops[0] == 0) {    // COPY -> STORE
00281         unsigned SrcReg = MI->getOperand(1).getReg();
00282         bool isKill = MI->getOperand(1).isKill();
00283         bool isUndef = MI->getOperand(2).isUndef();
00284         NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc))
00285           .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
00286           .addImm(0).addFrameIndex(FI) ;
00287       } else {              // COPY -> LOAD
00288         unsigned DstReg = MI->getOperand(0).getReg();
00289         bool isDead = MI->getOperand(0).isDead();
00290         bool isUndef = MI->getOperand(0).isUndef();
00291         NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc))
00292           .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
00293                   getUndefRegState(isUndef))
00294           .addImm(0).addFrameIndex(FI);
00295       }
00296     }
00297     break;
00298   }
00299 
00300   return NewMI;
00301 }
00302 
00303 //===----------------------------------------------------------------------===//
00304 // Branch Analysis
00305 //===----------------------------------------------------------------------===//
00306 
00307 /// GetCondFromBranchOpc - Return the Mips CC that matches 
00308 /// the correspondent Branch instruction opcode.
00309 static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc) 
00310 {
00311   switch (BrOpc) {
00312   default: return Mips::COND_INVALID;
00313   case Mips::BEQ  : return Mips::COND_E;
00314   case Mips::BNE  : return Mips::COND_NE;
00315   case Mips::BGTZ : return Mips::COND_GZ;
00316   case Mips::BGEZ : return Mips::COND_GEZ;
00317   case Mips::BLTZ : return Mips::COND_LZ;
00318   case Mips::BLEZ : return Mips::COND_LEZ;
00319 
00320   // We dont do fp branch analysis yet!  
00321   case Mips::BC1T : 
00322   case Mips::BC1F : return Mips::COND_INVALID;
00323   }
00324 }
00325 
00326 /// GetCondBranchFromCond - Return the Branch instruction
00327 /// opcode that matches the cc.
00328 unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC) 
00329 {
00330   switch (CC) {
00331   default: llvm_unreachable("Illegal condition code!");
00332   case Mips::COND_E   : return Mips::BEQ;
00333   case Mips::COND_NE  : return Mips::BNE;
00334   case Mips::COND_GZ  : return Mips::BGTZ;
00335   case Mips::COND_GEZ : return Mips::BGEZ;
00336   case Mips::COND_LZ  : return Mips::BLTZ;
00337   case Mips::COND_LEZ : return Mips::BLEZ;
00338 
00339   case Mips::FCOND_F:
00340   case Mips::FCOND_UN:
00341   case Mips::FCOND_EQ:
00342   case Mips::FCOND_UEQ:
00343   case Mips::FCOND_OLT:
00344   case Mips::FCOND_ULT:
00345   case Mips::FCOND_OLE:
00346   case Mips::FCOND_ULE:
00347   case Mips::FCOND_SF:
00348   case Mips::FCOND_NGLE:
00349   case Mips::FCOND_SEQ:
00350   case Mips::FCOND_NGL:
00351   case Mips::FCOND_LT:
00352   case Mips::FCOND_NGE:
00353   case Mips::FCOND_LE:
00354   case Mips::FCOND_NGT: return Mips::BC1T;
00355 
00356   case Mips::FCOND_T:
00357   case Mips::FCOND_OR:
00358   case Mips::FCOND_NEQ:
00359   case Mips::FCOND_OGL:
00360   case Mips::FCOND_UGE:
00361   case Mips::FCOND_OGE:
00362   case Mips::FCOND_UGT:
00363   case Mips::FCOND_OGT:
00364   case Mips::FCOND_ST:
00365   case Mips::FCOND_GLE:
00366   case Mips::FCOND_SNE:
00367   case Mips::FCOND_GL:
00368   case Mips::FCOND_NLT:
00369   case Mips::FCOND_GE:
00370   case Mips::FCOND_NLE:
00371   case Mips::FCOND_GT: return Mips::BC1F;
00372   }
00373 }
00374 
00375 /// GetOppositeBranchCondition - Return the inverse of the specified 
00376 /// condition, e.g. turning COND_E to COND_NE.
00377 Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC) 
00378 {
00379   switch (CC) {
00380   default: llvm_unreachable("Illegal condition code!");
00381   case Mips::COND_E   : return Mips::COND_NE;
00382   case Mips::COND_NE  : return Mips::COND_E;
00383   case Mips::COND_GZ  : return Mips::COND_LEZ;
00384   case Mips::COND_GEZ : return Mips::COND_LZ;
00385   case Mips::COND_LZ  : return Mips::COND_GEZ;
00386   case Mips::COND_LEZ : return Mips::COND_GZ;
00387   case Mips::FCOND_F  : return Mips::FCOND_T;
00388   case Mips::FCOND_UN : return Mips::FCOND_OR;
00389   case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
00390   case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
00391   case Mips::FCOND_OLT: return Mips::FCOND_UGE;
00392   case Mips::FCOND_ULT: return Mips::FCOND_OGE;
00393   case Mips::FCOND_OLE: return Mips::FCOND_UGT;
00394   case Mips::FCOND_ULE: return Mips::FCOND_OGT;
00395   case Mips::FCOND_SF:  return Mips::FCOND_ST;
00396   case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
00397   case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
00398   case Mips::FCOND_NGL: return Mips::FCOND_GL;
00399   case Mips::FCOND_LT:  return Mips::FCOND_NLT;
00400   case Mips::FCOND_NGE: return Mips::FCOND_GE;
00401   case Mips::FCOND_LE:  return Mips::FCOND_NLE;
00402   case Mips::FCOND_NGT: return Mips::FCOND_GT;
00403   }
00404 }
00405 
00406 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, 
00407                                   MachineBasicBlock *&TBB,
00408                                   MachineBasicBlock *&FBB,
00409                                   SmallVectorImpl<MachineOperand> &Cond,
00410                                   bool AllowModify) const 
00411 {
00412   // If the block has no terminators, it just falls into the block after it.
00413   MachineBasicBlock::iterator I = MBB.end();
00414   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
00415     return false;
00416   
00417   // Get the last instruction in the block.
00418   MachineInstr *LastInst = I;
00419   
00420   // If there is only one terminator instruction, process it.
00421   unsigned LastOpc = LastInst->getOpcode();
00422   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
00423     if (!LastInst->getDesc().isBranch())
00424       return true;
00425 
00426     // Unconditional branch
00427     if (LastOpc == Mips::J) {
00428       TBB = LastInst->getOperand(0).getMBB();
00429       return false;
00430     }
00431 
00432     Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
00433     if (BranchCode == Mips::COND_INVALID)
00434       return true;  // Can't handle indirect branch.
00435 
00436     // Conditional branch
00437     // Block ends with fall-through condbranch.
00438     if (LastOpc != Mips::COND_INVALID) {
00439       int LastNumOp = LastInst->getNumOperands();
00440 
00441       TBB = LastInst->getOperand(LastNumOp-1).getMBB();
00442       Cond.push_back(MachineOperand::CreateImm(BranchCode));
00443 
00444       for (int i=0; i<LastNumOp-1; i++) {
00445         Cond.push_back(LastInst->getOperand(i));
00446       }
00447 
00448       return false;
00449     }
00450   }
00451   
00452   // Get the instruction before it if it is a terminator.
00453   MachineInstr *SecondLastInst = I;
00454   
00455   // If there are three terminators, we don't know what sort of block this is.
00456   if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
00457     return true;
00458 
00459   // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
00460   unsigned SecondLastOpc    = SecondLastInst->getOpcode();
00461   Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
00462 
00463   if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
00464     int SecondNumOp = SecondLastInst->getNumOperands();
00465 
00466     TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
00467     Cond.push_back(MachineOperand::CreateImm(BranchCode));
00468 
00469     for (int i=0; i<SecondNumOp-1; i++) {
00470       Cond.push_back(SecondLastInst->getOperand(i));
00471     }
00472 
00473     FBB = LastInst->getOperand(0).getMBB();
00474     return false;
00475   }
00476   
00477   // If the block ends with two unconditional branches, handle it. The last 
00478   // one is not executed, so remove it.
00479   if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
00480     TBB = SecondLastInst->getOperand(0).getMBB();
00481     I = LastInst;
00482     if (AllowModify)
00483       I->eraseFromParent();
00484     return false;
00485   }
00486 
00487   // Otherwise, can't handle this.
00488   return true;
00489 }
00490 
00491 unsigned MipsInstrInfo::
00492 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, 
00493              MachineBasicBlock *FBB,
00494              const SmallVectorImpl<MachineOperand> &Cond) const {
00495   // FIXME this should probably have a DebugLoc argument
00496   DebugLoc dl = DebugLoc::getUnknownLoc();
00497   // Shouldn't be a fall through.
00498   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00499   assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
00500          "Mips branch conditions can have two|three components!");
00501 
00502   if (FBB == 0) { // One way branch.
00503     if (Cond.empty()) {
00504       // Unconditional branch?
00505       BuildMI(&MBB, dl, get(Mips::J)).addMBB(TBB);
00506     } else {
00507       // Conditional branch.
00508       unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
00509       const TargetInstrDesc &TID = get(Opc);
00510 
00511       if (TID.getNumOperands() == 3)
00512         BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
00513                           .addReg(Cond[2].getReg())
00514                           .addMBB(TBB);
00515       else
00516         BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
00517                           .addMBB(TBB);
00518 
00519     }                             
00520     return 1;
00521   }
00522   
00523   // Two-way Conditional branch.
00524   unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
00525   const TargetInstrDesc &TID = get(Opc);
00526 
00527   if (TID.getNumOperands() == 3)
00528     BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
00529                       .addMBB(TBB);
00530   else
00531     BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addMBB(TBB);
00532 
00533   BuildMI(&MBB, dl, get(Mips::J)).addMBB(FBB);
00534   return 2;
00535 }
00536 
00537 unsigned MipsInstrInfo::
00538 RemoveBranch(MachineBasicBlock &MBB) const 
00539 {
00540   MachineBasicBlock::iterator I = MBB.end();
00541   if (I == MBB.begin()) return 0;
00542   --I;
00543   if (I->getOpcode() != Mips::J && 
00544       GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
00545     return 0;
00546   
00547   // Remove the branch.
00548   I->eraseFromParent();
00549   
00550   I = MBB.end();
00551   
00552   if (I == MBB.begin()) return 1;
00553   --I;
00554   if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
00555     return 1;
00556   
00557   // Remove the branch.
00558   I->eraseFromParent();
00559   return 2;
00560 }
00561 
00562 /// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not
00563 /// fall-through into its successor block.
00564 bool MipsInstrInfo::
00565 BlockHasNoFallThrough(const MachineBasicBlock &MBB) const 
00566 {
00567   if (MBB.empty()) return false;
00568   
00569   switch (MBB.back().getOpcode()) {
00570   case Mips::RET:     // Return.
00571   case Mips::JR:      // Indirect branch.
00572   case Mips::J:       // Uncond branch.
00573     return true;
00574   default: return false;
00575   }
00576 }
00577 
00578 /// ReverseBranchCondition - Return the inverse opcode of the 
00579 /// specified Branch instruction.
00580 bool MipsInstrInfo::
00581 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const 
00582 {
00583   assert( (Cond.size() == 3 || Cond.size() == 2) && 
00584           "Invalid Mips branch condition!");
00585   Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
00586   return false;
00587 }
00588 
00589 /// getGlobalBaseReg - Return a virtual register initialized with the
00590 /// the global base register value. Output instructions required to
00591 /// initialize the register in the function entry block, if necessary.
00592 ///
00593 unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
00594   MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
00595   unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
00596   if (GlobalBaseReg != 0)
00597     return GlobalBaseReg;
00598 
00599   // Insert the set of GlobalBaseReg into the first MBB of the function
00600   MachineBasicBlock &FirstMBB = MF->front();
00601   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
00602   MachineRegisterInfo &RegInfo = MF->getRegInfo();
00603   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
00604 
00605   GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
00606   bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalBaseReg, Mips::GP,
00607                               Mips::CPURegsRegisterClass,
00608                               Mips::CPURegsRegisterClass);
00609   assert(Ok && "Couldn't assign to global base register!");
00610   Ok = Ok; // Silence warning when assertions are turned off.
00611   RegInfo.addLiveIn(Mips::GP);
00612 
00613   MipsFI->setGlobalBaseReg(GlobalBaseReg);
00614   return GlobalBaseReg;
00615 }