[llvm] 250a167 - [InstSimplify] avoid crashing by trying to rem-by-zero

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 6 13:06:39 PDT 2020


Author: Sanjay Patel
Date: 2020-08-06T16:06:31-04:00
New Revision: 250a167c41819aa5cedd290a29e4d3af4a9bafbe

URL: https://github.com/llvm/llvm-project/commit/250a167c41819aa5cedd290a29e4d3af4a9bafbe
DIFF: https://github.com/llvm/llvm-project/commit/250a167c41819aa5cedd290a29e4d3af4a9bafbe.diff

LOG: [InstSimplify] avoid crashing by trying to rem-by-zero

Bug was noted in the post-commit comments for:
rGe8760bb9a8a3

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 6683dd21f6f8..65f6666ca0e3 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -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;

diff  --git a/llvm/test/Transforms/InstSimplify/icmp-constant.ll b/llvm/test/Transforms/InstSimplify/icmp-constant.ll
index e8d2a4b483ef..cb3818c16df8 100644
--- a/llvm/test/Transforms/InstSimplify/icmp-constant.ll
+++ b/llvm/test/Transforms/InstSimplify/icmp-constant.ll
@@ -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
+}


        


More information about the llvm-commits mailing list