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