MachineInstrBuilder.h revision 6a8a9b4413cd595c8322e7ebb7d42cbb04ca6933
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/MachineBasicBlock.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( 42 int RegNo, 43 MachineOperand::UseType Ty = MachineOperand::Use) const { 44 MI->addRegOperand(RegNo, Ty); 45 return *this; 46 } 47 48 /// addReg - Add an LLVM value that is to be used as a register... 49 /// 50 const MachineInstrBuilder &addReg( 51 Value *V, 52 MachineOperand::UseType Ty = MachineOperand::Use) const { 53 MI->addRegOperand(V, Ty); 54 return *this; 55 } 56 57 /// addReg - Add an LLVM value that is to be used as a register... 58 /// 59 const MachineInstrBuilder &addCCReg( 60 Value *V, 61 MachineOperand::UseType Ty = MachineOperand::Use) const { 62 MI->addCCRegOperand(V, Ty); 63 return *this; 64 } 65 66 /// addRegDef - Add an LLVM value that is to be defined as a register... this 67 /// is the same as addReg(V, MachineOperand::Def). 68 /// 69 const MachineInstrBuilder &addRegDef(Value *V) const { 70 return addReg(V, MachineOperand::Def); 71 } 72 73 /// addPCDisp - Add an LLVM value to be treated as a PC relative 74 /// displacement... 75 /// 76 const MachineInstrBuilder &addPCDisp(Value *V) const { 77 MI->addPCDispOperand(V); 78 return *this; 79 } 80 81 /// addMReg - Add a machine register operand... 82 /// 83 const MachineInstrBuilder &addMReg(int Reg, MachineOperand::UseType Ty 84 = MachineOperand::Use) const { 85 MI->addMachineRegOperand(Reg, Ty); 86 return *this; 87 } 88 89 /// addImm - Add a new immediate operand. 90 /// 91 const MachineInstrBuilder &addImm(int Val) const { 92 MI->addZeroExtImmOperand(Val); 93 return *this; 94 } 95 96 /// addSImm - Add a new sign extended immediate operand... 97 /// 98 const MachineInstrBuilder &addSImm(int val) const { 99 MI->addSignExtImmOperand(val); 100 return *this; 101 } 102 103 /// addZImm - Add a new zero extended immediate operand... 104 /// 105 const MachineInstrBuilder &addZImm(unsigned Val) const { 106 MI->addZeroExtImmOperand(Val); 107 return *this; 108 } 109 110 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const { 111 MI->addMachineBasicBlockOperand(MBB); 112 return *this; 113 } 114 115 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const { 116 MI->addFrameIndexOperand(Idx); 117 return *this; 118 } 119 120 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx) const { 121 MI->addConstantPoolIndexOperand(Idx); 122 return *this; 123 } 124 125 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV, 126 bool isPCRelative = false) const { 127 MI->addGlobalAddressOperand(GV, isPCRelative); 128 return *this; 129 } 130 131 const MachineInstrBuilder &addExternalSymbol(const std::string &Name, 132 bool isPCRelative = false) const{ 133 MI->addExternalSymbolOperand(Name, isPCRelative); 134 return *this; 135 } 136}; 137 138/// BuildMI - Builder interface. Specify how to create the initial instruction 139/// itself. NumOperands is the number of operands to the machine instruction to 140/// allow for memory efficient representation of machine instructions. 141/// 142inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) { 143 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true)); 144} 145 146/// BuildMI - This version of the builder also sets up the first "operand" as a 147/// destination virtual register. NumOperands is the number of additional add* 148/// calls that are expected, it does not include the destination register. 149/// 150inline MachineInstrBuilder BuildMI( 151 int Opcode, unsigned NumOperands, 152 unsigned DestReg, 153 MachineOperand::UseType useType = MachineOperand::Def) { 154 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1, 155 true, true)).addReg(DestReg, useType); 156} 157 158 159/// BuildMI - Insert the instruction before a specified location in the basic 160/// block. 161inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 162 MachineBasicBlock::iterator I, 163 int Opcode, unsigned NumOperands, 164 unsigned DestReg) { 165 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true); 166 BB.insert(I, MI); 167 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def); 168} 169 170/// BMI - A special BuildMI variant that takes an iterator to insert the 171/// instruction at as well as a basic block. 172inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 173 MachineBasicBlock::iterator I, 174 int Opcode, unsigned NumOperands) { 175 MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true); 176 BB.insert(I, MI); 177 return MachineInstrBuilder(MI); 178} 179 180/// BuildMI - This version of the builder inserts the built MachineInstr into 181/// the specified MachineBasicBlock. 182/// 183inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode, 184 unsigned NumOperands) { 185 return BuildMI(*BB, BB->end(), Opcode, NumOperands); 186} 187 188/// BuildMI - This version of the builder inserts the built MachineInstr into 189/// the specified MachineBasicBlock, and also sets up the first "operand" as a 190/// destination virtual register. NumOperands is the number of additional add* 191/// calls that are expected, it does not include the destination register. 192/// 193inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode, 194 unsigned NumOperands, unsigned DestReg) { 195 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg); 196} 197 198} // End llvm namespace 199 200#endif 201