LLVM 23.0.0git
ModRef.h
Go to the documentation of this file.
1//===--- ModRef.h - Memory effect modeling ----------------------*- 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// Definitions of ModRefInfo and MemoryEffects, which are used to
10// describe the memory effects of instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_MODREF_H
15#define LLVM_SUPPORT_MODREF_H
16
18#include "llvm/ADT/Sequence.h"
21
22namespace llvm {
23
24/// Flags indicating whether a memory access modifies or references memory.
25///
26/// This is no access at all, a modification, a reference, or both
27/// a modification and a reference.
28enum class ModRefInfo : uint8_t {
29 /// The access neither references nor modifies the value stored in memory.
31 /// The access may reference the value stored in memory.
32 Ref = 1,
33 /// The access may modify the value stored in memory.
34 Mod = 2,
35 /// The access may reference and may modify the value stored in memory.
38};
39
40[[nodiscard]] inline bool isNoModRef(const ModRefInfo MRI) {
41 return MRI == ModRefInfo::NoModRef;
42}
43[[nodiscard]] inline bool isModOrRefSet(const ModRefInfo MRI) {
44 return MRI != ModRefInfo::NoModRef;
45}
46[[nodiscard]] inline bool isModAndRefSet(const ModRefInfo MRI) {
47 return MRI == ModRefInfo::ModRef;
48}
49[[nodiscard]] inline bool isModSet(const ModRefInfo MRI) {
50 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Mod);
51}
52[[nodiscard]] inline bool isRefSet(const ModRefInfo MRI) {
53 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Ref);
54}
55
56/// Debug print ModRefInfo.
57LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, ModRefInfo MR);
58
59/// The locations at which a function might access memory.
60enum class IRMemLocation {
61 /// Access to memory via argument pointers.
62 ArgMem = 0,
63 /// Memory that is inaccessible via LLVM IR.
65 /// Errno memory.
67 /// Any other memory.
68 Other = 3,
69 /// Represents target specific state.
72
73 /// Helpers to iterate all locations in the MemoryEffectsBase class.
76};
77
78template <typename LocationEnum> class MemoryEffectsBase {
79public:
80 using Location = LocationEnum;
81
82private:
83 uint32_t Data = 0;
84
85 static constexpr uint32_t BitsPerLoc = 2;
86 static constexpr uint32_t LocMask = (1 << BitsPerLoc) - 1;
87
88 static uint32_t getLocationPos(Location Loc) {
89 return (uint32_t)Loc * BitsPerLoc;
90 }
91
93
94 void setModRef(Location Loc, ModRefInfo MR) {
95 Data &= ~(LocMask << getLocationPos(Loc));
96 Data |= static_cast<uint32_t>(MR) << getLocationPos(Loc);
97 }
98
99public:
100 /// Returns iterator over all supported location kinds.
101 static auto locations() {
102 return enum_seq_inclusive(Location::First, Location::Last,
104 }
105
106 static auto targetMemLocations() {
107 return enum_seq_inclusive(Location::TargetMem0, Location::TargetMem1,
109 }
110
111 /// Create MemoryEffectsBase that can access only the given location with the
112 /// given ModRefInfo.
113 MemoryEffectsBase(Location Loc, ModRefInfo MR) { setModRef(Loc, MR); }
114
115 /// Create MemoryEffectsBase that can access any location with the given
116 /// ModRefInfo.
118 for (Location Loc : locations())
119 setModRef(Loc, MR);
120 }
121
122 /// Create MemoryEffectsBase that can read and write any memory.
123 static MemoryEffectsBase unknown() {
124 return MemoryEffectsBase(ModRefInfo::ModRef);
125 }
126
127 /// Create MemoryEffectsBase that cannot read or write any memory.
128 static MemoryEffectsBase none() {
129 return MemoryEffectsBase(ModRefInfo::NoModRef);
130 }
131
132 /// Create MemoryEffectsBase that can read any memory.
133 static MemoryEffectsBase readOnly() {
134 return MemoryEffectsBase(ModRefInfo::Ref);
135 }
136
137 /// Create MemoryEffectsBase that can write any memory.
138 static MemoryEffectsBase writeOnly() {
139 return MemoryEffectsBase(ModRefInfo::Mod);
140 }
141
142 /// Create MemoryEffectsBase that can only access argument memory.
143 static MemoryEffectsBase argMemOnly(ModRefInfo MR = ModRefInfo::ModRef) {
144 return MemoryEffectsBase(Location::ArgMem, MR);
145 }
146
147 /// Create MemoryEffectsBase that can only access inaccessible memory.
148 static MemoryEffectsBase
150 return MemoryEffectsBase(Location::InaccessibleMem, MR);
151 }
152
153 /// Create MemoryEffectsBase that can only access errno memory.
154 static MemoryEffectsBase errnoMemOnly(ModRefInfo MR = ModRefInfo::ModRef) {
155 return MemoryEffectsBase(Location::ErrnoMem, MR);
156 }
157
158 /// Create MemoryEffectsBase that can only access other memory.
159 static MemoryEffectsBase otherMemOnly(ModRefInfo MR = ModRefInfo::ModRef) {
160 return MemoryEffectsBase(Location::Other, MR);
161 }
162
163 /// Create MemoryEffectsBase that can only access inaccessible or argument
164 /// memory.
165 static MemoryEffectsBase
167 MemoryEffectsBase FRMB = none();
168 FRMB.setModRef(Location::ArgMem, MR);
169 FRMB.setModRef(Location::InaccessibleMem, MR);
170 return FRMB;
171 }
172
173 /// Create MemoryEffectsBase that can only access argument or errno memory.
174 static MemoryEffectsBase
176 ModRefInfo ErrnoMR = ModRefInfo::ModRef) {
177 MemoryEffectsBase FRMB = none();
178 FRMB.setModRef(Location::ArgMem, ArgMR);
179 FRMB.setModRef(Location::ErrnoMem, ErrnoMR);
180 return FRMB;
181 }
182
183 /// Create MemoryEffectsBase from an encoded integer value (used by memory
184 /// attribute).
185 static MemoryEffectsBase createFromIntValue(uint32_t Data) {
186 return MemoryEffectsBase(Data);
187 }
188
189 /// Convert MemoryEffectsBase into an encoded integer value (used by memory
190 /// attribute).
192 return Data;
193 }
194
195 /// Get ModRefInfo for the given Location.
197 return ModRefInfo((Data >> getLocationPos(Loc)) & LocMask);
198 }
199
200 /// Get new MemoryEffectsBase with modified ModRefInfo for Loc.
201 MemoryEffectsBase getWithModRef(Location Loc, ModRefInfo MR) const {
202 MemoryEffectsBase ME = *this;
203 ME.setModRef(Loc, MR);
204 return ME;
205 }
206
207 /// Get new MemoryEffectsBase with NoModRef on the given Loc.
208 MemoryEffectsBase getWithoutLoc(Location Loc) const {
209 MemoryEffectsBase ME = *this;
210 ME.setModRef(Loc, ModRefInfo::NoModRef);
211 return ME;
212 }
213
214 /// Get ModRefInfo for any location.
217 for (Location Loc : locations())
218 MR |= getModRef(Loc);
219 return MR;
220 }
221
222 /// Whether this function accesses no memory.
223 bool doesNotAccessMemory() const { return Data == 0; }
224
225 /// Whether this function only (at most) reads memory.
226 bool onlyReadsMemory() const { return !isModSet(getModRef()); }
227
228 /// Whether this function only (at most) writes memory.
229 bool onlyWritesMemory() const { return !isRefSet(getModRef()); }
230
231 /// Whether this function only (at most) accesses argument memory.
233 return getWithoutLoc(Location::ArgMem).doesNotAccessMemory();
234 }
235
236 /// Whether this function may access argument memory.
238 return isModOrRefSet(getModRef(Location::ArgMem));
239 }
240
241 /// Whether this function only (at most) accesses inaccessible memory.
243 return getWithoutLoc(Location::InaccessibleMem).doesNotAccessMemory();
244 }
245
246 /// Whether this function only (at most) accesses inaccessible or target
247 /// memory.
249 MemoryEffectsBase ME = *this;
251 ME &= ME.getWithoutLoc(Loc);
252 return ME.getWithoutLoc(Location::InaccessibleMem).doesNotAccessMemory();
253 }
254
255 /// Whether this function only (at most) accesses errno memory.
256 bool onlyAccessesErrnoMem() const {
257 return getWithoutLoc(Location::ErrnoMem).doesNotAccessMemory();
258 }
259
260 /// Whether this function only (at most) accesses argument and inaccessible
261 /// memory.
263 return getWithoutLoc(Location::InaccessibleMem)
264 .getWithoutLoc(Location::ArgMem)
266 }
267
268 /// Intersect with other MemoryEffectsBase.
269 MemoryEffectsBase operator&(MemoryEffectsBase Other) const {
270 return MemoryEffectsBase(Data & Other.Data);
271 }
272
273 /// Intersect (in-place) with other MemoryEffectsBase.
274 MemoryEffectsBase &operator&=(MemoryEffectsBase Other) {
275 Data &= Other.Data;
276 return *this;
277 }
278
279 /// Union with other MemoryEffectsBase.
280 MemoryEffectsBase operator|(MemoryEffectsBase Other) const {
281 return MemoryEffectsBase(Data | Other.Data);
282 }
283
284 /// Union (in-place) with other MemoryEffectsBase.
285 MemoryEffectsBase &operator|=(MemoryEffectsBase Other) {
286 Data |= Other.Data;
287 return *this;
288 }
289
290 /// Subtract other MemoryEffectsBase.
291 MemoryEffectsBase operator-(MemoryEffectsBase Other) const {
292 return MemoryEffectsBase(Data & ~Other.Data);
293 }
294
295 /// Subtract (in-place) with other MemoryEffectsBase.
296 MemoryEffectsBase &operator-=(MemoryEffectsBase Other) {
297 Data &= ~Other.Data;
298 return *this;
299 }
300
301 /// Check whether this is the same as other MemoryEffectsBase.
302 bool operator==(MemoryEffectsBase Other) const { return Data == Other.Data; }
303
304 /// Check whether this is different from other MemoryEffectsBase.
305 bool operator!=(MemoryEffectsBase Other) const { return !operator==(Other); }
306};
307
308/// Summary of how a function affects memory in the program.
309///
310/// Loads from constant globals are not considered memory accesses for this
311/// interface. Also, functions may freely modify stack space local to their
312/// invocation without having to report it through these interfaces.
314
315/// Debug print MemoryEffects.
317
318// Legacy alias.
320
321/// Components of the pointer that may be captured.
331
333 return CC == CaptureComponents::None;
334}
335
337 return CC != CaptureComponents::None;
338}
339
343
347
352
356
360
362 return CC == CaptureComponents::All;
363}
364
365LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, CaptureComponents CC);
366
367/// Represents which components of the pointer may be captured in which
368/// location. This represents the captures(...) attribute in IR.
369///
370/// For more information on the precise semantics see LangRef.
372 CaptureComponents OtherComponents;
373 CaptureComponents RetComponents;
374
375public:
377 CaptureComponents RetComponents)
378 : OtherComponents(OtherComponents), RetComponents(RetComponents) {}
379
381 : OtherComponents(Components), RetComponents(Components) {}
382
383 /// Create CaptureInfo that does not capture any components of the pointer
385
386 /// Create CaptureInfo that may capture all components of the pointer.
388
389 /// Create CaptureInfo that may only capture via the return value.
390 static CaptureInfo
392 return CaptureInfo(CaptureComponents::None, RetComponents);
393 }
394
395 /// Whether the pointer is only captured via the return value.
396 bool isRetOnly() const { return capturesNothing(OtherComponents); }
397
398 /// Get components potentially captured by the return value.
399 CaptureComponents getRetComponents() const { return RetComponents; }
400
401 /// Get components potentially captured through locations other than the
402 /// return value.
403 CaptureComponents getOtherComponents() const { return OtherComponents; }
404
405 /// Get the potentially captured components of the pointer (regardless of
406 /// location).
407 operator CaptureComponents() const { return OtherComponents | RetComponents; }
408
410 return OtherComponents == Other.OtherComponents &&
411 RetComponents == Other.RetComponents;
412 }
413
414 bool operator!=(CaptureInfo Other) const { return !(*this == Other); }
415
416 /// Compute union of CaptureInfos.
418 return CaptureInfo(OtherComponents | Other.OtherComponents,
419 RetComponents | Other.RetComponents);
420 }
421
422 /// Compute intersection of CaptureInfos.
424 return CaptureInfo(OtherComponents & Other.OtherComponents,
425 RetComponents & Other.RetComponents);
426 }
427
428 /// Compute union of CaptureInfos in-place.
430 OtherComponents |= Other.OtherComponents;
431 RetComponents |= Other.RetComponents;
432 return *this;
433 }
434
435 /// Compute intersection of CaptureInfos in-place.
437 OtherComponents &= Other.OtherComponents;
438 RetComponents &= Other.RetComponents;
439 return *this;
440 }
441
446
447 /// Convert CaptureInfo into an encoded integer value (used by captures
448 /// attribute).
450 return (uint32_t(OtherComponents) << 4) | uint32_t(RetComponents);
451 }
452};
453
454LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, CaptureInfo Info);
455
456} // namespace llvm
457
458#endif
unsigned const MachineRegisterInfo * MRI
#define LLVM_ABI
Definition Compiler.h:213
Provides some synthesis utilities to produce sequences of values.
Represents which components of the pointer may be captured in which location.
Definition ModRef.h:371
static CaptureInfo createFromIntValue(uint32_t Data)
Definition ModRef.h:442
CaptureComponents getOtherComponents() const
Get components potentially captured through locations other than the return value.
Definition ModRef.h:403
static CaptureInfo none()
Create CaptureInfo that does not capture any components of the pointer.
Definition ModRef.h:384
bool operator==(CaptureInfo Other) const
Definition ModRef.h:409
bool operator!=(CaptureInfo Other) const
Definition ModRef.h:414
static CaptureInfo retOnly(CaptureComponents RetComponents=CaptureComponents::All)
Create CaptureInfo that may only capture via the return value.
Definition ModRef.h:391
static CaptureInfo all()
Create CaptureInfo that may capture all components of the pointer.
Definition ModRef.h:387
CaptureInfo operator&(CaptureInfo Other) const
Compute intersection of CaptureInfos.
Definition ModRef.h:423
CaptureInfo & operator|=(CaptureInfo Other)
Compute union of CaptureInfos in-place.
Definition ModRef.h:429
CaptureComponents getRetComponents() const
Get components potentially captured by the return value.
Definition ModRef.h:399
CaptureInfo(CaptureComponents OtherComponents, CaptureComponents RetComponents)
Definition ModRef.h:376
CaptureInfo & operator&=(CaptureInfo Other)
Compute intersection of CaptureInfos in-place.
Definition ModRef.h:436
CaptureInfo operator|(CaptureInfo Other) const
Compute union of CaptureInfos.
Definition ModRef.h:417
bool isRetOnly() const
Whether the pointer is only captured via the return value.
Definition ModRef.h:396
uint32_t toIntValue() const
Convert CaptureInfo into an encoded integer value (used by captures attribute).
Definition ModRef.h:449
CaptureInfo(CaptureComponents Components)
Definition ModRef.h:380
MemoryEffectsBase operator&(MemoryEffectsBase Other) const
Intersect with other MemoryEffectsBase.
Definition ModRef.h:269
static MemoryEffectsBase readOnly()
Create MemoryEffectsBase that can read any memory.
Definition ModRef.h:133
bool onlyWritesMemory() const
Whether this function only (at most) writes memory.
Definition ModRef.h:229
MemoryEffectsBase getWithoutLoc(Location Loc) const
Get new MemoryEffectsBase with NoModRef on the given Loc.
Definition ModRef.h:208
MemoryEffectsBase & operator|=(MemoryEffectsBase Other)
Union (in-place) with other MemoryEffectsBase.
Definition ModRef.h:285
MemoryEffectsBase getWithModRef(Location Loc, ModRefInfo MR) const
Get new MemoryEffectsBase with modified ModRefInfo for Loc.
Definition ModRef.h:201
static auto targetMemLocations()
Definition ModRef.h:106
bool operator!=(MemoryEffectsBase Other) const
Check whether this is different from other MemoryEffectsBase.
Definition ModRef.h:305
MemoryEffectsBase operator-(MemoryEffectsBase Other) const
Subtract other MemoryEffectsBase.
Definition ModRef.h:291
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition ModRef.h:223
static MemoryEffectsBase argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access argument memory.
Definition ModRef.h:143
MemoryEffectsBase(ModRefInfo MR)
Create MemoryEffectsBase that can access any location with the given ModRefInfo.
Definition ModRef.h:117
bool doesAccessArgPointees() const
Whether this function may access argument memory.
Definition ModRef.h:237
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible memory.
Definition ModRef.h:149
bool onlyAccessesInaccessibleMem() const
Whether this function only (at most) accesses inaccessible memory.
Definition ModRef.h:242
MemoryEffectsBase(Location Loc, ModRefInfo MR)
Create MemoryEffectsBase that can access only the given location with the given ModRefInfo.
Definition ModRef.h:113
ModRefInfo getModRef(Location Loc) const
Get ModRefInfo for the given Location.
Definition ModRef.h:196
MemoryEffectsBase & operator&=(MemoryEffectsBase Other)
Intersect (in-place) with other MemoryEffectsBase.
Definition ModRef.h:274
ModRefInfo getModRef() const
Get ModRefInfo for any location.
Definition ModRef.h:215
LocationEnum Location
Definition ModRef.h:80
bool onlyAccessesArgPointees() const
Whether this function only (at most) accesses argument memory.
Definition ModRef.h:232
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition ModRef.h:226
static MemoryEffectsBase errnoMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access errno memory.
Definition ModRef.h:154
static MemoryEffectsBase createFromIntValue(uint32_t Data)
Create MemoryEffectsBase from an encoded integer value (used by memory attribute).
Definition ModRef.h:185
MemoryEffectsBase & operator-=(MemoryEffectsBase Other)
Subtract (in-place) with other MemoryEffectsBase.
Definition ModRef.h:296
static MemoryEffectsBase writeOnly()
Create MemoryEffectsBase that can write any memory.
Definition ModRef.h:138
MemoryEffectsBase operator|(MemoryEffectsBase Other) const
Union with other MemoryEffectsBase.
Definition ModRef.h:280
static MemoryEffectsBase otherMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access other memory.
Definition ModRef.h:159
static auto locations()
Returns iterator over all supported location kinds.
Definition ModRef.h:101
bool onlyAccessesErrnoMem() const
Whether this function only (at most) accesses errno memory.
Definition ModRef.h:256
uint32_t toIntValue() const
Convert MemoryEffectsBase into an encoded integer value (used by memory attribute).
Definition ModRef.h:191
static MemoryEffectsBase inaccessibleOrArgMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible or argument memory.
Definition ModRef.h:166
static MemoryEffectsBase argumentOrErrnoMemOnly(ModRefInfo ArgMR=ModRefInfo::ModRef, ModRefInfo ErrnoMR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access argument or errno memory.
Definition ModRef.h:175
bool onlyAccessesInaccessibleOrTargetMem() const
Whether this function only (at most) accesses inaccessible or target memory.
Definition ModRef.h:248
static MemoryEffectsBase none()
Create MemoryEffectsBase that cannot read or write any memory.
Definition ModRef.h:128
bool operator==(MemoryEffectsBase Other) const
Check whether this is the same as other MemoryEffectsBase.
Definition ModRef.h:302
bool onlyAccessesInaccessibleOrArgMem() const
Whether this function only (at most) accesses argument and inaccessible memory.
Definition ModRef.h:262
static MemoryEffectsBase unknown()
Create MemoryEffectsBase that can read and write any memory.
Definition ModRef.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
bool capturesReadProvenanceOnly(CaptureComponents CC)
Definition ModRef.h:348
bool capturesAddressIsNullOnly(CaptureComponents CC)
Definition ModRef.h:340
auto enum_seq_inclusive(EnumT Begin, EnumT End)
Iterate over an enum type from Begin to End inclusive.
Definition Sequence.h:364
bool capturesAddress(CaptureComponents CC)
Definition ModRef.h:344
constexpr force_iteration_on_noniterable_enum_t force_iteration_on_noniterable_enum
Definition Sequence.h:109
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:313
MemoryEffects FunctionModRefBehavior
Definition ModRef.h:319
bool capturesFullProvenance(CaptureComponents CC)
Definition ModRef.h:353
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
bool isModOrRefSet(const ModRefInfo MRI)
Definition ModRef.h:43
CaptureComponents
Components of the pointer that may be captured.
Definition ModRef.h:322
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ ModRef
The access may reference and may modify the value stored in memory.
Definition ModRef.h:36
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
IRMemLocation
The locations at which a function might access memory.
Definition ModRef.h:60
@ ErrnoMem
Errno memory.
Definition ModRef.h:66
@ ArgMem
Access to memory via argument pointers.
Definition ModRef.h:62
@ TargetMem0
Represents target specific state.
Definition ModRef.h:70
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
@ InaccessibleMem
Memory that is inaccessible via LLVM IR.
Definition ModRef.h:64
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
bool isModAndRefSet(const ModRefInfo MRI)
Definition ModRef.h:46
bool capturesAnything(CaptureComponents CC)
Definition ModRef.h:336
bool capturesAll(CaptureComponents CC)
Definition ModRef.h:361
bool capturesNothing(CaptureComponents CC)
Definition ModRef.h:332
bool isNoModRef(const ModRefInfo MRI)
Definition ModRef.h:40
bool capturesAnyProvenance(CaptureComponents CC)
Definition ModRef.h:357
bool isRefSet(const ModRefInfo MRI)
Definition ModRef.h:52