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 new immediate operand.
87 const MachineInstrBuilder &addImm(int64_t Val) const {
88 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
92 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
93 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
97 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
98 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
102 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
103 unsigned char TargetFlags = 0) const {
104 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
108 const MachineInstrBuilder &addFrameIndex(int Idx) const {
109 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
113 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
115 unsigned char TargetFlags = 0) const {
116 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
120 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
121 unsigned char TargetFlags = 0) const {
122 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
127 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
128 unsigned char TargetFlags = 0) const {
129 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
133 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
135 unsigned char TargetFlags = 0) const {
136 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
140 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
141 unsigned char TargetFlags = 0) const {
142 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
146 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
148 unsigned char TargetFlags = 0) const {
149 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
153 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
154 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
158 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
159 MI->addMemOperand(*MF, MMO);
163 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
164 MachineInstr::mmo_iterator e) const {
165 MI->setMemRefs(b, e);
169 const MachineInstrBuilder &setMemRefs(std::pair<MachineInstr::mmo_iterator,
170 unsigned> MemOperandsRef) const {
171 MI->setMemRefs(MemOperandsRef);
175 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
176 MI->addOperand(*MF, MO);
180 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
181 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
182 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
184 "first MDNode argument of a DBG_VALUE not a variable");
188 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
189 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
193 const MachineInstrBuilder &addSym(MCSymbol *Sym,
194 unsigned char TargetFlags = 0) const {
195 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
199 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
204 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
209 // Add a displacement from an existing MachineOperand with an added offset.
210 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
211 unsigned char TargetFlags = 0) const {
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 (0 == TargetFlags)
217 TargetFlags = Disp.getTargetFlags();
219 switch (Disp.getType()) {
221 llvm_unreachable("Unhandled operand type in addDisp()");
222 case MachineOperand::MO_Immediate:
223 return addImm(Disp.getImm() + off);
224 case MachineOperand::MO_ConstantPoolIndex:
225 return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
227 case MachineOperand::MO_GlobalAddress:
228 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
233 /// Copy all the implicit operands from OtherMI onto this one.
234 const MachineInstrBuilder &
235 copyImplicitOps(const MachineInstr &OtherMI) const {
236 MI->copyImplicitOps(*MF, OtherMI);
241 /// Builder interface. Specify how to create the initial instruction itself.
242 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
243 const MCInstrDesc &MCID) {
244 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
247 /// This version of the builder sets up the first operand as a
248 /// destination virtual register.
249 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
250 const MCInstrDesc &MCID, unsigned DestReg) {
251 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
252 .addReg(DestReg, RegState::Define);
255 /// This version of the builder inserts the newly-built instruction before
256 /// the given position in the given MachineBasicBlock, and sets up the first
257 /// operand as a destination virtual register.
258 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
259 MachineBasicBlock::iterator I,
260 const DebugLoc &DL, const MCInstrDesc &MCID,
262 MachineFunction &MF = *BB.getParent();
263 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
265 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
268 /// This version of the builder inserts the newly-built instruction before
269 /// the given position in the given MachineBasicBlock, and sets up the first
270 /// operand as a destination virtual register.
272 /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
273 /// added to the same bundle.
274 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
275 MachineBasicBlock::instr_iterator I,
276 const DebugLoc &DL, const MCInstrDesc &MCID,
278 MachineFunction &MF = *BB.getParent();
279 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
281 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
284 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
285 const DebugLoc &DL, const MCInstrDesc &MCID,
287 // Calling the overload for instr_iterator is always correct. However, the
288 // definition is not available in headers, so inline the check.
289 if (I.isInsideBundle())
290 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
291 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
294 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
295 const DebugLoc &DL, const MCInstrDesc &MCID,
297 return BuildMI(BB, *I, DL, MCID, DestReg);
300 /// This version of the builder inserts the newly-built instruction before the
301 /// given position in the given MachineBasicBlock, and does NOT take a
302 /// destination register.
303 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
304 MachineBasicBlock::iterator I,
306 const MCInstrDesc &MCID) {
307 MachineFunction &MF = *BB.getParent();
308 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
310 return MachineInstrBuilder(MF, MI);
313 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
314 MachineBasicBlock::instr_iterator I,
316 const MCInstrDesc &MCID) {
317 MachineFunction &MF = *BB.getParent();
318 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
320 return MachineInstrBuilder(MF, MI);
323 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
325 const MCInstrDesc &MCID) {
326 // Calling the overload for instr_iterator is always correct. However, the
327 // definition is not available in headers, so inline the check.
328 if (I.isInsideBundle())
329 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
330 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
333 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
335 const MCInstrDesc &MCID) {
336 return BuildMI(BB, *I, DL, MCID);
339 /// This version of the builder inserts the newly-built instruction at the end
340 /// of the given MachineBasicBlock, and does NOT take a destination register.
341 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
342 const MCInstrDesc &MCID) {
343 return BuildMI(*BB, BB->end(), DL, MCID);
346 /// This version of the builder inserts the newly-built instruction at the
347 /// end of the given MachineBasicBlock, and sets up the first operand as a
348 /// destination virtual register.
349 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
350 const MCInstrDesc &MCID, unsigned DestReg) {
351 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
354 /// This version of the builder builds a DBG_VALUE intrinsic
355 /// for either a value in a register or a register-indirect+offset
356 /// address. The convention is that a DBG_VALUE is indirect iff the
357 /// second operand is an immediate.
358 MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
359 const MCInstrDesc &MCID, bool IsIndirect,
360 unsigned Reg, unsigned Offset,
361 const MDNode *Variable, const MDNode *Expr);
363 /// This version of the builder builds a DBG_VALUE intrinsic
364 /// for either a value in a register or a register-indirect+offset
365 /// address and inserts it at position I.
366 MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
367 MachineBasicBlock::iterator I, const DebugLoc &DL,
368 const MCInstrDesc &MCID, bool IsIndirect,
369 unsigned Reg, unsigned Offset,
370 const MDNode *Variable, const MDNode *Expr);
372 inline unsigned getDefRegState(bool B) {
373 return B ? RegState::Define : 0;
375 inline unsigned getImplRegState(bool B) {
376 return B ? RegState::Implicit : 0;
378 inline unsigned getKillRegState(bool B) {
379 return B ? RegState::Kill : 0;
381 inline unsigned getDeadRegState(bool B) {
382 return B ? RegState::Dead : 0;
384 inline unsigned getUndefRegState(bool B) {
385 return B ? RegState::Undef : 0;
387 inline unsigned getInternalReadRegState(bool B) {
388 return B ? RegState::InternalRead : 0;
390 inline unsigned getDebugRegState(bool B) {
391 return B ? RegState::Debug : 0;
394 /// Get all register state flags from machine operand \p RegOp.
395 inline unsigned getRegState(const MachineOperand &RegOp) {
396 assert(RegOp.isReg() && "Not a register operand");
397 return getDefRegState(RegOp.isDef()) |
398 getImplRegState(RegOp.isImplicit()) |
399 getKillRegState(RegOp.isKill()) |
400 getDeadRegState(RegOp.isDead()) |
401 getUndefRegState(RegOp.isUndef()) |
402 getInternalReadRegState(RegOp.isInternalRead()) |
403 getDebugRegState(RegOp.isDebug());
406 /// Helper class for constructing bundles of MachineInstrs.
408 /// MIBundleBuilder can create a bundle from scratch by inserting new
409 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
410 /// existing MachineInstrs in a basic block.
411 class MIBundleBuilder {
412 MachineBasicBlock &MBB;
413 MachineBasicBlock::instr_iterator Begin;
414 MachineBasicBlock::instr_iterator End;
417 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
418 /// BB above the bundle or instruction at Pos.
419 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
420 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
422 /// Create a bundle from the sequence of instructions between B and E.
423 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
424 MachineBasicBlock::iterator E)
425 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
426 assert(B != E && "No instructions to bundle");
429 MachineInstr &MI = *B;
435 /// Create an MIBundleBuilder representing an existing instruction or bundle
436 /// that has MI as its head.
437 explicit MIBundleBuilder(MachineInstr *MI)
438 : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(*MI)) {}
440 /// Return a reference to the basic block containing this bundle.
441 MachineBasicBlock &getMBB() const { return MBB; }
443 /// Return true if no instructions have been inserted in this bundle yet.
444 /// Empty bundles aren't representable in a MachineBasicBlock.
445 bool empty() const { return Begin == End; }
447 /// Return an iterator to the first bundled instruction.
448 MachineBasicBlock::instr_iterator begin() const { return Begin; }
450 /// Return an iterator beyond the last bundled instruction.
451 MachineBasicBlock::instr_iterator end() const { return End; }
453 /// Insert MI into this bundle before I which must point to an instruction in
454 /// the bundle, or end().
455 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
460 MI->bundleWithSucc();
461 Begin = MI->getIterator();
465 MI->bundleWithPred();
468 // MI was inserted in the middle of the bundle, so its neighbors' flags are
469 // already fine. Update MI's bundle flags manually.
470 MI->setFlag(MachineInstr::BundledPred);
471 MI->setFlag(MachineInstr::BundledSucc);
475 /// Insert MI into MBB by prepending it to the instructions in the bundle.
476 /// MI will become the first instruction in the bundle.
477 MIBundleBuilder &prepend(MachineInstr *MI) {
478 return insert(begin(), MI);
481 /// Insert MI into MBB by appending it to the instructions in the bundle.
482 /// MI will become the last instruction in the bundle.
483 MIBundleBuilder &append(MachineInstr *MI) {
484 return insert(end(), MI);
488 } // End llvm namespace