diff options
author | Serguei Katkov <serguei.katkov@azul.com> | 2017-12-15 05:24:42 +0000 |
---|---|---|
committer | Serguei Katkov <serguei.katkov@azul.com> | 2017-12-15 05:24:42 +0000 |
commit | 67da7696a04d9c2026a463f3ee2fc45e0e33010c (patch) | |
tree | df0e629deb2d32e02cd175b5232066b022cce2c5 /llvm/lib/Analysis/ScalarEvolutionExpander.cpp | |
parent | c41e2f6e7b71530bf1b3fed95e0f67ddbba29d3f (diff) | |
download | bcm5719-llvm-67da7696a04d9c2026a463f3ee2fc45e0e33010c.tar.gz bcm5719-llvm-67da7696a04d9c2026a463f3ee2fc45e0e33010c.zip |
[SCEV] Fix the movement of insertion point in expander. PR35406.
We cannot move the insertion point to header if SCEV contains div/rem
operations due to they may go over check for zero denominator.
Reviewers: sanjoy, mkazantsev, sebpop
Reviewed By: sebpop
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D41229
llvm-svn: 320789
Diffstat (limited to 'llvm/lib/Analysis/ScalarEvolutionExpander.cpp')
-rw-r--r-- | llvm/lib/Analysis/ScalarEvolutionExpander.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp index 0caff18264d..86f714b930d 100644 --- a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp @@ -1732,10 +1732,28 @@ Value *SCEVExpander::expand(const SCEV *S) { InsertPt = &*L->getHeader()->getFirstInsertionPt(); } } else { + // We can move insertion point only if there is no div or rem operations + // otherwise we are risky to move it over the check for zero denominator. + auto SafeToHoist = [](const SCEV *S) { + return !SCEVExprContains(S, [](const SCEV *S) { + if (const auto *D = dyn_cast<SCEVUDivExpr>(S)) { + if (const auto *SC = dyn_cast<SCEVConstant>(D->getRHS())) + // Division by non-zero constants can be hoisted. + return SC->getValue()->isZero(); + // All other divisions should not be moved as they may be + // divisions by zero and should be kept within the + // conditions of the surrounding loops that guard their + // execution (see PR35406). + return true; + } + return false; + }); + }; // If the SCEV is computable at this level, insert it into the header // after the PHIs (and after any other instructions that we've inserted // there) so that it is guaranteed to dominate any user inside the loop. - if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L)) + if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L) && + SafeToHoist(S)) InsertPt = &*L->getHeader()->getFirstInsertionPt(); while (InsertPt->getIterator() != Builder.GetInsertPoint() && (isInsertedInstruction(InsertPt) || |