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 /// addSImm - Add a new sign extended immediate operand...
52 const MachineInstrBuilder &addSImm(int val) const {
53 MI->addSignExtImmOperand(val);
57 /// addZImm - Add a new zero extended immediate operand...
59 const MachineInstrBuilder &addZImm(unsigned Val) const {
60 MI->addZeroExtImmOperand(Val);
64 /// addImm64 - Add a new 64-bit immediate operand...
66 const MachineInstrBuilder &addImm64(uint64_t Val) const {
67 MI->addZeroExtImm64Operand(Val);
71 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
72 MI->addMachineBasicBlockOperand(MBB);
76 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
77 MI->addFrameIndexOperand(Idx);
81 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
82 int Offset = 0) const {
83 MI->addConstantPoolIndexOperand(Idx, Offset);
87 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
88 MI->addJumpTableIndexOperand(Idx);
92 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
93 int Offset = 0) const {
94 MI->addGlobalAddressOperand(GV, Offset);
98 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
99 MI->addExternalSymbolOperand(FnName);
104 /// BuildMI - Builder interface. Specify how to create the initial instruction
105 /// itself. NumOperands is the number of operands to the machine instruction to
106 /// allow for memory efficient representation of machine instructions.
108 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
109 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true));
112 /// BuildMI - This version of the builder sets up the first operand as a
113 /// destination virtual register. NumOperands is the number of additional add*
114 /// calls that are expected, not including the destination register.
116 inline MachineInstrBuilder BuildMI(
117 int Opcode, unsigned NumOperands,
119 MachineOperand::UseType useType = MachineOperand::Def) {
120 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1,
121 true, true)).addReg(DestReg, useType);
124 /// BuildMI - This version of the builder inserts the newly-built
125 /// instruction before the given position in the given MachineBasicBlock, and
126 /// sets up the first operand as a destination virtual register.
127 /// NumOperands is the number of additional add* calls that are expected,
128 /// not including the destination register.
130 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
131 MachineBasicBlock::iterator I,
132 int Opcode, unsigned NumOperands,
134 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
136 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
139 /// BuildMI - This version of the builder inserts the newly-built
140 /// instruction before the given position in the given MachineBasicBlock, and
141 /// does NOT take a destination register.
143 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
144 MachineBasicBlock::iterator I,
145 int Opcode, unsigned NumOperands) {
146 MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
148 return MachineInstrBuilder(MI);
151 /// BuildMI - This version of the builder inserts the newly-built
152 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
153 /// destination register.
155 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
156 unsigned NumOperands) {
157 return BuildMI(*BB, BB->end(), Opcode, NumOperands);
160 /// BuildMI - This version of the builder inserts the newly-built
161 /// instruction at the end of the given MachineBasicBlock, and sets up the first
162 /// operand as a destination virtual register. NumOperands is the number of
163 /// additional add* calls that are expected, not including the destination
166 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
167 unsigned NumOperands, unsigned DestReg) {
168 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
171 } // End llvm namespace