LLVM  3.7.0
X86InstrBuilder.h
Go to the documentation of this file.
1 //===-- X86InstrBuilder.h - Functions to aid building x86 insts -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file exposes functions that may be used with BuildMI from the
11 // MachineInstrBuilder.h file to handle X86'isms in a clean way.
12 //
13 // The BuildMem function may be used with the BuildMI function to add entire
14 // memory references in a single, typed, function call. X86 memory references
15 // can be very complex expressions (described in the README), so wrapping them
16 // up behind an easier to use interface makes sense. Descriptions of the
17 // functions are included below.
18 //
19 // For reference, the order of operands for memory references is:
20 // (Operand), Base, Scale, Index, Displacement.
21 //
22 //===----------------------------------------------------------------------===//
23 
24 #ifndef LLVM_LIB_TARGET_X86_X86INSTRBUILDER_H
25 #define LLVM_LIB_TARGET_X86_X86INSTRBUILDER_H
26 
30 
31 namespace llvm {
32 
33 /// X86AddressMode - This struct holds a generalized full x86 address mode.
34 /// The base register can be a frame index, which will eventually be replaced
35 /// with BP or SP and Disp being offsetted accordingly. The displacement may
36 /// also include the offset of a global value.
38  enum {
41  } BaseType;
42 
43  union {
44  unsigned Reg;
46  } Base;
47 
48  unsigned Scale;
49  unsigned IndexReg;
50  int Disp;
51  const GlobalValue *GV;
52  unsigned GVOpFlags;
53 
55  : BaseType(RegBase), Scale(1), IndexReg(0), Disp(0), GV(nullptr),
56  GVOpFlags(0) {
57  Base.Reg = 0;
58  }
59 
60 
62  assert(Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8);
63 
65  MO.push_back(MachineOperand::CreateReg(Base.Reg, false, false,
66  false, false, false, 0, false));
67  else {
69  MO.push_back(MachineOperand::CreateFI(Base.FrameIndex));
70  }
71 
74  false, false, false, 0, false));
75 
76  if (GV)
78  else
80 
81  MO.push_back(MachineOperand::CreateReg(0, false, false,
82  false, false, false, 0, false));
83  }
84 };
85 
86 /// addDirectMem - This function is used to add a direct memory reference to the
87 /// current instruction -- that is, a dereference of an address in a register,
88 /// with no scale, index or displacement. An example is: DWORD PTR [EAX].
89 ///
90 static inline const MachineInstrBuilder &
91 addDirectMem(const MachineInstrBuilder &MIB, unsigned Reg) {
92  // Because memory references are always represented with five
93  // values, this adds: Reg, 1, NoReg, 0, NoReg to the instruction.
94  return MIB.addReg(Reg).addImm(1).addReg(0).addImm(0).addReg(0);
95 }
96 
97 
98 static inline const MachineInstrBuilder &
99 addOffset(const MachineInstrBuilder &MIB, int Offset) {
100  return MIB.addImm(1).addReg(0).addImm(Offset).addReg(0);
101 }
102 
103 /// addRegOffset - This function is used to add a memory reference of the form
104 /// [Reg + Offset], i.e., one with no scale or index, but with a
105 /// displacement. An example is: DWORD PTR [EAX + 4].
106 ///
107 static inline const MachineInstrBuilder &
109  unsigned Reg, bool isKill, int Offset) {
110  return addOffset(MIB.addReg(Reg, getKillRegState(isKill)), Offset);
111 }
112 
113 /// addRegReg - This function is used to add a memory reference of the form:
114 /// [Reg + Reg].
115 static inline const MachineInstrBuilder &addRegReg(const MachineInstrBuilder &MIB,
116  unsigned Reg1, bool isKill1,
117  unsigned Reg2, bool isKill2) {
118  return MIB.addReg(Reg1, getKillRegState(isKill1)).addImm(1)
119  .addReg(Reg2, getKillRegState(isKill2)).addImm(0).addReg(0);
120 }
121 
122 static inline const MachineInstrBuilder &
124  const X86AddressMode &AM) {
125  assert(AM.Scale == 1 || AM.Scale == 2 || AM.Scale == 4 || AM.Scale == 8);
126 
128  MIB.addReg(AM.Base.Reg);
129  else {
131  MIB.addFrameIndex(AM.Base.FrameIndex);
132  }
133 
134  MIB.addImm(AM.Scale).addReg(AM.IndexReg);
135  if (AM.GV)
136  MIB.addGlobalAddress(AM.GV, AM.Disp, AM.GVOpFlags);
137  else
138  MIB.addImm(AM.Disp);
139 
140  return MIB.addReg(0);
141 }
142 
143 /// addFrameReference - This function is used to add a reference to the base of
144 /// an abstract object on the stack frame of the current function. This
145 /// reference has base register as the FrameIndex offset until it is resolved.
146 /// This allows a constant offset to be specified as well...
147 ///
148 static inline const MachineInstrBuilder &
149 addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
150  MachineInstr *MI = MIB;
151  MachineFunction &MF = *MI->getParent()->getParent();
152  MachineFrameInfo &MFI = *MF.getFrameInfo();
153  const MCInstrDesc &MCID = MI->getDesc();
154  unsigned Flags = 0;
155  if (MCID.mayLoad())
156  Flags |= MachineMemOperand::MOLoad;
157  if (MCID.mayStore())
159  MachineMemOperand *MMO =
161  Flags, MFI.getObjectSize(FI),
162  MFI.getObjectAlignment(FI));
163  return addOffset(MIB.addFrameIndex(FI), Offset)
164  .addMemOperand(MMO);
165 }
166 
167 /// addConstantPoolReference - This function is used to add a reference to the
168 /// base of a constant value spilled to the per-function constant pool. The
169 /// reference uses the abstract ConstantPoolIndex which is retained until
170 /// either machine code emission or assembly output. In PIC mode on x86-32,
171 /// the GlobalBaseReg parameter can be used to make this a
172 /// GlobalBaseReg-relative reference.
173 ///
174 static inline const MachineInstrBuilder &
176  unsigned GlobalBaseReg, unsigned char OpFlags) {
177  //FIXME: factor this
178  return MIB.addReg(GlobalBaseReg).addImm(1).addReg(0)
179  .addConstantPoolIndex(CPI, 0, OpFlags).addReg(0);
180 }
181 
182 } // End llvm namespace
183 
184 #endif
The memory access reads data.
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
The memory access writes data.
static const MachineInstrBuilder & addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI, unsigned GlobalBaseReg, unsigned char OpFlags)
addConstantPoolReference - This function is used to add a reference to the base of a constant value s...
bool mayStore() const
Return true if this instruction could possibly modify memory.
Definition: MCInstrDesc.h:356
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:138
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:264
const GlobalValue * GV
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f, uint64_t s, unsigned base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
getMachineMemOperand - Allocate a new MachineMemOperand.
static MachinePointerInfo getFixedStack(int FI, int64_t offset=0)
getFixedStack - Return a MachinePointerInfo record that refers to the the specified FrameIndex...
GlobalBaseReg - On Darwin, this node represents the result of the mflr at function entry...
MachineMemOperand - A description of a memory reference used in the backend.
static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
Reg
All possible values of the reg field in the ModR/M byte.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
const MachineInstrBuilder & addImm(int64_t Val) const
addImm - Add a new immediate operand.
unsigned getKillRegState(bool B)
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:120
static const MachineInstrBuilder & addRegOffset(const MachineInstrBuilder &MIB, unsigned Reg, bool isKill, int Offset)
addRegOffset - This function is used to add a memory reference of the form [Reg + Offset]...
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned char TargetFlags=0)
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0, bool mem=true)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
unsigned getObjectAlignment(int ObjectIdx) const
Return the alignment of the specified stack object.
static const MachineInstrBuilder & addRegReg(const MachineInstrBuilder &MIB, unsigned Reg1, bool isKill1, unsigned Reg2, bool isKill2)
addRegReg - This function is used to add a memory reference of the form: [Reg + Reg].
enum llvm::X86AddressMode::@294 BaseType
void getFullAddress(SmallVectorImpl< MachineOperand > &MO)
bool mayLoad() const
Return true if this instruction could possibly read memory.
Definition: MCInstrDesc.h:350
MachineFrameInfo * getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned char TargetFlags=0) const
Representation of each machine instruction.
Definition: MachineInstr.h:51
static const MachineInstrBuilder & addDirectMem(const MachineInstrBuilder &MIB, unsigned Reg)
addDirectMem - This function is used to add a direct memory reference to the current instruction – th...
static const MachineInstrBuilder & addOffset(const MachineInstrBuilder &MIB, int Offset)
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned char TargetFlags=0) const
static MachineOperand CreateImm(int64_t Val)
X86AddressMode - This struct holds a generalized full x86 address mode.
union llvm::X86AddressMode::@295 Base
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
addReg - Add a new virtual register operand...
static MachineOperand CreateFI(int Idx)
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
static const MachineInstrBuilder & addFullAddress(const MachineInstrBuilder &MIB, const X86AddressMode &AM)