From 250a167c41819aa5cedd290a29e4d3af4a9bafbe Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Thu, 6 Aug 2020 16:05:04 -0400 Subject: [PATCH] [InstSimplify] avoid crashing by trying to rem-by-zero Bug was noted in the post-commit comments for: rGe8760bb9a8a3 --- llvm/lib/Analysis/InstructionSimplify.cpp | 4 +-- llvm/test/Transforms/InstSimplify/icmp-constant.ll | 42 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 6683dd2..65f6666 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 e8d2a4b..cb3818c 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 +} -- 2.7.4