Add an analyzeVirtReg() function.
[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     ImplicitDefine = Implicit | Define,
38     ImplicitKill   = Implicit | Kill
39   };
40 }
41
42 class MachineInstrBuilder {
43   MachineInstr *MI;
44 public:
45   MachineInstrBuilder() : MI(0) {}
46   explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
47
48   /// Allow automatic conversion to the machine instruction we are working on.
49   ///
50   operator MachineInstr*() const { return MI; }
51   MachineInstr *operator->() const { return MI; }
52   operator MachineBasicBlock::iterator() const { return MI; }
53
54   /// addReg - Add a new virtual register operand...
55   ///
56   const
57   MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
58                               unsigned SubReg = 0) const {
59     assert((flags & 0x1) == 0 &&
60            "Passing in 'true' to addReg is forbidden! Use enums instead.");
61     MI->addOperand(MachineOperand::CreateReg(RegNo,
62                                              flags & RegState::Define,
63                                              flags & RegState::Implicit,
64                                              flags & RegState::Kill,
65                                              flags & RegState::Dead,
66                                              flags & RegState::Undef,
67                                              flags & RegState::EarlyClobber,
68                                              SubReg,
69                                              flags & RegState::Debug));
70     return *this;
71   }
72
73   /// addImm - Add a new immediate operand.
74   ///
75   const MachineInstrBuilder &addImm(int64_t Val) const {
76     MI->addOperand(MachineOperand::CreateImm(Val));
77     return *this;
78   }
79
80   const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
81     MI->addOperand(MachineOperand::CreateCImm(Val));
82     return *this;
83   }
84
85   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
86     MI->addOperand(MachineOperand::CreateFPImm(Val));
87     return *this;
88   }
89
90   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
91                                     unsigned char TargetFlags = 0) const {
92     MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
93     return *this;
94   }
95
96   const MachineInstrBuilder &addFrameIndex(int Idx) const {
97     MI->addOperand(MachineOperand::CreateFI(Idx));
98     return *this;
99   }
100
101   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
102                                                   int Offset = 0,
103                                           unsigned char TargetFlags = 0) const {
104     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
105     return *this;
106   }
107
108   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
109                                           unsigned char TargetFlags = 0) const {
110     MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
111     return *this;
112   }
113
114   const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
115                                               int64_t Offset = 0,
116                                           unsigned char TargetFlags = 0) const {
117     MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
118     return *this;
119   }
120
121   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
122                                           unsigned char TargetFlags = 0) const {
123     MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
124     return *this;
125   }
126
127   const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
128     MI->addOperand(MachineOperand::CreateRegMask(Mask));
129     return *this;
130   }
131
132   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
133     MI->addMemOperand(*MI->getParent()->getParent(), MMO);
134     return *this;
135   }
136
137   const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
138                                         MachineInstr::mmo_iterator e) const {
139     MI->setMemRefs(b, e);
140     return *this;
141   }
142
143
144   const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
145     MI->addOperand(MO);
146     return *this;
147   }
148
149   const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
150     MI->addOperand(MachineOperand::CreateMetadata(MD));
151     return *this;
152   }
153   
154   const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
155     MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
156     return *this;
157   }
158
159   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
160     MI->setFlags(Flags);
161     return *this;
162   }
163
164   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
165     MI->setFlag(Flag);
166     return *this;
167   }
168
169   // Add a displacement from an existing MachineOperand with an added offset.
170   const MachineInstrBuilder &addDisp(const MachineOperand &Disp,
171                                      int64_t off) const {
172     switch (Disp.getType()) {
173       default:
174         llvm_unreachable("Unhandled operand type in addDisp()");
175       case MachineOperand::MO_Immediate:
176         return addImm(Disp.getImm() + off);
177       case MachineOperand::MO_GlobalAddress:
178         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off);
179     }
180   }
181 };
182
183 /// BuildMI - Builder interface.  Specify how to create the initial instruction
184 /// itself.
185 ///
186 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
187                                    DebugLoc DL,
188                                    const MCInstrDesc &MCID) {
189   return MachineInstrBuilder(MF.CreateMachineInstr(MCID, DL));
190 }
191
192 /// BuildMI - This version of the builder sets up the first operand as a
193 /// destination virtual register.
194 ///
195 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
196                                    DebugLoc DL,
197                                    const MCInstrDesc &MCID,
198                                    unsigned DestReg) {
199   return MachineInstrBuilder(MF.CreateMachineInstr(MCID, DL))
200            .addReg(DestReg, RegState::Define);
201 }
202
203 /// BuildMI - This version of the builder inserts the newly-built
204 /// instruction before the given position in the given MachineBasicBlock, and
205 /// sets up the first operand as a destination virtual register.
206 ///
207 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
208                                    MachineBasicBlock::iterator I,
209                                    DebugLoc DL,
210                                    const MCInstrDesc &MCID,
211                                    unsigned DestReg) {
212   MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
213   BB.insert(I, MI);
214   return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
215 }
216
217 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
218                                    MachineBasicBlock::instr_iterator I,
219                                    DebugLoc DL,
220                                    const MCInstrDesc &MCID,
221                                    unsigned DestReg) {
222   MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
223   BB.insert(I, MI);
224   return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
225 }
226
227 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
228                                    MachineInstr *I,
229                                    DebugLoc DL,
230                                    const MCInstrDesc &MCID,
231                                    unsigned DestReg) {
232   if (I->isInsideBundle()) {
233     MachineBasicBlock::instr_iterator MII = I;
234     return BuildMI(BB, MII, DL, MCID, DestReg);
235   }
236
237   MachineBasicBlock::iterator MII = I;
238   return BuildMI(BB, MII, DL, MCID, DestReg);
239 }
240
241 /// BuildMI - This version of the builder inserts the newly-built
242 /// instruction before the given position in the given MachineBasicBlock, and
243 /// does NOT take a destination register.
244 ///
245 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
246                                    MachineBasicBlock::iterator I,
247                                    DebugLoc DL,
248                                    const MCInstrDesc &MCID) {
249   MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
250   BB.insert(I, MI);
251   return MachineInstrBuilder(MI);
252 }
253
254 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
255                                    MachineBasicBlock::instr_iterator I,
256                                    DebugLoc DL,
257                                    const MCInstrDesc &MCID) {
258   MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
259   BB.insert(I, MI);
260   return MachineInstrBuilder(MI);
261 }
262
263 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
264                                    MachineInstr *I,
265                                    DebugLoc DL,
266                                    const MCInstrDesc &MCID) {
267   if (I->isInsideBundle()) {
268     MachineBasicBlock::instr_iterator MII = I;
269     return BuildMI(BB, MII, DL, MCID);
270   }
271
272   MachineBasicBlock::iterator MII = I;
273   return BuildMI(BB, MII, DL, MCID);
274 }
275
276 /// BuildMI - This version of the builder inserts the newly-built
277 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
278 /// destination register.
279 ///
280 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
281                                    DebugLoc DL,
282                                    const MCInstrDesc &MCID) {
283   return BuildMI(*BB, BB->end(), DL, MCID);
284 }
285
286 /// BuildMI - This version of the builder inserts the newly-built
287 /// instruction at the end of the given MachineBasicBlock, and sets up the first
288 /// operand as a destination virtual register.
289 ///
290 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
291                                    DebugLoc DL,
292                                    const MCInstrDesc &MCID,
293                                    unsigned DestReg) {
294   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
295 }
296
297 inline unsigned getDefRegState(bool B) {
298   return B ? RegState::Define : 0;
299 }
300 inline unsigned getImplRegState(bool B) {
301   return B ? RegState::Implicit : 0;
302 }
303 inline unsigned getKillRegState(bool B) {
304   return B ? RegState::Kill : 0;
305 }
306 inline unsigned getDeadRegState(bool B) {
307   return B ? RegState::Dead : 0;
308 }
309 inline unsigned getUndefRegState(bool B) {
310   return B ? RegState::Undef : 0;
311 }
312
313 } // End llvm namespace
314
315 #endif