1//===--- ModRef.h - Memory effect modeling ----------------------*- C++ -*-===//
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
7//===----------------------------------------------------------------------===//
9// Definitions of ModRefInfo and MemoryEffects, which are used to
10// describe the memory effects of instructions.
12//===----------------------------------------------------------------------===//
14#ifndef LLVM_SUPPORT_MODREF_H
15#define LLVM_SUPPORT_MODREF_H
24/// Flags indicating whether a memory access modifies or references memory.
26/// This is no access at all, a modification, a reference, or both
27/// a modification and a reference.
29 /// The access neither references nor modifies the value stored in memory.
31 /// The access may reference the value stored in memory.
33 /// The access may modify the value stored in memory.
35 /// The access may reference and may modify the value stored in memory.
56/// Debug print ModRefInfo.
59/// The locations at which a function might access memory.
61 /// Access to memory via argument pointers.
63 /// Memory that is inaccessible via LLVM IR.
70 /// Helpers to iterate all locations in the MemoryEffectsBase class.
75 template <
typename LocationEnum>
class MemoryEffectsBase {
82 static constexpr uint32_t BitsPerLoc = 2;
83 static constexpr uint32_t LocMask = (1 << BitsPerLoc) - 1;
92 Data &= ~(LocMask << getLocationPos(Loc));
93 Data |=
static_cast<uint32_t >(MR) << getLocationPos(Loc);
97 /// Returns iterator over all supported location kinds.
103 /// Create MemoryEffectsBase that can access only the given location with the
104 /// given ModRefInfo.
107 /// Create MemoryEffectsBase that can access any location with the given
114 /// Create MemoryEffectsBase that can read and write any memory.
119 /// Create MemoryEffectsBase that cannot read or write any memory.
120 static MemoryEffectsBase
none() {
124 /// Create MemoryEffectsBase that can read any memory.
129 /// Create MemoryEffectsBase that can write any memory.
134 /// Create MemoryEffectsBase that can only access argument memory.
136 return MemoryEffectsBase(Location::ArgMem, MR);
139 /// Create MemoryEffectsBase that can only access inaccessible memory.
142 return MemoryEffectsBase(Location::InaccessibleMem, MR);
145 /// Create MemoryEffectsBase that can only access errno memory.
147 return MemoryEffectsBase(Location::ErrnoMem, MR);
150 /// Create MemoryEffectsBase that can only access other memory.
152 return MemoryEffectsBase(Location::Other, MR);
155 /// Create MemoryEffectsBase that can only access inaccessible or argument
159 MemoryEffectsBase FRMB =
none();
160 FRMB.setModRef(Location::ArgMem, MR);
161 FRMB.setModRef(Location::InaccessibleMem, MR);
165 /// Create MemoryEffectsBase that can only access argument or errno memory.
169 MemoryEffectsBase FRMB =
none();
170 FRMB.setModRef(Location::ArgMem, ArgMR);
171 FRMB.setModRef(Location::ErrnoMem, ErrnoMR);
175 /// Create MemoryEffectsBase from an encoded integer value (used by memory
178 return MemoryEffectsBase(Data);
181 /// Convert MemoryEffectsBase into an encoded integer value (used by memory
187 /// Get ModRefInfo for the given Location.
192 /// Get new MemoryEffectsBase with modified ModRefInfo for Loc.
194 MemoryEffectsBase ME = *
this;
195 ME.setModRef(
Loc, MR);
199 /// Get new MemoryEffectsBase with NoModRef on the given Loc.
201 MemoryEffectsBase ME = *
this;
206 /// Get ModRefInfo for any location.
214 /// Whether this function accesses no memory.
217 /// Whether this function only (at most) reads memory.
220 /// Whether this function only (at most) writes memory.
223 /// Whether this function only (at most) accesses argument memory.
228 /// Whether this function may access argument memory.
233 /// Whether this function only (at most) accesses inaccessible memory.
238 /// Whether this function only (at most) accesses errno memory.
243 /// Whether this function only (at most) accesses argument and inaccessible
251 /// Intersect with other MemoryEffectsBase.
253 return MemoryEffectsBase(Data &
Other.Data);
256 /// Intersect (in-place) with other MemoryEffectsBase.
262 /// Union with other MemoryEffectsBase.
264 return MemoryEffectsBase(Data |
Other.Data);
267 /// Union (in-place) with other MemoryEffectsBase.
273 /// Subtract other MemoryEffectsBase.
275 return MemoryEffectsBase(Data & ~
Other.Data);
278 /// Subtract (in-place) with other MemoryEffectsBase.
284 /// Check whether this is the same as other MemoryEffectsBase.
287 /// Check whether this is different from other MemoryEffectsBase.
291/// Summary of how a function affects memory in the program.
293/// Loads from constant globals are not considered memory accesses for this
294/// interface. Also, functions may freely modify stack space local to their
295/// invocation without having to report it through these interfaces.
298/// Debug print MemoryEffects.
304/// Components of the pointer that may be captured.
350/// Represents which components of the pointer may be captured in which
351/// location. This represents the captures(...) attribute in IR.
353/// For more information on the precise semantics see LangRef.
361 : OtherComponents(OtherComponents), RetComponents(RetComponents) {}
364 : OtherComponents(Components), RetComponents(Components) {}
366 /// Create CaptureInfo that does not capture any components of the pointer
369 /// Create CaptureInfo that may capture all components of the pointer.
372 /// Create CaptureInfo that may only capture via the return value.
378 /// Whether the pointer is only captured via the return value.
381 /// Get components potentially captured by the return value.
384 /// Get components potentially captured through locations other than the
388 /// Get the potentially captured components of the pointer (regardless of
393 return OtherComponents ==
Other.OtherComponents &&
394 RetComponents ==
Other.RetComponents;
399 /// Compute union of CaptureInfos.
402 RetComponents |
Other.RetComponents);
405 /// Compute intersection of CaptureInfos.
408 RetComponents &
Other.RetComponents);
411 /// Compute union of CaptureInfos in-place.
413 OtherComponents |=
Other.OtherComponents;
414 RetComponents |=
Other.RetComponents;
418 /// Compute intersection of CaptureInfos in-place.
420 OtherComponents &=
Other.OtherComponents;
421 RetComponents &=
Other.RetComponents;
430 /// Convert CaptureInfo into an encoded integer value (used by captures
unsigned const MachineRegisterInfo * MRI
Analysis containing CSE Info
Provides some synthesis utilities to produce sequences of values.
Represents which components of the pointer may be captured in which location.
static CaptureInfo createFromIntValue(uint32_t Data)
CaptureComponents getOtherComponents() const
Get components potentially captured through locations other than the return value.
static CaptureInfo none()
Create CaptureInfo that does not capture any components of the pointer.
bool operator==(CaptureInfo Other) const
bool operator!=(CaptureInfo Other) const
static CaptureInfo retOnly(CaptureComponents RetComponents=CaptureComponents::All)
Create CaptureInfo that may only capture via the return value.
static CaptureInfo all()
Create CaptureInfo that may capture all components of the pointer.
CaptureInfo operator&(CaptureInfo Other) const
Compute intersection of CaptureInfos.
CaptureInfo & operator|=(CaptureInfo Other)
Compute union of CaptureInfos in-place.
CaptureComponents getRetComponents() const
Get components potentially captured by the return value.
CaptureInfo(CaptureComponents OtherComponents, CaptureComponents RetComponents)
CaptureInfo & operator&=(CaptureInfo Other)
Compute intersection of CaptureInfos in-place.
CaptureInfo operator|(CaptureInfo Other) const
Compute union of CaptureInfos.
bool isRetOnly() const
Whether the pointer is only captured via the return value.
uint32_t toIntValue() const
Convert CaptureInfo into an encoded integer value (used by captures attribute).
CaptureInfo(CaptureComponents Components)
MemoryEffectsBase operator&(MemoryEffectsBase Other) const
Intersect with other MemoryEffectsBase.
static MemoryEffectsBase readOnly()
Create MemoryEffectsBase that can read any memory.
bool onlyWritesMemory() const
Whether this function only (at most) writes memory.
MemoryEffectsBase getWithoutLoc(Location Loc) const
Get new MemoryEffectsBase with NoModRef on the given Loc.
MemoryEffectsBase & operator|=(MemoryEffectsBase Other)
Union (in-place) with other MemoryEffectsBase.
MemoryEffectsBase getWithModRef(Location Loc, ModRefInfo MR) const
Get new MemoryEffectsBase with modified ModRefInfo for Loc.
bool operator!=(MemoryEffectsBase Other) const
Check whether this is different from other MemoryEffectsBase.
MemoryEffectsBase operator-(MemoryEffectsBase Other) const
Subtract other MemoryEffectsBase.
bool doesNotAccessMemory() const
Whether this function accesses no memory.
static MemoryEffectsBase argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access argument memory.
MemoryEffectsBase(ModRefInfo MR)
Create MemoryEffectsBase that can access any location with the given ModRefInfo.
bool doesAccessArgPointees() const
Whether this function may access argument memory.
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible memory.
bool onlyAccessesInaccessibleMem() const
Whether this function only (at most) accesses inaccessible memory.
MemoryEffectsBase(Location Loc, ModRefInfo MR)
Create MemoryEffectsBase that can access only the given location with the given ModRefInfo.
ModRefInfo getModRef(Location Loc) const
Get ModRefInfo for the given Location.
MemoryEffectsBase & operator&=(MemoryEffectsBase Other)
Intersect (in-place) with other MemoryEffectsBase.
ModRefInfo getModRef() const
Get ModRefInfo for any location.
bool onlyAccessesArgPointees() const
Whether this function only (at most) accesses argument memory.
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
static MemoryEffectsBase errnoMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access errno memory.
static MemoryEffectsBase createFromIntValue(uint32_t Data)
Create MemoryEffectsBase from an encoded integer value (used by memory attribute).
MemoryEffectsBase & operator-=(MemoryEffectsBase Other)
Subtract (in-place) with other MemoryEffectsBase.
static MemoryEffectsBase writeOnly()
Create MemoryEffectsBase that can write any memory.
MemoryEffectsBase operator|(MemoryEffectsBase Other) const
Union with other MemoryEffectsBase.
static MemoryEffectsBase otherMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access other memory.
static auto locations()
Returns iterator over all supported location kinds.
bool onlyAccessesErrnoMem() const
Whether this function only (at most) accesses errno memory.
uint32_t toIntValue() const
Convert MemoryEffectsBase into an encoded integer value (used by memory attribute).
static MemoryEffectsBase inaccessibleOrArgMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible or argument memory.
static MemoryEffectsBase argumentOrErrnoMemOnly(ModRefInfo ArgMR=ModRefInfo::ModRef, ModRefInfo ErrnoMR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access argument or errno memory.
static MemoryEffectsBase none()
Create MemoryEffectsBase that cannot read or write any memory.
bool operator==(MemoryEffectsBase Other) const
Check whether this is the same as other MemoryEffectsBase.
bool onlyAccessesInaccessibleOrArgMem() const
Whether this function only (at most) accesses argument and inaccessible memory.
static MemoryEffectsBase unknown()
Create MemoryEffectsBase that can read and write any memory.
This class implements an extremely fast bulk output stream that can only output to a stream.
This is an optimization pass for GlobalISel generic memory operations.
bool capturesReadProvenanceOnly(CaptureComponents CC)
bool capturesAddressIsNullOnly(CaptureComponents CC)
auto enum_seq_inclusive(EnumT Begin, EnumT End)
Iterate over an enum type from Begin to End inclusive.
bool capturesAddress(CaptureComponents CC)
constexpr force_iteration_on_noniterable_enum_t force_iteration_on_noniterable_enum
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
MemoryEffects FunctionModRefBehavior
bool capturesFullProvenance(CaptureComponents CC)
bool isModSet(const ModRefInfo MRI)
bool isModOrRefSet(const ModRefInfo MRI)
CaptureComponents
Components of the pointer that may be captured.
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
@ Ref
The access may reference the value stored in memory.
@ ModRef
The access may reference and may modify the value stored in memory.
@ Mod
The access may modify the value stored in memory.
@ LLVM_MARK_AS_BITMASK_ENUM
@ NoModRef
The access neither references nor modifies the value stored in memory.
IRMemLocation
The locations at which a function might access memory.
@ ArgMem
Access to memory via argument pointers.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ InaccessibleMem
Memory that is inaccessible via LLVM IR.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
bool isModAndRefSet(const ModRefInfo MRI)
bool capturesAnything(CaptureComponents CC)
bool capturesAll(CaptureComponents CC)
bool capturesNothing(CaptureComponents CC)
bool isNoModRef(const ModRefInfo MRI)
bool capturesAnyProvenance(CaptureComponents CC)
bool isRefSet(const ModRefInfo MRI)