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: sbbb %al, %al
14 %cmp = icmp eq i8 %x, 0
15 %sel = select i1 %cmp, i8 0, i8 -1
19 ; (X == 0) ? 0 : -1 --> (X == 0) - 1
21 define i16 @i16_select_0_or_neg1_as_math(i16 %x) {
22 ; CHECK-LABEL: i16_select_0_or_neg1_as_math:
24 ; CHECK-NEXT: negw %di
25 ; CHECK-NEXT: sbbw %ax, %ax
27 %cmp = icmp eq i16 %x, 0
28 %ext = zext i1 %cmp to i16
29 %add = add i16 %ext, -1
33 ; (X != 0) ? -1 : 0 --> 0 - (X != 0)
35 define i32 @i32_select_0_or_neg1_commuted(i32 %x) {
36 ; CHECK-LABEL: i32_select_0_or_neg1_commuted:
38 ; CHECK-NEXT: negl %edi
39 ; CHECK-NEXT: sbbl %eax, %eax
41 %cmp = icmp ne i32 %x, 0
42 %sel = select i1 %cmp, i32 -1, i32 0
46 ; (X != 0) ? -1 : 0 --> 0 - (X != 0)
48 define i64 @i64_select_0_or_neg1_commuted_as_math(i64 %x) {
49 ; CHECK-LABEL: i64_select_0_or_neg1_commuted_as_math:
51 ; CHECK-NEXT: negq %rdi
52 ; CHECK-NEXT: sbbq %rax, %rax
54 %cmp = icmp ne i64 %x, 0
55 %ext = zext i1 %cmp to i64
56 %sub = sub i64 0, %ext
60 ; (X == 0) ? -1 : 0 --> 0 - (X == 0)
62 define i64 @i64_select_neg1_or_0(i64 %x) {
63 ; CHECK-LABEL: i64_select_neg1_or_0:
65 ; CHECK-NEXT: cmpq $1, %rdi
66 ; CHECK-NEXT: sbbq %rax, %rax
68 %cmp = icmp eq i64 %x, 0
69 %sel = select i1 %cmp, i64 -1, i64 0
73 ; (X == 0) ? -1 : 0 --> 0 - (X == 0)
75 define i32 @i32_select_neg1_or_0_as_math(i32 %x) {
76 ; CHECK-LABEL: i32_select_neg1_or_0_as_math:
78 ; CHECK-NEXT: cmpl $1, %edi
79 ; CHECK-NEXT: sbbl %eax, %eax
81 %cmp = icmp eq i32 %x, 0
82 %ext = zext i1 %cmp to i32
83 %sub = sub i32 0, %ext
87 ; (X != 0) ? 0 : -1 --> (X != 0) - 1
89 define i16 @i16_select_neg1_or_0_commuted(i16 %x) {
90 ; CHECK-LABEL: i16_select_neg1_or_0_commuted:
92 ; CHECK-NEXT: cmpw $1, %di
93 ; CHECK-NEXT: sbbw %ax, %ax
95 %cmp = icmp ne i16 %x, 0
96 %sel = select i1 %cmp, i16 0, i16 -1
100 ; (X != 0) ? 0 : -1 --> (X != 0) - 1
102 define i8 @i8_select_neg1_or_0_commuted_as_math(i8 %x) {
103 ; CHECK-LABEL: i8_select_neg1_or_0_commuted_as_math:
105 ; CHECK-NEXT: cmpb $1, %dil
106 ; CHECK-NEXT: sbbb %al, %al
108 %cmp = icmp ne i8 %x, 0
109 %ext = zext i1 %cmp to i8
110 %add = add i8 %ext, -1
114 ; (X <u Y) ? -1 : 0 --> cmp, sbb
116 define i32 @ult_select_neg1_or_0(i32 %x, i32 %y) nounwind {
117 ; CHECK-LABEL: ult_select_neg1_or_0:
119 ; CHECK-NEXT: cmpl %esi, %edi
120 ; CHECK-NEXT: sbbl %eax, %eax
122 %cmp = icmp ult i32 %x, %y
123 %ext = sext i1 %cmp to i32
127 ; Swap the predicate and compare operands:
128 ; (Y >u X) ? -1 : 0 --> cmp, sbb
130 define i32 @ugt_select_neg1_or_0(i32 %x, i32 %y) nounwind {
131 ; CHECK-LABEL: ugt_select_neg1_or_0:
133 ; CHECK-NEXT: cmpl %esi, %edi
134 ; CHECK-NEXT: sbbl %eax, %eax
136 %cmp = icmp ugt i32 %y, %x
137 %ext = sext i1 %cmp to i32
141 ; Invert the predicate and effectively swap the select operands:
142 ; (X >=u Y) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb
144 define i32 @uge_select_0_or_neg1(i32 %x, i32 %y) nounwind {
145 ; CHECK-LABEL: uge_select_0_or_neg1:
147 ; CHECK-NEXT: cmpl %esi, %edi
148 ; CHECK-NEXT: sbbl %eax, %eax
150 %cmp = icmp uge i32 %x, %y
151 %ext = zext i1 %cmp to i32
152 %add = add i32 %ext, -1
156 ; Swap the predicate and compare operands:
157 ; (Y <=u X) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb
159 define i32 @ule_select_0_or_neg1(i32 %x, i32 %y) nounwind {
160 ; CHECK-LABEL: ule_select_0_or_neg1:
162 ; CHECK-NEXT: cmpl %esi, %edi
163 ; CHECK-NEXT: sbbl %eax, %eax
165 %cmp = icmp ule i32 %y, %x
166 %ext = zext i1 %cmp to i32
167 %add = add i32 %ext, -1
171 ; Verify that subtract with constant is the same thing.
172 ; (X >=u Y) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb
174 define i32 @uge_select_0_or_neg1_sub(i32 %x, i32 %y) nounwind {
175 ; CHECK-LABEL: uge_select_0_or_neg1_sub:
177 ; CHECK-NEXT: cmpl %esi, %edi
178 ; CHECK-NEXT: sbbl %eax, %eax
180 %cmp = icmp uge i32 %x, %y
181 %ext = zext i1 %cmp to i32
182 %sub = sub i32 %ext, 1
186 ; Check more sub-from-zero patterns.
187 ; (X >u Y) ? -1 : 0 --> cmp, sbb
189 define i64 @ugt_select_neg1_or_0_sub(i64 %x, i64 %y) nounwind {
190 ; CHECK-LABEL: ugt_select_neg1_or_0_sub:
192 ; CHECK-NEXT: cmpq %rdi, %rsi
193 ; CHECK-NEXT: sbbq %rax, %rax
195 %cmp = icmp ugt i64 %x, %y
196 %zext = zext i1 %cmp to i64
197 %sub = sub i64 0, %zext
201 ; Swap the predicate and compare operands:
202 ; (Y <u X) ? -1 : 0 --> cmp, sbb
204 define i16 @ult_select_neg1_or_0_sub(i16 %x, i16 %y) nounwind {
205 ; CHECK-LABEL: ult_select_neg1_or_0_sub:
207 ; CHECK-NEXT: cmpw %di, %si
208 ; CHECK-NEXT: sbbw %ax, %ax
210 %cmp = icmp ult i16 %y, %x
211 %zext = zext i1 %cmp to i16
212 %sub = sub i16 0, %zext
218 ; Make sure we're creating nodes with the right value types. This would crash.
219 ; https://bugs.llvm.org/show_bug.cgi?id=33560
221 define void @PR33560(i8 %x, i64 %y) {
222 ; CHECK-LABEL: PR33560:
223 ; CHECK: # %bb.0: # %entry
224 ; CHECK-NEXT: negb %dil
225 ; CHECK-NEXT: sbbq %rax, %rax
226 ; CHECK-NEXT: cmpq %rsi, %rax
229 %cmp1 = icmp eq i8 %x, 0
230 %ext = zext i1 %cmp1 to i64
231 %add = add i64 %ext, -1
232 %cmp2 = icmp eq i64 %add, %y
233 br i1 %cmp2, label %end, label %else
236 %tmp7 = zext i1 %cmp1 to i8