Skip to content

Commit

Permalink
[PM] Port ScalarEvolution to the new pass manager.
Browse files Browse the repository at this point in the history
This change makes ScalarEvolution a stand-alone object and just produces
one from a pass as needed. Making this work well requires making the
object movable, using references instead of overwritten pointers in
a number of places, and other refactorings.

I've also wired it up to the new pass manager and added a RUN line to
a test to exercise it under the new pass manager. This includes basic
printing support much like with other analyses.

But there is a big and somewhat scary change here. Prior to this patch
ScalarEvolution was never *actually* invalidated!!! Re-running the pass
just re-wired up the various other analyses and didn't remove any of the
existing entries in the SCEV caches or clear out anything at all. This
might seem OK as everything in SCEV that can uses ValueHandles to track
updates to the values that serve as SCEV keys. However, this still means
that as we ran SCEV over each function in the module, we kept
accumulating more and more SCEVs into the cache. At the end, we would
have a SCEV cache with every value that we ever needed a SCEV for in the
entire module!!! Yowzers. The releaseMemory routine would dump all of
this, but that isn't realy called during normal runs of the pipeline as
far as I can see.

To make matters worse, there *is* actually a key that we don't update
with value handles -- there is a map keyed off of Loop*s. Because
LoopInfo *does* release its memory from run to run, it is entirely
possible to run SCEV over one function, then over another function, and
then lookup a Loop* from the second function but find an entry inserted
for the first function! Ouch.

To make matters still worse, there are plenty of updates that *don't*
trip a value handle. It seems incredibly unlikely that today GVN or
another pass that invalidates SCEV can update values in *just* such
a way that a subsequent run of SCEV will incorrectly find lookups in
a cache, but it is theoretically possible and would be a nightmare to
debug.

With this refactoring, I've fixed all this by actually destroying and
recreating the ScalarEvolution object from run to run. Technically, this
could increase the amount of malloc traffic we see, but then again it is
also technically correct. ;] I don't actually think we're suffering from
tons of malloc traffic from SCEV because if we were, the fact that we
never clear the memory would seem more likely to have come up as an
actual problem before now. So, I've made the simple fix here. If in fact
there are serious issues with too much allocation and deallocation,
I can work on a clever fix that preserves the allocations (while
clearing the data) between each run, but I'd prefer to do that kind of
optimization with a test case / benchmark that shows why we need such
cleverness (and that can test that we actually make it faster). It's
possible that this will make some things faster by making the SCEV
caches have higher locality (due to being significantly smaller) so
until there is a clear benchmark, I think the simple change is best.

Differential Revision: http://reviews.llvm.org/D12063

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245193 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
chandlerc committed Aug 17, 2015
1 parent df8fbfc commit bfe1f1c
Show file tree
Hide file tree
Showing 43 changed files with 393 additions and 293 deletions.
92 changes: 68 additions & 24 deletions include/llvm/Analysis/ScalarEvolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/Allocator.h"
Expand Down Expand Up @@ -170,11 +171,10 @@ namespace llvm {
static bool classof(const SCEV *S);
};

