LLVM API Documentation

MachineInstrBuilder.h
Go to the documentation of this file.
00001 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- 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 exposes a function named BuildMI, which is useful for dramatically
00011 // simplifying how MachineInstr's are created.  It allows use of code like this:
00012 //
00013 //   M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
00018 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
00019 
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineInstrBundle.h"
00022 #include "llvm/Support/ErrorHandling.h"
00023 
00024 namespace llvm {
00025 
00026 class MCInstrDesc;
00027 class MDNode;
00028 
00029 namespace RegState {
00030   enum {
00031     Define         = 0x2,
00032     Implicit       = 0x4,
00033     Kill           = 0x8,
00034     Dead           = 0x10,
00035     Undef          = 0x20,
00036     EarlyClobber   = 0x40,
00037     Debug          = 0x80,
00038     InternalRead   = 0x100,
00039     DefineNoRead   = Define | Undef,
00040     ImplicitDefine = Implicit | Define,
00041     ImplicitKill   = Implicit | Kill
00042   };
00043 }
00044 
00045 class MachineInstrBuilder {
00046   MachineFunction *MF;
00047   MachineInstr *MI;
00048 public:
00049   MachineInstrBuilder() : MF(nullptr), MI(nullptr) {}
00050 
00051   /// Create a MachineInstrBuilder for manipulating an existing instruction.
00052   /// F must be the machine function  that was used to allocate I.
00053   MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
00054 
00055   /// Allow automatic conversion to the machine instruction we are working on.
00056   ///
00057   operator MachineInstr*() const { return MI; }
00058   MachineInstr *operator->() const { return MI; }
00059   operator MachineBasicBlock::iterator() const { return MI; }
00060 
00061   /// addReg - Add a new virtual register operand...
00062   ///
00063   const
00064   MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
00065                               unsigned SubReg = 0) const {
00066     assert((flags & 0x1) == 0 &&
00067            "Passing in 'true' to addReg is forbidden! Use enums instead.");
00068     MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
00069                                                flags & RegState::Define,
00070                                                flags & RegState::Implicit,
00071                                                flags & RegState::Kill,
00072                                                flags & RegState::Dead,
00073                                                flags & RegState::Undef,
00074                                                flags & RegState::EarlyClobber,
00075                                                SubReg,
00076                                                flags & RegState::Debug,
00077                                                flags & RegState::InternalRead));
00078     return *this;
00079   }
00080 
00081   /// addImm - Add a new immediate operand.
00082   ///
00083   const MachineInstrBuilder &addImm(int64_t Val) const {
00084     MI->addOperand(*MF, MachineOperand::CreateImm(Val));
00085     return *this;
00086   }
00087 
00088   const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
00089     MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
00090     return *this;
00091   }
00092 
00093   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
00094     MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
00095     return *this;
00096   }
00097 
00098   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
00099                                     unsigned char TargetFlags = 0) const {
00100     MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
00101     return *this;
00102   }
00103 
00104   const MachineInstrBuilder &addFrameIndex(int Idx) const {
00105     MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
00106     return *this;
00107   }
00108 
00109   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
00110                                                   int Offset = 0,
00111                                           unsigned char TargetFlags = 0) const {
00112     MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
00113     return *this;
00114   }
00115 
00116   const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
00117                                           unsigned char TargetFlags = 0) const {
00118     MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
00119                                                           TargetFlags));
00120     return *this;
00121   }
00122 
00123   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
00124                                           unsigned char TargetFlags = 0) const {
00125     MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
00126     return *this;
00127   }
00128 
00129   const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
00130                                               int64_t Offset = 0,
00131                                           unsigned char TargetFlags = 0) const {
00132     MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
00133     return *this;
00134   }
00135 
00136   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
00137                                           unsigned char TargetFlags = 0) const {
00138     MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
00139     return *this;
00140   }
00141 
00142   const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
00143                                              int64_t Offset = 0,
00144                                           unsigned char TargetFlags = 0) const {
00145     MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
00146     return *this;
00147   }
00148 
00149   const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
00150     MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
00151     return *this;
00152   }
00153 
00154   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
00155     MI->addMemOperand(*MF, MMO);
00156     return *this;
00157   }
00158 
00159   const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
00160                                         MachineInstr::mmo_iterator e) const {
00161     MI->setMemRefs(b, e);
00162     return *this;
00163   }
00164 
00165 
00166   const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
00167     MI->addOperand(*MF, MO);
00168     return *this;
00169   }
00170 
00171   const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
00172     MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
00173     return *this;
00174   }
00175 
00176   const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
00177     MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
00178     return *this;
00179   }
00180 
00181   const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
00182     MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym));
00183     return *this;
00184   }
00185 
00186   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
00187     MI->setFlags(Flags);
00188     return *this;
00189   }
00190 
00191   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
00192     MI->setFlag(Flag);
00193     return *this;
00194   }
00195 
00196   // Add a displacement from an existing MachineOperand with an added offset.
00197   const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
00198                                      unsigned char TargetFlags = 0) const {
00199     switch (Disp.getType()) {
00200       default:
00201         llvm_unreachable("Unhandled operand type in addDisp()");
00202       case MachineOperand::MO_Immediate:
00203         return addImm(Disp.getImm() + off);
00204       case MachineOperand::MO_GlobalAddress: {
00205         // If caller specifies new TargetFlags then use it, otherwise the
00206         // default behavior is to copy the target flags from the existing
00207         // MachineOperand. This means if the caller wants to clear the
00208         // target flags it needs to do so explicitly.
00209         if (TargetFlags)
00210           return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
00211                                   TargetFlags);
00212         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
00213                                 Disp.getTargetFlags());
00214       }
00215     }
00216   }
00217 
00218   /// Copy all the implicit operands from OtherMI onto this one.
00219   const MachineInstrBuilder &copyImplicitOps(const MachineInstr *OtherMI) {
00220     MI->copyImplicitOps(*MF, OtherMI);
00221     return *this;
00222   }
00223 };
00224 
00225 /// BuildMI - Builder interface.  Specify how to create the initial instruction
00226 /// itself.
00227 ///
00228 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00229                                    DebugLoc DL,
00230                                    const MCInstrDesc &MCID) {
00231   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
00232 }
00233 
00234 /// BuildMI - This version of the builder sets up the first operand as a
00235 /// destination virtual register.
00236 ///
00237 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00238                                    DebugLoc DL,
00239                                    const MCInstrDesc &MCID,
00240                                    unsigned DestReg) {
00241   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
00242            .addReg(DestReg, RegState::Define);
00243 }
00244 
00245 /// BuildMI - This version of the builder inserts the newly-built
00246 /// instruction before the given position in the given MachineBasicBlock, and
00247 /// sets up the first operand as a destination virtual register.
00248 ///
00249 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00250                                    MachineBasicBlock::iterator I,
00251                                    DebugLoc DL,
00252                                    const MCInstrDesc &MCID,
00253                                    unsigned DestReg) {
00254   MachineFunction &MF = *BB.getParent();
00255   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
00256   BB.insert(I, MI);
00257   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
00258 }
00259 
00260 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00261                                    MachineBasicBlock::instr_iterator I,
00262                                    DebugLoc DL,
00263                                    const MCInstrDesc &MCID,
00264                                    unsigned DestReg) {
00265   MachineFunction &MF = *BB.getParent();
00266   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
00267   BB.insert(I, MI);
00268   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
00269 }
00270 
00271 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00272                                    MachineInstr *I,
00273                                    DebugLoc DL,
00274                                    const MCInstrDesc &MCID,
00275                                    unsigned DestReg) {
00276   if (I->isInsideBundle()) {
00277     MachineBasicBlock::instr_iterator MII = I;
00278     return BuildMI(BB, MII, DL, MCID, DestReg);
00279   }
00280 
00281   MachineBasicBlock::iterator MII = I;
00282   return BuildMI(BB, MII, DL, MCID, DestReg);
00283 }
00284 
00285 /// BuildMI - This version of the builder inserts the newly-built
00286 /// instruction before the given position in the given MachineBasicBlock, and
00287 /// does NOT take a destination register.
00288 ///
00289 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00290                                    MachineBasicBlock::iterator I,
00291                                    DebugLoc DL,
00292                                    const MCInstrDesc &MCID) {
00293   MachineFunction &MF = *BB.getParent();
00294   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
00295   BB.insert(I, MI);
00296   return MachineInstrBuilder(MF, MI);
00297 }
00298 
00299 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00300                                    MachineBasicBlock::instr_iterator I,
00301                                    DebugLoc DL,
00302                                    const MCInstrDesc &MCID) {
00303   MachineFunction &MF = *BB.getParent();
00304   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
00305   BB.insert(I, MI);
00306   return MachineInstrBuilder(MF, MI);
00307 }
00308 
00309 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00310                                    MachineInstr *I,
00311                                    DebugLoc DL,
00312                                    const MCInstrDesc &MCID) {
00313   if (I->isInsideBundle()) {
00314     MachineBasicBlock::instr_iterator MII = I;
00315     return BuildMI(BB, MII, DL, MCID);
00316   }
00317 
00318   MachineBasicBlock::iterator MII = I;
00319   return BuildMI(BB, MII, DL, MCID);
00320 }
00321 
00322 /// BuildMI - This version of the builder inserts the newly-built
00323 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
00324 /// destination register.
00325 ///
00326 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
00327                                    DebugLoc DL,
00328                                    const MCInstrDesc &MCID) {
00329   return BuildMI(*BB, BB->end(), DL, MCID);
00330 }
00331 
00332 /// BuildMI - This version of the builder inserts the newly-built
00333 /// instruction at the end of the given MachineBasicBlock, and sets up the first
00334 /// operand as a destination virtual register.
00335 ///
00336 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
00337                                    DebugLoc DL,
00338                                    const MCInstrDesc &MCID,
00339                                    unsigned DestReg) {
00340   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
00341 }
00342 
00343 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
00344 /// for either a value in a register or a register-indirect+offset
00345 /// address.  The convention is that a DBG_VALUE is indirect iff the
00346 /// second operand is an immediate.
00347 ///
00348 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00349                                    DebugLoc DL,
00350                                    const MCInstrDesc &MCID,
00351                                    bool IsIndirect,
00352                                    unsigned Reg,
00353                                    unsigned Offset,
00354                                    const MDNode *MD) {
00355   if (IsIndirect)
00356     return BuildMI(MF, DL, MCID)
00357       .addReg(Reg, RegState::Debug)
00358       .addImm(Offset)
00359       .addMetadata(MD);
00360   else {
00361     assert(Offset == 0 && "A direct address cannot have an offset.");
00362     return BuildMI(MF, DL, MCID)
00363       .addReg(Reg, RegState::Debug)
00364       .addReg(0U, RegState::Debug)
00365       .addMetadata(MD);
00366   }
00367 }
00368 
00369 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
00370 /// for either a value in a register or a register-indirect+offset
00371 /// address and inserts it at position I.
00372 ///
00373 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00374                                    MachineBasicBlock::iterator I,
00375                                    DebugLoc DL,
00376                                    const MCInstrDesc &MCID,
00377                                    bool IsIndirect,
00378                                    unsigned Reg,
00379                                    unsigned Offset,
00380                                    const MDNode *MD) {
00381   MachineFunction &MF = *BB.getParent();
00382   MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, MD);
00383   BB.insert(I, MI);
00384   return MachineInstrBuilder(MF, MI);
00385 }
00386 
00387 
00388 inline unsigned getDefRegState(bool B) {
00389   return B ? RegState::Define : 0;
00390 }
00391 inline unsigned getImplRegState(bool B) {
00392   return B ? RegState::Implicit : 0;
00393 }
00394 inline unsigned getKillRegState(bool B) {
00395   return B ? RegState::Kill : 0;
00396 }
00397 inline unsigned getDeadRegState(bool B) {
00398   return B ? RegState::Dead : 0;
00399 }
00400 inline unsigned getUndefRegState(bool B) {
00401   return B ? RegState::Undef : 0;
00402 }
00403 inline unsigned getInternalReadRegState(bool B) {
00404   return B ? RegState::InternalRead : 0;
00405 }
00406 inline unsigned getDebugRegState(bool B) {
00407   return B ? RegState::Debug : 0;
00408 }
00409 
00410 
00411 /// Helper class for constructing bundles of MachineInstrs.
00412 ///
00413 /// MIBundleBuilder can create a bundle from scratch by inserting new
00414 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
00415 /// existing MachineInstrs in a basic block.
00416 class MIBundleBuilder {
00417   MachineBasicBlock &MBB;
00418   MachineBasicBlock::instr_iterator Begin;
00419   MachineBasicBlock::instr_iterator End;
00420 
00421 public:
00422   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
00423   /// BB above the bundle or instruction at Pos.
00424   MIBundleBuilder(MachineBasicBlock &BB,
00425                   MachineBasicBlock::iterator Pos)
00426     : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
00427 
00428   /// Create a bundle from the sequence of instructions between B and E.
00429   MIBundleBuilder(MachineBasicBlock &BB,
00430                   MachineBasicBlock::iterator B,
00431                   MachineBasicBlock::iterator E)
00432     : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
00433     assert(B != E && "No instructions to bundle");
00434     ++B;
00435     while (B != E) {
00436       MachineInstr *MI = B;
00437       ++B;
00438       MI->bundleWithPred();
00439     }
00440   }
00441 
00442   /// Create an MIBundleBuilder representing an existing instruction or bundle
00443   /// that has MI as its head.
00444   explicit MIBundleBuilder(MachineInstr *MI)
00445     : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
00446 
00447   /// Return a reference to the basic block containing this bundle.
00448   MachineBasicBlock &getMBB() const { return MBB; }
00449 
00450   /// Return true if no instructions have been inserted in this bundle yet.
00451   /// Empty bundles aren't representable in a MachineBasicBlock.
00452   bool empty() const { return Begin == End; }
00453 
00454   /// Return an iterator to the first bundled instruction.
00455   MachineBasicBlock::instr_iterator begin() const { return Begin; }
00456 
00457   /// Return an iterator beyond the last bundled instruction.
00458   MachineBasicBlock::instr_iterator end() const { return End; }
00459 
00460   /// Insert MI into this bundle before I which must point to an instruction in
00461   /// the bundle, or end().
00462   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
00463                           MachineInstr *MI) {
00464     MBB.insert(I, MI);
00465     if (I == Begin) {
00466       if (!empty())
00467         MI->bundleWithSucc();
00468       Begin = MI;
00469       return *this;
00470     }
00471     if (I == End) {
00472       MI->bundleWithPred();
00473       return *this;
00474     }
00475     // MI was inserted in the middle of the bundle, so its neighbors' flags are
00476     // already fine. Update MI's bundle flags manually.
00477     MI->setFlag(MachineInstr::BundledPred);
00478     MI->setFlag(MachineInstr::BundledSucc);
00479     return *this;
00480   }
00481 
00482   /// Insert MI into MBB by prepending it to the instructions in the bundle.
00483   /// MI will become the first instruction in the bundle.
00484   MIBundleBuilder &prepend(MachineInstr *MI) {
00485     return insert(begin(), MI);
00486   }
00487 
00488   /// Insert MI into MBB by appending it to the instructions in the bundle.
00489   /// MI will become the last instruction in the bundle.
00490   MIBundleBuilder &append(MachineInstr *MI) {
00491     return insert(end(), MI);
00492   }
00493 };
00494 
00495 } // End llvm namespace
00496 
00497 #endif