Skip to content

Commit c8c14d9

Browse files
committed
[InstCombine] Support vectors in SimplifyAddWithRemainder.
SimplifyAddWithRemainder currently also matches for vector types, but tries to create an integer constant, which causes a crash. By using Constant::getIntegerValue() we can support both the scalar and vector cases. The 2 added test cases crash without the fix. Reviewers: spatel, lebedev.ri Reviewed By: spatel, lebedev.ri Differential Revision: https://reviews.llvm.org/D75906
1 parent 714466b commit c8c14d9

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,7 @@ Value *InstCombiner::SimplifyAddWithRemainder(BinaryOperator &I) {
10441044
// Match RemOpV = X / C0
10451045
if (MatchDiv(RemOpV, DivOpV, DivOpC, IsSigned) && X == DivOpV &&
10461046
C0 == DivOpC && !MulWillOverflow(C0, C1, IsSigned)) {
1047-
Value *NewDivisor =
1048-
ConstantInt::get(X->getType()->getContext(), C0 * C1);
1047+
Value *NewDivisor = ConstantInt::get(X->getType(), C0 * C1);
10491048
return IsSigned ? Builder.CreateSRem(X, NewDivisor, "srem")
10501049
: Builder.CreateURem(X, NewDivisor, "urem");
10511050
}

llvm/test/Transforms/InstCombine/add4.ll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ define i64 @match_unsigned(i64 %x) {
1414
ret i64 %t4
1515
}
1616

17+
define <2 x i64> @match_unsigned_vector(<2 x i64> %x) {
18+
; CHECK-LABEL: @match_unsigned_vector(
19+
; CHECK-NEXT: bb:
20+
; CHECK-NEXT: [[UREM:%.*]] = urem <2 x i64> [[X:%.*]], <i64 19136, i64 19136>
21+
; CHECK-NEXT: ret <2 x i64> [[UREM]]
22+
;
23+
bb:
24+
%tmp = urem <2 x i64> %x, <i64 299, i64 299>
25+
%tmp1 = udiv <2 x i64> %x, <i64 299, i64 299>
26+
%tmp2 = urem <2 x i64> %tmp1, <i64 64, i64 64>
27+
%tmp3 = mul <2 x i64> %tmp2, <i64 299, i64 299>
28+
%tmp4 = add <2 x i64> %tmp, %tmp3
29+
ret <2 x i64> %tmp4
30+
}
1731
define i64 @match_andAsRem_lshrAsDiv_shlAsMul(i64 %x) {
1832
; CHECK-LABEL: @match_andAsRem_lshrAsDiv_shlAsMul(
1933
; CHECK-NEXT: [[UREM:%.*]] = urem i64 [[X:%.*]], 576
@@ -44,6 +58,25 @@ define i64 @match_signed(i64 %x) {
4458
ret i64 %t8
4559
}
4660

61+
define <2 x i64> @match_signed_vector(<2 x i64> %x) {
62+
; CHECK-LABEL: @match_signed_vector(
63+
; CHECK-NEXT: bb:
64+
; CHECK-NEXT: [[SREM1:%.*]] = srem <2 x i64> [[X:%.*]], <i64 172224, i64 172224>
65+
; CHECK-NEXT: ret <2 x i64> [[SREM1]]
66+
;
67+
bb:
68+
%tmp = srem <2 x i64> %x, <i64 299, i64 299>
69+
%tmp1 = sdiv <2 x i64> %x, <i64 299, i64 299>
70+
%tmp2 = srem <2 x i64> %tmp1, <i64 64, i64 64>
71+
%tmp3 = sdiv <2 x i64> %x, <i64 19136, i64 19136>
72+
%tmp4 = srem <2 x i64> %tmp3, <i64 9, i64 9>
73+
%tmp5 = mul <2 x i64> %tmp2, <i64 299, i64 299>
74+
%tmp6 = add <2 x i64> %tmp, %tmp5
75+
%tmp7 = mul <2 x i64> %tmp4, <i64 19136, i64 19136>
76+
%tmp8 = add <2 x i64> %tmp6, %tmp7
77+
ret <2 x i64> %tmp8
78+
}
79+
4780
define i64 @not_match_inconsistent_signs(i64 %x) {
4881
; CHECK-LABEL: @not_match_inconsistent_signs(
4982
; CHECK-NEXT: [[T:%.*]] = urem i64 [[X:%.*]], 299

0 commit comments

Comments
 (0)