/// ScalarEvolution - This class is the main scalar evolution driver. Because
/// client code (intentionally) can't do much with the SCEV objects directly,
/// they must ask this class for services.
///
class ScalarEvolution : public FunctionPass {
/// The main scalar evolution driver. Because client code (intentionally)
/// can't do much with the SCEV objects directly, they must ask this class
/// for services.
class ScalarEvolution {
public:
/// LoopDisposition - An enum describing the relationship between a
/// SCEV and a loop.
Expand Down Expand Up @@ -224,26 +224,26 @@ namespace llvm {

/// F - The function we are analyzing.
///
Function *F;

/// The tracker for @llvm.assume intrinsics in this function.
AssumptionCache *AC;

/// LI - The loop information for the function we are currently analyzing.
///
LoopInfo *LI;
Function &F;

/// TLI - The target library information for the target we are targeting.
///
TargetLibraryInfo *TLI;
TargetLibraryInfo &TLI;

/// The tracker for @llvm.assume intrinsics in this function.
AssumptionCache ∾

/// DT - The dominator tree.
///
DominatorTree *DT;
DominatorTree &DT;

/// LI - The loop information for the function we are currently analyzing.
///
LoopInfo &LI;

/// CouldNotCompute - This SCEV is used to represent unknown trip
/// counts and things.
SCEVCouldNotCompute CouldNotCompute;
std::unique_ptr<SCEVCouldNotCompute> CouldNotCompute;

/// ValueExprMapType - The typedef for ValueExprMap.
///
Expand Down Expand Up @@ -604,10 +604,12 @@ namespace llvm {
SCEV::NoWrapFlags getNoWrapFlagsFromUB(const Value *V);

public:
static char ID; // Pass identification, replacement for typeid
ScalarEvolution();
ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC,
DominatorTree &DT, LoopInfo &LI);
~ScalarEvolution();
ScalarEvolution(ScalarEvolution &&Arg);

LLVMContext &getContext() const { return F->getContext(); }
LLVMContext &getContext() const { return F.getContext(); }

/// isSCEVable - Test if values of the given type are analyzable within
/// the SCEV framework. This primarily includes integer types, and it
Expand Down Expand Up @@ -984,11 +986,8 @@ namespace llvm {
SmallVectorImpl<const SCEV *> &Sizes,
const SCEV *ElementSize) const;

bool runOnFunction(Function &F) override;
void releaseMemory() override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
void print(raw_ostream &OS, const Module* = nullptr) const override;
void verifyAnalysis() const override;
void print(raw_ostream &OS) const;
void verify() const;

/// Collect parametric terms occurring in step expressions.
void collectParametricTerms(const SCEV *Expr,
Expand Down Expand Up @@ -1097,6 +1096,51 @@ namespace llvm {
/// to locate them all and call their destructors.
SCEVUnknown *FirstUnknown;
};

/// \brief Analysis pass that exposes the \c ScalarEvolution for a function.
class ScalarEvolutionAnalysis {
static char PassID;

public:
typedef ScalarEvolution Result;

/// \brief Opaque, unique identifier for this analysis pass.
static void *ID() { return (void *)&PassID; }

/// \brief Provide a name for the analysis for debugging and logging.
static StringRef name() { return "ScalarEvolutionAnalysis"; }

ScalarEvolution run(Function &F, AnalysisManager<Function> *AM);
};

/// \brief Printer pass for the \c ScalarEvolutionAnalysis results.
class ScalarEvolutionPrinterPass {
raw_ostream &OS;

public:
explicit ScalarEvolutionPrinterPass(raw_ostream &OS) : OS(OS) {}
PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);

static StringRef name() { return "ScalarEvolutionPrinterPass"; }
};

class ScalarEvolutionWrapperPass : public FunctionPass {
std::unique_ptr<ScalarEvolution> SE;

public:
static char ID;

ScalarEvolutionWrapperPass();

ScalarEvolution &getSE() { return *SE; }
const ScalarEvolution &getSE() const { return *SE; }

bool runOnFunction(Function &F) override;
void releaseMemory() override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
void print(raw_ostream &OS, const Module * = nullptr) const override;
void verifyAnalysis() const override;
};
}

#endif
2 changes: 1 addition & 1 deletion include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void initializeSROAPass(PassRegistry&);
void initializeSROA_DTPass(PassRegistry&);
void initializeSROA_SSAUpPass(PassRegistry&);
void initializeScalarEvolutionAliasAnalysisPass(PassRegistry&);
void initializeScalarEvolutionPass(PassRegistry&);
void initializeScalarEvolutionWrapperPassPass(PassRegistry&);
void initializeShrinkWrapPass(PassRegistry &);
void initializeSimpleInlinerPass(PassRegistry&);
void initializeShadowStackGCLoweringPass(PassRegistry&);
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/LinkAllPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ namespace {
(void) llvm::createEliminateAvailableExternallyPass();

(void)new llvm::IntervalPartition();
(void)new llvm::ScalarEvolution();
(void)new llvm::ScalarEvolutionWrapperPass();
((llvm::Function*)nullptr)->viewCFGOnly();
llvm::RGPassManager RGM;
((llvm::RegionPass*)nullptr)->runOnRegion((llvm::Region*)nullptr, RGM);
Expand Down
2 changes: 1 addition & 1 deletion lib/Analysis/Analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void llvm::initializeAnalysis(PassRegistry &Registry) {
initializeRegionPrinterPass(Registry);
initializeRegionOnlyViewerPass(Registry);
initializeRegionOnlyPrinterPass(Registry);
initializeScalarEvolutionPass(Registry);
initializeScalarEvolutionWrapperPassPass(Registry);
initializeScalarEvolutionAliasAnalysisPass(Registry);
initializeTargetTransformInfoWrapperPassPass(Registry);
initializeTypeBasedAliasAnalysisPass(Registry);
Expand Down
4 changes: 2 additions & 2 deletions lib/Analysis/Delinearization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ class Delinearization : public FunctionPass {
void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<LoopInfoWrapperPass>();
AU.addRequired<ScalarEvolution>();
AU.addRequired<ScalarEvolutionWrapperPass>();
}

bool Delinearization::runOnFunction(Function &F) {
this->F = &F;
SE = &getAnalysis<ScalarEvolution>();
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Analysis/DependenceAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Delinearize("da-delinearize", cl::init(false), cl::Hidden, cl::ZeroOrMore,
INITIALIZE_PASS_BEGIN(DependenceAnalysis, "da",
"Dependence Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
INITIALIZE_PASS_END(DependenceAnalysis, "da",
"Dependence Analysis", true, true)
Expand All @@ -133,7 +133,7 @@ FunctionPass *llvm::createDependenceAnalysisPass() {
bool DependenceAnalysis::runOnFunction(Function &F) {
this->F = &F;
AA = &getAnalysis<AliasAnalysis>();
SE = &getAnalysis<ScalarEvolution>();
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
return false;
}
Expand All @@ -146,7 +146,7 @@ void DependenceAnalysis::releaseMemory() {
void DependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequiredTransitive<AliasAnalysis>();
AU.addRequiredTransitive<ScalarEvolution>();
AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
AU.addRequiredTransitive<LoopInfoWrapperPass>();
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Analysis/IVUsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ INITIALIZE_PASS_BEGIN(IVUsers, "iv-users",
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
INITIALIZE_PASS_END(IVUsers, "iv-users",
"Induction Variable Users", false, true)

Expand Down Expand Up @@ -255,7 +255,7 @@ void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<AssumptionCacheTracker>();
AU.addRequired<LoopInfoWrapperPass>();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<ScalarEvolution>();
AU.addRequired<ScalarEvolutionWrapperPass>();
AU.setPreservesAll();
}

Expand All @@ -266,7 +266,7 @@ bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
*L->getHeader()->getParent());
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
SE = &getAnalysis<ScalarEvolution>();
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();

// Collect ephemeral values so that AddUsersIfInteresting skips them.
EphValues.clear();
Expand Down
6 changes: 3 additions & 3 deletions lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,7 @@ void LoopAccessAnalysis::print(raw_ostream &OS, const Module *M) const {
}

bool LoopAccessAnalysis::runOnFunction(Function &F) {
SE = &getAnalysis<ScalarEvolution>();
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
TLI = TLIP ? &TLIP->getTLI() : nullptr;
AA = &getAnalysis<AliasAnalysis>();
Expand All @@ -1805,7 +1805,7 @@ bool LoopAccessAnalysis::runOnFunction(Function &F) {
}

void LoopAccessAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<ScalarEvolution>();
AU.addRequired<ScalarEvolutionWrapperPass>();
AU.addRequired<AliasAnalysis>();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<LoopInfoWrapperPass>();
Expand All @@ -1819,7 +1819,7 @@ static const char laa_name[] = "Loop Access Analysis";

INITIALIZE_PASS_BEGIN(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_END(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
Expand Down
Loading

0 comments on commit bfe1f1c

Please sign in to comment.