LLVM 17.0.0git
ModRef.h
Go to the documentation of this file.
1//===--- ModRef.h - Memory effect modelling ---------------------*- 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"
20
21namespace llvm {
22
23/// Flags indicating whether a memory access modifies or references memory.
24///
25/// This is no access at all, a modification, a reference, or both
26/// a modification and a reference.
27enum class ModRefInfo : uint8_t {
28 /// The access neither references nor modifies the value stored in memory.
29 NoModRef = 0,
30 /// The access may reference the value stored in memory.
31 Ref = 1,
32 /// The access may modify the value stored in memory.
33 Mod = 2,
34 /// The access may reference and may modify the value stored in memory.
35 ModRef = Ref | Mod,
37};
38
39[[nodiscard]] inline bool isNoModRef(const ModRefInfo MRI) {
40 return MRI == ModRefInfo::NoModRef;
41}
42[[nodiscard]] inline bool isModOrRefSet(const ModRefInfo MRI) {
43 return MRI != ModRefInfo::NoModRef;
44}
45[[nodiscard]] inline bool isModAndRefSet(const ModRefInfo MRI) {
46 return MRI == ModRefInfo::ModRef;
47}
48[[nodiscard]] inline bool isModSet(const ModRefInfo MRI) {
49 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Mod);
50}
51[[nodiscard]] inline bool isRefSet(const ModRefInfo MRI) {
52 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Ref);
53}
54
55/// Debug print ModRefInfo.
56raw_ostream &operator<<(raw_ostream &OS, ModRefInfo MR);
57
58/// Summary of how a function affects memory in the program.
59///
60/// Loads from constant globals are not considered memory accesses for this
61/// interface. Also, functions may freely modify stack space local to their
62/// invocation without having to report it through these interfaces.
64public:
65 /// The locations at which a function might access memory.
66 enum Location {
67 /// Access to memory via argument pointers.
68 ArgMem = 0,
69 /// Memory that is inaccessible via LLVM IR.
71 /// Any other memory.
72 Other = 2,
73 };
74
75private:
76 uint32_t Data = 0;
77
78 static constexpr uint32_t BitsPerLoc = 2;
79 static constexpr uint32_t LocMask = (1 << BitsPerLoc) - 1;
80
81 static uint32_t getLocationPos(Location Loc) {
82 return (uint32_t)Loc * BitsPerLoc;
83 }
84
85 MemoryEffects(uint32_t Data) : Data(Data) {}
86
87 void setModRef(Location Loc, ModRefInfo MR) {
88 Data &= ~(LocMask << getLocationPos(Loc));
89 Data |= static_cast<uint32_t>(MR) << getLocationPos(Loc);
90 }
91
93
94public:
95 /// Returns iterator over all supported location kinds.
96 static auto locations() {
99 }
100
101 /// Create MemoryEffects that can access only the given location with the
102 /// given ModRefInfo.
103 MemoryEffects(Location Loc, ModRefInfo MR) { setModRef(Loc, MR); }
104
105 /// Create MemoryEffects that can access any location with the given
106 /// ModRefInfo.
108 for (Location Loc : locations())
109 setModRef(Loc, MR);
110 }
111
112 /// Create MemoryEffects that can read and write any memory.
115 }
116
117 /// Create MemoryEffects that cannot read or write any memory.
120 }
121
122 /// Create MemoryEffects that can read any memory.
125 }
126
127 /// Create MemoryEffects that can write any memory.
130 }
131
132 /// Create MemoryEffects that can only access argument memory.
134 return MemoryEffects(ArgMem, MR);
135 }
136
137 /// Create MemoryEffects that can only access inaccessible memory.
139 return MemoryEffects(InaccessibleMem, MR);
140 }
141
142 /// Create MemoryEffects that can only access inaccessible or argument memory.
143 static MemoryEffects
145 MemoryEffects FRMB = none();
146 FRMB.setModRef(ArgMem, MR);
147 FRMB.setModRef(InaccessibleMem, MR);
148 return FRMB;
149 }
150
151 /// Create MemoryEffects from an encoded integer value (used by memory
152 /// attribute).
154 return MemoryEffects(Data);
155 }
156
157 /// Convert MemoryEffects into an encoded integer value (used by memory
158 /// attribute).
160 return Data;
161 }
162
163 /// Get ModRefInfo for the given Location.
165 return ModRefInfo((Data >> getLocationPos(Loc)) & LocMask);
166 }
167
168 /// Get new MemoryEffects with modified ModRefInfo for Loc.
170 MemoryEffects ME = *this;
171 ME.setModRef(Loc, MR);
172 return ME;
173 }
174
175 /// Get new MemoryEffects with NoModRef on the given Loc.
177 MemoryEffects ME = *this;
178 ME.setModRef(Loc, ModRefInfo::NoModRef);
179 return ME;
180 }
181
182 /// Get ModRefInfo for any location.
185 for (Location Loc : locations())
186 MR |= getModRef(Loc);
187 return MR;
188 }
189
190 /// Whether this function accesses no memory.
191 bool doesNotAccessMemory() const { return Data == 0; }
192
193 /// Whether this function only (at most) reads memory.
194 bool onlyReadsMemory() const { return !isModSet(getModRef()); }
195
196 /// Whether this function only (at most) writes memory.
197 bool onlyWritesMemory() const { return !isRefSet(getModRef()); }
198
199 /// Whether this function only (at most) accesses argument memory.
202 }
203
204 /// Whether this function may access argument memory.
207 }
208
209 /// Whether this function only (at most) accesses inaccessible memory.
212 }
213
214 /// Whether this function only (at most) accesses argument and inaccessible
215 /// memory.
217 return isNoModRef(getModRef(Other));
218 }
219
220 /// Intersect with other MemoryEffects.
222 return MemoryEffects(Data & Other.Data);
223 }
224
225 /// Intersect (in-place) with other MemoryEffects.
227 Data &= Other.Data;
228 return *this;
229 }
230
231 /// Union with other MemoryEffects.
233 return MemoryEffects(Data | Other.Data);
234 }
235
236 /// Union (in-place) with other MemoryEffects.
238 Data |= Other.Data;
239 return *this;
240 }
241
242 /// Check whether this is the same as other MemoryEffects.
244 return Data == Other.Data;
245 }
246
247 /// Check whether this is different from other MemoryEffects.
249 return !operator==(Other);
250 }
251};
252
253/// Debug print MemoryEffects.
254raw_ostream &operator<<(raw_ostream &OS, MemoryEffects RMRB);
255
256// Legacy alias.
258
259} // namespace llvm
260
261#endif
unsigned const MachineRegisterInfo * MRI
raw_pwrite_stream & OS
Provides some synthesis utilities to produce sequences of values.
Summary of how a function affects memory in the program.
Definition: ModRef.h:63
bool onlyAccessesArgPointees() const
Whether this function only (at most) accesses argument memory.
Definition: ModRef.h:200
static MemoryEffects writeOnly()
Create MemoryEffects that can write any memory.
Definition: ModRef.h:128
bool doesAccessArgPointees() const
Whether this function may access argument memory.
Definition: ModRef.h:205
MemoryEffects getWithModRef(Location Loc, ModRefInfo MR) const
Get new MemoryEffects with modified ModRefInfo for Loc.
Definition: ModRef.h:169
static MemoryEffects readOnly()
Create MemoryEffects that can read any memory.
Definition: ModRef.h:123
MemoryEffects & operator&=(MemoryEffects Other)
Intersect (in-place) with other MemoryEffects.
Definition: ModRef.h:226
static MemoryEffects inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffects that can only access inaccessible memory.
Definition: ModRef.h:138
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition: ModRef.h:194
MemoryEffects(Location Loc, ModRefInfo MR)
Create MemoryEffects that can access only the given location with the given ModRefInfo.
Definition: ModRef.h:103
bool onlyWritesMemory() const
Whether this function only (at most) writes memory.
Definition: ModRef.h:197
static MemoryEffects inaccessibleOrArgMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffects that can only access inaccessible or argument memory.
Definition: ModRef.h:144
bool onlyAccessesInaccessibleMem() const
Whether this function only (at most) accesses inaccessible memory.
Definition: ModRef.h:210
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition: ModRef.h:191
MemoryEffects(ModRefInfo MR)
Create MemoryEffects that can access any location with the given ModRefInfo.
Definition: ModRef.h:107
MemoryEffects & operator|=(MemoryEffects Other)
Union (in-place) with other MemoryEffects.
Definition: ModRef.h:237
friend raw_ostream & operator<<(raw_ostream &OS, MemoryEffects RMRB)
Debug print MemoryEffects.
static MemoryEffects createFromIntValue(uint32_t Data)
Create MemoryEffects from an encoded integer value (used by memory attribute).
Definition: ModRef.h:153
MemoryEffects operator&(MemoryEffects Other) const
Intersect with other MemoryEffects.
Definition: ModRef.h:221
Location
The locations at which a function might access memory.
Definition: ModRef.h:66
@ ArgMem
Access to memory via argument pointers.
Definition: ModRef.h:68
@ Other
Any other memory.
Definition: ModRef.h:72
@ InaccessibleMem
Memory that is inaccessible via LLVM IR.
Definition: ModRef.h:70
MemoryEffects getWithoutLoc(Location Loc) const
Get new MemoryEffects with NoModRef on the given Loc.
Definition: ModRef.h:176
ModRefInfo getModRef(Location Loc) const
Get ModRefInfo for the given Location.
Definition: ModRef.h:164
ModRefInfo getModRef() const
Get ModRefInfo for any location.
Definition: ModRef.h:183
bool onlyAccessesInaccessibleOrArgMem() const
Whether this function only (at most) accesses argument and inaccessible memory.
Definition: ModRef.h:216
uint32_t toIntValue() const
Convert MemoryEffects into an encoded integer value (used by memory attribute).
Definition: ModRef.h:159
static MemoryEffects none()
Create MemoryEffects that cannot read or write any memory.
Definition: ModRef.h:118
MemoryEffects operator|(MemoryEffects Other) const
Union with other MemoryEffects.
Definition: ModRef.h:232
static MemoryEffects argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffects that can only access argument memory.
Definition: ModRef.h:133
bool operator==(MemoryEffects Other) const
Check whether this is the same as other MemoryEffects.
Definition: ModRef.h:243
static auto locations()
Returns iterator over all supported location kinds.
Definition: ModRef.h:96
static MemoryEffects unknown()
Create MemoryEffects that can read and write any memory.
Definition: ModRef.h:113
bool operator!=(MemoryEffects Other) const
Check whether this is different from other MemoryEffects.
Definition: ModRef.h:248
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto enum_seq_inclusive(EnumT Begin, EnumT End)
Iterate over an enum type from Begin to End inclusive.
Definition: Sequence.h:354
constexpr force_iteration_on_noniterable_enum_t force_iteration_on_noniterable_enum
Definition: Sequence.h:108
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:48
bool isModOrRefSet(const ModRefInfo MRI)
Definition: ModRef.h:42
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:27
@ 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.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:292
bool isModAndRefSet(const ModRefInfo MRI)
Definition: ModRef.h:45
bool isNoModRef(const ModRefInfo MRI)
Definition: ModRef.h:39
bool isRefSet(const ModRefInfo MRI)
Definition: ModRef.h:51