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 X86INSTRBUILDER_H
00025 #define X86INSTRBUILDER_H
00026 
00027 #include "llvm/CodeGen/MachineFrameInfo.h"
00028 #include "llvm/CodeGen/MachineInstrBuilder.h"
00029 #include "llvm/CodeGen/PseudoSourceValue.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   unsigned Disp;
00051   GlobalValue *GV;
00052 
00053   X86AddressMode() : BaseType(RegBase), Scale(1), IndexReg(0), Disp(0), GV(0) {
00054     Base.Reg = 0;
00055   }
00056 };
00057 
00058 /// addDirectMem - This function is used to add a direct memory reference to the
00059 /// current instruction -- that is, a dereference of an address in a register,
00060 /// with no scale, index or displacement. An example is: DWORD PTR [EAX].
00061 ///
00062 inline const MachineInstrBuilder &addDirectMem(const MachineInstrBuilder &MIB,
00063                                                unsigned Reg) {
00064   // Because memory references are always represented with four
00065   // values, this adds: Reg, [1, NoReg, 0] to the instruction.
00066   return MIB.addReg(Reg).addImm(1).addReg(0).addImm(0);
00067 }
00068 
00069 
00070 /// addRegOffset - This function is used to add a memory reference of the form
00071 /// [Reg + Offset], i.e., one with no scale or index, but with a
00072 /// displacement. An example is: DWORD PTR [EAX + 4].
00073 ///
00074 inline const MachineInstrBuilder &addRegOffset(const MachineInstrBuilder &MIB,
00075                                                unsigned Reg, bool isKill,
00076                                                int Offset) {
00077   return MIB.addReg(Reg, false, false, isKill)
00078     .addImm(1).addReg(0).addImm(Offset);
00079 }
00080 
00081 /// addRegReg - This function is used to add a memory reference of the form:
00082 /// [Reg + Reg].
00083 inline const MachineInstrBuilder &addRegReg(const MachineInstrBuilder &MIB,
00084                                             unsigned Reg1, bool isKill1,
00085                                             unsigned Reg2, bool isKill2) {
00086   return MIB.addReg(Reg1, false, false, isKill1).addImm(1)
00087     .addReg(Reg2, false, false, isKill2).addImm(0);
00088 }
00089 
00090 inline const MachineInstrBuilder &addFullAddress(const MachineInstrBuilder &MIB,
00091                                                  const X86AddressMode &AM) {
00092   assert (AM.Scale == 1 || AM.Scale == 2 || AM.Scale == 4 || AM.Scale == 8);
00093 
00094   if (AM.BaseType == X86AddressMode::RegBase)
00095     MIB.addReg(AM.Base.Reg);
00096   else if (AM.BaseType == X86AddressMode::FrameIndexBase)
00097     MIB.addFrameIndex(AM.Base.FrameIndex);
00098   else
00099     assert (0);
00100   MIB.addImm(AM.Scale).addReg(AM.IndexReg);
00101   if (AM.GV)
00102     return MIB.addGlobalAddress(AM.GV, AM.Disp);
00103   else
00104     return MIB.addImm(AM.Disp);
00105 }
00106 
00107 /// addFrameReference - This function is used to add a reference to the base of
00108 /// an abstract object on the stack frame of the current function.  This
00109 /// reference has base register as the FrameIndex offset until it is resolved.
00110 /// This allows a constant offset to be specified as well...
00111 ///
00112 inline const MachineInstrBuilder &
00113 addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
00114   MachineInstr *MI = MIB;
00115   MachineFunction &MF = *MI->getParent()->getParent();
00116   MachineFrameInfo &MFI = *MF.getFrameInfo();
00117   const TargetInstrDesc &TID = MI->getDesc();
00118   unsigned Flags = 0;
00119   if (TID.mayLoad())
00120     Flags |= MachineMemOperand::MOLoad;
00121   if (TID.mayStore())
00122     Flags |= MachineMemOperand::MOStore;
00123   MachineMemOperand MMO(PseudoSourceValue::getFixedStack(FI),
00124                         Flags,
00125                         MFI.getObjectOffset(FI) + Offset,
00126                         MFI.getObjectSize(FI),
00127                         MFI.getObjectAlignment(FI));
00128   return MIB.addFrameIndex(FI).addImm(1).addReg(0).addImm(Offset)
00129             .addMemOperand(MMO);
00130 }
00131 
00132 /// addConstantPoolReference - This function is used to add a reference to the
00133 /// base of a constant value spilled to the per-function constant pool.  The
00134 /// reference uses the abstract ConstantPoolIndex which is retained until
00135 /// either machine code emission or assembly output. In PIC mode on x86-32,
00136 /// the GlobalBaseReg parameter can be used to make this a
00137 /// GlobalBaseReg-relative reference.
00138 ///
00139 inline const MachineInstrBuilder &
00140 addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI,
00141                          unsigned GlobalBaseReg = 0) {
00142   return MIB.addReg(GlobalBaseReg).addImm(1).addReg(0).addConstantPoolIndex(CPI);
00143 }
00144 
00145 } // End llvm namespace
00146 
00147 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.