Remove the explicit MachineInstrBuilder(MI) constructor.
[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/Support/ErrorHandling.h"
22
23 namespace llvm {
24
25 class MCInstrDesc;
26 class MDNode;
27
28 namespace RegState {
29   enum {
30     Define         = 0x2,
31     Implicit       = 0x4,
32     Kill           = 0x8,
33     Dead           = 0x10,
34     Undef          = 0x20,
35     EarlyClobber   = 0x40,
36     Debug          = 0x80,
37     InternalRead   = 0x100,
38     DefineNoRead   = Define | Undef,
39     ImplicitDefine = Implicit | Define,
40     ImplicitKill   = Implicit | Kill
41   };
42 }
43
44 class MachineInstrBuilder {
45   MachineFunction *MF;
46   MachineInstr *MI;
47 public:
48   MachineInstrBuilder() : MF(0), MI(0) {}
49
50   /// Create a MachineInstrBuilder for manipulating an existing instruction.
51   /// F must be the machine function  that was used to allocate I.
52   MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
53
54   /// Allow automatic conversion to the machine instruction we are working on.
55   ///
56   operator MachineInstr*() const { return MI; }
57   MachineInstr *operator->() const { return MI; }
58   operator MachineBasicBlock::iterator() const { return MI; }
59
60   /// addReg - Add a new virtual register operand...
61   ///
62   const
63   MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
64                               unsigned SubReg = 0) const {
65     assert((flags & 0x1) == 0 &&
66            "Passing in 'true' to addReg is forbidden! Use enums instead.");
67     MI->addOperand(MachineOperand::CreateReg(RegNo,
68                                              flags & RegState::Define,
69                                              flags & RegState::Implicit,
70                                              flags & RegState::Kill,
71                                              flags & RegState::Dead,
72                                              flags & RegState::Undef,
73                                              flags & RegState::EarlyClobber,
74                                              SubReg,
75                                              flags & RegState::Debug,
76                                              flags & RegState::InternalRead));
77     return *this;
78   }
79
80   /// addImm - Add a new immediate operand.
81   ///
82   const MachineInstrBuilder &addImm(int64_t Val) const {
83     MI->addOperand(MachineOperand::CreateImm(Val));
84     return *this;
85   }
86
87   const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
88     MI->addOperand(MachineOperand::CreateCImm(Val));
89     return *this;
90   }
91
92   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
93     MI->addOperand(MachineOperand::CreateFPImm(Val));
94     return *this;
95   }
96
97   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
98                                     unsigned char TargetFlags = 0) const {
99     MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
100     return *this;
101   }
102
103   const MachineInstrBuilder &addFrameIndex(int Idx) const {
104     MI->addOperand(MachineOperand::CreateFI(Idx));
105     return *this;
106   }
107
108   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
109                                                   int Offset = 0,
110                                           unsigned char TargetFlags = 0) const {
111     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
112     return *this;
113   }
114
115   const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
116                                           unsigned char TargetFlags = 0) const {
117     MI->addOperand(MachineOperand::CreateTargetIndex(Idx, Offset, TargetFlags));
118     return *this;
119   }
120
121   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
122                                           unsigned char TargetFlags = 0) const {
123     MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
124     return *this;
125   }
126
127   const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
128                                               int64_t Offset = 0,
129                                           unsigned char TargetFlags = 0) const {
130     MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
131     return *this;
132   }
133
134   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
135                                           unsigned char TargetFlags = 0) const {
136     MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
137     return *this;
138   }
139
140   const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
141     MI->addOperand(MachineOperand::CreateRegMask(Mask));
142     return *this;
143   }
144
145   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
146     MI->addMemOperand(*MI->getParent()->getParent(), MMO);
147     return *this;
148   }
149
150   const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
151                                         MachineInstr::mmo_iterator e) const {
152     MI->setMemRefs(b, e);
153     return *this;
154   }
155
156
157   const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
158     MI->addOperand(MO);
159     return *this;
160   }
161
162   const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
163     MI->addOperand(MachineOperand::CreateMetadata(MD));
164     return *this;
165   }
166   
167   const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
168     MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
169     return *this;
170   }
171
172   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
173     MI->setFlags(Flags);
174     return *this;
175   }
176
177   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
178     MI->setFlag(Flag);
179     return *this;
180   }
181
182   // Add a displacement from an existing MachineOperand with an added offset.
183   const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
184                                      unsigned char TargetFlags = 0) const {
185     switch (Disp.getType()) {
186       default:
187         llvm_unreachable("Unhandled operand type in addDisp()");
188       case MachineOperand::MO_Immediate:
189         return addImm(Disp.getImm() + off);
190       case MachineOperand::MO_GlobalAddress: {
191         // If caller specifies new TargetFlags then use it, otherwise the
192         // default behavior is to copy the target flags from the existing
193         // MachineOperand. This means if the caller wants to clear the
194         // target flags it needs to do so explicitly.
195         if (TargetFlags)
196           return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
197                                   TargetFlags);
198         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
199                                 Disp.getTargetFlags());
200       }
201     }
202   }
203 };
204
205 /// BuildMI - Builder interface.  Specify how to create the initial instruction
206 /// itself.
207 ///
208 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
209                                    DebugLoc DL,
210                                    const MCInstrDesc &MCID) {
211   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
212 }
213
214 /// BuildMI - This version of the builder sets up the first operand as a
215 /// destination virtual register.
216 ///
217 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
218                                    DebugLoc DL,
219                                    const MCInstrDesc &MCID,
220                                    unsigned DestReg) {
221   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
222            .addReg(DestReg, RegState::Define);
223 }
224
225 /// BuildMI - This version of the builder inserts the newly-built
226 /// instruction before the given position in the given MachineBasicBlock, and
227 /// sets up the first operand as a destination virtual register.
228 ///
229 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
230                                    MachineBasicBlock::iterator I,
231                                    DebugLoc DL,
232                                    const MCInstrDesc &MCID,
233                                    unsigned DestReg) {
234   MachineFunction &MF = *BB.getParent();
235   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
236   BB.insert(I, MI);
237   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
238 }
239
240 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
241                                    MachineBasicBlock::instr_iterator I,
242                                    DebugLoc DL,
243                                    const MCInstrDesc &MCID,
244                                    unsigned DestReg) {
245   MachineFunction &MF = *BB.getParent();
246   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
247   BB.insert(I, MI);
248   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
249 }
250
251 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
252                                    MachineInstr *I,
253                                    DebugLoc DL,
254                                    const MCInstrDesc &MCID,
255                                    unsigned DestReg) {
256   if (I->isInsideBundle()) {
257     MachineBasicBlock::instr_iterator MII = I;
258     return BuildMI(BB, MII, DL, MCID, DestReg);
259   }
260
261   MachineBasicBlock::iterator MII = I;
262   return BuildMI(BB, MII, DL, MCID, DestReg);
263 }
264
265 /// BuildMI - This version of the builder inserts the newly-built
266 /// instruction before the given position in the given MachineBasicBlock, and
267 /// does NOT take a destination register.
268 ///
269 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
270                                    MachineBasicBlock::iterator I,
271                                    DebugLoc DL,
272                                    const MCInstrDesc &MCID) {
273   MachineFunction &MF = *BB.getParent();
274   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
275   BB.insert(I, MI);
276   return MachineInstrBuilder(MF, MI);
277 }
278
279 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
280                                    MachineBasicBlock::instr_iterator I,
281                                    DebugLoc DL,
282                                    const MCInstrDesc &MCID) {
283   MachineFunction &MF = *BB.getParent();
284   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
285   BB.insert(I, MI);
286   return MachineInstrBuilder(MF, MI);
287 }
288
289 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
290                                    MachineInstr *I,
291                                    DebugLoc DL,
292                                    const MCInstrDesc &MCID) {
293   if (I->isInsideBundle()) {
294     MachineBasicBlock::instr_iterator MII = I;
295     return BuildMI(BB, MII, DL, MCID);
296   }
297
298   MachineBasicBlock::iterator MII = I;
299   return BuildMI(BB, MII, DL, MCID);
300 }
301
302 /// BuildMI - This version of the builder inserts the newly-built
303 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
304 /// destination register.
305 ///
306 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
307                                    DebugLoc DL,
308                                    const MCInstrDesc &MCID) {
309   return BuildMI(*BB, BB->end(), DL, MCID);
310 }
311
312 /// BuildMI - This version of the builder inserts the newly-built
313 /// instruction at the end of the given MachineBasicBlock, and sets up the first
314 /// operand as a destination virtual register.
315 ///
316 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
317                                    DebugLoc DL,
318                                    const MCInstrDesc &MCID,
319                                    unsigned DestReg) {
320   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
321 }
322
323 inline unsigned getDefRegState(bool B) {
324   return B ? RegState::Define : 0;
325 }
326 inline unsigned getImplRegState(bool B) {
327   return B ? RegState::Implicit : 0;
328 }
329 inline unsigned getKillRegState(bool B) {
330   return B ? RegState::Kill : 0;
331 }
332 inline unsigned getDeadRegState(bool B) {
333   return B ? RegState::Dead : 0;
334 }
335 inline unsigned getUndefRegState(bool B) {
336   return B ? RegState::Undef : 0;
337 }
338 inline unsigned getInternalReadRegState(bool B) {
339   return B ? RegState::InternalRead : 0;
340 }
341
342
343 /// Helper class for constructing bundles of MachineInstrs.
344 ///
345 /// MIBundleBuilder can create a bundle from scratch by inserting new
346 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
347 /// existing MachineInstrs in a basic block.
348 class MIBundleBuilder {
349   MachineBasicBlock &MBB;
350   MachineBasicBlock::instr_iterator Begin;
351   MachineBasicBlock::instr_iterator End;
352
353 public:
354   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
355   /// BB above the bundle or instruction at Pos.
356   MIBundleBuilder(MachineBasicBlock &BB,
357                   MachineBasicBlock::iterator Pos)
358     : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
359
360   /// Create a bundle from the sequence of instructions between B and E.
361   MIBundleBuilder(MachineBasicBlock &BB,
362                   MachineBasicBlock::iterator B,
363                   MachineBasicBlock::iterator E)
364     : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
365     assert(B != E && "No instructions to bundle");
366     ++B;
367     while (B != E) {
368       MachineInstr *MI = B;
369       ++B;
370       MI->bundleWithPred();
371     }
372   }
373
374   /// Create an MIBundleBuilder representing an existing instruction or bundle
375   /// that has MI as its head.
376   explicit MIBundleBuilder(MachineInstr *MI)
377     : MBB(*MI->getParent()), Begin(MI) {
378     MachineBasicBlock::iterator I = MI;
379     ++I;
380     End = I.getInstrIterator();
381   }
382
383   /// Return a reference to the basic block containing this bundle.
384   MachineBasicBlock &getMBB() const { return MBB; }
385
386   /// Return true if no instructions have been inserted in this bundle yet.
387   /// Empty bundles aren't representable in a MachineBasicBlock.
388   bool empty() const { return Begin == End; }
389
390   /// Return an iterator to the first bundled instruction.
391   MachineBasicBlock::instr_iterator begin() const { return Begin; }
392
393   /// Return an iterator beyond the last bundled instruction.
394   MachineBasicBlock::instr_iterator end() const { return End; }
395
396   /// Insert MI into this bundle before I which must point to an instruction in
397   /// the bundle, or end().
398   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
399                           MachineInstr *MI) {
400     MBB.insert(I, MI);
401     if (I == Begin) {
402       if (!empty())
403         MI->bundleWithSucc();
404       Begin = MI;
405       return *this;
406     }
407     if (I == End) {
408       MI->bundleWithPred();
409       return *this;
410     }
411     // MI was inserted in the middle of the bundle, so its neighbors' flags are
412     // already fine. Update MI's bundle flags manually.
413     MI->setFlag(MachineInstr::BundledPred);
414     MI->setFlag(MachineInstr::BundledSucc);
415     return *this;
416   }
417
418   /// Insert MI into MBB by prepending it to the instructions in the bundle.
419   /// MI will become the first instruction in the bundle.
420   MIBundleBuilder &prepend(MachineInstr *MI) {
421     return insert(begin(), MI);
422   }
423
424   /// Insert MI into MBB by appending it to the instructions in the bundle.
425   /// MI will become the last instruction in the bundle.
426   MIBundleBuilder &append(MachineInstr *MI) {
427     return insert(end(), MI);
428   }
429 };
430
431 } // End llvm namespace
432
433 #endif