Skip to content

Commit 5e13ff1

Browse files
author
Chen Zheng
committedApr 10, 2019
[InstCombine] Canonicalize (-X s/ Y) to -(X s/ Y).
Differential Revision: https://reviews.llvm.org/D60395 llvm-svn: 358050
1 parent 0c01607 commit 5e13ff1

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,12 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
10561056
}
10571057
}
10581058

1059+
// -X / Y --> -(X / Y)
1060+
Value *Y;
1061+
if (match(&I, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y))))
1062+
return BinaryOperator::CreateNSWNeg(
1063+
Builder.CreateSDiv(X, Y, I.getName(), I.isExact()));
1064+
10591065
// If the sign bits of both operands are zero (i.e. we can prove they are
10601066
// unsigned inputs), turn this into a udiv.
10611067
APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));

‎llvm/test/Transforms/InstCombine/div.ll

+6-6
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_undef(<2 x i8> %x) {
522522

523523
define <2 x i64> @sdiv_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
524524
; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec(
525-
; CHECK-NEXT: [[NEG:%.*]] = sub nsw <2 x i64> zeroinitializer, [[X:%.*]]
526-
; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i64> [[NEG]], <i64 3, i64 4>
525+
; CHECK-NEXT: [[DIV1:%.*]] = sdiv <2 x i64> [[X:%.*]], <i64 3, i64 4>
526+
; CHECK-NEXT: [[DIV:%.*]] = sub nsw <2 x i64> zeroinitializer, [[DIV1]]
527527
; CHECK-NEXT: ret <2 x i64> [[DIV]]
528528
;
529529
%neg = sub nsw <2 x i64> zeroinitializer, %x
@@ -533,8 +533,8 @@ define <2 x i64> @sdiv_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
533533

534534
define <2 x i64> @sdiv_exact_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
535535
; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec(
536-
; CHECK-NEXT: [[NEG:%.*]] = sub nsw <2 x i64> zeroinitializer, [[X:%.*]]
537-
; CHECK-NEXT: [[DIV:%.*]] = sdiv exact <2 x i64> [[NEG]], <i64 3, i64 4>
536+
; CHECK-NEXT: [[DIV1:%.*]] = sdiv exact <2 x i64> [[X:%.*]], <i64 3, i64 4>
537+
; CHECK-NEXT: [[DIV:%.*]] = sub nsw <2 x i64> zeroinitializer, [[DIV1]]
538538
; CHECK-NEXT: ret <2 x i64> [[DIV]]
539539
;
540540
%neg = sub nsw <2 x i64> zeroinitializer, %x
@@ -546,8 +546,8 @@ define <2 x i64> @sdiv_exact_negated_dividend_constant_divisor_vec(<2 x i64> %x)
546546

547547
define <2 x i8> @sdiv_exact_negated_dividend_constant_divisor_vec_overflow(<2 x i8> %x) {
548548
; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec_overflow(
549-
; CHECK-NEXT: [[NEG:%.*]] = sub nsw <2 x i8> zeroinitializer, [[X:%.*]]
550-
; CHECK-NEXT: [[DIV:%.*]] = sdiv exact <2 x i8> [[NEG]], <i8 -128, i8 42>
549+
; CHECK-NEXT: [[DIV1:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 -128, i8 42>
550+
; CHECK-NEXT: [[DIV:%.*]] = sub nsw <2 x i8> zeroinitializer, [[DIV1]]
551551
; CHECK-NEXT: ret <2 x i8> [[DIV]]
552552
;
553553
%neg = sub nsw <2 x i8> zeroinitializer, %x

‎llvm/test/Transforms/InstCombine/sdiv-canonicalize.ll

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) {
55
; CHECK-LABEL: @test_sdiv_canonicalize_op0(
6-
; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
7-
; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
6+
; CHECK-NEXT: [[SDIV1:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]]
7+
; CHECK-NEXT: [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]]
88
; CHECK-NEXT: ret i32 [[SDIV]]
99
;
1010
%neg = sub nsw i32 0, %x
@@ -14,15 +14,16 @@ define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) {
1414

1515
define i32 @test_sdiv_canonicalize_op0_exact(i32 %x, i32 %y) {
1616
; CHECK-LABEL: @test_sdiv_canonicalize_op0_exact(
17-
; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
18-
; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i32 [[NEG]], [[Y:%.*]]
17+
; CHECK-NEXT: [[SDIV1:%.*]] = sdiv exact i32 [[X:%.*]], [[Y:%.*]]
18+
; CHECK-NEXT: [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]]
1919
; CHECK-NEXT: ret i32 [[SDIV]]
2020
;
2121
%neg = sub nsw i32 0, %x
2222
%sdiv = sdiv exact i32 %neg, %y
2323
ret i32 %sdiv
2424
}
2525

26+
; (X/-Y) is not equal to -(X/Y), don't canonicalize.
2627
define i32 @test_sdiv_canonicalize_op1(i32 %x, i32 %z) {
2728
; CHECK-LABEL: @test_sdiv_canonicalize_op1(
2829
; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3
@@ -49,8 +50,8 @@ define i32 @test_sdiv_canonicalize_nonsw(i32 %x, i32 %y) {
4950

5051
define <2 x i32> @test_sdiv_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
5152
; CHECK-LABEL: @test_sdiv_canonicalize_vec(
52-
; CHECK-NEXT: [[NEG:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X:%.*]]
53-
; CHECK-NEXT: [[SDIV:%.*]] = sdiv <2 x i32> [[NEG]], [[Y:%.*]]
53+
; CHECK-NEXT: [[SDIV1:%.*]] = sdiv <2 x i32> [[X:%.*]], [[Y:%.*]]
54+
; CHECK-NEXT: [[SDIV:%.*]] = sub nsw <2 x i32> zeroinitializer, [[SDIV1]]
5455
; CHECK-NEXT: ret <2 x i32> [[SDIV]]
5556
;
5657
%neg = sub nsw <2 x i32> <i32 0, i32 0>, %x
@@ -71,9 +72,8 @@ define i32 @test_sdiv_canonicalize_multiple_uses(i32 %x, i32 %y) {
7172
ret i32 %sdiv2
7273
}
7374

74-
; There is combination: (X/-CE) -> -(X/CE)
75-
; There is another combination: -(X/CE) -> (X/-CE)
76-
; Make sure don't combine them endless.
75+
; There is combination: -(X/CE) -> (X/-CE).
76+
; If combines (X/-CE) to -(X/CE), make sure don't combine them endless.
7777

7878
@X = global i32 5
7979

0 commit comments

Comments
 (0)
Please sign in to comment.