LLVM API Documentation
00001 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file exposes a function named BuildMI, which is useful for dramatically 00011 // simplifying how MachineInstr's are created. It allows use of code like this: 00012 // 00013 // M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2); 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H 00018 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H 00019 00020 #include "llvm/CodeGen/MachineBasicBlock.h" 00021 00022 namespace llvm { 00023 00024 class MachineInstrBuilder { 00025 MachineInstr *MI; 00026 public: 00027 MachineInstrBuilder(MachineInstr *mi) : MI(mi) {} 00028 00029 /// Allow automatic conversion to the machine instruction we are working on. 00030 /// 00031 operator MachineInstr*() const { return MI; } 00032 operator MachineBasicBlock::iterator() const { return MI; } 00033 00034 /// addReg - Add a new virtual register operand... 00035 /// 00036 const MachineInstrBuilder &addReg( 00037 int RegNo, 00038 MachineOperand::UseType Ty = MachineOperand::Use) const { 00039 MI->addRegOperand(RegNo, Ty); 00040 return *this; 00041 } 00042 00043 /// addImm - Add a new immediate operand. 00044 /// 00045 const MachineInstrBuilder &addImm(int64_t Val) const { 00046 MI->addImmOperand(Val); 00047 return *this; 00048 } 00049 00050 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB) const { 00051 MI->addMachineBasicBlockOperand(MBB); 00052 return *this; 00053 } 00054 00055 const MachineInstrBuilder &addFrameIndex(unsigned Idx) const { 00056 MI->addFrameIndexOperand(Idx); 00057 return *this; 00058 } 00059 00060 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx, 00061 int Offset = 0) const { 00062 MI->addConstantPoolIndexOperand(Idx, Offset); 00063 return *this; 00064 } 00065 00066 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx) const { 00067 MI->addJumpTableIndexOperand(Idx); 00068 return *this; 00069 } 00070 00071 const MachineInstrBuilder &addGlobalAddress(GlobalValue *GV, 00072 int Offset = 0) const { 00073 MI->addGlobalAddressOperand(GV, Offset); 00074 return *this; 00075 } 00076 00077 const MachineInstrBuilder &addExternalSymbol(const char *FnName) const{ 00078 MI->addExternalSymbolOperand(FnName); 00079 return *this; 00080 } 00081 }; 00082 00083 /// BuildMI - Builder interface. Specify how to create the initial instruction 00084 /// itself. NumOperands is the number of operands to the machine instruction to 00085 /// allow for memory efficient representation of machine instructions. 00086 /// 00087 inline MachineInstrBuilder BuildMI(int Opcode, unsigned NumOperands) { 00088 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands)); 00089 } 00090 00091 /// BuildMI - This version of the builder sets up the first operand as a 00092 /// destination virtual register. NumOperands is the number of additional add* 00093 /// calls that are expected, not including the destination register. 00094 /// 00095 inline MachineInstrBuilder BuildMI( 00096 int Opcode, unsigned NumOperands, 00097 unsigned DestReg, 00098 MachineOperand::UseType useType = MachineOperand::Def) { 00099 return MachineInstrBuilder(new MachineInstr(Opcode, NumOperands+1)) 00100 .addReg(DestReg, useType); 00101 } 00102 00103 /// BuildMI - This version of the builder inserts the newly-built 00104 /// instruction before the given position in the given MachineBasicBlock, and 00105 /// sets up the first operand as a destination virtual register. 00106 /// NumOperands is the number of additional add* calls that are expected, 00107 /// not including the destination register. 00108 /// 00109 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 00110 MachineBasicBlock::iterator I, 00111 int Opcode, unsigned NumOperands, 00112 unsigned DestReg) { 00113 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1); 00114 BB.insert(I, MI); 00115 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def); 00116 } 00117 00118 /// BuildMI - This version of the builder inserts the newly-built 00119 /// instruction before the given position in the given MachineBasicBlock, and 00120 /// does NOT take a destination register. 00121 /// 00122 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, 00123 MachineBasicBlock::iterator I, 00124 int Opcode, unsigned NumOperands) { 00125 MachineInstr *MI = new MachineInstr(Opcode, NumOperands); 00126 BB.insert(I, MI); 00127 return MachineInstrBuilder(MI); 00128 } 00129 00130 /// BuildMI - This version of the builder inserts the newly-built 00131 /// instruction at the end of the given MachineBasicBlock, and does NOT take a 00132 /// destination register. 00133 /// 00134 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode, 00135 unsigned NumOperands) { 00136 return BuildMI(*BB, BB->end(), Opcode, NumOperands); 00137 } 00138 00139 /// BuildMI - This version of the builder inserts the newly-built 00140 /// instruction at the end of the given MachineBasicBlock, and sets up the first 00141 /// operand as a destination virtual register. NumOperands is the number of 00142 /// additional add* calls that are expected, not including the destination 00143 /// register. 00144 /// 00145 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, int Opcode, 00146 unsigned NumOperands, unsigned DestReg) { 00147 return BuildMI(*BB, BB->end(), Opcode, NumOperands, DestReg); 00148 } 00149 00150 } // End llvm namespace 00151 00152 #endif