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 MOTargetFlag5 = 1u << 10,
169 MOTargetFlag6 = 1u << 11,
170
171 LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ MOTargetFlag6)
172 };
173
174private:
175 /// Atomic information for this memory operation.
176 struct MachineAtomicInfo {
177 /// Synchronization scope ID for this memory operation.
178 unsigned SSID : 8; // SyncScope::ID
179 /// Atomic ordering requirements for this memory operation. For cmpxchg
180 /// atomic operations, atomic ordering requirements when store occurs.
181 unsigned Ordering : 4; // enum AtomicOrdering
182 /// For cmpxchg atomic operations, atomic ordering requirements when store
183 /// does not occur.
184 unsigned FailureOrdering : 4; // enum AtomicOrdering
185 };
186
187 MachinePointerInfo PtrInfo;
188
189 /// Track the memory type of the access. An access size which is unknown or
190 /// too large to be represented by LLT should use the invalid LLT.
191 LLT MemoryType;
192
193 Flags FlagVals;
194 Align BaseAlign;
195 MachineAtomicInfo AtomicInfo;
196 AAMDNodes AAInfo;
197 const MDNode *Ranges;
198 const MDNode *MemCacheHint;
199
200public:
201 /// Construct a MachineMemOperand object with the specified PtrInfo, flags,
202 /// size, base alignment, and metadata. For atomic operations the
203 /// synchronization scope and atomic ordering requirements must also be
204 /// specified. For cmpxchg atomic operations the atomic ordering requirements
205 /// when store does not occur must also be specified.
207 MachineMemOperand(MachinePointerInfo PtrInfo, Flags Flags, LocationSize TS,
208 Align A, const MMOMetadata &Metadata = MMOMetadata(),
210 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
211 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
213 MachineMemOperand(MachinePointerInfo PtrInfo, Flags Flags, LLT Type, Align A,
214 const MMOMetadata &Metadata = MMOMetadata(),
216 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
217 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
218
219 const MachinePointerInfo &getPointerInfo() const { return PtrInfo; }
220
221 /// Return the base address of the memory access. This may either be a normal
222 /// LLVM IR Value, or one of the special values used in CodeGen.
223 /// Special values are those obtained via
224 /// PseudoSourceValue::getFixedStack(int), PseudoSourceValue::getStack, and
225 /// other PseudoSourceValue member functions which return objects which stand
226 /// for frame/stack pointer relative references and other special references
227 /// which are not representable in the high-level IR.
228 const Value *getValue() const {
229 return dyn_cast_if_present<const Value *>(PtrInfo.V);
230 }
231
235
236 const void *getOpaqueValue() const { return PtrInfo.V.getOpaqueValue(); }
237
238 /// Return the raw flags of the source value, \see Flags.
239 Flags getFlags() const { return FlagVals; }
240
241 /// Bitwise OR the current flags with the given flags.
242 void setFlags(Flags f) { FlagVals |= f; }
243
244 /// For normal values, this is a byte offset added to the base address.
245 /// For PseudoSourceValue::FPRel values, this is the FrameIndex number.
246 int64_t getOffset() const { return PtrInfo.Offset; }
247
248 unsigned getAddrSpace() const { return PtrInfo.getAddrSpace(); }
249
250 /// Return the memory type of the memory reference. This should only be relied
251 /// on for GlobalISel G_* operation legalization.
252 LLT getMemoryType() const { return MemoryType; }
253
254 /// Return the size in bytes of the memory reference.
256 return MemoryType.isValid()
257 ? LocationSize::precise(MemoryType.getSizeInBytes())
259 }
260
261 /// Return the size in bits of the memory reference.
263 return MemoryType.isValid()
264 ? LocationSize::precise(MemoryType.getSizeInBits())
266 }
267
268 LLT getType() const {
269 return MemoryType;
270 }
271
272 /// Return the minimum known alignment in bytes of the actual memory
273 /// reference.
274 LLVM_ABI Align getAlign() const;
275
276 /// Return the minimum known alignment in bytes of the base address, without
277 /// the offset.
278 Align getBaseAlign() const { return BaseAlign; }
279
280 /// Return the AA tags for the memory reference.
281 AAMDNodes getAAInfo() const { return AAInfo; }
282
283 /// Return the range tag for the memory reference.
284 const MDNode *getRanges() const { return Ranges; }
285
286 /// Return the cache hint metadata for the memory reference.
287 const MDNode *getMemCacheHint() const { return MemCacheHint; }
288
289 /// Returns the synchronization scope ID for this memory operation.
291 return static_cast<SyncScope::ID>(AtomicInfo.SSID);
292 }
293
294 /// Return the atomic ordering requirements for this memory operation. For
295 /// cmpxchg atomic operations, return the atomic ordering requirements when
296 /// store occurs.
298 return static_cast<AtomicOrdering>(AtomicInfo.Ordering);
299 }
300
301 /// For cmpxchg atomic operations, return the atomic ordering requirements
302 /// when store does not occur.
304 return static_cast<AtomicOrdering>(AtomicInfo.FailureOrdering);
305 }
306
307 /// Return a single atomic ordering that is at least as strong as both the
308 /// success and failure orderings for an atomic operation. (For operations
309 /// other than cmpxchg, this is equivalent to getSuccessOrdering().)
313
314 bool isLoad() const { return FlagVals & MOLoad; }
315 bool isStore() const { return FlagVals & MOStore; }
316 bool isVolatile() const { return FlagVals & MOVolatile; }
317 bool isNonTemporal() const { return FlagVals & MONonTemporal; }
318 bool isDereferenceable() const { return FlagVals & MODereferenceable; }
319 bool isInvariant() const { return FlagVals & MOInvariant; }
320
321 /// Returns true if this operation has an atomic ordering requirement of
322 /// unordered or higher, false otherwise.
323 bool isAtomic() const {
325 }
326
327 /// Returns true if this memory operation doesn't have any ordering
328 /// constraints other than normal aliasing. Volatile and (ordered) atomic
329 /// memory operations can't be reordered.
335
336 /// Update this MachineMemOperand to reflect the alignment of MMO, if it has a
337 /// greater alignment. This must only be used when the new alignment applies
338 /// to all users of this MachineMemOperand.
340
341 /// Change the SourceValue for this MachineMemOperand. This should only be
342 /// used when an object is being relocated and all references to it are being
343 /// updated.
344 void setValue(const Value *NewSV) { PtrInfo.V = NewSV; }
345 void setValue(const PseudoSourceValue *NewSV) { PtrInfo.V = NewSV; }
346 void setOffset(int64_t NewOffset) { PtrInfo.Offset = NewOffset; }
347
348 /// Reset the tracked memory type.
349 void setType(LLT NewTy) {
350 MemoryType = NewTy;
351 }
352
353 /// Unset the tracked range metadata.
354 void clearRanges() { Ranges = nullptr; }
355
356 /// Unset the cache hint metadata.
357 void clearMemCacheHint() { MemCacheHint = nullptr; }
358
359 /// Support for operator<<.
360 /// @{
363 const LLVMContext &Context, const MachineFrameInfo *MFI,
364 const TargetInstrInfo *TII) const;
365 /// @}
366
367 friend bool operator==(const MachineMemOperand &LHS,
368 const MachineMemOperand &RHS) {
369 return LHS.getValue() == RHS.getValue() &&
370 LHS.getPseudoValue() == RHS.getPseudoValue() &&
371 LHS.getSize() == RHS.getSize() &&
372 LHS.getOffset() == RHS.getOffset() &&
373 LHS.getFlags() == RHS.getFlags() &&
374 LHS.getAAInfo() == RHS.getAAInfo() &&
375 LHS.getRanges() == RHS.getRanges() &&
376 LHS.getMemCacheHint() == RHS.getMemCacheHint() &&
377 LHS.getAlign() == RHS.getAlign() &&
378 LHS.getAddrSpace() == RHS.getAddrSpace() &&
379 LHS.getSuccessOrdering() == RHS.getSuccessOrdering() &&
380 LHS.getFailureOrdering() == RHS.getFailureOrdering() &&
381 LHS.getSyncScopeID() == RHS.getSyncScopeID();
382 }
383
384 friend bool operator!=(const MachineMemOperand &LHS,
385 const MachineMemOperand &RHS) {
386 return !(LHS == RHS);
387 }
388};
389
390} // End llvm namespace
391
392#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:211
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:76
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:1081
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:66
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
unsigned getPointerAddressSpace(const Type *T)
Definition SPIRVUtils.h:393
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:774
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.