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;
35 ImplicitDefine = Implicit | Define,
36 ImplicitKill = Implicit | Kill
40 class MachineInstrBuilder {
43 MachineInstrBuilder() : MI(0) {}
44 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
46 /// Allow automatic conversion to the machine instruction we are working on.
48 operator MachineInstr*() const { return MI; }
49 operator MachineBasicBlock::iterator() const { return MI; }
51 /// addReg - Add a new virtual register operand...
54 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
55 unsigned SubReg = 0) const {
56 assert((flags & 0x1) == 0 &&
57 "Passing in 'true' to addReg is forbidden! Use enums instead.");
58 MI->addOperand(MachineOperand::CreateReg(RegNo,
59 flags & RegState::Define,
60 flags & RegState::Implicit,
61 flags & RegState::Kill,
62 flags & RegState::Dead,
63 flags & RegState::Undef,
64 flags & RegState::EarlyClobber,
69 /// addImm - Add a new immediate operand.
71 const MachineInstrBuilder &addImm(int64_t Val) const {
72 MI->addOperand(MachineOperand::CreateImm(Val));
76 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
77 MI->addOperand(MachineOperand::CreateFPImm(Val));
81 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
82 unsigned char TargetFlags = 0) const {
83 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
87 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
88 MI->addOperand(MachineOperand::CreateFI(Idx));
92 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
94 unsigned char TargetFlags = 0) const {
95 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
99 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
100 unsigned char TargetFlags = 0) const {
101 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
105 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
107 unsigned char TargetFlags = 0) const {
108 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
112 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
113 unsigned char TargetFlags = 0) const {
114 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
118 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
119 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
123 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
128 const MachineInstrBuilder &addMetadata(MDNode *MD) const {
129 MI->addOperand(MachineOperand::CreateMetadata(MD));
134 /// BuildMI - Builder interface. Specify how to create the initial instruction
137 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
139 const TargetInstrDesc &TID) {
140 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
143 /// BuildMI - This version of the builder sets up the first operand as a
144 /// destination virtual register.
146 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
148 const TargetInstrDesc &TID,
150 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
151 .addReg(DestReg, RegState::Define);
154 /// BuildMI - This version of the builder inserts the newly-built
155 /// instruction before the given position in the given MachineBasicBlock, and
156 /// sets up the first operand as a destination virtual register.
158 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
159 MachineBasicBlock::iterator I,
161 const TargetInstrDesc &TID,
163 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
165 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
168 /// BuildMI - This version of the builder inserts the newly-built
169 /// instruction before the given position in the given MachineBasicBlock, and
170 /// does NOT take a destination register.
172 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
173 MachineBasicBlock::iterator I,
175 const TargetInstrDesc &TID) {
176 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
178 return MachineInstrBuilder(MI);
181 /// BuildMI - This version of the builder inserts the newly-built
182 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
183 /// destination register.
185 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
187 const TargetInstrDesc &TID) {
188 return BuildMI(*BB, BB->end(), DL, TID);
191 /// BuildMI - This version of the builder inserts the newly-built
192 /// instruction at the end of the given MachineBasicBlock, and sets up the first
193 /// operand as a destination virtual register.
195 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
197 const TargetInstrDesc &TID,
199 return BuildMI(*BB, BB->end(), DL, TID, DestReg);
202 inline unsigned getDefRegState(bool B) {
203 return B ? RegState::Define : 0;
205 inline unsigned getImplRegState(bool B) {
206 return B ? RegState::Implicit : 0;
208 inline unsigned getKillRegState(bool B) {
209 return B ? RegState::Kill : 0;
211 inline unsigned getDeadRegState(bool B) {
212 return B ? RegState::Dead : 0;
214 inline unsigned getUndefRegState(bool B) {
215 return B ? RegState::Undef : 0;
218 } // End llvm namespace