1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; and enhanced to include metadata checking.
4 ; RUN: opt < %s -instcombine -S | FileCheck %s
6 define i32 @foo(i32) local_unnamed_addr #0 {
8 ; CHECK-NEXT: [[TMP2:%.*]] = icmp sgt i32 %0, 2
9 ; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[TMP2]], i32 20, i32 -20, !prof ![[$MD1:[0-9]+]]
10 ; CHECK-NEXT: [[TMP3:%.*]] = add i32 [[DOTV]], %0
11 ; CHECK-NEXT: ret i32 [[TMP3]]
13 %2 = icmp sgt i32 %0, 2
14 %3 = add nsw i32 %0, 20
16 select i1 %2, i32 %3, i32 %4, !prof !1
20 define i8 @shrink_select(i1 %cond, i32 %x) {
21 ; CHECK-LABEL: @shrink_select(
22 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 %x to i8
23 ; CHECK-NEXT: [[TRUNC:%.*]] = select i1 %cond, i8 [[TMP1]], i8 42, !prof ![[$MD1]]
24 ; CHECK-NEXT: ret i8 [[TRUNC]]
26 %sel = select i1 %cond, i32 %x, i32 42, !prof !1
27 %trunc = trunc i32 %sel to i8
31 define void @min_max_bitcast(<4 x float> %a, <4 x float> %b, <4 x i32>* %ptr1, <4 x i32>* %ptr2) {
32 ; CHECK-LABEL: @min_max_bitcast(
33 ; CHECK-NEXT: [[CMP:%.*]] = fcmp olt <4 x float> %a, %b
34 ; CHECK-NEXT: [[SEL1_V:%.*]] = select <4 x i1> [[CMP]], <4 x float> %a, <4 x float> %b, !prof ![[$MD1]]
35 ; CHECK-NEXT: [[SEL2_V:%.*]] = select <4 x i1> [[CMP]], <4 x float> %b, <4 x float> %a, !prof ![[$MD1]]
36 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast <4 x i32>* %ptr1 to <4 x float>*
37 ; CHECK-NEXT: store <4 x float> [[SEL1_V]], <4 x float>* [[TMP1]], align 16
38 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast <4 x i32>* %ptr2 to <4 x float>*
39 ; CHECK-NEXT: store <4 x float> [[SEL2_V]], <4 x float>* [[TMP2]], align 16
40 ; CHECK-NEXT: ret void
42 %cmp = fcmp olt <4 x float> %a, %b
43 %bc1 = bitcast <4 x float> %a to <4 x i32>
44 %bc2 = bitcast <4 x float> %b to <4 x i32>
45 %sel1 = select <4 x i1> %cmp, <4 x i32> %bc1, <4 x i32> %bc2, !prof !1
46 %sel2 = select <4 x i1> %cmp, <4 x i32> %bc2, <4 x i32> %bc1, !prof !1
47 store <4 x i32> %sel1, <4 x i32>* %ptr1
48 store <4 x i32> %sel2, <4 x i32>* %ptr2
52 define i32 @foo2(i32, i32) local_unnamed_addr #0 {
54 ; CHECK-NEXT: [[TMP3:%.*]] = icmp sgt i32 %0, 2
55 ; CHECK-NEXT: [[TMP4:%.*]] = sub i32 0, %1
56 ; CHECK-NEXT: [[DOTP:%.*]] = select i1 [[TMP3]], i32 %1, i32 [[TMP4]], !prof ![[$MD1]]
57 ; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[DOTP]], %0
58 ; CHECK-NEXT: ret i32 [[TMP5]]
60 %3 = icmp sgt i32 %0, 2
61 %4 = add nsw i32 %0, %1
62 %5 = sub nsw i32 %0, %1
63 select i1 %3, i32 %4, i32 %5, !prof !1
67 define i64 @test43(i32 %a) nounwind {
68 ; CHECK-LABEL: @test43(
69 ; CHECK-NEXT: [[A_EXT:%.*]] = sext i32 %a to i64
70 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i64 [[A_EXT]], 0
71 ; CHECK-NEXT: [[MAX:%.*]] = select i1 [[TMP1]], i64 [[A_EXT]], i64 0, !prof ![[$MD1]]
72 ; CHECK-NEXT: ret i64 [[MAX]]
74 %a_ext = sext i32 %a to i64
75 %is_a_nonnegative = icmp sgt i32 %a, -1
76 %max = select i1 %is_a_nonnegative, i64 %a_ext, i64 0, !prof !1
80 define <2 x i32> @scalar_select_of_vectors_sext(<2 x i1> %cca, i1 %ccb) {
81 ; CHECK-LABEL: @scalar_select_of_vectors_sext(
82 ; CHECK-NEXT: [[NARROW:%.*]] = select i1 %ccb, <2 x i1> %cca, <2 x i1> zeroinitializer, !prof ![[$MD1]]
83 ; CHECK-NEXT: [[R:%.*]] = sext <2 x i1> [[NARROW]] to <2 x i32>
84 ; CHECK-NEXT: ret <2 x i32> [[R]]
86 %ccax = sext <2 x i1> %cca to <2 x i32>
87 %r = select i1 %ccb, <2 x i32> %ccax, <2 x i32> <i32 0, i32 0>, !prof !1
92 define i16 @t7(i32 %a) {
94 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 %a, -32768
95 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 %a, i32 -32768, !prof ![[$MD1]]
96 ; CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[TMP2]] to i16
97 ; CHECK-NEXT: ret i16 [[TMP3]]
99 %1 = icmp slt i32 %a, -32768
100 %2 = trunc i32 %a to i16
101 %3 = select i1 %1, i16 %2, i16 -32768, !prof !1
105 define i32 @abs_nabs_x01(i32 %x) {
106 ; CHECK-LABEL: @abs_nabs_x01(
107 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 %x, 0
108 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 0, %x
109 ; CHECK-NEXT: [[COND1:%.*]] = select i1 [[CMP]], i32 [[SUB]], i32 %x, !prof ![[$MD3:[0-9]+]]
110 ; CHECK-NEXT: ret i32 [[COND1]]
112 %cmp = icmp sgt i32 %x, -1
113 %sub = sub nsw i32 0, %x
114 %cond = select i1 %cmp, i32 %sub, i32 %x, !prof !1
115 %cmp1 = icmp sgt i32 %cond, -1
116 %sub16 = sub nsw i32 0, %cond
117 %cond18 = select i1 %cmp1, i32 %cond, i32 %sub16, !prof !2
121 ; Swap predicate / metadata order
123 define <2 x i32> @abs_nabs_x01_vec(<2 x i32> %x) {
124 ; CHECK-LABEL: @abs_nabs_x01_vec(
125 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i32> %x, zeroinitializer
126 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw <2 x i32> zeroinitializer, %x
127 ; CHECK-NEXT: [[COND1:%.*]] = select <2 x i1> [[CMP]], <2 x i32> [[SUB]], <2 x i32> %x, !prof ![[$MD3]]
128 ; CHECK-NEXT: ret <2 x i32> [[COND1]]
130 %cmp = icmp sgt <2 x i32> %x, <i32 -1, i32 -1>
131 %sub = sub nsw <2 x i32> zeroinitializer, %x
132 %cond = select <2 x i1> %cmp, <2 x i32> %sub, <2 x i32> %x, !prof !1
133 %cmp1 = icmp sgt <2 x i32> %cond, <i32 -1, i32 -1>
134 %sub16 = sub nsw <2 x i32> zeroinitializer, %cond
135 %cond18 = select <2 x i1> %cmp1, <2 x i32> %cond, <2 x i32> %sub16, !prof !2
136 ret <2 x i32> %cond18
139 ; SMAX(SMAX(x, y), x) -> SMAX(x, y)
140 define i32 @test30(i32 %x, i32 %y) {
141 ; CHECK-LABEL: @test30(
142 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 %x, %y
143 ; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 %x, i32 %y, !prof ![[$MD1]]
144 ; CHECK-NEXT: ret i32 [[COND]]
146 %cmp = icmp sgt i32 %x, %y
147 %cond = select i1 %cmp, i32 %x, i32 %y, !prof !1
148 %cmp5 = icmp sgt i32 %cond, %x
149 %retval = select i1 %cmp5, i32 %cond, i32 %x, !prof !2
153 ; SMAX(SMAX(75, X), 36) -> SMAX(X, 75)
154 define i32 @test70(i32 %x) {
155 ; CHECK-LABEL: @test70(
156 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 %x, 75
157 ; CHECK-NEXT: [[COND:%.*]] = select i1 [[TMP1]], i32 %x, i32 75, !prof ![[$MD3]]
158 ; CHECK-NEXT: ret i32 [[COND]]
160 %cmp = icmp slt i32 %x, 75
161 %cond = select i1 %cmp, i32 75, i32 %x, !prof !1
162 %cmp3 = icmp slt i32 %cond, 36
163 %retval = select i1 %cmp3, i32 36, i32 %cond, !prof !2
167 ; Swap predicate / metadata order
168 ; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11)
169 define i32 @test72(i32 %x) {
170 ; CHECK-LABEL: @test72(
171 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 %x, 11
172 ; CHECK-NEXT: [[RETVAL:%.*]] = select i1 [[TMP1]], i32 %x, i32 11, !prof ![[$MD4:[0-9]+]]
173 ; CHECK-NEXT: ret i32 [[RETVAL]]
175 %cmp = icmp sgt i32 %x, 92
176 %cond = select i1 %cmp, i32 92, i32 %x, !prof !1
177 %cmp3 = icmp sgt i32 %cond, 11
178 %retval = select i1 %cmp3, i32 11, i32 %cond, !prof !2
182 ; Swap predicate / metadata order
183 ; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75)
184 define i32 @test74(i32 %x) {
185 ; CHECK-LABEL: @test74(
186 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 %x, 75
187 ; CHECK-NEXT: [[RETVAL:%.*]] = select i1 [[TMP1]], i32 %x, i32 75, !prof ![[$MD4]]
188 ; CHECK-NEXT: ret i32 [[RETVAL]]
190 %cmp = icmp slt i32 %x, 36
191 %cond = select i1 %cmp, i32 36, i32 %x, !prof !1
192 %cmp3 = icmp slt i32 %cond, 75
193 %retval = select i1 %cmp3, i32 75, i32 %cond, !prof !2
197 ; The xor is moved after the select. The metadata remains the same because the select operands are not swapped only inverted.
198 define i32 @smin1(i32 %x) {
199 ; CHECK-LABEL: @smin1(
200 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
201 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0, !prof ![[$MD1]]
202 ; CHECK-NEXT: [[SEL:%.*]] = xor i32 [[TMP2]], -1
203 ; CHECK-NEXT: ret i32 [[SEL]]
205 %not_x = xor i32 %x, -1
206 %cmp = icmp sgt i32 %x, 0
207 %sel = select i1 %cmp, i32 %not_x, i32 -1, !prof !1
211 ; The compare should change, and the metadata is swapped because the select operands are swapped and inverted.
212 define i32 @smin2(i32 %x) {
213 ; CHECK-LABEL: @smin2(
214 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
215 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0, !prof ![[$MD3]]
216 ; CHECK-NEXT: [[SEL:%.*]] = xor i32 [[TMP2]], -1
217 ; CHECK-NEXT: ret i32 [[SEL]]
219 %not_x = xor i32 %x, -1
220 %cmp = icmp slt i32 %x, 0
221 %sel = select i1 %cmp, i32 -1, i32 %not_x, !prof !1
225 ; The xor is moved after the select. The metadata remains the same because the select operands are not swapped only inverted.
226 define i32 @smax1(i32 %x) {
227 ; CHECK-LABEL: @smax1(
228 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 0
229 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0, !prof ![[$MD1]]
230 ; CHECK-NEXT: [[SEL:%.*]] = xor i32 [[TMP2]], -1
231 ; CHECK-NEXT: ret i32 [[SEL]]
233 %not_x = xor i32 %x, -1
234 %cmp = icmp slt i32 %x, 0
235 %sel = select i1 %cmp, i32 %not_x, i32 -1, !prof !1
239 ; The compare should change, and the metadata is swapped because the select operands are swapped and inverted.
240 define i32 @smax2(i32 %x) {
241 ; CHECK-LABEL: @smax2(
242 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 0
243 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0, !prof ![[$MD3]]
244 ; CHECK-NEXT: [[SEL:%.*]] = xor i32 [[TMP2]], -1
245 ; CHECK-NEXT: ret i32 [[SEL]]
247 %not_x = xor i32 %x, -1
248 %cmp = icmp sgt i32 %x, 0
249 %sel = select i1 %cmp, i32 -1, i32 %not_x, !prof !1
253 ; The compare should change, but the metadata remains the same because the select operands are not swapped.
254 define i32 @umin1(i32 %x) {
255 ; CHECK-LABEL: @umin1(
256 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 %x, -2147483648
257 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 %x, i32 -2147483648, !prof ![[$MD1]]
258 ; CHECK-NEXT: ret i32 [[SEL]]
260 %cmp = icmp sgt i32 %x, -1
261 %sel = select i1 %cmp, i32 %x, i32 -2147483648, !prof !1
265 ; The compare should change, and the metadata is swapped because the select operands are swapped.
266 define i32 @umin2(i32 %x) {
267 ; CHECK-LABEL: @umin2(
268 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 %x, 2147483647
269 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 %x, i32 2147483647, !prof ![[$MD3]]
270 ; CHECK-NEXT: ret i32 [[SEL]]
272 %cmp = icmp slt i32 %x, 0
273 %sel = select i1 %cmp, i32 2147483647, i32 %x, !prof !1
277 ; The compare should change, but the metadata remains the same because the select operands are not swapped.
278 define i32 @umax1(i32 %x) {
279 ; CHECK-LABEL: @umax1(
280 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 %x, 2147483647
281 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 %x, i32 2147483647, !prof ![[$MD1]]
282 ; CHECK-NEXT: ret i32 [[SEL]]
284 %cmp = icmp slt i32 %x, 0
285 %sel = select i1 %cmp, i32 %x, i32 2147483647, !prof !1
289 ; The compare should change, and the metadata is swapped because the select operands are swapped.
290 define i32 @umax2(i32 %x) {
291 ; CHECK-LABEL: @umax2(
292 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 %x, -2147483648
293 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 %x, i32 -2147483648, !prof ![[$MD3]]
294 ; CHECK-NEXT: ret i32 [[SEL]]
296 %cmp = icmp sgt i32 %x, -1
297 %sel = select i1 %cmp, i32 -2147483648, i32 %x, !prof !1
301 ; The condition is inverted, and the select ops are swapped. The metadata should be swapped.
303 define i32 @not_cond(i1 %c, i32 %tv, i32 %fv) {
304 ; CHECK-LABEL: @not_cond(
305 ; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i32 [[FV:%.*]], i32 [[TV:%.*]], !prof ![[$MD3]]
306 ; CHECK-NEXT: ret i32 [[R]]
308 %notc = xor i1 %c, true
309 %r = select i1 %notc, i32 %tv, i32 %fv, !prof !1
313 ; The condition is inverted, and the select ops are swapped. The metadata should be swapped.
315 define <2 x i32> @not_cond_vec(<2 x i1> %c, <2 x i32> %tv, <2 x i32> %fv) {
316 ; CHECK-LABEL: @not_cond_vec(
317 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C:%.*]], <2 x i32> [[FV:%.*]], <2 x i32> [[TV:%.*]], !prof ![[$MD3]]
318 ; CHECK-NEXT: ret <2 x i32> [[R]]
320 %notc = xor <2 x i1> %c, <i1 true, i1 true>
321 %r = select <2 x i1> %notc, <2 x i32> %tv, <2 x i32> %fv, !prof !1
325 ; Should match vector 'not' with undef element.
326 ; The condition is inverted, and the select ops are swapped. The metadata should be swapped.
328 define <2 x i32> @not_cond_vec_undef(<2 x i1> %c, <2 x i32> %tv, <2 x i32> %fv) {
329 ; CHECK-LABEL: @not_cond_vec_undef(
330 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C:%.*]], <2 x i32> [[FV:%.*]], <2 x i32> [[TV:%.*]], !prof ![[$MD3]]
331 ; CHECK-NEXT: ret <2 x i32> [[R]]
333 %notc = xor <2 x i1> %c, <i1 undef, i1 true>
334 %r = select <2 x i1> %notc, <2 x i32> %tv, <2 x i32> %fv, !prof !1
339 !1 = !{!"branch_weights", i32 2, i32 10}
340 !2 = !{!"branch_weights", i32 3, i32 10}
342 ; CHECK-DAG: ![[$MD1]] = !{!"branch_weights", i32 2, i32 10}
343 ; CHECK-DAG: ![[$MD3]] = !{!"branch_weights", i32 10, i32 2}
344 ; CHECK-DAG: ![[$MD4]] = !{!"branch_weights", i32 10, i32 3}