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
5 ; Compare if negative and select of constants where one constant is zero.
7 define i32 @neg_sel_constants(i32 %a) {
8 ; CHECK-NOBMI-LABEL: neg_sel_constants:
9 ; CHECK-NOBMI: # %bb.0:
10 ; CHECK-NOBMI-NEXT: movl %edi, %eax
11 ; CHECK-NOBMI-NEXT: sarl $31, %eax
12 ; CHECK-NOBMI-NEXT: andl $5, %eax
13 ; CHECK-NOBMI-NEXT: retq
15 ; CHECK-BMI-LABEL: neg_sel_constants:
17 ; CHECK-BMI-NEXT: movl %edi, %eax
18 ; CHECK-BMI-NEXT: sarl $31, %eax
19 ; CHECK-BMI-NEXT: andl $5, %eax
20 ; CHECK-BMI-NEXT: retq
21 %tmp.1 = icmp slt i32 %a, 0
22 %retval = select i1 %tmp.1, i32 5, i32 0
26 ; Compare if negative and select of constants where one constant is zero and the other is a single bit.
28 define i32 @neg_sel_special_constant(i32 %a) {
29 ; CHECK-NOBMI-LABEL: neg_sel_special_constant:
30 ; CHECK-NOBMI: # %bb.0:
31 ; CHECK-NOBMI-NEXT: movl %edi, %eax
32 ; CHECK-NOBMI-NEXT: shrl $22, %eax
33 ; CHECK-NOBMI-NEXT: andl $512, %eax # imm = 0x200
34 ; CHECK-NOBMI-NEXT: retq
36 ; CHECK-BMI-LABEL: neg_sel_special_constant:
38 ; CHECK-BMI-NEXT: movl %edi, %eax
39 ; CHECK-BMI-NEXT: shrl $22, %eax
40 ; CHECK-BMI-NEXT: andl $512, %eax # imm = 0x200
41 ; CHECK-BMI-NEXT: retq
42 %tmp.1 = icmp slt i32 %a, 0
43 %retval = select i1 %tmp.1, i32 512, i32 0
47 ; Compare if negative and select variable or zero.
49 define 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: movl %edi, %eax
53 ; CHECK-NOBMI-NEXT: sarl $31, %eax
54 ; CHECK-NOBMI-NEXT: andl %esi, %eax
55 ; CHECK-NOBMI-NEXT: retq
57 ; CHECK-BMI-LABEL: neg_sel_variable_and_zero:
59 ; CHECK-BMI-NEXT: movl %edi, %eax
60 ; CHECK-BMI-NEXT: sarl $31, %eax
61 ; CHECK-BMI-NEXT: andl %esi, %eax
62 ; CHECK-BMI-NEXT: retq
63 %tmp.1 = icmp slt i32 %a, 0
64 %retval = select i1 %tmp.1, i32 %b, i32 0
68 ; Compare if not positive and select the same variable as being compared: smin(a, 0).
70 define 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
78 ; CHECK-BMI-LABEL: not_pos_sel_same_variable:
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
89 ; Flipping the comparison condition can be handled by getting the bitwise not of the sign mask.
91 ; Compare if positive and select of constants where one constant is zero.
93 define 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
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
114 ; Compare if positive and select of constants where one constant is zero and the other is a single bit.
116 define i32 @pos_sel_special_constant(i32 %a) {
117 ; CHECK-NOBMI-LABEL: pos_sel_special_constant:
118 ; CHECK-NOBMI: # %bb.0:
119 ; CHECK-NOBMI-NEXT: movl %edi, %eax
120 ; CHECK-NOBMI-NEXT: notl %eax
121 ; CHECK-NOBMI-NEXT: shrl $22, %eax
122 ; CHECK-NOBMI-NEXT: andl $512, %eax # imm = 0x200
123 ; CHECK-NOBMI-NEXT: retq
125 ; CHECK-BMI-LABEL: pos_sel_special_constant:
126 ; CHECK-BMI: # %bb.0:
127 ; CHECK-BMI-NEXT: movl %edi, %eax
128 ; CHECK-BMI-NEXT: notl %eax
129 ; CHECK-BMI-NEXT: shrl $22, %eax
130 ; CHECK-BMI-NEXT: andl $512, %eax # imm = 0x200
131 ; CHECK-BMI-NEXT: retq
132 %tmp.1 = icmp sgt i32 %a, -1
133 %retval = select i1 %tmp.1, i32 512, i32 0
137 ; Compare if positive and select variable or zero.
139 define 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
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
157 ; Compare if not negative or zero and select the same variable as being compared: smax(a, 0).
159 define 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
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
178 ; https://llvm.org/bugs/show_bug.cgi?id=31175
180 ; ret = (x-y) > 0 ? x-y : 0
181 define 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
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