Skip to content

Commit

Permalink
[ConstraintElim] fix crash with large constants in mul nsw
Browse files Browse the repository at this point in the history
Another case of #55085.

The added test would trip an assertion due to calling `getSExtValue()` on a value that doesn't fit in int64_t.

Differential Revision: https://reviews.llvm.org/D158810
  • Loading branch information
erikdesjardins committed Aug 25, 2023
1 parent 8d0c3db commit 66ec5df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ static Decomposition decompose(Value *V,
return MergeResults(Op0, Op1, IsSigned);

ConstantInt *CI;
if (match(V, m_NSWMul(m_Value(Op0), m_ConstantInt(CI)))) {
if (match(V, m_NSWMul(m_Value(Op0), m_ConstantInt(CI))) && canUseSExt(CI)) {
auto Result = decompose(Op0, Preconditions, IsSigned, DL);
Result.mul(CI->getSExtValue());
return Result;
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/Transforms/ConstraintElimination/large-constant-ints.ll
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ else:
ret i1 false
}

define i1 @mul_nsw_decomp(i128 %x) {
%val = mul nsw i128 %x, 9223372036854775808
%cmp = icmp sgt i128 %x, %val
br i1 %cmp, label %then, label %else

then:
%cmp2 = icmp sgt i128 %x, 0
ret i1 %cmp2

else:
ret i1 false
}

define i1 @add_nuw_decomp_recursive() {
; CHECK-LABEL: @add_nuw_decomp_recursive(
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i64 -9223372036854775808, 10
Expand Down

0 comments on commit 66ec5df

Please sign in to comment.