Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 25c7dc3

Browse files
chandlercBjörn Steinbrink
authored andcommitted
[AA] Hoist the logic to reformulate various AA queries in terms of other
parts of the AA interface out of the base class of every single AA result object. Because this logic reformulates the query in terms of some other aspect of the API, it would easily cause O(n^2) query patterns in alias analysis. These could in turn be magnified further based on the number of call arguments, and then further based on the number of AA queries made for a particular call. This ended up causing problems for Rust that were actually noticable enough to get a bug (PR26564) and probably other places as well. When originally re-working the AA infrastructure, the desire was to regularize the pattern of refinement without losing any generality. While I think it was successful, that is clearly proving to be too costly. And the cost is needless: we gain no actual improvement for this generality of making a direct query to tbaa actually be able to re-use some other alias analysis's refinement logic for one of the other APIs, or some such. In short, this is entirely wasted work. To the extent possible, delegation to other API surfaces should be done at the aggregation layer so that we can avoid re-walking the aggregation. In fact, this significantly simplifies the logic as we no longer need to smuggle the aggregation layer into each alias analysis (or the TargetLibraryInfo into each alias analysis just so we can form argument memory locations!). However, we also have some delegation logic inside of BasicAA and some of it even makes sense. When the delegation logic is baking in specific knowledge of aliasing properties of the LLVM IR, as opposed to simply reformulating the query to utilize a different alias analysis interface entry point, it makes a lot of sense to restrict that logic to a different layer such as BasicAA. So one aspect of the delegation that was in every AA base class is that when we don't have operand bundles, we re-use function AA results as a fallback for callsite alias results. This relies on the IR properties of calls and functions w.r.t. aliasing, and so seems a better fit to BasicAA. I've lifted the logic up to that point where it seems to be a natural fit. This still does a bit of redundant work (we query function attributes twice, once via the callsite and once via the function AA query) but it is *exactly* twice here, no more. The end result is that all of the delegation logic is hoisted out of the base class and into either the aggregation layer when it is a pure retargeting to a different API surface, or into BasicAA when it relies on the IR's aliasing properties. This should fix the quadratic query pattern reported in PR26564, although I don't have a stand-alone test case to reproduce it. It also seems general goodness. Now the numerous AAs that don't need target library info don't carry it around and depend on it. I think I can even rip out the general access to the aggregation layer and only expose that in BasicAA as it is the only place where we re-query in that manner. However, this is a non-trivial change to the AA infrastructure so I want to get some additional eyes on this before it lands. Sadly, it can't wait long because we should really cherry pick this into 3.8 if we're going to go this route. Differential Revision: http://reviews.llvm.org/D17329 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@262490 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 361ff3b commit 25c7dc3

21 files changed

+202
-257
lines changed

include/llvm/Analysis/AliasAnalysis.h

Lines changed: 21 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@
4343
#include "llvm/IR/Metadata.h"
4444
#include "llvm/IR/PassManager.h"
4545
#include "llvm/Analysis/MemoryLocation.h"
46+
#include "llvm/Analysis/TargetLibraryInfo.h"
4647

