]>
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 | ||
818ab71a | 5 | Copyright (C) 2014-2016 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 |
67dbe582 | 34 | integer_valued_real_p |
53a19317 RB |
35 | integer_pow2p |
36 | HONOR_NANS) | |
e0ee10ed | 37 | |
f84e7fd6 RB |
38 | /* Operator lists. */ |
39 | (define_operator_list tcc_comparison | |
40 | lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) | |
41 | (define_operator_list inverted_tcc_comparison | |
42 | ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq) | |
43 | (define_operator_list inverted_tcc_comparison_with_nans | |
44 | unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq) | |
534bd33b MG |
45 | (define_operator_list swapped_tcc_comparison |
46 | gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt) | |
07cdc2b8 RB |
47 | (define_operator_list simple_comparison lt le eq ne ge gt) |
48 | (define_operator_list swapped_simple_comparison gt ge eq ne le lt) | |
49 | ||
b1dc4a20 | 50 | #include "cfn-operators.pd" |
257aecb4 | 51 | |
543a9bcd RS |
52 | /* Define operand lists for math rounding functions {,i,l,ll}FN, |
53 | where the versions prefixed with "i" return an int, those prefixed with | |
54 | "l" return a long and those prefixed with "ll" return a long long. | |
55 | ||
56 | Also define operand lists: | |
57 | ||
58 | X<FN>F for all float functions, in the order i, l, ll | |
59 | X<FN> for all double functions, in the same order | |
60 | X<FN>L for all long double functions, in the same order. */ | |
61 | #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \ | |
543a9bcd RS |
62 | (define_operator_list X##FN##F BUILT_IN_I##FN##F \ |
63 | BUILT_IN_L##FN##F \ | |
64 | BUILT_IN_LL##FN##F) \ | |
65 | (define_operator_list X##FN BUILT_IN_I##FN \ | |
66 | BUILT_IN_L##FN \ | |
67 | BUILT_IN_LL##FN) \ | |
68 | (define_operator_list X##FN##L BUILT_IN_I##FN##L \ | |
69 | BUILT_IN_L##FN##L \ | |
70 | BUILT_IN_LL##FN##L) | |
71 | ||
543a9bcd RS |
72 | DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR) |
73 | DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL) | |
74 | DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND) | |
75 | DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) | |
f84e7fd6 | 76 | |
e0ee10ed | 77 | /* Simplifications of operations with one constant operand and |
36a60e48 | 78 | simplifications to constants or single values. */ |
e0ee10ed RB |
79 | |
80 | (for op (plus pointer_plus minus bit_ior bit_xor) | |
81 | (simplify | |
82 | (op @0 integer_zerop) | |
83 | (non_lvalue @0))) | |
84 | ||
a499aac5 RB |
85 | /* 0 +p index -> (type)index */ |
86 | (simplify | |
87 | (pointer_plus integer_zerop @1) | |
88 | (non_lvalue (convert @1))) | |
89 | ||
a7f24614 RB |
90 | /* See if ARG1 is zero and X + ARG1 reduces to X. |
91 | Likewise if the operands are reversed. */ | |
92 | (simplify | |
93 | (plus:c @0 real_zerop@1) | |
94 | (if (fold_real_zero_addition_p (type, @1, 0)) | |
95 | (non_lvalue @0))) | |
96 | ||
97 | /* See if ARG1 is zero and X - ARG1 reduces to X. */ | |
98 | (simplify | |
99 | (minus @0 real_zerop@1) | |
100 | (if (fold_real_zero_addition_p (type, @1, 1)) | |
101 | (non_lvalue @0))) | |
102 | ||
e0ee10ed RB |
103 | /* Simplify x - x. |
104 | This is unsafe for certain floats even in non-IEEE formats. | |
105 | In IEEE, it is unsafe because it does wrong for NaNs. | |
106 | Also note that operand_equal_p is always false if an operand | |
107 | is volatile. */ | |
108 | (simplify | |
a7f24614 | 109 | (minus @0 @0) |
1b457aa4 | 110 | (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type)) |
a7f24614 | 111 | { build_zero_cst (type); })) |
e0ee10ed RB |
112 | |
113 | (simplify | |
a7f24614 RB |
114 | (mult @0 integer_zerop@1) |
115 | @1) | |
116 | ||
117 | /* Maybe fold x * 0 to 0. The expressions aren't the same | |
118 | when x is NaN, since x * 0 is also NaN. Nor are they the | |
119 | same in modes with signed zeros, since multiplying a | |
120 | negative value by 0 gives -0, not +0. */ | |
121 | (simplify | |
122 | (mult @0 real_zerop@1) | |
8b5ee871 | 123 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) |
a7f24614 RB |
124 | @1)) |
125 | ||
126 | /* In IEEE floating point, x*1 is not equivalent to x for snans. | |
127 | Likewise for complex arithmetic with signed zeros. */ | |
128 | (simplify | |
129 | (mult @0 real_onep) | |
8b5ee871 MG |
130 | (if (!HONOR_SNANS (type) |
131 | && (!HONOR_SIGNED_ZEROS (type) | |
a7f24614 RB |
132 | || !COMPLEX_FLOAT_TYPE_P (type))) |
133 | (non_lvalue @0))) | |
134 | ||
135 | /* Transform x * -1.0 into -x. */ | |
136 | (simplify | |
137 | (mult @0 real_minus_onep) | |
8b5ee871 MG |
138 | (if (!HONOR_SNANS (type) |
139 | && (!HONOR_SIGNED_ZEROS (type) | |
a7f24614 RB |
140 | || !COMPLEX_FLOAT_TYPE_P (type))) |
141 | (negate @0))) | |
e0ee10ed RB |
142 | |
143 | /* Make sure to preserve divisions by zero. This is the reason why | |
144 | we don't simplify x / x to 1 or 0 / x to 0. */ | |
145 | (for op (mult trunc_div ceil_div floor_div round_div exact_div) | |
146 | (simplify | |
147 | (op @0 integer_onep) | |
148 | (non_lvalue @0))) | |
149 | ||
a7f24614 RB |
150 | /* X / -1 is -X. */ |
151 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
152 | (simplify | |
09240451 MG |
153 | (div @0 integer_minus_onep@1) |
154 | (if (!TYPE_UNSIGNED (type)) | |
a7f24614 RB |
155 | (negate @0)))) |
156 | ||
157 | /* For unsigned integral types, FLOOR_DIV_EXPR is the same as | |
158 | TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ | |
159 | (simplify | |
160 | (floor_div @0 @1) | |
09240451 MG |
161 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
162 | && TYPE_UNSIGNED (type)) | |
a7f24614 RB |
163 | (trunc_div @0 @1))) |
164 | ||
28093105 RB |
165 | /* Combine two successive divisions. Note that combining ceil_div |
166 | and floor_div is trickier and combining round_div even more so. */ | |
167 | (for div (trunc_div exact_div) | |
c306cfaf RB |
168 | (simplify |
169 | (div (div @0 INTEGER_CST@1) INTEGER_CST@2) | |
170 | (with { | |
171 | bool overflow_p; | |
172 | wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p); | |
173 | } | |
174 | (if (!overflow_p) | |
8fdc6c67 RB |
175 | (div @0 { wide_int_to_tree (type, mul); }) |
176 | (if (TYPE_UNSIGNED (type) | |
177 | || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)) | |
178 | { build_zero_cst (type); }))))) | |
c306cfaf | 179 | |
a7f24614 | 180 | /* Optimize A / A to 1.0 if we don't care about |
09240451 | 181 | NaNs or Infinities. */ |
a7f24614 RB |
182 | (simplify |
183 | (rdiv @0 @0) | |
09240451 | 184 | (if (FLOAT_TYPE_P (type) |
1b457aa4 | 185 | && ! HONOR_NANS (type) |
8b5ee871 | 186 | && ! HONOR_INFINITIES (type)) |
09240451 MG |
187 | { build_one_cst (type); })) |
188 | ||
189 | /* Optimize -A / A to -1.0 if we don't care about | |
190 | NaNs or Infinities. */ | |
191 | (simplify | |
e04d2a35 | 192 | (rdiv:C @0 (negate @0)) |
09240451 | 193 | (if (FLOAT_TYPE_P (type) |
1b457aa4 | 194 | && ! HONOR_NANS (type) |
8b5ee871 | 195 | && ! HONOR_INFINITIES (type)) |
09240451 | 196 | { build_minus_one_cst (type); })) |
a7f24614 | 197 | |
8c6961ca PK |
198 | /* PR71078: x / abs(x) -> copysign (1.0, x) */ |
199 | (simplify | |
200 | (rdiv:C (convert? @0) (convert? (abs @0))) | |
201 | (if (SCALAR_FLOAT_TYPE_P (type) | |
202 | && ! HONOR_NANS (type) | |
203 | && ! HONOR_INFINITIES (type)) | |
204 | (switch | |
205 | (if (types_match (type, float_type_node)) | |
206 | (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0))) | |
207 | (if (types_match (type, double_type_node)) | |
208 | (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0))) | |
209 | (if (types_match (type, long_double_type_node)) | |
210 | (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0)))))) | |
211 | ||
a7f24614 RB |
212 | /* In IEEE floating point, x/1 is not equivalent to x for snans. */ |
213 | (simplify | |
214 | (rdiv @0 real_onep) | |
8b5ee871 | 215 | (if (!HONOR_SNANS (type)) |
a7f24614 RB |
216 | (non_lvalue @0))) |
217 | ||
218 | /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ | |
219 | (simplify | |
220 | (rdiv @0 real_minus_onep) | |
8b5ee871 | 221 | (if (!HONOR_SNANS (type)) |
a7f24614 RB |
222 | (negate @0))) |
223 | ||
5711ac88 N |
224 | (if (flag_reciprocal_math) |
225 | /* Convert (A/B)/C to A/(B*C) */ | |
226 | (simplify | |
227 | (rdiv (rdiv:s @0 @1) @2) | |
228 | (rdiv @0 (mult @1 @2))) | |
229 | ||
230 | /* Convert A/(B/C) to (A/B)*C */ | |
231 | (simplify | |
232 | (rdiv @0 (rdiv:s @1 @2)) | |
233 | (mult (rdiv @0 @1) @2))) | |
234 | ||
235 | /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */ | |
236 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
237 | (simplify | |
238 | (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2) | |
239 | (if (integer_pow2p (@2) | |
240 | && tree_int_cst_sgn (@2) > 0 | |
241 | && wi::add (@2, @1) == 0 | |
242 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
243 | (rshift (convert @0) { build_int_cst (integer_type_node, | |
244 | wi::exact_log2 (@2)); })))) | |
245 | ||
a7f24614 RB |
246 | /* If ARG1 is a constant, we can convert this to a multiply by the |
247 | reciprocal. This does not have the same rounding properties, | |
248 | so only do this if -freciprocal-math. We can actually | |
249 | always safely do it if ARG1 is a power of two, but it's hard to | |
250 | tell if it is or not in a portable manner. */ | |
251 | (for cst (REAL_CST COMPLEX_CST VECTOR_CST) | |
252 | (simplify | |
253 | (rdiv @0 cst@1) | |
254 | (if (optimize) | |
53bc4b3a RB |
255 | (if (flag_reciprocal_math |
256 | && !real_zerop (@1)) | |
a7f24614 | 257 | (with |
249700b5 | 258 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); } |
a7f24614 | 259 | (if (tem) |
8fdc6c67 RB |
260 | (mult @0 { tem; } ))) |
261 | (if (cst != COMPLEX_CST) | |
262 | (with { tree inverse = exact_inverse (type, @1); } | |
263 | (if (inverse) | |
264 | (mult @0 { inverse; } )))))))) | |
a7f24614 | 265 | |
e0ee10ed RB |
266 | /* Same applies to modulo operations, but fold is inconsistent here |
267 | and simplifies 0 % x to 0, only preserving literal 0 % 0. */ | |
a7f24614 | 268 | (for mod (ceil_mod floor_mod round_mod trunc_mod) |
e0ee10ed RB |
269 | /* 0 % X is always zero. */ |
270 | (simplify | |
a7f24614 | 271 | (mod integer_zerop@0 @1) |
e0ee10ed RB |
272 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ |
273 | (if (!integer_zerop (@1)) | |
274 | @0)) | |
275 | /* X % 1 is always zero. */ | |
276 | (simplify | |
a7f24614 RB |
277 | (mod @0 integer_onep) |
278 | { build_zero_cst (type); }) | |
279 | /* X % -1 is zero. */ | |
280 | (simplify | |
09240451 MG |
281 | (mod @0 integer_minus_onep@1) |
282 | (if (!TYPE_UNSIGNED (type)) | |
bc4315fb MG |
283 | { build_zero_cst (type); })) |
284 | /* (X % Y) % Y is just X % Y. */ | |
285 | (simplify | |
286 | (mod (mod@2 @0 @1) @1) | |
98e30e51 RB |
287 | @2) |
288 | /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */ | |
289 | (simplify | |
290 | (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
291 | (if (ANY_INTEGRAL_TYPE_P (type) | |
292 | && TYPE_OVERFLOW_UNDEFINED (type) | |
293 | && wi::multiple_of_p (@1, @2, TYPE_SIGN (type))) | |
294 | { build_zero_cst (type); }))) | |
a7f24614 RB |
295 | |
296 | /* X % -C is the same as X % C. */ | |
297 | (simplify | |
298 | (trunc_mod @0 INTEGER_CST@1) | |
299 | (if (TYPE_SIGN (type) == SIGNED | |
300 | && !TREE_OVERFLOW (@1) | |
301 | && wi::neg_p (@1) | |
302 | && !TYPE_OVERFLOW_TRAPS (type) | |
303 | /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ | |
304 | && !sign_bit_p (@1, @1)) | |
305 | (trunc_mod @0 (negate @1)))) | |
e0ee10ed | 306 | |
8f0c696a RB |
307 | /* X % -Y is the same as X % Y. */ |
308 | (simplify | |
309 | (trunc_mod @0 (convert? (negate @1))) | |
a2a743a1 MP |
310 | (if (INTEGRAL_TYPE_P (type) |
311 | && !TYPE_UNSIGNED (type) | |
8f0c696a | 312 | && !TYPE_OVERFLOW_TRAPS (type) |
20b8d734 JJ |
313 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) |
314 | /* Avoid this transformation if X might be INT_MIN or | |
315 | Y might be -1, because we would then change valid | |
316 | INT_MIN % -(-1) into invalid INT_MIN % -1. */ | |
317 | && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type)) | |
318 | || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION | |
319 | (TREE_TYPE (@1)))))) | |
8f0c696a RB |
320 | (trunc_mod @0 (convert @1)))) |
321 | ||
f461569a MP |
322 | /* X - (X / Y) * Y is the same as X % Y. */ |
323 | (simplify | |
fba46f03 MG |
324 | (minus (convert1? @2) (convert2? (mult:c (trunc_div @0 @1) @1))) |
325 | /* We cannot use matching captures here, since in the case of | |
326 | constants we really want the type of @0, not @2. */ | |
327 | (if (operand_equal_p (@0, @2, 0) | |
328 | && (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))) | |
329 | (convert (trunc_mod @0 @1)))) | |
f461569a | 330 | |
8f0c696a RB |
331 | /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, |
332 | i.e. "X % C" into "X & (C - 1)", if X and C are positive. | |
333 | Also optimize A % (C << N) where C is a power of 2, | |
334 | to A & ((C << N) - 1). */ | |
335 | (match (power_of_two_cand @1) | |
336 | INTEGER_CST@1) | |
337 | (match (power_of_two_cand @1) | |
338 | (lshift INTEGER_CST@1 @2)) | |
339 | (for mod (trunc_mod floor_mod) | |
340 | (simplify | |
4ab1e111 | 341 | (mod @0 (convert?@3 (power_of_two_cand@1 @2))) |
8f0c696a RB |
342 | (if ((TYPE_UNSIGNED (type) |
343 | || tree_expr_nonnegative_p (@0)) | |
4ab1e111 | 344 | && tree_nop_conversion_p (type, TREE_TYPE (@3)) |
8f0c696a | 345 | && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) |
4ab1e111 | 346 | (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))))) |
8f0c696a | 347 | |
887ab609 N |
348 | /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ |
349 | (simplify | |
350 | (trunc_div (mult @0 integer_pow2p@1) @1) | |
351 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
352 | (bit_and @0 { wide_int_to_tree | |
353 | (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1), | |
354 | false, TYPE_PRECISION (type))); }))) | |
355 | ||
5f8d832e N |
356 | /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */ |
357 | (simplify | |
358 | (mult (trunc_div @0 integer_pow2p@1) @1) | |
359 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
360 | (bit_and @0 (negate @1)))) | |
361 | ||
95765f36 N |
362 | /* Simplify (t * 2) / 2) -> t. */ |
363 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
364 | (simplify | |
365 | (div (mult @0 @1) @1) | |
366 | (if (ANY_INTEGRAL_TYPE_P (type) | |
367 | && TYPE_OVERFLOW_UNDEFINED (type)) | |
368 | @0))) | |
369 | ||
d202f9bd | 370 | (for op (negate abs) |
9b054b08 RS |
371 | /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ |
372 | (for coss (COS COSH) | |
373 | (simplify | |
374 | (coss (op @0)) | |
375 | (coss @0))) | |
376 | /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */ | |
377 | (for pows (POW) | |
378 | (simplify | |
379 | (pows (op @0) REAL_CST@1) | |
380 | (with { HOST_WIDE_INT n; } | |
381 | (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) | |
5d3498b4 | 382 | (pows @0 @1))))) |
de3fbea3 RB |
383 | /* Likewise for powi. */ |
384 | (for pows (POWI) | |
385 | (simplify | |
386 | (pows (op @0) INTEGER_CST@1) | |
387 | (if (wi::bit_and (@1, 1) == 0) | |
388 | (pows @0 @1)))) | |
5d3498b4 RS |
389 | /* Strip negate and abs from both operands of hypot. */ |
390 | (for hypots (HYPOT) | |
391 | (simplify | |
392 | (hypots (op @0) @1) | |
393 | (hypots @0 @1)) | |
394 | (simplify | |
395 | (hypots @0 (op @1)) | |
396 | (hypots @0 @1))) | |
397 | /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */ | |
398 | (for copysigns (COPYSIGN) | |
399 | (simplify | |
400 | (copysigns (op @0) @1) | |
401 | (copysigns @0 @1)))) | |
402 | ||
403 | /* abs(x)*abs(x) -> x*x. Should be valid for all types. */ | |
404 | (simplify | |
405 | (mult (abs@1 @0) @1) | |
406 | (mult @0 @0)) | |
407 | ||
408 | /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */ | |
409 | (for coss (COS COSH) | |
410 | copysigns (COPYSIGN) | |
411 | (simplify | |
412 | (coss (copysigns @0 @1)) | |
413 | (coss @0))) | |
414 | ||
415 | /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */ | |
416 | (for pows (POW) | |
417 | copysigns (COPYSIGN) | |
418 | (simplify | |
de3fbea3 | 419 | (pows (copysigns @0 @2) REAL_CST@1) |
5d3498b4 RS |
420 | (with { HOST_WIDE_INT n; } |
421 | (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) | |
422 | (pows @0 @1))))) | |
de3fbea3 RB |
423 | /* Likewise for powi. */ |
424 | (for pows (POWI) | |
425 | copysigns (COPYSIGN) | |
426 | (simplify | |
427 | (pows (copysigns @0 @2) INTEGER_CST@1) | |
428 | (if (wi::bit_and (@1, 1) == 0) | |
429 | (pows @0 @1)))) | |
5d3498b4 RS |
430 | |
431 | (for hypots (HYPOT) | |
432 | copysigns (COPYSIGN) | |
433 | /* hypot(copysign(x, y), z) -> hypot(x, z). */ | |
434 | (simplify | |
435 | (hypots (copysigns @0 @1) @2) | |
436 | (hypots @0 @2)) | |
437 | /* hypot(x, copysign(y, z)) -> hypot(x, y). */ | |
438 | (simplify | |
439 | (hypots @0 (copysigns @1 @2)) | |
440 | (hypots @0 @1))) | |
441 | ||
442 | /* copysign(copysign(x, y), z) -> copysign(x, z). */ | |
443 | (for copysigns (COPYSIGN) | |
444 | (simplify | |
445 | (copysigns (copysigns @0 @1) @2) | |
446 | (copysigns @0 @2))) | |
447 | ||
448 | /* copysign(x,y)*copysign(x,y) -> x*x. */ | |
449 | (for copysigns (COPYSIGN) | |
450 | (simplify | |
451 | (mult (copysigns@2 @0 @1) @2) | |
452 | (mult @0 @0))) | |
453 | ||
454 | /* ccos(-x) -> ccos(x). Similarly for ccosh. */ | |
455 | (for ccoss (CCOS CCOSH) | |
456 | (simplify | |
457 | (ccoss (negate @0)) | |
458 | (ccoss @0))) | |
d202f9bd | 459 | |
abcc43f5 RS |
460 | /* cabs(-x) and cos(conj(x)) -> cabs(x). */ |
461 | (for ops (conj negate) | |
462 | (for cabss (CABS) | |
463 | (simplify | |
464 | (cabss (ops @0)) | |
465 | (cabss @0)))) | |
466 | ||
0a8f32b8 RB |
467 | /* Fold (a * (1 << b)) into (a << b) */ |
468 | (simplify | |
469 | (mult:c @0 (convert? (lshift integer_onep@1 @2))) | |
470 | (if (! FLOAT_TYPE_P (type) | |
ece46666 MG |
471 | && (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
472 | || TYPE_UNSIGNED (TREE_TYPE (@1)))) | |
0a8f32b8 RB |
473 | (lshift @0 @2))) |
474 | ||
475 | /* Fold (C1/X)*C2 into (C1*C2)/X. */ | |
476 | (simplify | |
ff86345f RB |
477 | (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2) |
478 | (if (flag_associative_math | |
479 | && single_use (@3)) | |
0a8f32b8 RB |
480 | (with |
481 | { tree tem = const_binop (MULT_EXPR, type, @0, @2); } | |
482 | (if (tem) | |
483 | (rdiv { tem; } @1))))) | |
484 | ||
5711ac88 N |
485 | /* Convert C1/(X*C2) into (C1/C2)/X */ |
486 | (simplify | |
487 | (rdiv REAL_CST@0 (mult @1 REAL_CST@2)) | |
488 | (if (flag_reciprocal_math) | |
489 | (with | |
490 | { tree tem = const_binop (RDIV_EXPR, type, @0, @2); } | |
491 | (if (tem) | |
492 | (rdiv { tem; } @1))))) | |
493 | ||
0a8f32b8 RB |
494 | /* Simplify ~X & X as zero. */ |
495 | (simplify | |
496 | (bit_and:c (convert? @0) (convert? (bit_not @0))) | |
497 | { build_zero_cst (type); }) | |
498 | ||
10158317 RB |
499 | /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */ |
500 | (simplify | |
a9658b11 | 501 | (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1)) |
10158317 RB |
502 | (minus (bit_xor @0 @1) @1)) |
503 | (simplify | |
504 | (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1)) | |
505 | (if (wi::bit_not (@2) == @1) | |
506 | (minus (bit_xor @0 @1) @1))) | |
507 | ||
508 | /* Fold (A & B) - (A & ~B) into B - (A ^ B). */ | |
509 | (simplify | |
a8e9f9a3 | 510 | (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1))) |
10158317 RB |
511 | (minus @1 (bit_xor @0 @1))) |
512 | ||
513 | /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */ | |
514 | (simplify | |
a9658b11 | 515 | (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1)) |
10158317 RB |
516 | (bit_xor @0 @1)) |
517 | (simplify | |
518 | (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1)) | |
519 | (if (wi::bit_not (@2) == @1) | |
520 | (bit_xor @0 @1))) | |
d982c5b7 MG |
521 | /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */ |
522 | #if GIMPLE | |
523 | (simplify | |
524 | (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1) | |
525 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
526 | && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0) | |
527 | (bit_xor @0 @1))) | |
528 | #endif | |
10158317 | 529 | |
bc4315fb MG |
530 | /* X % Y is smaller than Y. */ |
531 | (for cmp (lt ge) | |
532 | (simplify | |
533 | (cmp (trunc_mod @0 @1) @1) | |
534 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
535 | { constant_boolean_node (cmp == LT_EXPR, type); }))) | |
536 | (for cmp (gt le) | |
537 | (simplify | |
538 | (cmp @1 (trunc_mod @0 @1)) | |
539 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
540 | { constant_boolean_node (cmp == GT_EXPR, type); }))) | |
541 | ||
e0ee10ed RB |
542 | /* x | ~0 -> ~0 */ |
543 | (simplify | |
544 | (bit_ior @0 integer_all_onesp@1) | |
545 | @1) | |
546 | ||
547 | /* x & 0 -> 0 */ | |
548 | (simplify | |
549 | (bit_and @0 integer_zerop@1) | |
550 | @1) | |
551 | ||
a4398a30 | 552 | /* ~x | x -> -1 */ |
8b5ee871 MG |
553 | /* ~x ^ x -> -1 */ |
554 | /* ~x + x -> -1 */ | |
555 | (for op (bit_ior bit_xor plus) | |
556 | (simplify | |
557 | (op:c (convert? @0) (convert? (bit_not @0))) | |
558 | (convert { build_all_ones_cst (TREE_TYPE (@0)); }))) | |
a4398a30 | 559 | |
e0ee10ed RB |
560 | /* x ^ x -> 0 */ |
561 | (simplify | |
562 | (bit_xor @0 @0) | |
563 | { build_zero_cst (type); }) | |
564 | ||
36a60e48 RB |
565 | /* Canonicalize X ^ ~0 to ~X. */ |
566 | (simplify | |
567 | (bit_xor @0 integer_all_onesp@1) | |
568 | (bit_not @0)) | |
569 | ||
570 | /* x & ~0 -> x */ | |
571 | (simplify | |
572 | (bit_and @0 integer_all_onesp) | |
573 | (non_lvalue @0)) | |
574 | ||
575 | /* x & x -> x, x | x -> x */ | |
576 | (for bitop (bit_and bit_ior) | |
577 | (simplify | |
578 | (bitop @0 @0) | |
579 | (non_lvalue @0))) | |
580 | ||
c7986356 MG |
581 | /* x & C -> x if we know that x & ~C == 0. */ |
582 | #if GIMPLE | |
583 | (simplify | |
584 | (bit_and SSA_NAME@0 INTEGER_CST@1) | |
585 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
586 | && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0) | |
587 | @0)) | |
588 | #endif | |
589 | ||
0f770b01 RV |
590 | /* x + (x & 1) -> (x + 1) & ~1 */ |
591 | (simplify | |
44fc0a51 RB |
592 | (plus:c @0 (bit_and:s @0 integer_onep@1)) |
593 | (bit_and (plus @0 @1) (bit_not @1))) | |
0f770b01 RV |
594 | |
595 | /* x & ~(x & y) -> x & ~y */ | |
596 | /* x | ~(x | y) -> x | ~y */ | |
597 | (for bitop (bit_and bit_ior) | |
af563d4b | 598 | (simplify |
44fc0a51 RB |
599 | (bitop:c @0 (bit_not (bitop:cs @0 @1))) |
600 | (bitop @0 (bit_not @1)))) | |
af563d4b MG |
601 | |
602 | /* (x | y) & ~x -> y & ~x */ | |
603 | /* (x & y) | ~x -> y | ~x */ | |
604 | (for bitop (bit_and bit_ior) | |
605 | rbitop (bit_ior bit_and) | |
606 | (simplify | |
607 | (bitop:c (rbitop:c @0 @1) (bit_not@2 @0)) | |
608 | (bitop @1 @2))) | |
0f770b01 | 609 | |
f13c4673 MP |
610 | /* (x & y) ^ (x | y) -> x ^ y */ |
611 | (simplify | |
2d6f2dce MP |
612 | (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1)) |
613 | (bit_xor @0 @1)) | |
f13c4673 | 614 | |
9ea65ca6 MP |
615 | /* (x ^ y) ^ (x | y) -> x & y */ |
616 | (simplify | |
617 | (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1)) | |
618 | (bit_and @0 @1)) | |
619 | ||
620 | /* (x & y) + (x ^ y) -> x | y */ | |
621 | /* (x & y) | (x ^ y) -> x | y */ | |
622 | /* (x & y) ^ (x ^ y) -> x | y */ | |
623 | (for op (plus bit_ior bit_xor) | |
624 | (simplify | |
625 | (op:c (bit_and @0 @1) (bit_xor @0 @1)) | |
626 | (bit_ior @0 @1))) | |
627 | ||
628 | /* (x & y) + (x | y) -> x + y */ | |
629 | (simplify | |
630 | (plus:c (bit_and @0 @1) (bit_ior @0 @1)) | |
631 | (plus @0 @1)) | |
632 | ||
9737efaf MP |
633 | /* (x + y) - (x | y) -> x & y */ |
634 | (simplify | |
635 | (minus (plus @0 @1) (bit_ior @0 @1)) | |
636 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
637 | && !TYPE_SATURATING (type)) | |
638 | (bit_and @0 @1))) | |
639 | ||
640 | /* (x + y) - (x & y) -> x | y */ | |
641 | (simplify | |
642 | (minus (plus @0 @1) (bit_and @0 @1)) | |
643 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
644 | && !TYPE_SATURATING (type)) | |
645 | (bit_ior @0 @1))) | |
646 | ||
9ea65ca6 MP |
647 | /* (x | y) - (x ^ y) -> x & y */ |
648 | (simplify | |
649 | (minus (bit_ior @0 @1) (bit_xor @0 @1)) | |
650 | (bit_and @0 @1)) | |
651 | ||
652 | /* (x | y) - (x & y) -> x ^ y */ | |
653 | (simplify | |
654 | (minus (bit_ior @0 @1) (bit_and @0 @1)) | |
655 | (bit_xor @0 @1)) | |
656 | ||
66cc6273 MP |
657 | /* (x | y) & ~(x & y) -> x ^ y */ |
658 | (simplify | |
659 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1))) | |
660 | (bit_xor @0 @1)) | |
661 | ||
662 | /* (x | y) & (~x ^ y) -> x & y */ | |
663 | (simplify | |
664 | (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0))) | |
665 | (bit_and @0 @1)) | |
666 | ||
5b00d921 RB |
667 | /* ~x & ~y -> ~(x | y) |
668 | ~x | ~y -> ~(x & y) */ | |
669 | (for op (bit_and bit_ior) | |
670 | rop (bit_ior bit_and) | |
671 | (simplify | |
672 | (op (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
ece46666 MG |
673 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
674 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
5b00d921 RB |
675 | (bit_not (rop (convert @0) (convert @1)))))) |
676 | ||
14ea9f92 | 677 | /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing |
5b00d921 RB |
678 | with a constant, and the two constants have no bits in common, |
679 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
680 | simplifications. */ | |
14ea9f92 RB |
681 | (for op (bit_xor plus) |
682 | (simplify | |
683 | (op (convert1? (bit_and@4 @0 INTEGER_CST@1)) | |
684 | (convert2? (bit_and@5 @2 INTEGER_CST@3))) | |
685 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
686 | && tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
687 | && wi::bit_and (@1, @3) == 0) | |
688 | (bit_ior (convert @4) (convert @5))))) | |
5b00d921 RB |
689 | |
690 | /* (X | Y) ^ X -> Y & ~ X*/ | |
691 | (simplify | |
692 | (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0)) | |
693 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
694 | (convert (bit_and @1 (bit_not @0))))) | |
695 | ||
696 | /* Convert ~X ^ ~Y to X ^ Y. */ | |
697 | (simplify | |
698 | (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
ece46666 MG |
699 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
700 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
5b00d921 RB |
701 | (bit_xor (convert @0) (convert @1)))) |
702 | ||
703 | /* Convert ~X ^ C to X ^ ~C. */ | |
704 | (simplify | |
705 | (bit_xor (convert? (bit_not @0)) INTEGER_CST@1) | |
c8ba6498 EB |
706 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
707 | (bit_xor (convert @0) (bit_not @1)))) | |
5b00d921 | 708 | |
e39dab2c MG |
709 | /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */ |
710 | (for opo (bit_and bit_xor) | |
711 | opi (bit_xor bit_and) | |
712 | (simplify | |
713 | (opo:c (opi:c @0 @1) @1) | |
714 | (bit_and (bit_not @0) @1))) | |
97e77391 | 715 | |
14ea9f92 RB |
716 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both |
717 | operands are another bit-wise operation with a common input. If so, | |
718 | distribute the bit operations to save an operation and possibly two if | |
719 | constants are involved. For example, convert | |
720 | (A | B) & (A | C) into A | (B & C) | |
721 | Further simplification will occur if B and C are constants. */ | |
e07ab2fe MG |
722 | (for op (bit_and bit_ior bit_xor) |
723 | rop (bit_ior bit_and bit_and) | |
14ea9f92 | 724 | (simplify |
e07ab2fe MG |
725 | (op (convert? (rop:c @0 @1)) (convert? (rop:c @0 @2))) |
726 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
727 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
14ea9f92 RB |
728 | (rop (convert @0) (op (convert @1) (convert @2)))))) |
729 | ||
e39dab2c MG |
730 | /* Some simple reassociation for bit operations, also handled in reassoc. */ |
731 | /* (X & Y) & Y -> X & Y | |
732 | (X | Y) | Y -> X | Y */ | |
733 | (for op (bit_and bit_ior) | |
734 | (simplify | |
735 | (op:c (convert?@2 (op:c @0 @1)) (convert? @1)) | |
736 | @2)) | |
737 | /* (X ^ Y) ^ Y -> X */ | |
738 | (simplify | |
739 | (bit_xor:c (convert? (bit_xor:c @0 @1)) (convert? @1)) | |
ece46666 | 740 | (convert @0)) |
e39dab2c MG |
741 | /* (X & Y) & (X & Z) -> (X & Y) & Z |
742 | (X | Y) | (X | Z) -> (X | Y) | Z */ | |
743 | (for op (bit_and bit_ior) | |
744 | (simplify | |
745 | (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2))) | |
746 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
747 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
748 | (if (single_use (@5) && single_use (@6)) | |
749 | (op @3 (convert @2)) | |
750 | (if (single_use (@3) && single_use (@4)) | |
751 | (op (convert @1) @5)))))) | |
752 | /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */ | |
753 | (simplify | |
754 | (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2))) | |
755 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
756 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
d78789f5 | 757 | (bit_xor (convert @1) (convert @2)))) |
5b00d921 | 758 | |
b14a9c57 RB |
759 | (simplify |
760 | (abs (abs@1 @0)) | |
761 | @1) | |
f3582e54 RB |
762 | (simplify |
763 | (abs (negate @0)) | |
764 | (abs @0)) | |
765 | (simplify | |
766 | (abs tree_expr_nonnegative_p@0) | |
767 | @0) | |
768 | ||
55cf3946 RB |
769 | /* A few cases of fold-const.c negate_expr_p predicate. */ |
770 | (match negate_expr_p | |
771 | INTEGER_CST | |
b14a9c57 RB |
772 | (if ((INTEGRAL_TYPE_P (type) |
773 | && TYPE_OVERFLOW_WRAPS (type)) | |
774 | || (!TYPE_OVERFLOW_SANITIZED (type) | |
55cf3946 RB |
775 | && may_negate_without_overflow_p (t))))) |
776 | (match negate_expr_p | |
777 | FIXED_CST) | |
778 | (match negate_expr_p | |
779 | (negate @0) | |
780 | (if (!TYPE_OVERFLOW_SANITIZED (type)))) | |
781 | (match negate_expr_p | |
782 | REAL_CST | |
783 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t))))) | |
784 | /* VECTOR_CST handling of non-wrapping types would recurse in unsupported | |
785 | ways. */ | |
786 | (match negate_expr_p | |
787 | VECTOR_CST | |
788 | (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)))) | |
0a8f32b8 RB |
789 | |
790 | /* (-A) * (-B) -> A * B */ | |
791 | (simplify | |
792 | (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1)) | |
793 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
794 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
795 | (mult (convert @0) (convert (negate @1))))) | |
55cf3946 RB |
796 | |
797 | /* -(A + B) -> (-B) - A. */ | |
b14a9c57 | 798 | (simplify |
55cf3946 RB |
799 | (negate (plus:c @0 negate_expr_p@1)) |
800 | (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) | |
801 | && !HONOR_SIGNED_ZEROS (element_mode (type))) | |
802 | (minus (negate @1) @0))) | |
803 | ||
804 | /* A - B -> A + (-B) if B is easily negatable. */ | |
b14a9c57 | 805 | (simplify |
55cf3946 | 806 | (minus @0 negate_expr_p@1) |
e4e96a4f KT |
807 | (if (!FIXED_POINT_TYPE_P (type)) |
808 | (plus @0 (negate @1)))) | |
d4573ffe | 809 | |
5609420f RB |
810 | /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) |
811 | when profitable. | |
812 | For bitwise binary operations apply operand conversions to the | |
813 | binary operation result instead of to the operands. This allows | |
814 | to combine successive conversions and bitwise binary operations. | |
815 | We combine the above two cases by using a conditional convert. */ | |
816 | (for bitop (bit_and bit_ior bit_xor) | |
817 | (simplify | |
818 | (bitop (convert @0) (convert? @1)) | |
819 | (if (((TREE_CODE (@1) == INTEGER_CST | |
820 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
ad6f996c | 821 | && int_fits_type_p (@1, TREE_TYPE (@0))) |
aea417d7 | 822 | || types_match (@0, @1)) |
ad6f996c RB |
823 | /* ??? This transform conflicts with fold-const.c doing |
824 | Convert (T)(x & c) into (T)x & (T)c, if c is an integer | |
825 | constants (if x has signed type, the sign bit cannot be set | |
826 | in c). This folds extension into the BIT_AND_EXPR. | |
827 | Restrict it to GIMPLE to avoid endless recursions. */ | |
828 | && (bitop != BIT_AND_EXPR || GIMPLE) | |
5609420f RB |
829 | && (/* That's a good idea if the conversion widens the operand, thus |
830 | after hoisting the conversion the operation will be narrower. */ | |
831 | TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type) | |
832 | /* It's also a good idea if the conversion is to a non-integer | |
833 | mode. */ | |
834 | || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT | |
835 | /* Or if the precision of TO is not the same as the precision | |
836 | of its mode. */ | |
837 | || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type)))) | |
838 | (convert (bitop @0 (convert @1)))))) | |
839 | ||
b14a9c57 RB |
840 | (for bitop (bit_and bit_ior) |
841 | rbitop (bit_ior bit_and) | |
842 | /* (x | y) & x -> x */ | |
843 | /* (x & y) | x -> x */ | |
844 | (simplify | |
845 | (bitop:c (rbitop:c @0 @1) @0) | |
846 | @0) | |
847 | /* (~x | y) & x -> x & y */ | |
848 | /* (~x & y) | x -> x | y */ | |
849 | (simplify | |
850 | (bitop:c (rbitop:c (bit_not @0) @1) @0) | |
851 | (bitop @0 @1))) | |
852 | ||
5609420f RB |
853 | /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ |
854 | (simplify | |
855 | (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
856 | (bit_ior (bit_and @0 @2) (bit_and @1 @2))) | |
857 | ||
858 | /* Combine successive equal operations with constants. */ | |
859 | (for bitop (bit_and bit_ior bit_xor) | |
860 | (simplify | |
861 | (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
862 | (bitop @0 (bitop @1 @2)))) | |
863 | ||
864 | /* Try simple folding for X op !X, and X op X with the help | |
865 | of the truth_valued_p and logical_inverted_value predicates. */ | |
866 | (match truth_valued_p | |
867 | @0 | |
868 | (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))) | |
f84e7fd6 | 869 | (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor) |
5609420f RB |
870 | (match truth_valued_p |
871 | (op @0 @1))) | |
872 | (match truth_valued_p | |
873 | (truth_not @0)) | |
874 | ||
0a8f32b8 RB |
875 | (match (logical_inverted_value @0) |
876 | (truth_not @0)) | |
5609420f RB |
877 | (match (logical_inverted_value @0) |
878 | (bit_not truth_valued_p@0)) | |
879 | (match (logical_inverted_value @0) | |
09240451 | 880 | (eq @0 integer_zerop)) |
5609420f | 881 | (match (logical_inverted_value @0) |
09240451 | 882 | (ne truth_valued_p@0 integer_truep)) |
5609420f | 883 | (match (logical_inverted_value @0) |
09240451 | 884 | (bit_xor truth_valued_p@0 integer_truep)) |
5609420f RB |
885 | |
886 | /* X & !X -> 0. */ | |
887 | (simplify | |
888 | (bit_and:c @0 (logical_inverted_value @0)) | |
889 | { build_zero_cst (type); }) | |
890 | /* X | !X and X ^ !X -> 1, , if X is truth-valued. */ | |
891 | (for op (bit_ior bit_xor) | |
892 | (simplify | |
893 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
f84e7fd6 | 894 | { constant_boolean_node (true, type); })) |
59c20dc7 RB |
895 | /* X ==/!= !X is false/true. */ |
896 | (for op (eq ne) | |
897 | (simplify | |
898 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
899 | { constant_boolean_node (op == NE_EXPR ? true : false, type); })) | |
5609420f | 900 | |
5609420f RB |
901 | /* If arg1 and arg2 are booleans (or any single bit type) |
902 | then try to simplify: | |
903 | ||
904 | (~X & Y) -> X < Y | |
905 | (X & ~Y) -> Y < X | |
906 | (~X | Y) -> X <= Y | |
907 | (X | ~Y) -> Y <= X | |
908 | ||
909 | But only do this if our result feeds into a comparison as | |
910 | this transformation is not always a win, particularly on | |
911 | targets with and-not instructions. | |
912 | -> simplify_bitwise_binary_boolean */ | |
913 | (simplify | |
914 | (ne (bit_and:c (bit_not @0) @1) integer_zerop) | |
915 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
916 | && TYPE_PRECISION (TREE_TYPE (@1)) == 1) | |
0f3f9437 RB |
917 | (if (TYPE_UNSIGNED (TREE_TYPE (@1))) |
918 | (lt @0 @1) | |
919 | (gt @0 @1)))) | |
5609420f RB |
920 | (simplify |
921 | (ne (bit_ior:c (bit_not @0) @1) integer_zerop) | |
922 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
923 | && TYPE_PRECISION (TREE_TYPE (@1)) == 1) | |
0f3f9437 RB |
924 | (if (TYPE_UNSIGNED (TREE_TYPE (@1))) |
925 | (le @0 @1) | |
926 | (ge @0 @1)))) | |
5609420f | 927 | |
5609420f RB |
928 | /* ~~x -> x */ |
929 | (simplify | |
930 | (bit_not (bit_not @0)) | |
931 | @0) | |
932 | ||
b14a9c57 RB |
933 | /* Convert ~ (-A) to A - 1. */ |
934 | (simplify | |
935 | (bit_not (convert? (negate @0))) | |
ece46666 MG |
936 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
937 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
8b5ee871 | 938 | (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) |
b14a9c57 RB |
939 | |
940 | /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ | |
941 | (simplify | |
8b5ee871 | 942 | (bit_not (convert? (minus @0 integer_each_onep))) |
ece46666 MG |
943 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
944 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
b14a9c57 RB |
945 | (convert (negate @0)))) |
946 | (simplify | |
947 | (bit_not (convert? (plus @0 integer_all_onesp))) | |
ece46666 MG |
948 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
949 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
b14a9c57 RB |
950 | (convert (negate @0)))) |
951 | ||
952 | /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ | |
953 | (simplify | |
954 | (bit_not (convert? (bit_xor @0 INTEGER_CST@1))) | |
955 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
956 | (convert (bit_xor @0 (bit_not @1))))) | |
957 | (simplify | |
958 | (bit_not (convert? (bit_xor:c (bit_not @0) @1))) | |
959 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
960 | (convert (bit_xor @0 @1)))) | |
961 | ||
f52baa7b MP |
962 | /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ |
963 | (simplify | |
44fc0a51 RB |
964 | (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2)) |
965 | (bit_xor (bit_and (bit_xor @0 @1) @2) @0)) | |
f52baa7b | 966 | |
f7b7b0aa MP |
967 | /* Fold A - (A & B) into ~B & A. */ |
968 | (simplify | |
969 | (minus (convert? @0) (convert?:s (bit_and:cs @0 @1))) | |
970 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
971 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
972 | (convert (bit_and (bit_not @1) @0)))) | |
5609420f | 973 | |
84ff66b8 AV |
974 | |
975 | ||
976 | /* ((X inner_op C0) outer_op C1) | |
977 | With X being a tree where value_range has reasoned certain bits to always be | |
978 | zero throughout its computed value range, | |
979 | inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op | |
980 | where zero_mask has 1's for all bits that are sure to be 0 in | |
981 | and 0's otherwise. | |
982 | if (inner_op == '^') C0 &= ~C1; | |
983 | if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1) | |
984 | if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1) | |
985 | */ | |
986 | (for inner_op (bit_ior bit_xor) | |
987 | outer_op (bit_xor bit_ior) | |
988 | (simplify | |
989 | (outer_op | |
990 | (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1) | |
991 | (with | |
992 | { | |
993 | bool fail = false; | |
994 | wide_int zero_mask_not; | |
995 | wide_int C0; | |
996 | wide_int cst_emit; | |
997 | ||
998 | if (TREE_CODE (@2) == SSA_NAME) | |
999 | zero_mask_not = get_nonzero_bits (@2); | |
1000 | else | |
1001 | fail = true; | |
1002 | ||
1003 | if (inner_op == BIT_XOR_EXPR) | |
1004 | { | |
1005 | C0 = wi::bit_and_not (@0, @1); | |
1006 | cst_emit = wi::bit_or (C0, @1); | |
1007 | } | |
1008 | else | |
1009 | { | |
1010 | C0 = @0; | |
1011 | cst_emit = wi::bit_xor (@0, @1); | |
1012 | } | |
1013 | } | |
1014 | (if (!fail && wi::bit_and (C0, zero_mask_not) == 0) | |
1015 | (outer_op @2 { wide_int_to_tree (type, cst_emit); }) | |
1016 | (if (!fail && wi::bit_and (@1, zero_mask_not) == 0) | |
1017 | (inner_op @2 { wide_int_to_tree (type, cst_emit); })))))) | |
1018 | ||
a499aac5 RB |
1019 | /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */ |
1020 | (simplify | |
44fc0a51 RB |
1021 | (pointer_plus (pointer_plus:s @0 @1) @3) |
1022 | (pointer_plus @0 (plus @1 @3))) | |
a499aac5 RB |
1023 | |
1024 | /* Pattern match | |
1025 | tem1 = (long) ptr1; | |
1026 | tem2 = (long) ptr2; | |
1027 | tem3 = tem2 - tem1; | |
1028 | tem4 = (unsigned long) tem3; | |
1029 | tem5 = ptr1 + tem4; | |
1030 | and produce | |
1031 | tem5 = ptr2; */ | |
1032 | (simplify | |
1033 | (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0)))) | |
1034 | /* Conditionally look through a sign-changing conversion. */ | |
1035 | (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3)) | |
1036 | && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1))) | |
1037 | || (GENERIC && type == TREE_TYPE (@1)))) | |
1038 | @1)) | |
1039 | ||
1040 | /* Pattern match | |
1041 | tem = (sizetype) ptr; | |
1042 | tem = tem & algn; | |
1043 | tem = -tem; | |
1044 | ... = ptr p+ tem; | |
1045 | and produce the simpler and easier to analyze with respect to alignment | |
1046 | ... = ptr & ~algn; */ | |
1047 | (simplify | |
1048 | (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1))) | |
1049 | (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); } | |
1050 | (bit_and @0 { algn; }))) | |
1051 | ||
99e943a2 RB |
1052 | /* Try folding difference of addresses. */ |
1053 | (simplify | |
1054 | (minus (convert ADDR_EXPR@0) (convert @1)) | |
1055 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1056 | (with { HOST_WIDE_INT diff; } | |
1057 | (if (ptr_difference_const (@0, @1, &diff)) | |
1058 | { build_int_cst_type (type, diff); })))) | |
1059 | (simplify | |
1060 | (minus (convert @0) (convert ADDR_EXPR@1)) | |
1061 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1062 | (with { HOST_WIDE_INT diff; } | |
1063 | (if (ptr_difference_const (@0, @1, &diff)) | |
1064 | { build_int_cst_type (type, diff); })))) | |
1065 | ||
bab73f11 RB |
1066 | /* If arg0 is derived from the address of an object or function, we may |
1067 | be able to fold this expression using the object or function's | |
1068 | alignment. */ | |
1069 | (simplify | |
1070 | (bit_and (convert? @0) INTEGER_CST@1) | |
1071 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1072 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1073 | (with | |
1074 | { | |
1075 | unsigned int align; | |
1076 | unsigned HOST_WIDE_INT bitpos; | |
1077 | get_pointer_alignment_1 (@0, &align, &bitpos); | |
1078 | } | |
1079 | (if (wi::ltu_p (@1, align / BITS_PER_UNIT)) | |
1080 | { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); })))) | |
99e943a2 | 1081 | |
a499aac5 | 1082 | |
cc7b5acf RB |
1083 | /* We can't reassociate at all for saturating types. */ |
1084 | (if (!TYPE_SATURATING (type)) | |
1085 | ||
1086 | /* Contract negates. */ | |
1087 | /* A + (-B) -> A - B */ | |
1088 | (simplify | |
1089 | (plus:c (convert1? @0) (convert2? (negate @1))) | |
1090 | /* Apply STRIP_NOPS on @0 and the negate. */ | |
1091 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
1092 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 1093 | && !TYPE_OVERFLOW_SANITIZED (type)) |
cc7b5acf RB |
1094 | (minus (convert @0) (convert @1)))) |
1095 | /* A - (-B) -> A + B */ | |
1096 | (simplify | |
1097 | (minus (convert1? @0) (convert2? (negate @1))) | |
1098 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
2f68e8bc | 1099 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) |
6a4f0678 | 1100 | && !TYPE_OVERFLOW_SANITIZED (type)) |
cc7b5acf RB |
1101 | (plus (convert @0) (convert @1)))) |
1102 | /* -(-A) -> A */ | |
1103 | (simplify | |
1104 | (negate (convert? (negate @1))) | |
1105 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 1106 | && !TYPE_OVERFLOW_SANITIZED (type)) |
a0f12cf8 | 1107 | (convert @1))) |
cc7b5acf | 1108 | |
7318e44f RB |
1109 | /* We can't reassociate floating-point unless -fassociative-math |
1110 | or fixed-point plus or minus because of saturation to +-Inf. */ | |
1111 | (if ((!FLOAT_TYPE_P (type) || flag_associative_math) | |
1112 | && !FIXED_POINT_TYPE_P (type)) | |
cc7b5acf RB |
1113 | |
1114 | /* Match patterns that allow contracting a plus-minus pair | |
1115 | irrespective of overflow issues. */ | |
1116 | /* (A +- B) - A -> +- B */ | |
1117 | /* (A +- B) -+ B -> A */ | |
1118 | /* A - (A +- B) -> -+ B */ | |
1119 | /* A +- (B -+ A) -> +- B */ | |
1120 | (simplify | |
1121 | (minus (plus:c @0 @1) @0) | |
1122 | @1) | |
1123 | (simplify | |
1124 | (minus (minus @0 @1) @0) | |
1125 | (negate @1)) | |
1126 | (simplify | |
1127 | (plus:c (minus @0 @1) @1) | |
1128 | @0) | |
1129 | (simplify | |
1130 | (minus @0 (plus:c @0 @1)) | |
1131 | (negate @1)) | |
1132 | (simplify | |
1133 | (minus @0 (minus @0 @1)) | |
1134 | @1) | |
1135 | ||
1136 | /* (A +- CST) +- CST -> A + CST */ | |
1137 | (for outer_op (plus minus) | |
1138 | (for inner_op (plus minus) | |
1139 | (simplify | |
1140 | (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
1141 | /* If the constant operation overflows we cannot do the transform | |
1142 | as we would introduce undefined overflow, for example | |
1143 | with (a - 1) + INT_MIN. */ | |
23f27839 | 1144 | (with { tree cst = const_binop (outer_op == inner_op |
cc7b5acf RB |
1145 | ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); } |
1146 | (if (cst && !TREE_OVERFLOW (cst)) | |
1147 | (inner_op @0 { cst; } )))))) | |
1148 | ||
1149 | /* (CST - A) +- CST -> CST - A */ | |
1150 | (for outer_op (plus minus) | |
1151 | (simplify | |
1152 | (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2) | |
23f27839 | 1153 | (with { tree cst = const_binop (outer_op, type, @1, @2); } |
cc7b5acf RB |
1154 | (if (cst && !TREE_OVERFLOW (cst)) |
1155 | (minus { cst; } @0))))) | |
1156 | ||
1157 | /* ~A + A -> -1 */ | |
1158 | (simplify | |
1159 | (plus:c (bit_not @0) @0) | |
1160 | (if (!TYPE_OVERFLOW_TRAPS (type)) | |
1161 | { build_all_ones_cst (type); })) | |
1162 | ||
1163 | /* ~A + 1 -> -A */ | |
1164 | (simplify | |
e19740ae RB |
1165 | (plus (convert? (bit_not @0)) integer_each_onep) |
1166 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1167 | (negate (convert @0)))) | |
1168 | ||
1169 | /* -A - 1 -> ~A */ | |
1170 | (simplify | |
1171 | (minus (convert? (negate @0)) integer_each_onep) | |
1172 | (if (!TYPE_OVERFLOW_TRAPS (type) | |
1173 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1174 | (bit_not (convert @0)))) | |
1175 | ||
1176 | /* -1 - A -> ~A */ | |
1177 | (simplify | |
1178 | (minus integer_all_onesp @0) | |
bc4315fb | 1179 | (bit_not @0)) |
cc7b5acf RB |
1180 | |
1181 | /* (T)(P + A) - (T)P -> (T) A */ | |
1182 | (for add (plus pointer_plus) | |
1183 | (simplify | |
1184 | (minus (convert (add @0 @1)) | |
1185 | (convert @0)) | |
09240451 | 1186 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
cc7b5acf RB |
1187 | /* For integer types, if A has a smaller type |
1188 | than T the result depends on the possible | |
1189 | overflow in P + A. | |
1190 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
1191 | However, if an overflow in P + A would cause | |
1192 | undefined behavior, we can assume that there | |
1193 | is no overflow. */ | |
1194 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1195 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1196 | /* For pointer types, if the conversion of A to the | |
1197 | final type requires a sign- or zero-extension, | |
1198 | then we have to punt - it is not defined which | |
1199 | one is correct. */ | |
1200 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1201 | && TREE_CODE (@1) == INTEGER_CST | |
1202 | && tree_int_cst_sign_bit (@1) == 0)) | |
a8fc2579 RB |
1203 | (convert @1)))) |
1204 | ||
1205 | /* (T)P - (T)(P + A) -> -(T) A */ | |
1206 | (for add (plus pointer_plus) | |
1207 | (simplify | |
1208 | (minus (convert @0) | |
1209 | (convert (add @0 @1))) | |
1210 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
1211 | /* For integer types, if A has a smaller type | |
1212 | than T the result depends on the possible | |
1213 | overflow in P + A. | |
1214 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
1215 | However, if an overflow in P + A would cause | |
1216 | undefined behavior, we can assume that there | |
1217 | is no overflow. */ | |
1218 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1219 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1220 | /* For pointer types, if the conversion of A to the | |
1221 | final type requires a sign- or zero-extension, | |
1222 | then we have to punt - it is not defined which | |
1223 | one is correct. */ | |
1224 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1225 | && TREE_CODE (@1) == INTEGER_CST | |
1226 | && tree_int_cst_sign_bit (@1) == 0)) | |
1227 | (negate (convert @1))))) | |
1228 | ||
1229 | /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */ | |
1230 | (for add (plus pointer_plus) | |
1231 | (simplify | |
1232 | (minus (convert (add @0 @1)) | |
1233 | (convert (add @0 @2))) | |
1234 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
1235 | /* For integer types, if A has a smaller type | |
1236 | than T the result depends on the possible | |
1237 | overflow in P + A. | |
1238 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
1239 | However, if an overflow in P + A would cause | |
1240 | undefined behavior, we can assume that there | |
1241 | is no overflow. */ | |
1242 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1243 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1244 | /* For pointer types, if the conversion of A to the | |
1245 | final type requires a sign- or zero-extension, | |
1246 | then we have to punt - it is not defined which | |
1247 | one is correct. */ | |
1248 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1249 | && TREE_CODE (@1) == INTEGER_CST | |
1250 | && tree_int_cst_sign_bit (@1) == 0 | |
1251 | && TREE_CODE (@2) == INTEGER_CST | |
1252 | && tree_int_cst_sign_bit (@2) == 0)) | |
1253 | (minus (convert @1) (convert @2))))))) | |
cc7b5acf RB |
1254 | |
1255 | ||
0122e8e5 | 1256 | /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */ |
a7f24614 | 1257 | |
0122e8e5 | 1258 | (for minmax (min max FMIN FMAX) |
a7f24614 RB |
1259 | (simplify |
1260 | (minmax @0 @0) | |
1261 | @0)) | |
4a334cba RS |
1262 | /* min(max(x,y),y) -> y. */ |
1263 | (simplify | |
1264 | (min:c (max:c @0 @1) @1) | |
1265 | @1) | |
1266 | /* max(min(x,y),y) -> y. */ | |
1267 | (simplify | |
1268 | (max:c (min:c @0 @1) @1) | |
1269 | @1) | |
a7f24614 RB |
1270 | (simplify |
1271 | (min @0 @1) | |
2c2870a1 MG |
1272 | (switch |
1273 | (if (INTEGRAL_TYPE_P (type) | |
1274 | && TYPE_MIN_VALUE (type) | |
1275 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
1276 | @1) | |
1277 | (if (INTEGRAL_TYPE_P (type) | |
1278 | && TYPE_MAX_VALUE (type) | |
1279 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
1280 | @0))) | |
a7f24614 RB |
1281 | (simplify |
1282 | (max @0 @1) | |
2c2870a1 MG |
1283 | (switch |
1284 | (if (INTEGRAL_TYPE_P (type) | |
1285 | && TYPE_MAX_VALUE (type) | |
1286 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
1287 | @1) | |
1288 | (if (INTEGRAL_TYPE_P (type) | |
1289 | && TYPE_MIN_VALUE (type) | |
1290 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
1291 | @0))) | |
0122e8e5 RS |
1292 | (for minmax (FMIN FMAX) |
1293 | /* If either argument is NaN, return the other one. Avoid the | |
1294 | transformation if we get (and honor) a signalling NaN. */ | |
1295 | (simplify | |
1296 | (minmax:c @0 REAL_CST@1) | |
1297 | (if (real_isnan (TREE_REAL_CST_PTR (@1)) | |
1298 | && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)) | |
1299 | @0))) | |
1300 | /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these | |
1301 | functions to return the numeric arg if the other one is NaN. | |
1302 | MIN and MAX don't honor that, so only transform if -ffinite-math-only | |
1303 | is set. C99 doesn't require -0.0 to be handled, so we don't have to | |
1304 | worry about it either. */ | |
1305 | (if (flag_finite_math_only) | |
1306 | (simplify | |
1307 | (FMIN @0 @1) | |
1308 | (min @0 @1)) | |
1309 | (simplify | |
1310 | (FMAX @0 @1) | |
1311 | (max @0 @1))) | |
ce0e66ff MG |
1312 | /* min (-A, -B) -> -max (A, B) */ |
1313 | (for minmax (min max FMIN FMAX) | |
1314 | maxmin (max min FMAX FMIN) | |
1315 | (simplify | |
1316 | (minmax (negate:s@2 @0) (negate:s@3 @1)) | |
1317 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1318 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1319 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
1320 | (negate (maxmin @0 @1))))) | |
1321 | /* MIN (~X, ~Y) -> ~MAX (X, Y) | |
1322 | MAX (~X, ~Y) -> ~MIN (X, Y) */ | |
1323 | (for minmax (min max) | |
1324 | maxmin (max min) | |
1325 | (simplify | |
1326 | (minmax (bit_not:s@2 @0) (bit_not:s@3 @1)) | |
1327 | (bit_not (maxmin @0 @1)))) | |
a7f24614 | 1328 | |
b4817bd6 MG |
1329 | /* MIN (X, Y) == X -> X <= Y */ |
1330 | (for minmax (min min max max) | |
1331 | cmp (eq ne eq ne ) | |
1332 | out (le gt ge lt ) | |
1333 | (simplify | |
1334 | (cmp:c (minmax:c @0 @1) @0) | |
1335 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
1336 | (out @0 @1)))) | |
1337 | /* MIN (X, 5) == 0 -> X == 0 | |
1338 | MIN (X, 5) == 7 -> false */ | |
1339 | (for cmp (eq ne) | |
1340 | (simplify | |
1341 | (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2) | |
1342 | (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0)))) | |
1343 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
1344 | (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0)))) | |
1345 | (cmp @0 @2))))) | |
1346 | (for cmp (eq ne) | |
1347 | (simplify | |
1348 | (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2) | |
1349 | (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0)))) | |
1350 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
1351 | (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0)))) | |
1352 | (cmp @0 @2))))) | |
1353 | /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */ | |
1354 | (for minmax (min min max max min min max max ) | |
1355 | cmp (lt le gt ge gt ge lt le ) | |
1356 | comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and) | |
1357 | (simplify | |
1358 | (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2) | |
1359 | (comb (cmp @0 @2) (cmp @1 @2)))) | |
1360 | ||
a7f24614 RB |
1361 | /* Simplifications of shift and rotates. */ |
1362 | ||
1363 | (for rotate (lrotate rrotate) | |
1364 | (simplify | |
1365 | (rotate integer_all_onesp@0 @1) | |
1366 | @0)) | |
1367 | ||
1368 | /* Optimize -1 >> x for arithmetic right shifts. */ | |
1369 | (simplify | |
1370 | (rshift integer_all_onesp@0 @1) | |
1371 | (if (!TYPE_UNSIGNED (type) | |
1372 | && tree_expr_nonnegative_p (@1)) | |
1373 | @0)) | |
1374 | ||
12085390 N |
1375 | /* Optimize (x >> c) << c into x & (-1<<c). */ |
1376 | (simplify | |
1377 | (lshift (rshift @0 INTEGER_CST@1) @1) | |
1378 | (if (wi::ltu_p (@1, element_precision (type))) | |
1379 | (bit_and @0 (lshift { build_minus_one_cst (type); } @1)))) | |
1380 | ||
1381 | /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned | |
1382 | types. */ | |
1383 | (simplify | |
1384 | (rshift (lshift @0 INTEGER_CST@1) @1) | |
1385 | (if (TYPE_UNSIGNED (type) | |
1386 | && (wi::ltu_p (@1, element_precision (type)))) | |
1387 | (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) | |
1388 | ||
a7f24614 RB |
1389 | (for shiftrotate (lrotate rrotate lshift rshift) |
1390 | (simplify | |
1391 | (shiftrotate @0 integer_zerop) | |
1392 | (non_lvalue @0)) | |
1393 | (simplify | |
1394 | (shiftrotate integer_zerop@0 @1) | |
1395 | @0) | |
1396 | /* Prefer vector1 << scalar to vector1 << vector2 | |
1397 | if vector2 is uniform. */ | |
1398 | (for vec (VECTOR_CST CONSTRUCTOR) | |
1399 | (simplify | |
1400 | (shiftrotate @0 vec@1) | |
1401 | (with { tree tem = uniform_vector_p (@1); } | |
1402 | (if (tem) | |
1403 | (shiftrotate @0 { tem; })))))) | |
1404 | ||
1405 | /* Rewrite an LROTATE_EXPR by a constant into an | |
1406 | RROTATE_EXPR by a new constant. */ | |
1407 | (simplify | |
1408 | (lrotate @0 INTEGER_CST@1) | |
23f27839 | 1409 | (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1), |
a7f24614 RB |
1410 | build_int_cst (TREE_TYPE (@1), |
1411 | element_precision (type)), @1); })) | |
1412 | ||
14ea9f92 RB |
1413 | /* Turn (a OP c1) OP c2 into a OP (c1+c2). */ |
1414 | (for op (lrotate rrotate rshift lshift) | |
1415 | (simplify | |
1416 | (op (op @0 INTEGER_CST@1) INTEGER_CST@2) | |
1417 | (with { unsigned int prec = element_precision (type); } | |
1418 | (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))) | |
1419 | && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1))) | |
1420 | && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2))) | |
1421 | && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2)))) | |
1422 | (with { unsigned int low = wi::add (@1, @2).to_uhwi (); } | |
1423 | /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2 | |
1424 | being well defined. */ | |
1425 | (if (low >= prec) | |
1426 | (if (op == LROTATE_EXPR || op == RROTATE_EXPR) | |
8fdc6c67 | 1427 | (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); }) |
50301115 | 1428 | (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR) |
8fdc6c67 RB |
1429 | { build_zero_cst (type); } |
1430 | (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); }))) | |
1431 | (op @0 { build_int_cst (TREE_TYPE (@1), low); }))))))) | |
14ea9f92 RB |
1432 | |
1433 | ||
01ada710 MP |
1434 | /* ((1 << A) & 1) != 0 -> A == 0 |
1435 | ((1 << A) & 1) == 0 -> A != 0 */ | |
1436 | (for cmp (ne eq) | |
1437 | icmp (eq ne) | |
1438 | (simplify | |
1439 | (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop) | |
1440 | (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))) | |
cc7b5acf | 1441 | |
f2e609c3 MP |
1442 | /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) |
1443 | (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) | |
1444 | if CST2 != 0. */ | |
1445 | (for cmp (ne eq) | |
1446 | (simplify | |
1447 | (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2) | |
1448 | (with { int cand = wi::ctz (@2) - wi::ctz (@0); } | |
1449 | (if (cand < 0 | |
1450 | || (!integer_zerop (@2) | |
1451 | && wi::ne_p (wi::lshift (@0, cand), @2))) | |
8fdc6c67 RB |
1452 | { constant_boolean_node (cmp == NE_EXPR, type); } |
1453 | (if (!integer_zerop (@2) | |
1454 | && wi::eq_p (wi::lshift (@0, cand), @2)) | |
1455 | (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); })))))) | |
f2e609c3 | 1456 | |
1ffbaa3f RB |
1457 | /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) |
1458 | (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) | |
1459 | if the new mask might be further optimized. */ | |
1460 | (for shift (lshift rshift) | |
1461 | (simplify | |
44fc0a51 RB |
1462 | (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1)) |
1463 | INTEGER_CST@2) | |
1ffbaa3f RB |
1464 | (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5)) |
1465 | && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT | |
1466 | && tree_fits_uhwi_p (@1) | |
1467 | && tree_to_uhwi (@1) > 0 | |
1468 | && tree_to_uhwi (@1) < TYPE_PRECISION (type)) | |
1469 | (with | |
1470 | { | |
1471 | unsigned int shiftc = tree_to_uhwi (@1); | |
1472 | unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2); | |
1473 | unsigned HOST_WIDE_INT newmask, zerobits = 0; | |
1474 | tree shift_type = TREE_TYPE (@3); | |
1475 | unsigned int prec; | |
1476 | ||
1477 | if (shift == LSHIFT_EXPR) | |
fecfbfa4 | 1478 | zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1); |
1ffbaa3f RB |
1479 | else if (shift == RSHIFT_EXPR |
1480 | && (TYPE_PRECISION (shift_type) | |
1481 | == GET_MODE_PRECISION (TYPE_MODE (shift_type)))) | |
1482 | { | |
1483 | prec = TYPE_PRECISION (TREE_TYPE (@3)); | |
1484 | tree arg00 = @0; | |
1485 | /* See if more bits can be proven as zero because of | |
1486 | zero extension. */ | |
1487 | if (@3 != @0 | |
1488 | && TYPE_UNSIGNED (TREE_TYPE (@0))) | |
1489 | { | |
1490 | tree inner_type = TREE_TYPE (@0); | |
1491 | if ((TYPE_PRECISION (inner_type) | |
1492 | == GET_MODE_PRECISION (TYPE_MODE (inner_type))) | |
1493 | && TYPE_PRECISION (inner_type) < prec) | |
1494 | { | |
1495 | prec = TYPE_PRECISION (inner_type); | |
1496 | /* See if we can shorten the right shift. */ | |
1497 | if (shiftc < prec) | |
1498 | shift_type = inner_type; | |
1499 | /* Otherwise X >> C1 is all zeros, so we'll optimize | |
1500 | it into (X, 0) later on by making sure zerobits | |
1501 | is all ones. */ | |
1502 | } | |
1503 | } | |
dd4786fe | 1504 | zerobits = HOST_WIDE_INT_M1U; |
1ffbaa3f RB |
1505 | if (shiftc < prec) |
1506 | { | |
1507 | zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; | |
1508 | zerobits <<= prec - shiftc; | |
1509 | } | |
1510 | /* For arithmetic shift if sign bit could be set, zerobits | |
1511 | can contain actually sign bits, so no transformation is | |
1512 | possible, unless MASK masks them all away. In that | |
1513 | case the shift needs to be converted into logical shift. */ | |
1514 | if (!TYPE_UNSIGNED (TREE_TYPE (@3)) | |
1515 | && prec == TYPE_PRECISION (TREE_TYPE (@3))) | |
1516 | { | |
1517 | if ((mask & zerobits) == 0) | |
1518 | shift_type = unsigned_type_for (TREE_TYPE (@3)); | |
1519 | else | |
1520 | zerobits = 0; | |
1521 | } | |
1522 | } | |
1523 | } | |
1524 | /* ((X << 16) & 0xff00) is (X, 0). */ | |
1525 | (if ((mask & zerobits) == mask) | |
8fdc6c67 RB |
1526 | { build_int_cst (type, 0); } |
1527 | (with { newmask = mask | zerobits; } | |
1528 | (if (newmask != mask && (newmask & (newmask + 1)) == 0) | |
1529 | (with | |
1530 | { | |
1531 | /* Only do the transformation if NEWMASK is some integer | |
1532 | mode's mask. */ | |
1533 | for (prec = BITS_PER_UNIT; | |
1534 | prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) | |
fecfbfa4 | 1535 | if (newmask == (HOST_WIDE_INT_1U << prec) - 1) |
8fdc6c67 RB |
1536 | break; |
1537 | } | |
1538 | (if (prec < HOST_BITS_PER_WIDE_INT | |
dd4786fe | 1539 | || newmask == HOST_WIDE_INT_M1U) |
8fdc6c67 RB |
1540 | (with |
1541 | { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); } | |
1542 | (if (!tree_int_cst_equal (newmaskt, @2)) | |
1543 | (if (shift_type != TREE_TYPE (@3)) | |
1544 | (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; }) | |
1545 | (bit_and @4 { newmaskt; }))))))))))))) | |
1ffbaa3f | 1546 | |
84ff66b8 AV |
1547 | /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1) |
1548 | (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */ | |
98e30e51 | 1549 | (for shift (lshift rshift) |
84ff66b8 AV |
1550 | (for bit_op (bit_and bit_xor bit_ior) |
1551 | (simplify | |
1552 | (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1) | |
1553 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1554 | (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); } | |
1555 | (bit_op (shift (convert @0) @1) { mask; })))))) | |
98e30e51 | 1556 | |
ad1d92ab MM |
1557 | /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */ |
1558 | (simplify | |
1559 | (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2))) | |
1560 | (if (!TYPE_UNSIGNED (TREE_TYPE (@0)) | |
ece46666 MG |
1561 | && (element_precision (TREE_TYPE (@0)) |
1562 | <= element_precision (TREE_TYPE (@1)) | |
1563 | || !TYPE_UNSIGNED (TREE_TYPE (@1)))) | |
ad1d92ab MM |
1564 | (with |
1565 | { tree shift_type = TREE_TYPE (@0); } | |
1566 | (convert (rshift (convert:shift_type @1) @2))))) | |
1567 | ||
1568 | /* ~(~X >>r Y) -> X >>r Y | |
1569 | ~(~X <<r Y) -> X <<r Y */ | |
1570 | (for rotate (lrotate rrotate) | |
1571 | (simplify | |
1572 | (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2))) | |
ece46666 MG |
1573 | (if ((element_precision (TREE_TYPE (@0)) |
1574 | <= element_precision (TREE_TYPE (@1)) | |
1575 | || !TYPE_UNSIGNED (TREE_TYPE (@1))) | |
1576 | && (element_precision (type) <= element_precision (TREE_TYPE (@0)) | |
1577 | || !TYPE_UNSIGNED (TREE_TYPE (@0)))) | |
ad1d92ab MM |
1578 | (with |
1579 | { tree rotate_type = TREE_TYPE (@0); } | |
1580 | (convert (rotate (convert:rotate_type @1) @2)))))) | |
98e30e51 | 1581 | |
d4573ffe RB |
1582 | /* Simplifications of conversions. */ |
1583 | ||
1584 | /* Basic strip-useless-type-conversions / strip_nops. */ | |
f3582e54 | 1585 | (for cvt (convert view_convert float fix_trunc) |
d4573ffe RB |
1586 | (simplify |
1587 | (cvt @0) | |
1588 | (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0))) | |
1589 | || (GENERIC && type == TREE_TYPE (@0))) | |
1590 | @0))) | |
1591 | ||
1592 | /* Contract view-conversions. */ | |
1593 | (simplify | |
1594 | (view_convert (view_convert @0)) | |
1595 | (view_convert @0)) | |
1596 | ||
1597 | /* For integral conversions with the same precision or pointer | |
1598 | conversions use a NOP_EXPR instead. */ | |
1599 | (simplify | |
1600 | (view_convert @0) | |
1601 | (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)) | |
1602 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
1603 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))) | |
1604 | (convert @0))) | |
1605 | ||
1606 | /* Strip inner integral conversions that do not change precision or size. */ | |
1607 | (simplify | |
1608 | (view_convert (convert@0 @1)) | |
1609 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
1610 | && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
1611 | && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
1612 | && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1)))) | |
1613 | (view_convert @1))) | |
1614 | ||
1615 | /* Re-association barriers around constants and other re-association | |
1616 | barriers can be removed. */ | |
1617 | (simplify | |
1618 | (paren CONSTANT_CLASS_P@0) | |
1619 | @0) | |
1620 | (simplify | |
1621 | (paren (paren@1 @0)) | |
1622 | @1) | |
1e51d0a2 RB |
1623 | |
1624 | /* Handle cases of two conversions in a row. */ | |
1625 | (for ocvt (convert float fix_trunc) | |
1626 | (for icvt (convert float) | |
1627 | (simplify | |
1628 | (ocvt (icvt@1 @0)) | |
1629 | (with | |
1630 | { | |
1631 | tree inside_type = TREE_TYPE (@0); | |
1632 | tree inter_type = TREE_TYPE (@1); | |
1633 | int inside_int = INTEGRAL_TYPE_P (inside_type); | |
1634 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
1635 | int inside_float = FLOAT_TYPE_P (inside_type); | |
09240451 | 1636 | int inside_vec = VECTOR_TYPE_P (inside_type); |
1e51d0a2 RB |
1637 | unsigned int inside_prec = TYPE_PRECISION (inside_type); |
1638 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
1639 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
1640 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
1641 | int inter_float = FLOAT_TYPE_P (inter_type); | |
09240451 | 1642 | int inter_vec = VECTOR_TYPE_P (inter_type); |
1e51d0a2 RB |
1643 | unsigned int inter_prec = TYPE_PRECISION (inter_type); |
1644 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
1645 | int final_int = INTEGRAL_TYPE_P (type); | |
1646 | int final_ptr = POINTER_TYPE_P (type); | |
1647 | int final_float = FLOAT_TYPE_P (type); | |
09240451 | 1648 | int final_vec = VECTOR_TYPE_P (type); |
1e51d0a2 RB |
1649 | unsigned int final_prec = TYPE_PRECISION (type); |
1650 | int final_unsignedp = TYPE_UNSIGNED (type); | |
1651 | } | |
64d3a1f0 RB |
1652 | (switch |
1653 | /* In addition to the cases of two conversions in a row | |
1654 | handled below, if we are converting something to its own | |
1655 | type via an object of identical or wider precision, neither | |
1656 | conversion is needed. */ | |
1657 | (if (((GIMPLE && useless_type_conversion_p (type, inside_type)) | |
1658 | || (GENERIC | |
1659 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type))) | |
1660 | && (((inter_int || inter_ptr) && final_int) | |
1661 | || (inter_float && final_float)) | |
1662 | && inter_prec >= final_prec) | |
1663 | (ocvt @0)) | |
1664 | ||
1665 | /* Likewise, if the intermediate and initial types are either both | |
1666 | float or both integer, we don't need the middle conversion if the | |
1667 | former is wider than the latter and doesn't change the signedness | |
1668 | (for integers). Avoid this if the final type is a pointer since | |
36088299 | 1669 | then we sometimes need the middle conversion. */ |
64d3a1f0 RB |
1670 | (if (((inter_int && inside_int) || (inter_float && inside_float)) |
1671 | && (final_int || final_float) | |
1672 | && inter_prec >= inside_prec | |
36088299 | 1673 | && (inter_float || inter_unsignedp == inside_unsignedp)) |
64d3a1f0 RB |
1674 | (ocvt @0)) |
1675 | ||
1676 | /* If we have a sign-extension of a zero-extended value, we can | |
1677 | replace that by a single zero-extension. Likewise if the | |
1678 | final conversion does not change precision we can drop the | |
1679 | intermediate conversion. */ | |
1680 | (if (inside_int && inter_int && final_int | |
1681 | && ((inside_prec < inter_prec && inter_prec < final_prec | |
1682 | && inside_unsignedp && !inter_unsignedp) | |
1683 | || final_prec == inter_prec)) | |
1684 | (ocvt @0)) | |
1685 | ||
1686 | /* Two conversions in a row are not needed unless: | |
1e51d0a2 RB |
1687 | - some conversion is floating-point (overstrict for now), or |
1688 | - some conversion is a vector (overstrict for now), or | |
1689 | - the intermediate type is narrower than both initial and | |
1690 | final, or | |
1691 | - the intermediate type and innermost type differ in signedness, | |
1692 | and the outermost type is wider than the intermediate, or | |
1693 | - the initial type is a pointer type and the precisions of the | |
1694 | intermediate and final types differ, or | |
1695 | - the final type is a pointer type and the precisions of the | |
1696 | initial and intermediate types differ. */ | |
64d3a1f0 RB |
1697 | (if (! inside_float && ! inter_float && ! final_float |
1698 | && ! inside_vec && ! inter_vec && ! final_vec | |
1699 | && (inter_prec >= inside_prec || inter_prec >= final_prec) | |
1700 | && ! (inside_int && inter_int | |
1701 | && inter_unsignedp != inside_unsignedp | |
1702 | && inter_prec < final_prec) | |
1703 | && ((inter_unsignedp && inter_prec > inside_prec) | |
1704 | == (final_unsignedp && final_prec > inter_prec)) | |
1705 | && ! (inside_ptr && inter_prec != final_prec) | |
36088299 | 1706 | && ! (final_ptr && inside_prec != inter_prec)) |
64d3a1f0 RB |
1707 | (ocvt @0)) |
1708 | ||
1709 | /* A truncation to an unsigned type (a zero-extension) should be | |
1710 | canonicalized as bitwise and of a mask. */ | |
1d510e04 JJ |
1711 | (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */ |
1712 | && final_int && inter_int && inside_int | |
64d3a1f0 RB |
1713 | && final_prec == inside_prec |
1714 | && final_prec > inter_prec | |
1715 | && inter_unsignedp) | |
1716 | (convert (bit_and @0 { wide_int_to_tree | |
1717 | (inside_type, | |
1718 | wi::mask (inter_prec, false, | |
1719 | TYPE_PRECISION (inside_type))); }))) | |
1720 | ||
1721 | /* If we are converting an integer to a floating-point that can | |
1722 | represent it exactly and back to an integer, we can skip the | |
1723 | floating-point conversion. */ | |
1724 | (if (GIMPLE /* PR66211 */ | |
1725 | && inside_int && inter_float && final_int && | |
1726 | (unsigned) significand_size (TYPE_MODE (inter_type)) | |
1727 | >= inside_prec - !inside_unsignedp) | |
1728 | (convert @0))))))) | |
ea2042ba RB |
1729 | |
1730 | /* If we have a narrowing conversion to an integral type that is fed by a | |
1731 | BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely | |
1732 | masks off bits outside the final type (and nothing else). */ | |
1733 | (simplify | |
1734 | (convert (bit_and @0 INTEGER_CST@1)) | |
1735 | (if (INTEGRAL_TYPE_P (type) | |
1736 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1737 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) | |
1738 | && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1), | |
1739 | TYPE_PRECISION (type)), 0)) | |
1740 | (convert @0))) | |
a25454ea RB |
1741 | |
1742 | ||
1743 | /* (X /[ex] A) * A -> X. */ | |
1744 | (simplify | |
1745 | (mult (convert? (exact_div @0 @1)) @1) | |
1746 | /* Look through a sign-changing conversion. */ | |
257b01ba | 1747 | (convert @0)) |
eaeba53a | 1748 | |
a7f24614 RB |
1749 | /* Canonicalization of binary operations. */ |
1750 | ||
1751 | /* Convert X + -C into X - C. */ | |
1752 | (simplify | |
1753 | (plus @0 REAL_CST@1) | |
1754 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
23f27839 | 1755 | (with { tree tem = const_unop (NEGATE_EXPR, type, @1); } |
a7f24614 RB |
1756 | (if (!TREE_OVERFLOW (tem) || !flag_trapping_math) |
1757 | (minus @0 { tem; }))))) | |
1758 | ||
6b6aa8d3 | 1759 | /* Convert x+x into x*2. */ |
a7f24614 RB |
1760 | (simplify |
1761 | (plus @0 @0) | |
1762 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
6b6aa8d3 MG |
1763 | (mult @0 { build_real (type, dconst2); }) |
1764 | (if (INTEGRAL_TYPE_P (type)) | |
1765 | (mult @0 { build_int_cst (type, 2); })))) | |
a7f24614 RB |
1766 | |
1767 | (simplify | |
1768 | (minus integer_zerop @1) | |
1769 | (negate @1)) | |
1770 | ||
1771 | /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether | |
1772 | ARG0 is zero and X + ARG0 reduces to X, since that would mean | |
1773 | (-ARG1 + ARG0) reduces to -ARG1. */ | |
1774 | (simplify | |
1775 | (minus real_zerop@0 @1) | |
1776 | (if (fold_real_zero_addition_p (type, @0, 0)) | |
1777 | (negate @1))) | |
1778 | ||
1779 | /* Transform x * -1 into -x. */ | |
1780 | (simplify | |
1781 | (mult @0 integer_minus_onep) | |
1782 | (negate @0)) | |
eaeba53a | 1783 | |
96285749 RS |
1784 | /* True if we can easily extract the real and imaginary parts of a complex |
1785 | number. */ | |
1786 | (match compositional_complex | |
1787 | (convert? (complex @0 @1))) | |
1788 | ||
eaeba53a RB |
1789 | /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */ |
1790 | (simplify | |
1791 | (complex (realpart @0) (imagpart @0)) | |
1792 | @0) | |
1793 | (simplify | |
1794 | (realpart (complex @0 @1)) | |
1795 | @0) | |
1796 | (simplify | |
1797 | (imagpart (complex @0 @1)) | |
1798 | @1) | |
83633539 | 1799 | |
77c028c5 MG |
1800 | /* Sometimes we only care about half of a complex expression. */ |
1801 | (simplify | |
1802 | (realpart (convert?:s (conj:s @0))) | |
1803 | (convert (realpart @0))) | |
1804 | (simplify | |
1805 | (imagpart (convert?:s (conj:s @0))) | |
1806 | (convert (negate (imagpart @0)))) | |
1807 | (for part (realpart imagpart) | |
1808 | (for op (plus minus) | |
1809 | (simplify | |
1810 | (part (convert?:s@2 (op:s @0 @1))) | |
1811 | (convert (op (part @0) (part @1)))))) | |
1812 | (simplify | |
1813 | (realpart (convert?:s (CEXPI:s @0))) | |
1814 | (convert (COS @0))) | |
1815 | (simplify | |
1816 | (imagpart (convert?:s (CEXPI:s @0))) | |
1817 | (convert (SIN @0))) | |
1818 | ||
1819 | /* conj(conj(x)) -> x */ | |
1820 | (simplify | |
1821 | (conj (convert? (conj @0))) | |
1822 | (if (tree_nop_conversion_p (TREE_TYPE (@0), type)) | |
1823 | (convert @0))) | |
1824 | ||
1825 | /* conj({x,y}) -> {x,-y} */ | |
1826 | (simplify | |
1827 | (conj (convert?:s (complex:s @0 @1))) | |
1828 | (with { tree itype = TREE_TYPE (type); } | |
1829 | (complex (convert:itype @0) (negate (convert:itype @1))))) | |
83633539 RB |
1830 | |
1831 | /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */ | |
1832 | (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64) | |
1833 | (simplify | |
1834 | (bswap (bswap @0)) | |
1835 | @0) | |
1836 | (simplify | |
1837 | (bswap (bit_not (bswap @0))) | |
1838 | (bit_not @0)) | |
1839 | (for bitop (bit_xor bit_ior bit_and) | |
1840 | (simplify | |
1841 | (bswap (bitop:c (bswap @0) @1)) | |
1842 | (bitop @0 (bswap @1))))) | |
96994de0 RB |
1843 | |
1844 | ||
1845 | /* Combine COND_EXPRs and VEC_COND_EXPRs. */ | |
1846 | ||
1847 | /* Simplify constant conditions. | |
1848 | Only optimize constant conditions when the selected branch | |
1849 | has the same type as the COND_EXPR. This avoids optimizing | |
1850 | away "c ? x : throw", where the throw has a void type. | |
1851 | Note that we cannot throw away the fold-const.c variant nor | |
1852 | this one as we depend on doing this transform before possibly | |
1853 | A ? B : B -> B triggers and the fold-const.c one can optimize | |
1854 | 0 ? A : B to B even if A has side-effects. Something | |
1855 | genmatch cannot handle. */ | |
1856 | (simplify | |
1857 | (cond INTEGER_CST@0 @1 @2) | |
8fdc6c67 RB |
1858 | (if (integer_zerop (@0)) |
1859 | (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type)) | |
1860 | @2) | |
1861 | (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type)) | |
1862 | @1))) | |
96994de0 RB |
1863 | (simplify |
1864 | (vec_cond VECTOR_CST@0 @1 @2) | |
1865 | (if (integer_all_onesp (@0)) | |
8fdc6c67 RB |
1866 | @1 |
1867 | (if (integer_zerop (@0)) | |
1868 | @2))) | |
96994de0 RB |
1869 | |
1870 | (for cnd (cond vec_cond) | |
1871 | /* A ? B : (A ? X : C) -> A ? B : C. */ | |
1872 | (simplify | |
1873 | (cnd @0 (cnd @0 @1 @2) @3) | |
1874 | (cnd @0 @1 @3)) | |
1875 | (simplify | |
1876 | (cnd @0 @1 (cnd @0 @2 @3)) | |
1877 | (cnd @0 @1 @3)) | |
24a179f8 RB |
1878 | /* A ? B : (!A ? C : X) -> A ? B : C. */ |
1879 | /* ??? This matches embedded conditions open-coded because genmatch | |
1880 | would generate matching code for conditions in separate stmts only. | |
1881 | The following is still important to merge then and else arm cases | |
1882 | from if-conversion. */ | |
1883 | (simplify | |
1884 | (cnd @0 @1 (cnd @2 @3 @4)) | |
1885 | (if (COMPARISON_CLASS_P (@0) | |
1886 | && COMPARISON_CLASS_P (@2) | |
1887 | && invert_tree_comparison | |
1888 | (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2) | |
1889 | && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0) | |
1890 | && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0)) | |
1891 | (cnd @0 @1 @3))) | |
1892 | (simplify | |
1893 | (cnd @0 (cnd @1 @2 @3) @4) | |
1894 | (if (COMPARISON_CLASS_P (@0) | |
1895 | && COMPARISON_CLASS_P (@1) | |
1896 | && invert_tree_comparison | |
1897 | (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1) | |
1898 | && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0) | |
1899 | && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0)) | |
1900 | (cnd @0 @3 @4))) | |
96994de0 RB |
1901 | |
1902 | /* A ? B : B -> B. */ | |
1903 | (simplify | |
1904 | (cnd @0 @1 @1) | |
09240451 | 1905 | @1) |
96994de0 | 1906 | |
09240451 MG |
1907 | /* !A ? B : C -> A ? C : B. */ |
1908 | (simplify | |
1909 | (cnd (logical_inverted_value truth_valued_p@0) @1 @2) | |
1910 | (cnd @0 @2 @1))) | |
f84e7fd6 | 1911 | |
a3ca1bc5 RB |
1912 | /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons |
1913 | return all -1 or all 0 results. */ | |
f43d102e RS |
1914 | /* ??? We could instead convert all instances of the vec_cond to negate, |
1915 | but that isn't necessarily a win on its own. */ | |
1916 | (simplify | |
a3ca1bc5 | 1917 | (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) |
f43d102e | 1918 | (if (VECTOR_TYPE_P (type) |
4d8989d5 | 1919 | && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)) |
f43d102e | 1920 | && (TYPE_MODE (TREE_TYPE (type)) |
4d8989d5 | 1921 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) |
a3ca1bc5 | 1922 | (minus @3 (view_convert (vec_cond @0 (negate @1) @2))))) |
f43d102e | 1923 | |
a3ca1bc5 | 1924 | /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */ |
f43d102e | 1925 | (simplify |
a3ca1bc5 | 1926 | (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) |
f43d102e | 1927 | (if (VECTOR_TYPE_P (type) |
4d8989d5 | 1928 | && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)) |
f43d102e | 1929 | && (TYPE_MODE (TREE_TYPE (type)) |
4d8989d5 | 1930 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) |
a3ca1bc5 | 1931 | (plus @3 (view_convert (vec_cond @0 (negate @1) @2))))) |
f84e7fd6 | 1932 | |
2ee05f1e | 1933 | |
f84e7fd6 RB |
1934 | /* Simplifications of comparisons. */ |
1935 | ||
24f1db9c RB |
1936 | /* See if we can reduce the magnitude of a constant involved in a |
1937 | comparison by changing the comparison code. This is a canonicalization | |
1938 | formerly done by maybe_canonicalize_comparison_1. */ | |
1939 | (for cmp (le gt) | |
1940 | acmp (lt ge) | |
1941 | (simplify | |
1942 | (cmp @0 INTEGER_CST@1) | |
1943 | (if (tree_int_cst_sgn (@1) == -1) | |
1944 | (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); })))) | |
1945 | (for cmp (ge lt) | |
1946 | acmp (gt le) | |
1947 | (simplify | |
1948 | (cmp @0 INTEGER_CST@1) | |
1949 | (if (tree_int_cst_sgn (@1) == 1) | |
1950 | (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); })))) | |
1951 | ||
1952 | ||
f84e7fd6 RB |
1953 | /* We can simplify a logical negation of a comparison to the |
1954 | inverted comparison. As we cannot compute an expression | |
1955 | operator using invert_tree_comparison we have to simulate | |
1956 | that with expression code iteration. */ | |
1957 | (for cmp (tcc_comparison) | |
1958 | icmp (inverted_tcc_comparison) | |
1959 | ncmp (inverted_tcc_comparison_with_nans) | |
1960 | /* Ideally we'd like to combine the following two patterns | |
1961 | and handle some more cases by using | |
1962 | (logical_inverted_value (cmp @0 @1)) | |
1963 | here but for that genmatch would need to "inline" that. | |
1964 | For now implement what forward_propagate_comparison did. */ | |
1965 | (simplify | |
1966 | (bit_not (cmp @0 @1)) | |
1967 | (if (VECTOR_TYPE_P (type) | |
1968 | || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)) | |
1969 | /* Comparison inversion may be impossible for trapping math, | |
1970 | invert_tree_comparison will tell us. But we can't use | |
1971 | a computed operator in the replacement tree thus we have | |
1972 | to play the trick below. */ | |
1973 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 1974 | (cmp, HONOR_NANS (@0)); } |
f84e7fd6 | 1975 | (if (ic == icmp) |
8fdc6c67 RB |
1976 | (icmp @0 @1) |
1977 | (if (ic == ncmp) | |
1978 | (ncmp @0 @1)))))) | |
f84e7fd6 | 1979 | (simplify |
09240451 MG |
1980 | (bit_xor (cmp @0 @1) integer_truep) |
1981 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 1982 | (cmp, HONOR_NANS (@0)); } |
09240451 | 1983 | (if (ic == icmp) |
8fdc6c67 RB |
1984 | (icmp @0 @1) |
1985 | (if (ic == ncmp) | |
1986 | (ncmp @0 @1)))))) | |
e18c1d66 | 1987 | |
2ee05f1e RB |
1988 | /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. |
1989 | ??? The transformation is valid for the other operators if overflow | |
1990 | is undefined for the type, but performing it here badly interacts | |
1991 | with the transformation in fold_cond_expr_with_comparison which | |
1992 | attempts to synthetize ABS_EXPR. */ | |
1993 | (for cmp (eq ne) | |
1994 | (simplify | |
d9ba1961 RB |
1995 | (cmp (minus@2 @0 @1) integer_zerop) |
1996 | (if (single_use (@2)) | |
1997 | (cmp @0 @1)))) | |
2ee05f1e RB |
1998 | |
1999 | /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the | |
2000 | signed arithmetic case. That form is created by the compiler | |
2001 | often enough for folding it to be of value. One example is in | |
2002 | computing loop trip counts after Operator Strength Reduction. */ | |
07cdc2b8 RB |
2003 | (for cmp (simple_comparison) |
2004 | scmp (swapped_simple_comparison) | |
2ee05f1e | 2005 | (simplify |
bc6e9db4 | 2006 | (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2) |
2ee05f1e RB |
2007 | /* Handle unfolded multiplication by zero. */ |
2008 | (if (integer_zerop (@1)) | |
8fdc6c67 RB |
2009 | (cmp @1 @2) |
2010 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
bc6e9db4 RB |
2011 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) |
2012 | && single_use (@3)) | |
8fdc6c67 RB |
2013 | /* If @1 is negative we swap the sense of the comparison. */ |
2014 | (if (tree_int_cst_sgn (@1) < 0) | |
2015 | (scmp @0 @2) | |
2016 | (cmp @0 @2)))))) | |
2ee05f1e RB |
2017 | |
2018 | /* Simplify comparison of something with itself. For IEEE | |
2019 | floating-point, we can only do some of these simplifications. */ | |
287f8f17 | 2020 | (for cmp (eq ge le) |
2ee05f1e RB |
2021 | (simplify |
2022 | (cmp @0 @0) | |
287f8f17 | 2023 | (if (! FLOAT_TYPE_P (TREE_TYPE (@0)) |
b9407883 | 2024 | || ! HONOR_NANS (@0)) |
287f8f17 RB |
2025 | { constant_boolean_node (true, type); } |
2026 | (if (cmp != EQ_EXPR) | |
2027 | (eq @0 @0))))) | |
2ee05f1e RB |
2028 | (for cmp (ne gt lt) |
2029 | (simplify | |
2030 | (cmp @0 @0) | |
2031 | (if (cmp != NE_EXPR | |
2032 | || ! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
b9407883 | 2033 | || ! HONOR_NANS (@0)) |
2ee05f1e | 2034 | { constant_boolean_node (false, type); }))) |
b5d3d787 RB |
2035 | (for cmp (unle unge uneq) |
2036 | (simplify | |
2037 | (cmp @0 @0) | |
2038 | { constant_boolean_node (true, type); })) | |
dd53d197 MG |
2039 | (for cmp (unlt ungt) |
2040 | (simplify | |
2041 | (cmp @0 @0) | |
2042 | (unordered @0 @0))) | |
b5d3d787 RB |
2043 | (simplify |
2044 | (ltgt @0 @0) | |
2045 | (if (!flag_trapping_math) | |
2046 | { constant_boolean_node (false, type); })) | |
2ee05f1e RB |
2047 | |
2048 | /* Fold ~X op ~Y as Y op X. */ | |
07cdc2b8 | 2049 | (for cmp (simple_comparison) |
2ee05f1e | 2050 | (simplify |
7fe996ba RB |
2051 | (cmp (bit_not@2 @0) (bit_not@3 @1)) |
2052 | (if (single_use (@2) && single_use (@3)) | |
2053 | (cmp @1 @0)))) | |
2ee05f1e RB |
2054 | |
2055 | /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ | |
07cdc2b8 RB |
2056 | (for cmp (simple_comparison) |
2057 | scmp (swapped_simple_comparison) | |
2ee05f1e | 2058 | (simplify |
7fe996ba RB |
2059 | (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1) |
2060 | (if (single_use (@2) | |
2061 | && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)) | |
2ee05f1e RB |
2062 | (scmp @0 (bit_not @1))))) |
2063 | ||
07cdc2b8 RB |
2064 | (for cmp (simple_comparison) |
2065 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
2066 | (simplify | |
2067 | (cmp (convert@2 @0) (convert? @1)) | |
2068 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
2069 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
2070 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) | |
2071 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
2072 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))) | |
2073 | (with | |
2074 | { | |
2075 | tree type1 = TREE_TYPE (@1); | |
2076 | if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1)) | |
2077 | { | |
2078 | REAL_VALUE_TYPE orig = TREE_REAL_CST (@1); | |
2079 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node) | |
2080 | && exact_real_truncate (TYPE_MODE (float_type_node), &orig)) | |
2081 | type1 = float_type_node; | |
2082 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node) | |
2083 | && exact_real_truncate (TYPE_MODE (double_type_node), &orig)) | |
2084 | type1 = double_type_node; | |
2085 | } | |
2086 | tree newtype | |
2087 | = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1) | |
2088 | ? TREE_TYPE (@0) : type1); | |
2089 | } | |
2090 | (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype)) | |
2091 | (cmp (convert:newtype @0) (convert:newtype @1)))))) | |
2092 | ||
2093 | (simplify | |
2094 | (cmp @0 REAL_CST@1) | |
2095 | /* IEEE doesn't distinguish +0 and -0 in comparisons. */ | |
64d3a1f0 RB |
2096 | (switch |
2097 | /* a CMP (-0) -> a CMP 0 */ | |
2098 | (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))) | |
2099 | (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })) | |
2100 | /* x != NaN is always true, other ops are always false. */ | |
2101 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
2102 | && ! HONOR_SNANS (@1)) | |
2103 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
2104 | /* Fold comparisons against infinity. */ | |
2105 | (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1)) | |
2106 | && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1)))) | |
2107 | (with | |
2108 | { | |
2109 | REAL_VALUE_TYPE max; | |
2110 | enum tree_code code = cmp; | |
2111 | bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)); | |
2112 | if (neg) | |
2113 | code = swap_tree_comparison (code); | |
2114 | } | |
2115 | (switch | |
2116 | /* x > +Inf is always false, if with ignore sNANs. */ | |
2117 | (if (code == GT_EXPR | |
2118 | && ! HONOR_SNANS (@0)) | |
2119 | { constant_boolean_node (false, type); }) | |
2120 | (if (code == LE_EXPR) | |
2121 | /* x <= +Inf is always true, if we don't case about NaNs. */ | |
2122 | (if (! HONOR_NANS (@0)) | |
2123 | { constant_boolean_node (true, type); } | |
b0eb889b | 2124 | /* x <= +Inf is the same as x == x, i.e. !isnan(x). */ |
64d3a1f0 RB |
2125 | (eq @0 @0))) |
2126 | /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */ | |
2127 | (if (code == EQ_EXPR || code == GE_EXPR) | |
2128 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
2129 | (if (neg) | |
2130 | (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
2131 | (gt @0 { build_real (TREE_TYPE (@0), max); })))) | |
2132 | /* x < +Inf is always equal to x <= DBL_MAX. */ | |
2133 | (if (code == LT_EXPR) | |
2134 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
2135 | (if (neg) | |
2136 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
2137 | (le @0 { build_real (TREE_TYPE (@0), max); })))) | |
2138 | /* x != +Inf is always equal to !(x > DBL_MAX). */ | |
2139 | (if (code == NE_EXPR) | |
2140 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
2141 | (if (! HONOR_NANS (@0)) | |
2142 | (if (neg) | |
2143 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
2144 | (le @0 { build_real (TREE_TYPE (@0), max); })) | |
2145 | (if (neg) | |
2146 | (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
2147 | { build_one_cst (type); }) | |
2148 | (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); }) | |
2149 | { build_one_cst (type); })))))))))) | |
07cdc2b8 RB |
2150 | |
2151 | /* If this is a comparison of a real constant with a PLUS_EXPR | |
2152 | or a MINUS_EXPR of a real constant, we can convert it into a | |
2153 | comparison with a revised real constant as long as no overflow | |
2154 | occurs when unsafe_math_optimizations are enabled. */ | |
2155 | (if (flag_unsafe_math_optimizations) | |
2156 | (for op (plus minus) | |
2157 | (simplify | |
2158 | (cmp (op @0 REAL_CST@1) REAL_CST@2) | |
2159 | (with | |
2160 | { | |
2161 | tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR, | |
2162 | TREE_TYPE (@1), @2, @1); | |
2163 | } | |
f980c9a2 | 2164 | (if (tem && !TREE_OVERFLOW (tem)) |
07cdc2b8 RB |
2165 | (cmp @0 { tem; })))))) |
2166 | ||
2167 | /* Likewise, we can simplify a comparison of a real constant with | |
2168 | a MINUS_EXPR whose first operand is also a real constant, i.e. | |
2169 | (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on | |
2170 | floating-point types only if -fassociative-math is set. */ | |
2171 | (if (flag_associative_math) | |
2172 | (simplify | |
0409237b | 2173 | (cmp (minus REAL_CST@0 @1) REAL_CST@2) |
07cdc2b8 | 2174 | (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); } |
f980c9a2 | 2175 | (if (tem && !TREE_OVERFLOW (tem)) |
07cdc2b8 RB |
2176 | (cmp { tem; } @1))))) |
2177 | ||
2178 | /* Fold comparisons against built-in math functions. */ | |
2179 | (if (flag_unsafe_math_optimizations | |
2180 | && ! flag_errno_math) | |
2181 | (for sq (SQRT) | |
2182 | (simplify | |
2183 | (cmp (sq @0) REAL_CST@1) | |
64d3a1f0 RB |
2184 | (switch |
2185 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
2186 | (switch | |
2187 | /* sqrt(x) < y is always false, if y is negative. */ | |
2188 | (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR) | |
8fdc6c67 | 2189 | { constant_boolean_node (false, type); }) |
64d3a1f0 RB |
2190 | /* sqrt(x) > y is always true, if y is negative and we |
2191 | don't care about NaNs, i.e. negative values of x. */ | |
2192 | (if (cmp == NE_EXPR || !HONOR_NANS (@0)) | |
2193 | { constant_boolean_node (true, type); }) | |
2194 | /* sqrt(x) > y is the same as x >= 0, if y is negative. */ | |
2195 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))) | |
c53233c6 RS |
2196 | (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0)) |
2197 | (switch | |
2198 | /* sqrt(x) < 0 is always false. */ | |
2199 | (if (cmp == LT_EXPR) | |
2200 | { constant_boolean_node (false, type); }) | |
2201 | /* sqrt(x) >= 0 is always true if we don't care about NaNs. */ | |
2202 | (if (cmp == GE_EXPR && !HONOR_NANS (@0)) | |
2203 | { constant_boolean_node (true, type); }) | |
2204 | /* sqrt(x) <= 0 -> x == 0. */ | |
2205 | (if (cmp == LE_EXPR) | |
2206 | (eq @0 @1)) | |
2207 | /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >, | |
2208 | == or !=. In the last case: | |
2209 | ||
2210 | (sqrt(x) != 0) == (NaN != 0) == true == (x != 0) | |
2211 | ||
2212 | if x is negative or NaN. Due to -funsafe-math-optimizations, | |
2213 | the results for other x follow from natural arithmetic. */ | |
2214 | (cmp @0 @1))) | |
64d3a1f0 RB |
2215 | (if (cmp == GT_EXPR || cmp == GE_EXPR) |
2216 | (with | |
2217 | { | |
2218 | REAL_VALUE_TYPE c2; | |
5c88ea94 RS |
2219 | real_arithmetic (&c2, MULT_EXPR, |
2220 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
64d3a1f0 RB |
2221 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); |
2222 | } | |
2223 | (if (REAL_VALUE_ISINF (c2)) | |
2224 | /* sqrt(x) > y is x == +Inf, when y is very large. */ | |
2225 | (if (HONOR_INFINITIES (@0)) | |
2226 | (eq @0 { build_real (TREE_TYPE (@0), c2); }) | |
2227 | { constant_boolean_node (false, type); }) | |
2228 | /* sqrt(x) > c is the same as x > c*c. */ | |
2229 | (cmp @0 { build_real (TREE_TYPE (@0), c2); })))) | |
2230 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
2231 | (with | |
2232 | { | |
2233 | REAL_VALUE_TYPE c2; | |
5c88ea94 RS |
2234 | real_arithmetic (&c2, MULT_EXPR, |
2235 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
64d3a1f0 RB |
2236 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); |
2237 | } | |
2238 | (if (REAL_VALUE_ISINF (c2)) | |
2239 | (switch | |
2240 | /* sqrt(x) < y is always true, when y is a very large | |
2241 | value and we don't care about NaNs or Infinities. */ | |
2242 | (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0)) | |
2243 | { constant_boolean_node (true, type); }) | |
2244 | /* sqrt(x) < y is x != +Inf when y is very large and we | |
2245 | don't care about NaNs. */ | |
2246 | (if (! HONOR_NANS (@0)) | |
2247 | (ne @0 { build_real (TREE_TYPE (@0), c2); })) | |
2248 | /* sqrt(x) < y is x >= 0 when y is very large and we | |
2249 | don't care about Infinities. */ | |
2250 | (if (! HONOR_INFINITIES (@0)) | |
2251 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) | |
2252 | /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ | |
2253 | (if (GENERIC) | |
2254 | (truth_andif | |
2255 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
2256 | (ne @0 { build_real (TREE_TYPE (@0), c2); })))) | |
2257 | /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ | |
2258 | (if (! HONOR_NANS (@0)) | |
2259 | (cmp @0 { build_real (TREE_TYPE (@0), c2); }) | |
2260 | /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ | |
2261 | (if (GENERIC) | |
2262 | (truth_andif | |
2263 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
2264 | (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))))) | |
2ee05f1e | 2265 | |
cfdc4f33 MG |
2266 | /* Unordered tests if either argument is a NaN. */ |
2267 | (simplify | |
2268 | (bit_ior (unordered @0 @0) (unordered @1 @1)) | |
aea417d7 | 2269 | (if (types_match (@0, @1)) |
cfdc4f33 | 2270 | (unordered @0 @1))) |
257b01ba MG |
2271 | (simplify |
2272 | (bit_and (ordered @0 @0) (ordered @1 @1)) | |
2273 | (if (types_match (@0, @1)) | |
2274 | (ordered @0 @1))) | |
cfdc4f33 MG |
2275 | (simplify |
2276 | (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1)) | |
2277 | @2) | |
257b01ba MG |
2278 | (simplify |
2279 | (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1)) | |
2280 | @2) | |
e18c1d66 | 2281 | |
90c6f26c RB |
2282 | /* Simple range test simplifications. */ |
2283 | /* A < B || A >= B -> true. */ | |
5d30c58d RB |
2284 | (for test1 (lt le le le ne ge) |
2285 | test2 (ge gt ge ne eq ne) | |
90c6f26c RB |
2286 | (simplify |
2287 | (bit_ior:c (test1 @0 @1) (test2 @0 @1)) | |
2288 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2289 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) | |
2290 | { constant_boolean_node (true, type); }))) | |
2291 | /* A < B && A >= B -> false. */ | |
2292 | (for test1 (lt lt lt le ne eq) | |
2293 | test2 (ge gt eq gt eq gt) | |
2294 | (simplify | |
2295 | (bit_and:c (test1 @0 @1) (test2 @0 @1)) | |
2296 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2297 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) | |
2298 | { constant_boolean_node (false, type); }))) | |
2299 | ||
534bd33b MG |
2300 | /* -A CMP -B -> B CMP A. */ |
2301 | (for cmp (tcc_comparison) | |
2302 | scmp (swapped_tcc_comparison) | |
2303 | (simplify | |
2304 | (cmp (negate @0) (negate @1)) | |
2305 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
2306 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2307 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
2308 | (scmp @0 @1))) | |
2309 | (simplify | |
2310 | (cmp (negate @0) CONSTANT_CLASS_P@1) | |
2311 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
2312 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2313 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
23f27839 | 2314 | (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); } |
534bd33b MG |
2315 | (if (tem && !TREE_OVERFLOW (tem)) |
2316 | (scmp @0 { tem; })))))) | |
2317 | ||
b0eb889b MG |
2318 | /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */ |
2319 | (for op (eq ne) | |
2320 | (simplify | |
2321 | (op (abs @0) zerop@1) | |
2322 | (op @0 @1))) | |
2323 | ||
79d4f7c6 RB |
2324 | /* From fold_sign_changed_comparison and fold_widened_comparison. */ |
2325 | (for cmp (simple_comparison) | |
2326 | (simplify | |
2327 | (cmp (convert@0 @00) (convert?@1 @10)) | |
452ec2a5 | 2328 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
79d4f7c6 RB |
2329 | /* Disable this optimization if we're casting a function pointer |
2330 | type on targets that require function pointer canonicalization. */ | |
2331 | && !(targetm.have_canonicalize_funcptr_for_compare () | |
2332 | && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE | |
2fde61e3 RB |
2333 | && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE) |
2334 | && single_use (@0)) | |
79d4f7c6 RB |
2335 | (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0)) |
2336 | && (TREE_CODE (@10) == INTEGER_CST | |
2337 | || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00)))) | |
2338 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0)) | |
2339 | || cmp == NE_EXPR | |
2340 | || cmp == EQ_EXPR) | |
2341 | && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0)))) | |
2342 | /* ??? The special-casing of INTEGER_CST conversion was in the original | |
2343 | code and here to avoid a spurious overflow flag on the resulting | |
2344 | constant which fold_convert produces. */ | |
2345 | (if (TREE_CODE (@1) == INTEGER_CST) | |
2346 | (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0, | |
2347 | TREE_OVERFLOW (@1)); }) | |
2348 | (cmp @00 (convert @1))) | |
2349 | ||
2350 | (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00))) | |
2351 | /* If possible, express the comparison in the shorter mode. */ | |
2352 | (if ((cmp == EQ_EXPR || cmp == NE_EXPR | |
2353 | || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))) | |
2354 | && (types_match (TREE_TYPE (@10), TREE_TYPE (@00)) | |
2355 | || ((TYPE_PRECISION (TREE_TYPE (@00)) | |
2356 | >= TYPE_PRECISION (TREE_TYPE (@10))) | |
2357 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) | |
2358 | == TYPE_UNSIGNED (TREE_TYPE (@10)))) | |
2359 | || (TREE_CODE (@10) == INTEGER_CST | |
f6c15759 | 2360 | && INTEGRAL_TYPE_P (TREE_TYPE (@00)) |
79d4f7c6 RB |
2361 | && int_fits_type_p (@10, TREE_TYPE (@00))))) |
2362 | (cmp @00 (convert @10)) | |
2363 | (if (TREE_CODE (@10) == INTEGER_CST | |
f6c15759 | 2364 | && INTEGRAL_TYPE_P (TREE_TYPE (@00)) |
79d4f7c6 RB |
2365 | && !int_fits_type_p (@10, TREE_TYPE (@00))) |
2366 | (with | |
2367 | { | |
2368 | tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
2369 | tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
2370 | bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10)); | |
2371 | bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min)); | |
2372 | } | |
2373 | (if (above || below) | |
2374 | (if (cmp == EQ_EXPR || cmp == NE_EXPR) | |
2375 | { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); } | |
2376 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
2377 | { constant_boolean_node (above ? true : false, type); } | |
2378 | (if (cmp == GT_EXPR || cmp == GE_EXPR) | |
2379 | { constant_boolean_node (above ? false : true, type); })))))))))))) | |
66e1cacf | 2380 | |
96a111a3 RB |
2381 | (for cmp (eq ne) |
2382 | /* A local variable can never be pointed to by | |
2383 | the default SSA name of an incoming parameter. | |
2384 | SSA names are canonicalized to 2nd place. */ | |
2385 | (simplify | |
2386 | (cmp addr@0 SSA_NAME@1) | |
2387 | (if (SSA_NAME_IS_DEFAULT_DEF (@1) | |
2388 | && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL) | |
2389 | (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); } | |
2390 | (if (TREE_CODE (base) == VAR_DECL | |
2391 | && auto_var_in_fn_p (base, current_function_decl)) | |
2392 | (if (cmp == NE_EXPR) | |
2393 | { constant_boolean_node (true, type); } | |
2394 | { constant_boolean_node (false, type); })))))) | |
2395 | ||
66e1cacf RB |
2396 | /* Equality compare simplifications from fold_binary */ |
2397 | (for cmp (eq ne) | |
2398 | ||
2399 | /* If we have (A | C) == D where C & ~D != 0, convert this into 0. | |
2400 | Similarly for NE_EXPR. */ | |
2401 | (simplify | |
2402 | (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2) | |
2403 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
2404 | && wi::bit_and_not (@1, @2) != 0) | |
2405 | { constant_boolean_node (cmp == NE_EXPR, type); })) | |
2406 | ||
2407 | /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ | |
2408 | (simplify | |
2409 | (cmp (bit_xor @0 @1) integer_zerop) | |
2410 | (cmp @0 @1)) | |
2411 | ||
2412 | /* (X ^ Y) == Y becomes X == 0. | |
2413 | Likewise (X ^ Y) == X becomes Y == 0. */ | |
2414 | (simplify | |
99e943a2 | 2415 | (cmp:c (bit_xor:c @0 @1) @0) |
66e1cacf RB |
2416 | (cmp @1 { build_zero_cst (TREE_TYPE (@1)); })) |
2417 | ||
2418 | /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ | |
2419 | (simplify | |
2420 | (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2) | |
2421 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))) | |
d057c866 | 2422 | (cmp @0 (bit_xor @1 (convert @2))))) |
d057c866 RB |
2423 | |
2424 | (simplify | |
2425 | (cmp (convert? addr@0) integer_zerop) | |
2426 | (if (tree_single_nonzero_warnv_p (@0, NULL)) | |
2427 | { constant_boolean_node (cmp == NE_EXPR, type); }))) | |
2428 | ||
b0eb889b MG |
2429 | /* If we have (A & C) == C where C is a power of 2, convert this into |
2430 | (A & C) != 0. Similarly for NE_EXPR. */ | |
2431 | (for cmp (eq ne) | |
2432 | icmp (ne eq) | |
2433 | (simplify | |
2434 | (cmp (bit_and@2 @0 integer_pow2p@1) @1) | |
2435 | (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) | |
2436 | ||
2437 | /* If we have (A & C) != 0 where C is the sign bit of A, convert | |
2438 | this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ | |
2439 | (for cmp (eq ne) | |
2440 | ncmp (ge lt) | |
2441 | (simplify | |
2442 | (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop) | |
2443 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2444 | && (TYPE_PRECISION (TREE_TYPE (@0)) | |
2445 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) | |
2446 | && element_precision (@2) >= element_precision (@0) | |
2447 | && wi::only_sign_bit_p (@1, element_precision (@0))) | |
2448 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } | |
2449 | (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) | |
2450 | ||
68aba1f6 RB |
2451 | /* When the addresses are not directly of decls compare base and offset. |
2452 | This implements some remaining parts of fold_comparison address | |
2453 | comparisons but still no complete part of it. Still it is good | |
2454 | enough to make fold_stmt not regress when not dispatching to fold_binary. */ | |
2455 | (for cmp (simple_comparison) | |
2456 | (simplify | |
f501d5cd | 2457 | (cmp (convert1?@2 addr@0) (convert2? addr@1)) |
68aba1f6 RB |
2458 | (with |
2459 | { | |
2460 | HOST_WIDE_INT off0, off1; | |
2461 | tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0); | |
2462 | tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1); | |
2463 | if (base0 && TREE_CODE (base0) == MEM_REF) | |
2464 | { | |
2465 | off0 += mem_ref_offset (base0).to_short_addr (); | |
2466 | base0 = TREE_OPERAND (base0, 0); | |
2467 | } | |
2468 | if (base1 && TREE_CODE (base1) == MEM_REF) | |
2469 | { | |
2470 | off1 += mem_ref_offset (base1).to_short_addr (); | |
2471 | base1 = TREE_OPERAND (base1, 0); | |
2472 | } | |
2473 | } | |
da571fda RB |
2474 | (if (base0 && base1) |
2475 | (with | |
2476 | { | |
aad88aed | 2477 | int equal = 2; |
da571fda RB |
2478 | if (decl_in_symtab_p (base0) |
2479 | && decl_in_symtab_p (base1)) | |
2480 | equal = symtab_node::get_create (base0) | |
2481 | ->equal_address_to (symtab_node::get_create (base1)); | |
c3bea076 RB |
2482 | else if ((DECL_P (base0) |
2483 | || TREE_CODE (base0) == SSA_NAME | |
2484 | || TREE_CODE (base0) == STRING_CST) | |
2485 | && (DECL_P (base1) | |
2486 | || TREE_CODE (base1) == SSA_NAME | |
2487 | || TREE_CODE (base1) == STRING_CST)) | |
aad88aed | 2488 | equal = (base0 == base1); |
da571fda RB |
2489 | } |
2490 | (if (equal == 1 | |
2491 | && (cmp == EQ_EXPR || cmp == NE_EXPR | |
2492 | /* If the offsets are equal we can ignore overflow. */ | |
2493 | || off0 == off1 | |
2494 | || POINTER_TYPE_OVERFLOW_UNDEFINED | |
c3bea076 | 2495 | /* Or if we compare using pointers to decls or strings. */ |
da571fda | 2496 | || (POINTER_TYPE_P (TREE_TYPE (@2)) |
c3bea076 | 2497 | && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))) |
da571fda RB |
2498 | (switch |
2499 | (if (cmp == EQ_EXPR) | |
2500 | { constant_boolean_node (off0 == off1, type); }) | |
2501 | (if (cmp == NE_EXPR) | |
2502 | { constant_boolean_node (off0 != off1, type); }) | |
2503 | (if (cmp == LT_EXPR) | |
2504 | { constant_boolean_node (off0 < off1, type); }) | |
2505 | (if (cmp == LE_EXPR) | |
2506 | { constant_boolean_node (off0 <= off1, type); }) | |
2507 | (if (cmp == GE_EXPR) | |
2508 | { constant_boolean_node (off0 >= off1, type); }) | |
2509 | (if (cmp == GT_EXPR) | |
2510 | { constant_boolean_node (off0 > off1, type); })) | |
2511 | (if (equal == 0 | |
2512 | && DECL_P (base0) && DECL_P (base1) | |
2513 | /* If we compare this as integers require equal offset. */ | |
2514 | && (!INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
2515 | || off0 == off1)) | |
2516 | (switch | |
2517 | (if (cmp == EQ_EXPR) | |
2518 | { constant_boolean_node (false, type); }) | |
2519 | (if (cmp == NE_EXPR) | |
2520 | { constant_boolean_node (true, type); }))))))))) | |
66e1cacf | 2521 | |
98998245 RB |
2522 | /* Simplify pointer equality compares using PTA. */ |
2523 | (for neeq (ne eq) | |
2524 | (simplify | |
2525 | (neeq @0 @1) | |
2526 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
2527 | && ptrs_compare_unequal (@0, @1)) | |
2528 | { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; }))) | |
2529 | ||
8f63caf6 | 2530 | /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST. |
467719fb PK |
2531 | and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST. |
2532 | Disable the transform if either operand is pointer to function. | |
2533 | This broke pr22051-2.c for arm where function pointer | |
2534 | canonicalizaion is not wanted. */ | |
2535 | ||
8f63caf6 RB |
2536 | (for cmp (ne eq) |
2537 | (simplify | |
2538 | (cmp (convert @0) INTEGER_CST@1) | |
467719fb PK |
2539 | (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0))) |
2540 | && INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
2541 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1)) | |
2542 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1))))) | |
8f63caf6 RB |
2543 | (cmp @0 (convert @1))))) |
2544 | ||
21aacde4 RB |
2545 | /* Non-equality compare simplifications from fold_binary */ |
2546 | (for cmp (lt gt le ge) | |
2547 | /* Comparisons with the highest or lowest possible integer of | |
2548 | the specified precision will have known values. */ | |
2549 | (simplify | |
2550 | (cmp (convert?@2 @0) INTEGER_CST@1) | |
2551 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
2552 | && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))) | |
2553 | (with | |
2554 | { | |
2555 | tree arg1_type = TREE_TYPE (@1); | |
2556 | unsigned int prec = TYPE_PRECISION (arg1_type); | |
2557 | wide_int max = wi::max_value (arg1_type); | |
2558 | wide_int signed_max = wi::max_value (prec, SIGNED); | |
2559 | wide_int min = wi::min_value (arg1_type); | |
2560 | } | |
2561 | (switch | |
2562 | (if (wi::eq_p (@1, max)) | |
2563 | (switch | |
2564 | (if (cmp == GT_EXPR) | |
2565 | { constant_boolean_node (false, type); }) | |
2566 | (if (cmp == GE_EXPR) | |
2567 | (eq @2 @1)) | |
2568 | (if (cmp == LE_EXPR) | |
2569 | { constant_boolean_node (true, type); }) | |
2570 | (if (cmp == LT_EXPR) | |
2571 | (ne @2 @1)))) | |
21aacde4 RB |
2572 | (if (wi::eq_p (@1, min)) |
2573 | (switch | |
2574 | (if (cmp == LT_EXPR) | |
2575 | { constant_boolean_node (false, type); }) | |
2576 | (if (cmp == LE_EXPR) | |
2577 | (eq @2 @1)) | |
2578 | (if (cmp == GE_EXPR) | |
2579 | { constant_boolean_node (true, type); }) | |
2580 | (if (cmp == GT_EXPR) | |
2581 | (ne @2 @1)))) | |
9bc22d19 RB |
2582 | (if (wi::eq_p (@1, max - 1)) |
2583 | (switch | |
2584 | (if (cmp == GT_EXPR) | |
2585 | (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); })) | |
2586 | (if (cmp == LE_EXPR) | |
2587 | (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); })))) | |
21aacde4 RB |
2588 | (if (wi::eq_p (@1, min + 1)) |
2589 | (switch | |
2590 | (if (cmp == GE_EXPR) | |
2591 | (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); })) | |
2592 | (if (cmp == LT_EXPR) | |
2593 | (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); })))) | |
2594 | (if (wi::eq_p (@1, signed_max) | |
2595 | && TYPE_UNSIGNED (arg1_type) | |
2596 | /* We will flip the signedness of the comparison operator | |
2597 | associated with the mode of @1, so the sign bit is | |
2598 | specified by this mode. Check that @1 is the signed | |
2599 | max associated with this sign bit. */ | |
2600 | && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type)) | |
2601 | /* signed_type does not work on pointer types. */ | |
2602 | && INTEGRAL_TYPE_P (arg1_type)) | |
2603 | /* The following case also applies to X < signed_max+1 | |
2604 | and X >= signed_max+1 because previous transformations. */ | |
2605 | (if (cmp == LE_EXPR || cmp == GT_EXPR) | |
2606 | (with { tree st = signed_type_for (arg1_type); } | |
2607 | (if (cmp == LE_EXPR) | |
2608 | (ge (convert:st @0) { build_zero_cst (st); }) | |
2609 | (lt (convert:st @0) { build_zero_cst (st); })))))))))) | |
2610 | ||
b5d3d787 RB |
2611 | (for cmp (unordered ordered unlt unle ungt unge uneq ltgt) |
2612 | /* If the second operand is NaN, the result is constant. */ | |
2613 | (simplify | |
2614 | (cmp @0 REAL_CST@1) | |
2615 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
2616 | && (cmp != LTGT_EXPR || ! flag_trapping_math)) | |
50301115 | 2617 | { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR |
b5d3d787 | 2618 | ? false : true, type); }))) |
21aacde4 | 2619 | |
55cf3946 RB |
2620 | /* bool_var != 0 becomes bool_var. */ |
2621 | (simplify | |
b5d3d787 | 2622 | (ne @0 integer_zerop) |
55cf3946 RB |
2623 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
2624 | && types_match (type, TREE_TYPE (@0))) | |
2625 | (non_lvalue @0))) | |
2626 | /* bool_var == 1 becomes bool_var. */ | |
2627 | (simplify | |
b5d3d787 | 2628 | (eq @0 integer_onep) |
55cf3946 RB |
2629 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
2630 | && types_match (type, TREE_TYPE (@0))) | |
2631 | (non_lvalue @0))) | |
b5d3d787 RB |
2632 | /* Do not handle |
2633 | bool_var == 0 becomes !bool_var or | |
2634 | bool_var != 1 becomes !bool_var | |
2635 | here because that only is good in assignment context as long | |
2636 | as we require a tcc_comparison in GIMPLE_CONDs where we'd | |
2637 | replace if (x == 0) with tem = ~x; if (tem != 0) which is | |
2638 | clearly less optimal and which we'll transform again in forwprop. */ | |
55cf3946 | 2639 | |
ca1206be MG |
2640 | /* When one argument is a constant, overflow detection can be simplified. |
2641 | Currently restricted to single use so as not to interfere too much with | |
2642 | ADD_OVERFLOW detection in tree-ssa-math-opts.c. | |
2643 | A + CST CMP A -> A CMP' CST' */ | |
2644 | (for cmp (lt le ge gt) | |
2645 | out (gt gt le le) | |
2646 | (simplify | |
a8e9f9a3 | 2647 | (cmp:c (plus@2 @0 INTEGER_CST@1) @0) |
ca1206be MG |
2648 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
2649 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) | |
2650 | && wi::ne_p (@1, 0) | |
2651 | && single_use (@2)) | |
2652 | (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value | |
2653 | (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); })))) | |
2654 | ||
3563f78f MG |
2655 | /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A. |
2656 | However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c | |
2657 | expects the long form, so we restrict the transformation for now. */ | |
2658 | (for cmp (gt le) | |
2659 | (simplify | |
a8e9f9a3 | 2660 | (cmp:c (minus@2 @0 @1) @0) |
3563f78f MG |
2661 | (if (single_use (@2) |
2662 | && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2663 | && TYPE_UNSIGNED (TREE_TYPE (@0)) | |
2664 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
2665 | (cmp @1 @0)))) | |
3563f78f MG |
2666 | |
2667 | /* Testing for overflow is unnecessary if we already know the result. */ | |
3563f78f MG |
2668 | /* A - B > A */ |
2669 | (for cmp (gt le) | |
2670 | out (ne eq) | |
2671 | (simplify | |
a8e9f9a3 | 2672 | (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0) |
3563f78f MG |
2673 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
2674 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
2675 | (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) | |
2676 | /* A + B < A */ | |
2677 | (for cmp (lt ge) | |
2678 | out (ne eq) | |
2679 | (simplify | |
a8e9f9a3 | 2680 | (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0) |
3563f78f MG |
2681 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
2682 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
2683 | (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) | |
2684 | ||
603aeb87 | 2685 | /* For unsigned operands, -1 / B < A checks whether A * B would overflow. |
0557293f | 2686 | Simplify it to __builtin_mul_overflow (A, B, <unused>). */ |
0557293f AM |
2687 | (for cmp (lt ge) |
2688 | out (ne eq) | |
2689 | (simplify | |
603aeb87 | 2690 | (cmp:c (trunc_div:s integer_all_onesp @1) @0) |
0557293f AM |
2691 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0))) |
2692 | (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); } | |
2693 | (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); }))))) | |
55cf3946 | 2694 | |
53f3cd25 RS |
2695 | /* Simplification of math builtins. These rules must all be optimizations |
2696 | as well as IL simplifications. If there is a possibility that the new | |
2697 | form could be a pessimization, the rule should go in the canonicalization | |
2698 | section that follows this one. | |
e18c1d66 | 2699 | |
53f3cd25 RS |
2700 | Rules can generally go in this section if they satisfy one of |
2701 | the following: | |
2702 | ||
2703 | - the rule describes an identity | |
2704 | ||
2705 | - the rule replaces calls with something as simple as addition or | |
2706 | multiplication | |
2707 | ||
2708 | - the rule contains unary calls only and simplifies the surrounding | |
2709 | arithmetic. (The idea here is to exclude non-unary calls in which | |
2710 | one operand is constant and in which the call is known to be cheap | |
2711 | when the operand has that value.) */ | |
52c6378a | 2712 | |
53f3cd25 | 2713 | (if (flag_unsafe_math_optimizations) |
52c6378a N |
2714 | /* Simplify sqrt(x) * sqrt(x) -> x. */ |
2715 | (simplify | |
2716 | (mult (SQRT@1 @0) @1) | |
2717 | (if (!HONOR_SNANS (type)) | |
2718 | @0)) | |
2719 | ||
35401640 N |
2720 | /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */ |
2721 | (for root (SQRT CBRT) | |
2722 | (simplify | |
2723 | (mult (root:s @0) (root:s @1)) | |
2724 | (root (mult @0 @1)))) | |
2725 | ||
35401640 N |
2726 | /* Simplify expN(x) * expN(y) -> expN(x+y). */ |
2727 | (for exps (EXP EXP2 EXP10 POW10) | |
2728 | (simplify | |
2729 | (mult (exps:s @0) (exps:s @1)) | |
2730 | (exps (plus @0 @1)))) | |
2731 | ||
52c6378a | 2732 | /* Simplify a/root(b/c) into a*root(c/b). */ |
35401640 N |
2733 | (for root (SQRT CBRT) |
2734 | (simplify | |
2735 | (rdiv @0 (root:s (rdiv:s @1 @2))) | |
2736 | (mult @0 (root (rdiv @2 @1))))) | |
2737 | ||
2738 | /* Simplify x/expN(y) into x*expN(-y). */ | |
2739 | (for exps (EXP EXP2 EXP10 POW10) | |
2740 | (simplify | |
2741 | (rdiv @0 (exps:s @1)) | |
2742 | (mult @0 (exps (negate @1))))) | |
52c6378a | 2743 | |
eee7b6c4 RB |
2744 | (for logs (LOG LOG2 LOG10 LOG10) |
2745 | exps (EXP EXP2 EXP10 POW10) | |
8acda9b2 | 2746 | /* logN(expN(x)) -> x. */ |
e18c1d66 RB |
2747 | (simplify |
2748 | (logs (exps @0)) | |
8acda9b2 RS |
2749 | @0) |
2750 | /* expN(logN(x)) -> x. */ | |
2751 | (simplify | |
2752 | (exps (logs @0)) | |
2753 | @0)) | |
53f3cd25 | 2754 | |
e18c1d66 RB |
2755 | /* Optimize logN(func()) for various exponential functions. We |
2756 | want to determine the value "x" and the power "exponent" in | |
2757 | order to transform logN(x**exponent) into exponent*logN(x). */ | |
eee7b6c4 RB |
2758 | (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10) |
2759 | exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2) | |
e18c1d66 RB |
2760 | (simplify |
2761 | (logs (exps @0)) | |
c9e926ce RS |
2762 | (if (SCALAR_FLOAT_TYPE_P (type)) |
2763 | (with { | |
2764 | tree x; | |
2765 | switch (exps) | |
2766 | { | |
2767 | CASE_CFN_EXP: | |
2768 | /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */ | |
2769 | x = build_real_truncate (type, dconst_e ()); | |
2770 | break; | |
2771 | CASE_CFN_EXP2: | |
2772 | /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */ | |
2773 | x = build_real (type, dconst2); | |
2774 | break; | |
2775 | CASE_CFN_EXP10: | |
2776 | CASE_CFN_POW10: | |
2777 | /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */ | |
2778 | { | |
2779 | REAL_VALUE_TYPE dconst10; | |
2780 | real_from_integer (&dconst10, VOIDmode, 10, SIGNED); | |
2781 | x = build_real (type, dconst10); | |
2782 | } | |
2783 | break; | |
2784 | default: | |
2785 | gcc_unreachable (); | |
2786 | } | |
2787 | } | |
2788 | (mult (logs { x; }) @0))))) | |
53f3cd25 | 2789 | |
e18c1d66 RB |
2790 | (for logs (LOG LOG |
2791 | LOG2 LOG2 | |
2792 | LOG10 LOG10) | |
2793 | exps (SQRT CBRT) | |
2794 | (simplify | |
2795 | (logs (exps @0)) | |
c9e926ce RS |
2796 | (if (SCALAR_FLOAT_TYPE_P (type)) |
2797 | (with { | |
2798 | tree x; | |
2799 | switch (exps) | |
2800 | { | |
2801 | CASE_CFN_SQRT: | |
2802 | /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */ | |
2803 | x = build_real (type, dconsthalf); | |
2804 | break; | |
2805 | CASE_CFN_CBRT: | |
2806 | /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */ | |
2807 | x = build_real_truncate (type, dconst_third ()); | |
2808 | break; | |
2809 | default: | |
2810 | gcc_unreachable (); | |
2811 | } | |
2812 | } | |
2813 | (mult { x; } (logs @0)))))) | |
53f3cd25 RS |
2814 | |
2815 | /* logN(pow(x,exponent)) -> exponent*logN(x). */ | |
e18c1d66 RB |
2816 | (for logs (LOG LOG2 LOG10) |
2817 | pows (POW) | |
2818 | (simplify | |
2819 | (logs (pows @0 @1)) | |
53f3cd25 RS |
2820 | (mult @1 (logs @0)))) |
2821 | ||
2822 | (for sqrts (SQRT) | |
2823 | cbrts (CBRT) | |
b4838d77 | 2824 | pows (POW) |
53f3cd25 RS |
2825 | exps (EXP EXP2 EXP10 POW10) |
2826 | /* sqrt(expN(x)) -> expN(x*0.5). */ | |
2827 | (simplify | |
2828 | (sqrts (exps @0)) | |
2829 | (exps (mult @0 { build_real (type, dconsthalf); }))) | |
2830 | /* cbrt(expN(x)) -> expN(x/3). */ | |
2831 | (simplify | |
2832 | (cbrts (exps @0)) | |
b4838d77 RS |
2833 | (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))) |
2834 | /* pow(expN(x), y) -> expN(x*y). */ | |
2835 | (simplify | |
2836 | (pows (exps @0) @1) | |
2837 | (exps (mult @0 @1)))) | |
cfed37a0 RS |
2838 | |
2839 | /* tan(atan(x)) -> x. */ | |
2840 | (for tans (TAN) | |
2841 | atans (ATAN) | |
2842 | (simplify | |
2843 | (tans (atans @0)) | |
2844 | @0))) | |
53f3cd25 | 2845 | |
abcc43f5 RS |
2846 | /* cabs(x+0i) or cabs(0+xi) -> abs(x). */ |
2847 | (simplify | |
e04d2a35 | 2848 | (CABS (complex:C @0 real_zerop@1)) |
abcc43f5 RS |
2849 | (abs @0)) |
2850 | ||
67dbe582 RS |
2851 | /* trunc(trunc(x)) -> trunc(x), etc. */ |
2852 | (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT) | |
2853 | (simplify | |
2854 | (fns (fns @0)) | |
2855 | (fns @0))) | |
2856 | /* f(x) -> x if x is integer valued and f does nothing for such values. */ | |
afeb246c | 2857 | (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT) |
67dbe582 RS |
2858 | (simplify |
2859 | (fns integer_valued_real_p@0) | |
2860 | @0)) | |
67dbe582 | 2861 | |
4d7836c4 RS |
2862 | /* hypot(x,0) and hypot(0,x) -> abs(x). */ |
2863 | (simplify | |
c9e926ce | 2864 | (HYPOT:c @0 real_zerop@1) |
4d7836c4 RS |
2865 | (abs @0)) |
2866 | ||
b4838d77 RS |
2867 | /* pow(1,x) -> 1. */ |
2868 | (simplify | |
2869 | (POW real_onep@0 @1) | |
2870 | @0) | |
2871 | ||
461e4145 RS |
2872 | (simplify |
2873 | /* copysign(x,x) -> x. */ | |
2874 | (COPYSIGN @0 @0) | |
2875 | @0) | |
2876 | ||
2877 | (simplify | |
2878 | /* copysign(x,y) -> fabs(x) if y is nonnegative. */ | |
2879 | (COPYSIGN @0 tree_expr_nonnegative_p@1) | |
2880 | (abs @0)) | |
2881 | ||
86c0733f RS |
2882 | (for scale (LDEXP SCALBN SCALBLN) |
2883 | /* ldexp(0, x) -> 0. */ | |
2884 | (simplify | |
2885 | (scale real_zerop@0 @1) | |
2886 | @0) | |
2887 | /* ldexp(x, 0) -> x. */ | |
2888 | (simplify | |
2889 | (scale @0 integer_zerop@1) | |
2890 | @0) | |
2891 | /* ldexp(x, y) -> x if x is +-Inf or NaN. */ | |
2892 | (simplify | |
2893 | (scale REAL_CST@0 @1) | |
2894 | (if (!real_isfinite (TREE_REAL_CST_PTR (@0))) | |
2895 | @0))) | |
2896 | ||
53f3cd25 RS |
2897 | /* Canonicalization of sequences of math builtins. These rules represent |
2898 | IL simplifications but are not necessarily optimizations. | |
2899 | ||
2900 | The sincos pass is responsible for picking "optimal" implementations | |
2901 | of math builtins, which may be more complicated and can sometimes go | |
2902 | the other way, e.g. converting pow into a sequence of sqrts. | |
2903 | We only want to do these canonicalizations before the pass has run. */ | |
2904 | ||
2905 | (if (flag_unsafe_math_optimizations && canonicalize_math_p ()) | |
2906 | /* Simplify tan(x) * cos(x) -> sin(x). */ | |
2907 | (simplify | |
2908 | (mult:c (TAN:s @0) (COS:s @0)) | |
2909 | (SIN @0)) | |
2910 | ||
2911 | /* Simplify x * pow(x,c) -> pow(x,c+1). */ | |
2912 | (simplify | |
de3fbea3 | 2913 | (mult:c @0 (POW:s @0 REAL_CST@1)) |
53f3cd25 RS |
2914 | (if (!TREE_OVERFLOW (@1)) |
2915 | (POW @0 (plus @1 { build_one_cst (type); })))) | |
2916 | ||
2917 | /* Simplify sin(x) / cos(x) -> tan(x). */ | |
2918 | (simplify | |
2919 | (rdiv (SIN:s @0) (COS:s @0)) | |
2920 | (TAN @0)) | |
2921 | ||
2922 | /* Simplify cos(x) / sin(x) -> 1 / tan(x). */ | |
2923 | (simplify | |
2924 | (rdiv (COS:s @0) (SIN:s @0)) | |
2925 | (rdiv { build_one_cst (type); } (TAN @0))) | |
2926 | ||
2927 | /* Simplify sin(x) / tan(x) -> cos(x). */ | |
2928 | (simplify | |
2929 | (rdiv (SIN:s @0) (TAN:s @0)) | |
2930 | (if (! HONOR_NANS (@0) | |
2931 | && ! HONOR_INFINITIES (@0)) | |
c9e926ce | 2932 | (COS @0))) |
53f3cd25 RS |
2933 | |
2934 | /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */ | |
2935 | (simplify | |
2936 | (rdiv (TAN:s @0) (SIN:s @0)) | |
2937 | (if (! HONOR_NANS (@0) | |
2938 | && ! HONOR_INFINITIES (@0)) | |
2939 | (rdiv { build_one_cst (type); } (COS @0)))) | |
2940 | ||
2941 | /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */ | |
2942 | (simplify | |
2943 | (mult (POW:s @0 @1) (POW:s @0 @2)) | |
2944 | (POW @0 (plus @1 @2))) | |
2945 | ||
2946 | /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */ | |
2947 | (simplify | |
2948 | (mult (POW:s @0 @1) (POW:s @2 @1)) | |
2949 | (POW (mult @0 @2) @1)) | |
2950 | ||
de3fbea3 RB |
2951 | /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */ |
2952 | (simplify | |
2953 | (mult (POWI:s @0 @1) (POWI:s @2 @1)) | |
2954 | (POWI (mult @0 @2) @1)) | |
2955 | ||
53f3cd25 RS |
2956 | /* Simplify pow(x,c) / x -> pow(x,c-1). */ |
2957 | (simplify | |
2958 | (rdiv (POW:s @0 REAL_CST@1) @0) | |
2959 | (if (!TREE_OVERFLOW (@1)) | |
2960 | (POW @0 (minus @1 { build_one_cst (type); })))) | |
2961 | ||
2962 | /* Simplify x / pow (y,z) -> x * pow(y,-z). */ | |
2963 | (simplify | |
2964 | (rdiv @0 (POW:s @1 @2)) | |
2965 | (mult @0 (POW @1 (negate @2)))) | |
2966 | ||
2967 | (for sqrts (SQRT) | |
2968 | cbrts (CBRT) | |
2969 | pows (POW) | |
2970 | /* sqrt(sqrt(x)) -> pow(x,1/4). */ | |
2971 | (simplify | |
2972 | (sqrts (sqrts @0)) | |
2973 | (pows @0 { build_real (type, dconst_quarter ()); })) | |
2974 | /* sqrt(cbrt(x)) -> pow(x,1/6). */ | |
2975 | (simplify | |
2976 | (sqrts (cbrts @0)) | |
2977 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
2978 | /* cbrt(sqrt(x)) -> pow(x,1/6). */ | |
2979 | (simplify | |
2980 | (cbrts (sqrts @0)) | |
2981 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
2982 | /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */ | |
2983 | (simplify | |
2984 | (cbrts (cbrts tree_expr_nonnegative_p@0)) | |
2985 | (pows @0 { build_real_truncate (type, dconst_ninth ()); })) | |
2986 | /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */ | |
2987 | (simplify | |
2988 | (sqrts (pows @0 @1)) | |
2989 | (pows (abs @0) (mult @1 { build_real (type, dconsthalf); }))) | |
2990 | /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */ | |
2991 | (simplify | |
2992 | (cbrts (pows tree_expr_nonnegative_p@0 @1)) | |
b4838d77 RS |
2993 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) |
2994 | /* pow(sqrt(x),y) -> pow(x,y*0.5). */ | |
2995 | (simplify | |
2996 | (pows (sqrts @0) @1) | |
2997 | (pows @0 (mult @1 { build_real (type, dconsthalf); }))) | |
2998 | /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */ | |
2999 | (simplify | |
3000 | (pows (cbrts tree_expr_nonnegative_p@0) @1) | |
3001 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) | |
3002 | /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */ | |
3003 | (simplify | |
3004 | (pows (pows tree_expr_nonnegative_p@0 @1) @2) | |
3005 | (pows @0 (mult @1 @2)))) | |
abcc43f5 RS |
3006 | |
3007 | /* cabs(x+xi) -> fabs(x)*sqrt(2). */ | |
3008 | (simplify | |
3009 | (CABS (complex @0 @0)) | |
96285749 RS |
3010 | (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) |
3011 | ||
4d7836c4 RS |
3012 | /* hypot(x,x) -> fabs(x)*sqrt(2). */ |
3013 | (simplify | |
3014 | (HYPOT @0 @0) | |
3015 | (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) | |
3016 | ||
96285749 RS |
3017 | /* cexp(x+yi) -> exp(x)*cexpi(y). */ |
3018 | (for cexps (CEXP) | |
3019 | exps (EXP) | |
3020 | cexpis (CEXPI) | |
3021 | (simplify | |
3022 | (cexps compositional_complex@0) | |
3023 | (if (targetm.libc_has_function (function_c99_math_complex)) | |
3024 | (complex | |
3025 | (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0)))) | |
3026 | (mult @1 (imagpart @2))))))) | |
e18c1d66 | 3027 | |
67dbe582 RS |
3028 | (if (canonicalize_math_p ()) |
3029 | /* floor(x) -> trunc(x) if x is nonnegative. */ | |
3030 | (for floors (FLOOR) | |
3031 | truncs (TRUNC) | |
3032 | (simplify | |
3033 | (floors tree_expr_nonnegative_p@0) | |
3034 | (truncs @0)))) | |
3035 | ||
3036 | (match double_value_p | |
3037 | @0 | |
3038 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node))) | |
3039 | (for froms (BUILT_IN_TRUNCL | |
3040 | BUILT_IN_FLOORL | |
3041 | BUILT_IN_CEILL | |
3042 | BUILT_IN_ROUNDL | |
3043 | BUILT_IN_NEARBYINTL | |
3044 | BUILT_IN_RINTL) | |
3045 | tos (BUILT_IN_TRUNC | |
3046 | BUILT_IN_FLOOR | |
3047 | BUILT_IN_CEIL | |
3048 | BUILT_IN_ROUND | |
3049 | BUILT_IN_NEARBYINT | |
3050 | BUILT_IN_RINT) | |
3051 | /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */ | |
3052 | (if (optimize && canonicalize_math_p ()) | |
3053 | (simplify | |
3054 | (froms (convert double_value_p@0)) | |
3055 | (convert (tos @0))))) | |
3056 | ||
3057 | (match float_value_p | |
3058 | @0 | |
3059 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node))) | |
3060 | (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC | |
3061 | BUILT_IN_FLOORL BUILT_IN_FLOOR | |
3062 | BUILT_IN_CEILL BUILT_IN_CEIL | |
3063 | BUILT_IN_ROUNDL BUILT_IN_ROUND | |
3064 | BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT | |
3065 | BUILT_IN_RINTL BUILT_IN_RINT) | |
3066 | tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF | |
3067 | BUILT_IN_FLOORF BUILT_IN_FLOORF | |
3068 | BUILT_IN_CEILF BUILT_IN_CEILF | |
3069 | BUILT_IN_ROUNDF BUILT_IN_ROUNDF | |
3070 | BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF | |
3071 | BUILT_IN_RINTF BUILT_IN_RINTF) | |
3072 | /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc., | |
3073 | if x is a float. */ | |
5dac7dbd JDA |
3074 | (if (optimize && canonicalize_math_p () |
3075 | && targetm.libc_has_function (function_c99_misc)) | |
67dbe582 RS |
3076 | (simplify |
3077 | (froms (convert float_value_p@0)) | |
3078 | (convert (tos @0))))) | |
3079 | ||
543a9bcd RS |
3080 | (for froms (XFLOORL XCEILL XROUNDL XRINTL) |
3081 | tos (XFLOOR XCEIL XROUND XRINT) | |
3082 | /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */ | |
3083 | (if (optimize && canonicalize_math_p ()) | |
3084 | (simplify | |
3085 | (froms (convert double_value_p@0)) | |
3086 | (tos @0)))) | |
3087 | ||
3088 | (for froms (XFLOORL XCEILL XROUNDL XRINTL | |
3089 | XFLOOR XCEIL XROUND XRINT) | |
3090 | tos (XFLOORF XCEILF XROUNDF XRINTF) | |
3091 | /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc., | |
3092 | if x is a float. */ | |
3093 | (if (optimize && canonicalize_math_p ()) | |
3094 | (simplify | |
3095 | (froms (convert float_value_p@0)) | |
3096 | (tos @0)))) | |
3097 | ||
3098 | (if (canonicalize_math_p ()) | |
3099 | /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */ | |
3100 | (for floors (IFLOOR LFLOOR LLFLOOR) | |
3101 | (simplify | |
3102 | (floors tree_expr_nonnegative_p@0) | |
3103 | (fix_trunc @0)))) | |
3104 | ||
3105 | (if (canonicalize_math_p ()) | |
3106 | /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */ | |
3107 | (for fns (IFLOOR LFLOOR LLFLOOR | |
3108 | ICEIL LCEIL LLCEIL | |
3109 | IROUND LROUND LLROUND) | |
3110 | (simplify | |
3111 | (fns integer_valued_real_p@0) | |
3112 | (fix_trunc @0))) | |
3113 | (if (!flag_errno_math) | |
3114 | /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */ | |
3115 | (for rints (IRINT LRINT LLRINT) | |
3116 | (simplify | |
3117 | (rints integer_valued_real_p@0) | |
3118 | (fix_trunc @0))))) | |
3119 | ||
3120 | (if (canonicalize_math_p ()) | |
3121 | (for ifn (IFLOOR ICEIL IROUND IRINT) | |
3122 | lfn (LFLOOR LCEIL LROUND LRINT) | |
3123 | llfn (LLFLOOR LLCEIL LLROUND LLRINT) | |
3124 | /* Canonicalize iround (x) to lround (x) on ILP32 targets where | |
3125 | sizeof (int) == sizeof (long). */ | |
3126 | (if (TYPE_PRECISION (integer_type_node) | |
3127 | == TYPE_PRECISION (long_integer_type_node)) | |
3128 | (simplify | |
3129 | (ifn @0) | |
3130 | (lfn:long_integer_type_node @0))) | |
3131 | /* Canonicalize llround (x) to lround (x) on LP64 targets where | |
3132 | sizeof (long long) == sizeof (long). */ | |
3133 | (if (TYPE_PRECISION (long_long_integer_type_node) | |
3134 | == TYPE_PRECISION (long_integer_type_node)) | |
3135 | (simplify | |
3136 | (llfn @0) | |
3137 | (lfn:long_integer_type_node @0))))) | |
3138 | ||
92c52eab RS |
3139 | /* cproj(x) -> x if we're ignoring infinities. */ |
3140 | (simplify | |
3141 | (CPROJ @0) | |
3142 | (if (!HONOR_INFINITIES (type)) | |
3143 | @0)) | |
3144 | ||
4534c203 RB |
3145 | /* If the real part is inf and the imag part is known to be |
3146 | nonnegative, return (inf + 0i). */ | |
3147 | (simplify | |
3148 | (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1)) | |
3149 | (if (real_isinf (TREE_REAL_CST_PTR (@0))) | |
92c52eab RS |
3150 | { build_complex_inf (type, false); })) |
3151 | ||
4534c203 RB |
3152 | /* If the imag part is inf, return (inf+I*copysign(0,imag)). */ |
3153 | (simplify | |
3154 | (CPROJ (complex @0 REAL_CST@1)) | |
3155 | (if (real_isinf (TREE_REAL_CST_PTR (@1))) | |
92c52eab | 3156 | { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); })) |
4534c203 | 3157 | |
b4838d77 RS |
3158 | (for pows (POW) |
3159 | sqrts (SQRT) | |
3160 | cbrts (CBRT) | |
3161 | (simplify | |
3162 | (pows @0 REAL_CST@1) | |
3163 | (with { | |
3164 | const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1); | |
3165 | REAL_VALUE_TYPE tmp; | |
3166 | } | |
3167 | (switch | |
3168 | /* pow(x,0) -> 1. */ | |
3169 | (if (real_equal (value, &dconst0)) | |
3170 | { build_real (type, dconst1); }) | |
3171 | /* pow(x,1) -> x. */ | |
3172 | (if (real_equal (value, &dconst1)) | |
3173 | @0) | |
3174 | /* pow(x,-1) -> 1/x. */ | |
3175 | (if (real_equal (value, &dconstm1)) | |
3176 | (rdiv { build_real (type, dconst1); } @0)) | |
3177 | /* pow(x,0.5) -> sqrt(x). */ | |
3178 | (if (flag_unsafe_math_optimizations | |
3179 | && canonicalize_math_p () | |
3180 | && real_equal (value, &dconsthalf)) | |
3181 | (sqrts @0)) | |
3182 | /* pow(x,1/3) -> cbrt(x). */ | |
3183 | (if (flag_unsafe_math_optimizations | |
3184 | && canonicalize_math_p () | |
3185 | && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()), | |
3186 | real_equal (value, &tmp))) | |
3187 | (cbrts @0)))))) | |
4534c203 | 3188 | |
5ddc84ca RS |
3189 | /* powi(1,x) -> 1. */ |
3190 | (simplify | |
3191 | (POWI real_onep@0 @1) | |
3192 | @0) | |
3193 | ||
3194 | (simplify | |
3195 | (POWI @0 INTEGER_CST@1) | |
3196 | (switch | |
3197 | /* powi(x,0) -> 1. */ | |
3198 | (if (wi::eq_p (@1, 0)) | |
3199 | { build_real (type, dconst1); }) | |
3200 | /* powi(x,1) -> x. */ | |
3201 | (if (wi::eq_p (@1, 1)) | |
3202 | @0) | |
3203 | /* powi(x,-1) -> 1/x. */ | |
3204 | (if (wi::eq_p (@1, -1)) | |
3205 | (rdiv { build_real (type, dconst1); } @0)))) | |
3206 | ||
be144838 JL |
3207 | /* Narrowing of arithmetic and logical operations. |
3208 | ||
3209 | These are conceptually similar to the transformations performed for | |
3210 | the C/C++ front-ends by shorten_binary_op and shorten_compare. Long | |
3211 | term we want to move all that code out of the front-ends into here. */ | |
3212 | ||
3213 | /* If we have a narrowing conversion of an arithmetic operation where | |
3214 | both operands are widening conversions from the same type as the outer | |
3215 | narrowing conversion. Then convert the innermost operands to a suitable | |
9c582551 | 3216 | unsigned type (to avoid introducing undefined behavior), perform the |
be144838 JL |
3217 | operation and convert the result to the desired type. */ |
3218 | (for op (plus minus) | |
3219 | (simplify | |
44fc0a51 | 3220 | (convert (op:s (convert@2 @0) (convert@3 @1))) |
be144838 JL |
3221 | (if (INTEGRAL_TYPE_P (type) |
3222 | /* We check for type compatibility between @0 and @1 below, | |
3223 | so there's no need to check that @1/@3 are integral types. */ | |
3224 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3225 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
3226 | /* The precision of the type of each operand must match the | |
3227 | precision of the mode of each operand, similarly for the | |
3228 | result. */ | |
3229 | && (TYPE_PRECISION (TREE_TYPE (@0)) | |
3230 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) | |
3231 | && (TYPE_PRECISION (TREE_TYPE (@1)) | |
3232 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1)))) | |
3233 | && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type)) | |
3234 | /* The inner conversion must be a widening conversion. */ | |
3235 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
aea417d7 | 3236 | && types_match (@0, @1) |
44fc0a51 | 3237 | && types_match (@0, type)) |
be144838 | 3238 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) |
8fdc6c67 RB |
3239 | (convert (op @0 @1)) |
3240 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
3241 | (convert (op (convert:utype @0) (convert:utype @1)))))))) | |
48451e8f JL |
3242 | |
3243 | /* This is another case of narrowing, specifically when there's an outer | |
3244 | BIT_AND_EXPR which masks off bits outside the type of the innermost | |
3245 | operands. Like the previous case we have to convert the operands | |
9c582551 | 3246 | to unsigned types to avoid introducing undefined behavior for the |
48451e8f JL |
3247 | arithmetic operation. */ |
3248 | (for op (minus plus) | |
8fdc6c67 RB |
3249 | (simplify |
3250 | (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4) | |
3251 | (if (INTEGRAL_TYPE_P (type) | |
3252 | /* We check for type compatibility between @0 and @1 below, | |
3253 | so there's no need to check that @1/@3 are integral types. */ | |
3254 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3255 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
3256 | /* The precision of the type of each operand must match the | |
3257 | precision of the mode of each operand, similarly for the | |
3258 | result. */ | |
3259 | && (TYPE_PRECISION (TREE_TYPE (@0)) | |
3260 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) | |
3261 | && (TYPE_PRECISION (TREE_TYPE (@1)) | |
3262 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1)))) | |
3263 | && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type)) | |
3264 | /* The inner conversion must be a widening conversion. */ | |
3265 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
3266 | && types_match (@0, @1) | |
3267 | && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0))) | |
3268 | <= TYPE_PRECISION (TREE_TYPE (@0))) | |
0a8c1e23 JL |
3269 | && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)), |
3270 | true, TYPE_PRECISION (type))) == 0)) | |
8fdc6c67 RB |
3271 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) |
3272 | (with { tree ntype = TREE_TYPE (@0); } | |
3273 | (convert (bit_and (op @0 @1) (convert:ntype @4)))) | |
3274 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
3275 | (convert (bit_and (op (convert:utype @0) (convert:utype @1)) | |
3276 | (convert:utype @4)))))))) | |
4f7a5692 MC |
3277 | |
3278 | /* Transform (@0 < @1 and @0 < @2) to use min, | |
3279 | (@0 > @1 and @0 > @2) to use max */ | |
3280 | (for op (lt le gt ge) | |
3281 | ext (min min max max) | |
3282 | (simplify | |
4618c453 RB |
3283 | (bit_and (op:cs @0 @1) (op:cs @0 @2)) |
3284 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3285 | && TREE_CODE (@0) != INTEGER_CST) | |
4f7a5692 MC |
3286 | (op @0 (ext @1 @2))))) |
3287 | ||
7317ef4a RS |
3288 | (simplify |
3289 | /* signbit(x) -> 0 if x is nonnegative. */ | |
3290 | (SIGNBIT tree_expr_nonnegative_p@0) | |
3291 | { integer_zero_node; }) | |
3292 | ||
3293 | (simplify | |
3294 | /* signbit(x) -> x<0 if x doesn't have signed zeros. */ | |
3295 | (SIGNBIT @0) | |
3296 | (if (!HONOR_SIGNED_ZEROS (@0)) | |
3297 | (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); })))) | |
a8b85ce9 MG |
3298 | |
3299 | /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */ | |
3300 | (for cmp (eq ne) | |
3301 | (for op (plus minus) | |
3302 | rop (minus plus) | |
3303 | (simplify | |
3304 | (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) | |
3305 | (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) | |
3306 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) | |
3307 | && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0)) | |
3308 | && !TYPE_SATURATING (TREE_TYPE (@0))) | |
3309 | (with { tree res = int_const_binop (rop, @2, @1); } | |
3310 | (if (TREE_OVERFLOW (res)) | |
3311 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
3312 | (if (single_use (@3)) | |
3313 | (cmp @0 { res; })))))))) | |
3314 | (for cmp (lt le gt ge) | |
3315 | (for op (plus minus) | |
3316 | rop (minus plus) | |
3317 | (simplify | |
3318 | (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) | |
3319 | (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) | |
3320 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
3321 | (with { tree res = int_const_binop (rop, @2, @1); } | |
3322 | (if (TREE_OVERFLOW (res)) | |
3323 | { | |
3324 | fold_overflow_warning (("assuming signed overflow does not occur " | |
3325 | "when simplifying conditional to constant"), | |
3326 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
3327 | bool less = cmp == LE_EXPR || cmp == LT_EXPR; | |
3328 | /* wi::ges_p (@2, 0) should be sufficient for a signed type. */ | |
3329 | bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))) | |
3330 | != (op == MINUS_EXPR); | |
3331 | constant_boolean_node (less == ovf_high, type); | |
3332 | } | |
3333 | (if (single_use (@3)) | |
3334 | (with | |
3335 | { | |
3336 | fold_overflow_warning (("assuming signed overflow does not occur " | |
3337 | "when changing X +- C1 cmp C2 to " | |
3338 | "X cmp C2 -+ C1"), | |
3339 | WARN_STRICT_OVERFLOW_COMPARISON); | |
3340 | } | |
3341 | (cmp @0 { res; }))))))))) | |
d3e40b76 RB |
3342 | |
3343 | /* Canonicalizations of BIT_FIELD_REFs. */ | |
3344 | ||
3345 | (simplify | |
3346 | (BIT_FIELD_REF @0 @1 @2) | |
3347 | (switch | |
3348 | (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE | |
3349 | && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
3350 | (switch | |
3351 | (if (integer_zerop (@2)) | |
3352 | (view_convert (realpart @0))) | |
3353 | (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
3354 | (view_convert (imagpart @0))))) | |
3355 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3356 | && INTEGRAL_TYPE_P (type) | |
171f6f05 RB |
3357 | /* On GIMPLE this should only apply to register arguments. */ |
3358 | && (! GIMPLE || is_gimple_reg (@0)) | |
d3e40b76 RB |
3359 | /* A bit-field-ref that referenced the full argument can be stripped. */ |
3360 | && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0 | |
3361 | && integer_zerop (@2)) | |
3362 | /* Low-parts can be reduced to integral conversions. | |
3363 | ??? The following doesn't work for PDP endian. */ | |
3364 | || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN | |
3365 | /* Don't even think about BITS_BIG_ENDIAN. */ | |
3366 | && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0 | |
3367 | && TYPE_PRECISION (type) % BITS_PER_UNIT == 0 | |
3368 | && compare_tree_int (@2, (BYTES_BIG_ENDIAN | |
3369 | ? (TYPE_PRECISION (TREE_TYPE (@0)) | |
3370 | - TYPE_PRECISION (type)) | |
3371 | : 0)) == 0))) | |
3372 | (convert @0)))) | |
3373 | ||
3374 | /* Simplify vector extracts. */ | |
3375 | ||
3376 | (simplify | |
3377 | (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2) | |
3378 | (if (VECTOR_TYPE_P (TREE_TYPE (@0)) | |
3379 | && (types_match (type, TREE_TYPE (TREE_TYPE (@0))) | |
3380 | || (VECTOR_TYPE_P (type) | |
3381 | && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) | |
3382 | (with | |
3383 | { | |
3384 | tree ctor = (TREE_CODE (@0) == SSA_NAME | |
3385 | ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); | |
3386 | tree eltype = TREE_TYPE (TREE_TYPE (ctor)); | |
3387 | unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype)); | |
3388 | unsigned HOST_WIDE_INT n = tree_to_uhwi (@1); | |
3389 | unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2); | |
3390 | } | |
3391 | (if (n != 0 | |
3392 | && (idx % width) == 0 | |
3393 | && (n % width) == 0 | |
3394 | && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))) | |
3395 | (with | |
3396 | { | |
3397 | idx = idx / width; | |
3398 | n = n / width; | |
3399 | /* Constructor elements can be subvectors. */ | |
3400 | unsigned HOST_WIDE_INT k = 1; | |
3401 | if (CONSTRUCTOR_NELTS (ctor) != 0) | |
3402 | { | |
3403 | tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value); | |
3404 | if (TREE_CODE (cons_elem) == VECTOR_TYPE) | |
3405 | k = TYPE_VECTOR_SUBPARTS (cons_elem); | |
3406 | } | |
3407 | } | |
3408 | (switch | |
3409 | /* We keep an exact subset of the constructor elements. */ | |
3410 | (if ((idx % k) == 0 && (n % k) == 0) | |
3411 | (if (CONSTRUCTOR_NELTS (ctor) == 0) | |
3412 | { build_constructor (type, NULL); } | |
3413 | (with | |
3414 | { | |
3415 | idx /= k; | |
3416 | n /= k; | |
3417 | } | |
3418 | (if (n == 1) | |
3419 | (if (idx < CONSTRUCTOR_NELTS (ctor)) | |
3420 | { CONSTRUCTOR_ELT (ctor, idx)->value; } | |
3421 | { build_zero_cst (type); }) | |
3422 | { | |
3423 | vec<constructor_elt, va_gc> *vals; | |
3424 | vec_alloc (vals, n); | |
3425 | for (unsigned i = 0; | |
3426 | i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i) | |
3427 | CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, | |
3428 | CONSTRUCTOR_ELT (ctor, idx + i)->value); | |
3429 | build_constructor (type, vals); | |
3430 | })))) | |
3431 | /* The bitfield references a single constructor element. */ | |
3432 | (if (idx + n <= (idx / k + 1) * k) | |
3433 | (switch | |
3434 | (if (CONSTRUCTOR_NELTS (ctor) <= idx / k) | |
3435 | { build_zero_cst (type); }) | |
3436 | (if (n == k) | |
3437 | { CONSTRUCTOR_ELT (ctor, idx / k)->value; }) | |
3438 | (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; } | |
3439 | @1 { bitsize_int ((idx % k) * width); }))))))))) |