Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / test / Transforms / InstCombine / minmax-fp.ll
blob11418156a4858451cd71af78750ca498bd458a09
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) {
6 ; CHECK-LABEL: @t1(
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
15   ret double %3
18 ; Check this is converted into canonical form, as above.
19 define double @t2(float %a) {
20 ; CHECK-LABEL: @t2(
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
29   ret double %3
32 ; Same again, with trunc.
33 define float @t4(double %a) {
34 ; CHECK-LABEL: @t4(
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
43   ret float %3
46 ; different values, should not be converted.
47 define double @t5(float %a) {
48 ; CHECK-LABEL: @t5(
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
57   ret double %3
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) {
64 ; CHECK-LABEL: @t6(
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
73   ret double %3
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) {
80 ; CHECK-LABEL: @t7(
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
89   ret double %3
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
104   ret float %min2
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
119   ret float %max2
122 define i64 @t8(float %a) {
123 ; CHECK-LABEL: @t8(
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
132   ret i64 %3
135 define i8 @t9(float %a) {
136 ; CHECK-LABEL: @t9(
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
145   ret i8 %3
148   ; Either operand could be NaN, but fast modifier applied.
149 define i8 @t11(float %a, float %b) {
150 ; CHECK-LABEL: @t11(
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
160   ret i8 %4
163 ; Either operand could be NaN, but nnan modifier applied.
164 define i8 @t12(float %a, float %b) {
165 ; CHECK-LABEL: @t12(
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
175   ret i8 %4
178 ; Float and int values do not match.
179 define i8 @t13(float %a) {
180 ; CHECK-LABEL: @t13(
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
189   ret i8 %3
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) {
194 ; CHECK-LABEL: @t14(
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
203   ret i8 %3
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
216   ret i8 %3
219 define i8 @t15(float %a) {
220 ; CHECK-LABEL: @t15(
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
229   ret i8 %3
232 define double @t16(i32 %x) {
233 ; CHECK-LABEL: @t16(
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
242   ret double %sel
245 define double @t17(i32 %x) {
246 ; CHECK-LABEL: @t17(
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
255   ret double %sel