4748
namespace llvm {
4849
class BasicAAResult;
4950
class LoadInst;
5051
class StoreInst;
5152
class VAArgInst;
5253
class DataLayout;
53-
class TargetLibraryInfo;
5454
class Pass;
5555
class AnalysisUsage;
5656
class MemTransferInst;
@@ -161,9 +161,8 @@ class AAResults {
161161
public:
162162
// Make these results default constructable and movable. We have to spell
163163
// these out because MSVC won't synthesize them.
164-
AAResults() {}
164+
AAResults(const TargetLibraryInfo &TLI) : TLI(TLI) {}
165165
AAResults(AAResults &&Arg);
166-
AAResults &operator=(AAResults &&Arg);
167166
~AAResults();
168167

169168
/// Register a specific AA result.
@@ -557,6 +556,8 @@ class AAResults {
557556

558557
template <typename T> friend class AAResultBase;
559558

559+
const TargetLibraryInfo &TLI;
560+
560561
std::vector<std::unique_ptr<Concept>> AAs;
561562
};
562563

@@ -753,20 +754,23 @@ template <typename DerivedT> class AAResultBase {
753754
}
754755
};
755756

756-
const TargetLibraryInfo &TLI;
757-
758-
explicit AAResultBase(const TargetLibraryInfo &TLI) : TLI(TLI) {}
757+
explicit AAResultBase() {}
759758

760759
// Provide all the copy and move constructors so that derived types aren't
761760
// constrained.
762-
AAResultBase(const AAResultBase &Arg) : TLI(Arg.TLI) {}
763-
AAResultBase(AAResultBase &&Arg) : TLI(Arg.TLI) {}
761+
AAResultBase(const AAResultBase &Arg) {}
762+
AAResultBase(AAResultBase &&Arg) {}
764763

765764
/// Get a proxy for the best AA result set to query at this time.
766765
///
767766
/// When this result is part of a larger aggregation, this will proxy to that
768767
/// aggregation. When this result is used in isolation, it will just delegate
769768
/// back to the derived class's implementation.
769+
///
770+
/// Note that callers of this need to take considerable care to not cause
771+
/// performance problems when they use this routine, in the case of a large
772+
/// number of alias analyses being aggregated, it can be expensive to walk
773+
/// back across the chain.
770774
AAResultsProxy getBestAAResults() { return AAResultsProxy(AAR, derived()); }
771775

772776
public:
@@ -783,173 +787,22 @@ template <typename DerivedT> class AAResultBase {
783787
}
784788

785789
FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) {
786-
if (!CS.hasOperandBundles())
787-
// If CS has operand bundles then aliasing attributes from the function it
788-
// calls do not directly apply to the CallSite. This can be made more
789-
// precise in the future.
790-
if (const Function *F = CS.getCalledFunction())
791-
return getBestAAResults().getModRefBehavior(F);
792-
793790
return FMRB_UnknownModRefBehavior;
794791
}
795792

796793
FunctionModRefBehavior getModRefBehavior(const Function *F) {
797794
return FMRB_UnknownModRefBehavior;
798795
}
799796

