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