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