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