LLVM 22.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 // TargetMem IDs must be at the end of the list.
78};
79
80template <typename LocationEnum> class MemoryEffectsBase {
81public:
82 using Location = LocationEnum;
83
84private:
85 uint32_t Data = 0;
86
87 static constexpr uint32_t BitsPerLoc = 2;
88 static constexpr uint32_t LocMask = (1 << BitsPerLoc) - 1;
89
90 static uint32_t getLocationPos(Location Loc) {
91 return (uint32_t)Loc * BitsPerLoc;
92 }
93
95
96 void setModRef(Location Loc, ModRefInfo MR) {
97 Data &= ~(LocMask << getLocationPos(Loc));
98 Data |= static_cast<uint32_t>(MR) << getLocationPos(Loc);
99 }
100
101public:
102 /// Returns iterator over all supported location kinds.
103 static auto locations() {
104 return enum_seq_inclusive(Location::First, Location::Last,
106 }
107
108 /// Create MemoryEffectsBase that can access only the given location with the
109 /// given ModRefInfo.
110 MemoryEffectsBase(Location Loc, ModRefInfo MR) { setModRef(Loc, MR); }
111
112 /// Create MemoryEffectsBase that can access any location with the given
113 /// ModRefInfo.
115 for (Location Loc : locations())
116 setModRef(Loc, MR);
117 }
118
119 /// Create MemoryEffectsBase that can read and write any memory.
120 static MemoryEffectsBase unknown() {
121 return MemoryEffectsBase(ModRefInfo::ModRef);
122 }
123
124 /// Create MemoryEffectsBase that cannot read or write any memory.
125 static MemoryEffectsBase none() {
126 return MemoryEffectsBase(ModRefInfo::NoModRef);
127 }
128
129 /// Create MemoryEffectsBase that can read any memory.
130 static MemoryEffectsBase readOnly() {
131 return MemoryEffectsBase(ModRefInfo::Ref);
132 }
133
134 /// Create MemoryEffectsBase that can write any memory.
135 static MemoryEffectsBase writeOnly() {
136 return MemoryEffectsBase(ModRefInfo::Mod);
137 }
138
139 /// Create MemoryEffectsBase that can only access argument memory.
140 static MemoryEffectsBase argMemOnly(ModRefInfo MR = ModRefInfo::ModRef) {
141 return MemoryEffectsBase(Location::ArgMem, MR);
142 }
143
144 /// Create MemoryEffectsBase that can only access inaccessible memory.
145 static MemoryEffectsBase
147 return MemoryEffectsBase(Location::InaccessibleMem, MR);
148 }
149
150 /// Create MemoryEffectsBase that can only access errno memory.
151 static MemoryEffectsBase errnoMemOnly(ModRefInfo MR = ModRefInfo::ModRef) {
152 return MemoryEffectsBase(Location::ErrnoMem, MR);
153 }
154
155 /// Create MemoryEffectsBase that can only access other memory.
156 static MemoryEffectsBase otherMemOnly(ModRefInfo MR = ModRefInfo::ModRef) {
157 return MemoryEffectsBase(Location::Other, MR);
158 }
159
160 /// Create MemoryEffectsBase that can only access inaccessible or argument
161 /// memory.
162 static MemoryEffectsBase
164 MemoryEffectsBase FRMB = none();
165 FRMB.setModRef(Location::ArgMem, MR);
166 FRMB.setModRef(Location::InaccessibleMem, MR);
167 return FRMB;
168 }
169
170 /// Create MemoryEffectsBase that can only access argument or errno memory.
171 static MemoryEffectsBase
173 ModRefInfo ErrnoMR = ModRefInfo::ModRef) {
174 MemoryEffectsBase FRMB = none();
175 FRMB.setModRef(Location::ArgMem, ArgMR);
176 FRMB.setModRef(Location::ErrnoMem, ErrnoMR);
177 return FRMB;
178 }
179
180 /// Create MemoryEffectsBase from an encoded integer value (used by memory
181 /// attribute).
182 static MemoryEffectsBase createFromIntValue(uint32_t Data) {
183 return MemoryEffectsBase(Data);
184 }
185
186 /// Convert MemoryEffectsBase into an encoded integer value (used by memory
187 /// attribute).
189 return Data;
190 }
191
192 /// Get ModRefInfo for the given Location.
194 return ModRefInfo((Data >> getLocationPos(Loc)) & LocMask);
195 }
196
197 /// Get new MemoryEffectsBase with modified ModRefInfo for Loc.
198 MemoryEffectsBase getWithModRef(Location Loc, ModRefInfo MR) const {
199 MemoryEffectsBase ME = *this;
200 ME.setModRef(Loc, MR);
201 return ME;
202 }
203
204 /// Get new MemoryEffectsBase with NoModRef on the given Loc.
205 MemoryEffectsBase getWithoutLoc(Location Loc) const {
206 MemoryEffectsBase ME = *this;
207 ME.setModRef(Loc, ModRefInfo::NoModRef);
208 return ME;
209 }
210
211 /// Get ModRefInfo for any location.
214 for (Location Loc : locations())
215 MR |= getModRef(Loc);
216 return MR;
217 }
218
219 /// Whether this function accesses no memory.
220 bool doesNotAccessMemory() const { return Data == 0; }
221
222 /// Whether this function only (at most) reads memory.
223 bool onlyReadsMemory() const { return !isModSet(getModRef()); }
224
225 /// Whether this function only (at most) writes memory.
226 bool onlyWritesMemory() const { return !isRefSet(getModRef()); }
227
228 /// Whether this function only (at most) accesses argument memory.
230 return getWithoutLoc(Location::ArgMem).doesNotAccessMemory();
231 }
232
233 /// Whether this function may access argument memory.
235 return isModOrRefSet(getModRef(Location::ArgMem));
236 }
237
238 /// Whether this function only (at most) accesses inaccessible memory.
240 return getWithoutLoc(Location::InaccessibleMem).doesNotAccessMemory();
241 }
242
243 /// Whether this function only (at most) accesses errno memory.
244 bool onlyAccessesErrnoMem() const {
245 return getWithoutLoc(Location::ErrnoMem).doesNotAccessMemory();
246 }
247
248 /// Whether this function only (at most) accesses argument and inaccessible
249 /// memory.
251 return getWithoutLoc(Location::InaccessibleMem)
252 .getWithoutLoc(Location::ArgMem)
254 }
255
256 /// Intersect with other MemoryEffectsBase.
257 MemoryEffectsBase operator&(MemoryEffectsBase Other) const {
258 return MemoryEffectsBase(Data & Other.Data);
259 }
260
261 /// Intersect (in-place) with other MemoryEffectsBase.
262 MemoryEffectsBase &operator&=(MemoryEffectsBase Other) {
263 Data &= Other.Data;
264 return *this;
265 }
266
267 /// Union with other MemoryEffectsBase.
268 MemoryEffectsBase operator|(MemoryEffectsBase Other) const {
269 return MemoryEffectsBase(Data | Other.Data);
270 }
271
272 /// Union (in-place) with other MemoryEffectsBase.
273 MemoryEffectsBase &operator|=(MemoryEffectsBase Other) {
274 Data |= Other.Data;
275 return *this;
276 }
277
278 /// Subtract other MemoryEffectsBase.
279 MemoryEffectsBase operator-(MemoryEffectsBase Other) const {
280 return MemoryEffectsBase(Data & ~Other.Data);
281 }
282
283 /// Subtract (in-place) with other MemoryEffectsBase.
284 MemoryEffectsBase &operator-=(MemoryEffectsBase Other) {
285 Data &= ~Other.Data;
286 return *this;
287 }
288
289 /// Check whether this is the same as other MemoryEffectsBase.
290 bool operator==(MemoryEffectsBase Other) const { return Data == Other.Data; }
291
292 /// Check whether this is different from other MemoryEffectsBase.
293 bool operator!=(MemoryEffectsBase Other) const { return !operator==(Other); }
294};
295
296/// Summary of how a function affects memory in the program.
297///
298/// Loads from constant globals are not considered memory accesses for this
299/// interface. Also, functions may freely modify stack space local to their
300/// invocation without having to report it through these interfaces.
302
303/// Debug print MemoryEffects.
305
306// Legacy alias.
308
309/// Components of the pointer that may be captured.
319
321 return CC == CaptureComponents::None;
322}
323
325 return CC != CaptureComponents::None;
326}
327
331
335
340
344
348
350 return CC == CaptureComponents::All;
351}
352
353LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, CaptureComponents CC);
354
355/// Represents which components of the pointer may be captured in which
356/// location. This represents the captures(...) attribute in IR.
357///
358/// For more information on the precise semantics see LangRef.
360 CaptureComponents OtherComponents;
361 CaptureComponents RetComponents;
362
363public:
365 CaptureComponents RetComponents)
366 : OtherComponents(OtherComponents), RetComponents(RetComponents) {}
367
369 : OtherComponents(Components), RetComponents(Components) {}
370
371 /// Create CaptureInfo that does not capture any components of the pointer
373
374 /// Create CaptureInfo that may capture all components of the pointer.
376
377 /// Create CaptureInfo that may only capture via the return value.
378 static CaptureInfo
380 return CaptureInfo(CaptureComponents::None, RetComponents);
381 }
382
383 /// Whether the pointer is only captured via the return value.
384 bool isRetOnly() const { return capturesNothing(OtherComponents); }
385
386 /// Get components potentially captured by the return value.
387 CaptureComponents getRetComponents() const { return RetComponents; }
388
389 /// Get components potentially captured through locations other than the
390 /// return value.
391 CaptureComponents getOtherComponents() const { return OtherComponents; }
392
393 /// Get the potentially captured components of the pointer (regardless of
394 /// location).
395 operator CaptureComponents() const { return OtherComponents | RetComponents; }
396
398 return OtherComponents == Other.OtherComponents &&
399 RetComponents == Other.RetComponents;
400 }
401
402 bool operator!=(CaptureInfo Other) const { return !(*this == Other); }
403
404 /// Compute union of CaptureInfos.
406 return CaptureInfo(OtherComponents | Other.OtherComponents,
407 RetComponents | Other.RetComponents);
408 }
409
410 /// Compute intersection of CaptureInfos.
412 return CaptureInfo(OtherComponents & Other.OtherComponents,
413 RetComponents & Other.RetComponents);
414 }
415
416 /// Compute union of CaptureInfos in-place.
418 OtherComponents |= Other.OtherComponents;
419 RetComponents |= Other.RetComponents;
420 return *this;
421 }
422
423 /// Compute intersection of CaptureInfos in-place.
425 OtherComponents &= Other.OtherComponents;
426 RetComponents &= Other.RetComponents;
427 return *this;
428 }
429
434
435 /// Convert CaptureInfo into an encoded integer value (used by captures
436 /// attribute).
438 return (uint32_t(OtherComponents) << 4) | uint32_t(RetComponents);
439 }
440};
441
442LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, CaptureInfo Info);
443
444} // namespace llvm
445
446#endif
unsigned const MachineRegisterInfo * MRI
Analysis containing CSE Info
Definition CSEInfo.cpp:27
#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:359
static CaptureInfo createFromIntValue(uint32_t Data)
Definition ModRef.h:430
CaptureComponents getOtherComponents() const
Get components potentially captured through locations other than the return value.
Definition ModRef.h:391
static CaptureInfo none()
Create CaptureInfo that does not capture any components of the pointer.
Definition ModRef.h:372
bool operator==(CaptureInfo Other) const
Definition ModRef.h:397
bool operator!=(CaptureInfo Other) const
Definition ModRef.h:402
static CaptureInfo retOnly(CaptureComponents RetComponents=CaptureComponents::All)
Create CaptureInfo that may only capture via the return value.
Definition ModRef.h:379
static CaptureInfo all()
Create CaptureInfo that may capture all components of the pointer.
Definition ModRef.h:375
CaptureInfo operator&(CaptureInfo Other) const
Compute intersection of CaptureInfos.
Definition ModRef.h:411
CaptureInfo & operator|=(CaptureInfo Other)
Compute union of CaptureInfos in-place.
Definition ModRef.h:417
CaptureComponents getRetComponents() const
Get components potentially captured by the return value.
Definition ModRef.h:387
CaptureInfo(CaptureComponents OtherComponents, CaptureComponents RetComponents)
Definition ModRef.h:364
CaptureInfo & operator&=(CaptureInfo Other)
Compute intersection of CaptureInfos in-place.
Definition ModRef.h:424
CaptureInfo operator|(CaptureInfo Other) const
Compute union of CaptureInfos.
Definition ModRef.h:405
bool isRetOnly() const
Whether the pointer is only captured via the return value.
Definition ModRef.h:384
uint32_t toIntValue() const
Convert CaptureInfo into an encoded integer value (used by captures attribute).
Definition ModRef.h:437
CaptureInfo(CaptureComponents Components)
Definition ModRef.h:368
MemoryEffectsBase operator&(MemoryEffectsBase Other) const
Intersect with other MemoryEffectsBase.
Definition ModRef.h:257
static MemoryEffectsBase readOnly()
Create MemoryEffectsBase that can read any memory.
Definition ModRef.h:130
bool onlyWritesMemory() const
Whether this function only (at most) writes memory.
Definition ModRef.h:226
MemoryEffectsBase getWithoutLoc(Location Loc) const
Get new MemoryEffectsBase with NoModRef on the given Loc.
Definition ModRef.h:205
MemoryEffectsBase & operator|=(MemoryEffectsBase Other)
Union (in-place) with other MemoryEffectsBase.
Definition ModRef.h:273
MemoryEffectsBase getWithModRef(Location Loc, ModRefInfo MR) const
Get new MemoryEffectsBase with modified ModRefInfo for Loc.
Definition ModRef.h:198
bool operator!=(MemoryEffectsBase Other) const
Check whether this is different from other MemoryEffectsBase.
Definition ModRef.h:293
MemoryEffectsBase operator-(MemoryEffectsBase Other) const
Subtract other MemoryEffectsBase.
Definition ModRef.h:279
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition ModRef.h:220
static MemoryEffectsBase argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access argument memory.
Definition ModRef.h:140
MemoryEffectsBase(ModRefInfo MR)
Create MemoryEffectsBase that can access any location with the given ModRefInfo.
Definition ModRef.h:114
bool doesAccessArgPointees() const
Whether this function may access argument memory.
Definition ModRef.h:234
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible memory.
Definition ModRef.h:146
bool onlyAccessesInaccessibleMem() const
Whether this function only (at most) accesses inaccessible memory.
Definition ModRef.h:239
MemoryEffectsBase(Location Loc, ModRefInfo MR)
Create MemoryEffectsBase that can access only the given location with the given ModRefInfo.
Definition ModRef.h:110
ModRefInfo getModRef(Location Loc) const
Get ModRefInfo for the given Location.
Definition ModRef.h:193
MemoryEffectsBase & operator&=(MemoryEffectsBase Other)
Intersect (in-place) with other MemoryEffectsBase.
Definition ModRef.h:262
ModRefInfo getModRef() const
Get ModRefInfo for any location.
Definition ModRef.h:212
LocationEnum Location
Definition ModRef.h:82
bool onlyAccessesArgPointees() const
Whether this function only (at most) accesses argument memory.
Definition ModRef.h:229
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition ModRef.h:223
static MemoryEffectsBase errnoMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access errno memory.
Definition ModRef.h:151
static MemoryEffectsBase createFromIntValue(uint32_t Data)
Create MemoryEffectsBase from an encoded integer value (used by memory attribute).
Definition ModRef.h:182
MemoryEffectsBase & operator-=(MemoryEffectsBase Other)
Subtract (in-place) with other MemoryEffectsBase.
Definition ModRef.h:284
static MemoryEffectsBase writeOnly()
Create MemoryEffectsBase that can write any memory.
Definition ModRef.h:135
MemoryEffectsBase operator|(MemoryEffectsBase Other) const
Union with other MemoryEffectsBase.
Definition ModRef.h:268
static MemoryEffectsBase otherMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access other memory.
Definition ModRef.h:156
static auto locations()
Returns iterator over all supported location kinds.
Definition ModRef.h:103
bool onlyAccessesErrnoMem() const
Whether this function only (at most) accesses errno memory.
Definition ModRef.h:244
uint32_t toIntValue() const
Convert MemoryEffectsBase into an encoded integer value (used by memory attribute).
Definition ModRef.h:188
static MemoryEffectsBase inaccessibleOrArgMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible or argument memory.
Definition ModRef.h:163
static MemoryEffectsBase argumentOrErrnoMemOnly(ModRefInfo ArgMR=ModRefInfo::ModRef, ModRefInfo ErrnoMR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access argument or errno memory.
Definition ModRef.h:172
static MemoryEffectsBase none()
Create MemoryEffectsBase that cannot read or write any memory.
Definition ModRef.h:125
bool operator==(MemoryEffectsBase Other) const
Check whether this is the same as other MemoryEffectsBase.
Definition ModRef.h:290
bool onlyAccessesInaccessibleOrArgMem() const
Whether this function only (at most) accesses argument and inaccessible memory.
Definition ModRef.h:250
static MemoryEffectsBase unknown()
Create MemoryEffectsBase that can read and write any memory.
Definition ModRef.h:120
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.
bool capturesReadProvenanceOnly(CaptureComponents CC)
Definition ModRef.h:336
bool capturesAddressIsNullOnly(CaptureComponents CC)
Definition ModRef.h:328
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:332
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:301
MemoryEffects FunctionModRefBehavior
Definition ModRef.h:307
bool capturesFullProvenance(CaptureComponents CC)
Definition ModRef.h:341
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:310
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
@ LLVM_MARK_AS_BITMASK_ENUM
Definition ModRef.h:37
@ 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:324
bool capturesAll(CaptureComponents CC)
Definition ModRef.h:349
bool capturesNothing(CaptureComponents CC)
Definition ModRef.h:320
bool isNoModRef(const ModRefInfo MRI)
Definition ModRef.h:40
bool capturesAnyProvenance(CaptureComponents CC)
Definition ModRef.h:345
bool isRefSet(const ModRefInfo MRI)
Definition ModRef.h:52