1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the AliasSetTracker and AliasSet classes.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/AliasSetTracker.h"
14 #include "llvm/Analysis/AliasAnalysis.h"
15 #include "llvm/Analysis/GuardUtils.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/MemoryLocation.h"
18 #include "llvm/Config/llvm-config.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/InstIterator.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/PassManager.h"
27 #include "llvm/IR/PatternMatch.h"
28 #include "llvm/IR/Value.h"
29 #include "llvm/InitializePasses.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/AtomicOrdering.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
40 static cl::opt
<unsigned>
41 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden
,
43 cl::desc("The maximum number of pointers may-alias "
44 "sets may contain before degradation"));
46 /// mergeSetIn - Merge the specified alias set into this alias set.
48 void AliasSet::mergeSetIn(AliasSet
&AS
, AliasSetTracker
&AST
) {
49 assert(!AS
.Forward
&& "Alias set is already forwarding!");
50 assert(!Forward
&& "This set is a forwarding set!!");
52 bool WasMustAlias
= (Alias
== SetMustAlias
);
53 // Update the alias and access types of this set...
57 if (Alias
== SetMustAlias
) {
58 // Check that these two merged sets really are must aliases. Since both
59 // used to be must-alias sets, we can just check any pointer from each set
61 AliasAnalysis
&AA
= AST
.getAliasAnalysis();
62 PointerRec
*L
= getSomePointer();
63 PointerRec
*R
= AS
.getSomePointer();
65 // If the pointers are not a must-alias pair, this set becomes a may alias.
67 MemoryLocation(L
->getValue(), L
->getSize(), L
->getAAInfo()),
68 MemoryLocation(R
->getValue(), R
->getSize(), R
->getAAInfo())))
72 if (Alias
== SetMayAlias
) {
74 AST
.TotalMayAliasSetSize
+= size();
75 if (AS
.Alias
== SetMustAlias
)
76 AST
.TotalMayAliasSetSize
+= AS
.size();
79 bool ASHadUnknownInsts
= !AS
.UnknownInsts
.empty();
80 if (UnknownInsts
.empty()) { // Merge call sites...
81 if (ASHadUnknownInsts
) {
82 std::swap(UnknownInsts
, AS
.UnknownInsts
);
85 } else if (ASHadUnknownInsts
) {
86 llvm::append_range(UnknownInsts
, AS
.UnknownInsts
);
87 AS
.UnknownInsts
.clear();
90 AS
.Forward
= this; // Forward across AS now...
91 addRef(); // AS is now pointing to us...
93 // Merge the list of constituent pointers...
97 *PtrListEnd
= AS
.PtrList
;
98 AS
.PtrList
->setPrevInList(PtrListEnd
);
99 PtrListEnd
= AS
.PtrListEnd
;
101 AS
.PtrList
= nullptr;
102 AS
.PtrListEnd
= &AS
.PtrList
;
103 assert(*AS
.PtrListEnd
== nullptr && "End of list is not null?");
105 if (ASHadUnknownInsts
)
109 void AliasSetTracker::removeAliasSet(AliasSet
*AS
) {
110 if (AliasSet
*Fwd
= AS
->Forward
) {
112 AS
->Forward
= nullptr;
113 } else // Update TotalMayAliasSetSize only if not forwarding.
114 if (AS
->Alias
== AliasSet::SetMayAlias
)
115 TotalMayAliasSetSize
-= AS
->size();
118 // If we've removed the saturated alias set, set saturated marker back to
119 // nullptr and ensure this tracker is empty.
120 if (AS
== AliasAnyAS
) {
121 AliasAnyAS
= nullptr;
122 assert(AliasSets
.empty() && "Tracker not empty");
126 void AliasSet::removeFromTracker(AliasSetTracker
&AST
) {
127 assert(RefCount
== 0 && "Cannot remove non-dead alias set from tracker!");
128 AST
.removeAliasSet(this);
131 void AliasSet::addPointer(AliasSetTracker
&AST
, PointerRec
&Entry
,
132 LocationSize Size
, const AAMDNodes
&AAInfo
,
133 bool KnownMustAlias
, bool SkipSizeUpdate
) {
134 assert(!Entry
.hasAliasSet() && "Entry already in set!");
136 // Check to see if we have to downgrade to _may_ alias.
138 if (PointerRec
*P
= getSomePointer()) {
139 if (!KnownMustAlias
) {
140 AliasAnalysis
&AA
= AST
.getAliasAnalysis();
141 AliasResult Result
= AA
.alias(
142 MemoryLocation(P
->getValue(), P
->getSize(), P
->getAAInfo()),
143 MemoryLocation(Entry
.getValue(), Size
, AAInfo
));
144 if (Result
!= AliasResult::MustAlias
) {
146 AST
.TotalMayAliasSetSize
+= size();
148 assert(Result
!= AliasResult::NoAlias
&& "Cannot be part of must set!");
149 } else if (!SkipSizeUpdate
)
150 P
->updateSizeAndAAInfo(Size
, AAInfo
);
153 Entry
.setAliasSet(this);
154 Entry
.updateSizeAndAAInfo(Size
, AAInfo
);
156 // Add it to the end of the list...
158 assert(*PtrListEnd
== nullptr && "End of list is not null?");
159 *PtrListEnd
= &Entry
;
160 PtrListEnd
= Entry
.setPrevInList(PtrListEnd
);
161 assert(*PtrListEnd
== nullptr && "End of list is not null?");
162 // Entry points to alias set.
165 if (Alias
== SetMayAlias
)
166 AST
.TotalMayAliasSetSize
++;
169 void AliasSet::addUnknownInst(Instruction
*I
, AliasAnalysis
&AA
) {
170 if (UnknownInsts
.empty())
172 UnknownInsts
.emplace_back(I
);
174 // Guards are marked as modifying memory for control flow modelling purposes,
175 // but don't actually modify any specific memory location.
176 using namespace PatternMatch
;
177 bool MayWriteMemory
= I
->mayWriteToMemory() && !isGuard(I
) &&
178 !(I
->use_empty() && match(I
, m_Intrinsic
<Intrinsic::invariant_start
>()));
179 if (!MayWriteMemory
) {
185 // FIXME: This should use mod/ref information to make this not suck so bad
187 Access
= ModRefAccess
;
190 /// aliasesPointer - If the specified pointer "may" (or must) alias one of the
191 /// members in the set return the appropriate AliasResult. Otherwise return
194 AliasResult
AliasSet::aliasesPointer(const Value
*Ptr
, LocationSize Size
,
195 const AAMDNodes
&AAInfo
,
196 AliasAnalysis
&AA
) const {
198 return AliasResult::MayAlias
;
200 if (Alias
== SetMustAlias
) {
201 assert(UnknownInsts
.empty() && "Illegal must alias set!");
203 // If this is a set of MustAliases, only check to see if the pointer aliases
204 // SOME value in the set.
205 PointerRec
*SomePtr
= getSomePointer();
206 assert(SomePtr
&& "Empty must-alias set??");
207 return AA
.alias(MemoryLocation(SomePtr
->getValue(), SomePtr
->getSize(),
208 SomePtr
->getAAInfo()),
209 MemoryLocation(Ptr
, Size
, AAInfo
));
212 // If this is a may-alias set, we have to check all of the pointers in the set
213 // to be sure it doesn't alias the set...
214 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
216 AA
.alias(MemoryLocation(Ptr
, Size
, AAInfo
),
217 MemoryLocation(I
.getPointer(), I
.getSize(), I
.getAAInfo()));
218 if (AR
!= AliasResult::NoAlias
)
222 // Check the unknown instructions...
223 if (!UnknownInsts
.empty()) {
224 for (unsigned i
= 0, e
= UnknownInsts
.size(); i
!= e
; ++i
)
225 if (auto *Inst
= getUnknownInst(i
))
227 AA
.getModRefInfo(Inst
, MemoryLocation(Ptr
, Size
, AAInfo
))))
228 return AliasResult::MayAlias
;
231 return AliasResult::NoAlias
;
234 bool AliasSet::aliasesUnknownInst(const Instruction
*Inst
,
235 AliasAnalysis
&AA
) const {
240 assert(Inst
->mayReadOrWriteMemory() &&
241 "Instruction must either read or write memory.");
243 for (unsigned i
= 0, e
= UnknownInsts
.size(); i
!= e
; ++i
) {
244 if (auto *UnknownInst
= getUnknownInst(i
)) {
245 const auto *C1
= dyn_cast
<CallBase
>(UnknownInst
);
246 const auto *C2
= dyn_cast
<CallBase
>(Inst
);
247 if (!C1
|| !C2
|| isModOrRefSet(AA
.getModRefInfo(C1
, C2
)) ||
248 isModOrRefSet(AA
.getModRefInfo(C2
, C1
)))
253 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
)
254 if (isModOrRefSet(AA
.getModRefInfo(
255 Inst
, MemoryLocation(I
.getPointer(), I
.getSize(), I
.getAAInfo()))))
261 Instruction
* AliasSet::getUniqueInstruction() {
263 // May have collapses alias set
265 if (begin() != end()) {
266 if (!UnknownInsts
.empty())
267 // Another instruction found
269 if (std::next(begin()) != end())
270 // Another instruction found
272 Value
*Addr
= begin()->getValue();
273 assert(!Addr
->user_empty() &&
274 "where's the instruction which added this pointer?");
275 if (std::next(Addr
->user_begin()) != Addr
->user_end())
276 // Another instruction found -- this is really restrictive
279 return cast
<Instruction
>(*(Addr
->user_begin()));
281 if (1 != UnknownInsts
.size())
283 return cast
<Instruction
>(UnknownInsts
[0]);
286 void AliasSetTracker::clear() {
287 // Delete all the PointerRec entries.
288 for (auto &I
: PointerMap
)
289 I
.second
->eraseFromList();
293 // The alias sets should all be clear now.
297 /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
298 /// alias the pointer. Return the unified set, or nullptr if no set that aliases
299 /// the pointer was found. MustAliasAll is updated to true/false if the pointer
300 /// is found to MustAlias all the sets it merged.
301 AliasSet
*AliasSetTracker::mergeAliasSetsForPointer(const Value
*Ptr
,
303 const AAMDNodes
&AAInfo
,
304 bool &MustAliasAll
) {
305 AliasSet
*FoundSet
= nullptr;
307 for (AliasSet
&AS
: llvm::make_early_inc_range(*this)) {
311 AliasResult AR
= AS
.aliasesPointer(Ptr
, Size
, AAInfo
, AA
);
312 if (AR
== AliasResult::NoAlias
)
315 if (AR
!= AliasResult::MustAlias
)
316 MustAliasAll
= false;
319 // If this is the first alias set ptr can go into, remember it.
322 // Otherwise, we must merge the sets.
323 FoundSet
->mergeSetIn(AS
, *this);
330 AliasSet
*AliasSetTracker::findAliasSetForUnknownInst(Instruction
*Inst
) {
331 AliasSet
*FoundSet
= nullptr;
332 for (AliasSet
&AS
: llvm::make_early_inc_range(*this)) {
333 if (AS
.Forward
|| !AS
.aliasesUnknownInst(Inst
, AA
))
336 // If this is the first alias set ptr can go into, remember it.
339 // Otherwise, we must merge the sets.
340 FoundSet
->mergeSetIn(AS
, *this);
346 AliasSet
&AliasSetTracker::getAliasSetFor(const MemoryLocation
&MemLoc
) {
348 Value
* const Pointer
= const_cast<Value
*>(MemLoc
.Ptr
);
349 const LocationSize Size
= MemLoc
.Size
;
350 const AAMDNodes
&AAInfo
= MemLoc
.AATags
;
352 AliasSet::PointerRec
&Entry
= getEntryFor(Pointer
);
355 // At this point, the AST is saturated, so we only have one active alias
356 // set. That means we already know which alias set we want to return, and
357 // just need to add the pointer to that set to keep the data structure
359 // This, of course, means that we will never need a merge here.
360 if (Entry
.hasAliasSet()) {
361 Entry
.updateSizeAndAAInfo(Size
, AAInfo
);
362 assert(Entry
.getAliasSet(*this) == AliasAnyAS
&&
363 "Entry in saturated AST must belong to only alias set");
365 AliasAnyAS
->addPointer(*this, Entry
, Size
, AAInfo
);
370 bool MustAliasAll
= false;
371 // Check to see if the pointer is already known.
372 if (Entry
.hasAliasSet()) {
373 // If the size changed, we may need to merge several alias sets.
374 // Note that we can *not* return the result of mergeAliasSetsForPointer
375 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
376 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
377 // the right set for undef, even if it exists.
378 if (Entry
.updateSizeAndAAInfo(Size
, AAInfo
))
379 mergeAliasSetsForPointer(Pointer
, Size
, AAInfo
, MustAliasAll
);
381 return *Entry
.getAliasSet(*this)->getForwardedTarget(*this);
385 mergeAliasSetsForPointer(Pointer
, Size
, AAInfo
, MustAliasAll
)) {
386 // Add it to the alias set it aliases.
387 AS
->addPointer(*this, Entry
, Size
, AAInfo
, MustAliasAll
);
391 // Otherwise create a new alias set to hold the loaded pointer.
392 AliasSets
.push_back(new AliasSet());
393 AliasSets
.back().addPointer(*this, Entry
, Size
, AAInfo
, true);
394 return AliasSets
.back();
397 void AliasSetTracker::add(Value
*Ptr
, LocationSize Size
,
398 const AAMDNodes
&AAInfo
) {
399 addPointer(MemoryLocation(Ptr
, Size
, AAInfo
), AliasSet::NoAccess
);
402 void AliasSetTracker::add(LoadInst
*LI
) {
403 if (isStrongerThanMonotonic(LI
->getOrdering()))
404 return addUnknown(LI
);
405 addPointer(MemoryLocation::get(LI
), AliasSet::RefAccess
);
408 void AliasSetTracker::add(StoreInst
*SI
) {
409 if (isStrongerThanMonotonic(SI
->getOrdering()))
410 return addUnknown(SI
);
411 addPointer(MemoryLocation::get(SI
), AliasSet::ModAccess
);
414 void AliasSetTracker::add(VAArgInst
*VAAI
) {
415 addPointer(MemoryLocation::get(VAAI
), AliasSet::ModRefAccess
);
418 void AliasSetTracker::add(AnyMemSetInst
*MSI
) {
419 addPointer(MemoryLocation::getForDest(MSI
), AliasSet::ModAccess
);
422 void AliasSetTracker::add(AnyMemTransferInst
*MTI
) {
423 addPointer(MemoryLocation::getForDest(MTI
), AliasSet::ModAccess
);
424 addPointer(MemoryLocation::getForSource(MTI
), AliasSet::RefAccess
);
427 void AliasSetTracker::addUnknown(Instruction
*Inst
) {
428 if (isa
<DbgInfoIntrinsic
>(Inst
))
429 return; // Ignore DbgInfo Intrinsics.
431 if (auto *II
= dyn_cast
<IntrinsicInst
>(Inst
)) {
432 // These intrinsics will show up as affecting memory, but they are just
434 switch (II
->getIntrinsicID()) {
437 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
438 case Intrinsic::assume
:
439 case Intrinsic::experimental_noalias_scope_decl
:
440 case Intrinsic::sideeffect
:
441 case Intrinsic::pseudoprobe
:
445 if (!Inst
->mayReadOrWriteMemory())
446 return; // doesn't alias anything
448 if (AliasSet
*AS
= findAliasSetForUnknownInst(Inst
)) {
449 AS
->addUnknownInst(Inst
, AA
);
452 AliasSets
.push_back(new AliasSet());
453 AliasSets
.back().addUnknownInst(Inst
, AA
);
456 void AliasSetTracker::add(Instruction
*I
) {
457 // Dispatch to one of the other add methods.
458 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(I
))
460 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(I
))
462 if (VAArgInst
*VAAI
= dyn_cast
<VAArgInst
>(I
))
464 if (AnyMemSetInst
*MSI
= dyn_cast
<AnyMemSetInst
>(I
))
466 if (AnyMemTransferInst
*MTI
= dyn_cast
<AnyMemTransferInst
>(I
))
469 // Handle all calls with known mod/ref sets genericall
470 if (auto *Call
= dyn_cast
<CallBase
>(I
))
471 if (Call
->onlyAccessesArgMemory()) {
472 auto getAccessFromModRef
= [](ModRefInfo MRI
) {
473 if (isRefSet(MRI
) && isModSet(MRI
))
474 return AliasSet::ModRefAccess
;
475 else if (isModSet(MRI
))
476 return AliasSet::ModAccess
;
477 else if (isRefSet(MRI
))
478 return AliasSet::RefAccess
;
480 return AliasSet::NoAccess
;
483 ModRefInfo CallMask
= createModRefInfo(AA
.getModRefBehavior(Call
));
485 // Some intrinsics are marked as modifying memory for control flow
486 // modelling purposes, but don't actually modify any specific memory
488 using namespace PatternMatch
;
489 if (Call
->use_empty() &&
490 match(Call
, m_Intrinsic
<Intrinsic::invariant_start
>()))
491 CallMask
= clearMod(CallMask
);
493 for (auto IdxArgPair
: enumerate(Call
->args())) {
494 int ArgIdx
= IdxArgPair
.index();
495 const Value
*Arg
= IdxArgPair
.value();
496 if (!Arg
->getType()->isPointerTy())
498 MemoryLocation ArgLoc
=
499 MemoryLocation::getForArgument(Call
, ArgIdx
, nullptr);
500 ModRefInfo ArgMask
= AA
.getArgModRefInfo(Call
, ArgIdx
);
501 ArgMask
= intersectModRef(CallMask
, ArgMask
);
502 if (!isNoModRef(ArgMask
))
503 addPointer(ArgLoc
, getAccessFromModRef(ArgMask
));
508 return addUnknown(I
);
511 void AliasSetTracker::add(BasicBlock
&BB
) {
516 void AliasSetTracker::add(const AliasSetTracker
&AST
) {
517 assert(&AA
== &AST
.AA
&&
518 "Merging AliasSetTracker objects with different Alias Analyses!");
520 // Loop over all of the alias sets in AST, adding the pointers contained
521 // therein into the current alias sets. This can cause alias sets to be
522 // merged together in the current AST.
523 for (const AliasSet
&AS
: AST
) {
525 continue; // Ignore forwarding alias sets
527 // If there are any call sites in the alias set, add them to this AST.
528 for (unsigned i
= 0, e
= AS
.UnknownInsts
.size(); i
!= e
; ++i
)
529 if (auto *Inst
= AS
.getUnknownInst(i
))
532 // Loop over all of the pointers in this alias set.
533 for (AliasSet::iterator ASI
= AS
.begin(), E
= AS
.end(); ASI
!= E
; ++ASI
)
535 MemoryLocation(ASI
.getPointer(), ASI
.getSize(), ASI
.getAAInfo()),
536 (AliasSet::AccessLattice
)AS
.Access
);
540 // deleteValue method - This method is used to remove a pointer value from the
541 // AliasSetTracker entirely. It should be used when an instruction is deleted
542 // from the program to update the AST. If you don't use this, you would have
543 // dangling pointers to deleted instructions.
545 void AliasSetTracker::deleteValue(Value
*PtrVal
) {
546 // First, look up the PointerRec for this pointer.
547 PointerMapType::iterator I
= PointerMap
.find_as(PtrVal
);
548 if (I
== PointerMap
.end()) return; // Noop
550 // If we found one, remove the pointer from the alias set it is in.
551 AliasSet::PointerRec
*PtrValEnt
= I
->second
;
552 AliasSet
*AS
= PtrValEnt
->getAliasSet(*this);
554 // Unlink and delete from the list of values.
555 PtrValEnt
->eraseFromList();
557 if (AS
->Alias
== AliasSet::SetMayAlias
) {
559 TotalMayAliasSetSize
--;
562 // Stop using the alias set.
568 // copyValue - This method should be used whenever a preexisting value in the
569 // program is copied or cloned, introducing a new value. Note that it is ok for
570 // clients that use this method to introduce the same value multiple times: if
571 // the tracker already knows about a value, it will ignore the request.
573 void AliasSetTracker::copyValue(Value
*From
, Value
*To
) {
574 // First, look up the PointerRec for this pointer.
575 PointerMapType::iterator I
= PointerMap
.find_as(From
);
576 if (I
== PointerMap
.end())
578 assert(I
->second
->hasAliasSet() && "Dead entry?");
580 AliasSet::PointerRec
&Entry
= getEntryFor(To
);
581 if (Entry
.hasAliasSet()) return; // Already in the tracker!
583 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
584 I
= PointerMap
.find_as(From
);
585 // Add it to the alias set it aliases...
586 AliasSet
*AS
= I
->second
->getAliasSet(*this);
587 AS
->addPointer(*this, Entry
, I
->second
->getSize(), I
->second
->getAAInfo(),
591 AliasSet
&AliasSetTracker::mergeAllAliasSets() {
592 assert(!AliasAnyAS
&& (TotalMayAliasSetSize
> SaturationThreshold
) &&
593 "Full merge should happen once, when the saturation threshold is "
596 // Collect all alias sets, so that we can drop references with impunity
597 // without worrying about iterator invalidation.
598 std::vector
<AliasSet
*> ASVector
;
599 ASVector
.reserve(SaturationThreshold
);
600 for (AliasSet
&AS
: *this)
601 ASVector
.push_back(&AS
);
603 // Copy all instructions and pointers into a new set, and forward all other
605 AliasSets
.push_back(new AliasSet());
606 AliasAnyAS
= &AliasSets
.back();
607 AliasAnyAS
->Alias
= AliasSet::SetMayAlias
;
608 AliasAnyAS
->Access
= AliasSet::ModRefAccess
;
609 AliasAnyAS
->AliasAny
= true;
611 for (auto Cur
: ASVector
) {
612 // If Cur was already forwarding, just forward to the new AS instead.
613 AliasSet
*FwdTo
= Cur
->Forward
;
615 Cur
->Forward
= AliasAnyAS
;
616 AliasAnyAS
->addRef();
617 FwdTo
->dropRef(*this);
621 // Otherwise, perform the actual merge.
622 AliasAnyAS
->mergeSetIn(*Cur
, *this);
628 AliasSet
&AliasSetTracker::addPointer(MemoryLocation Loc
,
629 AliasSet::AccessLattice E
) {
630 AliasSet
&AS
= getAliasSetFor(Loc
);
633 if (!AliasAnyAS
&& (TotalMayAliasSetSize
> SaturationThreshold
)) {
634 // The AST is now saturated. From here on, we conservatively consider all
635 // pointers to alias each-other.
636 return mergeAllAliasSets();
642 //===----------------------------------------------------------------------===//
643 // AliasSet/AliasSetTracker Printing Support
644 //===----------------------------------------------------------------------===//
646 void AliasSet::print(raw_ostream
&OS
) const {
647 OS
<< " AliasSet[" << (const void*)this << ", " << RefCount
<< "] ";
648 OS
<< (Alias
== SetMustAlias
? "must" : "may") << " alias, ";
650 case NoAccess
: OS
<< "No access "; break;
651 case RefAccess
: OS
<< "Ref "; break;
652 case ModAccess
: OS
<< "Mod "; break;
653 case ModRefAccess
: OS
<< "Mod/Ref "; break;
654 default: llvm_unreachable("Bad value for Access!");
657 OS
<< " forwarding to " << (void*)Forward
;
661 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
662 if (I
!= begin()) OS
<< ", ";
663 I
.getPointer()->printAsOperand(OS
<< "(");
664 if (I
.getSize() == LocationSize::afterPointer())
665 OS
<< ", unknown after)";
666 else if (I
.getSize() == LocationSize::beforeOrAfterPointer())
667 OS
<< ", unknown before-or-after)";
669 OS
<< ", " << I
.getSize() << ")";
672 if (!UnknownInsts
.empty()) {
673 OS
<< "\n " << UnknownInsts
.size() << " Unknown instructions: ";
674 for (unsigned i
= 0, e
= UnknownInsts
.size(); i
!= e
; ++i
) {
676 if (auto *I
= getUnknownInst(i
)) {
678 I
->printAsOperand(OS
);
687 void AliasSetTracker::print(raw_ostream
&OS
) const {
688 OS
<< "Alias Set Tracker: " << AliasSets
.size();
690 OS
<< " (Saturated)";
691 OS
<< " alias sets for " << PointerMap
.size() << " pointer values.\n";
692 for (const AliasSet
&AS
: *this)
697 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
698 LLVM_DUMP_METHOD
void AliasSet::dump() const { print(dbgs()); }
699 LLVM_DUMP_METHOD
void AliasSetTracker::dump() const { print(dbgs()); }
702 //===----------------------------------------------------------------------===//
703 // ASTCallbackVH Class Implementation
704 //===----------------------------------------------------------------------===//
706 void AliasSetTracker::ASTCallbackVH::deleted() {
707 assert(AST
&& "ASTCallbackVH called with a null AliasSetTracker!");
708 AST
->deleteValue(getValPtr());
712 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value
*V
) {
713 AST
->copyValue(getValPtr(), V
);
716 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value
*V
, AliasSetTracker
*ast
)
717 : CallbackVH(V
), AST(ast
) {}
719 AliasSetTracker::ASTCallbackVH
&
720 AliasSetTracker::ASTCallbackVH::operator=(Value
*V
) {
721 return *this = ASTCallbackVH(V
, AST
);
724 //===----------------------------------------------------------------------===//
725 // AliasSetPrinter Pass
726 //===----------------------------------------------------------------------===//
730 class AliasSetPrinter
: public FunctionPass
{
732 static char ID
; // Pass identification, replacement for typeid
734 AliasSetPrinter() : FunctionPass(ID
) {
735 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
738 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
739 AU
.setPreservesAll();
740 AU
.addRequired
<AAResultsWrapperPass
>();
743 bool runOnFunction(Function
&F
) override
{
744 auto &AAWP
= getAnalysis
<AAResultsWrapperPass
>();
745 AliasSetTracker
Tracker(AAWP
.getAAResults());
746 errs() << "Alias sets for function '" << F
.getName() << "':\n";
747 for (Instruction
&I
: instructions(F
))
749 Tracker
.print(errs());
754 } // end anonymous namespace
756 char AliasSetPrinter::ID
= 0;
758 INITIALIZE_PASS_BEGIN(AliasSetPrinter
, "print-alias-sets",
759 "Alias Set Printer", false, true)
760 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass
)
761 INITIALIZE_PASS_END(AliasSetPrinter
, "print-alias-sets",
762 "Alias Set Printer", false, true)
764 AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream
&OS
) : OS(OS
) {}
766 PreservedAnalyses
AliasSetsPrinterPass::run(Function
&F
,
767 FunctionAnalysisManager
&AM
) {
768 auto &AA
= AM
.getResult
<AAManager
>(F
);
769 AliasSetTracker
Tracker(AA
);
770 OS
<< "Alias sets for function '" << F
.getName() << "':\n";
771 for (Instruction
&I
: instructions(F
))
774 return PreservedAnalyses::all();