MachineInstrBuilder.h revision 8b915b4ed2c6e43413937ac71c0cbcf476ad1a98
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 22namespace llvm { 23 24class MachineInstrBuilder { 25 MachineInstr *MI; 26public: 27 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {} 28 29 /// Allow automatic conversion to the machine instruction we are working on. 30 /// 31 operator MachineInstr*() const { return MI; } 32 operator MachineBasicBlock::iterator() const { return MI; } 33 34 /// addReg - Add a new virtual register operand... 35 /// 36 const MachineInstrBuilder &addReg( 37 int RegNo, 38 MachineOperand::UseType Ty = MachineOperand::Use) const { 39 MI->addRegOperand(RegNo, Ty); 40 return *this; 41 } 42 43 /// addImm - Add a new immediate operand. 44 /// 45 const MachineInstrBuilder &addImm(int64_t Val) const { 46 MI->addImmOperand(Val); 47 return *this; 48 } 49 50 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const { 51 MI->addMachineBasicBlockOperand(MBB); 52 return *this; 53 } 54 55 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const { 56 MI->addFrameIndexOperand(Idx); 57 return *this; 58 } 59 60 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx, 61 int Offset = 0) const { 62 MI->addConstantPoolIndexOperand(Idx, Offset); 63 return *this; 64 } 65 66 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const { 67 MI->addJumpTableIndexOperand(Idx); 68 return *this; 69 } 70 71 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV, 72 int Offset = 0) const { 73 MI->addGlobalAddressOperand(GV, Offset); 74 return *this; 75 } 76 77 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{ 78 MI->addExternalSymbolOperand(FnName); 79 return *this; 80 } 81}; 82 83/// BuildMI - Builder interface. Specify how to create the initial instruction 84/// itself. NumOperands is the number of operands to the machine instruction to 85/// allow for memory efficient representation of machine instructions. 86/// 87inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) { 88 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands)); 89} 90 91/// BuildMI - This version of the builder sets up the first operand as a 92/// destination virtual register. NumOperands is the number of additional add* 93/// calls that are expected, not including the destination register. 94/// 95inline MachineInstrBuilder BuildMI( 96 int Opcode, unsigned NumOperands, 97 unsigned DestReg, 98 MachineOperand::UseType useType = MachineOperand::Def) { 99 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1)) 100 .addReg(DestReg, useType); 101} 102 103/// BuildMI - This version of the builder inserts the newly-built 104/// instruction before the given position in the given MachineBasicBlock, and 105/// sets up the first operand as a destination virtual register. 106/// NumOperands is the number of additional add* calls that are expected, 107/// not including the destination register. 108/// 109inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 110 MachineBasicBlock::iterator I, 111 int Opcode, unsigned NumOperands, 112 unsigned DestReg) { 113 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1); 114 BB.insert(I, MI); 115 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def); 116} 117 118/// BuildMI - This version of the builder inserts the newly-built 119/// instruction before the given position in the given MachineBasicBlock, and 120/// does NOT take a destination register. 121/// 122inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 123 MachineBasicBlock::iterator I, 124 int Opcode, unsigned NumOperands) { 125 MachineInstr *MI = new MachineInstr(Opcode, NumOperands); 126 BB.insert(I, MI); 127 return MachineInstrBuilder(MI); 128} 129 130/// BuildMI - This version of the builder inserts the newly-built 131/// instruction at the end of the given MachineBasicBlock, and does NOT take a 132/// destination register. 133/// 134inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode, 135 unsigned NumOperands) { 136 return BuildMI(*BB, BB->end(), Opcode, NumOperands); 137} 138 139/// BuildMI - This version of the builder inserts the newly-built 140/// instruction at the end of the given MachineBasicBlock, and sets up the first 141/// operand as a destination virtual register. NumOperands is the number of 142/// additional add* calls that are expected, not including the destination 143/// register. 144/// 145inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode, 146 unsigned NumOperands, unsigned DestReg) { 147 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg); 148} 149 150} // End llvm namespace 151 152#endif 153