]>
Commit | Line | Data |
---|---|---|
3d2cf79f RB |
1 | /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding. |
2 | This file is consumed by genmatch which produces gimple-match.c | |
3 | and generic-match.c from it. | |
4 | ||
85ec4feb | 5 | Copyright (C) 2014-2018 Free Software Foundation, Inc. |
3d2cf79f RB |
6 | Contributed by Richard Biener <rguenther@suse.de> |
7 | and Prathamesh Kulkarni <bilbotheelffriend@gmail.com> | |
8 | ||
9 | This file is part of GCC. | |
10 | ||
11 | GCC is free software; you can redistribute it and/or modify it under | |
12 | the terms of the GNU General Public License as published by the Free | |
13 | Software Foundation; either version 3, or (at your option) any later | |
14 | version. | |
15 | ||
16 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
17 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
18 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with GCC; see the file COPYING3. If not see | |
23 | <http://www.gnu.org/licenses/>. */ | |
24 | ||
25 | ||
26 | /* Generic tree predicates we inherit. */ | |
27 | (define_predicates | |
cc7b5acf | 28 | integer_onep integer_zerop integer_all_onesp integer_minus_onep |
53a19317 | 29 | integer_each_onep integer_truep integer_nonzerop |
cc7b5acf | 30 | real_zerop real_onep real_minus_onep |
b0eb889b | 31 | zerop |
f3582e54 | 32 | CONSTANT_CLASS_P |
887ab609 | 33 | tree_expr_nonnegative_p |
e36c1cfe | 34 | tree_expr_nonzero_p |
67dbe582 | 35 | integer_valued_real_p |
53a19317 RB |
36 | integer_pow2p |
37 | HONOR_NANS) | |
e0ee10ed | 38 | |
f84e7fd6 RB |
39 | /* Operator lists. */ |
40 | (define_operator_list tcc_comparison | |
41 | lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) | |
42 | (define_operator_list inverted_tcc_comparison | |
43 | ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq) | |
44 | (define_operator_list inverted_tcc_comparison_with_nans | |
45 | unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq) | |
534bd33b MG |
46 | (define_operator_list swapped_tcc_comparison |
47 | gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt) | |
07cdc2b8 RB |
48 | (define_operator_list simple_comparison lt le eq ne ge gt) |
49 | (define_operator_list swapped_simple_comparison gt ge eq ne le lt) | |
50 | ||
b1dc4a20 | 51 | #include "cfn-operators.pd" |
257aecb4 | 52 | |
543a9bcd RS |
53 | /* Define operand lists for math rounding functions {,i,l,ll}FN, |
54 | where the versions prefixed with "i" return an int, those prefixed with | |
55 | "l" return a long and those prefixed with "ll" return a long long. | |
56 | ||
57 | Also define operand lists: | |
58 | ||
59 | X<FN>F for all float functions, in the order i, l, ll | |
60 | X<FN> for all double functions, in the same order | |
61 | X<FN>L for all long double functions, in the same order. */ | |
62 | #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \ | |
543a9bcd RS |
63 | (define_operator_list X##FN##F BUILT_IN_I##FN##F \ |
64 | BUILT_IN_L##FN##F \ | |
65 | BUILT_IN_LL##FN##F) \ | |
66 | (define_operator_list X##FN BUILT_IN_I##FN \ | |
67 | BUILT_IN_L##FN \ | |
68 | BUILT_IN_LL##FN) \ | |
69 | (define_operator_list X##FN##L BUILT_IN_I##FN##L \ | |
70 | BUILT_IN_L##FN##L \ | |
71 | BUILT_IN_LL##FN##L) | |
72 | ||
543a9bcd RS |
73 | DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR) |
74 | DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL) | |
75 | DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND) | |
76 | DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) | |
ed73f46f MG |
77 | |
78 | /* As opposed to convert?, this still creates a single pattern, so | |
79 | it is not a suitable replacement for convert? in all cases. */ | |
80 | (match (nop_convert @0) | |
81 | (convert @0) | |
82 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))))) | |
83 | (match (nop_convert @0) | |
84 | (view_convert @0) | |
85 | (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0)) | |
928686b1 RS |
86 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
87 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))) | |
ed73f46f MG |
88 | && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) |
89 | /* This one has to be last, or it shadows the others. */ | |
90 | (match (nop_convert @0) | |
91 | @0) | |
f84e7fd6 | 92 | |
e0ee10ed | 93 | /* Simplifications of operations with one constant operand and |
36a60e48 | 94 | simplifications to constants or single values. */ |
e0ee10ed RB |
95 | |
96 | (for op (plus pointer_plus minus bit_ior bit_xor) | |
97 | (simplify | |
98 | (op @0 integer_zerop) | |
99 | (non_lvalue @0))) | |
100 | ||
a499aac5 RB |
101 | /* 0 +p index -> (type)index */ |
102 | (simplify | |
103 | (pointer_plus integer_zerop @1) | |
104 | (non_lvalue (convert @1))) | |
105 | ||
d43177ad MG |
106 | /* ptr - 0 -> (type)ptr */ |
107 | (simplify | |
108 | (pointer_diff @0 integer_zerop) | |
109 | (convert @0)) | |
110 | ||
a7f24614 RB |
111 | /* See if ARG1 is zero and X + ARG1 reduces to X. |
112 | Likewise if the operands are reversed. */ | |
113 | (simplify | |
114 | (plus:c @0 real_zerop@1) | |
115 | (if (fold_real_zero_addition_p (type, @1, 0)) | |
116 | (non_lvalue @0))) | |
117 | ||
118 | /* See if ARG1 is zero and X - ARG1 reduces to X. */ | |
119 | (simplify | |
120 | (minus @0 real_zerop@1) | |
121 | (if (fold_real_zero_addition_p (type, @1, 1)) | |
122 | (non_lvalue @0))) | |
123 | ||
e0ee10ed RB |
124 | /* Simplify x - x. |
125 | This is unsafe for certain floats even in non-IEEE formats. | |
126 | In IEEE, it is unsafe because it does wrong for NaNs. | |
127 | Also note that operand_equal_p is always false if an operand | |
128 | is volatile. */ | |
129 | (simplify | |
a7f24614 | 130 | (minus @0 @0) |
1b457aa4 | 131 | (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type)) |
a7f24614 | 132 | { build_zero_cst (type); })) |
1af4ebf5 MG |
133 | (simplify |
134 | (pointer_diff @@0 @0) | |
135 | { build_zero_cst (type); }) | |
e0ee10ed RB |
136 | |
137 | (simplify | |
a7f24614 RB |
138 | (mult @0 integer_zerop@1) |
139 | @1) | |
140 | ||
141 | /* Maybe fold x * 0 to 0. The expressions aren't the same | |
142 | when x is NaN, since x * 0 is also NaN. Nor are they the | |
143 | same in modes with signed zeros, since multiplying a | |
144 | negative value by 0 gives -0, not +0. */ | |
145 | (simplify | |
146 | (mult @0 real_zerop@1) | |
8b5ee871 | 147 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) |
a7f24614 RB |
148 | @1)) |
149 | ||
150 | /* In IEEE floating point, x*1 is not equivalent to x for snans. | |
151 | Likewise for complex arithmetic with signed zeros. */ | |
152 | (simplify | |
153 | (mult @0 real_onep) | |
8b5ee871 MG |
154 | (if (!HONOR_SNANS (type) |
155 | && (!HONOR_SIGNED_ZEROS (type) | |
a7f24614 RB |
156 | || !COMPLEX_FLOAT_TYPE_P (type))) |
157 | (non_lvalue @0))) | |
158 | ||
159 | /* Transform x * -1.0 into -x. */ | |
160 | (simplify | |
161 | (mult @0 real_minus_onep) | |
8b5ee871 MG |
162 | (if (!HONOR_SNANS (type) |
163 | && (!HONOR_SIGNED_ZEROS (type) | |
a7f24614 RB |
164 | || !COMPLEX_FLOAT_TYPE_P (type))) |
165 | (negate @0))) | |
e0ee10ed | 166 | |
8c2805bb AP |
167 | (for cmp (gt ge lt le) |
168 | outp (convert convert negate negate) | |
169 | outn (negate negate convert convert) | |
170 | /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */ | |
171 | /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */ | |
172 | /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */ | |
173 | /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */ | |
174 | (simplify | |
175 | (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep) | |
176 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type) | |
177 | && types_match (type, TREE_TYPE (@0))) | |
178 | (switch | |
179 | (if (types_match (type, float_type_node)) | |
180 | (BUILT_IN_COPYSIGNF @1 (outp @0))) | |
181 | (if (types_match (type, double_type_node)) | |
182 | (BUILT_IN_COPYSIGN @1 (outp @0))) | |
183 | (if (types_match (type, long_double_type_node)) | |
184 | (BUILT_IN_COPYSIGNL @1 (outp @0)))))) | |
185 | /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */ | |
186 | /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */ | |
187 | /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */ | |
188 | /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */ | |
189 | (simplify | |
190 | (cond (cmp @0 real_zerop) real_minus_onep real_onep@1) | |
191 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type) | |
192 | && types_match (type, TREE_TYPE (@0))) | |
193 | (switch | |
194 | (if (types_match (type, float_type_node)) | |
195 | (BUILT_IN_COPYSIGNF @1 (outn @0))) | |
196 | (if (types_match (type, double_type_node)) | |
197 | (BUILT_IN_COPYSIGN @1 (outn @0))) | |
198 | (if (types_match (type, long_double_type_node)) | |
199 | (BUILT_IN_COPYSIGNL @1 (outn @0))))))) | |
200 | ||
201 | /* Transform X * copysign (1.0, X) into abs(X). */ | |
202 | (simplify | |
c6cfa2bf | 203 | (mult:c @0 (COPYSIGN_ALL real_onep @0)) |
8c2805bb AP |
204 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) |
205 | (abs @0))) | |
206 | ||
207 | /* Transform X * copysign (1.0, -X) into -abs(X). */ | |
208 | (simplify | |
c6cfa2bf | 209 | (mult:c @0 (COPYSIGN_ALL real_onep (negate @0))) |
8c2805bb AP |
210 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) |
211 | (negate (abs @0)))) | |
212 | ||
213 | /* Transform copysign (CST, X) into copysign (ABS(CST), X). */ | |
214 | (simplify | |
c6cfa2bf | 215 | (COPYSIGN_ALL REAL_CST@0 @1) |
8c2805bb | 216 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0))) |
c6cfa2bf | 217 | (COPYSIGN_ALL (negate @0) @1))) |
8c2805bb | 218 | |
5b7f6ed0 | 219 | /* X * 1, X / 1 -> X. */ |
e0ee10ed RB |
220 | (for op (mult trunc_div ceil_div floor_div round_div exact_div) |
221 | (simplify | |
222 | (op @0 integer_onep) | |
223 | (non_lvalue @0))) | |
224 | ||
71f82be9 JG |
225 | /* (A / (1 << B)) -> (A >> B). |
226 | Only for unsigned A. For signed A, this would not preserve rounding | |
227 | toward zero. | |
228 | For example: (-1 / ( 1 << B)) != -1 >> B. */ | |
229 | (simplify | |
230 | (trunc_div @0 (lshift integer_onep@1 @2)) | |
231 | (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) | |
232 | && (!VECTOR_TYPE_P (type) | |
233 | || target_supports_op_p (type, RSHIFT_EXPR, optab_vector) | |
234 | || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))) | |
235 | (rshift @0 @2))) | |
236 | ||
5b7f6ed0 MG |
237 | /* Preserve explicit divisions by 0: the C++ front-end wants to detect |
238 | undefined behavior in constexpr evaluation, and assuming that the division | |
239 | traps enables better optimizations than these anyway. */ | |
a7f24614 | 240 | (for div (trunc_div ceil_div floor_div round_div exact_div) |
5b7f6ed0 MG |
241 | /* 0 / X is always zero. */ |
242 | (simplify | |
243 | (div integer_zerop@0 @1) | |
244 | /* But not for 0 / 0 so that we can get the proper warnings and errors. */ | |
245 | (if (!integer_zerop (@1)) | |
246 | @0)) | |
da186c1f | 247 | /* X / -1 is -X. */ |
a7f24614 | 248 | (simplify |
09240451 MG |
249 | (div @0 integer_minus_onep@1) |
250 | (if (!TYPE_UNSIGNED (type)) | |
da186c1f | 251 | (negate @0))) |
5b7f6ed0 MG |
252 | /* X / X is one. */ |
253 | (simplify | |
254 | (div @0 @0) | |
9ebce098 JJ |
255 | /* But not for 0 / 0 so that we can get the proper warnings and errors. |
256 | And not for _Fract types where we can't build 1. */ | |
257 | (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type))) | |
5b7f6ed0 | 258 | { build_one_cst (type); })) |
da186c1f RB |
259 | /* X / abs (X) is X < 0 ? -1 : 1. */ |
260 | (simplify | |
d96a5585 RB |
261 | (div:C @0 (abs @0)) |
262 | (if (INTEGRAL_TYPE_P (type) | |
da186c1f RB |
263 | && TYPE_OVERFLOW_UNDEFINED (type)) |
264 | (cond (lt @0 { build_zero_cst (type); }) | |
265 | { build_minus_one_cst (type); } { build_one_cst (type); }))) | |
266 | /* X / -X is -1. */ | |
267 | (simplify | |
d96a5585 | 268 | (div:C @0 (negate @0)) |
da186c1f RB |
269 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
270 | && TYPE_OVERFLOW_UNDEFINED (type)) | |
271 | { build_minus_one_cst (type); }))) | |
a7f24614 RB |
272 | |
273 | /* For unsigned integral types, FLOOR_DIV_EXPR is the same as | |
274 | TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ | |
275 | (simplify | |
276 | (floor_div @0 @1) | |
09240451 MG |
277 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
278 | && TYPE_UNSIGNED (type)) | |
a7f24614 RB |
279 | (trunc_div @0 @1))) |
280 | ||
28093105 RB |
281 | /* Combine two successive divisions. Note that combining ceil_div |
282 | and floor_div is trickier and combining round_div even more so. */ | |
283 | (for div (trunc_div exact_div) | |
c306cfaf RB |
284 | (simplify |
285 | (div (div @0 INTEGER_CST@1) INTEGER_CST@2) | |
286 | (with { | |
287 | bool overflow_p; | |
8e6cdc90 RS |
288 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), |
289 | TYPE_SIGN (type), &overflow_p); | |
c306cfaf RB |
290 | } |
291 | (if (!overflow_p) | |
8fdc6c67 RB |
292 | (div @0 { wide_int_to_tree (type, mul); }) |
293 | (if (TYPE_UNSIGNED (type) | |
294 | || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)) | |
295 | { build_zero_cst (type); }))))) | |
c306cfaf | 296 | |
288fe52e AM |
297 | /* Combine successive multiplications. Similar to above, but handling |
298 | overflow is different. */ | |
299 | (simplify | |
300 | (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
301 | (with { | |
302 | bool overflow_p; | |
8e6cdc90 RS |
303 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), |
304 | TYPE_SIGN (type), &overflow_p); | |
288fe52e AM |
305 | } |
306 | /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN, | |
307 | otherwise undefined overflow implies that @0 must be zero. */ | |
308 | (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type)) | |
309 | (mult @0 { wide_int_to_tree (type, mul); })))) | |
310 | ||
a7f24614 | 311 | /* Optimize A / A to 1.0 if we don't care about |
09240451 | 312 | NaNs or Infinities. */ |
a7f24614 RB |
313 | (simplify |
314 | (rdiv @0 @0) | |
09240451 | 315 | (if (FLOAT_TYPE_P (type) |
1b457aa4 | 316 | && ! HONOR_NANS (type) |
8b5ee871 | 317 | && ! HONOR_INFINITIES (type)) |
09240451 MG |
318 | { build_one_cst (type); })) |
319 | ||
320 | /* Optimize -A / A to -1.0 if we don't care about | |
321 | NaNs or Infinities. */ | |
322 | (simplify | |
e04d2a35 | 323 | (rdiv:C @0 (negate @0)) |
09240451 | 324 | (if (FLOAT_TYPE_P (type) |
1b457aa4 | 325 | && ! HONOR_NANS (type) |
8b5ee871 | 326 | && ! HONOR_INFINITIES (type)) |
09240451 | 327 | { build_minus_one_cst (type); })) |
a7f24614 | 328 | |
8c6961ca PK |
329 | /* PR71078: x / abs(x) -> copysign (1.0, x) */ |
330 | (simplify | |
331 | (rdiv:C (convert? @0) (convert? (abs @0))) | |
332 | (if (SCALAR_FLOAT_TYPE_P (type) | |
333 | && ! HONOR_NANS (type) | |
334 | && ! HONOR_INFINITIES (type)) | |
335 | (switch | |
336 | (if (types_match (type, float_type_node)) | |
337 | (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0))) | |
338 | (if (types_match (type, double_type_node)) | |
339 | (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0))) | |
340 | (if (types_match (type, long_double_type_node)) | |
341 | (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0)))))) | |
342 | ||
a7f24614 RB |
343 | /* In IEEE floating point, x/1 is not equivalent to x for snans. */ |
344 | (simplify | |
345 | (rdiv @0 real_onep) | |
8b5ee871 | 346 | (if (!HONOR_SNANS (type)) |
a7f24614 RB |
347 | (non_lvalue @0))) |
348 | ||
349 | /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ | |
350 | (simplify | |
351 | (rdiv @0 real_minus_onep) | |
8b5ee871 | 352 | (if (!HONOR_SNANS (type)) |
a7f24614 RB |
353 | (negate @0))) |
354 | ||
5711ac88 | 355 | (if (flag_reciprocal_math) |
81825e28 | 356 | /* Convert (A/B)/C to A/(B*C). */ |
5711ac88 N |
357 | (simplify |
358 | (rdiv (rdiv:s @0 @1) @2) | |
81825e28 WD |
359 | (rdiv @0 (mult @1 @2))) |
360 | ||
361 | /* Canonicalize x / (C1 * y) to (x * C2) / y. */ | |
362 | (simplify | |
363 | (rdiv @0 (mult:s @1 REAL_CST@2)) | |
364 | (with | |
365 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); } | |
366 | (if (tem) | |
367 | (rdiv (mult @0 { tem; } ) @1)))) | |
5711ac88 N |
368 | |
369 | /* Convert A/(B/C) to (A/B)*C */ | |
370 | (simplify | |
371 | (rdiv @0 (rdiv:s @1 @2)) | |
372 | (mult (rdiv @0 @1) @2))) | |
373 | ||
6a435314 WD |
374 | /* Simplify x / (- y) to -x / y. */ |
375 | (simplify | |
376 | (rdiv @0 (negate @1)) | |
377 | (rdiv (negate @0) @1)) | |
378 | ||
5711ac88 N |
379 | /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */ |
380 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
381 | (simplify | |
382 | (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2) | |
383 | (if (integer_pow2p (@2) | |
384 | && tree_int_cst_sgn (@2) > 0 | |
a1488398 | 385 | && tree_nop_conversion_p (type, TREE_TYPE (@0)) |
8e6cdc90 RS |
386 | && wi::to_wide (@2) + wi::to_wide (@1) == 0) |
387 | (rshift (convert @0) | |
388 | { build_int_cst (integer_type_node, | |
389 | wi::exact_log2 (wi::to_wide (@2))); })))) | |
5711ac88 | 390 | |
a7f24614 RB |
391 | /* If ARG1 is a constant, we can convert this to a multiply by the |
392 | reciprocal. This does not have the same rounding properties, | |
393 | so only do this if -freciprocal-math. We can actually | |
394 | always safely do it if ARG1 is a power of two, but it's hard to | |
395 | tell if it is or not in a portable manner. */ | |
396 | (for cst (REAL_CST COMPLEX_CST VECTOR_CST) | |
397 | (simplify | |
398 | (rdiv @0 cst@1) | |
399 | (if (optimize) | |
53bc4b3a RB |
400 | (if (flag_reciprocal_math |
401 | && !real_zerop (@1)) | |
a7f24614 | 402 | (with |
249700b5 | 403 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); } |
a7f24614 | 404 | (if (tem) |
8fdc6c67 RB |
405 | (mult @0 { tem; } ))) |
406 | (if (cst != COMPLEX_CST) | |
407 | (with { tree inverse = exact_inverse (type, @1); } | |
408 | (if (inverse) | |
409 | (mult @0 { inverse; } )))))))) | |
a7f24614 | 410 | |
a7f24614 | 411 | (for mod (ceil_mod floor_mod round_mod trunc_mod) |
e0ee10ed RB |
412 | /* 0 % X is always zero. */ |
413 | (simplify | |
a7f24614 | 414 | (mod integer_zerop@0 @1) |
e0ee10ed RB |
415 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ |
416 | (if (!integer_zerop (@1)) | |
417 | @0)) | |
418 | /* X % 1 is always zero. */ | |
419 | (simplify | |
a7f24614 RB |
420 | (mod @0 integer_onep) |
421 | { build_zero_cst (type); }) | |
422 | /* X % -1 is zero. */ | |
423 | (simplify | |
09240451 MG |
424 | (mod @0 integer_minus_onep@1) |
425 | (if (!TYPE_UNSIGNED (type)) | |
bc4315fb | 426 | { build_zero_cst (type); })) |
5b7f6ed0 MG |
427 | /* X % X is zero. */ |
428 | (simplify | |
429 | (mod @0 @0) | |
430 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ | |
431 | (if (!integer_zerop (@0)) | |
432 | { build_zero_cst (type); })) | |
bc4315fb MG |
433 | /* (X % Y) % Y is just X % Y. */ |
434 | (simplify | |
435 | (mod (mod@2 @0 @1) @1) | |
98e30e51 RB |
436 | @2) |
437 | /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */ | |
438 | (simplify | |
439 | (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
440 | (if (ANY_INTEGRAL_TYPE_P (type) | |
441 | && TYPE_OVERFLOW_UNDEFINED (type) | |
8e6cdc90 RS |
442 | && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2), |
443 | TYPE_SIGN (type))) | |
98e30e51 | 444 | { build_zero_cst (type); }))) |
a7f24614 RB |
445 | |
446 | /* X % -C is the same as X % C. */ | |
447 | (simplify | |
448 | (trunc_mod @0 INTEGER_CST@1) | |
449 | (if (TYPE_SIGN (type) == SIGNED | |
450 | && !TREE_OVERFLOW (@1) | |
8e6cdc90 | 451 | && wi::neg_p (wi::to_wide (@1)) |
a7f24614 RB |
452 | && !TYPE_OVERFLOW_TRAPS (type) |
453 | /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ | |
454 | && !sign_bit_p (@1, @1)) | |
455 | (trunc_mod @0 (negate @1)))) | |
e0ee10ed | 456 | |
8f0c696a RB |
457 | /* X % -Y is the same as X % Y. */ |
458 | (simplify | |
459 | (trunc_mod @0 (convert? (negate @1))) | |
a2a743a1 MP |
460 | (if (INTEGRAL_TYPE_P (type) |
461 | && !TYPE_UNSIGNED (type) | |
8f0c696a | 462 | && !TYPE_OVERFLOW_TRAPS (type) |
20b8d734 JJ |
463 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) |
464 | /* Avoid this transformation if X might be INT_MIN or | |
465 | Y might be -1, because we would then change valid | |
466 | INT_MIN % -(-1) into invalid INT_MIN % -1. */ | |
8e6cdc90 | 467 | && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type))) |
20b8d734 JJ |
468 | || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION |
469 | (TREE_TYPE (@1)))))) | |
8f0c696a RB |
470 | (trunc_mod @0 (convert @1)))) |
471 | ||
f461569a MP |
472 | /* X - (X / Y) * Y is the same as X % Y. */ |
473 | (simplify | |
2eef1fc1 RB |
474 | (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1))) |
475 | (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) | |
fba46f03 | 476 | (convert (trunc_mod @0 @1)))) |
f461569a | 477 | |
8f0c696a RB |
478 | /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, |
479 | i.e. "X % C" into "X & (C - 1)", if X and C are positive. | |
480 | Also optimize A % (C << N) where C is a power of 2, | |
481 | to A & ((C << N) - 1). */ | |
482 | (match (power_of_two_cand @1) | |
483 | INTEGER_CST@1) | |
484 | (match (power_of_two_cand @1) | |
485 | (lshift INTEGER_CST@1 @2)) | |
486 | (for mod (trunc_mod floor_mod) | |
487 | (simplify | |
4ab1e111 | 488 | (mod @0 (convert?@3 (power_of_two_cand@1 @2))) |
8f0c696a RB |
489 | (if ((TYPE_UNSIGNED (type) |
490 | || tree_expr_nonnegative_p (@0)) | |
4ab1e111 | 491 | && tree_nop_conversion_p (type, TREE_TYPE (@3)) |
8f0c696a | 492 | && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) |
4ab1e111 | 493 | (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))))) |
8f0c696a | 494 | |
887ab609 N |
495 | /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ |
496 | (simplify | |
497 | (trunc_div (mult @0 integer_pow2p@1) @1) | |
498 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
499 | (bit_and @0 { wide_int_to_tree | |
8e6cdc90 RS |
500 | (type, wi::mask (TYPE_PRECISION (type) |
501 | - wi::exact_log2 (wi::to_wide (@1)), | |
887ab609 N |
502 | false, TYPE_PRECISION (type))); }))) |
503 | ||
5f8d832e N |
504 | /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */ |
505 | (simplify | |
506 | (mult (trunc_div @0 integer_pow2p@1) @1) | |
507 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
508 | (bit_and @0 (negate @1)))) | |
509 | ||
95765f36 N |
510 | /* Simplify (t * 2) / 2) -> t. */ |
511 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
512 | (simplify | |
55d84e61 | 513 | (div (mult:c @0 @1) @1) |
95765f36 N |
514 | (if (ANY_INTEGRAL_TYPE_P (type) |
515 | && TYPE_OVERFLOW_UNDEFINED (type)) | |
516 | @0))) | |
517 | ||
d202f9bd | 518 | (for op (negate abs) |
9b054b08 RS |
519 | /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ |
520 | (for coss (COS COSH) | |
521 | (simplify | |
522 | (coss (op @0)) | |
523 | (coss @0))) | |
524 | /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */ | |
525 | (for pows (POW) | |
526 | (simplify | |
527 | (pows (op @0) REAL_CST@1) | |
528 | (with { HOST_WIDE_INT n; } | |
529 | (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) | |
5d3498b4 | 530 | (pows @0 @1))))) |
de3fbea3 RB |
531 | /* Likewise for powi. */ |
532 | (for pows (POWI) | |
533 | (simplify | |
534 | (pows (op @0) INTEGER_CST@1) | |
8e6cdc90 | 535 | (if ((wi::to_wide (@1) & 1) == 0) |
de3fbea3 | 536 | (pows @0 @1)))) |
5d3498b4 RS |
537 | /* Strip negate and abs from both operands of hypot. */ |
538 | (for hypots (HYPOT) | |
539 | (simplify | |
540 | (hypots (op @0) @1) | |
541 | (hypots @0 @1)) | |
542 | (simplify | |
543 | (hypots @0 (op @1)) | |
544 | (hypots @0 @1))) | |
545 | /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */ | |
c6cfa2bf | 546 | (for copysigns (COPYSIGN_ALL) |
5d3498b4 RS |
547 | (simplify |
548 | (copysigns (op @0) @1) | |
549 | (copysigns @0 @1)))) | |
550 | ||
551 | /* abs(x)*abs(x) -> x*x. Should be valid for all types. */ | |
552 | (simplify | |
553 | (mult (abs@1 @0) @1) | |
554 | (mult @0 @0)) | |
555 | ||
556 | /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */ | |
557 | (for coss (COS COSH) | |
558 | copysigns (COPYSIGN) | |
559 | (simplify | |
560 | (coss (copysigns @0 @1)) | |
561 | (coss @0))) | |
562 | ||
563 | /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */ | |
564 | (for pows (POW) | |
565 | copysigns (COPYSIGN) | |
566 | (simplify | |
de3fbea3 | 567 | (pows (copysigns @0 @2) REAL_CST@1) |
5d3498b4 RS |
568 | (with { HOST_WIDE_INT n; } |
569 | (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) | |
570 | (pows @0 @1))))) | |
de3fbea3 RB |
571 | /* Likewise for powi. */ |
572 | (for pows (POWI) | |
573 | copysigns (COPYSIGN) | |
574 | (simplify | |
575 | (pows (copysigns @0 @2) INTEGER_CST@1) | |
8e6cdc90 | 576 | (if ((wi::to_wide (@1) & 1) == 0) |
de3fbea3 | 577 | (pows @0 @1)))) |
5d3498b4 RS |
578 | |
579 | (for hypots (HYPOT) | |
580 | copysigns (COPYSIGN) | |
581 | /* hypot(copysign(x, y), z) -> hypot(x, z). */ | |
582 | (simplify | |
583 | (hypots (copysigns @0 @1) @2) | |
584 | (hypots @0 @2)) | |
585 | /* hypot(x, copysign(y, z)) -> hypot(x, y). */ | |
586 | (simplify | |
587 | (hypots @0 (copysigns @1 @2)) | |
588 | (hypots @0 @1))) | |
589 | ||
eeb57981 | 590 | /* copysign(x, CST) -> [-]abs (x). */ |
c6cfa2bf | 591 | (for copysigns (COPYSIGN_ALL) |
eeb57981 RB |
592 | (simplify |
593 | (copysigns @0 REAL_CST@1) | |
594 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
595 | (negate (abs @0)) | |
596 | (abs @0)))) | |
597 | ||
5d3498b4 | 598 | /* copysign(copysign(x, y), z) -> copysign(x, z). */ |
c6cfa2bf | 599 | (for copysigns (COPYSIGN_ALL) |
5d3498b4 RS |
600 | (simplify |
601 | (copysigns (copysigns @0 @1) @2) | |
602 | (copysigns @0 @2))) | |
603 | ||
604 | /* copysign(x,y)*copysign(x,y) -> x*x. */ | |
c6cfa2bf | 605 | (for copysigns (COPYSIGN_ALL) |
5d3498b4 RS |
606 | (simplify |
607 | (mult (copysigns@2 @0 @1) @2) | |
608 | (mult @0 @0))) | |
609 | ||
610 | /* ccos(-x) -> ccos(x). Similarly for ccosh. */ | |
611 | (for ccoss (CCOS CCOSH) | |
612 | (simplify | |
613 | (ccoss (negate @0)) | |
614 | (ccoss @0))) | |
d202f9bd | 615 | |
abcc43f5 RS |
616 | /* cabs(-x) and cos(conj(x)) -> cabs(x). */ |
617 | (for ops (conj negate) | |
618 | (for cabss (CABS) | |
619 | (simplify | |
620 | (cabss (ops @0)) | |
621 | (cabss @0)))) | |
622 | ||
0a8f32b8 RB |
623 | /* Fold (a * (1 << b)) into (a << b) */ |
624 | (simplify | |
625 | (mult:c @0 (convert? (lshift integer_onep@1 @2))) | |
626 | (if (! FLOAT_TYPE_P (type) | |
9ff6fb6e | 627 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) |
0a8f32b8 RB |
628 | (lshift @0 @2))) |
629 | ||
4349b15f SD |
630 | /* Fold (1 << (C - x)) where C = precision(type) - 1 |
631 | into ((1 << C) >> x). */ | |
632 | (simplify | |
633 | (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3)) | |
634 | (if (INTEGRAL_TYPE_P (type) | |
56ccfbd6 | 635 | && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1) |
4349b15f SD |
636 | && single_use (@1)) |
637 | (if (TYPE_UNSIGNED (type)) | |
638 | (rshift (lshift @0 @2) @3) | |
639 | (with | |
640 | { tree utype = unsigned_type_for (type); } | |
641 | (convert (rshift (lshift (convert:utype @0) @2) @3)))))) | |
642 | ||
0a8f32b8 RB |
643 | /* Fold (C1/X)*C2 into (C1*C2)/X. */ |
644 | (simplify | |
ff86345f RB |
645 | (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2) |
646 | (if (flag_associative_math | |
647 | && single_use (@3)) | |
0a8f32b8 RB |
648 | (with |
649 | { tree tem = const_binop (MULT_EXPR, type, @0, @2); } | |
650 | (if (tem) | |
651 | (rdiv { tem; } @1))))) | |
652 | ||
653 | /* Simplify ~X & X as zero. */ | |
654 | (simplify | |
655 | (bit_and:c (convert? @0) (convert? (bit_not @0))) | |
656 | { build_zero_cst (type); }) | |
657 | ||
89b80c42 PK |
658 | /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */ |
659 | (simplify | |
660 | (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep)) | |
661 | (if (TYPE_UNSIGNED (type)) | |
662 | (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1))))) | |
663 | ||
7aa13860 PK |
664 | (for bitop (bit_and bit_ior) |
665 | cmp (eq ne) | |
a93952d2 JJ |
666 | /* PR35691: Transform |
667 | (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0. | |
668 | (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */ | |
7aa13860 PK |
669 | (simplify |
670 | (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop)) | |
671 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
a93952d2 JJ |
672 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
673 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
674 | (cmp (bit_ior @0 (convert @1)) @2))) | |
675 | /* Transform: | |
676 | (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1. | |
677 | (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */ | |
678 | (simplify | |
679 | (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp)) | |
680 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
681 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
682 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
683 | (cmp (bit_and @0 (convert @1)) @2)))) | |
7aa13860 | 684 | |
10158317 RB |
685 | /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */ |
686 | (simplify | |
a9658b11 | 687 | (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1)) |
10158317 RB |
688 | (minus (bit_xor @0 @1) @1)) |
689 | (simplify | |
690 | (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1)) | |
8e6cdc90 | 691 | (if (~wi::to_wide (@2) == wi::to_wide (@1)) |
10158317 RB |
692 | (minus (bit_xor @0 @1) @1))) |
693 | ||
694 | /* Fold (A & B) - (A & ~B) into B - (A ^ B). */ | |
695 | (simplify | |
a8e9f9a3 | 696 | (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1))) |
10158317 RB |
697 | (minus @1 (bit_xor @0 @1))) |
698 | ||
42bd89ce MG |
699 | /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */ |
700 | (for op (bit_ior bit_xor plus) | |
701 | (simplify | |
702 | (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1)) | |
703 | (bit_xor @0 @1)) | |
704 | (simplify | |
705 | (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1)) | |
8e6cdc90 | 706 | (if (~wi::to_wide (@2) == wi::to_wide (@1)) |
42bd89ce | 707 | (bit_xor @0 @1)))) |
2066ef6a PK |
708 | |
709 | /* PR53979: Transform ((a ^ b) | a) -> (a | b) */ | |
710 | (simplify | |
711 | (bit_ior:c (bit_xor:c @0 @1) @0) | |
712 | (bit_ior @0 @1)) | |
713 | ||
e268a77b MG |
714 | /* (a & ~b) | (a ^ b) --> a ^ b */ |
715 | (simplify | |
716 | (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1)) | |
717 | @2) | |
718 | ||
719 | /* (a & ~b) ^ ~a --> ~(a & b) */ | |
720 | (simplify | |
721 | (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0)) | |
722 | (bit_not (bit_and @0 @1))) | |
723 | ||
724 | /* (a | b) & ~(a ^ b) --> a & b */ | |
725 | (simplify | |
726 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1))) | |
727 | (bit_and @0 @1)) | |
728 | ||
729 | /* a | ~(a ^ b) --> a | ~b */ | |
730 | (simplify | |
731 | (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1))) | |
732 | (bit_ior @0 (bit_not @1))) | |
733 | ||
734 | /* (a | b) | (a &^ b) --> a | b */ | |
735 | (for op (bit_and bit_xor) | |
736 | (simplify | |
737 | (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1)) | |
738 | @2)) | |
739 | ||
740 | /* (a & b) | ~(a ^ b) --> ~(a ^ b) */ | |
741 | (simplify | |
742 | (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1))) | |
743 | @2) | |
744 | ||
745 | /* ~(~a & b) --> a | ~b */ | |
746 | (simplify | |
747 | (bit_not (bit_and:cs (bit_not @0) @1)) | |
748 | (bit_ior @0 (bit_not @1))) | |
749 | ||
d982c5b7 MG |
750 | /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */ |
751 | #if GIMPLE | |
752 | (simplify | |
753 | (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1) | |
754 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
8e6cdc90 | 755 | && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) |
d982c5b7 MG |
756 | (bit_xor @0 @1))) |
757 | #endif | |
10158317 | 758 | |
bc4315fb MG |
759 | /* X % Y is smaller than Y. */ |
760 | (for cmp (lt ge) | |
761 | (simplify | |
762 | (cmp (trunc_mod @0 @1) @1) | |
763 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
764 | { constant_boolean_node (cmp == LT_EXPR, type); }))) | |
765 | (for cmp (gt le) | |
766 | (simplify | |
767 | (cmp @1 (trunc_mod @0 @1)) | |
768 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
769 | { constant_boolean_node (cmp == GT_EXPR, type); }))) | |
770 | ||
e0ee10ed RB |
771 | /* x | ~0 -> ~0 */ |
772 | (simplify | |
ca0b7ece RB |
773 | (bit_ior @0 integer_all_onesp@1) |
774 | @1) | |
775 | ||
776 | /* x | 0 -> x */ | |
777 | (simplify | |
778 | (bit_ior @0 integer_zerop) | |
779 | @0) | |
e0ee10ed RB |
780 | |
781 | /* x & 0 -> 0 */ | |
782 | (simplify | |
ca0b7ece RB |
783 | (bit_and @0 integer_zerop@1) |
784 | @1) | |
e0ee10ed | 785 | |
a4398a30 | 786 | /* ~x | x -> -1 */ |
8b5ee871 MG |
787 | /* ~x ^ x -> -1 */ |
788 | /* ~x + x -> -1 */ | |
789 | (for op (bit_ior bit_xor plus) | |
790 | (simplify | |
791 | (op:c (convert? @0) (convert? (bit_not @0))) | |
792 | (convert { build_all_ones_cst (TREE_TYPE (@0)); }))) | |
a4398a30 | 793 | |
e0ee10ed RB |
794 | /* x ^ x -> 0 */ |
795 | (simplify | |
796 | (bit_xor @0 @0) | |
797 | { build_zero_cst (type); }) | |
798 | ||
36a60e48 RB |
799 | /* Canonicalize X ^ ~0 to ~X. */ |
800 | (simplify | |
801 | (bit_xor @0 integer_all_onesp@1) | |
802 | (bit_not @0)) | |
803 | ||
804 | /* x & ~0 -> x */ | |
805 | (simplify | |
806 | (bit_and @0 integer_all_onesp) | |
807 | (non_lvalue @0)) | |
808 | ||
809 | /* x & x -> x, x | x -> x */ | |
810 | (for bitop (bit_and bit_ior) | |
811 | (simplify | |
812 | (bitop @0 @0) | |
813 | (non_lvalue @0))) | |
814 | ||
c7986356 MG |
815 | /* x & C -> x if we know that x & ~C == 0. */ |
816 | #if GIMPLE | |
817 | (simplify | |
818 | (bit_and SSA_NAME@0 INTEGER_CST@1) | |
819 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
8e6cdc90 | 820 | && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) |
c7986356 MG |
821 | @0)) |
822 | #endif | |
823 | ||
0f770b01 RV |
824 | /* x + (x & 1) -> (x + 1) & ~1 */ |
825 | (simplify | |
44fc0a51 RB |
826 | (plus:c @0 (bit_and:s @0 integer_onep@1)) |
827 | (bit_and (plus @0 @1) (bit_not @1))) | |
0f770b01 RV |
828 | |
829 | /* x & ~(x & y) -> x & ~y */ | |
830 | /* x | ~(x | y) -> x | ~y */ | |
831 | (for bitop (bit_and bit_ior) | |
af563d4b | 832 | (simplify |
44fc0a51 RB |
833 | (bitop:c @0 (bit_not (bitop:cs @0 @1))) |
834 | (bitop @0 (bit_not @1)))) | |
af563d4b MG |
835 | |
836 | /* (x | y) & ~x -> y & ~x */ | |
837 | /* (x & y) | ~x -> y | ~x */ | |
838 | (for bitop (bit_and bit_ior) | |
839 | rbitop (bit_ior bit_and) | |
840 | (simplify | |
841 | (bitop:c (rbitop:c @0 @1) (bit_not@2 @0)) | |
842 | (bitop @1 @2))) | |
0f770b01 | 843 | |
f13c4673 MP |
844 | /* (x & y) ^ (x | y) -> x ^ y */ |
845 | (simplify | |
2d6f2dce MP |
846 | (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1)) |
847 | (bit_xor @0 @1)) | |
f13c4673 | 848 | |
9ea65ca6 MP |
849 | /* (x ^ y) ^ (x | y) -> x & y */ |
850 | (simplify | |
851 | (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1)) | |
852 | (bit_and @0 @1)) | |
853 | ||
854 | /* (x & y) + (x ^ y) -> x | y */ | |
855 | /* (x & y) | (x ^ y) -> x | y */ | |
856 | /* (x & y) ^ (x ^ y) -> x | y */ | |
857 | (for op (plus bit_ior bit_xor) | |
858 | (simplify | |
859 | (op:c (bit_and @0 @1) (bit_xor @0 @1)) | |
860 | (bit_ior @0 @1))) | |
861 | ||
862 | /* (x & y) + (x | y) -> x + y */ | |
863 | (simplify | |
864 | (plus:c (bit_and @0 @1) (bit_ior @0 @1)) | |
865 | (plus @0 @1)) | |
866 | ||
9737efaf MP |
867 | /* (x + y) - (x | y) -> x & y */ |
868 | (simplify | |
869 | (minus (plus @0 @1) (bit_ior @0 @1)) | |
870 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
871 | && !TYPE_SATURATING (type)) | |
872 | (bit_and @0 @1))) | |
873 | ||
874 | /* (x + y) - (x & y) -> x | y */ | |
875 | (simplify | |
876 | (minus (plus @0 @1) (bit_and @0 @1)) | |
877 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
878 | && !TYPE_SATURATING (type)) | |
879 | (bit_ior @0 @1))) | |
880 | ||
9ea65ca6 MP |
881 | /* (x | y) - (x ^ y) -> x & y */ |
882 | (simplify | |
883 | (minus (bit_ior @0 @1) (bit_xor @0 @1)) | |
884 | (bit_and @0 @1)) | |
885 | ||
886 | /* (x | y) - (x & y) -> x ^ y */ | |
887 | (simplify | |
888 | (minus (bit_ior @0 @1) (bit_and @0 @1)) | |
889 | (bit_xor @0 @1)) | |
890 | ||
66cc6273 MP |
891 | /* (x | y) & ~(x & y) -> x ^ y */ |
892 | (simplify | |
893 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1))) | |
894 | (bit_xor @0 @1)) | |
895 | ||
896 | /* (x | y) & (~x ^ y) -> x & y */ | |
897 | (simplify | |
898 | (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0))) | |
899 | (bit_and @0 @1)) | |
900 | ||
5b00d921 RB |
901 | /* ~x & ~y -> ~(x | y) |
902 | ~x | ~y -> ~(x & y) */ | |
903 | (for op (bit_and bit_ior) | |
904 | rop (bit_ior bit_and) | |
905 | (simplify | |
906 | (op (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
ece46666 MG |
907 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
908 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
5b00d921 RB |
909 | (bit_not (rop (convert @0) (convert @1)))))) |
910 | ||
14ea9f92 | 911 | /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing |
5b00d921 RB |
912 | with a constant, and the two constants have no bits in common, |
913 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
914 | simplifications. */ | |
14ea9f92 RB |
915 | (for op (bit_xor plus) |
916 | (simplify | |
917 | (op (convert1? (bit_and@4 @0 INTEGER_CST@1)) | |
918 | (convert2? (bit_and@5 @2 INTEGER_CST@3))) | |
919 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
920 | && tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
8e6cdc90 | 921 | && (wi::to_wide (@1) & wi::to_wide (@3)) == 0) |
14ea9f92 | 922 | (bit_ior (convert @4) (convert @5))))) |
5b00d921 RB |
923 | |
924 | /* (X | Y) ^ X -> Y & ~ X*/ | |
925 | (simplify | |
2eef1fc1 | 926 | (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0)) |
5b00d921 RB |
927 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
928 | (convert (bit_and @1 (bit_not @0))))) | |
929 | ||
930 | /* Convert ~X ^ ~Y to X ^ Y. */ | |
931 | (simplify | |
932 | (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
ece46666 MG |
933 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
934 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
5b00d921 RB |
935 | (bit_xor (convert @0) (convert @1)))) |
936 | ||
937 | /* Convert ~X ^ C to X ^ ~C. */ | |
938 | (simplify | |
939 | (bit_xor (convert? (bit_not @0)) INTEGER_CST@1) | |
c8ba6498 EB |
940 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
941 | (bit_xor (convert @0) (bit_not @1)))) | |
5b00d921 | 942 | |
e39dab2c MG |
943 | /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */ |
944 | (for opo (bit_and bit_xor) | |
945 | opi (bit_xor bit_and) | |
946 | (simplify | |
947 | (opo:c (opi:c @0 @1) @1) | |
948 | (bit_and (bit_not @0) @1))) | |
97e77391 | 949 | |
14ea9f92 RB |
950 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both |
951 | operands are another bit-wise operation with a common input. If so, | |
952 | distribute the bit operations to save an operation and possibly two if | |
953 | constants are involved. For example, convert | |
954 | (A | B) & (A | C) into A | (B & C) | |
955 | Further simplification will occur if B and C are constants. */ | |
e07ab2fe MG |
956 | (for op (bit_and bit_ior bit_xor) |
957 | rop (bit_ior bit_and bit_and) | |
14ea9f92 | 958 | (simplify |
2eef1fc1 | 959 | (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2))) |
e07ab2fe MG |
960 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) |
961 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
14ea9f92 RB |
962 | (rop (convert @0) (op (convert @1) (convert @2)))))) |
963 | ||
e39dab2c MG |
964 | /* Some simple reassociation for bit operations, also handled in reassoc. */ |
965 | /* (X & Y) & Y -> X & Y | |
966 | (X | Y) | Y -> X | Y */ | |
967 | (for op (bit_and bit_ior) | |
968 | (simplify | |
2eef1fc1 | 969 | (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1)) |
e39dab2c MG |
970 | @2)) |
971 | /* (X ^ Y) ^ Y -> X */ | |
972 | (simplify | |
2eef1fc1 | 973 | (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1)) |
ece46666 | 974 | (convert @0)) |
e39dab2c MG |
975 | /* (X & Y) & (X & Z) -> (X & Y) & Z |
976 | (X | Y) | (X | Z) -> (X | Y) | Z */ | |
977 | (for op (bit_and bit_ior) | |
978 | (simplify | |
6c35e5b0 | 979 | (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2))) |
e39dab2c MG |
980 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) |
981 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
982 | (if (single_use (@5) && single_use (@6)) | |
983 | (op @3 (convert @2)) | |
984 | (if (single_use (@3) && single_use (@4)) | |
985 | (op (convert @1) @5)))))) | |
986 | /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */ | |
987 | (simplify | |
988 | (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2))) | |
989 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
990 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
d78789f5 | 991 | (bit_xor (convert @1) (convert @2)))) |
5b00d921 | 992 | |
b14a9c57 RB |
993 | (simplify |
994 | (abs (abs@1 @0)) | |
995 | @1) | |
f3582e54 RB |
996 | (simplify |
997 | (abs (negate @0)) | |
998 | (abs @0)) | |
999 | (simplify | |
1000 | (abs tree_expr_nonnegative_p@0) | |
1001 | @0) | |
1002 | ||
55cf3946 RB |
1003 | /* A few cases of fold-const.c negate_expr_p predicate. */ |
1004 | (match negate_expr_p | |
1005 | INTEGER_CST | |
b14a9c57 | 1006 | (if ((INTEGRAL_TYPE_P (type) |
56a6d474 | 1007 | && TYPE_UNSIGNED (type)) |
b14a9c57 | 1008 | || (!TYPE_OVERFLOW_SANITIZED (type) |
55cf3946 RB |
1009 | && may_negate_without_overflow_p (t))))) |
1010 | (match negate_expr_p | |
1011 | FIXED_CST) | |
1012 | (match negate_expr_p | |
1013 | (negate @0) | |
1014 | (if (!TYPE_OVERFLOW_SANITIZED (type)))) | |
1015 | (match negate_expr_p | |
1016 | REAL_CST | |
1017 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t))))) | |
1018 | /* VECTOR_CST handling of non-wrapping types would recurse in unsupported | |
1019 | ways. */ | |
1020 | (match negate_expr_p | |
1021 | VECTOR_CST | |
1022 | (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)))) | |
81bd903a MG |
1023 | (match negate_expr_p |
1024 | (minus @0 @1) | |
1025 | (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)) | |
1026 | || (FLOAT_TYPE_P (type) | |
1027 | && !HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1028 | && !HONOR_SIGNED_ZEROS (type))))) | |
0a8f32b8 RB |
1029 | |
1030 | /* (-A) * (-B) -> A * B */ | |
1031 | (simplify | |
1032 | (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1)) | |
1033 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
1034 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
1035 | (mult (convert @0) (convert (negate @1))))) | |
55cf3946 RB |
1036 | |
1037 | /* -(A + B) -> (-B) - A. */ | |
b14a9c57 | 1038 | (simplify |
55cf3946 RB |
1039 | (negate (plus:c @0 negate_expr_p@1)) |
1040 | (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) | |
1041 | && !HONOR_SIGNED_ZEROS (element_mode (type))) | |
1042 | (minus (negate @1) @0))) | |
1043 | ||
81bd903a MG |
1044 | /* -(A - B) -> B - A. */ |
1045 | (simplify | |
1046 | (negate (minus @0 @1)) | |
1047 | (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type)) | |
1048 | || (FLOAT_TYPE_P (type) | |
1049 | && !HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1050 | && !HONOR_SIGNED_ZEROS (type))) | |
1051 | (minus @1 @0))) | |
1af4ebf5 MG |
1052 | (simplify |
1053 | (negate (pointer_diff @0 @1)) | |
1054 | (if (TYPE_OVERFLOW_UNDEFINED (type)) | |
1055 | (pointer_diff @1 @0))) | |
81bd903a | 1056 | |
55cf3946 | 1057 | /* A - B -> A + (-B) if B is easily negatable. */ |
b14a9c57 | 1058 | (simplify |
55cf3946 | 1059 | (minus @0 negate_expr_p@1) |
e4e96a4f KT |
1060 | (if (!FIXED_POINT_TYPE_P (type)) |
1061 | (plus @0 (negate @1)))) | |
d4573ffe | 1062 | |
5609420f RB |
1063 | /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) |
1064 | when profitable. | |
1065 | For bitwise binary operations apply operand conversions to the | |
1066 | binary operation result instead of to the operands. This allows | |
1067 | to combine successive conversions and bitwise binary operations. | |
1068 | We combine the above two cases by using a conditional convert. */ | |
1069 | (for bitop (bit_and bit_ior bit_xor) | |
1070 | (simplify | |
1071 | (bitop (convert @0) (convert? @1)) | |
1072 | (if (((TREE_CODE (@1) == INTEGER_CST | |
1073 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
ad6f996c | 1074 | && int_fits_type_p (@1, TREE_TYPE (@0))) |
aea417d7 | 1075 | || types_match (@0, @1)) |
ad6f996c RB |
1076 | /* ??? This transform conflicts with fold-const.c doing |
1077 | Convert (T)(x & c) into (T)x & (T)c, if c is an integer | |
1078 | constants (if x has signed type, the sign bit cannot be set | |
1079 | in c). This folds extension into the BIT_AND_EXPR. | |
1080 | Restrict it to GIMPLE to avoid endless recursions. */ | |
1081 | && (bitop != BIT_AND_EXPR || GIMPLE) | |
5609420f RB |
1082 | && (/* That's a good idea if the conversion widens the operand, thus |
1083 | after hoisting the conversion the operation will be narrower. */ | |
1084 | TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type) | |
1085 | /* It's also a good idea if the conversion is to a non-integer | |
1086 | mode. */ | |
1087 | || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT | |
1088 | /* Or if the precision of TO is not the same as the precision | |
1089 | of its mode. */ | |
2be65d9e | 1090 | || !type_has_mode_precision_p (type))) |
5609420f RB |
1091 | (convert (bitop @0 (convert @1)))))) |
1092 | ||
b14a9c57 RB |
1093 | (for bitop (bit_and bit_ior) |
1094 | rbitop (bit_ior bit_and) | |
1095 | /* (x | y) & x -> x */ | |
1096 | /* (x & y) | x -> x */ | |
1097 | (simplify | |
1098 | (bitop:c (rbitop:c @0 @1) @0) | |
1099 | @0) | |
1100 | /* (~x | y) & x -> x & y */ | |
1101 | /* (~x & y) | x -> x | y */ | |
1102 | (simplify | |
1103 | (bitop:c (rbitop:c (bit_not @0) @1) @0) | |
1104 | (bitop @0 @1))) | |
1105 | ||
5609420f RB |
1106 | /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ |
1107 | (simplify | |
1108 | (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
1109 | (bit_ior (bit_and @0 @2) (bit_and @1 @2))) | |
1110 | ||
1111 | /* Combine successive equal operations with constants. */ | |
1112 | (for bitop (bit_and bit_ior bit_xor) | |
1113 | (simplify | |
1114 | (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
fba05d9e RS |
1115 | (if (!CONSTANT_CLASS_P (@0)) |
1116 | /* This is the canonical form regardless of whether (bitop @1 @2) can be | |
1117 | folded to a constant. */ | |
1118 | (bitop @0 (bitop @1 @2)) | |
1119 | /* In this case we have three constants and (bitop @0 @1) doesn't fold | |
1120 | to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if | |
1121 | the values involved are such that the operation can't be decided at | |
1122 | compile time. Try folding one of @0 or @1 with @2 to see whether | |
1123 | that combination can be decided at compile time. | |
1124 | ||
1125 | Keep the existing form if both folds fail, to avoid endless | |
1126 | oscillation. */ | |
1127 | (with { tree cst1 = const_binop (bitop, type, @0, @2); } | |
1128 | (if (cst1) | |
1129 | (bitop @1 { cst1; }) | |
1130 | (with { tree cst2 = const_binop (bitop, type, @1, @2); } | |
1131 | (if (cst2) | |
1132 | (bitop @0 { cst2; })))))))) | |
5609420f RB |
1133 | |
1134 | /* Try simple folding for X op !X, and X op X with the help | |
1135 | of the truth_valued_p and logical_inverted_value predicates. */ | |
1136 | (match truth_valued_p | |
1137 | @0 | |
1138 | (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))) | |
f84e7fd6 | 1139 | (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor) |
5609420f RB |
1140 | (match truth_valued_p |
1141 | (op @0 @1))) | |
1142 | (match truth_valued_p | |
1143 | (truth_not @0)) | |
1144 | ||
0a8f32b8 RB |
1145 | (match (logical_inverted_value @0) |
1146 | (truth_not @0)) | |
5609420f RB |
1147 | (match (logical_inverted_value @0) |
1148 | (bit_not truth_valued_p@0)) | |
1149 | (match (logical_inverted_value @0) | |
09240451 | 1150 | (eq @0 integer_zerop)) |
5609420f | 1151 | (match (logical_inverted_value @0) |
09240451 | 1152 | (ne truth_valued_p@0 integer_truep)) |
5609420f | 1153 | (match (logical_inverted_value @0) |
09240451 | 1154 | (bit_xor truth_valued_p@0 integer_truep)) |
5609420f RB |
1155 | |
1156 | /* X & !X -> 0. */ | |
1157 | (simplify | |
1158 | (bit_and:c @0 (logical_inverted_value @0)) | |
1159 | { build_zero_cst (type); }) | |
1160 | /* X | !X and X ^ !X -> 1, , if X is truth-valued. */ | |
1161 | (for op (bit_ior bit_xor) | |
1162 | (simplify | |
1163 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
f84e7fd6 | 1164 | { constant_boolean_node (true, type); })) |
59c20dc7 RB |
1165 | /* X ==/!= !X is false/true. */ |
1166 | (for op (eq ne) | |
1167 | (simplify | |
1168 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
1169 | { constant_boolean_node (op == NE_EXPR ? true : false, type); })) | |
5609420f | 1170 | |
5609420f RB |
1171 | /* ~~x -> x */ |
1172 | (simplify | |
1173 | (bit_not (bit_not @0)) | |
1174 | @0) | |
1175 | ||
b14a9c57 RB |
1176 | /* Convert ~ (-A) to A - 1. */ |
1177 | (simplify | |
1178 | (bit_not (convert? (negate @0))) | |
ece46666 MG |
1179 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1180 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
8b5ee871 | 1181 | (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) |
b14a9c57 | 1182 | |
81bd903a MG |
1183 | /* Convert - (~A) to A + 1. */ |
1184 | (simplify | |
1185 | (negate (nop_convert (bit_not @0))) | |
1186 | (plus (view_convert @0) { build_each_one_cst (type); })) | |
1187 | ||
b14a9c57 RB |
1188 | /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ |
1189 | (simplify | |
8b5ee871 | 1190 | (bit_not (convert? (minus @0 integer_each_onep))) |
ece46666 MG |
1191 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1192 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
b14a9c57 RB |
1193 | (convert (negate @0)))) |
1194 | (simplify | |
1195 | (bit_not (convert? (plus @0 integer_all_onesp))) | |
ece46666 MG |
1196 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1197 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
b14a9c57 RB |
1198 | (convert (negate @0)))) |
1199 | ||
1200 | /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ | |
1201 | (simplify | |
1202 | (bit_not (convert? (bit_xor @0 INTEGER_CST@1))) | |
1203 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1204 | (convert (bit_xor @0 (bit_not @1))))) | |
1205 | (simplify | |
1206 | (bit_not (convert? (bit_xor:c (bit_not @0) @1))) | |
1207 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1208 | (convert (bit_xor @0 @1)))) | |
1209 | ||
e268a77b MG |
1210 | /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */ |
1211 | (simplify | |
1212 | (bit_xor:c (nop_convert:s (bit_not:s @0)) @1) | |
1213 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1214 | (bit_not (bit_xor (view_convert @0) @1)))) | |
1215 | ||
f52baa7b MP |
1216 | /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ |
1217 | (simplify | |
44fc0a51 RB |
1218 | (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2)) |
1219 | (bit_xor (bit_and (bit_xor @0 @1) @2) @0)) | |
f52baa7b | 1220 | |
f7b7b0aa MP |
1221 | /* Fold A - (A & B) into ~B & A. */ |
1222 | (simplify | |
2eef1fc1 | 1223 | (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1))) |
f7b7b0aa MP |
1224 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) |
1225 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
1226 | (convert (bit_and (bit_not @1) @0)))) | |
5609420f | 1227 | |
2071f8f9 N |
1228 | /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */ |
1229 | (for cmp (gt lt ge le) | |
1230 | (simplify | |
1231 | (mult (convert (cmp @0 @1)) @2) | |
1232 | (cond (cmp @0 @1) @2 { build_zero_cst (type); }))) | |
1233 | ||
e36c1cfe N |
1234 | /* For integral types with undefined overflow and C != 0 fold |
1235 | x * C EQ/NE y * C into x EQ/NE y. */ | |
1236 | (for cmp (eq ne) | |
1237 | (simplify | |
1238 | (cmp (mult:c @0 @1) (mult:c @2 @1)) | |
1239 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
1240 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1241 | && tree_expr_nonzero_p (@1)) | |
1242 | (cmp @0 @2)))) | |
1243 | ||
42bd89ce MG |
1244 | /* For integral types with wrapping overflow and C odd fold |
1245 | x * C EQ/NE y * C into x EQ/NE y. */ | |
1246 | (for cmp (eq ne) | |
1247 | (simplify | |
1248 | (cmp (mult @0 INTEGER_CST@1) (mult @2 @1)) | |
1249 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
1250 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) | |
1251 | && (TREE_INT_CST_LOW (@1) & 1) != 0) | |
1252 | (cmp @0 @2)))) | |
1253 | ||
e36c1cfe N |
1254 | /* For integral types with undefined overflow and C != 0 fold |
1255 | x * C RELOP y * C into: | |
84ff66b8 | 1256 | |
e36c1cfe N |
1257 | x RELOP y for nonnegative C |
1258 | y RELOP x for negative C */ | |
1259 | (for cmp (lt gt le ge) | |
1260 | (simplify | |
1261 | (cmp (mult:c @0 @1) (mult:c @2 @1)) | |
1262 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
1263 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1264 | (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1)) | |
1265 | (cmp @0 @2) | |
1266 | (if (TREE_CODE (@1) == INTEGER_CST | |
8e6cdc90 | 1267 | && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1)))) |
e36c1cfe | 1268 | (cmp @2 @0)))))) |
84ff66b8 | 1269 | |
564e405c JJ |
1270 | /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */ |
1271 | (for cmp (le gt) | |
1272 | icmp (gt le) | |
1273 | (simplify | |
1274 | (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2) | |
1275 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1276 | && TYPE_UNSIGNED (TREE_TYPE (@0)) | |
1277 | && TYPE_PRECISION (TREE_TYPE (@0)) > 1 | |
8e6cdc90 RS |
1278 | && (wi::to_wide (@2) |
1279 | == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1)) | |
564e405c JJ |
1280 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } |
1281 | (icmp (convert:stype @0) { build_int_cst (stype, 0); }))))) | |
1282 | ||
a8492d5e MG |
1283 | /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */ |
1284 | (for cmp (simple_comparison) | |
1285 | (simplify | |
1286 | (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2)) | |
8e6cdc90 | 1287 | (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))) |
a8492d5e MG |
1288 | (cmp @0 @1)))) |
1289 | ||
8d1628eb JJ |
1290 | /* X / C1 op C2 into a simple range test. */ |
1291 | (for cmp (simple_comparison) | |
1292 | (simplify | |
1293 | (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2) | |
1294 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1295 | && integer_nonzerop (@1) | |
1296 | && !TREE_OVERFLOW (@1) | |
1297 | && !TREE_OVERFLOW (@2)) | |
1298 | (with { tree lo, hi; bool neg_overflow; | |
1299 | enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi, | |
1300 | &neg_overflow); } | |
1301 | (switch | |
1302 | (if (code == LT_EXPR || code == GE_EXPR) | |
1303 | (if (TREE_OVERFLOW (lo)) | |
1304 | { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); } | |
1305 | (if (code == LT_EXPR) | |
1306 | (lt @0 { lo; }) | |
1307 | (ge @0 { lo; })))) | |
1308 | (if (code == LE_EXPR || code == GT_EXPR) | |
1309 | (if (TREE_OVERFLOW (hi)) | |
1310 | { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); } | |
1311 | (if (code == LE_EXPR) | |
1312 | (le @0 { hi; }) | |
1313 | (gt @0 { hi; })))) | |
1314 | (if (!lo && !hi) | |
1315 | { build_int_cst (type, code == NE_EXPR); }) | |
1316 | (if (code == EQ_EXPR && !hi) | |
1317 | (ge @0 { lo; })) | |
1318 | (if (code == EQ_EXPR && !lo) | |
1319 | (le @0 { hi; })) | |
1320 | (if (code == NE_EXPR && !hi) | |
1321 | (lt @0 { lo; })) | |
1322 | (if (code == NE_EXPR && !lo) | |
1323 | (gt @0 { hi; })) | |
1324 | (if (GENERIC) | |
1325 | { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR, | |
1326 | lo, hi); }) | |
1327 | (with | |
1328 | { | |
1329 | tree etype = range_check_type (TREE_TYPE (@0)); | |
1330 | if (etype) | |
1331 | { | |
1332 | if (! TYPE_UNSIGNED (etype)) | |
1333 | etype = unsigned_type_for (etype); | |
1334 | hi = fold_convert (etype, hi); | |
1335 | lo = fold_convert (etype, lo); | |
1336 | hi = const_binop (MINUS_EXPR, etype, hi, lo); | |
1337 | } | |
1338 | } | |
1339 | (if (etype && hi && !TREE_OVERFLOW (hi)) | |
1340 | (if (code == EQ_EXPR) | |
1341 | (le (minus (convert:etype @0) { lo; }) { hi; }) | |
1342 | (gt (minus (convert:etype @0) { lo; }) { hi; }))))))))) | |
1343 | ||
d35256b6 MG |
1344 | /* X + Z < Y + Z is the same as X < Y when there is no overflow. */ |
1345 | (for op (lt le ge gt) | |
1346 | (simplify | |
1347 | (op (plus:c @0 @2) (plus:c @1 @2)) | |
1348 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1349 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1350 | (op @0 @1)))) | |
1351 | /* For equality and subtraction, this is also true with wrapping overflow. */ | |
1352 | (for op (eq ne minus) | |
1353 | (simplify | |
1354 | (op (plus:c @0 @2) (plus:c @1 @2)) | |
1355 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1356 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1357 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
1358 | (op @0 @1)))) | |
1359 | ||
1360 | /* X - Z < Y - Z is the same as X < Y when there is no overflow. */ | |
1361 | (for op (lt le ge gt) | |
1362 | (simplify | |
1363 | (op (minus @0 @2) (minus @1 @2)) | |
1364 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1365 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1366 | (op @0 @1)))) | |
1367 | /* For equality and subtraction, this is also true with wrapping overflow. */ | |
1368 | (for op (eq ne minus) | |
1369 | (simplify | |
1370 | (op (minus @0 @2) (minus @1 @2)) | |
1371 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1372 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1373 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
1374 | (op @0 @1)))) | |
1af4ebf5 MG |
1375 | /* And for pointers... */ |
1376 | (for op (simple_comparison) | |
1377 | (simplify | |
1378 | (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2)) | |
1379 | (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
1380 | (op @0 @1)))) | |
1381 | (simplify | |
1382 | (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2)) | |
1383 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)) | |
1384 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
1385 | (pointer_diff @0 @1))) | |
d35256b6 MG |
1386 | |
1387 | /* Z - X < Z - Y is the same as Y < X when there is no overflow. */ | |
1388 | (for op (lt le ge gt) | |
1389 | (simplify | |
1390 | (op (minus @2 @0) (minus @2 @1)) | |
1391 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1392 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1393 | (op @1 @0)))) | |
1394 | /* For equality and subtraction, this is also true with wrapping overflow. */ | |
1395 | (for op (eq ne minus) | |
1396 | (simplify | |
1397 | (op (minus @2 @0) (minus @2 @1)) | |
1398 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1399 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1400 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
1401 | (op @1 @0)))) | |
1af4ebf5 MG |
1402 | /* And for pointers... */ |
1403 | (for op (simple_comparison) | |
1404 | (simplify | |
1405 | (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1)) | |
1406 | (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
1407 | (op @1 @0)))) | |
1408 | (simplify | |
1409 | (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1)) | |
1410 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)) | |
1411 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
1412 | (pointer_diff @1 @0))) | |
d35256b6 | 1413 | |
6358a676 MG |
1414 | /* X + Y < Y is the same as X < 0 when there is no overflow. */ |
1415 | (for op (lt le gt ge) | |
1416 | (simplify | |
1417 | (op:c (plus:c@2 @0 @1) @1) | |
1418 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1419 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1420 | && (CONSTANT_CLASS_P (@0) || single_use (@2))) | |
1421 | (op @0 { build_zero_cst (TREE_TYPE (@0)); })))) | |
1422 | /* For equality, this is also true with wrapping overflow. */ | |
1423 | (for op (eq ne) | |
1424 | (simplify | |
1425 | (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1)) | |
1426 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1427 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1428 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
1429 | && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3))) | |
1430 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2)) | |
1431 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))) | |
1432 | (op @0 { build_zero_cst (TREE_TYPE (@0)); }))) | |
1433 | (simplify | |
1434 | (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0)) | |
1435 | (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)) | |
1436 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
1437 | && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3)))) | |
1438 | (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) | |
1439 | ||
1440 | /* X - Y < X is the same as Y > 0 when there is no overflow. | |
1441 | For equality, this is also true with wrapping overflow. */ | |
1442 | (for op (simple_comparison) | |
1443 | (simplify | |
1444 | (op:c @0 (minus@2 @0 @1)) | |
1445 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1446 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1447 | || ((op == EQ_EXPR || op == NE_EXPR) | |
1448 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
1449 | && (CONSTANT_CLASS_P (@1) || single_use (@2))) | |
1450 | (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) | |
1451 | ||
1d6fadee PK |
1452 | /* Transform: |
1453 | * (X / Y) == 0 -> X < Y if X, Y are unsigned. | |
1454 | * (X / Y) != 0 -> X >= Y, if X, Y are unsigned. | |
1455 | */ | |
1456 | (for cmp (eq ne) | |
1457 | ocmp (lt ge) | |
1458 | (simplify | |
1459 | (cmp (trunc_div @0 @1) integer_zerop) | |
1460 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) | |
1461 | && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0)))) | |
1462 | (ocmp @0 @1)))) | |
1463 | ||
8b656ca7 MG |
1464 | /* X == C - X can never be true if C is odd. */ |
1465 | (for cmp (eq ne) | |
1466 | (simplify | |
1467 | (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0)))) | |
1468 | (if (TREE_INT_CST_LOW (@1) & 1) | |
1469 | { constant_boolean_node (cmp == NE_EXPR, type); }))) | |
1470 | ||
10bc8017 MG |
1471 | /* Arguments on which one can call get_nonzero_bits to get the bits |
1472 | possibly set. */ | |
1473 | (match with_possible_nonzero_bits | |
1474 | INTEGER_CST@0) | |
1475 | (match with_possible_nonzero_bits | |
1476 | SSA_NAME@0 | |
1477 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))))) | |
1478 | /* Slightly extended version, do not make it recursive to keep it cheap. */ | |
1479 | (match (with_possible_nonzero_bits2 @0) | |
1480 | with_possible_nonzero_bits@0) | |
1481 | (match (with_possible_nonzero_bits2 @0) | |
1482 | (bit_and:c with_possible_nonzero_bits@0 @2)) | |
1483 | ||
1484 | /* Same for bits that are known to be set, but we do not have | |
1485 | an equivalent to get_nonzero_bits yet. */ | |
1486 | (match (with_certain_nonzero_bits2 @0) | |
1487 | INTEGER_CST@0) | |
1488 | (match (with_certain_nonzero_bits2 @0) | |
1489 | (bit_ior @1 INTEGER_CST@0)) | |
1490 | ||
1491 | /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */ | |
1492 | (for cmp (eq ne) | |
1493 | (simplify | |
1494 | (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1)) | |
8e6cdc90 | 1495 | (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0) |
10bc8017 MG |
1496 | { constant_boolean_node (cmp == NE_EXPR, type); }))) |
1497 | ||
84ff66b8 AV |
1498 | /* ((X inner_op C0) outer_op C1) |
1499 | With X being a tree where value_range has reasoned certain bits to always be | |
1500 | zero throughout its computed value range, | |
1501 | inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op | |
1502 | where zero_mask has 1's for all bits that are sure to be 0 in | |
1503 | and 0's otherwise. | |
1504 | if (inner_op == '^') C0 &= ~C1; | |
1505 | if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1) | |
1506 | if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1) | |
1507 | */ | |
1508 | (for inner_op (bit_ior bit_xor) | |
1509 | outer_op (bit_xor bit_ior) | |
1510 | (simplify | |
1511 | (outer_op | |
1512 | (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1) | |
1513 | (with | |
1514 | { | |
1515 | bool fail = false; | |
1516 | wide_int zero_mask_not; | |
1517 | wide_int C0; | |
1518 | wide_int cst_emit; | |
1519 | ||
1520 | if (TREE_CODE (@2) == SSA_NAME) | |
1521 | zero_mask_not = get_nonzero_bits (@2); | |
1522 | else | |
1523 | fail = true; | |
1524 | ||
1525 | if (inner_op == BIT_XOR_EXPR) | |
1526 | { | |
8e6cdc90 RS |
1527 | C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1)); |
1528 | cst_emit = C0 | wi::to_wide (@1); | |
84ff66b8 AV |
1529 | } |
1530 | else | |
1531 | { | |
8e6cdc90 RS |
1532 | C0 = wi::to_wide (@0); |
1533 | cst_emit = C0 ^ wi::to_wide (@1); | |
84ff66b8 AV |
1534 | } |
1535 | } | |
8e6cdc90 | 1536 | (if (!fail && (C0 & zero_mask_not) == 0) |
84ff66b8 | 1537 | (outer_op @2 { wide_int_to_tree (type, cst_emit); }) |
8e6cdc90 | 1538 | (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0) |
84ff66b8 AV |
1539 | (inner_op @2 { wide_int_to_tree (type, cst_emit); })))))) |
1540 | ||
a499aac5 RB |
1541 | /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */ |
1542 | (simplify | |
44fc0a51 RB |
1543 | (pointer_plus (pointer_plus:s @0 @1) @3) |
1544 | (pointer_plus @0 (plus @1 @3))) | |
a499aac5 RB |
1545 | |
1546 | /* Pattern match | |
1547 | tem1 = (long) ptr1; | |
1548 | tem2 = (long) ptr2; | |
1549 | tem3 = tem2 - tem1; | |
1550 | tem4 = (unsigned long) tem3; | |
1551 | tem5 = ptr1 + tem4; | |
1552 | and produce | |
1553 | tem5 = ptr2; */ | |
1554 | (simplify | |
1555 | (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0)))) | |
1556 | /* Conditionally look through a sign-changing conversion. */ | |
1557 | (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3)) | |
1558 | && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1))) | |
1559 | || (GENERIC && type == TREE_TYPE (@1)))) | |
1560 | @1)) | |
1af4ebf5 MG |
1561 | (simplify |
1562 | (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0))) | |
1563 | (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3))) | |
1564 | (convert @1))) | |
a499aac5 RB |
1565 | |
1566 | /* Pattern match | |
1567 | tem = (sizetype) ptr; | |
1568 | tem = tem & algn; | |
1569 | tem = -tem; | |
1570 | ... = ptr p+ tem; | |
1571 | and produce the simpler and easier to analyze with respect to alignment | |
1572 | ... = ptr & ~algn; */ | |
1573 | (simplify | |
1574 | (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1))) | |
8e6cdc90 | 1575 | (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); } |
a499aac5 RB |
1576 | (bit_and @0 { algn; }))) |
1577 | ||
99e943a2 RB |
1578 | /* Try folding difference of addresses. */ |
1579 | (simplify | |
1580 | (minus (convert ADDR_EXPR@0) (convert @1)) | |
1581 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
f37fac2b | 1582 | (with { poly_int64 diff; } |
99e943a2 RB |
1583 | (if (ptr_difference_const (@0, @1, &diff)) |
1584 | { build_int_cst_type (type, diff); })))) | |
1585 | (simplify | |
1586 | (minus (convert @0) (convert ADDR_EXPR@1)) | |
1587 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
f37fac2b | 1588 | (with { poly_int64 diff; } |
99e943a2 RB |
1589 | (if (ptr_difference_const (@0, @1, &diff)) |
1590 | { build_int_cst_type (type, diff); })))) | |
1af4ebf5 MG |
1591 | (simplify |
1592 | (pointer_diff (convert?@2 ADDR_EXPR@0) (convert?@3 @1)) | |
1593 | (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0)) | |
1594 | && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1))) | |
f37fac2b | 1595 | (with { poly_int64 diff; } |
1af4ebf5 MG |
1596 | (if (ptr_difference_const (@0, @1, &diff)) |
1597 | { build_int_cst_type (type, diff); })))) | |
1598 | (simplify | |
1599 | (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1)) | |
1600 | (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0)) | |
1601 | && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1))) | |
f37fac2b | 1602 | (with { poly_int64 diff; } |
1af4ebf5 MG |
1603 | (if (ptr_difference_const (@0, @1, &diff)) |
1604 | { build_int_cst_type (type, diff); })))) | |
99e943a2 | 1605 | |
bab73f11 RB |
1606 | /* If arg0 is derived from the address of an object or function, we may |
1607 | be able to fold this expression using the object or function's | |
1608 | alignment. */ | |
1609 | (simplify | |
1610 | (bit_and (convert? @0) INTEGER_CST@1) | |
1611 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1612 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1613 | (with | |
1614 | { | |
1615 | unsigned int align; | |
1616 | unsigned HOST_WIDE_INT bitpos; | |
1617 | get_pointer_alignment_1 (@0, &align, &bitpos); | |
1618 | } | |
8e6cdc90 RS |
1619 | (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT)) |
1620 | { wide_int_to_tree (type, (wi::to_wide (@1) | |
1621 | & (bitpos / BITS_PER_UNIT))); })))) | |
99e943a2 | 1622 | |
a499aac5 | 1623 | |
cc7b5acf RB |
1624 | /* We can't reassociate at all for saturating types. */ |
1625 | (if (!TYPE_SATURATING (type)) | |
1626 | ||
1627 | /* Contract negates. */ | |
1628 | /* A + (-B) -> A - B */ | |
1629 | (simplify | |
248179b5 RB |
1630 | (plus:c @0 (convert? (negate @1))) |
1631 | /* Apply STRIP_NOPS on the negate. */ | |
1632 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 1633 | && !TYPE_OVERFLOW_SANITIZED (type)) |
248179b5 RB |
1634 | (with |
1635 | { | |
1636 | tree t1 = type; | |
1637 | if (INTEGRAL_TYPE_P (type) | |
1638 | && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
1639 | t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); | |
1640 | } | |
1641 | (convert (minus (convert:t1 @0) (convert:t1 @1)))))) | |
cc7b5acf RB |
1642 | /* A - (-B) -> A + B */ |
1643 | (simplify | |
248179b5 RB |
1644 | (minus @0 (convert? (negate @1))) |
1645 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 1646 | && !TYPE_OVERFLOW_SANITIZED (type)) |
248179b5 RB |
1647 | (with |
1648 | { | |
1649 | tree t1 = type; | |
1650 | if (INTEGRAL_TYPE_P (type) | |
1651 | && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
1652 | t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); | |
1653 | } | |
1654 | (convert (plus (convert:t1 @0) (convert:t1 @1)))))) | |
63626547 MG |
1655 | /* -(T)(-A) -> (T)A |
1656 | Sign-extension is ok except for INT_MIN, which thankfully cannot | |
1657 | happen without overflow. */ | |
1658 | (simplify | |
1659 | (negate (convert (negate @1))) | |
1660 | (if (INTEGRAL_TYPE_P (type) | |
1661 | && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1)) | |
1662 | || (!TYPE_UNSIGNED (TREE_TYPE (@1)) | |
1663 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
1664 | && !TYPE_OVERFLOW_SANITIZED (type) | |
1665 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) | |
a0f12cf8 | 1666 | (convert @1))) |
63626547 MG |
1667 | (simplify |
1668 | (negate (convert negate_expr_p@1)) | |
1669 | (if (SCALAR_FLOAT_TYPE_P (type) | |
1670 | && ((DECIMAL_FLOAT_TYPE_P (type) | |
1671 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)) | |
1672 | && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1))) | |
1673 | || !HONOR_SIGN_DEPENDENT_ROUNDING (type))) | |
1674 | (convert (negate @1)))) | |
1675 | (simplify | |
1676 | (negate (nop_convert (negate @1))) | |
1677 | (if (!TYPE_OVERFLOW_SANITIZED (type) | |
1678 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) | |
1679 | (view_convert @1))) | |
cc7b5acf | 1680 | |
7318e44f RB |
1681 | /* We can't reassociate floating-point unless -fassociative-math |
1682 | or fixed-point plus or minus because of saturation to +-Inf. */ | |
1683 | (if ((!FLOAT_TYPE_P (type) || flag_associative_math) | |
1684 | && !FIXED_POINT_TYPE_P (type)) | |
cc7b5acf RB |
1685 | |
1686 | /* Match patterns that allow contracting a plus-minus pair | |
1687 | irrespective of overflow issues. */ | |
1688 | /* (A +- B) - A -> +- B */ | |
1689 | /* (A +- B) -+ B -> A */ | |
1690 | /* A - (A +- B) -> -+ B */ | |
1691 | /* A +- (B -+ A) -> +- B */ | |
1692 | (simplify | |
1693 | (minus (plus:c @0 @1) @0) | |
1694 | @1) | |
1695 | (simplify | |
1696 | (minus (minus @0 @1) @0) | |
1697 | (negate @1)) | |
1698 | (simplify | |
1699 | (plus:c (minus @0 @1) @1) | |
1700 | @0) | |
1701 | (simplify | |
1702 | (minus @0 (plus:c @0 @1)) | |
1703 | (negate @1)) | |
1704 | (simplify | |
1705 | (minus @0 (minus @0 @1)) | |
1706 | @1) | |
1e7df2e6 MG |
1707 | /* (A +- B) + (C - A) -> C +- B */ |
1708 | /* (A + B) - (A - C) -> B + C */ | |
1709 | /* More cases are handled with comparisons. */ | |
1710 | (simplify | |
1711 | (plus:c (plus:c @0 @1) (minus @2 @0)) | |
1712 | (plus @2 @1)) | |
1713 | (simplify | |
1714 | (plus:c (minus @0 @1) (minus @2 @0)) | |
1715 | (minus @2 @1)) | |
1af4ebf5 MG |
1716 | (simplify |
1717 | (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0)) | |
1718 | (if (TYPE_OVERFLOW_UNDEFINED (type) | |
1719 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))) | |
1720 | (pointer_diff @2 @1))) | |
1e7df2e6 MG |
1721 | (simplify |
1722 | (minus (plus:c @0 @1) (minus @0 @2)) | |
1723 | (plus @1 @2)) | |
cc7b5acf | 1724 | |
ed73f46f MG |
1725 | /* (A +- CST1) +- CST2 -> A + CST3 |
1726 | Use view_convert because it is safe for vectors and equivalent for | |
1727 | scalars. */ | |
cc7b5acf RB |
1728 | (for outer_op (plus minus) |
1729 | (for inner_op (plus minus) | |
ed73f46f | 1730 | neg_inner_op (minus plus) |
cc7b5acf | 1731 | (simplify |
ed73f46f MG |
1732 | (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1)) |
1733 | CONSTANT_CLASS_P@2) | |
1734 | /* If one of the types wraps, use that one. */ | |
1735 | (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) | |
1736 | (if (outer_op == PLUS_EXPR) | |
1737 | (plus (view_convert @0) (inner_op @2 (view_convert @1))) | |
1738 | (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))) | |
1739 | (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1740 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
1741 | (if (outer_op == PLUS_EXPR) | |
1742 | (view_convert (plus @0 (inner_op (view_convert @2) @1))) | |
1743 | (view_convert (minus @0 (neg_inner_op (view_convert @2) @1)))) | |
1744 | /* If the constant operation overflows we cannot do the transform | |
1745 | directly as we would introduce undefined overflow, for example | |
1746 | with (a - 1) + INT_MIN. */ | |
1747 | (if (types_match (type, @0)) | |
1748 | (with { tree cst = const_binop (outer_op == inner_op | |
1749 | ? PLUS_EXPR : MINUS_EXPR, | |
1750 | type, @1, @2); } | |
1751 | (if (cst && !TREE_OVERFLOW (cst)) | |
1752 | (inner_op @0 { cst; } ) | |
1753 | /* X+INT_MAX+1 is X-INT_MIN. */ | |
1754 | (if (INTEGRAL_TYPE_P (type) && cst | |
8e6cdc90 RS |
1755 | && wi::to_wide (cst) == wi::min_value (type)) |
1756 | (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); }) | |
ed73f46f MG |
1757 | /* Last resort, use some unsigned type. */ |
1758 | (with { tree utype = unsigned_type_for (type); } | |
1759 | (view_convert (inner_op | |
1760 | (view_convert:utype @0) | |
1761 | (view_convert:utype | |
1762 | { drop_tree_overflow (cst); }))))))))))))) | |
cc7b5acf | 1763 | |
b302f2e0 | 1764 | /* (CST1 - A) +- CST2 -> CST3 - A */ |
cc7b5acf RB |
1765 | (for outer_op (plus minus) |
1766 | (simplify | |
1767 | (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2) | |
23f27839 | 1768 | (with { tree cst = const_binop (outer_op, type, @1, @2); } |
cc7b5acf RB |
1769 | (if (cst && !TREE_OVERFLOW (cst)) |
1770 | (minus { cst; } @0))))) | |
1771 | ||
b302f2e0 RB |
1772 | /* CST1 - (CST2 - A) -> CST3 + A */ |
1773 | (simplify | |
1774 | (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0)) | |
1775 | (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } | |
1776 | (if (cst && !TREE_OVERFLOW (cst)) | |
1777 | (plus { cst; } @0)))) | |
1778 | ||
cc7b5acf RB |
1779 | /* ~A + A -> -1 */ |
1780 | (simplify | |
1781 | (plus:c (bit_not @0) @0) | |
1782 | (if (!TYPE_OVERFLOW_TRAPS (type)) | |
1783 | { build_all_ones_cst (type); })) | |
1784 | ||
1785 | /* ~A + 1 -> -A */ | |
1786 | (simplify | |
e19740ae RB |
1787 | (plus (convert? (bit_not @0)) integer_each_onep) |
1788 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1789 | (negate (convert @0)))) | |
1790 | ||
1791 | /* -A - 1 -> ~A */ | |
1792 | (simplify | |
1793 | (minus (convert? (negate @0)) integer_each_onep) | |
1794 | (if (!TYPE_OVERFLOW_TRAPS (type) | |
1795 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1796 | (bit_not (convert @0)))) | |
1797 | ||
1798 | /* -1 - A -> ~A */ | |
1799 | (simplify | |
1800 | (minus integer_all_onesp @0) | |
bc4315fb | 1801 | (bit_not @0)) |
cc7b5acf RB |
1802 | |
1803 | /* (T)(P + A) - (T)P -> (T) A */ | |
d7f44d4d | 1804 | (simplify |
a72610d4 JJ |
1805 | (minus (convert (plus:c @@0 @1)) |
1806 | (convert? @0)) | |
d7f44d4d JJ |
1807 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
1808 | /* For integer types, if A has a smaller type | |
1809 | than T the result depends on the possible | |
1810 | overflow in P + A. | |
1811 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
1812 | However, if an overflow in P + A would cause | |
1813 | undefined behavior, we can assume that there | |
1814 | is no overflow. */ | |
a72610d4 JJ |
1815 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
1816 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
d7f44d4d JJ |
1817 | (convert @1))) |
1818 | (simplify | |
1819 | (minus (convert (pointer_plus @@0 @1)) | |
1820 | (convert @0)) | |
1821 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
1822 | /* For pointer types, if the conversion of A to the | |
1823 | final type requires a sign- or zero-extension, | |
1824 | then we have to punt - it is not defined which | |
1825 | one is correct. */ | |
1826 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1827 | && TREE_CODE (@1) == INTEGER_CST | |
1828 | && tree_int_cst_sign_bit (@1) == 0)) | |
1829 | (convert @1))) | |
1af4ebf5 MG |
1830 | (simplify |
1831 | (pointer_diff (pointer_plus @@0 @1) @0) | |
1832 | /* The second argument of pointer_plus must be interpreted as signed, and | |
1833 | thus sign-extended if necessary. */ | |
1834 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
1835 | (convert (convert:stype @1)))) | |
a8fc2579 RB |
1836 | |
1837 | /* (T)P - (T)(P + A) -> -(T) A */ | |
d7f44d4d | 1838 | (simplify |
a72610d4 JJ |
1839 | (minus (convert? @0) |
1840 | (convert (plus:c @@0 @1))) | |
d7f44d4d JJ |
1841 | (if (INTEGRAL_TYPE_P (type) |
1842 | && TYPE_OVERFLOW_UNDEFINED (type) | |
1843 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
1844 | (with { tree utype = unsigned_type_for (type); } | |
1845 | (convert (negate (convert:utype @1)))) | |
a8fc2579 RB |
1846 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
1847 | /* For integer types, if A has a smaller type | |
1848 | than T the result depends on the possible | |
1849 | overflow in P + A. | |
1850 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
1851 | However, if an overflow in P + A would cause | |
1852 | undefined behavior, we can assume that there | |
1853 | is no overflow. */ | |
a72610d4 JJ |
1854 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
1855 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
d7f44d4d JJ |
1856 | (negate (convert @1))))) |
1857 | (simplify | |
1858 | (minus (convert @0) | |
1859 | (convert (pointer_plus @@0 @1))) | |
1860 | (if (INTEGRAL_TYPE_P (type) | |
1861 | && TYPE_OVERFLOW_UNDEFINED (type) | |
1862 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
1863 | (with { tree utype = unsigned_type_for (type); } | |
1864 | (convert (negate (convert:utype @1)))) | |
1865 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
a8fc2579 RB |
1866 | /* For pointer types, if the conversion of A to the |
1867 | final type requires a sign- or zero-extension, | |
1868 | then we have to punt - it is not defined which | |
1869 | one is correct. */ | |
1870 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1871 | && TREE_CODE (@1) == INTEGER_CST | |
1872 | && tree_int_cst_sign_bit (@1) == 0)) | |
1873 | (negate (convert @1))))) | |
1af4ebf5 MG |
1874 | (simplify |
1875 | (pointer_diff @0 (pointer_plus @@0 @1)) | |
1876 | /* The second argument of pointer_plus must be interpreted as signed, and | |
1877 | thus sign-extended if necessary. */ | |
1878 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
1879 | (negate (convert (convert:stype @1))))) | |
a8fc2579 RB |
1880 | |
1881 | /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */ | |
d7f44d4d | 1882 | (simplify |
a72610d4 | 1883 | (minus (convert (plus:c @@0 @1)) |
d7f44d4d JJ |
1884 | (convert (plus:c @0 @2))) |
1885 | (if (INTEGRAL_TYPE_P (type) | |
1886 | && TYPE_OVERFLOW_UNDEFINED (type) | |
a72610d4 JJ |
1887 | && element_precision (type) <= element_precision (TREE_TYPE (@1)) |
1888 | && element_precision (type) <= element_precision (TREE_TYPE (@2))) | |
d7f44d4d JJ |
1889 | (with { tree utype = unsigned_type_for (type); } |
1890 | (convert (minus (convert:utype @1) (convert:utype @2)))) | |
a72610d4 JJ |
1891 | (if (((element_precision (type) <= element_precision (TREE_TYPE (@1))) |
1892 | == (element_precision (type) <= element_precision (TREE_TYPE (@2)))) | |
1893 | && (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
1894 | /* For integer types, if A has a smaller type | |
1895 | than T the result depends on the possible | |
1896 | overflow in P + A. | |
1897 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
1898 | However, if an overflow in P + A would cause | |
1899 | undefined behavior, we can assume that there | |
1900 | is no overflow. */ | |
1901 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
1902 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
1903 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)) | |
1904 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2))))) | |
d7f44d4d JJ |
1905 | (minus (convert @1) (convert @2))))) |
1906 | (simplify | |
1907 | (minus (convert (pointer_plus @@0 @1)) | |
1908 | (convert (pointer_plus @0 @2))) | |
1909 | (if (INTEGRAL_TYPE_P (type) | |
1910 | && TYPE_OVERFLOW_UNDEFINED (type) | |
1911 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
1912 | (with { tree utype = unsigned_type_for (type); } | |
1913 | (convert (minus (convert:utype @1) (convert:utype @2)))) | |
1914 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
a8fc2579 RB |
1915 | /* For pointer types, if the conversion of A to the |
1916 | final type requires a sign- or zero-extension, | |
1917 | then we have to punt - it is not defined which | |
1918 | one is correct. */ | |
1919 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1920 | && TREE_CODE (@1) == INTEGER_CST | |
1921 | && tree_int_cst_sign_bit (@1) == 0 | |
1922 | && TREE_CODE (@2) == INTEGER_CST | |
1923 | && tree_int_cst_sign_bit (@2) == 0)) | |
d7f44d4d | 1924 | (minus (convert @1) (convert @2))))) |
1af4ebf5 MG |
1925 | (simplify |
1926 | (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2)) | |
1927 | /* The second argument of pointer_plus must be interpreted as signed, and | |
1928 | thus sign-extended if necessary. */ | |
1929 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
d7f44d4d | 1930 | (minus (convert (convert:stype @1)) (convert (convert:stype @2))))))) |
cc7b5acf RB |
1931 | |
1932 | ||
0122e8e5 | 1933 | /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */ |
a7f24614 | 1934 | |
c6cfa2bf | 1935 | (for minmax (min max FMIN_ALL FMAX_ALL) |
a7f24614 RB |
1936 | (simplify |
1937 | (minmax @0 @0) | |
1938 | @0)) | |
4a334cba RS |
1939 | /* min(max(x,y),y) -> y. */ |
1940 | (simplify | |
1941 | (min:c (max:c @0 @1) @1) | |
1942 | @1) | |
1943 | /* max(min(x,y),y) -> y. */ | |
1944 | (simplify | |
1945 | (max:c (min:c @0 @1) @1) | |
1946 | @1) | |
d657e995 RB |
1947 | /* max(a,-a) -> abs(a). */ |
1948 | (simplify | |
1949 | (max:c @0 (negate @0)) | |
1950 | (if (TREE_CODE (type) != COMPLEX_TYPE | |
1951 | && (! ANY_INTEGRAL_TYPE_P (type) | |
1952 | || TYPE_OVERFLOW_UNDEFINED (type))) | |
1953 | (abs @0))) | |
54f84ca9 RB |
1954 | /* min(a,-a) -> -abs(a). */ |
1955 | (simplify | |
1956 | (min:c @0 (negate @0)) | |
1957 | (if (TREE_CODE (type) != COMPLEX_TYPE | |
1958 | && (! ANY_INTEGRAL_TYPE_P (type) | |
1959 | || TYPE_OVERFLOW_UNDEFINED (type))) | |
1960 | (negate (abs @0)))) | |
a7f24614 RB |
1961 | (simplify |
1962 | (min @0 @1) | |
2c2870a1 MG |
1963 | (switch |
1964 | (if (INTEGRAL_TYPE_P (type) | |
1965 | && TYPE_MIN_VALUE (type) | |
1966 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
1967 | @1) | |
1968 | (if (INTEGRAL_TYPE_P (type) | |
1969 | && TYPE_MAX_VALUE (type) | |
1970 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
1971 | @0))) | |
a7f24614 RB |
1972 | (simplify |
1973 | (max @0 @1) | |
2c2870a1 MG |
1974 | (switch |
1975 | (if (INTEGRAL_TYPE_P (type) | |
1976 | && TYPE_MAX_VALUE (type) | |
1977 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
1978 | @1) | |
1979 | (if (INTEGRAL_TYPE_P (type) | |
1980 | && TYPE_MIN_VALUE (type) | |
1981 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
1982 | @0))) | |
ad6e4ba8 | 1983 | |
182f37c9 N |
1984 | /* max (a, a + CST) -> a + CST where CST is positive. */ |
1985 | /* max (a, a + CST) -> a where CST is negative. */ | |
1986 | (simplify | |
1987 | (max:c @0 (plus@2 @0 INTEGER_CST@1)) | |
1988 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1989 | (if (tree_int_cst_sgn (@1) > 0) | |
1990 | @2 | |
1991 | @0))) | |
1992 | ||
1993 | /* min (a, a + CST) -> a where CST is positive. */ | |
1994 | /* min (a, a + CST) -> a + CST where CST is negative. */ | |
1995 | (simplify | |
1996 | (min:c @0 (plus@2 @0 INTEGER_CST@1)) | |
1997 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1998 | (if (tree_int_cst_sgn (@1) > 0) | |
1999 | @0 | |
2000 | @2))) | |
2001 | ||
ad6e4ba8 BC |
2002 | /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted |
2003 | and the outer convert demotes the expression back to x's type. */ | |
2004 | (for minmax (min max) | |
2005 | (simplify | |
2006 | (convert (minmax@0 (convert @1) INTEGER_CST@2)) | |
ebf41734 BC |
2007 | (if (INTEGRAL_TYPE_P (type) |
2008 | && types_match (@1, type) && int_fits_type_p (@2, type) | |
ad6e4ba8 BC |
2009 | && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type) |
2010 | && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)) | |
2011 | (minmax @1 (convert @2))))) | |
2012 | ||
c6cfa2bf | 2013 | (for minmax (FMIN_ALL FMAX_ALL) |
0122e8e5 RS |
2014 | /* If either argument is NaN, return the other one. Avoid the |
2015 | transformation if we get (and honor) a signalling NaN. */ | |
2016 | (simplify | |
2017 | (minmax:c @0 REAL_CST@1) | |
2018 | (if (real_isnan (TREE_REAL_CST_PTR (@1)) | |
2019 | && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)) | |
2020 | @0))) | |
2021 | /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these | |
2022 | functions to return the numeric arg if the other one is NaN. | |
2023 | MIN and MAX don't honor that, so only transform if -ffinite-math-only | |
2024 | is set. C99 doesn't require -0.0 to be handled, so we don't have to | |
2025 | worry about it either. */ | |
2026 | (if (flag_finite_math_only) | |
2027 | (simplify | |
c6cfa2bf | 2028 | (FMIN_ALL @0 @1) |
0122e8e5 | 2029 | (min @0 @1)) |
4119b2eb | 2030 | (simplify |
c6cfa2bf | 2031 | (FMAX_ALL @0 @1) |
0122e8e5 | 2032 | (max @0 @1))) |
ce0e66ff | 2033 | /* min (-A, -B) -> -max (A, B) */ |
c6cfa2bf MM |
2034 | (for minmax (min max FMIN_ALL FMAX_ALL) |
2035 | maxmin (max min FMAX_ALL FMIN_ALL) | |
ce0e66ff MG |
2036 | (simplify |
2037 | (minmax (negate:s@2 @0) (negate:s@3 @1)) | |
2038 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
2039 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2040 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
2041 | (negate (maxmin @0 @1))))) | |
2042 | /* MIN (~X, ~Y) -> ~MAX (X, Y) | |
2043 | MAX (~X, ~Y) -> ~MIN (X, Y) */ | |
2044 | (for minmax (min max) | |
2045 | maxmin (max min) | |
2046 | (simplify | |
2047 | (minmax (bit_not:s@2 @0) (bit_not:s@3 @1)) | |
2048 | (bit_not (maxmin @0 @1)))) | |
a7f24614 | 2049 | |
b4817bd6 MG |
2050 | /* MIN (X, Y) == X -> X <= Y */ |
2051 | (for minmax (min min max max) | |
2052 | cmp (eq ne eq ne ) | |
2053 | out (le gt ge lt ) | |
2054 | (simplify | |
2055 | (cmp:c (minmax:c @0 @1) @0) | |
2056 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
2057 | (out @0 @1)))) | |
2058 | /* MIN (X, 5) == 0 -> X == 0 | |
2059 | MIN (X, 5) == 7 -> false */ | |
2060 | (for cmp (eq ne) | |
2061 | (simplify | |
2062 | (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2) | |
8e6cdc90 RS |
2063 | (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2), |
2064 | TYPE_SIGN (TREE_TYPE (@0)))) | |
b4817bd6 | 2065 | { constant_boolean_node (cmp == NE_EXPR, type); } |
8e6cdc90 RS |
2066 | (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2), |
2067 | TYPE_SIGN (TREE_TYPE (@0)))) | |
b4817bd6 MG |
2068 | (cmp @0 @2))))) |
2069 | (for cmp (eq ne) | |
2070 | (simplify | |
2071 | (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2) | |
8e6cdc90 RS |
2072 | (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2), |
2073 | TYPE_SIGN (TREE_TYPE (@0)))) | |
b4817bd6 | 2074 | { constant_boolean_node (cmp == NE_EXPR, type); } |
8e6cdc90 RS |
2075 | (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2), |
2076 | TYPE_SIGN (TREE_TYPE (@0)))) | |
b4817bd6 MG |
2077 | (cmp @0 @2))))) |
2078 | /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */ | |
2079 | (for minmax (min min max max min min max max ) | |
2080 | cmp (lt le gt ge gt ge lt le ) | |
2081 | comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and) | |
2082 | (simplify | |
2083 | (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2) | |
2084 | (comb (cmp @0 @2) (cmp @1 @2)))) | |
2085 | ||
a7f24614 RB |
2086 | /* Simplifications of shift and rotates. */ |
2087 | ||
2088 | (for rotate (lrotate rrotate) | |
2089 | (simplify | |
2090 | (rotate integer_all_onesp@0 @1) | |
2091 | @0)) | |
2092 | ||
2093 | /* Optimize -1 >> x for arithmetic right shifts. */ | |
2094 | (simplify | |
2095 | (rshift integer_all_onesp@0 @1) | |
2096 | (if (!TYPE_UNSIGNED (type) | |
2097 | && tree_expr_nonnegative_p (@1)) | |
2098 | @0)) | |
2099 | ||
12085390 N |
2100 | /* Optimize (x >> c) << c into x & (-1<<c). */ |
2101 | (simplify | |
2102 | (lshift (rshift @0 INTEGER_CST@1) @1) | |
8e6cdc90 | 2103 | (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))) |
12085390 N |
2104 | (bit_and @0 (lshift { build_minus_one_cst (type); } @1)))) |
2105 | ||
2106 | /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned | |
2107 | types. */ | |
2108 | (simplify | |
2109 | (rshift (lshift @0 INTEGER_CST@1) @1) | |
2110 | (if (TYPE_UNSIGNED (type) | |
8e6cdc90 | 2111 | && (wi::ltu_p (wi::to_wide (@1), element_precision (type)))) |
12085390 N |
2112 | (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) |
2113 | ||
a7f24614 RB |
2114 | (for shiftrotate (lrotate rrotate lshift rshift) |
2115 | (simplify | |
2116 | (shiftrotate @0 integer_zerop) | |
2117 | (non_lvalue @0)) | |
2118 | (simplify | |
2119 | (shiftrotate integer_zerop@0 @1) | |
2120 | @0) | |
2121 | /* Prefer vector1 << scalar to vector1 << vector2 | |
2122 | if vector2 is uniform. */ | |
2123 | (for vec (VECTOR_CST CONSTRUCTOR) | |
2124 | (simplify | |
2125 | (shiftrotate @0 vec@1) | |
2126 | (with { tree tem = uniform_vector_p (@1); } | |
2127 | (if (tem) | |
2128 | (shiftrotate @0 { tem; })))))) | |
2129 | ||
165ba2e9 JJ |
2130 | /* Simplify X << Y where Y's low width bits are 0 to X, as only valid |
2131 | Y is 0. Similarly for X >> Y. */ | |
2132 | #if GIMPLE | |
2133 | (for shift (lshift rshift) | |
2134 | (simplify | |
2135 | (shift @0 SSA_NAME@1) | |
2136 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
2137 | (with { | |
2138 | int width = ceil_log2 (element_precision (TREE_TYPE (@0))); | |
2139 | int prec = TYPE_PRECISION (TREE_TYPE (@1)); | |
2140 | } | |
2141 | (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0) | |
2142 | @0))))) | |
2143 | #endif | |
2144 | ||
a7f24614 RB |
2145 | /* Rewrite an LROTATE_EXPR by a constant into an |
2146 | RROTATE_EXPR by a new constant. */ | |
2147 | (simplify | |
2148 | (lrotate @0 INTEGER_CST@1) | |
23f27839 | 2149 | (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1), |
a7f24614 RB |
2150 | build_int_cst (TREE_TYPE (@1), |
2151 | element_precision (type)), @1); })) | |
2152 | ||
14ea9f92 RB |
2153 | /* Turn (a OP c1) OP c2 into a OP (c1+c2). */ |
2154 | (for op (lrotate rrotate rshift lshift) | |
2155 | (simplify | |
2156 | (op (op @0 INTEGER_CST@1) INTEGER_CST@2) | |
2157 | (with { unsigned int prec = element_precision (type); } | |
8e6cdc90 RS |
2158 | (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))) |
2159 | && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1))) | |
2160 | && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))) | |
2161 | && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2)))) | |
a1488398 RS |
2162 | (with { unsigned int low = (tree_to_uhwi (@1) |
2163 | + tree_to_uhwi (@2)); } | |
14ea9f92 RB |
2164 | /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2 |
2165 | being well defined. */ | |
2166 | (if (low >= prec) | |
2167 | (if (op == LROTATE_EXPR || op == RROTATE_EXPR) | |
8fdc6c67 | 2168 | (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); }) |
50301115 | 2169 | (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR) |
8fdc6c67 RB |
2170 | { build_zero_cst (type); } |
2171 | (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); }))) | |
2172 | (op @0 { build_int_cst (TREE_TYPE (@1), low); }))))))) | |
14ea9f92 RB |
2173 | |
2174 | ||
01ada710 MP |
2175 | /* ((1 << A) & 1) != 0 -> A == 0 |
2176 | ((1 << A) & 1) == 0 -> A != 0 */ | |
2177 | (for cmp (ne eq) | |
2178 | icmp (eq ne) | |
2179 | (simplify | |
2180 | (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop) | |
2181 | (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))) | |
cc7b5acf | 2182 | |
f2e609c3 MP |
2183 | /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) |
2184 | (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) | |
2185 | if CST2 != 0. */ | |
2186 | (for cmp (ne eq) | |
2187 | (simplify | |
2188 | (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2) | |
8e6cdc90 | 2189 | (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); } |
f2e609c3 MP |
2190 | (if (cand < 0 |
2191 | || (!integer_zerop (@2) | |
8e6cdc90 | 2192 | && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2))) |
8fdc6c67 RB |
2193 | { constant_boolean_node (cmp == NE_EXPR, type); } |
2194 | (if (!integer_zerop (@2) | |
8e6cdc90 | 2195 | && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2)) |
8fdc6c67 | 2196 | (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); })))))) |
f2e609c3 | 2197 | |
1ffbaa3f RB |
2198 | /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) |
2199 | (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) | |
2200 | if the new mask might be further optimized. */ | |
2201 | (for shift (lshift rshift) | |
2202 | (simplify | |
44fc0a51 RB |
2203 | (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1)) |
2204 | INTEGER_CST@2) | |
1ffbaa3f RB |
2205 | (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5)) |
2206 | && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT | |
2207 | && tree_fits_uhwi_p (@1) | |
2208 | && tree_to_uhwi (@1) > 0 | |
2209 | && tree_to_uhwi (@1) < TYPE_PRECISION (type)) | |
2210 | (with | |
2211 | { | |
2212 | unsigned int shiftc = tree_to_uhwi (@1); | |
2213 | unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2); | |
2214 | unsigned HOST_WIDE_INT newmask, zerobits = 0; | |
2215 | tree shift_type = TREE_TYPE (@3); | |
2216 | unsigned int prec; | |
2217 | ||
2218 | if (shift == LSHIFT_EXPR) | |
fecfbfa4 | 2219 | zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1); |
1ffbaa3f | 2220 | else if (shift == RSHIFT_EXPR |
2be65d9e | 2221 | && type_has_mode_precision_p (shift_type)) |
1ffbaa3f RB |
2222 | { |
2223 | prec = TYPE_PRECISION (TREE_TYPE (@3)); | |
2224 | tree arg00 = @0; | |
2225 | /* See if more bits can be proven as zero because of | |
2226 | zero extension. */ | |
2227 | if (@3 != @0 | |
2228 | && TYPE_UNSIGNED (TREE_TYPE (@0))) | |
2229 | { | |
2230 | tree inner_type = TREE_TYPE (@0); | |
2be65d9e | 2231 | if (type_has_mode_precision_p (inner_type) |
1ffbaa3f RB |
2232 | && TYPE_PRECISION (inner_type) < prec) |
2233 | { | |
2234 | prec = TYPE_PRECISION (inner_type); | |
2235 | /* See if we can shorten the right shift. */ | |
2236 | if (shiftc < prec) | |
2237 | shift_type = inner_type; | |
2238 | /* Otherwise X >> C1 is all zeros, so we'll optimize | |
2239 | it into (X, 0) later on by making sure zerobits | |
2240 | is all ones. */ | |
2241 | } | |
2242 | } | |
dd4786fe | 2243 | zerobits = HOST_WIDE_INT_M1U; |
1ffbaa3f RB |
2244 | if (shiftc < prec) |
2245 | { | |
2246 | zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; | |
2247 | zerobits <<= prec - shiftc; | |
2248 | } | |
2249 | /* For arithmetic shift if sign bit could be set, zerobits | |
2250 | can contain actually sign bits, so no transformation is | |
2251 | possible, unless MASK masks them all away. In that | |
2252 | case the shift needs to be converted into logical shift. */ | |
2253 | if (!TYPE_UNSIGNED (TREE_TYPE (@3)) | |
2254 | && prec == TYPE_PRECISION (TREE_TYPE (@3))) | |
2255 | { | |
2256 | if ((mask & zerobits) == 0) | |
2257 | shift_type = unsigned_type_for (TREE_TYPE (@3)); | |
2258 | else | |
2259 | zerobits = 0; | |
2260 | } | |
2261 | } | |
2262 | } | |
2263 | /* ((X << 16) & 0xff00) is (X, 0). */ | |
2264 | (if ((mask & zerobits) == mask) | |
8fdc6c67 RB |
2265 | { build_int_cst (type, 0); } |
2266 | (with { newmask = mask | zerobits; } | |
2267 | (if (newmask != mask && (newmask & (newmask + 1)) == 0) | |
2268 | (with | |
2269 | { | |
2270 | /* Only do the transformation if NEWMASK is some integer | |
2271 | mode's mask. */ | |
2272 | for (prec = BITS_PER_UNIT; | |
2273 | prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) | |
fecfbfa4 | 2274 | if (newmask == (HOST_WIDE_INT_1U << prec) - 1) |
8fdc6c67 RB |
2275 | break; |
2276 | } | |
2277 | (if (prec < HOST_BITS_PER_WIDE_INT | |
dd4786fe | 2278 | || newmask == HOST_WIDE_INT_M1U) |
8fdc6c67 RB |
2279 | (with |
2280 | { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); } | |
2281 | (if (!tree_int_cst_equal (newmaskt, @2)) | |
2282 | (if (shift_type != TREE_TYPE (@3)) | |
2283 | (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; }) | |
2284 | (bit_and @4 { newmaskt; }))))))))))))) | |
1ffbaa3f | 2285 | |
84ff66b8 AV |
2286 | /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1) |
2287 | (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */ | |
98e30e51 | 2288 | (for shift (lshift rshift) |
84ff66b8 AV |
2289 | (for bit_op (bit_and bit_xor bit_ior) |
2290 | (simplify | |
2291 | (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1) | |
2292 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
2293 | (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); } | |
2294 | (bit_op (shift (convert @0) @1) { mask; })))))) | |
98e30e51 | 2295 | |
ad1d92ab MM |
2296 | /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */ |
2297 | (simplify | |
2298 | (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2))) | |
2299 | (if (!TYPE_UNSIGNED (TREE_TYPE (@0)) | |
ece46666 MG |
2300 | && (element_precision (TREE_TYPE (@0)) |
2301 | <= element_precision (TREE_TYPE (@1)) | |
2302 | || !TYPE_UNSIGNED (TREE_TYPE (@1)))) | |
ad1d92ab MM |
2303 | (with |
2304 | { tree shift_type = TREE_TYPE (@0); } | |
2305 | (convert (rshift (convert:shift_type @1) @2))))) | |
2306 | ||
2307 | /* ~(~X >>r Y) -> X >>r Y | |
2308 | ~(~X <<r Y) -> X <<r Y */ | |
2309 | (for rotate (lrotate rrotate) | |
2310 | (simplify | |
2311 | (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2))) | |
ece46666 MG |
2312 | (if ((element_precision (TREE_TYPE (@0)) |
2313 | <= element_precision (TREE_TYPE (@1)) | |
2314 | || !TYPE_UNSIGNED (TREE_TYPE (@1))) | |
2315 | && (element_precision (type) <= element_precision (TREE_TYPE (@0)) | |
2316 | || !TYPE_UNSIGNED (TREE_TYPE (@0)))) | |
ad1d92ab MM |
2317 | (with |
2318 | { tree rotate_type = TREE_TYPE (@0); } | |
2319 | (convert (rotate (convert:rotate_type @1) @2)))))) | |
98e30e51 | 2320 | |
d4573ffe RB |
2321 | /* Simplifications of conversions. */ |
2322 | ||
2323 | /* Basic strip-useless-type-conversions / strip_nops. */ | |
f3582e54 | 2324 | (for cvt (convert view_convert float fix_trunc) |
d4573ffe RB |
2325 | (simplify |
2326 | (cvt @0) | |
2327 | (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0))) | |
2328 | || (GENERIC && type == TREE_TYPE (@0))) | |
2329 | @0))) | |
2330 | ||
2331 | /* Contract view-conversions. */ | |
2332 | (simplify | |
2333 | (view_convert (view_convert @0)) | |
2334 | (view_convert @0)) | |
2335 | ||
2336 | /* For integral conversions with the same precision or pointer | |
2337 | conversions use a NOP_EXPR instead. */ | |
2338 | (simplify | |
2339 | (view_convert @0) | |
2340 | (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)) | |
2341 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
2342 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))) | |
2343 | (convert @0))) | |
2344 | ||
bce8ef71 MG |
2345 | /* Strip inner integral conversions that do not change precision or size, or |
2346 | zero-extend while keeping the same size (for bool-to-char). */ | |
d4573ffe RB |
2347 | (simplify |
2348 | (view_convert (convert@0 @1)) | |
2349 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
2350 | && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
bce8ef71 MG |
2351 | && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1)) |
2352 | && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)) | |
2353 | || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1)) | |
2354 | && TYPE_UNSIGNED (TREE_TYPE (@1))))) | |
d4573ffe RB |
2355 | (view_convert @1))) |
2356 | ||
2357 | /* Re-association barriers around constants and other re-association | |
2358 | barriers can be removed. */ | |
2359 | (simplify | |
2360 | (paren CONSTANT_CLASS_P@0) | |
2361 | @0) | |
2362 | (simplify | |
2363 | (paren (paren@1 @0)) | |
2364 | @1) | |
1e51d0a2 RB |
2365 | |
2366 | /* Handle cases of two conversions in a row. */ | |
2367 | (for ocvt (convert float fix_trunc) | |
2368 | (for icvt (convert float) | |
2369 | (simplify | |
2370 | (ocvt (icvt@1 @0)) | |
2371 | (with | |
2372 | { | |
2373 | tree inside_type = TREE_TYPE (@0); | |
2374 | tree inter_type = TREE_TYPE (@1); | |
2375 | int inside_int = INTEGRAL_TYPE_P (inside_type); | |
2376 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
2377 | int inside_float = FLOAT_TYPE_P (inside_type); | |
09240451 | 2378 | int inside_vec = VECTOR_TYPE_P (inside_type); |
1e51d0a2 RB |
2379 | unsigned int inside_prec = TYPE_PRECISION (inside_type); |
2380 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
2381 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
2382 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
2383 | int inter_float = FLOAT_TYPE_P (inter_type); | |
09240451 | 2384 | int inter_vec = VECTOR_TYPE_P (inter_type); |
1e51d0a2 RB |
2385 | unsigned int inter_prec = TYPE_PRECISION (inter_type); |
2386 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
2387 | int final_int = INTEGRAL_TYPE_P (type); | |
2388 | int final_ptr = POINTER_TYPE_P (type); | |
2389 | int final_float = FLOAT_TYPE_P (type); | |
09240451 | 2390 | int final_vec = VECTOR_TYPE_P (type); |
1e51d0a2 RB |
2391 | unsigned int final_prec = TYPE_PRECISION (type); |
2392 | int final_unsignedp = TYPE_UNSIGNED (type); | |
2393 | } | |
64d3a1f0 RB |
2394 | (switch |
2395 | /* In addition to the cases of two conversions in a row | |
2396 | handled below, if we are converting something to its own | |
2397 | type via an object of identical or wider precision, neither | |
2398 | conversion is needed. */ | |
2399 | (if (((GIMPLE && useless_type_conversion_p (type, inside_type)) | |
2400 | || (GENERIC | |
2401 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type))) | |
2402 | && (((inter_int || inter_ptr) && final_int) | |
2403 | || (inter_float && final_float)) | |
2404 | && inter_prec >= final_prec) | |
2405 | (ocvt @0)) | |
2406 | ||
2407 | /* Likewise, if the intermediate and initial types are either both | |
2408 | float or both integer, we don't need the middle conversion if the | |
2409 | former is wider than the latter and doesn't change the signedness | |
2410 | (for integers). Avoid this if the final type is a pointer since | |
36088299 | 2411 | then we sometimes need the middle conversion. */ |
64d3a1f0 RB |
2412 | (if (((inter_int && inside_int) || (inter_float && inside_float)) |
2413 | && (final_int || final_float) | |
2414 | && inter_prec >= inside_prec | |
36088299 | 2415 | && (inter_float || inter_unsignedp == inside_unsignedp)) |
64d3a1f0 RB |
2416 | (ocvt @0)) |
2417 | ||
2418 | /* If we have a sign-extension of a zero-extended value, we can | |
2419 | replace that by a single zero-extension. Likewise if the | |
2420 | final conversion does not change precision we can drop the | |
2421 | intermediate conversion. */ | |
2422 | (if (inside_int && inter_int && final_int | |
2423 | && ((inside_prec < inter_prec && inter_prec < final_prec | |
2424 | && inside_unsignedp && !inter_unsignedp) | |
2425 | || final_prec == inter_prec)) | |
2426 | (ocvt @0)) | |
2427 | ||
2428 | /* Two conversions in a row are not needed unless: | |
1e51d0a2 RB |
2429 | - some conversion is floating-point (overstrict for now), or |
2430 | - some conversion is a vector (overstrict for now), or | |
2431 | - the intermediate type is narrower than both initial and | |
2432 | final, or | |
2433 | - the intermediate type and innermost type differ in signedness, | |
2434 | and the outermost type is wider than the intermediate, or | |
2435 | - the initial type is a pointer type and the precisions of the | |
2436 | intermediate and final types differ, or | |
2437 | - the final type is a pointer type and the precisions of the | |
2438 | initial and intermediate types differ. */ | |
64d3a1f0 RB |
2439 | (if (! inside_float && ! inter_float && ! final_float |
2440 | && ! inside_vec && ! inter_vec && ! final_vec | |
2441 | && (inter_prec >= inside_prec || inter_prec >= final_prec) | |
2442 | && ! (inside_int && inter_int | |
2443 | && inter_unsignedp != inside_unsignedp | |
2444 | && inter_prec < final_prec) | |
2445 | && ((inter_unsignedp && inter_prec > inside_prec) | |
2446 | == (final_unsignedp && final_prec > inter_prec)) | |
2447 | && ! (inside_ptr && inter_prec != final_prec) | |
36088299 | 2448 | && ! (final_ptr && inside_prec != inter_prec)) |
64d3a1f0 RB |
2449 | (ocvt @0)) |
2450 | ||
2451 | /* A truncation to an unsigned type (a zero-extension) should be | |
2452 | canonicalized as bitwise and of a mask. */ | |
1d510e04 JJ |
2453 | (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */ |
2454 | && final_int && inter_int && inside_int | |
64d3a1f0 RB |
2455 | && final_prec == inside_prec |
2456 | && final_prec > inter_prec | |
2457 | && inter_unsignedp) | |
2458 | (convert (bit_and @0 { wide_int_to_tree | |
2459 | (inside_type, | |
2460 | wi::mask (inter_prec, false, | |
2461 | TYPE_PRECISION (inside_type))); }))) | |
2462 | ||
2463 | /* If we are converting an integer to a floating-point that can | |
2464 | represent it exactly and back to an integer, we can skip the | |
2465 | floating-point conversion. */ | |
2466 | (if (GIMPLE /* PR66211 */ | |
2467 | && inside_int && inter_float && final_int && | |
2468 | (unsigned) significand_size (TYPE_MODE (inter_type)) | |
2469 | >= inside_prec - !inside_unsignedp) | |
2470 | (convert @0))))))) | |
ea2042ba RB |
2471 | |
2472 | /* If we have a narrowing conversion to an integral type that is fed by a | |
2473 | BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely | |
2474 | masks off bits outside the final type (and nothing else). */ | |
2475 | (simplify | |
2476 | (convert (bit_and @0 INTEGER_CST@1)) | |
2477 | (if (INTEGRAL_TYPE_P (type) | |
2478 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2479 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) | |
2480 | && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1), | |
2481 | TYPE_PRECISION (type)), 0)) | |
2482 | (convert @0))) | |
a25454ea RB |
2483 | |
2484 | ||
2485 | /* (X /[ex] A) * A -> X. */ | |
2486 | (simplify | |
2eef1fc1 RB |
2487 | (mult (convert1? (exact_div @0 @@1)) (convert2? @1)) |
2488 | (convert @0)) | |
eaeba53a | 2489 | |
a7f24614 RB |
2490 | /* Canonicalization of binary operations. */ |
2491 | ||
2492 | /* Convert X + -C into X - C. */ | |
2493 | (simplify | |
2494 | (plus @0 REAL_CST@1) | |
2495 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
23f27839 | 2496 | (with { tree tem = const_unop (NEGATE_EXPR, type, @1); } |
a7f24614 RB |
2497 | (if (!TREE_OVERFLOW (tem) || !flag_trapping_math) |
2498 | (minus @0 { tem; }))))) | |
2499 | ||
6b6aa8d3 | 2500 | /* Convert x+x into x*2. */ |
a7f24614 RB |
2501 | (simplify |
2502 | (plus @0 @0) | |
2503 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
6b6aa8d3 MG |
2504 | (mult @0 { build_real (type, dconst2); }) |
2505 | (if (INTEGRAL_TYPE_P (type)) | |
2506 | (mult @0 { build_int_cst (type, 2); })))) | |
a7f24614 | 2507 | |
406520e2 | 2508 | /* 0 - X -> -X. */ |
a7f24614 RB |
2509 | (simplify |
2510 | (minus integer_zerop @1) | |
2511 | (negate @1)) | |
406520e2 MG |
2512 | (simplify |
2513 | (pointer_diff integer_zerop @1) | |
2514 | (negate (convert @1))) | |
a7f24614 RB |
2515 | |
2516 | /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether | |
2517 | ARG0 is zero and X + ARG0 reduces to X, since that would mean | |
2518 | (-ARG1 + ARG0) reduces to -ARG1. */ | |
2519 | (simplify | |
2520 | (minus real_zerop@0 @1) | |
2521 | (if (fold_real_zero_addition_p (type, @0, 0)) | |
2522 | (negate @1))) | |
2523 | ||
2524 | /* Transform x * -1 into -x. */ | |
2525 | (simplify | |
2526 | (mult @0 integer_minus_onep) | |
2527 | (negate @0)) | |
eaeba53a | 2528 | |
b771c609 AM |
2529 | /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce |
2530 | signed overflow for CST != 0 && CST != -1. */ | |
2531 | (simplify | |
2532 | (mult:c (mult:s @0 INTEGER_CST@1) @2) | |
2533 | (if (TREE_CODE (@2) != INTEGER_CST | |
2534 | && !integer_zerop (@1) && !integer_minus_onep (@1)) | |
2535 | (mult (mult @0 @2) @1))) | |
2536 | ||
96285749 RS |
2537 | /* True if we can easily extract the real and imaginary parts of a complex |
2538 | number. */ | |
2539 | (match compositional_complex | |
2540 | (convert? (complex @0 @1))) | |
2541 | ||
eaeba53a RB |
2542 | /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */ |
2543 | (simplify | |
2544 | (complex (realpart @0) (imagpart @0)) | |
2545 | @0) | |
2546 | (simplify | |
2547 | (realpart (complex @0 @1)) | |
2548 | @0) | |
2549 | (simplify | |
2550 | (imagpart (complex @0 @1)) | |
2551 | @1) | |
83633539 | 2552 | |
77c028c5 MG |
2553 | /* Sometimes we only care about half of a complex expression. */ |
2554 | (simplify | |
2555 | (realpart (convert?:s (conj:s @0))) | |
2556 | (convert (realpart @0))) | |
2557 | (simplify | |
2558 | (imagpart (convert?:s (conj:s @0))) | |
2559 | (convert (negate (imagpart @0)))) | |
2560 | (for part (realpart imagpart) | |
2561 | (for op (plus minus) | |
2562 | (simplify | |
2563 | (part (convert?:s@2 (op:s @0 @1))) | |
2564 | (convert (op (part @0) (part @1)))))) | |
2565 | (simplify | |
2566 | (realpart (convert?:s (CEXPI:s @0))) | |
2567 | (convert (COS @0))) | |
2568 | (simplify | |
2569 | (imagpart (convert?:s (CEXPI:s @0))) | |
2570 | (convert (SIN @0))) | |
2571 | ||
2572 | /* conj(conj(x)) -> x */ | |
2573 | (simplify | |
2574 | (conj (convert? (conj @0))) | |
2575 | (if (tree_nop_conversion_p (TREE_TYPE (@0), type)) | |
2576 | (convert @0))) | |
2577 | ||
2578 | /* conj({x,y}) -> {x,-y} */ | |
2579 | (simplify | |
2580 | (conj (convert?:s (complex:s @0 @1))) | |
2581 | (with { tree itype = TREE_TYPE (type); } | |
2582 | (complex (convert:itype @0) (negate (convert:itype @1))))) | |
83633539 RB |
2583 | |
2584 | /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */ | |
2585 | (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64) | |
2586 | (simplify | |
2587 | (bswap (bswap @0)) | |
2588 | @0) | |
2589 | (simplify | |
2590 | (bswap (bit_not (bswap @0))) | |
2591 | (bit_not @0)) | |
2592 | (for bitop (bit_xor bit_ior bit_and) | |
2593 | (simplify | |
2594 | (bswap (bitop:c (bswap @0) @1)) | |
2595 | (bitop @0 (bswap @1))))) | |
96994de0 RB |
2596 | |
2597 | ||
2598 | /* Combine COND_EXPRs and VEC_COND_EXPRs. */ | |
2599 | ||
2600 | /* Simplify constant conditions. | |
2601 | Only optimize constant conditions when the selected branch | |
2602 | has the same type as the COND_EXPR. This avoids optimizing | |
2603 | away "c ? x : throw", where the throw has a void type. | |
2604 | Note that we cannot throw away the fold-const.c variant nor | |
2605 | this one as we depend on doing this transform before possibly | |
2606 | A ? B : B -> B triggers and the fold-const.c one can optimize | |
2607 | 0 ? A : B to B even if A has side-effects. Something | |
2608 | genmatch cannot handle. */ | |
2609 | (simplify | |
2610 | (cond INTEGER_CST@0 @1 @2) | |
8fdc6c67 RB |
2611 | (if (integer_zerop (@0)) |
2612 | (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type)) | |
2613 | @2) | |
2614 | (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type)) | |
2615 | @1))) | |
96994de0 RB |
2616 | (simplify |
2617 | (vec_cond VECTOR_CST@0 @1 @2) | |
2618 | (if (integer_all_onesp (@0)) | |
8fdc6c67 RB |
2619 | @1 |
2620 | (if (integer_zerop (@0)) | |
2621 | @2))) | |
96994de0 | 2622 | |
b5481987 BC |
2623 | /* Simplification moved from fold_cond_expr_with_comparison. It may also |
2624 | be extended. */ | |
e2535011 BC |
2625 | /* This pattern implements two kinds simplification: |
2626 | ||
2627 | Case 1) | |
2628 | (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if: | |
b5481987 BC |
2629 | 1) Conversions are type widening from smaller type. |
2630 | 2) Const c1 equals to c2 after canonicalizing comparison. | |
2631 | 3) Comparison has tree code LT, LE, GT or GE. | |
2632 | This specific pattern is needed when (cmp (convert x) c) may not | |
2633 | be simplified by comparison patterns because of multiple uses of | |
2634 | x. It also makes sense here because simplifying across multiple | |
e2535011 BC |
2635 | referred var is always benefitial for complicated cases. |
2636 | ||
2637 | Case 2) | |
2638 | (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */ | |
2639 | (for cmp (lt le gt ge eq) | |
b5481987 | 2640 | (simplify |
ae22bc5d | 2641 | (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2) |
b5481987 BC |
2642 | (with |
2643 | { | |
2644 | tree from_type = TREE_TYPE (@1); | |
2645 | tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2); | |
ae22bc5d | 2646 | enum tree_code code = ERROR_MARK; |
b5481987 | 2647 | |
ae22bc5d BC |
2648 | if (INTEGRAL_TYPE_P (from_type) |
2649 | && int_fits_type_p (@2, from_type) | |
b5481987 BC |
2650 | && (types_match (c1_type, from_type) |
2651 | || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type) | |
2652 | && (TYPE_UNSIGNED (from_type) | |
2653 | || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type)))) | |
2654 | && (types_match (c2_type, from_type) | |
2655 | || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type) | |
2656 | && (TYPE_UNSIGNED (from_type) | |
2657 | || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type))))) | |
2658 | { | |
ae22bc5d | 2659 | if (cmp != EQ_EXPR) |
b5481987 | 2660 | { |
e2535011 BC |
2661 | if (wi::to_widest (@3) == (wi::to_widest (@2) - 1)) |
2662 | { | |
2663 | /* X <= Y - 1 equals to X < Y. */ | |
ae22bc5d | 2664 | if (cmp == LE_EXPR) |
e2535011 BC |
2665 | code = LT_EXPR; |
2666 | /* X > Y - 1 equals to X >= Y. */ | |
ae22bc5d | 2667 | if (cmp == GT_EXPR) |
e2535011 BC |
2668 | code = GE_EXPR; |
2669 | } | |
2670 | if (wi::to_widest (@3) == (wi::to_widest (@2) + 1)) | |
2671 | { | |
2672 | /* X < Y + 1 equals to X <= Y. */ | |
ae22bc5d | 2673 | if (cmp == LT_EXPR) |
e2535011 BC |
2674 | code = LE_EXPR; |
2675 | /* X >= Y + 1 equals to X > Y. */ | |
ae22bc5d | 2676 | if (cmp == GE_EXPR) |
e2535011 BC |
2677 | code = GT_EXPR; |
2678 | } | |
ae22bc5d BC |
2679 | if (code != ERROR_MARK |
2680 | || wi::to_widest (@2) == wi::to_widest (@3)) | |
e2535011 | 2681 | { |
ae22bc5d | 2682 | if (cmp == LT_EXPR || cmp == LE_EXPR) |
e2535011 | 2683 | code = MIN_EXPR; |
ae22bc5d | 2684 | if (cmp == GT_EXPR || cmp == GE_EXPR) |
e2535011 BC |
2685 | code = MAX_EXPR; |
2686 | } | |
b5481987 | 2687 | } |
e2535011 | 2688 | /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */ |
ae22bc5d BC |
2689 | else if (int_fits_type_p (@3, from_type)) |
2690 | code = EQ_EXPR; | |
b5481987 BC |
2691 | } |
2692 | } | |
2693 | (if (code == MAX_EXPR) | |
21aaaf1e | 2694 | (convert (max @1 (convert @2))) |
b5481987 | 2695 | (if (code == MIN_EXPR) |
21aaaf1e | 2696 | (convert (min @1 (convert @2))) |
e2535011 | 2697 | (if (code == EQ_EXPR) |
ae22bc5d | 2698 | (convert (cond (eq @1 (convert @3)) |
21aaaf1e | 2699 | (convert:from_type @3) (convert:from_type @2))))))))) |
b5481987 | 2700 | |
714445ae BC |
2701 | /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if: |
2702 | ||
2703 | 1) OP is PLUS or MINUS. | |
2704 | 2) CMP is LT, LE, GT or GE. | |
2705 | 3) C3 == (C1 op C2), and computation doesn't have undefined behavior. | |
2706 | ||
2707 | This pattern also handles special cases like: | |
2708 | ||
2709 | A) Operand x is a unsigned to signed type conversion and c1 is | |
2710 | integer zero. In this case, | |
2711 | (signed type)x < 0 <=> x > MAX_VAL(signed type) | |
2712 | (signed type)x >= 0 <=> x <= MAX_VAL(signed type) | |
2713 | B) Const c1 may not equal to (C3 op' C2). In this case we also | |
2714 | check equality for (c1+1) and (c1-1) by adjusting comparison | |
2715 | code. | |
2716 | ||
2717 | TODO: Though signed type is handled by this pattern, it cannot be | |
2718 | simplified at the moment because C standard requires additional | |
2719 | type promotion. In order to match&simplify it here, the IR needs | |
2720 | to be cleaned up by other optimizers, i.e, VRP. */ | |
2721 | (for op (plus minus) | |
2722 | (for cmp (lt le gt ge) | |
2723 | (simplify | |
2724 | (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3) | |
2725 | (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); } | |
2726 | (if (types_match (from_type, to_type) | |
2727 | /* Check if it is special case A). */ | |
2728 | || (TYPE_UNSIGNED (from_type) | |
2729 | && !TYPE_UNSIGNED (to_type) | |
2730 | && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type) | |
2731 | && integer_zerop (@1) | |
2732 | && (cmp == LT_EXPR || cmp == GE_EXPR))) | |
2733 | (with | |
2734 | { | |
2735 | bool overflow = false; | |
2736 | enum tree_code code, cmp_code = cmp; | |
8e6cdc90 RS |
2737 | wide_int real_c1; |
2738 | wide_int c1 = wi::to_wide (@1); | |
2739 | wide_int c2 = wi::to_wide (@2); | |
2740 | wide_int c3 = wi::to_wide (@3); | |
714445ae BC |
2741 | signop sgn = TYPE_SIGN (from_type); |
2742 | ||
2743 | /* Handle special case A), given x of unsigned type: | |
2744 | ((signed type)x < 0) <=> (x > MAX_VAL(signed type)) | |
2745 | ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */ | |
2746 | if (!types_match (from_type, to_type)) | |
2747 | { | |
2748 | if (cmp_code == LT_EXPR) | |
2749 | cmp_code = GT_EXPR; | |
2750 | if (cmp_code == GE_EXPR) | |
2751 | cmp_code = LE_EXPR; | |
2752 | c1 = wi::max_value (to_type); | |
2753 | } | |
2754 | /* To simplify this pattern, we require c3 = (c1 op c2). Here we | |
2755 | compute (c3 op' c2) and check if it equals to c1 with op' being | |
2756 | the inverted operator of op. Make sure overflow doesn't happen | |
2757 | if it is undefined. */ | |
2758 | if (op == PLUS_EXPR) | |
2759 | real_c1 = wi::sub (c3, c2, sgn, &overflow); | |
2760 | else | |
2761 | real_c1 = wi::add (c3, c2, sgn, &overflow); | |
2762 | ||
2763 | code = cmp_code; | |
2764 | if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type)) | |
2765 | { | |
2766 | /* Check if c1 equals to real_c1. Boundary condition is handled | |
2767 | by adjusting comparison operation if necessary. */ | |
2768 | if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn) | |
2769 | && !overflow) | |
2770 | { | |
2771 | /* X <= Y - 1 equals to X < Y. */ | |
2772 | if (cmp_code == LE_EXPR) | |
2773 | code = LT_EXPR; | |
2774 | /* X > Y - 1 equals to X >= Y. */ | |
2775 | if (cmp_code == GT_EXPR) | |
2776 | code = GE_EXPR; | |
2777 | } | |
2778 | if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn) | |
2779 | && !overflow) | |
2780 | { | |
2781 | /* X < Y + 1 equals to X <= Y. */ | |
2782 | if (cmp_code == LT_EXPR) | |
2783 | code = LE_EXPR; | |
2784 | /* X >= Y + 1 equals to X > Y. */ | |
2785 | if (cmp_code == GE_EXPR) | |
2786 | code = GT_EXPR; | |
2787 | } | |
2788 | if (code != cmp_code || !wi::cmp (real_c1, c1, sgn)) | |
2789 | { | |
2790 | if (cmp_code == LT_EXPR || cmp_code == LE_EXPR) | |
2791 | code = MIN_EXPR; | |
2792 | if (cmp_code == GT_EXPR || cmp_code == GE_EXPR) | |
2793 | code = MAX_EXPR; | |
2794 | } | |
2795 | } | |
2796 | } | |
2797 | (if (code == MAX_EXPR) | |
2798 | (op (max @X { wide_int_to_tree (from_type, real_c1); }) | |
2799 | { wide_int_to_tree (from_type, c2); }) | |
2800 | (if (code == MIN_EXPR) | |
2801 | (op (min @X { wide_int_to_tree (from_type, real_c1); }) | |
2802 | { wide_int_to_tree (from_type, c2); }))))))))) | |
2803 | ||
96994de0 RB |
2804 | (for cnd (cond vec_cond) |
2805 | /* A ? B : (A ? X : C) -> A ? B : C. */ | |
2806 | (simplify | |
2807 | (cnd @0 (cnd @0 @1 @2) @3) | |
2808 | (cnd @0 @1 @3)) | |
2809 | (simplify | |
2810 | (cnd @0 @1 (cnd @0 @2 @3)) | |
2811 | (cnd @0 @1 @3)) | |
24a179f8 RB |
2812 | /* A ? B : (!A ? C : X) -> A ? B : C. */ |
2813 | /* ??? This matches embedded conditions open-coded because genmatch | |
2814 | would generate matching code for conditions in separate stmts only. | |
2815 | The following is still important to merge then and else arm cases | |
2816 | from if-conversion. */ | |
2817 | (simplify | |
2818 | (cnd @0 @1 (cnd @2 @3 @4)) | |
2819 | (if (COMPARISON_CLASS_P (@0) | |
2820 | && COMPARISON_CLASS_P (@2) | |
2821 | && invert_tree_comparison | |
2822 | (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2) | |
2823 | && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0) | |
2824 | && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0)) | |
2825 | (cnd @0 @1 @3))) | |
2826 | (simplify | |
2827 | (cnd @0 (cnd @1 @2 @3) @4) | |
2828 | (if (COMPARISON_CLASS_P (@0) | |
2829 | && COMPARISON_CLASS_P (@1) | |
2830 | && invert_tree_comparison | |
2831 | (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1) | |
2832 | && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0) | |
2833 | && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0)) | |
2834 | (cnd @0 @3 @4))) | |
96994de0 RB |
2835 | |
2836 | /* A ? B : B -> B. */ | |
2837 | (simplify | |
2838 | (cnd @0 @1 @1) | |
09240451 | 2839 | @1) |
96994de0 | 2840 | |
09240451 MG |
2841 | /* !A ? B : C -> A ? C : B. */ |
2842 | (simplify | |
2843 | (cnd (logical_inverted_value truth_valued_p@0) @1 @2) | |
2844 | (cnd @0 @2 @1))) | |
f84e7fd6 | 2845 | |
a3ca1bc5 RB |
2846 | /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons |
2847 | return all -1 or all 0 results. */ | |
f43d102e RS |
2848 | /* ??? We could instead convert all instances of the vec_cond to negate, |
2849 | but that isn't necessarily a win on its own. */ | |
2850 | (simplify | |
a3ca1bc5 | 2851 | (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) |
f43d102e | 2852 | (if (VECTOR_TYPE_P (type) |
928686b1 RS |
2853 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
2854 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))) | |
f43d102e | 2855 | && (TYPE_MODE (TREE_TYPE (type)) |
4d8989d5 | 2856 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) |
a3ca1bc5 | 2857 | (minus @3 (view_convert (vec_cond @0 (negate @1) @2))))) |
f43d102e | 2858 | |
a3ca1bc5 | 2859 | /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */ |
f43d102e | 2860 | (simplify |
a3ca1bc5 | 2861 | (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) |
f43d102e | 2862 | (if (VECTOR_TYPE_P (type) |
928686b1 RS |
2863 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
2864 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))) | |
f43d102e | 2865 | && (TYPE_MODE (TREE_TYPE (type)) |
4d8989d5 | 2866 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) |
a3ca1bc5 | 2867 | (plus @3 (view_convert (vec_cond @0 (negate @1) @2))))) |
f84e7fd6 | 2868 | |
2ee05f1e | 2869 | |
f84e7fd6 RB |
2870 | /* Simplifications of comparisons. */ |
2871 | ||
24f1db9c RB |
2872 | /* See if we can reduce the magnitude of a constant involved in a |
2873 | comparison by changing the comparison code. This is a canonicalization | |
2874 | formerly done by maybe_canonicalize_comparison_1. */ | |
2875 | (for cmp (le gt) | |
2876 | acmp (lt ge) | |
2877 | (simplify | |
2878 | (cmp @0 INTEGER_CST@1) | |
2879 | (if (tree_int_cst_sgn (@1) == -1) | |
8e6cdc90 | 2880 | (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); })))) |
24f1db9c RB |
2881 | (for cmp (ge lt) |
2882 | acmp (gt le) | |
2883 | (simplify | |
2884 | (cmp @0 INTEGER_CST@1) | |
2885 | (if (tree_int_cst_sgn (@1) == 1) | |
8e6cdc90 | 2886 | (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); })))) |
24f1db9c RB |
2887 | |
2888 | ||
f84e7fd6 RB |
2889 | /* We can simplify a logical negation of a comparison to the |
2890 | inverted comparison. As we cannot compute an expression | |
2891 | operator using invert_tree_comparison we have to simulate | |
2892 | that with expression code iteration. */ | |
2893 | (for cmp (tcc_comparison) | |
2894 | icmp (inverted_tcc_comparison) | |
2895 | ncmp (inverted_tcc_comparison_with_nans) | |
2896 | /* Ideally we'd like to combine the following two patterns | |
2897 | and handle some more cases by using | |
2898 | (logical_inverted_value (cmp @0 @1)) | |
2899 | here but for that genmatch would need to "inline" that. | |
2900 | For now implement what forward_propagate_comparison did. */ | |
2901 | (simplify | |
2902 | (bit_not (cmp @0 @1)) | |
2903 | (if (VECTOR_TYPE_P (type) | |
2904 | || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)) | |
2905 | /* Comparison inversion may be impossible for trapping math, | |
2906 | invert_tree_comparison will tell us. But we can't use | |
2907 | a computed operator in the replacement tree thus we have | |
2908 | to play the trick below. */ | |
2909 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 2910 | (cmp, HONOR_NANS (@0)); } |
f84e7fd6 | 2911 | (if (ic == icmp) |
8fdc6c67 RB |
2912 | (icmp @0 @1) |
2913 | (if (ic == ncmp) | |
2914 | (ncmp @0 @1)))))) | |
f84e7fd6 | 2915 | (simplify |
09240451 MG |
2916 | (bit_xor (cmp @0 @1) integer_truep) |
2917 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 2918 | (cmp, HONOR_NANS (@0)); } |
09240451 | 2919 | (if (ic == icmp) |
8fdc6c67 RB |
2920 | (icmp @0 @1) |
2921 | (if (ic == ncmp) | |
2922 | (ncmp @0 @1)))))) | |
e18c1d66 | 2923 | |
2ee05f1e RB |
2924 | /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. |
2925 | ??? The transformation is valid for the other operators if overflow | |
2926 | is undefined for the type, but performing it here badly interacts | |
2927 | with the transformation in fold_cond_expr_with_comparison which | |
2928 | attempts to synthetize ABS_EXPR. */ | |
2929 | (for cmp (eq ne) | |
1af4ebf5 MG |
2930 | (for sub (minus pointer_diff) |
2931 | (simplify | |
2932 | (cmp (sub@2 @0 @1) integer_zerop) | |
2933 | (if (single_use (@2)) | |
2934 | (cmp @0 @1))))) | |
2ee05f1e RB |
2935 | |
2936 | /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the | |
2937 | signed arithmetic case. That form is created by the compiler | |
2938 | often enough for folding it to be of value. One example is in | |
2939 | computing loop trip counts after Operator Strength Reduction. */ | |
07cdc2b8 RB |
2940 | (for cmp (simple_comparison) |
2941 | scmp (swapped_simple_comparison) | |
2ee05f1e | 2942 | (simplify |
bc6e9db4 | 2943 | (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2) |
2ee05f1e RB |
2944 | /* Handle unfolded multiplication by zero. */ |
2945 | (if (integer_zerop (@1)) | |
8fdc6c67 RB |
2946 | (cmp @1 @2) |
2947 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
bc6e9db4 RB |
2948 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) |
2949 | && single_use (@3)) | |
8fdc6c67 RB |
2950 | /* If @1 is negative we swap the sense of the comparison. */ |
2951 | (if (tree_int_cst_sgn (@1) < 0) | |
2952 | (scmp @0 @2) | |
2953 | (cmp @0 @2)))))) | |
2ee05f1e RB |
2954 | |
2955 | /* Simplify comparison of something with itself. For IEEE | |
2956 | floating-point, we can only do some of these simplifications. */ | |
287f8f17 | 2957 | (for cmp (eq ge le) |
2ee05f1e RB |
2958 | (simplify |
2959 | (cmp @0 @0) | |
287f8f17 | 2960 | (if (! FLOAT_TYPE_P (TREE_TYPE (@0)) |
b9407883 | 2961 | || ! HONOR_NANS (@0)) |
287f8f17 RB |
2962 | { constant_boolean_node (true, type); } |
2963 | (if (cmp != EQ_EXPR) | |
2964 | (eq @0 @0))))) | |
2ee05f1e RB |
2965 | (for cmp (ne gt lt) |
2966 | (simplify | |
2967 | (cmp @0 @0) | |
2968 | (if (cmp != NE_EXPR | |
2969 | || ! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
b9407883 | 2970 | || ! HONOR_NANS (@0)) |
2ee05f1e | 2971 | { constant_boolean_node (false, type); }))) |
b5d3d787 RB |
2972 | (for cmp (unle unge uneq) |
2973 | (simplify | |
2974 | (cmp @0 @0) | |
2975 | { constant_boolean_node (true, type); })) | |
dd53d197 MG |
2976 | (for cmp (unlt ungt) |
2977 | (simplify | |
2978 | (cmp @0 @0) | |
2979 | (unordered @0 @0))) | |
b5d3d787 RB |
2980 | (simplify |
2981 | (ltgt @0 @0) | |
2982 | (if (!flag_trapping_math) | |
2983 | { constant_boolean_node (false, type); })) | |
2ee05f1e RB |
2984 | |
2985 | /* Fold ~X op ~Y as Y op X. */ | |
07cdc2b8 | 2986 | (for cmp (simple_comparison) |
2ee05f1e | 2987 | (simplify |
7fe996ba RB |
2988 | (cmp (bit_not@2 @0) (bit_not@3 @1)) |
2989 | (if (single_use (@2) && single_use (@3)) | |
2990 | (cmp @1 @0)))) | |
2ee05f1e RB |
2991 | |
2992 | /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ | |
07cdc2b8 RB |
2993 | (for cmp (simple_comparison) |
2994 | scmp (swapped_simple_comparison) | |
2ee05f1e | 2995 | (simplify |
7fe996ba RB |
2996 | (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1) |
2997 | (if (single_use (@2) | |
2998 | && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)) | |
2ee05f1e RB |
2999 | (scmp @0 (bit_not @1))))) |
3000 | ||
07cdc2b8 RB |
3001 | (for cmp (simple_comparison) |
3002 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
3003 | (simplify | |
3004 | (cmp (convert@2 @0) (convert? @1)) | |
3005 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
3006 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
3007 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) | |
3008 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
3009 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))) | |
3010 | (with | |
3011 | { | |
3012 | tree type1 = TREE_TYPE (@1); | |
3013 | if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1)) | |
3014 | { | |
3015 | REAL_VALUE_TYPE orig = TREE_REAL_CST (@1); | |
3016 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node) | |
3017 | && exact_real_truncate (TYPE_MODE (float_type_node), &orig)) | |
3018 | type1 = float_type_node; | |
3019 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node) | |
3020 | && exact_real_truncate (TYPE_MODE (double_type_node), &orig)) | |
3021 | type1 = double_type_node; | |
3022 | } | |
3023 | tree newtype | |
3024 | = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1) | |
3025 | ? TREE_TYPE (@0) : type1); | |
3026 | } | |
3027 | (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype)) | |
3028 | (cmp (convert:newtype @0) (convert:newtype @1)))))) | |
3029 | ||
3030 | (simplify | |
3031 | (cmp @0 REAL_CST@1) | |
3032 | /* IEEE doesn't distinguish +0 and -0 in comparisons. */ | |
64d3a1f0 RB |
3033 | (switch |
3034 | /* a CMP (-0) -> a CMP 0 */ | |
3035 | (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))) | |
3036 | (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })) | |
3037 | /* x != NaN is always true, other ops are always false. */ | |
3038 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
3039 | && ! HONOR_SNANS (@1)) | |
3040 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
3041 | /* Fold comparisons against infinity. */ | |
3042 | (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1)) | |
3043 | && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1)))) | |
3044 | (with | |
3045 | { | |
3046 | REAL_VALUE_TYPE max; | |
3047 | enum tree_code code = cmp; | |
3048 | bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)); | |
3049 | if (neg) | |
3050 | code = swap_tree_comparison (code); | |
3051 | } | |
3052 | (switch | |
3053 | /* x > +Inf is always false, if with ignore sNANs. */ | |
3054 | (if (code == GT_EXPR | |
3055 | && ! HONOR_SNANS (@0)) | |
3056 | { constant_boolean_node (false, type); }) | |
3057 | (if (code == LE_EXPR) | |
3058 | /* x <= +Inf is always true, if we don't case about NaNs. */ | |
3059 | (if (! HONOR_NANS (@0)) | |
3060 | { constant_boolean_node (true, type); } | |
b0eb889b | 3061 | /* x <= +Inf is the same as x == x, i.e. !isnan(x). */ |
64d3a1f0 RB |
3062 | (eq @0 @0))) |
3063 | /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */ | |
3064 | (if (code == EQ_EXPR || code == GE_EXPR) | |
3065 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
3066 | (if (neg) | |
3067 | (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
3068 | (gt @0 { build_real (TREE_TYPE (@0), max); })))) | |
3069 | /* x < +Inf is always equal to x <= DBL_MAX. */ | |
3070 | (if (code == LT_EXPR) | |
3071 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
3072 | (if (neg) | |
3073 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
3074 | (le @0 { build_real (TREE_TYPE (@0), max); })))) | |
3075 | /* x != +Inf is always equal to !(x > DBL_MAX). */ | |
3076 | (if (code == NE_EXPR) | |
3077 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
3078 | (if (! HONOR_NANS (@0)) | |
3079 | (if (neg) | |
3080 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
3081 | (le @0 { build_real (TREE_TYPE (@0), max); })) | |
3082 | (if (neg) | |
3083 | (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
3084 | { build_one_cst (type); }) | |
3085 | (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); }) | |
3086 | { build_one_cst (type); })))))))))) | |
07cdc2b8 RB |
3087 | |
3088 | /* If this is a comparison of a real constant with a PLUS_EXPR | |
3089 | or a MINUS_EXPR of a real constant, we can convert it into a | |
3090 | comparison with a revised real constant as long as no overflow | |
3091 | occurs when unsafe_math_optimizations are enabled. */ | |
3092 | (if (flag_unsafe_math_optimizations) | |
3093 | (for op (plus minus) | |
3094 | (simplify | |
3095 | (cmp (op @0 REAL_CST@1) REAL_CST@2) | |
3096 | (with | |
3097 | { | |
3098 | tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR, | |
3099 | TREE_TYPE (@1), @2, @1); | |
3100 | } | |
f980c9a2 | 3101 | (if (tem && !TREE_OVERFLOW (tem)) |
07cdc2b8 RB |
3102 | (cmp @0 { tem; })))))) |
3103 | ||
3104 | /* Likewise, we can simplify a comparison of a real constant with | |
3105 | a MINUS_EXPR whose first operand is also a real constant, i.e. | |
3106 | (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on | |
3107 | floating-point types only if -fassociative-math is set. */ | |
3108 | (if (flag_associative_math) | |
3109 | (simplify | |
0409237b | 3110 | (cmp (minus REAL_CST@0 @1) REAL_CST@2) |
07cdc2b8 | 3111 | (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); } |
f980c9a2 | 3112 | (if (tem && !TREE_OVERFLOW (tem)) |
07cdc2b8 RB |
3113 | (cmp { tem; } @1))))) |
3114 | ||
3115 | /* Fold comparisons against built-in math functions. */ | |
3116 | (if (flag_unsafe_math_optimizations | |
3117 | && ! flag_errno_math) | |
3118 | (for sq (SQRT) | |
3119 | (simplify | |
3120 | (cmp (sq @0) REAL_CST@1) | |
64d3a1f0 RB |
3121 | (switch |
3122 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
3123 | (switch | |
3124 | /* sqrt(x) < y is always false, if y is negative. */ | |
3125 | (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR) | |
8fdc6c67 | 3126 | { constant_boolean_node (false, type); }) |
64d3a1f0 RB |
3127 | /* sqrt(x) > y is always true, if y is negative and we |
3128 | don't care about NaNs, i.e. negative values of x. */ | |
3129 | (if (cmp == NE_EXPR || !HONOR_NANS (@0)) | |
3130 | { constant_boolean_node (true, type); }) | |
3131 | /* sqrt(x) > y is the same as x >= 0, if y is negative. */ | |
3132 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))) | |
c53233c6 RS |
3133 | (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0)) |
3134 | (switch | |
3135 | /* sqrt(x) < 0 is always false. */ | |
3136 | (if (cmp == LT_EXPR) | |
3137 | { constant_boolean_node (false, type); }) | |
3138 | /* sqrt(x) >= 0 is always true if we don't care about NaNs. */ | |
3139 | (if (cmp == GE_EXPR && !HONOR_NANS (@0)) | |
3140 | { constant_boolean_node (true, type); }) | |
3141 | /* sqrt(x) <= 0 -> x == 0. */ | |
3142 | (if (cmp == LE_EXPR) | |
3143 | (eq @0 @1)) | |
3144 | /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >, | |
3145 | == or !=. In the last case: | |
3146 | ||
3147 | (sqrt(x) != 0) == (NaN != 0) == true == (x != 0) | |
3148 | ||
3149 | if x is negative or NaN. Due to -funsafe-math-optimizations, | |
3150 | the results for other x follow from natural arithmetic. */ | |
3151 | (cmp @0 @1))) | |
64d3a1f0 RB |
3152 | (if (cmp == GT_EXPR || cmp == GE_EXPR) |
3153 | (with | |
3154 | { | |
3155 | REAL_VALUE_TYPE c2; | |
5c88ea94 RS |
3156 | real_arithmetic (&c2, MULT_EXPR, |
3157 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
64d3a1f0 RB |
3158 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); |
3159 | } | |
3160 | (if (REAL_VALUE_ISINF (c2)) | |
3161 | /* sqrt(x) > y is x == +Inf, when y is very large. */ | |
3162 | (if (HONOR_INFINITIES (@0)) | |
3163 | (eq @0 { build_real (TREE_TYPE (@0), c2); }) | |
3164 | { constant_boolean_node (false, type); }) | |
3165 | /* sqrt(x) > c is the same as x > c*c. */ | |
3166 | (cmp @0 { build_real (TREE_TYPE (@0), c2); })))) | |
3167 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
3168 | (with | |
3169 | { | |
3170 | REAL_VALUE_TYPE c2; | |
5c88ea94 RS |
3171 | real_arithmetic (&c2, MULT_EXPR, |
3172 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
64d3a1f0 RB |
3173 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); |
3174 | } | |
3175 | (if (REAL_VALUE_ISINF (c2)) | |
3176 | (switch | |
3177 | /* sqrt(x) < y is always true, when y is a very large | |
3178 | value and we don't care about NaNs or Infinities. */ | |
3179 | (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0)) | |
3180 | { constant_boolean_node (true, type); }) | |
3181 | /* sqrt(x) < y is x != +Inf when y is very large and we | |
3182 | don't care about NaNs. */ | |
3183 | (if (! HONOR_NANS (@0)) | |
3184 | (ne @0 { build_real (TREE_TYPE (@0), c2); })) | |
3185 | /* sqrt(x) < y is x >= 0 when y is very large and we | |
3186 | don't care about Infinities. */ | |
3187 | (if (! HONOR_INFINITIES (@0)) | |
3188 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) | |
3189 | /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ | |
3190 | (if (GENERIC) | |
3191 | (truth_andif | |
3192 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
3193 | (ne @0 { build_real (TREE_TYPE (@0), c2); })))) | |
3194 | /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ | |
3195 | (if (! HONOR_NANS (@0)) | |
3196 | (cmp @0 { build_real (TREE_TYPE (@0), c2); }) | |
3197 | /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ | |
3198 | (if (GENERIC) | |
3199 | (truth_andif | |
3200 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
0ca2e7f7 PK |
3201 | (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))) |
3202 | /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */ | |
3203 | (simplify | |
3204 | (cmp (sq @0) (sq @1)) | |
3205 | (if (! HONOR_NANS (@0)) | |
3206 | (cmp @0 @1)))))) | |
2ee05f1e | 3207 | |
c779bea5 YG |
3208 | /* Optimize various special cases of (FTYPE) N CMP CST. */ |
3209 | (for cmp (lt le eq ne ge gt) | |
3210 | icmp (le le eq ne ge ge) | |
3211 | (simplify | |
3212 | (cmp (float @0) REAL_CST@1) | |
3213 | (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1)) | |
3214 | && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))) | |
3215 | (with | |
3216 | { | |
3217 | tree itype = TREE_TYPE (@0); | |
3218 | signop isign = TYPE_SIGN (itype); | |
3219 | format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1)))); | |
3220 | const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1); | |
3221 | /* Be careful to preserve any potential exceptions due to | |
3222 | NaNs. qNaNs are ok in == or != context. | |
3223 | TODO: relax under -fno-trapping-math or | |
3224 | -fno-signaling-nans. */ | |
3225 | bool exception_p | |
3226 | = real_isnan (cst) && (cst->signalling | |
c651dca2 | 3227 | || (cmp != EQ_EXPR && cmp != NE_EXPR)); |
c779bea5 YG |
3228 | /* INT?_MIN is power-of-two so it takes |
3229 | only one mantissa bit. */ | |
3230 | bool signed_p = isign == SIGNED; | |
3231 | bool itype_fits_ftype_p | |
3232 | = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt); | |
3233 | } | |
3234 | /* TODO: allow non-fitting itype and SNaNs when | |
3235 | -fno-trapping-math. */ | |
3236 | (if (itype_fits_ftype_p && ! exception_p) | |
3237 | (with | |
3238 | { | |
3239 | REAL_VALUE_TYPE imin, imax; | |
3240 | real_from_integer (&imin, fmt, wi::min_value (itype), isign); | |
3241 | real_from_integer (&imax, fmt, wi::max_value (itype), isign); | |
3242 | ||
3243 | REAL_VALUE_TYPE icst; | |
3244 | if (cmp == GT_EXPR || cmp == GE_EXPR) | |
3245 | real_ceil (&icst, fmt, cst); | |
3246 | else if (cmp == LT_EXPR || cmp == LE_EXPR) | |
3247 | real_floor (&icst, fmt, cst); | |
3248 | else | |
3249 | real_trunc (&icst, fmt, cst); | |
3250 | ||
b09bf97b | 3251 | bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst); |
c779bea5 YG |
3252 | |
3253 | bool overflow_p = false; | |
3254 | wide_int icst_val | |
3255 | = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype)); | |
3256 | } | |
3257 | (switch | |
3258 | /* Optimize cases when CST is outside of ITYPE's range. */ | |
3259 | (if (real_compare (LT_EXPR, cst, &imin)) | |
3260 | { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR, | |
3261 | type); }) | |
3262 | (if (real_compare (GT_EXPR, cst, &imax)) | |
3263 | { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR, | |
3264 | type); }) | |
3265 | /* Remove cast if CST is an integer representable by ITYPE. */ | |
3266 | (if (cst_int_p) | |
3267 | (cmp @0 { gcc_assert (!overflow_p); | |
3268 | wide_int_to_tree (itype, icst_val); }) | |
3269 | ) | |
3270 | /* When CST is fractional, optimize | |
3271 | (FTYPE) N == CST -> 0 | |
3272 | (FTYPE) N != CST -> 1. */ | |
3273 | (if (cmp == EQ_EXPR || cmp == NE_EXPR) | |
3274 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
3275 | /* Otherwise replace with sensible integer constant. */ | |
3276 | (with | |
3277 | { | |
3278 | gcc_checking_assert (!overflow_p); | |
3279 | } | |
3280 | (icmp @0 { wide_int_to_tree (itype, icst_val); }))))))))) | |
3281 | ||
40fd269a MG |
3282 | /* Fold A /[ex] B CMP C to A CMP B * C. */ |
3283 | (for cmp (eq ne) | |
3284 | (simplify | |
3285 | (cmp (exact_div @0 @1) INTEGER_CST@2) | |
3286 | (if (!integer_zerop (@1)) | |
8e6cdc90 | 3287 | (if (wi::to_wide (@2) == 0) |
40fd269a MG |
3288 | (cmp @0 @2) |
3289 | (if (TREE_CODE (@1) == INTEGER_CST) | |
3290 | (with | |
3291 | { | |
3292 | bool ovf; | |
8e6cdc90 RS |
3293 | wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1), |
3294 | TYPE_SIGN (TREE_TYPE (@1)), &ovf); | |
40fd269a MG |
3295 | } |
3296 | (if (ovf) | |
3297 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
3298 | (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))))) | |
3299 | (for cmp (lt le gt ge) | |
3300 | (simplify | |
3301 | (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2) | |
8e6cdc90 | 3302 | (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))) |
40fd269a MG |
3303 | (with |
3304 | { | |
3305 | bool ovf; | |
8e6cdc90 RS |
3306 | wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1), |
3307 | TYPE_SIGN (TREE_TYPE (@1)), &ovf); | |
40fd269a MG |
3308 | } |
3309 | (if (ovf) | |
8e6cdc90 RS |
3310 | { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0, |
3311 | TYPE_SIGN (TREE_TYPE (@2))) | |
40fd269a MG |
3312 | != (cmp == LT_EXPR || cmp == LE_EXPR), type); } |
3313 | (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))) | |
3314 | ||
cfdc4f33 MG |
3315 | /* Unordered tests if either argument is a NaN. */ |
3316 | (simplify | |
3317 | (bit_ior (unordered @0 @0) (unordered @1 @1)) | |
aea417d7 | 3318 | (if (types_match (@0, @1)) |
cfdc4f33 | 3319 | (unordered @0 @1))) |
257b01ba MG |
3320 | (simplify |
3321 | (bit_and (ordered @0 @0) (ordered @1 @1)) | |
3322 | (if (types_match (@0, @1)) | |
3323 | (ordered @0 @1))) | |
cfdc4f33 MG |
3324 | (simplify |
3325 | (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1)) | |
3326 | @2) | |
257b01ba MG |
3327 | (simplify |
3328 | (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1)) | |
3329 | @2) | |
e18c1d66 | 3330 | |
90c6f26c RB |
3331 | /* Simple range test simplifications. */ |
3332 | /* A < B || A >= B -> true. */ | |
5d30c58d RB |
3333 | (for test1 (lt le le le ne ge) |
3334 | test2 (ge gt ge ne eq ne) | |
90c6f26c RB |
3335 | (simplify |
3336 | (bit_ior:c (test1 @0 @1) (test2 @0 @1)) | |
3337 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3338 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) | |
3339 | { constant_boolean_node (true, type); }))) | |
3340 | /* A < B && A >= B -> false. */ | |
3341 | (for test1 (lt lt lt le ne eq) | |
3342 | test2 (ge gt eq gt eq gt) | |
3343 | (simplify | |
3344 | (bit_and:c (test1 @0 @1) (test2 @0 @1)) | |
3345 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3346 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) | |
3347 | { constant_boolean_node (false, type); }))) | |
3348 | ||
9ebc3467 YG |
3349 | /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0 |
3350 | A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0 | |
3351 | ||
3352 | Note that comparisons | |
3353 | A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0 | |
3354 | A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0 | |
3355 | will be canonicalized to above so there's no need to | |
3356 | consider them here. | |
3357 | */ | |
3358 | ||
3359 | (for cmp (le gt) | |
3360 | eqcmp (eq ne) | |
3361 | (simplify | |
3362 | (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3) | |
3363 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
3364 | (with | |
3365 | { | |
3366 | tree ty = TREE_TYPE (@0); | |
3367 | unsigned prec = TYPE_PRECISION (ty); | |
3368 | wide_int mask = wi::to_wide (@2, prec); | |
3369 | wide_int rhs = wi::to_wide (@3, prec); | |
3370 | signop sgn = TYPE_SIGN (ty); | |
3371 | } | |
3372 | (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn) | |
3373 | && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn)) | |
3374 | (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); }) | |
3375 | { build_zero_cst (ty); })))))) | |
3376 | ||
534bd33b MG |
3377 | /* -A CMP -B -> B CMP A. */ |
3378 | (for cmp (tcc_comparison) | |
3379 | scmp (swapped_tcc_comparison) | |
3380 | (simplify | |
3381 | (cmp (negate @0) (negate @1)) | |
3382 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
3383 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3384 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
3385 | (scmp @0 @1))) | |
3386 | (simplify | |
3387 | (cmp (negate @0) CONSTANT_CLASS_P@1) | |
3388 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
3389 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3390 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
23f27839 | 3391 | (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); } |
534bd33b MG |
3392 | (if (tem && !TREE_OVERFLOW (tem)) |
3393 | (scmp @0 { tem; })))))) | |
3394 | ||
b0eb889b MG |
3395 | /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */ |
3396 | (for op (eq ne) | |
3397 | (simplify | |
3398 | (op (abs @0) zerop@1) | |
3399 | (op @0 @1))) | |
3400 | ||
6358a676 MG |
3401 | /* From fold_sign_changed_comparison and fold_widened_comparison. |
3402 | FIXME: the lack of symmetry is disturbing. */ | |
79d4f7c6 RB |
3403 | (for cmp (simple_comparison) |
3404 | (simplify | |
3405 | (cmp (convert@0 @00) (convert?@1 @10)) | |
452ec2a5 | 3406 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
79d4f7c6 RB |
3407 | /* Disable this optimization if we're casting a function pointer |
3408 | type on targets that require function pointer canonicalization. */ | |
3409 | && !(targetm.have_canonicalize_funcptr_for_compare () | |
3410 | && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE | |
2fde61e3 RB |
3411 | && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE) |
3412 | && single_use (@0)) | |
79d4f7c6 RB |
3413 | (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0)) |
3414 | && (TREE_CODE (@10) == INTEGER_CST | |
6358a676 | 3415 | || @1 != @10) |
79d4f7c6 RB |
3416 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0)) |
3417 | || cmp == NE_EXPR | |
3418 | || cmp == EQ_EXPR) | |
6358a676 | 3419 | && !POINTER_TYPE_P (TREE_TYPE (@00))) |
79d4f7c6 RB |
3420 | /* ??? The special-casing of INTEGER_CST conversion was in the original |
3421 | code and here to avoid a spurious overflow flag on the resulting | |
3422 | constant which fold_convert produces. */ | |
3423 | (if (TREE_CODE (@1) == INTEGER_CST) | |
3424 | (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0, | |
3425 | TREE_OVERFLOW (@1)); }) | |
3426 | (cmp @00 (convert @1))) | |
3427 | ||
3428 | (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00))) | |
3429 | /* If possible, express the comparison in the shorter mode. */ | |
3430 | (if ((cmp == EQ_EXPR || cmp == NE_EXPR | |
7fd82d52 PP |
3431 | || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)) |
3432 | || (!TYPE_UNSIGNED (TREE_TYPE (@0)) | |
3433 | && TYPE_UNSIGNED (TREE_TYPE (@00)))) | |
79d4f7c6 RB |
3434 | && (types_match (TREE_TYPE (@10), TREE_TYPE (@00)) |
3435 | || ((TYPE_PRECISION (TREE_TYPE (@00)) | |
3436 | >= TYPE_PRECISION (TREE_TYPE (@10))) | |
3437 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) | |
3438 | == TYPE_UNSIGNED (TREE_TYPE (@10)))) | |
3439 | || (TREE_CODE (@10) == INTEGER_CST | |
f6c15759 | 3440 | && INTEGRAL_TYPE_P (TREE_TYPE (@00)) |
79d4f7c6 RB |
3441 | && int_fits_type_p (@10, TREE_TYPE (@00))))) |
3442 | (cmp @00 (convert @10)) | |
3443 | (if (TREE_CODE (@10) == INTEGER_CST | |
f6c15759 | 3444 | && INTEGRAL_TYPE_P (TREE_TYPE (@00)) |
79d4f7c6 RB |
3445 | && !int_fits_type_p (@10, TREE_TYPE (@00))) |
3446 | (with | |
3447 | { | |
3448 | tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
3449 | tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
3450 | bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10)); | |
3451 | bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min)); | |
3452 | } | |
3453 | (if (above || below) | |
3454 | (if (cmp == EQ_EXPR || cmp == NE_EXPR) | |
3455 | { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); } | |
3456 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
3457 | { constant_boolean_node (above ? true : false, type); } | |
3458 | (if (cmp == GT_EXPR || cmp == GE_EXPR) | |
3459 | { constant_boolean_node (above ? false : true, type); })))))))))))) | |
66e1cacf | 3460 | |
96a111a3 RB |
3461 | (for cmp (eq ne) |
3462 | /* A local variable can never be pointed to by | |
3463 | the default SSA name of an incoming parameter. | |
3464 | SSA names are canonicalized to 2nd place. */ | |
3465 | (simplify | |
3466 | (cmp addr@0 SSA_NAME@1) | |
3467 | (if (SSA_NAME_IS_DEFAULT_DEF (@1) | |
3468 | && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL) | |
3469 | (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); } | |
3470 | (if (TREE_CODE (base) == VAR_DECL | |
3471 | && auto_var_in_fn_p (base, current_function_decl)) | |
3472 | (if (cmp == NE_EXPR) | |
3473 | { constant_boolean_node (true, type); } | |
3474 | { constant_boolean_node (false, type); })))))) | |
3475 | ||
66e1cacf RB |
3476 | /* Equality compare simplifications from fold_binary */ |
3477 | (for cmp (eq ne) | |
3478 | ||
3479 | /* If we have (A | C) == D where C & ~D != 0, convert this into 0. | |
3480 | Similarly for NE_EXPR. */ | |
3481 | (simplify | |
3482 | (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2) | |
3483 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
8e6cdc90 | 3484 | && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0) |
66e1cacf RB |
3485 | { constant_boolean_node (cmp == NE_EXPR, type); })) |
3486 | ||
3487 | /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ | |
3488 | (simplify | |
3489 | (cmp (bit_xor @0 @1) integer_zerop) | |
3490 | (cmp @0 @1)) | |
3491 | ||
3492 | /* (X ^ Y) == Y becomes X == 0. | |
3493 | Likewise (X ^ Y) == X becomes Y == 0. */ | |
3494 | (simplify | |
99e943a2 | 3495 | (cmp:c (bit_xor:c @0 @1) @0) |
66e1cacf RB |
3496 | (cmp @1 { build_zero_cst (TREE_TYPE (@1)); })) |
3497 | ||
3498 | /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ | |
3499 | (simplify | |
3500 | (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2) | |
3501 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))) | |
d057c866 | 3502 | (cmp @0 (bit_xor @1 (convert @2))))) |
d057c866 RB |
3503 | |
3504 | (simplify | |
3505 | (cmp (convert? addr@0) integer_zerop) | |
3506 | (if (tree_single_nonzero_warnv_p (@0, NULL)) | |
3507 | { constant_boolean_node (cmp == NE_EXPR, type); }))) | |
3508 | ||
b0eb889b MG |
3509 | /* If we have (A & C) == C where C is a power of 2, convert this into |
3510 | (A & C) != 0. Similarly for NE_EXPR. */ | |
3511 | (for cmp (eq ne) | |
3512 | icmp (ne eq) | |
3513 | (simplify | |
3514 | (cmp (bit_and@2 @0 integer_pow2p@1) @1) | |
3515 | (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) | |
3516 | ||
519e0faa PB |
3517 | /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2, |
3518 | convert this into a shift followed by ANDing with D. */ | |
3519 | (simplify | |
3520 | (cond | |
3521 | (ne (bit_and @0 integer_pow2p@1) integer_zerop) | |
3522 | integer_pow2p@2 integer_zerop) | |
3523 | (with { | |
8e6cdc90 RS |
3524 | int shift = (wi::exact_log2 (wi::to_wide (@2)) |
3525 | - wi::exact_log2 (wi::to_wide (@1))); | |
519e0faa PB |
3526 | } |
3527 | (if (shift > 0) | |
3528 | (bit_and | |
3529 | (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2) | |
3530 | (bit_and | |
3531 | (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2)))) | |
3532 | ||
b0eb889b MG |
3533 | /* If we have (A & C) != 0 where C is the sign bit of A, convert |
3534 | this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ | |
3535 | (for cmp (eq ne) | |
3536 | ncmp (ge lt) | |
3537 | (simplify | |
3538 | (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop) | |
3539 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2be65d9e | 3540 | && type_has_mode_precision_p (TREE_TYPE (@0)) |
b0eb889b | 3541 | && element_precision (@2) >= element_precision (@0) |
8e6cdc90 | 3542 | && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0))) |
b0eb889b MG |
3543 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } |
3544 | (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) | |
3545 | ||
519e0faa | 3546 | /* If we have A < 0 ? C : 0 where C is a power of 2, convert |
c0140e3c | 3547 | this into a right shift or sign extension followed by ANDing with C. */ |
519e0faa PB |
3548 | (simplify |
3549 | (cond | |
3550 | (lt @0 integer_zerop) | |
3551 | integer_pow2p@1 integer_zerop) | |
c0140e3c JJ |
3552 | (if (!TYPE_UNSIGNED (TREE_TYPE (@0))) |
3553 | (with { | |
8e6cdc90 | 3554 | int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1; |
c0140e3c JJ |
3555 | } |
3556 | (if (shift >= 0) | |
3557 | (bit_and | |
3558 | (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) | |
3559 | @1) | |
3560 | /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure | |
3561 | sign extension followed by AND with C will achieve the effect. */ | |
3562 | (bit_and (convert @0) @1))))) | |
519e0faa | 3563 | |
68aba1f6 RB |
3564 | /* When the addresses are not directly of decls compare base and offset. |
3565 | This implements some remaining parts of fold_comparison address | |
3566 | comparisons but still no complete part of it. Still it is good | |
3567 | enough to make fold_stmt not regress when not dispatching to fold_binary. */ | |
3568 | (for cmp (simple_comparison) | |
3569 | (simplify | |
f501d5cd | 3570 | (cmp (convert1?@2 addr@0) (convert2? addr@1)) |
68aba1f6 RB |
3571 | (with |
3572 | { | |
a90c8804 | 3573 | poly_int64 off0, off1; |
68aba1f6 RB |
3574 | tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0); |
3575 | tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1); | |
3576 | if (base0 && TREE_CODE (base0) == MEM_REF) | |
3577 | { | |
aca52e6f | 3578 | off0 += mem_ref_offset (base0).force_shwi (); |
68aba1f6 RB |
3579 | base0 = TREE_OPERAND (base0, 0); |
3580 | } | |
3581 | if (base1 && TREE_CODE (base1) == MEM_REF) | |
3582 | { | |
aca52e6f | 3583 | off1 += mem_ref_offset (base1).force_shwi (); |
68aba1f6 RB |
3584 | base1 = TREE_OPERAND (base1, 0); |
3585 | } | |
3586 | } | |
da571fda RB |
3587 | (if (base0 && base1) |
3588 | (with | |
3589 | { | |
aad88aed | 3590 | int equal = 2; |
70f40fea JJ |
3591 | /* Punt in GENERIC on variables with value expressions; |
3592 | the value expressions might point to fields/elements | |
3593 | of other vars etc. */ | |
3594 | if (GENERIC | |
3595 | && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0)) | |
3596 | || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1)))) | |
3597 | ; | |
3598 | else if (decl_in_symtab_p (base0) | |
3599 | && decl_in_symtab_p (base1)) | |
da571fda RB |
3600 | equal = symtab_node::get_create (base0) |
3601 | ->equal_address_to (symtab_node::get_create (base1)); | |
c3bea076 RB |
3602 | else if ((DECL_P (base0) |
3603 | || TREE_CODE (base0) == SSA_NAME | |
3604 | || TREE_CODE (base0) == STRING_CST) | |
3605 | && (DECL_P (base1) | |
3606 | || TREE_CODE (base1) == SSA_NAME | |
3607 | || TREE_CODE (base1) == STRING_CST)) | |
aad88aed | 3608 | equal = (base0 == base1); |
da571fda | 3609 | } |
5e19d437 | 3610 | (if (equal == 1) |
da571fda | 3611 | (switch |
a90c8804 RS |
3612 | (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1))) |
3613 | { constant_boolean_node (known_eq (off0, off1), type); }) | |
3614 | (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1))) | |
3615 | { constant_boolean_node (known_ne (off0, off1), type); }) | |
3616 | (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1))) | |
3617 | { constant_boolean_node (known_lt (off0, off1), type); }) | |
3618 | (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1))) | |
3619 | { constant_boolean_node (known_le (off0, off1), type); }) | |
3620 | (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1))) | |
3621 | { constant_boolean_node (known_ge (off0, off1), type); }) | |
3622 | (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1))) | |
3623 | { constant_boolean_node (known_gt (off0, off1), type); })) | |
da571fda RB |
3624 | (if (equal == 0 |
3625 | && DECL_P (base0) && DECL_P (base1) | |
3626 | /* If we compare this as integers require equal offset. */ | |
3627 | && (!INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
a90c8804 | 3628 | || known_eq (off0, off1))) |
da571fda RB |
3629 | (switch |
3630 | (if (cmp == EQ_EXPR) | |
3631 | { constant_boolean_node (false, type); }) | |
3632 | (if (cmp == NE_EXPR) | |
3633 | { constant_boolean_node (true, type); }))))))))) | |
66e1cacf | 3634 | |
98998245 RB |
3635 | /* Simplify pointer equality compares using PTA. */ |
3636 | (for neeq (ne eq) | |
3637 | (simplify | |
3638 | (neeq @0 @1) | |
3639 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
3640 | && ptrs_compare_unequal (@0, @1)) | |
3641 | { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; }))) | |
3642 | ||
8f63caf6 | 3643 | /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST. |
467719fb PK |
3644 | and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST. |
3645 | Disable the transform if either operand is pointer to function. | |
3646 | This broke pr22051-2.c for arm where function pointer | |
3647 | canonicalizaion is not wanted. */ | |
1c0a8806 | 3648 | |
8f63caf6 RB |
3649 | (for cmp (ne eq) |
3650 | (simplify | |
3651 | (cmp (convert @0) INTEGER_CST@1) | |
467719fb PK |
3652 | (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0))) |
3653 | && INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
3654 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1)) | |
3655 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1))))) | |
8f63caf6 RB |
3656 | (cmp @0 (convert @1))))) |
3657 | ||
21aacde4 RB |
3658 | /* Non-equality compare simplifications from fold_binary */ |
3659 | (for cmp (lt gt le ge) | |
3660 | /* Comparisons with the highest or lowest possible integer of | |
3661 | the specified precision will have known values. */ | |
3662 | (simplify | |
3663 | (cmp (convert?@2 @0) INTEGER_CST@1) | |
3664 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
3665 | && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))) | |
3666 | (with | |
3667 | { | |
3668 | tree arg1_type = TREE_TYPE (@1); | |
3669 | unsigned int prec = TYPE_PRECISION (arg1_type); | |
3670 | wide_int max = wi::max_value (arg1_type); | |
3671 | wide_int signed_max = wi::max_value (prec, SIGNED); | |
3672 | wide_int min = wi::min_value (arg1_type); | |
3673 | } | |
3674 | (switch | |
8e6cdc90 | 3675 | (if (wi::to_wide (@1) == max) |
21aacde4 RB |
3676 | (switch |
3677 | (if (cmp == GT_EXPR) | |
3678 | { constant_boolean_node (false, type); }) | |
3679 | (if (cmp == GE_EXPR) | |
3680 | (eq @2 @1)) | |
3681 | (if (cmp == LE_EXPR) | |
3682 | { constant_boolean_node (true, type); }) | |
3683 | (if (cmp == LT_EXPR) | |
3684 | (ne @2 @1)))) | |
8e6cdc90 | 3685 | (if (wi::to_wide (@1) == min) |
21aacde4 RB |
3686 | (switch |
3687 | (if (cmp == LT_EXPR) | |
3688 | { constant_boolean_node (false, type); }) | |
3689 | (if (cmp == LE_EXPR) | |
3690 | (eq @2 @1)) | |
3691 | (if (cmp == GE_EXPR) | |
3692 | { constant_boolean_node (true, type); }) | |
3693 | (if (cmp == GT_EXPR) | |
3694 | (ne @2 @1)))) | |
8e6cdc90 | 3695 | (if (wi::to_wide (@1) == max - 1) |
9bc22d19 RB |
3696 | (switch |
3697 | (if (cmp == GT_EXPR) | |
8e6cdc90 | 3698 | (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); })) |
9bc22d19 | 3699 | (if (cmp == LE_EXPR) |
8e6cdc90 RS |
3700 | (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); })))) |
3701 | (if (wi::to_wide (@1) == min + 1) | |
21aacde4 RB |
3702 | (switch |
3703 | (if (cmp == GE_EXPR) | |
8e6cdc90 | 3704 | (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); })) |
21aacde4 | 3705 | (if (cmp == LT_EXPR) |
8e6cdc90 RS |
3706 | (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); })))) |
3707 | (if (wi::to_wide (@1) == signed_max | |
21aacde4 RB |
3708 | && TYPE_UNSIGNED (arg1_type) |
3709 | /* We will flip the signedness of the comparison operator | |
3710 | associated with the mode of @1, so the sign bit is | |
3711 | specified by this mode. Check that @1 is the signed | |
3712 | max associated with this sign bit. */ | |
7a504f33 | 3713 | && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type)) |
21aacde4 RB |
3714 | /* signed_type does not work on pointer types. */ |
3715 | && INTEGRAL_TYPE_P (arg1_type)) | |
3716 | /* The following case also applies to X < signed_max+1 | |
3717 | and X >= signed_max+1 because previous transformations. */ | |
3718 | (if (cmp == LE_EXPR || cmp == GT_EXPR) | |
3719 | (with { tree st = signed_type_for (arg1_type); } | |
3720 | (if (cmp == LE_EXPR) | |
3721 | (ge (convert:st @0) { build_zero_cst (st); }) | |
3722 | (lt (convert:st @0) { build_zero_cst (st); })))))))))) | |
3723 | ||
b5d3d787 RB |
3724 | (for cmp (unordered ordered unlt unle ungt unge uneq ltgt) |
3725 | /* If the second operand is NaN, the result is constant. */ | |
3726 | (simplify | |
3727 | (cmp @0 REAL_CST@1) | |
3728 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
3729 | && (cmp != LTGT_EXPR || ! flag_trapping_math)) | |
50301115 | 3730 | { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR |
b5d3d787 | 3731 | ? false : true, type); }))) |
21aacde4 | 3732 | |
55cf3946 RB |
3733 | /* bool_var != 0 becomes bool_var. */ |
3734 | (simplify | |
b5d3d787 | 3735 | (ne @0 integer_zerop) |
55cf3946 RB |
3736 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
3737 | && types_match (type, TREE_TYPE (@0))) | |
3738 | (non_lvalue @0))) | |
3739 | /* bool_var == 1 becomes bool_var. */ | |
3740 | (simplify | |
b5d3d787 | 3741 | (eq @0 integer_onep) |
55cf3946 RB |
3742 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
3743 | && types_match (type, TREE_TYPE (@0))) | |
3744 | (non_lvalue @0))) | |
b5d3d787 RB |
3745 | /* Do not handle |
3746 | bool_var == 0 becomes !bool_var or | |
3747 | bool_var != 1 becomes !bool_var | |
3748 | here because that only is good in assignment context as long | |
3749 | as we require a tcc_comparison in GIMPLE_CONDs where we'd | |
3750 | replace if (x == 0) with tem = ~x; if (tem != 0) which is | |
3751 | clearly less optimal and which we'll transform again in forwprop. */ | |
55cf3946 | 3752 | |
ca1206be MG |
3753 | /* When one argument is a constant, overflow detection can be simplified. |
3754 | Currently restricted to single use so as not to interfere too much with | |
3755 | ADD_OVERFLOW detection in tree-ssa-math-opts.c. | |
3756 | A + CST CMP A -> A CMP' CST' */ | |
3757 | (for cmp (lt le ge gt) | |
3758 | out (gt gt le le) | |
3759 | (simplify | |
a8e9f9a3 | 3760 | (cmp:c (plus@2 @0 INTEGER_CST@1) @0) |
ca1206be MG |
3761 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
3762 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) | |
8e6cdc90 | 3763 | && wi::to_wide (@1) != 0 |
ca1206be | 3764 | && single_use (@2)) |
8e6cdc90 RS |
3765 | (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); } |
3766 | (out @0 { wide_int_to_tree (TREE_TYPE (@0), | |
3767 | wi::max_value (prec, UNSIGNED) | |
3768 | - wi::to_wide (@1)); }))))) | |
ca1206be | 3769 | |
3563f78f MG |
3770 | /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A. |
3771 | However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c | |
3772 | expects the long form, so we restrict the transformation for now. */ | |
3773 | (for cmp (gt le) | |
3774 | (simplify | |
a8e9f9a3 | 3775 | (cmp:c (minus@2 @0 @1) @0) |
3563f78f MG |
3776 | (if (single_use (@2) |
3777 | && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3778 | && TYPE_UNSIGNED (TREE_TYPE (@0)) | |
3779 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
3780 | (cmp @1 @0)))) | |
3563f78f MG |
3781 | |
3782 | /* Testing for overflow is unnecessary if we already know the result. */ | |
3563f78f MG |
3783 | /* A - B > A */ |
3784 | (for cmp (gt le) | |
3785 | out (ne eq) | |
3786 | (simplify | |
a8e9f9a3 | 3787 | (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0) |
3563f78f MG |
3788 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
3789 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
3790 | (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) | |
3791 | /* A + B < A */ | |
3792 | (for cmp (lt ge) | |
3793 | out (ne eq) | |
3794 | (simplify | |
a8e9f9a3 | 3795 | (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0) |
3563f78f MG |
3796 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
3797 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
3798 | (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) | |
3799 | ||
603aeb87 | 3800 | /* For unsigned operands, -1 / B < A checks whether A * B would overflow. |
0557293f | 3801 | Simplify it to __builtin_mul_overflow (A, B, <unused>). */ |
0557293f AM |
3802 | (for cmp (lt ge) |
3803 | out (ne eq) | |
3804 | (simplify | |
603aeb87 | 3805 | (cmp:c (trunc_div:s integer_all_onesp @1) @0) |
0557293f AM |
3806 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0))) |
3807 | (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); } | |
3808 | (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); }))))) | |
55cf3946 | 3809 | |
53f3cd25 RS |
3810 | /* Simplification of math builtins. These rules must all be optimizations |
3811 | as well as IL simplifications. If there is a possibility that the new | |
3812 | form could be a pessimization, the rule should go in the canonicalization | |
3813 | section that follows this one. | |
e18c1d66 | 3814 | |
53f3cd25 RS |
3815 | Rules can generally go in this section if they satisfy one of |
3816 | the following: | |
3817 | ||
3818 | - the rule describes an identity | |
3819 | ||
3820 | - the rule replaces calls with something as simple as addition or | |
3821 | multiplication | |
3822 | ||
3823 | - the rule contains unary calls only and simplifies the surrounding | |
3824 | arithmetic. (The idea here is to exclude non-unary calls in which | |
3825 | one operand is constant and in which the call is known to be cheap | |
3826 | when the operand has that value.) */ | |
52c6378a | 3827 | |
53f3cd25 | 3828 | (if (flag_unsafe_math_optimizations) |
52c6378a N |
3829 | /* Simplify sqrt(x) * sqrt(x) -> x. */ |
3830 | (simplify | |
c6cfa2bf | 3831 | (mult (SQRT_ALL@1 @0) @1) |
52c6378a N |
3832 | (if (!HONOR_SNANS (type)) |
3833 | @0)) | |
3834 | ||
ed17cb57 JW |
3835 | (for op (plus minus) |
3836 | /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */ | |
3837 | (simplify | |
3838 | (op (rdiv @0 @1) | |
3839 | (rdiv @2 @1)) | |
3840 | (rdiv (op @0 @2) @1))) | |
3841 | ||
35401640 N |
3842 | /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */ |
3843 | (for root (SQRT CBRT) | |
3844 | (simplify | |
3845 | (mult (root:s @0) (root:s @1)) | |
3846 | (root (mult @0 @1)))) | |
3847 | ||
35401640 N |
3848 | /* Simplify expN(x) * expN(y) -> expN(x+y). */ |
3849 | (for exps (EXP EXP2 EXP10 POW10) | |
3850 | (simplify | |
3851 | (mult (exps:s @0) (exps:s @1)) | |
3852 | (exps (plus @0 @1)))) | |
3853 | ||
52c6378a | 3854 | /* Simplify a/root(b/c) into a*root(c/b). */ |
35401640 N |
3855 | (for root (SQRT CBRT) |
3856 | (simplify | |
3857 | (rdiv @0 (root:s (rdiv:s @1 @2))) | |
3858 | (mult @0 (root (rdiv @2 @1))))) | |
3859 | ||
3860 | /* Simplify x/expN(y) into x*expN(-y). */ | |
3861 | (for exps (EXP EXP2 EXP10 POW10) | |
3862 | (simplify | |
3863 | (rdiv @0 (exps:s @1)) | |
3864 | (mult @0 (exps (negate @1))))) | |
52c6378a | 3865 | |
eee7b6c4 RB |
3866 | (for logs (LOG LOG2 LOG10 LOG10) |
3867 | exps (EXP EXP2 EXP10 POW10) | |
8acda9b2 | 3868 | /* logN(expN(x)) -> x. */ |
e18c1d66 RB |
3869 | (simplify |
3870 | (logs (exps @0)) | |
8acda9b2 RS |
3871 | @0) |
3872 | /* expN(logN(x)) -> x. */ | |
3873 | (simplify | |
3874 | (exps (logs @0)) | |
3875 | @0)) | |
53f3cd25 | 3876 | |
e18c1d66 RB |
3877 | /* Optimize logN(func()) for various exponential functions. We |
3878 | want to determine the value "x" and the power "exponent" in | |
3879 | order to transform logN(x**exponent) into exponent*logN(x). */ | |
eee7b6c4 RB |
3880 | (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10) |
3881 | exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2) | |
e18c1d66 RB |
3882 | (simplify |
3883 | (logs (exps @0)) | |
c9e926ce RS |
3884 | (if (SCALAR_FLOAT_TYPE_P (type)) |
3885 | (with { | |
3886 | tree x; | |
3887 | switch (exps) | |
3888 | { | |
3889 | CASE_CFN_EXP: | |
3890 | /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */ | |
3891 | x = build_real_truncate (type, dconst_e ()); | |
3892 | break; | |
3893 | CASE_CFN_EXP2: | |
3894 | /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */ | |
3895 | x = build_real (type, dconst2); | |
3896 | break; | |
3897 | CASE_CFN_EXP10: | |
3898 | CASE_CFN_POW10: | |
3899 | /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */ | |
3900 | { | |
3901 | REAL_VALUE_TYPE dconst10; | |
3902 | real_from_integer (&dconst10, VOIDmode, 10, SIGNED); | |
3903 | x = build_real (type, dconst10); | |
3904 | } | |
3905 | break; | |
3906 | default: | |
3907 | gcc_unreachable (); | |
3908 | } | |
3909 | } | |
3910 | (mult (logs { x; }) @0))))) | |
53f3cd25 | 3911 | |
e18c1d66 RB |
3912 | (for logs (LOG LOG |
3913 | LOG2 LOG2 | |
3914 | LOG10 LOG10) | |
3915 | exps (SQRT CBRT) | |
3916 | (simplify | |
3917 | (logs (exps @0)) | |
c9e926ce RS |
3918 | (if (SCALAR_FLOAT_TYPE_P (type)) |
3919 | (with { | |
3920 | tree x; | |
3921 | switch (exps) | |
3922 | { | |
3923 | CASE_CFN_SQRT: | |
3924 | /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */ | |
3925 | x = build_real (type, dconsthalf); | |
3926 | break; | |
3927 | CASE_CFN_CBRT: | |
3928 | /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */ | |
3929 | x = build_real_truncate (type, dconst_third ()); | |
3930 | break; | |
3931 | default: | |
3932 | gcc_unreachable (); | |
3933 | } | |
3934 | } | |
3935 | (mult { x; } (logs @0)))))) | |
53f3cd25 RS |
3936 | |
3937 | /* logN(pow(x,exponent)) -> exponent*logN(x). */ | |
e18c1d66 RB |
3938 | (for logs (LOG LOG2 LOG10) |
3939 | pows (POW) | |
3940 | (simplify | |
3941 | (logs (pows @0 @1)) | |
53f3cd25 RS |
3942 | (mult @1 (logs @0)))) |
3943 | ||
e83fe013 WD |
3944 | /* pow(C,x) -> exp(log(C)*x) if C > 0. */ |
3945 | (for pows (POW) | |
3946 | exps (EXP) | |
3947 | logs (LOG) | |
3948 | (simplify | |
3949 | (pows REAL_CST@0 @1) | |
3950 | (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0) | |
3951 | && real_isfinite (TREE_REAL_CST_PTR (@0))) | |
3952 | (exps (mult (logs @0) @1))))) | |
3953 | ||
53f3cd25 RS |
3954 | (for sqrts (SQRT) |
3955 | cbrts (CBRT) | |
b4838d77 | 3956 | pows (POW) |
53f3cd25 RS |
3957 | exps (EXP EXP2 EXP10 POW10) |
3958 | /* sqrt(expN(x)) -> expN(x*0.5). */ | |
3959 | (simplify | |
3960 | (sqrts (exps @0)) | |
3961 | (exps (mult @0 { build_real (type, dconsthalf); }))) | |
3962 | /* cbrt(expN(x)) -> expN(x/3). */ | |
3963 | (simplify | |
3964 | (cbrts (exps @0)) | |
b4838d77 RS |
3965 | (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))) |
3966 | /* pow(expN(x), y) -> expN(x*y). */ | |
3967 | (simplify | |
3968 | (pows (exps @0) @1) | |
3969 | (exps (mult @0 @1)))) | |
cfed37a0 RS |
3970 | |
3971 | /* tan(atan(x)) -> x. */ | |
3972 | (for tans (TAN) | |
3973 | atans (ATAN) | |
3974 | (simplify | |
3975 | (tans (atans @0)) | |
3976 | @0))) | |
53f3cd25 | 3977 | |
abcc43f5 RS |
3978 | /* cabs(x+0i) or cabs(0+xi) -> abs(x). */ |
3979 | (simplify | |
e04d2a35 | 3980 | (CABS (complex:C @0 real_zerop@1)) |
abcc43f5 RS |
3981 | (abs @0)) |
3982 | ||
67dbe582 | 3983 | /* trunc(trunc(x)) -> trunc(x), etc. */ |
c6cfa2bf | 3984 | (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL) |
67dbe582 RS |
3985 | (simplify |
3986 | (fns (fns @0)) | |
3987 | (fns @0))) | |
3988 | /* f(x) -> x if x is integer valued and f does nothing for such values. */ | |
c6cfa2bf | 3989 | (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL) |
67dbe582 RS |
3990 | (simplify |
3991 | (fns integer_valued_real_p@0) | |
3992 | @0)) | |
67dbe582 | 3993 | |
4d7836c4 RS |
3994 | /* hypot(x,0) and hypot(0,x) -> abs(x). */ |
3995 | (simplify | |
c9e926ce | 3996 | (HYPOT:c @0 real_zerop@1) |
4d7836c4 RS |
3997 | (abs @0)) |
3998 | ||
b4838d77 RS |
3999 | /* pow(1,x) -> 1. */ |
4000 | (simplify | |
4001 | (POW real_onep@0 @1) | |
4002 | @0) | |
4003 | ||
461e4145 RS |
4004 | (simplify |
4005 | /* copysign(x,x) -> x. */ | |
c6cfa2bf | 4006 | (COPYSIGN_ALL @0 @0) |
461e4145 RS |
4007 | @0) |
4008 | ||
4009 | (simplify | |
4010 | /* copysign(x,y) -> fabs(x) if y is nonnegative. */ | |
c6cfa2bf | 4011 | (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1) |
461e4145 RS |
4012 | (abs @0)) |
4013 | ||
86c0733f RS |
4014 | (for scale (LDEXP SCALBN SCALBLN) |
4015 | /* ldexp(0, x) -> 0. */ | |
4016 | (simplify | |
4017 | (scale real_zerop@0 @1) | |
4018 | @0) | |
4019 | /* ldexp(x, 0) -> x. */ | |
4020 | (simplify | |
4021 | (scale @0 integer_zerop@1) | |
4022 | @0) | |
4023 | /* ldexp(x, y) -> x if x is +-Inf or NaN. */ | |
4024 | (simplify | |
4025 | (scale REAL_CST@0 @1) | |
4026 | (if (!real_isfinite (TREE_REAL_CST_PTR (@0))) | |
4027 | @0))) | |
4028 | ||
53f3cd25 RS |
4029 | /* Canonicalization of sequences of math builtins. These rules represent |
4030 | IL simplifications but are not necessarily optimizations. | |
4031 | ||
4032 | The sincos pass is responsible for picking "optimal" implementations | |
4033 | of math builtins, which may be more complicated and can sometimes go | |
4034 | the other way, e.g. converting pow into a sequence of sqrts. | |
4035 | We only want to do these canonicalizations before the pass has run. */ | |
4036 | ||
4037 | (if (flag_unsafe_math_optimizations && canonicalize_math_p ()) | |
4038 | /* Simplify tan(x) * cos(x) -> sin(x). */ | |
4039 | (simplify | |
4040 | (mult:c (TAN:s @0) (COS:s @0)) | |
4041 | (SIN @0)) | |
4042 | ||
4043 | /* Simplify x * pow(x,c) -> pow(x,c+1). */ | |
4044 | (simplify | |
de3fbea3 | 4045 | (mult:c @0 (POW:s @0 REAL_CST@1)) |
53f3cd25 RS |
4046 | (if (!TREE_OVERFLOW (@1)) |
4047 | (POW @0 (plus @1 { build_one_cst (type); })))) | |
4048 | ||
4049 | /* Simplify sin(x) / cos(x) -> tan(x). */ | |
4050 | (simplify | |
4051 | (rdiv (SIN:s @0) (COS:s @0)) | |
4052 | (TAN @0)) | |
4053 | ||
4054 | /* Simplify cos(x) / sin(x) -> 1 / tan(x). */ | |
4055 | (simplify | |
4056 | (rdiv (COS:s @0) (SIN:s @0)) | |
4057 | (rdiv { build_one_cst (type); } (TAN @0))) | |
4058 | ||
4059 | /* Simplify sin(x) / tan(x) -> cos(x). */ | |
4060 | (simplify | |
4061 | (rdiv (SIN:s @0) (TAN:s @0)) | |
4062 | (if (! HONOR_NANS (@0) | |
4063 | && ! HONOR_INFINITIES (@0)) | |
c9e926ce | 4064 | (COS @0))) |
53f3cd25 RS |
4065 | |
4066 | /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */ | |
4067 | (simplify | |
4068 | (rdiv (TAN:s @0) (SIN:s @0)) | |
4069 | (if (! HONOR_NANS (@0) | |
4070 | && ! HONOR_INFINITIES (@0)) | |
4071 | (rdiv { build_one_cst (type); } (COS @0)))) | |
4072 | ||
4073 | /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */ | |
4074 | (simplify | |
4075 | (mult (POW:s @0 @1) (POW:s @0 @2)) | |
4076 | (POW @0 (plus @1 @2))) | |
4077 | ||
4078 | /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */ | |
4079 | (simplify | |
4080 | (mult (POW:s @0 @1) (POW:s @2 @1)) | |
4081 | (POW (mult @0 @2) @1)) | |
4082 | ||
de3fbea3 RB |
4083 | /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */ |
4084 | (simplify | |
4085 | (mult (POWI:s @0 @1) (POWI:s @2 @1)) | |
4086 | (POWI (mult @0 @2) @1)) | |
4087 | ||
53f3cd25 RS |
4088 | /* Simplify pow(x,c) / x -> pow(x,c-1). */ |
4089 | (simplify | |
4090 | (rdiv (POW:s @0 REAL_CST@1) @0) | |
4091 | (if (!TREE_OVERFLOW (@1)) | |
4092 | (POW @0 (minus @1 { build_one_cst (type); })))) | |
4093 | ||
4094 | /* Simplify x / pow (y,z) -> x * pow(y,-z). */ | |
4095 | (simplify | |
4096 | (rdiv @0 (POW:s @1 @2)) | |
4097 | (mult @0 (POW @1 (negate @2)))) | |
4098 | ||
4099 | (for sqrts (SQRT) | |
4100 | cbrts (CBRT) | |
4101 | pows (POW) | |
4102 | /* sqrt(sqrt(x)) -> pow(x,1/4). */ | |
4103 | (simplify | |
4104 | (sqrts (sqrts @0)) | |
4105 | (pows @0 { build_real (type, dconst_quarter ()); })) | |
4106 | /* sqrt(cbrt(x)) -> pow(x,1/6). */ | |
4107 | (simplify | |
4108 | (sqrts (cbrts @0)) | |
4109 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
4110 | /* cbrt(sqrt(x)) -> pow(x,1/6). */ | |
4111 | (simplify | |
4112 | (cbrts (sqrts @0)) | |
4113 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
4114 | /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */ | |
4115 | (simplify | |
4116 | (cbrts (cbrts tree_expr_nonnegative_p@0)) | |
4117 | (pows @0 { build_real_truncate (type, dconst_ninth ()); })) | |
4118 | /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */ | |
4119 | (simplify | |
4120 | (sqrts (pows @0 @1)) | |
4121 | (pows (abs @0) (mult @1 { build_real (type, dconsthalf); }))) | |
4122 | /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */ | |
4123 | (simplify | |
4124 | (cbrts (pows tree_expr_nonnegative_p@0 @1)) | |
b4838d77 RS |
4125 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) |
4126 | /* pow(sqrt(x),y) -> pow(x,y*0.5). */ | |
4127 | (simplify | |
4128 | (pows (sqrts @0) @1) | |
4129 | (pows @0 (mult @1 { build_real (type, dconsthalf); }))) | |
4130 | /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */ | |
4131 | (simplify | |
4132 | (pows (cbrts tree_expr_nonnegative_p@0) @1) | |
4133 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) | |
4134 | /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */ | |
4135 | (simplify | |
4136 | (pows (pows tree_expr_nonnegative_p@0 @1) @2) | |
4137 | (pows @0 (mult @1 @2)))) | |
abcc43f5 RS |
4138 | |
4139 | /* cabs(x+xi) -> fabs(x)*sqrt(2). */ | |
4140 | (simplify | |
4141 | (CABS (complex @0 @0)) | |
96285749 RS |
4142 | (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) |
4143 | ||
4d7836c4 RS |
4144 | /* hypot(x,x) -> fabs(x)*sqrt(2). */ |
4145 | (simplify | |
4146 | (HYPOT @0 @0) | |
4147 | (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) | |
4148 | ||
96285749 RS |
4149 | /* cexp(x+yi) -> exp(x)*cexpi(y). */ |
4150 | (for cexps (CEXP) | |
4151 | exps (EXP) | |
4152 | cexpis (CEXPI) | |
4153 | (simplify | |
4154 | (cexps compositional_complex@0) | |
4155 | (if (targetm.libc_has_function (function_c99_math_complex)) | |
4156 | (complex | |
4157 | (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0)))) | |
4158 | (mult @1 (imagpart @2))))))) | |
e18c1d66 | 4159 | |
67dbe582 RS |
4160 | (if (canonicalize_math_p ()) |
4161 | /* floor(x) -> trunc(x) if x is nonnegative. */ | |
c6cfa2bf MM |
4162 | (for floors (FLOOR_ALL) |
4163 | truncs (TRUNC_ALL) | |
67dbe582 RS |
4164 | (simplify |
4165 | (floors tree_expr_nonnegative_p@0) | |
4166 | (truncs @0)))) | |
4167 | ||
4168 | (match double_value_p | |
4169 | @0 | |
4170 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node))) | |
4171 | (for froms (BUILT_IN_TRUNCL | |
4172 | BUILT_IN_FLOORL | |
4173 | BUILT_IN_CEILL | |
4174 | BUILT_IN_ROUNDL | |
4175 | BUILT_IN_NEARBYINTL | |
4176 | BUILT_IN_RINTL) | |
4177 | tos (BUILT_IN_TRUNC | |
4178 | BUILT_IN_FLOOR | |
4179 | BUILT_IN_CEIL | |
4180 | BUILT_IN_ROUND | |
4181 | BUILT_IN_NEARBYINT | |
4182 | BUILT_IN_RINT) | |
4183 | /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */ | |
4184 | (if (optimize && canonicalize_math_p ()) | |
4185 | (simplify | |
4186 | (froms (convert double_value_p@0)) | |
4187 | (convert (tos @0))))) | |
4188 | ||
4189 | (match float_value_p | |
4190 | @0 | |
4191 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node))) | |
4192 | (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC | |
4193 | BUILT_IN_FLOORL BUILT_IN_FLOOR | |
4194 | BUILT_IN_CEILL BUILT_IN_CEIL | |
4195 | BUILT_IN_ROUNDL BUILT_IN_ROUND | |
4196 | BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT | |
4197 | BUILT_IN_RINTL BUILT_IN_RINT) | |
4198 | tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF | |
4199 | BUILT_IN_FLOORF BUILT_IN_FLOORF | |
4200 | BUILT_IN_CEILF BUILT_IN_CEILF | |
4201 | BUILT_IN_ROUNDF BUILT_IN_ROUNDF | |
4202 | BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF | |
4203 | BUILT_IN_RINTF BUILT_IN_RINTF) | |
4204 | /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc., | |
4205 | if x is a float. */ | |
5dac7dbd JDA |
4206 | (if (optimize && canonicalize_math_p () |
4207 | && targetm.libc_has_function (function_c99_misc)) | |
67dbe582 RS |
4208 | (simplify |
4209 | (froms (convert float_value_p@0)) | |
4210 | (convert (tos @0))))) | |
4211 | ||
543a9bcd RS |
4212 | (for froms (XFLOORL XCEILL XROUNDL XRINTL) |
4213 | tos (XFLOOR XCEIL XROUND XRINT) | |
4214 | /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */ | |
4215 | (if (optimize && canonicalize_math_p ()) | |
4216 | (simplify | |
4217 | (froms (convert double_value_p@0)) | |
4218 | (tos @0)))) | |
4219 | ||
4220 | (for froms (XFLOORL XCEILL XROUNDL XRINTL | |
4221 | XFLOOR XCEIL XROUND XRINT) | |
4222 | tos (XFLOORF XCEILF XROUNDF XRINTF) | |
4223 | /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc., | |
4224 | if x is a float. */ | |
4225 | (if (optimize && canonicalize_math_p ()) | |
4226 | (simplify | |
4227 | (froms (convert float_value_p@0)) | |
4228 | (tos @0)))) | |
4229 | ||
4230 | (if (canonicalize_math_p ()) | |
4231 | /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */ | |
4232 | (for floors (IFLOOR LFLOOR LLFLOOR) | |
4233 | (simplify | |
4234 | (floors tree_expr_nonnegative_p@0) | |
4235 | (fix_trunc @0)))) | |
4236 | ||
4237 | (if (canonicalize_math_p ()) | |
4238 | /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */ | |
4239 | (for fns (IFLOOR LFLOOR LLFLOOR | |
4240 | ICEIL LCEIL LLCEIL | |
4241 | IROUND LROUND LLROUND) | |
4242 | (simplify | |
4243 | (fns integer_valued_real_p@0) | |
4244 | (fix_trunc @0))) | |
4245 | (if (!flag_errno_math) | |
4246 | /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */ | |
4247 | (for rints (IRINT LRINT LLRINT) | |
4248 | (simplify | |
4249 | (rints integer_valued_real_p@0) | |
4250 | (fix_trunc @0))))) | |
4251 | ||
4252 | (if (canonicalize_math_p ()) | |
4253 | (for ifn (IFLOOR ICEIL IROUND IRINT) | |
4254 | lfn (LFLOOR LCEIL LROUND LRINT) | |
4255 | llfn (LLFLOOR LLCEIL LLROUND LLRINT) | |
4256 | /* Canonicalize iround (x) to lround (x) on ILP32 targets where | |
4257 | sizeof (int) == sizeof (long). */ | |
4258 | (if (TYPE_PRECISION (integer_type_node) | |
4259 | == TYPE_PRECISION (long_integer_type_node)) | |
4260 | (simplify | |
4261 | (ifn @0) | |
4262 | (lfn:long_integer_type_node @0))) | |
4263 | /* Canonicalize llround (x) to lround (x) on LP64 targets where | |
4264 | sizeof (long long) == sizeof (long). */ | |
4265 | (if (TYPE_PRECISION (long_long_integer_type_node) | |
4266 | == TYPE_PRECISION (long_integer_type_node)) | |
4267 | (simplify | |
4268 | (llfn @0) | |
4269 | (lfn:long_integer_type_node @0))))) | |
4270 | ||
92c52eab RS |
4271 | /* cproj(x) -> x if we're ignoring infinities. */ |
4272 | (simplify | |
4273 | (CPROJ @0) | |
4274 | (if (!HONOR_INFINITIES (type)) | |
4275 | @0)) | |
4276 | ||
4534c203 RB |
4277 | /* If the real part is inf and the imag part is known to be |
4278 | nonnegative, return (inf + 0i). */ | |
4279 | (simplify | |
4280 | (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1)) | |
4281 | (if (real_isinf (TREE_REAL_CST_PTR (@0))) | |
92c52eab RS |
4282 | { build_complex_inf (type, false); })) |
4283 | ||
4534c203 RB |
4284 | /* If the imag part is inf, return (inf+I*copysign(0,imag)). */ |
4285 | (simplify | |
4286 | (CPROJ (complex @0 REAL_CST@1)) | |
4287 | (if (real_isinf (TREE_REAL_CST_PTR (@1))) | |
92c52eab | 4288 | { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); })) |
4534c203 | 4289 | |
b4838d77 RS |
4290 | (for pows (POW) |
4291 | sqrts (SQRT) | |
4292 | cbrts (CBRT) | |
4293 | (simplify | |
4294 | (pows @0 REAL_CST@1) | |
4295 | (with { | |
4296 | const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1); | |
4297 | REAL_VALUE_TYPE tmp; | |
4298 | } | |
4299 | (switch | |
4300 | /* pow(x,0) -> 1. */ | |
4301 | (if (real_equal (value, &dconst0)) | |
4302 | { build_real (type, dconst1); }) | |
4303 | /* pow(x,1) -> x. */ | |
4304 | (if (real_equal (value, &dconst1)) | |
4305 | @0) | |
4306 | /* pow(x,-1) -> 1/x. */ | |
4307 | (if (real_equal (value, &dconstm1)) | |
4308 | (rdiv { build_real (type, dconst1); } @0)) | |
4309 | /* pow(x,0.5) -> sqrt(x). */ | |
4310 | (if (flag_unsafe_math_optimizations | |
4311 | && canonicalize_math_p () | |
4312 | && real_equal (value, &dconsthalf)) | |
4313 | (sqrts @0)) | |
4314 | /* pow(x,1/3) -> cbrt(x). */ | |
4315 | (if (flag_unsafe_math_optimizations | |
4316 | && canonicalize_math_p () | |
4317 | && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()), | |
4318 | real_equal (value, &tmp))) | |
4319 | (cbrts @0)))))) | |
4534c203 | 4320 | |
5ddc84ca RS |
4321 | /* powi(1,x) -> 1. */ |
4322 | (simplify | |
4323 | (POWI real_onep@0 @1) | |
4324 | @0) | |
4325 | ||
4326 | (simplify | |
4327 | (POWI @0 INTEGER_CST@1) | |
4328 | (switch | |
4329 | /* powi(x,0) -> 1. */ | |
8e6cdc90 | 4330 | (if (wi::to_wide (@1) == 0) |
5ddc84ca RS |
4331 | { build_real (type, dconst1); }) |
4332 | /* powi(x,1) -> x. */ | |
8e6cdc90 | 4333 | (if (wi::to_wide (@1) == 1) |
5ddc84ca RS |
4334 | @0) |
4335 | /* powi(x,-1) -> 1/x. */ | |
8e6cdc90 | 4336 | (if (wi::to_wide (@1) == -1) |
5ddc84ca RS |
4337 | (rdiv { build_real (type, dconst1); } @0)))) |
4338 | ||
be144838 JL |
4339 | /* Narrowing of arithmetic and logical operations. |
4340 | ||
4341 | These are conceptually similar to the transformations performed for | |
4342 | the C/C++ front-ends by shorten_binary_op and shorten_compare. Long | |
4343 | term we want to move all that code out of the front-ends into here. */ | |
4344 | ||
4345 | /* If we have a narrowing conversion of an arithmetic operation where | |
4346 | both operands are widening conversions from the same type as the outer | |
4347 | narrowing conversion. Then convert the innermost operands to a suitable | |
9c582551 | 4348 | unsigned type (to avoid introducing undefined behavior), perform the |
be144838 JL |
4349 | operation and convert the result to the desired type. */ |
4350 | (for op (plus minus) | |
4351 | (simplify | |
93f90bec | 4352 | (convert (op:s (convert@2 @0) (convert?@3 @1))) |
be144838 JL |
4353 | (if (INTEGRAL_TYPE_P (type) |
4354 | /* We check for type compatibility between @0 and @1 below, | |
4355 | so there's no need to check that @1/@3 are integral types. */ | |
4356 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
4357 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
4358 | /* The precision of the type of each operand must match the | |
4359 | precision of the mode of each operand, similarly for the | |
4360 | result. */ | |
2be65d9e RS |
4361 | && type_has_mode_precision_p (TREE_TYPE (@0)) |
4362 | && type_has_mode_precision_p (TREE_TYPE (@1)) | |
4363 | && type_has_mode_precision_p (type) | |
be144838 JL |
4364 | /* The inner conversion must be a widening conversion. */ |
4365 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
93f90bec BC |
4366 | && types_match (@0, type) |
4367 | && (types_match (@0, @1) | |
4368 | /* Or the second operand is const integer or converted const | |
4369 | integer from valueize. */ | |
4370 | || TREE_CODE (@1) == INTEGER_CST)) | |
be144838 | 4371 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) |
93f90bec | 4372 | (op @0 (convert @1)) |
8fdc6c67 | 4373 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } |
93f90bec BC |
4374 | (convert (op (convert:utype @0) |
4375 | (convert:utype @1)))))))) | |
48451e8f JL |
4376 | |
4377 | /* This is another case of narrowing, specifically when there's an outer | |
4378 | BIT_AND_EXPR which masks off bits outside the type of the innermost | |
4379 | operands. Like the previous case we have to convert the operands | |
9c582551 | 4380 | to unsigned types to avoid introducing undefined behavior for the |
48451e8f JL |
4381 | arithmetic operation. */ |
4382 | (for op (minus plus) | |
8fdc6c67 RB |
4383 | (simplify |
4384 | (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4) | |
4385 | (if (INTEGRAL_TYPE_P (type) | |
4386 | /* We check for type compatibility between @0 and @1 below, | |
4387 | so there's no need to check that @1/@3 are integral types. */ | |
4388 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
4389 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
4390 | /* The precision of the type of each operand must match the | |
4391 | precision of the mode of each operand, similarly for the | |
4392 | result. */ | |
2be65d9e RS |
4393 | && type_has_mode_precision_p (TREE_TYPE (@0)) |
4394 | && type_has_mode_precision_p (TREE_TYPE (@1)) | |
4395 | && type_has_mode_precision_p (type) | |
8fdc6c67 RB |
4396 | /* The inner conversion must be a widening conversion. */ |
4397 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
4398 | && types_match (@0, @1) | |
4399 | && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0))) | |
4400 | <= TYPE_PRECISION (TREE_TYPE (@0))) | |
8e6cdc90 RS |
4401 | && (wi::to_wide (@4) |
4402 | & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)), | |
4403 | true, TYPE_PRECISION (type))) == 0) | |
8fdc6c67 RB |
4404 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) |
4405 | (with { tree ntype = TREE_TYPE (@0); } | |
4406 | (convert (bit_and (op @0 @1) (convert:ntype @4)))) | |
4407 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
4408 | (convert (bit_and (op (convert:utype @0) (convert:utype @1)) | |
4409 | (convert:utype @4)))))))) | |
4f7a5692 MC |
4410 | |
4411 | /* Transform (@0 < @1 and @0 < @2) to use min, | |
4412 | (@0 > @1 and @0 > @2) to use max */ | |
4413 | (for op (lt le gt ge) | |
4414 | ext (min min max max) | |
4415 | (simplify | |
4618c453 RB |
4416 | (bit_and (op:cs @0 @1) (op:cs @0 @2)) |
4417 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
4418 | && TREE_CODE (@0) != INTEGER_CST) | |
4f7a5692 MC |
4419 | (op @0 (ext @1 @2))))) |
4420 | ||
7317ef4a RS |
4421 | (simplify |
4422 | /* signbit(x) -> 0 if x is nonnegative. */ | |
4423 | (SIGNBIT tree_expr_nonnegative_p@0) | |
4424 | { integer_zero_node; }) | |
4425 | ||
4426 | (simplify | |
4427 | /* signbit(x) -> x<0 if x doesn't have signed zeros. */ | |
4428 | (SIGNBIT @0) | |
4429 | (if (!HONOR_SIGNED_ZEROS (@0)) | |
4430 | (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); })))) | |
a8b85ce9 MG |
4431 | |
4432 | /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */ | |
4433 | (for cmp (eq ne) | |
4434 | (for op (plus minus) | |
4435 | rop (minus plus) | |
4436 | (simplify | |
4437 | (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) | |
4438 | (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) | |
4439 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) | |
4440 | && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0)) | |
4441 | && !TYPE_SATURATING (TREE_TYPE (@0))) | |
4442 | (with { tree res = int_const_binop (rop, @2, @1); } | |
75473a91 RB |
4443 | (if (TREE_OVERFLOW (res) |
4444 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
a8b85ce9 MG |
4445 | { constant_boolean_node (cmp == NE_EXPR, type); } |
4446 | (if (single_use (@3)) | |
11c1e63c JJ |
4447 | (cmp @0 { TREE_OVERFLOW (res) |
4448 | ? drop_tree_overflow (res) : res; })))))))) | |
a8b85ce9 MG |
4449 | (for cmp (lt le gt ge) |
4450 | (for op (plus minus) | |
4451 | rop (minus plus) | |
4452 | (simplify | |
4453 | (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) | |
4454 | (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) | |
4455 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
4456 | (with { tree res = int_const_binop (rop, @2, @1); } | |
4457 | (if (TREE_OVERFLOW (res)) | |
4458 | { | |
4459 | fold_overflow_warning (("assuming signed overflow does not occur " | |
4460 | "when simplifying conditional to constant"), | |
4461 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
4462 | bool less = cmp == LE_EXPR || cmp == LT_EXPR; | |
4463 | /* wi::ges_p (@2, 0) should be sufficient for a signed type. */ | |
8e6cdc90 RS |
4464 | bool ovf_high = wi::lt_p (wi::to_wide (@1), 0, |
4465 | TYPE_SIGN (TREE_TYPE (@1))) | |
a8b85ce9 MG |
4466 | != (op == MINUS_EXPR); |
4467 | constant_boolean_node (less == ovf_high, type); | |
4468 | } | |
4469 | (if (single_use (@3)) | |
4470 | (with | |
4471 | { | |
4472 | fold_overflow_warning (("assuming signed overflow does not occur " | |
4473 | "when changing X +- C1 cmp C2 to " | |
4474 | "X cmp C2 -+ C1"), | |
4475 | WARN_STRICT_OVERFLOW_COMPARISON); | |
4476 | } | |
4477 | (cmp @0 { res; }))))))))) | |
d3e40b76 RB |
4478 | |
4479 | /* Canonicalizations of BIT_FIELD_REFs. */ | |
4480 | ||
4481 | (simplify | |
4482 | (BIT_FIELD_REF @0 @1 @2) | |
4483 | (switch | |
4484 | (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE | |
4485 | && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
4486 | (switch | |
4487 | (if (integer_zerop (@2)) | |
4488 | (view_convert (realpart @0))) | |
4489 | (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
4490 | (view_convert (imagpart @0))))) | |
4491 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
4492 | && INTEGRAL_TYPE_P (type) | |
171f6f05 RB |
4493 | /* On GIMPLE this should only apply to register arguments. */ |
4494 | && (! GIMPLE || is_gimple_reg (@0)) | |
d3e40b76 RB |
4495 | /* A bit-field-ref that referenced the full argument can be stripped. */ |
4496 | && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0 | |
4497 | && integer_zerop (@2)) | |
4498 | /* Low-parts can be reduced to integral conversions. | |
4499 | ??? The following doesn't work for PDP endian. */ | |
4500 | || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN | |
4501 | /* Don't even think about BITS_BIG_ENDIAN. */ | |
4502 | && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0 | |
4503 | && TYPE_PRECISION (type) % BITS_PER_UNIT == 0 | |
4504 | && compare_tree_int (@2, (BYTES_BIG_ENDIAN | |
4505 | ? (TYPE_PRECISION (TREE_TYPE (@0)) | |
4506 | - TYPE_PRECISION (type)) | |
4507 | : 0)) == 0))) | |
4508 | (convert @0)))) | |
4509 | ||
4510 | /* Simplify vector extracts. */ | |
4511 | ||
4512 | (simplify | |
4513 | (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2) | |
4514 | (if (VECTOR_TYPE_P (TREE_TYPE (@0)) | |
4515 | && (types_match (type, TREE_TYPE (TREE_TYPE (@0))) | |
4516 | || (VECTOR_TYPE_P (type) | |
4517 | && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) | |
4518 | (with | |
4519 | { | |
4520 | tree ctor = (TREE_CODE (@0) == SSA_NAME | |
4521 | ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); | |
4522 | tree eltype = TREE_TYPE (TREE_TYPE (ctor)); | |
4523 | unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype)); | |
4524 | unsigned HOST_WIDE_INT n = tree_to_uhwi (@1); | |
4525 | unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2); | |
4526 | } | |
4527 | (if (n != 0 | |
4528 | && (idx % width) == 0 | |
4529 | && (n % width) == 0 | |
928686b1 RS |
4530 | && known_le ((idx + n) / width, |
4531 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))) | |
d3e40b76 RB |
4532 | (with |
4533 | { | |
4534 | idx = idx / width; | |
4535 | n = n / width; | |
4536 | /* Constructor elements can be subvectors. */ | |
d34457c1 | 4537 | poly_uint64 k = 1; |
d3e40b76 RB |
4538 | if (CONSTRUCTOR_NELTS (ctor) != 0) |
4539 | { | |
4540 | tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value); | |
4541 | if (TREE_CODE (cons_elem) == VECTOR_TYPE) | |
4542 | k = TYPE_VECTOR_SUBPARTS (cons_elem); | |
4543 | } | |
d34457c1 | 4544 | unsigned HOST_WIDE_INT elt, count, const_k; |
d3e40b76 RB |
4545 | } |
4546 | (switch | |
4547 | /* We keep an exact subset of the constructor elements. */ | |
d34457c1 | 4548 | (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count)) |
d3e40b76 RB |
4549 | (if (CONSTRUCTOR_NELTS (ctor) == 0) |
4550 | { build_constructor (type, NULL); } | |
d34457c1 RS |
4551 | (if (count == 1) |
4552 | (if (elt < CONSTRUCTOR_NELTS (ctor)) | |
4553 | { CONSTRUCTOR_ELT (ctor, elt)->value; } | |
4554 | { build_zero_cst (type); }) | |
d3e40b76 | 4555 | { |
d34457c1 RS |
4556 | vec<constructor_elt, va_gc> *vals; |
4557 | vec_alloc (vals, count); | |
4558 | for (unsigned i = 0; | |
4559 | i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i) | |
4560 | CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, | |
4561 | CONSTRUCTOR_ELT (ctor, elt + i)->value); | |
4562 | build_constructor (type, vals); | |
4563 | }))) | |
d3e40b76 | 4564 | /* The bitfield references a single constructor element. */ |
d34457c1 RS |
4565 | (if (k.is_constant (&const_k) |
4566 | && idx + n <= (idx / const_k + 1) * const_k) | |
d3e40b76 | 4567 | (switch |
d34457c1 | 4568 | (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k) |
d3e40b76 | 4569 | { build_zero_cst (type); }) |
d34457c1 RS |
4570 | (if (n == const_k) |
4571 | { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }) | |
4572 | (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; } | |
4573 | @1 { bitsize_int ((idx % const_k) * width); }))))))))) | |
92e29a5e RB |
4574 | |
4575 | /* Simplify a bit extraction from a bit insertion for the cases with | |
4576 | the inserted element fully covering the extraction or the insertion | |
4577 | not touching the extraction. */ | |
4578 | (simplify | |
4579 | (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos) | |
4580 | (with | |
4581 | { | |
4582 | unsigned HOST_WIDE_INT isize; | |
4583 | if (INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
4584 | isize = TYPE_PRECISION (TREE_TYPE (@1)); | |
4585 | else | |
4586 | isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1))); | |
4587 | } | |
4588 | (switch | |
8e6cdc90 RS |
4589 | (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos)) |
4590 | && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize), | |
4591 | wi::to_wide (@ipos) + isize)) | |
92e29a5e | 4592 | (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype, |
8e6cdc90 RS |
4593 | wi::to_wide (@rpos) |
4594 | - wi::to_wide (@ipos)); })) | |
4595 | (if (wi::geu_p (wi::to_wide (@ipos), | |
4596 | wi::to_wide (@rpos) + wi::to_wide (@rsize)) | |
4597 | || wi::geu_p (wi::to_wide (@rpos), | |
4598 | wi::to_wide (@ipos) + isize)) | |
92e29a5e | 4599 | (BIT_FIELD_REF @0 @rsize @rpos))))) |