1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown | 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: notb %dil
13 ; CHECK-NEXT: movzbl %dil, %eax
14 ; CHECK-NEXT: andl $1, %eax
16 %sel = select i1 %cond, i32 0, i32 1
20 define i32 @select_0_or_1_zeroext(i1 zeroext %cond) {
21 ; CHECK-LABEL: select_0_or_1_zeroext:
23 ; CHECK-NEXT: xorb $1, %dil
24 ; CHECK-NEXT: movzbl %dil, %eax
26 %sel = select i1 %cond, i32 0, i32 1
30 define i32 @select_0_or_1_signext(i1 signext %cond) {
31 ; CHECK-LABEL: select_0_or_1_signext:
33 ; CHECK-NEXT: notb %dil
34 ; CHECK-NEXT: movzbl %dil, %eax
35 ; CHECK-NEXT: andl $1, %eax
37 %sel = select i1 %cond, i32 0, i32 1
41 ; select Cond, 1, 0 --> zext (Cond)
43 define i32 @select_1_or_0(i1 %cond) {
44 ; CHECK-LABEL: select_1_or_0:
46 ; CHECK-NEXT: movl %edi, %eax
47 ; CHECK-NEXT: andl $1, %eax
49 %sel = select i1 %cond, i32 1, i32 0
53 define i32 @select_1_or_0_zeroext(i1 zeroext %cond) {
54 ; CHECK-LABEL: select_1_or_0_zeroext:
56 ; CHECK-NEXT: movl %edi, %eax
58 %sel = select i1 %cond, i32 1, i32 0
62 define i32 @select_1_or_0_signext(i1 signext %cond) {
63 ; CHECK-LABEL: select_1_or_0_signext:
65 ; CHECK-NEXT: movl %edi, %eax
66 ; CHECK-NEXT: andl $1, %eax
68 %sel = select i1 %cond, i32 1, i32 0
72 ; select Cond, 0, -1 --> sext (!Cond)
74 define i32 @select_0_or_neg1(i1 %cond) {
75 ; CHECK-LABEL: select_0_or_neg1:
77 ; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
78 ; CHECK-NEXT: andl $1, %edi
79 ; CHECK-NEXT: leal -1(%rdi), %eax
81 %sel = select i1 %cond, i32 0, i32 -1
85 define i32 @select_0_or_neg1_zeroext(i1 zeroext %cond) {
86 ; CHECK-LABEL: select_0_or_neg1_zeroext:
88 ; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
89 ; CHECK-NEXT: leal -1(%rdi), %eax
91 %sel = select i1 %cond, i32 0, i32 -1
95 define i32 @select_0_or_neg1_signext(i1 signext %cond) {
96 ; CHECK-LABEL: select_0_or_neg1_signext:
98 ; CHECK-NEXT: movl %edi, %eax
99 ; CHECK-NEXT: notl %eax
101 %sel = select i1 %cond, i32 0, i32 -1
105 ; select Cond, -1, 0 --> sext (Cond)
107 define i32 @select_neg1_or_0(i1 %cond) {
108 ; CHECK-LABEL: select_neg1_or_0:
110 ; CHECK-NEXT: movl %edi, %eax
111 ; CHECK-NEXT: andl $1, %eax
112 ; CHECK-NEXT: negl %eax
114 %sel = select i1 %cond, i32 -1, i32 0
118 define i32 @select_neg1_or_0_zeroext(i1 zeroext %cond) {
119 ; CHECK-LABEL: select_neg1_or_0_zeroext:
121 ; CHECK-NEXT: movl %edi, %eax
122 ; CHECK-NEXT: negl %eax
124 %sel = select i1 %cond, i32 -1, i32 0
128 define i32 @select_neg1_or_0_signext(i1 signext %cond) {
129 ; CHECK-LABEL: select_neg1_or_0_signext:
131 ; CHECK-NEXT: movl %edi, %eax
133 %sel = select i1 %cond, i32 -1, i32 0
137 ; select Cond, C+1, C --> add (zext Cond), C
139 define i32 @select_Cplus1_C(i1 %cond) {
140 ; CHECK-LABEL: select_Cplus1_C:
142 ; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
143 ; CHECK-NEXT: andl $1, %edi
144 ; CHECK-NEXT: leal 41(%rdi), %eax
146 %sel = select i1 %cond, i32 42, i32 41
150 define i32 @select_Cplus1_C_zeroext(i1 zeroext %cond) {
151 ; CHECK-LABEL: select_Cplus1_C_zeroext:
153 ; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
154 ; CHECK-NEXT: leal 41(%rdi), %eax
156 %sel = select i1 %cond, i32 42, i32 41
160 define i32 @select_Cplus1_C_signext(i1 signext %cond) {
161 ; CHECK-LABEL: select_Cplus1_C_signext:
163 ; CHECK-NEXT: movl $41, %eax
164 ; CHECK-NEXT: subl %edi, %eax
166 %sel = select i1 %cond, i32 42, i32 41
170 ; select Cond, C, C+1 --> add (sext Cond), C
172 define i32 @select_C_Cplus1(i1 %cond) {
173 ; CHECK-LABEL: select_C_Cplus1:
175 ; CHECK-NEXT: andl $1, %edi
176 ; CHECK-NEXT: movl $42, %eax
177 ; CHECK-NEXT: subl %edi, %eax
179 %sel = select i1 %cond, i32 41, i32 42
183 define i32 @select_C_Cplus1_zeroext(i1 zeroext %cond) {
184 ; CHECK-LABEL: select_C_Cplus1_zeroext:
186 ; CHECK-NEXT: movl $42, %eax
187 ; CHECK-NEXT: subl %edi, %eax
189 %sel = select i1 %cond, i32 41, i32 42
193 define i32 @select_C_Cplus1_signext(i1 signext %cond) {
194 ; CHECK-LABEL: select_C_Cplus1_signext:
196 ; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
197 ; CHECK-NEXT: leal 42(%rdi), %eax
199 %sel = select i1 %cond, i32 41, i32 42
203 ; If the constants differ by a small multiplier, use LEA.
204 ; select Cond, C1, C2 --> add (mul (zext Cond), C1-C2), C2 --> LEA C2(Cond * (C1-C2))
206 define i32 @select_lea_2(i1 zeroext %cond) {
207 ; CHECK-LABEL: select_lea_2:
209 ; CHECK-NEXT: xorb $1, %dil
210 ; CHECK-NEXT: movzbl %dil, %eax
211 ; CHECK-NEXT: leal -1(%rax,%rax), %eax
213 %sel = select i1 %cond, i32 -1, i32 1
217 define i64 @select_lea_3(i1 zeroext %cond) {
218 ; CHECK-LABEL: select_lea_3:
220 ; CHECK-NEXT: xorb $1, %dil
221 ; CHECK-NEXT: movzbl %dil, %eax
222 ; CHECK-NEXT: leaq -2(%rax,%rax,2), %rax
224 %sel = select i1 %cond, i64 -2, i64 1
228 define i32 @select_lea_5(i1 zeroext %cond) {
229 ; CHECK-LABEL: select_lea_5:
231 ; CHECK-NEXT: xorb $1, %dil
232 ; CHECK-NEXT: movzbl %dil, %eax
233 ; CHECK-NEXT: leal -2(%rax,%rax,4), %eax
235 %sel = select i1 %cond, i32 -2, i32 3
239 define i64 @select_lea_9(i1 zeroext %cond) {
240 ; CHECK-LABEL: select_lea_9:
242 ; CHECK-NEXT: xorb $1, %dil
243 ; CHECK-NEXT: movzbl %dil, %eax
244 ; CHECK-NEXT: leaq -7(%rax,%rax,8), %rax
246 %sel = select i1 %cond, i64 -7, i64 2
250 ; Should this be 'sbb x,x' or 'sbb 0,x' with simpler LEA or add?
252 define i64 @sel_1_2(i64 %x, i64 %y) {
253 ; CHECK-LABEL: sel_1_2:
255 ; CHECK-NEXT: cmpq $42, %rdi
256 ; CHECK-NEXT: sbbq $0, %rsi
257 ; CHECK-NEXT: leaq 2(%rsi), %rax
259 %cmp = icmp ult i64 %x, 42
260 %sel = select i1 %cmp, i64 1, i64 2
261 %sub = add i64 %sel, %y
265 ; No LEA with 8-bit, but this shouldn't need branches or cmov.
267 define i8 @sel_1_neg1(i32 %x) {
268 ; CHECK-LABEL: sel_1_neg1:
270 ; CHECK-NEXT: cmpl $42, %edi
271 ; CHECK-NEXT: setg %al
272 ; CHECK-NEXT: shlb $2, %al
273 ; CHECK-NEXT: decb %al
275 %cmp = icmp sgt i32 %x, 42
276 %sel = select i1 %cmp, i8 3, i8 -1
280 ; We get an LEA for 16-bit because we ignore the high-bits.
282 define i16 @sel_neg1_1(i32 %x) {
283 ; CHECK-LABEL: sel_neg1_1:
285 ; CHECK-NEXT: xorl %eax, %eax
286 ; CHECK-NEXT: cmpl $43, %edi
287 ; CHECK-NEXT: setl %al
288 ; CHECK-NEXT: leal -1(,%rax,4), %eax
289 ; CHECK-NEXT: # kill: def $ax killed $ax killed $eax
291 %cmp = icmp sgt i32 %x, 42
292 %sel = select i1 %cmp, i16 -1, i16 3
296 ; If the comparison is available, the predicate can be inverted.
298 define i32 @sel_1_neg1_32(i32 %x) {
299 ; CHECK-LABEL: sel_1_neg1_32:
301 ; CHECK-NEXT: xorl %eax, %eax
302 ; CHECK-NEXT: cmpl $42, %edi
303 ; CHECK-NEXT: setg %al
304 ; CHECK-NEXT: leal -1(%rax,%rax,8), %eax
306 %cmp = icmp sgt i32 %x, 42
307 %sel = select i1 %cmp, i32 8, i32 -1
311 define i32 @sel_neg1_1_32(i32 %x) {
312 ; CHECK-LABEL: sel_neg1_1_32:
314 ; CHECK-NEXT: xorl %eax, %eax
315 ; CHECK-NEXT: cmpl $43, %edi
316 ; CHECK-NEXT: setl %al
317 ; CHECK-NEXT: leal -7(%rax,%rax,8), %eax
319 %cmp = icmp sgt i32 %x, 42
320 %sel = select i1 %cmp, i32 -7, i32 2
325 ; If the constants differ by a large power-of-2, that can be a shift of the difference plus the smaller constant.
326 ; select Cond, C1, C2 --> add (mul (zext Cond), C1-C2), C2
328 define i8 @select_pow2_diff(i1 zeroext %cond) {
329 ; CHECK-LABEL: select_pow2_diff:
331 ; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
332 ; CHECK-NEXT: shlb $4, %dil
333 ; CHECK-NEXT: leal 3(%rdi), %eax
334 ; CHECK-NEXT: # kill: def $al killed $al killed $eax
336 %sel = select i1 %cond, i8 19, i8 3
340 define i16 @select_pow2_diff_invert(i1 zeroext %cond) {
341 ; CHECK-LABEL: select_pow2_diff_invert:
343 ; CHECK-NEXT: xorb $1, %dil
344 ; CHECK-NEXT: movzbl %dil, %eax
345 ; CHECK-NEXT: shll $6, %eax
346 ; CHECK-NEXT: orl $7, %eax
347 ; CHECK-NEXT: # kill: def $ax killed $ax killed $eax
349 %sel = select i1 %cond, i16 7, i16 71
353 define i32 @select_pow2_diff_neg(i1 zeroext %cond) {
354 ; CHECK-LABEL: select_pow2_diff_neg:
356 ; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
357 ; CHECK-NEXT: shll $4, %edi
358 ; CHECK-NEXT: leal -25(%rdi), %eax
360 %sel = select i1 %cond, i32 -9, i32 -25
364 define i64 @select_pow2_diff_neg_invert(i1 zeroext %cond) {
365 ; CHECK-LABEL: select_pow2_diff_neg_invert:
367 ; CHECK-NEXT: xorb $1, %dil
368 ; CHECK-NEXT: movzbl %dil, %eax
369 ; CHECK-NEXT: shlq $7, %rax
370 ; CHECK-NEXT: addq $-99, %rax
372 %sel = select i1 %cond, i64 -99, i64 29
376 ; This doesn't need a branch, but don't do the wrong thing if subtraction of the constants overflows.
378 define i8 @sel_67_neg125(i32 %x) {
379 ; CHECK-LABEL: sel_67_neg125:
381 ; CHECK-NEXT: cmpl $42, %edi
382 ; CHECK-NEXT: movl $67, %ecx
383 ; CHECK-NEXT: movl $131, %eax
384 ; CHECK-NEXT: cmovgl %ecx, %eax
385 ; CHECK-NEXT: # kill: def $al killed $al killed $eax
387 %cmp = icmp sgt i32 %x, 42
388 %sel = select i1 %cmp, i8 67, i8 -125
393 ; In general, select of 2 constants could be:
394 ; select Cond, C1, C2 --> add (mul (zext Cond), C1-C2), C2 --> add (and (sext Cond), C1-C2), C2
396 define i32 @select_C1_C2(i1 %cond) {
397 ; CHECK-LABEL: select_C1_C2:
399 ; CHECK-NEXT: testb $1, %dil
400 ; CHECK-NEXT: movl $421, %ecx # imm = 0x1A5
401 ; CHECK-NEXT: movl $42, %eax
402 ; CHECK-NEXT: cmovnel %ecx, %eax
404 %sel = select i1 %cond, i32 421, i32 42
408 define i32 @select_C1_C2_zeroext(i1 zeroext %cond) {
409 ; CHECK-LABEL: select_C1_C2_zeroext:
411 ; CHECK-NEXT: testl %edi, %edi
412 ; CHECK-NEXT: movl $421, %ecx # imm = 0x1A5
413 ; CHECK-NEXT: movl $42, %eax
414 ; CHECK-NEXT: cmovnel %ecx, %eax
416 %sel = select i1 %cond, i32 421, i32 42
420 define i32 @select_C1_C2_signext(i1 signext %cond) {
421 ; CHECK-LABEL: select_C1_C2_signext:
423 ; CHECK-NEXT: testb $1, %dil
424 ; CHECK-NEXT: movl $421, %ecx # imm = 0x1A5
425 ; CHECK-NEXT: movl $42, %eax
426 ; CHECK-NEXT: cmovnel %ecx, %eax
428 %sel = select i1 %cond, i32 421, i32 42
432 ; select (x == 2), 2, (x + 1) --> select (x == 2), x, (x + 1)
434 define i64 @select_2_or_inc(i64 %x) {
435 ; CHECK-LABEL: select_2_or_inc:
437 ; CHECK-NEXT: leaq 1(%rdi), %rax
438 ; CHECK-NEXT: cmpq $2, %rdi
439 ; CHECK-NEXT: cmoveq %rdi, %rax
441 %cmp = icmp eq i64 %x, 2
443 %retval.0 = select i1 %cmp, i64 2, i64 %add
447 define <4 x i32> @sel_constants_add_constant_vec(i1 %cond) {
448 ; CHECK-LABEL: sel_constants_add_constant_vec:
450 ; CHECK-NEXT: testb $1, %dil
451 ; CHECK-NEXT: jne .LBB36_1
452 ; CHECK-NEXT: # %bb.2:
453 ; CHECK-NEXT: movaps {{.*#+}} xmm0 = [12,13,14,15]
455 ; CHECK-NEXT: .LBB36_1:
456 ; CHECK-NEXT: movaps {{.*#+}} xmm0 = [4294967293,14,4,4]
458 %sel = select i1 %cond, <4 x i32> <i32 -4, i32 12, i32 1, i32 0>, <4 x i32> <i32 11, i32 11, i32 11, i32 11>
459 %bo = add <4 x i32> %sel, <i32 1, i32 2, i32 3, i32 4>
463 define <2 x double> @sel_constants_fmul_constant_vec(i1 %cond) {
464 ; CHECK-LABEL: sel_constants_fmul_constant_vec:
466 ; CHECK-NEXT: testb $1, %dil
467 ; CHECK-NEXT: jne .LBB37_1
468 ; CHECK-NEXT: # %bb.2:
469 ; CHECK-NEXT: movaps {{.*#+}} xmm0 = [1.1883E+2,3.4539999999999999E+1]
471 ; CHECK-NEXT: .LBB37_1:
472 ; CHECK-NEXT: movaps {{.*#+}} xmm0 = [-2.0399999999999999E+1,3.768E+1]
474 %sel = select i1 %cond, <2 x double> <double -4.0, double 12.0>, <2 x double> <double 23.3, double 11.0>
475 %bo = fmul <2 x double> %sel, <double 5.1, double 3.14>
479 ; 4294967297 = 0x100000001.
480 ; This becomes an opaque constant via ConstantHoisting, so we don't fold it into the select.
482 define i64 @opaque_constant(i1 %cond, i64 %x) {
483 ; CHECK-LABEL: opaque_constant:
485 ; CHECK-NEXT: testb $1, %dil
486 ; CHECK-NEXT: movq $-4, %rcx
487 ; CHECK-NEXT: movl $23, %eax
488 ; CHECK-NEXT: cmovneq %rcx, %rax
489 ; CHECK-NEXT: movabsq $4294967297, %rcx # imm = 0x100000001
490 ; CHECK-NEXT: andq %rcx, %rax
491 ; CHECK-NEXT: xorl %edx, %edx
492 ; CHECK-NEXT: cmpq %rcx, %rsi
493 ; CHECK-NEXT: sete %dl
494 ; CHECK-NEXT: subq %rdx, %rax
496 %sel = select i1 %cond, i64 -4, i64 23
497 %bo = and i64 %sel, 4294967297
498 %cmp = icmp eq i64 %x, 4294967297
499 %sext = sext i1 %cmp to i64
500 %add = add i64 %bo, %sext
504 define float @select_undef_fp(float %x) {
505 ; CHECK-LABEL: select_undef_fp:
507 ; CHECK-NEXT: movss {{.*#+}} xmm0 = mem[0],zero,zero,zero
509 %f = select i1 undef, float 4.0, float %x