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(int64_t Val) const {
46 MI->addImmOperand(Val);
50 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
51 MI->addMachineBasicBlockOperand(MBB);
55 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
56 MI->addFrameIndexOperand(Idx);
60 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
61 int Offset = 0) const {
62 MI->addConstantPoolIndexOperand(Idx, Offset);
66 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
67 MI->addJumpTableIndexOperand(Idx);
71 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
72 int Offset = 0) const {
73 MI->addGlobalAddressOperand(GV, Offset);
77 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
78 MI->addExternalSymbolOperand(FnName);
83 /// BuildMI - Builder interface. Specify how to create the initial instruction
84 /// itself. NumOperands is the number of operands to the machine instruction to
85 /// allow for memory efficient representation of machine instructions.
87 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
88 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands));
91 /// BuildMI - This version of the builder sets up the first operand as a
92 /// destination virtual register. NumOperands is the number of additional add*
93 /// calls that are expected, not including the destination register.
95 inline MachineInstrBuilder BuildMI(
96 int Opcode, unsigned NumOperands,
98 MachineOperand::UseType useType = MachineOperand::Def) {
99 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1))
100 .addReg(DestReg, useType);
103 /// BuildMI - This version of the builder inserts the newly-built
104 /// instruction before the given position in the given MachineBasicBlock, and
105 /// sets up the first operand as a destination virtual register.
106 /// NumOperands is the number of additional add* calls that are expected,
107 /// not including the destination register.
109 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
110 MachineBasicBlock::iterator I,
111 int Opcode, unsigned NumOperands,
113 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1);
115 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
118 /// BuildMI - This version of the builder inserts the newly-built
119 /// instruction before the given position in the given MachineBasicBlock, and
120 /// does NOT take a destination register.
122 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
123 MachineBasicBlock::iterator I,
124 int Opcode, unsigned NumOperands) {
125 MachineInstr *MI = new MachineInstr(Opcode, NumOperands);
127 return MachineInstrBuilder(MI);
130 /// BuildMI - This version of the builder inserts the newly-built
131 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
132 /// destination register.
134 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
135 unsigned NumOperands) {
136 return BuildMI(*BB, BB->end(), Opcode, NumOperands);
139 /// BuildMI - This version of the builder inserts the newly-built
140 /// instruction at the end of the given MachineBasicBlock, and sets up the first
141 /// operand as a destination virtual register. NumOperands is the number of
142 /// additional add* calls that are expected, not including the destination
145 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
146 unsigned NumOperands, unsigned DestReg) {
147 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
150 } // End llvm namespace