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