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(int RegNo, bool isDef = false) const {
37 MI->addRegOperand(RegNo, isDef);
41 /// addImm - Add a new immediate operand.
43 const MachineInstrBuilder &addImm(int64_t Val) const {
44 MI->addImmOperand(Val);
48 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
49 MI->addMachineBasicBlockOperand(MBB);
53 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
54 MI->addFrameIndexOperand(Idx);
58 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
59 int Offset = 0) const {
60 MI->addConstantPoolIndexOperand(Idx, Offset);
64 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
65 MI->addJumpTableIndexOperand(Idx);
69 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
70 int Offset = 0) const {
71 MI->addGlobalAddressOperand(GV, Offset);
75 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
76 MI->addExternalSymbolOperand(FnName);
81 /// BuildMI - Builder interface. Specify how to create the initial instruction
82 /// itself. NumOperands is the number of operands to the machine instruction to
83 /// allow for memory efficient representation of machine instructions.
85 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) {
86 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands));
89 /// BuildMI - This version of the builder sets up the first operand as a
90 /// destination virtual register. NumOperands is the number of additional add*
91 /// calls that are expected, not including the destination register.
93 inline MachineInstrBuilder
94 BuildMI(int Opcode, unsigned NumOperands, unsigned DestReg) {
95 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1))
96 .addReg(DestReg, true);
99 /// BuildMI - This version of the builder inserts the newly-built
100 /// instruction before the given position in the given MachineBasicBlock, and
101 /// sets up the first operand as a destination virtual register.
102 /// NumOperands is the number of additional add* calls that are expected,
103 /// not including the destination register.
105 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
106 MachineBasicBlock::iterator I,
107 int Opcode, unsigned NumOperands,
109 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1);
111 return MachineInstrBuilder(MI).addReg(DestReg, true);
114 /// BuildMI - This version of the builder inserts the newly-built
115 /// instruction before the given position in the given MachineBasicBlock, and
116 /// does NOT take a destination register.
118 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
119 MachineBasicBlock::iterator I,
120 int Opcode, unsigned NumOperands) {
121 MachineInstr *MI = new MachineInstr(Opcode, NumOperands);
123 return MachineInstrBuilder(MI);
126 /// BuildMI - This version of the builder inserts the newly-built
127 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
128 /// destination register.
130 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
131 unsigned NumOperands) {
132 return BuildMI(*BB, BB->end(), Opcode, NumOperands);
135 /// BuildMI - This version of the builder inserts the newly-built
136 /// instruction at the end of the given MachineBasicBlock, and sets up the first
137 /// operand as a destination virtual register. NumOperands is the number of
138 /// additional add* calls that are expected, not including the destination
141 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode,
142 unsigned NumOperands, unsigned DestReg) {
143 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg);
146 } // End llvm namespace