Further progration of metadata operands. The
[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
22 namespace llvm {
23
24 class TargetInstrDesc;
25 class MDNode;
26
27 namespace RegState {
28   enum {
29     Define         = 0x2,
30     Implicit       = 0x4,
31     Kill           = 0x8,
32     Dead           = 0x10,
33     Undef          = 0x20,
34     EarlyClobber   = 0x40,
35     ImplicitDefine = Implicit | Define,
36     ImplicitKill   = Implicit | Kill
37   };
38 }
39
40 class MachineInstrBuilder {
41   MachineInstr *MI;
42 public:
43   MachineInstrBuilder() : MI(0) {}
44   explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
45
46   /// Allow automatic conversion to the machine instruction we are working on.
47   ///
48   operator MachineInstr*() const { return MI; }
49   operator MachineBasicBlock::iterator() const { return MI; }
50
51   /// addReg - Add a new virtual register operand...
52   ///
53   const
54   MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
55                               unsigned SubReg = 0) const {
56     assert((flags & 0x1) == 0 &&
57            "Passing in 'true' to addReg is forbidden! Use enums instead.");
58     MI->addOperand(MachineOperand::CreateReg(RegNo,
59                                              flags & RegState::Define,
60                                              flags & RegState::Implicit,
61                                              flags & RegState::Kill,
62                                              flags & RegState::Dead,
63                                              flags & RegState::Undef,
64                                              flags & RegState::EarlyClobber,
65                                              SubReg));
66     return *this;
67   }
68
69   /// addImm - Add a new immediate operand.
70   ///
71   const MachineInstrBuilder &addImm(int64_t Val) const {
72     MI->addOperand(MachineOperand::CreateImm(Val));
73     return *this;
74   }
75
76   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
77     MI->addOperand(MachineOperand::CreateFPImm(Val));
78     return *this;
79   }
80
81   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
82                                     unsigned char TargetFlags = 0) const {
83     MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
84     return *this;
85   }
86
87   const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
88     MI->addOperand(MachineOperand::CreateFI(Idx));
89     return *this;
90   }
91
92   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
93                                                   int Offset = 0,
94                                           unsigned char TargetFlags = 0) const {
95     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
96     return *this;
97   }
98
99   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
100                                           unsigned char TargetFlags = 0) const {
101     MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
102     return *this;
103   }
104
105   const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
106                                               int64_t Offset = 0,
107                                           unsigned char TargetFlags = 0) const {
108     MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
109     return *this;
110   }
111
112   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
113                                           unsigned char TargetFlags = 0) const {
114     MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
115     return *this;
116   }
117
118   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
119     MI->addMemOperand(*MI->getParent()->getParent(), MMO);
120     return *this;
121   }
122
123   const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
124     MI->addOperand(MO);
125     return *this;
126   }
127
128   const MachineInstrBuilder &addMetadata(MDNode *MD) const {
129     MI->addOperand(MachineOperand::CreateMetadata(MD));
130     return *this;
131   }
132 };
133
134 /// BuildMI - Builder interface.  Specify how to create the initial instruction
135 /// itself.
136 ///
137 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
138                                    DebugLoc DL,
139                                    const TargetInstrDesc &TID) {
140   return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
141 }
142
143 /// BuildMI - This version of the builder sets up the first operand as a
144 /// destination virtual register.
145 ///
146 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
147                                    DebugLoc DL,
148                                    const TargetInstrDesc &TID,
149                                    unsigned DestReg) {
150   return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
151            .addReg(DestReg, RegState::Define);
152 }
153
154 /// BuildMI - This version of the builder inserts the newly-built
155 /// instruction before the given position in the given MachineBasicBlock, and
156 /// sets up the first operand as a destination virtual register.
157 ///
158 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
159                                    MachineBasicBlock::iterator I,
160                                    DebugLoc DL,
161                                    const TargetInstrDesc &TID,
162                                    unsigned DestReg) {
163   MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
164   BB.insert(I, MI);
165   return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
166 }
167
168 /// BuildMI - This version of the builder inserts the newly-built
169 /// instruction before the given position in the given MachineBasicBlock, and
170 /// does NOT take a destination register.
171 ///
172 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
173                                    MachineBasicBlock::iterator I,
174                                    DebugLoc DL,
175                                    const TargetInstrDesc &TID) {
176   MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
177   BB.insert(I, MI);
178   return MachineInstrBuilder(MI);
179 }
180
181 /// BuildMI - This version of the builder inserts the newly-built
182 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
183 /// destination register.
184 ///
185 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
186                                    DebugLoc DL,
187                                    const TargetInstrDesc &TID) {
188   return BuildMI(*BB, BB->end(), DL, TID);
189 }
190
191 /// BuildMI - This version of the builder inserts the newly-built
192 /// instruction at the end of the given MachineBasicBlock, and sets up the first
193 /// operand as a destination virtual register.
194 ///
195 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
196                                    DebugLoc DL,
197                                    const TargetInstrDesc &TID,
198                                    unsigned DestReg) {
199   return BuildMI(*BB, BB->end(), DL, TID, DestReg);
200 }
201
202 inline unsigned getDefRegState(bool B) {
203   return B ? RegState::Define : 0;
204 }
205 inline unsigned getImplRegState(bool B) {
206   return B ? RegState::Implicit : 0;
207 }
208 inline unsigned getKillRegState(bool B) {
209   return B ? RegState::Kill : 0;
210 }
211 inline unsigned getDeadRegState(bool B) {
212   return B ? RegState::Dead : 0;
213 }
214 inline unsigned getUndefRegState(bool B) {
215   return B ? RegState::Undef : 0;
216 }
217
218 } // End llvm namespace
219
220 #endif