@@ -1731,49 +1731,55 @@ Value *SCEVExpander::expand(const SCEV *S) {
1731
1731
// Compute an insertion point for this SCEV object. Hoist the instructions
1732
1732
// as far out in the loop nest as possible.
1733
1733
Instruction *InsertPt = &*Builder.GetInsertPoint ();
1734
- for (Loop *L = SE.LI .getLoopFor (Builder.GetInsertBlock ());;
1735
- L = L->getParentLoop ())
1736
- if (SE.isLoopInvariant (S, L)) {
1737
- if (!L) break ;
1738
- if (BasicBlock *Preheader = L->getLoopPreheader ())
1739
- InsertPt = Preheader->getTerminator ();
1740
- else {
1741
- // LSR sets the insertion point for AddRec start/step values to the
1742
- // block start to simplify value reuse, even though it's an invalid
1743
- // position. SCEVExpander must correct for this in all cases.
1744
- InsertPt = &*L->getHeader ()->getFirstInsertionPt ();
1745
- }
1746
- } else {
1747
- // We can move insertion point only if there is no div or rem operations
1748
- // otherwise we are risky to move it over the check for zero denominator.
1749
- auto SafeToHoist = [](const SCEV *S) {
1750
- return !SCEVExprContains (S, [](const SCEV *S) {
1751
- if (const auto *D = dyn_cast<SCEVUDivExpr>(S)) {
1752
- if (const auto *SC = dyn_cast<SCEVConstant>(D->getRHS ()))
1753
- // Division by non-zero constants can be hoisted.
1754
- return SC->getValue ()->isZero ();
1755
- // All other divisions should not be moved as they may be
1756
- // divisions by zero and should be kept within the
1757
- // conditions of the surrounding loops that guard their
1758
- // execution (see PR35406).
1759
- return true ;
1760
- }
1761
- return false ;
1762
- });
1763
- };
1764
- // If the SCEV is computable at this level, insert it into the header
1765
- // after the PHIs (and after any other instructions that we've inserted
1766
- // there) so that it is guaranteed to dominate any user inside the loop.
1767
- if (L && SE.hasComputableLoopEvolution (S, L) && !PostIncLoops.count (L) &&
1768
- SafeToHoist (S))
1769
- InsertPt = &*L->getHeader ()->getFirstInsertionPt ();
1770
- while (InsertPt->getIterator () != Builder.GetInsertPoint () &&
1771
- (isInsertedInstruction (InsertPt) ||
1772
- isa<DbgInfoIntrinsic>(InsertPt))) {
1773
- InsertPt = &*std::next (InsertPt->getIterator ());
1734
+
1735
+ // We can move insertion point only if there is no div or rem operations
1736
+ // otherwise we are risky to move it over the check for zero denominator.
1737
+ auto SafeToHoist = [](const SCEV *S) {
1738
+ return !SCEVExprContains (S, [](const SCEV *S) {
1739
+ if (const auto *D = dyn_cast<SCEVUDivExpr>(S)) {
1740
+ if (const auto *SC = dyn_cast<SCEVConstant>(D->getRHS ()))
1741
+ // Division by non-zero constants can be hoisted.
1742
+ return SC->getValue ()->isZero ();
1743
+ // All other divisions should not be moved as they may be
1744
+ // divisions by zero and should be kept within the
1745
+ // conditions of the surrounding loops that guard their
1746
+ // execution (see PR35406).
1747
+ return true ;
1748
+ }
1749
+ return false ;
1750
+ });
1751
+ };
1752
+ if (SafeToHoist (S)) {
1753
+ for (Loop *L = SE.LI .getLoopFor (Builder.GetInsertBlock ());;
1754
+ L = L->getParentLoop ()) {
1755
+ if (SE.isLoopInvariant (S, L)) {
1756
+ if (!L) break ;
1757
+ if (BasicBlock *Preheader = L->getLoopPreheader ())
1758
+ InsertPt = Preheader->getTerminator ();
1759
+ else
1760
+ // LSR sets the insertion point for AddRec start/step values to the
1761
+ // block start to simplify value reuse, even though it's an invalid
1762
+ // position. SCEVExpander must correct for this in all cases.
1763
+ InsertPt = &*L->getHeader ()->getFirstInsertionPt ();
1764
+ } else {
1765
+ // If the SCEV is computable at this level, insert it into the header
1766
+ // after the PHIs (and after any other instructions that we've inserted
1767
+ // there) so that it is guaranteed to dominate any user inside the loop.
1768
+ if (L && SE.hasComputableLoopEvolution (S, L) && !PostIncLoops.count (L))
1769
+ InsertPt = &*L->getHeader ()->getFirstInsertionPt ();
1770
+ while (InsertPt->getIterator () != Builder.GetInsertPoint () &&
1771
+ (isInsertedInstruction (InsertPt) ||
1772
+ isa<DbgInfoIntrinsic>(InsertPt)))
1773
+ InsertPt = &*std::next (InsertPt->getIterator ());
1774
+ break ;
1774
1775
}
1775
- break ;
1776
1776
}
1777
+ }
1778
+
1779
+ // IndVarSimplify sometimes sets the insertion point at the block start, even
1780
+ // when there are PHIs at that point. We must correct for this.
1781
+ if (isa<PHINode>(*InsertPt))
1782
+ InsertPt = &*InsertPt->getParent ()->getFirstInsertionPt ();
1777
1783
1778
1784
// Check to see if we already expanded this here.
1779
1785
auto I = InsertedExpressions.find (std::make_pair (S, InsertPt));
0 commit comments