MachineInstrBuilder.h revision 6f95014158208ad6cd6f6d1996c821fca61d7915
1//===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source 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, bool isImp = false, 41 bool isKill = false, bool isDead = false) const { 42 MI->addRegOperand(RegNo, isDef, isImp, isKill, isDead); 43 return *this; 44 } 45 46 /// addImm - Add a new immediate operand. 47 /// 48 const MachineInstrBuilder &addImm(int64_t Val) const { 49 MI->addImmOperand(Val); 50 return *this; 51 } 52 53 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const { 54 MI->addMachineBasicBlockOperand(MBB); 55 return *this; 56 } 57 58 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const { 59 MI->addFrameIndexOperand(Idx); 60 return *this; 61 } 62 63 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx, 64 int Offset = 0) const { 65 MI->addConstantPoolIndexOperand(Idx, Offset); 66 return *this; 67 } 68 69 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const { 70 MI->addJumpTableIndexOperand(Idx); 71 return *this; 72 } 73 74 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV, 75 int Offset = 0) const { 76 MI->addGlobalAddressOperand(GV, Offset); 77 return *this; 78 } 79 80 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{ 81 MI->addExternalSymbolOperand(FnName); 82 return *this; 83 } 84}; 85 86/// BuildMI - Builder interface. Specify how to create the initial instruction 87/// itself. 88/// 89inline MachineInstrBuilder BuildMI(const TargetInstrDescriptor &TID) { 90 return MachineInstrBuilder(new MachineInstr(TID)); 91} 92 93/// BuildMI - This version of the builder sets up the first operand as a 94/// destination virtual register. 95/// 96inline MachineInstrBuilder BuildMI(const TargetInstrDescriptor &TID, 97 unsigned DestReg) { 98 return MachineInstrBuilder(new MachineInstr(TID)).addReg(DestReg, true); 99} 100 101/// BuildMI - This version of the builder inserts the newly-built 102/// instruction before the given position in the given MachineBasicBlock, and 103/// sets up the first operand as a destination virtual register. 104/// 105inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 106 MachineBasicBlock::iterator I, 107 const TargetInstrDescriptor &TID, 108 unsigned DestReg) { 109 MachineInstr *MI = new MachineInstr(TID); 110 BB.insert(I, MI); 111 return MachineInstrBuilder(MI).addReg(DestReg, true); 112} 113 114/// BuildMI - This version of the builder inserts the newly-built 115/// instruction before the given position in the given MachineBasicBlock, and 116/// does NOT take a destination register. 117/// 118inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 119 MachineBasicBlock::iterator I, 120 const TargetInstrDescriptor &TID) { 121 MachineInstr *MI = new MachineInstr(TID); 122 BB.insert(I, MI); 123 return MachineInstrBuilder(MI); 124} 125 126/// BuildMI - This version of the builder inserts the newly-built 127/// instruction at the end of the given MachineBasicBlock, and does NOT take a 128/// destination register. 129/// 130inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 131 const TargetInstrDescriptor &TID) { 132 return BuildMI(*BB, BB->end(), TID); 133} 134 135/// BuildMI - This version of the builder inserts the newly-built 136/// instruction at the end of the given MachineBasicBlock, and sets up the first 137/// operand as a destination virtual register. 138/// 139inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, 140 const TargetInstrDescriptor &TID, 141 unsigned DestReg) { 142 return BuildMI(*BB, BB->end(), TID, DestReg); 143} 144 145} // End llvm namespace 146 147#endif 148