LLVM 24.0.0git
MachineMemOperand.h
Go to the documentation of this file.
1//==- llvm/CodeGen/MachineMemOperand.h - MachineMemOperand class -*- 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 contains the declaration of the MachineMemOperand class, which is a
10// description of a memory reference. It is used to help track dependencies
11// in the backend.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_MACHINEMEMOPERAND_H
16#define LLVM_CODEGEN_MACHINEMEMOPERAND_H
17
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Metadata.h"
26#include "llvm/IR/Value.h" // PointerLikeTypeTraits<Value*>
30
31namespace llvm {
32
33class MDNode;
34class raw_ostream;
35class MachineFunction;
37class TargetInstrInfo;
38
39/// This class contains a discriminated union of information about pointers in
40/// memory operands, relating them back to LLVM IR or to virtual locations (such
41/// as frame indices) that are exposed during codegen.
43 /// This is the IR pointer value for the access, or it is null if unknown.
45
46 /// Offset - This is an offset from the base Value*.
47 int64_t Offset;
48
49 unsigned AddrSpace = 0;
50
52
53 explicit MachinePointerInfo(const Value *v, int64_t offset = 0,
54 uint8_t ID = 0)
55 : V(v), Offset(offset), StackID(ID) {
56 AddrSpace = v ? v->getType()->getPointerAddressSpace() : 0;
57 }
58
59 explicit MachinePointerInfo(const PseudoSourceValue *v, int64_t offset = 0,
60 uint8_t ID = 0)
61 : V(v), Offset(offset), StackID(ID) {
62 AddrSpace = v ? v->getAddressSpace() : 0;
63 }
64
65 explicit MachinePointerInfo(unsigned AddressSpace = 0, int64_t offset = 0)
66 : V((const Value *)nullptr), Offset(offset), AddrSpace(AddressSpace),
67 StackID(0) {}
68
71 int64_t offset = 0,
72 uint8_t ID = 0)
73 : V(v), Offset(offset), StackID(ID) {
74 if (V) {
75 if (const auto *ValPtr = dyn_cast_if_present<const Value *>(V))
76 AddrSpace = ValPtr->getType()->getPointerAddressSpace();
77 else
78 AddrSpace = cast<const PseudoSourceValue *>(V)->getAddressSpace();
79 }
80 }
81
90
91 /// Return true if memory region [V, V+Offset+Size) is known to be
92 /// dereferenceable.
94 const DataLayout &DL) const;
95
96 /// Return the LLVM IR address space number that this pointer points into.
97 LLVM_ABI unsigned getAddrSpace() const;
98
99 /// Return a MachinePointerInfo record that refers to the constant pool.
101
102 /// Return a MachinePointerInfo record that refers to the specified
103 /// FrameIndex.
105 int64_t Offset = 0);
106
107 /// Return a MachinePointerInfo record that refers to a jump table entry.
109
110 /// Return a MachinePointerInfo record that refers to a GOT entry.
112
113 /// Stack pointer relative access.
115 int64_t Offset, uint8_t ID = 0);
116
117 /// Stack memory without other information.
119};
120
121/// LLVM IR metadata carried by a MachineMemOperand.
124 const MDNode *Ranges = nullptr;
125 const MDNode *MemCacheHint = nullptr;
126
127 MMOMetadata() = default;
128 MMOMetadata(const AAMDNodes &AAInfo, const MDNode *Ranges = nullptr,
129 const MDNode *MemCacheHint = nullptr)
131};
132
133//===----------------------------------------------------------------------===//
134/// A description of a memory reference used in the backend.
135/// Instead of holding a StoreInst or LoadInst, this class holds the address
136/// Value of the reference along with a byte size and offset. This allows it
137/// to describe lowered loads and stores. Also, the special PseudoSourceValue
138/// objects can be used to represent loads and stores to memory locations
139/// that aren't explicit in the regular LLVM IR.
140///
142public:
143 /// Flags values. These may be or'd together.
145 // No flags set.
147 /// The memory access reads data.
148 MOLoad = 1u << 0,
149 /// The memory access writes data.
150 MOStore = 1u << 1,
151 /// The memory access is volatile.
152 MOVolatile = 1u << 2,
153 /// The memory access is non-temporal.
154 MONonTemporal = 1u << 3,
155 /// The memory access is dereferenceable (i.e., doesn't trap).
157 /// The memory access always returns the same value (or traps).
158 MOInvariant = 1u << 5,
159
160 // Reserved for use by target-specific passes.
161 // Targets may override getSerializableMachineMemOperandTargetFlags() to
162 // enable MIR serialization/parsing of these flags. If more of these flags
163 // are added, the MIR printing/parsing code will need to be updated as well.
164 MOTargetFlag1 = 1u << 6,
165 MOTargetFlag2 = 1u << 7,
166 MOTargetFlag3 = 1u << 8,
167 MOTargetFlag4 = 1u << 9,
168
169 LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ MOTargetFlag4)
170 };
171
172private:
173 /// Atomic information for this memory operation.
174 struct MachineAtomicInfo {
175 /// Synchronization scope ID for this memory operation.
176 unsigned SSID : 8; // SyncScope::ID
177 /// Atomic ordering requirements for this memory operation. For cmpxchg
178 /// atomic operations, atomic ordering requirements when store occurs.
179 unsigned Ordering : 4; // enum AtomicOrdering
180 /// For cmpxchg atomic operations, atomic ordering requirements when store
181 /// does not occur.
182 unsigned FailureOrdering : 4; // enum AtomicOrdering
183 };
184
185 MachinePointerInfo PtrInfo;
186
187 /// Track the memory type of the access. An access size which is unknown or
188 /// too large to be represented by LLT should use the invalid LLT.
189 LLT MemoryType;
190
191 Flags FlagVals;
192 Align BaseAlign;
193 MachineAtomicInfo AtomicInfo;
194 AAMDNodes AAInfo;
195 const MDNode *Ranges;
196 const MDNode *MemCacheHint;
197
198public:
199 /// Construct a MachineMemOperand object with the specified PtrInfo, flags,
200 /// size, base alignment, and metadata. For atomic operations the
201 /// synchronization scope and atomic ordering requirements must also be
202 /// specified. For cmpxchg atomic operations the atomic ordering requirements
203 /// when store does not occur must also be specified.
205 MachineMemOperand(MachinePointerInfo PtrInfo, Flags Flags, LocationSize TS,
206 Align A, const MMOMetadata &Metadata = MMOMetadata(),
208 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
209 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
211 MachineMemOperand(MachinePointerInfo PtrInfo, Flags Flags, LLT Type, Align A,
212 const MMOMetadata &Metadata = MMOMetadata(),
214 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
215 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
216
217 const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
218
219 /// Return the base address of the memory access. This may either be a normal
220 /// LLVM IR Value, or one of the special values used in CodeGen.
221 /// Special values are those obtained via
222 /// PseudoSourceValue::getFixedStack(int), PseudoSourceValue::getStack, and
223 /// other PseudoSourceValue member functions which return objects which stand
224 /// for frame/stack pointer relative references and other special references
225 /// which are not representable in the high-level IR.
226 const Value *getValue() const {
227 return dyn_cast_if_present<const Value *>(PtrInfo.V);
228 }
229
233
234 const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
235
236 /// Return the raw flags of the source value, \see Flags.
237 Flags getFlags() const { return FlagVals; }
238
239 /// Bitwise OR the current flags with the given flags.
240 void setFlags(Flags f) { FlagVals |= f; }
241
242 /// For normal values, this is a byte offset added to the base address.
243 /// For PseudoSourceValue::FPRel values, this is the FrameIndex number.
244 int64_t getOffset() const { return PtrInfo.Offset; }
245
246 unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
247
248 /// Return the memory type of the memory reference. This should only be relied
249 /// on for GlobalISel G_* operation legalization.
250 LLT getMemoryType() const { return MemoryType; }
251
252 /// Return the size in bytes of the memory reference.
254 return MemoryType.isValid()
255 ? LocationSize::precise(MemoryType.getSizeInBytes())
257 }
258
259 /// Return the size in bits of the memory reference.
261 return MemoryType.isValid()
262 ? LocationSize::precise(MemoryType.getSizeInBits())
264 }
265
266 LLT getType() const {
267 return MemoryType;
268 }
269
270 /// Return the minimum known alignment in bytes of the actual memory
271 /// reference.
272 LLVM_ABI Align getAlign() const;
273
274 /// Return the minimum known alignment in bytes of the base address, without
275 /// the offset.
276 Align getBaseAlign() const { return BaseAlign; }
277
278 /// Return the AA tags for the memory reference.
279 AAMDNodes getAAInfo() const { return AAInfo; }
280
281 /// Return the range tag for the memory reference.
282 const MDNode *getRanges() const { return Ranges; }
283
284 /// Return the cache hint metadata for the memory reference.
285 const MDNode *getMemCacheHint() const { return MemCacheHint; }
286
287 /// Returns the synchronization scope ID for this memory operation.
289 return static_cast<SyncScope::ID>(AtomicInfo.SSID);
290 }
291
292 /// Return the atomic ordering requirements for this memory operation. For
293 /// cmpxchg atomic operations, return the atomic ordering requirements when
294 /// store occurs.
296 return static_cast<AtomicOrdering>(AtomicInfo.Ordering);
297 }
298
299 /// For cmpxchg atomic operations, return the atomic ordering requirements
300 /// when store does not occur.
302 return static_cast<AtomicOrdering>(AtomicInfo.FailureOrdering);
303 }
304
305 /// Return a single atomic ordering that is at least as strong as both the
306 /// success and failure orderings for an atomic operation. (For operations
307 /// other than cmpxchg, this is equivalent to getSuccessOrdering().)
311
312 bool isLoad() const { return FlagVals & MOLoad; }
313 bool isStore() const { return FlagVals & MOStore; }
314 bool isVolatile() const { return FlagVals & MOVolatile; }
315 bool isNonTemporal() const { return FlagVals & MONonTemporal; }
316 bool isDereferenceable() const { return FlagVals & MODereferenceable; }
317 bool isInvariant() const { return FlagVals & MOInvariant; }
318
319 /// Returns true if this operation has an atomic ordering requirement of
320 /// unordered or higher, false otherwise.
321 bool isAtomic() const {
323 }
324
325 /// Returns true if this memory operation doesn't have any ordering
326 /// constraints other than normal aliasing. Volatile and (ordered) atomic
327 /// memory operations can't be reordered.
333
334 /// Update this MachineMemOperand to reflect the alignment of MMO, if it has a
335 /// greater alignment. This must only be used when the new alignment applies
336 /// to all users of this MachineMemOperand.
338
339 /// Change the SourceValue for this MachineMemOperand. This should only be
340 /// used when an object is being relocated and all references to it are being
341 /// updated.
342 void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
343 void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
344 void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
345
346 /// Reset the tracked memory type.
347 void setType(LLT NewTy) {
348 MemoryType = NewTy;
349 }
350
351 /// Unset the tracked range metadata.
352 void clearRanges() { Ranges = nullptr; }
353
354 /// Unset the cache hint metadata.
355 void clearMemCacheHint() { MemCacheHint = nullptr; }
356
357 /// Support for operator<<.
358 /// @{
361 const LLVMContext &Context, const MachineFrameInfo *MFI,
362 const TargetInstrInfo *TII) const;
363 /// @}
364
365 friend bool operator==(const MachineMemOperand &LHS,
366 const MachineMemOperand &RHS) {
367 return LHS.getValue() == RHS.getValue() &&
368 LHS.getPseudoValue() == RHS.getPseudoValue() &&
369 LHS.getSize() == RHS.getSize() &&
370 LHS.getOffset() == RHS.getOffset() &&
371 LHS.getFlags() == RHS.getFlags() &&
372 LHS.getAAInfo() == RHS.getAAInfo() &&
373 LHS.getRanges() == RHS.getRanges() &&
374 LHS.getMemCacheHint() == RHS.getMemCacheHint() &&
375 LHS.getAlign() == RHS.getAlign() &&
376 LHS.getAddrSpace() == RHS.getAddrSpace() &&
377 LHS.getSuccessOrdering() == RHS.getSuccessOrdering() &&
378 LHS.getFailureOrdering() == RHS.getFailureOrdering() &&
379 LHS.getSyncScopeID() == RHS.getSyncScopeID();
380 }
381
382 friend bool operator!=(const MachineMemOperand &LHS,
383 const MachineMemOperand &RHS) {
384 return !(LHS == RHS);
385 }
386};
387
388} // End llvm namespace
389
390#endif
aarch64 promote const
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Atomic ordering constants.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:215
dxil translate DXIL Translate Metadata
const HexagonInstrInfo * TII
Implement a low-level type suitable for MachineInstr level instruction selection.
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
This file defines the PointerUnion class, which is a discriminated union of pointer types.
Value * RHS
Value * LHS
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static LocationSize precise(uint64_t Value)
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
Metadata node.
Definition Metadata.h:1069
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
A description of a memory reference used in the backend.
void setType(LLT NewTy)
Reset the tracked memory type.
LocationSize getSize() const
Return the size in bytes of the memory reference.
AtomicOrdering getFailureOrdering() const
For cmpxchg atomic operations, return the atomic ordering requirements when store does not occur.
const PseudoSourceValue * getPseudoValue() const
LLT getMemoryType() const
Return the memory type of the memory reference.
unsigned getAddrSpace() const
void clearRanges()
Unset the tracked range metadata.
LLVM_ABI void print(raw_ostream &OS, ModuleSlotTracker &MST, SmallVectorImpl< StringRef > &SSNs, const LLVMContext &Context, const MachineFrameInfo *MFI, const TargetInstrInfo *TII) const
Support for operator<<.
bool isUnordered() const
Returns true if this memory operation doesn't have any ordering constraints other than normal aliasin...
const MDNode * getRanges() const
Return the range tag for the memory reference.
void setValue(const Value *NewSV)
Change the SourceValue for this MachineMemOperand.
bool isAtomic() const
Returns true if this operation has an atomic ordering requirement of unordered or higher,...
LLVM_ABI void refineAlignment(const MachineMemOperand *MMO)
Update this MachineMemOperand to reflect the alignment of MMO, if it has a greater alignment.
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID for this memory operation.
const void * getOpaqueValue() const
friend bool operator!=(const MachineMemOperand &LHS, const MachineMemOperand &RHS)
void clearMemCacheHint()
Unset the cache hint metadata.
AtomicOrdering getMergedOrdering() const
Return a single atomic ordering that is at least as strong as both the success and failure orderings ...
void setOffset(int64_t NewOffset)
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
AtomicOrdering getSuccessOrdering() const
Return the atomic ordering requirements for this memory operation.
void setFlags(Flags f)
Bitwise OR the current flags with the given flags.
const MachinePointerInfo & getPointerInfo() const
LLVM_ABI MachineMemOperand(MachinePointerInfo PtrInfo, Flags Flags, LocationSize TS, Align A, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
Construct a MachineMemOperand object with the specified PtrInfo, flags, size, base alignment,...
Flags getFlags() const
Return the raw flags of the source value,.
LLVM_ABI Align getAlign() const
Return the minimum known alignment in bytes of the actual memory reference.
LocationSize getSizeInBits() const
Return the size in bits of the memory reference.
friend bool operator==(const MachineMemOperand &LHS, const MachineMemOperand &RHS)
AAMDNodes getAAInfo() const
Return the AA tags for the memory reference.
void setValue(const PseudoSourceValue *NewSV)
const Value * getValue() const
Return the base address of the memory access.
Align getBaseAlign() const
Return the minimum known alignment in bytes of the base address, without the offset.
const MDNode * getMemCacheHint() const
Return the cache hint metadata for the memory reference.
int64_t getOffset() const
For normal values, this is a byte offset added to the base address.
Manage lifetime of a slot tracker for printing IR.
A discriminated union of two or more pointer types, with the discriminator in the low bits of the poi...
Special value supplied for machine level alias analysis.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
TargetInstrInfo - Interface to description of machine instruction set.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:58
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
unsigned getPointerAddressSpace(const Type *T)
Definition SPIRVUtils.h:392
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
AtomicOrdering getMergedAtomicOrdering(AtomicOrdering AO, AtomicOrdering Other)
Return a single atomic ordering that is at least as strong as both the AO and Other orderings for an ...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
AtomicOrdering
Atomic ordering for LLVM's memory model.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:763
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
const MDNode * MemCacheHint
MMOMetadata(const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, const MDNode *MemCacheHint=nullptr)
const MDNode * Ranges
MMOMetadata()=default
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getJumpTable(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a jump table entry.
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
MachinePointerInfo(const PseudoSourceValue *v, int64_t offset=0, uint8_t ID=0)
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
static LLVM_ABI MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset, uint8_t ID=0)
Stack pointer relative access.
MachinePointerInfo(unsigned AddressSpace=0, int64_t offset=0)
int64_t Offset
Offset - This is an offset from the base Value*.
MachinePointerInfo(PointerUnion< const Value *, const PseudoSourceValue * > v, int64_t offset=0, uint8_t ID=0)
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getGOT(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a GOT entry.
MachinePointerInfo(const Value *v, int64_t offset=0, uint8_t ID=0)
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.