1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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:
12 //
13 // M = BuildMI(MBB, MI, DL, TII.get(X86::ADD8rr), Dst)
14 // .addReg(argVal1)
15 // .addReg(argVal2);
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
21
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBundle.h"
24 #include "llvm/Support/ErrorHandling.h"
25
26 namespace llvm {
27
28 class MCInstrDesc;
29 class MDNode;
30
31 namespace RegState {
32 enum {
33 Define = 0x2,
34 Implicit = 0x4,
35 Kill = 0x8,
36 Dead = 0x10,
37 Undef = 0x20,
38 EarlyClobber = 0x40,
39 Debug = 0x80,
40 InternalRead = 0x100,
41 DefineNoRead = Define | Undef,
42 ImplicitDefine = Implicit | Define,
43 ImplicitKill = Implicit | Kill
44 };
45 }
46
47 class MachineInstrBuilder {
48 MachineFunction *MF;
49 MachineInstr *MI;
50 public:
MachineInstrBuilder()51 MachineInstrBuilder() : MF(nullptr), MI(nullptr) {}
52
53 /// Create a MachineInstrBuilder for manipulating an existing instruction.
54 /// F must be the machine function that was used to allocate I.
MachineInstrBuilder(MachineFunction & F,MachineInstr * I)55 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
MachineInstrBuilder(MachineFunction & F,MachineBasicBlock::iterator I)56 MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
57 : MF(&F), MI(&*I) {}
58
59 /// Allow automatic conversion to the machine instruction we are working on.
60 operator MachineInstr*() const { return MI; }
61 MachineInstr *operator->() const { return MI; }
iterator()62 operator MachineBasicBlock::iterator() const { return MI; }
63
64 /// If conversion operators fail, use this method to get the MachineInstr
65 /// explicitly.
getInstr()66 MachineInstr *getInstr() const { return MI; }
67
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,
80 SubReg,
81 flags & RegState::Debug,
82 flags & RegState::InternalRead));
83 return *this;
84 }
85
86 /// Add a new immediate operand.
addImm(int64_t Val)87 const MachineInstrBuilder &addImm(int64_t Val) const {
88 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
89 return *this;
90 }
91
addCImm(const ConstantInt * Val)92 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
93 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
94 return *this;
95 }
96
addFPImm(const ConstantFP * Val)97 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
98 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
99 return *this;
100 }
101
102 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
103 unsigned char TargetFlags = 0) const {
104 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
105 return *this;
106 }
107
addFrameIndex(int Idx)108 const MachineInstrBuilder &addFrameIndex(int Idx) const {
109 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
110 return *this;
111 }
112
113 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
114 int Offset = 0,
115 unsigned char TargetFlags = 0) const {
116 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
117 return *this;
118 }
119
120 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
121 unsigned char TargetFlags = 0) const {
122 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
123 TargetFlags));
124 return *this;
125 }
126
127 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
128 unsigned char TargetFlags = 0) const {
129 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
130 return *this;
131 }
132
133 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
134 int64_t Offset = 0,
135 unsigned char TargetFlags = 0) const {
136 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
137 return *this;
138 }
139
140 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
141 unsigned char TargetFlags = 0) const {
142 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
143 return *this;
144 }
145
146 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
147 int64_t Offset = 0,
148 unsigned char TargetFlags = 0) const {
149 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
150 return *this;
151 }
152
addRegMask(const uint32_t * Mask)153 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
154 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
155 return *this;
156 }
157
addMemOperand(MachineMemOperand * MMO)158 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
159 MI->addMemOperand(*MF, MMO);
160 return *this;
161 }
162
setMemRefs(MachineInstr::mmo_iterator b,MachineInstr::mmo_iterator e)163 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
164 MachineInstr::mmo_iterator e) const {
165 MI->setMemRefs(b, e);
166 return *this;
167 }
168
setMemRefs(std::pair<MachineInstr::mmo_iterator,unsigned> MemOperandsRef)169 const MachineInstrBuilder &setMemRefs(std::pair<MachineInstr::mmo_iterator,
170 unsigned> MemOperandsRef) const {
171 MI->setMemRefs(MemOperandsRef);
172 return *this;
173 }
174
addOperand(const MachineOperand & MO)175 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
176 MI->addOperand(*MF, MO);
177 return *this;
178 }
179
addMetadata(const MDNode * MD)180 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
181 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
182 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
183 : true) &&
184 "first MDNode argument of a DBG_VALUE not a variable");
185 return *this;
186 }
187
addCFIIndex(unsigned CFIIndex)188 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
189 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
190 return *this;
191 }
192
193 const MachineInstrBuilder &addSym(MCSymbol *Sym,
194 unsigned char TargetFlags = 0) const {
195 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
196 return *this;
197 }
198
setMIFlags(unsigned Flags)199 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
200 MI->setFlags(Flags);
201 return *this;
202 }
203
setMIFlag(MachineInstr::MIFlag Flag)204 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
205 MI->setFlag(Flag);
206 return *this;
207 }
208
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();
218
219 switch (Disp.getType()) {
220 default:
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,
226 TargetFlags);
227 case MachineOperand::MO_GlobalAddress:
228 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
229 TargetFlags);
230 }
231 }
232
233 /// Copy all the implicit operands from OtherMI onto this one.
234 const MachineInstrBuilder &
copyImplicitOps(const MachineInstr & OtherMI)235 copyImplicitOps(const MachineInstr &OtherMI) const {
236 MI->copyImplicitOps(*MF, OtherMI);
237 return *this;
238 }
239 };
240
241 /// Builder interface. Specify how to create the initial instruction itself.
BuildMI(MachineFunction & MF,const DebugLoc & DL,const MCInstrDesc & MCID)242 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
243 const MCInstrDesc &MCID) {
244 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
245 }
246
247 /// This version of the builder sets up the first operand as a
248 /// destination virtual register.
BuildMI(MachineFunction & MF,const DebugLoc & DL,const MCInstrDesc & MCID,unsigned DestReg)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);
253 }
254
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.
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::iterator I,const DebugLoc & DL,const MCInstrDesc & MCID,unsigned DestReg)258 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
259 MachineBasicBlock::iterator I,
260 const DebugLoc &DL, const MCInstrDesc &MCID,
261 unsigned DestReg) {
262 MachineFunction &MF = *BB.getParent();
263 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
264 BB.insert(I, MI);
265 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
266 }
267
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.
271 ///
272 /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
273 /// added to the same bundle.
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::instr_iterator I,const DebugLoc & DL,const MCInstrDesc & MCID,unsigned DestReg)274 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
275 MachineBasicBlock::instr_iterator I,
276 const DebugLoc &DL, const MCInstrDesc &MCID,
277 unsigned DestReg) {
278 MachineFunction &MF = *BB.getParent();
279 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
280 BB.insert(I, MI);
281 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
282 }
283
BuildMI(MachineBasicBlock & BB,MachineInstr & I,const DebugLoc & DL,const MCInstrDesc & MCID,unsigned DestReg)284 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
285 const DebugLoc &DL, const MCInstrDesc &MCID,
286 unsigned DestReg) {
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);
292 }
293
BuildMI(MachineBasicBlock & BB,MachineInstr * I,const DebugLoc & DL,const MCInstrDesc & MCID,unsigned DestReg)294 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
295 const DebugLoc &DL, const MCInstrDesc &MCID,
296 unsigned DestReg) {
297 return BuildMI(BB, *I, DL, MCID, DestReg);
298 }
299
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.
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::iterator I,const DebugLoc & DL,const MCInstrDesc & MCID)303 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
304 MachineBasicBlock::iterator I,
305 const DebugLoc &DL,
306 const MCInstrDesc &MCID) {
307 MachineFunction &MF = *BB.getParent();
308 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
309 BB.insert(I, MI);
310 return MachineInstrBuilder(MF, MI);
311 }
312
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::instr_iterator I,const DebugLoc & DL,const MCInstrDesc & MCID)313 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
314 MachineBasicBlock::instr_iterator I,
315 const DebugLoc &DL,
316 const MCInstrDesc &MCID) {
317 MachineFunction &MF = *BB.getParent();
318 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
319 BB.insert(I, MI);
320 return MachineInstrBuilder(MF, MI);
321 }
322
BuildMI(MachineBasicBlock & BB,MachineInstr & I,const DebugLoc & DL,const MCInstrDesc & MCID)323 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
324 const DebugLoc &DL,
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);
331 }
332
BuildMI(MachineBasicBlock & BB,MachineInstr * I,const DebugLoc & DL,const MCInstrDesc & MCID)333 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
334 const DebugLoc &DL,
335 const MCInstrDesc &MCID) {
336 return BuildMI(BB, *I, DL, MCID);
337 }
338
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.
BuildMI(MachineBasicBlock * BB,const DebugLoc & DL,const MCInstrDesc & MCID)341 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
342 const MCInstrDesc &MCID) {
343 return BuildMI(*BB, BB->end(), DL, MCID);
344 }
345
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.
BuildMI(MachineBasicBlock * BB,const DebugLoc & DL,const MCInstrDesc & MCID,unsigned DestReg)349 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
350 const MCInstrDesc &MCID, unsigned DestReg) {
351 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
352 }
353
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);
362
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);
371
getDefRegState(bool B)372 inline unsigned getDefRegState(bool B) {
373 return B ? RegState::Define : 0;
374 }
getImplRegState(bool B)375 inline unsigned getImplRegState(bool B) {
376 return B ? RegState::Implicit : 0;
377 }
getKillRegState(bool B)378 inline unsigned getKillRegState(bool B) {
379 return B ? RegState::Kill : 0;
380 }
getDeadRegState(bool B)381 inline unsigned getDeadRegState(bool B) {
382 return B ? RegState::Dead : 0;
383 }
getUndefRegState(bool B)384 inline unsigned getUndefRegState(bool B) {
385 return B ? RegState::Undef : 0;
386 }
getInternalReadRegState(bool B)387 inline unsigned getInternalReadRegState(bool B) {
388 return B ? RegState::InternalRead : 0;
389 }
getDebugRegState(bool B)390 inline unsigned getDebugRegState(bool B) {
391 return B ? RegState::Debug : 0;
392 }
393
394 /// Get all register state flags from machine operand \p RegOp.
getRegState(const MachineOperand & 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());
404 }
405
406 /// Helper class for constructing bundles of MachineInstrs.
407 ///
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;
415
416 public:
417 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
418 /// BB above the bundle or instruction at Pos.
MIBundleBuilder(MachineBasicBlock & BB,MachineBasicBlock::iterator Pos)419 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
420 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
421
422 /// Create a bundle from the sequence of instructions between B and E.
MIBundleBuilder(MachineBasicBlock & BB,MachineBasicBlock::iterator B,MachineBasicBlock::iterator 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");
427 ++B;
428 while (B != E) {
429 MachineInstr &MI = *B;
430 ++B;
431 MI.bundleWithPred();
432 }
433 }
434
435 /// Create an MIBundleBuilder representing an existing instruction or bundle
436 /// that has MI as its head.
MIBundleBuilder(MachineInstr * MI)437 explicit MIBundleBuilder(MachineInstr *MI)
438 : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(*MI)) {}
439
440 /// Return a reference to the basic block containing this bundle.
getMBB()441 MachineBasicBlock &getMBB() const { return MBB; }
442
443 /// Return true if no instructions have been inserted in this bundle yet.
444 /// Empty bundles aren't representable in a MachineBasicBlock.
empty()445 bool empty() const { return Begin == End; }
446
447 /// Return an iterator to the first bundled instruction.
begin()448 MachineBasicBlock::instr_iterator begin() const { return Begin; }
449
450 /// Return an iterator beyond the last bundled instruction.
end()451 MachineBasicBlock::instr_iterator end() const { return End; }
452
453 /// Insert MI into this bundle before I which must point to an instruction in
454 /// the bundle, or end().
insert(MachineBasicBlock::instr_iterator I,MachineInstr * MI)455 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
456 MachineInstr *MI) {
457 MBB.insert(I, MI);
458 if (I == Begin) {
459 if (!empty())
460 MI->bundleWithSucc();
461 Begin = MI->getIterator();
462 return *this;
463 }
464 if (I == End) {
465 MI->bundleWithPred();
466 return *this;
467 }
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);
472 return *this;
473 }
474
475 /// Insert MI into MBB by prepending it to the instructions in the bundle.
476 /// MI will become the first instruction in the bundle.
prepend(MachineInstr * MI)477 MIBundleBuilder &prepend(MachineInstr *MI) {
478 return insert(begin(), MI);
479 }
480
481 /// Insert MI into MBB by appending it to the instructions in the bundle.
482 /// MI will become the last instruction in the bundle.
append(MachineInstr * MI)483 MIBundleBuilder &append(MachineInstr *MI) {
484 return insert(end(), MI);
485 }
486 };
487
488 } // End llvm namespace
489
490 #endif
491