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/CodeGen/MachineInstrBundle.h" 22 #include "llvm/Support/ErrorHandling.h" 23 24 namespace llvm { 25 26 class MCInstrDesc; 27 class MDNode; 28 29 namespace RegState { 30 enum { 31 Define = 0x2, 32 Implicit = 0x4, 33 Kill = 0x8, 34 Dead = 0x10, 35 Undef = 0x20, 36 EarlyClobber = 0x40, 37 Debug = 0x80, 38 InternalRead = 0x100, 39 DefineNoRead = Define | Undef, 40 ImplicitDefine = Implicit | Define, 41 ImplicitKill = Implicit | Kill 42 }; 43 } 44 45 class MachineInstrBuilder { 46 MachineFunction *MF; 47 MachineInstr *MI; 48 public: 49 MachineInstrBuilder() : MF(nullptr), MI(nullptr) {} 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 /// If conversion operators fail, use this method to get the MachineInstr 62 /// explicitly. 63 MachineInstr *getInstr() const { return MI; } 64 65 /// addReg - Add a new virtual register operand... 66 /// 67 const 68 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0, 69 unsigned SubReg = 0) const { 70 assert((flags & 0x1) == 0 && 71 "Passing in 'true' to addReg is forbidden! Use enums instead."); 72 MI->addOperand(*MF, MachineOperand::CreateReg(RegNo, 73 flags & RegState::Define, 74 flags & RegState::Implicit, 75 flags & RegState::Kill, 76 flags & RegState::Dead, 77 flags & RegState::Undef, 78 flags & RegState::EarlyClobber, 79 SubReg, 80 flags & RegState::Debug, 81 flags & RegState::InternalRead)); 82 return *this; 83 } 84 85 /// addImm - Add a new immediate operand. 86 /// 87 const MachineInstrBuilder &addImm(int64_t Val) const { 88 MI->addOperand(*MF, MachineOperand::CreateImm(Val)); 89 return *this; 90 } 91 92 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const { 93 MI->addOperand(*MF, MachineOperand::CreateCImm(Val)); 94 return *this; 95 } 96 97 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const { 98 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val)); 99 return *this; 100 } 101 102 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB, 103 unsigned char TargetFlags = 0) const { 104 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags)); 105 return *this; 106 } 107 108 const MachineInstrBuilder &addFrameIndex(int Idx) const { 109 MI->addOperand(*MF, MachineOperand::CreateFI(Idx)); 110 return *this; 111 } 112 113 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx, 114 int Offset = 0, 115 unsigned char TargetFlags = 0) const { 116 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags)); 117 return *this; 118 } 119 120 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0, 121 unsigned char TargetFlags = 0) const { 122 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset, 123 TargetFlags)); 124 return *this; 125 } 126 127 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx, 128 unsigned char TargetFlags = 0) const { 129 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags)); 130 return *this; 131 } 132 133 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV, 134 int64_t Offset = 0, 135 unsigned char TargetFlags = 0) const { 136 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags)); 137 return *this; 138 } 139 140 const MachineInstrBuilder &addExternalSymbol(const char *FnName, 141 unsigned char TargetFlags = 0) const { 142 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags)); 143 return *this; 144 } 145 146 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA, 147 int64_t Offset = 0, 148 unsigned char TargetFlags = 0) const { 149 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags)); 150 return *this; 151 } 152 153 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const { 154 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask)); 155 return *this; 156 } 157 158 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const { 159 MI->addMemOperand(*MF, MMO); 160 return *this; 161 } 162 163 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b, 164 MachineInstr::mmo_iterator e) const { 165 MI->setMemRefs(b, e); 166 return *this; 167 } 168 169 170 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const { 171 MI->addOperand(*MF, MO); 172 return *this; 173 } 174 175 const MachineInstrBuilder &addMetadata(const MDNode *MD) const { 176 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD)); 177 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable()) 178 : true) && 179 "first MDNode argument of a DBG_VALUE not a DIVariable"); 180 return *this; 181 } 182 183 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const { 184 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex)); 185 return *this; 186 } 187 188 const MachineInstrBuilder &addSym(MCSymbol *Sym) const { 189 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym)); 190 return *this; 191 } 192 193 const MachineInstrBuilder &setMIFlags(unsigned Flags) const { 194 MI->setFlags(Flags); 195 return *this; 196 } 197 198 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const { 199 MI->setFlag(Flag); 200 return *this; 201 } 202 203 // Add a displacement from an existing MachineOperand with an added offset. 204 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off, 205 unsigned char TargetFlags = 0) const { 206 switch (Disp.getType()) { 207 default: 208 llvm_unreachable("Unhandled operand type in addDisp()"); 209 case MachineOperand::MO_Immediate: 210 return addImm(Disp.getImm() + off); 211 case MachineOperand::MO_GlobalAddress: { 212 // If caller specifies new TargetFlags then use it, otherwise the 213 // default behavior is to copy the target flags from the existing 214 // MachineOperand. This means if the caller wants to clear the 215 // target flags it needs to do so explicitly. 216 if (TargetFlags) 217 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off, 218 TargetFlags); 219 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off, 220 Disp.getTargetFlags()); 221 } 222 } 223 } 224 225 /// Copy all the implicit operands from OtherMI onto this one. 226 const MachineInstrBuilder ©ImplicitOps(const MachineInstr *OtherMI) { 227 MI->copyImplicitOps(*MF, OtherMI); 228 return *this; 229 } 230 }; 231 232 /// BuildMI - Builder interface. Specify how to create the initial instruction 233 /// itself. 234 /// 235 inline MachineInstrBuilder BuildMI(MachineFunction &MF, 236 DebugLoc DL, 237 const MCInstrDesc &MCID) { 238 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL)); 239 } 240 241 /// BuildMI - This version of the builder sets up the first operand as a 242 /// destination virtual register. 243 /// 244 inline MachineInstrBuilder BuildMI(MachineFunction &MF, 245 DebugLoc DL, 246 const MCInstrDesc &MCID, 247 unsigned DestReg) { 248 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL)) 249 .addReg(DestReg, RegState::Define); 250 } 251 252 /// BuildMI - This version of the builder inserts the newly-built 253 /// instruction before the given position in the given MachineBasicBlock, and 254 /// sets up the first operand as a destination virtual register. 255 /// 256 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 257 MachineBasicBlock::iterator I, 258 DebugLoc DL, 259 const MCInstrDesc &MCID, 260 unsigned DestReg) { 261 MachineFunction &MF = *BB.getParent(); 262 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); 263 BB.insert(I, MI); 264 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define); 265 } 266 267 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 268 MachineBasicBlock::instr_iterator I, 269 DebugLoc DL, 270 const MCInstrDesc &MCID, 271 unsigned DestReg) { 272 MachineFunction &MF = *BB.getParent(); 273 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); 274 BB.insert(I, MI); 275 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define); 276 } 277 278 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 279 MachineInstr *I, 280 DebugLoc DL, 281 const MCInstrDesc &MCID, 282 unsigned DestReg) { 283 if (I->isInsideBundle()) { 284 MachineBasicBlock::instr_iterator MII = I; 285 return BuildMI(BB, MII, DL, MCID, DestReg); 286 } 287 288 MachineBasicBlock::iterator MII = I; 289 return BuildMI(BB, MII, DL, MCID, DestReg); 290 } 291 292 /// BuildMI - This version of the builder inserts the newly-built 293 /// instruction before the given position in the given MachineBasicBlock, and 294 /// does NOT take a destination register. 295 /// 296 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 297 MachineBasicBlock::iterator I, 298 DebugLoc DL, 299 const MCInstrDesc &MCID) { 300 MachineFunction &MF = *BB.getParent(); 301 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); 302 BB.insert(I, MI); 303 return MachineInstrBuilder(MF, MI); 304 } 305 306 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 307 MachineBasicBlock::instr_iterator I, 308 DebugLoc DL, 309 const MCInstrDesc &MCID) { 310 MachineFunction &MF = *BB.getParent(); 311 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); 312 BB.insert(I, MI); 313 return MachineInstrBuilder(MF, MI); 314 } 315 316 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 317 MachineInstr *I, 318 DebugLoc DL, 319 const MCInstrDesc &MCID) { 320 if (I->isInsideBundle()) { 321 MachineBasicBlock::instr_iterator MII = I; 322 return BuildMI(BB, MII, DL, MCID); 323 } 324 325 MachineBasicBlock::iterator MII = I; 326 return BuildMI(BB, MII, DL, MCID); 327 } 328 329 /// BuildMI - This version of the builder inserts the newly-built 330 /// instruction at the end of the given MachineBasicBlock, and does NOT take a 331 /// destination register. 332 /// 333 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 334 DebugLoc DL, 335 const MCInstrDesc &MCID) { 336 return BuildMI(*BB, BB->end(), DL, MCID); 337 } 338 339 /// BuildMI - This version of the builder inserts the newly-built 340 /// instruction at the end of the given MachineBasicBlock, and sets up the first 341 /// operand as a destination virtual register. 342 /// 343 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 344 DebugLoc DL, 345 const MCInstrDesc &MCID, 346 unsigned DestReg) { 347 return BuildMI(*BB, BB->end(), DL, MCID, DestReg); 348 } 349 350 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic 351 /// for either a value in a register or a register-indirect+offset 352 /// address. The convention is that a DBG_VALUE is indirect iff the 353 /// second operand is an immediate. 354 /// 355 inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, 356 const MCInstrDesc &MCID, bool IsIndirect, 357 unsigned Reg, unsigned Offset, 358 const MDNode *Variable, const MDNode *Expr) { 359 assert(isa<MDLocalVariable>(Variable) && "not a DIVariable"); 360 assert(cast<MDExpression>(Expr)->isValid() && "not a DIExpression"); 361 assert(cast<MDLocalVariable>(Variable)->isValidLocationForIntrinsic(DL) && 362 "Expected inlined-at fields to agree"); 363 if (IsIndirect) 364 return BuildMI(MF, DL, MCID) 365 .addReg(Reg, RegState::Debug) 366 .addImm(Offset) 367 .addMetadata(Variable) 368 .addMetadata(Expr); 369 else { 370 assert(Offset == 0 && "A direct address cannot have an offset."); 371 return BuildMI(MF, DL, MCID) 372 .addReg(Reg, RegState::Debug) 373 .addReg(0U, RegState::Debug) 374 .addMetadata(Variable) 375 .addMetadata(Expr); 376 } 377 } 378 379 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic 380 /// for either a value in a register or a register-indirect+offset 381 /// address and inserts it at position I. 382 /// 383 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 384 MachineBasicBlock::iterator I, DebugLoc DL, 385 const MCInstrDesc &MCID, bool IsIndirect, 386 unsigned Reg, unsigned Offset, 387 const MDNode *Variable, const MDNode *Expr) { 388 assert(isa<MDLocalVariable>(Variable) && "not a DIVariable"); 389 assert(cast<MDExpression>(Expr)->isValid() && "not a DIExpression"); 390 MachineFunction &MF = *BB.getParent(); 391 MachineInstr *MI = 392 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr); 393 BB.insert(I, MI); 394 return MachineInstrBuilder(MF, MI); 395 } 396 397 398 inline unsigned getDefRegState(bool B) { 399 return B ? RegState::Define : 0; 400 } 401 inline unsigned getImplRegState(bool B) { 402 return B ? RegState::Implicit : 0; 403 } 404 inline unsigned getKillRegState(bool B) { 405 return B ? RegState::Kill : 0; 406 } 407 inline unsigned getDeadRegState(bool B) { 408 return B ? RegState::Dead : 0; 409 } 410 inline unsigned getUndefRegState(bool B) { 411 return B ? RegState::Undef : 0; 412 } 413 inline unsigned getInternalReadRegState(bool B) { 414 return B ? RegState::InternalRead : 0; 415 } 416 inline unsigned getDebugRegState(bool B) { 417 return B ? RegState::Debug : 0; 418 } 419 420 421 /// Helper class for constructing bundles of MachineInstrs. 422 /// 423 /// MIBundleBuilder can create a bundle from scratch by inserting new 424 /// MachineInstrs one at a time, or it can create a bundle from a sequence of 425 /// existing MachineInstrs in a basic block. 426 class MIBundleBuilder { 427 MachineBasicBlock &MBB; 428 MachineBasicBlock::instr_iterator Begin; 429 MachineBasicBlock::instr_iterator End; 430 431 public: 432 /// Create an MIBundleBuilder that inserts instructions into a new bundle in 433 /// BB above the bundle or instruction at Pos. 434 MIBundleBuilder(MachineBasicBlock &BB, 435 MachineBasicBlock::iterator Pos) 436 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {} 437 438 /// Create a bundle from the sequence of instructions between B and E. 439 MIBundleBuilder(MachineBasicBlock &BB, 440 MachineBasicBlock::iterator B, 441 MachineBasicBlock::iterator E) 442 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) { 443 assert(B != E && "No instructions to bundle"); 444 ++B; 445 while (B != E) { 446 MachineInstr *MI = B; 447 ++B; 448 MI->bundleWithPred(); 449 } 450 } 451 452 /// Create an MIBundleBuilder representing an existing instruction or bundle 453 /// that has MI as its head. 454 explicit MIBundleBuilder(MachineInstr *MI) 455 : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {} 456 457 /// Return a reference to the basic block containing this bundle. 458 MachineBasicBlock &getMBB() const { return MBB; } 459 460 /// Return true if no instructions have been inserted in this bundle yet. 461 /// Empty bundles aren't representable in a MachineBasicBlock. 462 bool empty() const { return Begin == End; } 463 464 /// Return an iterator to the first bundled instruction. 465 MachineBasicBlock::instr_iterator begin() const { return Begin; } 466 467 /// Return an iterator beyond the last bundled instruction. 468 MachineBasicBlock::instr_iterator end() const { return End; } 469 470 /// Insert MI into this bundle before I which must point to an instruction in 471 /// the bundle, or end(). 472 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I, 473 MachineInstr *MI) { 474 MBB.insert(I, MI); 475 if (I == Begin) { 476 if (!empty()) 477 MI->bundleWithSucc(); 478 Begin = MI; 479 return *this; 480 } 481 if (I == End) { 482 MI->bundleWithPred(); 483 return *this; 484 } 485 // MI was inserted in the middle of the bundle, so its neighbors' flags are 486 // already fine. Update MI's bundle flags manually. 487 MI->setFlag(MachineInstr::BundledPred); 488 MI->setFlag(MachineInstr::BundledSucc); 489 return *this; 490 } 491 492 /// Insert MI into MBB by prepending it to the instructions in the bundle. 493 /// MI will become the first instruction in the bundle. 494 MIBundleBuilder &prepend(MachineInstr *MI) { 495 return insert(begin(), MI); 496 } 497 498 /// Insert MI into MBB by appending it to the instructions in the bundle. 499 /// MI will become the last instruction in the bundle. 500 MIBundleBuilder &append(MachineInstr *MI) { 501 return insert(end(), MI); 502 } 503 }; 504 505 } // End llvm namespace 506 507 #endif 508