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.
56 operator MachineInstr*() const { return MI; }
57 MachineInstr *operator->() const { return MI; }
58 operator MachineBasicBlock::iterator() const { return MI; }
60 /// If conversion operators fail, use this method to get the MachineInstr
62 MachineInstr *getInstr() const { return MI; }
64 /// Add a new virtual register operand.
65 const MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
66 unsigned SubReg = 0) const {
67 assert((flags & 0x1) == 0 &&
68 "Passing in 'true' to addReg is forbidden! Use enums instead.");
69 MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
70 flags & RegState::Define,
71 flags & RegState::Implicit,
72 flags & RegState::Kill,
73 flags & RegState::Dead,
74 flags & RegState::Undef,
75 flags & RegState::EarlyClobber,
77 flags & RegState::Debug,
78 flags & RegState::InternalRead));
82 /// Add a new immediate operand.
83 const MachineInstrBuilder &addImm(int64_t Val) const {
84 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
88 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
89 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
93 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
94 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
98 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
99 unsigned char TargetFlags = 0) const {
100 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
104 const MachineInstrBuilder &addFrameIndex(int Idx) const {
105 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
109 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
111 unsigned char TargetFlags = 0) const {
112 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
116 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
117 unsigned char TargetFlags = 0) const {
118 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
123 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
124 unsigned char TargetFlags = 0) const {
125 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
129 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
131 unsigned char TargetFlags = 0) const {
132 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
136 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
137 unsigned char TargetFlags = 0) const {
138 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
142 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
144 unsigned char TargetFlags = 0) const {
145 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
149 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
150 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
154 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
155 MI->addMemOperand(*MF, MMO);
159 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
160 MachineInstr::mmo_iterator e) const {
161 MI->setMemRefs(b, e);
166 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
167 MI->addOperand(*MF, MO);
171 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
172 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
173 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
175 "first MDNode argument of a DBG_VALUE not a variable");
179 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
180 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
184 const MachineInstrBuilder &addSym(MCSymbol *Sym,
185 unsigned char TargetFlags = 0) const {
186 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
190 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
195 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
200 // Add a displacement from an existing MachineOperand with an added offset.
201 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
202 unsigned char TargetFlags = 0) const {
203 switch (Disp.getType()) {
205 llvm_unreachable("Unhandled operand type in addDisp()");
206 case MachineOperand::MO_Immediate:
207 return addImm(Disp.getImm() + off);
208 case MachineOperand::MO_GlobalAddress: {
209 // If caller specifies new TargetFlags then use it, otherwise the
210 // default behavior is to copy the target flags from the existing
211 // MachineOperand. This means if the caller wants to clear the
212 // target flags it needs to do so explicitly.
214 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
216 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
217 Disp.getTargetFlags());
222 /// Copy all the implicit operands from OtherMI onto this one.
223 const MachineInstrBuilder ©ImplicitOps(const MachineInstr *OtherMI) {
224 MI->copyImplicitOps(*MF, OtherMI);
229 /// Builder interface. Specify how to create the initial instruction itself.
230 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
232 const MCInstrDesc &MCID) {
233 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
236 /// This version of the builder sets up the first operand as a
237 /// destination virtual register.
238 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
240 const MCInstrDesc &MCID,
242 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
243 .addReg(DestReg, RegState::Define);
246 /// This version of the builder inserts the newly-built instruction before
247 /// the given position in the given MachineBasicBlock, and sets up the first
248 /// operand as a destination virtual register.
249 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
250 MachineBasicBlock::iterator I,
252 const MCInstrDesc &MCID,
254 MachineFunction &MF = *BB.getParent();
255 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
257 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
260 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
261 MachineBasicBlock::instr_iterator I,
263 const MCInstrDesc &MCID,
265 MachineFunction &MF = *BB.getParent();
266 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
268 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
271 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
274 const MCInstrDesc &MCID,
276 if (I->isInsideBundle()) {
277 MachineBasicBlock::instr_iterator MII(I);
278 return BuildMI(BB, MII, DL, MCID, DestReg);
281 MachineBasicBlock::iterator MII = I;
282 return BuildMI(BB, MII, DL, MCID, DestReg);
285 /// This version of the builder inserts the newly-built instruction before the
286 /// given position in the given MachineBasicBlock, and does NOT take a
287 /// destination register.
288 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
289 MachineBasicBlock::iterator I,
291 const MCInstrDesc &MCID) {
292 MachineFunction &MF = *BB.getParent();
293 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
295 return MachineInstrBuilder(MF, MI);
298 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
299 MachineBasicBlock::instr_iterator I,
301 const MCInstrDesc &MCID) {
302 MachineFunction &MF = *BB.getParent();
303 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
305 return MachineInstrBuilder(MF, MI);
308 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
311 const MCInstrDesc &MCID) {
312 if (I->isInsideBundle()) {
313 MachineBasicBlock::instr_iterator MII(I);
314 return BuildMI(BB, MII, DL, MCID);
317 MachineBasicBlock::iterator MII = I;
318 return BuildMI(BB, MII, DL, MCID);
321 /// This version of the builder inserts the newly-built instruction at the end
322 /// of the given MachineBasicBlock, and does NOT take a destination register.
323 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
325 const MCInstrDesc &MCID) {
326 return BuildMI(*BB, BB->end(), DL, MCID);
329 /// This version of the builder inserts the newly-built instruction at the
330 /// end of the given MachineBasicBlock, and sets up the first operand as a
331 /// destination virtual register.
332 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
334 const MCInstrDesc &MCID,
336 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
339 /// This version of the builder builds a DBG_VALUE intrinsic
340 /// for either a value in a register or a register-indirect+offset
341 /// address. The convention is that a DBG_VALUE is indirect iff the
342 /// second operand is an immediate.
343 inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL,
344 const MCInstrDesc &MCID, bool IsIndirect,
345 unsigned Reg, unsigned Offset,
346 const MDNode *Variable, const MDNode *Expr) {
347 assert(isa<DILocalVariable>(Variable) && "not a variable");
348 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
349 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
350 "Expected inlined-at fields to agree");
352 return BuildMI(MF, DL, MCID)
353 .addReg(Reg, RegState::Debug)
355 .addMetadata(Variable)
358 assert(Offset == 0 && "A direct address cannot have an offset.");
359 return BuildMI(MF, DL, MCID)
360 .addReg(Reg, RegState::Debug)
361 .addReg(0U, RegState::Debug)
362 .addMetadata(Variable)
367 /// This version of the builder builds a DBG_VALUE intrinsic
368 /// for either a value in a register or a register-indirect+offset
369 /// address and inserts it at position I.
370 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
371 MachineBasicBlock::iterator I, DebugLoc DL,
372 const MCInstrDesc &MCID, bool IsIndirect,
373 unsigned Reg, unsigned Offset,
374 const MDNode *Variable, const MDNode *Expr) {
375 assert(isa<DILocalVariable>(Variable) && "not a variable");
376 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
377 MachineFunction &MF = *BB.getParent();
379 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
381 return MachineInstrBuilder(MF, MI);
385 inline unsigned getDefRegState(bool B) {
386 return B ? RegState::Define : 0;
388 inline unsigned getImplRegState(bool B) {
389 return B ? RegState::Implicit : 0;
391 inline unsigned getKillRegState(bool B) {
392 return B ? RegState::Kill : 0;
394 inline unsigned getDeadRegState(bool B) {
395 return B ? RegState::Dead : 0;
397 inline unsigned getUndefRegState(bool B) {
398 return B ? RegState::Undef : 0;
400 inline unsigned getInternalReadRegState(bool B) {
401 return B ? RegState::InternalRead : 0;
403 inline unsigned getDebugRegState(bool B) {
404 return B ? RegState::Debug : 0;
408 /// Helper class for constructing bundles of MachineInstrs.
410 /// MIBundleBuilder can create a bundle from scratch by inserting new
411 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
412 /// existing MachineInstrs in a basic block.
413 class MIBundleBuilder {
414 MachineBasicBlock &MBB;
415 MachineBasicBlock::instr_iterator Begin;
416 MachineBasicBlock::instr_iterator End;
419 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
420 /// BB above the bundle or instruction at Pos.
421 MIBundleBuilder(MachineBasicBlock &BB,
422 MachineBasicBlock::iterator Pos)
423 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
425 /// Create a bundle from the sequence of instructions between B and E.
426 MIBundleBuilder(MachineBasicBlock &BB,
427 MachineBasicBlock::iterator B,
428 MachineBasicBlock::iterator E)
429 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
430 assert(B != E && "No instructions to bundle");
433 MachineInstr *MI = B;
435 MI->bundleWithPred();
439 /// Create an MIBundleBuilder representing an existing instruction or bundle
440 /// that has MI as its head.
441 explicit MIBundleBuilder(MachineInstr *MI)
442 : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
444 /// Return a reference to the basic block containing this bundle.
445 MachineBasicBlock &getMBB() const { return MBB; }
447 /// Return true if no instructions have been inserted in this bundle yet.
448 /// Empty bundles aren't representable in a MachineBasicBlock.
449 bool empty() const { return Begin == End; }
451 /// Return an iterator to the first bundled instruction.
452 MachineBasicBlock::instr_iterator begin() const { return Begin; }
454 /// Return an iterator beyond the last bundled instruction.
455 MachineBasicBlock::instr_iterator end() const { return End; }
457 /// Insert MI into this bundle before I which must point to an instruction in
458 /// the bundle, or end().
459 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
464 MI->bundleWithSucc();
465 Begin = MI->getIterator();
469 MI->bundleWithPred();
472 // MI was inserted in the middle of the bundle, so its neighbors' flags are
473 // already fine. Update MI's bundle flags manually.
474 MI->setFlag(MachineInstr::BundledPred);
475 MI->setFlag(MachineInstr::BundledSucc);
479 /// Insert MI into MBB by prepending it to the instructions in the bundle.
480 /// MI will become the first instruction in the bundle.
481 MIBundleBuilder &prepend(MachineInstr *MI) {
482 return insert(begin(), MI);
485 /// Insert MI into MBB by appending it to the instructions in the bundle.
486 /// MI will become the last instruction in the bundle.
487 MIBundleBuilder &append(MachineInstr *MI) {
488 return insert(end(), MI);
492 } // End llvm namespace