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