LLVM API Documentation

X86InstrBuilder.h
Go to the documentation of this file.
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 LLVM_LIB_TARGET_X86_X86INSTRBUILDER_H
00025 #define LLVM_LIB_TARGET_X86_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(nullptr),
00056       GVOpFlags(0) {
00057     Base.Reg = 0;
00058   }
00059 
00060 
00061   void getFullAddress(SmallVectorImpl<MachineOperand> &MO) {
00062     assert(Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8);
00063 
00064     if (BaseType == X86AddressMode::RegBase)
00065       MO.push_back(MachineOperand::CreateReg(Base.Reg, false, false,
00066                                              false, false, false, 0, false));
00067     else {
00068       assert(BaseType == X86AddressMode::FrameIndexBase);
00069       MO.push_back(MachineOperand::CreateFI(Base.FrameIndex));
00070     }
00071 
00072     MO.push_back(MachineOperand::CreateImm(Scale));
00073     MO.push_back(MachineOperand::CreateReg(IndexReg, false, false,
00074                                            false, false, false, 0, false));
00075 
00076     if (GV)
00077       MO.push_back(MachineOperand::CreateGA(GV, Disp, GVOpFlags));
00078     else
00079       MO.push_back(MachineOperand::CreateImm(Disp));
00080 
00081     MO.push_back(MachineOperand::CreateReg(0, false, false,
00082                                            false, false, false, 0, false));
00083   }
00084 };
00085 
00086 /// addDirectMem - This function is used to add a direct memory reference to the
00087 /// current instruction -- that is, a dereference of an address in a register,
00088 /// with no scale, index or displacement. An example is: DWORD PTR [EAX].
00089 ///
00090 static inline const MachineInstrBuilder &
00091 addDirectMem(const MachineInstrBuilder &MIB, unsigned Reg) {
00092   // Because memory references are always represented with five
00093   // values, this adds: Reg, 1, NoReg, 0, NoReg to the instruction.
00094   return MIB.addReg(Reg).addImm(1).addReg(0).addImm(0).addReg(0);
00095 }
00096 
00097 
00098 static inline const MachineInstrBuilder &
00099 addOffset(const MachineInstrBuilder &MIB, int Offset) {
00100   return MIB.addImm(1).addReg(0).addImm(Offset).addReg(0);
00101 }
00102 
00103 /// addRegOffset - This function is used to add a memory reference of the form
00104 /// [Reg + Offset], i.e., one with no scale or index, but with a
00105 /// displacement. An example is: DWORD PTR [EAX + 4].
00106 ///
00107 static inline const MachineInstrBuilder &
00108 addRegOffset(const MachineInstrBuilder &MIB,
00109              unsigned Reg, bool isKill, int Offset) {
00110   return addOffset(MIB.addReg(Reg, getKillRegState(isKill)), Offset);
00111 }
00112 
00113 /// addRegReg - This function is used to add a memory reference of the form:
00114 /// [Reg + Reg].
00115 static inline const MachineInstrBuilder &addRegReg(const MachineInstrBuilder &MIB,
00116                                             unsigned Reg1, bool isKill1,
00117                                             unsigned Reg2, bool isKill2) {
00118   return MIB.addReg(Reg1, getKillRegState(isKill1)).addImm(1)
00119     .addReg(Reg2, getKillRegState(isKill2)).addImm(0).addReg(0);
00120 }
00121 
00122 static inline const MachineInstrBuilder &
00123 addFullAddress(const MachineInstrBuilder &MIB,
00124                const X86AddressMode &AM) {
00125   assert(AM.Scale == 1 || AM.Scale == 2 || AM.Scale == 4 || AM.Scale == 8);
00126 
00127   if (AM.BaseType == X86AddressMode::RegBase)
00128     MIB.addReg(AM.Base.Reg);
00129   else {
00130     assert(AM.BaseType == X86AddressMode::FrameIndexBase);
00131     MIB.addFrameIndex(AM.Base.FrameIndex);
00132   }
00133 
00134   MIB.addImm(AM.Scale).addReg(AM.IndexReg);
00135   if (AM.GV)
00136     MIB.addGlobalAddress(AM.GV, AM.Disp, AM.GVOpFlags);
00137   else
00138     MIB.addImm(AM.Disp);
00139 
00140   return MIB.addReg(0);
00141 }
00142 
00143 /// addFrameReference - This function is used to add a reference to the base of
00144 /// an abstract object on the stack frame of the current function.  This
00145 /// reference has base register as the FrameIndex offset until it is resolved.
00146 /// This allows a constant offset to be specified as well...
00147 ///
00148 static inline const MachineInstrBuilder &
00149 addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
00150   MachineInstr *MI = MIB;
00151   MachineFunction &MF = *MI->getParent()->getParent();
00152   MachineFrameInfo &MFI = *MF.getFrameInfo();
00153   const MCInstrDesc &MCID = MI->getDesc();
00154   unsigned Flags = 0;
00155   if (MCID.mayLoad())
00156     Flags |= MachineMemOperand::MOLoad;
00157   if (MCID.mayStore())
00158     Flags |= MachineMemOperand::MOStore;
00159   MachineMemOperand *MMO =
00160     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI, Offset),
00161                             Flags, MFI.getObjectSize(FI),
00162                             MFI.getObjectAlignment(FI));
00163   return addOffset(MIB.addFrameIndex(FI), Offset)
00164             .addMemOperand(MMO);
00165 }
00166 
00167 /// addConstantPoolReference - This function is used to add a reference to the
00168 /// base of a constant value spilled to the per-function constant pool.  The
00169 /// reference uses the abstract ConstantPoolIndex which is retained until
00170 /// either machine code emission or assembly output. In PIC mode on x86-32,
00171 /// the GlobalBaseReg parameter can be used to make this a
00172 /// GlobalBaseReg-relative reference.
00173 ///
00174 static inline const MachineInstrBuilder &
00175 addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI,
00176                          unsigned GlobalBaseReg, unsigned char OpFlags) {
00177   //FIXME: factor this
00178   return MIB.addReg(GlobalBaseReg).addImm(1).addReg(0)
00179     .addConstantPoolIndex(CPI, 0, OpFlags).addReg(0);
00180 }
00181 
00182 } // End llvm namespace
00183 
00184 #endif