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;
36 ImplicitDefine = Implicit | Define,
37 ImplicitKill = Implicit | Kill
41 class MachineInstrBuilder {
44 MachineInstrBuilder() : MI(0) {}
45 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
47 /// Allow automatic conversion to the machine instruction we are working on.
49 operator MachineInstr*() const { return MI; }
50 operator MachineBasicBlock::iterator() const { return MI; }
52 /// addReg - Add a new virtual register operand...
55 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
56 unsigned SubReg = 0) const {
57 assert((flags & 0x1) == 0 &&
58 "Passing in 'true' to addReg is forbidden! Use enums instead.");
59 MI->addOperand(MachineOperand::CreateReg(RegNo,
60 flags & RegState::Define,
61 flags & RegState::Implicit,
62 flags & RegState::Kill,
63 flags & RegState::Dead,
64 flags & RegState::Undef,
65 flags & RegState::EarlyClobber,
67 flags & RegState::Debug));
71 /// addImm - Add a new immediate operand.
73 const MachineInstrBuilder &addImm(int64_t Val) const {
74 MI->addOperand(MachineOperand::CreateImm(Val));
78 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
79 MI->addOperand(MachineOperand::CreateFPImm(Val));
83 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
84 unsigned char TargetFlags = 0) const {
85 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
89 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
90 MI->addOperand(MachineOperand::CreateFI(Idx));
94 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
96 unsigned char TargetFlags = 0) const {
97 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
101 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
102 unsigned char TargetFlags = 0) const {
103 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
107 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
109 unsigned char TargetFlags = 0) const {
110 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
114 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
115 unsigned char TargetFlags = 0) const {
116 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
120 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
121 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
125 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
130 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
131 MI->addOperand(MachineOperand::CreateMetadata(MD));
135 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
136 MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
141 /// BuildMI - Builder interface. Specify how to create the initial instruction
144 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
146 const TargetInstrDesc &TID) {
147 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
150 /// BuildMI - This version of the builder sets up the first operand as a
151 /// destination virtual register.
153 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
155 const TargetInstrDesc &TID,
157 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
158 .addReg(DestReg, RegState::Define);
161 /// BuildMI - This version of the builder inserts the newly-built
162 /// instruction before the given position in the given MachineBasicBlock, and
163 /// sets up the first operand as a destination virtual register.
165 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
166 MachineBasicBlock::iterator I,
168 const TargetInstrDesc &TID,
170 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
172 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
175 /// BuildMI - This version of the builder inserts the newly-built
176 /// instruction before the given position in the given MachineBasicBlock, and
177 /// does NOT take a destination register.
179 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
180 MachineBasicBlock::iterator I,
182 const TargetInstrDesc &TID) {
183 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
185 return MachineInstrBuilder(MI);
188 /// BuildMI - This version of the builder inserts the newly-built
189 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
190 /// destination register.
192 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
194 const TargetInstrDesc &TID) {
195 return BuildMI(*BB, BB->end(), DL, TID);
198 /// BuildMI - This version of the builder inserts the newly-built
199 /// instruction at the end of the given MachineBasicBlock, and sets up the first
200 /// operand as a destination virtual register.
202 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
204 const TargetInstrDesc &TID,
206 return BuildMI(*BB, BB->end(), DL, TID, DestReg);
209 inline unsigned getDefRegState(bool B) {
210 return B ? RegState::Define : 0;
212 inline unsigned getImplRegState(bool B) {
213 return B ? RegState::Implicit : 0;
215 inline unsigned getKillRegState(bool B) {
216 return B ? RegState::Kill : 0;
218 inline unsigned getDeadRegState(bool B) {
219 return B ? RegState::Dead : 0;
221 inline unsigned getUndefRegState(bool B) {
222 return B ? RegState::Undef : 0;
225 } // End llvm namespace