From: Chen Zheng Date: Tue, 9 Apr 2019 16:34:31 +0000 (+0000) Subject: [InstCombine] [InstCombine] Canonicalize (-X s/ Y) to -(X s/ Y). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c92ab7af6f8abd8f9ed129b454c6d282319f2c48;p=llvm [InstCombine] [InstCombine] Canonicalize (-X s/ Y) to -(X s/ Y). Differential Revision: https://reviews.llvm.org/D60395 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358017 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index cb8708446b2..2db6e849b19 100644 --- a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1043,6 +1043,12 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor); return new SExtInst(NarrowOp, Op0->getType()); } + + // -X / Y --> -(X / Y) + Value *Y; + if (match(&I, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y)))) + return BinaryOperator::CreateNSWNeg( + Builder.CreateSDiv(X, Y, I.getName(), I.isExact())); // -X / C --> X / -C (if the negation doesn't overflow). // TODO: This could be enhanced to handle arbitrary vector constants by diff --git a/test/Transforms/InstCombine/sdiv-canonicalize.ll b/test/Transforms/InstCombine/sdiv-canonicalize.ll index 8e1497b900a..39ba5120ed6 100644 --- a/test/Transforms/InstCombine/sdiv-canonicalize.ll +++ b/test/Transforms/InstCombine/sdiv-canonicalize.ll @@ -3,8 +3,8 @@ define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) { ; CHECK-LABEL: @test_sdiv_canonicalize_op0( -; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]] -; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]] +; CHECK-NEXT: [[SDIV1:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]] ; CHECK-NEXT: ret i32 [[SDIV]] ; %neg = sub nsw i32 0, %x @@ -14,8 +14,8 @@ define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) { define i32 @test_sdiv_canonicalize_op0_exact(i32 %x, i32 %y) { ; CHECK-LABEL: @test_sdiv_canonicalize_op0_exact( -; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]] -; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i32 [[NEG]], [[Y:%.*]] +; CHECK-NEXT: [[SDIV1:%.*]] = sdiv exact i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]] ; CHECK-NEXT: ret i32 [[SDIV]] ; %neg = sub nsw i32 0, %x @@ -23,6 +23,7 @@ define i32 @test_sdiv_canonicalize_op0_exact(i32 %x, i32 %y) { ret i32 %sdiv } +; (X/-Y) is not equal to -(X/Y), don't canonicalize. define i32 @test_sdiv_canonicalize_op1(i32 %x, i32 %z) { ; CHECK-LABEL: @test_sdiv_canonicalize_op1( ; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3 @@ -49,8 +50,8 @@ define i32 @test_sdiv_canonicalize_nonsw(i32 %x, i32 %y) { define <2 x i32> @test_sdiv_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @test_sdiv_canonicalize_vec( -; CHECK-NEXT: [[NEG:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X:%.*]] -; CHECK-NEXT: [[SDIV:%.*]] = sdiv <2 x i32> [[NEG]], [[Y:%.*]] +; CHECK-NEXT: [[SDIV1:%.*]] = sdiv <2 x i32> [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[SDIV:%.*]] = sub nsw <2 x i32> zeroinitializer, [[SDIV1]] ; CHECK-NEXT: ret <2 x i32> [[SDIV]] ; %neg = sub nsw <2 x i32> , %x @@ -71,9 +72,8 @@ define i32 @test_sdiv_canonicalize_multiple_uses(i32 %x, i32 %y) { ret i32 %sdiv2 } -; There is combination: (X/-CE) -> -(X/CE) -; There is another combination: -(X/CE) -> (X/-CE) -; Make sure don't combine them endless. +; There is combination: -(X/CE) -> (X/-CE). +; If combines (X/-CE) to -(X/CE), make sure don't combine them endless. @X = global i32 5