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