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. 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/MachineBasicBlock.h"
24 class MachineInstrBuilder {
27 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
29 /// Allow automatic conversion to the machine instruction we are working on.
31 operator MachineInstr*() const { return MI; }
32 operator MachineBasicBlock::iterator() const { return MI; }
34 /// addReg - Add a new virtual register operand...
36 const MachineInstrBuilder &addReg(
38 MachineOperand::UseType Ty = MachineOperand::Use) const {
39 MI->addRegOperand(RegNo, Ty);
43 /// addImm - Add a new immediate operand.
45 const MachineInstrBuilder &addImm(int Val) const {
46 MI->addZeroExtImmOperand(Val);
50 /// addZImm - Add a new zero extended immediate operand...
52 const MachineInstrBuilder &addZImm(unsigned Val) const {
53 MI->addZeroExtImmOperand(Val);
57 /// addImm64 - Add a new 64-bit immediate operand...
59 const MachineInstrBuilder &addImm64(uint64_t Val) const {
60 MI->addZeroExtImm64Operand(Val);
64 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
65 MI->addMachineBasicBlockOperand(MBB);
69 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
70 MI->addFrameIndexOperand(Idx);
74 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
75 int Offset = 0) const {
76 MI->addConstantPoolIndexOperand(Idx, Offset);
80 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
81 MI->addJumpTableIndexOperand(Idx);
85 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
86 int Offset = 0) const {
87 MI->addGlobalAddressOperand(GV, Offset);
91 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
92 MI->addExternalSymbolOperand(FnName);
97 /// BuildMI - Builder interface. Specify how to create the initial instruction
98 /// itself. NumOperands is the number of operands to the machine instruction to
99 /// allow for memory efficient representation of machine instructions.
101 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
102 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
105 /// BuildMI - This version of the builder sets up the first operand as a
106 /// destination virtual register. NumOperands is the number of additional add*
107 /// calls that are expected, not including the destination register.
109 inline MachineInstrBuilder BuildMI(
110 int Opcode, unsigned NumOperands,
112 MachineOperand::UseType useType = MachineOperand::Def) {
113 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
114 true, true)).addReg(DestReg, useType);
117 /// BuildMI - This version of the builder inserts the newly-built
118 /// instruction before the given position in the given MachineBasicBlock, and
119 /// sets up the first operand as a destination virtual register.
120 /// NumOperands is the number of additional add* calls that are expected,
121 /// not including the destination register.
123 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
124 MachineBasicBlock::iterator I,
125 int Opcode, unsigned NumOperands,
127 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
129 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
132 /// BuildMI - This version of the builder inserts the newly-built
133 /// instruction before the given position in the given MachineBasicBlock, and
134 /// does NOT take a destination register.
136 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
137 MachineBasicBlock::iterator I,
138 int Opcode, unsigned NumOperands) {
139 MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
141 return MachineInstrBuilder(MI);
144 /// BuildMI - This version of the builder inserts the newly-built
145 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
146 /// destination register.
148 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
149 unsigned NumOperands) {
150 return BuildMI(*BB, BB->end(), Opcode, NumOperands);
153 /// BuildMI - This version of the builder inserts the newly-built
154 /// instruction at the end of the given MachineBasicBlock, and sets up the first
155 /// operand as a destination virtual register. NumOperands is the number of
156 /// additional add* calls that are expected, not including the destination
159 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
160 unsigned NumOperands, unsigned DestReg) {
161 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
164 } // End llvm namespace