LLVM API Documentation
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 namespace RegState { 00027 enum { 00028 Define = 0x2, 00029 Implicit = 0x4, 00030 Kill = 0x8, 00031 Dead = 0x10, 00032 Undef = 0x20, 00033 EarlyClobber = 0x40, 00034 ImplicitDefine = Implicit | Define, 00035 ImplicitKill = Implicit | Kill 00036 }; 00037 } 00038 00039 class MachineInstrBuilder { 00040 MachineInstr *MI; 00041 public: 00042 MachineInstrBuilder() : MI(0) {} 00043 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {} 00044 00045 /// Allow automatic conversion to the machine instruction we are working on. 00046 /// 00047 operator MachineInstr*() const { return MI; } 00048 operator MachineBasicBlock::iterator() const { return MI; } 00049 00050 /// addReg - Add a new virtual register operand... 00051 /// 00052 const 00053 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0, 00054 unsigned SubReg = 0) const { 00055 assert((flags & 0x1) == 0 && 00056 "Passing in 'true' to addReg is forbidden! Use enums instead."); 00057 MI->addOperand(MachineOperand::CreateReg(RegNo, 00058 flags & RegState::Define, 00059 flags & RegState::Implicit, 00060 flags & RegState::Kill, 00061 flags & RegState::Dead, 00062 flags & RegState::Undef, 00063 flags & RegState::EarlyClobber, 00064 SubReg)); 00065 return *this; 00066 } 00067 00068 /// addImm - Add a new immediate operand. 00069 /// 00070 const MachineInstrBuilder &addImm(int64_t Val) const { 00071 MI->addOperand(MachineOperand::CreateImm(Val)); 00072 return *this; 00073 } 00074 00075 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const { 00076 MI->addOperand(MachineOperand::CreateFPImm(Val)); 00077 return *this; 00078 } 00079 00080 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB, 00081 unsigned char TargetFlags = 0) const { 00082 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags)); 00083 return *this; 00084 } 00085 00086 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const { 00087 MI->addOperand(MachineOperand::CreateFI(Idx)); 00088 return *this; 00089 } 00090 00091 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx, 00092 int Offset = 0, 00093 unsigned char TargetFlags = 0) const { 00094 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags)); 00095 return *this; 00096 } 00097 00098 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx, 00099 unsigned char TargetFlags = 0) const { 00100 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags)); 00101 return *this; 00102 } 00103 00104 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV, 00105 int64_t Offset = 0, 00106 unsigned char TargetFlags = 0) const { 00107 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags)); 00108 return *this; 00109 } 00110 00111 const MachineInstrBuilder &addMetadata(MDNode *N, 00112 int64_t Offset = 0, 00113 unsigned char TargetFlags = 0) const { 00114 MI->addOperand(MachineOperand::CreateMDNode(N, Offset, TargetFlags)); 00115 return *this; 00116 } 00117 00118 const MachineInstrBuilder &addExternalSymbol(const char *FnName, 00119 int64_t Offset = 0, 00120 unsigned char TargetFlags = 0) const { 00121 MI->addOperand(MachineOperand::CreateES(FnName, Offset, TargetFlags)); 00122 return *this; 00123 } 00124 00125 const MachineInstrBuilder &addMemOperand(const MachineMemOperand &MMO) const { 00126 MI->addMemOperand(*MI->getParent()->getParent(), MMO); 00127 return *this; 00128 } 00129 00130 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const { 00131 MI->addOperand(MO); 00132 return *this; 00133 } 00134 }; 00135 00136 /// BuildMI - Builder interface. Specify how to create the initial instruction 00137 /// itself. 00138 /// 00139 inline MachineInstrBuilder BuildMI(MachineFunction &MF, 00140 DebugLoc DL, 00141 const TargetInstrDesc &TID) { 00142 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL)); 00143 } 00144 00145 /// BuildMI - This version of the builder sets up the first operand as a 00146 /// destination virtual register. 00147 /// 00148 inline MachineInstrBuilder BuildMI(MachineFunction &MF, 00149 DebugLoc DL, 00150 const TargetInstrDesc &TID, 00151 unsigned DestReg) { 00152 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL)) 00153 .addReg(DestReg, RegState::Define); 00154 } 00155 00156 /// BuildMI - This version of the builder inserts the newly-built 00157 /// instruction before the given position in the given MachineBasicBlock, and 00158 /// sets up the first operand as a destination virtual register. 00159 /// 00160 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 00161 MachineBasicBlock::iterator I, 00162 DebugLoc DL, 00163 const TargetInstrDesc &TID, 00164 unsigned DestReg) { 00165 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL); 00166 BB.insert(I, MI); 00167 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define); 00168 } 00169 00170 /// BuildMI - This version of the builder inserts the newly-built 00171 /// instruction before the given position in the given MachineBasicBlock, and 00172 /// does NOT take a destination register. 00173 /// 00174 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 00175 MachineBasicBlock::iterator I, 00176 DebugLoc DL, 00177 const TargetInstrDesc &TID) { 00178 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL); 00179 BB.insert(I, MI); 00180 return MachineInstrBuilder(MI); 00181 } 00182 00183 /// BuildMI - This version of the builder inserts the newly-built 00184 /// instruction at the end of the given MachineBasicBlock, and does NOT take a 00185 /// destination register. 00186 /// 00187 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 00188 DebugLoc DL, 00189 const TargetInstrDesc &TID) { 00190 return BuildMI(*BB, BB->end(), DL, TID); 00191 } 00192 00193 /// BuildMI - This version of the builder inserts the newly-built 00194 /// instruction at the end of the given MachineBasicBlock, and sets up the first 00195 /// operand as a destination virtual register. 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 inline unsigned getDefRegState(bool B) { 00205 return B ? RegState::Define : 0; 00206 } 00207 inline unsigned getImplRegState(bool B) { 00208 return B ? RegState::Implicit : 0; 00209 } 00210 inline unsigned getKillRegState(bool B) { 00211 return B ? RegState::Kill : 0; 00212 } 00213 inline unsigned getDeadRegState(bool B) { 00214 return B ? RegState::Dead : 0; 00215 } 00216 inline unsigned getUndefRegState(bool B) { 00217 return B ? RegState::Undef : 0; 00218 } 00219 00220 } // End llvm namespace 00221 00222 #endif