MachineInstrBuilder.h revision e387de30841a5e106015379b6c47c568cde8b06e
1//===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===// 2// 3// This file exposes a function named BuildMI, which is useful for dramatically 4// simplifying how MachineInstr's are created. Instead of using code like this: 5// 6// M = new MachineInstr(X86::ADDrr32); 7// M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, argVal1); 8// M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, argVal2); 9// 10// we can now use code like this: 11// 12// M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2); 13// 14//===----------------------------------------------------------------------===// 15 16#ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H 17#define LLVM_CODEGEN_MACHINEINSTRBUILDER_H 18 19#include "llvm/CodeGen/MachineInstr.h" 20 21class MachineInstrBuilder { 22 MachineInstr *MI; 23public: 24 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {} 25 26 /// Allow automatic conversion to the machine instruction we are working on. 27 /// 28 operator MachineInstr*() const { return MI; } 29 30 /// addReg - Add a new virtual register operand... 31 /// 32 const MachineInstrBuilder &addReg(int RegNo, 33 MOTy::UseType Ty = MOTy::Use) const { 34 MI->addRegOperand(RegNo, Ty); 35 return *this; 36 } 37 38 /// addReg - Add an LLVM value that is to be used as a register... 39 /// 40 const MachineInstrBuilder &addReg(Value *V, 41 MOTy::UseType Ty = MOTy::Use) const { 42 MI->addRegOperand(V, Ty); 43 return *this; 44 } 45 46 /// addRegDef - Add an LLVM value that is to be defined as a register... this 47 /// is the same as addReg(V, MOTy::Def). 48 /// 49 const MachineInstrBuilder &addRegDef(Value *V) const { 50 return addReg(V, MOTy::Def); 51 } 52 53 /// addPCDisp - Add an LLVM value to be treated as a PC relative 54 /// displacement... 55 /// 56 const MachineInstrBuilder &addPCDisp(Value *V) const { 57 MI->addPCDispOperand(V); 58 return *this; 59 } 60 61 /// addMReg - Add a machine register operand... 62 /// 63 const MachineInstrBuilder &addMReg(int Reg, 64 MOTy::UseType Ty = MOTy::Use) const { 65 MI->addMachineRegOperand(Reg, Ty); 66 return *this; 67 } 68 69 /// addSImm - Add a new sign extended immediate operand... 70 /// 71 const MachineInstrBuilder &addSImm(int64_t val) const { 72 MI->addSignExtImmOperand(val); 73 return *this; 74 } 75 76 /// addZImm - Add a new zero extended immediate operand... 77 /// 78 const MachineInstrBuilder &addZImm(int64_t Val) const { 79 MI->addZeroExtImmOperand(Val); 80 return *this; 81 } 82 83 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const { 84 MI->addMachineBasicBlockOperand(MBB); 85 return *this; 86 } 87 88 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const { 89 MI->addFrameIndexOperand(Idx); 90 return *this; 91 } 92 93 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx) const { 94 MI->addConstantPoolIndexOperand(Idx); 95 return *this; 96 } 97 98 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV, 99 bool isPCRelative = false) const { 100 MI->addGlobalAddressOperand(GV, isPCRelative); 101 return *this; 102 } 103 104 const MachineInstrBuilder &addExternalSymbol(const std::string &Name, 105 bool isPCRelative = false) const{ 106 MI->addExternalSymbolOperand(Name, isPCRelative); 107 return *this; 108 } 109}; 110 111/// BuildMI - Builder interface. Specify how to create the initial instruction 112/// itself. NumOperands is the number of operands to the machine instruction to 113/// allow for memory efficient representation of machine instructions. 114/// 115inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands) { 116 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true)); 117} 118 119/// BuildMI - This version of the builder also sets up the first "operand" as a 120/// destination virtual register. NumOperands is the number of additional add* 121/// calls that are expected, it does not include the destination register. 122/// 123inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands, 124 unsigned DestReg) { 125 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1, 126 true, true)).addReg(DestReg, MOTy::Def); 127} 128 129 130/// BuildMI - This version of the builder inserts the built MachineInstr into 131/// the specified MachineBasicBlock. 132/// 133inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode, 134 unsigned NumOperands) { 135 return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands)); 136} 137 138/// BuildMI - This version of the builder inserts the built MachineInstr into 139/// the specified MachineBasicBlock, and also sets up the first "operand" as a 140/// destination virtual register. NumOperands is the number of additional add* 141/// calls that are expected, it does not include the destination register. 142/// 143inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode, 144 unsigned NumOperands, unsigned DestReg) { 145 return MachineInstrBuilder(new MachineInstr(BB, Opcode, 146 NumOperands+1)).addReg(DestReg, 147 MOTy::Def); 148} 149 150#endif 151