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"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/Target/TargetMachine.h"
26 class MachineInstrBuilder {
29 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
31 /// Allow automatic conversion to the machine instruction we are working on.
33 operator MachineInstr*() const { return MI; }
34 operator MachineBasicBlock::iterator() const { return MI; }
36 /// addReg - Add a new virtual register operand...
39 MachineInstrBuilder &addReg(int RegNo, bool isDef = false, bool isImp = false,
40 bool isKill = false, bool isDead = false) const {
41 MI->addRegOperand(RegNo, isDef, isImp);
45 /// addImm - Add a new immediate operand.
47 const MachineInstrBuilder &addImm(int64_t Val) const {
48 MI->addImmOperand(Val);
52 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
53 MI->addMachineBasicBlockOperand(MBB);
57 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
58 MI->addFrameIndexOperand(Idx);
62 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
63 int Offset = 0) const {
64 MI->addConstantPoolIndexOperand(Idx, Offset);
68 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
69 MI->addJumpTableIndexOperand(Idx);
73 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
74 int Offset = 0) const {
75 MI->addGlobalAddressOperand(GV, Offset);
79 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
80 MI->addExternalSymbolOperand(FnName);
85 /// BuildMI - Builder interface. Specify how to create the initial instruction
86 /// itself. NumOperands is the number of operands to the machine instruction to
87 /// allow for memory efficient representation of machine instructions.
89 inline MachineInstrBuilder BuildMI(const TargetInstrInfo &TII, int Opcode,
90 unsigned NumOperands) {
91 return MachineInstrBuilder(new MachineInstr(TII, Opcode, NumOperands));
94 /// BuildMI - This version of the builder sets up the first operand as a
95 /// destination virtual register. NumOperands is the number of additional add*
96 /// calls that are expected, not including the destination register.
98 inline MachineInstrBuilder BuildMI(const TargetInstrInfo &TII, int Opcode,
99 unsigned NumOperands, unsigned DestReg) {
100 return MachineInstrBuilder(new MachineInstr(TII, Opcode, NumOperands+1))
101 .addReg(DestReg, true);
104 /// BuildMI - This version of the builder inserts the newly-built
105 /// instruction before the given position in the given MachineBasicBlock, and
106 /// sets up the first operand as a destination virtual register.
107 /// NumOperands is the number of additional add* calls that are expected,
108 /// not including the destination register.
110 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
111 MachineBasicBlock::iterator I,
112 int Opcode, unsigned NumOperands,
114 MachineInstr *MI = new MachineInstr(*BB.getParent()->getTarget().
115 getInstrInfo(), Opcode, NumOperands+1);
117 return MachineInstrBuilder(MI).addReg(DestReg, true);
120 /// BuildMI - This version of the builder inserts the newly-built
121 /// instruction before the given position in the given MachineBasicBlock, and
122 /// does NOT take a destination register.
124 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
125 MachineBasicBlock::iterator I,
126 int Opcode, unsigned NumOperands) {
127 MachineInstr *MI = new MachineInstr(*BB.getParent()->getTarget().
128 getInstrInfo(), Opcode, NumOperands);
130 return MachineInstrBuilder(MI);
133 /// BuildMI - This version of the builder inserts the newly-built
134 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
135 /// destination register.
137 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
138 unsigned NumOperands) {
139 return BuildMI(*BB, BB->end(), Opcode, NumOperands);
142 /// BuildMI - This version of the builder inserts the newly-built
143 /// instruction at the end of the given MachineBasicBlock, and sets up the first
144 /// operand as a destination virtual register. NumOperands is the number of
145 /// additional add* calls that are expected, not including the destination
148 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
149 unsigned NumOperands, unsigned DestReg) {
150 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
153 } // End llvm namespace