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"
25 class TargetInstrDesc;
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 operator MachineBasicBlock::iterator() const { return MI; }
53 /// addReg - Add a new virtual register operand...
56 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
57 unsigned SubReg = 0) const {
58 assert((flags & 0x1) == 0 &&
59 "Passing in 'true' to addReg is forbidden! Use enums instead.");
60 MI->addOperand(MachineOperand::CreateReg(RegNo,
61 flags & RegState::Define,
62 flags & RegState::Implicit,
63 flags & RegState::Kill,
64 flags & RegState::Dead,
65 flags & RegState::Undef,
66 flags & RegState::EarlyClobber,
68 flags & RegState::Debug));
72 /// addImm - Add a new immediate operand.
74 const MachineInstrBuilder &addImm(int64_t Val) const {
75 MI->addOperand(MachineOperand::CreateImm(Val));
79 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
80 MI->addOperand(MachineOperand::CreateFPImm(Val));
84 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
85 unsigned char TargetFlags = 0) const {
86 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
90 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
91 MI->addOperand(MachineOperand::CreateFI(Idx));
95 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
97 unsigned char TargetFlags = 0) const {
98 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
102 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
103 unsigned char TargetFlags = 0) const {
104 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
108 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
110 unsigned char TargetFlags = 0) const {
111 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
115 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
116 unsigned char TargetFlags = 0) const {
117 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
121 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
122 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
126 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
127 MachineInstr::mmo_iterator e) const {
128 MI->setMemRefs(b, e);
133 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
138 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
139 MI->addOperand(MachineOperand::CreateMetadata(MD));
143 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
144 MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
148 // Add a displacement from an existing MachineOperand with an added offset.
149 const MachineInstrBuilder &addDisp(const MachineOperand &Disp,
151 switch (Disp.getType()) {
153 llvm_unreachable("Unhandled operand type in addDisp()");
154 case MachineOperand::MO_Immediate:
155 return addImm(Disp.getImm() + off);
156 case MachineOperand::MO_GlobalAddress:
157 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off);
162 /// BuildMI - Builder interface. Specify how to create the initial instruction
165 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
167 const TargetInstrDesc &TID) {
168 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
171 /// BuildMI - This version of the builder sets up the first operand as a
172 /// destination virtual register.
174 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
176 const TargetInstrDesc &TID,
178 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
179 .addReg(DestReg, RegState::Define);
182 /// BuildMI - This version of the builder inserts the newly-built
183 /// instruction before the given position in the given MachineBasicBlock, and
184 /// sets up the first operand as a destination virtual register.
186 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
187 MachineBasicBlock::iterator I,
189 const TargetInstrDesc &TID,
191 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
193 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
196 /// BuildMI - This version of the builder inserts the newly-built
197 /// instruction before the given position in the given MachineBasicBlock, and
198 /// does NOT take a destination register.
200 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
201 MachineBasicBlock::iterator I,
203 const TargetInstrDesc &TID) {
204 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
206 return MachineInstrBuilder(MI);
209 /// BuildMI - This version of the builder inserts the newly-built
210 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
211 /// destination register.
213 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
215 const TargetInstrDesc &TID) {
216 return BuildMI(*BB, BB->end(), DL, TID);
219 /// BuildMI - This version of the builder inserts the newly-built
220 /// instruction at the end of the given MachineBasicBlock, and sets up the first
221 /// operand as a destination virtual register.
223 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
225 const TargetInstrDesc &TID,
227 return BuildMI(*BB, BB->end(), DL, TID, DestReg);
230 inline unsigned getDefRegState(bool B) {
231 return B ? RegState::Define : 0;
233 inline unsigned getImplRegState(bool B) {
234 return B ? RegState::Implicit : 0;
236 inline unsigned getKillRegState(bool B) {
237 return B ? RegState::Kill : 0;
239 inline unsigned getDeadRegState(bool B) {
240 return B ? RegState::Dead : 0;
242 inline unsigned getUndefRegState(bool B) {
243 return B ? RegState::Undef : 0;
246 } // End llvm namespace