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/CodeGen/TargetRegisterInfo.h"
29 #include "llvm/IR/InstrTypes.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/Support/ErrorHandling.h"
53 DefineNoRead = Define | Undef,
54 ImplicitDefine = Implicit | Define,
55 ImplicitKill = Implicit | Kill
58 } // end namespace RegState
60 class MachineInstrBuilder {
61 MachineFunction *MF = nullptr;
62 MachineInstr *MI = nullptr;
65 MachineInstrBuilder() = default;
67 /// Create a MachineInstrBuilder for manipulating an existing instruction.
68 /// F must be the machine function that was used to allocate I.
69 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
70 MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
73 /// Allow automatic conversion to the machine instruction we are working on.
74 operator MachineInstr*() const { return MI; }
75 MachineInstr *operator->() const { return MI; }
76 operator MachineBasicBlock::iterator() const { return MI; }
78 /// If conversion operators fail, use this method to get the MachineInstr
80 MachineInstr *getInstr() const { return MI; }
82 /// Add a new virtual register operand.
83 const MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
84 unsigned SubReg = 0) const {
85 assert((flags & 0x1) == 0 &&
86 "Passing in 'true' to addReg is forbidden! Use enums instead.");
87 MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
88 flags & RegState::Define,
89 flags & RegState::Implicit,
90 flags & RegState::Kill,
91 flags & RegState::Dead,
92 flags & RegState::Undef,
93 flags & RegState::EarlyClobber,
95 flags & RegState::Debug,
96 flags & RegState::InternalRead,
97 flags & RegState::Renamable));
101 /// Add a virtual register definition operand.
102 const MachineInstrBuilder &addDef(unsigned RegNo, unsigned Flags = 0,
103 unsigned SubReg = 0) const {
104 return addReg(RegNo, Flags | RegState::Define, SubReg);
107 /// Add a virtual register use operand. It is an error for Flags to contain
108 /// `RegState::Define` when calling this function.
109 const MachineInstrBuilder &addUse(unsigned RegNo, unsigned Flags = 0,
110 unsigned SubReg = 0) const {
111 assert(!(Flags & RegState::Define) &&
112 "Misleading addUse defines register, use addReg instead.");
113 return addReg(RegNo, Flags, SubReg);
116 /// Add a new immediate operand.
117 const MachineInstrBuilder &addImm(int64_t Val) const {
118 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
122 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
123 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
127 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
128 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
132 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
133 unsigned char TargetFlags = 0) const {
134 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
138 const MachineInstrBuilder &addFrameIndex(int Idx) const {
139 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
143 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
145 unsigned char TargetFlags = 0) const {
146 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
150 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
151 unsigned char TargetFlags = 0) const {
152 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
157 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
158 unsigned char TargetFlags = 0) const {
159 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
163 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
165 unsigned char TargetFlags = 0) const {
166 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
170 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
171 unsigned char TargetFlags = 0) const {
172 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
176 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
178 unsigned char TargetFlags = 0) const {
179 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
183 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
184 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
188 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
189 MI->addMemOperand(*MF, MMO);
193 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
194 MachineInstr::mmo_iterator e) const {
195 MI->setMemRefs(b, e);
199 const MachineInstrBuilder &setMemRefs(std::pair<MachineInstr::mmo_iterator,
200 unsigned> MemOperandsRef) const {
201 MI->setMemRefs(MemOperandsRef);
205 const MachineInstrBuilder &add(const MachineOperand &MO) const {
206 MI->addOperand(*MF, MO);
210 const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const {
211 for (const MachineOperand &MO : MOs) {
212 MI->addOperand(*MF, MO);
217 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
218 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
219 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
221 "first MDNode argument of a DBG_VALUE not a variable");
225 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
226 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
230 const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
231 MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
235 const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
236 MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
240 const MachineInstrBuilder &addSym(MCSymbol *Sym,
241 unsigned char TargetFlags = 0) const {
242 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
246 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
251 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
256 // Add a displacement from an existing MachineOperand with an added offset.
257 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
258 unsigned char TargetFlags = 0) const {
259 // If caller specifies new TargetFlags then use it, otherwise the
260 // default behavior is to copy the target flags from the existing
261 // MachineOperand. This means if the caller wants to clear the
262 // target flags it needs to do so explicitly.
263 if (0 == TargetFlags)
264 TargetFlags = Disp.getTargetFlags();
266 switch (Disp.getType()) {
268 llvm_unreachable("Unhandled operand type in addDisp()");
269 case MachineOperand::MO_Immediate:
270 return addImm(Disp.getImm() + off);
271 case MachineOperand::MO_ConstantPoolIndex:
272 return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
274 case MachineOperand::MO_GlobalAddress:
275 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
280 /// Copy all the implicit operands from OtherMI onto this one.
281 const MachineInstrBuilder &
282 copyImplicitOps(const MachineInstr &OtherMI) const {
283 MI->copyImplicitOps(*MF, OtherMI);
288 /// Builder interface. Specify how to create the initial instruction itself.
289 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
290 const MCInstrDesc &MCID) {
291 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
294 /// This version of the builder sets up the first operand as a
295 /// destination virtual register.
296 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
297 const MCInstrDesc &MCID, unsigned DestReg) {
298 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
299 .addReg(DestReg, RegState::Define);
302 /// This version of the builder inserts the newly-built instruction before
303 /// the given position in the given MachineBasicBlock, and sets up the first
304 /// operand as a destination virtual register.
305 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
306 MachineBasicBlock::iterator I,
307 const DebugLoc &DL, const MCInstrDesc &MCID,
309 MachineFunction &MF = *BB.getParent();
310 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
312 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
315 /// This version of the builder inserts the newly-built instruction before
316 /// the given position in the given MachineBasicBlock, and sets up the first
317 /// operand as a destination virtual register.
319 /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
320 /// added to the same bundle.
321 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
322 MachineBasicBlock::instr_iterator I,
323 const DebugLoc &DL, const MCInstrDesc &MCID,
325 MachineFunction &MF = *BB.getParent();
326 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
328 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
331 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
332 const DebugLoc &DL, const MCInstrDesc &MCID,
334 // Calling the overload for instr_iterator is always correct. However, the
335 // definition is not available in headers, so inline the check.
336 if (I.isInsideBundle())
337 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
338 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
341 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
342 const DebugLoc &DL, const MCInstrDesc &MCID,
344 return BuildMI(BB, *I, DL, MCID, DestReg);
347 /// This version of the builder inserts the newly-built instruction before the
348 /// given position in the given MachineBasicBlock, and does NOT take a
349 /// destination register.
350 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
351 MachineBasicBlock::iterator I,
353 const MCInstrDesc &MCID) {
354 MachineFunction &MF = *BB.getParent();
355 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
357 return MachineInstrBuilder(MF, MI);
360 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
361 MachineBasicBlock::instr_iterator I,
363 const MCInstrDesc &MCID) {
364 MachineFunction &MF = *BB.getParent();
365 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
367 return MachineInstrBuilder(MF, MI);
370 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
372 const MCInstrDesc &MCID) {
373 // Calling the overload for instr_iterator is always correct. However, the
374 // definition is not available in headers, so inline the check.
375 if (I.isInsideBundle())
376 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
377 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
380 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
382 const MCInstrDesc &MCID) {
383 return BuildMI(BB, *I, DL, MCID);
386 /// This version of the builder inserts the newly-built instruction at the end
387 /// of the given MachineBasicBlock, and does NOT take a destination register.
388 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
389 const MCInstrDesc &MCID) {
390 return BuildMI(*BB, BB->end(), DL, MCID);
393 /// This version of the builder inserts the newly-built instruction at the
394 /// end of the given MachineBasicBlock, and sets up the first operand as a
395 /// destination virtual register.
396 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
397 const MCInstrDesc &MCID, unsigned DestReg) {
398 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
401 /// This version of the builder builds a DBG_VALUE intrinsic
402 /// for either a value in a register or a register-indirect
403 /// address. The convention is that a DBG_VALUE is indirect iff the
404 /// second operand is an immediate.
405 MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
406 const MCInstrDesc &MCID, bool IsIndirect,
407 unsigned Reg, const MDNode *Variable,
410 /// This version of the builder builds a DBG_VALUE intrinsic
411 /// for either a value in a register or a register-indirect
412 /// address and inserts it at position I.
413 MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
414 MachineBasicBlock::iterator I, const DebugLoc &DL,
415 const MCInstrDesc &MCID, bool IsIndirect,
416 unsigned Reg, const MDNode *Variable,
419 /// Clone a DBG_VALUE whose value has been spilled to FrameIndex.
420 MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB,
421 MachineBasicBlock::iterator I,
422 const MachineInstr &Orig, int FrameIndex);
424 /// Update a DBG_VALUE whose value has been spilled to FrameIndex. Useful when
425 /// modifying an instruction in place while iterating over a basic block.
426 void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex);
428 inline unsigned getDefRegState(bool B) {
429 return B ? RegState::Define : 0;
431 inline unsigned getImplRegState(bool B) {
432 return B ? RegState::Implicit : 0;
434 inline unsigned getKillRegState(bool B) {
435 return B ? RegState::Kill : 0;
437 inline unsigned getDeadRegState(bool B) {
438 return B ? RegState::Dead : 0;
440 inline unsigned getUndefRegState(bool B) {
441 return B ? RegState::Undef : 0;
443 inline unsigned getInternalReadRegState(bool B) {
444 return B ? RegState::InternalRead : 0;
446 inline unsigned getDebugRegState(bool B) {
447 return B ? RegState::Debug : 0;
449 inline unsigned getRenamableRegState(bool B) {
450 return B ? RegState::Renamable : 0;
453 /// Get all register state flags from machine operand \p RegOp.
454 inline unsigned getRegState(const MachineOperand &RegOp) {
455 assert(RegOp.isReg() && "Not a register operand");
456 return getDefRegState(RegOp.isDef()) |
457 getImplRegState(RegOp.isImplicit()) |
458 getKillRegState(RegOp.isKill()) |
459 getDeadRegState(RegOp.isDead()) |
460 getUndefRegState(RegOp.isUndef()) |
461 getInternalReadRegState(RegOp.isInternalRead()) |
462 getDebugRegState(RegOp.isDebug()) |
463 getRenamableRegState(
464 TargetRegisterInfo::isPhysicalRegister(RegOp.getReg()) &&
465 RegOp.isRenamable());
468 /// Helper class for constructing bundles of MachineInstrs.
470 /// MIBundleBuilder can create a bundle from scratch by inserting new
471 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
472 /// existing MachineInstrs in a basic block.
473 class MIBundleBuilder {
474 MachineBasicBlock &MBB;
475 MachineBasicBlock::instr_iterator Begin;
476 MachineBasicBlock::instr_iterator End;
479 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
480 /// BB above the bundle or instruction at Pos.
481 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
482 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
484 /// Create a bundle from the sequence of instructions between B and E.
485 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
486 MachineBasicBlock::iterator E)
487 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
488 assert(B != E && "No instructions to bundle");
491 MachineInstr &MI = *B;
497 /// Create an MIBundleBuilder representing an existing instruction or bundle
498 /// that has MI as its head.
499 explicit MIBundleBuilder(MachineInstr *MI)
500 : MBB(*MI->getParent()), Begin(MI),
501 End(getBundleEnd(MI->getIterator())) {}
503 /// Return a reference to the basic block containing this bundle.
504 MachineBasicBlock &getMBB() const { return MBB; }
506 /// Return true if no instructions have been inserted in this bundle yet.
507 /// Empty bundles aren't representable in a MachineBasicBlock.
508 bool empty() const { return Begin == End; }
510 /// Return an iterator to the first bundled instruction.
511 MachineBasicBlock::instr_iterator begin() const { return Begin; }
513 /// Return an iterator beyond the last bundled instruction.
514 MachineBasicBlock::instr_iterator end() const { return End; }
516 /// Insert MI into this bundle before I which must point to an instruction in
517 /// the bundle, or end().
518 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
523 MI->bundleWithSucc();
524 Begin = MI->getIterator();
528 MI->bundleWithPred();
531 // MI was inserted in the middle of the bundle, so its neighbors' flags are
532 // already fine. Update MI's bundle flags manually.
533 MI->setFlag(MachineInstr::BundledPred);
534 MI->setFlag(MachineInstr::BundledSucc);
538 /// Insert MI into MBB by prepending it to the instructions in the bundle.
539 /// MI will become the first instruction in the bundle.
540 MIBundleBuilder &prepend(MachineInstr *MI) {
541 return insert(begin(), MI);
544 /// Insert MI into MBB by appending it to the instructions in the bundle.
545 /// MI will become the last instruction in the bundle.
546 MIBundleBuilder &append(MachineInstr *MI) {
547 return insert(end(), MI);
551 } // end namespace llvm
553 #endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H