LLVM 24.0.0git
AliasSetTracker.h
Go to the documentation of this file.
1//===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- 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// This file defines two classes: AliasSetTracker and AliasSet. These interfaces
10// are used to classify a collection of memory locations into a maximal number
11// of disjoint sets. Each AliasSet object constructed by the AliasSetTracker
12// object refers to memory disjoint from the other sets.
13//
14// An AliasSetTracker can only be used on immutable IR.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
19#define LLVM_ANALYSIS_ALIASSETTRACKER_H
20
21#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/ilist.h"
24#include "llvm/ADT/ilist_node.h"
26#include "llvm/IR/PassManager.h"
27#include "llvm/IR/ValueHandle.h"
29#include "llvm/Support/ModRef.h"
30#include <cassert>
31#include <vector>
32
33namespace llvm {
34
35class AliasResult;
36class AliasSetTracker;
37class AnyMemSetInst;
39class BasicBlock;
40class BatchAAResults;
41class Function;
42class Instruction;
43class StoreInst;
44class LoadInst;
45class raw_ostream;
46class VAArgInst;
47class Value;
48
49class AliasSet : public ilist_node<AliasSet> {
50 friend class AliasSetTracker;
51
52 // Forwarding pointer.
53 AliasSet *Forward = nullptr;
54
55 /// Memory locations in this alias set.
57
58 /// All instructions without a specific address in this alias set.
59 std::vector<AssertingVH<Instruction>> UnknownInsts;
60
61 /// Number of nodes pointing to this AliasSet plus the number of AliasSets
62 /// forwarding to it.
63 unsigned RefCount : 30;
64
65 // Signifies that this set should be considered to alias any pointer.
66 // Use when the tracker holding this set is saturated.
67 unsigned AliasAny : 1;
68
69 /// The kind of alias relationship between pointers of the set.
70 ///
71 /// These represent conservatively correct alias results between any members
72 /// of the set. We represent these independently of the values of alias
73 /// results in order to pack it into a single bit. Lattice goes from
74 /// MustAlias to MayAlias.
75 enum AliasLattice {
76 SetMustAlias = 0, SetMayAlias = 1
77 };
78 unsigned Alias : 1;
79
80 // The kinds of access this alias set models.
82
83 void addRef() { ++RefCount; }
84
85 void dropRef(AliasSetTracker &AST) {
86 assert(RefCount >= 1 && "Invalid reference count detected!");
87 if (--RefCount == 0)
88 removeFromTracker(AST);
89 }
90
91public:
92 AliasSet(const AliasSet &) = delete;
93 AliasSet &operator=(const AliasSet &) = delete;
94
95 /// Accessors...
96 bool isRef() const { return isRefSet(Access); }
97 bool isMod() const { return isModSet(Access); }
98 bool isMustAlias() const { return Alias == SetMustAlias; }
99 bool isMayAlias() const { return Alias == SetMayAlias; }
100
101 /// Return true if this alias set should be ignored as part of the
102 /// AliasSetTracker object.
103 bool isForwardingAliasSet() const { return Forward; }
104
105 /// Merge the specified alias set into this alias set.
107 BatchAAResults &BatchAA);
108
109 // Alias Set iteration - Allow access to all of the memory locations which are
110 // part of this alias set.
112 iterator begin() const { return MemoryLocs.begin(); }
113 iterator end() const { return MemoryLocs.end(); }
114
115 unsigned size() const { return MemoryLocs.size(); }
116
117 /// Retrieve the pointer values for the memory locations in this alias set.
118 /// The order matches that of the memory locations, but duplicate pointer
119 /// values are omitted.
122
123 LLVM_ABI void print(raw_ostream &OS) const;
124 LLVM_ABI void dump() const;
125
126private:
127 // Can only be created by AliasSetTracker.
128 AliasSet()
129 : RefCount(0), AliasAny(false), Alias(SetMustAlias),
130 Access(ModRefInfo::NoModRef) {}
131
132 LLVM_ABI void removeFromTracker(AliasSetTracker &AST);
133
134 void addMemoryLocation(AliasSetTracker &AST, const MemoryLocation &MemLoc,
135 bool KnownMustAlias = false);
136 void addUnknownInst(Instruction *I, BatchAAResults &AA);
137
138public:
139 /// If the specified memory location "may" (or must) alias one of the members
140 /// in the set return the appropriate AliasResult. Otherwise return NoAlias.
142 BatchAAResults &AA) const;
143
145 BatchAAResults &AA) const;
146};
147
149 AS.print(OS);
150 return OS;
151}
152
154 BatchAAResults &AA;
155 ilist<AliasSet> AliasSets;
156
157 using PointerMapType = DenseMap<AssertingVH<const Value>, AliasSet *>;
158
159 // Map from pointer values to the alias set holding one or more memory
160 // locations with that pointer value.
161 PointerMapType PointerMap;
162
163public:
164 /// Create an empty collection of AliasSets, and use the specified alias
165 /// analysis object to disambiguate load and store addresses.
166 explicit AliasSetTracker(BatchAAResults &AA) : AA(AA) {}
168
169 /// These methods are used to add different types of instructions to the alias
170 /// sets. Adding a new instruction can result in one of three actions
171 /// happening:
172 ///
173 /// 1. If the instruction doesn't alias any other sets, create a new set.
174 /// 2. If the instruction aliases exactly one set, add it to the set
175 /// 3. If the instruction aliases multiple sets, merge the sets, and add
176 /// the instruction to the result.
177 ///
178 LLVM_ABI void add(const MemoryLocation &Loc);
179 LLVM_ABI void add(LoadInst *LI);
180 LLVM_ABI void add(StoreInst *SI);
182 LLVM_ABI void add(VAArgInst *VAAI);
183 LLVM_ABI void add(AnyMemSetInst *MSI);
185 LLVM_ABI void
186 add(Instruction *I); // Dispatch to one of the other add methods...
187 LLVM_ABI void add(BasicBlock &BB); // Add all instructions in basic block
188 LLVM_ABI void
189 add(const AliasSetTracker &AST); // Add alias relations from another AST
191
192 LLVM_ABI void clear();
193
194 /// Return the alias sets that are active.
195 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
196
197 /// Return the alias set which contains the specified memory location. If
198 /// the memory location aliases two or more existing alias sets, will have
199 /// the effect of merging those alias sets before the single resulting alias
200 /// set is returned.
202
203 /// Return the underlying alias analysis object used by this tracker.
204 BatchAAResults &getAliasAnalysis() const { return AA; }
205
208
209 const_iterator begin() const { return AliasSets.begin(); }
210 const_iterator end() const { return AliasSets.end(); }
211
212 iterator begin() { return AliasSets.begin(); }
213 iterator end() { return AliasSets.end(); }
214
215 LLVM_ABI void print(raw_ostream &OS) const;
216 LLVM_ABI void dump() const;
217
218private:
219 friend class AliasSet;
220
221 // The total number of memory locations contained in all alias sets.
222 unsigned TotalAliasSetSize = 0;
223
224 // A non-null value signifies this AST is saturated. A saturated AST lumps
225 // all elements into a single "May" set.
226 AliasSet *AliasAnyAS = nullptr;
227
228 void removeAliasSet(AliasSet *AS);
229
230 // Update an alias set field to point to its real destination. If the field is
231 // pointing to a set that has been merged with another set and is forwarding,
232 // the field is updated to point to the set obtained by following the
233 // forwarding links. The Forward fields of intermediate alias sets are
234 // collapsed as well, and alias set reference counts are updated to reflect
235 // the new situation.
236 void collapseForwardingIn(AliasSet *&AS) {
237 if (AS->Forward) {
238 collapseForwardingIn(AS->Forward);
239 // Swap out AS for AS->Forward, while updating reference counts.
240 AliasSet *NewAS = AS->Forward;
241 NewAS->addRef();
242 AS->dropRef(*this);
243 AS = NewAS;
244 }
245 }
246
247 AliasSet &addMemoryLocation(MemoryLocation Loc, ModRefInfo MR);
248 AliasSet *mergeAliasSetsForMemoryLocation(const MemoryLocation &MemLoc,
249 AliasSet *PtrAS,
250 bool &MustAliasAll);
251
252 /// Merge all alias sets into a single set that is considered to alias
253 /// any memory location or instruction.
254 AliasSet &mergeAllAliasSets();
255
256 AliasSet *findAliasSetForUnknownInst(Instruction *Inst);
257};
258
260 AST.print(OS);
261 return OS;
262}
263
265 : public RequiredPassInfoMixin<AliasSetsPrinterPass> {
266 raw_ostream &OS;
267
268public:
271};
272
273} // end namespace llvm
274
275#endif // LLVM_ANALYSIS_ALIASSETTRACKER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
DXIL Resource Access
This file defines the DenseMap class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file provides utility analysis objects describing memory locations.
This file defines the SmallVector class.
The possible results of an alias query.
ilist< AliasSet >::iterator iterator
LLVM_ABI void dump() const
const ilist< AliasSet > & getAliasSets() const
Return the alias sets that are active.
BatchAAResults & getAliasAnalysis() const
Return the underlying alias analysis object used by this tracker.
LLVM_ABI AliasSet & getAliasSetFor(const MemoryLocation &MemLoc)
Return the alias set which contains the specified memory location.
LLVM_ABI void addUnknown(Instruction *I)
AliasSetTracker(BatchAAResults &AA)
Create an empty collection of AliasSets, and use the specified alias analysis object to disambiguate ...
LLVM_ABI void addWithoutAATags(StoreInst *SI)
const_iterator end() const
ilist< AliasSet >::const_iterator const_iterator
const_iterator begin() const
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI void add(const MemoryLocation &Loc)
These methods are used to add different types of instructions to the alias sets.
unsigned size() const
iterator begin() const
LLVM_ABI void mergeSetIn(AliasSet &AS, AliasSetTracker &AST, BatchAAResults &BatchAA)
Merge the specified alias set into this alias set.
LLVM_ABI void print(raw_ostream &OS) const
AliasSet(const AliasSet &)=delete
iterator end() const
bool isMayAlias() const
bool isForwardingAliasSet() const
Return true if this alias set should be ignored as part of the AliasSetTracker object.
AliasSet & operator=(const AliasSet &)=delete
LLVM_ABI ModRefInfo aliasesUnknownInst(const Instruction *Inst, BatchAAResults &AA) const
bool isMustAlias() const
LLVM_ABI AliasResult aliasesMemoryLocation(const MemoryLocation &MemLoc, BatchAAResults &AA) const
If the specified memory location "may" (or must) alias one of the members in the set return the appro...
friend class AliasSetTracker
SmallVectorImpl< MemoryLocation >::const_iterator iterator
bool isMod() const
bool isRef() const
Accessors...
LLVM_ABI PointerVector getPointers() const
LLVM_ABI void dump() const
SmallVector< const Value *, 8 > PointerVector
Retrieve the pointer values for the memory locations in this alias set.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI AliasSetsPrinterPass(raw_ostream &OS)
This class represents any memset intrinsic.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
An instruction for reading from memory.
Representation for a specific memory location.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
typename SuperClass::const_iterator const_iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
LLVM Value Representation.
Definition Value.h:75
typename base_list_type::const_iterator const_iterator
Definition ilist.h:122
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This file defines classes to implement an intrusive doubly linked list class (i.e.
This file defines the ilist_node class template, which is a convenient base class for creating classe...
Abstract Attribute helper functions.
Definition Attributor.h:165
This is an optimization pass for GlobalISel generic memory operations.
iplist< T, Options... > ilist
Definition ilist.h:344
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
bool isRefSet(const ModRefInfo MRI)
Definition ModRef.h:52
A CRTP mix-in for passes that should not be skipped.