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 interfaces
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/DenseMapInfo.h"
22 #include "llvm/ADT/ilist.h"
23 #include "llvm/ADT/ilist_node.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/IR/Instruction.h"
26 #include "llvm/IR/Metadata.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include "llvm/Support/Casting.h"
37 class AliasSetTracker;
41 class MemTransferInst;
47 class AliasSet : public ilist_node<AliasSet> {
48 friend class AliasSetTracker;
51 Value *Val; // The pointer this record corresponds to.
52 PointerRec **PrevInList = nullptr;
53 PointerRec *NextInList = nullptr;
54 AliasSet *AS = nullptr;
60 : Val(V), AAInfo(DenseMapInfo<AAMDNodes>::getEmptyKey()) {}
62 Value *getValue() const { return Val; }
64 PointerRec *getNext() const { return NextInList; }
65 bool hasAliasSet() const { return AS != nullptr; }
67 PointerRec** setPrevInList(PointerRec **PIL) {
72 bool updateSizeAndAAInfo(uint64_t NewSize, const AAMDNodes &NewAAInfo) {
73 bool SizeChanged = false;
79 if (AAInfo == DenseMapInfo<AAMDNodes>::getEmptyKey())
80 // We don't have a AAInfo yet. Set it to NewAAInfo.
83 AAMDNodes Intersection(AAInfo.intersect(NewAAInfo));
85 // NewAAInfo conflicts with AAInfo.
86 AAInfo = DenseMapInfo<AAMDNodes>::getTombstoneKey();
89 AAInfo = Intersection;
94 uint64_t getSize() const { return Size; }
96 /// Return the AAInfo, or null if there is no information or conflicting
98 AAMDNodes getAAInfo() const {
99 // If we have missing or conflicting AAInfo, return null.
100 if (AAInfo == DenseMapInfo<AAMDNodes>::getEmptyKey() ||
101 AAInfo == DenseMapInfo<AAMDNodes>::getTombstoneKey())
106 AliasSet *getAliasSet(AliasSetTracker &AST) {
107 assert(AS && "No AliasSet yet!");
109 AliasSet *OldAS = AS;
110 AS = OldAS->getForwardedTarget(AST);
117 void setAliasSet(AliasSet *as) {
118 assert(!AS && "Already have an alias set!");
122 void eraseFromList() {
123 if (NextInList) NextInList->PrevInList = PrevInList;
124 *PrevInList = NextInList;
125 if (AS->PtrListEnd == &NextInList) {
126 AS->PtrListEnd = PrevInList;
127 assert(*AS->PtrListEnd == nullptr && "List not terminated right!");
133 // Doubly linked list of nodes.
134 PointerRec *PtrList = nullptr;
135 PointerRec **PtrListEnd;
136 // Forwarding pointer.
137 AliasSet *Forward = nullptr;
139 /// All instructions without a specific address in this alias set.
140 /// In rare cases this vector can have a null'ed out WeakVH
141 /// instances (can happen if some other loop pass deletes an
142 /// instruction in this list).
143 std::vector<WeakVH> UnknownInsts;
145 /// Number of nodes pointing to this AliasSet plus the number of AliasSets
146 /// forwarding to it.
147 unsigned RefCount : 27;
149 // Signifies that this set should be considered to alias any pointer.
150 // Use when the tracker holding this set is saturated.
151 unsigned AliasAny : 1;
153 /// The kinds of access this alias set models.
155 /// We keep track of whether this alias set merely refers to the locations of
156 /// memory (and not any particular access), whether it modifies or references
157 /// the memory, or whether it does both. The lattice goes from "NoAccess" to
158 /// either RefAccess or ModAccess, then to ModRefAccess as necessary.
163 ModRefAccess = RefAccess | ModAccess
167 /// The kind of alias relationship between pointers of the set.
169 /// These represent conservatively correct alias results between any members
170 /// of the set. We represent these independently of the values of alias
171 /// results in order to pack it into a single bit. Lattice goes from
172 /// MustAlias to MayAlias.
174 SetMustAlias = 0, SetMayAlias = 1
178 /// True if this alias set contains volatile loads or stores.
179 unsigned Volatile : 1;
181 unsigned SetSize = 0;
183 void addRef() { ++RefCount; }
185 void dropRef(AliasSetTracker &AST) {
186 assert(RefCount >= 1 && "Invalid reference count detected!");
188 removeFromTracker(AST);
191 Instruction *getUnknownInst(unsigned i) const {
192 assert(i < UnknownInsts.size());
193 return cast_or_null<Instruction>(UnknownInsts[i]);
197 AliasSet(const AliasSet &) = delete;
198 AliasSet &operator=(const AliasSet &) = delete;
201 bool isRef() const { return Access & RefAccess; }
202 bool isMod() const { return Access & ModAccess; }
203 bool isMustAlias() const { return Alias == SetMustAlias; }
204 bool isMayAlias() const { return Alias == SetMayAlias; }
206 /// Return true if this alias set contains volatile loads or stores.
207 bool isVolatile() const { return Volatile; }
209 /// Return true if this alias set should be ignored as part of the
210 /// AliasSetTracker object.
211 bool isForwardingAliasSet() const { return Forward; }
213 /// Merge the specified alias set into this alias set.
214 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
216 // Alias Set iteration - Allow access to all of the pointers which are part of
219 iterator begin() const { return iterator(PtrList); }
220 iterator end() const { return iterator(); }
221 bool empty() const { return PtrList == nullptr; }
223 // Unfortunately, ilist::size() is linear, so we have to add code to keep
224 // track of the list's exact size.
225 unsigned size() { return SetSize; }
227 void print(raw_ostream &OS) const;
230 /// Define an iterator for alias sets... this is just a forward iterator.
231 class iterator : public std::iterator<std::forward_iterator_tag,
232 PointerRec, ptrdiff_t> {
236 explicit iterator(PointerRec *CN = nullptr) : CurNode(CN) {}
238 bool operator==(const iterator& x) const {
239 return CurNode == x.CurNode;
241 bool operator!=(const iterator& x) const { return !operator==(x); }
243 value_type &operator*() const {
244 assert(CurNode && "Dereferencing AliasSet.end()!");
247 value_type *operator->() const { return &operator*(); }
249 Value *getPointer() const { return CurNode->getValue(); }
250 uint64_t getSize() const { return CurNode->getSize(); }
251 AAMDNodes getAAInfo() const { return CurNode->getAAInfo(); }
253 iterator& operator++() { // Preincrement
254 assert(CurNode && "Advancing past AliasSet.end()!");
255 CurNode = CurNode->getNext();
258 iterator operator++(int) { // Postincrement
259 iterator tmp = *this; ++*this; return tmp;
264 // Can only be created by AliasSetTracker.
266 : PtrListEnd(&PtrList), RefCount(0), AliasAny(false), Access(NoAccess),
267 Alias(SetMustAlias), Volatile(false) {}
269 PointerRec *getSomePointer() const {
273 /// Return the real alias set this represents. If this has been merged with
274 /// another set and is forwarding, return the ultimate destination set. This
275 /// also implements the union-find collapsing as well.
276 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
277 if (!Forward) return this;
279 AliasSet *Dest = Forward->getForwardedTarget(AST);
280 if (Dest != Forward) {
282 Forward->dropRef(AST);
288 void removeFromTracker(AliasSetTracker &AST);
290 void addPointer(AliasSetTracker &AST, PointerRec &Entry, uint64_t Size,
291 const AAMDNodes &AAInfo,
292 bool KnownMustAlias = false);
293 void addUnknownInst(Instruction *I, AliasAnalysis &AA);
295 void removeUnknownInst(AliasSetTracker &AST, Instruction *I) {
296 bool WasEmpty = UnknownInsts.empty();
297 for (size_t i = 0, e = UnknownInsts.size(); i != e; ++i)
298 if (UnknownInsts[i] == I) {
299 UnknownInsts[i] = UnknownInsts.back();
300 UnknownInsts.pop_back();
301 --i; --e; // Revisit the moved entry.
303 if (!WasEmpty && UnknownInsts.empty())
307 void setVolatile() { Volatile = true; }
310 /// Return true if the specified pointer "may" (or must) alias one of the
311 /// members in the set.
312 bool aliasesPointer(const Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo,
313 AliasAnalysis &AA) const;
314 bool aliasesUnknownInst(const Instruction *Inst, AliasAnalysis &AA) const;
317 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) {
322 class AliasSetTracker {
323 /// A CallbackVH to arrange for AliasSetTracker to be notified whenever a
324 /// Value is deleted.
325 class ASTCallbackVH final : public CallbackVH {
326 AliasSetTracker *AST;
328 void deleted() override;
329 void allUsesReplacedWith(Value *) override;
332 ASTCallbackVH(Value *V, AliasSetTracker *AST = nullptr);
334 ASTCallbackVH &operator=(Value *V);
336 /// Traits to tell DenseMap that tell us how to compare and hash the value
338 struct ASTCallbackVHDenseMapInfo : public DenseMapInfo<Value *> {};
341 ilist<AliasSet> AliasSets;
343 using PointerMapType = DenseMap<ASTCallbackVH, AliasSet::PointerRec *,
344 ASTCallbackVHDenseMapInfo>;
346 // Map from pointers to their node
347 PointerMapType PointerMap;
350 /// Create an empty collection of AliasSets, and use the specified alias
351 /// analysis object to disambiguate load and store addresses.
352 explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
353 ~AliasSetTracker() { clear(); }
355 /// These methods are used to add different types of instructions to the alias
356 /// sets. Adding a new instruction can result in one of three actions
359 /// 1. If the instruction doesn't alias any other sets, create a new set.
360 /// 2. If the instruction aliases exactly one set, add it to the set
361 /// 3. If the instruction aliases multiple sets, merge the sets, and add
362 /// the instruction to the result.
364 /// These methods return true if inserting the instruction resulted in the
365 /// addition of a new alias set (i.e., the pointer did not alias anything).
367 void add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo); // Add a loc.
368 void add(LoadInst *LI);
369 void add(StoreInst *SI);
370 void add(VAArgInst *VAAI);
371 void add(MemSetInst *MSI);
372 void add(MemTransferInst *MTI);
373 void add(Instruction *I); // Dispatch to one of the other add methods...
374 void add(BasicBlock &BB); // Add all instructions in basic block
375 void add(const AliasSetTracker &AST); // Add alias relations from another AST
376 void addUnknown(Instruction *I);
380 /// Return the alias sets that are active.
381 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
383 /// Return the alias set that the specified pointer lives in. If the New
384 /// argument is non-null, this method sets the value to true if a new alias
385 /// set is created to contain the pointer (because the pointer didn't alias
387 AliasSet &getAliasSetForPointer(Value *P, uint64_t Size,
388 const AAMDNodes &AAInfo);
390 /// Return the alias set containing the location specified if one exists,
391 /// otherwise return null.
392 AliasSet *getAliasSetForPointerIfExists(const Value *P, uint64_t Size,
393 const AAMDNodes &AAInfo) {
394 return mergeAliasSetsForPointer(P, Size, AAInfo);
397 /// Return true if the specified instruction "may" (or must) alias one of the
398 /// members in any of the sets.
399 bool containsUnknown(const Instruction *I) const;
401 /// Return the underlying alias analysis object used by this tracker.
402 AliasAnalysis &getAliasAnalysis() const { return AA; }
404 /// This method is used to remove a pointer value from the AliasSetTracker
405 /// entirely. It should be used when an instruction is deleted from the
406 /// program to update the AST. If you don't use this, you would have dangling
407 /// pointers to deleted instructions.
408 void deleteValue(Value *PtrVal);
410 /// This method should be used whenever a preexisting value in the program is
411 /// copied or cloned, introducing a new value. Note that it is ok for clients
412 /// that use this method to introduce the same value multiple times: if the
413 /// tracker already knows about a value, it will ignore the request.
414 void copyValue(Value *From, Value *To);
416 using iterator = ilist<AliasSet>::iterator;
417 using const_iterator = ilist<AliasSet>::const_iterator;
419 const_iterator begin() const { return AliasSets.begin(); }
420 const_iterator end() const { return AliasSets.end(); }
422 iterator begin() { return AliasSets.begin(); }
423 iterator end() { return AliasSets.end(); }
425 void print(raw_ostream &OS) const;
429 friend class AliasSet;
431 // The total number of pointers contained in all "may" alias sets.
432 unsigned TotalMayAliasSetSize = 0;
434 // A non-null value signifies this AST is saturated. A saturated AST lumps
435 // all pointers into a single "May" set.
436 AliasSet *AliasAnyAS = nullptr;
438 void removeAliasSet(AliasSet *AS);
440 /// Just like operator[] on the map, except that it creates an entry for the
441 /// pointer if it doesn't already exist.
442 AliasSet::PointerRec &getEntryFor(Value *V) {
443 AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)];
445 Entry = new AliasSet::PointerRec(V);
449 AliasSet &addPointer(Value *P, uint64_t Size, const AAMDNodes &AAInfo,
450 AliasSet::AccessLattice E);
451 AliasSet *mergeAliasSetsForPointer(const Value *Ptr, uint64_t Size,
452 const AAMDNodes &AAInfo);
454 /// Merge all alias sets into a single set that is considered to alias any
456 AliasSet &mergeAllAliasSets();
458 AliasSet *findAliasSetForUnknownInst(Instruction *Inst);
461 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) {
466 } // end namespace llvm
468 #endif // LLVM_ANALYSIS_ALIASSETTRACKER_H