Move the complex address expression out of DIVariable and into an extra
[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   ///
57   operator MachineInstr*() const { return MI; }
58   MachineInstr *operator->() const { return MI; }
59   operator MachineBasicBlock::iterator() const { return MI; }
60
61   /// addReg - Add a new virtual register operand...
62   ///
63   const
64   MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
65                               unsigned SubReg = 0) const {
66     assert((flags & 0x1) == 0 &&
67            "Passing in 'true' to addReg is forbidden! Use enums instead.");
68     MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
69                                                flags & RegState::Define,
70                                                flags & RegState::Implicit,
71                                                flags & RegState::Kill,
72                                                flags & RegState::Dead,
73                                                flags & RegState::Undef,
74                                                flags & RegState::EarlyClobber,
75                                                SubReg,
76                                                flags & RegState::Debug,
77                                                flags & RegState::InternalRead));
78     return *this;
79   }
80
81   /// addImm - Add a new immediate operand.
82   ///
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() ? MI->getDebugVariable().Verify() : true) &&
174            "first MDNode argument of a DBG_VALUE not a DIVariable");
175     return *this;
176   }
177
178   const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
179     MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
180     return *this;
181   }
182
183   const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
184     MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym));
185     return *this;
186   }
187
188   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
189     MI->setFlags(Flags);
190     return *this;
191   }
192
193   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
194     MI->setFlag(Flag);
195     return *this;
196   }
197
198   // Add a displacement from an existing MachineOperand with an added offset.
199   const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
200                                      unsigned char TargetFlags = 0) const {
201     switch (Disp.getType()) {
202       default:
203         llvm_unreachable("Unhandled operand type in addDisp()");
204       case MachineOperand::MO_Immediate:
205         return addImm(Disp.getImm() + off);
206       case MachineOperand::MO_GlobalAddress: {
207         // If caller specifies new TargetFlags then use it, otherwise the
208         // default behavior is to copy the target flags from the existing
209         // MachineOperand. This means if the caller wants to clear the
210         // target flags it needs to do so explicitly.
211         if (TargetFlags)
212           return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
213                                   TargetFlags);
214         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
215                                 Disp.getTargetFlags());
216       }
217     }
218   }
219
220   /// Copy all the implicit operands from OtherMI onto this one.
221   const MachineInstrBuilder &copyImplicitOps(const MachineInstr *OtherMI) {
222     MI->copyImplicitOps(*MF, OtherMI);
223     return *this;
224   }
225 };
226
227 /// BuildMI - Builder interface.  Specify how to create the initial instruction
228 /// itself.
229 ///
230 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
231                                    DebugLoc DL,
232                                    const MCInstrDesc &MCID) {
233   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
234 }
235
236 /// BuildMI - This version of the builder sets up the first operand as a
237 /// destination virtual register.
238 ///
239 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
240                                    DebugLoc DL,
241                                    const MCInstrDesc &MCID,
242                                    unsigned DestReg) {
243   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
244            .addReg(DestReg, RegState::Define);
245 }
246
247 /// BuildMI - This version of the builder inserts the newly-built
248 /// instruction before the given position in the given MachineBasicBlock, and
249 /// sets up the first operand as a destination virtual register.
250 ///
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 /// BuildMI - This version of the builder inserts the newly-built
288 /// instruction before the given position in the given MachineBasicBlock, and
289 /// does NOT take a destination register.
290 ///
291 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
292                                    MachineBasicBlock::iterator I,
293                                    DebugLoc DL,
294                                    const MCInstrDesc &MCID) {
295   MachineFunction &MF = *BB.getParent();
296   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
297   BB.insert(I, MI);
298   return MachineInstrBuilder(MF, MI);
299 }
300
301 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
302                                    MachineBasicBlock::instr_iterator I,
303                                    DebugLoc DL,
304                                    const MCInstrDesc &MCID) {
305   MachineFunction &MF = *BB.getParent();
306   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
307   BB.insert(I, MI);
308   return MachineInstrBuilder(MF, MI);
309 }
310
311 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
312                                    MachineInstr *I,
313                                    DebugLoc DL,
314                                    const MCInstrDesc &MCID) {
315   if (I->isInsideBundle()) {
316     MachineBasicBlock::instr_iterator MII = I;
317     return BuildMI(BB, MII, DL, MCID);
318   }
319
320   MachineBasicBlock::iterator MII = I;
321   return BuildMI(BB, MII, DL, MCID);
322 }
323
324 /// BuildMI - This version of the builder inserts the newly-built
325 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
326 /// destination register.
327 ///
328 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
329                                    DebugLoc DL,
330                                    const MCInstrDesc &MCID) {
331   return BuildMI(*BB, BB->end(), DL, MCID);
332 }
333
334 /// BuildMI - This version of the builder inserts the newly-built
335 /// instruction at the end of the given MachineBasicBlock, and sets up the first
336 /// operand as a destination virtual register.
337 ///
338 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
339                                    DebugLoc DL,
340                                    const MCInstrDesc &MCID,
341                                    unsigned DestReg) {
342   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
343 }
344
345 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
346 /// for either a value in a register or a register-indirect+offset
347 /// address.  The convention is that a DBG_VALUE is indirect iff the
348 /// second operand is an immediate.
349 ///
350 inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL,
351                                    const MCInstrDesc &MCID, bool IsIndirect,
352                                    unsigned Reg, unsigned Offset,
353                                    const MDNode *Variable, const MDNode *Expr) {
354   assert(DIVariable(Variable).Verify() && "not a DIVariable");
355   assert(DIExpression(Expr).Verify() && "not a DIExpression");
356   if (IsIndirect)
357     return BuildMI(MF, DL, MCID)
358         .addReg(Reg, RegState::Debug)
359         .addImm(Offset)
360         .addMetadata(Variable)
361         .addMetadata(Expr);
362   else {
363     assert(Offset == 0 && "A direct address cannot have an offset.");
364     return BuildMI(MF, DL, MCID)
365         .addReg(Reg, RegState::Debug)
366         .addReg(0U, RegState::Debug)
367         .addMetadata(Variable)
368         .addMetadata(Expr);
369   }
370 }
371
372 /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
373 /// for either a value in a register or a register-indirect+offset
374 /// address and inserts it at position I.
375 ///
376 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
377                                    MachineBasicBlock::iterator I, DebugLoc DL,
378                                    const MCInstrDesc &MCID, bool IsIndirect,
379                                    unsigned Reg, unsigned Offset,
380                                    const MDNode *Variable, const MDNode *Expr) {
381   assert(DIVariable(Variable).Verify() && "not a DIVariable");
382   assert(DIExpression(Expr).Verify() && "not a DIExpression");
383   MachineFunction &MF = *BB.getParent();
384   MachineInstr *MI =
385       BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
386   BB.insert(I, MI);
387   return MachineInstrBuilder(MF, MI);
388 }
389
390
391 inline unsigned getDefRegState(bool B) {
392   return B ? RegState::Define : 0;
393 }
394 inline unsigned getImplRegState(bool B) {
395   return B ? RegState::Implicit : 0;
396 }
397 inline unsigned getKillRegState(bool B) {
398   return B ? RegState::Kill : 0;
399 }
400 inline unsigned getDeadRegState(bool B) {
401   return B ? RegState::Dead : 0;
402 }
403 inline unsigned getUndefRegState(bool B) {
404   return B ? RegState::Undef : 0;
405 }
406 inline unsigned getInternalReadRegState(bool B) {
407   return B ? RegState::InternalRead : 0;
408 }
409 inline unsigned getDebugRegState(bool B) {
410   return B ? RegState::Debug : 0;
411 }
412
413
414 /// Helper class for constructing bundles of MachineInstrs.
415 ///
416 /// MIBundleBuilder can create a bundle from scratch by inserting new
417 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
418 /// existing MachineInstrs in a basic block.
419 class MIBundleBuilder {
420   MachineBasicBlock &MBB;
421   MachineBasicBlock::instr_iterator Begin;
422   MachineBasicBlock::instr_iterator End;
423
424 public:
425   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
426   /// BB above the bundle or instruction at Pos.
427   MIBundleBuilder(MachineBasicBlock &BB,
428                   MachineBasicBlock::iterator Pos)
429     : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
430
431   /// Create a bundle from the sequence of instructions between B and E.
432   MIBundleBuilder(MachineBasicBlock &BB,
433                   MachineBasicBlock::iterator B,
434                   MachineBasicBlock::iterator E)
435     : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
436     assert(B != E && "No instructions to bundle");
437     ++B;
438     while (B != E) {
439       MachineInstr *MI = B;
440       ++B;
441       MI->bundleWithPred();
442     }
443   }
444
445   /// Create an MIBundleBuilder representing an existing instruction or bundle
446   /// that has MI as its head.
447   explicit MIBundleBuilder(MachineInstr *MI)
448     : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
449
450   /// Return a reference to the basic block containing this bundle.
451   MachineBasicBlock &getMBB() const { return MBB; }
452
453   /// Return true if no instructions have been inserted in this bundle yet.
454   /// Empty bundles aren't representable in a MachineBasicBlock.
455   bool empty() const { return Begin == End; }
456
457   /// Return an iterator to the first bundled instruction.
458   MachineBasicBlock::instr_iterator begin() const { return Begin; }
459
460   /// Return an iterator beyond the last bundled instruction.
461   MachineBasicBlock::instr_iterator end() const { return End; }
462
463   /// Insert MI into this bundle before I which must point to an instruction in
464   /// the bundle, or end().
465   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
466                           MachineInstr *MI) {
467     MBB.insert(I, MI);
468     if (I == Begin) {
469       if (!empty())
470         MI->bundleWithSucc();
471       Begin = MI;
472       return *this;
473     }
474     if (I == End) {
475       MI->bundleWithPred();
476       return *this;
477     }
478     // MI was inserted in the middle of the bundle, so its neighbors' flags are
479     // already fine. Update MI's bundle flags manually.
480     MI->setFlag(MachineInstr::BundledPred);
481     MI->setFlag(MachineInstr::BundledSucc);
482     return *this;
483   }
484
485   /// Insert MI into MBB by prepending it to the instructions in the bundle.
486   /// MI will become the first instruction in the bundle.
487   MIBundleBuilder &prepend(MachineInstr *MI) {
488     return insert(begin(), MI);
489   }
490
491   /// Insert MI into MBB by appending it to the instructions in the bundle.
492   /// MI will become the last instruction in the bundle.
493   MIBundleBuilder &append(MachineInstr *MI) {
494     return insert(end(), MI);
495   }
496 };
497
498 } // End llvm namespace
499
500 #endif