LLVM API Documentation

MipsInstrInfo.cpp
Go to the documentation of this file.
00001 //===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===//
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 "InstPrinter/MipsInstPrinter.h"
00016 #include "MipsAnalyzeImmediate.h"
00017 #include "MipsMachineFunction.h"
00018 #include "MipsTargetMachine.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/CodeGen/MachineInstrBuilder.h"
00021 #include "llvm/CodeGen/MachineRegisterInfo.h"
00022 #include "llvm/Support/ErrorHandling.h"
00023 #include "llvm/Support/TargetRegistry.h"
00024 
00025 using namespace llvm;
00026 
00027 #define GET_INSTRINFO_CTOR_DTOR
00028 #include "MipsGenInstrInfo.inc"
00029 
00030 // Pin the vtable to this file.
00031 void MipsInstrInfo::anchor() {}
00032 
00033 MipsInstrInfo::MipsInstrInfo(const MipsSubtarget &STI, unsigned UncondBr)
00034     : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
00035       Subtarget(STI), UncondBrOpc(UncondBr) {}
00036 
00037 const MipsInstrInfo *MipsInstrInfo::create(MipsSubtarget &STI) {
00038   if (STI.inMips16Mode())
00039     return llvm::createMips16InstrInfo(STI);
00040 
00041   return llvm::createMipsSEInstrInfo(STI);
00042 }
00043 
00044 bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
00045   return op.isImm() && op.getImm() == 0;
00046 }
00047 
00048 /// insertNoop - If data hazard condition is found insert the target nop
00049 /// instruction.
00050 void MipsInstrInfo::
00051 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
00052 {
00053   DebugLoc DL;
00054   BuildMI(MBB, MI, DL, get(Mips::NOP));
00055 }
00056 
00057 MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
00058                                                 unsigned Flag) const {
00059   MachineFunction &MF = *MBB.getParent();
00060   MachineFrameInfo &MFI = *MF.getFrameInfo();
00061   unsigned Align = MFI.getObjectAlignment(FI);
00062 
00063   return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
00064                                  MFI.getObjectSize(FI), Align);
00065 }
00066 
00067 //===----------------------------------------------------------------------===//
00068 // Branch Analysis
00069 //===----------------------------------------------------------------------===//
00070 
00071 void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
00072                                   MachineBasicBlock *&BB,
00073                                   SmallVectorImpl<MachineOperand> &Cond) const {
00074   assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
00075   int NumOp = Inst->getNumExplicitOperands();
00076 
00077   // for both int and fp branches, the last explicit operand is the
00078   // MBB.
00079   BB = Inst->getOperand(NumOp-1).getMBB();
00080   Cond.push_back(MachineOperand::CreateImm(Opc));
00081 
00082   for (int i=0; i<NumOp-1; i++)
00083     Cond.push_back(Inst->getOperand(i));
00084 }
00085 
00086 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
00087                                   MachineBasicBlock *&TBB,
00088                                   MachineBasicBlock *&FBB,
00089                                   SmallVectorImpl<MachineOperand> &Cond,
00090                                   bool AllowModify) const {
00091   SmallVector<MachineInstr*, 2> BranchInstrs;
00092   BranchType BT = AnalyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
00093 
00094   return (BT == BT_None) || (BT == BT_Indirect);
00095 }
00096 
00097 void
00098 MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00099                            DebugLoc DL,
00100                            const SmallVectorImpl<MachineOperand> &Cond) const {
00101   unsigned Opc = Cond[0].getImm();
00102   const MCInstrDesc &MCID = get(Opc);
00103   MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
00104 
00105   for (unsigned i = 1; i < Cond.size(); ++i) {
00106     if (Cond[i].isReg())
00107       MIB.addReg(Cond[i].getReg());
00108     else if (Cond[i].isImm())
00109       MIB.addImm(Cond[i].getImm());
00110     else
00111        assert(true && "Cannot copy operand");
00112   }
00113   MIB.addMBB(TBB);
00114 }
00115 
00116 unsigned MipsInstrInfo::InsertBranch(
00117     MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
00118     const SmallVectorImpl<MachineOperand> &Cond, DebugLoc DL) const {
00119   // Shouldn't be a fall through.
00120   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00121 
00122   // # of condition operands:
00123   //  Unconditional branches: 0
00124   //  Floating point branches: 1 (opc)
00125   //  Int BranchZero: 2 (opc, reg)
00126   //  Int Branch: 3 (opc, reg0, reg1)
00127   assert((Cond.size() <= 3) &&
00128          "# of Mips branch conditions must be <= 3!");
00129 
00130   // Two-way Conditional branch.
00131   if (FBB) {
00132     BuildCondBr(MBB, TBB, DL, Cond);
00133     BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
00134     return 2;
00135   }
00136 
00137   // One way branch.
00138   // Unconditional branch.
00139   if (Cond.empty())
00140     BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
00141   else // Conditional branch.
00142     BuildCondBr(MBB, TBB, DL, Cond);
00143   return 1;
00144 }
00145 
00146 unsigned MipsInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
00147   MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
00148   MachineBasicBlock::reverse_iterator FirstBr;
00149   unsigned removed;
00150 
00151   // Skip all the debug instructions.
00152   while (I != REnd && I->isDebugValue())
00153     ++I;
00154 
00155   FirstBr = I;
00156 
00157   // Up to 2 branches are removed.
00158   // Note that indirect branches are not removed.
00159   for (removed = 0; I != REnd && removed < 2; ++I, ++removed)
00160     if (!getAnalyzableBrOpc(I->getOpcode()))
00161       break;
00162 
00163   MBB.erase(I.base(), FirstBr.base());
00164 
00165   return removed;
00166 }
00167 
00168 /// ReverseBranchCondition - Return the inverse opcode of the
00169 /// specified Branch instruction.
00170 bool MipsInstrInfo::ReverseBranchCondition(
00171     SmallVectorImpl<MachineOperand> &Cond) const {
00172   assert( (Cond.size() && Cond.size() <= 3) &&
00173           "Invalid Mips branch condition!");
00174   Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
00175   return false;
00176 }
00177 
00178 MipsInstrInfo::BranchType MipsInstrInfo::AnalyzeBranch(
00179     MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
00180     SmallVectorImpl<MachineOperand> &Cond, bool AllowModify,
00181     SmallVectorImpl<MachineInstr *> &BranchInstrs) const {
00182 
00183   MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
00184 
00185   // Skip all the debug instructions.
00186   while (I != REnd && I->isDebugValue())
00187     ++I;
00188 
00189   if (I == REnd || !isUnpredicatedTerminator(&*I)) {
00190     // This block ends with no branches (it just falls through to its succ).
00191     // Leave TBB/FBB null.
00192     TBB = FBB = nullptr;
00193     return BT_NoBranch;
00194   }
00195 
00196   MachineInstr *LastInst = &*I;
00197   unsigned LastOpc = LastInst->getOpcode();
00198   BranchInstrs.push_back(LastInst);
00199 
00200   // Not an analyzable branch (e.g., indirect jump).
00201   if (!getAnalyzableBrOpc(LastOpc))
00202     return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
00203 
00204   // Get the second to last instruction in the block.
00205   unsigned SecondLastOpc = 0;
00206   MachineInstr *SecondLastInst = nullptr;
00207 
00208   if (++I != REnd) {
00209     SecondLastInst = &*I;
00210     SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
00211 
00212     // Not an analyzable branch (must be an indirect jump).
00213     if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
00214       return BT_None;
00215   }
00216 
00217   // If there is only one terminator instruction, process it.
00218   if (!SecondLastOpc) {
00219     // Unconditional branch.
00220     if (LastOpc == UncondBrOpc) {
00221       TBB = LastInst->getOperand(0).getMBB();
00222       return BT_Uncond;
00223     }
00224 
00225     // Conditional branch
00226     AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
00227     return BT_Cond;
00228   }
00229 
00230   // If we reached here, there are two branches.
00231   // If there are three terminators, we don't know what sort of block this is.
00232   if (++I != REnd && isUnpredicatedTerminator(&*I))
00233     return BT_None;
00234 
00235   BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
00236 
00237   // If second to last instruction is an unconditional branch,
00238   // analyze it and remove the last instruction.
00239   if (SecondLastOpc == UncondBrOpc) {
00240     // Return if the last instruction cannot be removed.
00241     if (!AllowModify)
00242       return BT_None;
00243 
00244     TBB = SecondLastInst->getOperand(0).getMBB();
00245     LastInst->eraseFromParent();
00246     BranchInstrs.pop_back();
00247     return BT_Uncond;
00248   }
00249 
00250   // Conditional branch followed by an unconditional branch.
00251   // The last one must be unconditional.
00252   if (LastOpc != UncondBrOpc)
00253     return BT_None;
00254 
00255   AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
00256   FBB = LastInst->getOperand(0).getMBB();
00257 
00258   return BT_CondUncond;
00259 }
00260 
00261 /// Return the number of bytes of code the specified instruction may be.
00262 unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
00263   switch (MI->getOpcode()) {
00264   default:
00265     return MI->getDesc().getSize();
00266   case  TargetOpcode::INLINEASM: {       // Inline Asm: Variable size.
00267     const MachineFunction *MF = MI->getParent()->getParent();
00268     const char *AsmStr = MI->getOperand(0).getSymbolName();
00269     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
00270   }
00271   case Mips::CONSTPOOL_ENTRY:
00272     // If this machine instr is a constant pool entry, its size is recorded as
00273     // operand #2.
00274     return MI->getOperand(2).getImm();
00275   }
00276 }
00277 
00278 MachineInstrBuilder
00279 MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
00280                                   MachineBasicBlock::iterator I) const {
00281   MachineInstrBuilder MIB;
00282   MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
00283 
00284   for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J)
00285     MIB.addOperand(I->getOperand(J));
00286 
00287   MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());
00288   return MIB;
00289 }