1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
3 ; RUN: -riscv-experimental-rv64-legal-i32 | FileCheck %s -check-prefix=RV64I
4 ; RUN: llc -mtriple=riscv64 -mattr=+zbb -verify-machineinstrs < %s \
5 ; RUN: -riscv-experimental-rv64-legal-i32 | FileCheck %s -check-prefix=RV64ZBB
7 declare i32 @llvm.ctlz.i32(i32, i1)
9 define signext i32 @ctlz_i32(i32 signext %a) nounwind {
10 ; RV64I-LABEL: ctlz_i32:
12 ; RV64I-NEXT: beqz a0, .LBB0_2
13 ; RV64I-NEXT: # %bb.1: # %cond.false
14 ; RV64I-NEXT: addi sp, sp, -16
15 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
16 ; RV64I-NEXT: srliw a1, a0, 1
17 ; RV64I-NEXT: or a0, a0, a1
18 ; RV64I-NEXT: srliw a1, a0, 2
19 ; RV64I-NEXT: or a0, a0, a1
20 ; RV64I-NEXT: srliw a1, a0, 4
21 ; RV64I-NEXT: or a0, a0, a1
22 ; RV64I-NEXT: srliw a1, a0, 8
23 ; RV64I-NEXT: or a0, a0, a1
24 ; RV64I-NEXT: srliw a1, a0, 16
25 ; RV64I-NEXT: or a0, a0, a1
26 ; RV64I-NEXT: not a0, a0
27 ; RV64I-NEXT: srliw a1, a0, 1
28 ; RV64I-NEXT: lui a2, 349525
29 ; RV64I-NEXT: addi a2, a2, 1365
30 ; RV64I-NEXT: and a1, a1, a2
31 ; RV64I-NEXT: subw a0, a0, a1
32 ; RV64I-NEXT: lui a1, 209715
33 ; RV64I-NEXT: addi a1, a1, 819
34 ; RV64I-NEXT: and a2, a0, a1
35 ; RV64I-NEXT: srliw a0, a0, 2
36 ; RV64I-NEXT: and a0, a0, a1
37 ; RV64I-NEXT: add a0, a2, a0
38 ; RV64I-NEXT: srliw a1, a0, 4
39 ; RV64I-NEXT: addw a0, a0, a1
40 ; RV64I-NEXT: lui a1, 61681
41 ; RV64I-NEXT: addiw a1, a1, -241
42 ; RV64I-NEXT: and a0, a0, a1
43 ; RV64I-NEXT: lui a1, 4112
44 ; RV64I-NEXT: addiw a1, a1, 257
45 ; RV64I-NEXT: call __muldi3
46 ; RV64I-NEXT: srliw a0, a0, 24
47 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
48 ; RV64I-NEXT: addi sp, sp, 16
50 ; RV64I-NEXT: .LBB0_2:
51 ; RV64I-NEXT: li a0, 32
54 ; RV64ZBB-LABEL: ctlz_i32:
56 ; RV64ZBB-NEXT: clzw a0, a0
58 %1 = call i32 @llvm.ctlz.i32(i32 %a, i1 false)
62 define signext i32 @log2_i32(i32 signext %a) nounwind {
63 ; RV64I-LABEL: log2_i32:
65 ; RV64I-NEXT: beqz a0, .LBB1_2
66 ; RV64I-NEXT: # %bb.1: # %cond.false
67 ; RV64I-NEXT: addi sp, sp, -16
68 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
69 ; RV64I-NEXT: srliw a1, a0, 1
70 ; RV64I-NEXT: or a0, a0, a1
71 ; RV64I-NEXT: srliw a1, a0, 2
72 ; RV64I-NEXT: or a0, a0, a1
73 ; RV64I-NEXT: srliw a1, a0, 4
74 ; RV64I-NEXT: or a0, a0, a1
75 ; RV64I-NEXT: srliw a1, a0, 8
76 ; RV64I-NEXT: or a0, a0, a1
77 ; RV64I-NEXT: srliw a1, a0, 16
78 ; RV64I-NEXT: or a0, a0, a1
79 ; RV64I-NEXT: not a0, a0
80 ; RV64I-NEXT: srliw a1, a0, 1
81 ; RV64I-NEXT: lui a2, 349525
82 ; RV64I-NEXT: addi a2, a2, 1365
83 ; RV64I-NEXT: and a1, a1, a2
84 ; RV64I-NEXT: subw a0, a0, a1
85 ; RV64I-NEXT: lui a1, 209715
86 ; RV64I-NEXT: addi a1, a1, 819
87 ; RV64I-NEXT: and a2, a0, a1
88 ; RV64I-NEXT: srliw a0, a0, 2
89 ; RV64I-NEXT: and a0, a0, a1
90 ; RV64I-NEXT: add a0, a2, a0
91 ; RV64I-NEXT: srliw a1, a0, 4
92 ; RV64I-NEXT: addw a0, a0, a1
93 ; RV64I-NEXT: lui a1, 61681
94 ; RV64I-NEXT: addiw a1, a1, -241
95 ; RV64I-NEXT: and a0, a0, a1
96 ; RV64I-NEXT: lui a1, 4112
97 ; RV64I-NEXT: addiw a1, a1, 257
98 ; RV64I-NEXT: call __muldi3
99 ; RV64I-NEXT: srliw a0, a0, 24
100 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
101 ; RV64I-NEXT: addi sp, sp, 16
102 ; RV64I-NEXT: j .LBB1_3
103 ; RV64I-NEXT: .LBB1_2:
104 ; RV64I-NEXT: li a0, 32
105 ; RV64I-NEXT: .LBB1_3: # %cond.end
106 ; RV64I-NEXT: li a1, 31
107 ; RV64I-NEXT: subw a0, a1, a0
110 ; RV64ZBB-LABEL: log2_i32:
112 ; RV64ZBB-NEXT: clzw a0, a0
113 ; RV64ZBB-NEXT: li a1, 31
114 ; RV64ZBB-NEXT: subw a0, a1, a0
116 %1 = call i32 @llvm.ctlz.i32(i32 %a, i1 false)
121 define signext i32 @log2_ceil_i32(i32 signext %a) nounwind {
122 ; RV64I-LABEL: log2_ceil_i32:
124 ; RV64I-NEXT: addi sp, sp, -16
125 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
126 ; RV64I-NEXT: sd s0, 0(sp) # 8-byte Folded Spill
127 ; RV64I-NEXT: addiw a0, a0, -1
128 ; RV64I-NEXT: li s0, 32
129 ; RV64I-NEXT: li a1, 32
130 ; RV64I-NEXT: beqz a0, .LBB2_2
131 ; RV64I-NEXT: # %bb.1: # %cond.false
132 ; RV64I-NEXT: srliw a1, a0, 1
133 ; RV64I-NEXT: or a0, a0, a1
134 ; RV64I-NEXT: srliw a1, a0, 2
135 ; RV64I-NEXT: or a0, a0, a1
136 ; RV64I-NEXT: srliw a1, a0, 4
137 ; RV64I-NEXT: or a0, a0, a1
138 ; RV64I-NEXT: srliw a1, a0, 8
139 ; RV64I-NEXT: or a0, a0, a1
140 ; RV64I-NEXT: srliw a1, a0, 16
141 ; RV64I-NEXT: or a0, a0, a1
142 ; RV64I-NEXT: not a0, a0
143 ; RV64I-NEXT: srliw a1, a0, 1
144 ; RV64I-NEXT: lui a2, 349525
145 ; RV64I-NEXT: addi a2, a2, 1365
146 ; RV64I-NEXT: and a1, a1, a2
147 ; RV64I-NEXT: subw a0, a0, a1
148 ; RV64I-NEXT: lui a1, 209715
149 ; RV64I-NEXT: addi a1, a1, 819
150 ; RV64I-NEXT: and a2, a0, a1
151 ; RV64I-NEXT: srliw a0, a0, 2
152 ; RV64I-NEXT: and a0, a0, a1
153 ; RV64I-NEXT: add a0, a2, a0
154 ; RV64I-NEXT: srliw a1, a0, 4
155 ; RV64I-NEXT: addw a0, a0, a1
156 ; RV64I-NEXT: lui a1, 61681
157 ; RV64I-NEXT: addiw a1, a1, -241
158 ; RV64I-NEXT: and a0, a0, a1
159 ; RV64I-NEXT: lui a1, 4112
160 ; RV64I-NEXT: addiw a1, a1, 257
161 ; RV64I-NEXT: call __muldi3
162 ; RV64I-NEXT: srliw a1, a0, 24
163 ; RV64I-NEXT: .LBB2_2: # %cond.end
164 ; RV64I-NEXT: subw a0, s0, a1
165 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
166 ; RV64I-NEXT: ld s0, 0(sp) # 8-byte Folded Reload
167 ; RV64I-NEXT: addi sp, sp, 16
170 ; RV64ZBB-LABEL: log2_ceil_i32:
172 ; RV64ZBB-NEXT: addi a0, a0, -1
173 ; RV64ZBB-NEXT: clzw a0, a0
174 ; RV64ZBB-NEXT: li a1, 32
175 ; RV64ZBB-NEXT: subw a0, a1, a0
178 %2 = call i32 @llvm.ctlz.i32(i32 %1, i1 false)
183 define signext i32 @findLastSet_i32(i32 signext %a) nounwind {
184 ; RV64I-LABEL: findLastSet_i32:
186 ; RV64I-NEXT: addi sp, sp, -16
187 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
188 ; RV64I-NEXT: sd s0, 0(sp) # 8-byte Folded Spill
189 ; RV64I-NEXT: mv s0, a0
190 ; RV64I-NEXT: srliw a0, a0, 1
191 ; RV64I-NEXT: or a0, s0, a0
192 ; RV64I-NEXT: srliw a1, a0, 2
193 ; RV64I-NEXT: or a0, a0, a1
194 ; RV64I-NEXT: srliw a1, a0, 4
195 ; RV64I-NEXT: or a0, a0, a1
196 ; RV64I-NEXT: srliw a1, a0, 8
197 ; RV64I-NEXT: or a0, a0, a1
198 ; RV64I-NEXT: srliw a1, a0, 16
199 ; RV64I-NEXT: or a0, a0, a1
200 ; RV64I-NEXT: not a0, a0
201 ; RV64I-NEXT: srliw a1, a0, 1
202 ; RV64I-NEXT: lui a2, 349525
203 ; RV64I-NEXT: addi a2, a2, 1365
204 ; RV64I-NEXT: and a1, a1, a2
205 ; RV64I-NEXT: subw a0, a0, a1
206 ; RV64I-NEXT: lui a1, 209715
207 ; RV64I-NEXT: addi a1, a1, 819
208 ; RV64I-NEXT: and a2, a0, a1
209 ; RV64I-NEXT: srliw a0, a0, 2
210 ; RV64I-NEXT: and a0, a0, a1
211 ; RV64I-NEXT: add a0, a2, a0
212 ; RV64I-NEXT: srliw a1, a0, 4
213 ; RV64I-NEXT: addw a0, a0, a1
214 ; RV64I-NEXT: lui a1, 61681
215 ; RV64I-NEXT: addiw a1, a1, -241
216 ; RV64I-NEXT: and a0, a0, a1
217 ; RV64I-NEXT: lui a1, 4112
218 ; RV64I-NEXT: addiw a1, a1, 257
219 ; RV64I-NEXT: call __muldi3
220 ; RV64I-NEXT: srliw a0, a0, 24
221 ; RV64I-NEXT: xori a0, a0, 31
222 ; RV64I-NEXT: snez a1, s0
223 ; RV64I-NEXT: addiw a1, a1, -1
224 ; RV64I-NEXT: or a0, a1, a0
225 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
226 ; RV64I-NEXT: ld s0, 0(sp) # 8-byte Folded Reload
227 ; RV64I-NEXT: addi sp, sp, 16
230 ; RV64ZBB-LABEL: findLastSet_i32:
232 ; RV64ZBB-NEXT: clzw a1, a0
233 ; RV64ZBB-NEXT: xori a1, a1, 31
234 ; RV64ZBB-NEXT: snez a0, a0
235 ; RV64ZBB-NEXT: addiw a0, a0, -1
236 ; RV64ZBB-NEXT: or a0, a0, a1
238 %1 = call i32 @llvm.ctlz.i32(i32 %a, i1 true)
240 %3 = icmp eq i32 %a, 0
241 %4 = select i1 %3, i32 -1, i32 %2
245 define i32 @ctlz_lshr_i32(i32 signext %a) {
246 ; RV64I-LABEL: ctlz_lshr_i32:
248 ; RV64I-NEXT: srliw a0, a0, 1
249 ; RV64I-NEXT: beqz a0, .LBB4_2
250 ; RV64I-NEXT: # %bb.1: # %cond.false
251 ; RV64I-NEXT: addi sp, sp, -16
252 ; RV64I-NEXT: .cfi_def_cfa_offset 16
253 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
254 ; RV64I-NEXT: .cfi_offset ra, -8
255 ; RV64I-NEXT: srliw a1, a0, 1
256 ; RV64I-NEXT: or a0, a0, a1
257 ; RV64I-NEXT: srliw a1, a0, 2
258 ; RV64I-NEXT: or a0, a0, a1
259 ; RV64I-NEXT: srliw a1, a0, 4
260 ; RV64I-NEXT: or a0, a0, a1
261 ; RV64I-NEXT: srliw a1, a0, 8
262 ; RV64I-NEXT: or a0, a0, a1
263 ; RV64I-NEXT: srliw a1, a0, 16
264 ; RV64I-NEXT: or a0, a0, a1
265 ; RV64I-NEXT: not a0, a0
266 ; RV64I-NEXT: srliw a1, a0, 1
267 ; RV64I-NEXT: lui a2, 349525
268 ; RV64I-NEXT: addi a2, a2, 1365
269 ; RV64I-NEXT: and a1, a1, a2
270 ; RV64I-NEXT: subw a0, a0, a1
271 ; RV64I-NEXT: lui a1, 209715
272 ; RV64I-NEXT: addi a1, a1, 819
273 ; RV64I-NEXT: and a2, a0, a1
274 ; RV64I-NEXT: srliw a0, a0, 2
275 ; RV64I-NEXT: and a0, a0, a1
276 ; RV64I-NEXT: add a0, a2, a0
277 ; RV64I-NEXT: srliw a1, a0, 4
278 ; RV64I-NEXT: addw a0, a0, a1
279 ; RV64I-NEXT: lui a1, 61681
280 ; RV64I-NEXT: addiw a1, a1, -241
281 ; RV64I-NEXT: and a0, a0, a1
282 ; RV64I-NEXT: lui a1, 4112
283 ; RV64I-NEXT: addiw a1, a1, 257
284 ; RV64I-NEXT: call __muldi3
285 ; RV64I-NEXT: srliw a0, a0, 24
286 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
287 ; RV64I-NEXT: addi sp, sp, 16
289 ; RV64I-NEXT: .LBB4_2:
290 ; RV64I-NEXT: li a0, 32
293 ; RV64ZBB-LABEL: ctlz_lshr_i32:
295 ; RV64ZBB-NEXT: srliw a0, a0, 1
296 ; RV64ZBB-NEXT: clzw a0, a0
299 %2 = call i32 @llvm.ctlz.i32(i32 %1, i1 false)
303 declare i64 @llvm.ctlz.i64(i64, i1)
305 define i64 @ctlz_i64(i64 %a) nounwind {
306 ; RV64I-LABEL: ctlz_i64:
308 ; RV64I-NEXT: beqz a0, .LBB5_2
309 ; RV64I-NEXT: # %bb.1: # %cond.false
310 ; RV64I-NEXT: addi sp, sp, -16
311 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
312 ; RV64I-NEXT: srli a1, a0, 1
313 ; RV64I-NEXT: or a0, a0, a1
314 ; RV64I-NEXT: srli a1, a0, 2
315 ; RV64I-NEXT: or a0, a0, a1
316 ; RV64I-NEXT: srli a1, a0, 4
317 ; RV64I-NEXT: or a0, a0, a1
318 ; RV64I-NEXT: srli a1, a0, 8
319 ; RV64I-NEXT: or a0, a0, a1
320 ; RV64I-NEXT: srli a1, a0, 16
321 ; RV64I-NEXT: or a0, a0, a1
322 ; RV64I-NEXT: srli a1, a0, 32
323 ; RV64I-NEXT: or a0, a0, a1
324 ; RV64I-NEXT: not a0, a0
325 ; RV64I-NEXT: srli a1, a0, 1
326 ; RV64I-NEXT: lui a2, 349525
327 ; RV64I-NEXT: addiw a2, a2, 1365
328 ; RV64I-NEXT: slli a3, a2, 32
329 ; RV64I-NEXT: add a2, a2, a3
330 ; RV64I-NEXT: and a1, a1, a2
331 ; RV64I-NEXT: sub a0, a0, a1
332 ; RV64I-NEXT: lui a1, 209715
333 ; RV64I-NEXT: addiw a1, a1, 819
334 ; RV64I-NEXT: slli a2, a1, 32
335 ; RV64I-NEXT: add a1, a1, a2
336 ; RV64I-NEXT: and a2, a0, a1
337 ; RV64I-NEXT: srli a0, a0, 2
338 ; RV64I-NEXT: and a0, a0, a1
339 ; RV64I-NEXT: add a0, a2, a0
340 ; RV64I-NEXT: srli a1, a0, 4
341 ; RV64I-NEXT: add a0, a0, a1
342 ; RV64I-NEXT: lui a1, 61681
343 ; RV64I-NEXT: addiw a1, a1, -241
344 ; RV64I-NEXT: slli a2, a1, 32
345 ; RV64I-NEXT: add a1, a1, a2
346 ; RV64I-NEXT: and a0, a0, a1
347 ; RV64I-NEXT: lui a1, 4112
348 ; RV64I-NEXT: addiw a1, a1, 257
349 ; RV64I-NEXT: slli a2, a1, 32
350 ; RV64I-NEXT: add a1, a1, a2
351 ; RV64I-NEXT: call __muldi3
352 ; RV64I-NEXT: srli a0, a0, 56
353 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
354 ; RV64I-NEXT: addi sp, sp, 16
356 ; RV64I-NEXT: .LBB5_2:
357 ; RV64I-NEXT: li a0, 64
360 ; RV64ZBB-LABEL: ctlz_i64:
362 ; RV64ZBB-NEXT: clz a0, a0
364 %1 = call i64 @llvm.ctlz.i64(i64 %a, i1 false)
368 declare i32 @llvm.cttz.i32(i32, i1)
370 define signext i32 @cttz_i32(i32 signext %a) nounwind {
371 ; RV64I-LABEL: cttz_i32:
373 ; RV64I-NEXT: beqz a0, .LBB6_2
374 ; RV64I-NEXT: # %bb.1: # %cond.false
375 ; RV64I-NEXT: addi sp, sp, -16
376 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
377 ; RV64I-NEXT: negw a1, a0
378 ; RV64I-NEXT: and a0, a0, a1
379 ; RV64I-NEXT: lui a1, 30667
380 ; RV64I-NEXT: addiw a1, a1, 1329
381 ; RV64I-NEXT: call __muldi3
382 ; RV64I-NEXT: srliw a0, a0, 27
383 ; RV64I-NEXT: lui a1, %hi(.LCPI6_0)
384 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI6_0)
385 ; RV64I-NEXT: add a0, a1, a0
386 ; RV64I-NEXT: lbu a0, 0(a0)
387 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
388 ; RV64I-NEXT: addi sp, sp, 16
390 ; RV64I-NEXT: .LBB6_2:
391 ; RV64I-NEXT: li a0, 32
394 ; RV64ZBB-LABEL: cttz_i32:
396 ; RV64ZBB-NEXT: ctzw a0, a0
398 %1 = call i32 @llvm.cttz.i32(i32 %a, i1 false)
402 define signext i32 @cttz_zero_undef_i32(i32 signext %a) nounwind {
403 ; RV64I-LABEL: cttz_zero_undef_i32:
405 ; RV64I-NEXT: addi sp, sp, -16
406 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
407 ; RV64I-NEXT: negw a1, a0
408 ; RV64I-NEXT: and a0, a0, a1
409 ; RV64I-NEXT: lui a1, 30667
410 ; RV64I-NEXT: addiw a1, a1, 1329
411 ; RV64I-NEXT: call __muldi3
412 ; RV64I-NEXT: srliw a0, a0, 27
413 ; RV64I-NEXT: lui a1, %hi(.LCPI7_0)
414 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI7_0)
415 ; RV64I-NEXT: add a0, a1, a0
416 ; RV64I-NEXT: lbu a0, 0(a0)
417 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
418 ; RV64I-NEXT: addi sp, sp, 16
421 ; RV64ZBB-LABEL: cttz_zero_undef_i32:
423 ; RV64ZBB-NEXT: ctzw a0, a0
425 %1 = call i32 @llvm.cttz.i32(i32 %a, i1 true)
429 define signext i32 @findFirstSet_i32(i32 signext %a) nounwind {
430 ; RV64I-LABEL: findFirstSet_i32:
432 ; RV64I-NEXT: addi sp, sp, -16
433 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
434 ; RV64I-NEXT: sd s0, 0(sp) # 8-byte Folded Spill
435 ; RV64I-NEXT: mv s0, a0
436 ; RV64I-NEXT: negw a0, a0
437 ; RV64I-NEXT: and a0, s0, a0
438 ; RV64I-NEXT: lui a1, 30667
439 ; RV64I-NEXT: addiw a1, a1, 1329
440 ; RV64I-NEXT: call __muldi3
441 ; RV64I-NEXT: srliw a0, a0, 27
442 ; RV64I-NEXT: lui a1, %hi(.LCPI8_0)
443 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI8_0)
444 ; RV64I-NEXT: add a0, a1, a0
445 ; RV64I-NEXT: lbu a0, 0(a0)
446 ; RV64I-NEXT: snez a1, s0
447 ; RV64I-NEXT: addi a1, a1, -1
448 ; RV64I-NEXT: or a0, a1, a0
449 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
450 ; RV64I-NEXT: ld s0, 0(sp) # 8-byte Folded Reload
451 ; RV64I-NEXT: addi sp, sp, 16
454 ; RV64ZBB-LABEL: findFirstSet_i32:
456 ; RV64ZBB-NEXT: ctzw a1, a0
457 ; RV64ZBB-NEXT: snez a0, a0
458 ; RV64ZBB-NEXT: addiw a0, a0, -1
459 ; RV64ZBB-NEXT: or a0, a0, a1
461 %1 = call i32 @llvm.cttz.i32(i32 %a, i1 true)
462 %2 = icmp eq i32 %a, 0
463 %3 = select i1 %2, i32 -1, i32 %1
467 define signext i32 @ffs_i32(i32 signext %a) nounwind {
468 ; RV64I-LABEL: ffs_i32:
470 ; RV64I-NEXT: addi sp, sp, -16
471 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
472 ; RV64I-NEXT: sd s0, 0(sp) # 8-byte Folded Spill
473 ; RV64I-NEXT: mv s0, a0
474 ; RV64I-NEXT: negw a0, a0
475 ; RV64I-NEXT: and a0, s0, a0
476 ; RV64I-NEXT: lui a1, 30667
477 ; RV64I-NEXT: addiw a1, a1, 1329
478 ; RV64I-NEXT: call __muldi3
479 ; RV64I-NEXT: srliw a0, a0, 27
480 ; RV64I-NEXT: lui a1, %hi(.LCPI9_0)
481 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI9_0)
482 ; RV64I-NEXT: add a0, a1, a0
483 ; RV64I-NEXT: lbu a0, 0(a0)
484 ; RV64I-NEXT: addi a0, a0, 1
485 ; RV64I-NEXT: seqz a1, s0
486 ; RV64I-NEXT: addi a1, a1, -1
487 ; RV64I-NEXT: and a0, a1, a0
488 ; RV64I-NEXT: slli a0, a0, 32
489 ; RV64I-NEXT: srli a0, a0, 32
490 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
491 ; RV64I-NEXT: ld s0, 0(sp) # 8-byte Folded Reload
492 ; RV64I-NEXT: addi sp, sp, 16
495 ; RV64ZBB-LABEL: ffs_i32:
497 ; RV64ZBB-NEXT: ctzw a1, a0
498 ; RV64ZBB-NEXT: addi a1, a1, 1
499 ; RV64ZBB-NEXT: seqz a0, a0
500 ; RV64ZBB-NEXT: addi a0, a0, -1
501 ; RV64ZBB-NEXT: and a0, a0, a1
502 ; RV64ZBB-NEXT: zext.h a0, a0
504 %1 = call i32 @llvm.cttz.i32(i32 %a, i1 true)
506 %3 = icmp eq i32 %a, 0
507 %4 = select i1 %3, i32 0, i32 %2
511 declare i64 @llvm.cttz.i64(i64, i1)
513 define i64 @cttz_i64(i64 %a) nounwind {
514 ; RV64I-LABEL: cttz_i64:
516 ; RV64I-NEXT: beqz a0, .LBB10_2
517 ; RV64I-NEXT: # %bb.1: # %cond.false
518 ; RV64I-NEXT: addi sp, sp, -16
519 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
520 ; RV64I-NEXT: neg a1, a0
521 ; RV64I-NEXT: and a0, a0, a1
522 ; RV64I-NEXT: lui a1, %hi(.LCPI10_0)
523 ; RV64I-NEXT: ld a1, %lo(.LCPI10_0)(a1)
524 ; RV64I-NEXT: call __muldi3
525 ; RV64I-NEXT: srli a0, a0, 58
526 ; RV64I-NEXT: lui a1, %hi(.LCPI10_1)
527 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI10_1)
528 ; RV64I-NEXT: add a0, a1, a0
529 ; RV64I-NEXT: lbu a0, 0(a0)
530 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
531 ; RV64I-NEXT: addi sp, sp, 16
533 ; RV64I-NEXT: .LBB10_2:
534 ; RV64I-NEXT: li a0, 64
537 ; RV64ZBB-LABEL: cttz_i64:
539 ; RV64ZBB-NEXT: ctz a0, a0
541 %1 = call i64 @llvm.cttz.i64(i64 %a, i1 false)
545 declare i32 @llvm.ctpop.i32(i32)
547 define signext i32 @ctpop_i32(i32 signext %a) nounwind {
548 ; RV64I-LABEL: ctpop_i32:
550 ; RV64I-NEXT: addi sp, sp, -16
551 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
552 ; RV64I-NEXT: srliw a1, a0, 1
553 ; RV64I-NEXT: lui a2, 349525
554 ; RV64I-NEXT: addi a2, a2, 1365
555 ; RV64I-NEXT: and a1, a1, a2
556 ; RV64I-NEXT: subw a0, a0, a1
557 ; RV64I-NEXT: lui a1, 209715
558 ; RV64I-NEXT: addi a1, a1, 819
559 ; RV64I-NEXT: and a2, a0, a1
560 ; RV64I-NEXT: srliw a0, a0, 2
561 ; RV64I-NEXT: and a0, a0, a1
562 ; RV64I-NEXT: add a0, a2, a0
563 ; RV64I-NEXT: srliw a1, a0, 4
564 ; RV64I-NEXT: addw a0, a0, a1
565 ; RV64I-NEXT: lui a1, 61681
566 ; RV64I-NEXT: addiw a1, a1, -241
567 ; RV64I-NEXT: and a0, a0, a1
568 ; RV64I-NEXT: lui a1, 4112
569 ; RV64I-NEXT: addiw a1, a1, 257
570 ; RV64I-NEXT: call __muldi3
571 ; RV64I-NEXT: srliw a0, a0, 24
572 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
573 ; RV64I-NEXT: addi sp, sp, 16
576 ; RV64ZBB-LABEL: ctpop_i32:
578 ; RV64ZBB-NEXT: cpopw a0, a0
580 %1 = call i32 @llvm.ctpop.i32(i32 %a)
584 define signext i32 @ctpop_i32_load(ptr %p) nounwind {
585 ; RV64I-LABEL: ctpop_i32_load:
587 ; RV64I-NEXT: addi sp, sp, -16
588 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
589 ; RV64I-NEXT: lw a0, 0(a0)
590 ; RV64I-NEXT: srliw a1, a0, 1
591 ; RV64I-NEXT: lui a2, 349525
592 ; RV64I-NEXT: addi a2, a2, 1365
593 ; RV64I-NEXT: and a1, a1, a2
594 ; RV64I-NEXT: subw a0, a0, a1
595 ; RV64I-NEXT: lui a1, 209715
596 ; RV64I-NEXT: addi a1, a1, 819
597 ; RV64I-NEXT: and a2, a0, a1
598 ; RV64I-NEXT: srliw a0, a0, 2
599 ; RV64I-NEXT: and a0, a0, a1
600 ; RV64I-NEXT: add a0, a2, a0
601 ; RV64I-NEXT: srliw a1, a0, 4
602 ; RV64I-NEXT: addw a0, a0, a1
603 ; RV64I-NEXT: lui a1, 61681
604 ; RV64I-NEXT: addiw a1, a1, -241
605 ; RV64I-NEXT: and a0, a0, a1
606 ; RV64I-NEXT: lui a1, 4112
607 ; RV64I-NEXT: addiw a1, a1, 257
608 ; RV64I-NEXT: call __muldi3
609 ; RV64I-NEXT: srliw a0, a0, 24
610 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
611 ; RV64I-NEXT: addi sp, sp, 16
614 ; RV64ZBB-LABEL: ctpop_i32_load:
616 ; RV64ZBB-NEXT: lw a0, 0(a0)
617 ; RV64ZBB-NEXT: cpopw a0, a0
619 %a = load i32, ptr %p
620 %1 = call i32 @llvm.ctpop.i32(i32 %a)
624 declare i64 @llvm.ctpop.i64(i64)
626 define i64 @ctpop_i64(i64 %a) nounwind {
627 ; RV64I-LABEL: ctpop_i64:
629 ; RV64I-NEXT: addi sp, sp, -16
630 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
631 ; RV64I-NEXT: srli a1, a0, 1
632 ; RV64I-NEXT: lui a2, 349525
633 ; RV64I-NEXT: addiw a2, a2, 1365
634 ; RV64I-NEXT: slli a3, a2, 32
635 ; RV64I-NEXT: add a2, a2, a3
636 ; RV64I-NEXT: and a1, a1, a2
637 ; RV64I-NEXT: sub a0, a0, a1
638 ; RV64I-NEXT: lui a1, 209715
639 ; RV64I-NEXT: addiw a1, a1, 819
640 ; RV64I-NEXT: slli a2, a1, 32
641 ; RV64I-NEXT: add a1, a1, a2
642 ; RV64I-NEXT: and a2, a0, a1
643 ; RV64I-NEXT: srli a0, a0, 2
644 ; RV64I-NEXT: and a0, a0, a1
645 ; RV64I-NEXT: add a0, a2, a0
646 ; RV64I-NEXT: srli a1, a0, 4
647 ; RV64I-NEXT: add a0, a0, a1
648 ; RV64I-NEXT: lui a1, 61681
649 ; RV64I-NEXT: addiw a1, a1, -241
650 ; RV64I-NEXT: slli a2, a1, 32
651 ; RV64I-NEXT: add a1, a1, a2
652 ; RV64I-NEXT: and a0, a0, a1
653 ; RV64I-NEXT: lui a1, 4112
654 ; RV64I-NEXT: addiw a1, a1, 257
655 ; RV64I-NEXT: slli a2, a1, 32
656 ; RV64I-NEXT: add a1, a1, a2
657 ; RV64I-NEXT: call __muldi3
658 ; RV64I-NEXT: srli a0, a0, 56
659 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
660 ; RV64I-NEXT: addi sp, sp, 16
663 ; RV64ZBB-LABEL: ctpop_i64:
665 ; RV64ZBB-NEXT: cpop a0, a0
667 %1 = call i64 @llvm.ctpop.i64(i64 %a)
671 define signext i32 @sextb_i32(i32 signext %a) nounwind {
672 ; RV64I-LABEL: sextb_i32:
674 ; RV64I-NEXT: slli a0, a0, 56
675 ; RV64I-NEXT: srai a0, a0, 56
678 ; RV64ZBB-LABEL: sextb_i32:
680 ; RV64ZBB-NEXT: sext.b a0, a0
682 %shl = shl i32 %a, 24
683 %shr = ashr exact i32 %shl, 24
687 define i64 @sextb_i64(i64 %a) nounwind {
688 ; RV64I-LABEL: sextb_i64:
690 ; RV64I-NEXT: slli a0, a0, 56
691 ; RV64I-NEXT: srai a0, a0, 56
694 ; RV64ZBB-LABEL: sextb_i64:
696 ; RV64ZBB-NEXT: sext.b a0, a0
698 %shl = shl i64 %a, 56
699 %shr = ashr exact i64 %shl, 56
703 define signext i32 @sexth_i32(i32 signext %a) nounwind {
704 ; RV64I-LABEL: sexth_i32:
706 ; RV64I-NEXT: slli a0, a0, 48
707 ; RV64I-NEXT: srai a0, a0, 48
710 ; RV64ZBB-LABEL: sexth_i32:
712 ; RV64ZBB-NEXT: sext.h a0, a0
714 %shl = shl i32 %a, 16
715 %shr = ashr exact i32 %shl, 16
719 define i64 @sexth_i64(i64 %a) nounwind {
720 ; RV64I-LABEL: sexth_i64:
722 ; RV64I-NEXT: slli a0, a0, 48
723 ; RV64I-NEXT: srai a0, a0, 48
726 ; RV64ZBB-LABEL: sexth_i64:
728 ; RV64ZBB-NEXT: sext.h a0, a0
730 %shl = shl i64 %a, 48
731 %shr = ashr exact i64 %shl, 48
735 define signext i32 @min_i32(i32 signext %a, i32 signext %b) nounwind {
736 ; RV64I-LABEL: min_i32:
738 ; RV64I-NEXT: blt a0, a1, .LBB18_2
739 ; RV64I-NEXT: # %bb.1:
740 ; RV64I-NEXT: mv a0, a1
741 ; RV64I-NEXT: .LBB18_2:
744 ; RV64ZBB-LABEL: min_i32:
746 ; RV64ZBB-NEXT: min a0, a0, a1
748 %cmp = icmp slt i32 %a, %b
749 %cond = select i1 %cmp, i32 %a, i32 %b
753 define i64 @min_i64(i64 %a, i64 %b) nounwind {
754 ; RV64I-LABEL: min_i64:
756 ; RV64I-NEXT: blt a0, a1, .LBB19_2
757 ; RV64I-NEXT: # %bb.1:
758 ; RV64I-NEXT: mv a0, a1
759 ; RV64I-NEXT: .LBB19_2:
762 ; RV64ZBB-LABEL: min_i64:
764 ; RV64ZBB-NEXT: min a0, a0, a1
766 %cmp = icmp slt i64 %a, %b
767 %cond = select i1 %cmp, i64 %a, i64 %b
771 define signext i32 @max_i32(i32 signext %a, i32 signext %b) nounwind {
772 ; RV64I-LABEL: max_i32:
774 ; RV64I-NEXT: blt a1, a0, .LBB20_2
775 ; RV64I-NEXT: # %bb.1:
776 ; RV64I-NEXT: mv a0, a1
777 ; RV64I-NEXT: .LBB20_2:
780 ; RV64ZBB-LABEL: max_i32:
782 ; RV64ZBB-NEXT: max a0, a0, a1
784 %cmp = icmp sgt i32 %a, %b
785 %cond = select i1 %cmp, i32 %a, i32 %b
789 define i64 @max_i64(i64 %a, i64 %b) nounwind {
790 ; RV64I-LABEL: max_i64:
792 ; RV64I-NEXT: blt a1, a0, .LBB21_2
793 ; RV64I-NEXT: # %bb.1:
794 ; RV64I-NEXT: mv a0, a1
795 ; RV64I-NEXT: .LBB21_2:
798 ; RV64ZBB-LABEL: max_i64:
800 ; RV64ZBB-NEXT: max a0, a0, a1
802 %cmp = icmp sgt i64 %a, %b
803 %cond = select i1 %cmp, i64 %a, i64 %b
807 define signext i32 @minu_i32(i32 signext %a, i32 signext %b) nounwind {
808 ; RV64I-LABEL: minu_i32:
810 ; RV64I-NEXT: bltu a0, a1, .LBB22_2
811 ; RV64I-NEXT: # %bb.1:
812 ; RV64I-NEXT: mv a0, a1
813 ; RV64I-NEXT: .LBB22_2:
816 ; RV64ZBB-LABEL: minu_i32:
818 ; RV64ZBB-NEXT: minu a0, a0, a1
820 %cmp = icmp ult i32 %a, %b
821 %cond = select i1 %cmp, i32 %a, i32 %b
825 define i64 @minu_i64(i64 %a, i64 %b) nounwind {
826 ; RV64I-LABEL: minu_i64:
828 ; RV64I-NEXT: bltu a0, a1, .LBB23_2
829 ; RV64I-NEXT: # %bb.1:
830 ; RV64I-NEXT: mv a0, a1
831 ; RV64I-NEXT: .LBB23_2:
834 ; RV64ZBB-LABEL: minu_i64:
836 ; RV64ZBB-NEXT: minu a0, a0, a1
838 %cmp = icmp ult i64 %a, %b
839 %cond = select i1 %cmp, i64 %a, i64 %b
843 define signext i32 @maxu_i32(i32 signext %a, i32 signext %b) nounwind {
844 ; RV64I-LABEL: maxu_i32:
846 ; RV64I-NEXT: bltu a1, a0, .LBB24_2
847 ; RV64I-NEXT: # %bb.1:
848 ; RV64I-NEXT: mv a0, a1
849 ; RV64I-NEXT: .LBB24_2:
852 ; RV64ZBB-LABEL: maxu_i32:
854 ; RV64ZBB-NEXT: maxu a0, a0, a1
856 %cmp = icmp ugt i32 %a, %b
857 %cond = select i1 %cmp, i32 %a, i32 %b
861 define i64 @maxu_i64(i64 %a, i64 %b) nounwind {
862 ; RV64I-LABEL: maxu_i64:
864 ; RV64I-NEXT: bltu a1, a0, .LBB25_2
865 ; RV64I-NEXT: # %bb.1:
866 ; RV64I-NEXT: mv a0, a1
867 ; RV64I-NEXT: .LBB25_2:
870 ; RV64ZBB-LABEL: maxu_i64:
872 ; RV64ZBB-NEXT: maxu a0, a0, a1
874 %cmp = icmp ugt i64 %a, %b
875 %cond = select i1 %cmp, i64 %a, i64 %b
879 declare i32 @llvm.abs.i32(i32, i1 immarg)
881 define i32 @abs_i32(i32 %x) {
882 ; RV64I-LABEL: abs_i32:
884 ; RV64I-NEXT: sraiw a1, a0, 31
885 ; RV64I-NEXT: xor a0, a0, a1
886 ; RV64I-NEXT: subw a0, a0, a1
889 ; RV64ZBB-LABEL: abs_i32:
891 ; RV64ZBB-NEXT: sraiw a1, a0, 31
892 ; RV64ZBB-NEXT: xor a0, a0, a1
893 ; RV64ZBB-NEXT: subw a0, a0, a1
895 %abs = tail call i32 @llvm.abs.i32(i32 %x, i1 true)
899 define signext i32 @abs_i32_sext(i32 signext %x) {
900 ; RV64I-LABEL: abs_i32_sext:
902 ; RV64I-NEXT: sraiw a1, a0, 31
903 ; RV64I-NEXT: xor a0, a0, a1
904 ; RV64I-NEXT: subw a0, a0, a1
907 ; RV64ZBB-LABEL: abs_i32_sext:
909 ; RV64ZBB-NEXT: sraiw a1, a0, 31
910 ; RV64ZBB-NEXT: xor a0, a0, a1
911 ; RV64ZBB-NEXT: subw a0, a0, a1
913 %abs = tail call i32 @llvm.abs.i32(i32 %x, i1 true)
917 declare i64 @llvm.abs.i64(i64, i1 immarg)
919 define i64 @abs_i64(i64 %x) {
920 ; RV64I-LABEL: abs_i64:
922 ; RV64I-NEXT: srai a1, a0, 63
923 ; RV64I-NEXT: xor a0, a0, a1
924 ; RV64I-NEXT: sub a0, a0, a1
927 ; RV64ZBB-LABEL: abs_i64:
929 ; RV64ZBB-NEXT: neg a1, a0
930 ; RV64ZBB-NEXT: max a0, a0, a1
932 %abs = tail call i64 @llvm.abs.i64(i64 %x, i1 true)
936 define i32 @zexth_i32(i32 %a) nounwind {
937 ; RV64I-LABEL: zexth_i32:
939 ; RV64I-NEXT: slli a0, a0, 48
940 ; RV64I-NEXT: srli a0, a0, 48
943 ; RV64ZBB-LABEL: zexth_i32:
945 ; RV64ZBB-NEXT: slli a0, a0, 48
946 ; RV64ZBB-NEXT: srli a0, a0, 48
948 %and = and i32 %a, 65535
952 define i64 @zexth_i64(i64 %a) nounwind {
953 ; RV64I-LABEL: zexth_i64:
955 ; RV64I-NEXT: slli a0, a0, 48
956 ; RV64I-NEXT: srli a0, a0, 48
959 ; RV64ZBB-LABEL: zexth_i64:
961 ; RV64ZBB-NEXT: zext.h a0, a0
963 %and = and i64 %a, 65535
967 declare i32 @llvm.bswap.i32(i32)
969 define signext i32 @bswap_i32(i32 signext %a) nounwind {
970 ; RV64I-LABEL: bswap_i32:
972 ; RV64I-NEXT: srliw a1, a0, 8
973 ; RV64I-NEXT: lui a2, 16
974 ; RV64I-NEXT: addiw a2, a2, -256
975 ; RV64I-NEXT: and a1, a1, a2
976 ; RV64I-NEXT: srliw a3, a0, 24
977 ; RV64I-NEXT: or a1, a1, a3
978 ; RV64I-NEXT: and a2, a0, a2
979 ; RV64I-NEXT: slliw a2, a2, 8
980 ; RV64I-NEXT: slliw a0, a0, 24
981 ; RV64I-NEXT: or a0, a0, a2
982 ; RV64I-NEXT: or a0, a0, a1
985 ; RV64ZBB-LABEL: bswap_i32:
987 ; RV64ZBB-NEXT: rev8 a0, a0
988 ; RV64ZBB-NEXT: srai a0, a0, 32
990 %1 = tail call i32 @llvm.bswap.i32(i32 %a)
994 ; Similar to bswap_i32 but the result is not sign extended.
995 define void @bswap_i32_nosext(i32 signext %a, ptr %x) nounwind {
996 ; RV64I-LABEL: bswap_i32_nosext:
998 ; RV64I-NEXT: srliw a2, a0, 8
999 ; RV64I-NEXT: lui a3, 16
1000 ; RV64I-NEXT: addi a3, a3, -256
1001 ; RV64I-NEXT: and a2, a2, a3
1002 ; RV64I-NEXT: srliw a4, a0, 24
1003 ; RV64I-NEXT: or a2, a2, a4
1004 ; RV64I-NEXT: and a3, a0, a3
1005 ; RV64I-NEXT: slli a3, a3, 8
1006 ; RV64I-NEXT: slli a0, a0, 24
1007 ; RV64I-NEXT: or a0, a0, a3
1008 ; RV64I-NEXT: or a0, a0, a2
1009 ; RV64I-NEXT: sw a0, 0(a1)
1012 ; RV64ZBB-LABEL: bswap_i32_nosext:
1014 ; RV64ZBB-NEXT: rev8 a0, a0
1015 ; RV64ZBB-NEXT: srli a0, a0, 32
1016 ; RV64ZBB-NEXT: sw a0, 0(a1)
1018 %1 = tail call i32 @llvm.bswap.i32(i32 %a)
1019 store i32 %1, ptr %x
1023 declare i64 @llvm.bswap.i64(i64)
1025 define i64 @bswap_i64(i64 %a) {
1026 ; RV64I-LABEL: bswap_i64:
1028 ; RV64I-NEXT: srli a1, a0, 40
1029 ; RV64I-NEXT: lui a2, 16
1030 ; RV64I-NEXT: addiw a2, a2, -256
1031 ; RV64I-NEXT: and a1, a1, a2
1032 ; RV64I-NEXT: srli a3, a0, 56
1033 ; RV64I-NEXT: or a1, a1, a3
1034 ; RV64I-NEXT: srli a3, a0, 24
1035 ; RV64I-NEXT: lui a4, 4080
1036 ; RV64I-NEXT: and a3, a3, a4
1037 ; RV64I-NEXT: srli a5, a0, 8
1038 ; RV64I-NEXT: srliw a5, a5, 24
1039 ; RV64I-NEXT: slli a5, a5, 24
1040 ; RV64I-NEXT: or a3, a5, a3
1041 ; RV64I-NEXT: or a1, a3, a1
1042 ; RV64I-NEXT: and a4, a0, a4
1043 ; RV64I-NEXT: slli a4, a4, 24
1044 ; RV64I-NEXT: srliw a3, a0, 24
1045 ; RV64I-NEXT: slli a3, a3, 32
1046 ; RV64I-NEXT: or a3, a4, a3
1047 ; RV64I-NEXT: and a2, a0, a2
1048 ; RV64I-NEXT: slli a2, a2, 40
1049 ; RV64I-NEXT: slli a0, a0, 56
1050 ; RV64I-NEXT: or a0, a0, a2
1051 ; RV64I-NEXT: or a0, a0, a3
1052 ; RV64I-NEXT: or a0, a0, a1
1055 ; RV64ZBB-LABEL: bswap_i64:
1057 ; RV64ZBB-NEXT: rev8 a0, a0
1059 %1 = call i64 @llvm.bswap.i64(i64 %a)