1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv32 -target-abi=ilp32 -verify-machineinstrs < %s \
3 ; RUN: | FileCheck -check-prefixes=RV32,RV32I %s
4 ; RUN: llc -mtriple=riscv32 -mattr=+f -target-abi=ilp32 -verify-machineinstrs < %s \
5 ; RUN: | FileCheck -check-prefixes=RV32,RV32IF %s
6 ; RUN: llc -mtriple=riscv64 -target-abi=lp64 -verify-machineinstrs < %s \
7 ; RUN: | FileCheck -check-prefixes=RV64,RV64I %s
8 ; RUN: llc -mtriple=riscv64 -mattr=+f,+d -target-abi=lp64 -verify-machineinstrs < %s \
9 ; RUN: | FileCheck -check-prefixes=RV64,RV64IFD %s
11 ;; This tests how good we are at materialising constants using `select`. The aim
12 ;; is that we do so without a branch if possible (at the moment our lowering of
13 ;; select always introduces a branch).
15 ;; Currently the hook `convertSelectOfConstantsToMath` only is useful when the
16 ;; constants are either 1 away from each other, or one is a power of two and
19 define signext i32 @select_const_int_easy(i1 zeroext %a) nounwind {
20 ; RV32-LABEL: select_const_int_easy:
24 ; RV64-LABEL: select_const_int_easy:
27 %1 = select i1 %a, i32 1, i32 0
31 define signext i32 @select_const_int_one_away(i1 zeroext %a) nounwind {
32 ; RV32-LABEL: select_const_int_one_away:
35 ; RV32-NEXT: sub a0, a1, a0
38 ; RV64-LABEL: select_const_int_one_away:
41 ; RV64-NEXT: sub a0, a1, a0
43 %1 = select i1 %a, i32 3, i32 4
47 define signext i32 @select_const_int_pow2_zero(i1 zeroext %a) nounwind {
48 ; RV32-LABEL: select_const_int_pow2_zero:
50 ; RV32-NEXT: slli a0, a0, 2
53 ; RV64-LABEL: select_const_int_pow2_zero:
55 ; RV64-NEXT: slli a0, a0, 2
57 %1 = select i1 %a, i32 4, i32 0
61 define signext i32 @select_const_int_harder(i1 zeroext %a) nounwind {
62 ; RV32-LABEL: select_const_int_harder:
64 ; RV32-NEXT: mv a1, a0
66 ; RV32-NEXT: bnez a1, .LBB3_2
68 ; RV32-NEXT: li a0, 38
72 ; RV64-LABEL: select_const_int_harder:
74 ; RV64-NEXT: mv a1, a0
76 ; RV64-NEXT: bnez a1, .LBB3_2
78 ; RV64-NEXT: li a0, 38
81 %1 = select i1 %a, i32 6, i32 38
85 define float @select_const_fp(i1 zeroext %a) nounwind {
86 ; RV32I-LABEL: select_const_fp:
88 ; RV32I-NEXT: mv a1, a0
89 ; RV32I-NEXT: lui a0, 263168
90 ; RV32I-NEXT: bnez a1, .LBB4_2
91 ; RV32I-NEXT: # %bb.1:
92 ; RV32I-NEXT: lui a0, 264192
93 ; RV32I-NEXT: .LBB4_2:
96 ; RV32IF-LABEL: select_const_fp:
98 ; RV32IF-NEXT: bnez a0, .LBB4_2
99 ; RV32IF-NEXT: # %bb.1:
100 ; RV32IF-NEXT: lui a0, 264192
101 ; RV32IF-NEXT: j .LBB4_3
102 ; RV32IF-NEXT: .LBB4_2:
103 ; RV32IF-NEXT: lui a0, 263168
104 ; RV32IF-NEXT: .LBB4_3:
105 ; RV32IF-NEXT: fmv.w.x fa5, a0
106 ; RV32IF-NEXT: fmv.x.w a0, fa5
109 ; RV64I-LABEL: select_const_fp:
111 ; RV64I-NEXT: mv a1, a0
112 ; RV64I-NEXT: lui a0, 263168
113 ; RV64I-NEXT: bnez a1, .LBB4_2
114 ; RV64I-NEXT: # %bb.1:
115 ; RV64I-NEXT: lui a0, 264192
116 ; RV64I-NEXT: .LBB4_2:
119 ; RV64IFD-LABEL: select_const_fp:
121 ; RV64IFD-NEXT: bnez a0, .LBB4_2
122 ; RV64IFD-NEXT: # %bb.1:
123 ; RV64IFD-NEXT: lui a0, 264192
124 ; RV64IFD-NEXT: j .LBB4_3
125 ; RV64IFD-NEXT: .LBB4_2:
126 ; RV64IFD-NEXT: lui a0, 263168
127 ; RV64IFD-NEXT: .LBB4_3:
128 ; RV64IFD-NEXT: fmv.w.x fa5, a0
129 ; RV64IFD-NEXT: fmv.x.w a0, fa5
131 %1 = select i1 %a, float 3.0, float 4.0
135 define signext i32 @select_eq_zero_negone(i32 signext %a, i32 signext %b) nounwind {
136 ; RV32-LABEL: select_eq_zero_negone:
138 ; RV32-NEXT: xor a0, a0, a1
139 ; RV32-NEXT: snez a0, a0
140 ; RV32-NEXT: addi a0, a0, -1
143 ; RV64-LABEL: select_eq_zero_negone:
145 ; RV64-NEXT: xor a0, a0, a1
146 ; RV64-NEXT: snez a0, a0
147 ; RV64-NEXT: addi a0, a0, -1
149 %1 = icmp eq i32 %a, %b
150 %2 = select i1 %1, i32 -1, i32 0
154 define signext i32 @select_ne_zero_negone(i32 signext %a, i32 signext %b) nounwind {
155 ; RV32-LABEL: select_ne_zero_negone:
157 ; RV32-NEXT: xor a0, a0, a1
158 ; RV32-NEXT: seqz a0, a0
159 ; RV32-NEXT: addi a0, a0, -1
162 ; RV64-LABEL: select_ne_zero_negone:
164 ; RV64-NEXT: xor a0, a0, a1
165 ; RV64-NEXT: seqz a0, a0
166 ; RV64-NEXT: addi a0, a0, -1
168 %1 = icmp ne i32 %a, %b
169 %2 = select i1 %1, i32 -1, i32 0
173 define signext i32 @select_sgt_zero_negone(i32 signext %a, i32 signext %b) nounwind {
174 ; RV32-LABEL: select_sgt_zero_negone:
176 ; RV32-NEXT: slt a0, a1, a0
177 ; RV32-NEXT: neg a0, a0
180 ; RV64-LABEL: select_sgt_zero_negone:
182 ; RV64-NEXT: slt a0, a1, a0
183 ; RV64-NEXT: neg a0, a0
185 %1 = icmp sgt i32 %a, %b
186 %2 = select i1 %1, i32 -1, i32 0
190 define signext i32 @select_slt_zero_negone(i32 signext %a, i32 signext %b) nounwind {
191 ; RV32-LABEL: select_slt_zero_negone:
193 ; RV32-NEXT: slt a0, a0, a1
194 ; RV32-NEXT: neg a0, a0
197 ; RV64-LABEL: select_slt_zero_negone:
199 ; RV64-NEXT: slt a0, a0, a1
200 ; RV64-NEXT: neg a0, a0
202 %1 = icmp slt i32 %a, %b
203 %2 = select i1 %1, i32 -1, i32 0
207 define signext i32 @select_sge_zero_negone(i32 signext %a, i32 signext %b) nounwind {
208 ; RV32-LABEL: select_sge_zero_negone:
210 ; RV32-NEXT: slt a0, a0, a1
211 ; RV32-NEXT: addi a0, a0, -1
214 ; RV64-LABEL: select_sge_zero_negone:
216 ; RV64-NEXT: slt a0, a0, a1
217 ; RV64-NEXT: addi a0, a0, -1
219 %1 = icmp sge i32 %a, %b
220 %2 = select i1 %1, i32 -1, i32 0
224 define signext i32 @select_sle_zero_negone(i32 signext %a, i32 signext %b) nounwind {
225 ; RV32-LABEL: select_sle_zero_negone:
227 ; RV32-NEXT: slt a0, a1, a0
228 ; RV32-NEXT: addi a0, a0, -1
231 ; RV64-LABEL: select_sle_zero_negone:
233 ; RV64-NEXT: slt a0, a1, a0
234 ; RV64-NEXT: addi a0, a0, -1
236 %1 = icmp sle i32 %a, %b
237 %2 = select i1 %1, i32 -1, i32 0
241 define signext i32 @select_ugt_zero_negone(i32 signext %a, i32 signext %b) nounwind {
242 ; RV32-LABEL: select_ugt_zero_negone:
244 ; RV32-NEXT: sltu a0, a1, a0
245 ; RV32-NEXT: neg a0, a0
248 ; RV64-LABEL: select_ugt_zero_negone:
250 ; RV64-NEXT: sltu a0, a1, a0
251 ; RV64-NEXT: neg a0, a0
253 %1 = icmp ugt i32 %a, %b
254 %2 = select i1 %1, i32 -1, i32 0
258 define signext i32 @select_ult_zero_negone(i32 signext %a, i32 signext %b) nounwind {
259 ; RV32-LABEL: select_ult_zero_negone:
261 ; RV32-NEXT: sltu a0, a0, a1
262 ; RV32-NEXT: neg a0, a0
265 ; RV64-LABEL: select_ult_zero_negone:
267 ; RV64-NEXT: sltu a0, a0, a1
268 ; RV64-NEXT: neg a0, a0
270 %1 = icmp ult i32 %a, %b
271 %2 = select i1 %1, i32 -1, i32 0
275 define signext i32 @select_uge_zero_negone(i32 signext %a, i32 signext %b) nounwind {
276 ; RV32-LABEL: select_uge_zero_negone:
278 ; RV32-NEXT: sltu a0, a0, a1
279 ; RV32-NEXT: addi a0, a0, -1
282 ; RV64-LABEL: select_uge_zero_negone:
284 ; RV64-NEXT: sltu a0, a0, a1
285 ; RV64-NEXT: addi a0, a0, -1
287 %1 = icmp uge i32 %a, %b
288 %2 = select i1 %1, i32 -1, i32 0
292 define signext i32 @select_ule_zero_negone(i32 signext %a, i32 signext %b) nounwind {
293 ; RV32-LABEL: select_ule_zero_negone:
295 ; RV32-NEXT: sltu a0, a1, a0
296 ; RV32-NEXT: addi a0, a0, -1
299 ; RV64-LABEL: select_ule_zero_negone:
301 ; RV64-NEXT: sltu a0, a1, a0
302 ; RV64-NEXT: addi a0, a0, -1
304 %1 = icmp ule i32 %a, %b
305 %2 = select i1 %1, i32 -1, i32 0
309 define i32 @select_eq_1_2(i32 signext %a, i32 signext %b) {
310 ; RV32-LABEL: select_eq_1_2:
312 ; RV32-NEXT: xor a0, a0, a1
313 ; RV32-NEXT: snez a0, a0
314 ; RV32-NEXT: addi a0, a0, 1
317 ; RV64-LABEL: select_eq_1_2:
319 ; RV64-NEXT: xor a0, a0, a1
320 ; RV64-NEXT: snez a0, a0
321 ; RV64-NEXT: addi a0, a0, 1
323 %1 = icmp eq i32 %a, %b
324 %2 = select i1 %1, i32 1, i32 2
328 define i32 @select_ne_1_2(i32 signext %a, i32 signext %b) {
329 ; RV32-LABEL: select_ne_1_2:
331 ; RV32-NEXT: xor a0, a0, a1
332 ; RV32-NEXT: seqz a0, a0
333 ; RV32-NEXT: addi a0, a0, 1
336 ; RV64-LABEL: select_ne_1_2:
338 ; RV64-NEXT: xor a0, a0, a1
339 ; RV64-NEXT: seqz a0, a0
340 ; RV64-NEXT: addi a0, a0, 1
342 %1 = icmp ne i32 %a, %b
343 %2 = select i1 %1, i32 1, i32 2
347 define i32 @select_eq_10000_10001(i32 signext %a, i32 signext %b) {
348 ; RV32-LABEL: select_eq_10000_10001:
350 ; RV32-NEXT: xor a0, a0, a1
351 ; RV32-NEXT: lui a1, 2
352 ; RV32-NEXT: seqz a0, a0
353 ; RV32-NEXT: addi a1, a1, 1810
354 ; RV32-NEXT: sub a0, a1, a0
357 ; RV64-LABEL: select_eq_10000_10001:
359 ; RV64-NEXT: xor a0, a0, a1
360 ; RV64-NEXT: lui a1, 2
361 ; RV64-NEXT: seqz a0, a0
362 ; RV64-NEXT: addiw a1, a1, 1810
363 ; RV64-NEXT: sub a0, a1, a0
365 %1 = icmp eq i32 %a, %b
366 %2 = select i1 %1, i32 10001, i32 10002
370 define i32 @select_ne_10001_10002(i32 signext %a, i32 signext %b) {
371 ; RV32-LABEL: select_ne_10001_10002:
373 ; RV32-NEXT: xor a0, a0, a1
374 ; RV32-NEXT: lui a1, 2
375 ; RV32-NEXT: snez a0, a0
376 ; RV32-NEXT: addi a1, a1, 1810
377 ; RV32-NEXT: sub a0, a1, a0
380 ; RV64-LABEL: select_ne_10001_10002:
382 ; RV64-NEXT: xor a0, a0, a1
383 ; RV64-NEXT: lui a1, 2
384 ; RV64-NEXT: snez a0, a0
385 ; RV64-NEXT: addiw a1, a1, 1810
386 ; RV64-NEXT: sub a0, a1, a0
388 %1 = icmp ne i32 %a, %b
389 %2 = select i1 %1, i32 10001, i32 10002
393 define i32 @select_slt_zero_constant1_constant2(i32 signext %x) {
394 ; RV32-LABEL: select_slt_zero_constant1_constant2:
396 ; RV32-NEXT: srai a0, a0, 31
397 ; RV32-NEXT: andi a0, a0, 10
398 ; RV32-NEXT: addi a0, a0, -3
401 ; RV64-LABEL: select_slt_zero_constant1_constant2:
403 ; RV64-NEXT: srai a0, a0, 63
404 ; RV64-NEXT: andi a0, a0, 10
405 ; RV64-NEXT: addi a0, a0, -3
407 %cmp = icmp slt i32 %x, 0
408 %cond = select i1 %cmp, i32 7, i32 -3
412 define i32 @select_sgt_negative_one_constant1_constant2(i32 signext %x) {
413 ; RV32-LABEL: select_sgt_negative_one_constant1_constant2:
415 ; RV32-NEXT: srai a0, a0, 31
416 ; RV32-NEXT: andi a0, a0, -10
417 ; RV32-NEXT: addi a0, a0, 7
420 ; RV64-LABEL: select_sgt_negative_one_constant1_constant2:
422 ; RV64-NEXT: srai a0, a0, 63
423 ; RV64-NEXT: andi a0, a0, -10
424 ; RV64-NEXT: addi a0, a0, 7
426 %cmp = icmp sgt i32 %x, -1
427 %cond = select i1 %cmp, i32 7, i32 -3