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"
24 class TargetInstrDesc;
33 ImplicitDefine = Implicit | Define,
34 ImplicitKill = Implicit | Kill
38 class MachineInstrBuilder {
41 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
43 /// Allow automatic conversion to the machine instruction we are working on.
45 operator MachineInstr*() const { return MI; }
46 operator MachineBasicBlock::iterator() const { return MI; }
48 /// addReg - Add a new virtual register operand...
51 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
52 unsigned SubReg = 0) const {
53 assert((flags & 0x1) == 0 &&
54 "Passing in 'true' to addReg is forbidden! Use enums instead.");
55 MI->addOperand(MachineOperand::CreateReg(RegNo,
56 flags & RegState::Define,
57 flags & RegState::Implicit,
58 flags & RegState::Kill,
59 flags & RegState::Dead,
61 flags & RegState::EarlyClobber));
65 /// addImm - Add a new immediate operand.
67 const MachineInstrBuilder &addImm(int64_t Val) const {
68 MI->addOperand(MachineOperand::CreateImm(Val));
72 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
73 MI->addOperand(MachineOperand::CreateFPImm(Val));
77 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
78 unsigned char TargetFlags = 0) const {
79 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
83 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
84 MI->addOperand(MachineOperand::CreateFI(Idx));
88 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
90 unsigned char TargetFlags = 0) const {
91 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
95 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
96 unsigned char TargetFlags = 0) const {
97 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
101 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
103 unsigned char TargetFlags = 0) const {
104 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
108 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
110 unsigned char TargetFlags = 0) const {
111 MI->addOperand(MachineOperand::CreateES(FnName, Offset, TargetFlags));
115 const MachineInstrBuilder &addMemOperand(const MachineMemOperand &MMO) const {
116 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
120 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
122 return addReg(MO.getReg(),
123 (MO.isDef() ? RegState::Define : 0) |
124 (MO.isImplicit() ? RegState::Implicit : 0) |
125 (MO.isKill() ? RegState::Kill : 0) |
126 (MO.isDead() ? RegState::Dead : 0) |
127 (MO.isEarlyClobber() ? RegState::EarlyClobber : 0),
130 return addImm(MO.getImm());
132 return addFrameIndex(MO.getIndex());
134 return addGlobalAddress(MO.getGlobal(), MO.getOffset(),
135 MO.getTargetFlags());
137 return addMBB(MO.getMBB(), MO.getTargetFlags());
139 return addConstantPoolIndex(MO.getIndex(), MO.getOffset(),
140 MO.getTargetFlags());
142 return addExternalSymbol(MO.getSymbolName(), MO.getOffset(),
143 MO.getTargetFlags());
145 return addJumpTableIndex(MO.getIndex(),
146 MO.getTargetFlags());
148 assert(0 && "Unknown operand for MachineInstrBuilder::AddOperand!");
153 /// BuildMI - Builder interface. Specify how to create the initial instruction
156 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
158 const TargetInstrDesc &TID) {
159 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
162 /// BuildMI - This version of the builder sets up the first operand as a
163 /// destination virtual register.
165 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
167 const TargetInstrDesc &TID,
169 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
170 .addReg(DestReg, RegState::Define);
173 /// BuildMI - This version of the builder inserts the newly-built
174 /// instruction before the given position in the given MachineBasicBlock, and
175 /// sets up the first operand as a destination virtual register.
177 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
178 MachineBasicBlock::iterator I,
180 const TargetInstrDesc &TID,
182 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
184 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
187 /// BuildMI - This version of the builder inserts the newly-built
188 /// instruction before the given position in the given MachineBasicBlock, and
189 /// does NOT take a destination register.
191 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
192 MachineBasicBlock::iterator I,
194 const TargetInstrDesc &TID) {
195 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
197 return MachineInstrBuilder(MI);
200 /// BuildMI - This version of the builder inserts the newly-built
201 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
202 /// destination register.
204 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
206 const TargetInstrDesc &TID) {
207 return BuildMI(*BB, BB->end(), DL, TID);
210 /// BuildMI - This version of the builder inserts the newly-built
211 /// instruction at the end of the given MachineBasicBlock, and sets up the first
212 /// operand as a destination virtual register.
214 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
216 const TargetInstrDesc &TID,
218 return BuildMI(*BB, BB->end(), DL, TID, DestReg);
221 inline unsigned getDefRegState(bool B) {
222 return B ? RegState::Define : 0;
224 inline unsigned getImplRegState(bool B) {
225 return B ? RegState::Implicit : 0;
227 inline unsigned getKillRegState(bool B) {
228 return B ? RegState::Kill : 0;
230 inline unsigned getDeadRegState(bool B) {
231 return B ? RegState::Dead : 0;
234 } // End llvm namespace