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