800-
ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
801-
802-
ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2);
803-
};
804-
805-
/// Synthesize \c ModRefInfo for a call site and memory location by examining
806-
/// the general behavior of the call site and any specific information for its
807-
/// arguments.
808-
///
809-
/// This essentially, delegates across the alias analysis interface to collect
810-
/// information which may be enough to (conservatively) fulfill the query.
811-
template <typename DerivedT>
812-
ModRefInfo AAResultBase<DerivedT>::getModRefInfo(ImmutableCallSite CS,
813-
const MemoryLocation &Loc) {
814-
auto MRB = getBestAAResults().getModRefBehavior(CS);
815-
if (MRB == FMRB_DoesNotAccessMemory)
816-
return MRI_NoModRef;
817-
818-
ModRefInfo Mask = MRI_ModRef;
819-
if (AAResults::onlyReadsMemory(MRB))
820-
Mask = MRI_Ref;
821-
822-
if (AAResults::onlyAccessesArgPointees(MRB)) {
823-
bool DoesAlias = false;
824-
ModRefInfo AllArgsMask = MRI_NoModRef;
825-
if (AAResults::doesAccessArgPointees(MRB)) {
826-
for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(),
827-
AE = CS.arg_end();
828-
AI != AE; ++AI) {
829-
const Value *Arg = *AI;
830-
if (!Arg->getType()->isPointerTy())
831-
continue;
832-
unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
833-
MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
834-
AliasResult ArgAlias = getBestAAResults().alias(ArgLoc, Loc);
835-
if (ArgAlias != NoAlias) {
836-
ModRefInfo ArgMask = getBestAAResults().getArgModRefInfo(CS, ArgIdx);
837-
DoesAlias = true;
838-
AllArgsMask = ModRefInfo(AllArgsMask | ArgMask);
839-
}
840-
}
841-
}
842-
if (!DoesAlias)
843-
return MRI_NoModRef;
844-
Mask = ModRefInfo(Mask & AllArgsMask);
845-
}
846-
847-
// If Loc is a constant memory location, the call definitely could not
848-
// modify the memory location.
849-
if ((Mask & MRI_Mod) &&
850-
getBestAAResults().pointsToConstantMemory(Loc, /*OrLocal*/ false))
851-
Mask = ModRefInfo(Mask & ~MRI_Mod);
852-
853-
return Mask;
854-
}
855-
856-
/// Synthesize \c ModRefInfo for two call sites by examining the general
857-
/// behavior of the call site and any specific information for its arguments.
858-
///
859-
/// This essentially, delegates across the alias analysis interface to collect
860-
/// information which may be enough to (conservatively) fulfill the query.
861-
template <typename DerivedT>
862-
ModRefInfo AAResultBase<DerivedT>::getModRefInfo(ImmutableCallSite CS1,
863-
ImmutableCallSite CS2) {
864-
// If CS1 or CS2 are readnone, they don't interact.
865-
auto CS1B = getBestAAResults().getModRefBehavior(CS1);
866-
if (CS1B == FMRB_DoesNotAccessMemory)
867-
return MRI_NoModRef;
868-
869-
auto CS2B = getBestAAResults().getModRefBehavior(CS2);
870-
if (CS2B == FMRB_DoesNotAccessMemory)
871-
return MRI_NoModRef;
872-
873-
// If they both only read from memory, there is no dependence.
874-
if (AAResults::onlyReadsMemory(CS1B) && AAResults::onlyReadsMemory(CS2B))
875-
return MRI_NoModRef;
876-
877-
ModRefInfo Mask = MRI_ModRef;
878-
879-
// If CS1 only reads memory, the only dependence on CS2 can be
880-
// from CS1 reading memory written by CS2.
881-
if (AAResults::onlyReadsMemory(CS1B))
882-
Mask = ModRefInfo(Mask & MRI_Ref);
883-
884-
// If CS2 only access memory through arguments, accumulate the mod/ref
885-
// information from CS1's references to the memory referenced by
886-
// CS2's arguments.
887-
if (AAResults::onlyAccessesArgPointees(CS2B)) {
888-
ModRefInfo R = MRI_NoModRef;
889-
if (AAResults::doesAccessArgPointees(CS2B)) {
890-
for (ImmutableCallSite::arg_iterator I = CS2.arg_begin(),
891-
E = CS2.arg_end();
892-
I != E; ++I) {
893-
const Value *Arg = *I;
894-
if (!Arg->getType()->isPointerTy())
895-
continue;
896-
unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
897-
auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
898-
899-
// ArgMask indicates what CS2 might do to CS2ArgLoc, and the dependence
900-
// of CS1 on that location is the inverse.
901-
ModRefInfo ArgMask =
902-
getBestAAResults().getArgModRefInfo(CS2, CS2ArgIdx);
903-
if (ArgMask == MRI_Mod)
904-
ArgMask = MRI_ModRef;
905-
else if (ArgMask == MRI_Ref)
906-
ArgMask = MRI_Mod;
907-
908-
ArgMask = ModRefInfo(ArgMask &
909-
getBestAAResults().getModRefInfo(CS1, CS2ArgLoc));
910-
911-
R = ModRefInfo((R | ArgMask) & Mask);
912-
if (R == Mask)
913-
break;
914-
}
915-
}
916-
return R;
797+
ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc) {
798+
return MRI_ModRef;
917799
}
918800

919-
// If CS1 only accesses memory through arguments, check if CS2 references
920-
// any of the memory referenced by CS1's arguments. If not, return NoModRef.
921-
if (AAResults::onlyAccessesArgPointees(CS1B)) {
922-
ModRefInfo R = MRI_NoModRef;
923-
if (AAResults::doesAccessArgPointees(CS1B)) {
924-
for (ImmutableCallSite::arg_iterator I = CS1.arg_begin(),
925-
E = CS1.arg_end();
926-
I != E; ++I) {
927-
const Value *Arg = *I;
928-
if (!Arg->getType()->isPointerTy())
929-
continue;
930-
unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
931-
auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
932-
933-
// ArgMask indicates what CS1 might do to CS1ArgLoc; if CS1 might Mod
934-
// CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If CS1
935-
// might Ref, then we care only about a Mod by CS2.
936-
ModRefInfo ArgMask = getBestAAResults().getArgModRefInfo(CS1, CS1ArgIdx);
937-
ModRefInfo ArgR = getBestAAResults().getModRefInfo(CS2, CS1ArgLoc);
938-
if (((ArgMask & MRI_Mod) != MRI_NoModRef &&
939-
(ArgR & MRI_ModRef) != MRI_NoModRef) ||
940-
((ArgMask & MRI_Ref) != MRI_NoModRef &&
941-
(ArgR & MRI_Mod) != MRI_NoModRef))
942-
R = ModRefInfo((R | ArgMask) & Mask);
943-
944-
if (R == Mask)
945-
break;
946-
}
947-
}
948-
return R;
801+
ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
802+
return MRI_ModRef;
949803
}
804+
};
950805

