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(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
18 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
19
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBundle.h"
22 #include "llvm/Support/ErrorHandling.h"
23
24 namespace llvm {
25
26 class MCInstrDesc;
27 class MDNode;
28
29 namespace RegState {
30 enum {
31 Define = 0x2,
32 Implicit = 0x4,
33 Kill = 0x8,
34 Dead = 0x10,
35 Undef = 0x20,
36 EarlyClobber = 0x40,
37 Debug = 0x80,
38 InternalRead = 0x100,
39 DefineNoRead = Define | Undef,
40 ImplicitDefine = Implicit | Define,
41 ImplicitKill = Implicit | Kill
42 };
43 }
44
45 class MachineInstrBuilder {
46 MachineFunction *MF;
47 MachineInstr *MI;
48 public:
MachineInstrBuilder()49 MachineInstrBuilder() : MF(nullptr), MI(nullptr) {}
50
51 /// Create a MachineInstrBuilder for manipulating an existing instruction.
52 /// F must be the machine function that was used to allocate I.
MachineInstrBuilder(MachineFunction & F,MachineInstr * I)53 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
54
55 /// Allow automatic conversion to the machine instruction we are working on.
56 ///
57 operator MachineInstr*() const { return MI; }
58 MachineInstr *operator->() const { return MI; }
iterator()59 operator MachineBasicBlock::iterator() const { return MI; }
60
61 /// If conversion operators fail, use this method to get the MachineInstr
62 /// explicitly.
getInstr()63 MachineInstr *getInstr() const { return MI; }
64
65 /// addReg - Add a new virtual register operand...
66 ///
67 const
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,
79 SubReg,
80 flags & RegState::Debug,
81 flags & RegState::InternalRead));
82 return *this;
83 }
84
85 /// addImm - Add a new immediate operand.
86 ///
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
169
addOperand(const MachineOperand & MO)170 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
171 MI->addOperand(*MF, MO);
172 return *this;
173 }
174
addMetadata(const MDNode * MD)175 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
176 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
177 assert((MI->isDebugValue() ? MI->getDebugVariable().Verify() : true) &&
178 "first MDNode argument of a DBG_VALUE not a DIVariable");
179 return *this;
180 }
181
addCFIIndex(unsigned CFIIndex)182 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
183 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
184 return *this;
185 }
186
addSym(MCSymbol * Sym)187 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
188 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym));
189 return *this;
190 }
191
setMIFlags(unsigned Flags)192 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
193 MI->setFlags(Flags);
194 return *this;
195 }
196
setMIFlag(MachineInstr::MIFlag Flag)197 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
198 MI->setFlag(Flag);
199 return *this;
200 }
201
202 // Add a displacement from an existing MachineOperand with an added offset.
203 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
204 unsigned char TargetFlags = 0) const {
205 switch (Disp.getType()) {
206 default:
207 llvm_unreachable("Unhandled operand type in addDisp()");
208 case MachineOperand::MO_Immediate:
209 return addImm(Disp.getImm() + off);
210 case MachineOperand::MO_GlobalAddress: {
211 // If caller specifies new TargetFlags then use it, otherwise the
212 // default behavior is to copy the target flags from the existing
213 // MachineOperand. This means if the caller wants to clear the
214 // target flags it needs to do so explicitly.
215 if (TargetFlags)
216 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
217 TargetFlags);
218 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
219 Disp.getTargetFlags());
220 }
221 }
222 }
223
224 /// Copy all the implicit operands from OtherMI onto this one.
copyImplicitOps(const MachineInstr * OtherMI)225 const MachineInstrBuilder ©ImplicitOps(const MachineInstr *OtherMI) {
226 MI->copyImplicitOps(*MF, OtherMI);
227 return *this;
228 }
229 };
230
231 /// BuildMI - Builder interface. Specify how to create the initial instruction
232 /// itself.
233 ///
BuildMI(MachineFunction & MF,DebugLoc DL,const MCInstrDesc & MCID)234 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
235 DebugLoc DL,
236 const MCInstrDesc &MCID) {
237 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
238 }
239
240 /// BuildMI - This version of the builder sets up the first operand as a
241 /// destination virtual register.
242 ///
BuildMI(MachineFunction & MF,DebugLoc DL,const MCInstrDesc & MCID,unsigned DestReg)243 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
244 DebugLoc DL,
245 const MCInstrDesc &MCID,
246 unsigned DestReg) {
247 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
248 .addReg(DestReg, RegState::Define);
249 }
250
251 /// BuildMI - This version of the builder inserts the newly-built
252 /// instruction before the given position in the given MachineBasicBlock, and
253 /// sets up the first operand as a destination virtual register.
254 ///
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::iterator I,DebugLoc DL,const MCInstrDesc & MCID,unsigned DestReg)255 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
256 MachineBasicBlock::iterator I,
257 DebugLoc DL,
258 const MCInstrDesc &MCID,
259 unsigned DestReg) {
260 MachineFunction &MF = *BB.getParent();
261 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
262 BB.insert(I, MI);
263 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
264 }
265
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::instr_iterator I,DebugLoc DL,const MCInstrDesc & MCID,unsigned DestReg)266 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
267 MachineBasicBlock::instr_iterator I,
268 DebugLoc DL,
269 const MCInstrDesc &MCID,
270 unsigned DestReg) {
271 MachineFunction &MF = *BB.getParent();
272 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
273 BB.insert(I, MI);
274 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
275 }
276
BuildMI(MachineBasicBlock & BB,MachineInstr * I,DebugLoc DL,const MCInstrDesc & MCID,unsigned DestReg)277 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
278 MachineInstr *I,
279 DebugLoc DL,
280 const MCInstrDesc &MCID,
281 unsigned DestReg) {
282 if (I->isInsideBundle()) {
283 MachineBasicBlock::instr_iterator MII = I;
284 return BuildMI(BB, MII, DL, MCID, DestReg);
285 }
286
287 MachineBasicBlock::iterator MII = I;
288 return BuildMI(BB, MII, DL, MCID, DestReg);
289 }
290
291 /// BuildMI - This version of the builder inserts the newly-built
292 /// instruction before the given position in the given MachineBasicBlock, and
293 /// does NOT take a destination register.
294 ///
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::iterator I,DebugLoc DL,const MCInstrDesc & MCID)295 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
296 MachineBasicBlock::iterator I,
297 DebugLoc DL,
298 const MCInstrDesc &MCID) {
299 MachineFunction &MF = *BB.getParent();
300 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
301 BB.insert(I, MI);
302 return MachineInstrBuilder(MF, MI);
303 }
304
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::instr_iterator I,DebugLoc DL,const MCInstrDesc & MCID)305 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
306 MachineBasicBlock::instr_iterator I,
307 DebugLoc DL,
308 const MCInstrDesc &MCID) {
309 MachineFunction &MF = *BB.getParent();
310 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
311 BB.insert(I, MI);
312 return MachineInstrBuilder(MF, MI);
313 }
314
BuildMI(MachineBasicBlock & BB,MachineInstr * I,DebugLoc DL,const MCInstrDesc & MCID)315 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
316 MachineInstr *I,
317 DebugLoc DL,
318 const MCInstrDesc &MCID) {
319 if (I->isInsideBundle()) {
320 MachineBasicBlock::instr_iterator MII = I;
321 return BuildMI(BB, MII, DL, MCID);
322 }
323
324 MachineBasicBlock::iterator MII = I;
325 return BuildMI(BB, MII, DL, MCID);
326 }
327
328 /// BuildMI - This version of the builder inserts the newly-built
329 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
330 /// destination register.
331 ///
BuildMI(MachineBasicBlock * BB,DebugLoc DL,const MCInstrDesc & MCID)332 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
333 DebugLoc DL,
334 const MCInstrDesc &MCID) {
335 return BuildMI(*BB, BB->end(), DL, MCID);
336 }
337
338 /// BuildMI - This version of the builder inserts the newly-built
339 /// instruction at the end of the given MachineBasicBlock, and sets up the first
340 /// operand as a destination virtual register.
341 ///
BuildMI(MachineBasicBlock * BB,DebugLoc DL,const MCInstrDesc & MCID,unsigned DestReg)342 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
343 DebugLoc DL,
344 const MCInstrDesc &MCID,
345 unsigned DestReg) {
346 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
347 }
348
349 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
350 /// for either a value in a register or a register-indirect+offset
351 /// address. The convention is that a DBG_VALUE is indirect iff the
352 /// second operand is an immediate.
353 ///
BuildMI(MachineFunction & MF,DebugLoc DL,const MCInstrDesc & MCID,bool IsIndirect,unsigned Reg,unsigned Offset,const MDNode * Variable,const MDNode * Expr)354 inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL,
355 const MCInstrDesc &MCID, bool IsIndirect,
356 unsigned Reg, unsigned Offset,
357 const MDNode *Variable, const MDNode *Expr) {
358 assert(DIVariable(Variable).Verify() && "not a DIVariable");
359 assert(DIExpression(Expr).Verify() && "not a DIExpression");
360 if (IsIndirect)
361 return BuildMI(MF, DL, MCID)
362 .addReg(Reg, RegState::Debug)
363 .addImm(Offset)
364 .addMetadata(Variable)
365 .addMetadata(Expr);
366 else {
367 assert(Offset == 0 && "A direct address cannot have an offset.");
368 return BuildMI(MF, DL, MCID)
369 .addReg(Reg, RegState::Debug)
370 .addReg(0U, RegState::Debug)
371 .addMetadata(Variable)
372 .addMetadata(Expr);
373 }
374 }
375
376 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
377 /// for either a value in a register or a register-indirect+offset
378 /// address and inserts it at position I.
379 ///
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::iterator I,DebugLoc DL,const MCInstrDesc & MCID,bool IsIndirect,unsigned Reg,unsigned Offset,const MDNode * Variable,const MDNode * Expr)380 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
381 MachineBasicBlock::iterator I, DebugLoc DL,
382 const MCInstrDesc &MCID, bool IsIndirect,
383 unsigned Reg, unsigned Offset,
384 const MDNode *Variable, const MDNode *Expr) {
385 assert(DIVariable(Variable).Verify() && "not a DIVariable");
386 assert(DIExpression(Expr).Verify() && "not a DIExpression");
387 MachineFunction &MF = *BB.getParent();
388 MachineInstr *MI =
389 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
390 BB.insert(I, MI);
391 return MachineInstrBuilder(MF, MI);
392 }
393
394
getDefRegState(bool B)395 inline unsigned getDefRegState(bool B) {
396 return B ? RegState::Define : 0;
397 }
getImplRegState(bool B)398 inline unsigned getImplRegState(bool B) {
399 return B ? RegState::Implicit : 0;
400 }
getKillRegState(bool B)401 inline unsigned getKillRegState(bool B) {
402 return B ? RegState::Kill : 0;
403 }
getDeadRegState(bool B)404 inline unsigned getDeadRegState(bool B) {
405 return B ? RegState::Dead : 0;
406 }
getUndefRegState(bool B)407 inline unsigned getUndefRegState(bool B) {
408 return B ? RegState::Undef : 0;
409 }
getInternalReadRegState(bool B)410 inline unsigned getInternalReadRegState(bool B) {
411 return B ? RegState::InternalRead : 0;
412 }
getDebugRegState(bool B)413 inline unsigned getDebugRegState(bool B) {
414 return B ? RegState::Debug : 0;
415 }
416
417
418 /// Helper class for constructing bundles of MachineInstrs.
419 ///
420 /// MIBundleBuilder can create a bundle from scratch by inserting new
421 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
422 /// existing MachineInstrs in a basic block.
423 class MIBundleBuilder {
424 MachineBasicBlock &MBB;
425 MachineBasicBlock::instr_iterator Begin;
426 MachineBasicBlock::instr_iterator End;
427
428 public:
429 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
430 /// BB above the bundle or instruction at Pos.
MIBundleBuilder(MachineBasicBlock & BB,MachineBasicBlock::iterator Pos)431 MIBundleBuilder(MachineBasicBlock &BB,
432 MachineBasicBlock::iterator Pos)
433 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
434
435 /// Create a bundle from the sequence of instructions between B and E.
MIBundleBuilder(MachineBasicBlock & BB,MachineBasicBlock::iterator B,MachineBasicBlock::iterator E)436 MIBundleBuilder(MachineBasicBlock &BB,
437 MachineBasicBlock::iterator B,
438 MachineBasicBlock::iterator E)
439 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
440 assert(B != E && "No instructions to bundle");
441 ++B;
442 while (B != E) {
443 MachineInstr *MI = B;
444 ++B;
445 MI->bundleWithPred();
446 }
447 }
448
449 /// Create an MIBundleBuilder representing an existing instruction or bundle
450 /// that has MI as its head.
MIBundleBuilder(MachineInstr * MI)451 explicit MIBundleBuilder(MachineInstr *MI)
452 : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
453
454 /// Return a reference to the basic block containing this bundle.
getMBB()455 MachineBasicBlock &getMBB() const { return MBB; }
456
457 /// Return true if no instructions have been inserted in this bundle yet.
458 /// Empty bundles aren't representable in a MachineBasicBlock.
empty()459 bool empty() const { return Begin == End; }
460
461 /// Return an iterator to the first bundled instruction.
begin()462 MachineBasicBlock::instr_iterator begin() const { return Begin; }
463
464 /// Return an iterator beyond the last bundled instruction.
end()465 MachineBasicBlock::instr_iterator end() const { return End; }
466
467 /// Insert MI into this bundle before I which must point to an instruction in
468 /// the bundle, or end().
insert(MachineBasicBlock::instr_iterator I,MachineInstr * MI)469 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
470 MachineInstr *MI) {
471 MBB.insert(I, MI);
472 if (I == Begin) {
473 if (!empty())
474 MI->bundleWithSucc();
475 Begin = MI;
476 return *this;
477 }
478 if (I == End) {
479 MI->bundleWithPred();
480 return *this;
481 }
482 // MI was inserted in the middle of the bundle, so its neighbors' flags are
483 // already fine. Update MI's bundle flags manually.
484 MI->setFlag(MachineInstr::BundledPred);
485 MI->setFlag(MachineInstr::BundledSucc);
486 return *this;
487 }
488
489 /// Insert MI into MBB by prepending it to the instructions in the bundle.
490 /// MI will become the first instruction in the bundle.
prepend(MachineInstr * MI)491 MIBundleBuilder &prepend(MachineInstr *MI) {
492 return insert(begin(), MI);
493 }
494
495 /// Insert MI into MBB by appending it to the instructions in the bundle.
496 /// MI will become the last instruction in the bundle.
append(MachineInstr * MI)497 MIBundleBuilder &append(MachineInstr *MI) {
498 return insert(end(), MI);
499 }
500 };
501
502 } // End llvm namespace
503
504 #endif
505