2006-05-27 05:11:53 +08:00
|
|
|
//===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
|
2006-05-26 21:58:26 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-05-26 21:58:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass transforms loops by placing phi nodes at the end of the loops for
|
|
|
|
// all values that are live across the loop boundary. For example, it turns
|
|
|
|
// the left into the right code:
|
|
|
|
//
|
|
|
|
// for (...) for (...)
|
2007-05-12 05:10:54 +08:00
|
|
|
// if (c) if (c)
|
2006-05-26 21:58:26 +08:00
|
|
|
// X1 = ... X1 = ...
|
|
|
|
// else else
|
|
|
|
// X2 = ... X2 = ...
|
|
|
|
// X3 = phi(X1, X2) X3 = phi(X1, X2)
|
2008-06-03 08:57:21 +08:00
|
|
|
// ... = X3 + 4 X4 = phi(X3)
|
|
|
|
// ... = X4 + 4
|
2006-05-26 21:58:26 +08:00
|
|
|
//
|
|
|
|
// This is still valid LLVM; the extra phi nodes are purely redundant, and will
|
|
|
|
// be trivially eliminated by InstCombine. The major benefit of this
|
|
|
|
// transformation is that it makes many other loop optimizations, such as
|
|
|
|
// LoopUnswitching, simpler.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-06-10 03:44:46 +08:00
|
|
|
#include "llvm/Transforms/Utils/LCSSA.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2014-02-11 03:39:35 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2016-02-19 11:12:14 +08:00
|
|
|
#include "llvm/Analysis/BasicAliasAnalysis.h"
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2007-07-14 07:57:11 +08:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2014-03-04 20:09:19 +08:00
|
|
|
#include "llvm/IR/PredIteratorCache.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2016-06-10 03:44:46 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2014-01-25 12:07:24 +08:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2006-05-26 21:58:26 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "lcssa"
|
|
|
|
|
2006-12-20 06:17:40 +08:00
|
|
|
STATISTIC(NumLCSSA, "Number of live out of a loop variables");
|
|
|
|
|
2014-01-25 12:07:24 +08:00
|
|
|
/// Return true if the specified block is in the list.
|
2009-10-11 10:53:37 +08:00
|
|
|
static bool isExitBlock(BasicBlock *BB,
|
2014-01-25 12:07:24 +08:00
|
|
|
const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
|
2016-05-28 04:25:31 +08:00
|
|
|
return find(ExitBlocks, BB) != ExitBlocks.end();
|
2009-10-11 10:53:37 +08:00
|
|
|
}
|
2006-08-02 08:06:09 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
/// For every instruction from the worklist, check to see if it has any uses
|
|
|
|
/// that are outside the current loop. If so, insert LCSSA PHI nodes and
|
|
|
|
/// rewrite the uses.
|
|
|
|
bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
|
|
|
|
DominatorTree &DT, LoopInfo &LI) {
|
2014-01-25 12:07:24 +08:00
|
|
|
SmallVector<Use *, 16> UsesToRewrite;
|
2016-07-16 05:08:41 +08:00
|
|
|
SmallVector<BasicBlock *, 8> ExitBlocks;
|
2016-07-20 09:55:27 +08:00
|
|
|
SmallSetVector<PHINode *, 16> PHIsToRemove;
|
2016-07-16 05:08:41 +08:00
|
|
|
PredIteratorCache PredCache;
|
|
|
|
bool Changed = false;
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
while (!Worklist.empty()) {
|
|
|
|
UsesToRewrite.clear();
|
|
|
|
ExitBlocks.clear();
|
2015-12-19 02:12:35 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
Instruction *I = Worklist.pop_back_val();
|
|
|
|
BasicBlock *InstBB = I->getParent();
|
|
|
|
Loop *L = LI.getLoopFor(InstBB);
|
|
|
|
L->getExitBlocks(ExitBlocks);
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
if (ExitBlocks.empty())
|
|
|
|
continue;
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// Tokens cannot be used in PHI nodes, so we skip over them.
|
|
|
|
// We can run into tokens which are live out of a loop with catchswitch
|
|
|
|
// instructions in Windows EH if the catchswitch has one catchpad which
|
|
|
|
// is inside the loop and another which is not.
|
|
|
|
if (I->getType()->isTokenTy())
|
|
|
|
continue;
|
2010-07-09 22:29:14 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
for (Use &U : I->uses()) {
|
|
|
|
Instruction *User = cast<Instruction>(U.getUser());
|
|
|
|
BasicBlock *UserBB = User->getParent();
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(User))
|
|
|
|
UserBB = PN->getIncomingBlock(U);
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
if (InstBB != UserBB && !L->contains(UserBB))
|
|
|
|
UsesToRewrite.push_back(&U);
|
|
|
|
}
|
2009-06-26 08:31:13 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// If there are no uses outside the loop, exit with no change.
|
|
|
|
if (UsesToRewrite.empty())
|
|
|
|
continue;
|
2009-06-26 08:31:13 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
++NumLCSSA; // We are applying the transformation
|
2006-08-02 08:06:09 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// Invoke instructions are special in that their result value is not
|
|
|
|
// available along their unwind edge. The code below tests to see whether
|
|
|
|
// DomBB dominates the value, so adjust DomBB to the normal destination
|
|
|
|
// block, which is effectively where the value is first usable.
|
|
|
|
BasicBlock *DomBB = InstBB;
|
|
|
|
if (InvokeInst *Inv = dyn_cast<InvokeInst>(I))
|
|
|
|
DomBB = Inv->getNormalDest();
|
2011-03-15 15:41:25 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
DomTreeNode *DomNode = DT.getNode(DomBB);
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
SmallVector<PHINode *, 16> AddedPHIs;
|
|
|
|
SmallVector<PHINode *, 8> PostProcessPHIs;
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-07-20 09:55:27 +08:00
|
|
|
SmallVector<PHINode *, 4> InsertedPHIs;
|
|
|
|
SSAUpdater SSAUpdate(&InsertedPHIs);
|
2016-07-16 05:08:41 +08:00
|
|
|
SSAUpdate.Initialize(I->getType(), I->getName());
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// Insert the LCSSA phi's into all of the exit blocks dominated by the
|
|
|
|
// value, and add them to the Phi's map.
|
|
|
|
for (BasicBlock *ExitBB : ExitBlocks) {
|
|
|
|
if (!DT.dominates(DomNode, DT.getNode(ExitBB)))
|
|
|
|
continue;
|
2006-08-02 08:06:09 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// If we already inserted something for this BB, don't reprocess it.
|
|
|
|
if (SSAUpdate.HasValueForBlock(ExitBB))
|
|
|
|
continue;
|
2009-11-10 02:28:24 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
PHINode *PN = PHINode::Create(I->getType(), PredCache.size(ExitBB),
|
|
|
|
I->getName() + ".lcssa", &ExitBB->front());
|
|
|
|
|
|
|
|
// Add inputs from inside the loop for this PHI.
|
|
|
|
for (BasicBlock *Pred : PredCache.get(ExitBB)) {
|
|
|
|
PN->addIncoming(I, Pred);
|
|
|
|
|
|
|
|
// If the exit block has a predecessor not within the loop, arrange for
|
|
|
|
// the incoming value use corresponding to that predecessor to be
|
|
|
|
// rewritten in terms of a different LCSSA PHI.
|
|
|
|
if (!L->contains(Pred))
|
|
|
|
UsesToRewrite.push_back(
|
|
|
|
&PN->getOperandUse(PN->getOperandNumForIncomingValue(
|
|
|
|
PN->getNumIncomingValues() - 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
AddedPHIs.push_back(PN);
|
|
|
|
|
|
|
|
// Remember that this phi makes the value alive in this block.
|
|
|
|
SSAUpdate.AddAvailableValue(ExitBB, PN);
|
|
|
|
|
|
|
|
// LoopSimplify might fail to simplify some loops (e.g. when indirect
|
|
|
|
// branches are involved). In such situations, it might happen that an
|
|
|
|
// exit for Loop L1 is the header of a disjoint Loop L2. Thus, when we
|
|
|
|
// create PHIs in such an exit block, we are also inserting PHIs into L2's
|
|
|
|
// header. This could break LCSSA form for L2 because these inserted PHIs
|
|
|
|
// can also have uses outside of L2. Remember all PHIs in such situation
|
|
|
|
// as to revisit than later on. FIXME: Remove this if indirectbr support
|
|
|
|
// into LoopSimplify gets improved.
|
|
|
|
if (auto *OtherLoop = LI.getLoopFor(ExitBB))
|
|
|
|
if (!L->contains(OtherLoop))
|
|
|
|
PostProcessPHIs.push_back(PN);
|
2009-11-10 02:28:24 +08:00
|
|
|
}
|
2011-03-15 15:41:25 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// Rewrite all uses outside the loop in terms of the new PHIs we just
|
|
|
|
// inserted.
|
|
|
|
for (Use *UseToRewrite : UsesToRewrite) {
|
|
|
|
// If this use is in an exit block, rewrite to use the newly inserted PHI.
|
|
|
|
// This is required for correctness because SSAUpdate doesn't handle uses
|
|
|
|
// in the same block. It assumes the PHI we inserted is at the end of the
|
|
|
|
// block.
|
|
|
|
Instruction *User = cast<Instruction>(UseToRewrite->getUser());
|
|
|
|
BasicBlock *UserBB = User->getParent();
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(User))
|
|
|
|
UserBB = PN->getIncomingBlock(*UseToRewrite);
|
|
|
|
|
|
|
|
if (isa<PHINode>(UserBB->begin()) && isExitBlock(UserBB, ExitBlocks)) {
|
|
|
|
// Tell the VHs that the uses changed. This updates SCEV's caches.
|
|
|
|
if (UseToRewrite->get()->hasValueHandle())
|
|
|
|
ValueHandleBase::ValueIsRAUWd(*UseToRewrite, &UserBB->front());
|
|
|
|
UseToRewrite->set(&UserBB->front());
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-31 18:01:29 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// Otherwise, do full PHI insertion.
|
|
|
|
SSAUpdate.RewriteUse(*UseToRewrite);
|
2016-07-20 09:55:27 +08:00
|
|
|
|
|
|
|
// SSAUpdater might have inserted phi-nodes inside other loops. We'll need
|
|
|
|
// to post-process them to keep LCSSA form.
|
|
|
|
for (PHINode *InsertedPN : InsertedPHIs) {
|
|
|
|
if (auto *OtherLoop = LI.getLoopFor(InsertedPN->getParent()))
|
|
|
|
if (!L->contains(OtherLoop))
|
|
|
|
PostProcessPHIs.push_back(InsertedPN);
|
|
|
|
}
|
2006-06-01 14:05:47 +08:00
|
|
|
}
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// Post process PHI instructions that were inserted into another disjoint
|
|
|
|
// loop and update their exits properly.
|
|
|
|
for (auto *PostProcessPN : PostProcessPHIs) {
|
|
|
|
if (PostProcessPN->use_empty())
|
|
|
|
continue;
|
2011-03-15 15:41:25 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
// Reprocess each PHI instruction.
|
|
|
|
Worklist.push_back(PostProcessPN);
|
|
|
|
}
|
2014-12-23 06:35:46 +08:00
|
|
|
|
2016-07-20 09:55:27 +08:00
|
|
|
// Keep track of PHI nodes that we want to remove because they did not have
|
|
|
|
// any uses rewritten.
|
2016-07-16 05:08:41 +08:00
|
|
|
for (PHINode *PN : AddedPHIs)
|
|
|
|
if (PN->use_empty())
|
2016-07-20 09:55:27 +08:00
|
|
|
PHIsToRemove.insert(PN);
|
2014-12-23 06:35:46 +08:00
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
Changed = true;
|
2014-12-23 06:35:46 +08:00
|
|
|
}
|
2016-07-20 09:55:27 +08:00
|
|
|
// Remove PHI nodes that did not have any uses rewritten.
|
|
|
|
for (PHINode *PN : PHIsToRemove) {
|
|
|
|
assert (PN->use_empty() && "Trying to remove a phi with uses.");
|
|
|
|
PN->eraseFromParent();
|
|
|
|
}
|
2016-07-16 05:08:41 +08:00
|
|
|
return Changed;
|
2006-06-01 04:55:06 +08:00
|
|
|
}
|
2006-08-02 08:06:09 +08:00
|
|
|
|
2014-01-25 12:07:24 +08:00
|
|
|
/// Return true if the specified block dominates at least
|
|
|
|
/// one of the blocks in the specified list.
|
|
|
|
static bool
|
|
|
|
blockDominatesAnExit(BasicBlock *BB,
|
|
|
|
DominatorTree &DT,
|
|
|
|
const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
|
|
|
|
DomTreeNode *DomNode = DT.getNode(BB);
|
2016-05-18 03:01:02 +08:00
|
|
|
return llvm::any_of(ExitBlocks, [&](BasicBlock * EB) {
|
2016-05-17 22:24:41 +08:00
|
|
|
return DT.dominates(DomNode, DT.getNode(EB));
|
|
|
|
});
|
2014-01-25 12:07:24 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 06:35:46 +08:00
|
|
|
bool llvm::formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI,
|
|
|
|
ScalarEvolution *SE) {
|
2014-01-25 12:07:24 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Get the set of exiting blocks.
|
|
|
|
SmallVector<BasicBlock *, 8> ExitBlocks;
|
|
|
|
L.getExitBlocks(ExitBlocks);
|
|
|
|
|
|
|
|
if (ExitBlocks.empty())
|
|
|
|
return false;
|
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
SmallVector<Instruction *, 8> Worklist;
|
2014-01-25 12:07:24 +08:00
|
|
|
|
|
|
|
// Look at all the instructions in the loop, checking to see if they have uses
|
2016-07-16 05:08:41 +08:00
|
|
|
// outside the loop. If so, put them into the worklist to rewrite those uses.
|
2015-10-26 03:08:32 +08:00
|
|
|
for (BasicBlock *BB : L.blocks()) {
|
2014-01-25 12:07:24 +08:00
|
|
|
// For large loops, avoid use-scanning by using dominance information: In
|
|
|
|
// particular, if a block does not dominate any of the loop exits, then none
|
|
|
|
// of the values defined in the block could be used outside the loop.
|
|
|
|
if (!blockDominatesAnExit(BB, DT, ExitBlocks))
|
|
|
|
continue;
|
|
|
|
|
2015-10-26 03:08:32 +08:00
|
|
|
for (Instruction &I : *BB) {
|
2014-01-25 12:07:24 +08:00
|
|
|
// Reject two common cases fast: instructions with no uses (like stores)
|
|
|
|
// and instructions with one use that is in the same block as this.
|
2015-10-26 03:08:32 +08:00
|
|
|
if (I.use_empty() ||
|
|
|
|
(I.hasOneUse() && I.user_back()->getParent() == BB &&
|
|
|
|
!isa<PHINode>(I.user_back())))
|
2014-01-25 12:07:24 +08:00
|
|
|
continue;
|
|
|
|
|
2016-07-16 05:08:41 +08:00
|
|
|
Worklist.push_back(&I);
|
2014-01-25 12:07:24 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-16 05:08:41 +08:00
|
|
|
Changed = formLCSSAForInstructions(Worklist, DT, *LI);
|
2014-01-25 12:07:24 +08:00
|
|
|
|
|
|
|
// If we modified the code, remove any caches about the loop from SCEV to
|
|
|
|
// avoid dangling entries.
|
|
|
|
// FIXME: This is a big hammer, can we clear the cache more selectively?
|
|
|
|
if (SE && Changed)
|
|
|
|
SE->forgetLoop(&L);
|
|
|
|
|
|
|
|
assert(L.isLCSSAForm(DT));
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2014-01-28 09:25:38 +08:00
|
|
|
/// Process a loop nest depth first.
|
2014-12-23 06:35:46 +08:00
|
|
|
bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI,
|
2014-01-28 09:25:38 +08:00
|
|
|
ScalarEvolution *SE) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Recurse depth-first through inner loops.
|
2015-10-26 03:27:17 +08:00
|
|
|
for (Loop *SubLoop : L.getSubLoops())
|
|
|
|
Changed |= formLCSSARecursively(*SubLoop, DT, LI, SE);
|
2014-01-28 09:25:38 +08:00
|
|
|
|
2014-12-23 06:35:46 +08:00
|
|
|
Changed |= formLCSSA(L, DT, LI, SE);
|
2014-01-28 09:25:38 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2016-06-10 03:44:46 +08:00
|
|
|
/// Process all loops in the function, inner-most out.
|
|
|
|
static bool formLCSSAOnAllLoops(LoopInfo *LI, DominatorTree &DT,
|
|
|
|
ScalarEvolution *SE) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto &L : *LI)
|
|
|
|
Changed |= formLCSSARecursively(*L, DT, LI, SE);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2014-01-25 12:07:24 +08:00
|
|
|
namespace {
|
2016-06-10 03:44:46 +08:00
|
|
|
struct LCSSAWrapperPass : public FunctionPass {
|
2014-01-25 12:07:24 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2016-06-10 03:44:46 +08:00
|
|
|
LCSSAWrapperPass() : FunctionPass(ID) {
|
|
|
|
initializeLCSSAWrapperPassPass(*PassRegistry::getPassRegistry());
|
2014-01-25 12:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cached analysis information for the current function.
|
|
|
|
DominatorTree *DT;
|
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
2016-07-28 07:35:53 +08:00
|
|
|
void verifyAnalysis() const override {
|
|
|
|
assert(
|
|
|
|
all_of(*LI, [&](Loop *L) { return L->isRecursivelyLCSSAForm(*DT); }) &&
|
|
|
|
"LCSSA form is broken!");
|
|
|
|
};
|
2014-01-25 12:07:24 +08:00
|
|
|
|
|
|
|
/// This transformation requires natural loop information & requires that
|
|
|
|
/// loop preheaders be inserted into the CFG. It maintains both of these,
|
|
|
|
/// as well as the CFG. It also requires dominator information.
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-25 12:07:24 +08:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2015-01-17 22:16:18 +08:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
2014-01-25 12:07:24 +08:00
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
AU.addPreserved<AAResultsWrapperPass>();
|
2016-02-19 11:12:14 +08:00
|
|
|
AU.addPreserved<BasicAAWrapperPass>();
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
[PM] Port ScalarEvolution to the new pass manager.
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
llvm-svn: 245193
2015-08-17 10:08:17 +08:00
|
|
|
AU.addPreserved<ScalarEvolutionWrapperPass>();
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
AU.addPreserved<SCEVAAWrapperPass>();
|
2014-01-25 12:07:24 +08:00
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-06-10 03:44:46 +08:00
|
|
|
char LCSSAWrapperPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LCSSAWrapperPass, "lcssa", "Loop-Closed SSA Form Pass",
|
|
|
|
false, false)
|
2014-01-25 12:07:24 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2015-01-17 22:16:18 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
2016-06-10 03:44:46 +08:00
|
|
|
INITIALIZE_PASS_END(LCSSAWrapperPass, "lcssa", "Loop-Closed SSA Form Pass",
|
|
|
|
false, false)
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-06-10 03:44:46 +08:00
|
|
|
Pass *llvm::createLCSSAPass() { return new LCSSAWrapperPass(); }
|
|
|
|
char &llvm::LCSSAID = LCSSAWrapperPass::ID;
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-06-10 03:44:46 +08:00
|
|
|
/// Transform \p F into loop-closed SSA form.
|
|
|
|
bool LCSSAWrapperPass::runOnFunction(Function &F) {
|
2015-01-17 22:16:18 +08:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2014-01-25 12:07:24 +08:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
[PM] Port ScalarEvolution to the new pass manager.
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
llvm-svn: 245193
2015-08-17 10:08:17 +08:00
|
|
|
auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
|
|
|
|
SE = SEWP ? &SEWP->getSE() : nullptr;
|
2014-01-25 12:07:24 +08:00
|
|
|
|
2016-06-10 03:44:46 +08:00
|
|
|
return formLCSSAOnAllLoops(LI, *DT, SE);
|
2014-01-25 12:07:24 +08:00
|
|
|
}
|
|
|
|
|
2016-08-09 08:28:15 +08:00
|
|
|
PreservedAnalyses LCSSAPass::run(Function &F, FunctionAnalysisManager &AM) {
|
2016-06-10 03:44:46 +08:00
|
|
|
auto &LI = AM.getResult<LoopAnalysis>(F);
|
|
|
|
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
|
|
|
|
auto *SE = AM.getCachedResult<ScalarEvolutionAnalysis>(F);
|
|
|
|
if (!formLCSSAOnAllLoops(&LI, DT, SE))
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
2016-06-28 08:54:12 +08:00
|
|
|
// FIXME: This should also 'preserve the CFG'.
|
2016-06-10 03:44:46 +08:00
|
|
|
PreservedAnalyses PA;
|
|
|
|
PA.preserve<BasicAA>();
|
|
|
|
PA.preserve<GlobalsAA>();
|
|
|
|
PA.preserve<SCEVAA>();
|
|
|
|
PA.preserve<ScalarEvolutionAnalysis>();
|
|
|
|
return PA;
|
|
|
|
}
|