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 explicit 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(unsigned RegNo, bool isDef = false,
41 bool isImp = false, bool isKill = false,
42 bool isDead = false, unsigned SubReg = 0) const {
43 MI->addRegOperand(RegNo, isDef, isImp, isKill, isDead, SubReg);
47 /// addImm - Add a new immediate operand.
49 const MachineInstrBuilder &addImm(int64_t Val) const {
50 MI->addImmOperand(Val);
54 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
55 MI->addMachineBasicBlockOperand(MBB);
59 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
60 MI->addFrameIndexOperand(Idx);
64 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
65 int Offset = 0) const {
66 MI->addConstantPoolIndexOperand(Idx, Offset);
70 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
71 MI->addJumpTableIndexOperand(Idx);
75 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
76 int Offset = 0) const {
77 MI->addGlobalAddressOperand(GV, Offset);
81 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{
82 MI->addExternalSymbolOperand(FnName);
87 /// BuildMI - Builder interface. Specify how to create the initial instruction
90 inline MachineInstrBuilder BuildMI(const TargetInstrDescriptor &TID) {
91 return MachineInstrBuilder(new MachineInstr(TID));
94 /// BuildMI - This version of the builder sets up the first operand as a
95 /// destination virtual register.
97 inline MachineInstrBuilder BuildMI(const TargetInstrDescriptor &TID,
99 return MachineInstrBuilder(new MachineInstr(TID)).addReg(DestReg, true);
102 /// BuildMI - This version of the builder inserts the newly-built
103 /// instruction before the given position in the given MachineBasicBlock, and
104 /// sets up the first operand as a destination virtual register.
106 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
107 MachineBasicBlock::iterator I,
108 const TargetInstrDescriptor &TID,
110 MachineInstr *MI = new MachineInstr(TID);
112 return MachineInstrBuilder(MI).addReg(DestReg, true);
115 /// BuildMI - This version of the builder inserts the newly-built
116 /// instruction before the given position in the given MachineBasicBlock, and
117 /// does NOT take a destination register.
119 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
120 MachineBasicBlock::iterator I,
121 const TargetInstrDescriptor &TID) {
122 MachineInstr *MI = new MachineInstr(TID);
124 return MachineInstrBuilder(MI);
127 /// BuildMI - This version of the builder inserts the newly-built
128 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
129 /// destination register.
131 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
132 const TargetInstrDescriptor &TID) {
133 return BuildMI(*BB, BB->end(), TID);
136 /// BuildMI - This version of the builder inserts the newly-built
137 /// instruction at the end of the given MachineBasicBlock, and sets up the first
138 /// operand as a destination virtual register.
140 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
141 const TargetInstrDescriptor &TID,
143 return BuildMI(*BB, BB->end(), TID, DestReg);
146 } // End llvm namespace