diff options
author | Dan Gohman <gohman@apple.com> | 2010-04-12 02:21:50 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2010-04-12 02:21:50 +0000 |
commit | 931e345e76e75391d2a7c96530e305f802b5429d (patch) | |
tree | 4530af5803a73c7046c06d1b84e8eed92c18b783 /lib/Transforms | |
parent | 17e8b7fbde5175c27c734f905959ebfae1e317b4 (diff) |
Re-apply r101000, with a fix: Don't eliminate an icmp which is part of
the loop exit test. This usually doesn't come up for a variety of
reasons, but it isn't impossible, so make IndVarSimplify handle it
conservatively.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@101008 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r-- | lib/Transforms/Scalar/IndVarSimplify.cpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index f87d69360c..5dbde99baa 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -97,6 +97,7 @@ namespace { private: + void EliminateIVComparisons(); void RewriteNonIntegerIVs(Loop *L); ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount, @@ -336,6 +337,40 @@ void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { SE->forgetLoop(L); } +void IndVarSimplify::EliminateIVComparisons() { + // Look for ICmp users. + for (IVUsers::iterator I = IU->begin(), E = IU->end(); I != E;) { + IVStrideUse &UI = *I++; + ICmpInst *ICmp = dyn_cast<ICmpInst>(UI.getUser()); + if (!ICmp) continue; + + bool Swapped = UI.getOperandValToReplace() == ICmp->getOperand(1); + ICmpInst::Predicate Pred = ICmp->getPredicate(); + if (Swapped) Pred = ICmpInst::getSwappedPredicate(Pred); + + // Get the SCEVs for the ICmp operands. + const SCEV *S = IU->getReplacementExpr(UI); + const SCEV *X = SE->getSCEV(ICmp->getOperand(!Swapped)); + + // Simplify unnecessary loops away. + const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); + S = SE->getSCEVAtScope(S, ICmpLoop); + X = SE->getSCEVAtScope(X, ICmpLoop); + + // If the condition is always true or always false, replace it with + // a constant value. + if (SE->isKnownPredicate(Pred, S, X)) + ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext())); + else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) + ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext())); + else + continue; + + DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); + ICmp->eraseFromParent(); + } +} + bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { IU = &getAnalysis<IVUsers>(); LI = &getAnalysis<LoopInfo>(); @@ -427,10 +462,19 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { ExitingBlock) { assert(NeedCannIV && "LinearFunctionTestReplace requires a canonical induction variable"); + // Can't rewrite non-branch yet. - if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) + if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) { + // Eliminate comparisons which are always true or always false, due to + // the known backedge-taken count. This may include comparisons which + // are currently controlling (part of) the loop exit, so we can only do + // it when we know we're going to insert our own loop exit code. + EliminateIVComparisons(); + + // Insert new loop exit code. NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar, ExitingBlock, BI, Rewriter); + } } // Rewrite IV-derived expressions. Clears the rewriter cache. |