0fc284cbd8522d54d0745736f66406bd3b34aec0
[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
213 /// BuildMI - Builder interface.  Specify how to create the initial instruction
214 /// itself.
215 ///
216 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
217                                    DebugLoc DL,
218                                    const MCInstrDesc &MCID) {
219   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
220 }
221
222 /// BuildMI - This version of the builder sets up the first operand as a
223 /// destination virtual register.
224 ///
225 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
226                                    DebugLoc DL,
227                                    const MCInstrDesc &MCID,
228                                    unsigned DestReg) {
229   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
230            .addReg(DestReg, RegState::Define);
231 }
232
233 /// BuildMI - This version of the builder inserts the newly-built
234 /// instruction before the given position in the given MachineBasicBlock, and
235 /// sets up the first operand as a destination virtual register.
236 ///
237 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
238                                    MachineBasicBlock::iterator I,
239                                    DebugLoc DL,
240                                    const MCInstrDesc &MCID,
241                                    unsigned DestReg) {
242   MachineFunction &MF = *BB.getParent();
243   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
244   BB.insert(I, MI);
245   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
246 }
247
248 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
249                                    MachineBasicBlock::instr_iterator I,
250                                    DebugLoc DL,
251                                    const MCInstrDesc &MCID,
252                                    unsigned DestReg) {
253   MachineFunction &MF = *BB.getParent();
254   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
255   BB.insert(I, MI);
256   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
257 }
258
259 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
260                                    MachineInstr *I,
261                                    DebugLoc DL,
262                                    const MCInstrDesc &MCID,
263                                    unsigned DestReg) {
264   if (I->isInsideBundle()) {
265     MachineBasicBlock::instr_iterator MII = I;
266     return BuildMI(BB, MII, DL, MCID, DestReg);
267   }
268
269   MachineBasicBlock::iterator MII = I;
270   return BuildMI(BB, MII, DL, MCID, DestReg);
271 }
272
273 /// BuildMI - This version of the builder inserts the newly-built
274 /// instruction before the given position in the given MachineBasicBlock, and
275 /// does NOT take a destination register.
276 ///
277 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
278                                    MachineBasicBlock::iterator I,
279                                    DebugLoc DL,
280                                    const MCInstrDesc &MCID) {
281   MachineFunction &MF = *BB.getParent();
282   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
283   BB.insert(I, MI);
284   return MachineInstrBuilder(MF, MI);
285 }
286
287 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
288                                    MachineBasicBlock::instr_iterator I,
289                                    DebugLoc DL,
290                                    const MCInstrDesc &MCID) {
291   MachineFunction &MF = *BB.getParent();
292   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
293   BB.insert(I, MI);
294   return MachineInstrBuilder(MF, MI);
295 }
296
297 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
298                                    MachineInstr *I,
299                                    DebugLoc DL,
300                                    const MCInstrDesc &MCID) {
301   if (I->isInsideBundle()) {
302     MachineBasicBlock::instr_iterator MII = I;
303     return BuildMI(BB, MII, DL, MCID);
304   }
305
306   MachineBasicBlock::iterator MII = I;
307   return BuildMI(BB, MII, DL, MCID);
308 }
309
310 /// BuildMI - This version of the builder inserts the newly-built
311 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
312 /// destination register.
313 ///
314 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
315                                    DebugLoc DL,
316                                    const MCInstrDesc &MCID) {
317   return BuildMI(*BB, BB->end(), DL, MCID);
318 }
319
320 /// BuildMI - This version of the builder inserts the newly-built
321 /// instruction at the end of the given MachineBasicBlock, and sets up the first
322 /// operand as a destination virtual register.
323 ///
324 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
325                                    DebugLoc DL,
326                                    const MCInstrDesc &MCID,
327                                    unsigned DestReg) {
328   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
329 }
330
331 inline unsigned getDefRegState(bool B) {
332   return B ? RegState::Define : 0;
333 }
334 inline unsigned getImplRegState(bool B) {
335   return B ? RegState::Implicit : 0;
336 }
337 inline unsigned getKillRegState(bool B) {
338   return B ? RegState::Kill : 0;
339 }
340 inline unsigned getDeadRegState(bool B) {
341   return B ? RegState::Dead : 0;
342 }
343 inline unsigned getUndefRegState(bool B) {
344   return B ? RegState::Undef : 0;
345 }
346 inline unsigned getInternalReadRegState(bool B) {
347   return B ? RegState::InternalRead : 0;
348 }
349 inline unsigned getDebugRegState(bool B) {
350   return B ? RegState::Debug : 0;
351 }
352
353
354 /// Helper class for constructing bundles of MachineInstrs.
355 ///
356 /// MIBundleBuilder can create a bundle from scratch by inserting new
357 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
358 /// existing MachineInstrs in a basic block.
359 class MIBundleBuilder {
360   MachineBasicBlock &MBB;
361   MachineBasicBlock::instr_iterator Begin;
362   MachineBasicBlock::instr_iterator End;
363
364 public:
365   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
366   /// BB above the bundle or instruction at Pos.
367   MIBundleBuilder(MachineBasicBlock &BB,
368                   MachineBasicBlock::iterator Pos)
369     : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
370
371   /// Create a bundle from the sequence of instructions between B and E.
372   MIBundleBuilder(MachineBasicBlock &BB,
373                   MachineBasicBlock::iterator B,
374                   MachineBasicBlock::iterator E)
375     : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
376     assert(B != E && "No instructions to bundle");
377     ++B;
378     while (B != E) {
379       MachineInstr *MI = B;
380       ++B;
381       MI->bundleWithPred();
382     }
383   }
384
385   /// Create an MIBundleBuilder representing an existing instruction or bundle
386   /// that has MI as its head.
387   explicit MIBundleBuilder(MachineInstr *MI)
388     : MBB(*MI->getParent()), Begin(MI) {
389     MachineBasicBlock::iterator I = MI;
390     ++I;
391     End = I.getInstrIterator();
392   }
393
394   /// Return a reference to the basic block containing this bundle.
395   MachineBasicBlock &getMBB() const { return MBB; }
396
397   /// Return true if no instructions have been inserted in this bundle yet.
398   /// Empty bundles aren't representable in a MachineBasicBlock.
399   bool empty() const { return Begin == End; }
400
401   /// Return an iterator to the first bundled instruction.
402   MachineBasicBlock::instr_iterator begin() const { return Begin; }
403
404   /// Return an iterator beyond the last bundled instruction.
405   MachineBasicBlock::instr_iterator end() const { return End; }
406
407   /// Insert MI into this bundle before I which must point to an instruction in
408   /// the bundle, or end().
409   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
410                           MachineInstr *MI) {
411     MBB.insert(I, MI);
412     if (I == Begin) {
413       if (!empty())
414         MI->bundleWithSucc();
415       Begin = MI;
416       return *this;
417     }
418     if (I == End) {
419       MI->bundleWithPred();
420       return *this;
421     }
422     // MI was inserted in the middle of the bundle, so its neighbors' flags are
423     // already fine. Update MI's bundle flags manually.
424     MI->setFlag(MachineInstr::BundledPred);
425     MI->setFlag(MachineInstr::BundledSucc);
426     return *this;
427   }
428
429   /// Insert MI into MBB by prepending it to the instructions in the bundle.
430   /// MI will become the first instruction in the bundle.
431   MIBundleBuilder &prepend(MachineInstr *MI) {
432     return insert(begin(), MI);
433   }
434
435   /// Insert MI into MBB by appending it to the instructions in the bundle.
436   /// MI will become the last instruction in the bundle.
437   MIBundleBuilder &append(MachineInstr *MI) {
438     return insert(end(), MI);
439   }
440 };
441
442 } // End llvm namespace
443
444 #endif