1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/MachineFunction.h"
24 class TargetInstrDesc;
26 class MachineInstrBuilder {
29 explicit 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(unsigned RegNo, bool isDef = false,
40 bool isImp = false, bool isKill = false,
41 bool isDead = false, unsigned SubReg = 0) const {
42 MI->addOperand(MachineOperand::CreateReg(RegNo, isDef, isImp, isKill,
47 /// addImm - Add a new immediate operand.
49 const MachineInstrBuilder &addImm(int64_t Val) const {
50 MI->addOperand(MachineOperand::CreateImm(Val));
54 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
55 MI->addOperand(MachineOperand::CreateFPImm(Val));
59 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const {
60 MI->addOperand(MachineOperand::CreateMBB(MBB));
64 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
65 MI->addOperand(MachineOperand::CreateFI(Idx));
69 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
70 int Offset = 0) const {
71 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
75 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const {
76 MI->addOperand(MachineOperand::CreateJTI(Idx));
80 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
81 int64_t Offset = 0) const {
82 MI->addOperand(MachineOperand::CreateGA(GV, Offset));
86 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
87 int64_t Offset = 0) const {
88 MI->addOperand(MachineOperand::CreateES(FnName, Offset));
92 const MachineInstrBuilder &addMemOperand(const MachineMemOperand &MMO) const {
93 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
98 /// BuildMI - Builder interface. Specify how to create the initial instruction
101 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
102 const TargetInstrDesc &TID) {
103 return MachineInstrBuilder(MF.CreateMachineInstr(TID,
104 DebugLoc::getUnknownLoc()));
106 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
108 const TargetInstrDesc &TID) {
109 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
112 /// BuildMI - This version of the builder sets up the first operand as a
113 /// destination virtual register.
115 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
116 const TargetInstrDesc &TID,
118 return MachineInstrBuilder(MF.CreateMachineInstr(TID,
119 DebugLoc::getUnknownLoc()))
120 .addReg(DestReg, true);
122 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
124 const TargetInstrDesc &TID,
126 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
127 .addReg(DestReg, true);
130 /// BuildMI - This version of the builder inserts the newly-built
131 /// instruction before the given position in the given MachineBasicBlock, and
132 /// sets up the first operand as a destination virtual register.
134 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
135 MachineBasicBlock::iterator I,
136 const TargetInstrDesc &TID,
139 BB.getParent()->CreateMachineInstr(TID, DebugLoc::getUnknownLoc());
141 return MachineInstrBuilder(MI).addReg(DestReg, true);
143 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
144 MachineBasicBlock::iterator I,
146 const TargetInstrDesc &TID,
148 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
150 return MachineInstrBuilder(MI).addReg(DestReg, true);
153 /// BuildMI - This version of the builder inserts the newly-built
154 /// instruction before the given position in the given MachineBasicBlock, and
155 /// does NOT take a destination register.
157 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
158 MachineBasicBlock::iterator I,
159 const TargetInstrDesc &TID) {
161 BB.getParent()->CreateMachineInstr(TID, DebugLoc::getUnknownLoc());
163 return MachineInstrBuilder(MI);
165 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
166 MachineBasicBlock::iterator I,
168 const TargetInstrDesc &TID) {
169 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
171 return MachineInstrBuilder(MI);
174 /// BuildMI - This version of the builder inserts the newly-built
175 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
176 /// destination register.
178 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
179 const TargetInstrDesc &TID) {
180 return BuildMI(*BB, BB->end(), TID);
182 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
184 const TargetInstrDesc &TID) {
185 return BuildMI(*BB, BB->end(), DL, TID);
188 /// BuildMI - This version of the builder inserts the newly-built
189 /// instruction at the end of the given MachineBasicBlock, and sets up the first
190 /// operand as a destination virtual register.
192 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
193 const TargetInstrDesc &TID,
195 return BuildMI(*BB, BB->end(), TID, DestReg);
197 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
199 const TargetInstrDesc &TID,
201 return BuildMI(*BB, BB->end(), DL, TID, DestReg);
204 } // End llvm namespace