1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
3 ; RUN: | FileCheck %s -check-prefix=RV64I
4 ; RUN: llc -mtriple=riscv64 -mattr=+xtheadbb -verify-machineinstrs < %s \
5 ; RUN: | FileCheck %s -check-prefix=RV64XTHEADBB
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: srliw a1, a0, 1
15 ; RV64I-NEXT: or a0, a0, a1
16 ; RV64I-NEXT: srliw a1, a0, 2
17 ; RV64I-NEXT: or a0, a0, a1
18 ; RV64I-NEXT: srliw a1, a0, 4
19 ; RV64I-NEXT: or a0, a0, a1
20 ; RV64I-NEXT: srliw a1, a0, 8
21 ; RV64I-NEXT: or a0, a0, a1
22 ; RV64I-NEXT: srliw a1, a0, 16
23 ; RV64I-NEXT: or a0, a0, a1
24 ; RV64I-NEXT: not a0, a0
25 ; RV64I-NEXT: srli a1, a0, 1
26 ; RV64I-NEXT: lui a2, 349525
27 ; RV64I-NEXT: addiw a2, a2, 1365
28 ; RV64I-NEXT: and a1, a1, a2
29 ; RV64I-NEXT: sub a0, a0, a1
30 ; RV64I-NEXT: lui a1, 209715
31 ; RV64I-NEXT: addiw a1, a1, 819
32 ; RV64I-NEXT: and a2, a0, a1
33 ; RV64I-NEXT: srli a0, a0, 2
34 ; RV64I-NEXT: and a0, a0, a1
35 ; RV64I-NEXT: add a0, a2, a0
36 ; RV64I-NEXT: srli a1, a0, 4
37 ; RV64I-NEXT: add a0, a0, a1
38 ; RV64I-NEXT: lui a1, 61681
39 ; RV64I-NEXT: addi a1, a1, -241
40 ; RV64I-NEXT: and a0, a0, a1
41 ; RV64I-NEXT: slli a1, a0, 8
42 ; RV64I-NEXT: add a0, a0, a1
43 ; RV64I-NEXT: slli a1, a0, 16
44 ; RV64I-NEXT: add a0, a0, a1
45 ; RV64I-NEXT: srliw a0, a0, 24
47 ; RV64I-NEXT: .LBB0_2:
48 ; RV64I-NEXT: li a0, 32
51 ; RV64XTHEADBB-LABEL: ctlz_i32:
52 ; RV64XTHEADBB: # %bb.0:
53 ; RV64XTHEADBB-NEXT: not a0, a0
54 ; RV64XTHEADBB-NEXT: slli a0, a0, 32
55 ; RV64XTHEADBB-NEXT: th.ff0 a0, a0
56 ; RV64XTHEADBB-NEXT: ret
57 %1 = call i32 @llvm.ctlz.i32(i32 %a, i1 false)
61 define signext i32 @log2_i32(i32 signext %a) nounwind {
62 ; RV64I-LABEL: log2_i32:
64 ; RV64I-NEXT: beqz a0, .LBB1_2
65 ; RV64I-NEXT: # %bb.1: # %cond.false
66 ; RV64I-NEXT: srliw a1, a0, 1
67 ; RV64I-NEXT: or a0, a0, a1
68 ; RV64I-NEXT: srliw a1, a0, 2
69 ; RV64I-NEXT: or a0, a0, a1
70 ; RV64I-NEXT: srliw a1, a0, 4
71 ; RV64I-NEXT: or a0, a0, a1
72 ; RV64I-NEXT: srliw a1, a0, 8
73 ; RV64I-NEXT: or a0, a0, a1
74 ; RV64I-NEXT: srliw a1, a0, 16
75 ; RV64I-NEXT: or a0, a0, a1
76 ; RV64I-NEXT: not a0, a0
77 ; RV64I-NEXT: srli a1, a0, 1
78 ; RV64I-NEXT: lui a2, 349525
79 ; RV64I-NEXT: addiw a2, a2, 1365
80 ; RV64I-NEXT: and a1, a1, a2
81 ; RV64I-NEXT: sub a0, a0, a1
82 ; RV64I-NEXT: lui a1, 209715
83 ; RV64I-NEXT: addiw a1, a1, 819
84 ; RV64I-NEXT: and a2, a0, a1
85 ; RV64I-NEXT: srli a0, a0, 2
86 ; RV64I-NEXT: and a0, a0, a1
87 ; RV64I-NEXT: add a0, a2, a0
88 ; RV64I-NEXT: srli a1, a0, 4
89 ; RV64I-NEXT: add a0, a0, a1
90 ; RV64I-NEXT: lui a1, 61681
91 ; RV64I-NEXT: addi a1, a1, -241
92 ; RV64I-NEXT: and a0, a0, a1
93 ; RV64I-NEXT: slli a1, a0, 8
94 ; RV64I-NEXT: add a0, a0, a1
95 ; RV64I-NEXT: slli a1, a0, 16
96 ; RV64I-NEXT: add a0, a0, a1
97 ; RV64I-NEXT: srliw a0, a0, 24
98 ; RV64I-NEXT: j .LBB1_3
99 ; RV64I-NEXT: .LBB1_2:
100 ; RV64I-NEXT: li a0, 32
101 ; RV64I-NEXT: .LBB1_3: # %cond.end
102 ; RV64I-NEXT: li a1, 31
103 ; RV64I-NEXT: sub a0, a1, a0
106 ; RV64XTHEADBB-LABEL: log2_i32:
107 ; RV64XTHEADBB: # %bb.0:
108 ; RV64XTHEADBB-NEXT: not a0, a0
109 ; RV64XTHEADBB-NEXT: slli a0, a0, 32
110 ; RV64XTHEADBB-NEXT: th.ff0 a0, a0
111 ; RV64XTHEADBB-NEXT: li a1, 31
112 ; RV64XTHEADBB-NEXT: sub a0, a1, a0
113 ; RV64XTHEADBB-NEXT: ret
114 %1 = call i32 @llvm.ctlz.i32(i32 %a, i1 false)
119 define signext i32 @log2_ceil_i32(i32 signext %a) nounwind {
120 ; RV64I-LABEL: log2_ceil_i32:
122 ; RV64I-NEXT: addiw a1, a0, -1
123 ; RV64I-NEXT: li a0, 32
124 ; RV64I-NEXT: li a2, 32
125 ; RV64I-NEXT: beqz a1, .LBB2_2
126 ; RV64I-NEXT: # %bb.1: # %cond.false
127 ; RV64I-NEXT: srliw a2, a1, 1
128 ; RV64I-NEXT: or a1, a1, a2
129 ; RV64I-NEXT: srliw a2, a1, 2
130 ; RV64I-NEXT: or a1, a1, a2
131 ; RV64I-NEXT: srliw a2, a1, 4
132 ; RV64I-NEXT: or a1, a1, a2
133 ; RV64I-NEXT: srliw a2, a1, 8
134 ; RV64I-NEXT: or a1, a1, a2
135 ; RV64I-NEXT: srliw a2, a1, 16
136 ; RV64I-NEXT: or a1, a1, a2
137 ; RV64I-NEXT: not a1, a1
138 ; RV64I-NEXT: srli a2, a1, 1
139 ; RV64I-NEXT: lui a3, 349525
140 ; RV64I-NEXT: addiw a3, a3, 1365
141 ; RV64I-NEXT: and a2, a2, a3
142 ; RV64I-NEXT: sub a1, a1, a2
143 ; RV64I-NEXT: lui a2, 209715
144 ; RV64I-NEXT: addiw a2, a2, 819
145 ; RV64I-NEXT: and a3, a1, a2
146 ; RV64I-NEXT: srli a1, a1, 2
147 ; RV64I-NEXT: and a1, a1, a2
148 ; RV64I-NEXT: add a1, a3, a1
149 ; RV64I-NEXT: srli a2, a1, 4
150 ; RV64I-NEXT: add a1, a1, a2
151 ; RV64I-NEXT: lui a2, 61681
152 ; RV64I-NEXT: addi a2, a2, -241
153 ; RV64I-NEXT: and a1, a1, a2
154 ; RV64I-NEXT: slli a2, a1, 8
155 ; RV64I-NEXT: add a1, a1, a2
156 ; RV64I-NEXT: slli a2, a1, 16
157 ; RV64I-NEXT: add a1, a1, a2
158 ; RV64I-NEXT: srliw a2, a1, 24
159 ; RV64I-NEXT: .LBB2_2: # %cond.end
160 ; RV64I-NEXT: sub a0, a0, a2
163 ; RV64XTHEADBB-LABEL: log2_ceil_i32:
164 ; RV64XTHEADBB: # %bb.0:
165 ; RV64XTHEADBB-NEXT: addi a0, a0, -1
166 ; RV64XTHEADBB-NEXT: not a0, a0
167 ; RV64XTHEADBB-NEXT: slli a0, a0, 32
168 ; RV64XTHEADBB-NEXT: th.ff0 a0, a0
169 ; RV64XTHEADBB-NEXT: li a1, 32
170 ; RV64XTHEADBB-NEXT: sub a0, a1, a0
171 ; RV64XTHEADBB-NEXT: ret
173 %2 = call i32 @llvm.ctlz.i32(i32 %1, i1 false)
178 define signext i32 @findLastSet_i32(i32 signext %a) nounwind {
179 ; RV64I-LABEL: findLastSet_i32:
181 ; RV64I-NEXT: srliw a1, a0, 1
182 ; RV64I-NEXT: or a1, a0, a1
183 ; RV64I-NEXT: srliw a2, a1, 2
184 ; RV64I-NEXT: or a1, a1, a2
185 ; RV64I-NEXT: srliw a2, a1, 4
186 ; RV64I-NEXT: or a1, a1, a2
187 ; RV64I-NEXT: srliw a2, a1, 8
188 ; RV64I-NEXT: or a1, a1, a2
189 ; RV64I-NEXT: srliw a2, a1, 16
190 ; RV64I-NEXT: or a1, a1, a2
191 ; RV64I-NEXT: not a1, a1
192 ; RV64I-NEXT: srli a2, a1, 1
193 ; RV64I-NEXT: lui a3, 349525
194 ; RV64I-NEXT: addiw a3, a3, 1365
195 ; RV64I-NEXT: and a2, a2, a3
196 ; RV64I-NEXT: sub a1, a1, a2
197 ; RV64I-NEXT: lui a2, 209715
198 ; RV64I-NEXT: addiw a2, a2, 819
199 ; RV64I-NEXT: and a3, a1, a2
200 ; RV64I-NEXT: srli a1, a1, 2
201 ; RV64I-NEXT: and a1, a1, a2
202 ; RV64I-NEXT: add a1, a3, a1
203 ; RV64I-NEXT: srli a2, a1, 4
204 ; RV64I-NEXT: add a1, a1, a2
205 ; RV64I-NEXT: lui a2, 61681
206 ; RV64I-NEXT: addi a2, a2, -241
207 ; RV64I-NEXT: and a1, a1, a2
208 ; RV64I-NEXT: slli a2, a1, 8
209 ; RV64I-NEXT: add a1, a1, a2
210 ; RV64I-NEXT: slli a2, a1, 16
211 ; RV64I-NEXT: add a1, a1, a2
212 ; RV64I-NEXT: srliw a1, a1, 24
213 ; RV64I-NEXT: xori a1, a1, 31
214 ; RV64I-NEXT: snez a0, a0
215 ; RV64I-NEXT: addi a0, a0, -1
216 ; RV64I-NEXT: or a0, a0, a1
219 ; RV64XTHEADBB-LABEL: findLastSet_i32:
220 ; RV64XTHEADBB: # %bb.0:
221 ; RV64XTHEADBB-NEXT: not a1, a0
222 ; RV64XTHEADBB-NEXT: slli a1, a1, 32
223 ; RV64XTHEADBB-NEXT: th.ff0 a1, a1
224 ; RV64XTHEADBB-NEXT: xori a1, a1, 31
225 ; RV64XTHEADBB-NEXT: snez a0, a0
226 ; RV64XTHEADBB-NEXT: addi a0, a0, -1
227 ; RV64XTHEADBB-NEXT: or a0, a0, a1
228 ; RV64XTHEADBB-NEXT: ret
229 %1 = call i32 @llvm.ctlz.i32(i32 %a, i1 true)
231 %3 = icmp eq i32 %a, 0
232 %4 = select i1 %3, i32 -1, i32 %2
236 define i32 @ctlz_lshr_i32(i32 signext %a) {
237 ; RV64I-LABEL: ctlz_lshr_i32:
239 ; RV64I-NEXT: srliw a0, a0, 1
240 ; RV64I-NEXT: beqz a0, .LBB4_2
241 ; RV64I-NEXT: # %bb.1: # %cond.false
242 ; RV64I-NEXT: srliw a1, a0, 1
243 ; RV64I-NEXT: or a0, a0, a1
244 ; RV64I-NEXT: srliw a1, a0, 2
245 ; RV64I-NEXT: or a0, a0, a1
246 ; RV64I-NEXT: srliw a1, a0, 4
247 ; RV64I-NEXT: or a0, a0, a1
248 ; RV64I-NEXT: srliw a1, a0, 8
249 ; RV64I-NEXT: or a0, a0, a1
250 ; RV64I-NEXT: srliw a1, a0, 16
251 ; RV64I-NEXT: or a0, a0, a1
252 ; RV64I-NEXT: not a0, a0
253 ; RV64I-NEXT: srli a1, a0, 1
254 ; RV64I-NEXT: lui a2, 349525
255 ; RV64I-NEXT: addiw a2, a2, 1365
256 ; RV64I-NEXT: and a1, a1, a2
257 ; RV64I-NEXT: sub a0, a0, a1
258 ; RV64I-NEXT: lui a1, 209715
259 ; RV64I-NEXT: addiw a1, a1, 819
260 ; RV64I-NEXT: and a2, a0, a1
261 ; RV64I-NEXT: srli a0, a0, 2
262 ; RV64I-NEXT: and a0, a0, a1
263 ; RV64I-NEXT: add a0, a2, a0
264 ; RV64I-NEXT: srli a1, a0, 4
265 ; RV64I-NEXT: add a0, a0, a1
266 ; RV64I-NEXT: lui a1, 61681
267 ; RV64I-NEXT: addi a1, a1, -241
268 ; RV64I-NEXT: and a0, a0, a1
269 ; RV64I-NEXT: slli a1, a0, 8
270 ; RV64I-NEXT: add a0, a0, a1
271 ; RV64I-NEXT: slli a1, a0, 16
272 ; RV64I-NEXT: add a0, a0, a1
273 ; RV64I-NEXT: srliw a0, a0, 24
275 ; RV64I-NEXT: .LBB4_2:
276 ; RV64I-NEXT: li a0, 32
279 ; RV64XTHEADBB-LABEL: ctlz_lshr_i32:
280 ; RV64XTHEADBB: # %bb.0:
281 ; RV64XTHEADBB-NEXT: srliw a0, a0, 1
282 ; RV64XTHEADBB-NEXT: not a0, a0
283 ; RV64XTHEADBB-NEXT: slli a0, a0, 32
284 ; RV64XTHEADBB-NEXT: th.ff0 a0, a0
285 ; RV64XTHEADBB-NEXT: ret
287 %2 = call i32 @llvm.ctlz.i32(i32 %1, i1 false)
291 declare i64 @llvm.ctlz.i64(i64, i1)
293 define i64 @ctlz_i64(i64 %a) nounwind {
294 ; RV64I-LABEL: ctlz_i64:
296 ; RV64I-NEXT: beqz a0, .LBB5_2
297 ; RV64I-NEXT: # %bb.1: # %cond.false
298 ; RV64I-NEXT: srli a1, a0, 1
299 ; RV64I-NEXT: or a0, a0, a1
300 ; RV64I-NEXT: srli a1, a0, 2
301 ; RV64I-NEXT: or a0, a0, a1
302 ; RV64I-NEXT: srli a1, a0, 4
303 ; RV64I-NEXT: or a0, a0, a1
304 ; RV64I-NEXT: srli a1, a0, 8
305 ; RV64I-NEXT: or a0, a0, a1
306 ; RV64I-NEXT: srli a1, a0, 16
307 ; RV64I-NEXT: or a0, a0, a1
308 ; RV64I-NEXT: srli a1, a0, 32
309 ; RV64I-NEXT: or a0, a0, a1
310 ; RV64I-NEXT: not a0, a0
311 ; RV64I-NEXT: srli a1, a0, 1
312 ; RV64I-NEXT: lui a2, 349525
313 ; RV64I-NEXT: addiw a2, a2, 1365
314 ; RV64I-NEXT: slli a3, a2, 32
315 ; RV64I-NEXT: add a2, a2, a3
316 ; RV64I-NEXT: and a1, a1, a2
317 ; RV64I-NEXT: sub a0, a0, a1
318 ; RV64I-NEXT: lui a1, 209715
319 ; RV64I-NEXT: addiw a1, a1, 819
320 ; RV64I-NEXT: slli a2, a1, 32
321 ; RV64I-NEXT: add a1, a1, a2
322 ; RV64I-NEXT: and a2, a0, a1
323 ; RV64I-NEXT: srli a0, a0, 2
324 ; RV64I-NEXT: and a0, a0, a1
325 ; RV64I-NEXT: add a0, a2, a0
326 ; RV64I-NEXT: srli a1, a0, 4
327 ; RV64I-NEXT: add a0, a0, a1
328 ; RV64I-NEXT: lui a1, 61681
329 ; RV64I-NEXT: addiw a1, a1, -241
330 ; RV64I-NEXT: slli a2, a1, 32
331 ; RV64I-NEXT: add a1, a1, a2
332 ; RV64I-NEXT: and a0, a0, a1
333 ; RV64I-NEXT: slli a1, a0, 8
334 ; RV64I-NEXT: add a0, a0, a1
335 ; RV64I-NEXT: slli a1, a0, 16
336 ; RV64I-NEXT: add a0, a0, a1
337 ; RV64I-NEXT: slli a1, a0, 32
338 ; RV64I-NEXT: add a0, a0, a1
339 ; RV64I-NEXT: srli a0, a0, 56
341 ; RV64I-NEXT: .LBB5_2:
342 ; RV64I-NEXT: li a0, 64
345 ; RV64XTHEADBB-LABEL: ctlz_i64:
346 ; RV64XTHEADBB: # %bb.0:
347 ; RV64XTHEADBB-NEXT: th.ff1 a0, a0
348 ; RV64XTHEADBB-NEXT: ret
349 %1 = call i64 @llvm.ctlz.i64(i64 %a, i1 false)
353 declare i32 @llvm.cttz.i32(i32, i1)
355 define signext i32 @cttz_i32(i32 signext %a) nounwind {
356 ; RV64I-LABEL: cttz_i32:
358 ; RV64I-NEXT: beqz a0, .LBB6_2
359 ; RV64I-NEXT: # %bb.1: # %cond.false
360 ; RV64I-NEXT: addi sp, sp, -16
361 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
362 ; RV64I-NEXT: neg a1, a0
363 ; RV64I-NEXT: and a0, a0, a1
364 ; RV64I-NEXT: lui a1, 30667
365 ; RV64I-NEXT: addiw a1, a1, 1329
366 ; RV64I-NEXT: call __muldi3
367 ; RV64I-NEXT: srliw a0, a0, 27
368 ; RV64I-NEXT: lui a1, %hi(.LCPI6_0)
369 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI6_0)
370 ; RV64I-NEXT: add a0, a1, a0
371 ; RV64I-NEXT: lbu a0, 0(a0)
372 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
373 ; RV64I-NEXT: addi sp, sp, 16
375 ; RV64I-NEXT: .LBB6_2:
376 ; RV64I-NEXT: li a0, 32
379 ; RV64XTHEADBB-LABEL: cttz_i32:
380 ; RV64XTHEADBB: # %bb.0:
381 ; RV64XTHEADBB-NEXT: beqz a0, .LBB6_2
382 ; RV64XTHEADBB-NEXT: # %bb.1: # %cond.false
383 ; RV64XTHEADBB-NEXT: addi a1, a0, -1
384 ; RV64XTHEADBB-NEXT: not a0, a0
385 ; RV64XTHEADBB-NEXT: and a0, a0, a1
386 ; RV64XTHEADBB-NEXT: th.ff1 a0, a0
387 ; RV64XTHEADBB-NEXT: li a1, 64
388 ; RV64XTHEADBB-NEXT: sub a0, a1, a0
389 ; RV64XTHEADBB-NEXT: ret
390 ; RV64XTHEADBB-NEXT: .LBB6_2:
391 ; RV64XTHEADBB-NEXT: li a0, 32
392 ; RV64XTHEADBB-NEXT: ret
393 %1 = call i32 @llvm.cttz.i32(i32 %a, i1 false)
397 define signext i32 @cttz_zero_undef_i32(i32 signext %a) nounwind {
398 ; RV64I-LABEL: cttz_zero_undef_i32:
400 ; RV64I-NEXT: addi sp, sp, -16
401 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
402 ; RV64I-NEXT: neg a1, a0
403 ; RV64I-NEXT: and a0, a0, a1
404 ; RV64I-NEXT: lui a1, 30667
405 ; RV64I-NEXT: addiw a1, a1, 1329
406 ; RV64I-NEXT: call __muldi3
407 ; RV64I-NEXT: srliw a0, a0, 27
408 ; RV64I-NEXT: lui a1, %hi(.LCPI7_0)
409 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI7_0)
410 ; RV64I-NEXT: add a0, a1, a0
411 ; RV64I-NEXT: lbu a0, 0(a0)
412 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
413 ; RV64I-NEXT: addi sp, sp, 16
416 ; RV64XTHEADBB-LABEL: cttz_zero_undef_i32:
417 ; RV64XTHEADBB: # %bb.0:
418 ; RV64XTHEADBB-NEXT: addi a1, a0, -1
419 ; RV64XTHEADBB-NEXT: not a0, a0
420 ; RV64XTHEADBB-NEXT: and a0, a0, a1
421 ; RV64XTHEADBB-NEXT: th.ff1 a0, a0
422 ; RV64XTHEADBB-NEXT: li a1, 64
423 ; RV64XTHEADBB-NEXT: sub a0, a1, a0
424 ; RV64XTHEADBB-NEXT: ret
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: neg 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 ; RV64XTHEADBB-LABEL: findFirstSet_i32:
455 ; RV64XTHEADBB: # %bb.0:
456 ; RV64XTHEADBB-NEXT: addi a1, a0, -1
457 ; RV64XTHEADBB-NEXT: not a2, a0
458 ; RV64XTHEADBB-NEXT: and a1, a2, a1
459 ; RV64XTHEADBB-NEXT: th.ff1 a1, a1
460 ; RV64XTHEADBB-NEXT: li a2, 64
461 ; RV64XTHEADBB-NEXT: sub a2, a2, a1
462 ; RV64XTHEADBB-NEXT: snez a0, a0
463 ; RV64XTHEADBB-NEXT: addi a0, a0, -1
464 ; RV64XTHEADBB-NEXT: or a0, a0, a2
465 ; RV64XTHEADBB-NEXT: ret
466 %1 = call i32 @llvm.cttz.i32(i32 %a, i1 true)
467 %2 = icmp eq i32 %a, 0
468 %3 = select i1 %2, i32 -1, i32 %1
472 define signext i32 @ffs_i32(i32 signext %a) nounwind {
473 ; RV64I-LABEL: ffs_i32:
475 ; RV64I-NEXT: addi sp, sp, -16
476 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
477 ; RV64I-NEXT: sd s0, 0(sp) # 8-byte Folded Spill
478 ; RV64I-NEXT: mv s0, a0
479 ; RV64I-NEXT: neg a0, a0
480 ; RV64I-NEXT: and a0, s0, a0
481 ; RV64I-NEXT: lui a1, 30667
482 ; RV64I-NEXT: addiw a1, a1, 1329
483 ; RV64I-NEXT: call __muldi3
484 ; RV64I-NEXT: srliw a0, a0, 27
485 ; RV64I-NEXT: lui a1, %hi(.LCPI9_0)
486 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI9_0)
487 ; RV64I-NEXT: add a0, a1, a0
488 ; RV64I-NEXT: lbu a0, 0(a0)
489 ; RV64I-NEXT: addi a0, a0, 1
490 ; RV64I-NEXT: seqz a1, s0
491 ; RV64I-NEXT: addi a1, a1, -1
492 ; RV64I-NEXT: and a0, a1, a0
493 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
494 ; RV64I-NEXT: ld s0, 0(sp) # 8-byte Folded Reload
495 ; RV64I-NEXT: addi sp, sp, 16
498 ; RV64XTHEADBB-LABEL: ffs_i32:
499 ; RV64XTHEADBB: # %bb.0:
500 ; RV64XTHEADBB-NEXT: addi a1, a0, -1
501 ; RV64XTHEADBB-NEXT: not a2, a0
502 ; RV64XTHEADBB-NEXT: and a1, a2, a1
503 ; RV64XTHEADBB-NEXT: th.ff1 a1, a1
504 ; RV64XTHEADBB-NEXT: li a2, 65
505 ; RV64XTHEADBB-NEXT: sub a2, a2, a1
506 ; RV64XTHEADBB-NEXT: seqz a0, a0
507 ; RV64XTHEADBB-NEXT: addi a0, a0, -1
508 ; RV64XTHEADBB-NEXT: and a0, a0, a2
509 ; RV64XTHEADBB-NEXT: ret
510 %1 = call i32 @llvm.cttz.i32(i32 %a, i1 true)
512 %3 = icmp eq i32 %a, 0
513 %4 = select i1 %3, i32 0, i32 %2
517 declare i64 @llvm.cttz.i64(i64, i1)
519 define i64 @cttz_i64(i64 %a) nounwind {
520 ; RV64I-LABEL: cttz_i64:
522 ; RV64I-NEXT: beqz a0, .LBB10_2
523 ; RV64I-NEXT: # %bb.1: # %cond.false
524 ; RV64I-NEXT: addi sp, sp, -16
525 ; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
526 ; RV64I-NEXT: neg a1, a0
527 ; RV64I-NEXT: and a0, a0, a1
528 ; RV64I-NEXT: lui a1, %hi(.LCPI10_0)
529 ; RV64I-NEXT: ld a1, %lo(.LCPI10_0)(a1)
530 ; RV64I-NEXT: call __muldi3
531 ; RV64I-NEXT: srli a0, a0, 58
532 ; RV64I-NEXT: lui a1, %hi(.LCPI10_1)
533 ; RV64I-NEXT: addi a1, a1, %lo(.LCPI10_1)
534 ; RV64I-NEXT: add a0, a1, a0
535 ; RV64I-NEXT: lbu a0, 0(a0)
536 ; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
537 ; RV64I-NEXT: addi sp, sp, 16
539 ; RV64I-NEXT: .LBB10_2:
540 ; RV64I-NEXT: li a0, 64
543 ; RV64XTHEADBB-LABEL: cttz_i64:
544 ; RV64XTHEADBB: # %bb.0:
545 ; RV64XTHEADBB-NEXT: beqz a0, .LBB10_2
546 ; RV64XTHEADBB-NEXT: # %bb.1: # %cond.false
547 ; RV64XTHEADBB-NEXT: addi a1, a0, -1
548 ; RV64XTHEADBB-NEXT: not a0, a0
549 ; RV64XTHEADBB-NEXT: and a0, a0, a1
550 ; RV64XTHEADBB-NEXT: th.ff1 a0, a0
551 ; RV64XTHEADBB-NEXT: li a1, 64
552 ; RV64XTHEADBB-NEXT: sub a0, a1, a0
553 ; RV64XTHEADBB-NEXT: ret
554 ; RV64XTHEADBB-NEXT: .LBB10_2:
555 ; RV64XTHEADBB-NEXT: li a0, 64
556 ; RV64XTHEADBB-NEXT: ret
557 %1 = call i64 @llvm.cttz.i64(i64 %a, i1 false)
561 define signext i32 @sextb_i32(i32 signext %a) nounwind {
562 ; RV64I-LABEL: sextb_i32:
564 ; RV64I-NEXT: slli a0, a0, 56
565 ; RV64I-NEXT: srai a0, a0, 56
568 ; RV64XTHEADBB-LABEL: sextb_i32:
569 ; RV64XTHEADBB: # %bb.0:
570 ; RV64XTHEADBB-NEXT: th.ext a0, a0, 7, 0
571 ; RV64XTHEADBB-NEXT: ret
572 %shl = shl i32 %a, 24
573 %shr = ashr exact i32 %shl, 24
577 define i64 @sextb_i64(i64 %a) nounwind {
578 ; RV64I-LABEL: sextb_i64:
580 ; RV64I-NEXT: slli a0, a0, 56
581 ; RV64I-NEXT: srai a0, a0, 56
584 ; RV64XTHEADBB-LABEL: sextb_i64:
585 ; RV64XTHEADBB: # %bb.0:
586 ; RV64XTHEADBB-NEXT: th.ext a0, a0, 7, 0
587 ; RV64XTHEADBB-NEXT: ret
588 %shl = shl i64 %a, 56
589 %shr = ashr exact i64 %shl, 56
593 define signext i32 @sexth_i32(i32 signext %a) nounwind {
594 ; RV64I-LABEL: sexth_i32:
596 ; RV64I-NEXT: slli a0, a0, 48
597 ; RV64I-NEXT: srai a0, a0, 48
600 ; RV64XTHEADBB-LABEL: sexth_i32:
601 ; RV64XTHEADBB: # %bb.0:
602 ; RV64XTHEADBB-NEXT: th.ext a0, a0, 15, 0
603 ; RV64XTHEADBB-NEXT: ret
604 %shl = shl i32 %a, 16
605 %shr = ashr exact i32 %shl, 16
609 define signext i32 @no_sexth_i32(i32 signext %a) nounwind {
610 ; RV64I-LABEL: no_sexth_i32:
612 ; RV64I-NEXT: slli a0, a0, 49
613 ; RV64I-NEXT: srai a0, a0, 48
616 ; RV64XTHEADBB-LABEL: no_sexth_i32:
617 ; RV64XTHEADBB: # %bb.0:
618 ; RV64XTHEADBB-NEXT: slli a0, a0, 49
619 ; RV64XTHEADBB-NEXT: srai a0, a0, 48
620 ; RV64XTHEADBB-NEXT: ret
621 %shl = shl i32 %a, 17
622 %shr = ashr exact i32 %shl, 16
626 define i64 @sexth_i64(i64 %a) nounwind {
627 ; RV64I-LABEL: sexth_i64:
629 ; RV64I-NEXT: slli a0, a0, 48
630 ; RV64I-NEXT: srai a0, a0, 48
633 ; RV64XTHEADBB-LABEL: sexth_i64:
634 ; RV64XTHEADBB: # %bb.0:
635 ; RV64XTHEADBB-NEXT: th.ext a0, a0, 15, 0
636 ; RV64XTHEADBB-NEXT: ret
637 %shl = shl i64 %a, 48
638 %shr = ashr exact i64 %shl, 48
642 define i64 @no_sexth_i64(i64 %a) nounwind {
643 ; RV64I-LABEL: no_sexth_i64:
645 ; RV64I-NEXT: slli a0, a0, 49
646 ; RV64I-NEXT: srai a0, a0, 48
649 ; RV64XTHEADBB-LABEL: no_sexth_i64:
650 ; RV64XTHEADBB: # %bb.0:
651 ; RV64XTHEADBB-NEXT: slli a0, a0, 49
652 ; RV64XTHEADBB-NEXT: srai a0, a0, 48
653 ; RV64XTHEADBB-NEXT: ret
654 %shl = shl i64 %a, 49
655 %shr = ashr exact i64 %shl, 48
659 define i32 @zexth_i32(i32 %a) nounwind {
660 ; RV64I-LABEL: zexth_i32:
662 ; RV64I-NEXT: slli a0, a0, 48
663 ; RV64I-NEXT: srli a0, a0, 48
666 ; RV64XTHEADBB-LABEL: zexth_i32:
667 ; RV64XTHEADBB: # %bb.0:
668 ; RV64XTHEADBB-NEXT: th.extu a0, a0, 15, 0
669 ; RV64XTHEADBB-NEXT: ret
670 %and = and i32 %a, 65535
674 define i64 @zexth_i64(i64 %a) nounwind {
675 ; RV64I-LABEL: zexth_i64:
677 ; RV64I-NEXT: slli a0, a0, 48
678 ; RV64I-NEXT: srli a0, a0, 48
681 ; RV64XTHEADBB-LABEL: zexth_i64:
682 ; RV64XTHEADBB: # %bb.0:
683 ; RV64XTHEADBB-NEXT: th.extu a0, a0, 15, 0
684 ; RV64XTHEADBB-NEXT: ret
685 %and = and i64 %a, 65535
689 define i64 @zext_bf_i64(i64 %a) nounwind {
690 ; RV64I-LABEL: zext_bf_i64:
692 ; RV64I-NEXT: slli a0, a0, 47
693 ; RV64I-NEXT: srli a0, a0, 48
696 ; RV64XTHEADBB-LABEL: zext_bf_i64:
697 ; RV64XTHEADBB: # %bb.0:
698 ; RV64XTHEADBB-NEXT: th.extu a0, a0, 16, 1
699 ; RV64XTHEADBB-NEXT: ret
701 %and = and i64 %1, 65535
705 define i64 @zext_i64_srliw(i64 %a) nounwind {
706 ; RV64I-LABEL: zext_i64_srliw:
708 ; RV64I-NEXT: srliw a0, a0, 16
711 ; RV64XTHEADBB-LABEL: zext_i64_srliw:
712 ; RV64XTHEADBB: # %bb.0:
713 ; RV64XTHEADBB-NEXT: srliw a0, a0, 16
714 ; RV64XTHEADBB-NEXT: ret
716 %and = and i64 %1, 65535
720 declare i32 @llvm.bswap.i32(i32)
722 define signext i32 @bswap_i32(i32 signext %a) nounwind {
723 ; RV64I-LABEL: bswap_i32:
725 ; RV64I-NEXT: srli a1, a0, 8
726 ; RV64I-NEXT: lui a2, 16
727 ; RV64I-NEXT: addiw a2, a2, -256
728 ; RV64I-NEXT: and a1, a1, a2
729 ; RV64I-NEXT: srliw a3, a0, 24
730 ; RV64I-NEXT: or a1, a1, a3
731 ; RV64I-NEXT: and a2, a0, a2
732 ; RV64I-NEXT: slli a2, a2, 8
733 ; RV64I-NEXT: slliw a0, a0, 24
734 ; RV64I-NEXT: or a0, a0, a2
735 ; RV64I-NEXT: or a0, a0, a1
738 ; RV64XTHEADBB-LABEL: bswap_i32:
739 ; RV64XTHEADBB: # %bb.0:
740 ; RV64XTHEADBB-NEXT: th.revw a0, a0
741 ; RV64XTHEADBB-NEXT: ret
742 %1 = tail call i32 @llvm.bswap.i32(i32 %a)
746 ; Similar to bswap_i32 but the result is not sign extended.
747 define void @bswap_i32_nosext(i32 signext %a, ptr %x) nounwind {
748 ; RV64I-LABEL: bswap_i32_nosext:
750 ; RV64I-NEXT: srli a2, a0, 8
751 ; RV64I-NEXT: lui a3, 16
752 ; RV64I-NEXT: addi a3, a3, -256
753 ; RV64I-NEXT: and a2, a2, a3
754 ; RV64I-NEXT: srliw a4, a0, 24
755 ; RV64I-NEXT: or a2, a2, a4
756 ; RV64I-NEXT: and a3, a0, a3
757 ; RV64I-NEXT: slli a3, a3, 8
758 ; RV64I-NEXT: slli a0, a0, 24
759 ; RV64I-NEXT: or a0, a0, a3
760 ; RV64I-NEXT: or a0, a0, a2
761 ; RV64I-NEXT: sw a0, 0(a1)
764 ; RV64XTHEADBB-LABEL: bswap_i32_nosext:
765 ; RV64XTHEADBB: # %bb.0:
766 ; RV64XTHEADBB-NEXT: th.revw a0, a0
767 ; RV64XTHEADBB-NEXT: sw a0, 0(a1)
768 ; RV64XTHEADBB-NEXT: ret
769 %1 = tail call i32 @llvm.bswap.i32(i32 %a)
774 declare i64 @llvm.bswap.i64(i64)
776 define i64 @bswap_i64(i64 %a) {
777 ; RV64I-LABEL: bswap_i64:
779 ; RV64I-NEXT: srli a1, a0, 40
780 ; RV64I-NEXT: lui a2, 16
781 ; RV64I-NEXT: addiw a2, a2, -256
782 ; RV64I-NEXT: and a1, a1, a2
783 ; RV64I-NEXT: srli a3, a0, 56
784 ; RV64I-NEXT: or a1, a1, a3
785 ; RV64I-NEXT: srli a3, a0, 24
786 ; RV64I-NEXT: lui a4, 4080
787 ; RV64I-NEXT: and a3, a3, a4
788 ; RV64I-NEXT: srli a5, a0, 8
789 ; RV64I-NEXT: srliw a5, a5, 24
790 ; RV64I-NEXT: slli a5, a5, 24
791 ; RV64I-NEXT: or a3, a5, a3
792 ; RV64I-NEXT: or a1, a3, a1
793 ; RV64I-NEXT: and a4, a0, a4
794 ; RV64I-NEXT: slli a4, a4, 24
795 ; RV64I-NEXT: srliw a3, a0, 24
796 ; RV64I-NEXT: slli a3, a3, 32
797 ; RV64I-NEXT: or a3, a4, a3
798 ; RV64I-NEXT: and a2, a0, a2
799 ; RV64I-NEXT: slli a2, a2, 40
800 ; RV64I-NEXT: slli a0, a0, 56
801 ; RV64I-NEXT: or a0, a0, a2
802 ; RV64I-NEXT: or a0, a0, a3
803 ; RV64I-NEXT: or a0, a0, a1
806 ; RV64XTHEADBB-LABEL: bswap_i64:
807 ; RV64XTHEADBB: # %bb.0:
808 ; RV64XTHEADBB-NEXT: th.rev a0, a0
809 ; RV64XTHEADBB-NEXT: ret
810 %1 = call i64 @llvm.bswap.i64(i64 %a)