1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=aarch64-unknown-linux-gnu < %s | FileCheck %s
4 ;------------------------------------------------------------------------------;
6 ;------------------------------------------------------------------------------;
8 ; This tests the BuildREMEqFold optimization with UREM, i32, odd divisor, SETEQ.
9 ; The corresponding pseudocode is:
10 ; Q <- [N * multInv(5, 2^32)] <=> [N * 0xCCCCCCCD] <=> [N * (-858993459)]
11 ; res <- [Q <= (2^32 - 1) / 5] <=> [Q <= 858993459] <=> [Q < 858993460]
12 define i32 @test_urem_odd(i32 %X) nounwind {
13 ; CHECK-LABEL: test_urem_odd:
15 ; CHECK-NEXT: mov w8, #52429
16 ; CHECK-NEXT: movk w8, #52428, lsl #16
17 ; CHECK-NEXT: mov w9, #13108
18 ; CHECK-NEXT: mul w8, w0, w8
19 ; CHECK-NEXT: movk w9, #13107, lsl #16
20 ; CHECK-NEXT: cmp w8, w9
21 ; CHECK-NEXT: cset w0, lo
23 %urem = urem i32 %X, 5
24 %cmp = icmp eq i32 %urem, 0
25 %ret = zext i1 %cmp to i32
29 define i32 @test_urem_odd_25(i32 %X) nounwind {
30 ; CHECK-LABEL: test_urem_odd_25:
32 ; CHECK-NEXT: mov w8, #23593
33 ; CHECK-NEXT: movk w8, #49807, lsl #16
34 ; CHECK-NEXT: mov w9, #28836
35 ; CHECK-NEXT: mul w8, w0, w8
36 ; CHECK-NEXT: movk w9, #2621, lsl #16
37 ; CHECK-NEXT: cmp w8, w9
38 ; CHECK-NEXT: cset w0, lo
40 %urem = urem i32 %X, 25
41 %cmp = icmp eq i32 %urem, 0
42 %ret = zext i1 %cmp to i32
46 ; This is like test_urem_odd, except the divisor has bit 30 set.
47 define i32 @test_urem_odd_bit30(i32 %X) nounwind {
48 ; CHECK-LABEL: test_urem_odd_bit30:
50 ; CHECK-NEXT: mov w8, #43691
51 ; CHECK-NEXT: movk w8, #27306, lsl #16
52 ; CHECK-NEXT: mul w8, w0, w8
53 ; CHECK-NEXT: cmp w8, #4 // =4
54 ; CHECK-NEXT: cset w0, lo
56 %urem = urem i32 %X, 1073741827
57 %cmp = icmp eq i32 %urem, 0
58 %ret = zext i1 %cmp to i32
62 ; This is like test_urem_odd, except the divisor has bit 31 set.
63 define i32 @test_urem_odd_bit31(i32 %X) nounwind {
64 ; CHECK-LABEL: test_urem_odd_bit31:
66 ; CHECK-NEXT: mov w8, #43691
67 ; CHECK-NEXT: movk w8, #10922, lsl #16
68 ; CHECK-NEXT: mul w8, w0, w8
69 ; CHECK-NEXT: cmp w8, #2 // =2
70 ; CHECK-NEXT: cset w0, lo
72 %urem = urem i32 %X, 2147483651
73 %cmp = icmp eq i32 %urem, 0
74 %ret = zext i1 %cmp to i32
78 ;------------------------------------------------------------------------------;
80 ;------------------------------------------------------------------------------;
82 ; This tests the BuildREMEqFold optimization with UREM, i16, even divisor, SETNE.
83 ; In this case, D <=> 14 <=> 7 * 2^1, so D0 = 7 and K = 1.
84 ; The corresponding pseudocode is:
85 ; Q <- [N * multInv(D0, 2^16)] <=> [N * multInv(7, 2^16)] <=> [N * 28087]
86 ; Q <- [Q >>rot K] <=> [Q >>rot 1]
87 ; res <- ![Q <= (2^16 - 1) / 7] <=> ![Q <= 9362] <=> [Q > 9362]
88 define i16 @test_urem_even(i16 %X) nounwind {
89 ; CHECK-LABEL: test_urem_even:
91 ; CHECK-NEXT: mov w9, #28087
92 ; CHECK-NEXT: and w8, w0, #0xffff
93 ; CHECK-NEXT: movk w9, #46811, lsl #16
94 ; CHECK-NEXT: mul w8, w8, w9
95 ; CHECK-NEXT: mov w9, #9362
96 ; CHECK-NEXT: ror w8, w8, #1
97 ; CHECK-NEXT: movk w9, #4681, lsl #16
98 ; CHECK-NEXT: cmp w8, w9
99 ; CHECK-NEXT: cset w0, hi
101 %urem = urem i16 %X, 14
102 %cmp = icmp ne i16 %urem, 0
103 %ret = zext i1 %cmp to i16
107 define i32 @test_urem_even_100(i32 %X) nounwind {
108 ; CHECK-LABEL: test_urem_even_100:
110 ; CHECK-NEXT: mov w8, #23593
111 ; CHECK-NEXT: movk w8, #49807, lsl #16
112 ; CHECK-NEXT: mul w8, w0, w8
113 ; CHECK-NEXT: mov w9, #23593
114 ; CHECK-NEXT: ror w8, w8, #2
115 ; CHECK-NEXT: movk w9, #655, lsl #16
116 ; CHECK-NEXT: cmp w8, w9
117 ; CHECK-NEXT: cset w0, lo
119 %urem = urem i32 %X, 100
120 %cmp = icmp eq i32 %urem, 0
121 %ret = zext i1 %cmp to i32
125 ; This is like test_urem_even, except the divisor has bit 30 set.
126 define i32 @test_urem_even_bit30(i32 %X) nounwind {
127 ; CHECK-LABEL: test_urem_even_bit30:
129 ; CHECK-NEXT: mov w8, #20165
130 ; CHECK-NEXT: movk w8, #64748, lsl #16
131 ; CHECK-NEXT: mul w8, w0, w8
132 ; CHECK-NEXT: ror w8, w8, #3
133 ; CHECK-NEXT: cmp w8, #4 // =4
134 ; CHECK-NEXT: cset w0, lo
136 %urem = urem i32 %X, 1073741928
137 %cmp = icmp eq i32 %urem, 0
138 %ret = zext i1 %cmp to i32
142 ; This is like test_urem_odd, except the divisor has bit 31 set.
143 define i32 @test_urem_even_bit31(i32 %X) nounwind {
144 ; CHECK-LABEL: test_urem_even_bit31:
146 ; CHECK-NEXT: mov w8, #64251
147 ; CHECK-NEXT: movk w8, #47866, lsl #16
148 ; CHECK-NEXT: mul w8, w0, w8
149 ; CHECK-NEXT: ror w8, w8, #1
150 ; CHECK-NEXT: cmp w8, #2 // =2
151 ; CHECK-NEXT: cset w0, lo
153 %urem = urem i32 %X, 2147483750
154 %cmp = icmp eq i32 %urem, 0
155 %ret = zext i1 %cmp to i32
159 ;------------------------------------------------------------------------------;
161 ;------------------------------------------------------------------------------;
163 ; 'NE' predicate is fine too.
164 define i32 @test_urem_odd_setne(i32 %X) nounwind {
165 ; CHECK-LABEL: test_urem_odd_setne:
167 ; CHECK-NEXT: mov w8, #52429
168 ; CHECK-NEXT: movk w8, #52428, lsl #16
169 ; CHECK-NEXT: mul w8, w0, w8
170 ; CHECK-NEXT: mov w9, #858993459
171 ; CHECK-NEXT: cmp w8, w9
172 ; CHECK-NEXT: cset w0, hi
174 %urem = urem i32 %X, 5
175 %cmp = icmp ne i32 %urem, 0
176 %ret = zext i1 %cmp to i32
180 ;------------------------------------------------------------------------------;
182 ;------------------------------------------------------------------------------;
184 ; The fold is invalid if divisor is 1.
185 define i32 @test_urem_one(i32 %X) nounwind {
186 ; CHECK-LABEL: test_urem_one:
188 ; CHECK-NEXT: mov w0, #1
190 %urem = urem i32 %X, 1
191 %cmp = icmp eq i32 %urem, 0
192 %ret = zext i1 %cmp to i32
196 ; We can lower remainder of division by all-ones much better elsewhere.
197 define i32 @test_urem_allones(i32 %X) nounwind {
198 ; CHECK-LABEL: test_urem_allones:
200 ; CHECK-NEXT: neg w8, w0
201 ; CHECK-NEXT: cmp w8, #2 // =2
202 ; CHECK-NEXT: cset w0, lo
204 %urem = urem i32 %X, 4294967295
205 %cmp = icmp eq i32 %urem, 0
206 %ret = zext i1 %cmp to i32
210 ; We can lower remainder of division by powers of two much better elsewhere.
211 define i32 @test_urem_pow2(i32 %X) nounwind {
212 ; CHECK-LABEL: test_urem_pow2:
214 ; CHECK-NEXT: tst w0, #0xf
215 ; CHECK-NEXT: cset w0, eq
217 %urem = urem i32 %X, 16
218 %cmp = icmp eq i32 %urem, 0
219 %ret = zext i1 %cmp to i32