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(0), MI(0) {}
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 &addSym(MCSymbol *Sym) const {
00177     MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym));
00178     return *this;
00179   }
00180 
00181   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
00182     MI->setFlags(Flags);
00183     return *this;
00184   }
00185 
00186   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
00187     MI->setFlag(Flag);
00188     return *this;
00189   }
00190 
00191   // Add a displacement from an existing MachineOperand with an added offset.
00192   const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
00193                                      unsigned char TargetFlags = 0) const {
00194     switch (Disp.getType()) {
00195       default:
00196         llvm_unreachable("Unhandled operand type in addDisp()");
00197       case MachineOperand::MO_Immediate:
00198         return addImm(Disp.getImm() + off);
00199       case MachineOperand::MO_GlobalAddress: {
00200         // If caller specifies new TargetFlags then use it, otherwise the
00201         // default behavior is to copy the target flags from the existing
00202         // MachineOperand. This means if the caller wants to clear the
00203         // target flags it needs to do so explicitly.
00204         if (TargetFlags)
00205           return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
00206                                   TargetFlags);
00207         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
00208                                 Disp.getTargetFlags());
00209       }
00210     }
00211   }
00212 
00213   /// Copy all the implicit operands from OtherMI onto this one.
00214   const MachineInstrBuilder &copyImplicitOps(const MachineInstr *OtherMI) {
00215     MI->copyImplicitOps(*MF, OtherMI);
00216     return *this;
00217   }
00218 };
00219 
00220 /// BuildMI - Builder interface.  Specify how to create the initial instruction
00221 /// itself.
00222 ///
00223 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00224                                    DebugLoc DL,
00225                                    const MCInstrDesc &MCID) {
00226   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
00227 }
00228 
00229 /// BuildMI - This version of the builder sets up the first operand as a
00230 /// destination virtual register.
00231 ///
00232 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00233                                    DebugLoc DL,
00234                                    const MCInstrDesc &MCID,
00235                                    unsigned DestReg) {
00236   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
00237            .addReg(DestReg, RegState::Define);
00238 }
00239 
00240 /// BuildMI - This version of the builder inserts the newly-built
00241 /// instruction before the given position in the given MachineBasicBlock, and
00242 /// sets up the first operand as a destination virtual register.
00243 ///
00244 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00245                                    MachineBasicBlock::iterator I,
00246                                    DebugLoc DL,
00247                                    const MCInstrDesc &MCID,
00248                                    unsigned DestReg) {
00249   MachineFunction &MF = *BB.getParent();
00250   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
00251   BB.insert(I, MI);
00252   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
00253 }
00254 
00255 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00256                                    MachineBasicBlock::instr_iterator I,
00257                                    DebugLoc DL,
00258                                    const MCInstrDesc &MCID,
00259                                    unsigned DestReg) {
00260   MachineFunction &MF = *BB.getParent();
00261   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
00262   BB.insert(I, MI);
00263   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
00264 }
00265 
00266 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00267                                    MachineInstr *I,
00268                                    DebugLoc DL,
00269                                    const MCInstrDesc &MCID,
00270                                    unsigned DestReg) {
00271   if (I->isInsideBundle()) {
00272     MachineBasicBlock::instr_iterator MII = I;
00273     return BuildMI(BB, MII, DL, MCID, DestReg);
00274   }
00275 
00276   MachineBasicBlock::iterator MII = I;
00277   return BuildMI(BB, MII, DL, MCID, DestReg);
00278 }
00279 
00280 /// BuildMI - This version of the builder inserts the newly-built
00281 /// instruction before the given position in the given MachineBasicBlock, and
00282 /// does NOT take a destination register.
00283 ///
00284 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00285                                    MachineBasicBlock::iterator I,
00286                                    DebugLoc DL,
00287                                    const MCInstrDesc &MCID) {
00288   MachineFunction &MF = *BB.getParent();
00289   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
00290   BB.insert(I, MI);
00291   return MachineInstrBuilder(MF, MI);
00292 }
00293 
00294 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00295                                    MachineBasicBlock::instr_iterator I,
00296                                    DebugLoc DL,
00297                                    const MCInstrDesc &MCID) {
00298   MachineFunction &MF = *BB.getParent();
00299   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
00300   BB.insert(I, MI);
00301   return MachineInstrBuilder(MF, MI);
00302 }
00303 
00304 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00305                                    MachineInstr *I,
00306                                    DebugLoc DL,
00307                                    const MCInstrDesc &MCID) {
00308   if (I->isInsideBundle()) {
00309     MachineBasicBlock::instr_iterator MII = I;
00310     return BuildMI(BB, MII, DL, MCID);
00311   }
00312 
00313   MachineBasicBlock::iterator MII = I;
00314   return BuildMI(BB, MII, DL, MCID);
00315 }
00316 
00317 /// BuildMI - This version of the builder inserts the newly-built
00318 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
00319 /// destination register.
00320 ///
00321 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
00322                                    DebugLoc DL,
00323                                    const MCInstrDesc &MCID) {
00324   return BuildMI(*BB, BB->end(), DL, MCID);
00325 }
00326 
00327 /// BuildMI - This version of the builder inserts the newly-built
00328 /// instruction at the end of the given MachineBasicBlock, and sets up the first
00329 /// operand as a destination virtual register.
00330 ///
00331 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
00332                                    DebugLoc DL,
00333                                    const MCInstrDesc &MCID,
00334                                    unsigned DestReg) {
00335   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
00336 }
00337 
00338 inline unsigned getDefRegState(bool B) {
00339   return B ? RegState::Define : 0;
00340 }
00341 inline unsigned getImplRegState(bool B) {
00342   return B ? RegState::Implicit : 0;
00343 }
00344 inline unsigned getKillRegState(bool B) {
00345   return B ? RegState::Kill : 0;
00346 }
00347 inline unsigned getDeadRegState(bool B) {
00348   return B ? RegState::Dead : 0;
00349 }
00350 inline unsigned getUndefRegState(bool B) {
00351   return B ? RegState::Undef : 0;
00352 }
00353 inline unsigned getInternalReadRegState(bool B) {
00354   return B ? RegState::InternalRead : 0;
00355 }
00356 inline unsigned getDebugRegState(bool B) {
00357   return B ? RegState::Debug : 0;
00358 }
00359 
00360 
00361 /// Helper class for constructing bundles of MachineInstrs.
00362 ///
00363 /// MIBundleBuilder can create a bundle from scratch by inserting new
00364 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
00365 /// existing MachineInstrs in a basic block.
00366 class MIBundleBuilder {
00367   MachineBasicBlock &MBB;
00368   MachineBasicBlock::instr_iterator Begin;
00369   MachineBasicBlock::instr_iterator End;
00370 
00371 public:
00372   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
00373   /// BB above the bundle or instruction at Pos.
00374   MIBundleBuilder(MachineBasicBlock &BB,
00375                   MachineBasicBlock::iterator Pos)
00376     : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
00377 
00378   /// Create a bundle from the sequence of instructions between B and E.
00379   MIBundleBuilder(MachineBasicBlock &BB,
00380                   MachineBasicBlock::iterator B,
00381                   MachineBasicBlock::iterator E)
00382     : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
00383     assert(B != E && "No instructions to bundle");
00384     ++B;
00385     while (B != E) {
00386       MachineInstr *MI = B;
00387       ++B;
00388       MI->bundleWithPred();
00389     }
00390   }
00391 
00392   /// Create an MIBundleBuilder representing an existing instruction or bundle
00393   /// that has MI as its head.
00394   explicit MIBundleBuilder(MachineInstr *MI)
00395     : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
00396 
00397   /// Return a reference to the basic block containing this bundle.
00398   MachineBasicBlock &getMBB() const { return MBB; }
00399 
00400   /// Return true if no instructions have been inserted in this bundle yet.
00401   /// Empty bundles aren't representable in a MachineBasicBlock.
00402   bool empty() const { return Begin == End; }
00403 
00404   /// Return an iterator to the first bundled instruction.
00405   MachineBasicBlock::instr_iterator begin() const { return Begin; }
00406 
00407   /// Return an iterator beyond the last bundled instruction.
00408   MachineBasicBlock::instr_iterator end() const { return End; }
00409 
00410   /// Insert MI into this bundle before I which must point to an instruction in
00411   /// the bundle, or end().
00412   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
00413                           MachineInstr *MI) {
00414     MBB.insert(I, MI);
00415     if (I == Begin) {
00416       if (!empty())
00417         MI->bundleWithSucc();
00418       Begin = MI;
00419       return *this;
00420     }
00421     if (I == End) {
00422       MI->bundleWithPred();
00423       return *this;
00424     }
00425     // MI was inserted in the middle of the bundle, so its neighbors' flags are
00426     // already fine. Update MI's bundle flags manually.
00427     MI->setFlag(MachineInstr::BundledPred);
00428     MI->setFlag(MachineInstr::BundledSucc);
00429     return *this;
00430   }
00431 
00432   /// Insert MI into MBB by prepending it to the instructions in the bundle.
00433   /// MI will become the first instruction in the bundle.
00434   MIBundleBuilder &prepend(MachineInstr *MI) {
00435     return insert(begin(), MI);
00436   }
00437 
00438   /// Insert MI into MBB by appending it to the instructions in the bundle.
00439   /// MI will become the last instruction in the bundle.
00440   MIBundleBuilder &append(MachineInstr *MI) {
00441     return insert(end(), MI);
00442   }
00443 };
00444 
00445 } // End llvm namespace
00446 
00447 #endif