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