1 //===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines two classes: AliasSetTracker and AliasSet. These interface
11 // are used to classify a collection of pointer references into a maximal number
12 // of disjoint sets. Each AliasSet object constructed by the AliasSetTracker
13 // object refers to memory disjoint from the other sets.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
18 #define LLVM_ANALYSIS_ALIASSETTRACKER_H
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/ilist.h"
22 #include "llvm/ADT/ilist_node.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/IR/ValueHandle.h"
33 class AliasSetTracker;
36 class AliasSet : public ilist_node<AliasSet> {
37 friend class AliasSetTracker;
40 Value *Val; // The pointer this record corresponds to.
41 PointerRec **PrevInList, *NextInList;
48 : Val(V), PrevInList(nullptr), NextInList(nullptr), AS(nullptr), Size(0),
49 AAInfo(DenseMapInfo<AAMDNodes>::getEmptyKey()) {}
51 Value *getValue() const { return Val; }
53 PointerRec *getNext() const { return NextInList; }
54 bool hasAliasSet() const { return AS != nullptr; }
56 PointerRec** setPrevInList(PointerRec **PIL) {
61 void updateSizeAndAAInfo(uint64_t NewSize, const AAMDNodes &NewAAInfo) {
62 if (NewSize > Size) Size = NewSize;
64 if (AAInfo == DenseMapInfo<AAMDNodes>::getEmptyKey())
65 // We don't have a AAInfo yet. Set it to NewAAInfo.
67 else if (AAInfo != NewAAInfo)
68 // NewAAInfo conflicts with AAInfo.
69 AAInfo = DenseMapInfo<AAMDNodes>::getTombstoneKey();
72 uint64_t getSize() const { return Size; }
74 /// getAAInfo - Return the AAInfo, or null if there is no
75 /// information or conflicting information.
76 AAMDNodes getAAInfo() const {
77 // If we have missing or conflicting AAInfo, return null.
78 if (AAInfo == DenseMapInfo<AAMDNodes>::getEmptyKey() ||
79 AAInfo == DenseMapInfo<AAMDNodes>::getTombstoneKey())
84 AliasSet *getAliasSet(AliasSetTracker &AST) {
85 assert(AS && "No AliasSet yet!");
88 AS = OldAS->getForwardedTarget(AST);
95 void setAliasSet(AliasSet *as) {
96 assert(!AS && "Already have an alias set!");
100 void eraseFromList() {
101 if (NextInList) NextInList->PrevInList = PrevInList;
102 *PrevInList = NextInList;
103 if (AS->PtrListEnd == &NextInList) {
104 AS->PtrListEnd = PrevInList;
105 assert(*AS->PtrListEnd == nullptr && "List not terminated right!");
111 PointerRec *PtrList, **PtrListEnd; // Doubly linked list of nodes.
112 AliasSet *Forward; // Forwarding pointer.
114 // All instructions without a specific address in this alias set.
115 std::vector<AssertingVH<Instruction> > UnknownInsts;
117 // RefCount - Number of nodes pointing to this AliasSet plus the number of
118 // AliasSets forwarding to it.
119 unsigned RefCount : 28;
121 /// The kinds of access this alias set models.
123 /// We keep track of whether this alias set merely refers to the locations of
124 /// memory (and not any particular access), whether it modifies or references
125 /// the memory, or whether it does both. The lattice goes from "NoAccess" to
126 /// either RefAccess or ModAccess, then to ModRefAccess as necessary.
131 ModRefAccess = RefAccess | ModAccess
135 /// The kind of alias relationship between pointers of the set.
137 /// These represent conservatively correct alias results between any members
138 /// of the set. We represent these independently of the values of alias
139 /// results in order to pack it into a single bit. Lattice goes from
140 /// MustAlias to MayAlias.
142 SetMustAlias = 0, SetMayAlias = 1
146 // Volatile - True if this alias set contains volatile loads or stores.
149 void addRef() { ++RefCount; }
150 void dropRef(AliasSetTracker &AST) {
151 assert(RefCount >= 1 && "Invalid reference count detected!");
153 removeFromTracker(AST);
156 Instruction *getUnknownInst(unsigned i) const {
157 assert(i < UnknownInsts.size());
158 return UnknownInsts[i];
163 bool isRef() const { return Access & RefAccess; }
164 bool isMod() const { return Access & ModAccess; }
165 bool isMustAlias() const { return Alias == SetMustAlias; }
166 bool isMayAlias() const { return Alias == SetMayAlias; }
168 // isVolatile - Return true if this alias set contains volatile loads or
170 bool isVolatile() const { return Volatile; }
172 /// isForwardingAliasSet - Return true if this alias set should be ignored as
173 /// part of the AliasSetTracker object.
174 bool isForwardingAliasSet() const { return Forward; }
176 /// mergeSetIn - Merge the specified alias set into this alias set...
178 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
180 // Alias Set iteration - Allow access to all of the pointer which are part of
183 iterator begin() const { return iterator(PtrList); }
184 iterator end() const { return iterator(); }
185 bool empty() const { return PtrList == nullptr; }
187 void print(raw_ostream &OS) const;
190 /// Define an iterator for alias sets... this is just a forward iterator.
191 class iterator : public std::iterator<std::forward_iterator_tag,
192 PointerRec, ptrdiff_t> {
196 explicit iterator(PointerRec *CN = nullptr) : CurNode(CN) {}
198 bool operator==(const iterator& x) const {
199 return CurNode == x.CurNode;
201 bool operator!=(const iterator& x) const { return !operator==(x); }
203 value_type &operator*() const {
204 assert(CurNode && "Dereferencing AliasSet.end()!");
207 value_type *operator->() const { return &operator*(); }
209 Value *getPointer() const { return CurNode->getValue(); }
210 uint64_t getSize() const { return CurNode->getSize(); }
211 AAMDNodes getAAInfo() const { return CurNode->getAAInfo(); }
213 iterator& operator++() { // Preincrement
214 assert(CurNode && "Advancing past AliasSet.end()!");
215 CurNode = CurNode->getNext();
218 iterator operator++(int) { // Postincrement
219 iterator tmp = *this; ++*this; return tmp;
224 // Can only be created by AliasSetTracker. Also, ilist creates one
225 // to serve as a sentinel.
226 friend struct ilist_sentinel_traits<AliasSet>;
228 : PtrList(nullptr), PtrListEnd(&PtrList), Forward(nullptr), RefCount(0),
229 Access(NoAccess), Alias(SetMustAlias), Volatile(false) {
232 AliasSet(const AliasSet &AS) = delete;
233 void operator=(const AliasSet &AS) = delete;
235 PointerRec *getSomePointer() const {
239 /// getForwardedTarget - Return the real alias set this represents. If this
240 /// has been merged with another set and is forwarding, return the ultimate
241 /// destination set. This also implements the union-find collapsing as well.
242 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
243 if (!Forward) return this;
245 AliasSet *Dest = Forward->getForwardedTarget(AST);
246 if (Dest != Forward) {
248 Forward->dropRef(AST);
254 void removeFromTracker(AliasSetTracker &AST);
256 void addPointer(AliasSetTracker &AST, PointerRec &Entry, uint64_t Size,
257 const AAMDNodes &AAInfo,
258 bool KnownMustAlias = false);
259 void addUnknownInst(Instruction *I, AliasAnalysis &AA);
260 void removeUnknownInst(AliasSetTracker &AST, Instruction *I) {
261 bool WasEmpty = UnknownInsts.empty();
262 for (size_t i = 0, e = UnknownInsts.size(); i != e; ++i)
263 if (UnknownInsts[i] == I) {
264 UnknownInsts[i] = UnknownInsts.back();
265 UnknownInsts.pop_back();
266 --i; --e; // Revisit the moved entry.
268 if (!WasEmpty && UnknownInsts.empty())
271 void setVolatile() { Volatile = true; }
274 /// aliasesPointer - Return true if the specified pointer "may" (or must)
275 /// alias one of the members in the set.
277 bool aliasesPointer(const Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo,
278 AliasAnalysis &AA) const;
279 bool aliasesUnknownInst(const Instruction *Inst, AliasAnalysis &AA) const;
282 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) {
287 class AliasSetTracker {
288 /// CallbackVH - A CallbackVH to arrange for AliasSetTracker to be
289 /// notified whenever a Value is deleted.
290 class ASTCallbackVH final : public CallbackVH {
291 AliasSetTracker *AST;
292 void deleted() override;
293 void allUsesReplacedWith(Value *) override;
296 ASTCallbackVH(Value *V, AliasSetTracker *AST = nullptr);
297 ASTCallbackVH &operator=(Value *V);
299 /// ASTCallbackVHDenseMapInfo - Traits to tell DenseMap that tell us how to
300 /// compare and hash the value handle.
301 struct ASTCallbackVHDenseMapInfo : public DenseMapInfo<Value *> {};
304 ilist<AliasSet> AliasSets;
306 typedef DenseMap<ASTCallbackVH, AliasSet::PointerRec*,
307 ASTCallbackVHDenseMapInfo>
310 // Map from pointers to their node
311 PointerMapType PointerMap;
314 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
315 /// the specified alias analysis object to disambiguate load and store
317 explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
318 ~AliasSetTracker() { clear(); }
320 /// add methods - These methods are used to add different types of
321 /// instructions to the alias sets. Adding a new instruction can result in
322 /// one of three actions happening:
324 /// 1. If the instruction doesn't alias any other sets, create a new set.
325 /// 2. If the instruction aliases exactly one set, add it to the set
326 /// 3. If the instruction aliases multiple sets, merge the sets, and add
327 /// the instruction to the result.
329 /// These methods return true if inserting the instruction resulted in the
330 /// addition of a new alias set (i.e., the pointer did not alias anything).
332 bool add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo); // Add a loc.
333 bool add(LoadInst *LI);
334 bool add(StoreInst *SI);
335 bool add(VAArgInst *VAAI);
336 bool add(Instruction *I); // Dispatch to one of the other add methods...
337 void add(BasicBlock &BB); // Add all instructions in basic block
338 void add(const AliasSetTracker &AST); // Add alias relations from another AST
339 bool addUnknown(Instruction *I);
341 /// remove methods - These methods are used to remove all entries that might
342 /// be aliased by the specified instruction. These methods return true if any
343 /// alias sets were eliminated.
345 bool remove(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo);
346 bool remove(LoadInst *LI);
347 bool remove(StoreInst *SI);
348 bool remove(VAArgInst *VAAI);
349 bool remove(Instruction *I);
350 void remove(AliasSet &AS);
351 bool removeUnknown(Instruction *I);
355 /// getAliasSets - Return the alias sets that are active.
357 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
359 /// getAliasSetForPointer - Return the alias set that the specified pointer
360 /// lives in. If the New argument is non-null, this method sets the value to
361 /// true if a new alias set is created to contain the pointer (because the
362 /// pointer didn't alias anything).
363 AliasSet &getAliasSetForPointer(Value *P, uint64_t Size,
364 const AAMDNodes &AAInfo,
365 bool *New = nullptr);
367 /// getAliasSetForPointerIfExists - Return the alias set containing the
368 /// location specified if one exists, otherwise return null.
369 AliasSet *getAliasSetForPointerIfExists(const Value *P, uint64_t Size,
370 const AAMDNodes &AAInfo) {
371 return findAliasSetForPointer(P, Size, AAInfo);
374 /// containsPointer - Return true if the specified location is represented by
375 /// this alias set, false otherwise. This does not modify the AST object or
377 bool containsPointer(const Value *P, uint64_t Size,
378 const AAMDNodes &AAInfo) const;
380 /// Return true if the specified instruction "may" (or must) alias one of the
381 /// members in any of the sets.
382 bool containsUnknown(const Instruction *I) const;
384 /// getAliasAnalysis - Return the underlying alias analysis object used by
386 AliasAnalysis &getAliasAnalysis() const { return AA; }
388 /// deleteValue method - This method is used to remove a pointer value from
389 /// the AliasSetTracker entirely. It should be used when an instruction is
390 /// deleted from the program to update the AST. If you don't use this, you
391 /// would have dangling pointers to deleted instructions.
393 void deleteValue(Value *PtrVal);
395 /// copyValue - This method should be used whenever a preexisting value in the
396 /// program is copied or cloned, introducing a new value. Note that it is ok
397 /// for clients that use this method to introduce the same value multiple
398 /// times: if the tracker already knows about a value, it will ignore the
401 void copyValue(Value *From, Value *To);
403 typedef ilist<AliasSet>::iterator iterator;
404 typedef ilist<AliasSet>::const_iterator const_iterator;
406 const_iterator begin() const { return AliasSets.begin(); }
407 const_iterator end() const { return AliasSets.end(); }
409 iterator begin() { return AliasSets.begin(); }
410 iterator end() { return AliasSets.end(); }
412 void print(raw_ostream &OS) const;
416 friend class AliasSet;
417 void removeAliasSet(AliasSet *AS);
419 // getEntryFor - Just like operator[] on the map, except that it creates an
420 // entry for the pointer if it doesn't already exist.
421 AliasSet::PointerRec &getEntryFor(Value *V) {
422 AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)];
424 Entry = new AliasSet::PointerRec(V);
428 AliasSet &addPointer(Value *P, uint64_t Size, const AAMDNodes &AAInfo,
429 AliasSet::AccessLattice E,
432 AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo, &NewSet);
436 AliasSet *findAliasSetForPointer(const Value *Ptr, uint64_t Size,
437 const AAMDNodes &AAInfo);
439 AliasSet *findAliasSetForUnknownInst(Instruction *Inst);
442 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) {
447 } // End llvm namespace