[CVP] processOverflowIntrinsic(): don't crash if constant-holding happened

As reported by Mikael Holmén in post-commit review in
https://reviews.llvm.org/D60791#1469765

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358559 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index b650971..a56a37a 100644
--- a/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -416,10 +416,13 @@
   IRBuilder<> B(WO);
   Value *NewOp = B.CreateBinOp(
       WO->getBinaryOp(), WO->getLHS(), WO->getRHS(), WO->getName());
-  if (WO->isSigned())
-    cast<Instruction>(NewOp)->setHasNoSignedWrap();
-  else
-    cast<Instruction>(NewOp)->setHasNoUnsignedWrap();
+  // Constant-holing could have happened.
+  if (auto *Inst = dyn_cast<Instruction>(NewOp)) {
+    if (WO->isSigned())
+      Inst->setHasNoSignedWrap();
+    else
+      Inst->setHasNoUnsignedWrap();
+  }
 
   Value *NewI = B.CreateInsertValue(UndefValue::get(WO->getType()), NewOp, 0);
   NewI = B.CreateInsertValue(NewI, ConstantInt::getFalse(WO->getContext()), 1);
diff --git a/test/Transforms/CorrelatedValuePropagation/overflows.ll b/test/Transforms/CorrelatedValuePropagation/overflows.ll
index db758b8..9edf478 100644
--- a/test/Transforms/CorrelatedValuePropagation/overflows.ll
+++ b/test/Transforms/CorrelatedValuePropagation/overflows.ll
@@ -715,3 +715,12 @@
 cleanup2:                                         ; preds = %while.end
   ret void
 }
+
+define { i8, i1 } @signed_mul_constant_folding() {
+; CHECK-LABEL: @signed_mul_constant_folding(
+; CHECK-NEXT:    ret { i8, i1 } { i8 2, i1 false }
+  %mul = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 1, i8 2)
+  ret { i8, i1 } %mul
+}
+
+declare { i8, i1 } @llvm.umul.with.overflow.i8(i8, i8)