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