1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc < %s -mtriple=aarch64-- | FileCheck %s
4 ; Select of constants: control flow / conditional moves can always be replaced by logic+math (but may not be worth it?).
5 ; Test the zeroext/signext variants of each pattern to see if that makes a difference.
7 ; select Cond, 0, 1 --> zext (!Cond)
9 define i32 @select_0_or_1(i1 %cond) {
10 ; CHECK-LABEL: select_0_or_1:
12 ; CHECK-NEXT: mvn w8, w0
13 ; CHECK-NEXT: and w0, w8, #0x1
15 %sel = select i1 %cond, i32 0, i32 1
19 define i32 @select_0_or_1_zeroext(i1 zeroext %cond) {
20 ; CHECK-LABEL: select_0_or_1_zeroext:
22 ; CHECK-NEXT: eor w0, w0, #0x1
24 %sel = select i1 %cond, i32 0, i32 1
28 define i32 @select_0_or_1_signext(i1 signext %cond) {
29 ; CHECK-LABEL: select_0_or_1_signext:
31 ; CHECK-NEXT: mvn w8, w0
32 ; CHECK-NEXT: and w0, w8, #0x1
34 %sel = select i1 %cond, i32 0, i32 1
38 ; select Cond, 1, 0 --> zext (Cond)
40 define i32 @select_1_or_0(i1 %cond) {
41 ; CHECK-LABEL: select_1_or_0:
43 ; CHECK-NEXT: and w0, w0, #0x1
45 %sel = select i1 %cond, i32 1, i32 0
49 define i32 @select_1_or_0_zeroext(i1 zeroext %cond) {
50 ; CHECK-LABEL: select_1_or_0_zeroext:
53 %sel = select i1 %cond, i32 1, i32 0
57 define i32 @select_1_or_0_signext(i1 signext %cond) {
58 ; CHECK-LABEL: select_1_or_0_signext:
60 ; CHECK-NEXT: and w0, w0, #0x1
62 %sel = select i1 %cond, i32 1, i32 0
66 ; select Cond, 0, -1 --> sext (!Cond)
68 define i32 @select_0_or_neg1(i1 %cond) {
69 ; CHECK-LABEL: select_0_or_neg1:
71 ; CHECK-NEXT: mvn w8, w0
72 ; CHECK-NEXT: sbfx w0, w8, #0, #1
74 %sel = select i1 %cond, i32 0, i32 -1
78 define i32 @select_0_or_neg1_zeroext(i1 zeroext %cond) {
79 ; CHECK-LABEL: select_0_or_neg1_zeroext:
81 ; CHECK-NEXT: mvn w8, w0
82 ; CHECK-NEXT: sbfx w0, w8, #0, #1
84 %sel = select i1 %cond, i32 0, i32 -1
88 define i32 @select_0_or_neg1_signext(i1 signext %cond) {
89 ; CHECK-LABEL: select_0_or_neg1_signext:
91 ; CHECK-NEXT: mvn w0, w0
93 %sel = select i1 %cond, i32 0, i32 -1
97 ; select Cond, -1, 0 --> sext (Cond)
99 define i32 @select_neg1_or_0(i1 %cond) {
100 ; CHECK-LABEL: select_neg1_or_0:
102 ; CHECK-NEXT: sbfx w0, w0, #0, #1
104 %sel = select i1 %cond, i32 -1, i32 0
108 define i32 @select_neg1_or_0_zeroext(i1 zeroext %cond) {
109 ; CHECK-LABEL: select_neg1_or_0_zeroext:
111 ; CHECK-NEXT: sbfx w0, w0, #0, #1
113 %sel = select i1 %cond, i32 -1, i32 0
117 define i32 @select_neg1_or_0_signext(i1 signext %cond) {
118 ; CHECK-LABEL: select_neg1_or_0_signext:
121 %sel = select i1 %cond, i32 -1, i32 0
125 ; select Cond, C+1, C --> add (zext Cond), C
127 define i32 @select_Cplus1_C(i1 %cond) {
128 ; CHECK-LABEL: select_Cplus1_C:
130 ; CHECK-NEXT: tst w0, #0x1
131 ; CHECK-NEXT: mov w8, #41
132 ; CHECK-NEXT: cinc w0, w8, ne
134 %sel = select i1 %cond, i32 42, i32 41
138 define i32 @select_Cplus1_C_zeroext(i1 zeroext %cond) {
139 ; CHECK-LABEL: select_Cplus1_C_zeroext:
141 ; CHECK-NEXT: cmp w0, #0 // =0
142 ; CHECK-NEXT: mov w8, #41
143 ; CHECK-NEXT: cinc w0, w8, ne
145 %sel = select i1 %cond, i32 42, i32 41
149 define i32 @select_Cplus1_C_signext(i1 signext %cond) {
150 ; CHECK-LABEL: select_Cplus1_C_signext:
152 ; CHECK-NEXT: tst w0, #0x1
153 ; CHECK-NEXT: mov w8, #41
154 ; CHECK-NEXT: cinc w0, w8, ne
156 %sel = select i1 %cond, i32 42, i32 41
160 ; select Cond, C, C+1 --> add (sext Cond), C
162 define i32 @select_C_Cplus1(i1 %cond) {
163 ; CHECK-LABEL: select_C_Cplus1:
165 ; CHECK-NEXT: tst w0, #0x1
166 ; CHECK-NEXT: mov w8, #41
167 ; CHECK-NEXT: cinc w0, w8, eq
169 %sel = select i1 %cond, i32 41, i32 42
173 define i32 @select_C_Cplus1_zeroext(i1 zeroext %cond) {
174 ; CHECK-LABEL: select_C_Cplus1_zeroext:
176 ; CHECK-NEXT: cmp w0, #0 // =0
177 ; CHECK-NEXT: mov w8, #41
178 ; CHECK-NEXT: cinc w0, w8, eq
180 %sel = select i1 %cond, i32 41, i32 42
184 define i32 @select_C_Cplus1_signext(i1 signext %cond) {
185 ; CHECK-LABEL: select_C_Cplus1_signext:
187 ; CHECK-NEXT: tst w0, #0x1
188 ; CHECK-NEXT: mov w8, #41
189 ; CHECK-NEXT: cinc w0, w8, eq
191 %sel = select i1 %cond, i32 41, i32 42
195 ; In general, select of 2 constants could be:
196 ; select Cond, C1, C2 --> add (mul (zext Cond), C1-C2), C2 --> add (and (sext Cond), C1-C2), C2
198 define i32 @select_C1_C2(i1 %cond) {
199 ; CHECK-LABEL: select_C1_C2:
201 ; CHECK-NEXT: tst w0, #0x1
202 ; CHECK-NEXT: mov w8, #42
203 ; CHECK-NEXT: mov w9, #421
204 ; CHECK-NEXT: csel w0, w9, w8, ne
206 %sel = select i1 %cond, i32 421, i32 42
210 define i32 @select_C1_C2_zeroext(i1 zeroext %cond) {
211 ; CHECK-LABEL: select_C1_C2_zeroext:
213 ; CHECK-NEXT: cmp w0, #0 // =0
214 ; CHECK-NEXT: mov w8, #42
215 ; CHECK-NEXT: mov w9, #421
216 ; CHECK-NEXT: csel w0, w9, w8, ne
218 %sel = select i1 %cond, i32 421, i32 42
222 define i32 @select_C1_C2_signext(i1 signext %cond) {
223 ; CHECK-LABEL: select_C1_C2_signext:
225 ; CHECK-NEXT: tst w0, #0x1
226 ; CHECK-NEXT: mov w8, #42
227 ; CHECK-NEXT: mov w9, #421
228 ; CHECK-NEXT: csel w0, w9, w8, ne
230 %sel = select i1 %cond, i32 421, i32 42
234 ; A binary operator with constant after the select should always get folded into the select.
236 define i8 @sel_constants_add_constant(i1 %cond) {
237 ; CHECK-LABEL: sel_constants_add_constant:
239 ; CHECK-NEXT: tst w0, #0x1
240 ; CHECK-NEXT: mov w8, #28
241 ; CHECK-NEXT: csinc w0, w8, wzr, eq
243 %sel = select i1 %cond, i8 -4, i8 23
248 define i8 @sel_constants_sub_constant(i1 %cond) {
249 ; CHECK-LABEL: sel_constants_sub_constant:
251 ; CHECK-NEXT: tst w0, #0x1
252 ; CHECK-NEXT: mov w8, #18
253 ; CHECK-NEXT: mov w9, #-9
254 ; CHECK-NEXT: csel w0, w9, w8, ne
256 %sel = select i1 %cond, i8 -4, i8 23
261 define i8 @sel_constants_sub_constant_sel_constants(i1 %cond) {
262 ; CHECK-LABEL: sel_constants_sub_constant_sel_constants:
264 ; CHECK-NEXT: tst w0, #0x1
265 ; CHECK-NEXT: mov w8, #2
266 ; CHECK-NEXT: mov w9, #9
267 ; CHECK-NEXT: csel w0, w9, w8, ne
269 %sel = select i1 %cond, i8 -4, i8 3
274 define i8 @sel_constants_mul_constant(i1 %cond) {
275 ; CHECK-LABEL: sel_constants_mul_constant:
277 ; CHECK-NEXT: tst w0, #0x1
278 ; CHECK-NEXT: mov w8, #115
279 ; CHECK-NEXT: mov w9, #-20
280 ; CHECK-NEXT: csel w0, w9, w8, ne
282 %sel = select i1 %cond, i8 -4, i8 23
287 define i8 @sel_constants_sdiv_constant(i1 %cond) {
288 ; CHECK-LABEL: sel_constants_sdiv_constant:
290 ; CHECK-NEXT: tst w0, #0x1
291 ; CHECK-NEXT: mov w8, #4
292 ; CHECK-NEXT: csel w0, wzr, w8, ne
294 %sel = select i1 %cond, i8 -4, i8 23
295 %bo = sdiv i8 %sel, 5
299 define i8 @sdiv_constant_sel_constants(i1 %cond) {
300 ; CHECK-LABEL: sdiv_constant_sel_constants:
302 ; CHECK-NEXT: tst w0, #0x1
303 ; CHECK-NEXT: mov w8, #5
304 ; CHECK-NEXT: csel w0, wzr, w8, ne
306 %sel = select i1 %cond, i8 121, i8 23
307 %bo = sdiv i8 120, %sel
311 define i8 @sel_constants_udiv_constant(i1 %cond) {
312 ; CHECK-LABEL: sel_constants_udiv_constant:
314 ; CHECK-NEXT: tst w0, #0x1
315 ; CHECK-NEXT: mov w8, #4
316 ; CHECK-NEXT: mov w9, #50
317 ; CHECK-NEXT: csel w0, w9, w8, ne
319 %sel = select i1 %cond, i8 -4, i8 23
320 %bo = udiv i8 %sel, 5
324 define i8 @udiv_constant_sel_constants(i1 %cond) {
325 ; CHECK-LABEL: udiv_constant_sel_constants:
327 ; CHECK-NEXT: tst w0, #0x1
328 ; CHECK-NEXT: mov w8, #5
329 ; CHECK-NEXT: csel w0, wzr, w8, ne
331 %sel = select i1 %cond, i8 -4, i8 23
332 %bo = udiv i8 120, %sel
336 define i8 @sel_constants_srem_constant(i1 %cond) {
337 ; CHECK-LABEL: sel_constants_srem_constant:
339 ; CHECK-NEXT: tst w0, #0x1
340 ; CHECK-NEXT: mov w8, #-4
341 ; CHECK-NEXT: cinv w0, w8, eq
343 %sel = select i1 %cond, i8 -4, i8 23
344 %bo = srem i8 %sel, 5
348 define i8 @srem_constant_sel_constants(i1 %cond) {
349 ; CHECK-LABEL: srem_constant_sel_constants:
351 ; CHECK-NEXT: tst w0, #0x1
352 ; CHECK-NEXT: mov w8, #5
353 ; CHECK-NEXT: mov w9, #120
354 ; CHECK-NEXT: csel w0, w9, w8, ne
356 %sel = select i1 %cond, i8 121, i8 23
357 %bo = srem i8 120, %sel
361 define i8 @sel_constants_urem_constant(i1 %cond) {
362 ; CHECK-LABEL: sel_constants_urem_constant:
364 ; CHECK-NEXT: tst w0, #0x1
365 ; CHECK-NEXT: mov w8, #2
366 ; CHECK-NEXT: cinc w0, w8, eq
368 %sel = select i1 %cond, i8 -4, i8 23
369 %bo = urem i8 %sel, 5
373 define i8 @urem_constant_sel_constants(i1 %cond) {
374 ; CHECK-LABEL: urem_constant_sel_constants:
376 ; CHECK-NEXT: tst w0, #0x1
377 ; CHECK-NEXT: mov w8, #5
378 ; CHECK-NEXT: mov w9, #120
379 ; CHECK-NEXT: csel w0, w9, w8, ne
381 %sel = select i1 %cond, i8 -4, i8 23
382 %bo = urem i8 120, %sel
386 define i8 @sel_constants_and_constant(i1 %cond) {
387 ; CHECK-LABEL: sel_constants_and_constant:
389 ; CHECK-NEXT: tst w0, #0x1
390 ; CHECK-NEXT: mov w8, #4
391 ; CHECK-NEXT: cinc w0, w8, eq
393 %sel = select i1 %cond, i8 -4, i8 23
398 define i8 @sel_constants_or_constant(i1 %cond) {
399 ; CHECK-LABEL: sel_constants_or_constant:
401 ; CHECK-NEXT: tst w0, #0x1
402 ; CHECK-NEXT: mov w8, #23
403 ; CHECK-NEXT: mov w9, #-3
404 ; CHECK-NEXT: csel w0, w9, w8, ne
406 %sel = select i1 %cond, i8 -4, i8 23
411 define i8 @sel_constants_xor_constant(i1 %cond) {
412 ; CHECK-LABEL: sel_constants_xor_constant:
414 ; CHECK-NEXT: tst w0, #0x1
415 ; CHECK-NEXT: mov w8, #18
416 ; CHECK-NEXT: mov w9, #-7
417 ; CHECK-NEXT: csel w0, w9, w8, ne
419 %sel = select i1 %cond, i8 -4, i8 23
424 define i8 @sel_constants_shl_constant(i1 %cond) {
425 ; CHECK-LABEL: sel_constants_shl_constant:
427 ; CHECK-NEXT: tst w0, #0x1
428 ; CHECK-NEXT: mov w8, #-32
429 ; CHECK-NEXT: mov w9, #-128
430 ; CHECK-NEXT: csel w0, w9, w8, ne
432 %sel = select i1 %cond, i8 -4, i8 23
437 define i8 @shl_constant_sel_constants(i1 %cond) {
438 ; CHECK-LABEL: shl_constant_sel_constants:
440 ; CHECK-NEXT: tst w0, #0x1
441 ; CHECK-NEXT: mov w8, #2
442 ; CHECK-NEXT: cinc x8, x8, eq
443 ; CHECK-NEXT: mov w9, #1
444 ; CHECK-NEXT: lsl w0, w9, w8
446 %sel = select i1 %cond, i8 2, i8 3
451 define i8 @sel_constants_lshr_constant(i1 %cond) {
452 ; CHECK-LABEL: sel_constants_lshr_constant:
454 ; CHECK-NEXT: tst w0, #0x1
455 ; CHECK-NEXT: mov w8, #7
456 ; CHECK-NEXT: csel w0, w8, wzr, ne
458 %sel = select i1 %cond, i8 -4, i8 23
459 %bo = lshr i8 %sel, 5
463 define i8 @lshr_constant_sel_constants(i1 %cond) {
464 ; CHECK-LABEL: lshr_constant_sel_constants:
466 ; CHECK-NEXT: tst w0, #0x1
467 ; CHECK-NEXT: mov w8, #2
468 ; CHECK-NEXT: cinc x8, x8, eq
469 ; CHECK-NEXT: mov w9, #64
470 ; CHECK-NEXT: lsr w0, w9, w8
472 %sel = select i1 %cond, i8 2, i8 3
473 %bo = lshr i8 64, %sel
478 define i8 @sel_constants_ashr_constant(i1 %cond) {
479 ; CHECK-LABEL: sel_constants_ashr_constant:
481 ; CHECK-NEXT: sbfx w0, w0, #0, #1
483 %sel = select i1 %cond, i8 -4, i8 23
484 %bo = ashr i8 %sel, 5
488 define i8 @ashr_constant_sel_constants(i1 %cond) {
489 ; CHECK-LABEL: ashr_constant_sel_constants:
491 ; CHECK-NEXT: tst w0, #0x1
492 ; CHECK-NEXT: mov w8, #2
493 ; CHECK-NEXT: cinc x8, x8, eq
494 ; CHECK-NEXT: mov w9, #-128
495 ; CHECK-NEXT: asr w0, w9, w8
497 %sel = select i1 %cond, i8 2, i8 3
498 %bo = ashr i8 128, %sel
502 define double @sel_constants_fadd_constant(i1 %cond) {
503 ; CHECK-LABEL: sel_constants_fadd_constant:
505 ; CHECK-NEXT: adrp x8, .LCPI42_0
506 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI42_0]
507 ; CHECK-NEXT: mov x8, #7378697629483820646
508 ; CHECK-NEXT: movk x8, #16444, lsl #48
509 ; CHECK-NEXT: tst w0, #0x1
510 ; CHECK-NEXT: fmov d1, x8
511 ; CHECK-NEXT: fcsel d0, d0, d1, ne
513 %sel = select i1 %cond, double -4.0, double 23.3
514 %bo = fadd double %sel, 5.1
518 define double @sel_constants_fsub_constant(i1 %cond) {
519 ; CHECK-LABEL: sel_constants_fsub_constant:
521 ; CHECK-NEXT: adrp x8, .LCPI43_0
522 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI43_0]
523 ; CHECK-NEXT: mov x8, #3689348814741910323
524 ; CHECK-NEXT: movk x8, #49186, lsl #48
525 ; CHECK-NEXT: tst w0, #0x1
526 ; CHECK-NEXT: fmov d1, x8
527 ; CHECK-NEXT: fcsel d0, d1, d0, ne
529 %sel = select i1 %cond, double -4.0, double 23.3
530 %bo = fsub double %sel, 5.1
534 define double @fsub_constant_sel_constants(i1 %cond) {
535 ; CHECK-LABEL: fsub_constant_sel_constants:
537 ; CHECK-NEXT: adrp x8, .LCPI44_0
538 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI44_0]
539 ; CHECK-NEXT: mov x8, #3689348814741910323
540 ; CHECK-NEXT: movk x8, #16418, lsl #48
541 ; CHECK-NEXT: tst w0, #0x1
542 ; CHECK-NEXT: fmov d1, x8
543 ; CHECK-NEXT: fcsel d0, d1, d0, ne
545 %sel = select i1 %cond, double -4.0, double 23.3
546 %bo = fsub double 5.1, %sel
550 define double @sel_constants_fmul_constant(i1 %cond) {
551 ; CHECK-LABEL: sel_constants_fmul_constant:
553 ; CHECK-NEXT: adrp x8, .LCPI45_0
554 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI45_0]
555 ; CHECK-NEXT: mov x8, #7378697629483820646
556 ; CHECK-NEXT: movk x8, #49204, lsl #48
557 ; CHECK-NEXT: tst w0, #0x1
558 ; CHECK-NEXT: fmov d1, x8
559 ; CHECK-NEXT: fcsel d0, d1, d0, ne
561 %sel = select i1 %cond, double -4.0, double 23.3
562 %bo = fmul double %sel, 5.1
566 define double @sel_constants_fdiv_constant(i1 %cond) {
567 ; CHECK-LABEL: sel_constants_fdiv_constant:
569 ; CHECK-NEXT: adrp x8, .LCPI46_0
570 ; CHECK-NEXT: adrp x9, .LCPI46_1
571 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI46_0]
572 ; CHECK-NEXT: ldr d1, [x9, :lo12:.LCPI46_1]
573 ; CHECK-NEXT: tst w0, #0x1
574 ; CHECK-NEXT: fcsel d0, d1, d0, ne
576 %sel = select i1 %cond, double -4.0, double 23.3
577 %bo = fdiv double %sel, 5.1
581 define double @fdiv_constant_sel_constants(i1 %cond) {
582 ; CHECK-LABEL: fdiv_constant_sel_constants:
584 ; CHECK-NEXT: adrp x8, .LCPI47_0
585 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI47_0]
586 ; CHECK-NEXT: mov x8, #7378697629483820646
587 ; CHECK-NEXT: movk x8, #49140, lsl #48
588 ; CHECK-NEXT: tst w0, #0x1
589 ; CHECK-NEXT: fmov d1, x8
590 ; CHECK-NEXT: fcsel d0, d1, d0, ne
592 %sel = select i1 %cond, double -4.0, double 23.3
593 %bo = fdiv double 5.1, %sel
597 define double @sel_constants_frem_constant(i1 %cond) {
598 ; CHECK-LABEL: sel_constants_frem_constant:
600 ; CHECK-NEXT: adrp x8, .LCPI48_0
601 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI48_0]
602 ; CHECK-NEXT: tst w0, #0x1
603 ; CHECK-NEXT: fmov d1, #-4.00000000
604 ; CHECK-NEXT: fcsel d0, d1, d0, ne
606 %sel = select i1 %cond, double -4.0, double 23.3
607 %bo = frem double %sel, 5.1
611 define double @frem_constant_sel_constants(i1 %cond) {
612 ; CHECK-LABEL: frem_constant_sel_constants:
614 ; CHECK-NEXT: adrp x8, .LCPI49_0
615 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI49_0]
616 ; CHECK-NEXT: mov x8, #7378697629483820646
617 ; CHECK-NEXT: movk x8, #16404, lsl #48
618 ; CHECK-NEXT: tst w0, #0x1
619 ; CHECK-NEXT: fmov d1, x8
620 ; CHECK-NEXT: fcsel d0, d0, d1, ne
622 %sel = select i1 %cond, double -4.0, double 23.3
623 %bo = frem double 5.1, %sel