[InstSimplify] avoid crashing by trying to rem-by-zero
authorSanjay Patel <spatel@rotateright.com>
Thu, 6 Aug 2020 20:05:04 +0000 (16:05 -0400)
committerSanjay Patel <spatel@rotateright.com>
Thu, 6 Aug 2020 20:06:31 +0000 (16:06 -0400)
Bug was noted in the post-commit comments for:
rGe8760bb9a8a3

llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/icmp-constant.ll

index 6683dd2..65f6666 100644 (file)
@@ -2755,9 +2755,9 @@ static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
   const APInt *MulC;
   if (ICmpInst::isEquality(Pred) &&
       ((match(LHS, m_NUWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
-        C->urem(*MulC) != 0) ||
+        *MulC != 0 && C->urem(*MulC) != 0) ||
        (match(LHS, m_NSWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
-        C->srem(*MulC) != 0)))
+        *MulC != 0 && C->srem(*MulC) != 0)))
     return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
 
   return nullptr;
index e8d2a4b..cb3818c 100644 (file)
@@ -1023,3 +1023,45 @@ define i1 @mul_nsw_srem_cmp_neg_constant_is_0(i8 %x) {
   %r = icmp eq i8 %m, -84
   ret i1 %r
 }
+
+; Don't crash trying to div/rem-by-zero.
+
+define i1 @mul_nsw_by_zero(i8 %x) {
+; CHECK-LABEL: @mul_nsw_by_zero(
+; CHECK-NEXT:  bb1:
+; CHECK-NEXT:    br label [[BB3:%.*]]
+; CHECK:       bb2:
+; CHECK-NEXT:    ret i1 false
+; CHECK:       bb3:
+; CHECK-NEXT:    br label [[BB2:%.*]]
+;
+bb1:
+  br label %bb3
+bb2:
+  %r = icmp eq i8 %m, 45
+  ret i1 %r
+bb3:
+  %m = mul nsw i8 %x, 0
+  br label %bb2
+}
+
+; Don't crash trying to div/rem-by-zero.
+
+define i1 @mul_nuw_by_zero(i8 %x) {
+; CHECK-LABEL: @mul_nuw_by_zero(
+; CHECK-NEXT:  bb1:
+; CHECK-NEXT:    br label [[BB3:%.*]]
+; CHECK:       bb2:
+; CHECK-NEXT:    ret i1 false
+; CHECK:       bb3:
+; CHECK-NEXT:    br label [[BB2:%.*]]
+;
+bb1:
+  br label %bb3
+bb2:
+  %r = icmp eq i8 %m, 45
+  ret i1 %r
+bb3:
+  %m = mul nuw i8 %x, 0
+  br label %bb2
+}