]>
Commit | Line | Data |
---|---|---|
2165588a | 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 | ||
fbd26352 | 5 | Copyright (C) 2014-2019 Free Software Foundation, Inc. |
2165588a | 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 | |
662274ee | 28 | integer_onep integer_zerop integer_all_onesp integer_minus_onep |
18b38664 | 29 | integer_each_onep integer_truep integer_nonzerop |
662274ee | 30 | real_zerop real_onep real_minus_onep |
06e4870e | 31 | zerop |
34dc851a | 32 | initializer_each_zero_or_onep |
2ad7e37a | 33 | CONSTANT_CLASS_P |
71486465 | 34 | tree_expr_nonnegative_p |
70229d3b | 35 | tree_expr_nonzero_p |
6f5f406a | 36 | integer_valued_real_p |
18b38664 | 37 | integer_pow2p |
7931f9ca | 38 | uniform_integer_cst_p |
b006d66f | 39 | HONOR_NANS |
40 | uniform_vector_p) | |
55534d34 | 41 | |
9ec260f0 | 42 | /* Operator lists. */ |
43 | (define_operator_list tcc_comparison | |
44 | lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) | |
45 | (define_operator_list inverted_tcc_comparison | |
46 | ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq) | |
47 | (define_operator_list inverted_tcc_comparison_with_nans | |
48 | unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq) | |
0c3c84e3 | 49 | (define_operator_list swapped_tcc_comparison |
50 | gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt) | |
c915fa32 | 51 | (define_operator_list simple_comparison lt le eq ne ge gt) |
52 | (define_operator_list swapped_simple_comparison gt ge eq ne le lt) | |
53 | ||
ff690865 | 54 | #include "cfn-operators.pd" |
89ff0c87 | 55 | |
1117ed35 | 56 | /* Define operand lists for math rounding functions {,i,l,ll}FN, |
57 | where the versions prefixed with "i" return an int, those prefixed with | |
58 | "l" return a long and those prefixed with "ll" return a long long. | |
59 | ||
60 | Also define operand lists: | |
61 | ||
62 | X<FN>F for all float functions, in the order i, l, ll | |
63 | X<FN> for all double functions, in the same order | |
64 | X<FN>L for all long double functions, in the same order. */ | |
65 | #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \ | |
1117ed35 | 66 | (define_operator_list X##FN##F BUILT_IN_I##FN##F \ |
67 | BUILT_IN_L##FN##F \ | |
68 | BUILT_IN_LL##FN##F) \ | |
69 | (define_operator_list X##FN BUILT_IN_I##FN \ | |
70 | BUILT_IN_L##FN \ | |
71 | BUILT_IN_LL##FN) \ | |
72 | (define_operator_list X##FN##L BUILT_IN_I##FN##L \ | |
73 | BUILT_IN_L##FN##L \ | |
74 | BUILT_IN_LL##FN##L) | |
75 | ||
1117ed35 | 76 | DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR) |
77 | DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL) | |
78 | DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND) | |
79 | DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) | |
dbc7e6ae | 80 | |
81 | /* Binary operations and their associated IFN_COND_* function. */ | |
82 | (define_operator_list UNCOND_BINARY | |
83 | plus minus | |
cdb4d5d0 | 84 | mult trunc_div trunc_mod rdiv |
dbc7e6ae | 85 | min max |
8d60bb3e | 86 | bit_and bit_ior bit_xor |
87 | lshift rshift) | |
dbc7e6ae | 88 | (define_operator_list COND_BINARY |
89 | IFN_COND_ADD IFN_COND_SUB | |
cdb4d5d0 | 90 | IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV |
dbc7e6ae | 91 | IFN_COND_MIN IFN_COND_MAX |
8d60bb3e | 92 | IFN_COND_AND IFN_COND_IOR IFN_COND_XOR |
93 | IFN_COND_SHL IFN_COND_SHR) | |
6682fc02 | 94 | |
95 | /* Same for ternary operations. */ | |
96 | (define_operator_list UNCOND_TERNARY | |
97 | IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS) | |
98 | (define_operator_list COND_TERNARY | |
99 | IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS) | |
9603b585 | 100 | |
049318f9 | 101 | /* As opposed to convert?, this still creates a single pattern, so |
102 | it is not a suitable replacement for convert? in all cases. */ | |
103 | (match (nop_convert @0) | |
104 | (convert @0) | |
105 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))))) | |
106 | (match (nop_convert @0) | |
107 | (view_convert @0) | |
108 | (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0)) | |
f08ee65f | 109 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
110 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))) | |
049318f9 | 111 | && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) |
112 | /* This one has to be last, or it shadows the others. */ | |
113 | (match (nop_convert @0) | |
9603b585 | 114 | @0) |
9ec260f0 | 115 | |
1c67942e | 116 | /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x> |
117 | ABSU_EXPR returns unsigned absolute value of the operand and the operand | |
118 | of the ABSU_EXPR will have the corresponding signed type. */ | |
119 | (simplify (abs (convert @0)) | |
120 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
121 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
122 | && element_precision (type) > element_precision (TREE_TYPE (@0))) | |
123 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
124 | (convert (absu:utype @0))))) | |
125 | ||
126 | ||
55534d34 | 127 | /* Simplifications of operations with one constant operand and |
58810b92 | 128 | simplifications to constants or single values. */ |
55534d34 | 129 | |
130 | (for op (plus pointer_plus minus bit_ior bit_xor) | |
131 | (simplify | |
132 | (op @0 integer_zerop) | |
133 | (non_lvalue @0))) | |
134 | ||
770ae4bb | 135 | /* 0 +p index -> (type)index */ |
136 | (simplify | |
137 | (pointer_plus integer_zerop @1) | |
138 | (non_lvalue (convert @1))) | |
139 | ||
e52afc20 | 140 | /* ptr - 0 -> (type)ptr */ |
141 | (simplify | |
142 | (pointer_diff @0 integer_zerop) | |
143 | (convert @0)) | |
144 | ||
6a78ea5d | 145 | /* See if ARG1 is zero and X + ARG1 reduces to X. |
146 | Likewise if the operands are reversed. */ | |
147 | (simplify | |
148 | (plus:c @0 real_zerop@1) | |
149 | (if (fold_real_zero_addition_p (type, @1, 0)) | |
150 | (non_lvalue @0))) | |
151 | ||
152 | /* See if ARG1 is zero and X - ARG1 reduces to X. */ | |
153 | (simplify | |
154 | (minus @0 real_zerop@1) | |
155 | (if (fold_real_zero_addition_p (type, @1, 1)) | |
156 | (non_lvalue @0))) | |
c57b5e25 | 157 | |
158 | /* Even if the fold_real_zero_addition_p can't simplify X + 0.0 | |
159 | into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0 | |
160 | or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0 | |
161 | if not -frounding-math. For sNaNs the first operation would raise | |
162 | exceptions but turn the result into qNan, so the second operation | |
163 | would not raise it. */ | |
164 | (for inner_op (plus minus) | |
165 | (for outer_op (plus minus) | |
166 | (simplify | |
167 | (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2) | |
168 | (if (real_zerop (@1) | |
169 | && real_zerop (@2) | |
170 | && !HONOR_SIGN_DEPENDENT_ROUNDING (type)) | |
171 | (with { bool inner_plus = ((inner_op == PLUS_EXPR) | |
172 | ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))); | |
173 | bool outer_plus | |
174 | = ((outer_op == PLUS_EXPR) | |
175 | ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); } | |
176 | (if (outer_plus && !inner_plus) | |
177 | (outer_op @0 @2) | |
178 | @3)))))) | |
6a78ea5d | 179 | |
55534d34 | 180 | /* Simplify x - x. |
181 | This is unsafe for certain floats even in non-IEEE formats. | |
182 | In IEEE, it is unsafe because it does wrong for NaNs. | |
183 | Also note that operand_equal_p is always false if an operand | |
184 | is volatile. */ | |
185 | (simplify | |
6a78ea5d | 186 | (minus @0 @0) |
93633022 | 187 | (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type)) |
6a78ea5d | 188 | { build_zero_cst (type); })) |
57e83b58 | 189 | (simplify |
190 | (pointer_diff @@0 @0) | |
191 | { build_zero_cst (type); }) | |
55534d34 | 192 | |
193 | (simplify | |
6a78ea5d | 194 | (mult @0 integer_zerop@1) |
195 | @1) | |
196 | ||
197 | /* Maybe fold x * 0 to 0. The expressions aren't the same | |
198 | when x is NaN, since x * 0 is also NaN. Nor are they the | |
199 | same in modes with signed zeros, since multiplying a | |
200 | negative value by 0 gives -0, not +0. */ | |
201 | (simplify | |
202 | (mult @0 real_zerop@1) | |
c8047e01 | 203 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) |
6a78ea5d | 204 | @1)) |
205 | ||
206 | /* In IEEE floating point, x*1 is not equivalent to x for snans. | |
207 | Likewise for complex arithmetic with signed zeros. */ | |
208 | (simplify | |
209 | (mult @0 real_onep) | |
c8047e01 | 210 | (if (!HONOR_SNANS (type) |
211 | && (!HONOR_SIGNED_ZEROS (type) | |
6a78ea5d | 212 | || !COMPLEX_FLOAT_TYPE_P (type))) |
213 | (non_lvalue @0))) | |
214 | ||
215 | /* Transform x * -1.0 into -x. */ | |
216 | (simplify | |
217 | (mult @0 real_minus_onep) | |
c8047e01 | 218 | (if (!HONOR_SNANS (type) |
219 | && (!HONOR_SIGNED_ZEROS (type) | |
6a78ea5d | 220 | || !COMPLEX_FLOAT_TYPE_P (type))) |
221 | (negate @0))) | |
55534d34 | 222 | |
09504b03 | 223 | /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */ |
224 | (simplify | |
225 | (mult SSA_NAME@1 SSA_NAME@2) | |
226 | (if (INTEGRAL_TYPE_P (type) | |
227 | && get_nonzero_bits (@1) == 1 | |
228 | && get_nonzero_bits (@2) == 1) | |
229 | (bit_and @1 @2))) | |
230 | ||
34dc851a | 231 | /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...}, |
232 | unless the target has native support for the former but not the latter. */ | |
233 | (simplify | |
234 | (mult @0 VECTOR_CST@1) | |
235 | (if (initializer_each_zero_or_onep (@1) | |
236 | && !HONOR_SNANS (type) | |
237 | && !HONOR_SIGNED_ZEROS (type)) | |
238 | (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; } | |
239 | (if (itype | |
240 | && (!VECTOR_MODE_P (TYPE_MODE (type)) | |
241 | || (VECTOR_MODE_P (TYPE_MODE (itype)) | |
242 | && optab_handler (and_optab, | |
243 | TYPE_MODE (itype)) != CODE_FOR_nothing))) | |
244 | (view_convert (bit_and:itype (view_convert @0) | |
245 | (ne @1 { build_zero_cst (type); }))))))) | |
246 | ||
6e0b4434 | 247 | (for cmp (gt ge lt le) |
248 | outp (convert convert negate negate) | |
249 | outn (negate negate convert convert) | |
250 | /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */ | |
251 | /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */ | |
252 | /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */ | |
253 | /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */ | |
254 | (simplify | |
255 | (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep) | |
256 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type) | |
257 | && types_match (type, TREE_TYPE (@0))) | |
258 | (switch | |
259 | (if (types_match (type, float_type_node)) | |
260 | (BUILT_IN_COPYSIGNF @1 (outp @0))) | |
261 | (if (types_match (type, double_type_node)) | |
262 | (BUILT_IN_COPYSIGN @1 (outp @0))) | |
263 | (if (types_match (type, long_double_type_node)) | |
264 | (BUILT_IN_COPYSIGNL @1 (outp @0)))))) | |
265 | /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */ | |
266 | /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */ | |
267 | /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */ | |
268 | /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */ | |
269 | (simplify | |
270 | (cond (cmp @0 real_zerop) real_minus_onep real_onep@1) | |
271 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type) | |
272 | && types_match (type, TREE_TYPE (@0))) | |
273 | (switch | |
274 | (if (types_match (type, float_type_node)) | |
275 | (BUILT_IN_COPYSIGNF @1 (outn @0))) | |
276 | (if (types_match (type, double_type_node)) | |
277 | (BUILT_IN_COPYSIGN @1 (outn @0))) | |
278 | (if (types_match (type, long_double_type_node)) | |
279 | (BUILT_IN_COPYSIGNL @1 (outn @0))))))) | |
280 | ||
281 | /* Transform X * copysign (1.0, X) into abs(X). */ | |
282 | (simplify | |
054e9558 | 283 | (mult:c @0 (COPYSIGN_ALL real_onep @0)) |
6e0b4434 | 284 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) |
285 | (abs @0))) | |
286 | ||
287 | /* Transform X * copysign (1.0, -X) into -abs(X). */ | |
288 | (simplify | |
054e9558 | 289 | (mult:c @0 (COPYSIGN_ALL real_onep (negate @0))) |
6e0b4434 | 290 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) |
291 | (negate (abs @0)))) | |
292 | ||
293 | /* Transform copysign (CST, X) into copysign (ABS(CST), X). */ | |
294 | (simplify | |
054e9558 | 295 | (COPYSIGN_ALL REAL_CST@0 @1) |
6e0b4434 | 296 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0))) |
054e9558 | 297 | (COPYSIGN_ALL (negate @0) @1))) |
6e0b4434 | 298 | |
6062d7aa | 299 | /* X * 1, X / 1 -> X. */ |
55534d34 | 300 | (for op (mult trunc_div ceil_div floor_div round_div exact_div) |
301 | (simplify | |
302 | (op @0 integer_onep) | |
303 | (non_lvalue @0))) | |
304 | ||
f769c6cf | 305 | /* (A / (1 << B)) -> (A >> B). |
306 | Only for unsigned A. For signed A, this would not preserve rounding | |
307 | toward zero. | |
308 | For example: (-1 / ( 1 << B)) != -1 >> B. */ | |
309 | (simplify | |
310 | (trunc_div @0 (lshift integer_onep@1 @2)) | |
311 | (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) | |
312 | && (!VECTOR_TYPE_P (type) | |
313 | || target_supports_op_p (type, RSHIFT_EXPR, optab_vector) | |
314 | || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))) | |
315 | (rshift @0 @2))) | |
316 | ||
6062d7aa | 317 | /* Preserve explicit divisions by 0: the C++ front-end wants to detect |
318 | undefined behavior in constexpr evaluation, and assuming that the division | |
319 | traps enables better optimizations than these anyway. */ | |
6a78ea5d | 320 | (for div (trunc_div ceil_div floor_div round_div exact_div) |
6062d7aa | 321 | /* 0 / X is always zero. */ |
322 | (simplify | |
323 | (div integer_zerop@0 @1) | |
324 | /* But not for 0 / 0 so that we can get the proper warnings and errors. */ | |
325 | (if (!integer_zerop (@1)) | |
326 | @0)) | |
98da8d3b | 327 | /* X / -1 is -X. */ |
6a78ea5d | 328 | (simplify |
ccb14741 | 329 | (div @0 integer_minus_onep@1) |
330 | (if (!TYPE_UNSIGNED (type)) | |
98da8d3b | 331 | (negate @0))) |
6062d7aa | 332 | /* X / X is one. */ |
333 | (simplify | |
334 | (div @0 @0) | |
63d693d7 | 335 | /* But not for 0 / 0 so that we can get the proper warnings and errors. |
336 | And not for _Fract types where we can't build 1. */ | |
337 | (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type))) | |
6062d7aa | 338 | { build_one_cst (type); })) |
9603b585 | 339 | /* X / abs (X) is X < 0 ? -1 : 1. */ |
98da8d3b | 340 | (simplify |
22b30400 | 341 | (div:C @0 (abs @0)) |
342 | (if (INTEGRAL_TYPE_P (type) | |
98da8d3b | 343 | && TYPE_OVERFLOW_UNDEFINED (type)) |
344 | (cond (lt @0 { build_zero_cst (type); }) | |
345 | { build_minus_one_cst (type); } { build_one_cst (type); }))) | |
346 | /* X / -X is -1. */ | |
347 | (simplify | |
22b30400 | 348 | (div:C @0 (negate @0)) |
98da8d3b | 349 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
350 | && TYPE_OVERFLOW_UNDEFINED (type)) | |
351 | { build_minus_one_cst (type); }))) | |
6a78ea5d | 352 | |
353 | /* For unsigned integral types, FLOOR_DIV_EXPR is the same as | |
354 | TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ | |
355 | (simplify | |
356 | (floor_div @0 @1) | |
ccb14741 | 357 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
358 | && TYPE_UNSIGNED (type)) | |
6a78ea5d | 359 | (trunc_div @0 @1))) |
360 | ||
62d4473c | 361 | /* Combine two successive divisions. Note that combining ceil_div |
362 | and floor_div is trickier and combining round_div even more so. */ | |
363 | (for div (trunc_div exact_div) | |
2309dbdc | 364 | (simplify |
9c2ad52a | 365 | (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2) |
2309dbdc | 366 | (with { |
30b5769f | 367 | wi::overflow_type overflow; |
e3d0f65c | 368 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), |
30b5769f | 369 | TYPE_SIGN (type), &overflow); |
2309dbdc | 370 | } |
9c2ad52a | 371 | (if (div == EXACT_DIV_EXPR |
372 | || optimize_successive_divisions_p (@2, @3)) | |
373 | (if (!overflow) | |
374 | (div @0 { wide_int_to_tree (type, mul); }) | |
375 | (if (TYPE_UNSIGNED (type) | |
376 | || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)) | |
377 | { build_zero_cst (type); })))))) | |
2309dbdc | 378 | |
e4f48180 | 379 | /* Combine successive multiplications. Similar to above, but handling |
380 | overflow is different. */ | |
381 | (simplify | |
382 | (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
383 | (with { | |
30b5769f | 384 | wi::overflow_type overflow; |
e3d0f65c | 385 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), |
30b5769f | 386 | TYPE_SIGN (type), &overflow); |
e4f48180 | 387 | } |
388 | /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN, | |
389 | otherwise undefined overflow implies that @0 must be zero. */ | |
30b5769f | 390 | (if (!overflow || TYPE_OVERFLOW_WRAPS (type)) |
e4f48180 | 391 | (mult @0 { wide_int_to_tree (type, mul); })))) |
392 | ||
6a78ea5d | 393 | /* Optimize A / A to 1.0 if we don't care about |
ccb14741 | 394 | NaNs or Infinities. */ |
6a78ea5d | 395 | (simplify |
396 | (rdiv @0 @0) | |
ccb14741 | 397 | (if (FLOAT_TYPE_P (type) |
93633022 | 398 | && ! HONOR_NANS (type) |
c8047e01 | 399 | && ! HONOR_INFINITIES (type)) |
ccb14741 | 400 | { build_one_cst (type); })) |
401 | ||
402 | /* Optimize -A / A to -1.0 if we don't care about | |
403 | NaNs or Infinities. */ | |
404 | (simplify | |
2f99fc04 | 405 | (rdiv:C @0 (negate @0)) |
ccb14741 | 406 | (if (FLOAT_TYPE_P (type) |
93633022 | 407 | && ! HONOR_NANS (type) |
c8047e01 | 408 | && ! HONOR_INFINITIES (type)) |
ccb14741 | 409 | { build_minus_one_cst (type); })) |
6a78ea5d | 410 | |
12dabcee | 411 | /* PR71078: x / abs(x) -> copysign (1.0, x) */ |
412 | (simplify | |
413 | (rdiv:C (convert? @0) (convert? (abs @0))) | |
414 | (if (SCALAR_FLOAT_TYPE_P (type) | |
415 | && ! HONOR_NANS (type) | |
416 | && ! HONOR_INFINITIES (type)) | |
417 | (switch | |
418 | (if (types_match (type, float_type_node)) | |
419 | (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0))) | |
420 | (if (types_match (type, double_type_node)) | |
421 | (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0))) | |
422 | (if (types_match (type, long_double_type_node)) | |
423 | (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0)))))) | |
424 | ||
6a78ea5d | 425 | /* In IEEE floating point, x/1 is not equivalent to x for snans. */ |
426 | (simplify | |
427 | (rdiv @0 real_onep) | |
c8047e01 | 428 | (if (!HONOR_SNANS (type)) |
6a78ea5d | 429 | (non_lvalue @0))) |
430 | ||
431 | /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ | |
432 | (simplify | |
433 | (rdiv @0 real_minus_onep) | |
c8047e01 | 434 | (if (!HONOR_SNANS (type)) |
6a78ea5d | 435 | (negate @0))) |
436 | ||
ee5e3723 | 437 | (if (flag_reciprocal_math) |
07d4034c | 438 | /* Convert (A/B)/C to A/(B*C). */ |
ee5e3723 | 439 | (simplify |
440 | (rdiv (rdiv:s @0 @1) @2) | |
07d4034c | 441 | (rdiv @0 (mult @1 @2))) |
442 | ||
443 | /* Canonicalize x / (C1 * y) to (x * C2) / y. */ | |
444 | (simplify | |
445 | (rdiv @0 (mult:s @1 REAL_CST@2)) | |
446 | (with | |
447 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); } | |
448 | (if (tem) | |
449 | (rdiv (mult @0 { tem; } ) @1)))) | |
ee5e3723 | 450 | |
451 | /* Convert A/(B/C) to (A/B)*C */ | |
452 | (simplify | |
453 | (rdiv @0 (rdiv:s @1 @2)) | |
454 | (mult (rdiv @0 @1) @2))) | |
455 | ||
081337d3 | 456 | /* Simplify x / (- y) to -x / y. */ |
457 | (simplify | |
458 | (rdiv @0 (negate @1)) | |
459 | (rdiv (negate @0) @1)) | |
460 | ||
7abecd46 | 461 | (if (flag_unsafe_math_optimizations) |
462 | /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan. | |
463 | Since C / x may underflow to zero, do this only for unsafe math. */ | |
464 | (for op (lt le gt ge) | |
465 | neg_op (gt ge lt le) | |
466 | (simplify | |
467 | (op (rdiv REAL_CST@0 @1) real_zerop@2) | |
468 | (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1)) | |
469 | (switch | |
470 | (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0))) | |
471 | (op @1 @2)) | |
472 | /* For C < 0, use the inverted operator. */ | |
473 | (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0)) | |
474 | (neg_op @1 @2))))))) | |
475 | ||
ee5e3723 | 476 | /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */ |
477 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
478 | (simplify | |
479 | (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2) | |
480 | (if (integer_pow2p (@2) | |
481 | && tree_int_cst_sgn (@2) > 0 | |
cb7bca5f | 482 | && tree_nop_conversion_p (type, TREE_TYPE (@0)) |
e3d0f65c | 483 | && wi::to_wide (@2) + wi::to_wide (@1) == 0) |
484 | (rshift (convert @0) | |
485 | { build_int_cst (integer_type_node, | |
486 | wi::exact_log2 (wi::to_wide (@2))); })))) | |
ee5e3723 | 487 | |
6a78ea5d | 488 | /* If ARG1 is a constant, we can convert this to a multiply by the |
489 | reciprocal. This does not have the same rounding properties, | |
490 | so only do this if -freciprocal-math. We can actually | |
491 | always safely do it if ARG1 is a power of two, but it's hard to | |
492 | tell if it is or not in a portable manner. */ | |
493 | (for cst (REAL_CST COMPLEX_CST VECTOR_CST) | |
494 | (simplify | |
495 | (rdiv @0 cst@1) | |
496 | (if (optimize) | |
cd6db277 | 497 | (if (flag_reciprocal_math |
498 | && !real_zerop (@1)) | |
6a78ea5d | 499 | (with |
abe4e033 | 500 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); } |
6a78ea5d | 501 | (if (tem) |
d74b7335 | 502 | (mult @0 { tem; } ))) |
503 | (if (cst != COMPLEX_CST) | |
504 | (with { tree inverse = exact_inverse (type, @1); } | |
505 | (if (inverse) | |
506 | (mult @0 { inverse; } )))))))) | |
6a78ea5d | 507 | |
6a78ea5d | 508 | (for mod (ceil_mod floor_mod round_mod trunc_mod) |
55534d34 | 509 | /* 0 % X is always zero. */ |
510 | (simplify | |
6a78ea5d | 511 | (mod integer_zerop@0 @1) |
55534d34 | 512 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ |
513 | (if (!integer_zerop (@1)) | |
514 | @0)) | |
515 | /* X % 1 is always zero. */ | |
516 | (simplify | |
6a78ea5d | 517 | (mod @0 integer_onep) |
518 | { build_zero_cst (type); }) | |
519 | /* X % -1 is zero. */ | |
520 | (simplify | |
ccb14741 | 521 | (mod @0 integer_minus_onep@1) |
522 | (if (!TYPE_UNSIGNED (type)) | |
d792dcdf | 523 | { build_zero_cst (type); })) |
6062d7aa | 524 | /* X % X is zero. */ |
525 | (simplify | |
526 | (mod @0 @0) | |
527 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ | |
528 | (if (!integer_zerop (@0)) | |
529 | { build_zero_cst (type); })) | |
d792dcdf | 530 | /* (X % Y) % Y is just X % Y. */ |
531 | (simplify | |
532 | (mod (mod@2 @0 @1) @1) | |
54c06b77 | 533 | @2) |
534 | /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */ | |
535 | (simplify | |
536 | (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
537 | (if (ANY_INTEGRAL_TYPE_P (type) | |
538 | && TYPE_OVERFLOW_UNDEFINED (type) | |
e3d0f65c | 539 | && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2), |
540 | TYPE_SIGN (type))) | |
e3f07d30 | 541 | { build_zero_cst (type); })) |
542 | /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned | |
543 | modulo and comparison, since it is simpler and equivalent. */ | |
544 | (for cmp (eq ne) | |
545 | (simplify | |
546 | (cmp (mod @0 integer_pow2p@2) integer_zerop@1) | |
547 | (if (!TYPE_UNSIGNED (TREE_TYPE (@0))) | |
548 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
549 | (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1))))))) | |
6a78ea5d | 550 | |
551 | /* X % -C is the same as X % C. */ | |
552 | (simplify | |
553 | (trunc_mod @0 INTEGER_CST@1) | |
554 | (if (TYPE_SIGN (type) == SIGNED | |
555 | && !TREE_OVERFLOW (@1) | |
e3d0f65c | 556 | && wi::neg_p (wi::to_wide (@1)) |
6a78ea5d | 557 | && !TYPE_OVERFLOW_TRAPS (type) |
558 | /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ | |
559 | && !sign_bit_p (@1, @1)) | |
560 | (trunc_mod @0 (negate @1)))) | |
55534d34 | 561 | |
c09f2960 | 562 | /* X % -Y is the same as X % Y. */ |
563 | (simplify | |
564 | (trunc_mod @0 (convert? (negate @1))) | |
1bc579ed | 565 | (if (INTEGRAL_TYPE_P (type) |
566 | && !TYPE_UNSIGNED (type) | |
c09f2960 | 567 | && !TYPE_OVERFLOW_TRAPS (type) |
7dff0499 | 568 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) |
569 | /* Avoid this transformation if X might be INT_MIN or | |
570 | Y might be -1, because we would then change valid | |
571 | INT_MIN % -(-1) into invalid INT_MIN % -1. */ | |
e3d0f65c | 572 | && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type))) |
7dff0499 | 573 | || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION |
574 | (TREE_TYPE (@1)))))) | |
c09f2960 | 575 | (trunc_mod @0 (convert @1)))) |
576 | ||
ef5e7d0d | 577 | /* X - (X / Y) * Y is the same as X % Y. */ |
578 | (simplify | |
c76c65ae | 579 | (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1))) |
580 | (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) | |
f801d40f | 581 | (convert (trunc_mod @0 @1)))) |
ef5e7d0d | 582 | |
c09f2960 | 583 | /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, |
584 | i.e. "X % C" into "X & (C - 1)", if X and C are positive. | |
585 | Also optimize A % (C << N) where C is a power of 2, | |
586 | to A & ((C << N) - 1). */ | |
587 | (match (power_of_two_cand @1) | |
588 | INTEGER_CST@1) | |
589 | (match (power_of_two_cand @1) | |
590 | (lshift INTEGER_CST@1 @2)) | |
591 | (for mod (trunc_mod floor_mod) | |
592 | (simplify | |
928ea46c | 593 | (mod @0 (convert?@3 (power_of_two_cand@1 @2))) |
c09f2960 | 594 | (if ((TYPE_UNSIGNED (type) |
595 | || tree_expr_nonnegative_p (@0)) | |
928ea46c | 596 | && tree_nop_conversion_p (type, TREE_TYPE (@3)) |
c09f2960 | 597 | && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) |
928ea46c | 598 | (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))))) |
c09f2960 | 599 | |
71486465 | 600 | /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ |
601 | (simplify | |
602 | (trunc_div (mult @0 integer_pow2p@1) @1) | |
603 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
604 | (bit_and @0 { wide_int_to_tree | |
e3d0f65c | 605 | (type, wi::mask (TYPE_PRECISION (type) |
606 | - wi::exact_log2 (wi::to_wide (@1)), | |
71486465 | 607 | false, TYPE_PRECISION (type))); }))) |
608 | ||
20c41e0d | 609 | /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */ |
610 | (simplify | |
611 | (mult (trunc_div @0 integer_pow2p@1) @1) | |
612 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
613 | (bit_and @0 (negate @1)))) | |
614 | ||
3849736e | 615 | /* Simplify (t * 2) / 2) -> t. */ |
616 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
617 | (simplify | |
ac947eae | 618 | (div (mult:c @0 @1) @1) |
3849736e | 619 | (if (ANY_INTEGRAL_TYPE_P (type) |
620 | && TYPE_OVERFLOW_UNDEFINED (type)) | |
621 | @0))) | |
622 | ||
5702129f | 623 | (for op (negate abs) |
a614e82c | 624 | /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ |
625 | (for coss (COS COSH) | |
626 | (simplify | |
627 | (coss (op @0)) | |
628 | (coss @0))) | |
629 | /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */ | |
630 | (for pows (POW) | |
631 | (simplify | |
632 | (pows (op @0) REAL_CST@1) | |
633 | (with { HOST_WIDE_INT n; } | |
634 | (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) | |
a15183eb | 635 | (pows @0 @1))))) |
cbcde149 | 636 | /* Likewise for powi. */ |
637 | (for pows (POWI) | |
638 | (simplify | |
639 | (pows (op @0) INTEGER_CST@1) | |
e3d0f65c | 640 | (if ((wi::to_wide (@1) & 1) == 0) |
cbcde149 | 641 | (pows @0 @1)))) |
a15183eb | 642 | /* Strip negate and abs from both operands of hypot. */ |
643 | (for hypots (HYPOT) | |
644 | (simplify | |
645 | (hypots (op @0) @1) | |
646 | (hypots @0 @1)) | |
647 | (simplify | |
648 | (hypots @0 (op @1)) | |
649 | (hypots @0 @1))) | |
650 | /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */ | |
054e9558 | 651 | (for copysigns (COPYSIGN_ALL) |
a15183eb | 652 | (simplify |
653 | (copysigns (op @0) @1) | |
654 | (copysigns @0 @1)))) | |
655 | ||
656 | /* abs(x)*abs(x) -> x*x. Should be valid for all types. */ | |
657 | (simplify | |
658 | (mult (abs@1 @0) @1) | |
659 | (mult @0 @0)) | |
660 | ||
b3d1b253 | 661 | /* Convert absu(x)*absu(x) -> x*x. */ |
662 | (simplify | |
663 | (mult (absu@1 @0) @1) | |
664 | (mult (convert@2 @0) @2)) | |
665 | ||
a15183eb | 666 | /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */ |
667 | (for coss (COS COSH) | |
668 | copysigns (COPYSIGN) | |
669 | (simplify | |
670 | (coss (copysigns @0 @1)) | |
671 | (coss @0))) | |
672 | ||
673 | /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */ | |
674 | (for pows (POW) | |
675 | copysigns (COPYSIGN) | |
676 | (simplify | |
cbcde149 | 677 | (pows (copysigns @0 @2) REAL_CST@1) |
a15183eb | 678 | (with { HOST_WIDE_INT n; } |
679 | (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) | |
680 | (pows @0 @1))))) | |
cbcde149 | 681 | /* Likewise for powi. */ |
682 | (for pows (POWI) | |
683 | copysigns (COPYSIGN) | |
684 | (simplify | |
685 | (pows (copysigns @0 @2) INTEGER_CST@1) | |
e3d0f65c | 686 | (if ((wi::to_wide (@1) & 1) == 0) |
cbcde149 | 687 | (pows @0 @1)))) |
a15183eb | 688 | |
689 | (for hypots (HYPOT) | |
690 | copysigns (COPYSIGN) | |
691 | /* hypot(copysign(x, y), z) -> hypot(x, z). */ | |
692 | (simplify | |
693 | (hypots (copysigns @0 @1) @2) | |
694 | (hypots @0 @2)) | |
695 | /* hypot(x, copysign(y, z)) -> hypot(x, y). */ | |
696 | (simplify | |
697 | (hypots @0 (copysigns @1 @2)) | |
698 | (hypots @0 @1))) | |
699 | ||
eb93b162 | 700 | /* copysign(x, CST) -> [-]abs (x). */ |
054e9558 | 701 | (for copysigns (COPYSIGN_ALL) |
eb93b162 | 702 | (simplify |
703 | (copysigns @0 REAL_CST@1) | |
704 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
705 | (negate (abs @0)) | |
706 | (abs @0)))) | |
707 | ||
a15183eb | 708 | /* copysign(copysign(x, y), z) -> copysign(x, z). */ |
054e9558 | 709 | (for copysigns (COPYSIGN_ALL) |
a15183eb | 710 | (simplify |
711 | (copysigns (copysigns @0 @1) @2) | |
712 | (copysigns @0 @2))) | |
713 | ||
714 | /* copysign(x,y)*copysign(x,y) -> x*x. */ | |
054e9558 | 715 | (for copysigns (COPYSIGN_ALL) |
a15183eb | 716 | (simplify |
717 | (mult (copysigns@2 @0 @1) @2) | |
718 | (mult @0 @0))) | |
719 | ||
720 | /* ccos(-x) -> ccos(x). Similarly for ccosh. */ | |
721 | (for ccoss (CCOS CCOSH) | |
722 | (simplify | |
723 | (ccoss (negate @0)) | |
724 | (ccoss @0))) | |
5702129f | 725 | |
d064d976 | 726 | /* cabs(-x) and cos(conj(x)) -> cabs(x). */ |
727 | (for ops (conj negate) | |
728 | (for cabss (CABS) | |
729 | (simplify | |
730 | (cabss (ops @0)) | |
731 | (cabss @0)))) | |
732 | ||
35c8219c | 733 | /* Fold (a * (1 << b)) into (a << b) */ |
734 | (simplify | |
735 | (mult:c @0 (convert? (lshift integer_onep@1 @2))) | |
736 | (if (! FLOAT_TYPE_P (type) | |
51a43c06 | 737 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) |
35c8219c | 738 | (lshift @0 @2))) |
739 | ||
a6eef1ed | 740 | /* Fold (1 << (C - x)) where C = precision(type) - 1 |
741 | into ((1 << C) >> x). */ | |
742 | (simplify | |
743 | (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3)) | |
744 | (if (INTEGRAL_TYPE_P (type) | |
12f26ffd | 745 | && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1) |
a6eef1ed | 746 | && single_use (@1)) |
747 | (if (TYPE_UNSIGNED (type)) | |
748 | (rshift (lshift @0 @2) @3) | |
749 | (with | |
750 | { tree utype = unsigned_type_for (type); } | |
751 | (convert (rshift (lshift (convert:utype @0) @2) @3)))))) | |
752 | ||
35c8219c | 753 | /* Fold (C1/X)*C2 into (C1*C2)/X. */ |
754 | (simplify | |
b4007655 | 755 | (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2) |
756 | (if (flag_associative_math | |
757 | && single_use (@3)) | |
35c8219c | 758 | (with |
759 | { tree tem = const_binop (MULT_EXPR, type, @0, @2); } | |
760 | (if (tem) | |
761 | (rdiv { tem; } @1))))) | |
762 | ||
763 | /* Simplify ~X & X as zero. */ | |
764 | (simplify | |
765 | (bit_and:c (convert? @0) (convert? (bit_not @0))) | |
766 | { build_zero_cst (type); }) | |
767 | ||
6a0d39d6 | 768 | /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */ |
769 | (simplify | |
770 | (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep)) | |
771 | (if (TYPE_UNSIGNED (type)) | |
772 | (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1))))) | |
773 | ||
4a9eb7eb | 774 | (for bitop (bit_and bit_ior) |
775 | cmp (eq ne) | |
8ced4965 | 776 | /* PR35691: Transform |
777 | (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0. | |
778 | (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */ | |
4a9eb7eb | 779 | (simplify |
780 | (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop)) | |
781 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
8ced4965 | 782 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
783 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
784 | (cmp (bit_ior @0 (convert @1)) @2))) | |
785 | /* Transform: | |
786 | (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1. | |
787 | (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */ | |
788 | (simplify | |
789 | (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp)) | |
790 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
791 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
792 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
793 | (cmp (bit_and @0 (convert @1)) @2)))) | |
4a9eb7eb | 794 | |
7bbdeb32 | 795 | /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */ |
796 | (simplify | |
99618e62 | 797 | (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1)) |
7bbdeb32 | 798 | (minus (bit_xor @0 @1) @1)) |
799 | (simplify | |
800 | (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1)) | |
e3d0f65c | 801 | (if (~wi::to_wide (@2) == wi::to_wide (@1)) |
7bbdeb32 | 802 | (minus (bit_xor @0 @1) @1))) |
803 | ||
804 | /* Fold (A & B) - (A & ~B) into B - (A ^ B). */ | |
805 | (simplify | |
a1a80007 | 806 | (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1))) |
7bbdeb32 | 807 | (minus @1 (bit_xor @0 @1))) |
808 | ||
20ce76cb | 809 | /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */ |
810 | (for op (bit_ior bit_xor plus) | |
811 | (simplify | |
812 | (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1)) | |
813 | (bit_xor @0 @1)) | |
814 | (simplify | |
815 | (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1)) | |
e3d0f65c | 816 | (if (~wi::to_wide (@2) == wi::to_wide (@1)) |
20ce76cb | 817 | (bit_xor @0 @1)))) |
ab056ef4 | 818 | |
819 | /* PR53979: Transform ((a ^ b) | a) -> (a | b) */ | |
820 | (simplify | |
821 | (bit_ior:c (bit_xor:c @0 @1) @0) | |
822 | (bit_ior @0 @1)) | |
823 | ||
270a468f | 824 | /* (a & ~b) | (a ^ b) --> a ^ b */ |
825 | (simplify | |
826 | (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1)) | |
827 | @2) | |
828 | ||
829 | /* (a & ~b) ^ ~a --> ~(a & b) */ | |
830 | (simplify | |
831 | (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0)) | |
832 | (bit_not (bit_and @0 @1))) | |
833 | ||
834 | /* (a | b) & ~(a ^ b) --> a & b */ | |
835 | (simplify | |
836 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1))) | |
837 | (bit_and @0 @1)) | |
838 | ||
839 | /* a | ~(a ^ b) --> a | ~b */ | |
840 | (simplify | |
841 | (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1))) | |
842 | (bit_ior @0 (bit_not @1))) | |
843 | ||
844 | /* (a | b) | (a &^ b) --> a | b */ | |
845 | (for op (bit_and bit_xor) | |
846 | (simplify | |
847 | (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1)) | |
848 | @2)) | |
849 | ||
850 | /* (a & b) | ~(a ^ b) --> ~(a ^ b) */ | |
851 | (simplify | |
852 | (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1))) | |
853 | @2) | |
854 | ||
855 | /* ~(~a & b) --> a | ~b */ | |
856 | (simplify | |
857 | (bit_not (bit_and:cs (bit_not @0) @1)) | |
858 | (bit_ior @0 (bit_not @1))) | |
859 | ||
4c202e6d | 860 | /* ~(~a | b) --> a & ~b */ |
861 | (simplify | |
862 | (bit_not (bit_ior:cs (bit_not @0) @1)) | |
863 | (bit_and @0 (bit_not @1))) | |
864 | ||
a8719545 | 865 | /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */ |
866 | #if GIMPLE | |
867 | (simplify | |
868 | (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1) | |
869 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
e3d0f65c | 870 | && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) |
a8719545 | 871 | (bit_xor @0 @1))) |
872 | #endif | |
7bbdeb32 | 873 | |
dba2ece2 | 874 | /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M, |
875 | ((A & N) + B) & M -> (A + B) & M | |
876 | Similarly if (N & M) == 0, | |
877 | ((A | N) + B) & M -> (A + B) & M | |
878 | and for - instead of + (or unary - instead of +) | |
879 | and/or ^ instead of |. | |
880 | If B is constant and (B & M) == 0, fold into A & M. */ | |
881 | (for op (plus minus) | |
882 | (for bitop (bit_and bit_ior bit_xor) | |
883 | (simplify | |
884 | (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2) | |
885 | (with | |
886 | { tree pmop[2]; | |
887 | tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop, | |
888 | @3, @4, @1, ERROR_MARK, NULL_TREE, | |
889 | NULL_TREE, pmop); } | |
890 | (if (utype) | |
891 | (convert (bit_and (op (convert:utype { pmop[0]; }) | |
892 | (convert:utype { pmop[1]; })) | |
893 | (convert:utype @2)))))) | |
894 | (simplify | |
895 | (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2) | |
896 | (with | |
897 | { tree pmop[2]; | |
898 | tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK, | |
899 | NULL_TREE, NULL_TREE, @1, bitop, @3, | |
900 | @4, pmop); } | |
901 | (if (utype) | |
902 | (convert (bit_and (op (convert:utype { pmop[0]; }) | |
903 | (convert:utype { pmop[1]; })) | |
904 | (convert:utype @2))))))) | |
905 | (simplify | |
906 | (bit_and (op:s @0 @1) INTEGER_CST@2) | |
907 | (with | |
908 | { tree pmop[2]; | |
909 | tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK, | |
910 | NULL_TREE, NULL_TREE, @1, ERROR_MARK, | |
911 | NULL_TREE, NULL_TREE, pmop); } | |
912 | (if (utype) | |
913 | (convert (bit_and (op (convert:utype { pmop[0]; }) | |
914 | (convert:utype { pmop[1]; })) | |
915 | (convert:utype @2))))))) | |
916 | (for bitop (bit_and bit_ior bit_xor) | |
917 | (simplify | |
918 | (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1) | |
919 | (with | |
920 | { tree pmop[2]; | |
921 | tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0, | |
922 | bitop, @2, @3, NULL_TREE, ERROR_MARK, | |
923 | NULL_TREE, NULL_TREE, pmop); } | |
924 | (if (utype) | |
925 | (convert (bit_and (negate (convert:utype { pmop[0]; })) | |
926 | (convert:utype @1))))))) | |
927 | ||
d792dcdf | 928 | /* X % Y is smaller than Y. */ |
929 | (for cmp (lt ge) | |
930 | (simplify | |
931 | (cmp (trunc_mod @0 @1) @1) | |
932 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
933 | { constant_boolean_node (cmp == LT_EXPR, type); }))) | |
934 | (for cmp (gt le) | |
935 | (simplify | |
936 | (cmp @1 (trunc_mod @0 @1)) | |
937 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
938 | { constant_boolean_node (cmp == GT_EXPR, type); }))) | |
939 | ||
55534d34 | 940 | /* x | ~0 -> ~0 */ |
941 | (simplify | |
e728d5ba | 942 | (bit_ior @0 integer_all_onesp@1) |
943 | @1) | |
944 | ||
945 | /* x | 0 -> x */ | |
946 | (simplify | |
947 | (bit_ior @0 integer_zerop) | |
948 | @0) | |
55534d34 | 949 | |
950 | /* x & 0 -> 0 */ | |
951 | (simplify | |
e728d5ba | 952 | (bit_and @0 integer_zerop@1) |
953 | @1) | |
55534d34 | 954 | |
f6f5b85f | 955 | /* ~x | x -> -1 */ |
c8047e01 | 956 | /* ~x ^ x -> -1 */ |
957 | /* ~x + x -> -1 */ | |
958 | (for op (bit_ior bit_xor plus) | |
959 | (simplify | |
960 | (op:c (convert? @0) (convert? (bit_not @0))) | |
961 | (convert { build_all_ones_cst (TREE_TYPE (@0)); }))) | |
f6f5b85f | 962 | |
55534d34 | 963 | /* x ^ x -> 0 */ |
964 | (simplify | |
965 | (bit_xor @0 @0) | |
966 | { build_zero_cst (type); }) | |
967 | ||
58810b92 | 968 | /* Canonicalize X ^ ~0 to ~X. */ |
969 | (simplify | |
970 | (bit_xor @0 integer_all_onesp@1) | |
971 | (bit_not @0)) | |
972 | ||
973 | /* x & ~0 -> x */ | |
974 | (simplify | |
975 | (bit_and @0 integer_all_onesp) | |
976 | (non_lvalue @0)) | |
977 | ||
978 | /* x & x -> x, x | x -> x */ | |
979 | (for bitop (bit_and bit_ior) | |
980 | (simplify | |
981 | (bitop @0 @0) | |
982 | (non_lvalue @0))) | |
983 | ||
b6dd53bf | 984 | /* x & C -> x if we know that x & ~C == 0. */ |
985 | #if GIMPLE | |
986 | (simplify | |
987 | (bit_and SSA_NAME@0 INTEGER_CST@1) | |
988 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
e3d0f65c | 989 | && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) |
b6dd53bf | 990 | @0)) |
991 | #endif | |
992 | ||
c0cf1b24 | 993 | /* x + (x & 1) -> (x + 1) & ~1 */ |
994 | (simplify | |
2bc3151f | 995 | (plus:c @0 (bit_and:s @0 integer_onep@1)) |
996 | (bit_and (plus @0 @1) (bit_not @1))) | |
c0cf1b24 | 997 | |
998 | /* x & ~(x & y) -> x & ~y */ | |
999 | /* x | ~(x | y) -> x | ~y */ | |
1000 | (for bitop (bit_and bit_ior) | |
5af95dbe | 1001 | (simplify |
2bc3151f | 1002 | (bitop:c @0 (bit_not (bitop:cs @0 @1))) |
1003 | (bitop @0 (bit_not @1)))) | |
5af95dbe | 1004 | |
9603b585 | 1005 | /* (~x & y) | ~(x | y) -> ~x */ |
1006 | (simplify | |
1007 | (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1))) | |
1008 | @2) | |
1009 | ||
1010 | /* (x | y) ^ (x | ~y) -> ~x */ | |
1011 | (simplify | |
1012 | (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1))) | |
1013 | (bit_not @0)) | |
1014 | ||
1015 | /* (x & y) | ~(x | y) -> ~(x ^ y) */ | |
1016 | (simplify | |
1017 | (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1))) | |
1018 | (bit_not (bit_xor @0 @1))) | |
1019 | ||
1020 | /* (~x | y) ^ (x ^ y) -> x | ~y */ | |
1021 | (simplify | |
1022 | (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1)) | |
1023 | (bit_ior @0 (bit_not @1))) | |
1024 | ||
1025 | /* (x ^ y) | ~(x | y) -> ~(x & y) */ | |
1026 | (simplify | |
1027 | (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1))) | |
1028 | (bit_not (bit_and @0 @1))) | |
1029 | ||
5af95dbe | 1030 | /* (x | y) & ~x -> y & ~x */ |
1031 | /* (x & y) | ~x -> y | ~x */ | |
1032 | (for bitop (bit_and bit_ior) | |
1033 | rbitop (bit_ior bit_and) | |
1034 | (simplify | |
1035 | (bitop:c (rbitop:c @0 @1) (bit_not@2 @0)) | |
1036 | (bitop @1 @2))) | |
c0cf1b24 | 1037 | |
88fc652a | 1038 | /* (x & y) ^ (x | y) -> x ^ y */ |
1039 | (simplify | |
2652cce5 | 1040 | (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1)) |
1041 | (bit_xor @0 @1)) | |
88fc652a | 1042 | |
800f9fe9 | 1043 | /* (x ^ y) ^ (x | y) -> x & y */ |
1044 | (simplify | |
1045 | (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1)) | |
1046 | (bit_and @0 @1)) | |
1047 | ||
1048 | /* (x & y) + (x ^ y) -> x | y */ | |
1049 | /* (x & y) | (x ^ y) -> x | y */ | |
1050 | /* (x & y) ^ (x ^ y) -> x | y */ | |
1051 | (for op (plus bit_ior bit_xor) | |
1052 | (simplify | |
1053 | (op:c (bit_and @0 @1) (bit_xor @0 @1)) | |
1054 | (bit_ior @0 @1))) | |
1055 | ||
1056 | /* (x & y) + (x | y) -> x + y */ | |
1057 | (simplify | |
1058 | (plus:c (bit_and @0 @1) (bit_ior @0 @1)) | |
1059 | (plus @0 @1)) | |
1060 | ||
88323eef | 1061 | /* (x + y) - (x | y) -> x & y */ |
1062 | (simplify | |
1063 | (minus (plus @0 @1) (bit_ior @0 @1)) | |
1064 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
1065 | && !TYPE_SATURATING (type)) | |
1066 | (bit_and @0 @1))) | |
1067 | ||
1068 | /* (x + y) - (x & y) -> x | y */ | |
1069 | (simplify | |
1070 | (minus (plus @0 @1) (bit_and @0 @1)) | |
1071 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
1072 | && !TYPE_SATURATING (type)) | |
1073 | (bit_ior @0 @1))) | |
1074 | ||
800f9fe9 | 1075 | /* (x | y) - (x ^ y) -> x & y */ |
1076 | (simplify | |
1077 | (minus (bit_ior @0 @1) (bit_xor @0 @1)) | |
1078 | (bit_and @0 @1)) | |
1079 | ||
1080 | /* (x | y) - (x & y) -> x ^ y */ | |
1081 | (simplify | |
1082 | (minus (bit_ior @0 @1) (bit_and @0 @1)) | |
1083 | (bit_xor @0 @1)) | |
1084 | ||
001eae4f | 1085 | /* (x | y) & ~(x & y) -> x ^ y */ |
1086 | (simplify | |
1087 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1))) | |
1088 | (bit_xor @0 @1)) | |
1089 | ||
1090 | /* (x | y) & (~x ^ y) -> x & y */ | |
1091 | (simplify | |
1092 | (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0))) | |
1093 | (bit_and @0 @1)) | |
1094 | ||
4c202e6d | 1095 | /* (~x | y) & (x | ~y) -> ~(x ^ y) */ |
1096 | (simplify | |
1097 | (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1))) | |
1098 | (bit_not (bit_xor @0 @1))) | |
1099 | ||
1100 | /* (~x | y) ^ (x | ~y) -> x ^ y */ | |
1101 | (simplify | |
1102 | (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1))) | |
1103 | (bit_xor @0 @1)) | |
1104 | ||
168a6cd2 | 1105 | /* ~x & ~y -> ~(x | y) |
1106 | ~x | ~y -> ~(x & y) */ | |
1107 | (for op (bit_and bit_ior) | |
1108 | rop (bit_ior bit_and) | |
1109 | (simplify | |
1110 | (op (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
c0205bab | 1111 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1112 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
168a6cd2 | 1113 | (bit_not (rop (convert @0) (convert @1)))))) |
1114 | ||
a76fc0fd | 1115 | /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing |
168a6cd2 | 1116 | with a constant, and the two constants have no bits in common, |
1117 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
1118 | simplifications. */ | |
a76fc0fd | 1119 | (for op (bit_xor plus) |
1120 | (simplify | |
1121 | (op (convert1? (bit_and@4 @0 INTEGER_CST@1)) | |
1122 | (convert2? (bit_and@5 @2 INTEGER_CST@3))) | |
1123 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
1124 | && tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
e3d0f65c | 1125 | && (wi::to_wide (@1) & wi::to_wide (@3)) == 0) |
a76fc0fd | 1126 | (bit_ior (convert @4) (convert @5))))) |
168a6cd2 | 1127 | |
1128 | /* (X | Y) ^ X -> Y & ~ X*/ | |
1129 | (simplify | |
c76c65ae | 1130 | (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0)) |
168a6cd2 | 1131 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
1132 | (convert (bit_and @1 (bit_not @0))))) | |
1133 | ||
1134 | /* Convert ~X ^ ~Y to X ^ Y. */ | |
1135 | (simplify | |
1136 | (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
c0205bab | 1137 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1138 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
168a6cd2 | 1139 | (bit_xor (convert @0) (convert @1)))) |
1140 | ||
1141 | /* Convert ~X ^ C to X ^ ~C. */ | |
1142 | (simplify | |
1143 | (bit_xor (convert? (bit_not @0)) INTEGER_CST@1) | |
535b528e | 1144 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
1145 | (bit_xor (convert @0) (bit_not @1)))) | |
168a6cd2 | 1146 | |
f9153689 | 1147 | /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */ |
1148 | (for opo (bit_and bit_xor) | |
1149 | opi (bit_xor bit_and) | |
1150 | (simplify | |
d5e19fa1 | 1151 | (opo:c (opi:cs @0 @1) @1) |
f9153689 | 1152 | (bit_and (bit_not @0) @1))) |
486bdb6a | 1153 | |
a76fc0fd | 1154 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both |
1155 | operands are another bit-wise operation with a common input. If so, | |
1156 | distribute the bit operations to save an operation and possibly two if | |
1157 | constants are involved. For example, convert | |
1158 | (A | B) & (A | C) into A | (B & C) | |
1159 | Further simplification will occur if B and C are constants. */ | |
846521ce | 1160 | (for op (bit_and bit_ior bit_xor) |
1161 | rop (bit_ior bit_and bit_and) | |
a76fc0fd | 1162 | (simplify |
c76c65ae | 1163 | (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2))) |
846521ce | 1164 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) |
1165 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
a76fc0fd | 1166 | (rop (convert @0) (op (convert @1) (convert @2)))))) |
1167 | ||
f9153689 | 1168 | /* Some simple reassociation for bit operations, also handled in reassoc. */ |
1169 | /* (X & Y) & Y -> X & Y | |
1170 | (X | Y) | Y -> X | Y */ | |
1171 | (for op (bit_and bit_ior) | |
1172 | (simplify | |
c76c65ae | 1173 | (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1)) |
f9153689 | 1174 | @2)) |
1175 | /* (X ^ Y) ^ Y -> X */ | |
1176 | (simplify | |
c76c65ae | 1177 | (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1)) |
c0205bab | 1178 | (convert @0)) |
f9153689 | 1179 | /* (X & Y) & (X & Z) -> (X & Y) & Z |
1180 | (X | Y) | (X | Z) -> (X | Y) | Z */ | |
1181 | (for op (bit_and bit_ior) | |
1182 | (simplify | |
460001de | 1183 | (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2))) |
f9153689 | 1184 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) |
1185 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
1186 | (if (single_use (@5) && single_use (@6)) | |
1187 | (op @3 (convert @2)) | |
1188 | (if (single_use (@3) && single_use (@4)) | |
1189 | (op (convert @1) @5)))))) | |
1190 | /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */ | |
1191 | (simplify | |
1192 | (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2))) | |
1193 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
1194 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
92e3ffbf | 1195 | (bit_xor (convert @1) (convert @2)))) |
168a6cd2 | 1196 | |
b3d1b253 | 1197 | /* Convert abs (abs (X)) into abs (X). |
1198 | also absu (absu (X)) into absu (X). */ | |
98b69bcc | 1199 | (simplify |
1200 | (abs (abs@1 @0)) | |
1201 | @1) | |
b3d1b253 | 1202 | |
1203 | (simplify | |
1204 | (absu (convert@2 (absu@1 @0))) | |
1205 | (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1))) | |
1206 | @1)) | |
1207 | ||
1208 | /* Convert abs[u] (-X) -> abs[u] (X). */ | |
2ad7e37a | 1209 | (simplify |
1210 | (abs (negate @0)) | |
1211 | (abs @0)) | |
b3d1b253 | 1212 | |
1213 | (simplify | |
1214 | (absu (negate @0)) | |
1215 | (absu @0)) | |
1216 | ||
1217 | /* Convert abs[u] (X) where X is nonnegative -> (X). */ | |
2ad7e37a | 1218 | (simplify |
1219 | (abs tree_expr_nonnegative_p@0) | |
1220 | @0) | |
1221 | ||
b3d1b253 | 1222 | (simplify |
1223 | (absu tree_expr_nonnegative_p@0) | |
1224 | (convert @0)) | |
1225 | ||
004b4ca3 | 1226 | /* A few cases of fold-const.c negate_expr_p predicate. */ |
1227 | (match negate_expr_p | |
1228 | INTEGER_CST | |
98b69bcc | 1229 | (if ((INTEGRAL_TYPE_P (type) |
da78c088 | 1230 | && TYPE_UNSIGNED (type)) |
98b69bcc | 1231 | || (!TYPE_OVERFLOW_SANITIZED (type) |
004b4ca3 | 1232 | && may_negate_without_overflow_p (t))))) |
1233 | (match negate_expr_p | |
1234 | FIXED_CST) | |
1235 | (match negate_expr_p | |
1236 | (negate @0) | |
1237 | (if (!TYPE_OVERFLOW_SANITIZED (type)))) | |
1238 | (match negate_expr_p | |
1239 | REAL_CST | |
1240 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t))))) | |
1241 | /* VECTOR_CST handling of non-wrapping types would recurse in unsupported | |
1242 | ways. */ | |
1243 | (match negate_expr_p | |
1244 | VECTOR_CST | |
1245 | (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)))) | |
c3aa270e | 1246 | (match negate_expr_p |
1247 | (minus @0 @1) | |
1248 | (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)) | |
1249 | || (FLOAT_TYPE_P (type) | |
1250 | && !HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1251 | && !HONOR_SIGNED_ZEROS (type))))) | |
35c8219c | 1252 | |
1253 | /* (-A) * (-B) -> A * B */ | |
1254 | (simplify | |
1255 | (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1)) | |
1256 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
1257 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
1258 | (mult (convert @0) (convert (negate @1))))) | |
9603b585 | 1259 | |
004b4ca3 | 1260 | /* -(A + B) -> (-B) - A. */ |
98b69bcc | 1261 | (simplify |
004b4ca3 | 1262 | (negate (plus:c @0 negate_expr_p@1)) |
1263 | (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) | |
1264 | && !HONOR_SIGNED_ZEROS (element_mode (type))) | |
1265 | (minus (negate @1) @0))) | |
1266 | ||
c3aa270e | 1267 | /* -(A - B) -> B - A. */ |
1268 | (simplify | |
1269 | (negate (minus @0 @1)) | |
1270 | (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type)) | |
1271 | || (FLOAT_TYPE_P (type) | |
1272 | && !HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1273 | && !HONOR_SIGNED_ZEROS (type))) | |
1274 | (minus @1 @0))) | |
57e83b58 | 1275 | (simplify |
1276 | (negate (pointer_diff @0 @1)) | |
1277 | (if (TYPE_OVERFLOW_UNDEFINED (type)) | |
1278 | (pointer_diff @1 @0))) | |
c3aa270e | 1279 | |
004b4ca3 | 1280 | /* A - B -> A + (-B) if B is easily negatable. */ |
98b69bcc | 1281 | (simplify |
004b4ca3 | 1282 | (minus @0 negate_expr_p@1) |
b1eac901 | 1283 | (if (!FIXED_POINT_TYPE_P (type)) |
1284 | (plus @0 (negate @1)))) | |
6d58fd21 | 1285 | |
d0eb9b3d | 1286 | /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) |
1287 | when profitable. | |
1288 | For bitwise binary operations apply operand conversions to the | |
1289 | binary operation result instead of to the operands. This allows | |
1290 | to combine successive conversions and bitwise binary operations. | |
1291 | We combine the above two cases by using a conditional convert. */ | |
1292 | (for bitop (bit_and bit_ior bit_xor) | |
1293 | (simplify | |
1294 | (bitop (convert @0) (convert? @1)) | |
1295 | (if (((TREE_CODE (@1) == INTEGER_CST | |
1296 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
a6e46537 | 1297 | && int_fits_type_p (@1, TREE_TYPE (@0))) |
b1306f12 | 1298 | || types_match (@0, @1)) |
a6e46537 | 1299 | /* ??? This transform conflicts with fold-const.c doing |
1300 | Convert (T)(x & c) into (T)x & (T)c, if c is an integer | |
1301 | constants (if x has signed type, the sign bit cannot be set | |
1302 | in c). This folds extension into the BIT_AND_EXPR. | |
1303 | Restrict it to GIMPLE to avoid endless recursions. */ | |
1304 | && (bitop != BIT_AND_EXPR || GIMPLE) | |
d0eb9b3d | 1305 | && (/* That's a good idea if the conversion widens the operand, thus |
1306 | after hoisting the conversion the operation will be narrower. */ | |
1307 | TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type) | |
1308 | /* It's also a good idea if the conversion is to a non-integer | |
1309 | mode. */ | |
1310 | || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT | |
1311 | /* Or if the precision of TO is not the same as the precision | |
1312 | of its mode. */ | |
654ba22c | 1313 | || !type_has_mode_precision_p (type))) |
d0eb9b3d | 1314 | (convert (bitop @0 (convert @1)))))) |
1315 | ||
98b69bcc | 1316 | (for bitop (bit_and bit_ior) |
1317 | rbitop (bit_ior bit_and) | |
1318 | /* (x | y) & x -> x */ | |
1319 | /* (x & y) | x -> x */ | |
1320 | (simplify | |
1321 | (bitop:c (rbitop:c @0 @1) @0) | |
1322 | @0) | |
1323 | /* (~x | y) & x -> x & y */ | |
1324 | /* (~x & y) | x -> x | y */ | |
1325 | (simplify | |
1326 | (bitop:c (rbitop:c (bit_not @0) @1) @0) | |
1327 | (bitop @0 @1))) | |
1328 | ||
d0eb9b3d | 1329 | /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ |
1330 | (simplify | |
1331 | (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
1332 | (bit_ior (bit_and @0 @2) (bit_and @1 @2))) | |
1333 | ||
1334 | /* Combine successive equal operations with constants. */ | |
1335 | (for bitop (bit_and bit_ior bit_xor) | |
1336 | (simplify | |
1337 | (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
6a833a2e | 1338 | (if (!CONSTANT_CLASS_P (@0)) |
1339 | /* This is the canonical form regardless of whether (bitop @1 @2) can be | |
1340 | folded to a constant. */ | |
1341 | (bitop @0 (bitop @1 @2)) | |
1342 | /* In this case we have three constants and (bitop @0 @1) doesn't fold | |
1343 | to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if | |
1344 | the values involved are such that the operation can't be decided at | |
1345 | compile time. Try folding one of @0 or @1 with @2 to see whether | |
1346 | that combination can be decided at compile time. | |
1347 | ||
1348 | Keep the existing form if both folds fail, to avoid endless | |
1349 | oscillation. */ | |
1350 | (with { tree cst1 = const_binop (bitop, type, @0, @2); } | |
1351 | (if (cst1) | |
1352 | (bitop @1 { cst1; }) | |
1353 | (with { tree cst2 = const_binop (bitop, type, @1, @2); } | |
1354 | (if (cst2) | |
1355 | (bitop @0 { cst2; })))))))) | |
d0eb9b3d | 1356 | |
1357 | /* Try simple folding for X op !X, and X op X with the help | |
1358 | of the truth_valued_p and logical_inverted_value predicates. */ | |
1359 | (match truth_valued_p | |
1360 | @0 | |
1361 | (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))) | |
9ec260f0 | 1362 | (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor) |
d0eb9b3d | 1363 | (match truth_valued_p |
1364 | (op @0 @1))) | |
1365 | (match truth_valued_p | |
1366 | (truth_not @0)) | |
1367 | ||
35c8219c | 1368 | (match (logical_inverted_value @0) |
1369 | (truth_not @0)) | |
d0eb9b3d | 1370 | (match (logical_inverted_value @0) |
1371 | (bit_not truth_valued_p@0)) | |
1372 | (match (logical_inverted_value @0) | |
ccb14741 | 1373 | (eq @0 integer_zerop)) |
d0eb9b3d | 1374 | (match (logical_inverted_value @0) |
ccb14741 | 1375 | (ne truth_valued_p@0 integer_truep)) |
d0eb9b3d | 1376 | (match (logical_inverted_value @0) |
ccb14741 | 1377 | (bit_xor truth_valued_p@0 integer_truep)) |
d0eb9b3d | 1378 | |
1379 | /* X & !X -> 0. */ | |
1380 | (simplify | |
1381 | (bit_and:c @0 (logical_inverted_value @0)) | |
1382 | { build_zero_cst (type); }) | |
1383 | /* X | !X and X ^ !X -> 1, , if X is truth-valued. */ | |
1384 | (for op (bit_ior bit_xor) | |
1385 | (simplify | |
1386 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
9ec260f0 | 1387 | { constant_boolean_node (true, type); })) |
373bcaaf | 1388 | /* X ==/!= !X is false/true. */ |
1389 | (for op (eq ne) | |
1390 | (simplify | |
1391 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
1392 | { constant_boolean_node (op == NE_EXPR ? true : false, type); })) | |
d0eb9b3d | 1393 | |
d0eb9b3d | 1394 | /* ~~x -> x */ |
1395 | (simplify | |
1396 | (bit_not (bit_not @0)) | |
1397 | @0) | |
1398 | ||
98b69bcc | 1399 | /* Convert ~ (-A) to A - 1. */ |
1400 | (simplify | |
1401 | (bit_not (convert? (negate @0))) | |
c0205bab | 1402 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1403 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
c8047e01 | 1404 | (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) |
98b69bcc | 1405 | |
c3aa270e | 1406 | /* Convert - (~A) to A + 1. */ |
1407 | (simplify | |
1408 | (negate (nop_convert (bit_not @0))) | |
1409 | (plus (view_convert @0) { build_each_one_cst (type); })) | |
1410 | ||
98b69bcc | 1411 | /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ |
1412 | (simplify | |
c8047e01 | 1413 | (bit_not (convert? (minus @0 integer_each_onep))) |
c0205bab | 1414 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1415 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
98b69bcc | 1416 | (convert (negate @0)))) |
1417 | (simplify | |
1418 | (bit_not (convert? (plus @0 integer_all_onesp))) | |
c0205bab | 1419 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1420 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
98b69bcc | 1421 | (convert (negate @0)))) |
1422 | ||
1423 | /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ | |
1424 | (simplify | |
1425 | (bit_not (convert? (bit_xor @0 INTEGER_CST@1))) | |
1426 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1427 | (convert (bit_xor @0 (bit_not @1))))) | |
1428 | (simplify | |
1429 | (bit_not (convert? (bit_xor:c (bit_not @0) @1))) | |
1430 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1431 | (convert (bit_xor @0 @1)))) | |
1432 | ||
270a468f | 1433 | /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */ |
1434 | (simplify | |
1435 | (bit_xor:c (nop_convert:s (bit_not:s @0)) @1) | |
1436 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1437 | (bit_not (bit_xor (view_convert @0) @1)))) | |
1438 | ||
277f7164 | 1439 | /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ |
1440 | (simplify | |
2bc3151f | 1441 | (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2)) |
1442 | (bit_xor (bit_and (bit_xor @0 @1) @2) @0)) | |
277f7164 | 1443 | |
bd9846d0 | 1444 | /* Fold A - (A & B) into ~B & A. */ |
1445 | (simplify | |
c76c65ae | 1446 | (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1))) |
bd9846d0 | 1447 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) |
1448 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
1449 | (convert (bit_and (bit_not @1) @0)))) | |
d0eb9b3d | 1450 | |
0aa23bd4 | 1451 | /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */ |
1452 | (for cmp (gt lt ge le) | |
1453 | (simplify | |
1454 | (mult (convert (cmp @0 @1)) @2) | |
1455 | (cond (cmp @0 @1) @2 { build_zero_cst (type); }))) | |
1456 | ||
70229d3b | 1457 | /* For integral types with undefined overflow and C != 0 fold |
1458 | x * C EQ/NE y * C into x EQ/NE y. */ | |
1459 | (for cmp (eq ne) | |
1460 | (simplify | |
1461 | (cmp (mult:c @0 @1) (mult:c @2 @1)) | |
1462 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
1463 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1464 | && tree_expr_nonzero_p (@1)) | |
1465 | (cmp @0 @2)))) | |
1466 | ||
20ce76cb | 1467 | /* For integral types with wrapping overflow and C odd fold |
1468 | x * C EQ/NE y * C into x EQ/NE y. */ | |
1469 | (for cmp (eq ne) | |
1470 | (simplify | |
1471 | (cmp (mult @0 INTEGER_CST@1) (mult @2 @1)) | |
1472 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
1473 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) | |
1474 | && (TREE_INT_CST_LOW (@1) & 1) != 0) | |
1475 | (cmp @0 @2)))) | |
1476 | ||
70229d3b | 1477 | /* For integral types with undefined overflow and C != 0 fold |
1478 | x * C RELOP y * C into: | |
4de74241 | 1479 | |
70229d3b | 1480 | x RELOP y for nonnegative C |
1481 | y RELOP x for negative C */ | |
1482 | (for cmp (lt gt le ge) | |
1483 | (simplify | |
1484 | (cmp (mult:c @0 @1) (mult:c @2 @1)) | |
1485 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
1486 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1487 | (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1)) | |
1488 | (cmp @0 @2) | |
1489 | (if (TREE_CODE (@1) == INTEGER_CST | |
e3d0f65c | 1490 | && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1)))) |
70229d3b | 1491 | (cmp @2 @0)))))) |
4de74241 | 1492 | |
205b32be | 1493 | /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */ |
1494 | (for cmp (le gt) | |
1495 | icmp (gt le) | |
1496 | (simplify | |
1497 | (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2) | |
1498 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1499 | && TYPE_UNSIGNED (TREE_TYPE (@0)) | |
1500 | && TYPE_PRECISION (TREE_TYPE (@0)) > 1 | |
e3d0f65c | 1501 | && (wi::to_wide (@2) |
1502 | == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1)) | |
205b32be | 1503 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } |
1504 | (icmp (convert:stype @0) { build_int_cst (stype, 0); }))))) | |
1505 | ||
c487041b | 1506 | /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */ |
1507 | (for cmp (simple_comparison) | |
1508 | (simplify | |
c434f6f1 | 1509 | (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2))) |
1510 | (if (element_precision (@3) >= element_precision (@0) | |
1511 | && types_match (@0, @1)) | |
952ba7dd | 1512 | (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))) |
c434f6f1 | 1513 | (if (!TYPE_UNSIGNED (TREE_TYPE (@3))) |
1514 | (cmp @1 @0) | |
1515 | (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1)) | |
1516 | (with | |
1517 | { | |
1518 | tree utype = unsigned_type_for (TREE_TYPE (@0)); | |
1519 | } | |
1520 | (cmp (convert:utype @1) (convert:utype @0))))) | |
1521 | (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2)))) | |
1522 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3))) | |
1523 | (cmp @0 @1) | |
1524 | (with | |
1525 | { | |
1526 | tree utype = unsigned_type_for (TREE_TYPE (@0)); | |
1527 | } | |
1528 | (cmp (convert:utype @0) (convert:utype @1))))))))) | |
c487041b | 1529 | |
14c7029b | 1530 | /* X / C1 op C2 into a simple range test. */ |
1531 | (for cmp (simple_comparison) | |
1532 | (simplify | |
1533 | (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2) | |
1534 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1535 | && integer_nonzerop (@1) | |
1536 | && !TREE_OVERFLOW (@1) | |
1537 | && !TREE_OVERFLOW (@2)) | |
1538 | (with { tree lo, hi; bool neg_overflow; | |
1539 | enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi, | |
1540 | &neg_overflow); } | |
1541 | (switch | |
1542 | (if (code == LT_EXPR || code == GE_EXPR) | |
1543 | (if (TREE_OVERFLOW (lo)) | |
1544 | { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); } | |
1545 | (if (code == LT_EXPR) | |
1546 | (lt @0 { lo; }) | |
1547 | (ge @0 { lo; })))) | |
1548 | (if (code == LE_EXPR || code == GT_EXPR) | |
1549 | (if (TREE_OVERFLOW (hi)) | |
1550 | { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); } | |
1551 | (if (code == LE_EXPR) | |
1552 | (le @0 { hi; }) | |
1553 | (gt @0 { hi; })))) | |
1554 | (if (!lo && !hi) | |
1555 | { build_int_cst (type, code == NE_EXPR); }) | |
1556 | (if (code == EQ_EXPR && !hi) | |
1557 | (ge @0 { lo; })) | |
1558 | (if (code == EQ_EXPR && !lo) | |
1559 | (le @0 { hi; })) | |
1560 | (if (code == NE_EXPR && !hi) | |
1561 | (lt @0 { lo; })) | |
1562 | (if (code == NE_EXPR && !lo) | |
1563 | (gt @0 { hi; })) | |
1564 | (if (GENERIC) | |
1565 | { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR, | |
1566 | lo, hi); }) | |
1567 | (with | |
1568 | { | |
1569 | tree etype = range_check_type (TREE_TYPE (@0)); | |
1570 | if (etype) | |
1571 | { | |
1572 | if (! TYPE_UNSIGNED (etype)) | |
1573 | etype = unsigned_type_for (etype); | |
1574 | hi = fold_convert (etype, hi); | |
1575 | lo = fold_convert (etype, lo); | |
1576 | hi = const_binop (MINUS_EXPR, etype, hi, lo); | |
1577 | } | |
1578 | } | |
1579 | (if (etype && hi && !TREE_OVERFLOW (hi)) | |
1580 | (if (code == EQ_EXPR) | |
1581 | (le (minus (convert:etype @0) { lo; }) { hi; }) | |
1582 | (gt (minus (convert:etype @0) { lo; }) { hi; }))))))))) | |
1583 | ||
9222792c | 1584 | /* X + Z < Y + Z is the same as X < Y when there is no overflow. */ |
1585 | (for op (lt le ge gt) | |
1586 | (simplify | |
1587 | (op (plus:c @0 @2) (plus:c @1 @2)) | |
1588 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1589 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1590 | (op @0 @1)))) | |
1591 | /* For equality and subtraction, this is also true with wrapping overflow. */ | |
1592 | (for op (eq ne minus) | |
1593 | (simplify | |
1594 | (op (plus:c @0 @2) (plus:c @1 @2)) | |
1595 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1596 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1597 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
1598 | (op @0 @1)))) | |
1599 | ||
1600 | /* X - Z < Y - Z is the same as X < Y when there is no overflow. */ | |
1601 | (for op (lt le ge gt) | |
1602 | (simplify | |
1603 | (op (minus @0 @2) (minus @1 @2)) | |
1604 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1605 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1606 | (op @0 @1)))) | |
1607 | /* For equality and subtraction, this is also true with wrapping overflow. */ | |
1608 | (for op (eq ne minus) | |
1609 | (simplify | |
1610 | (op (minus @0 @2) (minus @1 @2)) | |
1611 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1612 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1613 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
1614 | (op @0 @1)))) | |
57e83b58 | 1615 | /* And for pointers... */ |
1616 | (for op (simple_comparison) | |
1617 | (simplify | |
1618 | (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2)) | |
1619 | (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
1620 | (op @0 @1)))) | |
1621 | (simplify | |
1622 | (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2)) | |
1623 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)) | |
1624 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
1625 | (pointer_diff @0 @1))) | |
9222792c | 1626 | |
1627 | /* Z - X < Z - Y is the same as Y < X when there is no overflow. */ | |
1628 | (for op (lt le ge gt) | |
1629 | (simplify | |
1630 | (op (minus @2 @0) (minus @2 @1)) | |
1631 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1632 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1633 | (op @1 @0)))) | |
1634 | /* For equality and subtraction, this is also true with wrapping overflow. */ | |
1635 | (for op (eq ne minus) | |
1636 | (simplify | |
1637 | (op (minus @2 @0) (minus @2 @1)) | |
1638 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1639 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1640 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
1641 | (op @1 @0)))) | |
57e83b58 | 1642 | /* And for pointers... */ |
1643 | (for op (simple_comparison) | |
1644 | (simplify | |
1645 | (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1)) | |
1646 | (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
1647 | (op @1 @0)))) | |
1648 | (simplify | |
1649 | (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1)) | |
1650 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)) | |
1651 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
1652 | (pointer_diff @1 @0))) | |
9222792c | 1653 | |
3e39e829 | 1654 | /* X + Y < Y is the same as X < 0 when there is no overflow. */ |
1655 | (for op (lt le gt ge) | |
1656 | (simplify | |
1657 | (op:c (plus:c@2 @0 @1) @1) | |
1658 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1659 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
3ff1e878 | 1660 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) |
3e39e829 | 1661 | && (CONSTANT_CLASS_P (@0) || single_use (@2))) |
1662 | (op @0 { build_zero_cst (TREE_TYPE (@0)); })))) | |
1663 | /* For equality, this is also true with wrapping overflow. */ | |
1664 | (for op (eq ne) | |
1665 | (simplify | |
1666 | (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1)) | |
1667 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1668 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1669 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
1670 | && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3))) | |
1671 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2)) | |
1672 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))) | |
1673 | (op @0 { build_zero_cst (TREE_TYPE (@0)); }))) | |
1674 | (simplify | |
1675 | (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0)) | |
1676 | (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)) | |
1677 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
1678 | && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3)))) | |
1679 | (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) | |
1680 | ||
1681 | /* X - Y < X is the same as Y > 0 when there is no overflow. | |
1682 | For equality, this is also true with wrapping overflow. */ | |
1683 | (for op (simple_comparison) | |
1684 | (simplify | |
1685 | (op:c @0 (minus@2 @0 @1)) | |
1686 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1687 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
1688 | || ((op == EQ_EXPR || op == NE_EXPR) | |
1689 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
1690 | && (CONSTANT_CLASS_P (@1) || single_use (@2))) | |
1691 | (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) | |
1692 | ||
ed14c914 | 1693 | /* Transform: |
13585794 | 1694 | (X / Y) == 0 -> X < Y if X, Y are unsigned. |
1695 | (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */ | |
ed14c914 | 1696 | (for cmp (eq ne) |
1697 | ocmp (lt ge) | |
1698 | (simplify | |
1699 | (cmp (trunc_div @0 @1) integer_zerop) | |
1700 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) | |
13585794 | 1701 | /* Complex ==/!= is allowed, but not </>=. */ |
1702 | && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE | |
ed14c914 | 1703 | && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0)))) |
1704 | (ocmp @0 @1)))) | |
1705 | ||
7eb46067 | 1706 | /* X == C - X can never be true if C is odd. */ |
1707 | (for cmp (eq ne) | |
1708 | (simplify | |
1709 | (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0)))) | |
1710 | (if (TREE_INT_CST_LOW (@1) & 1) | |
1711 | { constant_boolean_node (cmp == NE_EXPR, type); }))) | |
1712 | ||
ef820217 | 1713 | /* Arguments on which one can call get_nonzero_bits to get the bits |
1714 | possibly set. */ | |
1715 | (match with_possible_nonzero_bits | |
1716 | INTEGER_CST@0) | |
1717 | (match with_possible_nonzero_bits | |
1718 | SSA_NAME@0 | |
1719 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))))) | |
1720 | /* Slightly extended version, do not make it recursive to keep it cheap. */ | |
1721 | (match (with_possible_nonzero_bits2 @0) | |
1722 | with_possible_nonzero_bits@0) | |
1723 | (match (with_possible_nonzero_bits2 @0) | |
1724 | (bit_and:c with_possible_nonzero_bits@0 @2)) | |
1725 | ||
1726 | /* Same for bits that are known to be set, but we do not have | |
1727 | an equivalent to get_nonzero_bits yet. */ | |
1728 | (match (with_certain_nonzero_bits2 @0) | |
1729 | INTEGER_CST@0) | |
1730 | (match (with_certain_nonzero_bits2 @0) | |
1731 | (bit_ior @1 INTEGER_CST@0)) | |
1732 | ||
1733 | /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */ | |
1734 | (for cmp (eq ne) | |
1735 | (simplify | |
1736 | (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1)) | |
e3d0f65c | 1737 | (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0) |
ef820217 | 1738 | { constant_boolean_node (cmp == NE_EXPR, type); }))) |
1739 | ||
4de74241 | 1740 | /* ((X inner_op C0) outer_op C1) |
1741 | With X being a tree where value_range has reasoned certain bits to always be | |
1742 | zero throughout its computed value range, | |
1743 | inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op | |
1744 | where zero_mask has 1's for all bits that are sure to be 0 in | |
1745 | and 0's otherwise. | |
1746 | if (inner_op == '^') C0 &= ~C1; | |
1747 | if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1) | |
1748 | if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1) | |
1749 | */ | |
1750 | (for inner_op (bit_ior bit_xor) | |
1751 | outer_op (bit_xor bit_ior) | |
1752 | (simplify | |
1753 | (outer_op | |
1754 | (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1) | |
1755 | (with | |
1756 | { | |
1757 | bool fail = false; | |
1758 | wide_int zero_mask_not; | |
1759 | wide_int C0; | |
1760 | wide_int cst_emit; | |
1761 | ||
1762 | if (TREE_CODE (@2) == SSA_NAME) | |
1763 | zero_mask_not = get_nonzero_bits (@2); | |
1764 | else | |
1765 | fail = true; | |
1766 | ||
1767 | if (inner_op == BIT_XOR_EXPR) | |
1768 | { | |
e3d0f65c | 1769 | C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1)); |
1770 | cst_emit = C0 | wi::to_wide (@1); | |
4de74241 | 1771 | } |
1772 | else | |
1773 | { | |
e3d0f65c | 1774 | C0 = wi::to_wide (@0); |
1775 | cst_emit = C0 ^ wi::to_wide (@1); | |
4de74241 | 1776 | } |
1777 | } | |
e3d0f65c | 1778 | (if (!fail && (C0 & zero_mask_not) == 0) |
4de74241 | 1779 | (outer_op @2 { wide_int_to_tree (type, cst_emit); }) |
e3d0f65c | 1780 | (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0) |
4de74241 | 1781 | (inner_op @2 { wide_int_to_tree (type, cst_emit); })))))) |
1782 | ||
770ae4bb | 1783 | /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */ |
1784 | (simplify | |
2bc3151f | 1785 | (pointer_plus (pointer_plus:s @0 @1) @3) |
1786 | (pointer_plus @0 (plus @1 @3))) | |
770ae4bb | 1787 | |
1788 | /* Pattern match | |
1789 | tem1 = (long) ptr1; | |
1790 | tem2 = (long) ptr2; | |
1791 | tem3 = tem2 - tem1; | |
1792 | tem4 = (unsigned long) tem3; | |
1793 | tem5 = ptr1 + tem4; | |
1794 | and produce | |
1795 | tem5 = ptr2; */ | |
1796 | (simplify | |
1797 | (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0)))) | |
1798 | /* Conditionally look through a sign-changing conversion. */ | |
1799 | (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3)) | |
1800 | && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1))) | |
1801 | || (GENERIC && type == TREE_TYPE (@1)))) | |
1802 | @1)) | |
57e83b58 | 1803 | (simplify |
1804 | (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0))) | |
1805 | (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3))) | |
1806 | (convert @1))) | |
770ae4bb | 1807 | |
1808 | /* Pattern match | |
1809 | tem = (sizetype) ptr; | |
1810 | tem = tem & algn; | |
1811 | tem = -tem; | |
1812 | ... = ptr p+ tem; | |
1813 | and produce the simpler and easier to analyze with respect to alignment | |
1814 | ... = ptr & ~algn; */ | |
1815 | (simplify | |
1816 | (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1))) | |
e3d0f65c | 1817 | (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); } |
770ae4bb | 1818 | (bit_and @0 { algn; }))) |
1819 | ||
6b440285 | 1820 | /* Try folding difference of addresses. */ |
1821 | (simplify | |
1822 | (minus (convert ADDR_EXPR@0) (convert @1)) | |
1823 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
81bc0f0f | 1824 | (with { poly_int64 diff; } |
6b440285 | 1825 | (if (ptr_difference_const (@0, @1, &diff)) |
1826 | { build_int_cst_type (type, diff); })))) | |
1827 | (simplify | |
1828 | (minus (convert @0) (convert ADDR_EXPR@1)) | |
1829 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
81bc0f0f | 1830 | (with { poly_int64 diff; } |
6b440285 | 1831 | (if (ptr_difference_const (@0, @1, &diff)) |
1832 | { build_int_cst_type (type, diff); })))) | |
57e83b58 | 1833 | (simplify |
622c830b | 1834 | (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1)) |
57e83b58 | 1835 | (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0)) |
1836 | && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1))) | |
81bc0f0f | 1837 | (with { poly_int64 diff; } |
57e83b58 | 1838 | (if (ptr_difference_const (@0, @1, &diff)) |
1839 | { build_int_cst_type (type, diff); })))) | |
1840 | (simplify | |
622c830b | 1841 | (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1)) |
57e83b58 | 1842 | (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0)) |
1843 | && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1))) | |
81bc0f0f | 1844 | (with { poly_int64 diff; } |
57e83b58 | 1845 | (if (ptr_difference_const (@0, @1, &diff)) |
1846 | { build_int_cst_type (type, diff); })))) | |
6b440285 | 1847 | |
a2f9b7c9 | 1848 | /* If arg0 is derived from the address of an object or function, we may |
1849 | be able to fold this expression using the object or function's | |
1850 | alignment. */ | |
1851 | (simplify | |
1852 | (bit_and (convert? @0) INTEGER_CST@1) | |
1853 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
1854 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
1855 | (with | |
1856 | { | |
1857 | unsigned int align; | |
1858 | unsigned HOST_WIDE_INT bitpos; | |
1859 | get_pointer_alignment_1 (@0, &align, &bitpos); | |
1860 | } | |
e3d0f65c | 1861 | (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT)) |
1862 | { wide_int_to_tree (type, (wi::to_wide (@1) | |
1863 | & (bitpos / BITS_PER_UNIT))); })))) | |
6b440285 | 1864 | |
770ae4bb | 1865 | |
662274ee | 1866 | /* We can't reassociate at all for saturating types. */ |
1867 | (if (!TYPE_SATURATING (type)) | |
1868 | ||
1869 | /* Contract negates. */ | |
1870 | /* A + (-B) -> A - B */ | |
1871 | (simplify | |
2bbfc137 | 1872 | (plus:c @0 (convert? (negate @1))) |
1873 | /* Apply STRIP_NOPS on the negate. */ | |
1874 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
a8875ee0 | 1875 | && !TYPE_OVERFLOW_SANITIZED (type)) |
2bbfc137 | 1876 | (with |
1877 | { | |
1878 | tree t1 = type; | |
1879 | if (INTEGRAL_TYPE_P (type) | |
1880 | && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
1881 | t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); | |
1882 | } | |
1883 | (convert (minus (convert:t1 @0) (convert:t1 @1)))))) | |
662274ee | 1884 | /* A - (-B) -> A + B */ |
1885 | (simplify | |
2bbfc137 | 1886 | (minus @0 (convert? (negate @1))) |
1887 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
a8875ee0 | 1888 | && !TYPE_OVERFLOW_SANITIZED (type)) |
2bbfc137 | 1889 | (with |
1890 | { | |
1891 | tree t1 = type; | |
1892 | if (INTEGRAL_TYPE_P (type) | |
1893 | && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
1894 | t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); | |
1895 | } | |
1896 | (convert (plus (convert:t1 @0) (convert:t1 @1)))))) | |
48eb107e | 1897 | /* -(T)(-A) -> (T)A |
1898 | Sign-extension is ok except for INT_MIN, which thankfully cannot | |
1899 | happen without overflow. */ | |
1900 | (simplify | |
1901 | (negate (convert (negate @1))) | |
1902 | (if (INTEGRAL_TYPE_P (type) | |
1903 | && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1)) | |
1904 | || (!TYPE_UNSIGNED (TREE_TYPE (@1)) | |
1905 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
1906 | && !TYPE_OVERFLOW_SANITIZED (type) | |
1907 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) | |
c229fef3 | 1908 | (convert @1))) |
48eb107e | 1909 | (simplify |
1910 | (negate (convert negate_expr_p@1)) | |
1911 | (if (SCALAR_FLOAT_TYPE_P (type) | |
1912 | && ((DECIMAL_FLOAT_TYPE_P (type) | |
1913 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)) | |
1914 | && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1))) | |
1915 | || !HONOR_SIGN_DEPENDENT_ROUNDING (type))) | |
1916 | (convert (negate @1)))) | |
1917 | (simplify | |
1918 | (negate (nop_convert (negate @1))) | |
1919 | (if (!TYPE_OVERFLOW_SANITIZED (type) | |
1920 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) | |
1921 | (view_convert @1))) | |
662274ee | 1922 | |
f215226b | 1923 | /* We can't reassociate floating-point unless -fassociative-math |
1924 | or fixed-point plus or minus because of saturation to +-Inf. */ | |
1925 | (if ((!FLOAT_TYPE_P (type) || flag_associative_math) | |
1926 | && !FIXED_POINT_TYPE_P (type)) | |
662274ee | 1927 | |
1928 | /* Match patterns that allow contracting a plus-minus pair | |
1929 | irrespective of overflow issues. */ | |
1930 | /* (A +- B) - A -> +- B */ | |
1931 | /* (A +- B) -+ B -> A */ | |
1932 | /* A - (A +- B) -> -+ B */ | |
1933 | /* A +- (B -+ A) -> +- B */ | |
1934 | (simplify | |
1935 | (minus (plus:c @0 @1) @0) | |
1936 | @1) | |
1937 | (simplify | |
1938 | (minus (minus @0 @1) @0) | |
1939 | (negate @1)) | |
1940 | (simplify | |
1941 | (plus:c (minus @0 @1) @1) | |
1942 | @0) | |
1943 | (simplify | |
1944 | (minus @0 (plus:c @0 @1)) | |
1945 | (negate @1)) | |
1946 | (simplify | |
1947 | (minus @0 (minus @0 @1)) | |
1948 | @1) | |
bf1cce2e | 1949 | /* (A +- B) + (C - A) -> C +- B */ |
1950 | /* (A + B) - (A - C) -> B + C */ | |
1951 | /* More cases are handled with comparisons. */ | |
1952 | (simplify | |
1953 | (plus:c (plus:c @0 @1) (minus @2 @0)) | |
1954 | (plus @2 @1)) | |
1955 | (simplify | |
1956 | (plus:c (minus @0 @1) (minus @2 @0)) | |
1957 | (minus @2 @1)) | |
57e83b58 | 1958 | (simplify |
1959 | (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0)) | |
1960 | (if (TYPE_OVERFLOW_UNDEFINED (type) | |
1961 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))) | |
1962 | (pointer_diff @2 @1))) | |
bf1cce2e | 1963 | (simplify |
1964 | (minus (plus:c @0 @1) (minus @0 @2)) | |
1965 | (plus @1 @2)) | |
662274ee | 1966 | |
049318f9 | 1967 | /* (A +- CST1) +- CST2 -> A + CST3 |
1968 | Use view_convert because it is safe for vectors and equivalent for | |
1969 | scalars. */ | |
662274ee | 1970 | (for outer_op (plus minus) |
1971 | (for inner_op (plus minus) | |
049318f9 | 1972 | neg_inner_op (minus plus) |
662274ee | 1973 | (simplify |
049318f9 | 1974 | (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1)) |
1975 | CONSTANT_CLASS_P@2) | |
1976 | /* If one of the types wraps, use that one. */ | |
1977 | (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) | |
50e96fc3 | 1978 | /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse |
1979 | forever if something doesn't simplify into a constant. */ | |
1980 | (if (!CONSTANT_CLASS_P (@0)) | |
1981 | (if (outer_op == PLUS_EXPR) | |
1982 | (plus (view_convert @0) (inner_op @2 (view_convert @1))) | |
1983 | (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))) | |
049318f9 | 1984 | (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
1985 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
1986 | (if (outer_op == PLUS_EXPR) | |
1987 | (view_convert (plus @0 (inner_op (view_convert @2) @1))) | |
1988 | (view_convert (minus @0 (neg_inner_op (view_convert @2) @1)))) | |
1989 | /* If the constant operation overflows we cannot do the transform | |
1990 | directly as we would introduce undefined overflow, for example | |
1991 | with (a - 1) + INT_MIN. */ | |
1992 | (if (types_match (type, @0)) | |
1993 | (with { tree cst = const_binop (outer_op == inner_op | |
1994 | ? PLUS_EXPR : MINUS_EXPR, | |
1995 | type, @1, @2); } | |
1996 | (if (cst && !TREE_OVERFLOW (cst)) | |
1997 | (inner_op @0 { cst; } ) | |
1998 | /* X+INT_MAX+1 is X-INT_MIN. */ | |
1999 | (if (INTEGRAL_TYPE_P (type) && cst | |
e3d0f65c | 2000 | && wi::to_wide (cst) == wi::min_value (type)) |
2001 | (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); }) | |
049318f9 | 2002 | /* Last resort, use some unsigned type. */ |
2003 | (with { tree utype = unsigned_type_for (type); } | |
ec676679 | 2004 | (if (utype) |
2005 | (view_convert (inner_op | |
2006 | (view_convert:utype @0) | |
2007 | (view_convert:utype | |
2008 | { drop_tree_overflow (cst); })))))))))))))) | |
662274ee | 2009 | |
e5c2af08 | 2010 | /* (CST1 - A) +- CST2 -> CST3 - A */ |
662274ee | 2011 | (for outer_op (plus minus) |
2012 | (simplify | |
2013 | (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2) | |
0808dfce | 2014 | (with { tree cst = const_binop (outer_op, type, @1, @2); } |
662274ee | 2015 | (if (cst && !TREE_OVERFLOW (cst)) |
2016 | (minus { cst; } @0))))) | |
2017 | ||
e5c2af08 | 2018 | /* CST1 - (CST2 - A) -> CST3 + A */ |
2019 | (simplify | |
2020 | (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0)) | |
2021 | (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } | |
2022 | (if (cst && !TREE_OVERFLOW (cst)) | |
2023 | (plus { cst; } @0)))) | |
2024 | ||
662274ee | 2025 | /* ~A + A -> -1 */ |
2026 | (simplify | |
2027 | (plus:c (bit_not @0) @0) | |
2028 | (if (!TYPE_OVERFLOW_TRAPS (type)) | |
2029 | { build_all_ones_cst (type); })) | |
2030 | ||
2031 | /* ~A + 1 -> -A */ | |
2032 | (simplify | |
c0a7a7df | 2033 | (plus (convert? (bit_not @0)) integer_each_onep) |
2034 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
2035 | (negate (convert @0)))) | |
2036 | ||
2037 | /* -A - 1 -> ~A */ | |
2038 | (simplify | |
2039 | (minus (convert? (negate @0)) integer_each_onep) | |
2040 | (if (!TYPE_OVERFLOW_TRAPS (type) | |
2041 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
2042 | (bit_not (convert @0)))) | |
2043 | ||
2044 | /* -1 - A -> ~A */ | |
2045 | (simplify | |
2046 | (minus integer_all_onesp @0) | |
d792dcdf | 2047 | (bit_not @0)) |
662274ee | 2048 | |
2049 | /* (T)(P + A) - (T)P -> (T) A */ | |
bdcc09da | 2050 | (simplify |
f3e5187b | 2051 | (minus (convert (plus:c @@0 @1)) |
2052 | (convert? @0)) | |
bdcc09da | 2053 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
2054 | /* For integer types, if A has a smaller type | |
2055 | than T the result depends on the possible | |
2056 | overflow in P + A. | |
2057 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
2058 | However, if an overflow in P + A would cause | |
2059 | undefined behavior, we can assume that there | |
2060 | is no overflow. */ | |
f3e5187b | 2061 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
2062 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
bdcc09da | 2063 | (convert @1))) |
2064 | (simplify | |
2065 | (minus (convert (pointer_plus @@0 @1)) | |
2066 | (convert @0)) | |
2067 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
2068 | /* For pointer types, if the conversion of A to the | |
2069 | final type requires a sign- or zero-extension, | |
2070 | then we have to punt - it is not defined which | |
2071 | one is correct. */ | |
2072 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
2073 | && TREE_CODE (@1) == INTEGER_CST | |
2074 | && tree_int_cst_sign_bit (@1) == 0)) | |
2075 | (convert @1))) | |
57e83b58 | 2076 | (simplify |
2077 | (pointer_diff (pointer_plus @@0 @1) @0) | |
2078 | /* The second argument of pointer_plus must be interpreted as signed, and | |
2079 | thus sign-extended if necessary. */ | |
2080 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
992a51f2 | 2081 | /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR |
2082 | second arg is unsigned even when we need to consider it as signed, | |
2083 | we don't want to diagnose overflow here. */ | |
2084 | (convert (view_convert:stype @1)))) | |
844cece0 | 2085 | |
2086 | /* (T)P - (T)(P + A) -> -(T) A */ | |
bdcc09da | 2087 | (simplify |
f3e5187b | 2088 | (minus (convert? @0) |
2089 | (convert (plus:c @@0 @1))) | |
bdcc09da | 2090 | (if (INTEGRAL_TYPE_P (type) |
2091 | && TYPE_OVERFLOW_UNDEFINED (type) | |
2092 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
2093 | (with { tree utype = unsigned_type_for (type); } | |
2094 | (convert (negate (convert:utype @1)))) | |
844cece0 | 2095 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
2096 | /* For integer types, if A has a smaller type | |
2097 | than T the result depends on the possible | |
2098 | overflow in P + A. | |
2099 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
2100 | However, if an overflow in P + A would cause | |
2101 | undefined behavior, we can assume that there | |
2102 | is no overflow. */ | |
f3e5187b | 2103 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
2104 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
bdcc09da | 2105 | (negate (convert @1))))) |
2106 | (simplify | |
2107 | (minus (convert @0) | |
2108 | (convert (pointer_plus @@0 @1))) | |
2109 | (if (INTEGRAL_TYPE_P (type) | |
2110 | && TYPE_OVERFLOW_UNDEFINED (type) | |
2111 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
2112 | (with { tree utype = unsigned_type_for (type); } | |
2113 | (convert (negate (convert:utype @1)))) | |
2114 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
844cece0 | 2115 | /* For pointer types, if the conversion of A to the |
2116 | final type requires a sign- or zero-extension, | |
2117 | then we have to punt - it is not defined which | |
2118 | one is correct. */ | |
2119 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
2120 | && TREE_CODE (@1) == INTEGER_CST | |
2121 | && tree_int_cst_sign_bit (@1) == 0)) | |
2122 | (negate (convert @1))))) | |
57e83b58 | 2123 | (simplify |
2124 | (pointer_diff @0 (pointer_plus @@0 @1)) | |
2125 | /* The second argument of pointer_plus must be interpreted as signed, and | |
2126 | thus sign-extended if necessary. */ | |
2127 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
992a51f2 | 2128 | /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR |
2129 | second arg is unsigned even when we need to consider it as signed, | |
2130 | we don't want to diagnose overflow here. */ | |
2131 | (negate (convert (view_convert:stype @1))))) | |
844cece0 | 2132 | |
2133 | /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */ | |
bdcc09da | 2134 | (simplify |
f3e5187b | 2135 | (minus (convert (plus:c @@0 @1)) |
bdcc09da | 2136 | (convert (plus:c @0 @2))) |
2137 | (if (INTEGRAL_TYPE_P (type) | |
2138 | && TYPE_OVERFLOW_UNDEFINED (type) | |
f3e5187b | 2139 | && element_precision (type) <= element_precision (TREE_TYPE (@1)) |
2140 | && element_precision (type) <= element_precision (TREE_TYPE (@2))) | |
bdcc09da | 2141 | (with { tree utype = unsigned_type_for (type); } |
2142 | (convert (minus (convert:utype @1) (convert:utype @2)))) | |
f3e5187b | 2143 | (if (((element_precision (type) <= element_precision (TREE_TYPE (@1))) |
2144 | == (element_precision (type) <= element_precision (TREE_TYPE (@2)))) | |
2145 | && (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
2146 | /* For integer types, if A has a smaller type | |
2147 | than T the result depends on the possible | |
2148 | overflow in P + A. | |
2149 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
2150 | However, if an overflow in P + A would cause | |
2151 | undefined behavior, we can assume that there | |
2152 | is no overflow. */ | |
2153 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
2154 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
2155 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)) | |
2156 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2))))) | |
bdcc09da | 2157 | (minus (convert @1) (convert @2))))) |
2158 | (simplify | |
2159 | (minus (convert (pointer_plus @@0 @1)) | |
2160 | (convert (pointer_plus @0 @2))) | |
2161 | (if (INTEGRAL_TYPE_P (type) | |
2162 | && TYPE_OVERFLOW_UNDEFINED (type) | |
2163 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
2164 | (with { tree utype = unsigned_type_for (type); } | |
2165 | (convert (minus (convert:utype @1) (convert:utype @2)))) | |
2166 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
844cece0 | 2167 | /* For pointer types, if the conversion of A to the |
2168 | final type requires a sign- or zero-extension, | |
2169 | then we have to punt - it is not defined which | |
2170 | one is correct. */ | |
2171 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
2172 | && TREE_CODE (@1) == INTEGER_CST | |
2173 | && tree_int_cst_sign_bit (@1) == 0 | |
2174 | && TREE_CODE (@2) == INTEGER_CST | |
2175 | && tree_int_cst_sign_bit (@2) == 0)) | |
bdcc09da | 2176 | (minus (convert @1) (convert @2))))) |
57e83b58 | 2177 | (simplify |
2178 | (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2)) | |
2179 | /* The second argument of pointer_plus must be interpreted as signed, and | |
2180 | thus sign-extended if necessary. */ | |
2181 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
992a51f2 | 2182 | /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR |
2183 | second arg is unsigned even when we need to consider it as signed, | |
2184 | we don't want to diagnose overflow here. */ | |
2185 | (minus (convert (view_convert:stype @1)) | |
2186 | (convert (view_convert:stype @2))))))) | |
662274ee | 2187 | |
6227228e | 2188 | /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1). |
2189 | Modeled after fold_plusminus_mult_expr. */ | |
2190 | (if (!TYPE_SATURATING (type) | |
2191 | && (!FLOAT_TYPE_P (type) || flag_associative_math)) | |
2192 | (for plusminus (plus minus) | |
2193 | (simplify | |
60bd955f | 2194 | (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2)) |
2195 | (if ((!ANY_INTEGRAL_TYPE_P (type) | |
6227228e | 2196 | || TYPE_OVERFLOW_WRAPS (type) |
2197 | || (INTEGRAL_TYPE_P (type) | |
2198 | && tree_expr_nonzero_p (@0) | |
2199 | && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type))))) | |
60bd955f | 2200 | /* If @1 +- @2 is constant require a hard single-use on either |
2201 | original operand (but not on both). */ | |
2202 | && (single_use (@3) || single_use (@4))) | |
2203 | (mult (plusminus @1 @2) @0))) | |
2204 | /* We cannot generate constant 1 for fract. */ | |
2205 | (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))) | |
2206 | (simplify | |
2207 | (plusminus @0 (mult:c@3 @0 @2)) | |
2208 | (if ((!ANY_INTEGRAL_TYPE_P (type) | |
2209 | || TYPE_OVERFLOW_WRAPS (type) | |
2210 | || (INTEGRAL_TYPE_P (type) | |
2211 | && tree_expr_nonzero_p (@0) | |
2212 | && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type))))) | |
2213 | && single_use (@3)) | |
6227228e | 2214 | (mult (plusminus { build_one_cst (type); } @2) @0))) |
2215 | (simplify | |
60bd955f | 2216 | (plusminus (mult:c@3 @0 @2) @0) |
2217 | (if ((!ANY_INTEGRAL_TYPE_P (type) | |
2218 | || TYPE_OVERFLOW_WRAPS (type) | |
2219 | || (INTEGRAL_TYPE_P (type) | |
2220 | && tree_expr_nonzero_p (@0) | |
2221 | && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type))))) | |
2222 | && single_use (@3)) | |
6227228e | 2223 | (mult (plusminus @2 { build_one_cst (type); }) @0)))))) |
662274ee | 2224 | |
129004d7 | 2225 | /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */ |
6a78ea5d | 2226 | |
054e9558 | 2227 | (for minmax (min max FMIN_ALL FMAX_ALL) |
6a78ea5d | 2228 | (simplify |
2229 | (minmax @0 @0) | |
2230 | @0)) | |
b2edae6c | 2231 | /* min(max(x,y),y) -> y. */ |
2232 | (simplify | |
2233 | (min:c (max:c @0 @1) @1) | |
2234 | @1) | |
2235 | /* max(min(x,y),y) -> y. */ | |
2236 | (simplify | |
2237 | (max:c (min:c @0 @1) @1) | |
2238 | @1) | |
b0ab3ca0 | 2239 | /* max(a,-a) -> abs(a). */ |
2240 | (simplify | |
2241 | (max:c @0 (negate @0)) | |
2242 | (if (TREE_CODE (type) != COMPLEX_TYPE | |
2243 | && (! ANY_INTEGRAL_TYPE_P (type) | |
2244 | || TYPE_OVERFLOW_UNDEFINED (type))) | |
2245 | (abs @0))) | |
d82d27a3 | 2246 | /* min(a,-a) -> -abs(a). */ |
2247 | (simplify | |
2248 | (min:c @0 (negate @0)) | |
2249 | (if (TREE_CODE (type) != COMPLEX_TYPE | |
2250 | && (! ANY_INTEGRAL_TYPE_P (type) | |
2251 | || TYPE_OVERFLOW_UNDEFINED (type))) | |
2252 | (negate (abs @0)))) | |
6a78ea5d | 2253 | (simplify |
2254 | (min @0 @1) | |
523f3a9d | 2255 | (switch |
2256 | (if (INTEGRAL_TYPE_P (type) | |
2257 | && TYPE_MIN_VALUE (type) | |
2258 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
2259 | @1) | |
2260 | (if (INTEGRAL_TYPE_P (type) | |
2261 | && TYPE_MAX_VALUE (type) | |
2262 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
2263 | @0))) | |
6a78ea5d | 2264 | (simplify |
2265 | (max @0 @1) | |
523f3a9d | 2266 | (switch |
2267 | (if (INTEGRAL_TYPE_P (type) | |
2268 | && TYPE_MAX_VALUE (type) | |
2269 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
2270 | @1) | |
2271 | (if (INTEGRAL_TYPE_P (type) | |
2272 | && TYPE_MIN_VALUE (type) | |
2273 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
2274 | @0))) | |
15eeb126 | 2275 | |
5f645ad1 | 2276 | /* max (a, a + CST) -> a + CST where CST is positive. */ |
2277 | /* max (a, a + CST) -> a where CST is negative. */ | |
2278 | (simplify | |
2279 | (max:c @0 (plus@2 @0 INTEGER_CST@1)) | |
2280 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
2281 | (if (tree_int_cst_sgn (@1) > 0) | |
2282 | @2 | |
2283 | @0))) | |
2284 | ||
2285 | /* min (a, a + CST) -> a where CST is positive. */ | |
2286 | /* min (a, a + CST) -> a + CST where CST is negative. */ | |
2287 | (simplify | |
2288 | (min:c @0 (plus@2 @0 INTEGER_CST@1)) | |
2289 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
2290 | (if (tree_int_cst_sgn (@1) > 0) | |
2291 | @0 | |
2292 | @2))) | |
2293 | ||
15eeb126 | 2294 | /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted |
2295 | and the outer convert demotes the expression back to x's type. */ | |
2296 | (for minmax (min max) | |
2297 | (simplify | |
2298 | (convert (minmax@0 (convert @1) INTEGER_CST@2)) | |
a671e245 | 2299 | (if (INTEGRAL_TYPE_P (type) |
2300 | && types_match (@1, type) && int_fits_type_p (@2, type) | |
15eeb126 | 2301 | && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type) |
2302 | && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)) | |
2303 | (minmax @1 (convert @2))))) | |
2304 | ||
054e9558 | 2305 | (for minmax (FMIN_ALL FMAX_ALL) |
129004d7 | 2306 | /* If either argument is NaN, return the other one. Avoid the |
2307 | transformation if we get (and honor) a signalling NaN. */ | |
2308 | (simplify | |
2309 | (minmax:c @0 REAL_CST@1) | |
2310 | (if (real_isnan (TREE_REAL_CST_PTR (@1)) | |
2311 | && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)) | |
2312 | @0))) | |
2313 | /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these | |
2314 | functions to return the numeric arg if the other one is NaN. | |
2315 | MIN and MAX don't honor that, so only transform if -ffinite-math-only | |
2316 | is set. C99 doesn't require -0.0 to be handled, so we don't have to | |
2317 | worry about it either. */ | |
2318 | (if (flag_finite_math_only) | |
2319 | (simplify | |
054e9558 | 2320 | (FMIN_ALL @0 @1) |
129004d7 | 2321 | (min @0 @1)) |
7d3df64a | 2322 | (simplify |
054e9558 | 2323 | (FMAX_ALL @0 @1) |
129004d7 | 2324 | (max @0 @1))) |
f3ec0970 | 2325 | /* min (-A, -B) -> -max (A, B) */ |
054e9558 | 2326 | (for minmax (min max FMIN_ALL FMAX_ALL) |
2327 | maxmin (max min FMAX_ALL FMIN_ALL) | |
f3ec0970 | 2328 | (simplify |
2329 | (minmax (negate:s@2 @0) (negate:s@3 @1)) | |
2330 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
2331 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2332 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
2333 | (negate (maxmin @0 @1))))) | |
2334 | /* MIN (~X, ~Y) -> ~MAX (X, Y) | |
2335 | MAX (~X, ~Y) -> ~MIN (X, Y) */ | |
2336 | (for minmax (min max) | |
2337 | maxmin (max min) | |
2338 | (simplify | |
2339 | (minmax (bit_not:s@2 @0) (bit_not:s@3 @1)) | |
2340 | (bit_not (maxmin @0 @1)))) | |
6a78ea5d | 2341 | |
81787808 | 2342 | /* MIN (X, Y) == X -> X <= Y */ |
2343 | (for minmax (min min max max) | |
2344 | cmp (eq ne eq ne ) | |
2345 | out (le gt ge lt ) | |
2346 | (simplify | |
2347 | (cmp:c (minmax:c @0 @1) @0) | |
2348 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
2349 | (out @0 @1)))) | |
2350 | /* MIN (X, 5) == 0 -> X == 0 | |
2351 | MIN (X, 5) == 7 -> false */ | |
2352 | (for cmp (eq ne) | |
2353 | (simplify | |
2354 | (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2) | |
e3d0f65c | 2355 | (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2), |
2356 | TYPE_SIGN (TREE_TYPE (@0)))) | |
81787808 | 2357 | { constant_boolean_node (cmp == NE_EXPR, type); } |
e3d0f65c | 2358 | (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2), |
2359 | TYPE_SIGN (TREE_TYPE (@0)))) | |
81787808 | 2360 | (cmp @0 @2))))) |
2361 | (for cmp (eq ne) | |
2362 | (simplify | |
2363 | (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2) | |
e3d0f65c | 2364 | (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2), |
2365 | TYPE_SIGN (TREE_TYPE (@0)))) | |
81787808 | 2366 | { constant_boolean_node (cmp == NE_EXPR, type); } |
e3d0f65c | 2367 | (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2), |
2368 | TYPE_SIGN (TREE_TYPE (@0)))) | |
81787808 | 2369 | (cmp @0 @2))))) |
2370 | /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */ | |
2371 | (for minmax (min min max max min min max max ) | |
2372 | cmp (lt le gt ge gt ge lt le ) | |
2373 | comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and) | |
2374 | (simplify | |
2375 | (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2) | |
2376 | (comb (cmp @0 @2) (cmp @1 @2)))) | |
2377 | ||
6a78ea5d | 2378 | /* Simplifications of shift and rotates. */ |
2379 | ||
2380 | (for rotate (lrotate rrotate) | |
2381 | (simplify | |
2382 | (rotate integer_all_onesp@0 @1) | |
2383 | @0)) | |
2384 | ||
2385 | /* Optimize -1 >> x for arithmetic right shifts. */ | |
2386 | (simplify | |
2387 | (rshift integer_all_onesp@0 @1) | |
2388 | (if (!TYPE_UNSIGNED (type) | |
2389 | && tree_expr_nonnegative_p (@1)) | |
2390 | @0)) | |
2391 | ||
f0e28f23 | 2392 | /* Optimize (x >> c) << c into x & (-1<<c). */ |
2393 | (simplify | |
2394 | (lshift (rshift @0 INTEGER_CST@1) @1) | |
e3d0f65c | 2395 | (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))) |
f0e28f23 | 2396 | (bit_and @0 (lshift { build_minus_one_cst (type); } @1)))) |
2397 | ||
2398 | /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned | |
2399 | types. */ | |
2400 | (simplify | |
2401 | (rshift (lshift @0 INTEGER_CST@1) @1) | |
2402 | (if (TYPE_UNSIGNED (type) | |
e3d0f65c | 2403 | && (wi::ltu_p (wi::to_wide (@1), element_precision (type)))) |
f0e28f23 | 2404 | (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) |
2405 | ||
6a78ea5d | 2406 | (for shiftrotate (lrotate rrotate lshift rshift) |
2407 | (simplify | |
2408 | (shiftrotate @0 integer_zerop) | |
2409 | (non_lvalue @0)) | |
2410 | (simplify | |
2411 | (shiftrotate integer_zerop@0 @1) | |
2412 | @0) | |
2413 | /* Prefer vector1 << scalar to vector1 << vector2 | |
2414 | if vector2 is uniform. */ | |
2415 | (for vec (VECTOR_CST CONSTRUCTOR) | |
2416 | (simplify | |
2417 | (shiftrotate @0 vec@1) | |
2418 | (with { tree tem = uniform_vector_p (@1); } | |
2419 | (if (tem) | |
2420 | (shiftrotate @0 { tem; })))))) | |
2421 | ||
aff1170a | 2422 | /* Simplify X << Y where Y's low width bits are 0 to X, as only valid |
2423 | Y is 0. Similarly for X >> Y. */ | |
2424 | #if GIMPLE | |
2425 | (for shift (lshift rshift) | |
2426 | (simplify | |
2427 | (shift @0 SSA_NAME@1) | |
2428 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
2429 | (with { | |
2430 | int width = ceil_log2 (element_precision (TREE_TYPE (@0))); | |
2431 | int prec = TYPE_PRECISION (TREE_TYPE (@1)); | |
2432 | } | |
2433 | (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0) | |
2434 | @0))))) | |
2435 | #endif | |
2436 | ||
6a78ea5d | 2437 | /* Rewrite an LROTATE_EXPR by a constant into an |
2438 | RROTATE_EXPR by a new constant. */ | |
2439 | (simplify | |
2440 | (lrotate @0 INTEGER_CST@1) | |
0808dfce | 2441 | (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1), |
6a78ea5d | 2442 | build_int_cst (TREE_TYPE (@1), |
2443 | element_precision (type)), @1); })) | |
2444 | ||
a76fc0fd | 2445 | /* Turn (a OP c1) OP c2 into a OP (c1+c2). */ |
2446 | (for op (lrotate rrotate rshift lshift) | |
2447 | (simplify | |
2448 | (op (op @0 INTEGER_CST@1) INTEGER_CST@2) | |
2449 | (with { unsigned int prec = element_precision (type); } | |
e3d0f65c | 2450 | (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))) |
2451 | && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1))) | |
2452 | && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))) | |
2453 | && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2)))) | |
cb7bca5f | 2454 | (with { unsigned int low = (tree_to_uhwi (@1) |
2455 | + tree_to_uhwi (@2)); } | |
a76fc0fd | 2456 | /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2 |
2457 | being well defined. */ | |
2458 | (if (low >= prec) | |
2459 | (if (op == LROTATE_EXPR || op == RROTATE_EXPR) | |
d74b7335 | 2460 | (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); }) |
05999bfc | 2461 | (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR) |
d74b7335 | 2462 | { build_zero_cst (type); } |
2463 | (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); }))) | |
2464 | (op @0 { build_int_cst (TREE_TYPE (@1), low); }))))))) | |
a76fc0fd | 2465 | |
2466 | ||
2e4fb82e | 2467 | /* ((1 << A) & 1) != 0 -> A == 0 |
2468 | ((1 << A) & 1) == 0 -> A != 0 */ | |
2469 | (for cmp (ne eq) | |
2470 | icmp (eq ne) | |
2471 | (simplify | |
2472 | (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop) | |
2473 | (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))) | |
662274ee | 2474 | |
9d5c80c1 | 2475 | /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) |
2476 | (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) | |
2477 | if CST2 != 0. */ | |
2478 | (for cmp (ne eq) | |
2479 | (simplify | |
2480 | (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2) | |
e3d0f65c | 2481 | (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); } |
9d5c80c1 | 2482 | (if (cand < 0 |
2483 | || (!integer_zerop (@2) | |
e3d0f65c | 2484 | && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2))) |
d74b7335 | 2485 | { constant_boolean_node (cmp == NE_EXPR, type); } |
2486 | (if (!integer_zerop (@2) | |
e3d0f65c | 2487 | && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2)) |
d74b7335 | 2488 | (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); })))))) |
9d5c80c1 | 2489 | |
4c8f3f72 | 2490 | /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) |
2491 | (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) | |
2492 | if the new mask might be further optimized. */ | |
2493 | (for shift (lshift rshift) | |
2494 | (simplify | |
2bc3151f | 2495 | (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1)) |
2496 | INTEGER_CST@2) | |
4c8f3f72 | 2497 | (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5)) |
2498 | && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT | |
2499 | && tree_fits_uhwi_p (@1) | |
2500 | && tree_to_uhwi (@1) > 0 | |
2501 | && tree_to_uhwi (@1) < TYPE_PRECISION (type)) | |
2502 | (with | |
2503 | { | |
2504 | unsigned int shiftc = tree_to_uhwi (@1); | |
2505 | unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2); | |
2506 | unsigned HOST_WIDE_INT newmask, zerobits = 0; | |
2507 | tree shift_type = TREE_TYPE (@3); | |
2508 | unsigned int prec; | |
2509 | ||
2510 | if (shift == LSHIFT_EXPR) | |
edc19fd0 | 2511 | zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1); |
4c8f3f72 | 2512 | else if (shift == RSHIFT_EXPR |
654ba22c | 2513 | && type_has_mode_precision_p (shift_type)) |
4c8f3f72 | 2514 | { |
2515 | prec = TYPE_PRECISION (TREE_TYPE (@3)); | |
2516 | tree arg00 = @0; | |
2517 | /* See if more bits can be proven as zero because of | |
2518 | zero extension. */ | |
2519 | if (@3 != @0 | |
2520 | && TYPE_UNSIGNED (TREE_TYPE (@0))) | |
2521 | { | |
2522 | tree inner_type = TREE_TYPE (@0); | |
654ba22c | 2523 | if (type_has_mode_precision_p (inner_type) |
4c8f3f72 | 2524 | && TYPE_PRECISION (inner_type) < prec) |
2525 | { | |
2526 | prec = TYPE_PRECISION (inner_type); | |
2527 | /* See if we can shorten the right shift. */ | |
2528 | if (shiftc < prec) | |
2529 | shift_type = inner_type; | |
2530 | /* Otherwise X >> C1 is all zeros, so we'll optimize | |
2531 | it into (X, 0) later on by making sure zerobits | |
2532 | is all ones. */ | |
2533 | } | |
2534 | } | |
7097b942 | 2535 | zerobits = HOST_WIDE_INT_M1U; |
4c8f3f72 | 2536 | if (shiftc < prec) |
2537 | { | |
2538 | zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; | |
2539 | zerobits <<= prec - shiftc; | |
2540 | } | |
2541 | /* For arithmetic shift if sign bit could be set, zerobits | |
2542 | can contain actually sign bits, so no transformation is | |
2543 | possible, unless MASK masks them all away. In that | |
2544 | case the shift needs to be converted into logical shift. */ | |
2545 | if (!TYPE_UNSIGNED (TREE_TYPE (@3)) | |
2546 | && prec == TYPE_PRECISION (TREE_TYPE (@3))) | |
2547 | { | |
2548 | if ((mask & zerobits) == 0) | |
2549 | shift_type = unsigned_type_for (TREE_TYPE (@3)); | |
2550 | else | |
2551 | zerobits = 0; | |
2552 | } | |
2553 | } | |
2554 | } | |
2555 | /* ((X << 16) & 0xff00) is (X, 0). */ | |
2556 | (if ((mask & zerobits) == mask) | |
d74b7335 | 2557 | { build_int_cst (type, 0); } |
2558 | (with { newmask = mask | zerobits; } | |
2559 | (if (newmask != mask && (newmask & (newmask + 1)) == 0) | |
2560 | (with | |
2561 | { | |
2562 | /* Only do the transformation if NEWMASK is some integer | |
2563 | mode's mask. */ | |
2564 | for (prec = BITS_PER_UNIT; | |
2565 | prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) | |
edc19fd0 | 2566 | if (newmask == (HOST_WIDE_INT_1U << prec) - 1) |
d74b7335 | 2567 | break; |
2568 | } | |
2569 | (if (prec < HOST_BITS_PER_WIDE_INT | |
7097b942 | 2570 | || newmask == HOST_WIDE_INT_M1U) |
d74b7335 | 2571 | (with |
2572 | { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); } | |
2573 | (if (!tree_int_cst_equal (newmaskt, @2)) | |
2574 | (if (shift_type != TREE_TYPE (@3)) | |
2575 | (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; }) | |
2576 | (bit_and @4 { newmaskt; }))))))))))))) | |
4c8f3f72 | 2577 | |
4de74241 | 2578 | /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1) |
2579 | (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */ | |
54c06b77 | 2580 | (for shift (lshift rshift) |
4de74241 | 2581 | (for bit_op (bit_and bit_xor bit_ior) |
2582 | (simplify | |
2583 | (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1) | |
2584 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
2585 | (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); } | |
2586 | (bit_op (shift (convert @0) @1) { mask; })))))) | |
54c06b77 | 2587 | |
2f6a4d21 | 2588 | /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */ |
2589 | (simplify | |
2590 | (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2))) | |
2591 | (if (!TYPE_UNSIGNED (TREE_TYPE (@0)) | |
c0205bab | 2592 | && (element_precision (TREE_TYPE (@0)) |
2593 | <= element_precision (TREE_TYPE (@1)) | |
2594 | || !TYPE_UNSIGNED (TREE_TYPE (@1)))) | |
2f6a4d21 | 2595 | (with |
2596 | { tree shift_type = TREE_TYPE (@0); } | |
2597 | (convert (rshift (convert:shift_type @1) @2))))) | |
2598 | ||
2599 | /* ~(~X >>r Y) -> X >>r Y | |
2600 | ~(~X <<r Y) -> X <<r Y */ | |
2601 | (for rotate (lrotate rrotate) | |
2602 | (simplify | |
2603 | (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2))) | |
c0205bab | 2604 | (if ((element_precision (TREE_TYPE (@0)) |
2605 | <= element_precision (TREE_TYPE (@1)) | |
2606 | || !TYPE_UNSIGNED (TREE_TYPE (@1))) | |
2607 | && (element_precision (type) <= element_precision (TREE_TYPE (@0)) | |
2608 | || !TYPE_UNSIGNED (TREE_TYPE (@0)))) | |
2f6a4d21 | 2609 | (with |
2610 | { tree rotate_type = TREE_TYPE (@0); } | |
2611 | (convert (rotate (convert:rotate_type @1) @2)))))) | |
54c06b77 | 2612 | |
6d58fd21 | 2613 | /* Simplifications of conversions. */ |
2614 | ||
2615 | /* Basic strip-useless-type-conversions / strip_nops. */ | |
2ad7e37a | 2616 | (for cvt (convert view_convert float fix_trunc) |
6d58fd21 | 2617 | (simplify |
2618 | (cvt @0) | |
2619 | (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0))) | |
2620 | || (GENERIC && type == TREE_TYPE (@0))) | |
2621 | @0))) | |
2622 | ||
2623 | /* Contract view-conversions. */ | |
2624 | (simplify | |
2625 | (view_convert (view_convert @0)) | |
2626 | (view_convert @0)) | |
2627 | ||
2628 | /* For integral conversions with the same precision or pointer | |
2629 | conversions use a NOP_EXPR instead. */ | |
2630 | (simplify | |
2631 | (view_convert @0) | |
2632 | (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)) | |
2633 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
2634 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))) | |
2635 | (convert @0))) | |
2636 | ||
ac857b3d | 2637 | /* Strip inner integral conversions that do not change precision or size, or |
2638 | zero-extend while keeping the same size (for bool-to-char). */ | |
6d58fd21 | 2639 | (simplify |
2640 | (view_convert (convert@0 @1)) | |
2641 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
2642 | && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
ac857b3d | 2643 | && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1)) |
2644 | && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)) | |
2645 | || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1)) | |
2646 | && TYPE_UNSIGNED (TREE_TYPE (@1))))) | |
6d58fd21 | 2647 | (view_convert @1))) |
2648 | ||
559783f6 | 2649 | /* Simplify a view-converted empty constructor. */ |
2650 | (simplify | |
2651 | (view_convert CONSTRUCTOR@0) | |
2652 | (if (TREE_CODE (@0) != SSA_NAME | |
2653 | && CONSTRUCTOR_NELTS (@0) == 0) | |
2654 | { build_zero_cst (type); })) | |
2655 | ||
6d58fd21 | 2656 | /* Re-association barriers around constants and other re-association |
2657 | barriers can be removed. */ | |
2658 | (simplify | |
2659 | (paren CONSTANT_CLASS_P@0) | |
2660 | @0) | |
2661 | (simplify | |
2662 | (paren (paren@1 @0)) | |
2663 | @1) | |
c434260f | 2664 | |
2665 | /* Handle cases of two conversions in a row. */ | |
2666 | (for ocvt (convert float fix_trunc) | |
2667 | (for icvt (convert float) | |
2668 | (simplify | |
2669 | (ocvt (icvt@1 @0)) | |
2670 | (with | |
2671 | { | |
2672 | tree inside_type = TREE_TYPE (@0); | |
2673 | tree inter_type = TREE_TYPE (@1); | |
2674 | int inside_int = INTEGRAL_TYPE_P (inside_type); | |
2675 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
2676 | int inside_float = FLOAT_TYPE_P (inside_type); | |
ccb14741 | 2677 | int inside_vec = VECTOR_TYPE_P (inside_type); |
c434260f | 2678 | unsigned int inside_prec = TYPE_PRECISION (inside_type); |
2679 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
2680 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
2681 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
2682 | int inter_float = FLOAT_TYPE_P (inter_type); | |
ccb14741 | 2683 | int inter_vec = VECTOR_TYPE_P (inter_type); |
c434260f | 2684 | unsigned int inter_prec = TYPE_PRECISION (inter_type); |
2685 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
2686 | int final_int = INTEGRAL_TYPE_P (type); | |
2687 | int final_ptr = POINTER_TYPE_P (type); | |
2688 | int final_float = FLOAT_TYPE_P (type); | |
ccb14741 | 2689 | int final_vec = VECTOR_TYPE_P (type); |
c434260f | 2690 | unsigned int final_prec = TYPE_PRECISION (type); |
2691 | int final_unsignedp = TYPE_UNSIGNED (type); | |
2692 | } | |
f998447f | 2693 | (switch |
2694 | /* In addition to the cases of two conversions in a row | |
2695 | handled below, if we are converting something to its own | |
2696 | type via an object of identical or wider precision, neither | |
2697 | conversion is needed. */ | |
2698 | (if (((GIMPLE && useless_type_conversion_p (type, inside_type)) | |
2699 | || (GENERIC | |
2700 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type))) | |
2701 | && (((inter_int || inter_ptr) && final_int) | |
2702 | || (inter_float && final_float)) | |
2703 | && inter_prec >= final_prec) | |
2704 | (ocvt @0)) | |
2705 | ||
2706 | /* Likewise, if the intermediate and initial types are either both | |
2707 | float or both integer, we don't need the middle conversion if the | |
2708 | former is wider than the latter and doesn't change the signedness | |
2709 | (for integers). Avoid this if the final type is a pointer since | |
f2bfcc60 | 2710 | then we sometimes need the middle conversion. */ |
f998447f | 2711 | (if (((inter_int && inside_int) || (inter_float && inside_float)) |
2712 | && (final_int || final_float) | |
2713 | && inter_prec >= inside_prec | |
f2bfcc60 | 2714 | && (inter_float || inter_unsignedp == inside_unsignedp)) |
f998447f | 2715 | (ocvt @0)) |
2716 | ||
2717 | /* If we have a sign-extension of a zero-extended value, we can | |
2718 | replace that by a single zero-extension. Likewise if the | |
2719 | final conversion does not change precision we can drop the | |
2720 | intermediate conversion. */ | |
2721 | (if (inside_int && inter_int && final_int | |
2722 | && ((inside_prec < inter_prec && inter_prec < final_prec | |
2723 | && inside_unsignedp && !inter_unsignedp) | |
2724 | || final_prec == inter_prec)) | |
2725 | (ocvt @0)) | |
2726 | ||
2727 | /* Two conversions in a row are not needed unless: | |
c434260f | 2728 | - some conversion is floating-point (overstrict for now), or |
2729 | - some conversion is a vector (overstrict for now), or | |
2730 | - the intermediate type is narrower than both initial and | |
2731 | final, or | |
2732 | - the intermediate type and innermost type differ in signedness, | |
2733 | and the outermost type is wider than the intermediate, or | |
2734 | - the initial type is a pointer type and the precisions of the | |
2735 | intermediate and final types differ, or | |
2736 | - the final type is a pointer type and the precisions of the | |
2737 | initial and intermediate types differ. */ | |
f998447f | 2738 | (if (! inside_float && ! inter_float && ! final_float |
2739 | && ! inside_vec && ! inter_vec && ! final_vec | |
2740 | && (inter_prec >= inside_prec || inter_prec >= final_prec) | |
2741 | && ! (inside_int && inter_int | |
2742 | && inter_unsignedp != inside_unsignedp | |
2743 | && inter_prec < final_prec) | |
2744 | && ((inter_unsignedp && inter_prec > inside_prec) | |
2745 | == (final_unsignedp && final_prec > inter_prec)) | |
2746 | && ! (inside_ptr && inter_prec != final_prec) | |
f2bfcc60 | 2747 | && ! (final_ptr && inside_prec != inter_prec)) |
f998447f | 2748 | (ocvt @0)) |
2749 | ||
2750 | /* A truncation to an unsigned type (a zero-extension) should be | |
2751 | canonicalized as bitwise and of a mask. */ | |
645a9d8e | 2752 | (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */ |
2753 | && final_int && inter_int && inside_int | |
f998447f | 2754 | && final_prec == inside_prec |
2755 | && final_prec > inter_prec | |
2756 | && inter_unsignedp) | |
2757 | (convert (bit_and @0 { wide_int_to_tree | |
2758 | (inside_type, | |
2759 | wi::mask (inter_prec, false, | |
2760 | TYPE_PRECISION (inside_type))); }))) | |
2761 | ||
2762 | /* If we are converting an integer to a floating-point that can | |
2763 | represent it exactly and back to an integer, we can skip the | |
2764 | floating-point conversion. */ | |
2765 | (if (GIMPLE /* PR66211 */ | |
2766 | && inside_int && inter_float && final_int && | |
2767 | (unsigned) significand_size (TYPE_MODE (inter_type)) | |
2768 | >= inside_prec - !inside_unsignedp) | |
2769 | (convert @0))))))) | |
e4076540 | 2770 | |
2771 | /* If we have a narrowing conversion to an integral type that is fed by a | |
2772 | BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely | |
2773 | masks off bits outside the final type (and nothing else). */ | |
2774 | (simplify | |
2775 | (convert (bit_and @0 INTEGER_CST@1)) | |
2776 | (if (INTEGRAL_TYPE_P (type) | |
2777 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2778 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) | |
2779 | && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1), | |
2780 | TYPE_PRECISION (type)), 0)) | |
2781 | (convert @0))) | |
6f5ffe81 | 2782 | |
2783 | ||
2784 | /* (X /[ex] A) * A -> X. */ | |
2785 | (simplify | |
c76c65ae | 2786 | (mult (convert1? (exact_div @0 @@1)) (convert2? @1)) |
2787 | (convert @0)) | |
20ba0684 | 2788 | |
295b8968 | 2789 | /* Simplify (A / B) * B + (A % B) -> A. */ |
2790 | (for div (trunc_div ceil_div floor_div round_div) | |
2791 | mod (trunc_mod ceil_mod floor_mod round_mod) | |
2792 | (simplify | |
2793 | (plus:c (mult:c (div @0 @1) @1) (mod @0 @1)) | |
2794 | @0)) | |
2795 | ||
54333f7f | 2796 | /* ((X /[ex] A) +- B) * A --> X +- A * B. */ |
2797 | (for op (plus minus) | |
2798 | (simplify | |
2799 | (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1) | |
2800 | (if (tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
2801 | && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))) | |
2802 | (with | |
2803 | { | |
2804 | wi::overflow_type overflow; | |
2805 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), | |
2806 | TYPE_SIGN (type), &overflow); | |
2807 | } | |
2808 | (if (types_match (type, TREE_TYPE (@2)) | |
2809 | && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow) | |
2810 | (op @0 { wide_int_to_tree (type, mul); }) | |
2811 | (with { tree utype = unsigned_type_for (type); } | |
2812 | (convert (op (convert:utype @0) | |
2813 | (mult (convert:utype @1) (convert:utype @2)))))))))) | |
2814 | ||
6a78ea5d | 2815 | /* Canonicalization of binary operations. */ |
2816 | ||
2817 | /* Convert X + -C into X - C. */ | |
2818 | (simplify | |
2819 | (plus @0 REAL_CST@1) | |
2820 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
0808dfce | 2821 | (with { tree tem = const_unop (NEGATE_EXPR, type, @1); } |
6a78ea5d | 2822 | (if (!TREE_OVERFLOW (tem) || !flag_trapping_math) |
2823 | (minus @0 { tem; }))))) | |
2824 | ||
bff407f2 | 2825 | /* Convert x+x into x*2. */ |
6a78ea5d | 2826 | (simplify |
2827 | (plus @0 @0) | |
2828 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
bff407f2 | 2829 | (mult @0 { build_real (type, dconst2); }) |
2830 | (if (INTEGRAL_TYPE_P (type)) | |
2831 | (mult @0 { build_int_cst (type, 2); })))) | |
6a78ea5d | 2832 | |
ff3aa16e | 2833 | /* 0 - X -> -X. */ |
6a78ea5d | 2834 | (simplify |
2835 | (minus integer_zerop @1) | |
2836 | (negate @1)) | |
ff3aa16e | 2837 | (simplify |
2838 | (pointer_diff integer_zerop @1) | |
2839 | (negate (convert @1))) | |
6a78ea5d | 2840 | |
2841 | /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether | |
2842 | ARG0 is zero and X + ARG0 reduces to X, since that would mean | |
2843 | (-ARG1 + ARG0) reduces to -ARG1. */ | |
2844 | (simplify | |
2845 | (minus real_zerop@0 @1) | |
2846 | (if (fold_real_zero_addition_p (type, @0, 0)) | |
2847 | (negate @1))) | |
2848 | ||
2849 | /* Transform x * -1 into -x. */ | |
2850 | (simplify | |
2851 | (mult @0 integer_minus_onep) | |
2852 | (negate @0)) | |
20ba0684 | 2853 | |
6b7eaf05 | 2854 | /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce |
2855 | signed overflow for CST != 0 && CST != -1. */ | |
2856 | (simplify | |
f359ebf1 | 2857 | (mult:c (mult:s@3 @0 INTEGER_CST@1) @2) |
6b7eaf05 | 2858 | (if (TREE_CODE (@2) != INTEGER_CST |
f359ebf1 | 2859 | && single_use (@3) |
6b7eaf05 | 2860 | && !integer_zerop (@1) && !integer_minus_onep (@1)) |
2861 | (mult (mult @0 @2) @1))) | |
2862 | ||
5e324b84 | 2863 | /* True if we can easily extract the real and imaginary parts of a complex |
2864 | number. */ | |
2865 | (match compositional_complex | |
2866 | (convert? (complex @0 @1))) | |
2867 | ||
20ba0684 | 2868 | /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */ |
2869 | (simplify | |
2870 | (complex (realpart @0) (imagpart @0)) | |
2871 | @0) | |
2872 | (simplify | |
2873 | (realpart (complex @0 @1)) | |
2874 | @0) | |
2875 | (simplify | |
2876 | (imagpart (complex @0 @1)) | |
2877 | @1) | |
e18f6095 | 2878 | |
d92f7c8d | 2879 | /* Sometimes we only care about half of a complex expression. */ |
2880 | (simplify | |
2881 | (realpart (convert?:s (conj:s @0))) | |
2882 | (convert (realpart @0))) | |
2883 | (simplify | |
2884 | (imagpart (convert?:s (conj:s @0))) | |
2885 | (convert (negate (imagpart @0)))) | |
2886 | (for part (realpart imagpart) | |
2887 | (for op (plus minus) | |
2888 | (simplify | |
2889 | (part (convert?:s@2 (op:s @0 @1))) | |
2890 | (convert (op (part @0) (part @1)))))) | |
2891 | (simplify | |
2892 | (realpart (convert?:s (CEXPI:s @0))) | |
2893 | (convert (COS @0))) | |
2894 | (simplify | |
2895 | (imagpart (convert?:s (CEXPI:s @0))) | |
2896 | (convert (SIN @0))) | |
2897 | ||
2898 | /* conj(conj(x)) -> x */ | |
2899 | (simplify | |
2900 | (conj (convert? (conj @0))) | |
2901 | (if (tree_nop_conversion_p (TREE_TYPE (@0), type)) | |
2902 | (convert @0))) | |
2903 | ||
2904 | /* conj({x,y}) -> {x,-y} */ | |
2905 | (simplify | |
2906 | (conj (convert?:s (complex:s @0 @1))) | |
2907 | (with { tree itype = TREE_TYPE (type); } | |
2908 | (complex (convert:itype @0) (negate (convert:itype @1))))) | |
e18f6095 | 2909 | |
2910 | /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */ | |
2911 | (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64) | |
2912 | (simplify | |
2913 | (bswap (bswap @0)) | |
2914 | @0) | |
2915 | (simplify | |
2916 | (bswap (bit_not (bswap @0))) | |
2917 | (bit_not @0)) | |
2918 | (for bitop (bit_xor bit_ior bit_and) | |
2919 | (simplify | |
2920 | (bswap (bitop:c (bswap @0) @1)) | |
2921 | (bitop @0 (bswap @1))))) | |
84debb86 | 2922 | |
2923 | ||
2924 | /* Combine COND_EXPRs and VEC_COND_EXPRs. */ | |
2925 | ||
2926 | /* Simplify constant conditions. | |
2927 | Only optimize constant conditions when the selected branch | |
2928 | has the same type as the COND_EXPR. This avoids optimizing | |
2929 | away "c ? x : throw", where the throw has a void type. | |
2930 | Note that we cannot throw away the fold-const.c variant nor | |
2931 | this one as we depend on doing this transform before possibly | |
2932 | A ? B : B -> B triggers and the fold-const.c one can optimize | |
2933 | 0 ? A : B to B even if A has side-effects. Something | |
2934 | genmatch cannot handle. */ | |
2935 | (simplify | |
2936 | (cond INTEGER_CST@0 @1 @2) | |
d74b7335 | 2937 | (if (integer_zerop (@0)) |
2938 | (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type)) | |
2939 | @2) | |
2940 | (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type)) | |
2941 | @1))) | |
84debb86 | 2942 | (simplify |
2943 | (vec_cond VECTOR_CST@0 @1 @2) | |
2944 | (if (integer_all_onesp (@0)) | |
d74b7335 | 2945 | @1 |
2946 | (if (integer_zerop (@0)) | |
2947 | @2))) | |
84debb86 | 2948 | |
87cfc44d | 2949 | /* Sink unary operations to constant branches, but only if we do fold it to |
2950 | constants. */ | |
2951 | (for op (negate bit_not abs absu) | |
2952 | (simplify | |
2953 | (op (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)) | |
2954 | (with | |
2955 | { | |
2956 | tree cst1, cst2; | |
2957 | cst1 = const_unop (op, type, @1); | |
2958 | if (cst1) | |
2959 | cst2 = const_unop (op, type, @2); | |
2960 | } | |
2961 | (if (cst1 && cst2) | |
2962 | (vec_cond @0 { cst1; } { cst2; }))))) | |
2963 | ||
83c0fb43 | 2964 | /* Simplification moved from fold_cond_expr_with_comparison. It may also |
2965 | be extended. */ | |
4e33b632 | 2966 | /* This pattern implements two kinds simplification: |
2967 | ||
2968 | Case 1) | |
2969 | (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if: | |
83c0fb43 | 2970 | 1) Conversions are type widening from smaller type. |
2971 | 2) Const c1 equals to c2 after canonicalizing comparison. | |
2972 | 3) Comparison has tree code LT, LE, GT or GE. | |
2973 | This specific pattern is needed when (cmp (convert x) c) may not | |
2974 | be simplified by comparison patterns because of multiple uses of | |
2975 | x. It also makes sense here because simplifying across multiple | |
4e33b632 | 2976 | referred var is always benefitial for complicated cases. |
2977 | ||
2978 | Case 2) | |
2979 | (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */ | |
2980 | (for cmp (lt le gt ge eq) | |
83c0fb43 | 2981 | (simplify |
a5a96d9f | 2982 | (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2) |
83c0fb43 | 2983 | (with |
2984 | { | |
2985 | tree from_type = TREE_TYPE (@1); | |
2986 | tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2); | |
a5a96d9f | 2987 | enum tree_code code = ERROR_MARK; |
83c0fb43 | 2988 | |
a5a96d9f | 2989 | if (INTEGRAL_TYPE_P (from_type) |
2990 | && int_fits_type_p (@2, from_type) | |
83c0fb43 | 2991 | && (types_match (c1_type, from_type) |
2992 | || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type) | |
2993 | && (TYPE_UNSIGNED (from_type) | |
2994 | || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type)))) | |
2995 | && (types_match (c2_type, from_type) | |
2996 | || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type) | |
2997 | && (TYPE_UNSIGNED (from_type) | |
2998 | || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type))))) | |
2999 | { | |
a5a96d9f | 3000 | if (cmp != EQ_EXPR) |
83c0fb43 | 3001 | { |
4e33b632 | 3002 | if (wi::to_widest (@3) == (wi::to_widest (@2) - 1)) |
3003 | { | |
3004 | /* X <= Y - 1 equals to X < Y. */ | |
a5a96d9f | 3005 | if (cmp == LE_EXPR) |
4e33b632 | 3006 | code = LT_EXPR; |
3007 | /* X > Y - 1 equals to X >= Y. */ | |
a5a96d9f | 3008 | if (cmp == GT_EXPR) |
4e33b632 | 3009 | code = GE_EXPR; |
3010 | } | |
3011 | if (wi::to_widest (@3) == (wi::to_widest (@2) + 1)) | |
3012 | { | |
3013 | /* X < Y + 1 equals to X <= Y. */ | |
a5a96d9f | 3014 | if (cmp == LT_EXPR) |
4e33b632 | 3015 | code = LE_EXPR; |
3016 | /* X >= Y + 1 equals to X > Y. */ | |
a5a96d9f | 3017 | if (cmp == GE_EXPR) |
4e33b632 | 3018 | code = GT_EXPR; |
3019 | } | |
a5a96d9f | 3020 | if (code != ERROR_MARK |
3021 | || wi::to_widest (@2) == wi::to_widest (@3)) | |
4e33b632 | 3022 | { |
a5a96d9f | 3023 | if (cmp == LT_EXPR || cmp == LE_EXPR) |
4e33b632 | 3024 | code = MIN_EXPR; |
a5a96d9f | 3025 | if (cmp == GT_EXPR || cmp == GE_EXPR) |
4e33b632 | 3026 | code = MAX_EXPR; |
3027 | } | |
83c0fb43 | 3028 | } |
4e33b632 | 3029 | /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */ |
a5a96d9f | 3030 | else if (int_fits_type_p (@3, from_type)) |
3031 | code = EQ_EXPR; | |
83c0fb43 | 3032 | } |
3033 | } | |
3034 | (if (code == MAX_EXPR) | |
70241e81 | 3035 | (convert (max @1 (convert @2))) |
83c0fb43 | 3036 | (if (code == MIN_EXPR) |
70241e81 | 3037 | (convert (min @1 (convert @2))) |
4e33b632 | 3038 | (if (code == EQ_EXPR) |
a5a96d9f | 3039 | (convert (cond (eq @1 (convert @3)) |
70241e81 | 3040 | (convert:from_type @3) (convert:from_type @2))))))))) |
83c0fb43 | 3041 | |
e7a6ef61 | 3042 | /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if: |
3043 | ||
3044 | 1) OP is PLUS or MINUS. | |
3045 | 2) CMP is LT, LE, GT or GE. | |
3046 | 3) C3 == (C1 op C2), and computation doesn't have undefined behavior. | |
3047 | ||
3048 | This pattern also handles special cases like: | |
3049 | ||
3050 | A) Operand x is a unsigned to signed type conversion and c1 is | |
3051 | integer zero. In this case, | |
3052 | (signed type)x < 0 <=> x > MAX_VAL(signed type) | |
3053 | (signed type)x >= 0 <=> x <= MAX_VAL(signed type) | |
3054 | B) Const c1 may not equal to (C3 op' C2). In this case we also | |
3055 | check equality for (c1+1) and (c1-1) by adjusting comparison | |
3056 | code. | |
3057 | ||
3058 | TODO: Though signed type is handled by this pattern, it cannot be | |
3059 | simplified at the moment because C standard requires additional | |
3060 | type promotion. In order to match&simplify it here, the IR needs | |
3061 | to be cleaned up by other optimizers, i.e, VRP. */ | |
3062 | (for op (plus minus) | |
3063 | (for cmp (lt le gt ge) | |
3064 | (simplify | |
3065 | (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3) | |
3066 | (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); } | |
3067 | (if (types_match (from_type, to_type) | |
3068 | /* Check if it is special case A). */ | |
3069 | || (TYPE_UNSIGNED (from_type) | |
3070 | && !TYPE_UNSIGNED (to_type) | |
3071 | && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type) | |
3072 | && integer_zerop (@1) | |
3073 | && (cmp == LT_EXPR || cmp == GE_EXPR))) | |
3074 | (with | |
3075 | { | |
30b5769f | 3076 | wi::overflow_type overflow = wi::OVF_NONE; |
e7a6ef61 | 3077 | enum tree_code code, cmp_code = cmp; |
e3d0f65c | 3078 | wide_int real_c1; |
3079 | wide_int c1 = wi::to_wide (@1); | |
3080 | wide_int c2 = wi::to_wide (@2); | |
3081 | wide_int c3 = wi::to_wide (@3); | |
e7a6ef61 | 3082 | signop sgn = TYPE_SIGN (from_type); |
3083 | ||
3084 | /* Handle special case A), given x of unsigned type: | |
3085 | ((signed type)x < 0) <=> (x > MAX_VAL(signed type)) | |
3086 | ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */ | |
3087 | if (!types_match (from_type, to_type)) | |
3088 | { | |
3089 | if (cmp_code == LT_EXPR) | |
3090 | cmp_code = GT_EXPR; | |
3091 | if (cmp_code == GE_EXPR) | |
3092 | cmp_code = LE_EXPR; | |
3093 | c1 = wi::max_value (to_type); | |
3094 | } | |
3095 | /* To simplify this pattern, we require c3 = (c1 op c2). Here we | |
3096 | compute (c3 op' c2) and check if it equals to c1 with op' being | |
3097 | the inverted operator of op. Make sure overflow doesn't happen | |
3098 | if it is undefined. */ | |
3099 | if (op == PLUS_EXPR) | |
3100 | real_c1 = wi::sub (c3, c2, sgn, &overflow); | |
3101 | else | |
3102 | real_c1 = wi::add (c3, c2, sgn, &overflow); | |
3103 | ||
3104 | code = cmp_code; | |
3105 | if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type)) | |
3106 | { | |
3107 | /* Check if c1 equals to real_c1. Boundary condition is handled | |
3108 | by adjusting comparison operation if necessary. */ | |
3109 | if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn) | |
3110 | && !overflow) | |
3111 | { | |
3112 | /* X <= Y - 1 equals to X < Y. */ | |
3113 | if (cmp_code == LE_EXPR) | |
3114 | code = LT_EXPR; | |
3115 | /* X > Y - 1 equals to X >= Y. */ | |
3116 | if (cmp_code == GT_EXPR) | |
3117 | code = GE_EXPR; | |
3118 | } | |
3119 | if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn) | |
3120 | && !overflow) | |
3121 | { | |
3122 | /* X < Y + 1 equals to X <= Y. */ | |
3123 | if (cmp_code == LT_EXPR) | |
3124 | code = LE_EXPR; | |
3125 | /* X >= Y + 1 equals to X > Y. */ | |
3126 | if (cmp_code == GE_EXPR) | |
3127 | code = GT_EXPR; | |
3128 | } | |
3129 | if (code != cmp_code || !wi::cmp (real_c1, c1, sgn)) | |
3130 | { | |
3131 | if (cmp_code == LT_EXPR || cmp_code == LE_EXPR) | |
3132 | code = MIN_EXPR; | |
3133 | if (cmp_code == GT_EXPR || cmp_code == GE_EXPR) | |
3134 | code = MAX_EXPR; | |
3135 | } | |
3136 | } | |
3137 | } | |
3138 | (if (code == MAX_EXPR) | |
3139 | (op (max @X { wide_int_to_tree (from_type, real_c1); }) | |
3140 | { wide_int_to_tree (from_type, c2); }) | |
3141 | (if (code == MIN_EXPR) | |
3142 | (op (min @X { wide_int_to_tree (from_type, real_c1); }) | |
3143 | { wide_int_to_tree (from_type, c2); }))))))))) | |
3144 | ||
84debb86 | 3145 | (for cnd (cond vec_cond) |
3146 | /* A ? B : (A ? X : C) -> A ? B : C. */ | |
3147 | (simplify | |
3148 | (cnd @0 (cnd @0 @1 @2) @3) | |
3149 | (cnd @0 @1 @3)) | |
3150 | (simplify | |
3151 | (cnd @0 @1 (cnd @0 @2 @3)) | |
3152 | (cnd @0 @1 @3)) | |
79f1ed25 | 3153 | /* A ? B : (!A ? C : X) -> A ? B : C. */ |
3154 | /* ??? This matches embedded conditions open-coded because genmatch | |
3155 | would generate matching code for conditions in separate stmts only. | |
3156 | The following is still important to merge then and else arm cases | |
3157 | from if-conversion. */ | |
3158 | (simplify | |
3159 | (cnd @0 @1 (cnd @2 @3 @4)) | |
03821886 | 3160 | (if (inverse_conditions_p (@0, @2)) |
79f1ed25 | 3161 | (cnd @0 @1 @3))) |
3162 | (simplify | |
3163 | (cnd @0 (cnd @1 @2 @3) @4) | |
03821886 | 3164 | (if (inverse_conditions_p (@0, @1)) |
79f1ed25 | 3165 | (cnd @0 @3 @4))) |
84debb86 | 3166 | |
3167 | /* A ? B : B -> B. */ | |
3168 | (simplify | |
3169 | (cnd @0 @1 @1) | |
ccb14741 | 3170 | @1) |
84debb86 | 3171 | |
ccb14741 | 3172 | /* !A ? B : C -> A ? C : B. */ |
3173 | (simplify | |
3174 | (cnd (logical_inverted_value truth_valued_p@0) @1 @2) | |
3175 | (cnd @0 @2 @1))) | |
9ec260f0 | 3176 | |
19db1ec8 | 3177 | /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons |
3178 | return all -1 or all 0 results. */ | |
b13fec6a | 3179 | /* ??? We could instead convert all instances of the vec_cond to negate, |
3180 | but that isn't necessarily a win on its own. */ | |
3181 | (simplify | |
19db1ec8 | 3182 | (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) |
b13fec6a | 3183 | (if (VECTOR_TYPE_P (type) |
f08ee65f | 3184 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
3185 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))) | |
b13fec6a | 3186 | && (TYPE_MODE (TREE_TYPE (type)) |
1a86097c | 3187 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) |
19db1ec8 | 3188 | (minus @3 (view_convert (vec_cond @0 (negate @1) @2))))) |
b13fec6a | 3189 | |
19db1ec8 | 3190 | /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */ |
b13fec6a | 3191 | (simplify |
19db1ec8 | 3192 | (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) |
b13fec6a | 3193 | (if (VECTOR_TYPE_P (type) |
f08ee65f | 3194 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
3195 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))) | |
b13fec6a | 3196 | && (TYPE_MODE (TREE_TYPE (type)) |
1a86097c | 3197 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) |
19db1ec8 | 3198 | (plus @3 (view_convert (vec_cond @0 (negate @1) @2))))) |
9ec260f0 | 3199 | |
fd425e62 | 3200 | |
9ec260f0 | 3201 | /* Simplifications of comparisons. */ |
3202 | ||
67d07229 | 3203 | /* See if we can reduce the magnitude of a constant involved in a |
3204 | comparison by changing the comparison code. This is a canonicalization | |
3205 | formerly done by maybe_canonicalize_comparison_1. */ | |
3206 | (for cmp (le gt) | |
3207 | acmp (lt ge) | |
3208 | (simplify | |
7931f9ca | 3209 | (cmp @0 uniform_integer_cst_p@1) |
3210 | (with { tree cst = uniform_integer_cst_p (@1); } | |
3211 | (if (tree_int_cst_sgn (cst) == -1) | |
3212 | (acmp @0 { build_uniform_cst (TREE_TYPE (@1), | |
3213 | wide_int_to_tree (TREE_TYPE (cst), | |
3214 | wi::to_wide (cst) | |
3215 | + 1)); }))))) | |
67d07229 | 3216 | (for cmp (ge lt) |
3217 | acmp (gt le) | |
3218 | (simplify | |
7931f9ca | 3219 | (cmp @0 uniform_integer_cst_p@1) |
3220 | (with { tree cst = uniform_integer_cst_p (@1); } | |
3221 | (if (tree_int_cst_sgn (cst) == 1) | |
3222 | (acmp @0 { build_uniform_cst (TREE_TYPE (@1), | |
3223 | wide_int_to_tree (TREE_TYPE (cst), | |
3224 | wi::to_wide (cst) - 1)); }))))) | |
67d07229 | 3225 | |
9ec260f0 | 3226 | /* We can simplify a logical negation of a comparison to the |
3227 | inverted comparison. As we cannot compute an expression | |
3228 | operator using invert_tree_comparison we have to simulate | |
3229 | that with expression code iteration. */ | |
3230 | (for cmp (tcc_comparison) | |
3231 | icmp (inverted_tcc_comparison) | |
3232 | ncmp (inverted_tcc_comparison_with_nans) | |
3233 | /* Ideally we'd like to combine the following two patterns | |
3234 | and handle some more cases by using | |
3235 | (logical_inverted_value (cmp @0 @1)) | |
3236 | here but for that genmatch would need to "inline" that. | |
3237 | For now implement what forward_propagate_comparison did. */ | |
3238 | (simplify | |
3239 | (bit_not (cmp @0 @1)) | |
3240 | (if (VECTOR_TYPE_P (type) | |
3241 | || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)) | |
3242 | /* Comparison inversion may be impossible for trapping math, | |
3243 | invert_tree_comparison will tell us. But we can't use | |
3244 | a computed operator in the replacement tree thus we have | |
3245 | to play the trick below. */ | |
3246 | (with { enum tree_code ic = invert_tree_comparison | |
93633022 | 3247 | (cmp, HONOR_NANS (@0)); } |
9ec260f0 | 3248 | (if (ic == icmp) |
d74b7335 | 3249 | (icmp @0 @1) |
3250 | (if (ic == ncmp) | |
3251 | (ncmp @0 @1)))))) | |
9ec260f0 | 3252 | (simplify |
ccb14741 | 3253 | (bit_xor (cmp @0 @1) integer_truep) |
3254 | (with { enum tree_code ic = invert_tree_comparison | |
93633022 | 3255 | (cmp, HONOR_NANS (@0)); } |
ccb14741 | 3256 | (if (ic == icmp) |
d74b7335 | 3257 | (icmp @0 @1) |
3258 | (if (ic == ncmp) | |
3259 | (ncmp @0 @1)))))) | |
f8dad9b4 | 3260 | |
fd425e62 | 3261 | /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. |
3262 | ??? The transformation is valid for the other operators if overflow | |
3263 | is undefined for the type, but performing it here badly interacts | |
3264 | with the transformation in fold_cond_expr_with_comparison which | |
3265 | attempts to synthetize ABS_EXPR. */ | |
3266 | (for cmp (eq ne) | |
57e83b58 | 3267 | (for sub (minus pointer_diff) |
3268 | (simplify | |
3269 | (cmp (sub@2 @0 @1) integer_zerop) | |
3270 | (if (single_use (@2)) | |
3271 | (cmp @0 @1))))) | |
fd425e62 | 3272 | |
3273 | /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the | |
3274 | signed arithmetic case. That form is created by the compiler | |
3275 | often enough for folding it to be of value. One example is in | |
3276 | computing loop trip counts after Operator Strength Reduction. */ | |
c915fa32 | 3277 | (for cmp (simple_comparison) |
3278 | scmp (swapped_simple_comparison) | |
fd425e62 | 3279 | (simplify |
cf293d8e | 3280 | (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2) |
fd425e62 | 3281 | /* Handle unfolded multiplication by zero. */ |
3282 | (if (integer_zerop (@1)) | |
d74b7335 | 3283 | (cmp @1 @2) |
3284 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
cf293d8e | 3285 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) |
3286 | && single_use (@3)) | |
d74b7335 | 3287 | /* If @1 is negative we swap the sense of the comparison. */ |
3288 | (if (tree_int_cst_sgn (@1) < 0) | |
3289 | (scmp @0 @2) | |
3290 | (cmp @0 @2)))))) | |
9603b585 | 3291 | |
fd425e62 | 3292 | /* Simplify comparison of something with itself. For IEEE |
3293 | floating-point, we can only do some of these simplifications. */ | |
be6e4283 | 3294 | (for cmp (eq ge le) |
fd425e62 | 3295 | (simplify |
3296 | (cmp @0 @0) | |
be6e4283 | 3297 | (if (! FLOAT_TYPE_P (TREE_TYPE (@0)) |
2daa2cda | 3298 | || ! HONOR_NANS (@0)) |
be6e4283 | 3299 | { constant_boolean_node (true, type); } |
3300 | (if (cmp != EQ_EXPR) | |
3301 | (eq @0 @0))))) | |
fd425e62 | 3302 | (for cmp (ne gt lt) |
3303 | (simplify | |
3304 | (cmp @0 @0) | |
3305 | (if (cmp != NE_EXPR | |
3306 | || ! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
2daa2cda | 3307 | || ! HONOR_NANS (@0)) |
fd425e62 | 3308 | { constant_boolean_node (false, type); }))) |
56b4132e | 3309 | (for cmp (unle unge uneq) |
3310 | (simplify | |
3311 | (cmp @0 @0) | |
3312 | { constant_boolean_node (true, type); })) | |
47ef9633 | 3313 | (for cmp (unlt ungt) |
3314 | (simplify | |
3315 | (cmp @0 @0) | |
3316 | (unordered @0 @0))) | |
56b4132e | 3317 | (simplify |
3318 | (ltgt @0 @0) | |
3319 | (if (!flag_trapping_math) | |
3320 | { constant_boolean_node (false, type); })) | |
fd425e62 | 3321 | |
3322 | /* Fold ~X op ~Y as Y op X. */ | |
c915fa32 | 3323 | (for cmp (simple_comparison) |
fd425e62 | 3324 | (simplify |
46c54c0f | 3325 | (cmp (bit_not@2 @0) (bit_not@3 @1)) |
3326 | (if (single_use (@2) && single_use (@3)) | |
3327 | (cmp @1 @0)))) | |
fd425e62 | 3328 | |
3329 | /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ | |
c915fa32 | 3330 | (for cmp (simple_comparison) |
3331 | scmp (swapped_simple_comparison) | |
fd425e62 | 3332 | (simplify |
46c54c0f | 3333 | (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1) |
3334 | (if (single_use (@2) | |
3335 | && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)) | |
fd425e62 | 3336 | (scmp @0 (bit_not @1))))) |
3337 | ||
c915fa32 | 3338 | (for cmp (simple_comparison) |
3339 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
3340 | (simplify | |
3341 | (cmp (convert@2 @0) (convert? @1)) | |
3342 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
3343 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
3344 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) | |
3345 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
3346 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))) | |
3347 | (with | |
3348 | { | |
3349 | tree type1 = TREE_TYPE (@1); | |
3350 | if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1)) | |
3351 | { | |
3352 | REAL_VALUE_TYPE orig = TREE_REAL_CST (@1); | |
3353 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node) | |
3354 | && exact_real_truncate (TYPE_MODE (float_type_node), &orig)) | |
3355 | type1 = float_type_node; | |
3356 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node) | |
3357 | && exact_real_truncate (TYPE_MODE (double_type_node), &orig)) | |
3358 | type1 = double_type_node; | |
3359 | } | |
3360 | tree newtype | |
3361 | = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1) | |
9603b585 | 3362 | ? TREE_TYPE (@0) : type1); |
c915fa32 | 3363 | } |
3364 | (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype)) | |
3365 | (cmp (convert:newtype @0) (convert:newtype @1)))))) | |
9603b585 | 3366 | |
c915fa32 | 3367 | (simplify |
3368 | (cmp @0 REAL_CST@1) | |
3369 | /* IEEE doesn't distinguish +0 and -0 in comparisons. */ | |
f998447f | 3370 | (switch |
3371 | /* a CMP (-0) -> a CMP 0 */ | |
3372 | (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))) | |
3373 | (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })) | |
3374 | /* x != NaN is always true, other ops are always false. */ | |
3375 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
3376 | && ! HONOR_SNANS (@1)) | |
3377 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
3378 | /* Fold comparisons against infinity. */ | |
3379 | (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1)) | |
3380 | && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1)))) | |
3381 | (with | |
3382 | { | |
3383 | REAL_VALUE_TYPE max; | |
3384 | enum tree_code code = cmp; | |
3385 | bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)); | |
3386 | if (neg) | |
3387 | code = swap_tree_comparison (code); | |
3388 | } | |
3389 | (switch | |
38f4caae | 3390 | /* x > +Inf is always false, if we ignore NaNs or exceptions. */ |
f998447f | 3391 | (if (code == GT_EXPR |
38f4caae | 3392 | && !(HONOR_NANS (@0) && flag_trapping_math)) |
f998447f | 3393 | { constant_boolean_node (false, type); }) |
3394 | (if (code == LE_EXPR) | |
38f4caae | 3395 | /* x <= +Inf is always true, if we don't care about NaNs. */ |
f998447f | 3396 | (if (! HONOR_NANS (@0)) |
3397 | { constant_boolean_node (true, type); } | |
38f4caae | 3398 | /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses |
3399 | an "invalid" exception. */ | |
3400 | (if (!flag_trapping_math) | |
3401 | (eq @0 @0)))) | |
3402 | /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but | |
3403 | for == this introduces an exception for x a NaN. */ | |
3404 | (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math)) | |
3405 | || code == GE_EXPR) | |
f998447f | 3406 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } |
3407 | (if (neg) | |
3408 | (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
3409 | (gt @0 { build_real (TREE_TYPE (@0), max); })))) | |
3410 | /* x < +Inf is always equal to x <= DBL_MAX. */ | |
3411 | (if (code == LT_EXPR) | |
3412 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
3413 | (if (neg) | |
3414 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
3415 | (le @0 { build_real (TREE_TYPE (@0), max); })))) | |
38f4caae | 3416 | /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces |
3417 | an exception for x a NaN so use an unordered comparison. */ | |
f998447f | 3418 | (if (code == NE_EXPR) |
3419 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
3420 | (if (! HONOR_NANS (@0)) | |
3421 | (if (neg) | |
3422 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
3423 | (le @0 { build_real (TREE_TYPE (@0), max); })) | |
3424 | (if (neg) | |
38f4caae | 3425 | (unge @0 { build_real (TREE_TYPE (@0), max); }) |
3426 | (unle @0 { build_real (TREE_TYPE (@0), max); })))))))))) | |
c915fa32 | 3427 | |
3428 | /* If this is a comparison of a real constant with a PLUS_EXPR | |
3429 | or a MINUS_EXPR of a real constant, we can convert it into a | |
3430 | comparison with a revised real constant as long as no overflow | |
3431 | occurs when unsafe_math_optimizations are enabled. */ | |
3432 | (if (flag_unsafe_math_optimizations) | |
3433 | (for op (plus minus) | |
3434 | (simplify | |
3435 | (cmp (op @0 REAL_CST@1) REAL_CST@2) | |
3436 | (with | |
3437 | { | |
3438 | tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR, | |
3439 | TREE_TYPE (@1), @2, @1); | |
3440 | } | |
a7b8025b | 3441 | (if (tem && !TREE_OVERFLOW (tem)) |
c915fa32 | 3442 | (cmp @0 { tem; })))))) |
3443 | ||
3444 | /* Likewise, we can simplify a comparison of a real constant with | |
3445 | a MINUS_EXPR whose first operand is also a real constant, i.e. | |
3446 | (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on | |
3447 | floating-point types only if -fassociative-math is set. */ | |
3448 | (if (flag_associative_math) | |
3449 | (simplify | |
e7966fe9 | 3450 | (cmp (minus REAL_CST@0 @1) REAL_CST@2) |
c915fa32 | 3451 | (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); } |
a7b8025b | 3452 | (if (tem && !TREE_OVERFLOW (tem)) |
c915fa32 | 3453 | (cmp { tem; } @1))))) |
3454 | ||
3455 | /* Fold comparisons against built-in math functions. */ | |
3456 | (if (flag_unsafe_math_optimizations | |
3457 | && ! flag_errno_math) | |
3458 | (for sq (SQRT) | |
3459 | (simplify | |
3460 | (cmp (sq @0) REAL_CST@1) | |
f998447f | 3461 | (switch |
3462 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
3463 | (switch | |
3464 | /* sqrt(x) < y is always false, if y is negative. */ | |
3465 | (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR) | |
d74b7335 | 3466 | { constant_boolean_node (false, type); }) |
f998447f | 3467 | /* sqrt(x) > y is always true, if y is negative and we |
3468 | don't care about NaNs, i.e. negative values of x. */ | |
3469 | (if (cmp == NE_EXPR || !HONOR_NANS (@0)) | |
3470 | { constant_boolean_node (true, type); }) | |
3471 | /* sqrt(x) > y is the same as x >= 0, if y is negative. */ | |
3472 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))) | |
b5cb0512 | 3473 | (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0)) |
3474 | (switch | |
3475 | /* sqrt(x) < 0 is always false. */ | |
3476 | (if (cmp == LT_EXPR) | |
3477 | { constant_boolean_node (false, type); }) | |
3478 | /* sqrt(x) >= 0 is always true if we don't care about NaNs. */ | |
3479 | (if (cmp == GE_EXPR && !HONOR_NANS (@0)) | |
3480 | { constant_boolean_node (true, type); }) | |
3481 | /* sqrt(x) <= 0 -> x == 0. */ | |
3482 | (if (cmp == LE_EXPR) | |
3483 | (eq @0 @1)) | |
3484 | /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >, | |
3485 | == or !=. In the last case: | |
3486 | ||
3487 | (sqrt(x) != 0) == (NaN != 0) == true == (x != 0) | |
3488 | ||
3489 | if x is negative or NaN. Due to -funsafe-math-optimizations, | |
3490 | the results for other x follow from natural arithmetic. */ | |
3491 | (cmp @0 @1))) | |
f998447f | 3492 | (if (cmp == GT_EXPR || cmp == GE_EXPR) |
3493 | (with | |
3494 | { | |
3495 | REAL_VALUE_TYPE c2; | |
f2ad9e38 | 3496 | real_arithmetic (&c2, MULT_EXPR, |
3497 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
f998447f | 3498 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); |
3499 | } | |
3500 | (if (REAL_VALUE_ISINF (c2)) | |
3501 | /* sqrt(x) > y is x == +Inf, when y is very large. */ | |
3502 | (if (HONOR_INFINITIES (@0)) | |
3503 | (eq @0 { build_real (TREE_TYPE (@0), c2); }) | |
3504 | { constant_boolean_node (false, type); }) | |
3505 | /* sqrt(x) > c is the same as x > c*c. */ | |
3506 | (cmp @0 { build_real (TREE_TYPE (@0), c2); })))) | |
3507 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
3508 | (with | |
3509 | { | |
3510 | REAL_VALUE_TYPE c2; | |
f2ad9e38 | 3511 | real_arithmetic (&c2, MULT_EXPR, |
3512 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
f998447f | 3513 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); |
3514 | } | |
3515 | (if (REAL_VALUE_ISINF (c2)) | |
3516 | (switch | |
3517 | /* sqrt(x) < y is always true, when y is a very large | |
3518 | value and we don't care about NaNs or Infinities. */ | |
3519 | (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0)) | |
3520 | { constant_boolean_node (true, type); }) | |
3521 | /* sqrt(x) < y is x != +Inf when y is very large and we | |
3522 | don't care about NaNs. */ | |
3523 | (if (! HONOR_NANS (@0)) | |
3524 | (ne @0 { build_real (TREE_TYPE (@0), c2); })) | |
3525 | /* sqrt(x) < y is x >= 0 when y is very large and we | |
3526 | don't care about Infinities. */ | |
3527 | (if (! HONOR_INFINITIES (@0)) | |
3528 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) | |
3529 | /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ | |
3530 | (if (GENERIC) | |
3531 | (truth_andif | |
3532 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
3533 | (ne @0 { build_real (TREE_TYPE (@0), c2); })))) | |
3534 | /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ | |
3535 | (if (! HONOR_NANS (@0)) | |
3536 | (cmp @0 { build_real (TREE_TYPE (@0), c2); }) | |
3537 | /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ | |
3538 | (if (GENERIC) | |
3539 | (truth_andif | |
3540 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
44142143 | 3541 | (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))) |
3542 | /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */ | |
3543 | (simplify | |
3544 | (cmp (sq @0) (sq @1)) | |
3545 | (if (! HONOR_NANS (@0)) | |
3546 | (cmp @0 @1)))))) | |
fd425e62 | 3547 | |
91a82d53 | 3548 | /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */ |
8a2dee61 | 3549 | (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) |
3550 | icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne) | |
91a82d53 | 3551 | (simplify |
3552 | (cmp (float@0 @1) (float @2)) | |
3553 | (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0)) | |
3554 | && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) | |
3555 | (with | |
3556 | { | |
3557 | format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)))); | |
3558 | tree type1 = TREE_TYPE (@1); | |
3559 | bool type1_signed_p = TYPE_SIGN (type1) == SIGNED; | |
3560 | tree type2 = TREE_TYPE (@2); | |
3561 | bool type2_signed_p = TYPE_SIGN (type2) == SIGNED; | |
3562 | } | |
3563 | (if (fmt.can_represent_integral_type_p (type1) | |
3564 | && fmt.can_represent_integral_type_p (type2)) | |
8a2dee61 | 3565 | (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR) |
3566 | { constant_boolean_node (cmp == ORDERED_EXPR, type); } | |
3567 | (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2) | |
3568 | && type1_signed_p >= type2_signed_p) | |
3569 | (icmp @1 (convert @2)) | |
3570 | (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2) | |
3571 | && type1_signed_p <= type2_signed_p) | |
3572 | (icmp (convert:type2 @1) @2) | |
3573 | (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2) | |
3574 | && type1_signed_p == type2_signed_p) | |
3575 | (icmp @1 @2)))))))))) | |
91a82d53 | 3576 | |
29587d84 | 3577 | /* Optimize various special cases of (FTYPE) N CMP CST. */ |
3578 | (for cmp (lt le eq ne ge gt) | |
3579 | icmp (le le eq ne ge ge) | |
3580 | (simplify | |
3581 | (cmp (float @0) REAL_CST@1) | |
3582 | (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1)) | |
3583 | && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))) | |
3584 | (with | |
3585 | { | |
3586 | tree itype = TREE_TYPE (@0); | |
29587d84 | 3587 | format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1)))); |
3588 | const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1); | |
3589 | /* Be careful to preserve any potential exceptions due to | |
3590 | NaNs. qNaNs are ok in == or != context. | |
3591 | TODO: relax under -fno-trapping-math or | |
3592 | -fno-signaling-nans. */ | |
3593 | bool exception_p | |
3594 | = real_isnan (cst) && (cst->signalling | |
a924ac80 | 3595 | || (cmp != EQ_EXPR && cmp != NE_EXPR)); |
29587d84 | 3596 | } |
3597 | /* TODO: allow non-fitting itype and SNaNs when | |
3598 | -fno-trapping-math. */ | |
91a82d53 | 3599 | (if (fmt.can_represent_integral_type_p (itype) && ! exception_p) |
29587d84 | 3600 | (with |
3601 | { | |
91a82d53 | 3602 | signop isign = TYPE_SIGN (itype); |
29587d84 | 3603 | REAL_VALUE_TYPE imin, imax; |
3604 | real_from_integer (&imin, fmt, wi::min_value (itype), isign); | |
3605 | real_from_integer (&imax, fmt, wi::max_value (itype), isign); | |
3606 | ||
3607 | REAL_VALUE_TYPE icst; | |
3608 | if (cmp == GT_EXPR || cmp == GE_EXPR) | |
3609 | real_ceil (&icst, fmt, cst); | |
3610 | else if (cmp == LT_EXPR || cmp == LE_EXPR) | |
3611 | real_floor (&icst, fmt, cst); | |
3612 | else | |
3613 | real_trunc (&icst, fmt, cst); | |
3614 | ||
8360e5df | 3615 | bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst); |
29587d84 | 3616 | |
3617 | bool overflow_p = false; | |
3618 | wide_int icst_val | |
3619 | = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype)); | |
3620 | } | |
3621 | (switch | |
3622 | /* Optimize cases when CST is outside of ITYPE's range. */ | |
3623 | (if (real_compare (LT_EXPR, cst, &imin)) | |
3624 | { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR, | |
3625 | type); }) | |
3626 | (if (real_compare (GT_EXPR, cst, &imax)) | |
3627 | { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR, | |
3628 | type); }) | |
3629 | /* Remove cast if CST is an integer representable by ITYPE. */ | |
3630 | (if (cst_int_p) | |
3631 | (cmp @0 { gcc_assert (!overflow_p); | |
3632 | wide_int_to_tree (itype, icst_val); }) | |
3633 | ) | |
3634 | /* When CST is fractional, optimize | |
3635 | (FTYPE) N == CST -> 0 | |
3636 | (FTYPE) N != CST -> 1. */ | |
3637 | (if (cmp == EQ_EXPR || cmp == NE_EXPR) | |
9603b585 | 3638 | { constant_boolean_node (cmp == NE_EXPR, type); }) |
29587d84 | 3639 | /* Otherwise replace with sensible integer constant. */ |
3640 | (with | |
3641 | { | |
3642 | gcc_checking_assert (!overflow_p); | |
3643 | } | |
3644 | (icmp @0 { wide_int_to_tree (itype, icst_val); }))))))))) | |
3645 | ||
d040acf9 | 3646 | /* Fold A /[ex] B CMP C to A CMP B * C. */ |
3647 | (for cmp (eq ne) | |
3648 | (simplify | |
3649 | (cmp (exact_div @0 @1) INTEGER_CST@2) | |
3650 | (if (!integer_zerop (@1)) | |
e3d0f65c | 3651 | (if (wi::to_wide (@2) == 0) |
d040acf9 | 3652 | (cmp @0 @2) |
3653 | (if (TREE_CODE (@1) == INTEGER_CST) | |
3654 | (with | |
3655 | { | |
30b5769f | 3656 | wi::overflow_type ovf; |
e3d0f65c | 3657 | wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1), |
3658 | TYPE_SIGN (TREE_TYPE (@1)), &ovf); | |
d040acf9 | 3659 | } |
3660 | (if (ovf) | |
3661 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
3662 | (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))))) | |
3663 | (for cmp (lt le gt ge) | |
3664 | (simplify | |
3665 | (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2) | |
e3d0f65c | 3666 | (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))) |
d040acf9 | 3667 | (with |
3668 | { | |
30b5769f | 3669 | wi::overflow_type ovf; |
e3d0f65c | 3670 | wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1), |
3671 | TYPE_SIGN (TREE_TYPE (@1)), &ovf); | |
d040acf9 | 3672 | } |
3673 | (if (ovf) | |
e3d0f65c | 3674 | { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0, |
3675 | TYPE_SIGN (TREE_TYPE (@2))) | |
d040acf9 | 3676 | != (cmp == LT_EXPR || cmp == LE_EXPR), type); } |
3677 | (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))) | |
3678 | ||
952ba7dd | 3679 | /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0. |
3680 | ||
3681 | For small C (less than max/B), this is (size_t)A CMP (size_t)B * C. | |
3682 | For large C (more than min/B+2^size), this is also true, with the | |
3683 | multiplication computed modulo 2^size. | |
3684 | For intermediate C, this just tests the sign of A. */ | |
3685 | (for cmp (lt le gt ge) | |
3686 | cmp2 (ge ge lt lt) | |
3687 | (simplify | |
3688 | (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2) | |
3689 | (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)) | |
3690 | && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
3691 | && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))) | |
3692 | (with | |
3693 | { | |
3694 | tree utype = TREE_TYPE (@2); | |
3695 | wide_int denom = wi::to_wide (@1); | |
3696 | wide_int right = wi::to_wide (@2); | |
3697 | wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom); | |
3698 | wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom); | |
3699 | bool small = wi::leu_p (right, smax); | |
3700 | bool large = wi::geu_p (right, smin); | |
3701 | } | |
3702 | (if (small || large) | |
3703 | (cmp (convert:utype @0) (mult @2 (convert @1))) | |
3704 | (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })))))) | |
3705 | ||
57d742ac | 3706 | /* Unordered tests if either argument is a NaN. */ |
3707 | (simplify | |
3708 | (bit_ior (unordered @0 @0) (unordered @1 @1)) | |
b1306f12 | 3709 | (if (types_match (@0, @1)) |
57d742ac | 3710 | (unordered @0 @1))) |
c59372d1 | 3711 | (simplify |
3712 | (bit_and (ordered @0 @0) (ordered @1 @1)) | |
3713 | (if (types_match (@0, @1)) | |
3714 | (ordered @0 @1))) | |
57d742ac | 3715 | (simplify |
3716 | (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1)) | |
3717 | @2) | |
c59372d1 | 3718 | (simplify |
3719 | (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1)) | |
3720 | @2) | |
f8dad9b4 | 3721 | |
f9ec342f | 3722 | /* Simple range test simplifications. */ |
3723 | /* A < B || A >= B -> true. */ | |
7717d90b | 3724 | (for test1 (lt le le le ne ge) |
3725 | test2 (ge gt ge ne eq ne) | |
f9ec342f | 3726 | (simplify |
3727 | (bit_ior:c (test1 @0 @1) (test2 @0 @1)) | |
3728 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3729 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) | |
3730 | { constant_boolean_node (true, type); }))) | |
3731 | /* A < B && A >= B -> false. */ | |
3732 | (for test1 (lt lt lt le ne eq) | |
3733 | test2 (ge gt eq gt eq gt) | |
3734 | (simplify | |
3735 | (bit_and:c (test1 @0 @1) (test2 @0 @1)) | |
3736 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3737 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) | |
3738 | { constant_boolean_node (false, type); }))) | |
3739 | ||
77b822cc | 3740 | /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0 |
3741 | A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0 | |
3742 | ||
3743 | Note that comparisons | |
3744 | A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0 | |
3745 | A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0 | |
3746 | will be canonicalized to above so there's no need to | |
3747 | consider them here. | |
3748 | */ | |
3749 | ||
3750 | (for cmp (le gt) | |
3751 | eqcmp (eq ne) | |
3752 | (simplify | |
3753 | (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3) | |
3754 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
3755 | (with | |
3756 | { | |
3757 | tree ty = TREE_TYPE (@0); | |
3758 | unsigned prec = TYPE_PRECISION (ty); | |
3759 | wide_int mask = wi::to_wide (@2, prec); | |
3760 | wide_int rhs = wi::to_wide (@3, prec); | |
3761 | signop sgn = TYPE_SIGN (ty); | |
3762 | } | |
3763 | (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn) | |
3764 | && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn)) | |
3765 | (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); }) | |
3766 | { build_zero_cst (ty); })))))) | |
3767 | ||
0c3c84e3 | 3768 | /* -A CMP -B -> B CMP A. */ |
3769 | (for cmp (tcc_comparison) | |
3770 | scmp (swapped_tcc_comparison) | |
3771 | (simplify | |
3772 | (cmp (negate @0) (negate @1)) | |
3773 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
3774 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3775 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
3776 | (scmp @0 @1))) | |
3777 | (simplify | |
3778 | (cmp (negate @0) CONSTANT_CLASS_P@1) | |
3779 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
3780 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3781 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
0808dfce | 3782 | (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); } |
0c3c84e3 | 3783 | (if (tem && !TREE_OVERFLOW (tem)) |
3784 | (scmp @0 { tem; })))))) | |
3785 | ||
06e4870e | 3786 | /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */ |
3787 | (for op (eq ne) | |
3788 | (simplify | |
3789 | (op (abs @0) zerop@1) | |
3790 | (op @0 @1))) | |
3791 | ||
3e39e829 | 3792 | /* From fold_sign_changed_comparison and fold_widened_comparison. |
3793 | FIXME: the lack of symmetry is disturbing. */ | |
7725cd5b | 3794 | (for cmp (simple_comparison) |
3795 | (simplify | |
3796 | (cmp (convert@0 @00) (convert?@1 @10)) | |
7550d855 | 3797 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
7725cd5b | 3798 | /* Disable this optimization if we're casting a function pointer |
3799 | type on targets that require function pointer canonicalization. */ | |
3800 | && !(targetm.have_canonicalize_funcptr_for_compare () | |
b60c096b | 3801 | && ((POINTER_TYPE_P (TREE_TYPE (@00)) |
3802 | && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00)))) | |
3803 | || (POINTER_TYPE_P (TREE_TYPE (@10)) | |
3804 | && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10)))))) | |
4e503e6f | 3805 | && single_use (@0)) |
7725cd5b | 3806 | (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0)) |
3807 | && (TREE_CODE (@10) == INTEGER_CST | |
3e39e829 | 3808 | || @1 != @10) |
7725cd5b | 3809 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0)) |
3810 | || cmp == NE_EXPR | |
3811 | || cmp == EQ_EXPR) | |
3e39e829 | 3812 | && !POINTER_TYPE_P (TREE_TYPE (@00))) |
7725cd5b | 3813 | /* ??? The special-casing of INTEGER_CST conversion was in the original |
3814 | code and here to avoid a spurious overflow flag on the resulting | |
3815 | constant which fold_convert produces. */ | |
3816 | (if (TREE_CODE (@1) == INTEGER_CST) | |
3817 | (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0, | |
3818 | TREE_OVERFLOW (@1)); }) | |
3819 | (cmp @00 (convert @1))) | |
3820 | ||
3821 | (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00))) | |
3822 | /* If possible, express the comparison in the shorter mode. */ | |
3823 | (if ((cmp == EQ_EXPR || cmp == NE_EXPR | |
7079d57a | 3824 | || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)) |
3825 | || (!TYPE_UNSIGNED (TREE_TYPE (@0)) | |
3826 | && TYPE_UNSIGNED (TREE_TYPE (@00)))) | |
7725cd5b | 3827 | && (types_match (TREE_TYPE (@10), TREE_TYPE (@00)) |
3828 | || ((TYPE_PRECISION (TREE_TYPE (@00)) | |
3829 | >= TYPE_PRECISION (TREE_TYPE (@10))) | |
3830 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) | |
3831 | == TYPE_UNSIGNED (TREE_TYPE (@10)))) | |
3832 | || (TREE_CODE (@10) == INTEGER_CST | |
5f223778 | 3833 | && INTEGRAL_TYPE_P (TREE_TYPE (@00)) |
7725cd5b | 3834 | && int_fits_type_p (@10, TREE_TYPE (@00))))) |
3835 | (cmp @00 (convert @10)) | |
3836 | (if (TREE_CODE (@10) == INTEGER_CST | |
5f223778 | 3837 | && INTEGRAL_TYPE_P (TREE_TYPE (@00)) |
7725cd5b | 3838 | && !int_fits_type_p (@10, TREE_TYPE (@00))) |
3839 | (with | |
3840 | { | |
3841 | tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
3842 | tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
3843 | bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10)); | |
3844 | bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min)); | |
3845 | } | |
3846 | (if (above || below) | |
3847 | (if (cmp == EQ_EXPR || cmp == NE_EXPR) | |
3848 | { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); } | |
3849 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
3850 | { constant_boolean_node (above ? true : false, type); } | |
3851 | (if (cmp == GT_EXPR || cmp == GE_EXPR) | |
3852 | { constant_boolean_node (above ? false : true, type); })))))))))))) | |
69693ea7 | 3853 | |
8218bff7 | 3854 | (for cmp (eq ne) |
3855 | /* A local variable can never be pointed to by | |
3856 | the default SSA name of an incoming parameter. | |
3857 | SSA names are canonicalized to 2nd place. */ | |
3858 | (simplify | |
3859 | (cmp addr@0 SSA_NAME@1) | |
3860 | (if (SSA_NAME_IS_DEFAULT_DEF (@1) | |
3861 | && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL) | |
3862 | (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); } | |
3863 | (if (TREE_CODE (base) == VAR_DECL | |
3864 | && auto_var_in_fn_p (base, current_function_decl)) | |
3865 | (if (cmp == NE_EXPR) | |
3866 | { constant_boolean_node (true, type); } | |
3867 | { constant_boolean_node (false, type); })))))) | |
3868 | ||
69693ea7 | 3869 | /* Equality compare simplifications from fold_binary */ |
3870 | (for cmp (eq ne) | |
3871 | ||
3872 | /* If we have (A | C) == D where C & ~D != 0, convert this into 0. | |
3873 | Similarly for NE_EXPR. */ | |
3874 | (simplify | |
3875 | (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2) | |
3876 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
e3d0f65c | 3877 | && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0) |
69693ea7 | 3878 | { constant_boolean_node (cmp == NE_EXPR, type); })) |
3879 | ||
3880 | /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ | |
3881 | (simplify | |
3882 | (cmp (bit_xor @0 @1) integer_zerop) | |
3883 | (cmp @0 @1)) | |
3884 | ||
3885 | /* (X ^ Y) == Y becomes X == 0. | |
3886 | Likewise (X ^ Y) == X becomes Y == 0. */ | |
3887 | (simplify | |
6b440285 | 3888 | (cmp:c (bit_xor:c @0 @1) @0) |
69693ea7 | 3889 | (cmp @1 { build_zero_cst (TREE_TYPE (@1)); })) |
3890 | ||
3891 | /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ | |
3892 | (simplify | |
3893 | (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2) | |
3894 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))) | |
f1054432 | 3895 | (cmp @0 (bit_xor @1 (convert @2))))) |
f1054432 | 3896 | |
3897 | (simplify | |
3898 | (cmp (convert? addr@0) integer_zerop) | |
3899 | (if (tree_single_nonzero_warnv_p (@0, NULL)) | |
3900 | { constant_boolean_node (cmp == NE_EXPR, type); }))) | |
3901 | ||
06e4870e | 3902 | /* If we have (A & C) == C where C is a power of 2, convert this into |
3903 | (A & C) != 0. Similarly for NE_EXPR. */ | |
3904 | (for cmp (eq ne) | |
3905 | icmp (ne eq) | |
3906 | (simplify | |
3907 | (cmp (bit_and@2 @0 integer_pow2p@1) @1) | |
3908 | (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) | |
9603b585 | 3909 | |
458de25d | 3910 | /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2, |
3911 | convert this into a shift followed by ANDing with D. */ | |
3912 | (simplify | |
3913 | (cond | |
3914 | (ne (bit_and @0 integer_pow2p@1) integer_zerop) | |
9859f662 | 3915 | INTEGER_CST@2 integer_zerop) |
3916 | (if (integer_pow2p (@2)) | |
3917 | (with { | |
3918 | int shift = (wi::exact_log2 (wi::to_wide (@2)) | |
3919 | - wi::exact_log2 (wi::to_wide (@1))); | |
3920 | } | |
3921 | (if (shift > 0) | |
3922 | (bit_and | |
3923 | (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2) | |
3924 | (bit_and | |
3925 | (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) | |
3926 | @2))))) | |
458de25d | 3927 | |
06e4870e | 3928 | /* If we have (A & C) != 0 where C is the sign bit of A, convert |
3929 | this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ | |
3930 | (for cmp (eq ne) | |
3931 | ncmp (ge lt) | |
3932 | (simplify | |
3933 | (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop) | |
3934 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
654ba22c | 3935 | && type_has_mode_precision_p (TREE_TYPE (@0)) |
06e4870e | 3936 | && element_precision (@2) >= element_precision (@0) |
e3d0f65c | 3937 | && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0))) |
06e4870e | 3938 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } |
3939 | (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) | |
3940 | ||
458de25d | 3941 | /* If we have A < 0 ? C : 0 where C is a power of 2, convert |
fbfc7874 | 3942 | this into a right shift or sign extension followed by ANDing with C. */ |
458de25d | 3943 | (simplify |
3944 | (cond | |
3945 | (lt @0 integer_zerop) | |
9859f662 | 3946 | INTEGER_CST@1 integer_zerop) |
3947 | (if (integer_pow2p (@1) | |
3948 | && !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
fbfc7874 | 3949 | (with { |
e3d0f65c | 3950 | int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1; |
fbfc7874 | 3951 | } |
3952 | (if (shift >= 0) | |
3953 | (bit_and | |
3954 | (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) | |
3955 | @1) | |
3956 | /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure | |
3957 | sign extension followed by AND with C will achieve the effect. */ | |
3958 | (bit_and (convert @0) @1))))) | |
458de25d | 3959 | |
aa0bf078 | 3960 | /* When the addresses are not directly of decls compare base and offset. |
3961 | This implements some remaining parts of fold_comparison address | |
3962 | comparisons but still no complete part of it. Still it is good | |
3963 | enough to make fold_stmt not regress when not dispatching to fold_binary. */ | |
3964 | (for cmp (simple_comparison) | |
3965 | (simplify | |
c4699ff5 | 3966 | (cmp (convert1?@2 addr@0) (convert2? addr@1)) |
aa0bf078 | 3967 | (with |
3968 | { | |
773078cb | 3969 | poly_int64 off0, off1; |
aa0bf078 | 3970 | tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0); |
3971 | tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1); | |
3972 | if (base0 && TREE_CODE (base0) == MEM_REF) | |
3973 | { | |
90ca1268 | 3974 | off0 += mem_ref_offset (base0).force_shwi (); |
aa0bf078 | 3975 | base0 = TREE_OPERAND (base0, 0); |
3976 | } | |
3977 | if (base1 && TREE_CODE (base1) == MEM_REF) | |
3978 | { | |
90ca1268 | 3979 | off1 += mem_ref_offset (base1).force_shwi (); |
aa0bf078 | 3980 | base1 = TREE_OPERAND (base1, 0); |
3981 | } | |
3982 | } | |
5e0b3d2f | 3983 | (if (base0 && base1) |
3984 | (with | |
3985 | { | |
84ad0bc1 | 3986 | int equal = 2; |
9c8aeb66 | 3987 | /* Punt in GENERIC on variables with value expressions; |
3988 | the value expressions might point to fields/elements | |
3989 | of other vars etc. */ | |
3990 | if (GENERIC | |
3991 | && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0)) | |
3992 | || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1)))) | |
3993 | ; | |
3994 | else if (decl_in_symtab_p (base0) | |
3995 | && decl_in_symtab_p (base1)) | |
5e0b3d2f | 3996 | equal = symtab_node::get_create (base0) |
3997 | ->equal_address_to (symtab_node::get_create (base1)); | |
04a37b12 | 3998 | else if ((DECL_P (base0) |
3999 | || TREE_CODE (base0) == SSA_NAME | |
4000 | || TREE_CODE (base0) == STRING_CST) | |
4001 | && (DECL_P (base1) | |
4002 | || TREE_CODE (base1) == SSA_NAME | |
4003 | || TREE_CODE (base1) == STRING_CST)) | |
84ad0bc1 | 4004 | equal = (base0 == base1); |
15934f6e | 4005 | if (equal == 0) |
4006 | { | |
8bdbe403 | 4007 | HOST_WIDE_INT ioff0 = -1, ioff1 = -1; |
4008 | off0.is_constant (&ioff0); | |
4009 | off1.is_constant (&ioff1); | |
4010 | if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST) | |
4011 | || (TREE_CODE (base0) == STRING_CST && DECL_P (base1)) | |
4012 | || (TREE_CODE (base0) == STRING_CST | |
4013 | && TREE_CODE (base1) == STRING_CST | |
4014 | && ioff0 >= 0 && ioff1 >= 0 | |
4015 | && ioff0 < TREE_STRING_LENGTH (base0) | |
4016 | && ioff1 < TREE_STRING_LENGTH (base1) | |
4017 | /* This is a too conservative test that the STRING_CSTs | |
4018 | will not end up being string-merged. */ | |
4019 | && strncmp (TREE_STRING_POINTER (base0) + ioff0, | |
4020 | TREE_STRING_POINTER (base1) + ioff1, | |
4021 | MIN (TREE_STRING_LENGTH (base0) - ioff0, | |
4022 | TREE_STRING_LENGTH (base1) - ioff1)) != 0)) | |
4023 | ; | |
4024 | else if (!DECL_P (base0) || !DECL_P (base1)) | |
15934f6e | 4025 | equal = 2; |
4026 | else if (cmp != EQ_EXPR && cmp != NE_EXPR) | |
4027 | equal = 2; | |
4028 | /* If this is a pointer comparison, ignore for now even | |
4029 | valid equalities where one pointer is the offset zero | |
4030 | of one object and the other to one past end of another one. */ | |
4031 | else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2))) | |
4032 | ; | |
4033 | /* Assume that automatic variables can't be adjacent to global | |
4034 | variables. */ | |
4035 | else if (is_global_var (base0) != is_global_var (base1)) | |
4036 | ; | |
4037 | else | |
4038 | { | |
4039 | tree sz0 = DECL_SIZE_UNIT (base0); | |
4040 | tree sz1 = DECL_SIZE_UNIT (base1); | |
4041 | /* If sizes are unknown, e.g. VLA or not representable, | |
4042 | punt. */ | |
4043 | if (!tree_fits_poly_int64_p (sz0) | |
4044 | || !tree_fits_poly_int64_p (sz1)) | |
4045 | equal = 2; | |
4046 | else | |
4047 | { | |
4048 | poly_int64 size0 = tree_to_poly_int64 (sz0); | |
4049 | poly_int64 size1 = tree_to_poly_int64 (sz1); | |
4050 | /* If one offset is pointing (or could be) to the beginning | |
4051 | of one object and the other is pointing to one past the | |
4052 | last byte of the other object, punt. */ | |
4053 | if (maybe_eq (off0, 0) && maybe_eq (off1, size1)) | |
4054 | equal = 2; | |
4055 | else if (maybe_eq (off1, 0) && maybe_eq (off0, size0)) | |
4056 | equal = 2; | |
4057 | /* If both offsets are the same, there are some cases | |
4058 | we know that are ok. Either if we know they aren't | |
4059 | zero, or if we know both sizes are no zero. */ | |
4060 | if (equal == 2 | |
4061 | && known_eq (off0, off1) | |
4062 | && (known_ne (off0, 0) | |
4063 | || (known_ne (size0, 0) && known_ne (size1, 0)))) | |
4064 | equal = 0; | |
4065 | } | |
4066 | } | |
4067 | } | |
5e0b3d2f | 4068 | } |
977361a1 | 4069 | (if (equal == 1 |
4070 | && (cmp == EQ_EXPR || cmp == NE_EXPR | |
4071 | /* If the offsets are equal we can ignore overflow. */ | |
4072 | || known_eq (off0, off1) | |
4073 | || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
4074 | /* Or if we compare using pointers to decls or strings. */ | |
4075 | || (POINTER_TYPE_P (TREE_TYPE (@2)) | |
4076 | && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))) | |
5e0b3d2f | 4077 | (switch |
773078cb | 4078 | (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1))) |
4079 | { constant_boolean_node (known_eq (off0, off1), type); }) | |
4080 | (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1))) | |
4081 | { constant_boolean_node (known_ne (off0, off1), type); }) | |
4082 | (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1))) | |
4083 | { constant_boolean_node (known_lt (off0, off1), type); }) | |
4084 | (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1))) | |
4085 | { constant_boolean_node (known_le (off0, off1), type); }) | |
4086 | (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1))) | |
4087 | { constant_boolean_node (known_ge (off0, off1), type); }) | |
4088 | (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1))) | |
4089 | { constant_boolean_node (known_gt (off0, off1), type); })) | |
15934f6e | 4090 | (if (equal == 0) |
4091 | (switch | |
4092 | (if (cmp == EQ_EXPR) | |
4093 | { constant_boolean_node (false, type); }) | |
4094 | (if (cmp == NE_EXPR) | |
4095 | { constant_boolean_node (true, type); }))))))))) | |
69693ea7 | 4096 | |
73447cc5 | 4097 | /* Simplify pointer equality compares using PTA. */ |
4098 | (for neeq (ne eq) | |
4099 | (simplify | |
4100 | (neeq @0 @1) | |
4101 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
4102 | && ptrs_compare_unequal (@0, @1)) | |
326e3d4e | 4103 | { constant_boolean_node (neeq != EQ_EXPR, type); }))) |
73447cc5 | 4104 | |
b2930f09 | 4105 | /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST. |
c91ee73b | 4106 | and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST. |
4107 | Disable the transform if either operand is pointer to function. | |
4108 | This broke pr22051-2.c for arm where function pointer | |
4109 | canonicalizaion is not wanted. */ | |
a2f87f25 | 4110 | |
b2930f09 | 4111 | (for cmp (ne eq) |
4112 | (simplify | |
4113 | (cmp (convert @0) INTEGER_CST@1) | |
ba3186de | 4114 | (if (((POINTER_TYPE_P (TREE_TYPE (@0)) |
4115 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0))) | |
4116 | && INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
4117 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
4118 | && POINTER_TYPE_P (TREE_TYPE (@1)) | |
4119 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1))))) | |
4120 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
b2930f09 | 4121 | (cmp @0 (convert @1))))) |
4122 | ||
0a45b027 | 4123 | /* Non-equality compare simplifications from fold_binary */ |
4124 | (for cmp (lt gt le ge) | |
4125 | /* Comparisons with the highest or lowest possible integer of | |
4126 | the specified precision will have known values. */ | |
4127 | (simplify | |
7931f9ca | 4128 | (cmp (convert?@2 @0) uniform_integer_cst_p@1) |
4129 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
4130 | || POINTER_TYPE_P (TREE_TYPE (@1)) | |
4131 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1))) | |
0a45b027 | 4132 | && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))) |
4133 | (with | |
4134 | { | |
7931f9ca | 4135 | tree cst = uniform_integer_cst_p (@1); |
4136 | tree arg1_type = TREE_TYPE (cst); | |
0a45b027 | 4137 | unsigned int prec = TYPE_PRECISION (arg1_type); |
4138 | wide_int max = wi::max_value (arg1_type); | |
4139 | wide_int signed_max = wi::max_value (prec, SIGNED); | |
4140 | wide_int min = wi::min_value (arg1_type); | |
4141 | } | |
4142 | (switch | |
7931f9ca | 4143 | (if (wi::to_wide (cst) == max) |
0a45b027 | 4144 | (switch |
4145 | (if (cmp == GT_EXPR) | |
4146 | { constant_boolean_node (false, type); }) | |
4147 | (if (cmp == GE_EXPR) | |
4148 | (eq @2 @1)) | |
4149 | (if (cmp == LE_EXPR) | |
4150 | { constant_boolean_node (true, type); }) | |
4151 | (if (cmp == LT_EXPR) | |
4152 | (ne @2 @1)))) | |
7931f9ca | 4153 | (if (wi::to_wide (cst) == min) |
0a45b027 | 4154 | (switch |
4155 | (if (cmp == LT_EXPR) | |
4156 | { constant_boolean_node (false, type); }) | |
4157 | (if (cmp == LE_EXPR) | |
4158 | (eq @2 @1)) | |
4159 | (if (cmp == GE_EXPR) | |
4160 | { constant_boolean_node (true, type); }) | |
4161 | (if (cmp == GT_EXPR) | |
4162 | (ne @2 @1)))) | |
7931f9ca | 4163 | (if (wi::to_wide (cst) == max - 1) |
39e21ca8 | 4164 | (switch |
4165 | (if (cmp == GT_EXPR) | |
7931f9ca | 4166 | (eq @2 { build_uniform_cst (TREE_TYPE (@1), |
4167 | wide_int_to_tree (TREE_TYPE (cst), | |
4168 | wi::to_wide (cst) | |
4169 | + 1)); })) | |
39e21ca8 | 4170 | (if (cmp == LE_EXPR) |
7931f9ca | 4171 | (ne @2 { build_uniform_cst (TREE_TYPE (@1), |
4172 | wide_int_to_tree (TREE_TYPE (cst), | |
4173 | wi::to_wide (cst) | |
4174 | + 1)); })))) | |
4175 | (if (wi::to_wide (cst) == min + 1) | |
0a45b027 | 4176 | (switch |
4177 | (if (cmp == GE_EXPR) | |
7931f9ca | 4178 | (ne @2 { build_uniform_cst (TREE_TYPE (@1), |
4179 | wide_int_to_tree (TREE_TYPE (cst), | |
4180 | wi::to_wide (cst) | |
4181 | - 1)); })) | |
0a45b027 | 4182 | (if (cmp == LT_EXPR) |
7931f9ca | 4183 | (eq @2 { build_uniform_cst (TREE_TYPE (@1), |
4184 | wide_int_to_tree (TREE_TYPE (cst), | |
4185 | wi::to_wide (cst) | |
4186 | - 1)); })))) | |
4187 | (if (wi::to_wide (cst) == signed_max | |
0a45b027 | 4188 | && TYPE_UNSIGNED (arg1_type) |
4189 | /* We will flip the signedness of the comparison operator | |
4190 | associated with the mode of @1, so the sign bit is | |
4191 | specified by this mode. Check that @1 is the signed | |
4192 | max associated with this sign bit. */ | |
03b7a719 | 4193 | && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type)) |
0a45b027 | 4194 | /* signed_type does not work on pointer types. */ |
4195 | && INTEGRAL_TYPE_P (arg1_type)) | |
4196 | /* The following case also applies to X < signed_max+1 | |
4197 | and X >= signed_max+1 because previous transformations. */ | |
4198 | (if (cmp == LE_EXPR || cmp == GT_EXPR) | |
7931f9ca | 4199 | (with { tree st = signed_type_for (TREE_TYPE (@1)); } |
4200 | (switch | |
4201 | (if (cst == @1 && cmp == LE_EXPR) | |
4202 | (ge (convert:st @0) { build_zero_cst (st); })) | |
4203 | (if (cst == @1 && cmp == GT_EXPR) | |
4204 | (lt (convert:st @0) { build_zero_cst (st); })) | |
4205 | (if (cmp == LE_EXPR) | |
4206 | (ge (view_convert:st @0) { build_zero_cst (st); })) | |
4207 | (if (cmp == GT_EXPR) | |
4208 | (lt (view_convert:st @0) { build_zero_cst (st); }))))))))))) | |
9603b585 | 4209 | |
56b4132e | 4210 | (for cmp (unordered ordered unlt unle ungt unge uneq ltgt) |
4211 | /* If the second operand is NaN, the result is constant. */ | |
4212 | (simplify | |
4213 | (cmp @0 REAL_CST@1) | |
4214 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
4215 | && (cmp != LTGT_EXPR || ! flag_trapping_math)) | |
05999bfc | 4216 | { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR |
56b4132e | 4217 | ? false : true, type); }))) |
0a45b027 | 4218 | |
004b4ca3 | 4219 | /* bool_var != 0 becomes bool_var. */ |
4220 | (simplify | |
56b4132e | 4221 | (ne @0 integer_zerop) |
004b4ca3 | 4222 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
4223 | && types_match (type, TREE_TYPE (@0))) | |
4224 | (non_lvalue @0))) | |
4225 | /* bool_var == 1 becomes bool_var. */ | |
4226 | (simplify | |
56b4132e | 4227 | (eq @0 integer_onep) |
004b4ca3 | 4228 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
4229 | && types_match (type, TREE_TYPE (@0))) | |
4230 | (non_lvalue @0))) | |
56b4132e | 4231 | /* Do not handle |
4232 | bool_var == 0 becomes !bool_var or | |
4233 | bool_var != 1 becomes !bool_var | |
4234 | here because that only is good in assignment context as long | |
4235 | as we require a tcc_comparison in GIMPLE_CONDs where we'd | |
4236 | replace if (x == 0) with tem = ~x; if (tem != 0) which is | |
4237 | clearly less optimal and which we'll transform again in forwprop. */ | |
004b4ca3 | 4238 | |
8c51f92f | 4239 | /* When one argument is a constant, overflow detection can be simplified. |
4240 | Currently restricted to single use so as not to interfere too much with | |
4241 | ADD_OVERFLOW detection in tree-ssa-math-opts.c. | |
4242 | A + CST CMP A -> A CMP' CST' */ | |
4243 | (for cmp (lt le ge gt) | |
4244 | out (gt gt le le) | |
4245 | (simplify | |
a1a80007 | 4246 | (cmp:c (plus@2 @0 INTEGER_CST@1) @0) |
8c51f92f | 4247 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
4248 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) | |
e3d0f65c | 4249 | && wi::to_wide (@1) != 0 |
8c51f92f | 4250 | && single_use (@2)) |
e3d0f65c | 4251 | (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); } |
4252 | (out @0 { wide_int_to_tree (TREE_TYPE (@0), | |
4253 | wi::max_value (prec, UNSIGNED) | |
4254 | - wi::to_wide (@1)); }))))) | |
8c51f92f | 4255 | |
dfba0905 | 4256 | /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A. |
4257 | However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c | |
4258 | expects the long form, so we restrict the transformation for now. */ | |
4259 | (for cmp (gt le) | |
4260 | (simplify | |
a1a80007 | 4261 | (cmp:c (minus@2 @0 @1) @0) |
dfba0905 | 4262 | (if (single_use (@2) |
4263 | && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
4264 | && TYPE_UNSIGNED (TREE_TYPE (@0)) | |
4265 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
4266 | (cmp @1 @0)))) | |
dfba0905 | 4267 | |
4268 | /* Testing for overflow is unnecessary if we already know the result. */ | |
dfba0905 | 4269 | /* A - B > A */ |
4270 | (for cmp (gt le) | |
4271 | out (ne eq) | |
4272 | (simplify | |
a1a80007 | 4273 | (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0) |
dfba0905 | 4274 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
4275 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
4276 | (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) | |
4277 | /* A + B < A */ | |
4278 | (for cmp (lt ge) | |
4279 | out (ne eq) | |
4280 | (simplify | |
a1a80007 | 4281 | (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0) |
dfba0905 | 4282 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
4283 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
4284 | (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) | |
4285 | ||
8928f8d3 | 4286 | /* For unsigned operands, -1 / B < A checks whether A * B would overflow. |
765557c0 | 4287 | Simplify it to __builtin_mul_overflow (A, B, <unused>). */ |
765557c0 | 4288 | (for cmp (lt ge) |
4289 | out (ne eq) | |
4290 | (simplify | |
8928f8d3 | 4291 | (cmp:c (trunc_div:s integer_all_onesp @1) @0) |
765557c0 | 4292 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0))) |
4293 | (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); } | |
4294 | (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); }))))) | |
004b4ca3 | 4295 | |
a153e7b3 | 4296 | /* Simplification of math builtins. These rules must all be optimizations |
4297 | as well as IL simplifications. If there is a possibility that the new | |
4298 | form could be a pessimization, the rule should go in the canonicalization | |
4299 | section that follows this one. | |
f8dad9b4 | 4300 | |
a153e7b3 | 4301 | Rules can generally go in this section if they satisfy one of |
4302 | the following: | |
4303 | ||
4304 | - the rule describes an identity | |
4305 | ||
4306 | - the rule replaces calls with something as simple as addition or | |
4307 | multiplication | |
4308 | ||
4309 | - the rule contains unary calls only and simplifies the surrounding | |
4310 | arithmetic. (The idea here is to exclude non-unary calls in which | |
4311 | one operand is constant and in which the call is known to be cheap | |
4312 | when the operand has that value.) */ | |
e09fc89c | 4313 | |
a153e7b3 | 4314 | (if (flag_unsafe_math_optimizations) |
e09fc89c | 4315 | /* Simplify sqrt(x) * sqrt(x) -> x. */ |
4316 | (simplify | |
054e9558 | 4317 | (mult (SQRT_ALL@1 @0) @1) |
e09fc89c | 4318 | (if (!HONOR_SNANS (type)) |
4319 | @0)) | |
4320 | ||
402ad641 | 4321 | (for op (plus minus) |
4322 | /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */ | |
4323 | (simplify | |
4324 | (op (rdiv @0 @1) | |
4325 | (rdiv @2 @1)) | |
4326 | (rdiv (op @0 @2) @1))) | |
4327 | ||
7abecd46 | 4328 | (for cmp (lt le gt ge) |
4329 | neg_cmp (gt ge lt le) | |
4330 | /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */ | |
4331 | (simplify | |
4332 | (cmp (mult @0 REAL_CST@1) REAL_CST@2) | |
4333 | (with | |
4334 | { tree tem = const_binop (RDIV_EXPR, type, @2, @1); } | |
4335 | (if (tem | |
4336 | && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem)) | |
4337 | || (real_zerop (tem) && !real_zerop (@1)))) | |
4338 | (switch | |
4339 | (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1))) | |
4340 | (cmp @0 { tem; })) | |
4341 | (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0)) | |
4342 | (neg_cmp @0 { tem; }))))))) | |
4343 | ||
b036e74b | 4344 | /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */ |
4345 | (for root (SQRT CBRT) | |
4346 | (simplify | |
4347 | (mult (root:s @0) (root:s @1)) | |
4348 | (root (mult @0 @1)))) | |
4349 | ||
b036e74b | 4350 | /* Simplify expN(x) * expN(y) -> expN(x+y). */ |
4351 | (for exps (EXP EXP2 EXP10 POW10) | |
4352 | (simplify | |
4353 | (mult (exps:s @0) (exps:s @1)) | |
4354 | (exps (plus @0 @1)))) | |
4355 | ||
e09fc89c | 4356 | /* Simplify a/root(b/c) into a*root(c/b). */ |
b036e74b | 4357 | (for root (SQRT CBRT) |
4358 | (simplify | |
4359 | (rdiv @0 (root:s (rdiv:s @1 @2))) | |
4360 | (mult @0 (root (rdiv @2 @1))))) | |
4361 | ||
4362 | /* Simplify x/expN(y) into x*expN(-y). */ | |
4363 | (for exps (EXP EXP2 EXP10 POW10) | |
4364 | (simplify | |
4365 | (rdiv @0 (exps:s @1)) | |
4366 | (mult @0 (exps (negate @1))))) | |
e09fc89c | 4367 | |
6b0c8e84 | 4368 | (for logs (LOG LOG2 LOG10 LOG10) |
4369 | exps (EXP EXP2 EXP10 POW10) | |
762f6b3b | 4370 | /* logN(expN(x)) -> x. */ |
f8dad9b4 | 4371 | (simplify |
4372 | (logs (exps @0)) | |
762f6b3b | 4373 | @0) |
4374 | /* expN(logN(x)) -> x. */ | |
4375 | (simplify | |
4376 | (exps (logs @0)) | |
4377 | @0)) | |
a153e7b3 | 4378 | |
f8dad9b4 | 4379 | /* Optimize logN(func()) for various exponential functions. We |
4380 | want to determine the value "x" and the power "exponent" in | |
4381 | order to transform logN(x**exponent) into exponent*logN(x). */ | |
6b0c8e84 | 4382 | (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10) |
4383 | exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2) | |
f8dad9b4 | 4384 | (simplify |
4385 | (logs (exps @0)) | |
3a18d05c | 4386 | (if (SCALAR_FLOAT_TYPE_P (type)) |
4387 | (with { | |
4388 | tree x; | |
4389 | switch (exps) | |
4390 | { | |
4391 | CASE_CFN_EXP: | |
4392 | /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */ | |
4393 | x = build_real_truncate (type, dconst_e ()); | |
4394 | break; | |
4395 | CASE_CFN_EXP2: | |
4396 | /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */ | |
4397 | x = build_real (type, dconst2); | |
4398 | break; | |
4399 | CASE_CFN_EXP10: | |
4400 | CASE_CFN_POW10: | |
4401 | /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */ | |
4402 | { | |
4403 | REAL_VALUE_TYPE dconst10; | |
4404 | real_from_integer (&dconst10, VOIDmode, 10, SIGNED); | |
4405 | x = build_real (type, dconst10); | |
4406 | } | |
4407 | break; | |
4408 | default: | |
4409 | gcc_unreachable (); | |
4410 | } | |
4411 | } | |
4412 | (mult (logs { x; }) @0))))) | |
a153e7b3 | 4413 | |
f8dad9b4 | 4414 | (for logs (LOG LOG |
4415 | LOG2 LOG2 | |
4416 | LOG10 LOG10) | |
4417 | exps (SQRT CBRT) | |
4418 | (simplify | |
4419 | (logs (exps @0)) | |
3a18d05c | 4420 | (if (SCALAR_FLOAT_TYPE_P (type)) |
4421 | (with { | |
4422 | tree x; | |
4423 | switch (exps) | |
4424 | { | |
4425 | CASE_CFN_SQRT: | |
4426 | /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */ | |
4427 | x = build_real (type, dconsthalf); | |
4428 | break; | |
4429 | CASE_CFN_CBRT: | |
4430 | /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */ | |
4431 | x = build_real_truncate (type, dconst_third ()); | |
4432 | break; | |
4433 | default: | |
4434 | gcc_unreachable (); | |
4435 | } | |
4436 | } | |
4437 | (mult { x; } (logs @0)))))) | |
a153e7b3 | 4438 | |
4439 | /* logN(pow(x,exponent)) -> exponent*logN(x). */ | |
f8dad9b4 | 4440 | (for logs (LOG LOG2 LOG10) |
4441 | pows (POW) | |
4442 | (simplify | |
4443 | (logs (pows @0 @1)) | |
a153e7b3 | 4444 | (mult @1 (logs @0)))) |
4445 | ||
a35ca444 | 4446 | /* pow(C,x) -> exp(log(C)*x) if C > 0, |
4447 | or if C is a positive power of 2, | |
4448 | pow(C,x) -> exp2(log2(C)*x). */ | |
91674b2a | 4449 | #if GIMPLE |
6b0b3b89 | 4450 | (for pows (POW) |
4451 | exps (EXP) | |
4452 | logs (LOG) | |
a35ca444 | 4453 | exp2s (EXP2) |
4454 | log2s (LOG2) | |
6b0b3b89 | 4455 | (simplify |
4456 | (pows REAL_CST@0 @1) | |
a35ca444 | 4457 | (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0) |
d38b0287 | 4458 | && real_isfinite (TREE_REAL_CST_PTR (@0)) |
4459 | /* As libmvec doesn't have a vectorized exp2, defer optimizing | |
4460 | the use_exp2 case until after vectorization. It seems actually | |
4461 | beneficial for all constants to postpone this until later, | |
4462 | because exp(log(C)*x), while faster, will have worse precision | |
4463 | and if x folds into a constant too, that is unnecessary | |
4464 | pessimization. */ | |
4465 | && canonicalize_math_after_vectorization_p ()) | |
a35ca444 | 4466 | (with { |
4467 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0); | |
4468 | bool use_exp2 = false; | |
4469 | if (targetm.libc_has_function (function_c99_misc) | |
4470 | && value->cl == rvc_normal) | |
4471 | { | |
4472 | REAL_VALUE_TYPE frac_rvt = *value; | |
4473 | SET_REAL_EXP (&frac_rvt, 1); | |
4474 | if (real_equal (&frac_rvt, &dconst1)) | |
4475 | use_exp2 = true; | |
4476 | } | |
4477 | } | |
4478 | (if (!use_exp2) | |
91674b2a | 4479 | (if (optimize_pow_to_exp (@0, @1)) |
4480 | (exps (mult (logs @0) @1))) | |
d38b0287 | 4481 | (exp2s (mult (log2s @0) @1))))))) |
91674b2a | 4482 | #endif |
6b0b3b89 | 4483 | |
8664f262 | 4484 | /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */ |
4485 | (for pows (POW) | |
4486 | exps (EXP EXP2 EXP10 POW10) | |
4487 | logs (LOG LOG2 LOG10 LOG10) | |
4488 | (simplify | |
4489 | (mult:c (pows:s REAL_CST@0 @1) (exps:s @2)) | |
4490 | (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0) | |
4491 | && real_isfinite (TREE_REAL_CST_PTR (@0))) | |
4492 | (exps (plus (mult (logs @0) @1) @2))))) | |
4493 | ||
a153e7b3 | 4494 | (for sqrts (SQRT) |
4495 | cbrts (CBRT) | |
cef15d09 | 4496 | pows (POW) |
a153e7b3 | 4497 | exps (EXP EXP2 EXP10 POW10) |
4498 | /* sqrt(expN(x)) -> expN(x*0.5). */ | |
4499 | (simplify | |
4500 | (sqrts (exps @0)) | |
4501 | (exps (mult @0 { build_real (type, dconsthalf); }))) | |
4502 | /* cbrt(expN(x)) -> expN(x/3). */ | |
4503 | (simplify | |
4504 | (cbrts (exps @0)) | |
cef15d09 | 4505 | (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))) |
4506 | /* pow(expN(x), y) -> expN(x*y). */ | |
4507 | (simplify | |
4508 | (pows (exps @0) @1) | |
4509 | (exps (mult @0 @1)))) | |
390c3cff | 4510 | |
4511 | /* tan(atan(x)) -> x. */ | |
4512 | (for tans (TAN) | |
4513 | atans (ATAN) | |
4514 | (simplify | |
4515 | (tans (atans @0)) | |
4516 | @0))) | |
a153e7b3 | 4517 | |
985ceb32 | 4518 | /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */ |
4519 | (for sins (SIN) | |
4520 | atans (ATAN) | |
4521 | sqrts (SQRT) | |
4522 | copysigns (COPYSIGN) | |
4523 | (simplify | |
4524 | (sins (atans:s @0)) | |
4525 | (with | |
4526 | { | |
4527 | REAL_VALUE_TYPE r_cst; | |
4528 | build_sinatan_real (&r_cst, type); | |
4529 | tree t_cst = build_real (type, r_cst); | |
4530 | tree t_one = build_one_cst (type); | |
4531 | } | |
4532 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
b7d73df0 | 4533 | (cond (lt (abs @0) { t_cst; }) |
985ceb32 | 4534 | (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; }))) |
4535 | (copysigns { t_one; } @0)))))) | |
4536 | ||
4537 | /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */ | |
4538 | (for coss (COS) | |
4539 | atans (ATAN) | |
4540 | sqrts (SQRT) | |
4541 | copysigns (COPYSIGN) | |
4542 | (simplify | |
4543 | (coss (atans:s @0)) | |
4544 | (with | |
4545 | { | |
4546 | REAL_VALUE_TYPE r_cst; | |
4547 | build_sinatan_real (&r_cst, type); | |
4548 | tree t_cst = build_real (type, r_cst); | |
4549 | tree t_one = build_one_cst (type); | |
4550 | tree t_zero = build_zero_cst (type); | |
4551 | } | |
4552 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
b7d73df0 | 4553 | (cond (lt (abs @0) { t_cst; }) |
985ceb32 | 4554 | (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; }))) |
4555 | (copysigns { t_zero; } @0)))))) | |
4556 | ||
fcc6ff28 | 4557 | (if (!flag_errno_math) |
4558 | /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */ | |
4559 | (for sinhs (SINH) | |
4560 | atanhs (ATANH) | |
4561 | sqrts (SQRT) | |
4562 | (simplify | |
4563 | (sinhs (atanhs:s @0)) | |
4564 | (with { tree t_one = build_one_cst (type); } | |
4565 | (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))) | |
4566 | ||
4567 | /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */ | |
4568 | (for coshs (COSH) | |
4569 | atanhs (ATANH) | |
4570 | sqrts (SQRT) | |
4571 | (simplify | |
4572 | (coshs (atanhs:s @0)) | |
4573 | (with { tree t_one = build_one_cst (type); } | |
4574 | (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))) | |
4575 | ||
d064d976 | 4576 | /* cabs(x+0i) or cabs(0+xi) -> abs(x). */ |
4577 | (simplify | |
2f99fc04 | 4578 | (CABS (complex:C @0 real_zerop@1)) |
d064d976 | 4579 | (abs @0)) |
4580 | ||
6f5f406a | 4581 | /* trunc(trunc(x)) -> trunc(x), etc. */ |
054e9558 | 4582 | (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL) |
6f5f406a | 4583 | (simplify |
4584 | (fns (fns @0)) | |
4585 | (fns @0))) | |
4586 | /* f(x) -> x if x is integer valued and f does nothing for such values. */ | |
054e9558 | 4587 | (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL) |
6f5f406a | 4588 | (simplify |
4589 | (fns integer_valued_real_p@0) | |
4590 | @0)) | |
6f5f406a | 4591 | |
35c5f68c | 4592 | /* hypot(x,0) and hypot(0,x) -> abs(x). */ |
4593 | (simplify | |
3a18d05c | 4594 | (HYPOT:c @0 real_zerop@1) |
35c5f68c | 4595 | (abs @0)) |
4596 | ||
cef15d09 | 4597 | /* pow(1,x) -> 1. */ |
4598 | (simplify | |
4599 | (POW real_onep@0 @1) | |
4600 | @0) | |
4601 | ||
66dc9fcd | 4602 | (simplify |
4603 | /* copysign(x,x) -> x. */ | |
054e9558 | 4604 | (COPYSIGN_ALL @0 @0) |
66dc9fcd | 4605 | @0) |
4606 | ||
4607 | (simplify | |
4608 | /* copysign(x,y) -> fabs(x) if y is nonnegative. */ | |
054e9558 | 4609 | (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1) |
66dc9fcd | 4610 | (abs @0)) |
4611 | ||
7d705d57 | 4612 | (for scale (LDEXP SCALBN SCALBLN) |
4613 | /* ldexp(0, x) -> 0. */ | |
4614 | (simplify | |
4615 | (scale real_zerop@0 @1) | |
4616 | @0) | |
4617 | /* ldexp(x, 0) -> x. */ | |
4618 | (simplify | |
4619 | (scale @0 integer_zerop@1) | |
4620 | @0) | |
4621 | /* ldexp(x, y) -> x if x is +-Inf or NaN. */ | |
4622 | (simplify | |
4623 | (scale REAL_CST@0 @1) | |
4624 | (if (!real_isfinite (TREE_REAL_CST_PTR (@0))) | |
4625 | @0))) | |
4626 | ||
a153e7b3 | 4627 | /* Canonicalization of sequences of math builtins. These rules represent |
4628 | IL simplifications but are not necessarily optimizations. | |
4629 | ||
4630 | The sincos pass is responsible for picking "optimal" implementations | |
4631 | of math builtins, which may be more complicated and can sometimes go | |
4632 | the other way, e.g. converting pow into a sequence of sqrts. | |
4633 | We only want to do these canonicalizations before the pass has run. */ | |
4634 | ||
4635 | (if (flag_unsafe_math_optimizations && canonicalize_math_p ()) | |
4636 | /* Simplify tan(x) * cos(x) -> sin(x). */ | |
4637 | (simplify | |
4638 | (mult:c (TAN:s @0) (COS:s @0)) | |
4639 | (SIN @0)) | |
4640 | ||
4641 | /* Simplify x * pow(x,c) -> pow(x,c+1). */ | |
4642 | (simplify | |
cbcde149 | 4643 | (mult:c @0 (POW:s @0 REAL_CST@1)) |
a153e7b3 | 4644 | (if (!TREE_OVERFLOW (@1)) |
4645 | (POW @0 (plus @1 { build_one_cst (type); })))) | |
4646 | ||
4647 | /* Simplify sin(x) / cos(x) -> tan(x). */ | |
4648 | (simplify | |
4649 | (rdiv (SIN:s @0) (COS:s @0)) | |
4650 | (TAN @0)) | |
4651 | ||
4652 | /* Simplify cos(x) / sin(x) -> 1 / tan(x). */ | |
4653 | (simplify | |
4654 | (rdiv (COS:s @0) (SIN:s @0)) | |
4655 | (rdiv { build_one_cst (type); } (TAN @0))) | |
4656 | ||
4657 | /* Simplify sin(x) / tan(x) -> cos(x). */ | |
4658 | (simplify | |
4659 | (rdiv (SIN:s @0) (TAN:s @0)) | |
4660 | (if (! HONOR_NANS (@0) | |
4661 | && ! HONOR_INFINITIES (@0)) | |
3a18d05c | 4662 | (COS @0))) |
a153e7b3 | 4663 | |
4664 | /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */ | |
4665 | (simplify | |
4666 | (rdiv (TAN:s @0) (SIN:s @0)) | |
4667 | (if (! HONOR_NANS (@0) | |
4668 | && ! HONOR_INFINITIES (@0)) | |
4669 | (rdiv { build_one_cst (type); } (COS @0)))) | |
4670 | ||
4671 | /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */ | |
4672 | (simplify | |
4673 | (mult (POW:s @0 @1) (POW:s @0 @2)) | |
4674 | (POW @0 (plus @1 @2))) | |
4675 | ||
4676 | /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */ | |
4677 | (simplify | |
4678 | (mult (POW:s @0 @1) (POW:s @2 @1)) | |
4679 | (POW (mult @0 @2) @1)) | |
4680 | ||
cbcde149 | 4681 | /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */ |
4682 | (simplify | |
4683 | (mult (POWI:s @0 @1) (POWI:s @2 @1)) | |
4684 | (POWI (mult @0 @2) @1)) | |
4685 | ||
a153e7b3 | 4686 | /* Simplify pow(x,c) / x -> pow(x,c-1). */ |
4687 | (simplify | |
4688 | (rdiv (POW:s @0 REAL_CST@1) @0) | |
4689 | (if (!TREE_OVERFLOW (@1)) | |
4690 | (POW @0 (minus @1 { build_one_cst (type); })))) | |
4691 | ||
4692 | /* Simplify x / pow (y,z) -> x * pow(y,-z). */ | |
4693 | (simplify | |
4694 | (rdiv @0 (POW:s @1 @2)) | |
4695 | (mult @0 (POW @1 (negate @2)))) | |
4696 | ||
4697 | (for sqrts (SQRT) | |
4698 | cbrts (CBRT) | |
4699 | pows (POW) | |
4700 | /* sqrt(sqrt(x)) -> pow(x,1/4). */ | |
4701 | (simplify | |
4702 | (sqrts (sqrts @0)) | |
4703 | (pows @0 { build_real (type, dconst_quarter ()); })) | |
4704 | /* sqrt(cbrt(x)) -> pow(x,1/6). */ | |
4705 | (simplify | |
4706 | (sqrts (cbrts @0)) | |
4707 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
4708 | /* cbrt(sqrt(x)) -> pow(x,1/6). */ | |
4709 | (simplify | |
4710 | (cbrts (sqrts @0)) | |
4711 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
4712 | /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */ | |
4713 | (simplify | |
4714 | (cbrts (cbrts tree_expr_nonnegative_p@0)) | |
4715 | (pows @0 { build_real_truncate (type, dconst_ninth ()); })) | |
4716 | /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */ | |
4717 | (simplify | |
4718 | (sqrts (pows @0 @1)) | |
4719 | (pows (abs @0) (mult @1 { build_real (type, dconsthalf); }))) | |
4720 | /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */ | |
4721 | (simplify | |
4722 | (cbrts (pows tree_expr_nonnegative_p@0 @1)) | |
cef15d09 | 4723 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) |
4724 | /* pow(sqrt(x),y) -> pow(x,y*0.5). */ | |
4725 | (simplify | |
4726 | (pows (sqrts @0) @1) | |
4727 | (pows @0 (mult @1 { build_real (type, dconsthalf); }))) | |
4728 | /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */ | |
4729 | (simplify | |
4730 | (pows (cbrts tree_expr_nonnegative_p@0) @1) | |
4731 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) | |
4732 | /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */ | |
4733 | (simplify | |
4734 | (pows (pows tree_expr_nonnegative_p@0 @1) @2) | |
4735 | (pows @0 (mult @1 @2)))) | |
d064d976 | 4736 | |
4737 | /* cabs(x+xi) -> fabs(x)*sqrt(2). */ | |
4738 | (simplify | |
4739 | (CABS (complex @0 @0)) | |
5e324b84 | 4740 | (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) |
4741 | ||
35c5f68c | 4742 | /* hypot(x,x) -> fabs(x)*sqrt(2). */ |
4743 | (simplify | |
4744 | (HYPOT @0 @0) | |
4745 | (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) | |
4746 | ||
5e324b84 | 4747 | /* cexp(x+yi) -> exp(x)*cexpi(y). */ |
4748 | (for cexps (CEXP) | |
4749 | exps (EXP) | |
4750 | cexpis (CEXPI) | |
4751 | (simplify | |
4752 | (cexps compositional_complex@0) | |
4753 | (if (targetm.libc_has_function (function_c99_math_complex)) | |
4754 | (complex | |
4755 | (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0)))) | |
4756 | (mult @1 (imagpart @2))))))) | |
f8dad9b4 | 4757 | |
6f5f406a | 4758 | (if (canonicalize_math_p ()) |
4759 | /* floor(x) -> trunc(x) if x is nonnegative. */ | |
054e9558 | 4760 | (for floors (FLOOR_ALL) |
4761 | truncs (TRUNC_ALL) | |
6f5f406a | 4762 | (simplify |
4763 | (floors tree_expr_nonnegative_p@0) | |
4764 | (truncs @0)))) | |
4765 | ||
4766 | (match double_value_p | |
4767 | @0 | |
4768 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node))) | |
4769 | (for froms (BUILT_IN_TRUNCL | |
4770 | BUILT_IN_FLOORL | |
4771 | BUILT_IN_CEILL | |
4772 | BUILT_IN_ROUNDL | |
4773 | BUILT_IN_NEARBYINTL | |
4774 | BUILT_IN_RINTL) | |
4775 | tos (BUILT_IN_TRUNC | |
4776 | BUILT_IN_FLOOR | |
4777 | BUILT_IN_CEIL | |
4778 | BUILT_IN_ROUND | |
4779 | BUILT_IN_NEARBYINT | |
4780 | BUILT_IN_RINT) | |
4781 | /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */ | |
4782 | (if (optimize && canonicalize_math_p ()) | |
4783 | (simplify | |
4784 | (froms (convert double_value_p@0)) | |
4785 | (convert (tos @0))))) | |
4786 | ||
4787 | (match float_value_p | |
4788 | @0 | |
4789 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node))) | |
4790 | (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC | |
4791 | BUILT_IN_FLOORL BUILT_IN_FLOOR | |
4792 | BUILT_IN_CEILL BUILT_IN_CEIL | |
4793 | BUILT_IN_ROUNDL BUILT_IN_ROUND | |
4794 | BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT | |
4795 | BUILT_IN_RINTL BUILT_IN_RINT) | |
4796 | tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF | |
4797 | BUILT_IN_FLOORF BUILT_IN_FLOORF | |
4798 | BUILT_IN_CEILF BUILT_IN_CEILF | |
4799 | BUILT_IN_ROUNDF BUILT_IN_ROUNDF | |
4800 | BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF | |
4801 | BUILT_IN_RINTF BUILT_IN_RINTF) | |
4802 | /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc., | |
4803 | if x is a float. */ | |
92159af9 | 4804 | (if (optimize && canonicalize_math_p () |
4805 | && targetm.libc_has_function (function_c99_misc)) | |
6f5f406a | 4806 | (simplify |
4807 | (froms (convert float_value_p@0)) | |
4808 | (convert (tos @0))))) | |
4809 | ||
1117ed35 | 4810 | (for froms (XFLOORL XCEILL XROUNDL XRINTL) |
4811 | tos (XFLOOR XCEIL XROUND XRINT) | |
4812 | /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */ | |
4813 | (if (optimize && canonicalize_math_p ()) | |
4814 | (simplify | |
4815 | (froms (convert double_value_p@0)) | |
4816 | (tos @0)))) | |
4817 | ||
4818 | (for froms (XFLOORL XCEILL XROUNDL XRINTL | |
4819 | XFLOOR XCEIL XROUND XRINT) | |
4820 | tos (XFLOORF XCEILF XROUNDF XRINTF) | |
4821 | /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc., | |
4822 | if x is a float. */ | |
4823 | (if (optimize && canonicalize_math_p ()) | |
4824 | (simplify | |
4825 | (froms (convert float_value_p@0)) | |
4826 | (tos @0)))) | |
4827 | ||
4828 | (if (canonicalize_math_p ()) | |
4829 | /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */ | |
4830 | (for floors (IFLOOR LFLOOR LLFLOOR) | |
4831 | (simplify | |
4832 | (floors tree_expr_nonnegative_p@0) | |
4833 | (fix_trunc @0)))) | |
4834 | ||
4835 | (if (canonicalize_math_p ()) | |
4836 | /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */ | |
4837 | (for fns (IFLOOR LFLOOR LLFLOOR | |
4838 | ICEIL LCEIL LLCEIL | |
4839 | IROUND LROUND LLROUND) | |
4840 | (simplify | |
4841 | (fns integer_valued_real_p@0) | |
4842 | (fix_trunc @0))) | |
4843 | (if (!flag_errno_math) | |
4844 | /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */ | |
4845 | (for rints (IRINT LRINT LLRINT) | |
4846 | (simplify | |
4847 | (rints integer_valued_real_p@0) | |
4848 | (fix_trunc @0))))) | |
4849 | ||
4850 | (if (canonicalize_math_p ()) | |
4851 | (for ifn (IFLOOR ICEIL IROUND IRINT) | |
4852 | lfn (LFLOOR LCEIL LROUND LRINT) | |
4853 | llfn (LLFLOOR LLCEIL LLROUND LLRINT) | |
4854 | /* Canonicalize iround (x) to lround (x) on ILP32 targets where | |
4855 | sizeof (int) == sizeof (long). */ | |
4856 | (if (TYPE_PRECISION (integer_type_node) | |
4857 | == TYPE_PRECISION (long_integer_type_node)) | |
4858 | (simplify | |
4859 | (ifn @0) | |
4860 | (lfn:long_integer_type_node @0))) | |
4861 | /* Canonicalize llround (x) to lround (x) on LP64 targets where | |
4862 | sizeof (long long) == sizeof (long). */ | |
4863 | (if (TYPE_PRECISION (long_long_integer_type_node) | |
4864 | == TYPE_PRECISION (long_integer_type_node)) | |
4865 | (simplify | |
4866 | (llfn @0) | |
4867 | (lfn:long_integer_type_node @0))))) | |
4868 | ||
92f3c1b2 | 4869 | /* cproj(x) -> x if we're ignoring infinities. */ |
4870 | (simplify | |
4871 | (CPROJ @0) | |
4872 | (if (!HONOR_INFINITIES (type)) | |
4873 | @0)) | |
4874 | ||
ec11da34 | 4875 | /* If the real part is inf and the imag part is known to be |
4876 | nonnegative, return (inf + 0i). */ | |
4877 | (simplify | |
4878 | (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1)) | |
4879 | (if (real_isinf (TREE_REAL_CST_PTR (@0))) | |
92f3c1b2 | 4880 | { build_complex_inf (type, false); })) |
4881 | ||
ec11da34 | 4882 | /* If the imag part is inf, return (inf+I*copysign(0,imag)). */ |
4883 | (simplify | |
4884 | (CPROJ (complex @0 REAL_CST@1)) | |
4885 | (if (real_isinf (TREE_REAL_CST_PTR (@1))) | |
92f3c1b2 | 4886 | { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); })) |
ec11da34 | 4887 | |
cef15d09 | 4888 | (for pows (POW) |
4889 | sqrts (SQRT) | |
4890 | cbrts (CBRT) | |
4891 | (simplify | |
4892 | (pows @0 REAL_CST@1) | |
4893 | (with { | |
4894 | const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1); | |
4895 | REAL_VALUE_TYPE tmp; | |
4896 | } | |
4897 | (switch | |
4898 | /* pow(x,0) -> 1. */ | |
4899 | (if (real_equal (value, &dconst0)) | |
4900 | { build_real (type, dconst1); }) | |
4901 | /* pow(x,1) -> x. */ | |
4902 | (if (real_equal (value, &dconst1)) | |
4903 | @0) | |
4904 | /* pow(x,-1) -> 1/x. */ | |
4905 | (if (real_equal (value, &dconstm1)) | |
4906 | (rdiv { build_real (type, dconst1); } @0)) | |
4907 | /* pow(x,0.5) -> sqrt(x). */ | |
4908 | (if (flag_unsafe_math_optimizations | |
4909 | && canonicalize_math_p () | |
4910 | && real_equal (value, &dconsthalf)) | |
4911 | (sqrts @0)) | |
4912 | /* pow(x,1/3) -> cbrt(x). */ | |
4913 | (if (flag_unsafe_math_optimizations | |
4914 | && canonicalize_math_p () | |
4915 | && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()), | |
4916 | real_equal (value, &tmp))) | |
4917 | (cbrts @0)))))) | |
ec11da34 | 4918 | |
ff190980 | 4919 | /* powi(1,x) -> 1. */ |
4920 | (simplify | |
4921 | (POWI real_onep@0 @1) | |
4922 | @0) | |
4923 | ||
4924 | (simplify | |
4925 | (POWI @0 INTEGER_CST@1) | |
4926 | (switch | |
4927 | /* powi(x,0) -> 1. */ | |
e3d0f65c | 4928 | (if (wi::to_wide (@1) == 0) |
ff190980 | 4929 | { build_real (type, dconst1); }) |
4930 | /* powi(x,1) -> x. */ | |
e3d0f65c | 4931 | (if (wi::to_wide (@1) == 1) |
ff190980 | 4932 | @0) |
4933 | /* powi(x,-1) -> 1/x. */ | |
e3d0f65c | 4934 | (if (wi::to_wide (@1) == -1) |
ff190980 | 4935 | (rdiv { build_real (type, dconst1); } @0)))) |
4936 | ||
9603b585 | 4937 | /* Narrowing of arithmetic and logical operations. |
eaa6752e | 4938 | |
4939 | These are conceptually similar to the transformations performed for | |
4940 | the C/C++ front-ends by shorten_binary_op and shorten_compare. Long | |
4941 | term we want to move all that code out of the front-ends into here. */ | |
4942 | ||
f3abf7fd | 4943 | /* Convert (outertype)((innertype0)a+(innertype1)b) |
4944 | into ((newtype)a+(newtype)b) where newtype | |
4945 | is the widest mode from all of these. */ | |
4946 | (for op (plus minus mult rdiv) | |
4947 | (simplify | |
4948 | (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2))) | |
4949 | /* If we have a narrowing conversion of an arithmetic operation where | |
4950 | both operands are widening conversions from the same type as the outer | |
4951 | narrowing conversion. Then convert the innermost operands to a | |
4952 | suitable unsigned type (to avoid introducing undefined behavior), | |
4953 | perform the operation and convert the result to the desired type. */ | |
4954 | (if (INTEGRAL_TYPE_P (type) | |
4955 | && op != MULT_EXPR | |
4956 | && op != RDIV_EXPR | |
4957 | /* We check for type compatibility between @0 and @1 below, | |
4958 | so there's no need to check that @2/@4 are integral types. */ | |
4959 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
4960 | && INTEGRAL_TYPE_P (TREE_TYPE (@3)) | |
4961 | /* The precision of the type of each operand must match the | |
4962 | precision of the mode of each operand, similarly for the | |
4963 | result. */ | |
4964 | && type_has_mode_precision_p (TREE_TYPE (@1)) | |
4965 | && type_has_mode_precision_p (TREE_TYPE (@2)) | |
4966 | && type_has_mode_precision_p (type) | |
4967 | /* The inner conversion must be a widening conversion. */ | |
4968 | && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1)) | |
4969 | && types_match (@1, type) | |
4970 | && (types_match (@1, @2) | |
4971 | /* Or the second operand is const integer or converted const | |
4972 | integer from valueize. */ | |
4973 | || TREE_CODE (@2) == INTEGER_CST)) | |
4974 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
4975 | (op @1 (convert @2)) | |
4976 | (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); } | |
4977 | (convert (op (convert:utype @1) | |
4978 | (convert:utype @2))))) | |
4979 | (if (FLOAT_TYPE_P (type) | |
4980 | && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)) | |
4981 | == DECIMAL_FLOAT_TYPE_P (type)) | |
4982 | (with { tree arg0 = strip_float_extensions (@1); | |
4983 | tree arg1 = strip_float_extensions (@2); | |
4984 | tree itype = TREE_TYPE (@0); | |
4985 | tree ty1 = TREE_TYPE (arg0); | |
4986 | tree ty2 = TREE_TYPE (arg1); | |
4987 | enum tree_code code = TREE_CODE (itype); } | |
4988 | (if (FLOAT_TYPE_P (ty1) | |
4989 | && FLOAT_TYPE_P (ty2)) | |
4990 | (with { tree newtype = type; | |
4991 | if (TYPE_MODE (ty1) == SDmode | |
4992 | || TYPE_MODE (ty2) == SDmode | |
4993 | || TYPE_MODE (type) == SDmode) | |
4994 | newtype = dfloat32_type_node; | |
4995 | if (TYPE_MODE (ty1) == DDmode | |
4996 | || TYPE_MODE (ty2) == DDmode | |
4997 | || TYPE_MODE (type) == DDmode) | |
4998 | newtype = dfloat64_type_node; | |
4999 | if (TYPE_MODE (ty1) == TDmode | |
5000 | || TYPE_MODE (ty2) == TDmode | |
5001 | || TYPE_MODE (type) == TDmode) | |
5002 | newtype = dfloat128_type_node; } | |
5003 | (if ((newtype == dfloat32_type_node | |
5004 | || newtype == dfloat64_type_node | |
5005 | || newtype == dfloat128_type_node) | |
5006 | && newtype == type | |
5007 | && types_match (newtype, type)) | |
5008 | (op (convert:newtype @1) (convert:newtype @2)) | |
5009 | (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype)) | |
5010 | newtype = ty1; | |
5011 | if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype)) | |
5012 | newtype = ty2; } | |
5013 | /* Sometimes this transformation is safe (cannot | |
5014 | change results through affecting double rounding | |
5015 | cases) and sometimes it is not. If NEWTYPE is | |
5016 | wider than TYPE, e.g. (float)((long double)double | |
5017 | + (long double)double) converted to | |
5018 | (float)(double + double), the transformation is | |
5019 | unsafe regardless of the details of the types | |
5020 | involved; double rounding can arise if the result | |
5021 | of NEWTYPE arithmetic is a NEWTYPE value half way | |
5022 | between two representable TYPE values but the | |
5023 | exact value is sufficiently different (in the | |
5024 | right direction) for this difference to be | |
5025 | visible in ITYPE arithmetic. If NEWTYPE is the | |
5026 | same as TYPE, however, the transformation may be | |
5027 | safe depending on the types involved: it is safe | |
5028 | if the ITYPE has strictly more than twice as many | |
5029 | mantissa bits as TYPE, can represent infinities | |
5030 | and NaNs if the TYPE can, and has sufficient | |
5031 | exponent range for the product or ratio of two | |
5032 | values representable in the TYPE to be within the | |
5033 | range of normal values of ITYPE. */ | |
5034 | (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype) | |
5035 | && (flag_unsafe_math_optimizations | |
5036 | || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type) | |
5037 | && real_can_shorten_arithmetic (TYPE_MODE (itype), | |
5038 | TYPE_MODE (type)) | |
5039 | && !excess_precision_type (newtype))) | |
5040 | && !types_match (itype, newtype)) | |
5041 | (convert:type (op (convert:newtype @1) | |
5042 | (convert:newtype @2))) | |
5043 | )))) ) | |
5044 | )) | |
5045 | ))) | |
8cd9143e | 5046 | |
5047 | /* This is another case of narrowing, specifically when there's an outer | |
5048 | BIT_AND_EXPR which masks off bits outside the type of the innermost | |
5049 | operands. Like the previous case we have to convert the operands | |
67cf9b55 | 5050 | to unsigned types to avoid introducing undefined behavior for the |
8cd9143e | 5051 | arithmetic operation. */ |
5052 | (for op (minus plus) | |
d74b7335 | 5053 | (simplify |
5054 | (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4) | |
5055 | (if (INTEGRAL_TYPE_P (type) | |
5056 | /* We check for type compatibility between @0 and @1 below, | |
5057 | so there's no need to check that @1/@3 are integral types. */ | |
5058 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5059 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
5060 | /* The precision of the type of each operand must match the | |
5061 | precision of the mode of each operand, similarly for the | |
5062 | result. */ | |
654ba22c | 5063 | && type_has_mode_precision_p (TREE_TYPE (@0)) |
5064 | && type_has_mode_precision_p (TREE_TYPE (@1)) | |
5065 | && type_has_mode_precision_p (type) | |
d74b7335 | 5066 | /* The inner conversion must be a widening conversion. */ |
5067 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
5068 | && types_match (@0, @1) | |
5069 | && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0))) | |
5070 | <= TYPE_PRECISION (TREE_TYPE (@0))) | |
e3d0f65c | 5071 | && (wi::to_wide (@4) |
5072 | & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)), | |
5073 | true, TYPE_PRECISION (type))) == 0) | |
d74b7335 | 5074 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) |
5075 | (with { tree ntype = TREE_TYPE (@0); } | |
5076 | (convert (bit_and (op @0 @1) (convert:ntype @4)))) | |
5077 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
5078 | (convert (bit_and (op (convert:utype @0) (convert:utype @1)) | |
5079 | (convert:utype @4)))))))) | |
6a8b7746 | 5080 | |
9603b585 | 5081 | /* Transform (@0 < @1 and @0 < @2) to use min, |
6a8b7746 | 5082 | (@0 > @1 and @0 > @2) to use max */ |
2e933017 | 5083 | (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior) |
5084 | op (lt le gt ge lt le gt ge ) | |
5085 | ext (min min max max max max min min ) | |
6a8b7746 | 5086 | (simplify |
2e933017 | 5087 | (logic (op:cs @0 @1) (op:cs @0 @2)) |
0c1f7d17 | 5088 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
5089 | && TREE_CODE (@0) != INTEGER_CST) | |
6a8b7746 | 5090 | (op @0 (ext @1 @2))))) |
5091 | ||
01156bcb | 5092 | (simplify |
5093 | /* signbit(x) -> 0 if x is nonnegative. */ | |
5094 | (SIGNBIT tree_expr_nonnegative_p@0) | |
5095 | { integer_zero_node; }) | |
5096 | ||
5097 | (simplify | |
5098 | /* signbit(x) -> x<0 if x doesn't have signed zeros. */ | |
5099 | (SIGNBIT @0) | |
5100 | (if (!HONOR_SIGNED_ZEROS (@0)) | |
5101 | (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); })))) | |
eb1a077c | 5102 | |
5103 | /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */ | |
5104 | (for cmp (eq ne) | |
5105 | (for op (plus minus) | |
5106 | rop (minus plus) | |
5107 | (simplify | |
5108 | (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) | |
5109 | (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) | |
5110 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) | |
5111 | && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0)) | |
5112 | && !TYPE_SATURATING (TREE_TYPE (@0))) | |
5113 | (with { tree res = int_const_binop (rop, @2, @1); } | |
af8ad81e | 5114 | (if (TREE_OVERFLOW (res) |
5115 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
eb1a077c | 5116 | { constant_boolean_node (cmp == NE_EXPR, type); } |
5117 | (if (single_use (@3)) | |
07b69d3f | 5118 | (cmp @0 { TREE_OVERFLOW (res) |
5119 | ? drop_tree_overflow (res) : res; })))))))) | |
eb1a077c | 5120 | (for cmp (lt le gt ge) |
5121 | (for op (plus minus) | |
5122 | rop (minus plus) | |
5123 | (simplify | |
5124 | (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) | |
5125 | (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) | |
5126 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
5127 | (with { tree res = int_const_binop (rop, @2, @1); } | |
5128 | (if (TREE_OVERFLOW (res)) | |
5129 | { | |
5130 | fold_overflow_warning (("assuming signed overflow does not occur " | |
5131 | "when simplifying conditional to constant"), | |
5132 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
5133 | bool less = cmp == LE_EXPR || cmp == LT_EXPR; | |
5134 | /* wi::ges_p (@2, 0) should be sufficient for a signed type. */ | |
e3d0f65c | 5135 | bool ovf_high = wi::lt_p (wi::to_wide (@1), 0, |
5136 | TYPE_SIGN (TREE_TYPE (@1))) | |
eb1a077c | 5137 | != (op == MINUS_EXPR); |
5138 | constant_boolean_node (less == ovf_high, type); | |
5139 | } | |
5140 | (if (single_use (@3)) | |
5141 | (with | |
5142 | { | |
5143 | fold_overflow_warning (("assuming signed overflow does not occur " | |
5144 | "when changing X +- C1 cmp C2 to " | |
5145 | "X cmp C2 -+ C1"), | |
5146 | WARN_STRICT_OVERFLOW_COMPARISON); | |
5147 | } | |
5148 | (cmp @0 { res; }))))))))) | |
26280bcd | 5149 | |
5150 | /* Canonicalizations of BIT_FIELD_REFs. */ | |
5151 | ||
1826cc19 | 5152 | (simplify |
5153 | (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4) | |
5154 | (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); })) | |
5155 | ||
5156 | (simplify | |
5157 | (BIT_FIELD_REF (view_convert @0) @1 @2) | |
5158 | (BIT_FIELD_REF @0 @1 @2)) | |
5159 | ||
5160 | (simplify | |
5161 | (BIT_FIELD_REF @0 @1 integer_zerop) | |
5162 | (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0)))) | |
5163 | (view_convert @0))) | |
5164 | ||
26280bcd | 5165 | (simplify |
5166 | (BIT_FIELD_REF @0 @1 @2) | |
5167 | (switch | |
5168 | (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE | |
5169 | && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
5170 | (switch | |
5171 | (if (integer_zerop (@2)) | |
5172 | (view_convert (realpart @0))) | |
5173 | (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
5174 | (view_convert (imagpart @0))))) | |
5175 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5176 | && INTEGRAL_TYPE_P (type) | |
a83b7b63 | 5177 | /* On GIMPLE this should only apply to register arguments. */ |
5178 | && (! GIMPLE || is_gimple_reg (@0)) | |
26280bcd | 5179 | /* A bit-field-ref that referenced the full argument can be stripped. */ |
5180 | && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0 | |
5181 | && integer_zerop (@2)) | |
5182 | /* Low-parts can be reduced to integral conversions. | |
5183 | ??? The following doesn't work for PDP endian. */ | |
5184 | || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN | |
5185 | /* Don't even think about BITS_BIG_ENDIAN. */ | |
5186 | && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0 | |
5187 | && TYPE_PRECISION (type) % BITS_PER_UNIT == 0 | |
5188 | && compare_tree_int (@2, (BYTES_BIG_ENDIAN | |
5189 | ? (TYPE_PRECISION (TREE_TYPE (@0)) | |
5190 | - TYPE_PRECISION (type)) | |
5191 | : 0)) == 0))) | |
5192 | (convert @0)))) | |
5193 | ||
5194 | /* Simplify vector extracts. */ | |
5195 | ||
5196 | (simplify | |
5197 | (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2) | |
5198 | (if (VECTOR_TYPE_P (TREE_TYPE (@0)) | |
5199 | && (types_match (type, TREE_TYPE (TREE_TYPE (@0))) | |
5200 | || (VECTOR_TYPE_P (type) | |
5201 | && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) | |
5202 | (with | |
5203 | { | |
5204 | tree ctor = (TREE_CODE (@0) == SSA_NAME | |
5205 | ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); | |
5206 | tree eltype = TREE_TYPE (TREE_TYPE (ctor)); | |
5207 | unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype)); | |
5208 | unsigned HOST_WIDE_INT n = tree_to_uhwi (@1); | |
5209 | unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2); | |
5210 | } | |
5211 | (if (n != 0 | |
5212 | && (idx % width) == 0 | |
5213 | && (n % width) == 0 | |
f08ee65f | 5214 | && known_le ((idx + n) / width, |
5215 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))) | |
26280bcd | 5216 | (with |
5217 | { | |
5218 | idx = idx / width; | |
5219 | n = n / width; | |
5220 | /* Constructor elements can be subvectors. */ | |
c64de46c | 5221 | poly_uint64 k = 1; |
26280bcd | 5222 | if (CONSTRUCTOR_NELTS (ctor) != 0) |
5223 | { | |
5224 | tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value); | |
5225 | if (TREE_CODE (cons_elem) == VECTOR_TYPE) | |
5226 | k = TYPE_VECTOR_SUBPARTS (cons_elem); | |
5227 | } | |
c64de46c | 5228 | unsigned HOST_WIDE_INT elt, count, const_k; |
26280bcd | 5229 | } |
5230 | (switch | |
5231 | /* We keep an exact subset of the constructor elements. */ | |
c64de46c | 5232 | (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count)) |
26280bcd | 5233 | (if (CONSTRUCTOR_NELTS (ctor) == 0) |
5234 | { build_constructor (type, NULL); } | |
c64de46c | 5235 | (if (count == 1) |
5236 | (if (elt < CONSTRUCTOR_NELTS (ctor)) | |
93b93567 | 5237 | (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; }) |
c64de46c | 5238 | { build_zero_cst (type); }) |
26280bcd | 5239 | { |
c64de46c | 5240 | vec<constructor_elt, va_gc> *vals; |
5241 | vec_alloc (vals, count); | |
5242 | for (unsigned i = 0; | |
5243 | i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i) | |
5244 | CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, | |
5245 | CONSTRUCTOR_ELT (ctor, elt + i)->value); | |
5246 | build_constructor (type, vals); | |
5247 | }))) | |
26280bcd | 5248 | /* The bitfield references a single constructor element. */ |
c64de46c | 5249 | (if (k.is_constant (&const_k) |
5250 | && idx + n <= (idx / const_k + 1) * const_k) | |
26280bcd | 5251 | (switch |
c64de46c | 5252 | (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k) |
26280bcd | 5253 | { build_zero_cst (type); }) |
c64de46c | 5254 | (if (n == const_k) |
93b93567 | 5255 | (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; })) |
c64de46c | 5256 | (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; } |
5257 | @1 { bitsize_int ((idx % const_k) * width); }))))))))) | |
a07b1b15 | 5258 | |
5259 | /* Simplify a bit extraction from a bit insertion for the cases with | |
5260 | the inserted element fully covering the extraction or the insertion | |
5261 | not touching the extraction. */ | |
5262 | (simplify | |
5263 | (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos) | |
5264 | (with | |
5265 | { | |
5266 | unsigned HOST_WIDE_INT isize; | |
5267 | if (INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
5268 | isize = TYPE_PRECISION (TREE_TYPE (@1)); | |
5269 | else | |
5270 | isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1))); | |
5271 | } | |
5272 | (switch | |
e3d0f65c | 5273 | (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos)) |
5274 | && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize), | |
5275 | wi::to_wide (@ipos) + isize)) | |
a07b1b15 | 5276 | (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype, |
e3d0f65c | 5277 | wi::to_wide (@rpos) |
5278 | - wi::to_wide (@ipos)); })) | |
5279 | (if (wi::geu_p (wi::to_wide (@ipos), | |
5280 | wi::to_wide (@rpos) + wi::to_wide (@rsize)) | |
5281 | || wi::geu_p (wi::to_wide (@rpos), | |
5282 | wi::to_wide (@ipos) + isize)) | |
a07b1b15 | 5283 | (BIT_FIELD_REF @0 @rsize @rpos))))) |
143c3c9a | 5284 | |
bdcde03a | 5285 | (if (canonicalize_math_after_vectorization_p ()) |
5286 | (for fmas (FMA) | |
5287 | (simplify | |
5288 | (fmas:c (negate @0) @1 @2) | |
5289 | (IFN_FNMA @0 @1 @2)) | |
5290 | (simplify | |
5291 | (fmas @0 @1 (negate @2)) | |
5292 | (IFN_FMS @0 @1 @2)) | |
5293 | (simplify | |
5294 | (fmas:c (negate @0) @1 (negate @2)) | |
5295 | (IFN_FNMS @0 @1 @2)) | |
5296 | (simplify | |
5297 | (negate (fmas@3 @0 @1 @2)) | |
5298 | (if (single_use (@3)) | |
5299 | (IFN_FNMS @0 @1 @2)))) | |
5300 | ||
143c3c9a | 5301 | (simplify |
bdcde03a | 5302 | (IFN_FMS:c (negate @0) @1 @2) |
5303 | (IFN_FNMS @0 @1 @2)) | |
5304 | (simplify | |
5305 | (IFN_FMS @0 @1 (negate @2)) | |
5306 | (IFN_FMA @0 @1 @2)) | |
5307 | (simplify | |
5308 | (IFN_FMS:c (negate @0) @1 (negate @2)) | |
143c3c9a | 5309 | (IFN_FNMA @0 @1 @2)) |
5310 | (simplify | |
bdcde03a | 5311 | (negate (IFN_FMS@3 @0 @1 @2)) |
5312 | (if (single_use (@3)) | |
5313 | (IFN_FNMA @0 @1 @2))) | |
5314 | ||
5315 | (simplify | |
5316 | (IFN_FNMA:c (negate @0) @1 @2) | |
5317 | (IFN_FMA @0 @1 @2)) | |
143c3c9a | 5318 | (simplify |
bdcde03a | 5319 | (IFN_FNMA @0 @1 (negate @2)) |
143c3c9a | 5320 | (IFN_FNMS @0 @1 @2)) |
5321 | (simplify | |
bdcde03a | 5322 | (IFN_FNMA:c (negate @0) @1 (negate @2)) |
5323 | (IFN_FMS @0 @1 @2)) | |
5324 | (simplify | |
5325 | (negate (IFN_FNMA@3 @0 @1 @2)) | |
143c3c9a | 5326 | (if (single_use (@3)) |
bdcde03a | 5327 | (IFN_FMS @0 @1 @2))) |
143c3c9a | 5328 | |
bdcde03a | 5329 | (simplify |
5330 | (IFN_FNMS:c (negate @0) @1 @2) | |
5331 | (IFN_FMS @0 @1 @2)) | |
5332 | (simplify | |
5333 | (IFN_FNMS @0 @1 (negate @2)) | |
5334 | (IFN_FNMA @0 @1 @2)) | |
5335 | (simplify | |
5336 | (IFN_FNMS:c (negate @0) @1 (negate @2)) | |
5337 | (IFN_FMA @0 @1 @2)) | |
5338 | (simplify | |
5339 | (negate (IFN_FNMS@3 @0 @1 @2)) | |
143c3c9a | 5340 | (if (single_use (@3)) |
bdcde03a | 5341 | (IFN_FMA @0 @1 @2)))) |
d1f6ca14 | 5342 | |
5343 | /* POPCOUNT simplifications. */ | |
5344 | (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL | |
5345 | BUILT_IN_POPCOUNTIMAX) | |
5346 | /* popcount(X&1) is nop_expr(X&1). */ | |
5347 | (simplify | |
5348 | (popcount @0) | |
5349 | (if (tree_nonzero_bits (@0) == 1) | |
5350 | (convert @0))) | |
5351 | /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */ | |
5352 | (simplify | |
5353 | (plus (popcount:s @0) (popcount:s @1)) | |
5354 | (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0) | |
5355 | (popcount (bit_ior @0 @1)))) | |
5356 | /* popcount(X) == 0 is X == 0, and related (in)equalities. */ | |
5357 | (for cmp (le eq ne gt) | |
5358 | rep (eq eq ne ne) | |
5359 | (simplify | |
5360 | (cmp (popcount @0) integer_zerop) | |
5361 | (rep @0 { build_zero_cst (TREE_TYPE (@0)); })))) | |
dbc7e6ae | 5362 | |
5363 | /* Simplify: | |
5364 | ||
5365 | a = a1 op a2 | |
5366 | r = c ? a : b; | |
5367 | ||
5368 | to: | |
5369 | ||
5370 | r = c ? a1 op a2 : b; | |
5371 | ||
5372 | if the target can do it in one go. This makes the operation conditional | |
5373 | on c, so could drop potentially-trapping arithmetic, but that's a valid | |
cf97f636 | 5374 | simplification if the result of the operation isn't needed. |
5375 | ||
5376 | Avoid speculatively generating a stand-alone vector comparison | |
5377 | on targets that might not support them. Any target implementing | |
5378 | conditional internal functions must support the same comparisons | |
5379 | inside and outside a VEC_COND_EXPR. */ | |
5380 | ||
a41f256a | 5381 | #if GIMPLE |
dbc7e6ae | 5382 | (for uncond_op (UNCOND_BINARY) |
5383 | cond_op (COND_BINARY) | |
5384 | (simplify | |
5385 | (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3) | |
cf97f636 | 5386 | (with { tree op_type = TREE_TYPE (@4); } |
5387 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
a41f256a | 5388 | && element_precision (type) == element_precision (op_type)) |
dbc7e6ae | 5389 | (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3)))))) |
5390 | (simplify | |
5391 | (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3))) | |
cf97f636 | 5392 | (with { tree op_type = TREE_TYPE (@4); } |
5393 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
a41f256a | 5394 | && element_precision (type) == element_precision (op_type)) |
dbc7e6ae | 5395 | (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1))))))) |
d8483dd1 | 5396 | |
6682fc02 | 5397 | /* Same for ternary operations. */ |
5398 | (for uncond_op (UNCOND_TERNARY) | |
5399 | cond_op (COND_TERNARY) | |
5400 | (simplify | |
5401 | (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4) | |
cf97f636 | 5402 | (with { tree op_type = TREE_TYPE (@5); } |
5403 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
a41f256a | 5404 | && element_precision (type) == element_precision (op_type)) |
6682fc02 | 5405 | (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4)))))) |
5406 | (simplify | |
5407 | (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4))) | |
cf97f636 | 5408 | (with { tree op_type = TREE_TYPE (@5); } |
5409 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
a41f256a | 5410 | && element_precision (type) == element_precision (op_type)) |
6682fc02 | 5411 | (view_convert (cond_op (bit_not @0) @2 @3 @4 |
5412 | (view_convert:op_type @1))))))) | |
a41f256a | 5413 | #endif |
6682fc02 | 5414 | |
d8483dd1 | 5415 | /* Detect cases in which a VEC_COND_EXPR effectively replaces the |
5416 | "else" value of an IFN_COND_*. */ | |
5417 | (for cond_op (COND_BINARY) | |
5418 | (simplify | |
5419 | (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4) | |
5420 | (with { tree op_type = TREE_TYPE (@3); } | |
5421 | (if (element_precision (type) == element_precision (op_type)) | |
03821886 | 5422 | (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4)))))) |
5423 | (simplify | |
5424 | (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5))) | |
5425 | (with { tree op_type = TREE_TYPE (@5); } | |
5426 | (if (inverse_conditions_p (@0, @2) | |
5427 | && element_precision (type) == element_precision (op_type)) | |
5428 | (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1))))))) | |
6682fc02 | 5429 | |
5430 | /* Same for ternary operations. */ | |
5431 | (for cond_op (COND_TERNARY) | |
5432 | (simplify | |
5433 | (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5) | |
5434 | (with { tree op_type = TREE_TYPE (@4); } | |
5435 | (if (element_precision (type) == element_precision (op_type)) | |
03821886 | 5436 | (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5)))))) |
5437 | (simplify | |
5438 | (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6))) | |
5439 | (with { tree op_type = TREE_TYPE (@6); } | |
5440 | (if (inverse_conditions_p (@0, @2) | |
5441 | && element_precision (type) == element_precision (op_type)) | |
5442 | (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1))))))) | |
fe4311f2 | 5443 | |
5444 | /* For pointers @0 and @2 and nonnegative constant offset @1, look for | |
5445 | expressions like: | |
5446 | ||
5447 | A: (@0 + @1 < @2) | (@2 + @1 < @0) | |
5448 | B: (@0 + @1 <= @2) | (@2 + @1 <= @0) | |
5449 | ||
5450 | If pointers are known not to wrap, B checks whether @1 bytes starting | |
5451 | at @0 and @2 do not overlap, while A tests the same thing for @1 + 1 | |
5452 | bytes. A is more efficiently tested as: | |
5453 | ||
5454 | A: (sizetype) (@0 + @1 - @2) > @1 * 2 | |
5455 | ||
5456 | The equivalent expression for B is given by replacing @1 with @1 - 1: | |
5457 | ||
5458 | B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2 | |
5459 | ||
5460 | @0 and @2 can be swapped in both expressions without changing the result. | |
5461 | ||
5462 | The folds rely on sizetype's being unsigned (which is always true) | |
5463 | and on its being the same width as the pointer (which we have to check). | |
5464 | ||
5465 | The fold replaces two pointer_plus expressions, two comparisons and | |
5466 | an IOR with a pointer_plus, a pointer_diff, and a comparison, so in | |
5467 | the best case it's a saving of two operations. The A fold retains one | |
5468 | of the original pointer_pluses, so is a win even if both pointer_pluses | |
5469 | are used elsewhere. The B fold is a wash if both pointer_pluses are | |
5470 | used elsewhere, since all we end up doing is replacing a comparison with | |
5471 | a pointer_plus. We do still apply the fold under those circumstances | |
5472 | though, in case applying it to other conditions eventually makes one of the | |
5473 | pointer_pluses dead. */ | |
5474 | (for ior (truth_orif truth_or bit_ior) | |
5475 | (for cmp (le lt) | |
5476 | (simplify | |
5477 | (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2) | |
5478 | (cmp:cs (pointer_plus@4 @2 @1) @0)) | |
5479 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
5480 | && TYPE_OVERFLOW_WRAPS (sizetype) | |
5481 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype)) | |
5482 | /* Calculate the rhs constant. */ | |
5483 | (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0); | |
5484 | offset_int rhs = off * 2; } | |
5485 | /* Always fails for negative values. */ | |
5486 | (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype)) | |
5487 | /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p | |
5488 | pick a canonical order. This increases the chances of using the | |
5489 | same pointer_plus in multiple checks. */ | |
5490 | (with { bool swap_p = tree_swap_operands_p (@0, @2); | |
5491 | tree rhs_tree = wide_int_to_tree (sizetype, rhs); } | |
5492 | (if (cmp == LT_EXPR) | |
5493 | (gt (convert:sizetype | |
5494 | (pointer_diff:ssizetype { swap_p ? @4 : @3; } | |
5495 | { swap_p ? @0 : @2; })) | |
5496 | { rhs_tree; }) | |
5497 | (gt (convert:sizetype | |
5498 | (pointer_diff:ssizetype | |
5499 | (pointer_plus { swap_p ? @2 : @0; } | |
5500 | { wide_int_to_tree (sizetype, off); }) | |
5501 | { swap_p ? @0 : @2; })) | |
5502 | { rhs_tree; }))))))))) | |
df2148cf | 5503 | |
5504 | /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero | |
5505 | element of @1. */ | |
5506 | (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR) | |
5507 | (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1))) | |
5508 | (with { int i = single_nonzero_element (@1); } | |
5509 | (if (i >= 0) | |
5510 | (with { tree elt = vector_cst_elt (@1, i); | |
5511 | tree elt_type = TREE_TYPE (elt); | |
5512 | unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type)); | |
5513 | tree size = bitsize_int (elt_bits); | |
5514 | tree pos = bitsize_int (elt_bits * i); } | |
5515 | (view_convert | |
5516 | (bit_and:elt_type | |
5517 | (BIT_FIELD_REF:elt_type @0 { size; } { pos; }) | |
5518 | { elt; }))))))) | |
143aadc0 | 5519 | |
5520 | (simplify | |
5521 | (vec_perm @0 @1 VECTOR_CST@2) | |
5522 | (with | |
5523 | { | |
5524 | tree op0 = @0, op1 = @1, op2 = @2; | |
5525 | ||
5526 | /* Build a vector of integers from the tree mask. */ | |
5527 | vec_perm_builder builder; | |
5528 | if (!tree_to_vec_perm_builder (&builder, op2)) | |
5529 | return NULL_TREE; | |
5530 | ||
5531 | /* Create a vec_perm_indices for the integer vector. */ | |
5532 | poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type); | |
5533 | bool single_arg = (op0 == op1); | |
5534 | vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts); | |
5535 | } | |
5536 | (if (sel.series_p (0, 1, 0, 1)) | |
5537 | { op0; } | |
5538 | (if (sel.series_p (0, 1, nelts, 1)) | |
5539 | { op1; } | |
5540 | (with | |
5541 | { | |
5542 | if (!single_arg) | |
5543 | { | |
5544 | if (sel.all_from_input_p (0)) | |
5545 | op1 = op0; | |
5546 | else if (sel.all_from_input_p (1)) | |
5547 | { | |
5548 | op0 = op1; | |
5549 | sel.rotate_inputs (1); | |
5550 | } | |
a25cdd8a | 5551 | else if (known_ge (poly_uint64 (sel[0]), nelts)) |
5552 | { | |
5553 | std::swap (op0, op1); | |
5554 | sel.rotate_inputs (1); | |
5555 | } | |
143aadc0 | 5556 | } |
5557 | gassign *def; | |
5558 | tree cop0 = op0, cop1 = op1; | |
5559 | if (TREE_CODE (op0) == SSA_NAME | |
5560 | && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0))) | |
5561 | && gimple_assign_rhs_code (def) == CONSTRUCTOR) | |
5562 | cop0 = gimple_assign_rhs1 (def); | |
5563 | if (TREE_CODE (op1) == SSA_NAME | |
5564 | && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1))) | |
5565 | && gimple_assign_rhs_code (def) == CONSTRUCTOR) | |
5566 | cop1 = gimple_assign_rhs1 (def); | |
5567 | ||
5568 | tree t; | |
5569 | } | |
5570 | (if ((TREE_CODE (cop0) == VECTOR_CST | |
5571 | || TREE_CODE (cop0) == CONSTRUCTOR) | |
5572 | && (TREE_CODE (cop1) == VECTOR_CST | |
5573 | || TREE_CODE (cop1) == CONSTRUCTOR) | |
5574 | && (t = fold_vec_perm (type, cop0, cop1, sel))) | |
5575 | { t; } | |
5576 | (with | |
5577 | { | |
5578 | bool changed = (op0 == op1 && !single_arg); | |
a25cdd8a | 5579 | tree ins = NULL_TREE; |
5580 | unsigned at = 0; | |
5581 | ||
5582 | /* See if the permutation is performing a single element | |
5583 | insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR | |
5584 | in that case. But only if the vector mode is supported, | |
5585 | otherwise this is invalid GIMPLE. */ | |
5586 | if (TYPE_MODE (type) != BLKmode | |
5587 | && (TREE_CODE (cop0) == VECTOR_CST | |
5588 | || TREE_CODE (cop0) == CONSTRUCTOR | |
5589 | || TREE_CODE (cop1) == VECTOR_CST | |
5590 | || TREE_CODE (cop1) == CONSTRUCTOR)) | |
5591 | { | |
5592 | if (sel.series_p (1, 1, nelts + 1, 1)) | |
5593 | { | |
5594 | /* After canonicalizing the first elt to come from the | |
5595 | first vector we only can insert the first elt from | |
5596 | the first vector. */ | |
5597 | at = 0; | |
8c508cf4 | 5598 | if ((ins = fold_read_from_vector (cop0, sel[0]))) |
c9cea6b6 | 5599 | op0 = op1; |
a25cdd8a | 5600 | } |
5601 | else | |
5602 | { | |
5603 | unsigned int encoded_nelts = sel.encoding ().encoded_nelts (); | |
5604 | for (at = 0; at < encoded_nelts; ++at) | |
5605 | if (maybe_ne (sel[at], at)) | |
5606 | break; | |
5607 | if (at < encoded_nelts && sel.series_p (at + 1, 1, at + 1, 1)) | |
5608 | { | |
5609 | if (known_lt (at, nelts)) | |
5610 | ins = fold_read_from_vector (cop0, sel[at]); | |
5611 | else | |
5612 | ins = fold_read_from_vector (cop1, sel[at] - nelts); | |
5613 | } | |
5614 | } | |
5615 | } | |
143aadc0 | 5616 | |
5617 | /* Generate a canonical form of the selector. */ | |
a25cdd8a | 5618 | if (!ins && sel.encoding () != builder) |
143aadc0 | 5619 | { |
5620 | /* Some targets are deficient and fail to expand a single | |
5621 | argument permutation while still allowing an equivalent | |
5622 | 2-argument version. */ | |
5623 | tree oldop2 = op2; | |
5624 | if (sel.ninputs () == 2 | |
5625 | || can_vec_perm_const_p (TYPE_MODE (type), sel, false)) | |
5626 | op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel); | |
5627 | else | |
5628 | { | |
5629 | vec_perm_indices sel2 (builder, 2, nelts); | |
5630 | if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false)) | |
5631 | op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2); | |
5632 | else | |
5633 | /* Not directly supported with either encoding, | |
5634 | so use the preferred form. */ | |
5635 | op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel); | |
5636 | } | |
a25cdd8a | 5637 | if (!operand_equal_p (op2, oldop2, 0)) |
5638 | changed = true; | |
143aadc0 | 5639 | } |
5640 | } | |
a25cdd8a | 5641 | (if (ins) |
5642 | (bit_insert { op0; } { ins; } | |
5643 | { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); }) | |
5644 | (if (changed) | |
5645 | (vec_perm { op0; } { op1; } { op2; })))))))))) | |
b006d66f | 5646 | |
5647 | /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */ | |
5648 | ||
5649 | (match vec_same_elem_p | |
5650 | @0 | |
5651 | (if (uniform_vector_p (@0)))) | |
5652 | ||
5653 | (match vec_same_elem_p | |
5654 | (vec_duplicate @0)) | |
5655 | ||
5656 | (simplify | |
5657 | (vec_perm vec_same_elem_p@0 @0 @1) | |
5658 | @0) |