diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2021-08-30 12:27:41 +0300 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2021-08-30 12:28:24 +0300 |
commit | 795d142d237e0a06c136886a6dd9c49235c37295 (patch) | |
tree | 5573117a52fc18909ba0eb83886b3988c3d3be19 /llvm/lib/Transforms/Utils/LoopUtils.cpp | |
parent | b1b68b4c0190b7fc7f0ebe823277966056b39494 (diff) | |
download | llvm-795d142d237e0a06c136886a6dd9c49235c37295.zip llvm-795d142d237e0a06c136886a6dd9c49235c37295.tar.gz llvm-795d142d237e0a06c136886a6dd9c49235c37295.tar.bz2 |
[NFCI][IndVars] rewriteLoopExitValues(): don't expand SCEV's until needed
Previously, we'd expand *ALL* the SCEV's eagerly, because we needed to
check with `isValidRewrite()`, and discard bad rewrite candidates,
but now that we do not do that, we also don't need to always expand.
In particular, this avoids expanding potentially-huge SCEV's that we
would discard anyways because they are high-cost and we aren't
rewriting aggressively.
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUtils.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/LoopUtils.cpp | 54 |
1 files changed, 22 insertions, 32 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index 25d22f1..36d8bc5 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -1142,8 +1142,6 @@ struct RewritePhi { Instruction *ExpansionPoint; // Where we'd like to expand that SCEV? bool HighCost; // Is this expansion a high-cost? - Value *Expansion = nullptr; - RewritePhi(PHINode *P, unsigned I, const SCEV *Val, Instruction *ExpansionPt, bool H) : PN(P), Ith(I), ExpansionSCEV(Val), ExpansionPoint(ExpansionPt), @@ -1313,32 +1311,10 @@ int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, } } - // Now that we've done preliminary filtering and billed all the SCEV's, - // we can perform the last sanity check - the expansion must be valid. - for (RewritePhi &Phi : RewritePhiSet) { - Phi.Expansion = Rewriter.expandCodeFor(Phi.ExpansionSCEV, Phi.PN->getType(), - Phi.ExpansionPoint); - - LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: AfterLoopVal = " - << *(Phi.Expansion) << '\n' - << " LoopVal = " << *(Phi.ExpansionPoint) << "\n"); - -#ifndef NDEBUG - // If we reuse an instruction from a loop which is neither L nor one of - // its containing loops, we end up breaking LCSSA form for this loop by - // creating a new use of its instruction. - if (auto *ExitInsn = dyn_cast<Instruction>(Phi.Expansion)) - if (auto *EVL = LI->getLoopFor(ExitInsn->getParent())) - if (EVL != L) - assert(EVL->contains(L) && "LCSSA breach detected!"); -#endif - } - - // TODO: after isValidRewrite() is an assertion, evaluate whether - // it is beneficial to change how we calculate high-cost: - // if we have SCEV 'A' which we know we will expand, should we calculate - // the cost of other SCEV's after expanding SCEV 'A', - // thus potentially giving cost bonus to those other SCEV's? + // TODO: evaluate whether it is beneficial to change how we calculate + // high-cost: if we have SCEV 'A' which we know we will expand, should we + // calculate the cost of other SCEV's after expanding SCEV 'A', thus + // potentially giving cost bonus to those other SCEV's? bool LoopCanBeDel = canLoopBeDeleted(L, RewritePhiSet); int NumReplaced = 0; @@ -1346,14 +1322,28 @@ int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, // Transformation. for (const RewritePhi &Phi : RewritePhiSet) { PHINode *PN = Phi.PN; - Value *ExitVal = Phi.Expansion; // Only do the rewrite when the ExitValue can be expanded cheaply. // If LoopCanBeDel is true, rewrite exit value aggressively. - if (ReplaceExitValue == OnlyCheapRepl && !LoopCanBeDel && Phi.HighCost) { - DeadInsts.push_back(ExitVal); + if (ReplaceExitValue == OnlyCheapRepl && !LoopCanBeDel && Phi.HighCost) continue; - } + + Value *ExitVal = Rewriter.expandCodeFor( + Phi.ExpansionSCEV, Phi.PN->getType(), Phi.ExpansionPoint); + + LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: AfterLoopVal = " << *ExitVal + << '\n' + << " LoopVal = " << *(Phi.ExpansionPoint) << "\n"); + +#ifndef NDEBUG + // If we reuse an instruction from a loop which is neither L nor one of + // its containing loops, we end up breaking LCSSA form for this loop by + // creating a new use of its instruction. + if (auto *ExitInsn = dyn_cast<Instruction>(ExitVal)) + if (auto *EVL = LI->getLoopFor(ExitInsn->getParent())) + if (EVL != L) + assert(EVL->contains(L) && "LCSSA breach detected!"); +#endif NumReplaced++; Instruction *Inst = cast<Instruction>(PN->getIncomingValue(Phi.Ith)); |