MachineInstrBuilder.h revision 9cc361579b6a0aad9a71dc617eedd4d909d48acf
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 21struct MachineInstrBuilder { 22 MachineInstr *MI; 23 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 MachineInstrBuilder &addReg(int RegNo, bool isDef = false) { 33 MI->addRegOperand(RegNo, isDef); 34 return *this; 35 } 36 37 /// addReg - Add an LLVM value that is to be used as a register...x 38 /// 39 MachineInstrBuilder &addReg(Value *V, bool isDef = false, bool isDNU = false){ 40 MI->addRegOperand(V, isDef, isDNU); 41 return *this; 42 } 43 44 /// addPCDisp - Add an LLVM value to be treated as a PC relative 45 /// displacement... 46 /// 47 MachineInstrBuilder &addPCDisp(Value *V) { 48 MI->addPCDispOperand(V); 49 return *this; 50 } 51 52 /// addMReg - Add a machine register operand... 53 /// 54 MachineInstrBuilder &addMReg(int Reg, bool isDef=false) { 55 MI->addMachineRegOperand(Reg, isDef); 56 return *this; 57 } 58 59 /// addSImm - Add a new sign extended immediate operand... 60 /// 61 MachineInstrBuilder &addSImm(int64_t val) { 62 MI->addSignExtImmOperand(val); 63 return *this; 64 } 65 66 /// addZImm - Add a new zero extended immediate operand... 67 /// 68 MachineInstrBuilder &addZImm(int64_t Val) { 69 MI->addZeroExtImmOperand(Val); 70 return *this; 71 } 72}; 73 74/// BuildMI - Builder interface. Specify how to create the initial instruction 75/// itself. NumOperands is the number of operands to the machine instruction to 76/// allow for memory efficient representation of machine instructions. 77/// 78inline MachineInstrBuilder BuildMI(MachineOpCode Opcode, unsigned NumOperands) { 79 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands, true, true)); 80} 81 82/// BuildMI - This version of the builder inserts the built MachineInstr into 83/// the specified MachineBasicBlock. 84/// 85inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode, 86 unsigned NumOperands) { 87 return MachineInstrBuilder(new MachineInstr(BB, Opcode, NumOperands)); 88} 89 90/// BuildMI - This version of the builder inserts the built MachineInstr into 91/// the specified MachineBasicBlock, and also sets up the first "operand" as a 92/// destination virtual register. NumOperands is the number of additional add* 93/// calls that are expected, it does not include the destination register. 94/// 95inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, MachineOpCode Opcode, 96 unsigned NumOperands, unsigned DestReg) { 97 return MachineInstrBuilder(new MachineInstr(BB, Opcode, 98 NumOperands+1)).addReg(DestReg, 99 true); 100} 101 102#endif 103