Optimize ScalarEvolution::getAddExpr's operand factoring code by
authorDan Gohman <gohman@apple.com>
Thu, 12 Aug 2010 15:00:23 +0000 (15:00 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 12 Aug 2010 15:00:23 +0000 (15:00 +0000)
having it finish processing all of the muliply operands before
starting the whole getAddExpr process over again, instead of
immediately after the first simplification.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@110916 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ScalarEvolution.cpp

index 1803c3c7b5dff222f3cccc795fe4dfafe2bf0361..394e0159db8a059a657c1ed3afb5516f838ccee9 100644 (file)
@@ -1541,6 +1541,7 @@ const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
         }
 
       // Check this multiply against other multiplies being added together.
+      bool AnyFold = false;
       for (unsigned OtherMulIdx = Idx+1;
            OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
            ++OtherMulIdx) {
@@ -1568,12 +1569,14 @@ const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &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);
     }
   }