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 [[TMP0:%.*]], 2
9 ; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[TMP2]], i32 20, i32 -20, !prof !0
10 ; CHECK-NEXT: [[TMP3:%.*]] = add i32 [[DOTV]], [[TMP0]]
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 !0
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 !0
35 ; CHECK-NEXT: [[SEL2_V:%.*]] = select <4 x i1> [[CMP]], <4 x float> [[B]], <4 x float> [[A]], !prof !0
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 [[TMP0:%.*]], 2
55 ; CHECK-NEXT: [[TMP4:%.*]] = sub i32 0, [[TMP1:%.*]]
56 ; CHECK-NEXT: [[DOTP:%.*]] = select i1 [[TMP3]], i32 [[TMP1]], i32 [[TMP4]], !prof !0
57 ; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[DOTP]], [[TMP0]]
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 !0
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 !0
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 !0
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: [[TMP1:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
108 ; CHECK-NEXT: ret i32 [[TMP1]]
110 %cmp = icmp sgt i32 %x, -1
111 %sub = sub nsw i32 0, %x
112 %cond = select i1 %cmp, i32 %sub, i32 %x, !prof !1
113 %cmp1 = icmp sgt i32 %cond, -1
114 %sub16 = sub nsw i32 0, %cond
115 %cond18 = select i1 %cmp1, i32 %cond, i32 %sub16, !prof !2
119 ; Swap predicate / metadata order
121 define <2 x i32> @abs_nabs_x01_vec(<2 x i32> %x) {
122 ; CHECK-LABEL: @abs_nabs_x01_vec(
123 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i32> @llvm.abs.v2i32(<2 x i32> [[X:%.*]], i1 false)
124 ; CHECK-NEXT: ret <2 x i32> [[TMP1]]
126 %cmp = icmp sgt <2 x i32> %x, <i32 -1, i32 -1>
127 %sub = sub nsw <2 x i32> zeroinitializer, %x
128 %cond = select <2 x i1> %cmp, <2 x i32> %sub, <2 x i32> %x, !prof !1
129 %cmp1 = icmp sgt <2 x i32> %cond, <i32 -1, i32 -1>
130 %sub16 = sub nsw <2 x i32> zeroinitializer, %cond
131 %cond18 = select <2 x i1> %cmp1, <2 x i32> %cond, <2 x i32> %sub16, !prof !2
132 ret <2 x i32> %cond18
135 ; SMAX(SMAX(x, y), x) -> SMAX(x, y)
136 define i32 @test30(i32 %x, i32 %y) {
137 ; CHECK-LABEL: @test30(
138 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
139 ; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 [[X]], i32 [[Y]], !prof !0
140 ; CHECK-NEXT: ret i32 [[COND]]
142 %cmp = icmp sgt i32 %x, %y
143 %cond = select i1 %cmp, i32 %x, i32 %y, !prof !1
144 %cmp5 = icmp sgt i32 %cond, %x
145 %retval = select i1 %cmp5, i32 %cond, i32 %x, !prof !2
149 ; SMAX(SMAX(75, X), 36) -> SMAX(X, 75)
150 define i32 @test70(i32 %x) {
151 ; CHECK-LABEL: @test70(
152 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 75
153 ; CHECK-NEXT: [[COND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 75, !prof !1
154 ; CHECK-NEXT: ret i32 [[COND]]
156 %cmp = icmp slt i32 %x, 75
157 %cond = select i1 %cmp, i32 75, i32 %x, !prof !1
158 %cmp3 = icmp slt i32 %cond, 36
159 %retval = select i1 %cmp3, i32 36, i32 %cond, !prof !2
163 ; Swap predicate / metadata order
164 ; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11)
165 define i32 @test72(i32 %x) {
166 ; CHECK-LABEL: @test72(
167 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 11
168 ; CHECK-NEXT: [[RETVAL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 11, !prof !2
169 ; CHECK-NEXT: ret i32 [[RETVAL]]
171 %cmp = icmp sgt i32 %x, 92
172 %cond = select i1 %cmp, i32 92, i32 %x, !prof !1
173 %cmp3 = icmp sgt i32 %cond, 11
174 %retval = select i1 %cmp3, i32 11, i32 %cond, !prof !2
178 ; Swap predicate / metadata order
179 ; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75)
180 define i32 @test74(i32 %x) {
181 ; CHECK-LABEL: @test74(
182 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 75
183 ; CHECK-NEXT: [[RETVAL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 75, !prof !2
184 ; CHECK-NEXT: ret i32 [[RETVAL]]
186 %cmp = icmp slt i32 %x, 36
187 %cond = select i1 %cmp, i32 36, i32 %x, !prof !1
188 %cmp3 = icmp slt i32 %cond, 75
189 %retval = select i1 %cmp3, i32 75, i32 %cond, !prof !2
193 ; The xor is moved after the select. The metadata remains the same because the select operands are not swapped only inverted.
194 define i32 @smin1(i32 %x) {
195 ; CHECK-LABEL: @smin1(
196 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
197 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0, !prof !0
198 ; CHECK-NEXT: [[SEL:%.*]] = xor i32 [[TMP2]], -1
199 ; CHECK-NEXT: ret i32 [[SEL]]
201 %not_x = xor i32 %x, -1
202 %cmp = icmp sgt i32 %x, 0
203 %sel = select i1 %cmp, i32 %not_x, i32 -1, !prof !1
207 ; The compare should change, and the metadata is swapped because the select operands are swapped and inverted.
208 define i32 @smin2(i32 %x) {
209 ; CHECK-LABEL: @smin2(
210 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
211 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0, !prof !1
212 ; CHECK-NEXT: [[SEL:%.*]] = xor i32 [[TMP2]], -1
213 ; CHECK-NEXT: ret i32 [[SEL]]
215 %not_x = xor i32 %x, -1
216 %cmp = icmp slt i32 %x, 0
217 %sel = select i1 %cmp, i32 -1, i32 %not_x, !prof !1
221 ; The xor is moved after the select. The metadata remains the same because the select operands are not swapped only inverted.
222 define i32 @smax1(i32 %x) {
223 ; CHECK-LABEL: @smax1(
224 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 0
225 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0, !prof !0
226 ; CHECK-NEXT: [[SEL:%.*]] = xor i32 [[TMP2]], -1
227 ; CHECK-NEXT: ret i32 [[SEL]]
229 %not_x = xor i32 %x, -1
230 %cmp = icmp slt i32 %x, 0
231 %sel = select i1 %cmp, i32 %not_x, i32 -1, !prof !1
235 ; The compare should change, and the metadata is swapped because the select operands are swapped and inverted.
236 define i32 @smax2(i32 %x) {
237 ; CHECK-LABEL: @smax2(
238 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 0
239 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0, !prof !1
240 ; CHECK-NEXT: [[SEL:%.*]] = xor i32 [[TMP2]], -1
241 ; CHECK-NEXT: ret i32 [[SEL]]
243 %not_x = xor i32 %x, -1
244 %cmp = icmp sgt i32 %x, 0
245 %sel = select i1 %cmp, i32 -1, i32 %not_x, !prof !1
249 ; The compare should change, but the metadata remains the same because the select operands are not swapped.
250 define i32 @umin1(i32 %x) {
251 ; CHECK-LABEL: @umin1(
252 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], -2147483648
253 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -2147483648, !prof !0
254 ; CHECK-NEXT: ret i32 [[SEL]]
256 %cmp = icmp sgt i32 %x, -1
257 %sel = select i1 %cmp, i32 %x, i32 -2147483648, !prof !1
261 ; The compare should change, and the metadata is swapped because the select operands are swapped.
262 define i32 @umin2(i32 %x) {
263 ; CHECK-LABEL: @umin2(
264 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], 2147483647
265 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 2147483647, !prof !1
266 ; CHECK-NEXT: ret i32 [[SEL]]
268 %cmp = icmp slt i32 %x, 0
269 %sel = select i1 %cmp, i32 2147483647, i32 %x, !prof !1
273 ; The compare should change, but the metadata remains the same because the select operands are not swapped.
274 define i32 @umax1(i32 %x) {
275 ; CHECK-LABEL: @umax1(
276 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], 2147483647
277 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 2147483647, !prof !0
278 ; CHECK-NEXT: ret i32 [[SEL]]
280 %cmp = icmp slt i32 %x, 0
281 %sel = select i1 %cmp, i32 %x, i32 2147483647, !prof !1
285 ; The compare should change, and the metadata is swapped because the select operands are swapped.
286 define i32 @umax2(i32 %x) {
287 ; CHECK-LABEL: @umax2(
288 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], -2147483648
289 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -2147483648, !prof !1
290 ; CHECK-NEXT: ret i32 [[SEL]]
292 %cmp = icmp sgt i32 %x, -1
293 %sel = select i1 %cmp, i32 -2147483648, i32 %x, !prof !1
297 ; The condition is inverted, and the select ops are swapped. The metadata should be swapped.
299 define i32 @not_cond(i1 %c, i32 %tv, i32 %fv) {
300 ; CHECK-LABEL: @not_cond(
301 ; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i32 [[FV:%.*]], i32 [[TV:%.*]], !prof !1
302 ; CHECK-NEXT: ret i32 [[R]]
304 %notc = xor i1 %c, true
305 %r = select i1 %notc, i32 %tv, i32 %fv, !prof !1
309 ; The condition is inverted, and the select ops are swapped. The metadata should be swapped.
311 define <2 x i32> @not_cond_vec(<2 x i1> %c, <2 x i32> %tv, <2 x i32> %fv) {
312 ; CHECK-LABEL: @not_cond_vec(
313 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C:%.*]], <2 x i32> [[FV:%.*]], <2 x i32> [[TV:%.*]], !prof !1
314 ; CHECK-NEXT: ret <2 x i32> [[R]]
316 %notc = xor <2 x i1> %c, <i1 true, i1 true>
317 %r = select <2 x i1> %notc, <2 x i32> %tv, <2 x i32> %fv, !prof !1
321 ; Should match vector 'not' with undef element.
322 ; The condition is inverted, and the select ops are swapped. The metadata should be swapped.
324 define <2 x i32> @not_cond_vec_undef(<2 x i1> %c, <2 x i32> %tv, <2 x i32> %fv) {
325 ; CHECK-LABEL: @not_cond_vec_undef(
326 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C:%.*]], <2 x i32> [[FV:%.*]], <2 x i32> [[TV:%.*]], !prof !1
327 ; CHECK-NEXT: ret <2 x i32> [[R]]
329 %notc = xor <2 x i1> %c, <i1 undef, i1 true>
330 %r = select <2 x i1> %notc, <2 x i32> %tv, <2 x i32> %fv, !prof !1
335 !1 = !{!"branch_weights", i32 2, i32 10}
336 !2 = !{!"branch_weights", i32 3, i32 10}
338 ; CHECK: !0 = !{!"branch_weights", i32 2, i32 10}
339 ; CHECK-NEXT: !1 = !{!"branch_weights", i32 10, i32 2}
340 ; CHECK-NEXT: !2 = !{!"branch_weights", i32 10, i32 3}