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"
38 DefineNoRead = Define | Undef,
39 ImplicitDefine = Implicit | Define,
40 ImplicitKill = Implicit | Kill
44 class MachineInstrBuilder {
47 MachineInstrBuilder() : MI(0) {}
48 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
50 /// Allow automatic conversion to the machine instruction we are working on.
52 operator MachineInstr*() const { return MI; }
53 MachineInstr *operator->() const { return MI; }
54 operator MachineBasicBlock::iterator() const { return MI; }
56 /// addReg - Add a new virtual register operand...
59 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
60 unsigned SubReg = 0) const {
61 assert((flags & 0x1) == 0 &&
62 "Passing in 'true' to addReg is forbidden! Use enums instead.");
63 MI->addOperand(MachineOperand::CreateReg(RegNo,
64 flags & RegState::Define,
65 flags & RegState::Implicit,
66 flags & RegState::Kill,
67 flags & RegState::Dead,
68 flags & RegState::Undef,
69 flags & RegState::EarlyClobber,
71 flags & RegState::Debug,
72 flags & RegState::InternalRead));
76 /// addImm - Add a new immediate operand.
78 const MachineInstrBuilder &addImm(int64_t Val) const {
79 MI->addOperand(MachineOperand::CreateImm(Val));
83 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
84 MI->addOperand(MachineOperand::CreateCImm(Val));
88 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
89 MI->addOperand(MachineOperand::CreateFPImm(Val));
93 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
94 unsigned char TargetFlags = 0) const {
95 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
99 const MachineInstrBuilder &addFrameIndex(int Idx) const {
100 MI->addOperand(MachineOperand::CreateFI(Idx));
104 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
106 unsigned char TargetFlags = 0) const {
107 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
111 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
112 unsigned char TargetFlags = 0) const {
113 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
117 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
119 unsigned char TargetFlags = 0) const {
120 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
124 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
125 unsigned char TargetFlags = 0) const {
126 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
130 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
131 MI->addOperand(MachineOperand::CreateRegMask(Mask));
135 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
136 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
140 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
141 MachineInstr::mmo_iterator e) const {
142 MI->setMemRefs(b, e);
147 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
152 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
153 MI->addOperand(MachineOperand::CreateMetadata(MD));
157 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
158 MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
162 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
167 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
172 // Add a displacement from an existing MachineOperand with an added offset.
173 const MachineInstrBuilder &addDisp(const MachineOperand &Disp,
175 switch (Disp.getType()) {
177 llvm_unreachable("Unhandled operand type in addDisp()");
178 case MachineOperand::MO_Immediate:
179 return addImm(Disp.getImm() + off);
180 case MachineOperand::MO_GlobalAddress:
181 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off);
186 /// BuildMI - Builder interface. Specify how to create the initial instruction
189 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
191 const MCInstrDesc &MCID) {
192 return MachineInstrBuilder(MF.CreateMachineInstr(MCID, DL));
195 /// BuildMI - This version of the builder sets up the first operand as a
196 /// destination virtual register.
198 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
200 const MCInstrDesc &MCID,
202 return MachineInstrBuilder(MF.CreateMachineInstr(MCID, DL))
203 .addReg(DestReg, RegState::Define);
206 /// BuildMI - This version of the builder inserts the newly-built
207 /// instruction before the given position in the given MachineBasicBlock, and
208 /// sets up the first operand as a destination virtual register.
210 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
211 MachineBasicBlock::iterator I,
213 const MCInstrDesc &MCID,
215 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
217 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
220 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
221 MachineBasicBlock::instr_iterator I,
223 const MCInstrDesc &MCID,
225 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
227 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
230 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
233 const MCInstrDesc &MCID,
235 if (I->isInsideBundle()) {
236 MachineBasicBlock::instr_iterator MII = I;
237 return BuildMI(BB, MII, DL, MCID, DestReg);
240 MachineBasicBlock::iterator MII = I;
241 return BuildMI(BB, MII, DL, MCID, DestReg);
244 /// BuildMI - This version of the builder inserts the newly-built
245 /// instruction before the given position in the given MachineBasicBlock, and
246 /// does NOT take a destination register.
248 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
249 MachineBasicBlock::iterator I,
251 const MCInstrDesc &MCID) {
252 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
254 return MachineInstrBuilder(MI);
257 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
258 MachineBasicBlock::instr_iterator I,
260 const MCInstrDesc &MCID) {
261 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
263 return MachineInstrBuilder(MI);
266 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
269 const MCInstrDesc &MCID) {
270 if (I->isInsideBundle()) {
271 MachineBasicBlock::instr_iterator MII = I;
272 return BuildMI(BB, MII, DL, MCID);
275 MachineBasicBlock::iterator MII = I;
276 return BuildMI(BB, MII, DL, MCID);
279 /// BuildMI - This version of the builder inserts the newly-built
280 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
281 /// destination register.
283 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
285 const MCInstrDesc &MCID) {
286 return BuildMI(*BB, BB->end(), DL, MCID);
289 /// BuildMI - This version of the builder inserts the newly-built
290 /// instruction at the end of the given MachineBasicBlock, and sets up the first
291 /// operand as a destination virtual register.
293 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
295 const MCInstrDesc &MCID,
297 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
300 inline unsigned getDefRegState(bool B) {
301 return B ? RegState::Define : 0;
303 inline unsigned getImplRegState(bool B) {
304 return B ? RegState::Implicit : 0;
306 inline unsigned getKillRegState(bool B) {
307 return B ? RegState::Kill : 0;
309 inline unsigned getDeadRegState(bool B) {
310 return B ? RegState::Dead : 0;
312 inline unsigned getUndefRegState(bool B) {
313 return B ? RegState::Undef : 0;
315 inline unsigned getInternalReadRegState(bool B) {
316 return B ? RegState::InternalRead : 0;
319 } // End llvm namespace