[MachineInstrBuilder] Support for adding a ConstantPoolIndex MO with an additional...
[oota-llvm.git] / include / llvm / CodeGen / MachineInstrBuilder.h
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:
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.
53   MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
54
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; }
59
60   /// If conversion operators fail, use this method to get the MachineInstr
61   /// explicitly.
62   MachineInstr *getInstr() const { return MI; }
63
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,
76                                                SubReg,
77                                                flags & RegState::Debug,
78                                                flags & RegState::InternalRead));
79     return *this;
80   }
81
82   /// Add a new immediate operand.
83   const MachineInstrBuilder &addImm(int64_t Val) const {
84     MI->addOperand(*MF, MachineOperand::CreateImm(Val));
85     return *this;
86   }
87
88   const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
89     MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
90     return *this;
91   }
92
93   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
94     MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
95     return *this;
96   }
97
98   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
99                                     unsigned char TargetFlags = 0) const {
100     MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
101     return *this;
102   }
103
104   const MachineInstrBuilder &addFrameIndex(int Idx) const {
105     MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
106     return *this;
107   }
108
109   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
110                                                   int Offset = 0,
111                                           unsigned char TargetFlags = 0) const {
112     MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
113     return *this;
114   }
115
116   const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
117                                           unsigned char TargetFlags = 0) const {
118     MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
119                                                           TargetFlags));
120     return *this;
121   }
122
123   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
124                                           unsigned char TargetFlags = 0) const {
125     MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
126     return *this;
127   }
128
129   const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
130                                               int64_t Offset = 0,
131                                           unsigned char TargetFlags = 0) const {
132     MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
133     return *this;
134   }
135
136   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
137                                           unsigned char TargetFlags = 0) const {
138     MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
139     return *this;
140   }
141
142   const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
143                                              int64_t Offset = 0,
144                                           unsigned char TargetFlags = 0) const {
145     MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
146     return *this;
147   }
148
149   const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
150     MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
151     return *this;
152   }
153
154   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
155     MI->addMemOperand(*MF, MMO);
156     return *this;
157   }
158
159   const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
160                                         MachineInstr::mmo_iterator e) const {
161     MI->setMemRefs(b, e);
162     return *this;
163   }
164
165
166   const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
167     MI->addOperand(*MF, MO);
168     return *this;
169   }
170
171   const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
172     MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
173     assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
174                                : true) &&
175            "first MDNode argument of a DBG_VALUE not a variable");
176     return *this;
177   }
178
179   const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
180     MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
181     return *this;
182   }
183
184   const MachineInstrBuilder &addSym(MCSymbol *Sym,
185                                     unsigned char TargetFlags = 0) const {
186     MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
187     return *this;
188   }
189
190   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
191     MI->setFlags(Flags);
192     return *this;
193   }
194
195   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
196     MI->setFlag(Flag);
197     return *this;
198   }
199
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     // If caller specifies new TargetFlags then use it, otherwise the
204     // default behavior is to copy the target flags from the existing
205     // MachineOperand. This means if the caller wants to clear the
206     // target flags it needs to do so explicitly.
207     if (0 == TargetFlags)
208       TargetFlags = Disp.getTargetFlags();
209
210     switch (Disp.getType()) {
211       default:
212         llvm_unreachable("Unhandled operand type in addDisp()");
213       case MachineOperand::MO_Immediate:
214         return addImm(Disp.getImm() + off);
215       case MachineOperand::MO_ConstantPoolIndex:
216         return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
217                                     TargetFlags);
218       case MachineOperand::MO_GlobalAddress:
219         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
220                                 TargetFlags);
221     }
222   }
223
224   /// Copy all the implicit operands from OtherMI onto this one.
225   const MachineInstrBuilder &copyImplicitOps(const MachineInstr *OtherMI) {
226     MI->copyImplicitOps(*MF, OtherMI);
227     return *this;
228   }
229 };
230
231 /// Builder interface. Specify how to create the initial instruction itself.
232 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
233                                    DebugLoc DL,
234                                    const MCInstrDesc &MCID) {
235   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
236 }
237
238 /// This version of the builder sets up the first operand as a
239 /// destination virtual register.
240 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
241                                    DebugLoc DL,
242                                    const MCInstrDesc &MCID,
243                                    unsigned DestReg) {
244   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
245            .addReg(DestReg, RegState::Define);
246 }
247
248 /// This version of the builder inserts the newly-built instruction before
249 /// the given position in the given MachineBasicBlock, and sets up the first
250 /// operand as a destination virtual register.
251 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
252                                    MachineBasicBlock::iterator I,
253                                    DebugLoc DL,
254                                    const MCInstrDesc &MCID,
255                                    unsigned DestReg) {
256   MachineFunction &MF = *BB.getParent();
257   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
258   BB.insert(I, MI);
259   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
260 }
261
262 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
263                                    MachineBasicBlock::instr_iterator I,
264                                    DebugLoc DL,
265                                    const MCInstrDesc &MCID,
266                                    unsigned DestReg) {
267   MachineFunction &MF = *BB.getParent();
268   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
269   BB.insert(I, MI);
270   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
271 }
272
273 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
274                                    MachineInstr *I,
275                                    DebugLoc DL,
276                                    const MCInstrDesc &MCID,
277                                    unsigned DestReg) {
278   if (I->isInsideBundle()) {
279     MachineBasicBlock::instr_iterator MII(I);
280     return BuildMI(BB, MII, DL, MCID, DestReg);
281   }
282
283   MachineBasicBlock::iterator MII = I;
284   return BuildMI(BB, MII, DL, MCID, DestReg);
285 }
286
287 /// This version of the builder inserts the newly-built instruction before the
288 /// given position in the given MachineBasicBlock, and does NOT take a
289 /// destination register.
290 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
291                                    MachineBasicBlock::iterator I,
292                                    DebugLoc DL,
293                                    const MCInstrDesc &MCID) {
294   MachineFunction &MF = *BB.getParent();
295   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
296   BB.insert(I, MI);
297   return MachineInstrBuilder(MF, MI);
298 }
299
300 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
301                                    MachineBasicBlock::instr_iterator I,
302                                    DebugLoc DL,
303                                    const MCInstrDesc &MCID) {
304   MachineFunction &MF = *BB.getParent();
305   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
306   BB.insert(I, MI);
307   return MachineInstrBuilder(MF, MI);
308 }
309
310 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
311                                    MachineInstr *I,
312                                    DebugLoc DL,
313                                    const MCInstrDesc &MCID) {
314   if (I->isInsideBundle()) {
315     MachineBasicBlock::instr_iterator MII(I);
316     return BuildMI(BB, MII, DL, MCID);
317   }
318
319   MachineBasicBlock::iterator MII = I;
320   return BuildMI(BB, MII, DL, MCID);
321 }
322
323 /// This version of the builder inserts the newly-built instruction at the end
324 /// of the given MachineBasicBlock, and does NOT take a destination register.
325 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
326                                    DebugLoc DL,
327                                    const MCInstrDesc &MCID) {
328   return BuildMI(*BB, BB->end(), DL, MCID);
329 }
330
331 /// This version of the builder inserts the newly-built instruction at the
332 /// end of the given MachineBasicBlock, and sets up the first operand as a
333 /// destination virtual register.
334 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
335                                    DebugLoc DL,
336                                    const MCInstrDesc &MCID,
337                                    unsigned DestReg) {
338   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
339 }
340
341 /// This version of the builder builds a DBG_VALUE intrinsic
342 /// for either a value in a register or a register-indirect+offset
343 /// address.  The convention is that a DBG_VALUE is indirect iff the
344 /// second operand is an immediate.
345 inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL,
346                                    const MCInstrDesc &MCID, bool IsIndirect,
347                                    unsigned Reg, unsigned Offset,
348                                    const MDNode *Variable, const MDNode *Expr) {
349   assert(isa<DILocalVariable>(Variable) && "not a variable");
350   assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
351   assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
352          "Expected inlined-at fields to agree");
353   if (IsIndirect)
354     return BuildMI(MF, DL, MCID)
355         .addReg(Reg, RegState::Debug)
356         .addImm(Offset)
357         .addMetadata(Variable)
358         .addMetadata(Expr);
359   else {
360     assert(Offset == 0 && "A direct address cannot have an offset.");
361     return BuildMI(MF, DL, MCID)
362         .addReg(Reg, RegState::Debug)
363         .addReg(0U, RegState::Debug)
364         .addMetadata(Variable)
365         .addMetadata(Expr);
366   }
367 }
368
369 /// This version of the builder builds a DBG_VALUE intrinsic
370 /// for either a value in a register or a register-indirect+offset
371 /// address and inserts it at position I.
372 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
373                                    MachineBasicBlock::iterator I, DebugLoc DL,
374                                    const MCInstrDesc &MCID, bool IsIndirect,
375                                    unsigned Reg, unsigned Offset,
376                                    const MDNode *Variable, const MDNode *Expr) {
377   assert(isa<DILocalVariable>(Variable) && "not a variable");
378   assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
379   MachineFunction &MF = *BB.getParent();
380   MachineInstr *MI =
381       BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
382   BB.insert(I, MI);
383   return MachineInstrBuilder(MF, MI);
384 }
385
386
387 inline unsigned getDefRegState(bool B) {
388   return B ? RegState::Define : 0;
389 }
390 inline unsigned getImplRegState(bool B) {
391   return B ? RegState::Implicit : 0;
392 }
393 inline unsigned getKillRegState(bool B) {
394   return B ? RegState::Kill : 0;
395 }
396 inline unsigned getDeadRegState(bool B) {
397   return B ? RegState::Dead : 0;
398 }
399 inline unsigned getUndefRegState(bool B) {
400   return B ? RegState::Undef : 0;
401 }
402 inline unsigned getInternalReadRegState(bool B) {
403   return B ? RegState::InternalRead : 0;
404 }
405 inline unsigned getDebugRegState(bool B) {
406   return B ? RegState::Debug : 0;
407 }
408
409
410 /// Helper class for constructing bundles of MachineInstrs.
411 ///
412 /// MIBundleBuilder can create a bundle from scratch by inserting new
413 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
414 /// existing MachineInstrs in a basic block.
415 class MIBundleBuilder {
416   MachineBasicBlock &MBB;
417   MachineBasicBlock::instr_iterator Begin;
418   MachineBasicBlock::instr_iterator End;
419
420 public:
421   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
422   /// BB above the bundle or instruction at Pos.
423   MIBundleBuilder(MachineBasicBlock &BB,
424                   MachineBasicBlock::iterator Pos)
425     : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
426
427   /// Create a bundle from the sequence of instructions between B and E.
428   MIBundleBuilder(MachineBasicBlock &BB,
429                   MachineBasicBlock::iterator B,
430                   MachineBasicBlock::iterator E)
431     : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
432     assert(B != E && "No instructions to bundle");
433     ++B;
434     while (B != E) {
435       MachineInstr *MI = B;
436       ++B;
437       MI->bundleWithPred();
438     }
439   }
440
441   /// Create an MIBundleBuilder representing an existing instruction or bundle
442   /// that has MI as its head.
443   explicit MIBundleBuilder(MachineInstr *MI)
444     : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
445
446   /// Return a reference to the basic block containing this bundle.
447   MachineBasicBlock &getMBB() const { return MBB; }
448
449   /// Return true if no instructions have been inserted in this bundle yet.
450   /// Empty bundles aren't representable in a MachineBasicBlock.
451   bool empty() const { return Begin == End; }
452
453   /// Return an iterator to the first bundled instruction.
454   MachineBasicBlock::instr_iterator begin() const { return Begin; }
455
456   /// Return an iterator beyond the last bundled instruction.
457   MachineBasicBlock::instr_iterator end() const { return End; }
458
459   /// Insert MI into this bundle before I which must point to an instruction in
460   /// the bundle, or end().
461   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
462                           MachineInstr *MI) {
463     MBB.insert(I, MI);
464     if (I == Begin) {
465       if (!empty())
466         MI->bundleWithSucc();
467       Begin = MI->getIterator();
468       return *this;
469     }
470     if (I == End) {
471       MI->bundleWithPred();
472       return *this;
473     }
474     // MI was inserted in the middle of the bundle, so its neighbors' flags are
475     // already fine. Update MI's bundle flags manually.
476     MI->setFlag(MachineInstr::BundledPred);
477     MI->setFlag(MachineInstr::BundledSucc);
478     return *this;
479   }
480
481   /// Insert MI into MBB by prepending it to the instructions in the bundle.
482   /// MI will become the first instruction in the bundle.
483   MIBundleBuilder &prepend(MachineInstr *MI) {
484     return insert(begin(), MI);
485   }
486
487   /// Insert MI into MBB by appending it to the instructions in the bundle.
488   /// MI will become the last instruction in the bundle.
489   MIBundleBuilder &append(MachineInstr *MI) {
490     return insert(end(), MI);
491   }
492 };
493
494 } // End llvm namespace
495
496 #endif