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 ImplicitDefine = Implicit | Define,
38 ImplicitKill = Implicit | Kill
42 class MachineInstrBuilder {
45 MachineInstrBuilder() : MI(0) {}
46 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
48 /// Allow automatic conversion to the machine instruction we are working on.
50 operator MachineInstr*() const { return MI; }
51 MachineInstr *operator->() const { return MI; }
52 operator MachineBasicBlock::iterator() const { return MI; }
54 /// addReg - Add a new virtual register operand...
57 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
58 unsigned SubReg = 0) const {
59 assert((flags & 0x1) == 0 &&
60 "Passing in 'true' to addReg is forbidden! Use enums instead.");
61 MI->addOperand(MachineOperand::CreateReg(RegNo,
62 flags & RegState::Define,
63 flags & RegState::Implicit,
64 flags & RegState::Kill,
65 flags & RegState::Dead,
66 flags & RegState::Undef,
67 flags & RegState::EarlyClobber,
69 flags & RegState::Debug));
73 /// addImm - Add a new immediate operand.
75 const MachineInstrBuilder &addImm(int64_t Val) const {
76 MI->addOperand(MachineOperand::CreateImm(Val));
80 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
81 MI->addOperand(MachineOperand::CreateCImm(Val));
85 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
86 MI->addOperand(MachineOperand::CreateFPImm(Val));
90 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
91 unsigned char TargetFlags = 0) const {
92 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
96 const MachineInstrBuilder &addFrameIndex(int Idx) const {
97 MI->addOperand(MachineOperand::CreateFI(Idx));
101 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
103 unsigned char TargetFlags = 0) const {
104 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
108 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
109 unsigned char TargetFlags = 0) const {
110 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
114 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
116 unsigned char TargetFlags = 0) const {
117 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
121 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
122 unsigned char TargetFlags = 0) const {
123 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
127 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
128 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
132 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
133 MachineInstr::mmo_iterator e) const {
134 MI->setMemRefs(b, e);
139 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
144 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
145 MI->addOperand(MachineOperand::CreateMetadata(MD));
149 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
150 MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
154 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
159 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
164 // Add a displacement from an existing MachineOperand with an added offset.
165 const MachineInstrBuilder &addDisp(const MachineOperand &Disp,
167 switch (Disp.getType()) {
169 llvm_unreachable("Unhandled operand type in addDisp()");
170 case MachineOperand::MO_Immediate:
171 return addImm(Disp.getImm() + off);
172 case MachineOperand::MO_GlobalAddress:
173 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off);
178 /// BuildMI - Builder interface. Specify how to create the initial instruction
181 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
183 const MCInstrDesc &MCID) {
184 return MachineInstrBuilder(MF.CreateMachineInstr(MCID, DL));
187 /// BuildMI - This version of the builder sets up the first operand as a
188 /// destination virtual register.
190 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
192 const MCInstrDesc &MCID,
194 return MachineInstrBuilder(MF.CreateMachineInstr(MCID, DL))
195 .addReg(DestReg, RegState::Define);
198 /// BuildMI - This version of the builder inserts the newly-built
199 /// instruction before the given position in the given MachineBasicBlock, and
200 /// sets up the first operand as a destination virtual register.
202 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
203 MachineBasicBlock::iterator I,
205 const MCInstrDesc &MCID,
207 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
209 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
212 /// BuildMI - This version of the builder inserts the newly-built
213 /// instruction before the given position in the given MachineBasicBlock, and
214 /// does NOT take a destination register.
216 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
217 MachineBasicBlock::iterator I,
219 const MCInstrDesc &MCID) {
220 MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
222 return MachineInstrBuilder(MI);
225 /// BuildMI - This version of the builder inserts the newly-built
226 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
227 /// destination register.
229 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
231 const MCInstrDesc &MCID) {
232 return BuildMI(*BB, BB->end(), DL, MCID);
235 /// BuildMI - This version of the builder inserts the newly-built
236 /// instruction at the end of the given MachineBasicBlock, and sets up the first
237 /// operand as a destination virtual register.
239 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
241 const MCInstrDesc &MCID,
243 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
246 inline unsigned getDefRegState(bool B) {
247 return B ? RegState::Define : 0;
249 inline unsigned getImplRegState(bool B) {
250 return B ? RegState::Implicit : 0;
252 inline unsigned getKillRegState(bool B) {
253 return B ? RegState::Kill : 0;
255 inline unsigned getDeadRegState(bool B) {
256 return B ? RegState::Dead : 0;
258 inline unsigned getUndefRegState(bool B) {
259 return B ? RegState::Undef : 0;
262 } // End llvm namespace