1 //===- CodeGen/MachineInstrBuilder.h - Simplify creation of MIs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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:
13 // M = BuildMI(MBB, MI, DL, TII.get(X86::ADD8rr), Dst)
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineInstrBundle.h"
27 #include "llvm/CodeGen/MachineOperand.h"
28 #include "llvm/IR/InstrTypes.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/Support/ErrorHandling.h"
51 DefineNoRead = Define | Undef,
52 ImplicitDefine = Implicit | Define,
53 ImplicitKill = Implicit | Kill
56 } // end namespace RegState
58 class MachineInstrBuilder {
59 MachineFunction *MF = nullptr;
60 MachineInstr *MI = nullptr;
63 MachineInstrBuilder() = default;
65 /// Create a MachineInstrBuilder for manipulating an existing instruction.
66 /// F must be the machine function that was used to allocate I.
67 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
68 MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
71 /// Allow automatic conversion to the machine instruction we are working on.
72 operator MachineInstr*() const { return MI; }
73 MachineInstr *operator->() const { return MI; }
74 operator MachineBasicBlock::iterator() const { return MI; }
76 /// If conversion operators fail, use this method to get the MachineInstr
78 MachineInstr *getInstr() const { return MI; }
80 /// Add a new virtual register operand.
81 const MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
82 unsigned SubReg = 0) const {
83 assert((flags & 0x1) == 0 &&
84 "Passing in 'true' to addReg is forbidden! Use enums instead.");
85 MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
86 flags & RegState::Define,
87 flags & RegState::Implicit,
88 flags & RegState::Kill,
89 flags & RegState::Dead,
90 flags & RegState::Undef,
91 flags & RegState::EarlyClobber,
93 flags & RegState::Debug,
94 flags & RegState::InternalRead));
98 /// Add a virtual register definition operand.
99 const MachineInstrBuilder &addDef(unsigned RegNo, unsigned Flags = 0,
100 unsigned SubReg = 0) const {
101 return addReg(RegNo, Flags | RegState::Define, SubReg);
104 /// Add a virtual register use operand. It is an error for Flags to contain
105 /// `RegState::Define` when calling this function.
106 const MachineInstrBuilder &addUse(unsigned RegNo, unsigned Flags = 0,
107 unsigned SubReg = 0) const {
108 assert(!(Flags & RegState::Define) &&
109 "Misleading addUse defines register, use addReg instead.");
110 return addReg(RegNo, Flags, SubReg);
113 /// Add a new immediate operand.
114 const MachineInstrBuilder &addImm(int64_t Val) const {
115 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
119 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
120 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
124 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
125 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
129 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
130 unsigned char TargetFlags = 0) const {
131 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
135 const MachineInstrBuilder &addFrameIndex(int Idx) const {
136 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
140 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
142 unsigned char TargetFlags = 0) const {
143 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
147 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
148 unsigned char TargetFlags = 0) const {
149 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
154 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
155 unsigned char TargetFlags = 0) const {
156 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
160 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
162 unsigned char TargetFlags = 0) const {
163 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
167 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
168 unsigned char TargetFlags = 0) const {
169 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
173 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
175 unsigned char TargetFlags = 0) const {
176 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
180 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
181 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
185 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
186 MI->addMemOperand(*MF, MMO);
190 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
191 MachineInstr::mmo_iterator e) const {
192 MI->setMemRefs(b, e);
196 const MachineInstrBuilder &setMemRefs(std::pair<MachineInstr::mmo_iterator,
197 unsigned> MemOperandsRef) const {
198 MI->setMemRefs(MemOperandsRef);
202 const MachineInstrBuilder &add(const MachineOperand &MO) const {
203 MI->addOperand(*MF, MO);
207 const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const {
208 for (const MachineOperand &MO : MOs) {
209 MI->addOperand(*MF, MO);
214 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
215 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
216 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
218 "first MDNode argument of a DBG_VALUE not a variable");
222 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
223 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
227 const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
228 MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
232 const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
233 MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
237 const MachineInstrBuilder &addSym(MCSymbol *Sym,
238 unsigned char TargetFlags = 0) const {
239 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
243 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
248 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
253 // Add a displacement from an existing MachineOperand with an added offset.
254 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
255 unsigned char TargetFlags = 0) const {
256 // If caller specifies new TargetFlags then use it, otherwise the
257 // default behavior is to copy the target flags from the existing
258 // MachineOperand. This means if the caller wants to clear the
259 // target flags it needs to do so explicitly.
260 if (0 == TargetFlags)
261 TargetFlags = Disp.getTargetFlags();
263 switch (Disp.getType()) {
265 llvm_unreachable("Unhandled operand type in addDisp()");
266 case MachineOperand::MO_Immediate:
267 return addImm(Disp.getImm() + off);
268 case MachineOperand::MO_ConstantPoolIndex:
269 return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
271 case MachineOperand::MO_GlobalAddress:
272 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
277 /// Copy all the implicit operands from OtherMI onto this one.
278 const MachineInstrBuilder &
279 copyImplicitOps(const MachineInstr &OtherMI) const {
280 MI->copyImplicitOps(*MF, OtherMI);
285 /// Builder interface. Specify how to create the initial instruction itself.
286 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
287 const MCInstrDesc &MCID) {
288 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
291 /// This version of the builder sets up the first operand as a
292 /// destination virtual register.
293 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
294 const MCInstrDesc &MCID, unsigned DestReg) {
295 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
296 .addReg(DestReg, RegState::Define);
299 /// This version of the builder inserts the newly-built instruction before
300 /// the given position in the given MachineBasicBlock, and sets up the first
301 /// operand as a destination virtual register.
302 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
303 MachineBasicBlock::iterator I,
304 const DebugLoc &DL, const MCInstrDesc &MCID,
306 MachineFunction &MF = *BB.getParent();
307 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
309 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
312 /// This version of the builder inserts the newly-built instruction before
313 /// the given position in the given MachineBasicBlock, and sets up the first
314 /// operand as a destination virtual register.
316 /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
317 /// added to the same bundle.
318 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
319 MachineBasicBlock::instr_iterator I,
320 const DebugLoc &DL, const MCInstrDesc &MCID,
322 MachineFunction &MF = *BB.getParent();
323 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
325 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
328 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
329 const DebugLoc &DL, const MCInstrDesc &MCID,
331 // Calling the overload for instr_iterator is always correct. However, the
332 // definition is not available in headers, so inline the check.
333 if (I.isInsideBundle())
334 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
335 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
338 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
339 const DebugLoc &DL, const MCInstrDesc &MCID,
341 return BuildMI(BB, *I, DL, MCID, DestReg);
344 /// This version of the builder inserts the newly-built instruction before the
345 /// given position in the given MachineBasicBlock, and does NOT take a
346 /// destination register.
347 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
348 MachineBasicBlock::iterator I,
350 const MCInstrDesc &MCID) {
351 MachineFunction &MF = *BB.getParent();
352 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
354 return MachineInstrBuilder(MF, MI);
357 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
358 MachineBasicBlock::instr_iterator I,
360 const MCInstrDesc &MCID) {
361 MachineFunction &MF = *BB.getParent();
362 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
364 return MachineInstrBuilder(MF, MI);
367 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
369 const MCInstrDesc &MCID) {
370 // Calling the overload for instr_iterator is always correct. However, the
371 // definition is not available in headers, so inline the check.
372 if (I.isInsideBundle())
373 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
374 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
377 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
379 const MCInstrDesc &MCID) {
380 return BuildMI(BB, *I, DL, MCID);
383 /// This version of the builder inserts the newly-built instruction at the end
384 /// of the given MachineBasicBlock, and does NOT take a destination register.
385 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
386 const MCInstrDesc &MCID) {
387 return BuildMI(*BB, BB->end(), DL, MCID);
390 /// This version of the builder inserts the newly-built instruction at the
391 /// end of the given MachineBasicBlock, and sets up the first operand as a
392 /// destination virtual register.
393 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
394 const MCInstrDesc &MCID, unsigned DestReg) {
395 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
398 /// This version of the builder builds a DBG_VALUE intrinsic
399 /// for either a value in a register or a register-indirect+offset
400 /// address. The convention is that a DBG_VALUE is indirect iff the
401 /// second operand is an immediate.
402 MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
403 const MCInstrDesc &MCID, bool IsIndirect,
404 unsigned Reg, unsigned Offset,
405 const MDNode *Variable, const MDNode *Expr);
407 /// This version of the builder builds a DBG_VALUE intrinsic
408 /// for either a value in a register or a register-indirect+offset
409 /// address and inserts it at position I.
410 MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
411 MachineBasicBlock::iterator I, const DebugLoc &DL,
412 const MCInstrDesc &MCID, bool IsIndirect,
413 unsigned Reg, unsigned Offset,
414 const MDNode *Variable, const MDNode *Expr);
416 /// Clone a DBG_VALUE whose value has been spilled to FrameIndex.
417 MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB,
418 MachineBasicBlock::iterator I,
419 const MachineInstr &Orig, int FrameIndex);
421 inline unsigned getDefRegState(bool B) {
422 return B ? RegState::Define : 0;
424 inline unsigned getImplRegState(bool B) {
425 return B ? RegState::Implicit : 0;
427 inline unsigned getKillRegState(bool B) {
428 return B ? RegState::Kill : 0;
430 inline unsigned getDeadRegState(bool B) {
431 return B ? RegState::Dead : 0;
433 inline unsigned getUndefRegState(bool B) {
434 return B ? RegState::Undef : 0;
436 inline unsigned getInternalReadRegState(bool B) {
437 return B ? RegState::InternalRead : 0;
439 inline unsigned getDebugRegState(bool B) {
440 return B ? RegState::Debug : 0;
443 /// Get all register state flags from machine operand \p RegOp.
444 inline unsigned getRegState(const MachineOperand &RegOp) {
445 assert(RegOp.isReg() && "Not a register operand");
446 return getDefRegState(RegOp.isDef()) |
447 getImplRegState(RegOp.isImplicit()) |
448 getKillRegState(RegOp.isKill()) |
449 getDeadRegState(RegOp.isDead()) |
450 getUndefRegState(RegOp.isUndef()) |
451 getInternalReadRegState(RegOp.isInternalRead()) |
452 getDebugRegState(RegOp.isDebug());
455 /// Helper class for constructing bundles of MachineInstrs.
457 /// MIBundleBuilder can create a bundle from scratch by inserting new
458 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
459 /// existing MachineInstrs in a basic block.
460 class MIBundleBuilder {
461 MachineBasicBlock &MBB;
462 MachineBasicBlock::instr_iterator Begin;
463 MachineBasicBlock::instr_iterator End;
466 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
467 /// BB above the bundle or instruction at Pos.
468 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
469 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
471 /// Create a bundle from the sequence of instructions between B and E.
472 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
473 MachineBasicBlock::iterator E)
474 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
475 assert(B != E && "No instructions to bundle");
478 MachineInstr &MI = *B;
484 /// Create an MIBundleBuilder representing an existing instruction or bundle
485 /// that has MI as its head.
486 explicit MIBundleBuilder(MachineInstr *MI)
487 : MBB(*MI->getParent()), Begin(MI),
488 End(getBundleEnd(MI->getIterator())) {}
490 /// Return a reference to the basic block containing this bundle.
491 MachineBasicBlock &getMBB() const { return MBB; }
493 /// Return true if no instructions have been inserted in this bundle yet.
494 /// Empty bundles aren't representable in a MachineBasicBlock.
495 bool empty() const { return Begin == End; }
497 /// Return an iterator to the first bundled instruction.
498 MachineBasicBlock::instr_iterator begin() const { return Begin; }
500 /// Return an iterator beyond the last bundled instruction.
501 MachineBasicBlock::instr_iterator end() const { return End; }
503 /// Insert MI into this bundle before I which must point to an instruction in
504 /// the bundle, or end().
505 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
510 MI->bundleWithSucc();
511 Begin = MI->getIterator();
515 MI->bundleWithPred();
518 // MI was inserted in the middle of the bundle, so its neighbors' flags are
519 // already fine. Update MI's bundle flags manually.
520 MI->setFlag(MachineInstr::BundledPred);
521 MI->setFlag(MachineInstr::BundledSucc);
525 /// Insert MI into MBB by prepending it to the instructions in the bundle.
526 /// MI will become the first instruction in the bundle.
527 MIBundleBuilder &prepend(MachineInstr *MI) {
528 return insert(begin(), MI);
531 /// Insert MI into MBB by appending it to the instructions in the bundle.
532 /// MI will become the last instruction in the bundle.
533 MIBundleBuilder &append(MachineInstr *MI) {
534 return insert(end(), MI);
538 } // end namespace llvm
540 #endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H