diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -10718,7 +10718,11 @@ const SCEVAddRecExpr *LHS, const SCEV *RHS) { const Loop *L = LHS->getLoop(); - return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) && + const SCEV *Start = LHS->getStart(); + if (!isAvailableAtLoopEntry(Start, L)) + return false; + + return isLoopEntryGuardedByCond(L, Pred, Start, RHS) && isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this), RHS); } diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp --- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -1282,6 +1282,7 @@ MadeAnyChanges = true; ToMove->moveBefore(*ExitBlock, InsertPt); SE->forgetBlockAndLoopDispositions(ToMove); + SE->forgetValue(ToMove); if (Done) break; InsertPt = ToMove->getIterator(); }