1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 2; RUN: llc -mtriple=x86_64-unknown-linux-gnu -mattr=-bmi < %s | FileCheck %s --check-prefix=CHECK-NOBMI 3; RUN: llc -mtriple=x86_64-unknown-linux-gnu -mattr=+bmi < %s | FileCheck %s --check-prefix=CHECK-BMI 4 5; Compare if negative and select of constants where one constant is zero. 6 7define i32 @neg_sel_constants(i32 %a) { 8; CHECK-NOBMI-LABEL: neg_sel_constants: 9; CHECK-NOBMI: # %bb.0: 10; CHECK-NOBMI-NEXT: sarl $31, %edi 11; CHECK-NOBMI-NEXT: andl $5, %edi 12; CHECK-NOBMI-NEXT: movl %edi, %eax 13; CHECK-NOBMI-NEXT: retq 14; 15; CHECK-BMI-LABEL: neg_sel_constants: 16; CHECK-BMI: # %bb.0: 17; CHECK-BMI-NEXT: sarl $31, %edi 18; CHECK-BMI-NEXT: andl $5, %edi 19; CHECK-BMI-NEXT: movl %edi, %eax 20; CHECK-BMI-NEXT: retq 21 %tmp.1 = icmp slt i32 %a, 0 22 %retval = select i1 %tmp.1, i32 5, i32 0 23 ret i32 %retval 24} 25 26; Compare if negative and select of constants where one constant is zero and the other is a single bit. 27 28define i32 @neg_sel_special_constant(i32 %a) { 29; CHECK-NOBMI-LABEL: neg_sel_special_constant: 30; CHECK-NOBMI: # %bb.0: 31; CHECK-NOBMI-NEXT: shrl $22, %edi 32; CHECK-NOBMI-NEXT: andl $512, %edi # imm = 0x200 33; CHECK-NOBMI-NEXT: movl %edi, %eax 34; CHECK-NOBMI-NEXT: retq 35; 36; CHECK-BMI-LABEL: neg_sel_special_constant: 37; CHECK-BMI: # %bb.0: 38; CHECK-BMI-NEXT: shrl $22, %edi 39; CHECK-BMI-NEXT: andl $512, %edi # imm = 0x200 40; CHECK-BMI-NEXT: movl %edi, %eax 41; CHECK-BMI-NEXT: retq 42 %tmp.1 = icmp slt i32 %a, 0 43 %retval = select i1 %tmp.1, i32 512, i32 0 44 ret i32 %retval 45} 46 47; Compare if negative and select variable or zero. 48 49define i32 @neg_sel_variable_and_zero(i32 %a, i32 %b) { 50; CHECK-NOBMI-LABEL: neg_sel_variable_and_zero: 51; CHECK-NOBMI: # %bb.0: 52; CHECK-NOBMI-NEXT: sarl $31, %edi 53; CHECK-NOBMI-NEXT: andl %esi, %edi 54; CHECK-NOBMI-NEXT: movl %edi, %eax 55; CHECK-NOBMI-NEXT: retq 56; 57; CHECK-BMI-LABEL: neg_sel_variable_and_zero: 58; CHECK-BMI: # %bb.0: 59; CHECK-BMI-NEXT: sarl $31, %edi 60; CHECK-BMI-NEXT: andl %esi, %edi 61; CHECK-BMI-NEXT: movl %edi, %eax 62; CHECK-BMI-NEXT: retq 63 %tmp.1 = icmp slt i32 %a, 0 64 %retval = select i1 %tmp.1, i32 %b, i32 0 65 ret i32 %retval 66} 67 68; Compare if not positive and select the same variable as being compared: smin(a, 0). 69 70define i32 @not_pos_sel_same_variable(i32 %a) { 71; CHECK-NOBMI-LABEL: not_pos_sel_same_variable: 72; CHECK-NOBMI: # %bb.0: 73; CHECK-NOBMI-NEXT: movl %edi, %eax 74; CHECK-NOBMI-NEXT: sarl $31, %eax 75; CHECK-NOBMI-NEXT: andl %edi, %eax 76; CHECK-NOBMI-NEXT: retq 77; 78; CHECK-BMI-LABEL: not_pos_sel_same_variable: 79; CHECK-BMI: # %bb.0: 80; CHECK-BMI-NEXT: movl %edi, %eax 81; CHECK-BMI-NEXT: sarl $31, %eax 82; CHECK-BMI-NEXT: andl %edi, %eax 83; CHECK-BMI-NEXT: retq 84 %tmp = icmp slt i32 %a, 1 85 %min = select i1 %tmp, i32 %a, i32 0 86 ret i32 %min 87} 88 89; Flipping the comparison condition can be handled by getting the bitwise not of the sign mask. 90 91; Compare if positive and select of constants where one constant is zero. 92 93define i32 @pos_sel_constants(i32 %a) { 94; CHECK-NOBMI-LABEL: pos_sel_constants: 95; CHECK-NOBMI: # %bb.0: 96; CHECK-NOBMI-NEXT: # kill: def $edi killed $edi def $rdi 97; CHECK-NOBMI-NEXT: notl %edi 98; CHECK-NOBMI-NEXT: shrl $31, %edi 99; CHECK-NOBMI-NEXT: leal (%rdi,%rdi,4), %eax 100; CHECK-NOBMI-NEXT: retq 101; 102; CHECK-BMI-LABEL: pos_sel_constants: 103; CHECK-BMI: # %bb.0: 104; CHECK-BMI-NEXT: # kill: def $edi killed $edi def $rdi 105; CHECK-BMI-NEXT: notl %edi 106; CHECK-BMI-NEXT: shrl $31, %edi 107; CHECK-BMI-NEXT: leal (%rdi,%rdi,4), %eax 108; CHECK-BMI-NEXT: retq 109 %tmp.1 = icmp sgt i32 %a, -1 110 %retval = select i1 %tmp.1, i32 5, i32 0 111 ret i32 %retval 112} 113 114; Compare if positive and select of constants where one constant is zero and the other is a single bit. 115 116define i32 @pos_sel_special_constant(i32 %a) { 117; CHECK-NOBMI-LABEL: pos_sel_special_constant: 118; CHECK-NOBMI: # %bb.0: 119; CHECK-NOBMI-NEXT: notl %edi 120; CHECK-NOBMI-NEXT: shrl $22, %edi 121; CHECK-NOBMI-NEXT: andl $512, %edi # imm = 0x200 122; CHECK-NOBMI-NEXT: movl %edi, %eax 123; CHECK-NOBMI-NEXT: retq 124; 125; CHECK-BMI-LABEL: pos_sel_special_constant: 126; CHECK-BMI: # %bb.0: 127; CHECK-BMI-NEXT: notl %edi 128; CHECK-BMI-NEXT: shrl $22, %edi 129; CHECK-BMI-NEXT: andl $512, %edi # imm = 0x200 130; CHECK-BMI-NEXT: movl %edi, %eax 131; CHECK-BMI-NEXT: retq 132 %tmp.1 = icmp sgt i32 %a, -1 133 %retval = select i1 %tmp.1, i32 512, i32 0 134 ret i32 %retval 135} 136 137; Compare if positive and select variable or zero. 138 139define i32 @pos_sel_variable_and_zero(i32 %a, i32 %b) { 140; CHECK-NOBMI-LABEL: pos_sel_variable_and_zero: 141; CHECK-NOBMI: # %bb.0: 142; CHECK-NOBMI-NEXT: xorl %eax, %eax 143; CHECK-NOBMI-NEXT: testl %edi, %edi 144; CHECK-NOBMI-NEXT: cmovnsl %esi, %eax 145; CHECK-NOBMI-NEXT: retq 146; 147; CHECK-BMI-LABEL: pos_sel_variable_and_zero: 148; CHECK-BMI: # %bb.0: 149; CHECK-BMI-NEXT: sarl $31, %edi 150; CHECK-BMI-NEXT: andnl %esi, %edi, %eax 151; CHECK-BMI-NEXT: retq 152 %tmp.1 = icmp sgt i32 %a, -1 153 %retval = select i1 %tmp.1, i32 %b, i32 0 154 ret i32 %retval 155} 156 157; Compare if not negative or zero and select the same variable as being compared: smax(a, 0). 158 159define i32 @not_neg_sel_same_variable(i32 %a) { 160; CHECK-NOBMI-LABEL: not_neg_sel_same_variable: 161; CHECK-NOBMI: # %bb.0: 162; CHECK-NOBMI-NEXT: xorl %eax, %eax 163; CHECK-NOBMI-NEXT: testl %edi, %edi 164; CHECK-NOBMI-NEXT: cmovnsl %edi, %eax 165; CHECK-NOBMI-NEXT: retq 166; 167; CHECK-BMI-LABEL: not_neg_sel_same_variable: 168; CHECK-BMI: # %bb.0: 169; CHECK-BMI-NEXT: movl %edi, %eax 170; CHECK-BMI-NEXT: sarl $31, %eax 171; CHECK-BMI-NEXT: andnl %edi, %eax, %eax 172; CHECK-BMI-NEXT: retq 173 %tmp = icmp sgt i32 %a, 0 174 %min = select i1 %tmp, i32 %a, i32 0 175 ret i32 %min 176} 177 178; https://llvm.org/bugs/show_bug.cgi?id=31175 179 180; ret = (x-y) > 0 ? x-y : 0 181define i32 @PR31175(i32 %x, i32 %y) { 182; CHECK-NOBMI-LABEL: PR31175: 183; CHECK-NOBMI: # %bb.0: 184; CHECK-NOBMI-NEXT: xorl %eax, %eax 185; CHECK-NOBMI-NEXT: subl %esi, %edi 186; CHECK-NOBMI-NEXT: cmovnsl %edi, %eax 187; CHECK-NOBMI-NEXT: retq 188; 189; CHECK-BMI-LABEL: PR31175: 190; CHECK-BMI: # %bb.0: 191; CHECK-BMI-NEXT: subl %esi, %edi 192; CHECK-BMI-NEXT: movl %edi, %eax 193; CHECK-BMI-NEXT: sarl $31, %eax 194; CHECK-BMI-NEXT: andnl %edi, %eax, %eax 195; CHECK-BMI-NEXT: retq 196 %sub = sub nsw i32 %x, %y 197 %cmp = icmp sgt i32 %sub, 0 198 %sel = select i1 %cmp, i32 %sub, i32 0 199 ret i32 %sel 200} 201