]>
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 | ||
5624e564 | 5 | Copyright (C) 2014-2015 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 |
53a19317 RB |
34 | integer_pow2p |
35 | HONOR_NANS) | |
e0ee10ed | 36 | |
f84e7fd6 RB |
37 | /* Operator lists. */ |
38 | (define_operator_list tcc_comparison | |
39 | lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) | |
40 | (define_operator_list inverted_tcc_comparison | |
41 | ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq) | |
42 | (define_operator_list inverted_tcc_comparison_with_nans | |
43 | unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq) | |
534bd33b MG |
44 | (define_operator_list swapped_tcc_comparison |
45 | gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt) | |
07cdc2b8 RB |
46 | (define_operator_list simple_comparison lt le eq ne ge gt) |
47 | (define_operator_list swapped_simple_comparison gt ge eq ne le lt) | |
48 | ||
49 | (define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL) | |
50 | (define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL) | |
51 | (define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L) | |
52 | (define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L) | |
53 | (define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L) | |
54 | (define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L) | |
55 | (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL) | |
56 | (define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L) | |
57 | (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL) | |
58 | (define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL) | |
77c028c5 MG |
59 | (define_operator_list SIN BUILT_IN_SINF BUILT_IN_SIN BUILT_IN_SINL) |
60 | (define_operator_list COS BUILT_IN_COSF BUILT_IN_COS BUILT_IN_COSL) | |
61 | (define_operator_list TAN BUILT_IN_TANF BUILT_IN_TAN BUILT_IN_TANL) | |
62 | (define_operator_list COSH BUILT_IN_COSHF BUILT_IN_COSH BUILT_IN_COSHL) | |
63 | (define_operator_list CEXPI BUILT_IN_CEXPIF BUILT_IN_CEXPI BUILT_IN_CEXPIL) | |
f84e7fd6 | 64 | |
e0ee10ed | 65 | /* Simplifications of operations with one constant operand and |
36a60e48 | 66 | simplifications to constants or single values. */ |
e0ee10ed RB |
67 | |
68 | (for op (plus pointer_plus minus bit_ior bit_xor) | |
69 | (simplify | |
70 | (op @0 integer_zerop) | |
71 | (non_lvalue @0))) | |
72 | ||
a499aac5 RB |
73 | /* 0 +p index -> (type)index */ |
74 | (simplify | |
75 | (pointer_plus integer_zerop @1) | |
76 | (non_lvalue (convert @1))) | |
77 | ||
a7f24614 RB |
78 | /* See if ARG1 is zero and X + ARG1 reduces to X. |
79 | Likewise if the operands are reversed. */ | |
80 | (simplify | |
81 | (plus:c @0 real_zerop@1) | |
82 | (if (fold_real_zero_addition_p (type, @1, 0)) | |
83 | (non_lvalue @0))) | |
84 | ||
85 | /* See if ARG1 is zero and X - ARG1 reduces to X. */ | |
86 | (simplify | |
87 | (minus @0 real_zerop@1) | |
88 | (if (fold_real_zero_addition_p (type, @1, 1)) | |
89 | (non_lvalue @0))) | |
90 | ||
e0ee10ed RB |
91 | /* Simplify x - x. |
92 | This is unsafe for certain floats even in non-IEEE formats. | |
93 | In IEEE, it is unsafe because it does wrong for NaNs. | |
94 | Also note that operand_equal_p is always false if an operand | |
95 | is volatile. */ | |
96 | (simplify | |
a7f24614 | 97 | (minus @0 @0) |
1b457aa4 | 98 | (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type)) |
a7f24614 | 99 | { build_zero_cst (type); })) |
e0ee10ed RB |
100 | |
101 | (simplify | |
a7f24614 RB |
102 | (mult @0 integer_zerop@1) |
103 | @1) | |
104 | ||
105 | /* Maybe fold x * 0 to 0. The expressions aren't the same | |
106 | when x is NaN, since x * 0 is also NaN. Nor are they the | |
107 | same in modes with signed zeros, since multiplying a | |
108 | negative value by 0 gives -0, not +0. */ | |
109 | (simplify | |
110 | (mult @0 real_zerop@1) | |
8b5ee871 | 111 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) |
a7f24614 RB |
112 | @1)) |
113 | ||
114 | /* In IEEE floating point, x*1 is not equivalent to x for snans. | |
115 | Likewise for complex arithmetic with signed zeros. */ | |
116 | (simplify | |
117 | (mult @0 real_onep) | |
8b5ee871 MG |
118 | (if (!HONOR_SNANS (type) |
119 | && (!HONOR_SIGNED_ZEROS (type) | |
a7f24614 RB |
120 | || !COMPLEX_FLOAT_TYPE_P (type))) |
121 | (non_lvalue @0))) | |
122 | ||
123 | /* Transform x * -1.0 into -x. */ | |
124 | (simplify | |
125 | (mult @0 real_minus_onep) | |
8b5ee871 MG |
126 | (if (!HONOR_SNANS (type) |
127 | && (!HONOR_SIGNED_ZEROS (type) | |
a7f24614 RB |
128 | || !COMPLEX_FLOAT_TYPE_P (type))) |
129 | (negate @0))) | |
e0ee10ed RB |
130 | |
131 | /* Make sure to preserve divisions by zero. This is the reason why | |
132 | we don't simplify x / x to 1 or 0 / x to 0. */ | |
133 | (for op (mult trunc_div ceil_div floor_div round_div exact_div) | |
134 | (simplify | |
135 | (op @0 integer_onep) | |
136 | (non_lvalue @0))) | |
137 | ||
a7f24614 RB |
138 | /* X / -1 is -X. */ |
139 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
140 | (simplify | |
09240451 MG |
141 | (div @0 integer_minus_onep@1) |
142 | (if (!TYPE_UNSIGNED (type)) | |
a7f24614 RB |
143 | (negate @0)))) |
144 | ||
145 | /* For unsigned integral types, FLOOR_DIV_EXPR is the same as | |
146 | TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ | |
147 | (simplify | |
148 | (floor_div @0 @1) | |
09240451 MG |
149 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
150 | && TYPE_UNSIGNED (type)) | |
a7f24614 RB |
151 | (trunc_div @0 @1))) |
152 | ||
28093105 RB |
153 | /* Combine two successive divisions. Note that combining ceil_div |
154 | and floor_div is trickier and combining round_div even more so. */ | |
155 | (for div (trunc_div exact_div) | |
c306cfaf RB |
156 | (simplify |
157 | (div (div @0 INTEGER_CST@1) INTEGER_CST@2) | |
158 | (with { | |
159 | bool overflow_p; | |
160 | wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p); | |
161 | } | |
162 | (if (!overflow_p) | |
8fdc6c67 RB |
163 | (div @0 { wide_int_to_tree (type, mul); }) |
164 | (if (TYPE_UNSIGNED (type) | |
165 | || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)) | |
166 | { build_zero_cst (type); }))))) | |
c306cfaf | 167 | |
a7f24614 | 168 | /* Optimize A / A to 1.0 if we don't care about |
09240451 | 169 | NaNs or Infinities. */ |
a7f24614 RB |
170 | (simplify |
171 | (rdiv @0 @0) | |
09240451 | 172 | (if (FLOAT_TYPE_P (type) |
1b457aa4 | 173 | && ! HONOR_NANS (type) |
8b5ee871 | 174 | && ! HONOR_INFINITIES (type)) |
09240451 MG |
175 | { build_one_cst (type); })) |
176 | ||
177 | /* Optimize -A / A to -1.0 if we don't care about | |
178 | NaNs or Infinities. */ | |
179 | (simplify | |
180 | (rdiv:c @0 (negate @0)) | |
181 | (if (FLOAT_TYPE_P (type) | |
1b457aa4 | 182 | && ! HONOR_NANS (type) |
8b5ee871 | 183 | && ! HONOR_INFINITIES (type)) |
09240451 | 184 | { build_minus_one_cst (type); })) |
a7f24614 RB |
185 | |
186 | /* In IEEE floating point, x/1 is not equivalent to x for snans. */ | |
187 | (simplify | |
188 | (rdiv @0 real_onep) | |
8b5ee871 | 189 | (if (!HONOR_SNANS (type)) |
a7f24614 RB |
190 | (non_lvalue @0))) |
191 | ||
192 | /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ | |
193 | (simplify | |
194 | (rdiv @0 real_minus_onep) | |
8b5ee871 | 195 | (if (!HONOR_SNANS (type)) |
a7f24614 RB |
196 | (negate @0))) |
197 | ||
198 | /* If ARG1 is a constant, we can convert this to a multiply by the | |
199 | reciprocal. This does not have the same rounding properties, | |
200 | so only do this if -freciprocal-math. We can actually | |
201 | always safely do it if ARG1 is a power of two, but it's hard to | |
202 | tell if it is or not in a portable manner. */ | |
203 | (for cst (REAL_CST COMPLEX_CST VECTOR_CST) | |
204 | (simplify | |
205 | (rdiv @0 cst@1) | |
206 | (if (optimize) | |
53bc4b3a RB |
207 | (if (flag_reciprocal_math |
208 | && !real_zerop (@1)) | |
a7f24614 | 209 | (with |
249700b5 | 210 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); } |
a7f24614 | 211 | (if (tem) |
8fdc6c67 RB |
212 | (mult @0 { tem; } ))) |
213 | (if (cst != COMPLEX_CST) | |
214 | (with { tree inverse = exact_inverse (type, @1); } | |
215 | (if (inverse) | |
216 | (mult @0 { inverse; } )))))))) | |
a7f24614 | 217 | |
e0ee10ed RB |
218 | /* Same applies to modulo operations, but fold is inconsistent here |
219 | and simplifies 0 % x to 0, only preserving literal 0 % 0. */ | |
a7f24614 | 220 | (for mod (ceil_mod floor_mod round_mod trunc_mod) |
e0ee10ed RB |
221 | /* 0 % X is always zero. */ |
222 | (simplify | |
a7f24614 | 223 | (mod integer_zerop@0 @1) |
e0ee10ed RB |
224 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ |
225 | (if (!integer_zerop (@1)) | |
226 | @0)) | |
227 | /* X % 1 is always zero. */ | |
228 | (simplify | |
a7f24614 RB |
229 | (mod @0 integer_onep) |
230 | { build_zero_cst (type); }) | |
231 | /* X % -1 is zero. */ | |
232 | (simplify | |
09240451 MG |
233 | (mod @0 integer_minus_onep@1) |
234 | (if (!TYPE_UNSIGNED (type)) | |
bc4315fb MG |
235 | { build_zero_cst (type); })) |
236 | /* (X % Y) % Y is just X % Y. */ | |
237 | (simplify | |
238 | (mod (mod@2 @0 @1) @1) | |
98e30e51 RB |
239 | @2) |
240 | /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */ | |
241 | (simplify | |
242 | (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
243 | (if (ANY_INTEGRAL_TYPE_P (type) | |
244 | && TYPE_OVERFLOW_UNDEFINED (type) | |
245 | && wi::multiple_of_p (@1, @2, TYPE_SIGN (type))) | |
246 | { build_zero_cst (type); }))) | |
a7f24614 RB |
247 | |
248 | /* X % -C is the same as X % C. */ | |
249 | (simplify | |
250 | (trunc_mod @0 INTEGER_CST@1) | |
251 | (if (TYPE_SIGN (type) == SIGNED | |
252 | && !TREE_OVERFLOW (@1) | |
253 | && wi::neg_p (@1) | |
254 | && !TYPE_OVERFLOW_TRAPS (type) | |
255 | /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ | |
256 | && !sign_bit_p (@1, @1)) | |
257 | (trunc_mod @0 (negate @1)))) | |
e0ee10ed | 258 | |
8f0c696a RB |
259 | /* X % -Y is the same as X % Y. */ |
260 | (simplify | |
261 | (trunc_mod @0 (convert? (negate @1))) | |
262 | (if (!TYPE_UNSIGNED (type) | |
263 | && !TYPE_OVERFLOW_TRAPS (type) | |
264 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
265 | (trunc_mod @0 (convert @1)))) | |
266 | ||
f461569a MP |
267 | /* X - (X / Y) * Y is the same as X % Y. */ |
268 | (simplify | |
d3bc1d1b | 269 | (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1))) |
f461569a | 270 | (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
d3bc1d1b | 271 | (trunc_mod (convert @0) (convert @1)))) |
f461569a | 272 | |
8f0c696a RB |
273 | /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, |
274 | i.e. "X % C" into "X & (C - 1)", if X and C are positive. | |
275 | Also optimize A % (C << N) where C is a power of 2, | |
276 | to A & ((C << N) - 1). */ | |
277 | (match (power_of_two_cand @1) | |
278 | INTEGER_CST@1) | |
279 | (match (power_of_two_cand @1) | |
280 | (lshift INTEGER_CST@1 @2)) | |
281 | (for mod (trunc_mod floor_mod) | |
282 | (simplify | |
4ab1e111 | 283 | (mod @0 (convert?@3 (power_of_two_cand@1 @2))) |
8f0c696a RB |
284 | (if ((TYPE_UNSIGNED (type) |
285 | || tree_expr_nonnegative_p (@0)) | |
4ab1e111 | 286 | && tree_nop_conversion_p (type, TREE_TYPE (@3)) |
8f0c696a | 287 | && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) |
4ab1e111 | 288 | (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))))) |
8f0c696a | 289 | |
887ab609 N |
290 | /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ |
291 | (simplify | |
292 | (trunc_div (mult @0 integer_pow2p@1) @1) | |
293 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
294 | (bit_and @0 { wide_int_to_tree | |
295 | (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1), | |
296 | false, TYPE_PRECISION (type))); }))) | |
297 | ||
5f8d832e N |
298 | /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */ |
299 | (simplify | |
300 | (mult (trunc_div @0 integer_pow2p@1) @1) | |
301 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
302 | (bit_and @0 (negate @1)))) | |
303 | ||
95765f36 N |
304 | /* Simplify (t * 2) / 2) -> t. */ |
305 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
306 | (simplify | |
307 | (div (mult @0 @1) @1) | |
308 | (if (ANY_INTEGRAL_TYPE_P (type) | |
309 | && TYPE_OVERFLOW_UNDEFINED (type)) | |
310 | @0))) | |
311 | ||
d202f9bd RB |
312 | /* Simplify cos (-x) -> cos (x). */ |
313 | (for op (negate abs) | |
314 | (for coss (COS COSH) | |
315 | (simplify | |
316 | (coss (op @0)) | |
317 | (coss @0)))) | |
318 | ||
bc4315fb MG |
319 | /* X % Y is smaller than Y. */ |
320 | (for cmp (lt ge) | |
321 | (simplify | |
322 | (cmp (trunc_mod @0 @1) @1) | |
323 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
324 | { constant_boolean_node (cmp == LT_EXPR, type); }))) | |
325 | (for cmp (gt le) | |
326 | (simplify | |
327 | (cmp @1 (trunc_mod @0 @1)) | |
328 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
329 | { constant_boolean_node (cmp == GT_EXPR, type); }))) | |
330 | ||
e0ee10ed RB |
331 | /* x | ~0 -> ~0 */ |
332 | (simplify | |
333 | (bit_ior @0 integer_all_onesp@1) | |
334 | @1) | |
335 | ||
336 | /* x & 0 -> 0 */ | |
337 | (simplify | |
338 | (bit_and @0 integer_zerop@1) | |
339 | @1) | |
340 | ||
a4398a30 | 341 | /* ~x | x -> -1 */ |
8b5ee871 MG |
342 | /* ~x ^ x -> -1 */ |
343 | /* ~x + x -> -1 */ | |
344 | (for op (bit_ior bit_xor plus) | |
345 | (simplify | |
346 | (op:c (convert? @0) (convert? (bit_not @0))) | |
347 | (convert { build_all_ones_cst (TREE_TYPE (@0)); }))) | |
a4398a30 | 348 | |
e0ee10ed RB |
349 | /* x ^ x -> 0 */ |
350 | (simplify | |
351 | (bit_xor @0 @0) | |
352 | { build_zero_cst (type); }) | |
353 | ||
36a60e48 RB |
354 | /* Canonicalize X ^ ~0 to ~X. */ |
355 | (simplify | |
356 | (bit_xor @0 integer_all_onesp@1) | |
357 | (bit_not @0)) | |
358 | ||
359 | /* x & ~0 -> x */ | |
360 | (simplify | |
361 | (bit_and @0 integer_all_onesp) | |
362 | (non_lvalue @0)) | |
363 | ||
364 | /* x & x -> x, x | x -> x */ | |
365 | (for bitop (bit_and bit_ior) | |
366 | (simplify | |
367 | (bitop @0 @0) | |
368 | (non_lvalue @0))) | |
369 | ||
0f770b01 RV |
370 | /* x + (x & 1) -> (x + 1) & ~1 */ |
371 | (simplify | |
44fc0a51 RB |
372 | (plus:c @0 (bit_and:s @0 integer_onep@1)) |
373 | (bit_and (plus @0 @1) (bit_not @1))) | |
0f770b01 RV |
374 | |
375 | /* x & ~(x & y) -> x & ~y */ | |
376 | /* x | ~(x | y) -> x | ~y */ | |
377 | (for bitop (bit_and bit_ior) | |
af563d4b | 378 | (simplify |
44fc0a51 RB |
379 | (bitop:c @0 (bit_not (bitop:cs @0 @1))) |
380 | (bitop @0 (bit_not @1)))) | |
af563d4b MG |
381 | |
382 | /* (x | y) & ~x -> y & ~x */ | |
383 | /* (x & y) | ~x -> y | ~x */ | |
384 | (for bitop (bit_and bit_ior) | |
385 | rbitop (bit_ior bit_and) | |
386 | (simplify | |
387 | (bitop:c (rbitop:c @0 @1) (bit_not@2 @0)) | |
388 | (bitop @1 @2))) | |
0f770b01 | 389 | |
f13c4673 MP |
390 | /* (x & y) ^ (x | y) -> x ^ y */ |
391 | (simplify | |
2d6f2dce MP |
392 | (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1)) |
393 | (bit_xor @0 @1)) | |
f13c4673 | 394 | |
9ea65ca6 MP |
395 | /* (x ^ y) ^ (x | y) -> x & y */ |
396 | (simplify | |
397 | (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1)) | |
398 | (bit_and @0 @1)) | |
399 | ||
400 | /* (x & y) + (x ^ y) -> x | y */ | |
401 | /* (x & y) | (x ^ y) -> x | y */ | |
402 | /* (x & y) ^ (x ^ y) -> x | y */ | |
403 | (for op (plus bit_ior bit_xor) | |
404 | (simplify | |
405 | (op:c (bit_and @0 @1) (bit_xor @0 @1)) | |
406 | (bit_ior @0 @1))) | |
407 | ||
408 | /* (x & y) + (x | y) -> x + y */ | |
409 | (simplify | |
410 | (plus:c (bit_and @0 @1) (bit_ior @0 @1)) | |
411 | (plus @0 @1)) | |
412 | ||
9737efaf MP |
413 | /* (x + y) - (x | y) -> x & y */ |
414 | (simplify | |
415 | (minus (plus @0 @1) (bit_ior @0 @1)) | |
416 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
417 | && !TYPE_SATURATING (type)) | |
418 | (bit_and @0 @1))) | |
419 | ||
420 | /* (x + y) - (x & y) -> x | y */ | |
421 | (simplify | |
422 | (minus (plus @0 @1) (bit_and @0 @1)) | |
423 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
424 | && !TYPE_SATURATING (type)) | |
425 | (bit_ior @0 @1))) | |
426 | ||
9ea65ca6 MP |
427 | /* (x | y) - (x ^ y) -> x & y */ |
428 | (simplify | |
429 | (minus (bit_ior @0 @1) (bit_xor @0 @1)) | |
430 | (bit_and @0 @1)) | |
431 | ||
432 | /* (x | y) - (x & y) -> x ^ y */ | |
433 | (simplify | |
434 | (minus (bit_ior @0 @1) (bit_and @0 @1)) | |
435 | (bit_xor @0 @1)) | |
436 | ||
66cc6273 MP |
437 | /* (x | y) & ~(x & y) -> x ^ y */ |
438 | (simplify | |
439 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1))) | |
440 | (bit_xor @0 @1)) | |
441 | ||
442 | /* (x | y) & (~x ^ y) -> x & y */ | |
443 | (simplify | |
444 | (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0))) | |
445 | (bit_and @0 @1)) | |
446 | ||
5b00d921 RB |
447 | /* ~x & ~y -> ~(x | y) |
448 | ~x | ~y -> ~(x & y) */ | |
449 | (for op (bit_and bit_ior) | |
450 | rop (bit_ior bit_and) | |
451 | (simplify | |
452 | (op (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
453 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
454 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
455 | (bit_not (rop (convert @0) (convert @1)))))) | |
456 | ||
14ea9f92 | 457 | /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing |
5b00d921 RB |
458 | with a constant, and the two constants have no bits in common, |
459 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
460 | simplifications. */ | |
14ea9f92 RB |
461 | (for op (bit_xor plus) |
462 | (simplify | |
463 | (op (convert1? (bit_and@4 @0 INTEGER_CST@1)) | |
464 | (convert2? (bit_and@5 @2 INTEGER_CST@3))) | |
465 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
466 | && tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
467 | && wi::bit_and (@1, @3) == 0) | |
468 | (bit_ior (convert @4) (convert @5))))) | |
5b00d921 RB |
469 | |
470 | /* (X | Y) ^ X -> Y & ~ X*/ | |
471 | (simplify | |
472 | (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0)) | |
473 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
474 | (convert (bit_and @1 (bit_not @0))))) | |
475 | ||
476 | /* Convert ~X ^ ~Y to X ^ Y. */ | |
477 | (simplify | |
478 | (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
479 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
480 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
481 | (bit_xor (convert @0) (convert @1)))) | |
482 | ||
483 | /* Convert ~X ^ C to X ^ ~C. */ | |
484 | (simplify | |
485 | (bit_xor (convert? (bit_not @0)) INTEGER_CST@1) | |
c8ba6498 EB |
486 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
487 | (bit_xor (convert @0) (bit_not @1)))) | |
5b00d921 | 488 | |
97e77391 RB |
489 | /* Fold (X & Y) ^ Y as ~X & Y. */ |
490 | (simplify | |
491 | (bit_xor:c (bit_and:c @0 @1) @1) | |
492 | (bit_and (bit_not @0) @1)) | |
493 | ||
14ea9f92 RB |
494 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both |
495 | operands are another bit-wise operation with a common input. If so, | |
496 | distribute the bit operations to save an operation and possibly two if | |
497 | constants are involved. For example, convert | |
498 | (A | B) & (A | C) into A | (B & C) | |
499 | Further simplification will occur if B and C are constants. */ | |
500 | (for op (bit_and bit_ior) | |
501 | rop (bit_ior bit_and) | |
502 | (simplify | |
503 | (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2))) | |
504 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
505 | (rop (convert @0) (op (convert @1) (convert @2)))))) | |
506 | ||
5b00d921 | 507 | |
b14a9c57 RB |
508 | (simplify |
509 | (abs (abs@1 @0)) | |
510 | @1) | |
f3582e54 RB |
511 | (simplify |
512 | (abs (negate @0)) | |
513 | (abs @0)) | |
514 | (simplify | |
515 | (abs tree_expr_nonnegative_p@0) | |
516 | @0) | |
517 | ||
55cf3946 RB |
518 | /* A few cases of fold-const.c negate_expr_p predicate. */ |
519 | (match negate_expr_p | |
520 | INTEGER_CST | |
b14a9c57 RB |
521 | (if ((INTEGRAL_TYPE_P (type) |
522 | && TYPE_OVERFLOW_WRAPS (type)) | |
523 | || (!TYPE_OVERFLOW_SANITIZED (type) | |
55cf3946 RB |
524 | && may_negate_without_overflow_p (t))))) |
525 | (match negate_expr_p | |
526 | FIXED_CST) | |
527 | (match negate_expr_p | |
528 | (negate @0) | |
529 | (if (!TYPE_OVERFLOW_SANITIZED (type)))) | |
530 | (match negate_expr_p | |
531 | REAL_CST | |
532 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t))))) | |
533 | /* VECTOR_CST handling of non-wrapping types would recurse in unsupported | |
534 | ways. */ | |
535 | (match negate_expr_p | |
536 | VECTOR_CST | |
537 | (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)))) | |
538 | ||
539 | /* -(A + B) -> (-B) - A. */ | |
b14a9c57 | 540 | (simplify |
55cf3946 RB |
541 | (negate (plus:c @0 negate_expr_p@1)) |
542 | (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) | |
543 | && !HONOR_SIGNED_ZEROS (element_mode (type))) | |
544 | (minus (negate @1) @0))) | |
545 | ||
546 | /* A - B -> A + (-B) if B is easily negatable. */ | |
b14a9c57 | 547 | (simplify |
55cf3946 | 548 | (minus @0 negate_expr_p@1) |
e4e96a4f KT |
549 | (if (!FIXED_POINT_TYPE_P (type)) |
550 | (plus @0 (negate @1)))) | |
d4573ffe | 551 | |
5609420f RB |
552 | /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) |
553 | when profitable. | |
554 | For bitwise binary operations apply operand conversions to the | |
555 | binary operation result instead of to the operands. This allows | |
556 | to combine successive conversions and bitwise binary operations. | |
557 | We combine the above two cases by using a conditional convert. */ | |
558 | (for bitop (bit_and bit_ior bit_xor) | |
559 | (simplify | |
560 | (bitop (convert @0) (convert? @1)) | |
561 | (if (((TREE_CODE (@1) == INTEGER_CST | |
562 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
ad6f996c | 563 | && int_fits_type_p (@1, TREE_TYPE (@0))) |
aea417d7 | 564 | || types_match (@0, @1)) |
ad6f996c RB |
565 | /* ??? This transform conflicts with fold-const.c doing |
566 | Convert (T)(x & c) into (T)x & (T)c, if c is an integer | |
567 | constants (if x has signed type, the sign bit cannot be set | |
568 | in c). This folds extension into the BIT_AND_EXPR. | |
569 | Restrict it to GIMPLE to avoid endless recursions. */ | |
570 | && (bitop != BIT_AND_EXPR || GIMPLE) | |
5609420f RB |
571 | && (/* That's a good idea if the conversion widens the operand, thus |
572 | after hoisting the conversion the operation will be narrower. */ | |
573 | TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type) | |
574 | /* It's also a good idea if the conversion is to a non-integer | |
575 | mode. */ | |
576 | || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT | |
577 | /* Or if the precision of TO is not the same as the precision | |
578 | of its mode. */ | |
579 | || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type)))) | |
580 | (convert (bitop @0 (convert @1)))))) | |
581 | ||
b14a9c57 RB |
582 | (for bitop (bit_and bit_ior) |
583 | rbitop (bit_ior bit_and) | |
584 | /* (x | y) & x -> x */ | |
585 | /* (x & y) | x -> x */ | |
586 | (simplify | |
587 | (bitop:c (rbitop:c @0 @1) @0) | |
588 | @0) | |
589 | /* (~x | y) & x -> x & y */ | |
590 | /* (~x & y) | x -> x | y */ | |
591 | (simplify | |
592 | (bitop:c (rbitop:c (bit_not @0) @1) @0) | |
593 | (bitop @0 @1))) | |
594 | ||
5609420f RB |
595 | /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */ |
596 | (for bitop (bit_and bit_ior bit_xor) | |
597 | (simplify | |
598 | (bitop (bit_and:c @0 @1) (bit_and @2 @1)) | |
599 | (bit_and (bitop @0 @2) @1))) | |
600 | ||
601 | /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ | |
602 | (simplify | |
603 | (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
604 | (bit_ior (bit_and @0 @2) (bit_and @1 @2))) | |
605 | ||
606 | /* Combine successive equal operations with constants. */ | |
607 | (for bitop (bit_and bit_ior bit_xor) | |
608 | (simplify | |
609 | (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
610 | (bitop @0 (bitop @1 @2)))) | |
611 | ||
612 | /* Try simple folding for X op !X, and X op X with the help | |
613 | of the truth_valued_p and logical_inverted_value predicates. */ | |
614 | (match truth_valued_p | |
615 | @0 | |
616 | (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))) | |
f84e7fd6 | 617 | (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor) |
5609420f RB |
618 | (match truth_valued_p |
619 | (op @0 @1))) | |
620 | (match truth_valued_p | |
621 | (truth_not @0)) | |
622 | ||
623 | (match (logical_inverted_value @0) | |
624 | (bit_not truth_valued_p@0)) | |
625 | (match (logical_inverted_value @0) | |
09240451 | 626 | (eq @0 integer_zerop)) |
5609420f | 627 | (match (logical_inverted_value @0) |
09240451 | 628 | (ne truth_valued_p@0 integer_truep)) |
5609420f | 629 | (match (logical_inverted_value @0) |
09240451 | 630 | (bit_xor truth_valued_p@0 integer_truep)) |
5609420f RB |
631 | |
632 | /* X & !X -> 0. */ | |
633 | (simplify | |
634 | (bit_and:c @0 (logical_inverted_value @0)) | |
635 | { build_zero_cst (type); }) | |
636 | /* X | !X and X ^ !X -> 1, , if X is truth-valued. */ | |
637 | (for op (bit_ior bit_xor) | |
638 | (simplify | |
639 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
f84e7fd6 | 640 | { constant_boolean_node (true, type); })) |
59c20dc7 RB |
641 | /* X ==/!= !X is false/true. */ |
642 | (for op (eq ne) | |
643 | (simplify | |
644 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
645 | { constant_boolean_node (op == NE_EXPR ? true : false, type); })) | |
5609420f | 646 | |
5609420f RB |
647 | /* If arg1 and arg2 are booleans (or any single bit type) |
648 | then try to simplify: | |
649 | ||
650 | (~X & Y) -> X < Y | |
651 | (X & ~Y) -> Y < X | |
652 | (~X | Y) -> X <= Y | |
653 | (X | ~Y) -> Y <= X | |
654 | ||
655 | But only do this if our result feeds into a comparison as | |
656 | this transformation is not always a win, particularly on | |
657 | targets with and-not instructions. | |
658 | -> simplify_bitwise_binary_boolean */ | |
659 | (simplify | |
660 | (ne (bit_and:c (bit_not @0) @1) integer_zerop) | |
661 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
662 | && TYPE_PRECISION (TREE_TYPE (@1)) == 1) | |
663 | (lt @0 @1))) | |
664 | (simplify | |
665 | (ne (bit_ior:c (bit_not @0) @1) integer_zerop) | |
666 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
667 | && TYPE_PRECISION (TREE_TYPE (@1)) == 1) | |
668 | (le @0 @1))) | |
669 | ||
5609420f RB |
670 | /* ~~x -> x */ |
671 | (simplify | |
672 | (bit_not (bit_not @0)) | |
673 | @0) | |
674 | ||
b14a9c57 RB |
675 | /* Convert ~ (-A) to A - 1. */ |
676 | (simplify | |
677 | (bit_not (convert? (negate @0))) | |
678 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
8b5ee871 | 679 | (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) |
b14a9c57 RB |
680 | |
681 | /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ | |
682 | (simplify | |
8b5ee871 | 683 | (bit_not (convert? (minus @0 integer_each_onep))) |
b14a9c57 RB |
684 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
685 | (convert (negate @0)))) | |
686 | (simplify | |
687 | (bit_not (convert? (plus @0 integer_all_onesp))) | |
688 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
689 | (convert (negate @0)))) | |
690 | ||
691 | /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ | |
692 | (simplify | |
693 | (bit_not (convert? (bit_xor @0 INTEGER_CST@1))) | |
694 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
695 | (convert (bit_xor @0 (bit_not @1))))) | |
696 | (simplify | |
697 | (bit_not (convert? (bit_xor:c (bit_not @0) @1))) | |
698 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
699 | (convert (bit_xor @0 @1)))) | |
700 | ||
f52baa7b MP |
701 | /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ |
702 | (simplify | |
44fc0a51 RB |
703 | (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2)) |
704 | (bit_xor (bit_and (bit_xor @0 @1) @2) @0)) | |
f52baa7b | 705 | |
f7b7b0aa MP |
706 | /* Fold A - (A & B) into ~B & A. */ |
707 | (simplify | |
708 | (minus (convert? @0) (convert?:s (bit_and:cs @0 @1))) | |
709 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
710 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
711 | (convert (bit_and (bit_not @1) @0)))) | |
5609420f | 712 | |
a499aac5 RB |
713 | /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */ |
714 | (simplify | |
44fc0a51 RB |
715 | (pointer_plus (pointer_plus:s @0 @1) @3) |
716 | (pointer_plus @0 (plus @1 @3))) | |
a499aac5 RB |
717 | |
718 | /* Pattern match | |
719 | tem1 = (long) ptr1; | |
720 | tem2 = (long) ptr2; | |
721 | tem3 = tem2 - tem1; | |
722 | tem4 = (unsigned long) tem3; | |
723 | tem5 = ptr1 + tem4; | |
724 | and produce | |
725 | tem5 = ptr2; */ | |
726 | (simplify | |
727 | (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0)))) | |
728 | /* Conditionally look through a sign-changing conversion. */ | |
729 | (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3)) | |
730 | && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1))) | |
731 | || (GENERIC && type == TREE_TYPE (@1)))) | |
732 | @1)) | |
733 | ||
734 | /* Pattern match | |
735 | tem = (sizetype) ptr; | |
736 | tem = tem & algn; | |
737 | tem = -tem; | |
738 | ... = ptr p+ tem; | |
739 | and produce the simpler and easier to analyze with respect to alignment | |
740 | ... = ptr & ~algn; */ | |
741 | (simplify | |
742 | (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1))) | |
743 | (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); } | |
744 | (bit_and @0 { algn; }))) | |
745 | ||
99e943a2 RB |
746 | /* Try folding difference of addresses. */ |
747 | (simplify | |
748 | (minus (convert ADDR_EXPR@0) (convert @1)) | |
749 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
750 | (with { HOST_WIDE_INT diff; } | |
751 | (if (ptr_difference_const (@0, @1, &diff)) | |
752 | { build_int_cst_type (type, diff); })))) | |
753 | (simplify | |
754 | (minus (convert @0) (convert ADDR_EXPR@1)) | |
755 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
756 | (with { HOST_WIDE_INT diff; } | |
757 | (if (ptr_difference_const (@0, @1, &diff)) | |
758 | { build_int_cst_type (type, diff); })))) | |
759 | ||
bab73f11 RB |
760 | /* If arg0 is derived from the address of an object or function, we may |
761 | be able to fold this expression using the object or function's | |
762 | alignment. */ | |
763 | (simplify | |
764 | (bit_and (convert? @0) INTEGER_CST@1) | |
765 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
766 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
767 | (with | |
768 | { | |
769 | unsigned int align; | |
770 | unsigned HOST_WIDE_INT bitpos; | |
771 | get_pointer_alignment_1 (@0, &align, &bitpos); | |
772 | } | |
773 | (if (wi::ltu_p (@1, align / BITS_PER_UNIT)) | |
774 | { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); })))) | |
99e943a2 | 775 | |
a499aac5 | 776 | |
cc7b5acf RB |
777 | /* We can't reassociate at all for saturating types. */ |
778 | (if (!TYPE_SATURATING (type)) | |
779 | ||
780 | /* Contract negates. */ | |
781 | /* A + (-B) -> A - B */ | |
782 | (simplify | |
783 | (plus:c (convert1? @0) (convert2? (negate @1))) | |
784 | /* Apply STRIP_NOPS on @0 and the negate. */ | |
785 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
786 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 787 | && !TYPE_OVERFLOW_SANITIZED (type)) |
cc7b5acf RB |
788 | (minus (convert @0) (convert @1)))) |
789 | /* A - (-B) -> A + B */ | |
790 | (simplify | |
791 | (minus (convert1? @0) (convert2? (negate @1))) | |
792 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
2f68e8bc | 793 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) |
6a4f0678 | 794 | && !TYPE_OVERFLOW_SANITIZED (type)) |
cc7b5acf RB |
795 | (plus (convert @0) (convert @1)))) |
796 | /* -(-A) -> A */ | |
797 | (simplify | |
798 | (negate (convert? (negate @1))) | |
799 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 800 | && !TYPE_OVERFLOW_SANITIZED (type)) |
a0f12cf8 | 801 | (convert @1))) |
cc7b5acf | 802 | |
7318e44f RB |
803 | /* We can't reassociate floating-point unless -fassociative-math |
804 | or fixed-point plus or minus because of saturation to +-Inf. */ | |
805 | (if ((!FLOAT_TYPE_P (type) || flag_associative_math) | |
806 | && !FIXED_POINT_TYPE_P (type)) | |
cc7b5acf RB |
807 | |
808 | /* Match patterns that allow contracting a plus-minus pair | |
809 | irrespective of overflow issues. */ | |
810 | /* (A +- B) - A -> +- B */ | |
811 | /* (A +- B) -+ B -> A */ | |
812 | /* A - (A +- B) -> -+ B */ | |
813 | /* A +- (B -+ A) -> +- B */ | |
814 | (simplify | |
815 | (minus (plus:c @0 @1) @0) | |
816 | @1) | |
817 | (simplify | |
818 | (minus (minus @0 @1) @0) | |
819 | (negate @1)) | |
820 | (simplify | |
821 | (plus:c (minus @0 @1) @1) | |
822 | @0) | |
823 | (simplify | |
824 | (minus @0 (plus:c @0 @1)) | |
825 | (negate @1)) | |
826 | (simplify | |
827 | (minus @0 (minus @0 @1)) | |
828 | @1) | |
829 | ||
830 | /* (A +- CST) +- CST -> A + CST */ | |
831 | (for outer_op (plus minus) | |
832 | (for inner_op (plus minus) | |
833 | (simplify | |
834 | (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
835 | /* If the constant operation overflows we cannot do the transform | |
836 | as we would introduce undefined overflow, for example | |
837 | with (a - 1) + INT_MIN. */ | |
838 | (with { tree cst = fold_binary (outer_op == inner_op | |
839 | ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); } | |
840 | (if (cst && !TREE_OVERFLOW (cst)) | |
841 | (inner_op @0 { cst; } )))))) | |
842 | ||
843 | /* (CST - A) +- CST -> CST - A */ | |
844 | (for outer_op (plus minus) | |
845 | (simplify | |
846 | (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2) | |
847 | (with { tree cst = fold_binary (outer_op, type, @1, @2); } | |
848 | (if (cst && !TREE_OVERFLOW (cst)) | |
849 | (minus { cst; } @0))))) | |
850 | ||
851 | /* ~A + A -> -1 */ | |
852 | (simplify | |
853 | (plus:c (bit_not @0) @0) | |
854 | (if (!TYPE_OVERFLOW_TRAPS (type)) | |
855 | { build_all_ones_cst (type); })) | |
856 | ||
857 | /* ~A + 1 -> -A */ | |
858 | (simplify | |
e19740ae RB |
859 | (plus (convert? (bit_not @0)) integer_each_onep) |
860 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
861 | (negate (convert @0)))) | |
862 | ||
863 | /* -A - 1 -> ~A */ | |
864 | (simplify | |
865 | (minus (convert? (negate @0)) integer_each_onep) | |
866 | (if (!TYPE_OVERFLOW_TRAPS (type) | |
867 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
868 | (bit_not (convert @0)))) | |
869 | ||
870 | /* -1 - A -> ~A */ | |
871 | (simplify | |
872 | (minus integer_all_onesp @0) | |
bc4315fb | 873 | (bit_not @0)) |
cc7b5acf RB |
874 | |
875 | /* (T)(P + A) - (T)P -> (T) A */ | |
876 | (for add (plus pointer_plus) | |
877 | (simplify | |
878 | (minus (convert (add @0 @1)) | |
879 | (convert @0)) | |
09240451 | 880 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
cc7b5acf RB |
881 | /* For integer types, if A has a smaller type |
882 | than T the result depends on the possible | |
883 | overflow in P + A. | |
884 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
885 | However, if an overflow in P + A would cause | |
886 | undefined behavior, we can assume that there | |
887 | is no overflow. */ | |
888 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
889 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
890 | /* For pointer types, if the conversion of A to the | |
891 | final type requires a sign- or zero-extension, | |
892 | then we have to punt - it is not defined which | |
893 | one is correct. */ | |
894 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
895 | && TREE_CODE (@1) == INTEGER_CST | |
896 | && tree_int_cst_sign_bit (@1) == 0)) | |
897 | (convert @1)))))) | |
898 | ||
899 | ||
a7f24614 RB |
900 | /* Simplifications of MIN_EXPR and MAX_EXPR. */ |
901 | ||
902 | (for minmax (min max) | |
903 | (simplify | |
904 | (minmax @0 @0) | |
905 | @0)) | |
906 | (simplify | |
907 | (min @0 @1) | |
908 | (if (INTEGRAL_TYPE_P (type) | |
909 | && TYPE_MIN_VALUE (type) | |
910 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
911 | @1)) | |
912 | (simplify | |
913 | (max @0 @1) | |
914 | (if (INTEGRAL_TYPE_P (type) | |
915 | && TYPE_MAX_VALUE (type) | |
916 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
917 | @1)) | |
918 | ||
919 | ||
920 | /* Simplifications of shift and rotates. */ | |
921 | ||
922 | (for rotate (lrotate rrotate) | |
923 | (simplify | |
924 | (rotate integer_all_onesp@0 @1) | |
925 | @0)) | |
926 | ||
927 | /* Optimize -1 >> x for arithmetic right shifts. */ | |
928 | (simplify | |
929 | (rshift integer_all_onesp@0 @1) | |
930 | (if (!TYPE_UNSIGNED (type) | |
931 | && tree_expr_nonnegative_p (@1)) | |
932 | @0)) | |
933 | ||
12085390 N |
934 | /* Optimize (x >> c) << c into x & (-1<<c). */ |
935 | (simplify | |
936 | (lshift (rshift @0 INTEGER_CST@1) @1) | |
937 | (if (wi::ltu_p (@1, element_precision (type))) | |
938 | (bit_and @0 (lshift { build_minus_one_cst (type); } @1)))) | |
939 | ||
940 | /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned | |
941 | types. */ | |
942 | (simplify | |
943 | (rshift (lshift @0 INTEGER_CST@1) @1) | |
944 | (if (TYPE_UNSIGNED (type) | |
945 | && (wi::ltu_p (@1, element_precision (type)))) | |
946 | (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) | |
947 | ||
a7f24614 RB |
948 | (for shiftrotate (lrotate rrotate lshift rshift) |
949 | (simplify | |
950 | (shiftrotate @0 integer_zerop) | |
951 | (non_lvalue @0)) | |
952 | (simplify | |
953 | (shiftrotate integer_zerop@0 @1) | |
954 | @0) | |
955 | /* Prefer vector1 << scalar to vector1 << vector2 | |
956 | if vector2 is uniform. */ | |
957 | (for vec (VECTOR_CST CONSTRUCTOR) | |
958 | (simplify | |
959 | (shiftrotate @0 vec@1) | |
960 | (with { tree tem = uniform_vector_p (@1); } | |
961 | (if (tem) | |
962 | (shiftrotate @0 { tem; })))))) | |
963 | ||
964 | /* Rewrite an LROTATE_EXPR by a constant into an | |
965 | RROTATE_EXPR by a new constant. */ | |
966 | (simplify | |
967 | (lrotate @0 INTEGER_CST@1) | |
968 | (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1), | |
969 | build_int_cst (TREE_TYPE (@1), | |
970 | element_precision (type)), @1); })) | |
971 | ||
14ea9f92 RB |
972 | /* Turn (a OP c1) OP c2 into a OP (c1+c2). */ |
973 | (for op (lrotate rrotate rshift lshift) | |
974 | (simplify | |
975 | (op (op @0 INTEGER_CST@1) INTEGER_CST@2) | |
976 | (with { unsigned int prec = element_precision (type); } | |
977 | (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))) | |
978 | && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1))) | |
979 | && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2))) | |
980 | && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2)))) | |
981 | (with { unsigned int low = wi::add (@1, @2).to_uhwi (); } | |
982 | /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2 | |
983 | being well defined. */ | |
984 | (if (low >= prec) | |
985 | (if (op == LROTATE_EXPR || op == RROTATE_EXPR) | |
8fdc6c67 | 986 | (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); }) |
50301115 | 987 | (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR) |
8fdc6c67 RB |
988 | { build_zero_cst (type); } |
989 | (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); }))) | |
990 | (op @0 { build_int_cst (TREE_TYPE (@1), low); }))))))) | |
14ea9f92 RB |
991 | |
992 | ||
01ada710 MP |
993 | /* ((1 << A) & 1) != 0 -> A == 0 |
994 | ((1 << A) & 1) == 0 -> A != 0 */ | |
995 | (for cmp (ne eq) | |
996 | icmp (eq ne) | |
997 | (simplify | |
998 | (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop) | |
999 | (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))) | |
cc7b5acf | 1000 | |
f2e609c3 MP |
1001 | /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) |
1002 | (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) | |
1003 | if CST2 != 0. */ | |
1004 | (for cmp (ne eq) | |
1005 | (simplify | |
1006 | (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2) | |
1007 | (with { int cand = wi::ctz (@2) - wi::ctz (@0); } | |
1008 | (if (cand < 0 | |
1009 | || (!integer_zerop (@2) | |
1010 | && wi::ne_p (wi::lshift (@0, cand), @2))) | |
8fdc6c67 RB |
1011 | { constant_boolean_node (cmp == NE_EXPR, type); } |
1012 | (if (!integer_zerop (@2) | |
1013 | && wi::eq_p (wi::lshift (@0, cand), @2)) | |
1014 | (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); })))))) | |
f2e609c3 | 1015 | |
1ffbaa3f RB |
1016 | /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) |
1017 | (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) | |
1018 | if the new mask might be further optimized. */ | |
1019 | (for shift (lshift rshift) | |
1020 | (simplify | |
44fc0a51 RB |
1021 | (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1)) |
1022 | INTEGER_CST@2) | |
1ffbaa3f RB |
1023 | (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5)) |
1024 | && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT | |
1025 | && tree_fits_uhwi_p (@1) | |
1026 | && tree_to_uhwi (@1) > 0 | |
1027 | && tree_to_uhwi (@1) < TYPE_PRECISION (type)) | |
1028 | (with | |
1029 | { | |
1030 | unsigned int shiftc = tree_to_uhwi (@1); | |
1031 | unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2); | |
1032 | unsigned HOST_WIDE_INT newmask, zerobits = 0; | |
1033 | tree shift_type = TREE_TYPE (@3); | |
1034 | unsigned int prec; | |
1035 | ||
1036 | if (shift == LSHIFT_EXPR) | |
1037 | zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1); | |
1038 | else if (shift == RSHIFT_EXPR | |
1039 | && (TYPE_PRECISION (shift_type) | |
1040 | == GET_MODE_PRECISION (TYPE_MODE (shift_type)))) | |
1041 | { | |
1042 | prec = TYPE_PRECISION (TREE_TYPE (@3)); | |
1043 | tree arg00 = @0; | |
1044 | /* See if more bits can be proven as zero because of | |
1045 | zero extension. */ | |
1046 | if (@3 != @0 | |
1047 | && TYPE_UNSIGNED (TREE_TYPE (@0))) | |
1048 | { | |
1049 | tree inner_type = TREE_TYPE (@0); | |
1050 | if ((TYPE_PRECISION (inner_type) | |
1051 | == GET_MODE_PRECISION (TYPE_MODE (inner_type))) | |
1052 | && TYPE_PRECISION (inner_type) < prec) | |
1053 | { | |
1054 | prec = TYPE_PRECISION (inner_type); | |
1055 | /* See if we can shorten the right shift. */ | |
1056 | if (shiftc < prec) | |
1057 | shift_type = inner_type; | |
1058 | /* Otherwise X >> C1 is all zeros, so we'll optimize | |
1059 | it into (X, 0) later on by making sure zerobits | |
1060 | is all ones. */ | |
1061 | } | |
1062 | } | |
1063 | zerobits = ~(unsigned HOST_WIDE_INT) 0; | |
1064 | if (shiftc < prec) | |
1065 | { | |
1066 | zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; | |
1067 | zerobits <<= prec - shiftc; | |
1068 | } | |
1069 | /* For arithmetic shift if sign bit could be set, zerobits | |
1070 | can contain actually sign bits, so no transformation is | |
1071 | possible, unless MASK masks them all away. In that | |
1072 | case the shift needs to be converted into logical shift. */ | |
1073 | if (!TYPE_UNSIGNED (TREE_TYPE (@3)) | |
1074 | && prec == TYPE_PRECISION (TREE_TYPE (@3))) | |
1075 | { | |
1076 | if ((mask & zerobits) == 0) | |
1077 | shift_type = unsigned_type_for (TREE_TYPE (@3)); | |
1078 | else | |
1079 | zerobits = 0; | |
1080 | } | |
1081 | } | |
1082 | } | |
1083 | /* ((X << 16) & 0xff00) is (X, 0). */ | |
1084 | (if ((mask & zerobits) == mask) | |
8fdc6c67 RB |
1085 | { build_int_cst (type, 0); } |
1086 | (with { newmask = mask | zerobits; } | |
1087 | (if (newmask != mask && (newmask & (newmask + 1)) == 0) | |
1088 | (with | |
1089 | { | |
1090 | /* Only do the transformation if NEWMASK is some integer | |
1091 | mode's mask. */ | |
1092 | for (prec = BITS_PER_UNIT; | |
1093 | prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) | |
1094 | if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1) | |
1095 | break; | |
1096 | } | |
1097 | (if (prec < HOST_BITS_PER_WIDE_INT | |
1098 | || newmask == ~(unsigned HOST_WIDE_INT) 0) | |
1099 | (with | |
1100 | { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); } | |
1101 | (if (!tree_int_cst_equal (newmaskt, @2)) | |
1102 | (if (shift_type != TREE_TYPE (@3)) | |
1103 | (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; }) | |
1104 | (bit_and @4 { newmaskt; }))))))))))))) | |
1ffbaa3f | 1105 | |
98e30e51 RB |
1106 | /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1) |
1107 | (X & C2) >> C1 into (X >> C1) & (C2 >> C1). */ | |
1108 | (for shift (lshift rshift) | |
1109 | (simplify | |
2d799646 | 1110 | (shift (convert?:s (bit_and:s @0 INTEGER_CST@2)) INTEGER_CST@1) |
98e30e51 RB |
1111 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
1112 | (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); } | |
1113 | (bit_and (shift (convert @0) @1) { mask; }))))) | |
1114 | ||
1115 | ||
d4573ffe RB |
1116 | /* Simplifications of conversions. */ |
1117 | ||
1118 | /* Basic strip-useless-type-conversions / strip_nops. */ | |
f3582e54 | 1119 | (for cvt (convert view_convert float fix_trunc) |
d4573ffe RB |
1120 | (simplify |
1121 | (cvt @0) | |
1122 | (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0))) | |
1123 | || (GENERIC && type == TREE_TYPE (@0))) | |
1124 | @0))) | |
1125 | ||
1126 | /* Contract view-conversions. */ | |
1127 | (simplify | |
1128 | (view_convert (view_convert @0)) | |
1129 | (view_convert @0)) | |
1130 | ||
1131 | /* For integral conversions with the same precision or pointer | |
1132 | conversions use a NOP_EXPR instead. */ | |
1133 | (simplify | |
1134 | (view_convert @0) | |
1135 | (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)) | |
1136 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
1137 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))) | |
1138 | (convert @0))) | |
1139 | ||
1140 | /* Strip inner integral conversions that do not change precision or size. */ | |
1141 | (simplify | |
1142 | (view_convert (convert@0 @1)) | |
1143 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
1144 | && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
1145 | && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
1146 | && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1)))) | |
1147 | (view_convert @1))) | |
1148 | ||
1149 | /* Re-association barriers around constants and other re-association | |
1150 | barriers can be removed. */ | |
1151 | (simplify | |
1152 | (paren CONSTANT_CLASS_P@0) | |
1153 | @0) | |
1154 | (simplify | |
1155 | (paren (paren@1 @0)) | |
1156 | @1) | |
1e51d0a2 RB |
1157 | |
1158 | /* Handle cases of two conversions in a row. */ | |
1159 | (for ocvt (convert float fix_trunc) | |
1160 | (for icvt (convert float) | |
1161 | (simplify | |
1162 | (ocvt (icvt@1 @0)) | |
1163 | (with | |
1164 | { | |
1165 | tree inside_type = TREE_TYPE (@0); | |
1166 | tree inter_type = TREE_TYPE (@1); | |
1167 | int inside_int = INTEGRAL_TYPE_P (inside_type); | |
1168 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
1169 | int inside_float = FLOAT_TYPE_P (inside_type); | |
09240451 | 1170 | int inside_vec = VECTOR_TYPE_P (inside_type); |
1e51d0a2 RB |
1171 | unsigned int inside_prec = TYPE_PRECISION (inside_type); |
1172 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
1173 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
1174 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
1175 | int inter_float = FLOAT_TYPE_P (inter_type); | |
09240451 | 1176 | int inter_vec = VECTOR_TYPE_P (inter_type); |
1e51d0a2 RB |
1177 | unsigned int inter_prec = TYPE_PRECISION (inter_type); |
1178 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
1179 | int final_int = INTEGRAL_TYPE_P (type); | |
1180 | int final_ptr = POINTER_TYPE_P (type); | |
1181 | int final_float = FLOAT_TYPE_P (type); | |
09240451 | 1182 | int final_vec = VECTOR_TYPE_P (type); |
1e51d0a2 RB |
1183 | unsigned int final_prec = TYPE_PRECISION (type); |
1184 | int final_unsignedp = TYPE_UNSIGNED (type); | |
1185 | } | |
64d3a1f0 RB |
1186 | (switch |
1187 | /* In addition to the cases of two conversions in a row | |
1188 | handled below, if we are converting something to its own | |
1189 | type via an object of identical or wider precision, neither | |
1190 | conversion is needed. */ | |
1191 | (if (((GIMPLE && useless_type_conversion_p (type, inside_type)) | |
1192 | || (GENERIC | |
1193 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type))) | |
1194 | && (((inter_int || inter_ptr) && final_int) | |
1195 | || (inter_float && final_float)) | |
1196 | && inter_prec >= final_prec) | |
1197 | (ocvt @0)) | |
1198 | ||
1199 | /* Likewise, if the intermediate and initial types are either both | |
1200 | float or both integer, we don't need the middle conversion if the | |
1201 | former is wider than the latter and doesn't change the signedness | |
1202 | (for integers). Avoid this if the final type is a pointer since | |
1203 | then we sometimes need the middle conversion. Likewise if the | |
1204 | final type has a precision not equal to the size of its mode. */ | |
1205 | (if (((inter_int && inside_int) || (inter_float && inside_float)) | |
1206 | && (final_int || final_float) | |
1207 | && inter_prec >= inside_prec | |
1208 | && (inter_float || inter_unsignedp == inside_unsignedp) | |
1209 | && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type)) | |
1210 | && TYPE_MODE (type) == TYPE_MODE (inter_type))) | |
1211 | (ocvt @0)) | |
1212 | ||
1213 | /* If we have a sign-extension of a zero-extended value, we can | |
1214 | replace that by a single zero-extension. Likewise if the | |
1215 | final conversion does not change precision we can drop the | |
1216 | intermediate conversion. */ | |
1217 | (if (inside_int && inter_int && final_int | |
1218 | && ((inside_prec < inter_prec && inter_prec < final_prec | |
1219 | && inside_unsignedp && !inter_unsignedp) | |
1220 | || final_prec == inter_prec)) | |
1221 | (ocvt @0)) | |
1222 | ||
1223 | /* Two conversions in a row are not needed unless: | |
1e51d0a2 RB |
1224 | - some conversion is floating-point (overstrict for now), or |
1225 | - some conversion is a vector (overstrict for now), or | |
1226 | - the intermediate type is narrower than both initial and | |
1227 | final, or | |
1228 | - the intermediate type and innermost type differ in signedness, | |
1229 | and the outermost type is wider than the intermediate, or | |
1230 | - the initial type is a pointer type and the precisions of the | |
1231 | intermediate and final types differ, or | |
1232 | - the final type is a pointer type and the precisions of the | |
1233 | initial and intermediate types differ. */ | |
64d3a1f0 RB |
1234 | (if (! inside_float && ! inter_float && ! final_float |
1235 | && ! inside_vec && ! inter_vec && ! final_vec | |
1236 | && (inter_prec >= inside_prec || inter_prec >= final_prec) | |
1237 | && ! (inside_int && inter_int | |
1238 | && inter_unsignedp != inside_unsignedp | |
1239 | && inter_prec < final_prec) | |
1240 | && ((inter_unsignedp && inter_prec > inside_prec) | |
1241 | == (final_unsignedp && final_prec > inter_prec)) | |
1242 | && ! (inside_ptr && inter_prec != final_prec) | |
1243 | && ! (final_ptr && inside_prec != inter_prec) | |
1244 | && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type)) | |
1245 | && TYPE_MODE (type) == TYPE_MODE (inter_type))) | |
1246 | (ocvt @0)) | |
1247 | ||
1248 | /* A truncation to an unsigned type (a zero-extension) should be | |
1249 | canonicalized as bitwise and of a mask. */ | |
1250 | (if (final_int && inter_int && inside_int | |
1251 | && final_prec == inside_prec | |
1252 | && final_prec > inter_prec | |
1253 | && inter_unsignedp) | |
1254 | (convert (bit_and @0 { wide_int_to_tree | |
1255 | (inside_type, | |
1256 | wi::mask (inter_prec, false, | |
1257 | TYPE_PRECISION (inside_type))); }))) | |
1258 | ||
1259 | /* If we are converting an integer to a floating-point that can | |
1260 | represent it exactly and back to an integer, we can skip the | |
1261 | floating-point conversion. */ | |
1262 | (if (GIMPLE /* PR66211 */ | |
1263 | && inside_int && inter_float && final_int && | |
1264 | (unsigned) significand_size (TYPE_MODE (inter_type)) | |
1265 | >= inside_prec - !inside_unsignedp) | |
1266 | (convert @0))))))) | |
ea2042ba RB |
1267 | |
1268 | /* If we have a narrowing conversion to an integral type that is fed by a | |
1269 | BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely | |
1270 | masks off bits outside the final type (and nothing else). */ | |
1271 | (simplify | |
1272 | (convert (bit_and @0 INTEGER_CST@1)) | |
1273 | (if (INTEGRAL_TYPE_P (type) | |
1274 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1275 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) | |
1276 | && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1), | |
1277 | TYPE_PRECISION (type)), 0)) | |
1278 | (convert @0))) | |
a25454ea RB |
1279 | |
1280 | ||
1281 | /* (X /[ex] A) * A -> X. */ | |
1282 | (simplify | |
1283 | (mult (convert? (exact_div @0 @1)) @1) | |
1284 | /* Look through a sign-changing conversion. */ | |
257b01ba | 1285 | (convert @0)) |
eaeba53a | 1286 | |
a7f24614 RB |
1287 | /* Canonicalization of binary operations. */ |
1288 | ||
1289 | /* Convert X + -C into X - C. */ | |
1290 | (simplify | |
1291 | (plus @0 REAL_CST@1) | |
1292 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
1293 | (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); } | |
1294 | (if (!TREE_OVERFLOW (tem) || !flag_trapping_math) | |
1295 | (minus @0 { tem; }))))) | |
1296 | ||
1297 | /* Convert x+x into x*2.0. */ | |
1298 | (simplify | |
1299 | (plus @0 @0) | |
1300 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
1301 | (mult @0 { build_real (type, dconst2); }))) | |
1302 | ||
1303 | (simplify | |
1304 | (minus integer_zerop @1) | |
1305 | (negate @1)) | |
1306 | ||
1307 | /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether | |
1308 | ARG0 is zero and X + ARG0 reduces to X, since that would mean | |
1309 | (-ARG1 + ARG0) reduces to -ARG1. */ | |
1310 | (simplify | |
1311 | (minus real_zerop@0 @1) | |
1312 | (if (fold_real_zero_addition_p (type, @0, 0)) | |
1313 | (negate @1))) | |
1314 | ||
1315 | /* Transform x * -1 into -x. */ | |
1316 | (simplify | |
1317 | (mult @0 integer_minus_onep) | |
1318 | (negate @0)) | |
eaeba53a RB |
1319 | |
1320 | /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */ | |
1321 | (simplify | |
1322 | (complex (realpart @0) (imagpart @0)) | |
1323 | @0) | |
1324 | (simplify | |
1325 | (realpart (complex @0 @1)) | |
1326 | @0) | |
1327 | (simplify | |
1328 | (imagpart (complex @0 @1)) | |
1329 | @1) | |
83633539 | 1330 | |
77c028c5 MG |
1331 | /* Sometimes we only care about half of a complex expression. */ |
1332 | (simplify | |
1333 | (realpart (convert?:s (conj:s @0))) | |
1334 | (convert (realpart @0))) | |
1335 | (simplify | |
1336 | (imagpart (convert?:s (conj:s @0))) | |
1337 | (convert (negate (imagpart @0)))) | |
1338 | (for part (realpart imagpart) | |
1339 | (for op (plus minus) | |
1340 | (simplify | |
1341 | (part (convert?:s@2 (op:s @0 @1))) | |
1342 | (convert (op (part @0) (part @1)))))) | |
1343 | (simplify | |
1344 | (realpart (convert?:s (CEXPI:s @0))) | |
1345 | (convert (COS @0))) | |
1346 | (simplify | |
1347 | (imagpart (convert?:s (CEXPI:s @0))) | |
1348 | (convert (SIN @0))) | |
1349 | ||
1350 | /* conj(conj(x)) -> x */ | |
1351 | (simplify | |
1352 | (conj (convert? (conj @0))) | |
1353 | (if (tree_nop_conversion_p (TREE_TYPE (@0), type)) | |
1354 | (convert @0))) | |
1355 | ||
1356 | /* conj({x,y}) -> {x,-y} */ | |
1357 | (simplify | |
1358 | (conj (convert?:s (complex:s @0 @1))) | |
1359 | (with { tree itype = TREE_TYPE (type); } | |
1360 | (complex (convert:itype @0) (negate (convert:itype @1))))) | |
83633539 RB |
1361 | |
1362 | /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */ | |
1363 | (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64) | |
1364 | (simplify | |
1365 | (bswap (bswap @0)) | |
1366 | @0) | |
1367 | (simplify | |
1368 | (bswap (bit_not (bswap @0))) | |
1369 | (bit_not @0)) | |
1370 | (for bitop (bit_xor bit_ior bit_and) | |
1371 | (simplify | |
1372 | (bswap (bitop:c (bswap @0) @1)) | |
1373 | (bitop @0 (bswap @1))))) | |
96994de0 RB |
1374 | |
1375 | ||
1376 | /* Combine COND_EXPRs and VEC_COND_EXPRs. */ | |
1377 | ||
1378 | /* Simplify constant conditions. | |
1379 | Only optimize constant conditions when the selected branch | |
1380 | has the same type as the COND_EXPR. This avoids optimizing | |
1381 | away "c ? x : throw", where the throw has a void type. | |
1382 | Note that we cannot throw away the fold-const.c variant nor | |
1383 | this one as we depend on doing this transform before possibly | |
1384 | A ? B : B -> B triggers and the fold-const.c one can optimize | |
1385 | 0 ? A : B to B even if A has side-effects. Something | |
1386 | genmatch cannot handle. */ | |
1387 | (simplify | |
1388 | (cond INTEGER_CST@0 @1 @2) | |
8fdc6c67 RB |
1389 | (if (integer_zerop (@0)) |
1390 | (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type)) | |
1391 | @2) | |
1392 | (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type)) | |
1393 | @1))) | |
96994de0 RB |
1394 | (simplify |
1395 | (vec_cond VECTOR_CST@0 @1 @2) | |
1396 | (if (integer_all_onesp (@0)) | |
8fdc6c67 RB |
1397 | @1 |
1398 | (if (integer_zerop (@0)) | |
1399 | @2))) | |
96994de0 RB |
1400 | |
1401 | (for cnd (cond vec_cond) | |
1402 | /* A ? B : (A ? X : C) -> A ? B : C. */ | |
1403 | (simplify | |
1404 | (cnd @0 (cnd @0 @1 @2) @3) | |
1405 | (cnd @0 @1 @3)) | |
1406 | (simplify | |
1407 | (cnd @0 @1 (cnd @0 @2 @3)) | |
1408 | (cnd @0 @1 @3)) | |
1409 | ||
1410 | /* A ? B : B -> B. */ | |
1411 | (simplify | |
1412 | (cnd @0 @1 @1) | |
09240451 | 1413 | @1) |
96994de0 | 1414 | |
09240451 MG |
1415 | /* !A ? B : C -> A ? C : B. */ |
1416 | (simplify | |
1417 | (cnd (logical_inverted_value truth_valued_p@0) @1 @2) | |
1418 | (cnd @0 @2 @1))) | |
f84e7fd6 | 1419 | |
f43d102e RS |
1420 | /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons |
1421 | return all-1 or all-0 results. */ | |
1422 | /* ??? We could instead convert all instances of the vec_cond to negate, | |
1423 | but that isn't necessarily a win on its own. */ | |
1424 | (simplify | |
1425 | (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2))) | |
1426 | (if (VECTOR_TYPE_P (type) | |
1427 | && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)) | |
1428 | && (TYPE_MODE (TREE_TYPE (type)) | |
1429 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0))))) | |
1430 | (minus @3 (view_convert @0)))) | |
1431 | ||
1432 | /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */ | |
1433 | (simplify | |
1434 | (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2))) | |
1435 | (if (VECTOR_TYPE_P (type) | |
1436 | && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)) | |
1437 | && (TYPE_MODE (TREE_TYPE (type)) | |
1438 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0))))) | |
1439 | (plus @3 (view_convert @0)))) | |
f84e7fd6 | 1440 | |
2ee05f1e | 1441 | |
f84e7fd6 RB |
1442 | /* Simplifications of comparisons. */ |
1443 | ||
24f1db9c RB |
1444 | /* See if we can reduce the magnitude of a constant involved in a |
1445 | comparison by changing the comparison code. This is a canonicalization | |
1446 | formerly done by maybe_canonicalize_comparison_1. */ | |
1447 | (for cmp (le gt) | |
1448 | acmp (lt ge) | |
1449 | (simplify | |
1450 | (cmp @0 INTEGER_CST@1) | |
1451 | (if (tree_int_cst_sgn (@1) == -1) | |
1452 | (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); })))) | |
1453 | (for cmp (ge lt) | |
1454 | acmp (gt le) | |
1455 | (simplify | |
1456 | (cmp @0 INTEGER_CST@1) | |
1457 | (if (tree_int_cst_sgn (@1) == 1) | |
1458 | (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); })))) | |
1459 | ||
1460 | ||
f84e7fd6 RB |
1461 | /* We can simplify a logical negation of a comparison to the |
1462 | inverted comparison. As we cannot compute an expression | |
1463 | operator using invert_tree_comparison we have to simulate | |
1464 | that with expression code iteration. */ | |
1465 | (for cmp (tcc_comparison) | |
1466 | icmp (inverted_tcc_comparison) | |
1467 | ncmp (inverted_tcc_comparison_with_nans) | |
1468 | /* Ideally we'd like to combine the following two patterns | |
1469 | and handle some more cases by using | |
1470 | (logical_inverted_value (cmp @0 @1)) | |
1471 | here but for that genmatch would need to "inline" that. | |
1472 | For now implement what forward_propagate_comparison did. */ | |
1473 | (simplify | |
1474 | (bit_not (cmp @0 @1)) | |
1475 | (if (VECTOR_TYPE_P (type) | |
1476 | || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)) | |
1477 | /* Comparison inversion may be impossible for trapping math, | |
1478 | invert_tree_comparison will tell us. But we can't use | |
1479 | a computed operator in the replacement tree thus we have | |
1480 | to play the trick below. */ | |
1481 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 1482 | (cmp, HONOR_NANS (@0)); } |
f84e7fd6 | 1483 | (if (ic == icmp) |
8fdc6c67 RB |
1484 | (icmp @0 @1) |
1485 | (if (ic == ncmp) | |
1486 | (ncmp @0 @1)))))) | |
f84e7fd6 | 1487 | (simplify |
09240451 MG |
1488 | (bit_xor (cmp @0 @1) integer_truep) |
1489 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 1490 | (cmp, HONOR_NANS (@0)); } |
09240451 | 1491 | (if (ic == icmp) |
8fdc6c67 RB |
1492 | (icmp @0 @1) |
1493 | (if (ic == ncmp) | |
1494 | (ncmp @0 @1)))))) | |
e18c1d66 | 1495 | |
2ee05f1e RB |
1496 | /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. |
1497 | ??? The transformation is valid for the other operators if overflow | |
1498 | is undefined for the type, but performing it here badly interacts | |
1499 | with the transformation in fold_cond_expr_with_comparison which | |
1500 | attempts to synthetize ABS_EXPR. */ | |
1501 | (for cmp (eq ne) | |
1502 | (simplify | |
d9ba1961 RB |
1503 | (cmp (minus@2 @0 @1) integer_zerop) |
1504 | (if (single_use (@2)) | |
1505 | (cmp @0 @1)))) | |
2ee05f1e RB |
1506 | |
1507 | /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the | |
1508 | signed arithmetic case. That form is created by the compiler | |
1509 | often enough for folding it to be of value. One example is in | |
1510 | computing loop trip counts after Operator Strength Reduction. */ | |
07cdc2b8 RB |
1511 | (for cmp (simple_comparison) |
1512 | scmp (swapped_simple_comparison) | |
2ee05f1e RB |
1513 | (simplify |
1514 | (cmp (mult @0 INTEGER_CST@1) integer_zerop@2) | |
1515 | /* Handle unfolded multiplication by zero. */ | |
1516 | (if (integer_zerop (@1)) | |
8fdc6c67 RB |
1517 | (cmp @1 @2) |
1518 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1519 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1520 | /* If @1 is negative we swap the sense of the comparison. */ | |
1521 | (if (tree_int_cst_sgn (@1) < 0) | |
1522 | (scmp @0 @2) | |
1523 | (cmp @0 @2)))))) | |
2ee05f1e RB |
1524 | |
1525 | /* Simplify comparison of something with itself. For IEEE | |
1526 | floating-point, we can only do some of these simplifications. */ | |
1527 | (simplify | |
1528 | (eq @0 @0) | |
1529 | (if (! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1530 | || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0)))) | |
1531 | { constant_boolean_node (true, type); })) | |
1532 | (for cmp (ge le) | |
1533 | (simplify | |
1534 | (cmp @0 @0) | |
1535 | (eq @0 @0))) | |
1536 | (for cmp (ne gt lt) | |
1537 | (simplify | |
1538 | (cmp @0 @0) | |
1539 | (if (cmp != NE_EXPR | |
1540 | || ! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1541 | || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0)))) | |
1542 | { constant_boolean_node (false, type); }))) | |
b5d3d787 RB |
1543 | (for cmp (unle unge uneq) |
1544 | (simplify | |
1545 | (cmp @0 @0) | |
1546 | { constant_boolean_node (true, type); })) | |
1547 | (simplify | |
1548 | (ltgt @0 @0) | |
1549 | (if (!flag_trapping_math) | |
1550 | { constant_boolean_node (false, type); })) | |
2ee05f1e RB |
1551 | |
1552 | /* Fold ~X op ~Y as Y op X. */ | |
07cdc2b8 | 1553 | (for cmp (simple_comparison) |
2ee05f1e RB |
1554 | (simplify |
1555 | (cmp (bit_not @0) (bit_not @1)) | |
1556 | (cmp @1 @0))) | |
1557 | ||
1558 | /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ | |
07cdc2b8 RB |
1559 | (for cmp (simple_comparison) |
1560 | scmp (swapped_simple_comparison) | |
2ee05f1e RB |
1561 | (simplify |
1562 | (cmp (bit_not @0) CONSTANT_CLASS_P@1) | |
1563 | (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST) | |
1564 | (scmp @0 (bit_not @1))))) | |
1565 | ||
07cdc2b8 RB |
1566 | (for cmp (simple_comparison) |
1567 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
1568 | (simplify | |
1569 | (cmp (convert@2 @0) (convert? @1)) | |
1570 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1571 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
1572 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) | |
1573 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
1574 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))) | |
1575 | (with | |
1576 | { | |
1577 | tree type1 = TREE_TYPE (@1); | |
1578 | if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1)) | |
1579 | { | |
1580 | REAL_VALUE_TYPE orig = TREE_REAL_CST (@1); | |
1581 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node) | |
1582 | && exact_real_truncate (TYPE_MODE (float_type_node), &orig)) | |
1583 | type1 = float_type_node; | |
1584 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node) | |
1585 | && exact_real_truncate (TYPE_MODE (double_type_node), &orig)) | |
1586 | type1 = double_type_node; | |
1587 | } | |
1588 | tree newtype | |
1589 | = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1) | |
1590 | ? TREE_TYPE (@0) : type1); | |
1591 | } | |
1592 | (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype)) | |
1593 | (cmp (convert:newtype @0) (convert:newtype @1)))))) | |
1594 | ||
1595 | (simplify | |
1596 | (cmp @0 REAL_CST@1) | |
1597 | /* IEEE doesn't distinguish +0 and -0 in comparisons. */ | |
64d3a1f0 RB |
1598 | (switch |
1599 | /* a CMP (-0) -> a CMP 0 */ | |
1600 | (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))) | |
1601 | (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })) | |
1602 | /* x != NaN is always true, other ops are always false. */ | |
1603 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
1604 | && ! HONOR_SNANS (@1)) | |
1605 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
1606 | /* Fold comparisons against infinity. */ | |
1607 | (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1)) | |
1608 | && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1)))) | |
1609 | (with | |
1610 | { | |
1611 | REAL_VALUE_TYPE max; | |
1612 | enum tree_code code = cmp; | |
1613 | bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)); | |
1614 | if (neg) | |
1615 | code = swap_tree_comparison (code); | |
1616 | } | |
1617 | (switch | |
1618 | /* x > +Inf is always false, if with ignore sNANs. */ | |
1619 | (if (code == GT_EXPR | |
1620 | && ! HONOR_SNANS (@0)) | |
1621 | { constant_boolean_node (false, type); }) | |
1622 | (if (code == LE_EXPR) | |
1623 | /* x <= +Inf is always true, if we don't case about NaNs. */ | |
1624 | (if (! HONOR_NANS (@0)) | |
1625 | { constant_boolean_node (true, type); } | |
b0eb889b | 1626 | /* x <= +Inf is the same as x == x, i.e. !isnan(x). */ |
64d3a1f0 RB |
1627 | (eq @0 @0))) |
1628 | /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */ | |
1629 | (if (code == EQ_EXPR || code == GE_EXPR) | |
1630 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
1631 | (if (neg) | |
1632 | (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
1633 | (gt @0 { build_real (TREE_TYPE (@0), max); })))) | |
1634 | /* x < +Inf is always equal to x <= DBL_MAX. */ | |
1635 | (if (code == LT_EXPR) | |
1636 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
1637 | (if (neg) | |
1638 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
1639 | (le @0 { build_real (TREE_TYPE (@0), max); })))) | |
1640 | /* x != +Inf is always equal to !(x > DBL_MAX). */ | |
1641 | (if (code == NE_EXPR) | |
1642 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
1643 | (if (! HONOR_NANS (@0)) | |
1644 | (if (neg) | |
1645 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
1646 | (le @0 { build_real (TREE_TYPE (@0), max); })) | |
1647 | (if (neg) | |
1648 | (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
1649 | { build_one_cst (type); }) | |
1650 | (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); }) | |
1651 | { build_one_cst (type); })))))))))) | |
07cdc2b8 RB |
1652 | |
1653 | /* If this is a comparison of a real constant with a PLUS_EXPR | |
1654 | or a MINUS_EXPR of a real constant, we can convert it into a | |
1655 | comparison with a revised real constant as long as no overflow | |
1656 | occurs when unsafe_math_optimizations are enabled. */ | |
1657 | (if (flag_unsafe_math_optimizations) | |
1658 | (for op (plus minus) | |
1659 | (simplify | |
1660 | (cmp (op @0 REAL_CST@1) REAL_CST@2) | |
1661 | (with | |
1662 | { | |
1663 | tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR, | |
1664 | TREE_TYPE (@1), @2, @1); | |
1665 | } | |
f980c9a2 | 1666 | (if (tem && !TREE_OVERFLOW (tem)) |
07cdc2b8 RB |
1667 | (cmp @0 { tem; })))))) |
1668 | ||
1669 | /* Likewise, we can simplify a comparison of a real constant with | |
1670 | a MINUS_EXPR whose first operand is also a real constant, i.e. | |
1671 | (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on | |
1672 | floating-point types only if -fassociative-math is set. */ | |
1673 | (if (flag_associative_math) | |
1674 | (simplify | |
0409237b | 1675 | (cmp (minus REAL_CST@0 @1) REAL_CST@2) |
07cdc2b8 | 1676 | (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); } |
f980c9a2 | 1677 | (if (tem && !TREE_OVERFLOW (tem)) |
07cdc2b8 RB |
1678 | (cmp { tem; } @1))))) |
1679 | ||
1680 | /* Fold comparisons against built-in math functions. */ | |
1681 | (if (flag_unsafe_math_optimizations | |
1682 | && ! flag_errno_math) | |
1683 | (for sq (SQRT) | |
1684 | (simplify | |
1685 | (cmp (sq @0) REAL_CST@1) | |
64d3a1f0 RB |
1686 | (switch |
1687 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
1688 | (switch | |
1689 | /* sqrt(x) < y is always false, if y is negative. */ | |
1690 | (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR) | |
8fdc6c67 | 1691 | { constant_boolean_node (false, type); }) |
64d3a1f0 RB |
1692 | /* sqrt(x) > y is always true, if y is negative and we |
1693 | don't care about NaNs, i.e. negative values of x. */ | |
1694 | (if (cmp == NE_EXPR || !HONOR_NANS (@0)) | |
1695 | { constant_boolean_node (true, type); }) | |
1696 | /* sqrt(x) > y is the same as x >= 0, if y is negative. */ | |
1697 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))) | |
1698 | (if (cmp == GT_EXPR || cmp == GE_EXPR) | |
1699 | (with | |
1700 | { | |
1701 | REAL_VALUE_TYPE c2; | |
5c88ea94 RS |
1702 | real_arithmetic (&c2, MULT_EXPR, |
1703 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
64d3a1f0 RB |
1704 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); |
1705 | } | |
1706 | (if (REAL_VALUE_ISINF (c2)) | |
1707 | /* sqrt(x) > y is x == +Inf, when y is very large. */ | |
1708 | (if (HONOR_INFINITIES (@0)) | |
1709 | (eq @0 { build_real (TREE_TYPE (@0), c2); }) | |
1710 | { constant_boolean_node (false, type); }) | |
1711 | /* sqrt(x) > c is the same as x > c*c. */ | |
1712 | (cmp @0 { build_real (TREE_TYPE (@0), c2); })))) | |
1713 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
1714 | (with | |
1715 | { | |
1716 | REAL_VALUE_TYPE c2; | |
5c88ea94 RS |
1717 | real_arithmetic (&c2, MULT_EXPR, |
1718 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
64d3a1f0 RB |
1719 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); |
1720 | } | |
1721 | (if (REAL_VALUE_ISINF (c2)) | |
1722 | (switch | |
1723 | /* sqrt(x) < y is always true, when y is a very large | |
1724 | value and we don't care about NaNs or Infinities. */ | |
1725 | (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0)) | |
1726 | { constant_boolean_node (true, type); }) | |
1727 | /* sqrt(x) < y is x != +Inf when y is very large and we | |
1728 | don't care about NaNs. */ | |
1729 | (if (! HONOR_NANS (@0)) | |
1730 | (ne @0 { build_real (TREE_TYPE (@0), c2); })) | |
1731 | /* sqrt(x) < y is x >= 0 when y is very large and we | |
1732 | don't care about Infinities. */ | |
1733 | (if (! HONOR_INFINITIES (@0)) | |
1734 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) | |
1735 | /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ | |
1736 | (if (GENERIC) | |
1737 | (truth_andif | |
1738 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
1739 | (ne @0 { build_real (TREE_TYPE (@0), c2); })))) | |
1740 | /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ | |
1741 | (if (! HONOR_NANS (@0)) | |
1742 | (cmp @0 { build_real (TREE_TYPE (@0), c2); }) | |
1743 | /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ | |
1744 | (if (GENERIC) | |
1745 | (truth_andif | |
1746 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
1747 | (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))))) | |
2ee05f1e | 1748 | |
cfdc4f33 MG |
1749 | /* Unordered tests if either argument is a NaN. */ |
1750 | (simplify | |
1751 | (bit_ior (unordered @0 @0) (unordered @1 @1)) | |
aea417d7 | 1752 | (if (types_match (@0, @1)) |
cfdc4f33 | 1753 | (unordered @0 @1))) |
257b01ba MG |
1754 | (simplify |
1755 | (bit_and (ordered @0 @0) (ordered @1 @1)) | |
1756 | (if (types_match (@0, @1)) | |
1757 | (ordered @0 @1))) | |
cfdc4f33 MG |
1758 | (simplify |
1759 | (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1)) | |
1760 | @2) | |
257b01ba MG |
1761 | (simplify |
1762 | (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1)) | |
1763 | @2) | |
e18c1d66 | 1764 | |
534bd33b MG |
1765 | /* -A CMP -B -> B CMP A. */ |
1766 | (for cmp (tcc_comparison) | |
1767 | scmp (swapped_tcc_comparison) | |
1768 | (simplify | |
1769 | (cmp (negate @0) (negate @1)) | |
1770 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1771 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1772 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
1773 | (scmp @0 @1))) | |
1774 | (simplify | |
1775 | (cmp (negate @0) CONSTANT_CLASS_P@1) | |
1776 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1777 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1778 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
1779 | (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); } | |
1780 | (if (tem && !TREE_OVERFLOW (tem)) | |
1781 | (scmp @0 { tem; })))))) | |
1782 | ||
b0eb889b MG |
1783 | /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */ |
1784 | (for op (eq ne) | |
1785 | (simplify | |
1786 | (op (abs @0) zerop@1) | |
1787 | (op @0 @1))) | |
1788 | ||
79d4f7c6 RB |
1789 | /* From fold_sign_changed_comparison and fold_widened_comparison. */ |
1790 | (for cmp (simple_comparison) | |
1791 | (simplify | |
1792 | (cmp (convert@0 @00) (convert?@1 @10)) | |
1793 | (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE | |
1794 | /* Disable this optimization if we're casting a function pointer | |
1795 | type on targets that require function pointer canonicalization. */ | |
1796 | && !(targetm.have_canonicalize_funcptr_for_compare () | |
1797 | && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE | |
2fde61e3 RB |
1798 | && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE) |
1799 | && single_use (@0)) | |
79d4f7c6 RB |
1800 | (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0)) |
1801 | && (TREE_CODE (@10) == INTEGER_CST | |
1802 | || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00)))) | |
1803 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0)) | |
1804 | || cmp == NE_EXPR | |
1805 | || cmp == EQ_EXPR) | |
1806 | && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0)))) | |
1807 | /* ??? The special-casing of INTEGER_CST conversion was in the original | |
1808 | code and here to avoid a spurious overflow flag on the resulting | |
1809 | constant which fold_convert produces. */ | |
1810 | (if (TREE_CODE (@1) == INTEGER_CST) | |
1811 | (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0, | |
1812 | TREE_OVERFLOW (@1)); }) | |
1813 | (cmp @00 (convert @1))) | |
1814 | ||
1815 | (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00))) | |
1816 | /* If possible, express the comparison in the shorter mode. */ | |
1817 | (if ((cmp == EQ_EXPR || cmp == NE_EXPR | |
1818 | || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))) | |
1819 | && (types_match (TREE_TYPE (@10), TREE_TYPE (@00)) | |
1820 | || ((TYPE_PRECISION (TREE_TYPE (@00)) | |
1821 | >= TYPE_PRECISION (TREE_TYPE (@10))) | |
1822 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) | |
1823 | == TYPE_UNSIGNED (TREE_TYPE (@10)))) | |
1824 | || (TREE_CODE (@10) == INTEGER_CST | |
1825 | && (TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE | |
1826 | || TREE_CODE (TREE_TYPE (@00)) == BOOLEAN_TYPE) | |
1827 | && int_fits_type_p (@10, TREE_TYPE (@00))))) | |
1828 | (cmp @00 (convert @10)) | |
1829 | (if (TREE_CODE (@10) == INTEGER_CST | |
1830 | && TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE | |
1831 | && !int_fits_type_p (@10, TREE_TYPE (@00))) | |
1832 | (with | |
1833 | { | |
1834 | tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
1835 | tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
1836 | bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10)); | |
1837 | bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min)); | |
1838 | } | |
1839 | (if (above || below) | |
1840 | (if (cmp == EQ_EXPR || cmp == NE_EXPR) | |
1841 | { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); } | |
1842 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
1843 | { constant_boolean_node (above ? true : false, type); } | |
1844 | (if (cmp == GT_EXPR || cmp == GE_EXPR) | |
1845 | { constant_boolean_node (above ? false : true, type); })))))))))))) | |
66e1cacf | 1846 | |
96a111a3 RB |
1847 | (for cmp (eq ne) |
1848 | /* A local variable can never be pointed to by | |
1849 | the default SSA name of an incoming parameter. | |
1850 | SSA names are canonicalized to 2nd place. */ | |
1851 | (simplify | |
1852 | (cmp addr@0 SSA_NAME@1) | |
1853 | (if (SSA_NAME_IS_DEFAULT_DEF (@1) | |
1854 | && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL) | |
1855 | (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); } | |
1856 | (if (TREE_CODE (base) == VAR_DECL | |
1857 | && auto_var_in_fn_p (base, current_function_decl)) | |
1858 | (if (cmp == NE_EXPR) | |
1859 | { constant_boolean_node (true, type); } | |
1860 | { constant_boolean_node (false, type); })))))) | |
1861 | ||
66e1cacf RB |
1862 | /* Equality compare simplifications from fold_binary */ |
1863 | (for cmp (eq ne) | |
1864 | ||
1865 | /* If we have (A | C) == D where C & ~D != 0, convert this into 0. | |
1866 | Similarly for NE_EXPR. */ | |
1867 | (simplify | |
1868 | (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2) | |
1869 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
1870 | && wi::bit_and_not (@1, @2) != 0) | |
1871 | { constant_boolean_node (cmp == NE_EXPR, type); })) | |
1872 | ||
1873 | /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ | |
1874 | (simplify | |
1875 | (cmp (bit_xor @0 @1) integer_zerop) | |
1876 | (cmp @0 @1)) | |
1877 | ||
1878 | /* (X ^ Y) == Y becomes X == 0. | |
1879 | Likewise (X ^ Y) == X becomes Y == 0. */ | |
1880 | (simplify | |
99e943a2 | 1881 | (cmp:c (bit_xor:c @0 @1) @0) |
66e1cacf RB |
1882 | (cmp @1 { build_zero_cst (TREE_TYPE (@1)); })) |
1883 | ||
1884 | /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ | |
1885 | (simplify | |
1886 | (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2) | |
1887 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))) | |
d057c866 | 1888 | (cmp @0 (bit_xor @1 (convert @2))))) |
d057c866 RB |
1889 | |
1890 | (simplify | |
1891 | (cmp (convert? addr@0) integer_zerop) | |
1892 | (if (tree_single_nonzero_warnv_p (@0, NULL)) | |
1893 | { constant_boolean_node (cmp == NE_EXPR, type); }))) | |
1894 | ||
b0eb889b MG |
1895 | /* If we have (A & C) == C where C is a power of 2, convert this into |
1896 | (A & C) != 0. Similarly for NE_EXPR. */ | |
1897 | (for cmp (eq ne) | |
1898 | icmp (ne eq) | |
1899 | (simplify | |
1900 | (cmp (bit_and@2 @0 integer_pow2p@1) @1) | |
1901 | (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) | |
1902 | ||
1903 | /* If we have (A & C) != 0 where C is the sign bit of A, convert | |
1904 | this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ | |
1905 | (for cmp (eq ne) | |
1906 | ncmp (ge lt) | |
1907 | (simplify | |
1908 | (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop) | |
1909 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1910 | && (TYPE_PRECISION (TREE_TYPE (@0)) | |
1911 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) | |
1912 | && element_precision (@2) >= element_precision (@0) | |
1913 | && wi::only_sign_bit_p (@1, element_precision (@0))) | |
1914 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } | |
1915 | (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) | |
1916 | ||
68aba1f6 RB |
1917 | /* When the addresses are not directly of decls compare base and offset. |
1918 | This implements some remaining parts of fold_comparison address | |
1919 | comparisons but still no complete part of it. Still it is good | |
1920 | enough to make fold_stmt not regress when not dispatching to fold_binary. */ | |
1921 | (for cmp (simple_comparison) | |
1922 | (simplify | |
f501d5cd | 1923 | (cmp (convert1?@2 addr@0) (convert2? addr@1)) |
68aba1f6 RB |
1924 | (with |
1925 | { | |
1926 | HOST_WIDE_INT off0, off1; | |
1927 | tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0); | |
1928 | tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1); | |
1929 | if (base0 && TREE_CODE (base0) == MEM_REF) | |
1930 | { | |
1931 | off0 += mem_ref_offset (base0).to_short_addr (); | |
1932 | base0 = TREE_OPERAND (base0, 0); | |
1933 | } | |
1934 | if (base1 && TREE_CODE (base1) == MEM_REF) | |
1935 | { | |
1936 | off1 += mem_ref_offset (base1).to_short_addr (); | |
1937 | base1 = TREE_OPERAND (base1, 0); | |
1938 | } | |
1939 | } | |
da571fda RB |
1940 | (if (base0 && base1) |
1941 | (with | |
1942 | { | |
aad88aed | 1943 | int equal = 2; |
da571fda RB |
1944 | if (decl_in_symtab_p (base0) |
1945 | && decl_in_symtab_p (base1)) | |
1946 | equal = symtab_node::get_create (base0) | |
1947 | ->equal_address_to (symtab_node::get_create (base1)); | |
aad88aed RB |
1948 | else if ((DECL_P (base0) || TREE_CODE (base0) == SSA_NAME) |
1949 | && (DECL_P (base1) || TREE_CODE (base1) == SSA_NAME)) | |
1950 | equal = (base0 == base1); | |
da571fda RB |
1951 | } |
1952 | (if (equal == 1 | |
1953 | && (cmp == EQ_EXPR || cmp == NE_EXPR | |
1954 | /* If the offsets are equal we can ignore overflow. */ | |
1955 | || off0 == off1 | |
1956 | || POINTER_TYPE_OVERFLOW_UNDEFINED | |
1957 | /* Or if we compare using pointers to decls. */ | |
1958 | || (POINTER_TYPE_P (TREE_TYPE (@2)) | |
1959 | && DECL_P (base0)))) | |
1960 | (switch | |
1961 | (if (cmp == EQ_EXPR) | |
1962 | { constant_boolean_node (off0 == off1, type); }) | |
1963 | (if (cmp == NE_EXPR) | |
1964 | { constant_boolean_node (off0 != off1, type); }) | |
1965 | (if (cmp == LT_EXPR) | |
1966 | { constant_boolean_node (off0 < off1, type); }) | |
1967 | (if (cmp == LE_EXPR) | |
1968 | { constant_boolean_node (off0 <= off1, type); }) | |
1969 | (if (cmp == GE_EXPR) | |
1970 | { constant_boolean_node (off0 >= off1, type); }) | |
1971 | (if (cmp == GT_EXPR) | |
1972 | { constant_boolean_node (off0 > off1, type); })) | |
1973 | (if (equal == 0 | |
1974 | && DECL_P (base0) && DECL_P (base1) | |
1975 | /* If we compare this as integers require equal offset. */ | |
1976 | && (!INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
1977 | || off0 == off1)) | |
1978 | (switch | |
1979 | (if (cmp == EQ_EXPR) | |
1980 | { constant_boolean_node (false, type); }) | |
1981 | (if (cmp == NE_EXPR) | |
1982 | { constant_boolean_node (true, type); }))))))))) | |
66e1cacf | 1983 | |
21aacde4 RB |
1984 | /* Non-equality compare simplifications from fold_binary */ |
1985 | (for cmp (lt gt le ge) | |
1986 | /* Comparisons with the highest or lowest possible integer of | |
1987 | the specified precision will have known values. */ | |
1988 | (simplify | |
1989 | (cmp (convert?@2 @0) INTEGER_CST@1) | |
1990 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
1991 | && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))) | |
1992 | (with | |
1993 | { | |
1994 | tree arg1_type = TREE_TYPE (@1); | |
1995 | unsigned int prec = TYPE_PRECISION (arg1_type); | |
1996 | wide_int max = wi::max_value (arg1_type); | |
1997 | wide_int signed_max = wi::max_value (prec, SIGNED); | |
1998 | wide_int min = wi::min_value (arg1_type); | |
1999 | } | |
2000 | (switch | |
2001 | (if (wi::eq_p (@1, max)) | |
2002 | (switch | |
2003 | (if (cmp == GT_EXPR) | |
2004 | { constant_boolean_node (false, type); }) | |
2005 | (if (cmp == GE_EXPR) | |
2006 | (eq @2 @1)) | |
2007 | (if (cmp == LE_EXPR) | |
2008 | { constant_boolean_node (true, type); }) | |
2009 | (if (cmp == LT_EXPR) | |
2010 | (ne @2 @1)))) | |
21aacde4 RB |
2011 | (if (wi::eq_p (@1, min)) |
2012 | (switch | |
2013 | (if (cmp == LT_EXPR) | |
2014 | { constant_boolean_node (false, type); }) | |
2015 | (if (cmp == LE_EXPR) | |
2016 | (eq @2 @1)) | |
2017 | (if (cmp == GE_EXPR) | |
2018 | { constant_boolean_node (true, type); }) | |
2019 | (if (cmp == GT_EXPR) | |
2020 | (ne @2 @1)))) | |
9bc22d19 RB |
2021 | (if (wi::eq_p (@1, max - 1)) |
2022 | (switch | |
2023 | (if (cmp == GT_EXPR) | |
2024 | (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); })) | |
2025 | (if (cmp == LE_EXPR) | |
2026 | (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); })))) | |
21aacde4 RB |
2027 | (if (wi::eq_p (@1, min + 1)) |
2028 | (switch | |
2029 | (if (cmp == GE_EXPR) | |
2030 | (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); })) | |
2031 | (if (cmp == LT_EXPR) | |
2032 | (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); })))) | |
2033 | (if (wi::eq_p (@1, signed_max) | |
2034 | && TYPE_UNSIGNED (arg1_type) | |
2035 | /* We will flip the signedness of the comparison operator | |
2036 | associated with the mode of @1, so the sign bit is | |
2037 | specified by this mode. Check that @1 is the signed | |
2038 | max associated with this sign bit. */ | |
2039 | && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type)) | |
2040 | /* signed_type does not work on pointer types. */ | |
2041 | && INTEGRAL_TYPE_P (arg1_type)) | |
2042 | /* The following case also applies to X < signed_max+1 | |
2043 | and X >= signed_max+1 because previous transformations. */ | |
2044 | (if (cmp == LE_EXPR || cmp == GT_EXPR) | |
2045 | (with { tree st = signed_type_for (arg1_type); } | |
2046 | (if (cmp == LE_EXPR) | |
2047 | (ge (convert:st @0) { build_zero_cst (st); }) | |
2048 | (lt (convert:st @0) { build_zero_cst (st); })))))))))) | |
2049 | ||
b5d3d787 RB |
2050 | (for cmp (unordered ordered unlt unle ungt unge uneq ltgt) |
2051 | /* If the second operand is NaN, the result is constant. */ | |
2052 | (simplify | |
2053 | (cmp @0 REAL_CST@1) | |
2054 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
2055 | && (cmp != LTGT_EXPR || ! flag_trapping_math)) | |
50301115 | 2056 | { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR |
b5d3d787 | 2057 | ? false : true, type); }))) |
21aacde4 | 2058 | |
55cf3946 RB |
2059 | /* bool_var != 0 becomes bool_var. */ |
2060 | (simplify | |
b5d3d787 | 2061 | (ne @0 integer_zerop) |
55cf3946 RB |
2062 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
2063 | && types_match (type, TREE_TYPE (@0))) | |
2064 | (non_lvalue @0))) | |
2065 | /* bool_var == 1 becomes bool_var. */ | |
2066 | (simplify | |
b5d3d787 | 2067 | (eq @0 integer_onep) |
55cf3946 RB |
2068 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
2069 | && types_match (type, TREE_TYPE (@0))) | |
2070 | (non_lvalue @0))) | |
b5d3d787 RB |
2071 | /* Do not handle |
2072 | bool_var == 0 becomes !bool_var or | |
2073 | bool_var != 1 becomes !bool_var | |
2074 | here because that only is good in assignment context as long | |
2075 | as we require a tcc_comparison in GIMPLE_CONDs where we'd | |
2076 | replace if (x == 0) with tem = ~x; if (tem != 0) which is | |
2077 | clearly less optimal and which we'll transform again in forwprop. */ | |
55cf3946 RB |
2078 | |
2079 | ||
e18c1d66 RB |
2080 | /* Simplification of math builtins. */ |
2081 | ||
e18c1d66 RB |
2082 | /* fold_builtin_logarithm */ |
2083 | (if (flag_unsafe_math_optimizations) | |
52c6378a N |
2084 | |
2085 | /* Simplify sqrt(x) * sqrt(x) -> x. */ | |
2086 | (simplify | |
2087 | (mult (SQRT@1 @0) @1) | |
2088 | (if (!HONOR_SNANS (type)) | |
2089 | @0)) | |
2090 | ||
35401640 N |
2091 | /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */ |
2092 | (for root (SQRT CBRT) | |
2093 | (simplify | |
2094 | (mult (root:s @0) (root:s @1)) | |
2095 | (root (mult @0 @1)))) | |
2096 | ||
2097 | /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */ | |
2098 | (simplify | |
2099 | (mult (POW:s @0 @1) (POW:s @0 @2)) | |
2100 | (POW @0 (plus @1 @2))) | |
2101 | ||
52c6378a N |
2102 | /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */ |
2103 | (simplify | |
2104 | (mult (POW:s @0 @1) (POW:s @2 @1)) | |
2105 | (POW (mult @0 @2) @1)) | |
2106 | ||
35401640 N |
2107 | /* Simplify expN(x) * expN(y) -> expN(x+y). */ |
2108 | (for exps (EXP EXP2 EXP10 POW10) | |
2109 | (simplify | |
2110 | (mult (exps:s @0) (exps:s @1)) | |
2111 | (exps (plus @0 @1)))) | |
2112 | ||
52c6378a N |
2113 | /* Simplify tan(x) * cos(x) -> sin(x). */ |
2114 | (simplify | |
2115 | (mult:c (TAN:s @0) (COS:s @0)) | |
2116 | (SIN @0)) | |
2117 | ||
2118 | /* Simplify x * pow(x,c) -> pow(x,c+1). */ | |
2119 | (simplify | |
2120 | (mult @0 (POW:s @0 REAL_CST@1)) | |
2121 | (if (!TREE_OVERFLOW (@1)) | |
2122 | (POW @0 (plus @1 { build_one_cst (type); })))) | |
2123 | ||
2124 | /* Simplify sin(x) / cos(x) -> tan(x). */ | |
2125 | (simplify | |
2126 | (rdiv (SIN:s @0) (COS:s @0)) | |
2127 | (TAN @0)) | |
2128 | ||
2129 | /* Simplify cos(x) / sin(x) -> 1 / tan(x). */ | |
2130 | (simplify | |
2131 | (rdiv (COS:s @0) (SIN:s @0)) | |
2132 | (rdiv { build_one_cst (type); } (TAN @0))) | |
2133 | ||
2134 | /* Simplify sin(x) / tan(x) -> cos(x). */ | |
2135 | (simplify | |
2136 | (rdiv (SIN:s @0) (TAN:s @0)) | |
2137 | (if (! HONOR_NANS (@0) | |
2138 | && ! HONOR_INFINITIES (@0)) | |
2139 | (cos @0))) | |
2140 | ||
2141 | /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */ | |
2142 | (simplify | |
2143 | (rdiv (TAN:s @0) (SIN:s @0)) | |
2144 | (if (! HONOR_NANS (@0) | |
2145 | && ! HONOR_INFINITIES (@0)) | |
2146 | (rdiv { build_one_cst (type); } (COS @0)))) | |
2147 | ||
2148 | /* Simplify pow(x,c) / x -> pow(x,c-1). */ | |
2149 | (simplify | |
2150 | (rdiv (POW:s @0 REAL_CST@1) @0) | |
2151 | (if (!TREE_OVERFLOW (@1)) | |
2152 | (POW @0 (minus @1 { build_one_cst (type); })))) | |
2153 | ||
2154 | /* Simplify a/root(b/c) into a*root(c/b). */ | |
35401640 N |
2155 | (for root (SQRT CBRT) |
2156 | (simplify | |
2157 | (rdiv @0 (root:s (rdiv:s @1 @2))) | |
2158 | (mult @0 (root (rdiv @2 @1))))) | |
2159 | ||
2160 | /* Simplify x/expN(y) into x*expN(-y). */ | |
2161 | (for exps (EXP EXP2 EXP10 POW10) | |
2162 | (simplify | |
2163 | (rdiv @0 (exps:s @1)) | |
2164 | (mult @0 (exps (negate @1))))) | |
52c6378a N |
2165 | |
2166 | /* Simplify x / pow (y,z) -> x * pow(y,-z). */ | |
2167 | (simplify | |
2168 | (rdiv @0 (POW:s @1 @2)) | |
2169 | (mult @0 (POW @1 (negate @2)))) | |
2170 | ||
e18c1d66 RB |
2171 | /* Special case, optimize logN(expN(x)) = x. */ |
2172 | (for logs (LOG LOG2 LOG10) | |
2173 | exps (EXP EXP2 EXP10) | |
2174 | (simplify | |
2175 | (logs (exps @0)) | |
2176 | @0)) | |
2177 | /* Optimize logN(func()) for various exponential functions. We | |
2178 | want to determine the value "x" and the power "exponent" in | |
2179 | order to transform logN(x**exponent) into exponent*logN(x). */ | |
2180 | (for logs (LOG LOG LOG LOG | |
2181 | LOG2 LOG2 LOG2 LOG2 | |
2182 | LOG10 LOG10 LOG10 LOG10) | |
2183 | exps (EXP EXP2 EXP10 POW10) | |
2184 | (simplify | |
2185 | (logs (exps @0)) | |
2186 | (with { | |
2187 | tree x; | |
2188 | switch (exps) | |
2189 | { | |
2190 | CASE_FLT_FN (BUILT_IN_EXP): | |
2191 | /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */ | |
73463c5e | 2192 | x = build_real_truncate (type, dconst_e ()); |
e18c1d66 RB |
2193 | break; |
2194 | CASE_FLT_FN (BUILT_IN_EXP2): | |
2195 | /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */ | |
2196 | x = build_real (type, dconst2); | |
2197 | break; | |
2198 | CASE_FLT_FN (BUILT_IN_EXP10): | |
2199 | CASE_FLT_FN (BUILT_IN_POW10): | |
2200 | /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */ | |
2201 | { | |
2202 | REAL_VALUE_TYPE dconst10; | |
2203 | real_from_integer (&dconst10, VOIDmode, 10, SIGNED); | |
2204 | x = build_real (type, dconst10); | |
2205 | } | |
2206 | break; | |
50301115 RB |
2207 | default: |
2208 | gcc_unreachable (); | |
e18c1d66 RB |
2209 | } |
2210 | } | |
2211 | (mult (logs { x; }) @0)))) | |
2212 | (for logs (LOG LOG | |
2213 | LOG2 LOG2 | |
2214 | LOG10 LOG10) | |
2215 | exps (SQRT CBRT) | |
2216 | (simplify | |
2217 | (logs (exps @0)) | |
2218 | (with { | |
2219 | tree x; | |
2220 | switch (exps) | |
2221 | { | |
2222 | CASE_FLT_FN (BUILT_IN_SQRT): | |
2223 | /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */ | |
2224 | x = build_real (type, dconsthalf); | |
2225 | break; | |
2226 | CASE_FLT_FN (BUILT_IN_CBRT): | |
2227 | /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */ | |
73463c5e | 2228 | x = build_real_truncate (type, dconst_third ()); |
e18c1d66 | 2229 | break; |
50301115 RB |
2230 | default: |
2231 | gcc_unreachable (); | |
e18c1d66 RB |
2232 | } |
2233 | } | |
2234 | (mult { x; } (logs @0))))) | |
2235 | /* logN(pow(x,exponent) -> exponent*logN(x). */ | |
2236 | (for logs (LOG LOG2 LOG10) | |
2237 | pows (POW) | |
2238 | (simplify | |
2239 | (logs (pows @0 @1)) | |
2240 | (mult @1 (logs @0))))) | |
2241 | ||
be144838 JL |
2242 | /* Narrowing of arithmetic and logical operations. |
2243 | ||
2244 | These are conceptually similar to the transformations performed for | |
2245 | the C/C++ front-ends by shorten_binary_op and shorten_compare. Long | |
2246 | term we want to move all that code out of the front-ends into here. */ | |
2247 | ||
2248 | /* If we have a narrowing conversion of an arithmetic operation where | |
2249 | both operands are widening conversions from the same type as the outer | |
2250 | narrowing conversion. Then convert the innermost operands to a suitable | |
2251 | unsigned type (to avoid introducing undefined behaviour), perform the | |
2252 | operation and convert the result to the desired type. */ | |
2253 | (for op (plus minus) | |
2254 | (simplify | |
44fc0a51 | 2255 | (convert (op:s (convert@2 @0) (convert@3 @1))) |
be144838 JL |
2256 | (if (INTEGRAL_TYPE_P (type) |
2257 | /* We check for type compatibility between @0 and @1 below, | |
2258 | so there's no need to check that @1/@3 are integral types. */ | |
2259 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2260 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
2261 | /* The precision of the type of each operand must match the | |
2262 | precision of the mode of each operand, similarly for the | |
2263 | result. */ | |
2264 | && (TYPE_PRECISION (TREE_TYPE (@0)) | |
2265 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) | |
2266 | && (TYPE_PRECISION (TREE_TYPE (@1)) | |
2267 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1)))) | |
2268 | && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type)) | |
2269 | /* The inner conversion must be a widening conversion. */ | |
2270 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
aea417d7 | 2271 | && types_match (@0, @1) |
44fc0a51 | 2272 | && types_match (@0, type)) |
be144838 | 2273 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) |
8fdc6c67 RB |
2274 | (convert (op @0 @1)) |
2275 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
2276 | (convert (op (convert:utype @0) (convert:utype @1)))))))) | |
48451e8f JL |
2277 | |
2278 | /* This is another case of narrowing, specifically when there's an outer | |
2279 | BIT_AND_EXPR which masks off bits outside the type of the innermost | |
2280 | operands. Like the previous case we have to convert the operands | |
2281 | to unsigned types to avoid introducing undefined behaviour for the | |
2282 | arithmetic operation. */ | |
2283 | (for op (minus plus) | |
8fdc6c67 RB |
2284 | (simplify |
2285 | (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4) | |
2286 | (if (INTEGRAL_TYPE_P (type) | |
2287 | /* We check for type compatibility between @0 and @1 below, | |
2288 | so there's no need to check that @1/@3 are integral types. */ | |
2289 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2290 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
2291 | /* The precision of the type of each operand must match the | |
2292 | precision of the mode of each operand, similarly for the | |
2293 | result. */ | |
2294 | && (TYPE_PRECISION (TREE_TYPE (@0)) | |
2295 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) | |
2296 | && (TYPE_PRECISION (TREE_TYPE (@1)) | |
2297 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1)))) | |
2298 | && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type)) | |
2299 | /* The inner conversion must be a widening conversion. */ | |
2300 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
2301 | && types_match (@0, @1) | |
2302 | && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0))) | |
2303 | <= TYPE_PRECISION (TREE_TYPE (@0))) | |
2304 | && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) | |
2305 | || tree_int_cst_sgn (@4) >= 0)) | |
2306 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
2307 | (with { tree ntype = TREE_TYPE (@0); } | |
2308 | (convert (bit_and (op @0 @1) (convert:ntype @4)))) | |
2309 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
2310 | (convert (bit_and (op (convert:utype @0) (convert:utype @1)) | |
2311 | (convert:utype @4)))))))) | |
4835c978 RS |
2312 | |
2313 | (if (flag_unsafe_math_optimizations) | |
2314 | (for sqrts (SQRT) | |
2315 | cbrts (CBRT) | |
2316 | exps (EXP EXP2 EXP10 POW10) | |
2317 | /* sqrt(expN(x)) -> expN(x*0.5). */ | |
2318 | (simplify | |
2319 | (sqrts (exps @0)) | |
2320 | (exps (mult @0 { build_real (type, dconsthalf); }))) | |
2321 | /* cbrt(expN(x)) -> expN(x/3). */ | |
2322 | (simplify | |
2323 | (cbrts (exps @0)) | |
2324 | (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))) | |
2325 | ||
2326 | (for sqrts (SQRT) | |
2327 | cbrts (CBRT) | |
2328 | pows (POW) | |
2329 | /* sqrt(sqrt(x)) -> pow(x,1/4). */ | |
2330 | (simplify | |
2331 | (sqrts (sqrts @0)) | |
2332 | (pows @0 { build_real (type, dconst_quarter ()); })) | |
2333 | /* sqrt(cbrt(x)) -> pow(x,1/6). */ | |
2334 | (simplify | |
2335 | (sqrts (cbrts @0)) | |
2336 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
2337 | /* cbrt(sqrt(x)) -> pow(x,1/6). */ | |
2338 | (simplify | |
2339 | (cbrts (sqrts @0)) | |
2340 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
2341 | /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */ | |
2342 | (simplify | |
2343 | (cbrts (cbrts tree_expr_nonnegative_p@0)) | |
2344 | (pows @0 { build_real_truncate (type, dconst_ninth ()); })) | |
2345 | /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */ | |
2346 | (simplify | |
2347 | (sqrts (pows @0 @1)) | |
2348 | (pows (abs @0) (mult @1 { build_real (type, dconsthalf); }))) | |
2349 | /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */ | |
2350 | (simplify | |
2351 | (cbrts (pows tree_expr_nonnegative_p@0 @1)) | |
2352 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))))) |