1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
4 ; This is the canonical form for a type-changing min/max.
5 define double @t1(float %a) {
7 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], 5.000000e+00
8 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float 5.000000e+00, float [[A]]
9 ; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[TMP1]] to double
10 ; CHECK-NEXT: ret double [[TMP2]]
12 %1 = fcmp ult float %a, 5.0
13 %2 = select i1 %1, float %a, float 5.0
14 %3 = fpext float %2 to double
18 ; Check this is converted into canonical form, as above.
19 define double @t2(float %a) {
21 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], 5.000000e+00
22 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float 5.000000e+00, float [[A]]
23 ; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[TMP1]] to double
24 ; CHECK-NEXT: ret double [[TMP2]]
26 %1 = fcmp ult float %a, 5.0
27 %2 = fpext float %a to double
28 %3 = select i1 %1, double %2, double 5.0
32 ; Same again, with trunc.
33 define float @t4(double %a) {
35 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge double [[A:%.*]], 5.000000e+00
36 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], double 5.000000e+00, double [[A]]
37 ; CHECK-NEXT: [[TMP2:%.*]] = fptrunc double [[TMP1]] to float
38 ; CHECK-NEXT: ret float [[TMP2]]
40 %1 = fcmp ult double %a, 5.0
41 %2 = fptrunc double %a to float
42 %3 = select i1 %1, float %2, float 5.0
46 ; different values, should not be converted.
47 define double @t5(float %a) {
49 ; CHECK-NEXT: [[TMP1:%.*]] = fcmp ult float [[A:%.*]], 5.000000e+00
50 ; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[A]] to double
51 ; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], double [[TMP2]], double 5.001000e+00
52 ; CHECK-NEXT: ret double [[TMP3]]
54 %1 = fcmp ult float %a, 5.0
55 %2 = fpext float %a to double
56 %3 = select i1 %1, double %2, double 5.001
60 ; From IEEE754: "Comparisons shall ignore the sign of zero (so +0 = -0)."
61 ; So the compare constant may be treated as +0.0, and we sink the fpext.
63 define double @t6(float %a) {
65 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], 0.000000e+00
66 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float 0.000000e+00, float [[A]]
67 ; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[TMP1]] to double
68 ; CHECK-NEXT: ret double [[TMP2]]
70 %1 = fcmp ult float %a, -0.0
71 %2 = fpext float %a to double
72 %3 = select i1 %1, double %2, double 0.0
76 ; From IEEE754: "Comparisons shall ignore the sign of zero (so +0 = -0)."
77 ; So the compare constant may be treated as -0.0, and we sink the fpext.
79 define double @t7(float %a) {
81 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], 0.000000e+00
82 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float -0.000000e+00, float [[A]]
83 ; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[TMP1]] to double
84 ; CHECK-NEXT: ret double [[TMP2]]
86 %1 = fcmp ult float %a, 0.0
87 %2 = fpext float %a to double
88 %3 = select i1 %1, double %2, double -0.0
92 ; min(min(x, 0.0), 0.0) --> min(x, 0.0)
94 define float @fmin_fmin_zero_mismatch(float %x) {
95 ; CHECK-LABEL: @fmin_fmin_zero_mismatch(
96 ; CHECK-NEXT: [[TMP1:%.*]] = fcmp olt float [[X:%.*]], 0.000000e+00
97 ; CHECK-NEXT: [[MIN2:%.*]] = select i1 [[TMP1]], float [[X]], float 0.000000e+00
98 ; CHECK-NEXT: ret float [[MIN2]]
100 %cmp1 = fcmp olt float %x, -0.0
101 %min1 = select i1 %cmp1, float %x, float 0.0
102 %cmp2 = fcmp olt float %min1, 0.0
103 %min2 = select i1 %cmp2, float %min1, float 0.0
107 ; max(max(x, -0.0), -0.0) --> max(x, -0.0)
109 define float @fmax_fmax_zero_mismatch(float %x) {
110 ; CHECK-LABEL: @fmax_fmax_zero_mismatch(
111 ; CHECK-NEXT: [[TMP1:%.*]] = fcmp ogt float [[X:%.*]], -0.000000e+00
112 ; CHECK-NEXT: [[MAX11:%.*]] = select i1 [[TMP1]], float [[X]], float -0.000000e+00
113 ; CHECK-NEXT: ret float [[MAX11]]
115 %cmp1 = fcmp ogt float %x, 0.0
116 %max1 = select i1 %cmp1, float %x, float -0.0
117 %cmp2 = fcmp ogt float 0.0, %max1
118 %max2 = select i1 %cmp2, float -0.0, float %max1
122 define i64 @t8(float %a) {
124 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], 5.000000e+00
125 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float 5.000000e+00, float [[A]]
126 ; CHECK-NEXT: [[TMP2:%.*]] = fptoui float [[TMP1]] to i64
127 ; CHECK-NEXT: ret i64 [[TMP2]]
129 %1 = fcmp ult float %a, 5.0
130 %2 = fptoui float %a to i64
131 %3 = select i1 %1, i64 %2, i64 5
135 define i8 @t9(float %a) {
137 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], 0.000000e+00
138 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float 0.000000e+00, float [[A]]
139 ; CHECK-NEXT: [[TMP2:%.*]] = fptosi float [[TMP1]] to i8
140 ; CHECK-NEXT: ret i8 [[TMP2]]
142 %1 = fcmp ult float %a, 0.0
143 %2 = fptosi float %a to i8
144 %3 = select i1 %1, i8 %2, i8 0
148 ; Either operand could be NaN, but fast modifier applied.
149 define i8 @t11(float %a, float %b) {
151 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp fast oge float [[B:%.*]], [[A:%.*]]
152 ; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[DOTINV]], float [[A]], float [[B]]
153 ; CHECK-NEXT: [[TMP1:%.*]] = fptosi float [[DOTV]] to i8
154 ; CHECK-NEXT: ret i8 [[TMP1]]
156 %1 = fcmp fast ult float %b, %a
157 %2 = fptosi float %a to i8
158 %3 = fptosi float %b to i8
159 %4 = select i1 %1, i8 %3, i8 %2
163 ; Either operand could be NaN, but nnan modifier applied.
164 define i8 @t12(float %a, float %b) {
166 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp nnan oge float [[B:%.*]], [[A:%.*]]
167 ; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[DOTINV]], float [[A]], float [[B]]
168 ; CHECK-NEXT: [[TMP1:%.*]] = fptosi float [[DOTV]] to i8
169 ; CHECK-NEXT: ret i8 [[TMP1]]
171 %1 = fcmp nnan ult float %b, %a
172 %2 = fptosi float %a to i8
173 %3 = fptosi float %b to i8
174 %4 = select i1 %1, i8 %3, i8 %2
178 ; Float and int values do not match.
179 define i8 @t13(float %a) {
181 ; CHECK-NEXT: [[TMP1:%.*]] = fcmp ult float [[A:%.*]], 1.500000e+00
182 ; CHECK-NEXT: [[TMP2:%.*]] = fptosi float [[A]] to i8
183 ; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], i8 [[TMP2]], i8 1
184 ; CHECK-NEXT: ret i8 [[TMP3]]
186 %1 = fcmp ult float %a, 1.5
187 %2 = fptosi float %a to i8
188 %3 = select i1 %1, i8 %2, i8 1
192 ; %a could be -0.0, but it doesn't matter because the conversion to int is the same for 0.0 or -0.0.
193 define i8 @t14(float %a) {
195 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], 0.000000e+00
196 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float 0.000000e+00, float [[A]]
197 ; CHECK-NEXT: [[TMP2:%.*]] = fptosi float [[TMP1]] to i8
198 ; CHECK-NEXT: ret i8 [[TMP2]]
200 %1 = fcmp ule float %a, 0.0
201 %2 = fptosi float %a to i8
202 %3 = select i1 %1, i8 %2, i8 0
206 define i8 @t14_commute(float %a) {
207 ; CHECK-LABEL: @t14_commute(
208 ; CHECK-NEXT: [[TMP1:%.*]] = fcmp ogt float [[A:%.*]], 0.000000e+00
209 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], float [[A]], float 0.000000e+00
210 ; CHECK-NEXT: [[TMP3:%.*]] = fptosi float [[TMP2]] to i8
211 ; CHECK-NEXT: ret i8 [[TMP3]]
213 %1 = fcmp ule float %a, 0.0
214 %2 = fptosi float %a to i8
215 %3 = select i1 %1, i8 0, i8 %2
219 define i8 @t15(float %a) {
221 ; CHECK-NEXT: [[DOTINV:%.*]] = fcmp nsz oge float [[A:%.*]], 0.000000e+00
222 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float 0.000000e+00, float [[A]]
223 ; CHECK-NEXT: [[TMP2:%.*]] = fptosi float [[TMP1]] to i8
224 ; CHECK-NEXT: ret i8 [[TMP2]]
226 %1 = fcmp nsz ule float %a, 0.0
227 %2 = fptosi float %a to i8
228 %3 = select i1 %1, i8 %2, i8 0
232 define double @t16(i32 %x) {
234 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], 0
235 ; CHECK-NEXT: [[CST:%.*]] = sitofp i32 [[X]] to double
236 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], double [[CST]], double 5.000000e-01
237 ; CHECK-NEXT: ret double [[SEL]]
239 %cmp = icmp sgt i32 %x, 0
240 %cst = sitofp i32 %x to double
241 %sel = select i1 %cmp, double %cst, double 5.000000e-01
245 define double @t17(i32 %x) {
247 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 2
248 ; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 2
249 ; CHECK-NEXT: [[TMP2:%.*]] = sitofp i32 [[SEL1]] to double
250 ; CHECK-NEXT: ret double [[TMP2]]
252 %cmp = icmp sgt i32 %x, 2
253 %cst = sitofp i32 %x to double
254 %sel = select i1 %cmp, double %cst, double 2.0
258 define float @fneg_fmax(float %x, float %y) {
259 ; CHECK-LABEL: @fneg_fmax(
260 ; CHECK-NEXT: [[COND:%.*]] = fcmp nnan olt float [[X:%.*]], [[Y:%.*]]
261 ; CHECK-NEXT: [[MAX_V:%.*]] = select i1 [[COND]], float [[X]], float [[Y]]
262 ; CHECK-NEXT: [[MAX:%.*]] = fneg float [[MAX_V]]
263 ; CHECK-NEXT: ret float [[MAX]]
267 %cond = fcmp nnan ogt float %n1, %n2
268 %max = select i1 %cond, float %n1, float %n2
272 define <2 x float> @fsub_fmax(<2 x float> %x, <2 x float> %y) {
273 ; CHECK-LABEL: @fsub_fmax(
274 ; CHECK-NEXT: [[COND_INV:%.*]] = fcmp nnan nsz ogt <2 x float> [[X:%.*]], [[Y:%.*]]
275 ; CHECK-NEXT: [[MAX_V:%.*]] = select <2 x i1> [[COND_INV]], <2 x float> [[Y]], <2 x float> [[X]]
276 ; CHECK-NEXT: [[MAX:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[MAX_V]]
277 ; CHECK-NEXT: ret <2 x float> [[MAX]]
279 %n1 = fsub <2 x float> <float -0.0, float -0.0>, %x
280 %n2 = fsub <2 x float> <float -0.0, float -0.0>, %y
281 %cond = fcmp nsz nnan uge <2 x float> %n1, %n2
282 %max = select <2 x i1> %cond, <2 x float> %n1, <2 x float> %n2
286 define <2 x double> @fsub_fmin(<2 x double> %x, <2 x double> %y) {
287 ; CHECK-LABEL: @fsub_fmin(
288 ; CHECK-NEXT: [[COND:%.*]] = fcmp nnan ogt <2 x double> [[X:%.*]], [[Y:%.*]]
289 ; CHECK-NEXT: [[MAX_V:%.*]] = select <2 x i1> [[COND]], <2 x double> [[X]], <2 x double> [[Y]]
290 ; CHECK-NEXT: [[MAX:%.*]] = fsub <2 x double> <double -0.000000e+00, double -0.000000e+00>, [[MAX_V]]
291 ; CHECK-NEXT: ret <2 x double> [[MAX]]
293 %n1 = fsub <2 x double> <double -0.0, double -0.0>, %x
294 %n2 = fsub <2 x double> <double -0.0, double -0.0>, %y
295 %cond = fcmp nnan olt <2 x double> %n1, %n2
296 %max = select <2 x i1> %cond, <2 x double> %n1, <2 x double> %n2
297 ret <2 x double> %max
300 define double @fneg_fmin(double %x, double %y) {
301 ; CHECK-LABEL: @fneg_fmin(
302 ; CHECK-NEXT: [[COND_INV:%.*]] = fcmp nnan nsz olt double [[X:%.*]], [[Y:%.*]]
303 ; CHECK-NEXT: [[MAX_V:%.*]] = select i1 [[COND_INV]], double [[Y]], double [[X]]
304 ; CHECK-NEXT: [[MAX:%.*]] = fneg double [[MAX_V]]
305 ; CHECK-NEXT: ret double [[MAX]]
309 %cond = fcmp nsz nnan ule double %n1, %n2
310 %max = select i1 %cond, double %n1, double %n2
314 define float @maxnum_ogt_fmf_on_select(float %a, float %b) {
315 ; CHECK-LABEL: @maxnum_ogt_fmf_on_select(
316 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.maxnum.f32(float [[A:%.*]], float [[B:%.*]])
317 ; CHECK-NEXT: ret float [[TMP1]]
319 %cond = fcmp ogt float %a, %b
320 %f = select nnan nsz i1 %cond, float %a, float %b
324 define <2 x float> @maxnum_oge_fmf_on_select(<2 x float> %a, <2 x float> %b) {
325 ; CHECK-LABEL: @maxnum_oge_fmf_on_select(
326 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz <2 x float> @llvm.maxnum.v2f32(<2 x float> [[A:%.*]], <2 x float> [[B:%.*]])
327 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
329 %cond = fcmp oge <2 x float> %a, %b
330 %f = select ninf nnan nsz <2 x i1> %cond, <2 x float> %a, <2 x float> %b
334 define float @maxnum_ogt_fmf_on_fcmp(float %a, float %b) {
335 ; CHECK-LABEL: @maxnum_ogt_fmf_on_fcmp(
336 ; CHECK-NEXT: [[COND:%.*]] = fcmp nnan nsz ogt float [[A:%.*]], [[B:%.*]]
337 ; CHECK-NEXT: [[F:%.*]] = select i1 [[COND]], float [[A]], float [[B]]
338 ; CHECK-NEXT: ret float [[F]]
340 %cond = fcmp nnan nsz ogt float %a, %b
341 %f = select i1 %cond, float %a, float %b
345 define <2 x float> @maxnum_oge_fmf_on_fcmp(<2 x float> %a, <2 x float> %b) {
346 ; CHECK-LABEL: @maxnum_oge_fmf_on_fcmp(
347 ; CHECK-NEXT: [[COND:%.*]] = fcmp nnan ninf nsz oge <2 x float> [[A:%.*]], [[B:%.*]]
348 ; CHECK-NEXT: [[F:%.*]] = select <2 x i1> [[COND]], <2 x float> [[A]], <2 x float> [[B]]
349 ; CHECK-NEXT: ret <2 x float> [[F]]
351 %cond = fcmp ninf nnan nsz oge <2 x float> %a, %b
352 %f = select <2 x i1> %cond, <2 x float> %a, <2 x float> %b
356 define float @maxnum_no_nsz(float %a, float %b) {
357 ; CHECK-LABEL: @maxnum_no_nsz(
358 ; CHECK-NEXT: [[COND:%.*]] = fcmp ogt float [[A:%.*]], [[B:%.*]]
359 ; CHECK-NEXT: [[F:%.*]] = select nnan i1 [[COND]], float [[A]], float [[B]]
360 ; CHECK-NEXT: ret float [[F]]
362 %cond = fcmp ogt float %a, %b
363 %f = select nnan i1 %cond, float %a, float %b
367 define float @maxnum_no_nnan(float %a, float %b) {
368 ; CHECK-LABEL: @maxnum_no_nnan(
369 ; CHECK-NEXT: [[COND:%.*]] = fcmp oge float [[A:%.*]], [[B:%.*]]
370 ; CHECK-NEXT: [[F:%.*]] = select nsz i1 [[COND]], float [[A]], float [[B]]
371 ; CHECK-NEXT: ret float [[F]]
373 %cond = fcmp oge float %a, %b
374 %f = select nsz i1 %cond, float %a, float %b
378 define float @minnum_olt_fmf_on_select(float %a, float %b) {
379 ; CHECK-LABEL: @minnum_olt_fmf_on_select(
380 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.minnum.f32(float [[A:%.*]], float [[B:%.*]])
381 ; CHECK-NEXT: ret float [[TMP1]]
383 %cond = fcmp olt float %a, %b
384 %f = select nnan nsz i1 %cond, float %a, float %b
388 define <2 x float> @minnum_ole_fmf_on_select(<2 x float> %a, <2 x float> %b) {
389 ; CHECK-LABEL: @minnum_ole_fmf_on_select(
390 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz <2 x float> @llvm.minnum.v2f32(<2 x float> [[A:%.*]], <2 x float> [[B:%.*]])
391 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
393 %cond = fcmp ole <2 x float> %a, %b
394 %f = select ninf nnan nsz <2 x i1> %cond, <2 x float> %a, <2 x float> %b
398 define float @minnum_olt_fmf_on_fcmp(float %a, float %b) {
399 ; CHECK-LABEL: @minnum_olt_fmf_on_fcmp(
400 ; CHECK-NEXT: [[COND:%.*]] = fcmp nnan nsz olt float [[A:%.*]], [[B:%.*]]
401 ; CHECK-NEXT: [[F:%.*]] = select i1 [[COND]], float [[A]], float [[B]]
402 ; CHECK-NEXT: ret float [[F]]
404 %cond = fcmp nnan nsz olt float %a, %b
405 %f = select i1 %cond, float %a, float %b
409 define <2 x float> @minnum_ole_fmf_on_fcmp(<2 x float> %a, <2 x float> %b) {
410 ; CHECK-LABEL: @minnum_ole_fmf_on_fcmp(
411 ; CHECK-NEXT: [[COND:%.*]] = fcmp nnan ninf nsz ole <2 x float> [[A:%.*]], [[B:%.*]]
412 ; CHECK-NEXT: [[F:%.*]] = select <2 x i1> [[COND]], <2 x float> [[A]], <2 x float> [[B]]
413 ; CHECK-NEXT: ret <2 x float> [[F]]
415 %cond = fcmp ninf nnan nsz ole <2 x float> %a, %b
416 %f = select <2 x i1> %cond, <2 x float> %a, <2 x float> %b
420 define float @minnum_no_nsz(float %a, float %b) {
421 ; CHECK-LABEL: @minnum_no_nsz(
422 ; CHECK-NEXT: [[COND:%.*]] = fcmp olt float [[A:%.*]], [[B:%.*]]
423 ; CHECK-NEXT: [[F:%.*]] = select nnan i1 [[COND]], float [[A]], float [[B]]
424 ; CHECK-NEXT: ret float [[F]]
426 %cond = fcmp olt float %a, %b
427 %f = select nnan i1 %cond, float %a, float %b
431 define float @minnum_no_nnan(float %a, float %b) {
432 ; CHECK-LABEL: @minnum_no_nnan(
433 ; CHECK-NEXT: [[COND:%.*]] = fcmp ole float [[A:%.*]], [[B:%.*]]
434 ; CHECK-NEXT: [[F:%.*]] = select nsz i1 [[COND]], float [[A]], float [[B]]
435 ; CHECK-NEXT: ret float [[F]]
437 %cond = fcmp ole float %a, %b
438 %f = select nsz i1 %cond, float %a, float %b