LLVM 22.0.0git
X86InstrBuilder.h
Go to the documentation of this file.
1//===-- X86InstrBuilder.h - Functions to aid building x86 insts -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file exposes functions that may be used with BuildMI from the
10// MachineInstrBuilder.h file to handle X86'isms in a clean way.
11//
12// The BuildMem function may be used with the BuildMI function to add entire
13// memory references in a single, typed, function call. X86 memory references
14// can be very complex expressions (described in the README), so wrapping them
15// up behind an easier to use interface makes sense. Descriptions of the
16// functions are included below.
17//
18// For reference, the order of operands for memory references is:
19// (Operand), Base, Scale, Index, Displacement.
20//
21//===----------------------------------------------------------------------===//
22
23#ifndef LLVM_LIB_TARGET_X86_X86INSTRBUILDER_H
24#define LLVM_LIB_TARGET_X86_X86INSTRBUILDER_H
25
33#include "llvm/MC/MCInstrDesc.h"
34#include <cassert>
35
36namespace llvm {
37
38/// X86AddressMode - This struct holds a generalized full x86 address mode.
39/// The base register can be a frame index, which will eventually be replaced
40/// with BP or SP and Disp being offsetted accordingly. The displacement may
41/// also include the offset of a global value.
44
45 union BaseUnion {
48
49 BaseUnion() : Reg() {}
51
52 unsigned Scale = 1;
54 int Disp = 0;
55 const GlobalValue *GV = nullptr;
56 unsigned GVOpFlags = 0;
57 bool CP = false;
58
60 assert(Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8);
61
63 MO.push_back(MachineOperand::CreateReg(Base.Reg, false, false, false,
64 false, false, false, 0, false));
65 else {
68 }
69
71 MO.push_back(MachineOperand::CreateReg(IndexReg, false, false, false, false,
72 false, false, 0, false));
73
74 if (GV)
76 else
78
79 MO.push_back(MachineOperand::CreateReg(0, false, false, false, false, false,
80 false, 0, false));
81 }
82};
83
84/// Compute the addressing mode from an machine instruction starting with the
85/// given operand.
87 unsigned Operand) {
89 const MachineOperand &Op0 = MI->getOperand(Operand);
90 if (Op0.isReg()) {
92 AM.Base.Reg = Op0.getReg();
93 } else {
95 AM.Base.FrameIndex = Op0.getIndex();
96 }
97
98 const MachineOperand &Op1 = MI->getOperand(Operand + 1);
99 AM.Scale = Op1.getImm();
100
101 const MachineOperand &Op2 = MI->getOperand(Operand + 2);
102 AM.IndexReg = Op2.getReg();
103
104 const MachineOperand &Op3 = MI->getOperand(Operand + 3);
105 if (Op3.isGlobal())
106 AM.GV = Op3.getGlobal();
107 else
108 AM.Disp = Op3.getImm();
109
110 return AM;
111}
112
113/// addDirectMem - This function is used to add a direct memory reference to the
114/// current instruction -- that is, a dereference of an address in a register,
115/// with no scale, index or displacement. An example is: DWORD PTR [EAX].
116///
117static inline const MachineInstrBuilder &
119 // Because memory references are always represented with five
120 // values, this adds: Reg, 1, NoReg, 0, NoReg to the instruction.
121 return MIB.addReg(Reg).addImm(1).addReg(0).addImm(0).addReg(0);
122}
123
124/// Replace the address used in the instruction with the direct memory
125/// reference.
126static inline void setDirectAddressInInstr(MachineInstr *MI, unsigned Operand,
127 Register Reg) {
128 // Direct memory address is in a form of: Reg/FI, 1 (Scale), NoReg, 0, NoReg.
129 MI->getOperand(Operand).ChangeToRegister(Reg, /*isDef=*/false);
130 MI->getOperand(Operand + 1).setImm(1);
131 MI->getOperand(Operand + 2).setReg(0);
132 MI->getOperand(Operand + 3).ChangeToImmediate(0);
133 MI->getOperand(Operand + 4).setReg(0);
134}
135
136static inline const MachineInstrBuilder &
138 return MIB.addImm(1).addReg(0).addImm(Offset).addReg(0);
139}
140
141static inline const MachineInstrBuilder &
143 return MIB.addImm(1).addReg(0).add(Offset).addReg(0);
144}
145
146/// addRegOffset - This function is used to add a memory reference of the form
147/// [Reg + Offset], i.e., one with no scale or index, but with a
148/// displacement. An example is: DWORD PTR [EAX + 4].
149///
150static inline const MachineInstrBuilder &
152 int Offset) {
153 return addOffset(MIB.addReg(Reg, getKillRegState(isKill)), Offset);
154}
155
156/// addRegReg - This function is used to add a memory reference of the form:
157/// [Reg + Reg].
158static inline const MachineInstrBuilder &
159addRegReg(const MachineInstrBuilder &MIB, Register Reg1, bool isKill1,
160 unsigned SubReg1, Register Reg2, bool isKill2, unsigned SubReg2) {
161 return MIB.addReg(Reg1, getKillRegState(isKill1), SubReg1)
162 .addImm(1)
163 .addReg(Reg2, getKillRegState(isKill2), SubReg2)
164 .addImm(0)
165 .addReg(0);
166}
167
168static inline const MachineInstrBuilder &
170 const X86AddressMode &AM) {
171 assert(AM.Scale == 1 || AM.Scale == 2 || AM.Scale == 4 || AM.Scale == 8);
172
174 MIB.addReg(AM.Base.Reg);
175 else {
178 }
179
180 MIB.addImm(AM.Scale).addReg(AM.IndexReg);
181 if (AM.GV)
182 MIB.addGlobalAddress(AM.GV, AM.Disp, AM.GVOpFlags);
183 else
184 MIB.addImm(AM.Disp);
185
186 return MIB.addReg(0);
187}
188
189/// addFrameReference - This function is used to add a reference to the base of
190/// an abstract object on the stack frame of the current function. This
191/// reference has base register as the FrameIndex offset until it is resolved.
192/// This allows a constant offset to be specified as well...
193///
194static inline const MachineInstrBuilder &
195addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
196 MachineInstr *MI = MIB;
197 MachineFunction &MF = *MI->getParent()->getParent();
198 MachineFrameInfo &MFI = MF.getFrameInfo();
199 const MCInstrDesc &MCID = MI->getDesc();
200 auto Flags = MachineMemOperand::MONone;
201 if (MCID.mayLoad())
203 if (MCID.mayStore())
207 MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
208 return addOffset(MIB.addFrameIndex(FI), Offset)
209 .addMemOperand(MMO);
210}
211
212/// addConstantPoolReference - This function is used to add a reference to the
213/// base of a constant value spilled to the per-function constant pool. The
214/// reference uses the abstract ConstantPoolIndex which is retained until
215/// either machine code emission or assembly output. In PIC mode on x86-32,
216/// the GlobalBaseReg parameter can be used to make this a
217/// GlobalBaseReg-relative reference.
218///
219static inline const MachineInstrBuilder &
221 Register GlobalBaseReg, unsigned char OpFlags) {
222 //FIXME: factor this
223 return MIB.addReg(GlobalBaseReg).addImm(1).addReg(0)
224 .addConstantPoolIndex(CPI, 0, OpFlags).addReg(0);
225}
226
227} // end namespace llvm
228
229#endif // LLVM_LIB_TARGET_X86_X86INSTRBUILDER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
Register Reg
This file defines the SmallVector class.
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:199
bool mayStore() const
Return true if this instruction could possibly modify memory.
Definition: MCInstrDesc.h:446
bool mayLoad() const
Return true if this instruction could possibly read memory.
Definition: MCInstrDesc.h:440
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
Definition: MachineInstr.h:72
A description of a memory reference used in the backend.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static MachineOperand CreateImm(int64_t Val)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
static MachineOperand CreateReg(Register 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, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
static const MachineInstrBuilder & addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI, Register GlobalBaseReg, unsigned char OpFlags)
addConstantPoolReference - This function is used to add a reference to the base of a constant value s...
static const MachineInstrBuilder & addRegReg(const MachineInstrBuilder &MIB, Register Reg1, bool isKill1, unsigned SubReg1, Register Reg2, bool isKill2, unsigned SubReg2)
addRegReg - This function is used to add a memory reference of the form: [Reg + Reg].
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...
static const MachineInstrBuilder & addFullAddress(const MachineInstrBuilder &MIB, const X86AddressMode &AM)
static void setDirectAddressInInstr(MachineInstr *MI, unsigned Operand, Register Reg)
Replace the address used in the instruction with the direct memory reference.
static const MachineInstrBuilder & addOffset(const MachineInstrBuilder &MIB, int Offset)
unsigned getKillRegState(bool B)
static X86AddressMode getAddressFromInstr(const MachineInstr *MI, unsigned Operand)
Compute the addressing mode from an machine instruction starting with the given operand.
static const MachineInstrBuilder & addDirectMem(const MachineInstrBuilder &MIB, Register Reg)
addDirectMem - This function is used to add a direct memory reference to the current instruction – th...
static const MachineInstrBuilder & addRegOffset(const MachineInstrBuilder &MIB, Register Reg, bool isKill, int Offset)
addRegOffset - This function is used to add a memory reference of the form [Reg + Offset],...
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
X86AddressMode - This struct holds a generalized full x86 address mode.
enum llvm::X86AddressMode::@679 BaseType
void getFullAddress(SmallVectorImpl< MachineOperand > &MO)
const GlobalValue * GV
union llvm::X86AddressMode::BaseUnion Base