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