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