llvm.org GIT mirror llvm / 4cd899a
[NFC][InstCombine] Tests for 'rem' formation from sub-of-mul-by-'div' (PR42673) https://rise4fun.com/Alive/8Rp https://bugs.llvm.org/show_bug.cgi?id=42673 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366565 91177308-0d34-0410-b5e6-96231b3b80d8 Roman Lebedev a month ago
2 changed file(s) with 234 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt %s -instcombine -S | FileCheck %s
2
3 ; Fold
4 ; x - ((x / y) * y)
5 ; to
6 ; x % y
7
8 ; Also,
9 ; ((x / y) * y)
10 ; can then be simplified to
11 ; x - (x % y)
12
13 declare void @use8(i8)
14 declare void @use2xi8(<2 x i8>)
15
16 define i8 @t0_basic(i8 %x, i8 %y) {
17 ; CHECK-LABEL: @t0_basic(
18 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[X:%.*]], [[Y:%.*]]
19 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
20 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[DIV]], [[Y]]
21 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
22 ; CHECK-NEXT: ret i8 [[REM]]
23 ;
24 %div = sdiv i8 %x, %y
25 call void @use8(i8 %div)
26 %roundXdownToMultipleOfY = mul i8 %div, %y
27 %rem = sub i8 %x, %roundXdownToMultipleOfY
28 ret i8 %rem
29 }
30
31 define <2 x i8> @t1_vector(<2 x i8> %x, <2 x i8> %y) {
32 ; CHECK-LABEL: @t1_vector(
33 ; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], [[Y:%.*]]
34 ; CHECK-NEXT: call void @use2xi8(<2 x i8> [[DIV]])
35 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul <2 x i8> [[DIV]], [[Y]]
36 ; CHECK-NEXT: [[REM:%.*]] = sub <2 x i8> [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
37 ; CHECK-NEXT: ret <2 x i8> [[REM]]
38 ;
39 %div = sdiv <2 x i8> %x, %y
40 call void @use2xi8(<2 x i8> %div)
41 %roundXdownToMultipleOfY = mul <2 x i8> %div, %y
42 %rem = sub <2 x i8> %x, %roundXdownToMultipleOfY
43 ret <2 x i8> %rem
44 }
45
46 ; Extra use
47
48 define i8 @t4_extrause(i8 %x, i8 %y) {
49 ; CHECK-LABEL: @t4_extrause(
50 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[X:%.*]], [[Y:%.*]]
51 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
52 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[DIV]], [[Y]]
53 ; CHECK-NEXT: call void @use8(i8 [[ROUNDXDOWNTOMULTIPLEOFY]])
54 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
55 ; CHECK-NEXT: ret i8 [[REM]]
56 ;
57 %div = sdiv i8 %x, %y
58 call void @use8(i8 %div)
59 %roundXdownToMultipleOfY = mul i8 %div, %y
60 call void @use8(i8 %roundXdownToMultipleOfY)
61 %rem = sub i8 %x, %roundXdownToMultipleOfY
62 ret i8 %rem
63 }
64
65 ; Commutativity
66
67 declare i8 @gen8()
68
69 define i8 @t5_commutative(i8 %x) {
70 ; CHECK-LABEL: @t5_commutative(
71 ; CHECK-NEXT: [[Y:%.*]] = call i8 @gen8()
72 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[X:%.*]], [[Y]]
73 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
74 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[Y]], [[DIV]]
75 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
76 ; CHECK-NEXT: ret i8 [[REM]]
77 ;
78 %y = call i8 @gen8()
79 %div = sdiv i8 %x, %y
80 call void @use8(i8 %div)
81 %roundXdownToMultipleOfY = mul i8 %y, %div ; swapped
82 %rem = sub i8 %x, %roundXdownToMultipleOfY
83 ret i8 %rem
84 }
85
86 ; Negative tests
87
88 define i8 @n6_different_x(i8 %x0, i8 %x1, i8 %y) {
89 ; CHECK-LABEL: @n6_different_x(
90 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[X0:%.*]], [[Y:%.*]]
91 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
92 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[DIV]], [[Y]]
93 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X1:%.*]], [[ROUNDXDOWNTOMULTIPLEOFY]]
94 ; CHECK-NEXT: ret i8 [[REM]]
95 ;
96 %div = sdiv i8 %x0, %y
97 call void @use8(i8 %div)
98 %roundXdownToMultipleOfY = mul i8 %div, %y
99 %rem = sub i8 %x1, %roundXdownToMultipleOfY
100 ret i8 %rem
101 }
102
103 define i8 @n6_different_y(i8 %x, i8 %y0, i8 %y1) {
104 ; CHECK-LABEL: @n6_different_y(
105 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[X:%.*]], [[Y0:%.*]]
106 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
107 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[DIV]], [[Y1:%.*]]
108 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
109 ; CHECK-NEXT: ret i8 [[REM]]
110 ;
111 %div = sdiv i8 %x, %y0
112 call void @use8(i8 %div)
113 %roundXdownToMultipleOfY = mul i8 %div, %y1
114 %rem = sub i8 %x, %roundXdownToMultipleOfY
115 ret i8 %rem
116 }
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt %s -instcombine -S | FileCheck %s
2
3 ; Fold
4 ; x - ((x / y) * y)
5 ; to
6 ; x % y
7
8 ; Also,
9 ; ((x / y) * y)
10 ; can then be simplified to
11 ; x - (x % y)
12
13 declare void @use8(i8)
14 declare void @use2xi8(<2 x i8>)
15
16 define i8 @t0_basic(i8 %x, i8 %y) {
17 ; CHECK-LABEL: @t0_basic(
18 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
19 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
20 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[DIV]], [[Y]]
21 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
22 ; CHECK-NEXT: ret i8 [[REM]]
23 ;
24 %div = udiv i8 %x, %y
25 call void @use8(i8 %div)
26 %roundXdownToMultipleOfY = mul i8 %div, %y
27 %rem = sub i8 %x, %roundXdownToMultipleOfY
28 ret i8 %rem
29 }
30
31 define <2 x i8> @t1_vector(<2 x i8> %x, <2 x i8> %y) {
32 ; CHECK-LABEL: @t1_vector(
33 ; CHECK-NEXT: [[DIV:%.*]] = udiv <2 x i8> [[X:%.*]], [[Y:%.*]]
34 ; CHECK-NEXT: call void @use2xi8(<2 x i8> [[DIV]])
35 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul <2 x i8> [[DIV]], [[Y]]
36 ; CHECK-NEXT: [[REM:%.*]] = sub <2 x i8> [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
37 ; CHECK-NEXT: ret <2 x i8> [[REM]]
38 ;
39 %div = udiv <2 x i8> %x, %y
40 call void @use2xi8(<2 x i8> %div)
41 %roundXdownToMultipleOfY = mul <2 x i8> %div, %y
42 %rem = sub <2 x i8> %x, %roundXdownToMultipleOfY
43 ret <2 x i8> %rem
44 }
45
46 ; Extra use
47
48 define i8 @t4_extrause(i8 %x, i8 %y) {
49 ; CHECK-LABEL: @t4_extrause(
50 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
51 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
52 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[DIV]], [[Y]]
53 ; CHECK-NEXT: call void @use8(i8 [[ROUNDXDOWNTOMULTIPLEOFY]])
54 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
55 ; CHECK-NEXT: ret i8 [[REM]]
56 ;
57 %div = udiv i8 %x, %y
58 call void @use8(i8 %div)
59 %roundXdownToMultipleOfY = mul i8 %div, %y
60 call void @use8(i8 %roundXdownToMultipleOfY)
61 %rem = sub i8 %x, %roundXdownToMultipleOfY
62 ret i8 %rem
63 }
64
65 ; Commutativity
66
67 declare i8 @gen8()
68
69 define i8 @t5_commutative(i8 %x) {
70 ; CHECK-LABEL: @t5_commutative(
71 ; CHECK-NEXT: [[Y:%.*]] = call i8 @gen8()
72 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[X:%.*]], [[Y]]
73 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
74 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[Y]], [[DIV]]
75 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
76 ; CHECK-NEXT: ret i8 [[REM]]
77 ;
78 %y = call i8 @gen8()
79 %div = udiv i8 %x, %y
80 call void @use8(i8 %div)
81 %roundXdownToMultipleOfY = mul i8 %y, %div ; swapped
82 %rem = sub i8 %x, %roundXdownToMultipleOfY
83 ret i8 %rem
84 }
85
86 ; Negative tests
87
88 define i8 @n6_different_x(i8 %x0, i8 %x1, i8 %y) {
89 ; CHECK-LABEL: @n6_different_x(
90 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[X0:%.*]], [[Y:%.*]]
91 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
92 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[DIV]], [[Y]]
93 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X1:%.*]], [[ROUNDXDOWNTOMULTIPLEOFY]]
94 ; CHECK-NEXT: ret i8 [[REM]]
95 ;
96 %div = udiv i8 %x0, %y
97 call void @use8(i8 %div)
98 %roundXdownToMultipleOfY = mul i8 %div, %y
99 %rem = sub i8 %x1, %roundXdownToMultipleOfY
100 ret i8 %rem
101 }
102
103 define i8 @n6_different_y(i8 %x, i8 %y0, i8 %y1) {
104 ; CHECK-LABEL: @n6_different_y(
105 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[X:%.*]], [[Y0:%.*]]
106 ; CHECK-NEXT: call void @use8(i8 [[DIV]])
107 ; CHECK-NEXT: [[ROUNDXDOWNTOMULTIPLEOFY:%.*]] = mul i8 [[DIV]], [[Y1:%.*]]
108 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[X]], [[ROUNDXDOWNTOMULTIPLEOFY]]
109 ; CHECK-NEXT: ret i8 [[REM]]
110 ;
111 %div = udiv i8 %x, %y0
112 call void @use8(i8 %div)
113 %roundXdownToMultipleOfY = mul i8 %div, %y1
114 %rem = sub i8 %x, %roundXdownToMultipleOfY
115 ret i8 %rem
116 }