MachineInstrBuilder.h revision 7ed47a13356daed2a34cd2209a31f92552e3bdd8
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/MachineBasicBlock.h" 21#include "llvm/CodeGen/MachineFunction.h" 22 23namespace llvm { 24 25class TargetInstrDescriptor; 26 27class MachineInstrBuilder { 28 MachineInstr *MI; 29public: 30 explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {} 31 32 /// Allow automatic conversion to the machine instruction we are working on. 33 /// 34 operator MachineInstr*() const { return MI; } 35 operator MachineBasicBlock::iterator() const { return MI; } 36 37 /// addReg - Add a new virtual register operand... 38 /// 39 const 40 MachineInstrBuilder &addReg(unsigned RegNo, bool isDef = false, 41 bool isImp = false, bool isKill = false, 42 bool isDead = false, unsigned SubReg = 0) const { 43 MI->addRegOperand(RegNo, isDef, isImp, isKill, isDead, SubReg); 44 return *this; 45 } 46 47 /// addImm - Add a new immediate operand. 48 /// 49 const MachineInstrBuilder &addImm(int64_t Val) const { 50 MI->addImmOperand(Val); 51 return *this; 52 } 53 54 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const { 55 MI->addMachineBasicBlockOperand(MBB); 56 return *this; 57 } 58 59 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const { 60 MI->addFrameIndexOperand(Idx); 61 return *this; 62 } 63 64 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx, 65 int Offset = 0) const { 66 MI->addConstantPoolIndexOperand(Idx, Offset); 67 return *this; 68 } 69 70 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const { 71 MI->addJumpTableIndexOperand(Idx); 72 return *this; 73 } 74 75 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV, 76 int Offset = 0) const { 77 MI->addGlobalAddressOperand(GV, Offset); 78 return *this; 79 } 80 81 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{ 82 MI->addExternalSymbolOperand(FnName); 83 return *this; 84 } 85}; 86 87/// BuildMI - Builder interface. Specify how to create the initial instruction 88/// itself. 89/// 90inline MachineInstrBuilder BuildMI(const TargetInstrDescriptor &TID) { 91 return MachineInstrBuilder(new MachineInstr(TID)); 92} 93 94/// BuildMI - This version of the builder sets up the first operand as a 95/// destination virtual register. 96/// 97inline MachineInstrBuilder BuildMI(const TargetInstrDescriptor &TID, 98 unsigned DestReg) { 99 return MachineInstrBuilder(new MachineInstr(TID)).addReg(DestReg, true); 100} 101 102/// BuildMI - This version of the builder inserts the newly-built 103/// instruction before the given position in the given MachineBasicBlock, and 104/// sets up the first operand as a destination virtual register. 105/// 106inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 107 MachineBasicBlock::iterator I, 108 const TargetInstrDescriptor &TID, 109 unsigned DestReg) { 110 MachineInstr *MI = new MachineInstr(TID); 111 BB.insert(I, MI); 112 return MachineInstrBuilder(MI).addReg(DestReg, true); 113} 114 115/// BuildMI - This version of the builder inserts the newly-built 116/// instruction before the given position in the given MachineBasicBlock, and 117/// does NOT take a destination register. 118/// 119inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 120 MachineBasicBlock::iterator I, 121 const TargetInstrDescriptor &TID) { 122 MachineInstr *MI = new MachineInstr(TID); 123 BB.insert(I, MI); 124 return MachineInstrBuilder(MI); 125} 126 127/// BuildMI - This version of the builder inserts the newly-built 128/// instruction at the end of the given MachineBasicBlock, and does NOT take a 129/// destination register. 130/// 131inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 132 const TargetInstrDescriptor &TID) { 133 return BuildMI(*BB, BB->end(), TID); 134} 135 136/// BuildMI - This version of the builder inserts the newly-built 137/// instruction at the end of the given MachineBasicBlock, and sets up the first 138/// operand as a destination virtual register. 139/// 140inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 141 const TargetInstrDescriptor &TID, 142 unsigned DestReg) { 143 return BuildMI(*BB, BB->end(), TID, DestReg); 144} 145 146} // End llvm namespace 147 148#endif 149