1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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. Instead of using code like this:
13 // M = new MachineInstr(X86::ADDrr8);
14 // M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, argVal1);
15 // M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, argVal2);
17 // we can now use code like this:
19 // M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
24 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
26 #include "llvm/CodeGen/MachineBasicBlock.h"
30 class MachineInstrBuilder {
33 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
35 /// Allow automatic conversion to the machine instruction we are working on.
37 operator MachineInstr*() const { return MI; }
38 operator MachineBasicBlock::iterator() const { return MI; }
40 /// addReg - Add a new virtual register operand...
42 const MachineInstrBuilder &addReg(
44 MachineOperand::UseType Ty = MachineOperand::Use) const {
45 MI->addRegOperand(RegNo, Ty);
49 /// addReg - Add an LLVM value that is to be used as a register...
51 const MachineInstrBuilder &addReg(
53 MachineOperand::UseType Ty = MachineOperand::Use) const {
54 MI->addRegOperand(V, Ty);
58 /// addReg - Add an LLVM value that is to be used as a register...
60 const MachineInstrBuilder &addCCReg(
62 MachineOperand::UseType Ty = MachineOperand::Use) const {
63 MI->addCCRegOperand(V, Ty);
67 /// addRegDef - Add an LLVM value that is to be defined as a register... this
68 /// is the same as addReg(V, MachineOperand::Def).
70 const MachineInstrBuilder &addRegDef(Value *V) const {
71 return addReg(V, MachineOperand::Def);
74 /// addPCDisp - Add an LLVM value to be treated as a PC relative
77 const MachineInstrBuilder &addPCDisp(Value *V) const {
78 MI->addPCDispOperand(V);
82 /// addMReg - Add a machine register operand...
84 const MachineInstrBuilder &addMReg(int Reg, MachineOperand::UseType Ty
85 = MachineOperand::Use) const {
86 MI->addMachineRegOperand(Reg, Ty);
90 /// addImm - Add a new immediate operand.
92 const MachineInstrBuilder &addImm(int Val) const {
93 MI->addZeroExtImmOperand(Val);
97 /// addSImm - Add a new sign extended immediate operand...
99 const MachineInstrBuilder &addSImm(int val) const {
100 MI->addSignExtImmOperand(val);
104 /// addZImm - Add a new zero extended immediate operand...
106 const MachineInstrBuilder &addZImm(unsigned Val) const {
107 MI->addZeroExtImmOperand(Val);
111 /// addImm64 - Add a new 64-bit immediate operand...
113 const MachineInstrBuilder &addImm64(uint64_t Val) const {
114 MI->addZeroExtImm64Operand(Val);
118 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
119 MI->addMachineBasicBlockOperand(MBB);
123 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
124 MI->addFrameIndexOperand(Idx);
128 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
129 int Offset = 0) const {
130 MI->addConstantPoolIndexOperand(Idx, Offset);
134 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
135 MI->addJumpTableIndexOperand(Idx);
139 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
140 bool isPCRelative = false,
141 int Offset = 0) const {
142 MI->addGlobalAddressOperand(GV, isPCRelative, Offset);
146 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
147 bool isPCRelative = false) const{
148 MI->addExternalSymbolOperand(FnName, isPCRelative);
153 /// BuildMI - Builder interface. Specify how to create the initial instruction
154 /// itself. NumOperands is the number of operands to the machine instruction to
155 /// allow for memory efficient representation of machine instructions.
157 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
158 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
161 /// BuildMI - This version of the builder sets up the first operand as a
162 /// destination virtual register. NumOperands is the number of additional add*
163 /// calls that are expected, not including the destination register.
165 inline MachineInstrBuilder BuildMI(
166 int Opcode, unsigned NumOperands,
168 MachineOperand::UseType useType = MachineOperand::Def) {
169 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
170 true, true)).addReg(DestReg, useType);
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.
176 /// NumOperands is the number of additional add* calls that are expected,
177 /// not including the destination register.
179 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
180 MachineBasicBlock::iterator I,
181 int Opcode, unsigned NumOperands,
183 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
185 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
188 /// BuildMI - This version of the builder inserts the newly-built
189 /// instruction before the given position in the given MachineBasicBlock, and
190 /// does NOT take a destination register.
192 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
193 MachineBasicBlock::iterator I,
194 int Opcode, unsigned NumOperands) {
195 MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
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, int Opcode,
205 unsigned NumOperands) {
206 return BuildMI(*BB, BB->end(), Opcode, NumOperands);
209 /// BuildMI - This version of the builder inserts the newly-built
210 /// instruction at the end of the given MachineBasicBlock, and sets up the first
211 /// operand as a destination virtual register. NumOperands is the number of
212 /// additional add* calls that are expected, not including the destination
215 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
216 unsigned NumOperands, unsigned DestReg) {
217 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
220 } // End llvm namespace