1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 2; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s 3 4; Vary the operand sizes for extra coverage, but the transform should be identical in all cases. 5 6; (X == 0) ? 0 : -1 --> (X == 0) - 1 7 8define i8 @i8_select_0_or_neg1(i8 %x) { 9; CHECK-LABEL: i8_select_0_or_neg1: 10; CHECK: # %bb.0: 11; CHECK-NEXT: negb %dil 12; CHECK-NEXT: sbbb %al, %al 13; CHECK-NEXT: retq 14 %cmp = icmp eq i8 %x, 0 15 %sel = select i1 %cmp, i8 0, i8 -1 16 ret i8 %sel 17} 18 19; (X == 0) ? 0 : -1 --> (X == 0) - 1 20 21define i16 @i16_select_0_or_neg1_as_math(i16 %x) { 22; CHECK-LABEL: i16_select_0_or_neg1_as_math: 23; CHECK: # %bb.0: 24; CHECK-NEXT: negw %di 25; CHECK-NEXT: sbbw %ax, %ax 26; CHECK-NEXT: retq 27 %cmp = icmp eq i16 %x, 0 28 %ext = zext i1 %cmp to i16 29 %add = add i16 %ext, -1 30 ret i16 %add 31} 32 33; (X != 0) ? -1 : 0 --> 0 - (X != 0) 34 35define i32 @i32_select_0_or_neg1_commuted(i32 %x) { 36; CHECK-LABEL: i32_select_0_or_neg1_commuted: 37; CHECK: # %bb.0: 38; CHECK-NEXT: negl %edi 39; CHECK-NEXT: sbbl %eax, %eax 40; CHECK-NEXT: retq 41 %cmp = icmp ne i32 %x, 0 42 %sel = select i1 %cmp, i32 -1, i32 0 43 ret i32 %sel 44} 45 46; (X != 0) ? -1 : 0 --> 0 - (X != 0) 47 48define i64 @i64_select_0_or_neg1_commuted_as_math(i64 %x) { 49; CHECK-LABEL: i64_select_0_or_neg1_commuted_as_math: 50; CHECK: # %bb.0: 51; CHECK-NEXT: negq %rdi 52; CHECK-NEXT: sbbq %rax, %rax 53; CHECK-NEXT: retq 54 %cmp = icmp ne i64 %x, 0 55 %ext = zext i1 %cmp to i64 56 %sub = sub i64 0, %ext 57 ret i64 %sub 58} 59 60; (X == 0) ? -1 : 0 --> 0 - (X == 0) 61 62define i64 @i64_select_neg1_or_0(i64 %x) { 63; CHECK-LABEL: i64_select_neg1_or_0: 64; CHECK: # %bb.0: 65; CHECK-NEXT: cmpq $1, %rdi 66; CHECK-NEXT: sbbq %rax, %rax 67; CHECK-NEXT: retq 68 %cmp = icmp eq i64 %x, 0 69 %sel = select i1 %cmp, i64 -1, i64 0 70 ret i64 %sel 71} 72 73; (X == 0) ? -1 : 0 --> 0 - (X == 0) 74 75define i32 @i32_select_neg1_or_0_as_math(i32 %x) { 76; CHECK-LABEL: i32_select_neg1_or_0_as_math: 77; CHECK: # %bb.0: 78; CHECK-NEXT: cmpl $1, %edi 79; CHECK-NEXT: sbbl %eax, %eax 80; CHECK-NEXT: retq 81 %cmp = icmp eq i32 %x, 0 82 %ext = zext i1 %cmp to i32 83 %sub = sub i32 0, %ext 84 ret i32 %sub 85} 86 87; (X != 0) ? 0 : -1 --> (X != 0) - 1 88 89define i16 @i16_select_neg1_or_0_commuted(i16 %x) { 90; CHECK-LABEL: i16_select_neg1_or_0_commuted: 91; CHECK: # %bb.0: 92; CHECK-NEXT: cmpw $1, %di 93; CHECK-NEXT: sbbw %ax, %ax 94; CHECK-NEXT: retq 95 %cmp = icmp ne i16 %x, 0 96 %sel = select i1 %cmp, i16 0, i16 -1 97 ret i16 %sel 98} 99 100; (X != 0) ? 0 : -1 --> (X != 0) - 1 101 102define i8 @i8_select_neg1_or_0_commuted_as_math(i8 %x) { 103; CHECK-LABEL: i8_select_neg1_or_0_commuted_as_math: 104; CHECK: # %bb.0: 105; CHECK-NEXT: cmpb $1, %dil 106; CHECK-NEXT: sbbb %al, %al 107; CHECK-NEXT: retq 108 %cmp = icmp ne i8 %x, 0 109 %ext = zext i1 %cmp to i8 110 %add = add i8 %ext, -1 111 ret i8 %add 112} 113 114; (X <u Y) ? -1 : 0 --> cmp, sbb 115 116define i32 @ult_select_neg1_or_0(i32 %x, i32 %y) nounwind { 117; CHECK-LABEL: ult_select_neg1_or_0: 118; CHECK: # %bb.0: 119; CHECK-NEXT: cmpl %esi, %edi 120; CHECK-NEXT: sbbl %eax, %eax 121; CHECK-NEXT: retq 122 %cmp = icmp ult i32 %x, %y 123 %ext = sext i1 %cmp to i32 124 ret i32 %ext 125} 126 127; Swap the predicate and compare operands: 128; (Y >u X) ? -1 : 0 --> cmp, sbb 129 130define i32 @ugt_select_neg1_or_0(i32 %x, i32 %y) nounwind { 131; CHECK-LABEL: ugt_select_neg1_or_0: 132; CHECK: # %bb.0: 133; CHECK-NEXT: cmpl %esi, %edi 134; CHECK-NEXT: sbbl %eax, %eax 135; CHECK-NEXT: retq 136 %cmp = icmp ugt i32 %y, %x 137 %ext = sext i1 %cmp to i32 138 ret i32 %ext 139} 140 141; Invert the predicate and effectively swap the select operands: 142; (X >=u Y) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb 143 144define i32 @uge_select_0_or_neg1(i32 %x, i32 %y) nounwind { 145; CHECK-LABEL: uge_select_0_or_neg1: 146; CHECK: # %bb.0: 147; CHECK-NEXT: cmpl %esi, %edi 148; CHECK-NEXT: sbbl %eax, %eax 149; CHECK-NEXT: retq 150 %cmp = icmp uge i32 %x, %y 151 %ext = zext i1 %cmp to i32 152 %add = add i32 %ext, -1 153 ret i32 %add 154} 155 156; Swap the predicate and compare operands: 157; (Y <=u X) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb 158 159define i32 @ule_select_0_or_neg1(i32 %x, i32 %y) nounwind { 160; CHECK-LABEL: ule_select_0_or_neg1: 161; CHECK: # %bb.0: 162; CHECK-NEXT: cmpl %esi, %edi 163; CHECK-NEXT: sbbl %eax, %eax 164; CHECK-NEXT: retq 165 %cmp = icmp ule i32 %y, %x 166 %ext = zext i1 %cmp to i32 167 %add = add i32 %ext, -1 168 ret i32 %add 169} 170 171; Verify that subtract with constant is the same thing. 172; (X >=u Y) ? 0 : -1 --> (X <u Y) ? -1 : 0 --> cmp, sbb 173 174define i32 @uge_select_0_or_neg1_sub(i32 %x, i32 %y) nounwind { 175; CHECK-LABEL: uge_select_0_or_neg1_sub: 176; CHECK: # %bb.0: 177; CHECK-NEXT: cmpl %esi, %edi 178; CHECK-NEXT: sbbl %eax, %eax 179; CHECK-NEXT: retq 180 %cmp = icmp uge i32 %x, %y 181 %ext = zext i1 %cmp to i32 182 %sub = sub i32 %ext, 1 183 ret i32 %sub 184} 185 186; Check more sub-from-zero patterns. 187; (X >u Y) ? -1 : 0 --> cmp, sbb 188 189define i64 @ugt_select_neg1_or_0_sub(i64 %x, i64 %y) nounwind { 190; CHECK-LABEL: ugt_select_neg1_or_0_sub: 191; CHECK: # %bb.0: 192; CHECK-NEXT: cmpq %rdi, %rsi 193; CHECK-NEXT: sbbq %rax, %rax 194; CHECK-NEXT: retq 195 %cmp = icmp ugt i64 %x, %y 196 %zext = zext i1 %cmp to i64 197 %sub = sub i64 0, %zext 198 ret i64 %sub 199} 200 201; Swap the predicate and compare operands: 202; (Y <u X) ? -1 : 0 --> cmp, sbb 203 204define i16 @ult_select_neg1_or_0_sub(i16 %x, i16 %y) nounwind { 205; CHECK-LABEL: ult_select_neg1_or_0_sub: 206; CHECK: # %bb.0: 207; CHECK-NEXT: cmpw %di, %si 208; CHECK-NEXT: sbbw %ax, %ax 209; CHECK-NEXT: retq 210 %cmp = icmp ult i16 %y, %x 211 %zext = zext i1 %cmp to i16 212 %sub = sub i16 0, %zext 213 ret i16 %sub 214} 215 216 217 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 220 221define 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 227; CHECK-NEXT: retq 228entry: 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 234 235else: 236 %tmp7 = zext i1 %cmp1 to i8 237 br label %end 238 239end: 240 ret void 241} 242 243