From c8c14d979abc417d24081cc96c46a2844ce192c2 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Tue, 10 Mar 2020 14:22:19 +0000 Subject: [PATCH] [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 --- .../Transforms/InstCombine/InstCombineAddSub.cpp | 3 +- llvm/test/Transforms/InstCombine/add4.ll | 33 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp index a781251..cf2cac2 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1044,8 +1044,7 @@ Value *InstCombiner::SimplifyAddWithRemainder(BinaryOperator &I) { // Match RemOpV = X / C0 if (MatchDiv(RemOpV, DivOpV, DivOpC, IsSigned) && X == DivOpV && C0 == DivOpC && !MulWillOverflow(C0, C1, IsSigned)) { - Value *NewDivisor = - ConstantInt::get(X->getType()->getContext(), C0 * C1); + Value *NewDivisor = ConstantInt::get(X->getType(), C0 * C1); return IsSigned ? Builder.CreateSRem(X, NewDivisor, "srem") : Builder.CreateURem(X, NewDivisor, "urem"); } diff --git a/llvm/test/Transforms/InstCombine/add4.ll b/llvm/test/Transforms/InstCombine/add4.ll index a73050f..41eaf12 100644 --- a/llvm/test/Transforms/InstCombine/add4.ll +++ b/llvm/test/Transforms/InstCombine/add4.ll @@ -14,6 +14,20 @@ define i64 @match_unsigned(i64 %x) { ret i64 %t4 } +define <2 x i64> @match_unsigned_vector(<2 x i64> %x) { +; CHECK-LABEL: @match_unsigned_vector( +; CHECK-NEXT: bb: +; CHECK-NEXT: [[UREM:%.*]] = urem <2 x i64> [[X:%.*]], +; CHECK-NEXT: ret <2 x i64> [[UREM]] +; +bb: + %tmp = urem <2 x i64> %x, + %tmp1 = udiv <2 x i64> %x, + %tmp2 = urem <2 x i64> %tmp1, + %tmp3 = mul <2 x i64> %tmp2, + %tmp4 = add <2 x i64> %tmp, %tmp3 + ret <2 x i64> %tmp4 +} define i64 @match_andAsRem_lshrAsDiv_shlAsMul(i64 %x) { ; CHECK-LABEL: @match_andAsRem_lshrAsDiv_shlAsMul( ; CHECK-NEXT: [[UREM:%.*]] = urem i64 [[X:%.*]], 576 @@ -44,6 +58,25 @@ define i64 @match_signed(i64 %x) { ret i64 %t8 } +define <2 x i64> @match_signed_vector(<2 x i64> %x) { +; CHECK-LABEL: @match_signed_vector( +; CHECK-NEXT: bb: +; CHECK-NEXT: [[SREM1:%.*]] = srem <2 x i64> [[X:%.*]], +; CHECK-NEXT: ret <2 x i64> [[SREM1]] +; +bb: + %tmp = srem <2 x i64> %x, + %tmp1 = sdiv <2 x i64> %x, + %tmp2 = srem <2 x i64> %tmp1, + %tmp3 = sdiv <2 x i64> %x, + %tmp4 = srem <2 x i64> %tmp3, + %tmp5 = mul <2 x i64> %tmp2, + %tmp6 = add <2 x i64> %tmp, %tmp5 + %tmp7 = mul <2 x i64> %tmp4, + %tmp8 = add <2 x i64> %tmp6, %tmp7 + ret <2 x i64> %tmp8 +} + define i64 @not_match_inconsistent_signs(i64 %x) { ; CHECK-LABEL: @not_match_inconsistent_signs( ; CHECK-NEXT: [[T:%.*]] = urem i64 [[X:%.*]], 299 -- 2.7.4