MachineInstrBuilder.h revision a65aa0f0bba1ef2322d63d05c074a92168684c63
1//===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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: 12// 13// M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2); 14// 15//===----------------------------------------------------------------------===// 16 17#ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H 18#define LLVM_CODEGEN_MACHINEINSTRBUILDER_H 19 20#include "llvm/CodeGen/MachineFunction.h" 21 22namespace llvm { 23 24class TargetInstrDesc; 25class MDNode; 26 27namespace RegState { 28 enum { 29 Define = 0x2, 30 Implicit = 0x4, 31 Kill = 0x8, 32 Dead = 0x10, 33 Undef = 0x20, 34 EarlyClobber = 0x40, 35 Debug = 0x80, 36 ImplicitDefine = Implicit | Define, 37 ImplicitKill = Implicit | Kill 38 }; 39} 40 41class MachineInstrBuilder { 42 MachineInstr *MI; 43public: 44 MachineInstrBuilder() : MI(0) {} 45 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {} 46 47 /// Allow automatic conversion to the machine instruction we are working on. 48 /// 49 operator MachineInstr*() const { return MI; } 50 operator MachineBasicBlock::iterator() const { return MI; } 51 52 /// addReg - Add a new virtual register operand... 53 /// 54 const 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, 66 SubReg, 67 flags & RegState::Debug)); 68 return *this; 69 } 70 71 /// addImm - Add a new immediate operand. 72 /// 73 const MachineInstrBuilder &addImm(int64_t Val) const { 74 MI->addOperand(MachineOperand::CreateImm(Val)); 75 return *this; 76 } 77 78 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const { 79 MI->addOperand(MachineOperand::CreateFPImm(Val)); 80 return *this; 81 } 82 83 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB, 84 unsigned char TargetFlags = 0) const { 85 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags)); 86 return *this; 87 } 88 89 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const { 90 MI->addOperand(MachineOperand::CreateFI(Idx)); 91 return *this; 92 } 93 94 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx, 95 int Offset = 0, 96 unsigned char TargetFlags = 0) const { 97 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags)); 98 return *this; 99 } 100 101 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx, 102 unsigned char TargetFlags = 0) const { 103 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags)); 104 return *this; 105 } 106 107 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV, 108 int64_t Offset = 0, 109 unsigned char TargetFlags = 0) const { 110 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags)); 111 return *this; 112 } 113 114 const MachineInstrBuilder &addExternalSymbol(const char *FnName, 115 unsigned char TargetFlags = 0) const { 116 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags)); 117 return *this; 118 } 119 120 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const { 121 MI->addMemOperand(*MI->getParent()->getParent(), MMO); 122 return *this; 123 } 124 125 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const { 126 MI->addOperand(MO); 127 return *this; 128 } 129 130 const MachineInstrBuilder &addMetadata(MDNode *MD) const { 131 MI->addOperand(MachineOperand::CreateMetadata(MD)); 132 return *this; 133 } 134}; 135 136/// BuildMI - Builder interface. Specify how to create the initial instruction 137/// itself. 138/// 139inline MachineInstrBuilder BuildMI(MachineFunction &MF, 140 DebugLoc DL, 141 const TargetInstrDesc &TID) { 142 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL)); 143} 144 145/// BuildMI - This version of the builder sets up the first operand as a 146/// destination virtual register. 147/// 148inline MachineInstrBuilder BuildMI(MachineFunction &MF, 149 DebugLoc DL, 150 const TargetInstrDesc &TID, 151 unsigned DestReg) { 152 return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL)) 153 .addReg(DestReg, RegState::Define); 154} 155 156/// BuildMI - This version of the builder inserts the newly-built 157/// instruction before the given position in the given MachineBasicBlock, and 158/// sets up the first operand as a destination virtual register. 159/// 160inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 161 MachineBasicBlock::iterator I, 162 DebugLoc DL, 163 const TargetInstrDesc &TID, 164 unsigned DestReg) { 165 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL); 166 BB.insert(I, MI); 167 return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define); 168} 169 170/// BuildMI - This version of the builder inserts the newly-built 171/// instruction before the given position in the given MachineBasicBlock, and 172/// does NOT take a destination register. 173/// 174inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 175 MachineBasicBlock::iterator I, 176 DebugLoc DL, 177 const TargetInstrDesc &TID) { 178 MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL); 179 BB.insert(I, MI); 180 return MachineInstrBuilder(MI); 181} 182 183/// BuildMI - This version of the builder inserts the newly-built 184/// instruction at the end of the given MachineBasicBlock, and does NOT take a 185/// destination register. 186/// 187inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 188 DebugLoc DL, 189 const TargetInstrDesc &TID) { 190 return BuildMI(*BB, BB->end(), DL, TID); 191} 192 193/// BuildMI - This version of the builder inserts the newly-built 194/// instruction at the end of the given MachineBasicBlock, and sets up the first 195/// operand as a destination virtual register. 196/// 197inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 198 DebugLoc DL, 199 const TargetInstrDesc &TID, 200 unsigned DestReg) { 201 return BuildMI(*BB, BB->end(), DL, TID, DestReg); 202} 203 204inline unsigned getDefRegState(bool B) { 205 return B ? RegState::Define : 0; 206} 207inline unsigned getImplRegState(bool B) { 208 return B ? RegState::Implicit : 0; 209} 210inline unsigned getKillRegState(bool B) { 211 return B ? RegState::Kill : 0; 212} 213inline unsigned getDeadRegState(bool B) { 214 return B ? RegState::Dead : 0; 215} 216inline unsigned getUndefRegState(bool B) { 217 return B ? RegState::Undef : 0; 218} 219 220} // End llvm namespace 221 222#endif 223