1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv64 -global-isel -verify-machineinstrs < %s \
3 ; RUN: | FileCheck %s -check-prefixes=CHECK,RV64I
4 ; RUN: llc -mtriple=riscv64 -global-isel -mattr=+zbb -verify-machineinstrs < %s \
5 ; RUN: | FileCheck %s -check-prefixes=CHECK,RV64ZBB-ZBKB,RV64ZBB
6 ; RUN: llc -mtriple=riscv64 -global-isel -mattr=+zbkb -verify-machineinstrs < %s \
7 ; RUN: | FileCheck %s -check-prefixes=CHECK,RV64ZBB-ZBKB,RV64ZBKB
9 define signext i32 @andn_i32(i32 signext %a, i32 signext %b) nounwind {
10 ; RV64I-LABEL: andn_i32:
12 ; RV64I-NEXT: not a1, a1
13 ; RV64I-NEXT: and a0, a1, a0
16 ; RV64ZBB-ZBKB-LABEL: andn_i32:
17 ; RV64ZBB-ZBKB: # %bb.0:
18 ; RV64ZBB-ZBKB-NEXT: andn a0, a0, a1
19 ; RV64ZBB-ZBKB-NEXT: ret
21 %and = and i32 %neg, %a
25 define i64 @andn_i64(i64 %a, i64 %b) nounwind {
26 ; RV64I-LABEL: andn_i64:
28 ; RV64I-NEXT: not a1, a1
29 ; RV64I-NEXT: and a0, a1, a0
32 ; RV64ZBB-ZBKB-LABEL: andn_i64:
33 ; RV64ZBB-ZBKB: # %bb.0:
34 ; RV64ZBB-ZBKB-NEXT: andn a0, a0, a1
35 ; RV64ZBB-ZBKB-NEXT: ret
37 %and = and i64 %neg, %a
41 define signext i32 @orn_i32(i32 signext %a, i32 signext %b) nounwind {
42 ; RV64I-LABEL: orn_i32:
44 ; RV64I-NEXT: not a1, a1
45 ; RV64I-NEXT: or a0, a1, a0
48 ; RV64ZBB-ZBKB-LABEL: orn_i32:
49 ; RV64ZBB-ZBKB: # %bb.0:
50 ; RV64ZBB-ZBKB-NEXT: orn a0, a0, a1
51 ; RV64ZBB-ZBKB-NEXT: ret
57 define i64 @orn_i64(i64 %a, i64 %b) nounwind {
58 ; RV64I-LABEL: orn_i64:
60 ; RV64I-NEXT: not a1, a1
61 ; RV64I-NEXT: or a0, a1, a0
64 ; RV64ZBB-ZBKB-LABEL: orn_i64:
65 ; RV64ZBB-ZBKB: # %bb.0:
66 ; RV64ZBB-ZBKB-NEXT: orn a0, a0, a1
67 ; RV64ZBB-ZBKB-NEXT: ret
73 define signext i32 @xnor_i32(i32 signext %a, i32 signext %b) nounwind {
74 ; RV64I-LABEL: xnor_i32:
76 ; RV64I-NEXT: not a0, a0
77 ; RV64I-NEXT: xor a0, a0, a1
80 ; RV64ZBB-ZBKB-LABEL: xnor_i32:
81 ; RV64ZBB-ZBKB: # %bb.0:
82 ; RV64ZBB-ZBKB-NEXT: xnor a0, a0, a1
83 ; RV64ZBB-ZBKB-NEXT: ret
85 %xor = xor i32 %neg, %b
89 define i64 @xnor_i64(i64 %a, i64 %b) nounwind {
90 ; RV64I-LABEL: xnor_i64:
92 ; RV64I-NEXT: not a0, a0
93 ; RV64I-NEXT: xor a0, a0, a1
96 ; RV64ZBB-ZBKB-LABEL: xnor_i64:
97 ; RV64ZBB-ZBKB: # %bb.0:
98 ; RV64ZBB-ZBKB-NEXT: xnor a0, a0, a1
99 ; RV64ZBB-ZBKB-NEXT: ret
100 %neg = xor i64 %a, -1
101 %xor = xor i64 %neg, %b
105 declare i32 @llvm.fshl.i32(i32, i32, i32)
107 define signext i32 @rol_i32(i32 signext %a, i32 signext %b) nounwind {
108 ; RV64I-LABEL: rol_i32:
110 ; RV64I-NEXT: neg a2, a1
111 ; RV64I-NEXT: andi a1, a1, 31
112 ; RV64I-NEXT: sllw a1, a0, a1
113 ; RV64I-NEXT: andi a2, a2, 31
114 ; RV64I-NEXT: srlw a0, a0, a2
115 ; RV64I-NEXT: or a0, a1, a0
118 ; RV64ZBB-ZBKB-LABEL: rol_i32:
119 ; RV64ZBB-ZBKB: # %bb.0:
120 ; RV64ZBB-ZBKB-NEXT: rolw a0, a0, a1
121 ; RV64ZBB-ZBKB-NEXT: ret
122 %1 = tail call i32 @llvm.fshl.i32(i32 %a, i32 %a, i32 %b)
126 ; Similar to rol_i32, but doesn't sign extend the result.
127 define void @rol_i32_nosext(i32 signext %a, i32 signext %b, ptr %x) nounwind {
128 ; RV64I-LABEL: rol_i32_nosext:
130 ; RV64I-NEXT: neg a3, a1
131 ; RV64I-NEXT: andi a1, a1, 31
132 ; RV64I-NEXT: sllw a1, a0, a1
133 ; RV64I-NEXT: andi a3, a3, 31
134 ; RV64I-NEXT: srlw a0, a0, a3
135 ; RV64I-NEXT: or a0, a1, a0
136 ; RV64I-NEXT: sw a0, 0(a2)
139 ; RV64ZBB-ZBKB-LABEL: rol_i32_nosext:
140 ; RV64ZBB-ZBKB: # %bb.0:
141 ; RV64ZBB-ZBKB-NEXT: rolw a0, a0, a1
142 ; RV64ZBB-ZBKB-NEXT: sw a0, 0(a2)
143 ; RV64ZBB-ZBKB-NEXT: ret
144 %1 = tail call i32 @llvm.fshl.i32(i32 %a, i32 %a, i32 %b)
149 define signext i32 @rol_i32_neg_constant_rhs(i32 signext %a) nounwind {
150 ; RV64I-LABEL: rol_i32_neg_constant_rhs:
152 ; RV64I-NEXT: li a1, -2
153 ; RV64I-NEXT: neg a2, a0
154 ; RV64I-NEXT: andi a0, a0, 31
155 ; RV64I-NEXT: sllw a0, a1, a0
156 ; RV64I-NEXT: andi a2, a2, 31
157 ; RV64I-NEXT: srlw a1, a1, a2
158 ; RV64I-NEXT: or a0, a0, a1
161 ; RV64ZBB-ZBKB-LABEL: rol_i32_neg_constant_rhs:
162 ; RV64ZBB-ZBKB: # %bb.0:
163 ; RV64ZBB-ZBKB-NEXT: li a1, -2
164 ; RV64ZBB-ZBKB-NEXT: rolw a0, a1, a0
165 ; RV64ZBB-ZBKB-NEXT: ret
166 %1 = tail call i32 @llvm.fshl.i32(i32 -2, i32 -2, i32 %a)
170 declare i64 @llvm.fshl.i64(i64, i64, i64)
172 define i64 @rol_i64(i64 %a, i64 %b) nounwind {
173 ; RV64I-LABEL: rol_i64:
175 ; RV64I-NEXT: neg a2, a1
176 ; RV64I-NEXT: sll a1, a0, a1
177 ; RV64I-NEXT: srl a0, a0, a2
178 ; RV64I-NEXT: or a0, a1, a0
181 ; RV64ZBB-ZBKB-LABEL: rol_i64:
182 ; RV64ZBB-ZBKB: # %bb.0:
183 ; RV64ZBB-ZBKB-NEXT: rol a0, a0, a1
184 ; RV64ZBB-ZBKB-NEXT: ret
185 %or = tail call i64 @llvm.fshl.i64(i64 %a, i64 %a, i64 %b)
189 declare i32 @llvm.fshr.i32(i32, i32, i32)
191 define signext i32 @ror_i32(i32 signext %a, i32 signext %b) nounwind {
192 ; RV64I-LABEL: ror_i32:
194 ; RV64I-NEXT: neg a2, a1
195 ; RV64I-NEXT: andi a1, a1, 31
196 ; RV64I-NEXT: srlw a1, a0, a1
197 ; RV64I-NEXT: andi a2, a2, 31
198 ; RV64I-NEXT: sllw a0, a0, a2
199 ; RV64I-NEXT: or a0, a1, a0
202 ; RV64ZBB-ZBKB-LABEL: ror_i32:
203 ; RV64ZBB-ZBKB: # %bb.0:
204 ; RV64ZBB-ZBKB-NEXT: rorw a0, a0, a1
205 ; RV64ZBB-ZBKB-NEXT: ret
206 %1 = tail call i32 @llvm.fshr.i32(i32 %a, i32 %a, i32 %b)
210 ; Similar to ror_i32, but doesn't sign extend the result.
211 define void @ror_i32_nosext(i32 signext %a, i32 signext %b, ptr %x) nounwind {
212 ; RV64I-LABEL: ror_i32_nosext:
214 ; RV64I-NEXT: neg a3, a1
215 ; RV64I-NEXT: andi a1, a1, 31
216 ; RV64I-NEXT: srlw a1, a0, a1
217 ; RV64I-NEXT: andi a3, a3, 31
218 ; RV64I-NEXT: sllw a0, a0, a3
219 ; RV64I-NEXT: or a0, a1, a0
220 ; RV64I-NEXT: sw a0, 0(a2)
223 ; RV64ZBB-ZBKB-LABEL: ror_i32_nosext:
224 ; RV64ZBB-ZBKB: # %bb.0:
225 ; RV64ZBB-ZBKB-NEXT: rorw a0, a0, a1
226 ; RV64ZBB-ZBKB-NEXT: sw a0, 0(a2)
227 ; RV64ZBB-ZBKB-NEXT: ret
228 %1 = tail call i32 @llvm.fshr.i32(i32 %a, i32 %a, i32 %b)
233 define signext i32 @ror_i32_neg_constant_rhs(i32 signext %a) nounwind {
234 ; RV64I-LABEL: ror_i32_neg_constant_rhs:
236 ; RV64I-NEXT: li a1, -2
237 ; RV64I-NEXT: neg a2, a0
238 ; RV64I-NEXT: andi a0, a0, 31
239 ; RV64I-NEXT: srlw a0, a1, a0
240 ; RV64I-NEXT: andi a2, a2, 31
241 ; RV64I-NEXT: sllw a1, a1, a2
242 ; RV64I-NEXT: or a0, a0, a1
245 ; RV64ZBB-ZBKB-LABEL: ror_i32_neg_constant_rhs:
246 ; RV64ZBB-ZBKB: # %bb.0:
247 ; RV64ZBB-ZBKB-NEXT: li a1, -2
248 ; RV64ZBB-ZBKB-NEXT: rorw a0, a1, a0
249 ; RV64ZBB-ZBKB-NEXT: ret
250 %1 = tail call i32 @llvm.fshr.i32(i32 -2, i32 -2, i32 %a)
254 declare i64 @llvm.fshr.i64(i64, i64, i64)
256 define i64 @ror_i64(i64 %a, i64 %b) nounwind {
257 ; RV64I-LABEL: ror_i64:
259 ; RV64I-NEXT: neg a2, a1
260 ; RV64I-NEXT: srl a1, a0, a1
261 ; RV64I-NEXT: sll a0, a0, a2
262 ; RV64I-NEXT: or a0, a1, a0
265 ; RV64ZBB-ZBKB-LABEL: ror_i64:
266 ; RV64ZBB-ZBKB: # %bb.0:
267 ; RV64ZBB-ZBKB-NEXT: ror a0, a0, a1
268 ; RV64ZBB-ZBKB-NEXT: ret
269 %or = tail call i64 @llvm.fshr.i64(i64 %a, i64 %a, i64 %b)
273 define signext i32 @rori_i32_fshl(i32 signext %a) nounwind {
274 ; RV64I-LABEL: rori_i32_fshl:
276 ; RV64I-NEXT: slliw a1, a0, 31
277 ; RV64I-NEXT: srliw a0, a0, 1
278 ; RV64I-NEXT: or a0, a1, a0
281 ; RV64ZBB-ZBKB-LABEL: rori_i32_fshl:
282 ; RV64ZBB-ZBKB: # %bb.0:
283 ; RV64ZBB-ZBKB-NEXT: roriw a0, a0, 1
284 ; RV64ZBB-ZBKB-NEXT: ret
285 %1 = tail call i32 @llvm.fshl.i32(i32 %a, i32 %a, i32 31)
289 ; Similar to rori_i32_fshl, but doesn't sign extend the result.
290 define void @rori_i32_fshl_nosext(i32 signext %a, ptr %x) nounwind {
291 ; RV64I-LABEL: rori_i32_fshl_nosext:
293 ; RV64I-NEXT: slli a2, a0, 31
294 ; RV64I-NEXT: srliw a0, a0, 1
295 ; RV64I-NEXT: or a0, a2, a0
296 ; RV64I-NEXT: sw a0, 0(a1)
299 ; RV64ZBB-ZBKB-LABEL: rori_i32_fshl_nosext:
300 ; RV64ZBB-ZBKB: # %bb.0:
301 ; RV64ZBB-ZBKB-NEXT: roriw a0, a0, 1
302 ; RV64ZBB-ZBKB-NEXT: sw a0, 0(a1)
303 ; RV64ZBB-ZBKB-NEXT: ret
304 %1 = tail call i32 @llvm.fshl.i32(i32 %a, i32 %a, i32 31)
309 define signext i32 @rori_i32_fshr(i32 signext %a) nounwind {
310 ; RV64I-LABEL: rori_i32_fshr:
312 ; RV64I-NEXT: srliw a1, a0, 31
313 ; RV64I-NEXT: slliw a0, a0, 1
314 ; RV64I-NEXT: or a0, a1, a0
317 ; RV64ZBB-ZBKB-LABEL: rori_i32_fshr:
318 ; RV64ZBB-ZBKB: # %bb.0:
319 ; RV64ZBB-ZBKB-NEXT: roriw a0, a0, 31
320 ; RV64ZBB-ZBKB-NEXT: ret
321 %1 = tail call i32 @llvm.fshr.i32(i32 %a, i32 %a, i32 31)
325 ; Similar to rori_i32_fshr, but doesn't sign extend the result.
326 define void @rori_i32_fshr_nosext(i32 signext %a, ptr %x) nounwind {
327 ; RV64I-LABEL: rori_i32_fshr_nosext:
329 ; RV64I-NEXT: srliw a2, a0, 31
330 ; RV64I-NEXT: slli a0, a0, 1
331 ; RV64I-NEXT: or a0, a2, a0
332 ; RV64I-NEXT: sw a0, 0(a1)
335 ; RV64ZBB-ZBKB-LABEL: rori_i32_fshr_nosext:
336 ; RV64ZBB-ZBKB: # %bb.0:
337 ; RV64ZBB-ZBKB-NEXT: roriw a0, a0, 31
338 ; RV64ZBB-ZBKB-NEXT: sw a0, 0(a1)
339 ; RV64ZBB-ZBKB-NEXT: ret
340 %1 = tail call i32 @llvm.fshr.i32(i32 %a, i32 %a, i32 31)
345 ; This test is similar to the type legalized version of the fshl/fshr tests, but
346 ; instead of having the same input to both shifts it has different inputs. Make
347 ; sure we don't match it as a roriw.
348 define signext i32 @not_rori_i32(i32 signext %x, i32 signext %y) nounwind {
349 ; CHECK-LABEL: not_rori_i32:
351 ; CHECK-NEXT: slliw a0, a0, 31
352 ; CHECK-NEXT: srliw a1, a1, 1
353 ; CHECK-NEXT: or a0, a0, a1
361 ; This is similar to the type legalized roriw pattern, but the and mask is more
362 ; than 32 bits so the lshr doesn't shift zeroes into the lower 32 bits. Make
363 ; sure we don't match it to roriw.
364 define i64 @roriw_bug(i64 %x) nounwind {
365 ; CHECK-LABEL: roriw_bug:
367 ; CHECK-NEXT: andi a1, a0, -2
368 ; CHECK-NEXT: srli a2, a1, 1
369 ; CHECK-NEXT: slli a0, a0, 63
370 ; CHECK-NEXT: slli a2, a2, 32
371 ; CHECK-NEXT: or a0, a0, a2
372 ; CHECK-NEXT: srai a0, a0, 32
373 ; CHECK-NEXT: xor a0, a1, a0
376 %b = and i64 %x, 18446744073709551614
381 %g = xor i64 %b, %f ; to increase the use count on %b to disable SimplifyDemandedBits.
385 define i64 @rori_i64_fshl(i64 %a) nounwind {
386 ; RV64I-LABEL: rori_i64_fshl:
388 ; RV64I-NEXT: slli a1, a0, 63
389 ; RV64I-NEXT: srli a0, a0, 1
390 ; RV64I-NEXT: or a0, a1, a0
393 ; RV64ZBB-ZBKB-LABEL: rori_i64_fshl:
394 ; RV64ZBB-ZBKB: # %bb.0:
395 ; RV64ZBB-ZBKB-NEXT: rori a0, a0, 1
396 ; RV64ZBB-ZBKB-NEXT: ret
397 %1 = tail call i64 @llvm.fshl.i64(i64 %a, i64 %a, i64 63)
401 define i64 @rori_i64_fshr(i64 %a) nounwind {
402 ; RV64I-LABEL: rori_i64_fshr:
404 ; RV64I-NEXT: srli a1, a0, 63
405 ; RV64I-NEXT: slli a0, a0, 1
406 ; RV64I-NEXT: or a0, a1, a0
409 ; RV64ZBB-ZBKB-LABEL: rori_i64_fshr:
410 ; RV64ZBB-ZBKB: # %bb.0:
411 ; RV64ZBB-ZBKB-NEXT: rori a0, a0, 63
412 ; RV64ZBB-ZBKB-NEXT: ret
413 %1 = tail call i64 @llvm.fshr.i64(i64 %a, i64 %a, i64 63)
417 define i8 @srli_i8(i8 %a) nounwind {
418 ; CHECK-LABEL: srli_i8:
420 ; CHECK-NEXT: andi a0, a0, 255
421 ; CHECK-NEXT: srli a0, a0, 6
427 ; FIXME: We should use slli+srai with Zbb for better compression.
428 define i8 @srai_i8(i8 %a) nounwind {
429 ; RV64I-LABEL: srai_i8:
431 ; RV64I-NEXT: slli a0, a0, 56
432 ; RV64I-NEXT: srai a0, a0, 61
435 ; RV64ZBB-LABEL: srai_i8:
437 ; RV64ZBB-NEXT: sext.b a0, a0
438 ; RV64ZBB-NEXT: srai a0, a0, 5
441 ; RV64ZBKB-LABEL: srai_i8:
443 ; RV64ZBKB-NEXT: slli a0, a0, 56
444 ; RV64ZBKB-NEXT: srai a0, a0, 61
450 ; FIXME: We should use slli+srli.
451 define i16 @srli_i16(i16 %a) nounwind {
452 ; RV64I-LABEL: srli_i16:
454 ; RV64I-NEXT: lui a1, 16
455 ; RV64I-NEXT: addiw a1, a1, -1
456 ; RV64I-NEXT: and a0, a0, a1
457 ; RV64I-NEXT: srli a0, a0, 6
460 ; RV64ZBB-ZBKB-LABEL: srli_i16:
461 ; RV64ZBB-ZBKB: # %bb.0:
462 ; RV64ZBB-ZBKB-NEXT: zext.h a0, a0
463 ; RV64ZBB-ZBKB-NEXT: srli a0, a0, 6
464 ; RV64ZBB-ZBKB-NEXT: ret
469 ; FIXME: We should use slli+srai with Zbb for better compression.
470 define i16 @srai_i16(i16 %a) nounwind {
471 ; RV64I-LABEL: srai_i16:
473 ; RV64I-NEXT: slli a0, a0, 48
474 ; RV64I-NEXT: srai a0, a0, 57
477 ; RV64ZBB-LABEL: srai_i16:
479 ; RV64ZBB-NEXT: sext.h a0, a0
480 ; RV64ZBB-NEXT: srai a0, a0, 9
483 ; RV64ZBKB-LABEL: srai_i16:
485 ; RV64ZBKB-NEXT: slli a0, a0, 48
486 ; RV64ZBKB-NEXT: srai a0, a0, 57