1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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:
13 // M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
18 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/Support/ErrorHandling.h"
37 DefineNoRead = Define | Undef,
38 ImplicitDefine = Implicit | Define,
39 ImplicitKill = Implicit | Kill
43 class MachineInstrBuilder {
46 MachineInstrBuilder() : MI(0) {}
47 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
49 /// Allow automatic conversion to the machine instruction we are working on.
51 operator MachineInstr*() const { return MI; }
52 MachineInstr *operator->() const { return MI; }
53 operator MachineBasicBlock::iterator() const { return MI; }
55 /// addReg - Add a new virtual register operand...
58 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
59 unsigned SubReg = 0) const {
60 assert((flags & 0x1) == 0 &&
61 "Passing in 'true' to addReg is forbidden! Use enums instead.");
62 MI->addOperand(MachineOperand::CreateReg(RegNo,
63 flags & RegState::Define,
64 flags & RegState::Implicit,
65 flags & RegState::Kill,
66 flags & RegState::Dead,
67 flags & RegState::Undef,
68 flags & RegState::EarlyClobber,
70 flags & RegState::Debug));
74 /// addImm - Add a new immediate operand.
76 const MachineInstrBuilder &addImm(int64_t Val) const {
77 MI->addOperand(MachineOperand::CreateImm(Val));
81 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
82 MI->addOperand(MachineOperand::CreateCImm(Val));
86 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
87 MI->addOperand(MachineOperand::CreateFPImm(Val));
91 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
92 unsigned char TargetFlags = 0) const {
93 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
97 const MachineInstrBuilder &addFrameIndex(int Idx) const {
98 MI->addOperand(MachineOperand::CreateFI(Idx));
102 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
104 unsigned char TargetFlags = 0) const {
105 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
109 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
110 unsigned char TargetFlags = 0) const {
111 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
115 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
117 unsigned char TargetFlags = 0) const {
118 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
122 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
123 unsigned char TargetFlags = 0) const {
124 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
128 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
129 MI->addOperand(MachineOperand::CreateRegMask(Mask));
133 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
134 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
138 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
139 MachineInstr::mmo_iterator e) const {
140 MI->setMemRefs(b, e);
145 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
150 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
151 MI->addOperand(MachineOperand::CreateMetadata(MD));
155 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
156 MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
160 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
165 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
170 // Add a displacement from an existing MachineOperand with an added offset.
171 const MachineInstrBuilder &addDisp(const MachineOperand &Disp,
173 switch (Disp.getType()) {
175 llvm_unreachable("Unhandled operand type in addDisp()");
176 case MachineOperand::MO_Immediate:
177 return addImm(Disp.getImm() + off);
178 case MachineOperand::MO_GlobalAddress:
179 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off);
184 /// BuildMI - Builder interface. Specify how to create the initial instruction
187 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
189 const MCInstrDesc &MCID) {
190 return MachineInstrBuilder(MF.CreateMachineInstr(MCID, DL));
193 /// BuildMI - This version of the builder sets up the first operand as a
194 /// destination virtual register.
196 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
198 const MCInstrDesc &MCID,
200 return MachineInstrBuilder(MF.CreateMachineInstr(MCID, DL))
201 .addReg(DestReg, RegState::Define);
204 /// BuildMI - This version of the builder inserts the newly-built
205 /// instruction before the given position in the given MachineBasicBlock, and
206 /// sets up the first operand as a destination virtual register.
208 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
209 MachineBasicBlock::iterator I,
211 const MCInstrDesc &MCID,
213 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
215 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
218 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
219 MachineBasicBlock::instr_iterator I,
221 const MCInstrDesc &MCID,
223 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
225 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
228 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
231 const MCInstrDesc &MCID,
233 if (I->isInsideBundle()) {
234 MachineBasicBlock::instr_iterator MII = I;
235 return BuildMI(BB, MII, DL, MCID, DestReg);
238 MachineBasicBlock::iterator MII = I;
239 return BuildMI(BB, MII, DL, MCID, DestReg);
242 /// BuildMI - This version of the builder inserts the newly-built
243 /// instruction before the given position in the given MachineBasicBlock, and
244 /// does NOT take a destination register.
246 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
247 MachineBasicBlock::iterator I,
249 const MCInstrDesc &MCID) {
250 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
252 return MachineInstrBuilder(MI);
255 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
256 MachineBasicBlock::instr_iterator I,
258 const MCInstrDesc &MCID) {
259 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
261 return MachineInstrBuilder(MI);
264 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
267 const MCInstrDesc &MCID) {
268 if (I->isInsideBundle()) {
269 MachineBasicBlock::instr_iterator MII = I;
270 return BuildMI(BB, MII, DL, MCID);
273 MachineBasicBlock::iterator MII = I;
274 return BuildMI(BB, MII, DL, MCID);
277 /// BuildMI - This version of the builder inserts the newly-built
278 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
279 /// destination register.
281 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
283 const MCInstrDesc &MCID) {
284 return BuildMI(*BB, BB->end(), DL, MCID);
287 /// BuildMI - This version of the builder inserts the newly-built
288 /// instruction at the end of the given MachineBasicBlock, and sets up the first
289 /// operand as a destination virtual register.
291 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
293 const MCInstrDesc &MCID,
295 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
298 inline unsigned getDefRegState(bool B) {
299 return B ? RegState::Define : 0;
301 inline unsigned getImplRegState(bool B) {
302 return B ? RegState::Implicit : 0;
304 inline unsigned getKillRegState(bool B) {
305 return B ? RegState::Kill : 0;
307 inline unsigned getDeadRegState(bool B) {
308 return B ? RegState::Dead : 0;
310 inline unsigned getUndefRegState(bool B) {
311 return B ? RegState::Undef : 0;
314 } // End llvm namespace