951-
return Mask;
952-
}
953806

954807
/// isNoAliasCall - Return true if this pointer is returned by a noalias
955808
/// function.
@@ -1019,7 +872,7 @@ class AAManager {
1019872
}
1020873

1021874
Result run(Function &F, AnalysisManager<Function> *AM) {
1022-
Result R;
875+
Result R(AM->getResult<TargetLibraryAnalysis>(F));
1023876
for (auto &Getter : FunctionResultGetters)
1024877
(*Getter)(F, *AM, R);
1025878
return R;
@@ -1075,13 +928,13 @@ ImmutablePass *createExternalAAWrapperPass(
1075928
/// inside of a \c ModulePass or a \c CallGraphSCCPass.
1076929
///
1077930
/// If a \c ModulePass or a \c CallGraphSCCPass calls \p
1078-
/// createLegacyPMAAResults, it also needs to call \p addUsedAAAnalyses in \p
1079-
/// getAnalysisUsage.
931+
/// createLegacyPMAAResults, it also needs to call \p getAAResultsAnalysisUsage
932+
/// in \p / getAnalysisUsage.
1080933
AAResults createLegacyPMAAResults(Pass &P, Function &F, BasicAAResult &BAR);
1081934

1082935
/// A helper for the legacy pass manager to populate \p AU to add uses to make
1083936
/// sure the analyses required by \p createLegacyPMAAResults are available.
1084-
void addUsedAAAnalyses(AnalysisUsage &AU);
937+
void getAAResultsAnalysisUsage(AnalysisUsage &AU);
1085938

1086939
} // End llvm namespace
1087940

include/llvm/Analysis/BasicAliasAnalysis.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class BasicAAResult : public AAResultBase<BasicAAResult> {
4040
friend AAResultBase<BasicAAResult>;
4141

4242
const DataLayout &DL;
43+
const TargetLibraryInfo &TLI;
4344
AssumptionCache &AC;
4445
DominatorTree *DT;
4546
LoopInfo *LI;
@@ -48,13 +49,14 @@ class BasicAAResult : public AAResultBase<BasicAAResult> {
4849
BasicAAResult(const DataLayout &DL, const TargetLibraryInfo &TLI,
4950
AssumptionCache &AC, DominatorTree *DT = nullptr,
5051
LoopInfo *LI = nullptr)
51-
: AAResultBase(TLI), DL(DL), AC(AC), DT(DT), LI(LI) {}
52+
: AAResultBase(), DL(DL), TLI(TLI), AC(AC), DT(DT), LI(LI) {}
5253

5354
BasicAAResult(const BasicAAResult &Arg)
54-
: AAResultBase(Arg), DL(Arg.DL), AC(Arg.AC), DT(Arg.DT), LI(Arg.LI) {}
55-
BasicAAResult(BasicAAResult &&Arg)
56-
: AAResultBase(std::move(Arg)), DL(Arg.DL), AC(Arg.AC), DT(Arg.DT),
55+
: AAResultBase(Arg), DL(Arg.DL), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT),
5756
LI(Arg.LI) {}
57+
BasicAAResult(BasicAAResult &&Arg)
58+
: AAResultBase(std::move(Arg)), DL(Arg.DL), TLI(Arg.TLI), AC(Arg.AC),
59+
DT(Arg.DT), LI(Arg.LI) {}
5860

5961
/// Handle invalidation events from the new pass manager.
6062
///

include/llvm/Analysis/CFLAliasAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CFLAAResult : public AAResultBase<CFLAAResult> {
3232
struct FunctionInfo;
3333

3434
public:
35-
explicit CFLAAResult(const TargetLibraryInfo &TLI);
35+
explicit CFLAAResult();
3636
CFLAAResult(CFLAAResult &&Arg);
3737

3838
/// Handle invalidation events from the new pass manager.

include/llvm/Analysis/GlobalsModRef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class GlobalsAAResult : public AAResultBase<GlobalsAAResult> {
3535
class FunctionInfo;
3636

3737
const DataLayout &DL;
38+
const TargetLibraryInfo &TLI;
3839

3940
/// The globals that do not have their addresses taken.
4041
SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;

include/llvm/Analysis/ObjCARCAliasAnalysis.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#define LLVM_ANALYSIS_OBJCARCALIASANALYSIS_H
2525

2626
#include "llvm/Analysis/AliasAnalysis.h"
27-
#include "llvm/Analysis/TargetLibraryInfo.h"
2827
#include "llvm/Pass.h"
2928

3029
namespace llvm {
@@ -42,8 +41,7 @@ class ObjCARCAAResult : public AAResultBase<ObjCARCAAResult> {
4241
const DataLayout &DL;
4342

4443
public:
45-
explicit ObjCARCAAResult(const DataLayout &DL, const TargetLibraryInfo &TLI)
46-
: AAResultBase(TLI), DL(DL) {}
44+
explicit ObjCARCAAResult(const DataLayout &DL) : AAResultBase(), DL(DL) {}
4745
ObjCARCAAResult(ObjCARCAAResult &&Arg)
4846
: AAResultBase(std::move(Arg)), DL(Arg.DL) {}
4947

include/llvm/Analysis/ScalarEvolutionAliasAnalysis.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class SCEVAAResult : public AAResultBase<SCEVAAResult> {
2828
ScalarEvolution &SE;
2929

3030
public:
31-
explicit SCEVAAResult(const TargetLibraryInfo &TLI, ScalarEvolution &SE)
32-
: AAResultBase(TLI), SE(SE) {}
31+
explicit SCEVAAResult(ScalarEvolution &SE) : AAResultBase(), SE(SE) {}
3332
SCEVAAResult(SCEVAAResult &&Arg) : AAResultBase(std::move(Arg)), SE(Arg.SE) {}
3433

3534
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);

include/llvm/Analysis/ScopedNoAliasAA.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ class ScopedNoAliasAAResult : public AAResultBase<ScopedNoAliasAAResult> {
2727
friend AAResultBase<ScopedNoAliasAAResult>;
2828

2929
public:
30-
explicit ScopedNoAliasAAResult(const TargetLibraryInfo &TLI)
31-
: AAResultBase(TLI) {}
30+
explicit ScopedNoAliasAAResult() : AAResultBase() {}
3231
ScopedNoAliasAAResult(ScopedNoAliasAAResult &&Arg)
3332
: AAResultBase(std::move(Arg)) {}
3433

include/llvm/Analysis/TypeBasedAliasAnalysis.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ class TypeBasedAAResult : public AAResultBase<TypeBasedAAResult> {
2727
friend AAResultBase<TypeBasedAAResult>;
2828

2929
public:
30-
explicit TypeBasedAAResult(const TargetLibraryInfo &TLI)
31-
: AAResultBase(TLI) {}
30+
explicit TypeBasedAAResult() {}
3231
TypeBasedAAResult(TypeBasedAAResult &&Arg) : AAResultBase(std::move(Arg)) {}
3332

3433
/// Handle invalidation events from the new pass manager.

include/llvm/LinkAllPasses.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/Analysis/ScalarEvolution.h"
3232
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
3333
#include "llvm/Analysis/ScopedNoAliasAA.h"
34+
#include "llvm/Analysis/TargetLibraryInfo.h"
3435
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
3536
#include "llvm/CodeGen/Passes.h"
3637
#include "llvm/IR/Function.h"
@@ -190,7 +191,9 @@ namespace {
190191
(void)new llvm::ScalarEvolutionWrapperPass();
191192
llvm::Function::Create(nullptr, llvm::GlobalValue::ExternalLinkage)->viewCFGOnly();
192193
llvm::RGPassManager RGM;
193-
llvm::AliasAnalysis AA;
194+
llvm::TargetLibraryInfoImpl TLII;
195+
llvm::TargetLibraryInfo TLI(TLII);
196+
llvm::AliasAnalysis AA(TLI);
194197
llvm::AliasSetTracker X(AA);
195198
X.add(nullptr, 0, llvm::AAMDNodes()); // for -print-alias-sets
196199
(void) llvm::AreStatisticsEnabled();

0 commit comments

Comments
 (0)