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 
00022 namespace llvm {
00023 
00024 class TargetInstrDesc;
00025 
00026 class MachineInstrBuilder {
00027   MachineInstr *MI;
00028 public:
00029   explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
00030 
00031   /// Allow automatic conversion to the machine instruction we are working on.
00032   ///
00033   operator MachineInstr*() const { return MI; }
00034   operator MachineBasicBlock::iterator() const { return MI; }
00035 
00036   /// addReg - Add a new virtual register operand...
00037   ///
00038   const
00039   MachineInstrBuilder &addReg(unsigned RegNo, bool isDef = false, 
00040                               bool isImp = false, bool isKill = false, 
00041                               bool isDead = false, unsigned SubReg = 0) const {
00042     MI->addOperand(MachineOperand::CreateReg(RegNo, isDef, isImp, isKill,
00043                                              isDead, SubReg));
00044     return *this;
00045   }
00046 
00047   /// addImm - Add a new immediate operand.
00048   ///
00049   const MachineInstrBuilder &addImm(int64_t Val) const {
00050     MI->addOperand(MachineOperand::CreateImm(Val));
00051     return *this;
00052   }
00053 
00054   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
00055     MI->addOperand(MachineOperand::CreateFPImm(Val));
00056     return *this;
00057   }
00058 
00059   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
00060     MI->addOperand(MachineOperand::CreateMBB(MBB));
00061     return *this;
00062   }
00063 
00064   const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
00065     MI->addOperand(MachineOperand::CreateFI(Idx));
00066     return *this;
00067   }
00068 
00069   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
00070                                                   int Offset = 0) const {
00071     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
00072     return *this;
00073   }
00074 
00075   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
00076     MI->addOperand(MachineOperand::CreateJTI(Idx));
00077     return *this;
00078   }
00079 
00080   const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
00081                                               int64_t Offset = 0) const {
00082     MI->addOperand(MachineOperand::CreateGA(GV, Offset));
00083     return *this;
00084   }
00085 
00086   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
00087                                                int64_t Offset = 0) const {
00088     MI->addOperand(MachineOperand::CreateES(FnName, Offset));
00089     return *this;
00090   }
00091 
00092   const MachineInstrBuilder &addMemOperand(const MachineMemOperand &MMO) const {
00093     MI->addMemOperand(*MI->getParent()->getParent(), MMO);
00094     return *this;
00095   }
00096 };
00097 
00098 /// BuildMI - Builder interface.  Specify how to create the initial instruction
00099 /// itself.
00100 ///
00101 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00102                                    const TargetInstrDesc &TID) {
00103   return MachineInstrBuilder(MF.CreateMachineInstr(TID,
00104                                                    DebugLoc::getUnknownLoc()));
00105 }
00106 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00107                                    DebugLoc DL,
00108                                    const TargetInstrDesc &TID) {
00109   return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
00110 }
00111 
00112 /// BuildMI - This version of the builder sets up the first operand as a
00113 /// destination virtual register.
00114 ///
00115 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00116                                    const TargetInstrDesc &TID,
00117                                    unsigned DestReg) {
00118   return MachineInstrBuilder(MF.CreateMachineInstr(TID,
00119                                                    DebugLoc::getUnknownLoc()))
00120             .addReg(DestReg, true);
00121 }
00122 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
00123                                    DebugLoc DL,
00124                                    const TargetInstrDesc &TID,
00125                                    unsigned DestReg) {
00126   return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
00127            .addReg(DestReg, true);
00128 }
00129 
00130 /// BuildMI - This version of the builder inserts the newly-built
00131 /// instruction before the given position in the given MachineBasicBlock, and
00132 /// sets up the first operand as a destination virtual register.
00133 ///
00134 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00135                                    MachineBasicBlock::iterator I,
00136                                    const TargetInstrDesc &TID,
00137                                    unsigned DestReg) {
00138   MachineInstr *MI =
00139     BB.getParent()->CreateMachineInstr(TID, DebugLoc::getUnknownLoc());
00140   BB.insert(I, MI);
00141   return MachineInstrBuilder(MI).addReg(DestReg, true);
00142 }
00143 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00144                                    MachineBasicBlock::iterator I,
00145                                    DebugLoc DL,
00146                                    const TargetInstrDesc &TID,
00147                                    unsigned DestReg) {
00148   MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
00149   BB.insert(I, MI);
00150   return MachineInstrBuilder(MI).addReg(DestReg, true);
00151 }
00152 
00153 /// BuildMI - This version of the builder inserts the newly-built
00154 /// instruction before the given position in the given MachineBasicBlock, and
00155 /// does NOT take a destination register.
00156 ///
00157 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00158                                    MachineBasicBlock::iterator I,
00159                                    const TargetInstrDesc &TID) {
00160   MachineInstr *MI =
00161     BB.getParent()->CreateMachineInstr(TID, DebugLoc::getUnknownLoc());
00162   BB.insert(I, MI);
00163   return MachineInstrBuilder(MI);
00164 }
00165 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
00166                                    MachineBasicBlock::iterator I,
00167                                    DebugLoc DL,
00168                                    const TargetInstrDesc &TID) {
00169   MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
00170   BB.insert(I, MI);
00171   return MachineInstrBuilder(MI);
00172 }
00173 
00174 /// BuildMI - This version of the builder inserts the newly-built
00175 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
00176 /// destination register.
00177 ///
00178 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
00179                                    const TargetInstrDesc &TID) {
00180   return BuildMI(*BB, BB->end(), TID);
00181 }
00182 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
00183                                    DebugLoc DL,
00184                                    const TargetInstrDesc &TID) {
00185   return BuildMI(*BB, BB->end(), DL, TID);
00186 }
00187 
00188 /// BuildMI - This version of the builder inserts the newly-built
00189 /// instruction at the end of the given MachineBasicBlock, and sets up the first
00190 /// operand as a destination virtual register. 
00191 ///
00192 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
00193                                    const TargetInstrDesc &TID,
00194                                    unsigned DestReg) {
00195   return BuildMI(*BB, BB->end(), TID, DestReg);
00196 }
00197 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
00198                                    DebugLoc DL,
00199                                    const TargetInstrDesc &TID,
00200                                    unsigned DestReg) {
00201   return BuildMI(*BB, BB->end(), DL, TID, DestReg);
00202 }
00203 
00204 } // End llvm namespace
00205 
00206 #endif