Skip to content

Commit

Permalink
[InstSimplify] avoid crashing by trying to rem-by-zero
Browse files Browse the repository at this point in the history
Bug was noted in the post-commit comments for:
rGe8760bb9a8a3
  • Loading branch information
rotateright committed Aug 6, 2020
1 parent ba37b14 commit 250a167
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions llvm/test/Transforms/InstSimplify/icmp-constant.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 250a167

Please sign in to comment.