1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=instcombine < %s | FileCheck %s
4 ; This is the canonical form for a type-changing min/max.
5 define i64 @t1(i32 %a) {
7 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[A:%.*]], i32 5)
8 ; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[TMP1]] to i64
9 ; CHECK-NEXT: ret i64 [[TMP2]]
11 %1 = icmp slt i32 %a, 5
12 %2 = select i1 %1, i32 %a, i32 5
13 %3 = sext i32 %2 to i64
17 ; Check this is converted into canonical form, as above.
18 define i64 @t2(i32 %a) {
20 ; CHECK-NEXT: [[NARROW:%.*]] = call i32 @llvm.smin.i32(i32 [[A:%.*]], i32 5)
21 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[NARROW]] to i64
22 ; CHECK-NEXT: ret i64 [[TMP1]]
24 %1 = icmp slt i32 %a, 5
25 %2 = sext i32 %a to i64
26 %3 = select i1 %1, i64 %2, i64 5
30 ; Same as @t2, with flipped operands and zext instead of sext.
31 define i64 @t3(i32 %a) {
33 ; CHECK-NEXT: [[NARROW:%.*]] = call i32 @llvm.umax.i32(i32 [[A:%.*]], i32 5)
34 ; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[NARROW]] to i64
35 ; CHECK-NEXT: ret i64 [[TMP1]]
37 %1 = icmp ult i32 %a, 5
38 %2 = zext i32 %a to i64
39 %3 = select i1 %1, i64 5, i64 %2
43 ; Same again, with trunc.
44 define i32 @t4(i64 %a) {
46 ; CHECK-NEXT: [[TMP1:%.*]] = call i64 @llvm.smin.i64(i64 [[A:%.*]], i64 5)
47 ; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
48 ; CHECK-NEXT: ret i32 [[TMP2]]
50 %1 = icmp slt i64 %a, 5
51 %2 = trunc i64 %a to i32
52 %3 = select i1 %1, i32 %2, i32 5
56 ; Same as @t3, but with mismatched signedness between icmp and zext.
57 define i64 @t5(i32 %a) {
59 ; CHECK-NEXT: [[NARROW:%.*]] = call i32 @llvm.smax.i32(i32 [[A:%.*]], i32 5)
60 ; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i32 [[NARROW]] to i64
61 ; CHECK-NEXT: ret i64 [[TMP1]]
63 %1 = icmp slt i32 %a, 5
64 %2 = zext i32 %a to i64
65 %3 = select i1 %1, i64 5, i64 %2
69 define float @t6(i32 %a) {
71 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[A:%.*]], i32 0)
72 ; CHECK-NEXT: [[TMP2:%.*]] = sitofp i32 [[TMP1]] to float
73 ; CHECK-NEXT: ret float [[TMP2]]
75 %1 = icmp slt i32 %a, 0
76 %2 = select i1 %1, i32 %a, i32 0
77 %3 = sitofp i32 %2 to float
81 define i16 @t7(i32 %a) {
83 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[A:%.*]], i32 -32768)
84 ; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[TMP1]] to i16
85 ; CHECK-NEXT: ret i16 [[TMP2]]
87 %1 = icmp slt i32 %a, -32768
88 %2 = trunc i32 %a to i16
89 %3 = select i1 %1, i16 %2, i16 -32768
93 ; Just check for no infinite loop. InstSimplify liked to
94 ; "simplify" -32767 by removing all the sign bits,
95 ; which led to a canonicalization fight between different
96 ; parts of instcombine.
97 define i32 @t8(i64 %a, i32 %b) {
99 ; CHECK-NEXT: [[TMP1:%.*]] = call i64 @llvm.smin.i64(i64 [[A:%.*]], i64 -32767)
100 ; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
101 ; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[B:%.*]], 42
102 ; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i32 [[B]], [[TMP2]]
103 ; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i1 true, i1 [[TMP4]]
104 ; CHECK-NEXT: [[TMP6:%.*]] = zext i1 [[TMP5]] to i32
105 ; CHECK-NEXT: ret i32 [[TMP6]]
107 %1 = icmp slt i64 %a, -32767
108 %2 = select i1 %1, i64 %a, i64 -32767
109 %3 = trunc i64 %2 to i32
110 %4 = icmp slt i32 %b, 42
111 %5 = select i1 %4, i32 42, i32 %3
112 %6 = icmp ne i32 %5, %b
113 %7 = zext i1 %6 to i32
117 ; Ensure this doesn't get converted to a min/max.
118 define i64 @t9(i32 %a) {
120 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[A:%.*]], -1
121 ; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[A]] to i64
122 ; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], i64 [[TMP2]], i64 4294967295
123 ; CHECK-NEXT: ret i64 [[TMP3]]
125 %1 = icmp sgt i32 %a, -1
126 %2 = sext i32 %a to i64
127 %3 = select i1 %1, i64 %2, i64 4294967295
131 define float @t10(i32 %x) {
133 ; CHECK-NEXT: [[R1:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 255)
134 ; CHECK-NEXT: [[R:%.*]] = uitofp nneg i32 [[R1]] to float
135 ; CHECK-NEXT: ret float [[R]]
137 %f_x = sitofp i32 %x to float
138 %cmp = icmp sgt i32 %x, 255
139 %r = select i1 %cmp, float %f_x, float 255.0
143 define float @t11(i64 %x) {
145 ; CHECK-NEXT: [[R1:%.*]] = call i64 @llvm.smax.i64(i64 [[X:%.*]], i64 255)
146 ; CHECK-NEXT: [[R:%.*]] = uitofp nneg i64 [[R1]] to float
147 ; CHECK-NEXT: ret float [[R]]
149 %f_x = sitofp i64 %x to float
150 %cmp = icmp sgt i64 %x, 255
151 %r = select i1 %cmp, float %f_x, float 255.0
155 ; Reuse the first 2 bitcasts as the select operands.
157 define <4 x i32> @bitcasts_fcmp_1(<2 x i64> %a, <2 x i64> %b) {
158 ; CHECK-LABEL: @bitcasts_fcmp_1(
159 ; CHECK-NEXT: [[T0:%.*]] = bitcast <2 x i64> [[A:%.*]] to <4 x float>
160 ; CHECK-NEXT: [[T1:%.*]] = bitcast <2 x i64> [[B:%.*]] to <4 x float>
161 ; CHECK-NEXT: [[T2:%.*]] = fcmp olt <4 x float> [[T1]], [[T0]]
162 ; CHECK-NEXT: [[TMP1:%.*]] = select <4 x i1> [[T2]], <4 x float> [[T0]], <4 x float> [[T1]]
163 ; CHECK-NEXT: [[T5:%.*]] = bitcast <4 x float> [[TMP1]] to <4 x i32>
164 ; CHECK-NEXT: ret <4 x i32> [[T5]]
166 %t0 = bitcast <2 x i64> %a to <4 x float>
167 %t1 = bitcast <2 x i64> %b to <4 x float>
168 %t2 = fcmp olt <4 x float> %t1, %t0
169 %t3 = bitcast <2 x i64> %a to <4 x i32>
170 %t4 = bitcast <2 x i64> %b to <4 x i32>
171 %t5 = select <4 x i1> %t2, <4 x i32> %t3, <4 x i32> %t4
175 ; Switch cmp operand order.
177 define <4 x i32> @bitcasts_fcmp_2(<2 x i64> %a, <2 x i64> %b) {
178 ; CHECK-LABEL: @bitcasts_fcmp_2(
179 ; CHECK-NEXT: [[T0:%.*]] = bitcast <2 x i64> [[A:%.*]] to <4 x float>
180 ; CHECK-NEXT: [[T1:%.*]] = bitcast <2 x i64> [[B:%.*]] to <4 x float>
181 ; CHECK-NEXT: [[T2:%.*]] = fcmp olt <4 x float> [[T0]], [[T1]]
182 ; CHECK-NEXT: [[TMP1:%.*]] = select <4 x i1> [[T2]], <4 x float> [[T0]], <4 x float> [[T1]]
183 ; CHECK-NEXT: [[T5:%.*]] = bitcast <4 x float> [[TMP1]] to <4 x i32>
184 ; CHECK-NEXT: ret <4 x i32> [[T5]]
186 %t0 = bitcast <2 x i64> %a to <4 x float>
187 %t1 = bitcast <2 x i64> %b to <4 x float>
188 %t2 = fcmp olt <4 x float> %t0, %t1
189 %t3 = bitcast <2 x i64> %a to <4 x i32>
190 %t4 = bitcast <2 x i64> %b to <4 x i32>
191 %t5 = select <4 x i1> %t2, <4 x i32> %t3, <4 x i32> %t4
195 ; Integer cmp should have the same transforms.
197 define <4 x float> @bitcasts_icmp(<2 x i64> %a, <2 x i64> %b) {
198 ; CHECK-LABEL: @bitcasts_icmp(
199 ; CHECK-NEXT: [[T0:%.*]] = bitcast <2 x i64> [[A:%.*]] to <4 x i32>
200 ; CHECK-NEXT: [[T1:%.*]] = bitcast <2 x i64> [[B:%.*]] to <4 x i32>
201 ; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i32> @llvm.smax.v4i32(<4 x i32> [[T1]], <4 x i32> [[T0]])
202 ; CHECK-NEXT: [[T5:%.*]] = bitcast <4 x i32> [[TMP1]] to <4 x float>
203 ; CHECK-NEXT: ret <4 x float> [[T5]]
205 %t0 = bitcast <2 x i64> %a to <4 x i32>
206 %t1 = bitcast <2 x i64> %b to <4 x i32>
207 %t2 = icmp slt <4 x i32> %t1, %t0
208 %t3 = bitcast <2 x i64> %a to <4 x float>
209 %t4 = bitcast <2 x i64> %b to <4 x float>
210 %t5 = select <4 x i1> %t2, <4 x float> %t3, <4 x float> %t4
214 ; SMIN(SMIN(X, 11), 92) -> SMIN(X, 11)
215 define i32 @test68(i32 %x) {
216 ; CHECK-LABEL: @test68(
217 ; CHECK-NEXT: [[COND:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 11)
218 ; CHECK-NEXT: ret i32 [[COND]]
220 %cmp = icmp slt i32 11, %x
221 %cond = select i1 %cmp, i32 11, i32 %x
222 %cmp3 = icmp slt i32 92, %cond
223 %retval = select i1 %cmp3, i32 92, i32 %cond
227 define <2 x i32> @test68vec(<2 x i32> %x) {
228 ; CHECK-LABEL: @test68vec(
229 ; CHECK-NEXT: [[COND:%.*]] = call <2 x i32> @llvm.smin.v2i32(<2 x i32> [[X:%.*]], <2 x i32> splat (i32 11))
230 ; CHECK-NEXT: ret <2 x i32> [[COND]]
232 %cmp = icmp slt <2 x i32> <i32 11, i32 11>, %x
233 %cond = select <2 x i1> %cmp, <2 x i32> <i32 11, i32 11>, <2 x i32> %x
234 %cmp3 = icmp slt <2 x i32> <i32 92, i32 92>, %cond
235 %retval = select <2 x i1> %cmp3, <2 x i32> <i32 92, i32 92>, <2 x i32> %cond
236 ret <2 x i32> %retval
239 ; MIN(MIN(X, 24), 83) -> MIN(X, 24)
240 define i32 @test69(i32 %x) {
241 ; CHECK-LABEL: @test69(
242 ; CHECK-NEXT: [[COND:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 24)
243 ; CHECK-NEXT: ret i32 [[COND]]
245 %cmp = icmp ult i32 24, %x
246 %cond = select i1 %cmp, i32 24, i32 %x
247 %cmp3 = icmp ult i32 83, %cond
248 %retval = select i1 %cmp3, i32 83, i32 %cond
252 ; SMAX(SMAX(X, 75), 36) -> SMAX(X, 75)
253 define i32 @test70(i32 %x) {
254 ; CHECK-LABEL: @test70(
255 ; CHECK-NEXT: [[COND:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 75)
256 ; CHECK-NEXT: ret i32 [[COND]]
258 %cmp = icmp slt i32 %x, 75
259 %cond = select i1 %cmp, i32 75, i32 %x
260 %cmp3 = icmp slt i32 %cond, 36
261 %retval = select i1 %cmp3, i32 36, i32 %cond
265 ; MAX(MAX(X, 68), 47) -> MAX(X, 68)
266 define i32 @test71(i32 %x) {
267 ; CHECK-LABEL: @test71(
268 ; CHECK-NEXT: [[COND:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 68)
269 ; CHECK-NEXT: ret i32 [[COND]]
271 %cmp = icmp ult i32 %x, 68
272 %cond = select i1 %cmp, i32 68, i32 %x
273 %cmp3 = icmp ult i32 %cond, 47
274 %retval = select i1 %cmp3, i32 47, i32 %cond
278 ; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11)
279 define i32 @test72(i32 %x) {
280 ; CHECK-LABEL: @test72(
281 ; CHECK-NEXT: [[RETVAL:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 11)
282 ; CHECK-NEXT: ret i32 [[RETVAL]]
284 %cmp = icmp sgt i32 %x, 92
285 %cond = select i1 %cmp, i32 92, i32 %x
286 %cmp3 = icmp sgt i32 %cond, 11
287 %retval = select i1 %cmp3, i32 11, i32 %cond
291 define <2 x i32> @test72vec(<2 x i32> %x) {
292 ; CHECK-LABEL: @test72vec(
293 ; CHECK-NEXT: [[RETVAL:%.*]] = call <2 x i32> @llvm.smin.v2i32(<2 x i32> [[X:%.*]], <2 x i32> splat (i32 11))
294 ; CHECK-NEXT: ret <2 x i32> [[RETVAL]]
296 %cmp = icmp sgt <2 x i32> %x, <i32 92, i32 92>
297 %cond = select <2 x i1> %cmp, <2 x i32> <i32 92, i32 92>, <2 x i32> %x
298 %cmp3 = icmp sgt <2 x i32> %cond, <i32 11, i32 11>
299 %retval = select <2 x i1> %cmp3, <2 x i32> <i32 11, i32 11>, <2 x i32> %cond
300 ret <2 x i32> %retval
303 ; MIN(MIN(X, 83), 24) -> MIN(X, 24)
304 define i32 @test73(i32 %x) {
305 ; CHECK-LABEL: @test73(
306 ; CHECK-NEXT: [[RETVAL:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 24)
307 ; CHECK-NEXT: ret i32 [[RETVAL]]
309 %cmp = icmp ugt i32 %x, 83
310 %cond = select i1 %cmp, i32 83, i32 %x
311 %cmp3 = icmp ugt i32 %cond, 24
312 %retval = select i1 %cmp3, i32 24, i32 %cond
316 ; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75)
317 define i32 @test74(i32 %x) {
318 ; CHECK-LABEL: @test74(
319 ; CHECK-NEXT: [[RETVAL:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 75)
320 ; CHECK-NEXT: ret i32 [[RETVAL]]
322 %cmp = icmp slt i32 %x, 36
323 %cond = select i1 %cmp, i32 36, i32 %x
324 %cmp3 = icmp slt i32 %cond, 75
325 %retval = select i1 %cmp3, i32 75, i32 %cond
329 ; MAX(MAX(X, 47), 68) -> MAX(X, 68)
330 define i32 @test75(i32 %x) {
331 ; CHECK-LABEL: @test75(
332 ; CHECK-NEXT: [[RETVAL:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 68)
333 ; CHECK-NEXT: ret i32 [[RETVAL]]
335 %cmp = icmp ult i32 %x, 47
336 %cond = select i1 %cmp, i32 47, i32 %x
337 %cmp3 = icmp ult i32 %cond, 68
338 %retval = select i1 %cmp3, i32 68, i32 %cond
342 ; The next 10 tests are value clamping with constants:
343 ; https://llvm.org/bugs/show_bug.cgi?id=31693
345 ; (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
347 define i32 @clamp_signed1(i32 %x) {
348 ; CHECK-LABEL: @clamp_signed1(
349 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 255)
350 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smax.i32(i32 [[MIN]], i32 15)
351 ; CHECK-NEXT: ret i32 [[R]]
353 %cmp2 = icmp slt i32 %x, 255
354 %min = select i1 %cmp2, i32 %x, i32 255
355 %cmp1 = icmp slt i32 %x, 15
356 %r = select i1 %cmp1, i32 15, i32 %min
360 ; (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
362 define i32 @clamp_signed2(i32 %x) {
363 ; CHECK-LABEL: @clamp_signed2(
364 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 15)
365 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[MAX]], i32 255)
366 ; CHECK-NEXT: ret i32 [[R]]
368 %cmp2 = icmp sgt i32 %x, 15
369 %max = select i1 %cmp2, i32 %x, i32 15
370 %cmp1 = icmp sgt i32 %x, 255
371 %r = select i1 %cmp1, i32 255, i32 %max
375 ; (X >s C1) ? SMIN(X, C2) : C1 ==> SMAX(SMIN(X, C2), C1)
377 define i32 @clamp_signed3(i32 %x) {
378 ; CHECK-LABEL: @clamp_signed3(
379 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 255)
380 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smax.i32(i32 [[MIN]], i32 15)
381 ; CHECK-NEXT: ret i32 [[R]]
383 %cmp2 = icmp slt i32 %x, 255
384 %min = select i1 %cmp2, i32 %x, i32 255
385 %cmp1 = icmp sgt i32 %x, 15
386 %r = select i1 %cmp1, i32 %min, i32 15
390 ; (X <s C1) ? SMAX(X, C2) : C1 ==> SMIN(SMAX(X, C1), C2)
392 define i32 @clamp_signed4(i32 %x) {
393 ; CHECK-LABEL: @clamp_signed4(
394 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 15)
395 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[MAX]], i32 255)
396 ; CHECK-NEXT: ret i32 [[R]]
398 %cmp2 = icmp sgt i32 %x, 15
399 %max = select i1 %cmp2, i32 %x, i32 15
400 %cmp1 = icmp slt i32 %x, 255
401 %r = select i1 %cmp1, i32 %max, i32 255
405 ; (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
407 define i32 @clamp_unsigned1(i32 %x) {
408 ; CHECK-LABEL: @clamp_unsigned1(
409 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 255)
410 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umax.i32(i32 [[MIN]], i32 15)
411 ; CHECK-NEXT: ret i32 [[R]]
413 %cmp2 = icmp ult i32 %x, 255
414 %min = select i1 %cmp2, i32 %x, i32 255
415 %cmp1 = icmp ult i32 %x, 15
416 %r = select i1 %cmp1, i32 15, i32 %min
420 ; (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
422 define i32 @clamp_unsigned2(i32 %x) {
423 ; CHECK-LABEL: @clamp_unsigned2(
424 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 15)
425 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umin.i32(i32 [[MAX]], i32 255)
426 ; CHECK-NEXT: ret i32 [[R]]
428 %cmp2 = icmp ugt i32 %x, 15
429 %max = select i1 %cmp2, i32 %x, i32 15
430 %cmp1 = icmp ugt i32 %x, 255
431 %r = select i1 %cmp1, i32 255, i32 %max
435 ; (X >u C1) ? UMIN(X, C2) : C1 ==> UMAX(UMIN(X, C2), C1)
437 define i32 @clamp_unsigned3(i32 %x) {
438 ; CHECK-LABEL: @clamp_unsigned3(
439 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 255)
440 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umax.i32(i32 [[MIN]], i32 15)
441 ; CHECK-NEXT: ret i32 [[R]]
443 %cmp2 = icmp ult i32 %x, 255
444 %min = select i1 %cmp2, i32 %x, i32 255
445 %cmp1 = icmp ugt i32 %x, 15
446 %r = select i1 %cmp1, i32 %min, i32 15
450 ; (X <u C1) ? UMAX(X, C2) : C1 ==> UMIN(UMAX(X, C2), C1)
452 define i32 @clamp_unsigned4(i32 %x) {
453 ; CHECK-LABEL: @clamp_unsigned4(
454 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 15)
455 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umin.i32(i32 [[MAX]], i32 255)
456 ; CHECK-NEXT: ret i32 [[R]]
458 %cmp2 = icmp ugt i32 %x, 15
459 %max = select i1 %cmp2, i32 %x, i32 15
460 %cmp1 = icmp ult i32 %x, 255
461 %r = select i1 %cmp1, i32 %max, i32 255
465 ; Check that clamp is recognized and there is no infinite
466 ; loop because of reverse cmp transformation:
467 ; (icmp sgt smin(PositiveA, B) 0) -> (icmp sgt B 0)
468 define i32 @clamp_check_for_no_infinite_loop1(i32 %i) {
469 ; CHECK-LABEL: @clamp_check_for_no_infinite_loop1(
470 ; CHECK-NEXT: [[SEL1:%.*]] = call i32 @llvm.smin.i32(i32 [[I:%.*]], i32 255)
471 ; CHECK-NEXT: [[RES:%.*]] = call i32 @llvm.smax.i32(i32 [[SEL1]], i32 0)
472 ; CHECK-NEXT: ret i32 [[RES]]
474 %cmp1 = icmp slt i32 %i, 255
475 %sel1 = select i1 %cmp1, i32 %i, i32 255
476 %cmp2 = icmp slt i32 %i, 0
477 %res = select i1 %cmp2, i32 0, i32 %sel1
480 ; Check that there is no infinite loop in case of:
481 ; (icmp slt smax(NegativeA, B) 0) -> (icmp slt B 0)
482 define i32 @clamp_check_for_no_infinite_loop2(i32 %i) {
483 ; CHECK-LABEL: @clamp_check_for_no_infinite_loop2(
484 ; CHECK-NEXT: [[SEL1:%.*]] = call i32 @llvm.smax.i32(i32 [[I:%.*]], i32 -255)
485 ; CHECK-NEXT: [[RES:%.*]] = call i32 @llvm.smin.i32(i32 [[SEL1]], i32 0)
486 ; CHECK-NEXT: ret i32 [[RES]]
488 %cmp1 = icmp sgt i32 %i, -255
489 %sel1 = select i1 %cmp1, i32 %i, i32 -255
490 %cmp2 = icmp slt i32 %i, 0
491 %res = select i1 %cmp2, i32 %sel1, i32 0
495 ; Check that there is no infinite loop because of reverse cmp transformation:
496 ; (icmp slt smax(PositiveA, B) 2) -> (icmp eq B 1)
497 define i32 @clamp_check_for_no_infinite_loop3(i32 %i) {
498 ; CHECK-LABEL: @clamp_check_for_no_infinite_loop3(
499 ; CHECK-NEXT: br i1 true, label [[TRUELABEL:%.*]], label [[FALSELABEL:%.*]]
501 ; CHECK-NEXT: [[I3:%.*]] = call i32 @llvm.smax.i32(i32 [[I:%.*]], i32 1)
502 ; CHECK-NEXT: [[I6:%.*]] = call i32 @llvm.umin.i32(i32 [[I3]], i32 2)
503 ; CHECK-NEXT: [[I7:%.*]] = shl nuw nsw i32 [[I6]], 2
504 ; CHECK-NEXT: ret i32 [[I7]]
506 ; CHECK-NEXT: ret i32 0
509 %i2 = icmp sgt i32 %i, 1
510 %i3 = select i1 %i2, i32 %i, i32 1
511 %i4 = icmp sgt i32 %i3, 0
512 br i1 %i4, label %truelabel, label %falselabel
514 truelabel: ; %i<=1, %i3>0
515 %i5 = icmp slt i32 %i3, 2
516 %i6 = select i1 %i5, i32 %i3, i32 2
517 %i7 = shl nuw nsw i32 %i6, 2
524 ; The next 3 min tests should canonicalize to the same form...and not infinite loop.
526 define double @PR31751_umin1(i32 %x) {
527 ; CHECK-LABEL: @PR31751_umin1(
528 ; CHECK-NEXT: [[SEL:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 2147483647)
529 ; CHECK-NEXT: [[CONV:%.*]] = uitofp nneg i32 [[SEL]] to double
530 ; CHECK-NEXT: ret double [[CONV]]
532 %cmp = icmp slt i32 %x, 0
533 %sel = select i1 %cmp, i32 2147483647, i32 %x
534 %conv = sitofp i32 %sel to double
538 define double @PR31751_umin2(i32 %x) {
539 ; CHECK-LABEL: @PR31751_umin2(
540 ; CHECK-NEXT: [[SEL:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 2147483647)
541 ; CHECK-NEXT: [[CONV:%.*]] = uitofp nneg i32 [[SEL]] to double
542 ; CHECK-NEXT: ret double [[CONV]]
544 %cmp = icmp ult i32 %x, 2147483647
545 %sel = select i1 %cmp, i32 %x, i32 2147483647
546 %conv = sitofp i32 %sel to double
550 define double @PR31751_umin3(i32 %x) {
551 ; CHECK-LABEL: @PR31751_umin3(
552 ; CHECK-NEXT: [[SEL:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 2147483647)
553 ; CHECK-NEXT: [[CONV:%.*]] = uitofp nneg i32 [[SEL]] to double
554 ; CHECK-NEXT: ret double [[CONV]]
556 %cmp = icmp ugt i32 %x, 2147483647
557 %sel = select i1 %cmp, i32 2147483647, i32 %x
558 %conv = sitofp i32 %sel to double
562 ; The next 3 max tests should canonicalize to the same form...and not infinite loop.
564 define double @PR31751_umax1(i32 %x) {
565 ; CHECK-LABEL: @PR31751_umax1(
566 ; CHECK-NEXT: [[SEL:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 -2147483648)
567 ; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double
568 ; CHECK-NEXT: ret double [[CONV]]
570 %cmp = icmp sgt i32 %x, -1
571 %sel = select i1 %cmp, i32 2147483648, i32 %x
572 %conv = sitofp i32 %sel to double
576 define double @PR31751_umax2(i32 %x) {
577 ; CHECK-LABEL: @PR31751_umax2(
578 ; CHECK-NEXT: [[SEL:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 -2147483648)
579 ; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double
580 ; CHECK-NEXT: ret double [[CONV]]
582 %cmp = icmp ugt i32 %x, 2147483648
583 %sel = select i1 %cmp, i32 %x, i32 2147483648
584 %conv = sitofp i32 %sel to double
588 define double @PR31751_umax3(i32 %x) {
589 ; CHECK-LABEL: @PR31751_umax3(
590 ; CHECK-NEXT: [[SEL:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 -2147483648)
591 ; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[SEL]] to double
592 ; CHECK-NEXT: ret double [[CONV]]
594 %cmp = icmp ult i32 %x, 2147483648
595 %sel = select i1 %cmp, i32 2147483648, i32 %x
596 %conv = sitofp i32 %sel to double
600 ; The icmp/select form a canonical smax, so don't hide that by folding the final bitcast into the select.
602 define float @bitcast_scalar_smax(float %x, float %y) {
603 ; CHECK-LABEL: @bitcast_scalar_smax(
604 ; CHECK-NEXT: [[BCX:%.*]] = bitcast float [[X:%.*]] to i32
605 ; CHECK-NEXT: [[BCY:%.*]] = bitcast float [[Y:%.*]] to i32
606 ; CHECK-NEXT: [[SEL:%.*]] = call i32 @llvm.smax.i32(i32 [[BCX]], i32 [[BCY]])
607 ; CHECK-NEXT: [[BCS:%.*]] = bitcast i32 [[SEL]] to float
608 ; CHECK-NEXT: ret float [[BCS]]
610 %bcx = bitcast float %x to i32
611 %bcy = bitcast float %y to i32
612 %cmp = icmp sgt i32 %bcx, %bcy
613 %sel = select i1 %cmp, i32 %bcx, i32 %bcy
614 %bcs = bitcast i32 %sel to float
618 ; FIXME: Create a canonical umax by bitcasting the select.
620 define float @bitcast_scalar_umax(float %x, float %y) {
621 ; CHECK-LABEL: @bitcast_scalar_umax(
622 ; CHECK-NEXT: [[BCX:%.*]] = bitcast float [[X:%.*]] to i32
623 ; CHECK-NEXT: [[BCY:%.*]] = bitcast float [[Y:%.*]] to i32
624 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[BCX]], [[BCY]]
625 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[X]], float [[Y]]
626 ; CHECK-NEXT: ret float [[SEL]]
628 %bcx = bitcast float %x to i32
629 %bcy = bitcast float %y to i32
630 %cmp = icmp ugt i32 %bcx, %bcy
631 %sel = select i1 %cmp, float %x, float %y
635 ; PR32306 - https://bugs.llvm.org/show_bug.cgi?id=32306
636 ; The icmp/select form a canonical smin, so don't hide that by folding the final bitcast into the select.
638 define <8 x float> @bitcast_vector_smin(<8 x float> %x, <8 x float> %y) {
639 ; CHECK-LABEL: @bitcast_vector_smin(
640 ; CHECK-NEXT: [[BCX:%.*]] = bitcast <8 x float> [[X:%.*]] to <8 x i32>
641 ; CHECK-NEXT: [[BCY:%.*]] = bitcast <8 x float> [[Y:%.*]] to <8 x i32>
642 ; CHECK-NEXT: [[SEL:%.*]] = call <8 x i32> @llvm.smin.v8i32(<8 x i32> [[BCX]], <8 x i32> [[BCY]])
643 ; CHECK-NEXT: [[BCS:%.*]] = bitcast <8 x i32> [[SEL]] to <8 x float>
644 ; CHECK-NEXT: ret <8 x float> [[BCS]]
646 %bcx = bitcast <8 x float> %x to <8 x i32>
647 %bcy = bitcast <8 x float> %y to <8 x i32>
648 %cmp = icmp slt <8 x i32> %bcx, %bcy
649 %sel = select <8 x i1> %cmp, <8 x i32> %bcx, <8 x i32> %bcy
650 %bcs = bitcast <8 x i32> %sel to <8 x float>
654 ; FIXME: Create a canonical umin by bitcasting the select.
656 define <8 x float> @bitcast_vector_umin(<8 x float> %x, <8 x float> %y) {
657 ; CHECK-LABEL: @bitcast_vector_umin(
658 ; CHECK-NEXT: [[BCX:%.*]] = bitcast <8 x float> [[X:%.*]] to <8 x i32>
659 ; CHECK-NEXT: [[BCY:%.*]] = bitcast <8 x float> [[Y:%.*]] to <8 x i32>
660 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt <8 x i32> [[BCX]], [[BCY]]
661 ; CHECK-NEXT: [[SEL:%.*]] = select <8 x i1> [[CMP]], <8 x float> [[X]], <8 x float> [[Y]]
662 ; CHECK-NEXT: ret <8 x float> [[SEL]]
664 %bcx = bitcast <8 x float> %x to <8 x i32>
665 %bcy = bitcast <8 x float> %y to <8 x i32>
666 %cmp = icmp slt <8 x i32> %bcx, %bcy
667 %sel = select <8 x i1> %cmp, <8 x float> %x, <8 x float> %y
671 ; Check that we look through cast and recognize min idiom.
673 define zeroext i8 @look_through_cast1(i32 %x) {
674 ; CHECK-LABEL: @look_through_cast1(
675 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 511)
676 ; CHECK-NEXT: [[RES:%.*]] = trunc i32 [[RES1]] to i8
677 ; CHECK-NEXT: ret i8 [[RES]]
679 %cmp1 = icmp slt i32 %x, 511
680 %x_trunc = trunc i32 %x to i8
681 %res = select i1 %cmp1, i8 %x_trunc, i8 255
685 ; Check that we look through cast but min is not recognized.
687 define zeroext i8 @look_through_cast2(i32 %x) {
688 ; CHECK-LABEL: @look_through_cast2(
689 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[X:%.*]], 510
690 ; CHECK-NEXT: [[X_TRUNC:%.*]] = trunc i32 [[X]] to i8
691 ; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP1]], i8 [[X_TRUNC]], i8 -1
692 ; CHECK-NEXT: ret i8 [[RES]]
694 %cmp1 = icmp slt i32 %x, 510
695 %x_trunc = trunc i32 %x to i8
696 %res = select i1 %cmp1, i8 %x_trunc, i8 255
700 define <2 x i8> @min_through_cast_vec1(<2 x i32> %x) {
701 ; CHECK-LABEL: @min_through_cast_vec1(
702 ; CHECK-NEXT: [[RES1:%.*]] = call <2 x i32> @llvm.smin.v2i32(<2 x i32> [[X:%.*]], <2 x i32> <i32 510, i32 511>)
703 ; CHECK-NEXT: [[RES:%.*]] = trunc <2 x i32> [[RES1]] to <2 x i8>
704 ; CHECK-NEXT: ret <2 x i8> [[RES]]
706 %cmp = icmp slt <2 x i32> %x, <i32 510, i32 511>
707 %x_trunc = trunc <2 x i32> %x to <2 x i8>
708 %res = select <2 x i1> %cmp, <2 x i8> %x_trunc, <2 x i8> <i8 254, i8 255>
712 define <2 x i8> @min_through_cast_vec2(<2 x i32> %x) {
713 ; CHECK-LABEL: @min_through_cast_vec2(
714 ; CHECK-NEXT: [[RES1:%.*]] = call <2 x i32> @llvm.smin.v2i32(<2 x i32> [[X:%.*]], <2 x i32> splat (i32 511))
715 ; CHECK-NEXT: [[RES:%.*]] = trunc <2 x i32> [[RES1]] to <2 x i8>
716 ; CHECK-NEXT: ret <2 x i8> [[RES]]
718 %cmp = icmp slt <2 x i32> %x, <i32 511, i32 511>
719 %x_trunc = trunc <2 x i32> %x to <2 x i8>
720 %res = select <2 x i1> %cmp, <2 x i8> %x_trunc, <2 x i8> <i8 255, i8 255>
724 ; Remove a min/max op in a sequence with a common operand.
725 ; PR35717: https://bugs.llvm.org/show_bug.cgi?id=35717
727 ; min(min(a, b), min(b, c)) --> min(min(a, b), c)
729 define i32 @common_factor_smin(i32 %a, i32 %b, i32 %c) {
730 ; CHECK-LABEL: @common_factor_smin(
731 ; CHECK-NEXT: [[MIN_BC:%.*]] = call i32 @llvm.smin.i32(i32 [[B:%.*]], i32 [[C:%.*]])
732 ; CHECK-NEXT: [[MIN_ABC:%.*]] = call i32 @llvm.smin.i32(i32 [[MIN_BC]], i32 [[A:%.*]])
733 ; CHECK-NEXT: ret i32 [[MIN_ABC]]
735 %cmp_ab = icmp slt i32 %a, %b
736 %min_ab = select i1 %cmp_ab, i32 %a, i32 %b
737 %cmp_bc = icmp slt i32 %b, %c
738 %min_bc = select i1 %cmp_bc, i32 %b, i32 %c
739 %cmp_ab_bc = icmp slt i32 %min_ab, %min_bc
740 %min_abc = select i1 %cmp_ab_bc, i32 %min_ab, i32 %min_bc
744 ; max(max(a, b), max(c, b)) --> max(max(a, b), c)
746 define <2 x i32> @common_factor_smax(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) {
747 ; CHECK-LABEL: @common_factor_smax(
748 ; CHECK-NEXT: [[MAX_CB:%.*]] = call <2 x i32> @llvm.smax.v2i32(<2 x i32> [[C:%.*]], <2 x i32> [[B:%.*]])
749 ; CHECK-NEXT: [[MAX_ABC:%.*]] = call <2 x i32> @llvm.smax.v2i32(<2 x i32> [[MAX_CB]], <2 x i32> [[A:%.*]])
750 ; CHECK-NEXT: ret <2 x i32> [[MAX_ABC]]
752 %cmp_ab = icmp sgt <2 x i32> %a, %b
753 %max_ab = select <2 x i1> %cmp_ab, <2 x i32> %a, <2 x i32> %b
754 %cmp_cb = icmp sgt <2 x i32> %c, %b
755 %max_cb = select <2 x i1> %cmp_cb, <2 x i32> %c, <2 x i32> %b
756 %cmp_ab_cb = icmp sgt <2 x i32> %max_ab, %max_cb
757 %max_abc = select <2 x i1> %cmp_ab_cb, <2 x i32> %max_ab, <2 x i32> %max_cb
758 ret <2 x i32> %max_abc
761 ; min(min(b, c), min(a, b)) --> min(min(b, c), a)
763 define <2 x i32> @common_factor_umin(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) {
764 ; CHECK-LABEL: @common_factor_umin(
765 ; CHECK-NEXT: [[MIN_AB:%.*]] = call <2 x i32> @llvm.umin.v2i32(<2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]])
766 ; CHECK-NEXT: [[MIN_ABC:%.*]] = call <2 x i32> @llvm.umin.v2i32(<2 x i32> [[MIN_AB]], <2 x i32> [[C:%.*]])
767 ; CHECK-NEXT: ret <2 x i32> [[MIN_ABC]]
769 %cmp_bc = icmp ult <2 x i32> %b, %c
770 %min_bc = select <2 x i1> %cmp_bc, <2 x i32> %b, <2 x i32> %c
771 %cmp_ab = icmp ult <2 x i32> %a, %b
772 %min_ab = select <2 x i1> %cmp_ab, <2 x i32> %a, <2 x i32> %b
773 %cmp_bc_ab = icmp ult <2 x i32> %min_bc, %min_ab
774 %min_abc = select <2 x i1> %cmp_bc_ab, <2 x i32> %min_bc, <2 x i32> %min_ab
775 ret <2 x i32> %min_abc
778 ; max(max(b, c), max(b, a)) --> max(max(b, c), a)
780 define i32 @common_factor_umax(i32 %a, i32 %b, i32 %c) {
781 ; CHECK-LABEL: @common_factor_umax(
782 ; CHECK-NEXT: [[MAX_BA:%.*]] = call i32 @llvm.umax.i32(i32 [[B:%.*]], i32 [[A:%.*]])
783 ; CHECK-NEXT: [[MAX_ABC:%.*]] = call i32 @llvm.umax.i32(i32 [[MAX_BA]], i32 [[C:%.*]])
784 ; CHECK-NEXT: ret i32 [[MAX_ABC]]
786 %cmp_bc = icmp ugt i32 %b, %c
787 %max_bc = select i1 %cmp_bc, i32 %b, i32 %c
788 %cmp_ba = icmp ugt i32 %b, %a
789 %max_ba = select i1 %cmp_ba, i32 %b, i32 %a
790 %cmp_bc_ba = icmp ugt i32 %max_bc, %max_ba
791 %max_abc = select i1 %cmp_bc_ba, i32 %max_bc, i32 %max_ba
795 declare void @extra_use(i32)
797 define i32 @common_factor_umax_extra_use_lhs(i32 %a, i32 %b, i32 %c) {
798 ; CHECK-LABEL: @common_factor_umax_extra_use_lhs(
799 ; CHECK-NEXT: [[MAX_BC:%.*]] = call i32 @llvm.umax.i32(i32 [[B:%.*]], i32 [[C:%.*]])
800 ; CHECK-NEXT: [[MAX_ABC:%.*]] = call i32 @llvm.umax.i32(i32 [[MAX_BC]], i32 [[A:%.*]])
801 ; CHECK-NEXT: call void @extra_use(i32 [[MAX_BC]])
802 ; CHECK-NEXT: ret i32 [[MAX_ABC]]
804 %cmp_bc = icmp ugt i32 %b, %c
805 %max_bc = select i1 %cmp_bc, i32 %b, i32 %c
806 %cmp_ba = icmp ugt i32 %b, %a
807 %max_ba = select i1 %cmp_ba, i32 %b, i32 %a
808 %cmp_bc_ba = icmp ugt i32 %max_bc, %max_ba
809 %max_abc = select i1 %cmp_bc_ba, i32 %max_bc, i32 %max_ba
810 call void @extra_use(i32 %max_bc)
814 define i32 @common_factor_umax_extra_use_rhs(i32 %a, i32 %b, i32 %c) {
815 ; CHECK-LABEL: @common_factor_umax_extra_use_rhs(
816 ; CHECK-NEXT: [[MAX_BA:%.*]] = call i32 @llvm.umax.i32(i32 [[B:%.*]], i32 [[A:%.*]])
817 ; CHECK-NEXT: [[MAX_ABC:%.*]] = call i32 @llvm.umax.i32(i32 [[MAX_BA]], i32 [[C:%.*]])
818 ; CHECK-NEXT: call void @extra_use(i32 [[MAX_BA]])
819 ; CHECK-NEXT: ret i32 [[MAX_ABC]]
821 %cmp_bc = icmp ugt i32 %b, %c
822 %max_bc = select i1 %cmp_bc, i32 %b, i32 %c
823 %cmp_ba = icmp ugt i32 %b, %a
824 %max_ba = select i1 %cmp_ba, i32 %b, i32 %a
825 %cmp_bc_ba = icmp ugt i32 %max_bc, %max_ba
826 %max_abc = select i1 %cmp_bc_ba, i32 %max_bc, i32 %max_ba
827 call void @extra_use(i32 %max_ba)
831 define i32 @common_factor_umax_extra_use_both(i32 %a, i32 %b, i32 %c) {
832 ; CHECK-LABEL: @common_factor_umax_extra_use_both(
833 ; CHECK-NEXT: [[MAX_BC:%.*]] = call i32 @llvm.umax.i32(i32 [[B:%.*]], i32 [[C:%.*]])
834 ; CHECK-NEXT: [[MAX_BA:%.*]] = call i32 @llvm.umax.i32(i32 [[B]], i32 [[A:%.*]])
835 ; CHECK-NEXT: [[MAX_ABC:%.*]] = call i32 @llvm.umax.i32(i32 [[MAX_BC]], i32 [[MAX_BA]])
836 ; CHECK-NEXT: call void @extra_use(i32 [[MAX_BC]])
837 ; CHECK-NEXT: call void @extra_use(i32 [[MAX_BA]])
838 ; CHECK-NEXT: ret i32 [[MAX_ABC]]
840 %cmp_bc = icmp ugt i32 %b, %c
841 %max_bc = select i1 %cmp_bc, i32 %b, i32 %c
842 %cmp_ba = icmp ugt i32 %b, %a
843 %max_ba = select i1 %cmp_ba, i32 %b, i32 %a
844 %cmp_bc_ba = icmp ugt i32 %max_bc, %max_ba
845 %max_abc = select i1 %cmp_bc_ba, i32 %max_bc, i32 %max_ba
846 call void @extra_use(i32 %max_bc)
847 call void @extra_use(i32 %max_ba)
851 ; This would assert. Don't assume that earlier min/max types match a possible later min/max.
853 define float @not_min_of_min(i8 %i, float %x) {
854 ; CHECK-LABEL: @not_min_of_min(
855 ; CHECK-NEXT: [[MIN1:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float 1.000000e+00)
856 ; CHECK-NEXT: [[MIN2:%.*]] = call fast float @llvm.minnum.f32(float [[X]], float 2.000000e+00)
857 ; CHECK-NEXT: [[CMP3:%.*]] = icmp ult i8 [[I:%.*]], 16
858 ; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP3]], float [[MIN1]], float [[MIN2]]
859 ; CHECK-NEXT: ret float [[R]]
861 %cmp1 = fcmp fast ult float %x, 1.0
862 %min1 = select i1 %cmp1, float %x, float 1.0
863 %cmp2 = fcmp fast ult float %x, 2.0
864 %min2 = select i1 %cmp2, float %x, float 2.0
865 %cmp3 = icmp ult i8 %i, 16
866 %r = select i1 %cmp3, float %min1, float %min2
870 define i32 @add_umin(i32 %x) {
871 ; CHECK-LABEL: @add_umin(
872 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 27)
873 ; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[TMP1]], 15
874 ; CHECK-NEXT: ret i32 [[R]]
876 %a = add nuw i32 %x, 15
877 %c = icmp ult i32 %a, 42
878 %r = select i1 %c, i32 %a, i32 42
882 define i32 @add_umin_constant_limit(i32 %x) {
883 ; CHECK-LABEL: @add_umin_constant_limit(
884 ; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq i32 [[X:%.*]], 0
885 ; CHECK-NEXT: [[R:%.*]] = select i1 [[DOTNOT]], i32 41, i32 42
886 ; CHECK-NEXT: ret i32 [[R]]
888 %a = add nuw i32 %x, 41
889 %c = icmp ult i32 %a, 42
890 %r = select i1 %c, i32 %a, i32 42
895 ; TODO: assert that instsimplify always gets this?
897 define i32 @add_umin_simplify(i32 %x) {
898 ; CHECK-LABEL: @add_umin_simplify(
899 ; CHECK-NEXT: ret i32 42
901 %a = add nuw i32 %x, 42
902 %c = icmp ult i32 %a, 42
903 %r = select i1 %c, i32 %a, i32 42
908 ; TODO: assert that instsimplify always gets this?
910 define i32 @add_umin_simplify2(i32 %x) {
911 ; CHECK-LABEL: @add_umin_simplify2(
912 ; CHECK-NEXT: ret i32 42
914 %a = add nuw i32 %x, 43
915 %c = icmp ult i32 %a, 42
916 %r = select i1 %c, i32 %a, i32 42
922 define i32 @add_umin_wrong_pred(i32 %x) {
923 ; CHECK-LABEL: @add_umin_wrong_pred(
924 ; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 15
925 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[A]], i32 42)
926 ; CHECK-NEXT: ret i32 [[R]]
928 %a = add nuw i32 %x, 15
929 %c = icmp slt i32 %a, 42
930 %r = select i1 %c, i32 %a, i32 42
936 define i32 @add_umin_wrong_wrap(i32 %x) {
937 ; CHECK-LABEL: @add_umin_wrong_wrap(
938 ; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], 15
939 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umin.i32(i32 [[A]], i32 42)
940 ; CHECK-NEXT: ret i32 [[R]]
942 %a = add nsw i32 %x, 15
943 %c = icmp ult i32 %a, 42
944 %r = select i1 %c, i32 %a, i32 42
950 define i32 @add_umin_extra_use(i32 %x, ptr %p) {
951 ; CHECK-LABEL: @add_umin_extra_use(
952 ; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 15
953 ; CHECK-NEXT: store i32 [[A]], ptr [[P:%.*]], align 4
954 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umin.i32(i32 [[A]], i32 42)
955 ; CHECK-NEXT: ret i32 [[R]]
957 %a = add nuw i32 %x, 15
959 %c = icmp ult i32 %a, 42
960 %r = select i1 %c, i32 %a, i32 42
964 define <2 x i16> @add_umin_vec(<2 x i16> %x) {
965 ; CHECK-LABEL: @add_umin_vec(
966 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i16> @llvm.umin.v2i16(<2 x i16> [[X:%.*]], <2 x i16> splat (i16 225))
967 ; CHECK-NEXT: [[R:%.*]] = add nuw nsw <2 x i16> [[TMP1]], splat (i16 15)
968 ; CHECK-NEXT: ret <2 x i16> [[R]]
970 %a = add nuw <2 x i16> %x, <i16 15, i16 15>
971 %c = icmp ult <2 x i16> %a, <i16 240, i16 240>
972 %r = select <2 x i1> %c, <2 x i16> %a, <2 x i16> <i16 240, i16 240>
976 define i37 @add_umax(i37 %x) {
977 ; CHECK-LABEL: @add_umax(
978 ; CHECK-NEXT: [[TMP1:%.*]] = call i37 @llvm.umax.i37(i37 [[X:%.*]], i37 37)
979 ; CHECK-NEXT: [[R:%.*]] = add nuw i37 [[TMP1]], 5
980 ; CHECK-NEXT: ret i37 [[R]]
982 %a = add nuw i37 %x, 5
983 %c = icmp ugt i37 %a, 42
984 %r = select i1 %c, i37 %a, i37 42
988 define i37 @add_umax_constant_limit(i37 %x) {
989 ; CHECK-LABEL: @add_umax_constant_limit(
990 ; CHECK-NEXT: [[TMP1:%.*]] = call i37 @llvm.umax.i37(i37 [[X:%.*]], i37 1)
991 ; CHECK-NEXT: [[R:%.*]] = add nuw i37 [[TMP1]], 81
992 ; CHECK-NEXT: ret i37 [[R]]
994 %a = add nuw i37 %x, 81
995 %c = icmp ugt i37 %a, 82
996 %r = select i1 %c, i37 %a, i37 82
1001 ; TODO: assert that instsimplify always gets this?
1003 define i37 @add_umax_simplify(i37 %x) {
1004 ; CHECK-LABEL: @add_umax_simplify(
1005 ; CHECK-NEXT: [[A:%.*]] = add nuw i37 [[X:%.*]], 42
1006 ; CHECK-NEXT: ret i37 [[A]]
1008 %a = add nuw i37 %x, 42
1009 %c = icmp ugt i37 %a, 42
1010 %r = select i1 %c, i37 %a, i37 42
1015 ; TODO: assert that instsimplify always gets this?
1017 define i32 @add_umax_simplify2(i32 %x) {
1018 ; CHECK-LABEL: @add_umax_simplify2(
1019 ; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 57
1020 ; CHECK-NEXT: ret i32 [[A]]
1022 %a = add nuw i32 %x, 57
1023 %c = icmp ugt i32 %a, 56
1024 %r = select i1 %c, i32 %a, i32 56
1030 define i32 @add_umax_wrong_pred(i32 %x) {
1031 ; CHECK-LABEL: @add_umax_wrong_pred(
1032 ; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 15
1033 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smax.i32(i32 [[A]], i32 42)
1034 ; CHECK-NEXT: ret i32 [[R]]
1036 %a = add nuw i32 %x, 15
1037 %c = icmp sgt i32 %a, 42
1038 %r = select i1 %c, i32 %a, i32 42
1044 ; Without the nuw that would allow pushing the add through the umax, the
1045 ; add + icmp ugt combination can be interpreted as a range check, and would
1046 ; normally be canonicalized to use ult instead. However, this is not done when
1047 ; used as part of a umax to avoid breaking the SPF pattern.
1048 define i32 @add_umax_wrong_wrap(i32 %x) {
1049 ; CHECK-LABEL: @add_umax_wrong_wrap(
1050 ; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], 15
1051 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umax.i32(i32 [[A]], i32 42)
1052 ; CHECK-NEXT: ret i32 [[R]]
1054 %a = add nsw i32 %x, 15
1055 %c = icmp ugt i32 %a, 42
1056 %r = select i1 %c, i32 %a, i32 42
1062 define i32 @add_umax_extra_use(i32 %x, ptr %p) {
1063 ; CHECK-LABEL: @add_umax_extra_use(
1064 ; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 15
1065 ; CHECK-NEXT: store i32 [[A]], ptr [[P:%.*]], align 4
1066 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umax.i32(i32 [[A]], i32 42)
1067 ; CHECK-NEXT: ret i32 [[R]]
1069 %a = add nuw i32 %x, 15
1070 store i32 %a, ptr %p
1071 %c = icmp ugt i32 %a, 42
1072 %r = select i1 %c, i32 %a, i32 42
1076 define <2 x i33> @add_umax_vec(<2 x i33> %x) {
1077 ; CHECK-LABEL: @add_umax_vec(
1078 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i33> @llvm.umax.v2i33(<2 x i33> [[X:%.*]], <2 x i33> splat (i33 235))
1079 ; CHECK-NEXT: [[R:%.*]] = add nuw <2 x i33> [[TMP1]], splat (i33 5)
1080 ; CHECK-NEXT: ret <2 x i33> [[R]]
1082 %a = add nuw <2 x i33> %x, <i33 5, i33 5>
1083 %c = icmp ugt <2 x i33> %a, <i33 240, i33 240>
1084 %r = select <2 x i1> %c, <2 x i33> %a, <2 x i33> <i33 240, i33 240>
1088 define i8 @PR14613_umin(i8 %x) {
1089 ; CHECK-LABEL: @PR14613_umin(
1090 ; CHECK-NEXT: [[NARROW:%.*]] = call i8 @llvm.uadd.sat.i8(i8 [[X:%.*]], i8 15)
1091 ; CHECK-NEXT: ret i8 [[NARROW]]
1093 %u4 = zext i8 %x to i32
1094 %u5 = add nuw nsw i32 %u4, 15
1095 %u6 = icmp ult i32 %u5, 255
1096 %u7 = select i1 %u6, i32 %u5, i32 255
1097 %r = trunc i32 %u7 to i8
1101 define i8 @PR14613_umax(i8 %x) {
1102 ; CHECK-LABEL: @PR14613_umax(
1103 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 -16)
1104 ; CHECK-NEXT: [[U7:%.*]] = add nsw i8 [[TMP1]], 15
1105 ; CHECK-NEXT: ret i8 [[U7]]
1107 %u4 = zext i8 %x to i32
1108 %u5 = add nuw nsw i32 %u4, 15
1109 %u6 = icmp ugt i32 %u5, 255
1110 %u7 = select i1 %u6, i32 %u5, i32 255
1111 %r = trunc i32 %u7 to i8
1115 define i32 @add_smin(i32 %x) {
1116 ; CHECK-LABEL: @add_smin(
1117 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 27)
1118 ; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], 15
1119 ; CHECK-NEXT: ret i32 [[R]]
1121 %a = add nsw i32 %x, 15
1122 %c = icmp slt i32 %a, 42
1123 %r = select i1 %c, i32 %a, i32 42
1127 define i32 @add_smin_constant_limit(i32 %x) {
1128 ; CHECK-LABEL: @add_smin_constant_limit(
1129 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 2147483646)
1130 ; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], -3
1131 ; CHECK-NEXT: ret i32 [[R]]
1133 %a = add nsw i32 %x, -3
1134 %c = icmp slt i32 %a, 2147483643
1135 %r = select i1 %c, i32 %a, i32 2147483643
1140 ; TODO: assert that instsimplify always gets this?
1142 define i32 @add_smin_simplify(i32 %x) {
1143 ; CHECK-LABEL: @add_smin_simplify(
1144 ; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], -3
1145 ; CHECK-NEXT: ret i32 [[A]]
1147 %a = add nsw i32 %x, -3
1148 %c = icmp slt i32 %a, 2147483644
1149 %r = select i1 %c, i32 %a, i32 2147483644
1154 ; TODO: assert that instsimplify always gets this?
1156 define i32 @add_smin_simplify2(i32 %x) {
1157 ; CHECK-LABEL: @add_smin_simplify2(
1158 ; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], -3
1159 ; CHECK-NEXT: ret i32 [[A]]
1161 %a = add nsw i32 %x, -3
1162 %c = icmp slt i32 %a, 2147483645
1163 %r = select i1 %c, i32 %a, i32 2147483645
1169 define i32 @add_smin_wrong_pred(i32 %x) {
1170 ; CHECK-LABEL: @add_smin_wrong_pred(
1171 ; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], 15
1172 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umin.i32(i32 [[A]], i32 42)
1173 ; CHECK-NEXT: ret i32 [[R]]
1175 %a = add nsw i32 %x, 15
1176 %c = icmp ult i32 %a, 42
1177 %r = select i1 %c, i32 %a, i32 42
1183 define i32 @add_smin_wrong_wrap(i32 %x) {
1184 ; CHECK-LABEL: @add_smin_wrong_wrap(
1185 ; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 15
1186 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[A]], i32 42)
1187 ; CHECK-NEXT: ret i32 [[R]]
1189 %a = add nuw i32 %x, 15
1190 %c = icmp slt i32 %a, 42
1191 %r = select i1 %c, i32 %a, i32 42
1197 define i32 @add_smin_extra_use(i32 %x, ptr %p) {
1198 ; CHECK-LABEL: @add_smin_extra_use(
1199 ; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], 15
1200 ; CHECK-NEXT: store i32 [[A]], ptr [[P:%.*]], align 4
1201 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[A]], i32 42)
1202 ; CHECK-NEXT: ret i32 [[R]]
1204 %a = add nsw i32 %x, 15
1205 store i32 %a, ptr %p
1206 %c = icmp slt i32 %a, 42
1207 %r = select i1 %c, i32 %a, i32 42
1211 define <2 x i16> @add_smin_vec(<2 x i16> %x) {
1212 ; CHECK-LABEL: @add_smin_vec(
1213 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i16> @llvm.smin.v2i16(<2 x i16> [[X:%.*]], <2 x i16> splat (i16 225))
1214 ; CHECK-NEXT: [[R:%.*]] = add nsw <2 x i16> [[TMP1]], splat (i16 15)
1215 ; CHECK-NEXT: ret <2 x i16> [[R]]
1217 %a = add nsw <2 x i16> %x, <i16 15, i16 15>
1218 %c = icmp slt <2 x i16> %a, <i16 240, i16 240>
1219 %r = select <2 x i1> %c, <2 x i16> %a, <2 x i16> <i16 240, i16 240>
1223 define i37 @add_smax(i37 %x) {
1224 ; CHECK-LABEL: @add_smax(
1225 ; CHECK-NEXT: [[TMP1:%.*]] = call i37 @llvm.smax.i37(i37 [[X:%.*]], i37 37)
1226 ; CHECK-NEXT: [[R:%.*]] = add nuw nsw i37 [[TMP1]], 5
1227 ; CHECK-NEXT: ret i37 [[R]]
1229 %a = add nsw i37 %x, 5
1230 %c = icmp sgt i37 %a, 42
1231 %r = select i1 %c, i37 %a, i37 42
1235 define i8 @add_smax_constant_limit(i8 %x) {
1236 ; CHECK-LABEL: @add_smax_constant_limit(
1237 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.smax.i8(i8 [[X:%.*]], i8 -127)
1238 ; CHECK-NEXT: [[R:%.*]] = add nsw i8 [[TMP1]], 125
1239 ; CHECK-NEXT: ret i8 [[R]]
1241 %a = add nsw i8 %x, 125
1242 %c = icmp sgt i8 %a, -2
1243 %r = select i1 %c, i8 %a, i8 -2
1248 ; TODO: assert that instsimplify always gets this?
1250 define i8 @add_smax_simplify(i8 %x) {
1251 ; CHECK-LABEL: @add_smax_simplify(
1252 ; CHECK-NEXT: [[A:%.*]] = add nsw i8 [[X:%.*]], 126
1253 ; CHECK-NEXT: ret i8 [[A]]
1255 %a = add nsw i8 %x, 126
1256 %c = icmp sgt i8 %a, -2
1257 %r = select i1 %c, i8 %a, i8 -2
1262 ; TODO: assert that instsimplify always gets this?
1264 define i8 @add_smax_simplify2(i8 %x) {
1265 ; CHECK-LABEL: @add_smax_simplify2(
1266 ; CHECK-NEXT: [[A:%.*]] = add nsw i8 [[X:%.*]], 127
1267 ; CHECK-NEXT: ret i8 [[A]]
1269 %a = add nsw i8 %x, 127
1270 %c = icmp sgt i8 %a, -2
1271 %r = select i1 %c, i8 %a, i8 -2
1277 define i32 @add_smax_wrong_pred(i32 %x) {
1278 ; CHECK-LABEL: @add_smax_wrong_pred(
1279 ; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], 15
1280 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umax.i32(i32 [[A]], i32 42)
1281 ; CHECK-NEXT: ret i32 [[R]]
1283 %a = add nsw i32 %x, 15
1284 %c = icmp ugt i32 %a, 42
1285 %r = select i1 %c, i32 %a, i32 42
1291 define i32 @add_smax_wrong_wrap(i32 %x) {
1292 ; CHECK-LABEL: @add_smax_wrong_wrap(
1293 ; CHECK-NEXT: [[A:%.*]] = add nuw i32 [[X:%.*]], 15
1294 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smax.i32(i32 [[A]], i32 42)
1295 ; CHECK-NEXT: ret i32 [[R]]
1297 %a = add nuw i32 %x, 15
1298 %c = icmp sgt i32 %a, 42
1299 %r = select i1 %c, i32 %a, i32 42
1305 define i32 @add_smax_extra_use(i32 %x, ptr %p) {
1306 ; CHECK-LABEL: @add_smax_extra_use(
1307 ; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[X:%.*]], 15
1308 ; CHECK-NEXT: store i32 [[A]], ptr [[P:%.*]], align 4
1309 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smax.i32(i32 [[A]], i32 42)
1310 ; CHECK-NEXT: ret i32 [[R]]
1312 %a = add nsw i32 %x, 15
1313 store i32 %a, ptr %p
1314 %c = icmp sgt i32 %a, 42
1315 %r = select i1 %c, i32 %a, i32 42
1319 define <2 x i33> @add_smax_vec(<2 x i33> %x) {
1320 ; CHECK-LABEL: @add_smax_vec(
1321 ; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i33> @llvm.smax.v2i33(<2 x i33> [[X:%.*]], <2 x i33> splat (i33 235))
1322 ; CHECK-NEXT: [[R:%.*]] = add nuw nsw <2 x i33> [[TMP1]], splat (i33 5)
1323 ; CHECK-NEXT: ret <2 x i33> [[R]]
1325 %a = add nsw <2 x i33> %x, <i33 5, i33 5>
1326 %c = icmp sgt <2 x i33> %a, <i33 240, i33 240>
1327 %r = select <2 x i1> %c, <2 x i33> %a, <2 x i33> <i33 240, i33 240>
1331 define i8 @PR14613_smin(i8 %x) {
1332 ; CHECK-LABEL: @PR14613_smin(
1333 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.smin.i8(i8 [[X:%.*]], i8 40)
1334 ; CHECK-NEXT: [[NARROW:%.*]] = add nsw i8 [[TMP1]], 15
1335 ; CHECK-NEXT: ret i8 [[NARROW]]
1337 %u4 = sext i8 %x to i32
1338 %u5 = add nuw nsw i32 %u4, 15
1339 %u6 = icmp slt i32 %u5, 55
1340 %u7 = select i1 %u6, i32 %u5, i32 55
1341 %r = trunc i32 %u7 to i8
1345 define i8 @PR14613_smax(i8 %x) {
1346 ; CHECK-LABEL: @PR14613_smax(
1347 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.smax.i8(i8 [[X:%.*]], i8 40)
1348 ; CHECK-NEXT: [[NARROW:%.*]] = add nuw i8 [[TMP1]], 15
1349 ; CHECK-NEXT: ret i8 [[NARROW]]
1351 %u4 = sext i8 %x to i32
1352 %u5 = add nuw nsw i32 %u4, 15
1353 %u6 = icmp sgt i32 %u5, 55
1354 %u7 = select i1 %u6, i32 %u5, i32 55
1355 %r = trunc i32 %u7 to i8
1359 define i8 @PR46271(<2 x i8> %x) {
1360 ; CHECK-LABEL: @PR46271(
1361 ; CHECK-NEXT: [[NOT:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[X:%.*]], <2 x i8> splat (i8 -1))
1362 ; CHECK-NEXT: [[R:%.*]] = extractelement <2 x i8> [[NOT]], i64 1
1363 ; CHECK-NEXT: [[R1:%.*]] = xor i8 [[R]], -1
1364 ; CHECK-NEXT: ret i8 [[R1]]
1366 %a = icmp sgt <2 x i8> %x, <i8 -1, i8 -1>
1367 %b = select <2 x i1> %a, <2 x i8> %x, <2 x i8> <i8 poison, i8 -1>
1368 %not = xor <2 x i8> %b, <i8 poison, i8 -1>
1369 %r = extractelement <2 x i8> %not, i32 1
1373 define i32 @twoway_clamp_lt(i32 %num) {
1374 ; CHECK-LABEL: @twoway_clamp_lt(
1375 ; CHECK-NEXT: entry:
1376 ; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[NUM:%.*]], 13767
1377 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP0]], i32 13768, i32 13767
1378 ; CHECK-NEXT: ret i32 [[R]]
1381 %cmp1 = icmp slt i32 %num, 13768
1382 %s1 = select i1 %cmp1, i32 %num, i32 13768
1383 %cmp2 = icmp sgt i32 %s1, 13767
1384 %r = select i1 %cmp2, i32 %s1, i32 13767
1388 define i32 @twoway_clamp_gt(i32 %num) {
1389 ; CHECK-LABEL: @twoway_clamp_gt(
1390 ; CHECK-NEXT: entry:
1391 ; CHECK-NEXT: [[S1:%.*]] = call i32 @llvm.smax.i32(i32 [[NUM:%.*]], i32 13767)
1392 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.umin.i32(i32 [[S1]], i32 13768)
1393 ; CHECK-NEXT: ret i32 [[R]]
1396 %cmp1 = icmp sgt i32 %num, 13767
1397 %s1 = select i1 %cmp1, i32 %num, i32 13767
1398 %cmp2 = icmp slt i32 %s1, 13768
1399 %r = select i1 %cmp2, i32 %s1, i32 13768
1403 define i32 @twoway_clamp_gt_nonconst(i32 %num, i32 %k) {
1404 ; CHECK-LABEL: @twoway_clamp_gt_nonconst(
1405 ; CHECK-NEXT: entry:
1406 ; CHECK-NEXT: [[K1:%.*]] = add i32 [[K:%.*]], 1
1407 ; CHECK-NEXT: [[S1:%.*]] = call i32 @llvm.smax.i32(i32 [[NUM:%.*]], i32 [[K]])
1408 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[S1]], i32 [[K1]])
1409 ; CHECK-NEXT: ret i32 [[R]]
1413 %cmp1 = icmp sgt i32 %num, %k
1414 %s1 = select i1 %cmp1, i32 %num, i32 %k
1415 %cmp2 = icmp slt i32 %s1, %k1
1416 %r = select i1 %cmp2, i32 %s1, i32 %k1
1420 define i32 @test_umax_smax1(i32 %x) {
1421 ; CHECK-LABEL: @test_umax_smax1(
1422 ; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 1)
1423 ; CHECK-NEXT: ret i32 [[UMAX]]
1425 %smax = call i32 @llvm.smax.i32(i32 %x, i32 0)
1426 %umax = call i32 @llvm.umax.i32(i32 %smax, i32 1)
1430 define i32 @test_umax_smax2(i32 %x) {
1431 ; CHECK-LABEL: @test_umax_smax2(
1432 ; CHECK-NEXT: [[SMAX:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 20)
1433 ; CHECK-NEXT: ret i32 [[SMAX]]
1435 %smax = call i32 @llvm.smax.i32(i32 %x, i32 20)
1436 %umax = call i32 @llvm.umax.i32(i32 %smax, i32 10)
1440 define <2 x i32> @test_umax_smax_vec(<2 x i32> %x) {
1441 ; CHECK-LABEL: @test_umax_smax_vec(
1442 ; CHECK-NEXT: [[UMAX:%.*]] = call <2 x i32> @llvm.smax.v2i32(<2 x i32> [[X:%.*]], <2 x i32> <i32 1, i32 20>)
1443 ; CHECK-NEXT: ret <2 x i32> [[UMAX]]
1445 %smax = call <2 x i32> @llvm.smax.v2i32(<2 x i32> %x, <2 x i32> <i32 0, i32 20>)
1446 %umax = call <2 x i32> @llvm.umax.v2i32(<2 x i32> %smax, <2 x i32> <i32 1, i32 10>)
1450 define i32 @test_smin_umin1(i32 %x) {
1451 ; CHECK-LABEL: @test_smin_umin1(
1452 ; CHECK-NEXT: [[SMIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 10)
1453 ; CHECK-NEXT: ret i32 [[SMIN]]
1455 %smin = call i32 @llvm.umin.i32(i32 %x, i32 10)
1456 %umin = call i32 @llvm.smin.i32(i32 %smin, i32 20)
1460 define i32 @test_smin_umin2(i32 %x) {
1461 ; CHECK-LABEL: @test_smin_umin2(
1462 ; CHECK-NEXT: [[UMIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 10)
1463 ; CHECK-NEXT: ret i32 [[UMIN]]
1465 %smin = call i32 @llvm.umin.i32(i32 %x, i32 20)
1466 %umin = call i32 @llvm.smin.i32(i32 %smin, i32 10)
1470 define <2 x i32> @test_smin_umin_vec(<2 x i32> %x) {
1471 ; CHECK-LABEL: @test_smin_umin_vec(
1472 ; CHECK-NEXT: [[UMIN:%.*]] = call <2 x i32> @llvm.umin.v2i32(<2 x i32> [[X:%.*]], <2 x i32> splat (i32 10))
1473 ; CHECK-NEXT: ret <2 x i32> [[UMIN]]
1475 %smin = call <2 x i32> @llvm.umin.v2i32(<2 x i32> %x, <2 x i32> <i32 10, i32 20>)
1476 %umin = call <2 x i32> @llvm.smin.v2i32(<2 x i32> %smin, <2 x i32> <i32 20, i32 10>)
1482 define i32 @test_umax_smax3(i32 %x) {
1483 ; CHECK-LABEL: @test_umax_smax3(
1484 ; CHECK-NEXT: ret i32 -1
1486 %smax = call i32 @llvm.smax.i32(i32 %x, i32 0)
1487 %umax = call i32 @llvm.umax.i32(i32 %smax, i32 -1)
1491 define i32 @test_umax_smax4(i32 %x) {
1492 ; CHECK-LABEL: @test_umax_smax4(
1493 ; CHECK-NEXT: [[SMAX:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 -20)
1494 ; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[SMAX]], i32 10)
1495 ; CHECK-NEXT: ret i32 [[UMAX]]
1497 %smax = call i32 @llvm.smax.i32(i32 %x, i32 -20)
1498 %umax = call i32 @llvm.umax.i32(i32 %smax, i32 10)
1502 define i32 @test_smin_umin3(i32 %x) {
1503 ; CHECK-LABEL: @test_smin_umin3(
1504 ; CHECK-NEXT: ret i32 -20
1506 %smin = call i32 @llvm.umin.i32(i32 %x, i32 10)
1507 %umin = call i32 @llvm.smin.i32(i32 %smin, i32 -20)
1511 define i32 @test_smin_umin4(i32 %x) {
1512 ; CHECK-LABEL: @test_smin_umin4(
1513 ; CHECK-NEXT: [[SMIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 -20)
1514 ; CHECK-NEXT: [[UMIN:%.*]] = call i32 @llvm.smin.i32(i32 [[SMIN]], i32 10)
1515 ; CHECK-NEXT: ret i32 [[UMIN]]
1517 %smin = call i32 @llvm.umin.i32(i32 %x, i32 -20)
1518 %umin = call i32 @llvm.smin.i32(i32 %smin, i32 10)
1522 define i32 @test_umax_nonminmax(i32 %x) {
1523 ; CHECK-LABEL: @test_umax_nonminmax(
1524 ; CHECK-NEXT: [[Y:%.*]] = call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 [[X:%.*]])
1525 ; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[Y]], i32 1)
1526 ; CHECK-NEXT: ret i32 [[UMAX]]
1528 %y = call i32 @llvm.ctpop.i32(i32 %x)
1529 %umax = call i32 @llvm.umax.i32(i32 %y, i32 1)
1533 define <2 x i32> @test_umax_smax_vec_neg(<2 x i32> %x) {
1534 ; CHECK-LABEL: @test_umax_smax_vec_neg(
1535 ; CHECK-NEXT: [[SMAX:%.*]] = call <2 x i32> @llvm.smax.v2i32(<2 x i32> [[X:%.*]], <2 x i32> <i32 0, i32 -20>)
1536 ; CHECK-NEXT: [[UMAX:%.*]] = call <2 x i32> @llvm.umax.v2i32(<2 x i32> [[SMAX]], <2 x i32> <i32 1, i32 10>)
1537 ; CHECK-NEXT: ret <2 x i32> [[UMAX]]
1539 %smax = call <2 x i32> @llvm.smax.v2i32(<2 x i32> %x, <2 x i32> <i32 0, i32 -20>)
1540 %umax = call <2 x i32> @llvm.umax.v2i32(<2 x i32> %smax, <2 x i32> <i32 1, i32 10>)