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;
34 ImplicitDefine = Implicit | Define,
35 ImplicitKill = Implicit | Kill
39 class MachineInstrBuilder {
42 MachineInstrBuilder() : MI(0) {}
43 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
45 /// Allow automatic conversion to the machine instruction we are working on.
47 operator MachineInstr*() const { return MI; }
48 operator MachineBasicBlock::iterator() const { return MI; }
50 /// addReg - Add a new virtual register operand...
53 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
54 unsigned SubReg = 0) const {
55 assert((flags & 0x1) == 0 &&
56 "Passing in 'true' to addReg is forbidden! Use enums instead.");
57 MI->addOperand(MachineOperand::CreateReg(RegNo,
58 flags & RegState::Define,
59 flags & RegState::Implicit,
60 flags & RegState::Kill,
61 flags & RegState::Dead,
62 flags & RegState::Undef,
63 flags & RegState::EarlyClobber,
68 /// addImm - Add a new immediate operand.
70 const MachineInstrBuilder &addImm(int64_t Val) const {
71 MI->addOperand(MachineOperand::CreateImm(Val));
75 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
76 MI->addOperand(MachineOperand::CreateFPImm(Val));
80 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
81 unsigned char TargetFlags = 0) const {
82 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
86 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
87 MI->addOperand(MachineOperand::CreateFI(Idx));
91 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
93 unsigned char TargetFlags = 0) const {
94 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
98 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
99 unsigned char TargetFlags = 0) const {
100 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
104 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV,
106 unsigned char TargetFlags = 0) const {
107 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
111 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
112 unsigned char TargetFlags = 0) const {
113 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
117 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
118 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
122 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
128 /// BuildMI - Builder interface. Specify how to create the initial instruction
131 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
133 const TargetInstrDesc &TID) {
134 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
137 /// BuildMI - This version of the builder sets up the first operand as a
138 /// destination virtual register.
140 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
142 const TargetInstrDesc &TID,
144 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
145 .addReg(DestReg, RegState::Define);
148 /// BuildMI - This version of the builder inserts the newly-built
149 /// instruction before the given position in the given MachineBasicBlock, and
150 /// sets up the first operand as a destination virtual register.
152 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
153 MachineBasicBlock::iterator I,
155 const TargetInstrDesc &TID,
157 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
159 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
162 /// BuildMI - This version of the builder inserts the newly-built
163 /// instruction before the given position in the given MachineBasicBlock, and
164 /// does NOT take a destination register.
166 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
167 MachineBasicBlock::iterator I,
169 const TargetInstrDesc &TID) {
170 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
172 return MachineInstrBuilder(MI);
175 /// BuildMI - This version of the builder inserts the newly-built
176 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
177 /// destination register.
179 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
181 const TargetInstrDesc &TID) {
182 return BuildMI(*BB, BB->end(), DL, TID);
185 /// BuildMI - This version of the builder inserts the newly-built
186 /// instruction at the end of the given MachineBasicBlock, and sets up the first
187 /// operand as a destination virtual register.
189 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
191 const TargetInstrDesc &TID,
193 return BuildMI(*BB, BB->end(), DL, TID, DestReg);
196 inline unsigned getDefRegState(bool B) {
197 return B ? RegState::Define : 0;
199 inline unsigned getImplRegState(bool B) {
200 return B ? RegState::Implicit : 0;
202 inline unsigned getKillRegState(bool B) {
203 return B ? RegState::Kill : 0;
205 inline unsigned getDeadRegState(bool B) {
206 return B ? RegState::Dead : 0;
208 inline unsigned getUndefRegState(bool B) {
209 return B ? RegState::Undef : 0;
212 } // End llvm namespace