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: mov w8, #1 // =0x1
13 ; CHECK-NEXT: bic w0, w8, w0
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: mov w8, #1 // =0x1
32 ; CHECK-NEXT: bic w0, w8, w0
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: and w8, w0, #0x1
72 ; CHECK-NEXT: sub w0, w8, #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: sub w0, w0, #1
83 %sel = select i1 %cond, i32 0, i32 -1
87 define i32 @select_0_or_neg1_signext(i1 signext %cond) {
88 ; CHECK-LABEL: select_0_or_neg1_signext:
90 ; CHECK-NEXT: mvn w0, w0
92 %sel = select i1 %cond, i32 0, i32 -1
96 ; select Cond, -1, 0 --> sext (Cond)
98 define i32 @select_neg1_or_0(i1 %cond) {
99 ; CHECK-LABEL: select_neg1_or_0:
101 ; CHECK-NEXT: sbfx w0, w0, #0, #1
103 %sel = select i1 %cond, i32 -1, i32 0
107 define i32 @select_neg1_or_0_zeroext(i1 zeroext %cond) {
108 ; CHECK-LABEL: select_neg1_or_0_zeroext:
110 ; CHECK-NEXT: sbfx w0, w0, #0, #1
112 %sel = select i1 %cond, i32 -1, i32 0
116 define i32 @select_neg1_or_0_signext(i1 signext %cond) {
117 ; CHECK-LABEL: select_neg1_or_0_signext:
120 %sel = select i1 %cond, i32 -1, i32 0
124 ; select Cond, C+1, C --> add (zext Cond), C
126 define i32 @select_Cplus1_C(i1 %cond) {
127 ; CHECK-LABEL: select_Cplus1_C:
129 ; CHECK-NEXT: mov w8, #41 // =0x29
130 ; CHECK-NEXT: tst w0, #0x1
131 ; CHECK-NEXT: cinc w0, w8, ne
133 %sel = select i1 %cond, i32 42, i32 41
137 define i32 @select_Cplus1_C_zeroext(i1 zeroext %cond) {
138 ; CHECK-LABEL: select_Cplus1_C_zeroext:
140 ; CHECK-NEXT: mov w8, #41 // =0x29
141 ; CHECK-NEXT: cmp w0, #0
142 ; CHECK-NEXT: cinc w0, w8, ne
144 %sel = select i1 %cond, i32 42, i32 41
148 define i32 @select_Cplus1_C_signext(i1 signext %cond) {
149 ; CHECK-LABEL: select_Cplus1_C_signext:
151 ; CHECK-NEXT: mov w8, #41 // =0x29
152 ; CHECK-NEXT: tst w0, #0x1
153 ; CHECK-NEXT: cinc w0, w8, ne
155 %sel = select i1 %cond, i32 42, i32 41
159 ; select Cond, C, C+1 --> add (sext Cond), C
161 define i32 @select_C_Cplus1(i1 %cond) {
162 ; CHECK-LABEL: select_C_Cplus1:
164 ; CHECK-NEXT: mov w8, #41 // =0x29
165 ; CHECK-NEXT: tst w0, #0x1
166 ; CHECK-NEXT: cinc w0, w8, eq
168 %sel = select i1 %cond, i32 41, i32 42
172 define i32 @select_C_Cplus1_zeroext(i1 zeroext %cond) {
173 ; CHECK-LABEL: select_C_Cplus1_zeroext:
175 ; CHECK-NEXT: mov w8, #41 // =0x29
176 ; CHECK-NEXT: cmp w0, #0
177 ; CHECK-NEXT: cinc w0, w8, eq
179 %sel = select i1 %cond, i32 41, i32 42
183 define i32 @select_C_Cplus1_signext(i1 signext %cond) {
184 ; CHECK-LABEL: select_C_Cplus1_signext:
186 ; CHECK-NEXT: mov w8, #41 // =0x29
187 ; CHECK-NEXT: tst w0, #0x1
188 ; CHECK-NEXT: cinc w0, w8, eq
190 %sel = select i1 %cond, i32 41, i32 42
194 ; In general, select of 2 constants could be:
195 ; select Cond, C1, C2 --> add (mul (zext Cond), C1-C2), C2 --> add (and (sext Cond), C1-C2), C2
197 define i32 @select_C1_C2(i1 %cond) {
198 ; CHECK-LABEL: select_C1_C2:
200 ; CHECK-NEXT: mov w8, #42 // =0x2a
201 ; CHECK-NEXT: tst w0, #0x1
202 ; CHECK-NEXT: mov w9, #421 // =0x1a5
203 ; CHECK-NEXT: csel w0, w9, w8, ne
205 %sel = select i1 %cond, i32 421, i32 42
209 define i32 @select_C1_C2_zeroext(i1 zeroext %cond) {
210 ; CHECK-LABEL: select_C1_C2_zeroext:
212 ; CHECK-NEXT: mov w8, #42 // =0x2a
213 ; CHECK-NEXT: cmp w0, #0
214 ; CHECK-NEXT: mov w9, #421 // =0x1a5
215 ; CHECK-NEXT: csel w0, w9, w8, ne
217 %sel = select i1 %cond, i32 421, i32 42
221 define i32 @select_C1_C2_signext(i1 signext %cond) {
222 ; CHECK-LABEL: select_C1_C2_signext:
224 ; CHECK-NEXT: mov w8, #42 // =0x2a
225 ; CHECK-NEXT: tst w0, #0x1
226 ; CHECK-NEXT: mov w9, #421 // =0x1a5
227 ; CHECK-NEXT: csel w0, w9, w8, ne
229 %sel = select i1 %cond, i32 421, i32 42
233 ; A binary operator with constant after the select should always get folded into the select.
235 define i8 @sel_constants_add_constant(i1 %cond) {
236 ; CHECK-LABEL: sel_constants_add_constant:
238 ; CHECK-NEXT: mov w8, #28 // =0x1c
239 ; CHECK-NEXT: tst w0, #0x1
240 ; CHECK-NEXT: csinc w0, w8, wzr, eq
242 %sel = select i1 %cond, i8 -4, i8 23
247 define i8 @sel_constants_sub_constant(i1 %cond) {
248 ; CHECK-LABEL: sel_constants_sub_constant:
250 ; CHECK-NEXT: mov w8, #18 // =0x12
251 ; CHECK-NEXT: tst w0, #0x1
252 ; CHECK-NEXT: mov w9, #-9 // =0xfffffff7
253 ; CHECK-NEXT: csel w0, w9, w8, ne
255 %sel = select i1 %cond, i8 -4, i8 23
260 define i8 @sel_constants_sub_constant_sel_constants(i1 %cond) {
261 ; CHECK-LABEL: sel_constants_sub_constant_sel_constants:
263 ; CHECK-NEXT: mov w8, #2 // =0x2
264 ; CHECK-NEXT: tst w0, #0x1
265 ; CHECK-NEXT: mov w9, #9 // =0x9
266 ; CHECK-NEXT: csel w0, w9, w8, ne
268 %sel = select i1 %cond, i8 -4, i8 3
273 define i8 @sel_constants_mul_constant(i1 %cond) {
274 ; CHECK-LABEL: sel_constants_mul_constant:
276 ; CHECK-NEXT: mov w8, #115 // =0x73
277 ; CHECK-NEXT: tst w0, #0x1
278 ; CHECK-NEXT: mov w9, #-20 // =0xffffffec
279 ; CHECK-NEXT: csel w0, w9, w8, ne
281 %sel = select i1 %cond, i8 -4, i8 23
286 define i8 @sel_constants_sdiv_constant(i1 %cond) {
287 ; CHECK-LABEL: sel_constants_sdiv_constant:
289 ; CHECK-NEXT: mov w8, #4 // =0x4
290 ; CHECK-NEXT: tst w0, #0x1
291 ; CHECK-NEXT: csel w0, wzr, w8, ne
293 %sel = select i1 %cond, i8 -4, i8 23
294 %bo = sdiv i8 %sel, 5
298 define i8 @sdiv_constant_sel_constants(i1 %cond) {
299 ; CHECK-LABEL: sdiv_constant_sel_constants:
301 ; CHECK-NEXT: mov w8, #5 // =0x5
302 ; CHECK-NEXT: tst w0, #0x1
303 ; CHECK-NEXT: csel w0, wzr, w8, ne
305 %sel = select i1 %cond, i8 121, i8 23
306 %bo = sdiv i8 120, %sel
310 define i8 @sel_constants_udiv_constant(i1 %cond) {
311 ; CHECK-LABEL: sel_constants_udiv_constant:
313 ; CHECK-NEXT: mov w8, #4 // =0x4
314 ; CHECK-NEXT: tst w0, #0x1
315 ; CHECK-NEXT: mov w9, #50 // =0x32
316 ; CHECK-NEXT: csel w0, w9, w8, ne
318 %sel = select i1 %cond, i8 -4, i8 23
319 %bo = udiv i8 %sel, 5
323 define i8 @udiv_constant_sel_constants(i1 %cond) {
324 ; CHECK-LABEL: udiv_constant_sel_constants:
326 ; CHECK-NEXT: mov w8, #5 // =0x5
327 ; CHECK-NEXT: tst w0, #0x1
328 ; CHECK-NEXT: csel w0, wzr, w8, ne
330 %sel = select i1 %cond, i8 -4, i8 23
331 %bo = udiv i8 120, %sel
335 define i8 @sel_constants_srem_constant(i1 %cond) {
336 ; CHECK-LABEL: sel_constants_srem_constant:
338 ; CHECK-NEXT: mov w8, #-4 // =0xfffffffc
339 ; CHECK-NEXT: tst w0, #0x1
340 ; CHECK-NEXT: cinv w0, w8, eq
342 %sel = select i1 %cond, i8 -4, i8 23
343 %bo = srem i8 %sel, 5
347 define i8 @srem_constant_sel_constants(i1 %cond) {
348 ; CHECK-LABEL: srem_constant_sel_constants:
350 ; CHECK-NEXT: mov w8, #5 // =0x5
351 ; CHECK-NEXT: tst w0, #0x1
352 ; CHECK-NEXT: mov w9, #120 // =0x78
353 ; CHECK-NEXT: csel w0, w9, w8, ne
355 %sel = select i1 %cond, i8 121, i8 23
356 %bo = srem i8 120, %sel
360 define i8 @sel_constants_urem_constant(i1 %cond) {
361 ; CHECK-LABEL: sel_constants_urem_constant:
363 ; CHECK-NEXT: mov w8, #2 // =0x2
364 ; CHECK-NEXT: tst w0, #0x1
365 ; CHECK-NEXT: cinc w0, w8, eq
367 %sel = select i1 %cond, i8 -4, i8 23
368 %bo = urem i8 %sel, 5
372 define i8 @urem_constant_sel_constants(i1 %cond) {
373 ; CHECK-LABEL: urem_constant_sel_constants:
375 ; CHECK-NEXT: mov w8, #5 // =0x5
376 ; CHECK-NEXT: tst w0, #0x1
377 ; CHECK-NEXT: mov w9, #120 // =0x78
378 ; CHECK-NEXT: csel w0, w9, w8, ne
380 %sel = select i1 %cond, i8 -4, i8 23
381 %bo = urem i8 120, %sel
385 define i8 @sel_constants_and_constant(i1 %cond) {
386 ; CHECK-LABEL: sel_constants_and_constant:
388 ; CHECK-NEXT: mov w8, #4 // =0x4
389 ; CHECK-NEXT: tst w0, #0x1
390 ; CHECK-NEXT: cinc w0, w8, eq
392 %sel = select i1 %cond, i8 -4, i8 23
397 define i8 @sel_constants_or_constant(i1 %cond) {
398 ; CHECK-LABEL: sel_constants_or_constant:
400 ; CHECK-NEXT: mov w8, #23 // =0x17
401 ; CHECK-NEXT: tst w0, #0x1
402 ; CHECK-NEXT: mov w9, #-3 // =0xfffffffd
403 ; CHECK-NEXT: csel w0, w9, w8, ne
405 %sel = select i1 %cond, i8 -4, i8 23
410 define i8 @sel_constants_xor_constant(i1 %cond) {
411 ; CHECK-LABEL: sel_constants_xor_constant:
413 ; CHECK-NEXT: mov w8, #18 // =0x12
414 ; CHECK-NEXT: tst w0, #0x1
415 ; CHECK-NEXT: mov w9, #-7 // =0xfffffff9
416 ; CHECK-NEXT: csel w0, w9, w8, ne
418 %sel = select i1 %cond, i8 -4, i8 23
423 define i8 @sel_constants_shl_constant(i1 %cond) {
424 ; CHECK-LABEL: sel_constants_shl_constant:
426 ; CHECK-NEXT: mov w8, #-32 // =0xffffffe0
427 ; CHECK-NEXT: tst w0, #0x1
428 ; CHECK-NEXT: mov w9, #-128 // =0xffffff80
429 ; CHECK-NEXT: csel w0, w9, w8, ne
431 %sel = select i1 %cond, i8 -4, i8 23
436 define i8 @shl_constant_sel_constants(i1 %cond) {
437 ; CHECK-LABEL: shl_constant_sel_constants:
439 ; CHECK-NEXT: mov w8, #8 // =0x8
440 ; CHECK-NEXT: tst w0, #0x1
441 ; CHECK-NEXT: mov w9, #4 // =0x4
442 ; CHECK-NEXT: csel w0, w9, w8, ne
444 %sel = select i1 %cond, i8 2, i8 3
449 define i8 @sel_constants_lshr_constant(i1 %cond) {
450 ; CHECK-LABEL: sel_constants_lshr_constant:
452 ; CHECK-NEXT: mov w8, #7 // =0x7
453 ; CHECK-NEXT: tst w0, #0x1
454 ; CHECK-NEXT: csel w0, w8, wzr, ne
456 %sel = select i1 %cond, i8 -4, i8 23
457 %bo = lshr i8 %sel, 5
461 define i8 @lshr_constant_sel_constants(i1 %cond) {
462 ; CHECK-LABEL: lshr_constant_sel_constants:
464 ; CHECK-NEXT: mov w8, #8 // =0x8
465 ; CHECK-NEXT: tst w0, #0x1
466 ; CHECK-NEXT: mov w9, #16 // =0x10
467 ; CHECK-NEXT: csel w0, w9, w8, ne
469 %sel = select i1 %cond, i8 2, i8 3
470 %bo = lshr i8 64, %sel
475 define i8 @sel_constants_ashr_constant(i1 %cond) {
476 ; CHECK-LABEL: sel_constants_ashr_constant:
478 ; CHECK-NEXT: sbfx w0, w0, #0, #1
480 %sel = select i1 %cond, i8 -4, i8 23
481 %bo = ashr i8 %sel, 5
485 define i8 @ashr_constant_sel_constants(i1 %cond) {
486 ; CHECK-LABEL: ashr_constant_sel_constants:
488 ; CHECK-NEXT: mov w8, #-16 // =0xfffffff0
489 ; CHECK-NEXT: tst w0, #0x1
490 ; CHECK-NEXT: mov w9, #-32 // =0xffffffe0
491 ; CHECK-NEXT: csel w0, w9, w8, ne
493 %sel = select i1 %cond, i8 2, i8 3
494 %bo = ashr i8 128, %sel
498 define double @sel_constants_fadd_constant(i1 %cond) {
499 ; CHECK-LABEL: sel_constants_fadd_constant:
501 ; CHECK-NEXT: mov x9, #7378697629483820646 // =0x6666666666666666
502 ; CHECK-NEXT: adrp x8, .LCPI42_0
503 ; CHECK-NEXT: tst w0, #0x1
504 ; CHECK-NEXT: movk x9, #16444, lsl #48
505 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI42_0]
506 ; CHECK-NEXT: fmov d1, x9
507 ; CHECK-NEXT: fcsel d0, d0, d1, ne
509 %sel = select i1 %cond, double -4.0, double 23.3
510 %bo = fadd double %sel, 5.1
514 define double @sel_constants_fsub_constant(i1 %cond) {
515 ; CHECK-LABEL: sel_constants_fsub_constant:
517 ; CHECK-NEXT: adrp x8, .LCPI43_0
518 ; CHECK-NEXT: tst w0, #0x1
519 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI43_0]
520 ; CHECK-NEXT: mov x8, #3689348814741910323 // =0x3333333333333333
521 ; CHECK-NEXT: movk x8, #49186, lsl #48
522 ; CHECK-NEXT: fmov d1, x8
523 ; CHECK-NEXT: fcsel d0, d1, d0, ne
525 %sel = select i1 %cond, double -4.0, double 23.3
526 %bo = fsub double %sel, 5.1
530 define double @fsub_constant_sel_constants(i1 %cond) {
531 ; CHECK-LABEL: fsub_constant_sel_constants:
533 ; CHECK-NEXT: adrp x8, .LCPI44_0
534 ; CHECK-NEXT: tst w0, #0x1
535 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI44_0]
536 ; CHECK-NEXT: mov x8, #3689348814741910323 // =0x3333333333333333
537 ; CHECK-NEXT: movk x8, #16418, lsl #48
538 ; CHECK-NEXT: fmov d1, x8
539 ; CHECK-NEXT: fcsel d0, d1, d0, ne
541 %sel = select i1 %cond, double -4.0, double 23.3
542 %bo = fsub double 5.1, %sel
546 define double @sel_constants_fmul_constant(i1 %cond) {
547 ; CHECK-LABEL: sel_constants_fmul_constant:
549 ; CHECK-NEXT: adrp x8, .LCPI45_0
550 ; CHECK-NEXT: tst w0, #0x1
551 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI45_0]
552 ; CHECK-NEXT: mov x8, #7378697629483820646 // =0x6666666666666666
553 ; CHECK-NEXT: movk x8, #49204, lsl #48
554 ; CHECK-NEXT: fmov d1, x8
555 ; CHECK-NEXT: fcsel d0, d1, d0, ne
557 %sel = select i1 %cond, double -4.0, double 23.3
558 %bo = fmul double %sel, 5.1
562 define double @sel_constants_fdiv_constant(i1 %cond) {
563 ; CHECK-LABEL: sel_constants_fdiv_constant:
565 ; CHECK-NEXT: adrp x8, .LCPI46_0
566 ; CHECK-NEXT: adrp x9, .LCPI46_1
567 ; CHECK-NEXT: tst w0, #0x1
568 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI46_0]
569 ; CHECK-NEXT: ldr d1, [x9, :lo12:.LCPI46_1]
570 ; CHECK-NEXT: fcsel d0, d1, d0, ne
572 %sel = select i1 %cond, double -4.0, double 23.3
573 %bo = fdiv double %sel, 5.1
577 define double @fdiv_constant_sel_constants(i1 %cond) {
578 ; CHECK-LABEL: fdiv_constant_sel_constants:
580 ; CHECK-NEXT: adrp x8, .LCPI47_0
581 ; CHECK-NEXT: tst w0, #0x1
582 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI47_0]
583 ; CHECK-NEXT: mov x8, #7378697629483820646 // =0x6666666666666666
584 ; CHECK-NEXT: movk x8, #49140, lsl #48
585 ; CHECK-NEXT: fmov d1, x8
586 ; CHECK-NEXT: fcsel d0, d1, d0, ne
588 %sel = select i1 %cond, double -4.0, double 23.3
589 %bo = fdiv double 5.1, %sel
593 define double @sel_constants_frem_constant(i1 %cond) {
594 ; CHECK-LABEL: sel_constants_frem_constant:
596 ; CHECK-NEXT: adrp x8, .LCPI48_0
597 ; CHECK-NEXT: fmov d0, #-4.00000000
598 ; CHECK-NEXT: tst w0, #0x1
599 ; CHECK-NEXT: ldr d1, [x8, :lo12:.LCPI48_0]
600 ; CHECK-NEXT: fcsel d0, d0, d1, ne
602 %sel = select i1 %cond, double -4.0, double 23.3
603 %bo = frem double %sel, 5.1
607 define double @frem_constant_sel_constants(i1 %cond) {
608 ; CHECK-LABEL: frem_constant_sel_constants:
610 ; CHECK-NEXT: mov x9, #7378697629483820646 // =0x6666666666666666
611 ; CHECK-NEXT: adrp x8, .LCPI49_0
612 ; CHECK-NEXT: tst w0, #0x1
613 ; CHECK-NEXT: movk x9, #16404, lsl #48
614 ; CHECK-NEXT: ldr d0, [x8, :lo12:.LCPI49_0]
615 ; CHECK-NEXT: fmov d1, x9
616 ; CHECK-NEXT: fcsel d0, d0, d1, ne
618 %sel = select i1 %cond, double -4.0, double 23.3
619 %bo = frem double 5.1, %sel