SCEV: Handle a corner case reducing AddRecExpr * AddRecExpr

If integer overflow causes one of the terms to reach zero, that can
force the entire expression to zero.

Fixes PR12929: cast<Ty>() argument of incompatible type

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157673 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp
index 59f441e..8781441 100644
--- a/lib/Analysis/ScalarEvolution.cpp
+++ b/lib/Analysis/ScalarEvolution.cpp
@@ -2088,9 +2088,12 @@
           const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(),
                                                 SCEV::FlagAnyWrap);
           if (Ops.size() == 2) return NewAddRec;
-          Ops[Idx] = AddRec = cast<SCEVAddRecExpr>(NewAddRec);
+          Ops[Idx] = NewAddRec;
           Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
           OpsModified = true;
+          AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
+          if (!AddRec)
+            break;
         }
       }
       if (OpsModified)