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/IR/Metadata.h"
24 #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;
47 : Val(V), PrevInList(nullptr), NextInList(nullptr), AS(nullptr), Size(0),
48 AAInfo(DenseMapInfo<AAMDNodes>::getEmptyKey()) {}
50 Value *getValue() const { return Val; }
52 PointerRec *getNext() const { return NextInList; }
53 bool hasAliasSet() const { return AS != nullptr; }
55 PointerRec** setPrevInList(PointerRec **PIL) {
60 void updateSizeAndAAInfo(uint64_t NewSize, const AAMDNodes &NewAAInfo) {
61 if (NewSize > Size) Size = NewSize;
63 if (AAInfo == DenseMapInfo<AAMDNodes>::getEmptyKey())
64 // We don't have a AAInfo yet. Set it to NewAAInfo.
66 else if (AAInfo != NewAAInfo)
67 // NewAAInfo conflicts with AAInfo.
68 AAInfo = DenseMapInfo<AAMDNodes>::getTombstoneKey();
71 uint64_t getSize() const { return Size; }
73 /// getAAInfo - Return the AAInfo, or null if there is no
74 /// information or conflicting information.
75 AAMDNodes getAAInfo() const {
76 // If we have missing or conflicting AAInfo, return null.
77 if (AAInfo == DenseMapInfo<AAMDNodes>::getEmptyKey() ||
78 AAInfo == DenseMapInfo<AAMDNodes>::getTombstoneKey())
83 AliasSet *getAliasSet(AliasSetTracker &AST) {
84 assert(AS && "No AliasSet yet!");
87 AS = OldAS->getForwardedTarget(AST);
94 void setAliasSet(AliasSet *as) {
95 assert(!AS && "Already have an alias set!");
99 void eraseFromList() {
100 if (NextInList) NextInList->PrevInList = PrevInList;
101 *PrevInList = NextInList;
102 if (AS->PtrListEnd == &NextInList) {
103 AS->PtrListEnd = PrevInList;
104 assert(*AS->PtrListEnd == nullptr && "List not terminated right!");
110 PointerRec *PtrList, **PtrListEnd; // Doubly linked list of nodes.
111 AliasSet *Forward; // Forwarding pointer.
113 // All instructions without a specific address in this alias set.
114 std::vector<AssertingVH<Instruction> > UnknownInsts;
116 // RefCount - Number of nodes pointing to this AliasSet plus the number of
117 // AliasSets forwarding to it.
118 unsigned RefCount : 28;
120 /// AccessType - Keep track of whether this alias set merely refers to the
121 /// locations of memory, whether it modifies the memory, or whether it does
122 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
123 /// ModRef as necessary.
126 NoModRef = 0, Refs = 1, // Ref = bit 1
127 Mods = 2, ModRef = 3 // Mod = bit 2
129 unsigned AccessTy : 2;
131 /// AliasType - Keep track the relationships between the pointers in the set.
132 /// Lattice goes from MustAlias to MayAlias.
135 MustAlias = 0, MayAlias = 1
137 unsigned AliasTy : 1;
139 // Volatile - True if this alias set contains volatile loads or stores.
142 void addRef() { ++RefCount; }
143 void dropRef(AliasSetTracker &AST) {
144 assert(RefCount >= 1 && "Invalid reference count detected!");
146 removeFromTracker(AST);
149 Instruction *getUnknownInst(unsigned i) const {
150 assert(i < UnknownInsts.size());
151 return UnknownInsts[i];
156 bool isRef() const { return AccessTy & Refs; }
157 bool isMod() const { return AccessTy & Mods; }
158 bool isMustAlias() const { return AliasTy == MustAlias; }
159 bool isMayAlias() const { return AliasTy == MayAlias; }
161 // isVolatile - Return true if this alias set contains volatile loads or
163 bool isVolatile() const { return Volatile; }
165 /// isForwardingAliasSet - Return true if this alias set should be ignored as
166 /// part of the AliasSetTracker object.
167 bool isForwardingAliasSet() const { return Forward; }
169 /// mergeSetIn - Merge the specified alias set into this alias set...
171 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
173 // Alias Set iteration - Allow access to all of the pointer which are part of
176 iterator begin() const { return iterator(PtrList); }
177 iterator end() const { return iterator(); }
178 bool empty() const { return PtrList == nullptr; }
180 void print(raw_ostream &OS) const;
183 /// Define an iterator for alias sets... this is just a forward iterator.
184 class iterator : public std::iterator<std::forward_iterator_tag,
185 PointerRec, ptrdiff_t> {
188 explicit iterator(PointerRec *CN = nullptr) : CurNode(CN) {}
190 bool operator==(const iterator& x) const {
191 return CurNode == x.CurNode;
193 bool operator!=(const iterator& x) const { return !operator==(x); }
195 value_type &operator*() const {
196 assert(CurNode && "Dereferencing AliasSet.end()!");
199 value_type *operator->() const { return &operator*(); }
201 Value *getPointer() const { return CurNode->getValue(); }
202 uint64_t getSize() const { return CurNode->getSize(); }
203 AAMDNodes getAAInfo() const { return CurNode->getAAInfo(); }
205 iterator& operator++() { // Preincrement
206 assert(CurNode && "Advancing past AliasSet.end()!");
207 CurNode = CurNode->getNext();
210 iterator operator++(int) { // Postincrement
211 iterator tmp = *this; ++*this; return tmp;
216 // Can only be created by AliasSetTracker. Also, ilist creates one
217 // to serve as a sentinel.
218 friend struct ilist_sentinel_traits<AliasSet>;
220 : PtrList(nullptr), PtrListEnd(&PtrList), Forward(nullptr), RefCount(0),
221 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
224 AliasSet(const AliasSet &AS) = delete;
225 void operator=(const AliasSet &AS) = delete;
227 PointerRec *getSomePointer() const {
231 /// getForwardedTarget - Return the real alias set this represents. If this
232 /// has been merged with another set and is forwarding, return the ultimate
233 /// destination set. This also implements the union-find collapsing as well.
234 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
235 if (!Forward) return this;
237 AliasSet *Dest = Forward->getForwardedTarget(AST);
238 if (Dest != Forward) {
240 Forward->dropRef(AST);
246 void removeFromTracker(AliasSetTracker &AST);
248 void addPointer(AliasSetTracker &AST, PointerRec &Entry, uint64_t Size,
249 const AAMDNodes &AAInfo,
250 bool KnownMustAlias = false);
251 void addUnknownInst(Instruction *I, AliasAnalysis &AA);
252 void removeUnknownInst(AliasSetTracker &AST, Instruction *I) {
253 bool WasEmpty = UnknownInsts.empty();
254 for (size_t i = 0, e = UnknownInsts.size(); i != e; ++i)
255 if (UnknownInsts[i] == I) {
256 UnknownInsts[i] = UnknownInsts.back();
257 UnknownInsts.pop_back();
258 --i; --e; // Revisit the moved entry.
260 if (!WasEmpty && UnknownInsts.empty())
263 void setVolatile() { Volatile = true; }
266 /// aliasesPointer - Return true if the specified pointer "may" (or must)
267 /// alias one of the members in the set.
269 bool aliasesPointer(const Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo,
270 AliasAnalysis &AA) const;
271 bool aliasesUnknownInst(Instruction *Inst, AliasAnalysis &AA) const;
274 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) {
280 class AliasSetTracker {
281 /// CallbackVH - A CallbackVH to arrange for AliasSetTracker to be
282 /// notified whenever a Value is deleted.
283 class ASTCallbackVH : public CallbackVH {
284 AliasSetTracker *AST;
285 void deleted() override;
286 void allUsesReplacedWith(Value *) override;
288 ASTCallbackVH(Value *V, AliasSetTracker *AST = nullptr);
289 ASTCallbackVH &operator=(Value *V);
291 /// ASTCallbackVHDenseMapInfo - Traits to tell DenseMap that tell us how to
292 /// compare and hash the value handle.
293 struct ASTCallbackVHDenseMapInfo : public DenseMapInfo<Value *> {};
296 ilist<AliasSet> AliasSets;
298 typedef DenseMap<ASTCallbackVH, AliasSet::PointerRec*,
299 ASTCallbackVHDenseMapInfo>
302 // Map from pointers to their node
303 PointerMapType PointerMap;
306 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
307 /// the specified alias analysis object to disambiguate load and store
309 explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
310 ~AliasSetTracker() { clear(); }
312 /// add methods - These methods are used to add different types of
313 /// instructions to the alias sets. Adding a new instruction can result in
314 /// one of three actions happening:
316 /// 1. If the instruction doesn't alias any other sets, create a new set.
317 /// 2. If the instruction aliases exactly one set, add it to the set
318 /// 3. If the instruction aliases multiple sets, merge the sets, and add
319 /// the instruction to the result.
321 /// These methods return true if inserting the instruction resulted in the
322 /// addition of a new alias set (i.e., the pointer did not alias anything).
324 bool add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo); // Add a loc.
325 bool add(LoadInst *LI);
326 bool add(StoreInst *SI);
327 bool add(VAArgInst *VAAI);
328 bool add(Instruction *I); // Dispatch to one of the other add methods...
329 void add(BasicBlock &BB); // Add all instructions in basic block
330 void add(const AliasSetTracker &AST); // Add alias relations from another AST
331 bool addUnknown(Instruction *I);
333 /// remove methods - These methods are used to remove all entries that might
334 /// be aliased by the specified instruction. These methods return true if any
335 /// alias sets were eliminated.
337 bool remove(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo);
338 bool remove(LoadInst *LI);
339 bool remove(StoreInst *SI);
340 bool remove(VAArgInst *VAAI);
341 bool remove(Instruction *I);
342 void remove(AliasSet &AS);
343 bool removeUnknown(Instruction *I);
347 /// getAliasSets - Return the alias sets that are active.
349 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
351 /// getAliasSetForPointer - Return the alias set that the specified pointer
352 /// lives in. If the New argument is non-null, this method sets the value to
353 /// true if a new alias set is created to contain the pointer (because the
354 /// pointer didn't alias anything).
355 AliasSet &getAliasSetForPointer(Value *P, uint64_t Size,
356 const AAMDNodes &AAInfo,
357 bool *New = nullptr);
359 /// getAliasSetForPointerIfExists - Return the alias set containing the
360 /// location specified if one exists, otherwise return null.
361 AliasSet *getAliasSetForPointerIfExists(Value *P, uint64_t Size,
362 const AAMDNodes &AAInfo) {
363 return findAliasSetForPointer(P, Size, AAInfo);
366 /// containsPointer - Return true if the specified location is represented by
367 /// this alias set, false otherwise. This does not modify the AST object or
369 bool containsPointer(Value *P, uint64_t Size, const AAMDNodes &AAInfo) const;
371 /// Return true if the specified instruction "may" (or must) alias one of the
372 /// members in any of the sets.
373 bool containsUnknown(Instruction *I) const;
375 /// getAliasAnalysis - Return the underlying alias analysis object used by
377 AliasAnalysis &getAliasAnalysis() const { return AA; }
379 /// deleteValue method - This method is used to remove a pointer value from
380 /// the AliasSetTracker entirely. It should be used when an instruction is
381 /// deleted from the program to update the AST. If you don't use this, you
382 /// would have dangling pointers to deleted instructions.
384 void deleteValue(Value *PtrVal);
386 /// copyValue - This method should be used whenever a preexisting value in the
387 /// program is copied or cloned, introducing a new value. Note that it is ok
388 /// for clients that use this method to introduce the same value multiple
389 /// times: if the tracker already knows about a value, it will ignore the
392 void copyValue(Value *From, Value *To);
395 typedef ilist<AliasSet>::iterator iterator;
396 typedef ilist<AliasSet>::const_iterator const_iterator;
398 const_iterator begin() const { return AliasSets.begin(); }
399 const_iterator end() const { return AliasSets.end(); }
401 iterator begin() { return AliasSets.begin(); }
402 iterator end() { return AliasSets.end(); }
404 void print(raw_ostream &OS) const;
408 friend class AliasSet;
409 void removeAliasSet(AliasSet *AS);
411 // getEntryFor - Just like operator[] on the map, except that it creates an
412 // entry for the pointer if it doesn't already exist.
413 AliasSet::PointerRec &getEntryFor(Value *V) {
414 AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)];
416 Entry = new AliasSet::PointerRec(V);
420 AliasSet &addPointer(Value *P, uint64_t Size, const AAMDNodes &AAInfo,
421 AliasSet::AccessType E,
424 AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo, &NewSet);
428 AliasSet *findAliasSetForPointer(const Value *Ptr, uint64_t Size,
429 const AAMDNodes &AAInfo);
431 AliasSet *findAliasSetForUnknownInst(Instruction *Inst);
434 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) {
439 } // End llvm namespace