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 ; Vary the operand sizes for extra coverage, but the transform should be identical in all cases.
6 ; (X == 0) ? 0 : -1 --> (X == 0) - 1
8 define i8 @i8_select_0_or_neg1(i8 %x) {
9 ; CHECK-LABEL: i8_select_0_or_neg1:
11 ; CHECK-NEXT: negb %dil
12 ; CHECK-NEXT: sbbl %eax, %eax
13 ; CHECK-NEXT: # kill: def $al killed $al killed $eax
15 %cmp = icmp eq i8 %x, 0
16 %sel = select i1 %cmp, i8 0, i8 -1
20 ; (X == 0) ? 0 : -1 --> (X == 0) - 1
22 define i16 @i16_select_0_or_neg1_as_math(i16 %x) {
23 ; CHECK-LABEL: i16_select_0_or_neg1_as_math:
25 ; CHECK-NEXT: negw %di
26 ; CHECK-NEXT: sbbl %eax, %eax
27 ; CHECK-NEXT: # kill: def $ax killed $ax killed $eax
29 %cmp = icmp eq i16 %x, 0
30 %ext = zext i1 %cmp to i16
31 %add = add i16 %ext, -1
35 ; (X != 0) ? -1 : 0 --> 0 - (X != 0)
37 define i32 @i32_select_0_or_neg1_commuted(i32 %x) {
38 ; CHECK-LABEL: i32_select_0_or_neg1_commuted:
40 ; CHECK-NEXT: negl %edi
41 ; CHECK-NEXT: sbbl %eax, %eax
43 %cmp = icmp ne i32 %x, 0
44 %sel = select i1 %cmp, i32 -1, i32 0
48 ; (X != 0) ? -1 : 0 --> 0 - (X != 0)
50 define i64 @i64_select_0_or_neg1_commuted_as_math(i64 %x) {
51 ; CHECK-LABEL: i64_select_0_or_neg1_commuted_as_math:
53 ; CHECK-NEXT: negq %rdi
54 ; CHECK-NEXT: sbbq %rax, %rax
56 %cmp = icmp ne i64 %x, 0
57 %ext = zext i1 %cmp to i64
58 %sub = sub i64 0, %ext
62 ; (X == 0) ? -1 : 0 --> 0 - (X == 0)
64 define i64 @i64_select_neg1_or_0(i64 %x) {
65 ; CHECK-LABEL: i64_select_neg1_or_0:
67 ; CHECK-NEXT: cmpq $1, %rdi
68 ; CHECK-NEXT: sbbq %rax, %rax
70 %cmp = icmp eq i64 %x, 0
71 %sel = select i1 %cmp, i64 -1, i64 0
75 ; (X == 0) ? -1 : 0 --> 0 - (X == 0)
77 define i32 @i32_select_neg1_or_0_as_math(i32 %x) {
78 ; CHECK-LABEL: i32_select_neg1_or_0_as_math:
80 ; CHECK-NEXT: cmpl $1, %edi
81 ; CHECK-NEXT: sbbl %eax, %eax
83 %cmp = icmp eq i32 %x, 0
84 %ext = zext i1 %cmp to i32
85 %sub = sub i32 0, %ext
89 ; (X != 0) ? 0 : -1 --> (X != 0) - 1
91 define i16 @i16_select_neg1_or_0_commuted(i16 %x) {
92 ; CHECK-LABEL: i16_select_neg1_or_0_commuted:
94 ; CHECK-NEXT: cmpw $1, %di
95 ; CHECK-NEXT: sbbl %eax, %eax
96 ; CHECK-NEXT: # kill: def $ax killed $ax killed $eax
98 %cmp = icmp ne i16 %x, 0
99 %sel = select i1 %cmp, i16 0, i16 -1
103 ; (X != 0) ? 0 : -1 --> (X != 0) - 1
105 define i8 @i8_select_neg1_or_0_commuted_as_math(i8 %x) {
106 ; CHECK-LABEL: i8_select_neg1_or_0_commuted_as_math:
108 ; CHECK-NEXT: cmpb $1, %dil
109 ; CHECK-NEXT: sbbl %eax, %eax
110 ; CHECK-NEXT: # kill: def $al killed $al killed $eax
112 %cmp = icmp ne i8 %x, 0
113 %ext = zext i1 %cmp to i8
114 %add = add i8 %ext, -1
118 ; (X <u Y) ? -1 : 0 --> cmp, sbb
120 define i32 @ult_select_neg1_or_0(i32 %x, i32 %y) nounwind {
121 ; CHECK-LABEL: ult_select_neg1_or_0:
123 ; CHECK-NEXT: cmpl %esi, %edi
124 ; CHECK-NEXT: sbbl %eax, %eax
126 %cmp = icmp ult i32 %x, %y
127 %ext = sext i1 %cmp to i32
131 ; Swap the predicate and compare operands:
132 ; (Y >u X) ? -1 : 0 --> cmp, sbb
134 define i32 @ugt_select_neg1_or_0(i32 %x, i32 %y) nounwind {
135 ; CHECK-LABEL: ugt_select_neg1_or_0:
137 ; CHECK-NEXT: cmpl %esi, %edi
138 ; CHECK-NEXT: sbbl %eax, %eax
140 %cmp = icmp ugt i32 %y, %x
141 %ext = sext i1 %cmp to i32
145 ; Invert the predicate and effectively swap the select operands:
146 ; (X >=u Y) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb
148 define i32 @uge_select_0_or_neg1(i32 %x, i32 %y) nounwind {
149 ; CHECK-LABEL: uge_select_0_or_neg1:
151 ; CHECK-NEXT: cmpl %esi, %edi
152 ; CHECK-NEXT: sbbl %eax, %eax
154 %cmp = icmp uge i32 %x, %y
155 %ext = zext i1 %cmp to i32
156 %add = add i32 %ext, -1
160 ; Swap the predicate and compare operands:
161 ; (Y <=u X) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb
163 define i32 @ule_select_0_or_neg1(i32 %x, i32 %y) nounwind {
164 ; CHECK-LABEL: ule_select_0_or_neg1:
166 ; CHECK-NEXT: cmpl %esi, %edi
167 ; CHECK-NEXT: sbbl %eax, %eax
169 %cmp = icmp ule i32 %y, %x
170 %ext = zext i1 %cmp to i32
171 %add = add i32 %ext, -1
175 ; Verify that subtract with constant is the same thing.
176 ; (X >=u Y) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb
178 define i32 @uge_select_0_or_neg1_sub(i32 %x, i32 %y) nounwind {
179 ; CHECK-LABEL: uge_select_0_or_neg1_sub:
181 ; CHECK-NEXT: cmpl %esi, %edi
182 ; CHECK-NEXT: sbbl %eax, %eax
184 %cmp = icmp uge i32 %x, %y
185 %ext = zext i1 %cmp to i32
186 %sub = sub i32 %ext, 1
190 ; Check more sub-from-zero patterns.
191 ; (X >u Y) ? -1 : 0 --> cmp, sbb
193 define i64 @ugt_select_neg1_or_0_sub(i64 %x, i64 %y) nounwind {
194 ; CHECK-LABEL: ugt_select_neg1_or_0_sub:
196 ; CHECK-NEXT: cmpq %rdi, %rsi
197 ; CHECK-NEXT: sbbq %rax, %rax
199 %cmp = icmp ugt i64 %x, %y
200 %zext = zext i1 %cmp to i64
201 %sub = sub i64 0, %zext
205 ; Swap the predicate and compare operands:
206 ; (Y <u X) ? -1 : 0 --> cmp, sbb
208 define i16 @ult_select_neg1_or_0_sub(i16 %x, i16 %y) nounwind {
209 ; CHECK-LABEL: ult_select_neg1_or_0_sub:
211 ; CHECK-NEXT: cmpw %di, %si
212 ; CHECK-NEXT: sbbl %eax, %eax
213 ; CHECK-NEXT: # kill: def $ax killed $ax killed $eax
215 %cmp = icmp ult i16 %y, %x
216 %zext = zext i1 %cmp to i16
217 %sub = sub i16 0, %zext
223 ; Make sure we're creating nodes with the right value types. This would crash.
224 ; https://bugs.llvm.org/show_bug.cgi?id=33560
226 define void @PR33560(i8 %x, i64 %y) {
227 ; CHECK-LABEL: PR33560:
228 ; CHECK: # %bb.0: # %entry
229 ; CHECK-NEXT: negb %dil
230 ; CHECK-NEXT: sbbq %rax, %rax
231 ; CHECK-NEXT: cmpq %rsi, %rax
234 %cmp1 = icmp eq i8 %x, 0
235 %ext = zext i1 %cmp1 to i64
236 %add = add i64 %ext, -1
237 %cmp2 = icmp eq i64 %add, %y
238 br i1 %cmp2, label %end, label %else
241 %tmp7 = zext i1 %cmp1 to i8
249 define i32 @ult_zext_add(i32 %0, i32 %1, i32 %2) {
250 ; CHECK-LABEL: ult_zext_add:
252 ; CHECK-NEXT: movl %edi, %eax
253 ; CHECK-NEXT: cmpl %edx, %esi
254 ; CHECK-NEXT: adcl $0, %eax
256 %4 = icmp ult i32 %1, %2
257 %5 = zext i1 %4 to i32
258 %6 = add nsw i32 %5, %0
262 define i32 @ule_zext_add(i32 %0, i32 %1, i32 %2) {
263 ; CHECK-LABEL: ule_zext_add:
265 ; CHECK-NEXT: movl %edi, %eax
266 ; CHECK-NEXT: cmpl %esi, %edx
267 ; CHECK-NEXT: sbbl $-1, %eax
269 %4 = icmp ule i32 %1, %2
270 %5 = zext i1 %4 to i32
271 %6 = add nsw i32 %5, %0
275 define i32 @ugt_zext_add(i32 %0, i32 %1, i32 %2) {
276 ; CHECK-LABEL: ugt_zext_add:
278 ; CHECK-NEXT: movl %edi, %eax
279 ; CHECK-NEXT: cmpl %esi, %edx
280 ; CHECK-NEXT: adcl $0, %eax
282 %4 = icmp ugt i32 %1, %2
283 %5 = zext i1 %4 to i32
284 %6 = add nsw i32 %5, %0
288 define i32 @uge_zext_add(i32 %0, i32 %1, i32 %2) {
289 ; CHECK-LABEL: uge_zext_add:
291 ; CHECK-NEXT: movl %edi, %eax
292 ; CHECK-NEXT: cmpl %edx, %esi
293 ; CHECK-NEXT: sbbl $-1, %eax
295 %4 = icmp uge i32 %1, %2
296 %5 = zext i1 %4 to i32
297 %6 = add nsw i32 %5, %0
301 define i32 @ult_sext_add(i32 %0, i32 %1, i32 %2) {
302 ; CHECK-LABEL: ult_sext_add:
304 ; CHECK-NEXT: movl %edi, %eax
305 ; CHECK-NEXT: cmpl %edx, %esi
306 ; CHECK-NEXT: sbbl $0, %eax
308 %4 = icmp ult i32 %1, %2
309 %5 = sext i1 %4 to i32
310 %6 = add nsw i32 %5, %0
314 define i32 @ule_sext_add(i32 %0, i32 %1, i32 %2) {
315 ; CHECK-LABEL: ule_sext_add:
317 ; CHECK-NEXT: movl %edi, %eax
318 ; CHECK-NEXT: cmpl %esi, %edx
319 ; CHECK-NEXT: adcl $-1, %eax
321 %4 = icmp ule i32 %1, %2
322 %5 = sext i1 %4 to i32
323 %6 = add nsw i32 %5, %0
327 define i32 @ugt_sext_add(i32 %0, i32 %1, i32 %2) {
328 ; CHECK-LABEL: ugt_sext_add:
330 ; CHECK-NEXT: movl %edi, %eax
331 ; CHECK-NEXT: cmpl %esi, %edx
332 ; CHECK-NEXT: sbbl $0, %eax
334 %4 = icmp ugt i32 %1, %2
335 %5 = sext i1 %4 to i32
336 %6 = add nsw i32 %5, %0
340 define i32 @uge_sext_add(i32 %0, i32 %1, i32 %2) {
341 ; CHECK-LABEL: uge_sext_add:
343 ; CHECK-NEXT: movl %edi, %eax
344 ; CHECK-NEXT: cmpl %edx, %esi
345 ; CHECK-NEXT: adcl $-1, %eax
347 %4 = icmp uge i32 %1, %2
348 %5 = sext i1 %4 to i32
349 %6 = add nsw i32 %5, %0