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"
25 class TargetInstrDescriptor;
27 class MachineInstrBuilder {
30 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
32 /// Allow automatic conversion to the machine instruction we are working on.
34 operator MachineInstr*() const { return MI; }
35 operator MachineBasicBlock::iterator() const { return MI; }
37 /// addReg - Add a new virtual register operand...
40 MachineInstrBuilder &addReg(int RegNo, bool isDef = false, bool isImp = false,
41 bool isKill = false, bool isDead = false) const {
42 MI->addRegOperand(RegNo, isDef, isImp);
46 /// addImm - Add a new immediate operand.
48 const MachineInstrBuilder &addImm(int64_t Val) const {
49 MI->addImmOperand(Val);
53 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
54 MI->addMachineBasicBlockOperand(MBB);
58 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
59 MI->addFrameIndexOperand(Idx);
63 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
64 int Offset = 0) const {
65 MI->addConstantPoolIndexOperand(Idx, Offset);
69 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
70 MI->addJumpTableIndexOperand(Idx);
74 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
75 int Offset = 0) const {
76 MI->addGlobalAddressOperand(GV, Offset);
80 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
81 MI->addExternalSymbolOperand(FnName);
86 /// BuildMI - Builder interface. Specify how to create the initial instruction
89 inline MachineInstrBuilder BuildMI(const TargetInstrDescriptor &TID) {
90 return MachineInstrBuilder(new MachineInstr(TID));
93 /// BuildMI - This version of the builder sets up the first operand as a
94 /// destination virtual register.
96 inline MachineInstrBuilder BuildMI(const TargetInstrDescriptor &TID,
98 return MachineInstrBuilder(new MachineInstr(TID)).addReg(DestReg, true);
101 /// BuildMI - This version of the builder inserts the newly-built
102 /// instruction before the given position in the given MachineBasicBlock, and
103 /// sets up the first operand as a destination virtual register.
105 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
106 MachineBasicBlock::iterator I,
107 const TargetInstrDescriptor &TID,
109 MachineInstr *MI = new MachineInstr(TID);
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 const TargetInstrDescriptor &TID) {
121 MachineInstr *MI = new MachineInstr(TID);
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,
131 const TargetInstrDescriptor &TID) {
132 return BuildMI(*BB, BB->end(), TID);
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.
139 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
140 const TargetInstrDescriptor &TID,
142 return BuildMI(*BB, BB->end(), TID, DestReg);
145 } // End llvm namespace