diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 1803c3c7b5d..394e0159db8 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -1541,6 +1541,7 @@ const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl &Ops, } // Check this multiply against other multiplies being added together. + bool AnyFold = false; for (unsigned OtherMulIdx = Idx+1; OtherMulIdx < Ops.size() && isa(Ops[OtherMulIdx]); ++OtherMulIdx) { @@ -1568,12 +1569,14 @@ const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl &Ops, const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); if (Ops.size() == 2) return OuterMul; - Ops.erase(Ops.begin()+Idx); - Ops.erase(Ops.begin()+OtherMulIdx-1); - Ops.push_back(OuterMul); - return getAddExpr(Ops); + Ops[Idx] = OuterMul; + Ops.erase(Ops.begin()+OtherMulIdx); + OtherMulIdx = Idx; + AnyFold = true; } } + if (AnyFold) + return getAddExpr(Ops); } }