1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines two classes: AliasSetTracker and AliasSet. These interfaces
// are used to classify a collection of memory locations into a maximal number
// of disjoint sets. Each AliasSet object constructed by the AliasSetTracker
// object refers to memory disjoint from the other sets.
//
// An AliasSetTracker can only be used on immutable IR.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
#define LLVM_ANALYSIS_ALIASSETTRACKER_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/ValueHandle.h"
#include <cassert>
#include <vector>
namespace llvm {
class AliasResult;
class AliasSetTracker;
class AnyMemSetInst;
class AnyMemTransferInst;
class BasicBlock;
class BatchAAResults;
class LoadInst;
enum class ModRefInfo : uint8_t;
class raw_ostream;
class StoreInst;
class VAArgInst;
class Value;
class AliasSet : public ilist_node<AliasSet> {
friend class AliasSetTracker;
// Forwarding pointer.
AliasSet *Forward = nullptr;
/// Memory locations in this alias set.
SmallVector<MemoryLocation, 0> MemoryLocs;
/// All instructions without a specific address in this alias set.
std::vector<AssertingVH<Instruction>> UnknownInsts;
/// Number of nodes pointing to this AliasSet plus the number of AliasSets
/// forwarding to it.
unsigned RefCount : 27;
// Signifies that this set should be considered to alias any pointer.
// Use when the tracker holding this set is saturated.
unsigned AliasAny : 1;
/// The kinds of access this alias set models.
///
/// We keep track of whether this alias set merely refers to the locations of
/// memory (and not any particular access), whether it modifies or references
/// the memory, or whether it does both. The lattice goes from "NoAccess" to
/// either RefAccess or ModAccess, then to ModRefAccess as necessary.
enum AccessLattice {
NoAccess = 0,
RefAccess = 1,
ModAccess = 2,
ModRefAccess = RefAccess | ModAccess
};
unsigned Access : 2;
/// The kind of alias relationship between pointers of the set.
///
/// These represent conservatively correct alias results between any members
/// of the set. We represent these independently of the values of alias
/// results in order to pack it into a single bit. Lattice goes from
/// MustAlias to MayAlias.
enum AliasLattice {
SetMustAlias = 0, SetMayAlias = 1
};
unsigned Alias : 1;
void addRef() { ++RefCount; }
void dropRef(AliasSetTracker &AST) {
assert(RefCount >= 1 && "Invalid reference count detected!");
if (--RefCount == 0)
removeFromTracker(AST);
}
public:
AliasSet(const AliasSet &) = delete;
AliasSet &operator=(const AliasSet &) = delete;
/// Accessors...
bool isRef() const { return Access & RefAccess; }
bool isMod() const { return Access & ModAccess; }
bool isMustAlias() const { return Alias == SetMustAlias; }
bool isMayAlias() const { return Alias == SetMayAlias; }
/// Return true if this alias set should be ignored as part of the
/// AliasSetTracker object.
bool isForwardingAliasSet() const { return Forward; }
/// Merge the specified alias set into this alias set.
void mergeSetIn(AliasSet &AS, AliasSetTracker &AST, BatchAAResults &BatchAA);
// Alias Set iteration - Allow access to all of the memory locations which are
// part of this alias set.
using iterator = SmallVectorImpl<MemoryLocation>::const_iterator;
iterator begin() const { return MemoryLocs.begin(); }
iterator end() const { return MemoryLocs.end(); }
unsigned size() { return MemoryLocs.size(); }
/// Retrieve the pointer values for the memory locations in this alias set.
/// The order matches that of the memory locations, but duplicate pointer
/// values are omitted.
using PointerVector = SmallVector<const Value *, 8>;
PointerVector getPointers() const;
void print(raw_ostream &OS) const;
void dump() const;
private:
// Can only be created by AliasSetTracker.
AliasSet()
: RefCount(0), AliasAny(false), Access(NoAccess), Alias(SetMustAlias) {}
void removeFromTracker(AliasSetTracker &AST);
void addMemoryLocation(AliasSetTracker &AST, const MemoryLocation &MemLoc,
bool KnownMustAlias = false);
void addUnknownInst(Instruction *I, BatchAAResults &AA);
public:
/// If the specified memory location "may" (or must) alias one of the members
/// in the set return the appropriate AliasResult. Otherwise return NoAlias.
AliasResult aliasesMemoryLocation(const MemoryLocation &MemLoc,
BatchAAResults &AA) const;
ModRefInfo aliasesUnknownInst(const Instruction *Inst,
BatchAAResults &AA) const;
};
inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) {
AS.print(OS);
return OS;
}
class AliasSetTracker {
BatchAAResults &AA;
ilist<AliasSet> AliasSets;
using PointerMapType = DenseMap<AssertingVH<const Value>, AliasSet *>;
// Map from pointer values to the alias set holding one or more memory
// locations with that pointer value.
PointerMapType PointerMap;
public:
/// Create an empty collection of AliasSets, and use the specified alias
/// analysis object to disambiguate load and store addresses.
explicit AliasSetTracker(BatchAAResults &AA) : AA(AA) {}
~AliasSetTracker() { clear(); }
/// These methods are used to add different types of instructions to the alias
/// sets. Adding a new instruction can result in one of three actions
/// happening:
///
/// 1. If the instruction doesn't alias any other sets, create a new set.
/// 2. If the instruction aliases exactly one set, add it to the set
/// 3. If the instruction aliases multiple sets, merge the sets, and add
/// the instruction to the result.
///
void add(const MemoryLocation &Loc);
void add(LoadInst *LI);
void add(StoreInst *SI);
void add(VAArgInst *VAAI);
void add(AnyMemSetInst *MSI);
void add(AnyMemTransferInst *MTI);
void add(Instruction *I); // Dispatch to one of the other add methods...
void add(BasicBlock &BB); // Add all instructions in basic block
void add(const AliasSetTracker &AST); // Add alias relations from another AST
void addUnknown(Instruction *I);
void clear();
/// Return the alias sets that are active.
const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
/// Return the alias set which contains the specified memory location. If
/// the memory location aliases two or more existing alias sets, will have
/// the effect of merging those alias sets before the single resulting alias
/// set is returned.
AliasSet &getAliasSetFor(const MemoryLocation &MemLoc);
/// Return the underlying alias analysis object used by this tracker.
BatchAAResults &getAliasAnalysis() const { return AA; }
using iterator = ilist<AliasSet>::iterator;
using const_iterator = ilist<AliasSet>::const_iterator;
const_iterator begin() const { return AliasSets.begin(); }
const_iterator end() const { return AliasSets.end(); }
iterator begin() { return AliasSets.begin(); }
iterator end() { return AliasSets.end(); }
void print(raw_ostream &OS) const;
void dump() const;
private:
friend class AliasSet;
// The total number of memory locations contained in all alias sets.
unsigned TotalAliasSetSize = 0;
// A non-null value signifies this AST is saturated. A saturated AST lumps
// all elements into a single "May" set.
AliasSet *AliasAnyAS = nullptr;
void removeAliasSet(AliasSet *AS);
// Update an alias set field to point to its real destination. If the field is
// pointing to a set that has been merged with another set and is forwarding,
// the field is updated to point to the set obtained by following the
// forwarding links. The Forward fields of intermediate alias sets are
// collapsed as well, and alias set reference counts are updated to reflect
// the new situation.
void collapseForwardingIn(AliasSet *&AS) {
if (AS->Forward) {
collapseForwardingIn(AS->Forward);
// Swap out AS for AS->Forward, while updating reference counts.
AliasSet *NewAS = AS->Forward;
NewAS->addRef();
AS->dropRef(*this);
AS = NewAS;
}
}
AliasSet &addMemoryLocation(MemoryLocation Loc, AliasSet::AccessLattice E);
AliasSet *mergeAliasSetsForMemoryLocation(const MemoryLocation &MemLoc,
AliasSet *PtrAS,
bool &MustAliasAll);
/// Merge all alias sets into a single set that is considered to alias
/// any memory location or instruction.
AliasSet &mergeAllAliasSets();
AliasSet *findAliasSetForUnknownInst(Instruction *Inst);
};
inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) {
AST.print(OS);
return OS;
}
class AliasSetsPrinterPass : public PassInfoMixin<AliasSetsPrinterPass> {
raw_ostream &OS;
public:
explicit AliasSetsPrinterPass(raw_ostream &OS);
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
static bool isRequired() { return true; }
};
} // end namespace llvm
#endif // LLVM_ANALYSIS_ALIASSETTRACKER_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|