1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
3 ; RUN: | FileCheck %s -check-prefixes=RV32I
4 ; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
5 ; RUN: | FileCheck %s -check-prefixes=RV64I
7 define i32 @sdiv32_pow2_2(i32 %a) {
8 ; RV32I-LABEL: sdiv32_pow2_2:
9 ; RV32I: # %bb.0: # %entry
10 ; RV32I-NEXT: srli a1, a0, 31
11 ; RV32I-NEXT: add a0, a0, a1
12 ; RV32I-NEXT: srai a0, a0, 1
15 ; RV64I-LABEL: sdiv32_pow2_2:
16 ; RV64I: # %bb.0: # %entry
17 ; RV64I-NEXT: srliw a1, a0, 31
18 ; RV64I-NEXT: add a0, a0, a1
19 ; RV64I-NEXT: sraiw a0, a0, 1
26 define i32 @sdiv32_pow2_negative_2(i32 %a) {
27 ; RV32I-LABEL: sdiv32_pow2_negative_2:
28 ; RV32I: # %bb.0: # %entry
29 ; RV32I-NEXT: srli a1, a0, 31
30 ; RV32I-NEXT: add a0, a0, a1
31 ; RV32I-NEXT: srai a0, a0, 1
32 ; RV32I-NEXT: neg a0, a0
35 ; RV64I-LABEL: sdiv32_pow2_negative_2:
36 ; RV64I: # %bb.0: # %entry
37 ; RV64I-NEXT: srliw a1, a0, 31
38 ; RV64I-NEXT: add a0, a0, a1
39 ; RV64I-NEXT: sraiw a0, a0, 1
40 ; RV64I-NEXT: neg a0, a0
43 %div = sdiv i32 %a, -2
47 define i32 @sdiv32_pow2_2048(i32 %a) {
48 ; RV32I-LABEL: sdiv32_pow2_2048:
49 ; RV32I: # %bb.0: # %entry
50 ; RV32I-NEXT: srai a1, a0, 31
51 ; RV32I-NEXT: srli a1, a1, 21
52 ; RV32I-NEXT: add a0, a0, a1
53 ; RV32I-NEXT: srai a0, a0, 11
56 ; RV64I-LABEL: sdiv32_pow2_2048:
57 ; RV64I: # %bb.0: # %entry
58 ; RV64I-NEXT: sraiw a1, a0, 31
59 ; RV64I-NEXT: srliw a1, a1, 21
60 ; RV64I-NEXT: add a0, a0, a1
61 ; RV64I-NEXT: sraiw a0, a0, 11
64 %div = sdiv i32 %a, 2048
68 define i32 @sdiv32_pow2_negative_2048(i32 %a) {
69 ; RV32I-LABEL: sdiv32_pow2_negative_2048:
70 ; RV32I: # %bb.0: # %entry
71 ; RV32I-NEXT: srai a1, a0, 31
72 ; RV32I-NEXT: srli a1, a1, 21
73 ; RV32I-NEXT: add a0, a0, a1
74 ; RV32I-NEXT: srai a0, a0, 11
75 ; RV32I-NEXT: neg a0, a0
78 ; RV64I-LABEL: sdiv32_pow2_negative_2048:
79 ; RV64I: # %bb.0: # %entry
80 ; RV64I-NEXT: sraiw a1, a0, 31
81 ; RV64I-NEXT: srliw a1, a1, 21
82 ; RV64I-NEXT: add a0, a0, a1
83 ; RV64I-NEXT: sraiw a0, a0, 11
84 ; RV64I-NEXT: neg a0, a0
87 %div = sdiv i32 %a, -2048
91 define i32 @sdiv32_pow2_4096(i32 %a) {
92 ; RV32I-LABEL: sdiv32_pow2_4096:
93 ; RV32I: # %bb.0: # %entry
94 ; RV32I-NEXT: srai a1, a0, 31
95 ; RV32I-NEXT: srli a1, a1, 20
96 ; RV32I-NEXT: add a0, a0, a1
97 ; RV32I-NEXT: srai a0, a0, 12
100 ; RV64I-LABEL: sdiv32_pow2_4096:
101 ; RV64I: # %bb.0: # %entry
102 ; RV64I-NEXT: sraiw a1, a0, 31
103 ; RV64I-NEXT: srliw a1, a1, 20
104 ; RV64I-NEXT: add a0, a0, a1
105 ; RV64I-NEXT: sraiw a0, a0, 12
108 %div = sdiv i32 %a, 4096
112 define i32 @sdiv32_pow2_negative_4096(i32 %a) {
113 ; RV32I-LABEL: sdiv32_pow2_negative_4096:
114 ; RV32I: # %bb.0: # %entry
115 ; RV32I-NEXT: srai a1, a0, 31
116 ; RV32I-NEXT: srli a1, a1, 20
117 ; RV32I-NEXT: add a0, a0, a1
118 ; RV32I-NEXT: srai a0, a0, 12
119 ; RV32I-NEXT: neg a0, a0
122 ; RV64I-LABEL: sdiv32_pow2_negative_4096:
123 ; RV64I: # %bb.0: # %entry
124 ; RV64I-NEXT: sraiw a1, a0, 31
125 ; RV64I-NEXT: srliw a1, a1, 20
126 ; RV64I-NEXT: add a0, a0, a1
127 ; RV64I-NEXT: sraiw a0, a0, 12
128 ; RV64I-NEXT: neg a0, a0
131 %div = sdiv i32 %a, -4096
135 define i32 @sdiv32_pow2_65536(i32 %a) {
136 ; RV32I-LABEL: sdiv32_pow2_65536:
137 ; RV32I: # %bb.0: # %entry
138 ; RV32I-NEXT: srai a1, a0, 31
139 ; RV32I-NEXT: srli a1, a1, 16
140 ; RV32I-NEXT: add a0, a0, a1
141 ; RV32I-NEXT: srai a0, a0, 16
144 ; RV64I-LABEL: sdiv32_pow2_65536:
145 ; RV64I: # %bb.0: # %entry
146 ; RV64I-NEXT: sraiw a1, a0, 31
147 ; RV64I-NEXT: srliw a1, a1, 16
148 ; RV64I-NEXT: add a0, a0, a1
149 ; RV64I-NEXT: sraiw a0, a0, 16
152 %div = sdiv i32 %a, 65536
156 define i32 @sdiv32_pow2_negative_65536(i32 %a) {
157 ; RV32I-LABEL: sdiv32_pow2_negative_65536:
158 ; RV32I: # %bb.0: # %entry
159 ; RV32I-NEXT: srai a1, a0, 31
160 ; RV32I-NEXT: srli a1, a1, 16
161 ; RV32I-NEXT: add a0, a0, a1
162 ; RV32I-NEXT: srai a0, a0, 16
163 ; RV32I-NEXT: neg a0, a0
166 ; RV64I-LABEL: sdiv32_pow2_negative_65536:
167 ; RV64I: # %bb.0: # %entry
168 ; RV64I-NEXT: sraiw a1, a0, 31
169 ; RV64I-NEXT: srliw a1, a1, 16
170 ; RV64I-NEXT: add a0, a0, a1
171 ; RV64I-NEXT: sraiw a0, a0, 16
172 ; RV64I-NEXT: neg a0, a0
175 %div = sdiv i32 %a, -65536
179 define i64 @sdiv64_pow2_2(i64 %a) {
180 ; RV32I-LABEL: sdiv64_pow2_2:
181 ; RV32I: # %bb.0: # %entry
182 ; RV32I-NEXT: srli a2, a1, 31
183 ; RV32I-NEXT: add a2, a0, a2
184 ; RV32I-NEXT: srli a3, a2, 1
185 ; RV32I-NEXT: sltu a0, a2, a0
186 ; RV32I-NEXT: add a1, a1, a0
187 ; RV32I-NEXT: slli a0, a1, 31
188 ; RV32I-NEXT: or a0, a3, a0
189 ; RV32I-NEXT: srai a1, a1, 1
192 ; RV64I-LABEL: sdiv64_pow2_2:
193 ; RV64I: # %bb.0: # %entry
194 ; RV64I-NEXT: srli a1, a0, 63
195 ; RV64I-NEXT: add a0, a0, a1
196 ; RV64I-NEXT: srai a0, a0, 1
199 %div = sdiv i64 %a, 2
203 define i64 @sdiv64_pow2_negative_2(i64 %a) {
204 ; RV32I-LABEL: sdiv64_pow2_negative_2:
205 ; RV32I: # %bb.0: # %entry
206 ; RV32I-NEXT: srli a2, a1, 31
207 ; RV32I-NEXT: add a2, a0, a2
208 ; RV32I-NEXT: srli a3, a2, 1
209 ; RV32I-NEXT: sltu a0, a2, a0
210 ; RV32I-NEXT: add a1, a1, a0
211 ; RV32I-NEXT: slli a0, a1, 31
212 ; RV32I-NEXT: or a3, a3, a0
213 ; RV32I-NEXT: neg a0, a3
214 ; RV32I-NEXT: snez a2, a3
215 ; RV32I-NEXT: srai a1, a1, 1
216 ; RV32I-NEXT: neg a1, a1
217 ; RV32I-NEXT: sub a1, a1, a2
220 ; RV64I-LABEL: sdiv64_pow2_negative_2:
221 ; RV64I: # %bb.0: # %entry
222 ; RV64I-NEXT: srli a1, a0, 63
223 ; RV64I-NEXT: add a0, a0, a1
224 ; RV64I-NEXT: srai a0, a0, 1
225 ; RV64I-NEXT: neg a0, a0
228 %div = sdiv i64 %a, -2
232 define i64 @sdiv64_pow2_2048(i64 %a) {
233 ; RV32I-LABEL: sdiv64_pow2_2048:
234 ; RV32I: # %bb.0: # %entry
235 ; RV32I-NEXT: srai a2, a1, 31
236 ; RV32I-NEXT: srli a2, a2, 21
237 ; RV32I-NEXT: add a2, a0, a2
238 ; RV32I-NEXT: srli a3, a2, 11
239 ; RV32I-NEXT: sltu a0, a2, a0
240 ; RV32I-NEXT: add a1, a1, a0
241 ; RV32I-NEXT: slli a0, a1, 21
242 ; RV32I-NEXT: or a0, a3, a0
243 ; RV32I-NEXT: srai a1, a1, 11
246 ; RV64I-LABEL: sdiv64_pow2_2048:
247 ; RV64I: # %bb.0: # %entry
248 ; RV64I-NEXT: srai a1, a0, 63
249 ; RV64I-NEXT: srli a1, a1, 53
250 ; RV64I-NEXT: add a0, a0, a1
251 ; RV64I-NEXT: srai a0, a0, 11
254 %div = sdiv i64 %a, 2048
258 define i64 @sdiv64_pow2_negative_2048(i64 %a) {
259 ; RV32I-LABEL: sdiv64_pow2_negative_2048:
260 ; RV32I: # %bb.0: # %entry
261 ; RV32I-NEXT: srai a2, a1, 31
262 ; RV32I-NEXT: srli a2, a2, 21
263 ; RV32I-NEXT: add a2, a0, a2
264 ; RV32I-NEXT: srli a3, a2, 11
265 ; RV32I-NEXT: sltu a0, a2, a0
266 ; RV32I-NEXT: add a1, a1, a0
267 ; RV32I-NEXT: slli a0, a1, 21
268 ; RV32I-NEXT: or a3, a3, a0
269 ; RV32I-NEXT: neg a0, a3
270 ; RV32I-NEXT: snez a2, a3
271 ; RV32I-NEXT: srai a1, a1, 11
272 ; RV32I-NEXT: neg a1, a1
273 ; RV32I-NEXT: sub a1, a1, a2
276 ; RV64I-LABEL: sdiv64_pow2_negative_2048:
277 ; RV64I: # %bb.0: # %entry
278 ; RV64I-NEXT: srai a1, a0, 63
279 ; RV64I-NEXT: srli a1, a1, 53
280 ; RV64I-NEXT: add a0, a0, a1
281 ; RV64I-NEXT: srai a0, a0, 11
282 ; RV64I-NEXT: neg a0, a0
285 %div = sdiv i64 %a, -2048
289 define i64 @sdiv64_pow2_4096(i64 %a) {
290 ; RV32I-LABEL: sdiv64_pow2_4096:
291 ; RV32I: # %bb.0: # %entry
292 ; RV32I-NEXT: srai a2, a1, 31
293 ; RV32I-NEXT: srli a2, a2, 20
294 ; RV32I-NEXT: add a2, a0, a2
295 ; RV32I-NEXT: srli a3, a2, 12
296 ; RV32I-NEXT: sltu a0, a2, a0
297 ; RV32I-NEXT: add a1, a1, a0
298 ; RV32I-NEXT: slli a0, a1, 20
299 ; RV32I-NEXT: or a0, a3, a0
300 ; RV32I-NEXT: srai a1, a1, 12
303 ; RV64I-LABEL: sdiv64_pow2_4096:
304 ; RV64I: # %bb.0: # %entry
305 ; RV64I-NEXT: srai a1, a0, 63
306 ; RV64I-NEXT: srli a1, a1, 52
307 ; RV64I-NEXT: add a0, a0, a1
308 ; RV64I-NEXT: srai a0, a0, 12
311 %div = sdiv i64 %a, 4096
315 define i64 @sdiv64_pow2_negative_4096(i64 %a) {
316 ; RV32I-LABEL: sdiv64_pow2_negative_4096:
317 ; RV32I: # %bb.0: # %entry
318 ; RV32I-NEXT: srai a2, a1, 31
319 ; RV32I-NEXT: srli a2, a2, 20
320 ; RV32I-NEXT: add a2, a0, a2
321 ; RV32I-NEXT: srli a3, a2, 12
322 ; RV32I-NEXT: sltu a0, a2, a0
323 ; RV32I-NEXT: add a1, a1, a0
324 ; RV32I-NEXT: slli a0, a1, 20
325 ; RV32I-NEXT: or a3, a3, a0
326 ; RV32I-NEXT: neg a0, a3
327 ; RV32I-NEXT: snez a2, a3
328 ; RV32I-NEXT: srai a1, a1, 12
329 ; RV32I-NEXT: neg a1, a1
330 ; RV32I-NEXT: sub a1, a1, a2
333 ; RV64I-LABEL: sdiv64_pow2_negative_4096:
334 ; RV64I: # %bb.0: # %entry
335 ; RV64I-NEXT: srai a1, a0, 63
336 ; RV64I-NEXT: srli a1, a1, 52
337 ; RV64I-NEXT: add a0, a0, a1
338 ; RV64I-NEXT: srai a0, a0, 12
339 ; RV64I-NEXT: neg a0, a0
342 %div = sdiv i64 %a, -4096
346 define i64 @sdiv64_pow2_65536(i64 %a) {
347 ; RV32I-LABEL: sdiv64_pow2_65536:
348 ; RV32I: # %bb.0: # %entry
349 ; RV32I-NEXT: srai a2, a1, 31
350 ; RV32I-NEXT: srli a2, a2, 16
351 ; RV32I-NEXT: add a2, a0, a2
352 ; RV32I-NEXT: srli a3, a2, 16
353 ; RV32I-NEXT: sltu a0, a2, a0
354 ; RV32I-NEXT: add a1, a1, a0
355 ; RV32I-NEXT: slli a0, a1, 16
356 ; RV32I-NEXT: or a0, a3, a0
357 ; RV32I-NEXT: srai a1, a1, 16
360 ; RV64I-LABEL: sdiv64_pow2_65536:
361 ; RV64I: # %bb.0: # %entry
362 ; RV64I-NEXT: srai a1, a0, 63
363 ; RV64I-NEXT: srli a1, a1, 48
364 ; RV64I-NEXT: add a0, a0, a1
365 ; RV64I-NEXT: srai a0, a0, 16
368 %div = sdiv i64 %a, 65536
372 define i64 @sdiv64_pow2_negative_65536(i64 %a) {
373 ; RV32I-LABEL: sdiv64_pow2_negative_65536:
374 ; RV32I: # %bb.0: # %entry
375 ; RV32I-NEXT: srai a2, a1, 31
376 ; RV32I-NEXT: srli a2, a2, 16
377 ; RV32I-NEXT: add a2, a0, a2
378 ; RV32I-NEXT: srli a3, a2, 16
379 ; RV32I-NEXT: sltu a0, a2, a0
380 ; RV32I-NEXT: add a1, a1, a0
381 ; RV32I-NEXT: slli a0, a1, 16
382 ; RV32I-NEXT: or a3, a3, a0
383 ; RV32I-NEXT: neg a0, a3
384 ; RV32I-NEXT: snez a2, a3
385 ; RV32I-NEXT: srai a1, a1, 16
386 ; RV32I-NEXT: neg a1, a1
387 ; RV32I-NEXT: sub a1, a1, a2
390 ; RV64I-LABEL: sdiv64_pow2_negative_65536:
391 ; RV64I: # %bb.0: # %entry
392 ; RV64I-NEXT: srai a1, a0, 63
393 ; RV64I-NEXT: srli a1, a1, 48
394 ; RV64I-NEXT: add a0, a0, a1
395 ; RV64I-NEXT: srai a0, a0, 16
396 ; RV64I-NEXT: neg a0, a0
399 %div = sdiv i64 %a, -65536
403 define i64 @sdiv64_pow2_8589934592(i64 %a) {
404 ; RV32I-LABEL: sdiv64_pow2_8589934592:
405 ; RV32I: # %bb.0: # %entry
406 ; RV32I-NEXT: srli a2, a1, 31
407 ; RV32I-NEXT: add a2, a1, a2
408 ; RV32I-NEXT: srai a1, a1, 31
409 ; RV32I-NEXT: add a1, a0, a1
410 ; RV32I-NEXT: sltu a0, a1, a0
411 ; RV32I-NEXT: add a1, a2, a0
412 ; RV32I-NEXT: srai a0, a1, 1
413 ; RV32I-NEXT: srai a1, a1, 31
416 ; RV64I-LABEL: sdiv64_pow2_8589934592:
417 ; RV64I: # %bb.0: # %entry
418 ; RV64I-NEXT: srai a1, a0, 63
419 ; RV64I-NEXT: srli a1, a1, 31
420 ; RV64I-NEXT: add a0, a0, a1
421 ; RV64I-NEXT: srai a0, a0, 33
424 %div = sdiv i64 %a, 8589934592 ; 2^33
428 define i64 @sdiv64_pow2_negative_8589934592(i64 %a) {
429 ; RV32I-LABEL: sdiv64_pow2_negative_8589934592:
430 ; RV32I: # %bb.0: # %entry
431 ; RV32I-NEXT: srli a2, a1, 31
432 ; RV32I-NEXT: add a2, a1, a2
433 ; RV32I-NEXT: srai a1, a1, 31
434 ; RV32I-NEXT: add a1, a0, a1
435 ; RV32I-NEXT: sltu a0, a1, a0
436 ; RV32I-NEXT: add a0, a2, a0
437 ; RV32I-NEXT: srai a1, a0, 31
438 ; RV32I-NEXT: srai a0, a0, 1
439 ; RV32I-NEXT: snez a2, a0
440 ; RV32I-NEXT: neg a1, a1
441 ; RV32I-NEXT: sub a1, a1, a2
442 ; RV32I-NEXT: neg a0, a0
445 ; RV64I-LABEL: sdiv64_pow2_negative_8589934592:
446 ; RV64I: # %bb.0: # %entry
447 ; RV64I-NEXT: srai a1, a0, 63
448 ; RV64I-NEXT: srli a1, a1, 31
449 ; RV64I-NEXT: add a0, a0, a1
450 ; RV64I-NEXT: srai a0, a0, 33
451 ; RV64I-NEXT: neg a0, a0
454 %div = sdiv i64 %a, -8589934592 ; -2^33