LLVM API Documentation
00001 //===-- X86InstrBuilder.h - Functions to aid building x86 insts -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file exposes functions that may be used with BuildMI from the 00011 // MachineInstrBuilder.h file to handle X86'isms in a clean way. 00012 // 00013 // The BuildMem function may be used with the BuildMI function to add entire 00014 // memory references in a single, typed, function call. X86 memory references 00015 // can be very complex expressions (described in the README), so wrapping them 00016 // up behind an easier to use interface makes sense. Descriptions of the 00017 // functions are included below. 00018 // 00019 // For reference, the order of operands for memory references is: 00020 // (Operand), Base, Scale, Index, Displacement. 00021 // 00022 //===----------------------------------------------------------------------===// 00023 00024 #ifndef X86INSTRBUILDER_H 00025 #define X86INSTRBUILDER_H 00026 00027 #include "llvm/CodeGen/MachineFrameInfo.h" 00028 #include "llvm/CodeGen/MachineInstrBuilder.h" 00029 #include "llvm/CodeGen/MachineMemOperand.h" 00030 00031 namespace llvm { 00032 00033 /// X86AddressMode - This struct holds a generalized full x86 address mode. 00034 /// The base register can be a frame index, which will eventually be replaced 00035 /// with BP or SP and Disp being offsetted accordingly. The displacement may 00036 /// also include the offset of a global value. 00037 struct X86AddressMode { 00038 enum { 00039 RegBase, 00040 FrameIndexBase 00041 } BaseType; 00042 00043 union { 00044 unsigned Reg; 00045 int FrameIndex; 00046 } Base; 00047 00048 unsigned Scale; 00049 unsigned IndexReg; 00050 int Disp; 00051 const GlobalValue *GV; 00052 unsigned GVOpFlags; 00053 00054 X86AddressMode() 00055 : BaseType(RegBase), Scale(1), IndexReg(0), Disp(0), GV(0), GVOpFlags(0) { 00056 Base.Reg = 0; 00057 } 00058 00059 00060 void getFullAddress(SmallVectorImpl<MachineOperand> &MO) { 00061 assert(Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8); 00062 00063 if (BaseType == X86AddressMode::RegBase) 00064 MO.push_back(MachineOperand::CreateReg(Base.Reg, false, false, 00065 false, false, false, 0, false)); 00066 else { 00067 assert(BaseType == X86AddressMode::FrameIndexBase); 00068 MO.push_back(MachineOperand::CreateFI(Base.FrameIndex)); 00069 } 00070 00071 MO.push_back(MachineOperand::CreateImm(Scale)); 00072 MO.push_back(MachineOperand::CreateReg(IndexReg, false, false, 00073 false, false, false, 0, false)); 00074 00075 if (GV) 00076 MO.push_back(MachineOperand::CreateGA(GV, Disp, GVOpFlags)); 00077 else 00078 MO.push_back(MachineOperand::CreateImm(Disp)); 00079 00080 MO.push_back(MachineOperand::CreateReg(0, false, false, 00081 false, false, false, 0, false)); 00082 } 00083 }; 00084 00085 /// addDirectMem - This function is used to add a direct memory reference to the 00086 /// current instruction -- that is, a dereference of an address in a register, 00087 /// with no scale, index or displacement. An example is: DWORD PTR [EAX]. 00088 /// 00089 static inline const MachineInstrBuilder & 00090 addDirectMem(const MachineInstrBuilder &MIB, unsigned Reg) { 00091 // Because memory references are always represented with five 00092 // values, this adds: Reg, 1, NoReg, 0, NoReg to the instruction. 00093 return MIB.addReg(Reg).addImm(1).addReg(0).addImm(0).addReg(0); 00094 } 00095 00096 00097 static inline const MachineInstrBuilder & 00098 addOffset(const MachineInstrBuilder &MIB, int Offset) { 00099 return MIB.addImm(1).addReg(0).addImm(Offset).addReg(0); 00100 } 00101 00102 /// addRegOffset - This function is used to add a memory reference of the form 00103 /// [Reg + Offset], i.e., one with no scale or index, but with a 00104 /// displacement. An example is: DWORD PTR [EAX + 4]. 00105 /// 00106 static inline const MachineInstrBuilder & 00107 addRegOffset(const MachineInstrBuilder &MIB, 00108 unsigned Reg, bool isKill, int Offset) { 00109 return addOffset(MIB.addReg(Reg, getKillRegState(isKill)), Offset); 00110 } 00111 00112 /// addRegReg - This function is used to add a memory reference of the form: 00113 /// [Reg + Reg]. 00114 static inline const MachineInstrBuilder &addRegReg(const MachineInstrBuilder &MIB, 00115 unsigned Reg1, bool isKill1, 00116 unsigned Reg2, bool isKill2) { 00117 return MIB.addReg(Reg1, getKillRegState(isKill1)).addImm(1) 00118 .addReg(Reg2, getKillRegState(isKill2)).addImm(0).addReg(0); 00119 } 00120 00121 static inline const MachineInstrBuilder & 00122 addFullAddress(const MachineInstrBuilder &MIB, 00123 const X86AddressMode &AM) { 00124 assert(AM.Scale == 1 || AM.Scale == 2 || AM.Scale == 4 || AM.Scale == 8); 00125 00126 if (AM.BaseType == X86AddressMode::RegBase) 00127 MIB.addReg(AM.Base.Reg); 00128 else { 00129 assert(AM.BaseType == X86AddressMode::FrameIndexBase); 00130 MIB.addFrameIndex(AM.Base.FrameIndex); 00131 } 00132 00133 MIB.addImm(AM.Scale).addReg(AM.IndexReg); 00134 if (AM.GV) 00135 MIB.addGlobalAddress(AM.GV, AM.Disp, AM.GVOpFlags); 00136 else 00137 MIB.addImm(AM.Disp); 00138 00139 return MIB.addReg(0); 00140 } 00141 00142 /// addFrameReference - This function is used to add a reference to the base of 00143 /// an abstract object on the stack frame of the current function. This 00144 /// reference has base register as the FrameIndex offset until it is resolved. 00145 /// This allows a constant offset to be specified as well... 00146 /// 00147 static inline const MachineInstrBuilder & 00148 addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) { 00149 MachineInstr *MI = MIB; 00150 MachineFunction &MF = *MI->getParent()->getParent(); 00151 MachineFrameInfo &MFI = *MF.getFrameInfo(); 00152 const MCInstrDesc &MCID = MI->getDesc(); 00153 unsigned Flags = 0; 00154 if (MCID.mayLoad()) 00155 Flags |= MachineMemOperand::MOLoad; 00156 if (MCID.mayStore()) 00157 Flags |= MachineMemOperand::MOStore; 00158 MachineMemOperand *MMO = 00159 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI, Offset), 00160 Flags, MFI.getObjectSize(FI), 00161 MFI.getObjectAlignment(FI)); 00162 return addOffset(MIB.addFrameIndex(FI), Offset) 00163 .addMemOperand(MMO); 00164 } 00165 00166 /// addConstantPoolReference - This function is used to add a reference to the 00167 /// base of a constant value spilled to the per-function constant pool. The 00168 /// reference uses the abstract ConstantPoolIndex which is retained until 00169 /// either machine code emission or assembly output. In PIC mode on x86-32, 00170 /// the GlobalBaseReg parameter can be used to make this a 00171 /// GlobalBaseReg-relative reference. 00172 /// 00173 static inline const MachineInstrBuilder & 00174 addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI, 00175 unsigned GlobalBaseReg, unsigned char OpFlags) { 00176 //FIXME: factor this 00177 return MIB.addReg(GlobalBaseReg).addImm(1).addReg(0) 00178 .addConstantPoolIndex(CPI, 0, OpFlags).addReg(0); 00179 } 00180 00181 } // End llvm namespace 00182 00183 #endif