19 #include "llvm/Config/llvm-config.h" 48 cl::desc(
"The maximum number of pointers may-alias " 49 "sets may contain before degradation"));
54 assert(!AS.Forward &&
"Alias set is already forwarding!");
55 assert(!Forward &&
"This set is a forwarding set!!");
57 bool WasMustAlias = (Alias == SetMustAlias);
62 if (Alias == SetMustAlias) {
67 PointerRec *L = getSomePointer();
68 PointerRec *R = AS.getSomePointer();
77 if (Alias == SetMayAlias) {
79 AST.TotalMayAliasSetSize +=
size();
80 if (AS.Alias == SetMustAlias)
81 AST.TotalMayAliasSetSize += AS.
size();
84 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
85 if (UnknownInsts.empty()) {
86 if (ASHadUnknownInsts) {
90 }
else if (ASHadUnknownInsts) {
91 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
92 AS.UnknownInsts.clear();
100 SetSize += AS.
size();
102 *PtrListEnd = AS.PtrList;
103 AS.PtrList->setPrevInList(PtrListEnd);
104 PtrListEnd = AS.PtrListEnd;
106 AS.PtrList =
nullptr;
107 AS.PtrListEnd = &AS.PtrList;
108 assert(*AS.PtrListEnd ==
nullptr &&
"End of list is not null?");
110 if (ASHadUnknownInsts)
114 void AliasSetTracker::removeAliasSet(
AliasSet *AS) {
117 AS->Forward =
nullptr;
119 if (AS->Alias == AliasSet::SetMayAlias)
120 TotalMayAliasSetSize -= AS->
size();
125 if (AS == AliasAnyAS) {
126 AliasAnyAS =
nullptr;
127 assert(AliasSets.empty() &&
"Tracker not empty");
132 assert(RefCount == 0 &&
"Cannot remove non-dead alias set from tracker!");
133 AST.removeAliasSet(
this);
138 bool KnownMustAlias,
bool SkipSizeUpdate) {
139 assert(!Entry.hasAliasSet() &&
"Entry already in set!");
143 if (PointerRec *
P = getSomePointer()) {
144 if (!KnownMustAlias) {
151 AST.TotalMayAliasSetSize +=
size();
154 }
else if (!SkipSizeUpdate)
155 P->updateSizeAndAAInfo(Size, AAInfo);
158 Entry.setAliasSet(
this);
159 Entry.updateSizeAndAAInfo(Size, AAInfo);
163 assert(*PtrListEnd ==
nullptr &&
"End of list is not null?");
164 *PtrListEnd = &Entry;
165 PtrListEnd = Entry.setPrevInList(PtrListEnd);
166 assert(*PtrListEnd ==
nullptr &&
"End of list is not null?");
170 if (Alias == SetMayAlias)
171 AST.TotalMayAliasSetSize++;
175 if (UnknownInsts.empty())
177 UnknownInsts.emplace_back(I);
183 !(I->
use_empty() &&
match(I, m_Intrinsic<Intrinsic::invariant_start>()));
184 if (!MayWriteMemory) {
192 Access = ModRefAccess;
205 if (Alias == SetMustAlias) {
206 assert(UnknownInsts.empty() &&
"Illegal must alias set!");
210 PointerRec *SomePtr = getSomePointer();
211 assert(SomePtr &&
"Empty must-alias set??");
213 SomePtr->getAAInfo()),
226 if (!UnknownInsts.empty()) {
227 for (
unsigned i = 0,
e = UnknownInsts.size(); i !=
e; ++i)
228 if (
auto *Inst = getUnknownInst(i))
244 "Instruction must either read or write memory.");
246 for (
unsigned i = 0,
e = UnknownInsts.size(); i !=
e; ++i) {
247 if (
auto *UnknownInst = getUnknownInst(i)) {
258 Inst,
MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
269 if (!UnknownInsts.empty())
277 "where's the instruction which added this pointer?");
282 return cast<Instruction>(*(Addr->
user_begin()));
284 if (1 != UnknownInsts.size())
286 return cast<Instruction>(UnknownInsts[0]);
293 I->second->eraseFromList();
305 AliasSet *AliasSetTracker::mergeAliasSetsForPointer(
const Value *Ptr,
308 bool &MustAliasAll) {
316 AliasResult AR = Cur->aliasesPointer(Ptr, Size, AAInfo, AA);
340 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
359 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
367 if (Entry.hasAliasSet()) {
368 Entry.updateSizeAndAAInfo(Size, AAInfo);
369 assert(Entry.getAliasSet(*
this) == AliasAnyAS &&
370 "Entry in saturated AST must belong to only alias set");
372 AliasAnyAS->addPointer(*
this, Entry, Size, AAInfo);
377 bool MustAliasAll =
false;
379 if (Entry.hasAliasSet()) {
385 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
386 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll);
388 return *Entry.getAliasSet(*this)->getForwardedTarget(*
this);
392 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
394 AS->addPointer(*
this, Entry, Size, AAInfo, MustAliasAll);
399 AliasSets.push_back(
new AliasSet());
400 AliasSets.back().addPointer(*
this, Entry, Size, AAInfo,
true);
401 return AliasSets.back();
406 addPointer(
MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
411 return addUnknown(LI);
417 return addUnknown(SI);
435 if (isa<DbgInfoIntrinsic>(Inst))
438 if (
auto *II = dyn_cast<IntrinsicInst>(Inst)) {
441 switch (II->getIntrinsicID()) {
445 case Intrinsic::assume:
446 case Intrinsic::sideeffect:
453 if (
AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
454 AS->addUnknownInst(Inst, AA);
457 AliasSets.push_back(
new AliasSet());
458 AliasSets.back().addUnknownInst(Inst, AA);
463 if (
LoadInst *LI = dyn_cast<LoadInst>(I))
467 if (
VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
475 if (
auto *Call = dyn_cast<CallBase>(I))
476 if (Call->onlyAccessesArgMemory()) {
479 return AliasSet::ModRefAccess;
481 return AliasSet::ModAccess;
483 return AliasSet::RefAccess;
485 return AliasSet::NoAccess;
494 if (Call->use_empty() &&
495 match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
498 for (
auto IdxArgPair :
enumerate(Call->args())) {
499 int ArgIdx = IdxArgPair.index();
500 const Value *
Arg = IdxArgPair.value();
508 addPointer(ArgLoc, getAccessFromModRef(ArgMask));
513 return addUnknown(I);
523 "Merging AliasSetTracker objects with different Alias Analyses!");
533 for (
unsigned i = 0,
e = AS.UnknownInsts.size(); i !=
e; ++i)
534 if (
auto *Inst = AS.getUnknownInst(i))
541 (AliasSet::AccessLattice)AS.Access);
546 assert(MSSA && L &&
"MSSA and L must be available");
548 if (
auto *Accesses = MSSA->getBlockAccesses(BB))
549 for (
auto &Access : *Accesses)
550 if (
auto *MUD = dyn_cast<MemoryUseOrDef>(&Access))
551 add(MUD->getMemoryInst());
562 if (I == PointerMap.end())
return;
565 AliasSet::PointerRec *PtrValEnt = I->second;
566 AliasSet *AS = PtrValEnt->getAliasSet(*
this);
569 PtrValEnt->eraseFromList();
571 if (AS->Alias == AliasSet::SetMayAlias) {
573 TotalMayAliasSetSize--;
590 if (I == PointerMap.end())
592 assert(I->second->hasAliasSet() &&
"Dead entry?");
594 AliasSet::PointerRec &Entry = getEntryFor(To);
595 if (Entry.hasAliasSet())
return;
598 I = PointerMap.find_as(From);
600 AliasSet *AS = I->second->getAliasSet(*
this);
601 AS->addPointer(*
this, Entry, I->second->getSize(), I->second->getAAInfo(),
605 AliasSet &AliasSetTracker::mergeAllAliasSets() {
607 "Full merge should happen once, when the saturation threshold is " 612 std::vector<AliasSet *> ASVector;
615 ASVector.push_back(&*I);
619 AliasSets.push_back(
new AliasSet());
620 AliasAnyAS = &AliasSets.back();
621 AliasAnyAS->Alias = AliasSet::SetMayAlias;
622 AliasAnyAS->Access = AliasSet::ModRefAccess;
623 AliasAnyAS->AliasAny =
true;
625 for (
auto Cur : ASVector) {
629 Cur->Forward = AliasAnyAS;
630 AliasAnyAS->addRef();
631 FwdTo->dropRef(*
this);
636 AliasAnyAS->mergeSetIn(*Cur, *
this);
643 AliasSet::AccessLattice
E) {
650 return mergeAllAliasSets();
661 OS <<
" AliasSet[" << (
const void*)
this <<
", " << RefCount <<
"] ";
662 OS << (Alias == SetMustAlias ?
"must" :
"may") <<
" alias, ";
664 case NoAccess: OS <<
"No access ";
break;
665 case RefAccess: OS <<
"Ref ";
break;
666 case ModAccess: OS <<
"Mod ";
break;
667 case ModRefAccess: OS <<
"Mod/Ref ";
break;
671 OS <<
" forwarding to " << (
void*)Forward;
676 if (I !=
begin()) OS <<
", ";
681 OS <<
", " << I.getSize() <<
")";
684 if (!UnknownInsts.empty()) {
685 OS <<
"\n " << UnknownInsts.size() <<
" Unknown instructions: ";
686 for (
unsigned i = 0,
e = UnknownInsts.size(); i !=
e; ++i) {
688 if (
auto *I = getUnknownInst(i)) {
700 OS <<
"Alias Set Tracker: " << AliasSets.size();
702 OS <<
" (Saturated)";
703 OS <<
" alias sets for " << PointerMap.size() <<
" pointer values.\n";
709 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 718 void AliasSetTracker::ASTCallbackVH::deleted() {
719 assert(AST &&
"ASTCallbackVH called with a null AliasSetTracker!");
724 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(
Value *V) {
731 AliasSetTracker::ASTCallbackVH &
732 AliasSetTracker::ASTCallbackVH::operator=(
Value *V) {
733 return *
this = ASTCallbackVH(V, AST);
758 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
760 errs() <<
"Alias sets for function '" << F.
getName() <<
"':\n";
763 Tracker->print(
errs());
774 "Alias Set Printer",
false,
true)
void mergeSetIn(AliasSet &AS, AliasSetTracker &AST)
Merge the specified alias set into this alias set.
INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets", "Alias Set Printer", false, true) INITIALIZE_PASS_END(AliasSetPrinter
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Atomic ordering constants.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Define an iterator for alias sets... this is just a forward iterator.
static constexpr LocationSize unknown()
void add(Value *Ptr, LocationSize Size, const AAMDNodes &AAInfo)
These methods are used to add different types of instructions to the alias sets.
static cl::opt< unsigned > SaturationThreshold("alias-set-saturation-threshold", cl::Hidden, cl::init(250), cl::desc("The maximum number of pointers may-alias " "sets may contain before degradation"))
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
The two locations do not alias at all.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
block Block Frequency true
An instruction for reading from memory.
print alias Alias Set Printer
LLVM_NODISCARD ModRefInfo clearMod(const ModRefInfo MRI)
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
bool match(Val *V, const Pattern &P)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
inst_iterator inst_begin(Function *F)
void initializeAliasSetPrinterPass(PassRegistry &)
static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
FunctionModRefBehavior getModRefBehavior(const CallBase *Call)
Return the behavior of the given call site.
Type * getType() const
All values are typed, get the type of this value.
friend class AliasSetTracker
bool aliasesUnknownInst(const Instruction *Inst, AliasAnalysis &AA) const
static MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
Instruction * getUniqueInstruction()
If this alias set is known to contain a single instruction and only a single unique instruction...
An instruction for storing to memory.
bool isStrongerThanMonotonic(AtomicOrdering ao)
AliasResult
The possible results of an alias query.
static bool runOnFunction(Function &F, bool PostInlining)
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
initializer< Ty > init(const Ty &Val)
void print(raw_ostream &OS) const
unsigned const MachineRegisterInfo * MRI
LLVM Basic Block Representation.
AliasAnalysis & getAliasAnalysis() const
Return the underlying alias analysis object used by this tracker.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
Represent the analysis usage information of a pass.
AliasSet & getAliasSetFor(const MemoryLocation &MemLoc)
Return the alias set which contains the specified memory location.
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
FunctionPass class - This class is used to implement most global optimizations.
This class represents any memset intrinsic.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known...
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
The two locations may or may not alias. This is the least precise result.
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
The two locations precisely alias each other.
AliasResult aliasesPointer(const Value *Ptr, LocationSize Size, const AAMDNodes &AAInfo, AliasAnalysis &AA) const
If the specified pointer "may" (or must) alias one of the members in the set return the appropriate A...
void addAllInstructionsInLoopUsingMSSA()
Iterator for intrusive lists based on ilist_node.
BlockVerifier::State From
void print(raw_ostream &OS) const
void copyValue(Value *From, Value *To)
This method should be used whenever a preexisting value in the program is copied or cloned...
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
Module.h This file contains the declarations for the Module class.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental.guard intrinsic.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM_NODISCARD bool isNoModRef(const ModRefInfo MRI)
LLVM_NODISCARD bool isModSet(const ModRefInfo MRI)
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
AtomicOrdering getOrdering() const
Returns the ordering constraint of this store instruction.
This file provides utility analysis objects describing memory locations.
StringRef getName() const
Return a constant reference to the value's name.
LLVM_NODISCARD ModRefInfo intersectModRef(const ModRefInfo MRI1, const ModRefInfo MRI2)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the ModRef info associated with a pointer argument of a call.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
LLVM Value Representation.
LLVM_NODISCARD ModRefInfo createModRefInfo(const FunctionModRefBehavior FMRB)
This class implements an extremely fast bulk output stream that can only output to a stream...
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Value handle with callbacks on RAUW and destruction.
inst_iterator inst_end(Function *F)
LLVM_NODISCARD bool isModOrRefSet(const ModRefInfo MRI)
void addUnknown(Instruction *I)
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc)
getModRefInfo (for call sites) - Return information about whether a particular call site modifies or ...
void deleteValue(Value *PtrVal)
This method is used to remove a pointer value from the AliasSetTracker entirely.
detail::enumerator< R > enumerate(R &&TheRange)
Given an input range, returns a new range whose values are are pair (A,B) such that A is the 0-based ...
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
bool mayReadOrWriteMemory() const
Return true if this instruction may read or write memory.
LLVM_NODISCARD bool isRefSet(const ModRefInfo MRI)