MachineInstrBuilder.h revision 433cb080bac56b4fac56f7625e07e17b4824ce8a
1//===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file exposes a function named BuildMI, which is useful for dramatically 11// simplifying how MachineInstr's are created. It allows use of code like this: 12// 13// M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2); 14// 15//===----------------------------------------------------------------------===// 16 17#ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H 18#define LLVM_CODEGEN_MACHINEINSTRBUILDER_H 19 20#include "llvm/CodeGen/MachineFunction.h" 21#include "llvm/Support/ErrorHandling.h" 22 23namespace llvm { 24 25class MCInstrDesc; 26class MDNode; 27 28namespace RegState { 29 enum { 30 Define = 0x2, 31 Implicit = 0x4, 32 Kill = 0x8, 33 Dead = 0x10, 34 Undef = 0x20, 35 EarlyClobber = 0x40, 36 Debug = 0x80, 37 InternalRead = 0x100, 38 DefineNoRead = Define | Undef, 39 ImplicitDefine = Implicit | Define, 40 ImplicitKill = Implicit | Kill 41 }; 42} 43 44class MachineInstrBuilder { 45 MachineFunction *MF; 46 MachineInstr *MI; 47public: 48 MachineInstrBuilder() : MF(0), MI(0) {} 49 explicit MachineInstrBuilder(MachineInstr *mi) : MF(0), MI(mi) {} 50 51 /// Create a MachineInstrBuilder for manipulating an existing instruction. 52 /// F must be the machine function that was used to allocate I. 53 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {} 54 55 /// Allow automatic conversion to the machine instruction we are working on. 56 /// 57 operator MachineInstr*() const { return MI; } 58 MachineInstr *operator->() const { return MI; } 59 operator MachineBasicBlock::iterator() const { return MI; } 60 61 /// addReg - Add a new virtual register operand... 62 /// 63 const 64 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0, 65 unsigned SubReg = 0) const { 66 assert((flags & 0x1) == 0 && 67 "Passing in 'true' to addReg is forbidden! Use enums instead."); 68 MI->addOperand(MachineOperand::CreateReg(RegNo, 69 flags & RegState::Define, 70 flags & RegState::Implicit, 71 flags & RegState::Kill, 72 flags & RegState::Dead, 73 flags & RegState::Undef, 74 flags & RegState::EarlyClobber, 75 SubReg, 76 flags & RegState::Debug, 77 flags & RegState::InternalRead)); 78 return *this; 79 } 80 81 /// addImm - Add a new immediate operand. 82 /// 83 const MachineInstrBuilder &addImm(int64_t Val) const { 84 MI->addOperand(MachineOperand::CreateImm(Val)); 85 return *this; 86 } 87 88 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const { 89 MI->addOperand(MachineOperand::CreateCImm(Val)); 90 return *this; 91 } 92 93 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const { 94 MI->addOperand(MachineOperand::CreateFPImm(Val)); 95 return *this; 96 } 97 98 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB, 99 unsigned char TargetFlags = 0) const { 100 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags)); 101 return *this; 102 } 103 104 const MachineInstrBuilder &addFrameIndex(int Idx) const { 105 MI->addOperand(MachineOperand::CreateFI(Idx)); 106 return *this; 107 } 108 109 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx, 110 int Offset = 0, 111 unsigned char TargetFlags = 0) const { 112 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags)); 113 return *this; 114 } 115 116 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0, 117 unsigned char TargetFlags = 0) const { 118 MI->addOperand(MachineOperand::CreateTargetIndex(Idx, Offset, TargetFlags)); 119 return *this; 120 } 121 122 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx, 123 unsigned char TargetFlags = 0) const { 124 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags)); 125 return *this; 126 } 127 128 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV, 129 int64_t Offset = 0, 130 unsigned char TargetFlags = 0) const { 131 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags)); 132 return *this; 133 } 134 135 const MachineInstrBuilder &addExternalSymbol(const char *FnName, 136 unsigned char TargetFlags = 0) const { 137 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags)); 138 return *this; 139 } 140 141 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const { 142 MI->addOperand(MachineOperand::CreateRegMask(Mask)); 143 return *this; 144 } 145 146 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const { 147 MI->addMemOperand(*MI->getParent()->getParent(), MMO); 148 return *this; 149 } 150 151 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b, 152 MachineInstr::mmo_iterator e) const { 153 MI->setMemRefs(b, e); 154 return *this; 155 } 156 157 158 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const { 159 MI->addOperand(MO); 160 return *this; 161 } 162 163 const MachineInstrBuilder &addMetadata(const MDNode *MD) const { 164 MI->addOperand(MachineOperand::CreateMetadata(MD)); 165 return *this; 166 } 167 168 const MachineInstrBuilder &addSym(MCSymbol *Sym) const { 169 MI->addOperand(MachineOperand::CreateMCSymbol(Sym)); 170 return *this; 171 } 172 173 const MachineInstrBuilder &setMIFlags(unsigned Flags) const { 174 MI->setFlags(Flags); 175 return *this; 176 } 177 178 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const { 179 MI->setFlag(Flag); 180 return *this; 181 } 182 183 // Add a displacement from an existing MachineOperand with an added offset. 184 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off, 185 unsigned char TargetFlags = 0) const { 186 switch (Disp.getType()) { 187 default: 188 llvm_unreachable("Unhandled operand type in addDisp()"); 189 case MachineOperand::MO_Immediate: 190 return addImm(Disp.getImm() + off); 191 case MachineOperand::MO_GlobalAddress: { 192 // If caller specifies new TargetFlags then use it, otherwise the 193 // default behavior is to copy the target flags from the existing 194 // MachineOperand. This means if the caller wants to clear the 195 // target flags it needs to do so explicitly. 196 if (TargetFlags) 197 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off, 198 TargetFlags); 199 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off, 200 Disp.getTargetFlags()); 201 } 202 } 203 } 204}; 205 206/// BuildMI - Builder interface. Specify how to create the initial instruction 207/// itself. 208/// 209inline MachineInstrBuilder BuildMI(MachineFunction &MF, 210 DebugLoc DL, 211 const MCInstrDesc &MCID) { 212 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL)); 213} 214 215/// BuildMI - This version of the builder sets up the first operand as a 216/// destination virtual register. 217/// 218inline MachineInstrBuilder BuildMI(MachineFunction &MF, 219 DebugLoc DL, 220 const MCInstrDesc &MCID, 221 unsigned DestReg) { 222 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL)) 223 .addReg(DestReg, RegState::Define); 224} 225 226/// BuildMI - This version of the builder inserts the newly-built 227/// instruction before the given position in the given MachineBasicBlock, and 228/// sets up the first operand as a destination virtual register. 229/// 230inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 231 MachineBasicBlock::iterator I, 232 DebugLoc DL, 233 const MCInstrDesc &MCID, 234 unsigned DestReg) { 235 MachineFunction &MF = *BB.getParent(); 236 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); 237 BB.insert(I, MI); 238 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define); 239} 240 241inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 242 MachineBasicBlock::instr_iterator I, 243 DebugLoc DL, 244 const MCInstrDesc &MCID, 245 unsigned DestReg) { 246 MachineFunction &MF = *BB.getParent(); 247 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); 248 BB.insert(I, MI); 249 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define); 250} 251 252inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 253 MachineInstr *I, 254 DebugLoc DL, 255 const MCInstrDesc &MCID, 256 unsigned DestReg) { 257 if (I->isInsideBundle()) { 258 MachineBasicBlock::instr_iterator MII = I; 259 return BuildMI(BB, MII, DL, MCID, DestReg); 260 } 261 262 MachineBasicBlock::iterator MII = I; 263 return BuildMI(BB, MII, DL, MCID, DestReg); 264} 265 266/// BuildMI - This version of the builder inserts the newly-built 267/// instruction before the given position in the given MachineBasicBlock, and 268/// does NOT take a destination register. 269/// 270inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 271 MachineBasicBlock::iterator I, 272 DebugLoc DL, 273 const MCInstrDesc &MCID) { 274 MachineFunction &MF = *BB.getParent(); 275 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); 276 BB.insert(I, MI); 277 return MachineInstrBuilder(MF, MI); 278} 279 280inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 281 MachineBasicBlock::instr_iterator I, 282 DebugLoc DL, 283 const MCInstrDesc &MCID) { 284 MachineFunction &MF = *BB.getParent(); 285 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); 286 BB.insert(I, MI); 287 return MachineInstrBuilder(MF, MI); 288} 289 290inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 291 MachineInstr *I, 292 DebugLoc DL, 293 const MCInstrDesc &MCID) { 294 if (I->isInsideBundle()) { 295 MachineBasicBlock::instr_iterator MII = I; 296 return BuildMI(BB, MII, DL, MCID); 297 } 298 299 MachineBasicBlock::iterator MII = I; 300 return BuildMI(BB, MII, DL, MCID); 301} 302 303/// BuildMI - This version of the builder inserts the newly-built 304/// instruction at the end of the given MachineBasicBlock, and does NOT take a 305/// destination register. 306/// 307inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 308 DebugLoc DL, 309 const MCInstrDesc &MCID) { 310 return BuildMI(*BB, BB->end(), DL, MCID); 311} 312 313/// BuildMI - This version of the builder inserts the newly-built 314/// instruction at the end of the given MachineBasicBlock, and sets up the first 315/// operand as a destination virtual register. 316/// 317inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 318 DebugLoc DL, 319 const MCInstrDesc &MCID, 320 unsigned DestReg) { 321 return BuildMI(*BB, BB->end(), DL, MCID, DestReg); 322} 323 324inline unsigned getDefRegState(bool B) { 325 return B ? RegState::Define : 0; 326} 327inline unsigned getImplRegState(bool B) { 328 return B ? RegState::Implicit : 0; 329} 330inline unsigned getKillRegState(bool B) { 331 return B ? RegState::Kill : 0; 332} 333inline unsigned getDeadRegState(bool B) { 334 return B ? RegState::Dead : 0; 335} 336inline unsigned getUndefRegState(bool B) { 337 return B ? RegState::Undef : 0; 338} 339inline unsigned getInternalReadRegState(bool B) { 340 return B ? RegState::InternalRead : 0; 341} 342 343 344/// Helper class for constructing bundles of MachineInstrs. 345/// 346/// MIBundleBuilder can create a bundle from scratch by inserting new 347/// MachineInstrs one at a time, or it can create a bundle from a sequence of 348/// existing MachineInstrs in a basic block. 349class MIBundleBuilder { 350 MachineBasicBlock &MBB; 351 MachineBasicBlock::instr_iterator Begin; 352 MachineBasicBlock::instr_iterator End; 353 354public: 355 /// Create an MIBundleBuilder that inserts instructions into a new bundle in 356 /// BB above the bundle or instruction at Pos. 357 MIBundleBuilder(MachineBasicBlock &BB, 358 MachineBasicBlock::iterator Pos) 359 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {} 360 361 /// Create a bundle from the sequence of instructions between B and E. 362 MIBundleBuilder(MachineBasicBlock &BB, 363 MachineBasicBlock::iterator B, 364 MachineBasicBlock::iterator E) 365 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) { 366 assert(B != E && "No instructions to bundle"); 367 ++B; 368 while (B != E) { 369 MachineInstr *MI = B; 370 ++B; 371 MI->bundleWithPred(); 372 } 373 } 374 375 /// Create an MIBundleBuilder representing an existing instruction or bundle 376 /// that has MI as its head. 377 explicit MIBundleBuilder(MachineInstr *MI) 378 : MBB(*MI->getParent()), Begin(MI) { 379 MachineBasicBlock::iterator I = MI; 380 ++I; 381 End = I.getInstrIterator(); 382 } 383 384 /// Return a reference to the basic block containing this bundle. 385 MachineBasicBlock &getMBB() const { return MBB; } 386 387 /// Return true if no instructions have been inserted in this bundle yet. 388 /// Empty bundles aren't representable in a MachineBasicBlock. 389 bool empty() const { return Begin == End; } 390 391 /// Return an iterator to the first bundled instruction. 392 MachineBasicBlock::instr_iterator begin() const { return Begin; } 393 394 /// Return an iterator beyond the last bundled instruction. 395 MachineBasicBlock::instr_iterator end() const { return End; } 396 397 /// Insert MI into this bundle before I which must point to an instruction in 398 /// the bundle, or end(). 399 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I, 400 MachineInstr *MI) { 401 MBB.insert(I, MI); 402 if (I == Begin) { 403 if (!empty()) 404 MI->bundleWithSucc(); 405 Begin = MI; 406 return *this; 407 } 408 if (I == End) { 409 MI->bundleWithPred(); 410 return *this; 411 } 412 // MI was inserted in the middle of the bundle, so its neighbors' flags are 413 // already fine. Update MI's bundle flags manually. 414 MI->setFlag(MachineInstr::BundledPred); 415 MI->setFlag(MachineInstr::BundledSucc); 416 return *this; 417 } 418 419 /// Insert MI into MBB by prepending it to the instructions in the bundle. 420 /// MI will become the first instruction in the bundle. 421 MIBundleBuilder &prepend(MachineInstr *MI) { 422 return insert(begin(), MI); 423 } 424 425 /// Insert MI into MBB by appending it to the instructions in the bundle. 426 /// MI will become the last instruction in the bundle. 427 MIBundleBuilder &append(MachineInstr *MI) { 428 return insert(end(), MI); 429 } 430}; 431 432} // End llvm namespace 433 434#endif 435