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(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
18 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBundle.h"
22 #include "llvm/Support/ErrorHandling.h"
39 DefineNoRead = Define | Undef,
40 ImplicitDefine = Implicit | Define,
41 ImplicitKill = Implicit | Kill
45 class MachineInstrBuilder {
49 MachineInstrBuilder() : MF(nullptr), MI(nullptr) {}
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) {}
55 /// Allow automatic conversion to the machine instruction we are working on.
57 operator MachineInstr*() const { return MI; }
58 MachineInstr *operator->() const { return MI; }
59 operator MachineBasicBlock::iterator() const { return MI; }
61 /// If conversion operators fail, use this method to get the MachineInstr
63 MachineInstr *getInstr() const { return MI; }
65 /// addReg - Add a new virtual register operand...
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,
80 flags & RegState::Debug,
81 flags & RegState::InternalRead));
85 /// addImm - 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);
170 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
171 MI->addOperand(*MF, MO);
175 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
176 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
177 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
179 "first MDNode argument of a DBG_VALUE not a variable");
183 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
184 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
188 const MachineInstrBuilder &addSym(MCSymbol *Sym,
189 unsigned char TargetFlags = 0) const {
190 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
194 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
199 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
204 // Add a displacement from an existing MachineOperand with an added offset.
205 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
206 unsigned char TargetFlags = 0) const {
207 switch (Disp.getType()) {
209 llvm_unreachable("Unhandled operand type in addDisp()");
210 case MachineOperand::MO_Immediate:
211 return addImm(Disp.getImm() + off);
212 case MachineOperand::MO_GlobalAddress: {
213 // If caller specifies new TargetFlags then use it, otherwise the
214 // default behavior is to copy the target flags from the existing
215 // MachineOperand. This means if the caller wants to clear the
216 // target flags it needs to do so explicitly.
218 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
220 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
221 Disp.getTargetFlags());
226 /// Copy all the implicit operands from OtherMI onto this one.
227 const MachineInstrBuilder ©ImplicitOps(const MachineInstr *OtherMI) {
228 MI->copyImplicitOps(*MF, OtherMI);
233 /// BuildMI - Builder interface. Specify how to create the initial instruction
236 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
238 const MCInstrDesc &MCID) {
239 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
242 /// BuildMI - This version of the builder sets up the first operand as a
243 /// destination virtual register.
245 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
247 const MCInstrDesc &MCID,
249 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
250 .addReg(DestReg, RegState::Define);
253 /// BuildMI - This version of the builder inserts the newly-built
254 /// instruction before the given position in the given MachineBasicBlock, and
255 /// sets up the first operand as a destination virtual register.
257 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
258 MachineBasicBlock::iterator I,
260 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 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
269 MachineBasicBlock::instr_iterator I,
271 const MCInstrDesc &MCID,
273 MachineFunction &MF = *BB.getParent();
274 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
276 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
279 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
282 const MCInstrDesc &MCID,
284 if (I->isInsideBundle()) {
285 MachineBasicBlock::instr_iterator MII = I;
286 return BuildMI(BB, MII, DL, MCID, DestReg);
289 MachineBasicBlock::iterator MII = I;
290 return BuildMI(BB, MII, DL, MCID, DestReg);
293 /// BuildMI - This version of the builder inserts the newly-built
294 /// instruction before the given position in the given MachineBasicBlock, and
295 /// does NOT take a destination register.
297 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
298 MachineBasicBlock::iterator I,
300 const MCInstrDesc &MCID) {
301 MachineFunction &MF = *BB.getParent();
302 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
304 return MachineInstrBuilder(MF, MI);
307 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
308 MachineBasicBlock::instr_iterator I,
310 const MCInstrDesc &MCID) {
311 MachineFunction &MF = *BB.getParent();
312 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
314 return MachineInstrBuilder(MF, MI);
317 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
320 const MCInstrDesc &MCID) {
321 if (I->isInsideBundle()) {
322 MachineBasicBlock::instr_iterator MII = I;
323 return BuildMI(BB, MII, DL, MCID);
326 MachineBasicBlock::iterator MII = I;
327 return BuildMI(BB, MII, DL, MCID);
330 /// BuildMI - This version of the builder inserts the newly-built
331 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
332 /// destination register.
334 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
336 const MCInstrDesc &MCID) {
337 return BuildMI(*BB, BB->end(), DL, MCID);
340 /// BuildMI - This version of the builder inserts the newly-built
341 /// instruction at the end of the given MachineBasicBlock, and sets up the first
342 /// operand as a destination virtual register.
344 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
346 const MCInstrDesc &MCID,
348 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
351 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
352 /// for either a value in a register or a register-indirect+offset
353 /// address. The convention is that a DBG_VALUE is indirect iff the
354 /// second operand is an immediate.
356 inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL,
357 const MCInstrDesc &MCID, bool IsIndirect,
358 unsigned Reg, unsigned Offset,
359 const MDNode *Variable, const MDNode *Expr) {
360 assert(isa<DILocalVariable>(Variable) && "not a variable");
361 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
362 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
363 "Expected inlined-at fields to agree");
365 return BuildMI(MF, DL, MCID)
366 .addReg(Reg, RegState::Debug)
368 .addMetadata(Variable)
371 assert(Offset == 0 && "A direct address cannot have an offset.");
372 return BuildMI(MF, DL, MCID)
373 .addReg(Reg, RegState::Debug)
374 .addReg(0U, RegState::Debug)
375 .addMetadata(Variable)
380 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
381 /// for either a value in a register or a register-indirect+offset
382 /// address and inserts it at position I.
384 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
385 MachineBasicBlock::iterator I, DebugLoc DL,
386 const MCInstrDesc &MCID, bool IsIndirect,
387 unsigned Reg, unsigned Offset,
388 const MDNode *Variable, const MDNode *Expr) {
389 assert(isa<DILocalVariable>(Variable) && "not a variable");
390 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
391 MachineFunction &MF = *BB.getParent();
393 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
395 return MachineInstrBuilder(MF, MI);
399 inline unsigned getDefRegState(bool B) {
400 return B ? RegState::Define : 0;
402 inline unsigned getImplRegState(bool B) {
403 return B ? RegState::Implicit : 0;
405 inline unsigned getKillRegState(bool B) {
406 return B ? RegState::Kill : 0;
408 inline unsigned getDeadRegState(bool B) {
409 return B ? RegState::Dead : 0;
411 inline unsigned getUndefRegState(bool B) {
412 return B ? RegState::Undef : 0;
414 inline unsigned getInternalReadRegState(bool B) {
415 return B ? RegState::InternalRead : 0;
417 inline unsigned getDebugRegState(bool B) {
418 return B ? RegState::Debug : 0;
422 /// Helper class for constructing bundles of MachineInstrs.
424 /// MIBundleBuilder can create a bundle from scratch by inserting new
425 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
426 /// existing MachineInstrs in a basic block.
427 class MIBundleBuilder {
428 MachineBasicBlock &MBB;
429 MachineBasicBlock::instr_iterator Begin;
430 MachineBasicBlock::instr_iterator End;
433 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
434 /// BB above the bundle or instruction at Pos.
435 MIBundleBuilder(MachineBasicBlock &BB,
436 MachineBasicBlock::iterator Pos)
437 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
439 /// Create a bundle from the sequence of instructions between B and E.
440 MIBundleBuilder(MachineBasicBlock &BB,
441 MachineBasicBlock::iterator B,
442 MachineBasicBlock::iterator E)
443 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
444 assert(B != E && "No instructions to bundle");
447 MachineInstr *MI = B;
449 MI->bundleWithPred();
453 /// Create an MIBundleBuilder representing an existing instruction or bundle
454 /// that has MI as its head.
455 explicit MIBundleBuilder(MachineInstr *MI)
456 : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
458 /// Return a reference to the basic block containing this bundle.
459 MachineBasicBlock &getMBB() const { return MBB; }
461 /// Return true if no instructions have been inserted in this bundle yet.
462 /// Empty bundles aren't representable in a MachineBasicBlock.
463 bool empty() const { return Begin == End; }
465 /// Return an iterator to the first bundled instruction.
466 MachineBasicBlock::instr_iterator begin() const { return Begin; }
468 /// Return an iterator beyond the last bundled instruction.
469 MachineBasicBlock::instr_iterator end() const { return End; }
471 /// Insert MI into this bundle before I which must point to an instruction in
472 /// the bundle, or end().
473 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
478 MI->bundleWithSucc();
483 MI->bundleWithPred();
486 // MI was inserted in the middle of the bundle, so its neighbors' flags are
487 // already fine. Update MI's bundle flags manually.
488 MI->setFlag(MachineInstr::BundledPred);
489 MI->setFlag(MachineInstr::BundledSucc);
493 /// Insert MI into MBB by prepending it to the instructions in the bundle.
494 /// MI will become the first instruction in the bundle.
495 MIBundleBuilder &prepend(MachineInstr *MI) {
496 return insert(begin(), MI);
499 /// Insert MI into MBB by appending it to the instructions in the bundle.
500 /// MI will become the last instruction in the bundle.
501 MIBundleBuilder &append(MachineInstr *MI) {
502 return insert(end(), MI);
506 } // End llvm namespace