1 //===-- CodeGen/MachineInstBuilder.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/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBundle.h"
24 #include "llvm/Support/ErrorHandling.h"
41 DefineNoRead = Define | Undef,
42 ImplicitDefine = Implicit | Define,
43 ImplicitKill = Implicit | Kill
47 class MachineInstrBuilder {
51 MachineInstrBuilder() : MF(nullptr), MI(nullptr) {}
53 /// Create a MachineInstrBuilder for manipulating an existing instruction.
54 /// F must be the machine function that was used to allocate I.
55 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
56 MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
59 /// Allow automatic conversion to the machine instruction we are working on.
60 operator MachineInstr*() const { return MI; }
61 MachineInstr *operator->() const { return MI; }
62 operator MachineBasicBlock::iterator() const { return MI; }
64 /// If conversion operators fail, use this method to get the MachineInstr
66 MachineInstr *getInstr() const { return MI; }
68 /// Add a new virtual register operand.
69 const MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
70 unsigned SubReg = 0) const {
71 assert((flags & 0x1) == 0 &&
72 "Passing in 'true' to addReg is forbidden! Use enums instead.");
73 MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
74 flags & RegState::Define,
75 flags & RegState::Implicit,
76 flags & RegState::Kill,
77 flags & RegState::Dead,
78 flags & RegState::Undef,
79 flags & RegState::EarlyClobber,
81 flags & RegState::Debug,
82 flags & RegState::InternalRead));
86 /// Add a virtual register definition operand.
87 const MachineInstrBuilder &addDef(unsigned RegNo, unsigned Flags = 0,
88 unsigned SubReg = 0) const {
89 return addReg(RegNo, Flags | RegState::Define, SubReg);
92 /// Add a virtual register use operand. It is an error for Flags to contain
93 /// `RegState::Define` when calling this function.
94 const MachineInstrBuilder &addUse(unsigned RegNo, unsigned Flags = 0,
95 unsigned SubReg = 0) const {
96 assert(!(Flags & RegState::Define) &&
97 "Misleading addUse defines register, use addReg instead.");
98 return addReg(RegNo, Flags, SubReg);
101 /// Add a new immediate operand.
102 const MachineInstrBuilder &addImm(int64_t Val) const {
103 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
107 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
108 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
112 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
113 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
117 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
118 unsigned char TargetFlags = 0) const {
119 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
123 const MachineInstrBuilder &addFrameIndex(int Idx) const {
124 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
128 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
130 unsigned char TargetFlags = 0) const {
131 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
135 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
136 unsigned char TargetFlags = 0) const {
137 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
142 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
143 unsigned char TargetFlags = 0) const {
144 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
148 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
150 unsigned char TargetFlags = 0) const {
151 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
155 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
156 unsigned char TargetFlags = 0) const {
157 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
161 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
163 unsigned char TargetFlags = 0) const {
164 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
168 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
169 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
173 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
174 MI->addMemOperand(*MF, MMO);
178 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
179 MachineInstr::mmo_iterator e) const {
180 MI->setMemRefs(b, e);
184 const MachineInstrBuilder &setMemRefs(std::pair<MachineInstr::mmo_iterator,
185 unsigned> MemOperandsRef) const {
186 MI->setMemRefs(MemOperandsRef);
190 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
191 MI->addOperand(*MF, MO);
195 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
196 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
197 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
199 "first MDNode argument of a DBG_VALUE not a variable");
203 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
204 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
208 const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
209 MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
213 const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
214 MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
218 const MachineInstrBuilder &addSym(MCSymbol *Sym,
219 unsigned char TargetFlags = 0) const {
220 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
224 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
229 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
234 // Add a displacement from an existing MachineOperand with an added offset.
235 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
236 unsigned char TargetFlags = 0) const {
237 // If caller specifies new TargetFlags then use it, otherwise the
238 // default behavior is to copy the target flags from the existing
239 // MachineOperand. This means if the caller wants to clear the
240 // target flags it needs to do so explicitly.
241 if (0 == TargetFlags)
242 TargetFlags = Disp.getTargetFlags();
244 switch (Disp.getType()) {
246 llvm_unreachable("Unhandled operand type in addDisp()");
247 case MachineOperand::MO_Immediate:
248 return addImm(Disp.getImm() + off);
249 case MachineOperand::MO_ConstantPoolIndex:
250 return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
252 case MachineOperand::MO_GlobalAddress:
253 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
258 /// Copy all the implicit operands from OtherMI onto this one.
259 const MachineInstrBuilder &
260 copyImplicitOps(const MachineInstr &OtherMI) const {
261 MI->copyImplicitOps(*MF, OtherMI);
266 /// Builder interface. Specify how to create the initial instruction itself.
267 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
268 const MCInstrDesc &MCID) {
269 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
272 /// This version of the builder sets up the first operand as a
273 /// destination virtual register.
274 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
275 const MCInstrDesc &MCID, unsigned DestReg) {
276 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
277 .addReg(DestReg, RegState::Define);
280 /// This version of the builder inserts the newly-built instruction before
281 /// the given position in the given MachineBasicBlock, and sets up the first
282 /// operand as a destination virtual register.
283 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
284 MachineBasicBlock::iterator I,
285 const DebugLoc &DL, const MCInstrDesc &MCID,
287 MachineFunction &MF = *BB.getParent();
288 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
290 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
293 /// This version of the builder inserts the newly-built instruction before
294 /// the given position in the given MachineBasicBlock, and sets up the first
295 /// operand as a destination virtual register.
297 /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
298 /// added to the same bundle.
299 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
300 MachineBasicBlock::instr_iterator I,
301 const DebugLoc &DL, const MCInstrDesc &MCID,
303 MachineFunction &MF = *BB.getParent();
304 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
306 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
309 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
310 const DebugLoc &DL, const MCInstrDesc &MCID,
312 // Calling the overload for instr_iterator is always correct. However, the
313 // definition is not available in headers, so inline the check.
314 if (I.isInsideBundle())
315 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
316 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
319 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
320 const DebugLoc &DL, const MCInstrDesc &MCID,
322 return BuildMI(BB, *I, DL, MCID, DestReg);
325 /// This version of the builder inserts the newly-built instruction before the
326 /// given position in the given MachineBasicBlock, and does NOT take a
327 /// destination register.
328 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
329 MachineBasicBlock::iterator I,
331 const MCInstrDesc &MCID) {
332 MachineFunction &MF = *BB.getParent();
333 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
335 return MachineInstrBuilder(MF, MI);
338 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
339 MachineBasicBlock::instr_iterator I,
341 const MCInstrDesc &MCID) {
342 MachineFunction &MF = *BB.getParent();
343 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
345 return MachineInstrBuilder(MF, MI);
348 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
350 const MCInstrDesc &MCID) {
351 // Calling the overload for instr_iterator is always correct. However, the
352 // definition is not available in headers, so inline the check.
353 if (I.isInsideBundle())
354 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
355 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
358 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
360 const MCInstrDesc &MCID) {
361 return BuildMI(BB, *I, DL, MCID);
364 /// This version of the builder inserts the newly-built instruction at the end
365 /// of the given MachineBasicBlock, and does NOT take a destination register.
366 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
367 const MCInstrDesc &MCID) {
368 return BuildMI(*BB, BB->end(), DL, MCID);
371 /// This version of the builder inserts the newly-built instruction at the
372 /// end of the given MachineBasicBlock, and sets up the first operand as a
373 /// destination virtual register.
374 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
375 const MCInstrDesc &MCID, unsigned DestReg) {
376 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
379 /// 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. The convention is that a DBG_VALUE is indirect iff the
382 /// second operand is an immediate.
383 MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
384 const MCInstrDesc &MCID, bool IsIndirect,
385 unsigned Reg, unsigned Offset,
386 const MDNode *Variable, const MDNode *Expr);
388 /// This version of the builder builds a DBG_VALUE intrinsic
389 /// for either a value in a register or a register-indirect+offset
390 /// address and inserts it at position I.
391 MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
392 MachineBasicBlock::iterator I, const DebugLoc &DL,
393 const MCInstrDesc &MCID, bool IsIndirect,
394 unsigned Reg, unsigned Offset,
395 const MDNode *Variable, const MDNode *Expr);
397 inline unsigned getDefRegState(bool B) {
398 return B ? RegState::Define : 0;
400 inline unsigned getImplRegState(bool B) {
401 return B ? RegState::Implicit : 0;
403 inline unsigned getKillRegState(bool B) {
404 return B ? RegState::Kill : 0;
406 inline unsigned getDeadRegState(bool B) {
407 return B ? RegState::Dead : 0;
409 inline unsigned getUndefRegState(bool B) {
410 return B ? RegState::Undef : 0;
412 inline unsigned getInternalReadRegState(bool B) {
413 return B ? RegState::InternalRead : 0;
415 inline unsigned getDebugRegState(bool B) {
416 return B ? RegState::Debug : 0;
419 /// Get all register state flags from machine operand \p RegOp.
420 inline unsigned getRegState(const MachineOperand &RegOp) {
421 assert(RegOp.isReg() && "Not a register operand");
422 return getDefRegState(RegOp.isDef()) |
423 getImplRegState(RegOp.isImplicit()) |
424 getKillRegState(RegOp.isKill()) |
425 getDeadRegState(RegOp.isDead()) |
426 getUndefRegState(RegOp.isUndef()) |
427 getInternalReadRegState(RegOp.isInternalRead()) |
428 getDebugRegState(RegOp.isDebug());
431 /// Helper class for constructing bundles of MachineInstrs.
433 /// MIBundleBuilder can create a bundle from scratch by inserting new
434 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
435 /// existing MachineInstrs in a basic block.
436 class MIBundleBuilder {
437 MachineBasicBlock &MBB;
438 MachineBasicBlock::instr_iterator Begin;
439 MachineBasicBlock::instr_iterator End;
442 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
443 /// BB above the bundle or instruction at Pos.
444 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
445 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
447 /// Create a bundle from the sequence of instructions between B and E.
448 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
449 MachineBasicBlock::iterator E)
450 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
451 assert(B != E && "No instructions to bundle");
454 MachineInstr &MI = *B;
460 /// Create an MIBundleBuilder representing an existing instruction or bundle
461 /// that has MI as its head.
462 explicit MIBundleBuilder(MachineInstr *MI)
463 : MBB(*MI->getParent()), Begin(MI),
464 End(getBundleEnd(MI->getIterator())) {}
466 /// Return a reference to the basic block containing this bundle.
467 MachineBasicBlock &getMBB() const { return MBB; }
469 /// Return true if no instructions have been inserted in this bundle yet.
470 /// Empty bundles aren't representable in a MachineBasicBlock.
471 bool empty() const { return Begin == End; }
473 /// Return an iterator to the first bundled instruction.
474 MachineBasicBlock::instr_iterator begin() const { return Begin; }
476 /// Return an iterator beyond the last bundled instruction.
477 MachineBasicBlock::instr_iterator end() const { return End; }
479 /// Insert MI into this bundle before I which must point to an instruction in
480 /// the bundle, or end().
481 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
486 MI->bundleWithSucc();
487 Begin = MI->getIterator();
491 MI->bundleWithPred();
494 // MI was inserted in the middle of the bundle, so its neighbors' flags are
495 // already fine. Update MI's bundle flags manually.
496 MI->setFlag(MachineInstr::BundledPred);
497 MI->setFlag(MachineInstr::BundledSucc);
501 /// Insert MI into MBB by prepending it to the instructions in the bundle.
502 /// MI will become the first instruction in the bundle.
503 MIBundleBuilder &prepend(MachineInstr *MI) {
504 return insert(begin(), MI);
507 /// Insert MI into MBB by appending it to the instructions in the bundle.
508 /// MI will become the last instruction in the bundle.
509 MIBundleBuilder &append(MachineInstr *MI) {
510 return insert(end(), MI);
514 } // End llvm namespace