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.
5 Copyright (C) 2014-2019 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
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
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
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/>. */
26 /* Generic tree predicates we inherit. */
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
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
43 (define_operator_list tcc_comparison
44 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
45 (define_operator_list inverted_tcc_comparison
46 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
47 (define_operator_list inverted_tcc_comparison_with_nans
48 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list swapped_tcc_comparison
50 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
51 (define_operator_list simple_comparison lt le eq ne ge gt)
52 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
54 #include "cfn-operators.pd"
56 /* Define operand lists for math rounding functions {,i,l,ll}FN,
57 where the versions prefixed with "i" return an int, those prefixed with
58 "l" return a long and those prefixed with "ll" return a long long.
60 Also define operand lists:
62 X<FN>F for all float functions, in the order i, l, ll
63 X<FN> for all double functions, in the same order
64 X<FN>L for all long double functions, in the same order. */
65 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
66 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
69 (define_operator_list X##FN BUILT_IN_I##FN \
72 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
76 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
81 /* Binary operations and their associated IFN_COND_* function. */
82 (define_operator_list UNCOND_BINARY
84 mult trunc_div trunc_mod rdiv
86 bit_and bit_ior bit_xor)
87 (define_operator_list COND_BINARY
88 IFN_COND_ADD IFN_COND_SUB
89 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
90 IFN_COND_MIN IFN_COND_MAX
91 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
93 /* Same for ternary operations. */
94 (define_operator_list UNCOND_TERNARY
95 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
96 (define_operator_list COND_TERNARY
97 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
99 /* As opposed to convert?, this still creates a single pattern, so
100 it is not a suitable replacement for convert? in all cases. */
101 (match (nop_convert @0)
103 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
104 (match (nop_convert @0)
106 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
107 && known_eq (TYPE_VECTOR_SUBPARTS (type),
108 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
109 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
110 /* This one has to be last, or it shadows the others. */
111 (match (nop_convert @0)
114 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
115 ABSU_EXPR returns unsigned absolute value of the operand and the operand
116 of the ABSU_EXPR will have the corresponding signed type. */
117 (simplify (abs (convert @0))
118 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
119 && !TYPE_UNSIGNED (TREE_TYPE (@0))
120 && element_precision (type) > element_precision (TREE_TYPE (@0)))
121 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
122 (convert (absu:utype @0)))))
125 /* Simplifications of operations with one constant operand and
126 simplifications to constants or single values. */
128 (for op (plus pointer_plus minus bit_ior bit_xor)
130 (op @0 integer_zerop)
133 /* 0 +p index -> (type)index */
135 (pointer_plus integer_zerop @1)
136 (non_lvalue (convert @1)))
138 /* ptr - 0 -> (type)ptr */
140 (pointer_diff @0 integer_zerop)
143 /* See if ARG1 is zero and X + ARG1 reduces to X.
144 Likewise if the operands are reversed. */
146 (plus:c @0 real_zerop@1)
147 (if (fold_real_zero_addition_p (type, @1, 0))
150 /* See if ARG1 is zero and X - ARG1 reduces to X. */
152 (minus @0 real_zerop@1)
153 (if (fold_real_zero_addition_p (type, @1, 1))
156 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
157 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
158 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
159 if not -frounding-math. For sNaNs the first operation would raise
160 exceptions but turn the result into qNan, so the second operation
161 would not raise it. */
162 (for inner_op (plus minus)
163 (for outer_op (plus minus)
165 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
168 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
169 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
170 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
172 = ((outer_op == PLUS_EXPR)
173 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
174 (if (outer_plus && !inner_plus)
179 This is unsafe for certain floats even in non-IEEE formats.
180 In IEEE, it is unsafe because it does wrong for NaNs.
181 Also note that operand_equal_p is always false if an operand
185 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
186 { build_zero_cst (type); }))
188 (pointer_diff @@0 @0)
189 { build_zero_cst (type); })
192 (mult @0 integer_zerop@1)
195 /* Maybe fold x * 0 to 0. The expressions aren't the same
196 when x is NaN, since x * 0 is also NaN. Nor are they the
197 same in modes with signed zeros, since multiplying a
198 negative value by 0 gives -0, not +0. */
200 (mult @0 real_zerop@1)
201 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
204 /* In IEEE floating point, x*1 is not equivalent to x for snans.
205 Likewise for complex arithmetic with signed zeros. */
208 (if (!HONOR_SNANS (type)
209 && (!HONOR_SIGNED_ZEROS (type)
210 || !COMPLEX_FLOAT_TYPE_P (type)))
213 /* Transform x * -1.0 into -x. */
215 (mult @0 real_minus_onep)
216 (if (!HONOR_SNANS (type)
217 && (!HONOR_SIGNED_ZEROS (type)
218 || !COMPLEX_FLOAT_TYPE_P (type)))
221 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
223 (mult SSA_NAME@1 SSA_NAME@2)
224 (if (INTEGRAL_TYPE_P (type)
225 && get_nonzero_bits (@1) == 1
226 && get_nonzero_bits (@2) == 1)
229 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
230 unless the target has native support for the former but not the latter. */
232 (mult @0 VECTOR_CST@1)
233 (if (initializer_each_zero_or_onep (@1)
234 && !HONOR_SNANS (type)
235 && !HONOR_SIGNED_ZEROS (type))
236 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
238 && (!VECTOR_MODE_P (TYPE_MODE (type))
239 || (VECTOR_MODE_P (TYPE_MODE (itype))
240 && optab_handler (and_optab,
241 TYPE_MODE (itype)) != CODE_FOR_nothing)))
242 (view_convert (bit_and:itype (view_convert @0)
243 (ne @1 { build_zero_cst (type); })))))))
245 (for cmp (gt ge lt le)
246 outp (convert convert negate negate)
247 outn (negate negate convert convert)
248 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
249 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
250 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
251 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
253 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
254 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
255 && types_match (type, TREE_TYPE (@0)))
257 (if (types_match (type, float_type_node))
258 (BUILT_IN_COPYSIGNF @1 (outp @0)))
259 (if (types_match (type, double_type_node))
260 (BUILT_IN_COPYSIGN @1 (outp @0)))
261 (if (types_match (type, long_double_type_node))
262 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
263 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
264 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
265 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
266 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
268 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
269 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
270 && types_match (type, TREE_TYPE (@0)))
272 (if (types_match (type, float_type_node))
273 (BUILT_IN_COPYSIGNF @1 (outn @0)))
274 (if (types_match (type, double_type_node))
275 (BUILT_IN_COPYSIGN @1 (outn @0)))
276 (if (types_match (type, long_double_type_node))
277 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
279 /* Transform X * copysign (1.0, X) into abs(X). */
281 (mult:c @0 (COPYSIGN_ALL real_onep @0))
282 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
285 /* Transform X * copysign (1.0, -X) into -abs(X). */
287 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
288 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
291 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
293 (COPYSIGN_ALL REAL_CST@0 @1)
294 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
295 (COPYSIGN_ALL (negate @0) @1)))
297 /* X * 1, X / 1 -> X. */
298 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
303 /* (A / (1 << B)) -> (A >> B).
304 Only for unsigned A. For signed A, this would not preserve rounding
306 For example: (-1 / ( 1 << B)) != -1 >> B. */
308 (trunc_div @0 (lshift integer_onep@1 @2))
309 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
310 && (!VECTOR_TYPE_P (type)
311 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
312 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
315 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
316 undefined behavior in constexpr evaluation, and assuming that the division
317 traps enables better optimizations than these anyway. */
318 (for div (trunc_div ceil_div floor_div round_div exact_div)
319 /* 0 / X is always zero. */
321 (div integer_zerop@0 @1)
322 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
323 (if (!integer_zerop (@1))
327 (div @0 integer_minus_onep@1)
328 (if (!TYPE_UNSIGNED (type))
333 /* But not for 0 / 0 so that we can get the proper warnings and errors.
334 And not for _Fract types where we can't build 1. */
335 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
336 { build_one_cst (type); }))
337 /* X / abs (X) is X < 0 ? -1 : 1. */
340 (if (INTEGRAL_TYPE_P (type)
341 && TYPE_OVERFLOW_UNDEFINED (type))
342 (cond (lt @0 { build_zero_cst (type); })
343 { build_minus_one_cst (type); } { build_one_cst (type); })))
346 (div:C @0 (negate @0))
347 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
348 && TYPE_OVERFLOW_UNDEFINED (type))
349 { build_minus_one_cst (type); })))
351 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
352 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
355 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
356 && TYPE_UNSIGNED (type))
359 /* Combine two successive divisions. Note that combining ceil_div
360 and floor_div is trickier and combining round_div even more so. */
361 (for div (trunc_div exact_div)
363 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
365 wi::overflow_type overflow;
366 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
367 TYPE_SIGN (type), &overflow);
369 (if (div == EXACT_DIV_EXPR
370 || optimize_successive_divisions_p (@2, @3))
372 (div @0 { wide_int_to_tree (type, mul); })
373 (if (TYPE_UNSIGNED (type)
374 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
375 { build_zero_cst (type); }))))))
377 /* Combine successive multiplications. Similar to above, but handling
378 overflow is different. */
380 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
382 wi::overflow_type overflow;
383 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
384 TYPE_SIGN (type), &overflow);
386 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
387 otherwise undefined overflow implies that @0 must be zero. */
388 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
389 (mult @0 { wide_int_to_tree (type, mul); }))))
391 /* Optimize A / A to 1.0 if we don't care about
392 NaNs or Infinities. */
395 (if (FLOAT_TYPE_P (type)
396 && ! HONOR_NANS (type)
397 && ! HONOR_INFINITIES (type))
398 { build_one_cst (type); }))
400 /* Optimize -A / A to -1.0 if we don't care about
401 NaNs or Infinities. */
403 (rdiv:C @0 (negate @0))
404 (if (FLOAT_TYPE_P (type)
405 && ! HONOR_NANS (type)
406 && ! HONOR_INFINITIES (type))
407 { build_minus_one_cst (type); }))
409 /* PR71078: x / abs(x) -> copysign (1.0, x) */
411 (rdiv:C (convert? @0) (convert? (abs @0)))
412 (if (SCALAR_FLOAT_TYPE_P (type)
413 && ! HONOR_NANS (type)
414 && ! HONOR_INFINITIES (type))
416 (if (types_match (type, float_type_node))
417 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
418 (if (types_match (type, double_type_node))
419 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
420 (if (types_match (type, long_double_type_node))
421 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
423 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
426 (if (!HONOR_SNANS (type))
429 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
431 (rdiv @0 real_minus_onep)
432 (if (!HONOR_SNANS (type))
435 (if (flag_reciprocal_math)
436 /* Convert (A/B)/C to A/(B*C). */
438 (rdiv (rdiv:s @0 @1) @2)
439 (rdiv @0 (mult @1 @2)))
441 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
443 (rdiv @0 (mult:s @1 REAL_CST@2))
445 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
447 (rdiv (mult @0 { tem; } ) @1))))
449 /* Convert A/(B/C) to (A/B)*C */
451 (rdiv @0 (rdiv:s @1 @2))
452 (mult (rdiv @0 @1) @2)))
454 /* Simplify x / (- y) to -x / y. */
456 (rdiv @0 (negate @1))
457 (rdiv (negate @0) @1))
459 (if (flag_unsafe_math_optimizations)
460 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
461 Since C / x may underflow to zero, do this only for unsafe math. */
462 (for op (lt le gt ge)
465 (op (rdiv REAL_CST@0 @1) real_zerop@2)
466 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
468 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
470 /* For C < 0, use the inverted operator. */
471 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
474 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
475 (for div (trunc_div ceil_div floor_div round_div exact_div)
477 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
478 (if (integer_pow2p (@2)
479 && tree_int_cst_sgn (@2) > 0
480 && tree_nop_conversion_p (type, TREE_TYPE (@0))
481 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
483 { build_int_cst (integer_type_node,
484 wi::exact_log2 (wi::to_wide (@2))); }))))
486 /* If ARG1 is a constant, we can convert this to a multiply by the
487 reciprocal. This does not have the same rounding properties,
488 so only do this if -freciprocal-math. We can actually
489 always safely do it if ARG1 is a power of two, but it's hard to
490 tell if it is or not in a portable manner. */
491 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
495 (if (flag_reciprocal_math
498 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
500 (mult @0 { tem; } )))
501 (if (cst != COMPLEX_CST)
502 (with { tree inverse = exact_inverse (type, @1); }
504 (mult @0 { inverse; } ))))))))
506 (for mod (ceil_mod floor_mod round_mod trunc_mod)
507 /* 0 % X is always zero. */
509 (mod integer_zerop@0 @1)
510 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
511 (if (!integer_zerop (@1))
513 /* X % 1 is always zero. */
515 (mod @0 integer_onep)
516 { build_zero_cst (type); })
517 /* X % -1 is zero. */
519 (mod @0 integer_minus_onep@1)
520 (if (!TYPE_UNSIGNED (type))
521 { build_zero_cst (type); }))
525 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
526 (if (!integer_zerop (@0))
527 { build_zero_cst (type); }))
528 /* (X % Y) % Y is just X % Y. */
530 (mod (mod@2 @0 @1) @1)
532 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
534 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
535 (if (ANY_INTEGRAL_TYPE_P (type)
536 && TYPE_OVERFLOW_UNDEFINED (type)
537 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
539 { build_zero_cst (type); }))
540 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
541 modulo and comparison, since it is simpler and equivalent. */
544 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
545 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
546 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
547 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
549 /* X % -C is the same as X % C. */
551 (trunc_mod @0 INTEGER_CST@1)
552 (if (TYPE_SIGN (type) == SIGNED
553 && !TREE_OVERFLOW (@1)
554 && wi::neg_p (wi::to_wide (@1))
555 && !TYPE_OVERFLOW_TRAPS (type)
556 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
557 && !sign_bit_p (@1, @1))
558 (trunc_mod @0 (negate @1))))
560 /* X % -Y is the same as X % Y. */
562 (trunc_mod @0 (convert? (negate @1)))
563 (if (INTEGRAL_TYPE_P (type)
564 && !TYPE_UNSIGNED (type)
565 && !TYPE_OVERFLOW_TRAPS (type)
566 && tree_nop_conversion_p (type, TREE_TYPE (@1))
567 /* Avoid this transformation if X might be INT_MIN or
568 Y might be -1, because we would then change valid
569 INT_MIN % -(-1) into invalid INT_MIN % -1. */
570 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
571 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
573 (trunc_mod @0 (convert @1))))
575 /* X - (X / Y) * Y is the same as X % Y. */
577 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
578 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
579 (convert (trunc_mod @0 @1))))
581 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
582 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
583 Also optimize A % (C << N) where C is a power of 2,
584 to A & ((C << N) - 1). */
585 (match (power_of_two_cand @1)
587 (match (power_of_two_cand @1)
588 (lshift INTEGER_CST@1 @2))
589 (for mod (trunc_mod floor_mod)
591 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
592 (if ((TYPE_UNSIGNED (type)
593 || tree_expr_nonnegative_p (@0))
594 && tree_nop_conversion_p (type, TREE_TYPE (@3))
595 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
596 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
598 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
600 (trunc_div (mult @0 integer_pow2p@1) @1)
601 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
602 (bit_and @0 { wide_int_to_tree
603 (type, wi::mask (TYPE_PRECISION (type)
604 - wi::exact_log2 (wi::to_wide (@1)),
605 false, TYPE_PRECISION (type))); })))
607 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
609 (mult (trunc_div @0 integer_pow2p@1) @1)
610 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
611 (bit_and @0 (negate @1))))
613 /* Simplify (t * 2) / 2) -> t. */
614 (for div (trunc_div ceil_div floor_div round_div exact_div)
616 (div (mult:c @0 @1) @1)
617 (if (ANY_INTEGRAL_TYPE_P (type)
618 && TYPE_OVERFLOW_UNDEFINED (type))
622 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
627 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
630 (pows (op @0) REAL_CST@1)
631 (with { HOST_WIDE_INT n; }
632 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
634 /* Likewise for powi. */
637 (pows (op @0) INTEGER_CST@1)
638 (if ((wi::to_wide (@1) & 1) == 0)
640 /* Strip negate and abs from both operands of hypot. */
648 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
649 (for copysigns (COPYSIGN_ALL)
651 (copysigns (op @0) @1)
654 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
659 /* Convert absu(x)*absu(x) -> x*x. */
661 (mult (absu@1 @0) @1)
662 (mult (convert@2 @0) @2))
664 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
668 (coss (copysigns @0 @1))
671 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
675 (pows (copysigns @0 @2) REAL_CST@1)
676 (with { HOST_WIDE_INT n; }
677 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
679 /* Likewise for powi. */
683 (pows (copysigns @0 @2) INTEGER_CST@1)
684 (if ((wi::to_wide (@1) & 1) == 0)
689 /* hypot(copysign(x, y), z) -> hypot(x, z). */
691 (hypots (copysigns @0 @1) @2)
693 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
695 (hypots @0 (copysigns @1 @2))
698 /* copysign(x, CST) -> [-]abs (x). */
699 (for copysigns (COPYSIGN_ALL)
701 (copysigns @0 REAL_CST@1)
702 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
706 /* copysign(copysign(x, y), z) -> copysign(x, z). */
707 (for copysigns (COPYSIGN_ALL)
709 (copysigns (copysigns @0 @1) @2)
712 /* copysign(x,y)*copysign(x,y) -> x*x. */
713 (for copysigns (COPYSIGN_ALL)
715 (mult (copysigns@2 @0 @1) @2)
718 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
719 (for ccoss (CCOS CCOSH)
724 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
725 (for ops (conj negate)
731 /* Fold (a * (1 << b)) into (a << b) */
733 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
734 (if (! FLOAT_TYPE_P (type)
735 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
738 /* Fold (1 << (C - x)) where C = precision(type) - 1
739 into ((1 << C) >> x). */
741 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
742 (if (INTEGRAL_TYPE_P (type)
743 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
745 (if (TYPE_UNSIGNED (type))
746 (rshift (lshift @0 @2) @3)
748 { tree utype = unsigned_type_for (type); }
749 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
751 /* Fold (C1/X)*C2 into (C1*C2)/X. */
753 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
754 (if (flag_associative_math
757 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
759 (rdiv { tem; } @1)))))
761 /* Simplify ~X & X as zero. */
763 (bit_and:c (convert? @0) (convert? (bit_not @0)))
764 { build_zero_cst (type); })
766 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
768 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
769 (if (TYPE_UNSIGNED (type))
770 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
772 (for bitop (bit_and bit_ior)
774 /* PR35691: Transform
775 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
776 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
778 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
779 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
780 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
781 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
782 (cmp (bit_ior @0 (convert @1)) @2)))
784 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
785 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
787 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
788 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
789 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
790 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
791 (cmp (bit_and @0 (convert @1)) @2))))
793 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
795 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
796 (minus (bit_xor @0 @1) @1))
798 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
799 (if (~wi::to_wide (@2) == wi::to_wide (@1))
800 (minus (bit_xor @0 @1) @1)))
802 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
804 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
805 (minus @1 (bit_xor @0 @1)))
807 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
808 (for op (bit_ior bit_xor plus)
810 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
813 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
814 (if (~wi::to_wide (@2) == wi::to_wide (@1))
817 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
819 (bit_ior:c (bit_xor:c @0 @1) @0)
822 /* (a & ~b) | (a ^ b) --> a ^ b */
824 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
827 /* (a & ~b) ^ ~a --> ~(a & b) */
829 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
830 (bit_not (bit_and @0 @1)))
832 /* (a | b) & ~(a ^ b) --> a & b */
834 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
837 /* a | ~(a ^ b) --> a | ~b */
839 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
840 (bit_ior @0 (bit_not @1)))
842 /* (a | b) | (a &^ b) --> a | b */
843 (for op (bit_and bit_xor)
845 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
848 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
850 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
853 /* ~(~a & b) --> a | ~b */
855 (bit_not (bit_and:cs (bit_not @0) @1))
856 (bit_ior @0 (bit_not @1)))
858 /* ~(~a | b) --> a & ~b */
860 (bit_not (bit_ior:cs (bit_not @0) @1))
861 (bit_and @0 (bit_not @1)))
863 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
866 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
867 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
868 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
872 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
873 ((A & N) + B) & M -> (A + B) & M
874 Similarly if (N & M) == 0,
875 ((A | N) + B) & M -> (A + B) & M
876 and for - instead of + (or unary - instead of +)
877 and/or ^ instead of |.
878 If B is constant and (B & M) == 0, fold into A & M. */
880 (for bitop (bit_and bit_ior bit_xor)
882 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
885 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
886 @3, @4, @1, ERROR_MARK, NULL_TREE,
889 (convert (bit_and (op (convert:utype { pmop[0]; })
890 (convert:utype { pmop[1]; }))
891 (convert:utype @2))))))
893 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
896 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
897 NULL_TREE, NULL_TREE, @1, bitop, @3,
900 (convert (bit_and (op (convert:utype { pmop[0]; })
901 (convert:utype { pmop[1]; }))
902 (convert:utype @2)))))))
904 (bit_and (op:s @0 @1) INTEGER_CST@2)
907 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
908 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
909 NULL_TREE, NULL_TREE, pmop); }
911 (convert (bit_and (op (convert:utype { pmop[0]; })
912 (convert:utype { pmop[1]; }))
913 (convert:utype @2)))))))
914 (for bitop (bit_and bit_ior bit_xor)
916 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
919 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
920 bitop, @2, @3, NULL_TREE, ERROR_MARK,
921 NULL_TREE, NULL_TREE, pmop); }
923 (convert (bit_and (negate (convert:utype { pmop[0]; }))
924 (convert:utype @1)))))))
926 /* X % Y is smaller than Y. */
929 (cmp (trunc_mod @0 @1) @1)
930 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
931 { constant_boolean_node (cmp == LT_EXPR, type); })))
934 (cmp @1 (trunc_mod @0 @1))
935 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
936 { constant_boolean_node (cmp == GT_EXPR, type); })))
940 (bit_ior @0 integer_all_onesp@1)
945 (bit_ior @0 integer_zerop)
950 (bit_and @0 integer_zerop@1)
956 (for op (bit_ior bit_xor plus)
958 (op:c (convert? @0) (convert? (bit_not @0)))
959 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
964 { build_zero_cst (type); })
966 /* Canonicalize X ^ ~0 to ~X. */
968 (bit_xor @0 integer_all_onesp@1)
973 (bit_and @0 integer_all_onesp)
976 /* x & x -> x, x | x -> x */
977 (for bitop (bit_and bit_ior)
982 /* x & C -> x if we know that x & ~C == 0. */
985 (bit_and SSA_NAME@0 INTEGER_CST@1)
986 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
987 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
991 /* x + (x & 1) -> (x + 1) & ~1 */
993 (plus:c @0 (bit_and:s @0 integer_onep@1))
994 (bit_and (plus @0 @1) (bit_not @1)))
996 /* x & ~(x & y) -> x & ~y */
997 /* x | ~(x | y) -> x | ~y */
998 (for bitop (bit_and bit_ior)
1000 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1001 (bitop @0 (bit_not @1))))
1003 /* (~x & y) | ~(x | y) -> ~x */
1005 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1008 /* (x | y) ^ (x | ~y) -> ~x */
1010 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1013 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1015 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1016 (bit_not (bit_xor @0 @1)))
1018 /* (~x | y) ^ (x ^ y) -> x | ~y */
1020 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1021 (bit_ior @0 (bit_not @1)))
1023 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1025 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1026 (bit_not (bit_and @0 @1)))
1028 /* (x | y) & ~x -> y & ~x */
1029 /* (x & y) | ~x -> y | ~x */
1030 (for bitop (bit_and bit_ior)
1031 rbitop (bit_ior bit_and)
1033 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1036 /* (x & y) ^ (x | y) -> x ^ y */
1038 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1041 /* (x ^ y) ^ (x | y) -> x & y */
1043 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1046 /* (x & y) + (x ^ y) -> x | y */
1047 /* (x & y) | (x ^ y) -> x | y */
1048 /* (x & y) ^ (x ^ y) -> x | y */
1049 (for op (plus bit_ior bit_xor)
1051 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1054 /* (x & y) + (x | y) -> x + y */
1056 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1059 /* (x + y) - (x | y) -> x & y */
1061 (minus (plus @0 @1) (bit_ior @0 @1))
1062 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1063 && !TYPE_SATURATING (type))
1066 /* (x + y) - (x & y) -> x | y */
1068 (minus (plus @0 @1) (bit_and @0 @1))
1069 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1070 && !TYPE_SATURATING (type))
1073 /* (x | y) - (x ^ y) -> x & y */
1075 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1078 /* (x | y) - (x & y) -> x ^ y */
1080 (minus (bit_ior @0 @1) (bit_and @0 @1))
1083 /* (x | y) & ~(x & y) -> x ^ y */
1085 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1088 /* (x | y) & (~x ^ y) -> x & y */
1090 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1093 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1095 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1096 (bit_not (bit_xor @0 @1)))
1098 /* (~x | y) ^ (x | ~y) -> x ^ y */
1100 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1103 /* ~x & ~y -> ~(x | y)
1104 ~x | ~y -> ~(x & y) */
1105 (for op (bit_and bit_ior)
1106 rop (bit_ior bit_and)
1108 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1109 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1110 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1111 (bit_not (rop (convert @0) (convert @1))))))
1113 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1114 with a constant, and the two constants have no bits in common,
1115 we should treat this as a BIT_IOR_EXPR since this may produce more
1117 (for op (bit_xor plus)
1119 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1120 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1121 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1122 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1123 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1124 (bit_ior (convert @4) (convert @5)))))
1126 /* (X | Y) ^ X -> Y & ~ X*/
1128 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1129 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1130 (convert (bit_and @1 (bit_not @0)))))
1132 /* Convert ~X ^ ~Y to X ^ Y. */
1134 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1135 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1136 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1137 (bit_xor (convert @0) (convert @1))))
1139 /* Convert ~X ^ C to X ^ ~C. */
1141 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1142 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1143 (bit_xor (convert @0) (bit_not @1))))
1145 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1146 (for opo (bit_and bit_xor)
1147 opi (bit_xor bit_and)
1149 (opo:c (opi:cs @0 @1) @1)
1150 (bit_and (bit_not @0) @1)))
1152 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1153 operands are another bit-wise operation with a common input. If so,
1154 distribute the bit operations to save an operation and possibly two if
1155 constants are involved. For example, convert
1156 (A | B) & (A | C) into A | (B & C)
1157 Further simplification will occur if B and C are constants. */
1158 (for op (bit_and bit_ior bit_xor)
1159 rop (bit_ior bit_and bit_and)
1161 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1162 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1163 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1164 (rop (convert @0) (op (convert @1) (convert @2))))))
1166 /* Some simple reassociation for bit operations, also handled in reassoc. */
1167 /* (X & Y) & Y -> X & Y
1168 (X | Y) | Y -> X | Y */
1169 (for op (bit_and bit_ior)
1171 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1173 /* (X ^ Y) ^ Y -> X */
1175 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1177 /* (X & Y) & (X & Z) -> (X & Y) & Z
1178 (X | Y) | (X | Z) -> (X | Y) | Z */
1179 (for op (bit_and bit_ior)
1181 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1182 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1183 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1184 (if (single_use (@5) && single_use (@6))
1185 (op @3 (convert @2))
1186 (if (single_use (@3) && single_use (@4))
1187 (op (convert @1) @5))))))
1188 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1190 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1191 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1192 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1193 (bit_xor (convert @1) (convert @2))))
1195 /* Convert abs (abs (X)) into abs (X).
1196 also absu (absu (X)) into absu (X). */
1202 (absu (convert@2 (absu@1 @0)))
1203 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1206 /* Convert abs[u] (-X) -> abs[u] (X). */
1215 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1217 (abs tree_expr_nonnegative_p@0)
1221 (absu tree_expr_nonnegative_p@0)
1224 /* A few cases of fold-const.c negate_expr_p predicate. */
1225 (match negate_expr_p
1227 (if ((INTEGRAL_TYPE_P (type)
1228 && TYPE_UNSIGNED (type))
1229 || (!TYPE_OVERFLOW_SANITIZED (type)
1230 && may_negate_without_overflow_p (t)))))
1231 (match negate_expr_p
1233 (match negate_expr_p
1235 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1236 (match negate_expr_p
1238 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1239 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1241 (match negate_expr_p
1243 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1244 (match negate_expr_p
1246 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1247 || (FLOAT_TYPE_P (type)
1248 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1249 && !HONOR_SIGNED_ZEROS (type)))))
1251 /* (-A) * (-B) -> A * B */
1253 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1254 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1255 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1256 (mult (convert @0) (convert (negate @1)))))
1258 /* -(A + B) -> (-B) - A. */
1260 (negate (plus:c @0 negate_expr_p@1))
1261 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1262 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1263 (minus (negate @1) @0)))
1265 /* -(A - B) -> B - A. */
1267 (negate (minus @0 @1))
1268 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1269 || (FLOAT_TYPE_P (type)
1270 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1271 && !HONOR_SIGNED_ZEROS (type)))
1274 (negate (pointer_diff @0 @1))
1275 (if (TYPE_OVERFLOW_UNDEFINED (type))
1276 (pointer_diff @1 @0)))
1278 /* A - B -> A + (-B) if B is easily negatable. */
1280 (minus @0 negate_expr_p@1)
1281 (if (!FIXED_POINT_TYPE_P (type))
1282 (plus @0 (negate @1))))
1284 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1286 For bitwise binary operations apply operand conversions to the
1287 binary operation result instead of to the operands. This allows
1288 to combine successive conversions and bitwise binary operations.
1289 We combine the above two cases by using a conditional convert. */
1290 (for bitop (bit_and bit_ior bit_xor)
1292 (bitop (convert @0) (convert? @1))
1293 (if (((TREE_CODE (@1) == INTEGER_CST
1294 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1295 && int_fits_type_p (@1, TREE_TYPE (@0)))
1296 || types_match (@0, @1))
1297 /* ??? This transform conflicts with fold-const.c doing
1298 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1299 constants (if x has signed type, the sign bit cannot be set
1300 in c). This folds extension into the BIT_AND_EXPR.
1301 Restrict it to GIMPLE to avoid endless recursions. */
1302 && (bitop != BIT_AND_EXPR || GIMPLE)
1303 && (/* That's a good idea if the conversion widens the operand, thus
1304 after hoisting the conversion the operation will be narrower. */
1305 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1306 /* It's also a good idea if the conversion is to a non-integer
1308 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1309 /* Or if the precision of TO is not the same as the precision
1311 || !type_has_mode_precision_p (type)))
1312 (convert (bitop @0 (convert @1))))))
1314 (for bitop (bit_and bit_ior)
1315 rbitop (bit_ior bit_and)
1316 /* (x | y) & x -> x */
1317 /* (x & y) | x -> x */
1319 (bitop:c (rbitop:c @0 @1) @0)
1321 /* (~x | y) & x -> x & y */
1322 /* (~x & y) | x -> x | y */
1324 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1327 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1329 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1330 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1332 /* Combine successive equal operations with constants. */
1333 (for bitop (bit_and bit_ior bit_xor)
1335 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1336 (if (!CONSTANT_CLASS_P (@0))
1337 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1338 folded to a constant. */
1339 (bitop @0 (bitop @1 @2))
1340 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1341 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1342 the values involved are such that the operation can't be decided at
1343 compile time. Try folding one of @0 or @1 with @2 to see whether
1344 that combination can be decided at compile time.
1346 Keep the existing form if both folds fail, to avoid endless
1348 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1350 (bitop @1 { cst1; })
1351 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1353 (bitop @0 { cst2; }))))))))
1355 /* Try simple folding for X op !X, and X op X with the help
1356 of the truth_valued_p and logical_inverted_value predicates. */
1357 (match truth_valued_p
1359 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1360 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1361 (match truth_valued_p
1363 (match truth_valued_p
1366 (match (logical_inverted_value @0)
1368 (match (logical_inverted_value @0)
1369 (bit_not truth_valued_p@0))
1370 (match (logical_inverted_value @0)
1371 (eq @0 integer_zerop))
1372 (match (logical_inverted_value @0)
1373 (ne truth_valued_p@0 integer_truep))
1374 (match (logical_inverted_value @0)
1375 (bit_xor truth_valued_p@0 integer_truep))
1379 (bit_and:c @0 (logical_inverted_value @0))
1380 { build_zero_cst (type); })
1381 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1382 (for op (bit_ior bit_xor)
1384 (op:c truth_valued_p@0 (logical_inverted_value @0))
1385 { constant_boolean_node (true, type); }))
1386 /* X ==/!= !X is false/true. */
1389 (op:c truth_valued_p@0 (logical_inverted_value @0))
1390 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1394 (bit_not (bit_not @0))
1397 /* Convert ~ (-A) to A - 1. */
1399 (bit_not (convert? (negate @0)))
1400 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1401 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1402 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1404 /* Convert - (~A) to A + 1. */
1406 (negate (nop_convert (bit_not @0)))
1407 (plus (view_convert @0) { build_each_one_cst (type); }))
1409 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1411 (bit_not (convert? (minus @0 integer_each_onep)))
1412 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1413 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1414 (convert (negate @0))))
1416 (bit_not (convert? (plus @0 integer_all_onesp)))
1417 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1418 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1419 (convert (negate @0))))
1421 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1423 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1424 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1425 (convert (bit_xor @0 (bit_not @1)))))
1427 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1428 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1429 (convert (bit_xor @0 @1))))
1431 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1433 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1434 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1435 (bit_not (bit_xor (view_convert @0) @1))))
1437 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1439 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1440 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1442 /* Fold A - (A & B) into ~B & A. */
1444 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1445 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1446 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1447 (convert (bit_and (bit_not @1) @0))))
1449 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1450 (for cmp (gt lt ge le)
1452 (mult (convert (cmp @0 @1)) @2)
1453 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1455 /* For integral types with undefined overflow and C != 0 fold
1456 x * C EQ/NE y * C into x EQ/NE y. */
1459 (cmp (mult:c @0 @1) (mult:c @2 @1))
1460 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1461 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1462 && tree_expr_nonzero_p (@1))
1465 /* For integral types with wrapping overflow and C odd fold
1466 x * C EQ/NE y * C into x EQ/NE y. */
1469 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1470 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1471 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1472 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1475 /* For integral types with undefined overflow and C != 0 fold
1476 x * C RELOP y * C into:
1478 x RELOP y for nonnegative C
1479 y RELOP x for negative C */
1480 (for cmp (lt gt le ge)
1482 (cmp (mult:c @0 @1) (mult:c @2 @1))
1483 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1484 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1485 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1487 (if (TREE_CODE (@1) == INTEGER_CST
1488 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1491 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1495 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1496 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1497 && TYPE_UNSIGNED (TREE_TYPE (@0))
1498 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1499 && (wi::to_wide (@2)
1500 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1501 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1502 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1504 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1505 (for cmp (simple_comparison)
1507 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1508 (if (element_precision (@3) >= element_precision (@0)
1509 && types_match (@0, @1))
1510 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1511 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1513 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1516 tree utype = unsigned_type_for (TREE_TYPE (@0));
1518 (cmp (convert:utype @1) (convert:utype @0)))))
1519 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1520 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1524 tree utype = unsigned_type_for (TREE_TYPE (@0));
1526 (cmp (convert:utype @0) (convert:utype @1)))))))))
1528 /* X / C1 op C2 into a simple range test. */
1529 (for cmp (simple_comparison)
1531 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1532 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1533 && integer_nonzerop (@1)
1534 && !TREE_OVERFLOW (@1)
1535 && !TREE_OVERFLOW (@2))
1536 (with { tree lo, hi; bool neg_overflow;
1537 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1540 (if (code == LT_EXPR || code == GE_EXPR)
1541 (if (TREE_OVERFLOW (lo))
1542 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1543 (if (code == LT_EXPR)
1546 (if (code == LE_EXPR || code == GT_EXPR)
1547 (if (TREE_OVERFLOW (hi))
1548 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1549 (if (code == LE_EXPR)
1553 { build_int_cst (type, code == NE_EXPR); })
1554 (if (code == EQ_EXPR && !hi)
1556 (if (code == EQ_EXPR && !lo)
1558 (if (code == NE_EXPR && !hi)
1560 (if (code == NE_EXPR && !lo)
1563 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1567 tree etype = range_check_type (TREE_TYPE (@0));
1570 if (! TYPE_UNSIGNED (etype))
1571 etype = unsigned_type_for (etype);
1572 hi = fold_convert (etype, hi);
1573 lo = fold_convert (etype, lo);
1574 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1577 (if (etype && hi && !TREE_OVERFLOW (hi))
1578 (if (code == EQ_EXPR)
1579 (le (minus (convert:etype @0) { lo; }) { hi; })
1580 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1582 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1583 (for op (lt le ge gt)
1585 (op (plus:c @0 @2) (plus:c @1 @2))
1586 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1587 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1589 /* For equality and subtraction, this is also true with wrapping overflow. */
1590 (for op (eq ne minus)
1592 (op (plus:c @0 @2) (plus:c @1 @2))
1593 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1594 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1595 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1598 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1599 (for op (lt le ge gt)
1601 (op (minus @0 @2) (minus @1 @2))
1602 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1603 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1605 /* For equality and subtraction, this is also true with wrapping overflow. */
1606 (for op (eq ne minus)
1608 (op (minus @0 @2) (minus @1 @2))
1609 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1610 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1611 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1613 /* And for pointers... */
1614 (for op (simple_comparison)
1616 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1617 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1620 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1621 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1622 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1623 (pointer_diff @0 @1)))
1625 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1626 (for op (lt le ge gt)
1628 (op (minus @2 @0) (minus @2 @1))
1629 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1630 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1632 /* For equality and subtraction, this is also true with wrapping overflow. */
1633 (for op (eq ne minus)
1635 (op (minus @2 @0) (minus @2 @1))
1636 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1637 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1638 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1640 /* And for pointers... */
1641 (for op (simple_comparison)
1643 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1644 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1647 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1648 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1649 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1650 (pointer_diff @1 @0)))
1652 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1653 (for op (lt le gt ge)
1655 (op:c (plus:c@2 @0 @1) @1)
1656 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1657 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1658 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1659 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1660 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1661 /* For equality, this is also true with wrapping overflow. */
1664 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1665 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1666 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1667 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1668 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1669 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1670 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1671 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1673 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1674 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1675 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1676 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1677 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1679 /* X - Y < X is the same as Y > 0 when there is no overflow.
1680 For equality, this is also true with wrapping overflow. */
1681 (for op (simple_comparison)
1683 (op:c @0 (minus@2 @0 @1))
1684 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1685 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1686 || ((op == EQ_EXPR || op == NE_EXPR)
1687 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1688 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1689 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1692 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1693 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1697 (cmp (trunc_div @0 @1) integer_zerop)
1698 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1699 /* Complex ==/!= is allowed, but not </>=. */
1700 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1701 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1704 /* X == C - X can never be true if C is odd. */
1707 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1708 (if (TREE_INT_CST_LOW (@1) & 1)
1709 { constant_boolean_node (cmp == NE_EXPR, type); })))
1711 /* Arguments on which one can call get_nonzero_bits to get the bits
1713 (match with_possible_nonzero_bits
1715 (match with_possible_nonzero_bits
1717 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1718 /* Slightly extended version, do not make it recursive to keep it cheap. */
1719 (match (with_possible_nonzero_bits2 @0)
1720 with_possible_nonzero_bits@0)
1721 (match (with_possible_nonzero_bits2 @0)
1722 (bit_and:c with_possible_nonzero_bits@0 @2))
1724 /* Same for bits that are known to be set, but we do not have
1725 an equivalent to get_nonzero_bits yet. */
1726 (match (with_certain_nonzero_bits2 @0)
1728 (match (with_certain_nonzero_bits2 @0)
1729 (bit_ior @1 INTEGER_CST@0))
1731 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1734 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1735 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1736 { constant_boolean_node (cmp == NE_EXPR, type); })))
1738 /* ((X inner_op C0) outer_op C1)
1739 With X being a tree where value_range has reasoned certain bits to always be
1740 zero throughout its computed value range,
1741 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1742 where zero_mask has 1's for all bits that are sure to be 0 in
1744 if (inner_op == '^') C0 &= ~C1;
1745 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1746 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1748 (for inner_op (bit_ior bit_xor)
1749 outer_op (bit_xor bit_ior)
1752 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1756 wide_int zero_mask_not;
1760 if (TREE_CODE (@2) == SSA_NAME)
1761 zero_mask_not = get_nonzero_bits (@2);
1765 if (inner_op == BIT_XOR_EXPR)
1767 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1768 cst_emit = C0 | wi::to_wide (@1);
1772 C0 = wi::to_wide (@0);
1773 cst_emit = C0 ^ wi::to_wide (@1);
1776 (if (!fail && (C0 & zero_mask_not) == 0)
1777 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1778 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1779 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1781 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1783 (pointer_plus (pointer_plus:s @0 @1) @3)
1784 (pointer_plus @0 (plus @1 @3)))
1790 tem4 = (unsigned long) tem3;
1795 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1796 /* Conditionally look through a sign-changing conversion. */
1797 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1798 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1799 || (GENERIC && type == TREE_TYPE (@1))))
1802 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1803 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1807 tem = (sizetype) ptr;
1811 and produce the simpler and easier to analyze with respect to alignment
1812 ... = ptr & ~algn; */
1814 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1815 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1816 (bit_and @0 { algn; })))
1818 /* Try folding difference of addresses. */
1820 (minus (convert ADDR_EXPR@0) (convert @1))
1821 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1822 (with { poly_int64 diff; }
1823 (if (ptr_difference_const (@0, @1, &diff))
1824 { build_int_cst_type (type, diff); }))))
1826 (minus (convert @0) (convert ADDR_EXPR@1))
1827 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1828 (with { poly_int64 diff; }
1829 (if (ptr_difference_const (@0, @1, &diff))
1830 { build_int_cst_type (type, diff); }))))
1832 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1833 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1834 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1835 (with { poly_int64 diff; }
1836 (if (ptr_difference_const (@0, @1, &diff))
1837 { build_int_cst_type (type, diff); }))))
1839 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1840 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1841 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1842 (with { poly_int64 diff; }
1843 (if (ptr_difference_const (@0, @1, &diff))
1844 { build_int_cst_type (type, diff); }))))
1846 /* If arg0 is derived from the address of an object or function, we may
1847 be able to fold this expression using the object or function's
1850 (bit_and (convert? @0) INTEGER_CST@1)
1851 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1852 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1856 unsigned HOST_WIDE_INT bitpos;
1857 get_pointer_alignment_1 (@0, &align, &bitpos);
1859 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1860 { wide_int_to_tree (type, (wi::to_wide (@1)
1861 & (bitpos / BITS_PER_UNIT))); }))))
1864 /* We can't reassociate at all for saturating types. */
1865 (if (!TYPE_SATURATING (type))
1867 /* Contract negates. */
1868 /* A + (-B) -> A - B */
1870 (plus:c @0 (convert? (negate @1)))
1871 /* Apply STRIP_NOPS on the negate. */
1872 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1873 && !TYPE_OVERFLOW_SANITIZED (type))
1877 if (INTEGRAL_TYPE_P (type)
1878 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1879 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1881 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1882 /* A - (-B) -> A + B */
1884 (minus @0 (convert? (negate @1)))
1885 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1886 && !TYPE_OVERFLOW_SANITIZED (type))
1890 if (INTEGRAL_TYPE_P (type)
1891 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1892 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1894 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1896 Sign-extension is ok except for INT_MIN, which thankfully cannot
1897 happen without overflow. */
1899 (negate (convert (negate @1)))
1900 (if (INTEGRAL_TYPE_P (type)
1901 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1902 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1903 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1904 && !TYPE_OVERFLOW_SANITIZED (type)
1905 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1908 (negate (convert negate_expr_p@1))
1909 (if (SCALAR_FLOAT_TYPE_P (type)
1910 && ((DECIMAL_FLOAT_TYPE_P (type)
1911 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1912 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1913 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1914 (convert (negate @1))))
1916 (negate (nop_convert (negate @1)))
1917 (if (!TYPE_OVERFLOW_SANITIZED (type)
1918 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1921 /* We can't reassociate floating-point unless -fassociative-math
1922 or fixed-point plus or minus because of saturation to +-Inf. */
1923 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1924 && !FIXED_POINT_TYPE_P (type))
1926 /* Match patterns that allow contracting a plus-minus pair
1927 irrespective of overflow issues. */
1928 /* (A +- B) - A -> +- B */
1929 /* (A +- B) -+ B -> A */
1930 /* A - (A +- B) -> -+ B */
1931 /* A +- (B -+ A) -> +- B */
1933 (minus (plus:c @0 @1) @0)
1936 (minus (minus @0 @1) @0)
1939 (plus:c (minus @0 @1) @1)
1942 (minus @0 (plus:c @0 @1))
1945 (minus @0 (minus @0 @1))
1947 /* (A +- B) + (C - A) -> C +- B */
1948 /* (A + B) - (A - C) -> B + C */
1949 /* More cases are handled with comparisons. */
1951 (plus:c (plus:c @0 @1) (minus @2 @0))
1954 (plus:c (minus @0 @1) (minus @2 @0))
1957 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1958 (if (TYPE_OVERFLOW_UNDEFINED (type)
1959 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1960 (pointer_diff @2 @1)))
1962 (minus (plus:c @0 @1) (minus @0 @2))
1965 /* (A +- CST1) +- CST2 -> A + CST3
1966 Use view_convert because it is safe for vectors and equivalent for
1968 (for outer_op (plus minus)
1969 (for inner_op (plus minus)
1970 neg_inner_op (minus plus)
1972 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1974 /* If one of the types wraps, use that one. */
1975 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1976 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1977 forever if something doesn't simplify into a constant. */
1978 (if (!CONSTANT_CLASS_P (@0))
1979 (if (outer_op == PLUS_EXPR)
1980 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1981 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1982 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1983 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1984 (if (outer_op == PLUS_EXPR)
1985 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1986 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1987 /* If the constant operation overflows we cannot do the transform
1988 directly as we would introduce undefined overflow, for example
1989 with (a - 1) + INT_MIN. */
1990 (if (types_match (type, @0))
1991 (with { tree cst = const_binop (outer_op == inner_op
1992 ? PLUS_EXPR : MINUS_EXPR,
1994 (if (cst && !TREE_OVERFLOW (cst))
1995 (inner_op @0 { cst; } )
1996 /* X+INT_MAX+1 is X-INT_MIN. */
1997 (if (INTEGRAL_TYPE_P (type) && cst
1998 && wi::to_wide (cst) == wi::min_value (type))
1999 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2000 /* Last resort, use some unsigned type. */
2001 (with { tree utype = unsigned_type_for (type); }
2003 (view_convert (inner_op
2004 (view_convert:utype @0)
2006 { drop_tree_overflow (cst); }))))))))))))))
2008 /* (CST1 - A) +- CST2 -> CST3 - A */
2009 (for outer_op (plus minus)
2011 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
2012 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2013 (if (cst && !TREE_OVERFLOW (cst))
2014 (minus { cst; } @0)))))
2016 /* CST1 - (CST2 - A) -> CST3 + A */
2018 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
2019 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2020 (if (cst && !TREE_OVERFLOW (cst))
2021 (plus { cst; } @0))))
2025 (plus:c (bit_not @0) @0)
2026 (if (!TYPE_OVERFLOW_TRAPS (type))
2027 { build_all_ones_cst (type); }))
2031 (plus (convert? (bit_not @0)) integer_each_onep)
2032 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2033 (negate (convert @0))))
2037 (minus (convert? (negate @0)) integer_each_onep)
2038 (if (!TYPE_OVERFLOW_TRAPS (type)
2039 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2040 (bit_not (convert @0))))
2044 (minus integer_all_onesp @0)
2047 /* (T)(P + A) - (T)P -> (T) A */
2049 (minus (convert (plus:c @@0 @1))
2051 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2052 /* For integer types, if A has a smaller type
2053 than T the result depends on the possible
2055 E.g. T=size_t, A=(unsigned)429497295, P>0.
2056 However, if an overflow in P + A would cause
2057 undefined behavior, we can assume that there
2059 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2060 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2063 (minus (convert (pointer_plus @@0 @1))
2065 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2066 /* For pointer types, if the conversion of A to the
2067 final type requires a sign- or zero-extension,
2068 then we have to punt - it is not defined which
2070 || (POINTER_TYPE_P (TREE_TYPE (@0))
2071 && TREE_CODE (@1) == INTEGER_CST
2072 && tree_int_cst_sign_bit (@1) == 0))
2075 (pointer_diff (pointer_plus @@0 @1) @0)
2076 /* The second argument of pointer_plus must be interpreted as signed, and
2077 thus sign-extended if necessary. */
2078 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2079 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2080 second arg is unsigned even when we need to consider it as signed,
2081 we don't want to diagnose overflow here. */
2082 (convert (view_convert:stype @1))))
2084 /* (T)P - (T)(P + A) -> -(T) A */
2086 (minus (convert? @0)
2087 (convert (plus:c @@0 @1)))
2088 (if (INTEGRAL_TYPE_P (type)
2089 && TYPE_OVERFLOW_UNDEFINED (type)
2090 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2091 (with { tree utype = unsigned_type_for (type); }
2092 (convert (negate (convert:utype @1))))
2093 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2094 /* For integer types, if A has a smaller type
2095 than T the result depends on the possible
2097 E.g. T=size_t, A=(unsigned)429497295, P>0.
2098 However, if an overflow in P + A would cause
2099 undefined behavior, we can assume that there
2101 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2102 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2103 (negate (convert @1)))))
2106 (convert (pointer_plus @@0 @1)))
2107 (if (INTEGRAL_TYPE_P (type)
2108 && TYPE_OVERFLOW_UNDEFINED (type)
2109 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2110 (with { tree utype = unsigned_type_for (type); }
2111 (convert (negate (convert:utype @1))))
2112 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2113 /* For pointer types, if the conversion of A to the
2114 final type requires a sign- or zero-extension,
2115 then we have to punt - it is not defined which
2117 || (POINTER_TYPE_P (TREE_TYPE (@0))
2118 && TREE_CODE (@1) == INTEGER_CST
2119 && tree_int_cst_sign_bit (@1) == 0))
2120 (negate (convert @1)))))
2122 (pointer_diff @0 (pointer_plus @@0 @1))
2123 /* The second argument of pointer_plus must be interpreted as signed, and
2124 thus sign-extended if necessary. */
2125 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2126 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2127 second arg is unsigned even when we need to consider it as signed,
2128 we don't want to diagnose overflow here. */
2129 (negate (convert (view_convert:stype @1)))))
2131 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2133 (minus (convert (plus:c @@0 @1))
2134 (convert (plus:c @0 @2)))
2135 (if (INTEGRAL_TYPE_P (type)
2136 && TYPE_OVERFLOW_UNDEFINED (type)
2137 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2138 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2139 (with { tree utype = unsigned_type_for (type); }
2140 (convert (minus (convert:utype @1) (convert:utype @2))))
2141 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2142 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2143 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2144 /* For integer types, if A has a smaller type
2145 than T the result depends on the possible
2147 E.g. T=size_t, A=(unsigned)429497295, P>0.
2148 However, if an overflow in P + A would cause
2149 undefined behavior, we can assume that there
2151 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2152 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2153 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2154 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2155 (minus (convert @1) (convert @2)))))
2157 (minus (convert (pointer_plus @@0 @1))
2158 (convert (pointer_plus @0 @2)))
2159 (if (INTEGRAL_TYPE_P (type)
2160 && TYPE_OVERFLOW_UNDEFINED (type)
2161 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2162 (with { tree utype = unsigned_type_for (type); }
2163 (convert (minus (convert:utype @1) (convert:utype @2))))
2164 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2165 /* For pointer types, if the conversion of A to the
2166 final type requires a sign- or zero-extension,
2167 then we have to punt - it is not defined which
2169 || (POINTER_TYPE_P (TREE_TYPE (@0))
2170 && TREE_CODE (@1) == INTEGER_CST
2171 && tree_int_cst_sign_bit (@1) == 0
2172 && TREE_CODE (@2) == INTEGER_CST
2173 && tree_int_cst_sign_bit (@2) == 0))
2174 (minus (convert @1) (convert @2)))))
2176 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2177 /* The second argument of pointer_plus must be interpreted as signed, and
2178 thus sign-extended if necessary. */
2179 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2180 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2181 second arg is unsigned even when we need to consider it as signed,
2182 we don't want to diagnose overflow here. */
2183 (minus (convert (view_convert:stype @1))
2184 (convert (view_convert:stype @2)))))))
2186 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2187 Modeled after fold_plusminus_mult_expr. */
2188 (if (!TYPE_SATURATING (type)
2189 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2190 (for plusminus (plus minus)
2192 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2193 (if ((!ANY_INTEGRAL_TYPE_P (type)
2194 || TYPE_OVERFLOW_WRAPS (type)
2195 || (INTEGRAL_TYPE_P (type)
2196 && tree_expr_nonzero_p (@0)
2197 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2198 /* If @1 +- @2 is constant require a hard single-use on either
2199 original operand (but not on both). */
2200 && (single_use (@3) || single_use (@4)))
2201 (mult (plusminus @1 @2) @0)))
2202 /* We cannot generate constant 1 for fract. */
2203 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2205 (plusminus @0 (mult:c@3 @0 @2))
2206 (if ((!ANY_INTEGRAL_TYPE_P (type)
2207 || TYPE_OVERFLOW_WRAPS (type)
2208 || (INTEGRAL_TYPE_P (type)
2209 && tree_expr_nonzero_p (@0)
2210 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2212 (mult (plusminus { build_one_cst (type); } @2) @0)))
2214 (plusminus (mult:c@3 @0 @2) @0)
2215 (if ((!ANY_INTEGRAL_TYPE_P (type)
2216 || TYPE_OVERFLOW_WRAPS (type)
2217 || (INTEGRAL_TYPE_P (type)
2218 && tree_expr_nonzero_p (@0)
2219 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2221 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2223 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2225 (for minmax (min max FMIN_ALL FMAX_ALL)
2229 /* min(max(x,y),y) -> y. */
2231 (min:c (max:c @0 @1) @1)
2233 /* max(min(x,y),y) -> y. */
2235 (max:c (min:c @0 @1) @1)
2237 /* max(a,-a) -> abs(a). */
2239 (max:c @0 (negate @0))
2240 (if (TREE_CODE (type) != COMPLEX_TYPE
2241 && (! ANY_INTEGRAL_TYPE_P (type)
2242 || TYPE_OVERFLOW_UNDEFINED (type)))
2244 /* min(a,-a) -> -abs(a). */
2246 (min:c @0 (negate @0))
2247 (if (TREE_CODE (type) != COMPLEX_TYPE
2248 && (! ANY_INTEGRAL_TYPE_P (type)
2249 || TYPE_OVERFLOW_UNDEFINED (type)))
2254 (if (INTEGRAL_TYPE_P (type)
2255 && TYPE_MIN_VALUE (type)
2256 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2258 (if (INTEGRAL_TYPE_P (type)
2259 && TYPE_MAX_VALUE (type)
2260 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2265 (if (INTEGRAL_TYPE_P (type)
2266 && TYPE_MAX_VALUE (type)
2267 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2269 (if (INTEGRAL_TYPE_P (type)
2270 && TYPE_MIN_VALUE (type)
2271 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2274 /* max (a, a + CST) -> a + CST where CST is positive. */
2275 /* max (a, a + CST) -> a where CST is negative. */
2277 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2278 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2279 (if (tree_int_cst_sgn (@1) > 0)
2283 /* min (a, a + CST) -> a where CST is positive. */
2284 /* min (a, a + CST) -> a + CST where CST is negative. */
2286 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2287 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2288 (if (tree_int_cst_sgn (@1) > 0)
2292 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2293 and the outer convert demotes the expression back to x's type. */
2294 (for minmax (min max)
2296 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2297 (if (INTEGRAL_TYPE_P (type)
2298 && types_match (@1, type) && int_fits_type_p (@2, type)
2299 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2300 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2301 (minmax @1 (convert @2)))))
2303 (for minmax (FMIN_ALL FMAX_ALL)
2304 /* If either argument is NaN, return the other one. Avoid the
2305 transformation if we get (and honor) a signalling NaN. */
2307 (minmax:c @0 REAL_CST@1)
2308 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2309 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2311 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2312 functions to return the numeric arg if the other one is NaN.
2313 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2314 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2315 worry about it either. */
2316 (if (flag_finite_math_only)
2323 /* min (-A, -B) -> -max (A, B) */
2324 (for minmax (min max FMIN_ALL FMAX_ALL)
2325 maxmin (max min FMAX_ALL FMIN_ALL)
2327 (minmax (negate:s@2 @0) (negate:s@3 @1))
2328 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2329 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2330 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2331 (negate (maxmin @0 @1)))))
2332 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2333 MAX (~X, ~Y) -> ~MIN (X, Y) */
2334 (for minmax (min max)
2337 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2338 (bit_not (maxmin @0 @1))))
2340 /* MIN (X, Y) == X -> X <= Y */
2341 (for minmax (min min max max)
2345 (cmp:c (minmax:c @0 @1) @0)
2346 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2348 /* MIN (X, 5) == 0 -> X == 0
2349 MIN (X, 5) == 7 -> false */
2352 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2353 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2354 TYPE_SIGN (TREE_TYPE (@0))))
2355 { constant_boolean_node (cmp == NE_EXPR, type); }
2356 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2357 TYPE_SIGN (TREE_TYPE (@0))))
2361 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2362 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2363 TYPE_SIGN (TREE_TYPE (@0))))
2364 { constant_boolean_node (cmp == NE_EXPR, type); }
2365 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2366 TYPE_SIGN (TREE_TYPE (@0))))
2368 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2369 (for minmax (min min max max min min max max )
2370 cmp (lt le gt ge gt ge lt le )
2371 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2373 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2374 (comb (cmp @0 @2) (cmp @1 @2))))
2376 /* Simplifications of shift and rotates. */
2378 (for rotate (lrotate rrotate)
2380 (rotate integer_all_onesp@0 @1)
2383 /* Optimize -1 >> x for arithmetic right shifts. */
2385 (rshift integer_all_onesp@0 @1)
2386 (if (!TYPE_UNSIGNED (type)
2387 && tree_expr_nonnegative_p (@1))
2390 /* Optimize (x >> c) << c into x & (-1<<c). */
2392 (lshift (rshift @0 INTEGER_CST@1) @1)
2393 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2394 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2396 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2399 (rshift (lshift @0 INTEGER_CST@1) @1)
2400 (if (TYPE_UNSIGNED (type)
2401 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2402 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2404 (for shiftrotate (lrotate rrotate lshift rshift)
2406 (shiftrotate @0 integer_zerop)
2409 (shiftrotate integer_zerop@0 @1)
2411 /* Prefer vector1 << scalar to vector1 << vector2
2412 if vector2 is uniform. */
2413 (for vec (VECTOR_CST CONSTRUCTOR)
2415 (shiftrotate @0 vec@1)
2416 (with { tree tem = uniform_vector_p (@1); }
2418 (shiftrotate @0 { tem; }))))))
2420 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2421 Y is 0. Similarly for X >> Y. */
2423 (for shift (lshift rshift)
2425 (shift @0 SSA_NAME@1)
2426 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2428 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2429 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2431 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2435 /* Rewrite an LROTATE_EXPR by a constant into an
2436 RROTATE_EXPR by a new constant. */
2438 (lrotate @0 INTEGER_CST@1)
2439 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2440 build_int_cst (TREE_TYPE (@1),
2441 element_precision (type)), @1); }))
2443 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2444 (for op (lrotate rrotate rshift lshift)
2446 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2447 (with { unsigned int prec = element_precision (type); }
2448 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2449 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2450 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2451 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2452 (with { unsigned int low = (tree_to_uhwi (@1)
2453 + tree_to_uhwi (@2)); }
2454 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2455 being well defined. */
2457 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2458 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2459 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2460 { build_zero_cst (type); }
2461 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2462 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2465 /* ((1 << A) & 1) != 0 -> A == 0
2466 ((1 << A) & 1) == 0 -> A != 0 */
2470 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2471 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2473 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2474 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2478 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2479 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2481 || (!integer_zerop (@2)
2482 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2483 { constant_boolean_node (cmp == NE_EXPR, type); }
2484 (if (!integer_zerop (@2)
2485 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2486 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2488 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2489 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2490 if the new mask might be further optimized. */
2491 (for shift (lshift rshift)
2493 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2495 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2496 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2497 && tree_fits_uhwi_p (@1)
2498 && tree_to_uhwi (@1) > 0
2499 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2502 unsigned int shiftc = tree_to_uhwi (@1);
2503 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2504 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2505 tree shift_type = TREE_TYPE (@3);
2508 if (shift == LSHIFT_EXPR)
2509 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2510 else if (shift == RSHIFT_EXPR
2511 && type_has_mode_precision_p (shift_type))
2513 prec = TYPE_PRECISION (TREE_TYPE (@3));
2515 /* See if more bits can be proven as zero because of
2518 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2520 tree inner_type = TREE_TYPE (@0);
2521 if (type_has_mode_precision_p (inner_type)
2522 && TYPE_PRECISION (inner_type) < prec)
2524 prec = TYPE_PRECISION (inner_type);
2525 /* See if we can shorten the right shift. */
2527 shift_type = inner_type;
2528 /* Otherwise X >> C1 is all zeros, so we'll optimize
2529 it into (X, 0) later on by making sure zerobits
2533 zerobits = HOST_WIDE_INT_M1U;
2536 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2537 zerobits <<= prec - shiftc;
2539 /* For arithmetic shift if sign bit could be set, zerobits
2540 can contain actually sign bits, so no transformation is
2541 possible, unless MASK masks them all away. In that
2542 case the shift needs to be converted into logical shift. */
2543 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2544 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2546 if ((mask & zerobits) == 0)
2547 shift_type = unsigned_type_for (TREE_TYPE (@3));
2553 /* ((X << 16) & 0xff00) is (X, 0). */
2554 (if ((mask & zerobits) == mask)
2555 { build_int_cst (type, 0); }
2556 (with { newmask = mask | zerobits; }
2557 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2560 /* Only do the transformation if NEWMASK is some integer
2562 for (prec = BITS_PER_UNIT;
2563 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2564 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2567 (if (prec < HOST_BITS_PER_WIDE_INT
2568 || newmask == HOST_WIDE_INT_M1U)
2570 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2571 (if (!tree_int_cst_equal (newmaskt, @2))
2572 (if (shift_type != TREE_TYPE (@3))
2573 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2574 (bit_and @4 { newmaskt; })))))))))))))
2576 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2577 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2578 (for shift (lshift rshift)
2579 (for bit_op (bit_and bit_xor bit_ior)
2581 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2582 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2583 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2584 (bit_op (shift (convert @0) @1) { mask; }))))))
2586 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2588 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2589 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2590 && (element_precision (TREE_TYPE (@0))
2591 <= element_precision (TREE_TYPE (@1))
2592 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2594 { tree shift_type = TREE_TYPE (@0); }
2595 (convert (rshift (convert:shift_type @1) @2)))))
2597 /* ~(~X >>r Y) -> X >>r Y
2598 ~(~X <<r Y) -> X <<r Y */
2599 (for rotate (lrotate rrotate)
2601 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2602 (if ((element_precision (TREE_TYPE (@0))
2603 <= element_precision (TREE_TYPE (@1))
2604 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2605 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2606 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2608 { tree rotate_type = TREE_TYPE (@0); }
2609 (convert (rotate (convert:rotate_type @1) @2))))))
2611 /* Simplifications of conversions. */
2613 /* Basic strip-useless-type-conversions / strip_nops. */
2614 (for cvt (convert view_convert float fix_trunc)
2617 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2618 || (GENERIC && type == TREE_TYPE (@0)))
2621 /* Contract view-conversions. */
2623 (view_convert (view_convert @0))
2626 /* For integral conversions with the same precision or pointer
2627 conversions use a NOP_EXPR instead. */
2630 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2631 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2632 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2635 /* Strip inner integral conversions that do not change precision or size, or
2636 zero-extend while keeping the same size (for bool-to-char). */
2638 (view_convert (convert@0 @1))
2639 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2640 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2641 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2642 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2643 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2644 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2647 /* Simplify a view-converted empty constructor. */
2649 (view_convert CONSTRUCTOR@0)
2650 (if (TREE_CODE (@0) != SSA_NAME
2651 && CONSTRUCTOR_NELTS (@0) == 0)
2652 { build_zero_cst (type); }))
2654 /* Re-association barriers around constants and other re-association
2655 barriers can be removed. */
2657 (paren CONSTANT_CLASS_P@0)
2660 (paren (paren@1 @0))
2663 /* Handle cases of two conversions in a row. */
2664 (for ocvt (convert float fix_trunc)
2665 (for icvt (convert float)
2670 tree inside_type = TREE_TYPE (@0);
2671 tree inter_type = TREE_TYPE (@1);
2672 int inside_int = INTEGRAL_TYPE_P (inside_type);
2673 int inside_ptr = POINTER_TYPE_P (inside_type);
2674 int inside_float = FLOAT_TYPE_P (inside_type);
2675 int inside_vec = VECTOR_TYPE_P (inside_type);
2676 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2677 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2678 int inter_int = INTEGRAL_TYPE_P (inter_type);
2679 int inter_ptr = POINTER_TYPE_P (inter_type);
2680 int inter_float = FLOAT_TYPE_P (inter_type);
2681 int inter_vec = VECTOR_TYPE_P (inter_type);
2682 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2683 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2684 int final_int = INTEGRAL_TYPE_P (type);
2685 int final_ptr = POINTER_TYPE_P (type);
2686 int final_float = FLOAT_TYPE_P (type);
2687 int final_vec = VECTOR_TYPE_P (type);
2688 unsigned int final_prec = TYPE_PRECISION (type);
2689 int final_unsignedp = TYPE_UNSIGNED (type);
2692 /* In addition to the cases of two conversions in a row
2693 handled below, if we are converting something to its own
2694 type via an object of identical or wider precision, neither
2695 conversion is needed. */
2696 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2698 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2699 && (((inter_int || inter_ptr) && final_int)
2700 || (inter_float && final_float))
2701 && inter_prec >= final_prec)
2704 /* Likewise, if the intermediate and initial types are either both
2705 float or both integer, we don't need the middle conversion if the
2706 former is wider than the latter and doesn't change the signedness
2707 (for integers). Avoid this if the final type is a pointer since
2708 then we sometimes need the middle conversion. */
2709 (if (((inter_int && inside_int) || (inter_float && inside_float))
2710 && (final_int || final_float)
2711 && inter_prec >= inside_prec
2712 && (inter_float || inter_unsignedp == inside_unsignedp))
2715 /* If we have a sign-extension of a zero-extended value, we can
2716 replace that by a single zero-extension. Likewise if the
2717 final conversion does not change precision we can drop the
2718 intermediate conversion. */
2719 (if (inside_int && inter_int && final_int
2720 && ((inside_prec < inter_prec && inter_prec < final_prec
2721 && inside_unsignedp && !inter_unsignedp)
2722 || final_prec == inter_prec))
2725 /* Two conversions in a row are not needed unless:
2726 - some conversion is floating-point (overstrict for now), or
2727 - some conversion is a vector (overstrict for now), or
2728 - the intermediate type is narrower than both initial and
2730 - the intermediate type and innermost type differ in signedness,
2731 and the outermost type is wider than the intermediate, or
2732 - the initial type is a pointer type and the precisions of the
2733 intermediate and final types differ, or
2734 - the final type is a pointer type and the precisions of the
2735 initial and intermediate types differ. */
2736 (if (! inside_float && ! inter_float && ! final_float
2737 && ! inside_vec && ! inter_vec && ! final_vec
2738 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2739 && ! (inside_int && inter_int
2740 && inter_unsignedp != inside_unsignedp
2741 && inter_prec < final_prec)
2742 && ((inter_unsignedp && inter_prec > inside_prec)
2743 == (final_unsignedp && final_prec > inter_prec))
2744 && ! (inside_ptr && inter_prec != final_prec)
2745 && ! (final_ptr && inside_prec != inter_prec))
2748 /* A truncation to an unsigned type (a zero-extension) should be
2749 canonicalized as bitwise and of a mask. */
2750 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2751 && final_int && inter_int && inside_int
2752 && final_prec == inside_prec
2753 && final_prec > inter_prec
2755 (convert (bit_and @0 { wide_int_to_tree
2757 wi::mask (inter_prec, false,
2758 TYPE_PRECISION (inside_type))); })))
2760 /* If we are converting an integer to a floating-point that can
2761 represent it exactly and back to an integer, we can skip the
2762 floating-point conversion. */
2763 (if (GIMPLE /* PR66211 */
2764 && inside_int && inter_float && final_int &&
2765 (unsigned) significand_size (TYPE_MODE (inter_type))
2766 >= inside_prec - !inside_unsignedp)
2769 /* If we have a narrowing conversion to an integral type that is fed by a
2770 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2771 masks off bits outside the final type (and nothing else). */
2773 (convert (bit_and @0 INTEGER_CST@1))
2774 (if (INTEGRAL_TYPE_P (type)
2775 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2776 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2777 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2778 TYPE_PRECISION (type)), 0))
2782 /* (X /[ex] A) * A -> X. */
2784 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2787 /* Simplify (A / B) * B + (A % B) -> A. */
2788 (for div (trunc_div ceil_div floor_div round_div)
2789 mod (trunc_mod ceil_mod floor_mod round_mod)
2791 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
2794 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
2795 (for op (plus minus)
2797 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2798 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2799 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
2802 wi::overflow_type overflow;
2803 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
2804 TYPE_SIGN (type), &overflow);
2806 (if (types_match (type, TREE_TYPE (@2))
2807 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
2808 (op @0 { wide_int_to_tree (type, mul); })
2809 (with { tree utype = unsigned_type_for (type); }
2810 (convert (op (convert:utype @0)
2811 (mult (convert:utype @1) (convert:utype @2))))))))))
2813 /* Canonicalization of binary operations. */
2815 /* Convert X + -C into X - C. */
2817 (plus @0 REAL_CST@1)
2818 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2819 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2820 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2821 (minus @0 { tem; })))))
2823 /* Convert x+x into x*2. */
2826 (if (SCALAR_FLOAT_TYPE_P (type))
2827 (mult @0 { build_real (type, dconst2); })
2828 (if (INTEGRAL_TYPE_P (type))
2829 (mult @0 { build_int_cst (type, 2); }))))
2833 (minus integer_zerop @1)
2836 (pointer_diff integer_zerop @1)
2837 (negate (convert @1)))
2839 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2840 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2841 (-ARG1 + ARG0) reduces to -ARG1. */
2843 (minus real_zerop@0 @1)
2844 (if (fold_real_zero_addition_p (type, @0, 0))
2847 /* Transform x * -1 into -x. */
2849 (mult @0 integer_minus_onep)
2852 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2853 signed overflow for CST != 0 && CST != -1. */
2855 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2856 (if (TREE_CODE (@2) != INTEGER_CST
2858 && !integer_zerop (@1) && !integer_minus_onep (@1))
2859 (mult (mult @0 @2) @1)))
2861 /* True if we can easily extract the real and imaginary parts of a complex
2863 (match compositional_complex
2864 (convert? (complex @0 @1)))
2866 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2868 (complex (realpart @0) (imagpart @0))
2871 (realpart (complex @0 @1))
2874 (imagpart (complex @0 @1))
2877 /* Sometimes we only care about half of a complex expression. */
2879 (realpart (convert?:s (conj:s @0)))
2880 (convert (realpart @0)))
2882 (imagpart (convert?:s (conj:s @0)))
2883 (convert (negate (imagpart @0))))
2884 (for part (realpart imagpart)
2885 (for op (plus minus)
2887 (part (convert?:s@2 (op:s @0 @1)))
2888 (convert (op (part @0) (part @1))))))
2890 (realpart (convert?:s (CEXPI:s @0)))
2893 (imagpart (convert?:s (CEXPI:s @0)))
2896 /* conj(conj(x)) -> x */
2898 (conj (convert? (conj @0)))
2899 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2902 /* conj({x,y}) -> {x,-y} */
2904 (conj (convert?:s (complex:s @0 @1)))
2905 (with { tree itype = TREE_TYPE (type); }
2906 (complex (convert:itype @0) (negate (convert:itype @1)))))
2908 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2909 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2914 (bswap (bit_not (bswap @0)))
2916 (for bitop (bit_xor bit_ior bit_and)
2918 (bswap (bitop:c (bswap @0) @1))
2919 (bitop @0 (bswap @1)))))
2922 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2924 /* Simplify constant conditions.
2925 Only optimize constant conditions when the selected branch
2926 has the same type as the COND_EXPR. This avoids optimizing
2927 away "c ? x : throw", where the throw has a void type.
2928 Note that we cannot throw away the fold-const.c variant nor
2929 this one as we depend on doing this transform before possibly
2930 A ? B : B -> B triggers and the fold-const.c one can optimize
2931 0 ? A : B to B even if A has side-effects. Something
2932 genmatch cannot handle. */
2934 (cond INTEGER_CST@0 @1 @2)
2935 (if (integer_zerop (@0))
2936 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2938 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2941 (vec_cond VECTOR_CST@0 @1 @2)
2942 (if (integer_all_onesp (@0))
2944 (if (integer_zerop (@0))
2947 /* Sink unary operations to constant branches, but only if we do fold it to
2949 (for op (negate bit_not abs absu)
2951 (op (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2))
2955 cst1 = const_unop (op, type, @1);
2957 cst2 = const_unop (op, type, @2);
2960 (vec_cond @0 { cst1; } { cst2; })))))
2962 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2964 /* This pattern implements two kinds simplification:
2967 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2968 1) Conversions are type widening from smaller type.
2969 2) Const c1 equals to c2 after canonicalizing comparison.
2970 3) Comparison has tree code LT, LE, GT or GE.
2971 This specific pattern is needed when (cmp (convert x) c) may not
2972 be simplified by comparison patterns because of multiple uses of
2973 x. It also makes sense here because simplifying across multiple
2974 referred var is always benefitial for complicated cases.
2977 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2978 (for cmp (lt le gt ge eq)
2980 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2983 tree from_type = TREE_TYPE (@1);
2984 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2985 enum tree_code code = ERROR_MARK;
2987 if (INTEGRAL_TYPE_P (from_type)
2988 && int_fits_type_p (@2, from_type)
2989 && (types_match (c1_type, from_type)
2990 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2991 && (TYPE_UNSIGNED (from_type)
2992 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2993 && (types_match (c2_type, from_type)
2994 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2995 && (TYPE_UNSIGNED (from_type)
2996 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3000 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3002 /* X <= Y - 1 equals to X < Y. */
3005 /* X > Y - 1 equals to X >= Y. */
3009 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3011 /* X < Y + 1 equals to X <= Y. */
3014 /* X >= Y + 1 equals to X > Y. */
3018 if (code != ERROR_MARK
3019 || wi::to_widest (@2) == wi::to_widest (@3))
3021 if (cmp == LT_EXPR || cmp == LE_EXPR)
3023 if (cmp == GT_EXPR || cmp == GE_EXPR)
3027 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3028 else if (int_fits_type_p (@3, from_type))
3032 (if (code == MAX_EXPR)
3033 (convert (max @1 (convert @2)))
3034 (if (code == MIN_EXPR)
3035 (convert (min @1 (convert @2)))
3036 (if (code == EQ_EXPR)
3037 (convert (cond (eq @1 (convert @3))
3038 (convert:from_type @3) (convert:from_type @2)))))))))
3040 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3042 1) OP is PLUS or MINUS.
3043 2) CMP is LT, LE, GT or GE.
3044 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3046 This pattern also handles special cases like:
3048 A) Operand x is a unsigned to signed type conversion and c1 is
3049 integer zero. In this case,
3050 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3051 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3052 B) Const c1 may not equal to (C3 op' C2). In this case we also
3053 check equality for (c1+1) and (c1-1) by adjusting comparison
3056 TODO: Though signed type is handled by this pattern, it cannot be
3057 simplified at the moment because C standard requires additional
3058 type promotion. In order to match&simplify it here, the IR needs
3059 to be cleaned up by other optimizers, i.e, VRP. */
3060 (for op (plus minus)
3061 (for cmp (lt le gt ge)
3063 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3064 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3065 (if (types_match (from_type, to_type)
3066 /* Check if it is special case A). */
3067 || (TYPE_UNSIGNED (from_type)
3068 && !TYPE_UNSIGNED (to_type)
3069 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3070 && integer_zerop (@1)
3071 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3074 wi::overflow_type overflow = wi::OVF_NONE;
3075 enum tree_code code, cmp_code = cmp;
3077 wide_int c1 = wi::to_wide (@1);
3078 wide_int c2 = wi::to_wide (@2);
3079 wide_int c3 = wi::to_wide (@3);
3080 signop sgn = TYPE_SIGN (from_type);
3082 /* Handle special case A), given x of unsigned type:
3083 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3084 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3085 if (!types_match (from_type, to_type))
3087 if (cmp_code == LT_EXPR)
3089 if (cmp_code == GE_EXPR)
3091 c1 = wi::max_value (to_type);
3093 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3094 compute (c3 op' c2) and check if it equals to c1 with op' being
3095 the inverted operator of op. Make sure overflow doesn't happen
3096 if it is undefined. */
3097 if (op == PLUS_EXPR)
3098 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3100 real_c1 = wi::add (c3, c2, sgn, &overflow);
3103 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3105 /* Check if c1 equals to real_c1. Boundary condition is handled
3106 by adjusting comparison operation if necessary. */
3107 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3110 /* X <= Y - 1 equals to X < Y. */
3111 if (cmp_code == LE_EXPR)
3113 /* X > Y - 1 equals to X >= Y. */
3114 if (cmp_code == GT_EXPR)
3117 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3120 /* X < Y + 1 equals to X <= Y. */
3121 if (cmp_code == LT_EXPR)
3123 /* X >= Y + 1 equals to X > Y. */
3124 if (cmp_code == GE_EXPR)
3127 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3129 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3131 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3136 (if (code == MAX_EXPR)
3137 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3138 { wide_int_to_tree (from_type, c2); })
3139 (if (code == MIN_EXPR)
3140 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3141 { wide_int_to_tree (from_type, c2); })))))))))
3143 (for cnd (cond vec_cond)
3144 /* A ? B : (A ? X : C) -> A ? B : C. */
3146 (cnd @0 (cnd @0 @1 @2) @3)
3149 (cnd @0 @1 (cnd @0 @2 @3))
3151 /* A ? B : (!A ? C : X) -> A ? B : C. */
3152 /* ??? This matches embedded conditions open-coded because genmatch
3153 would generate matching code for conditions in separate stmts only.
3154 The following is still important to merge then and else arm cases
3155 from if-conversion. */
3157 (cnd @0 @1 (cnd @2 @3 @4))
3158 (if (inverse_conditions_p (@0, @2))
3161 (cnd @0 (cnd @1 @2 @3) @4)
3162 (if (inverse_conditions_p (@0, @1))
3165 /* A ? B : B -> B. */
3170 /* !A ? B : C -> A ? C : B. */
3172 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3175 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3176 return all -1 or all 0 results. */
3177 /* ??? We could instead convert all instances of the vec_cond to negate,
3178 but that isn't necessarily a win on its own. */
3180 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3181 (if (VECTOR_TYPE_P (type)
3182 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3183 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3184 && (TYPE_MODE (TREE_TYPE (type))
3185 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3186 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3188 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3190 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3191 (if (VECTOR_TYPE_P (type)
3192 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3193 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3194 && (TYPE_MODE (TREE_TYPE (type))
3195 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3196 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3199 /* Simplifications of comparisons. */
3201 /* See if we can reduce the magnitude of a constant involved in a
3202 comparison by changing the comparison code. This is a canonicalization
3203 formerly done by maybe_canonicalize_comparison_1. */
3207 (cmp @0 uniform_integer_cst_p@1)
3208 (with { tree cst = uniform_integer_cst_p (@1); }
3209 (if (tree_int_cst_sgn (cst) == -1)
3210 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3211 wide_int_to_tree (TREE_TYPE (cst),
3217 (cmp @0 uniform_integer_cst_p@1)
3218 (with { tree cst = uniform_integer_cst_p (@1); }
3219 (if (tree_int_cst_sgn (cst) == 1)
3220 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3221 wide_int_to_tree (TREE_TYPE (cst),
3222 wi::to_wide (cst) - 1)); })))))
3224 /* We can simplify a logical negation of a comparison to the
3225 inverted comparison. As we cannot compute an expression
3226 operator using invert_tree_comparison we have to simulate
3227 that with expression code iteration. */
3228 (for cmp (tcc_comparison)
3229 icmp (inverted_tcc_comparison)
3230 ncmp (inverted_tcc_comparison_with_nans)
3231 /* Ideally we'd like to combine the following two patterns
3232 and handle some more cases by using
3233 (logical_inverted_value (cmp @0 @1))
3234 here but for that genmatch would need to "inline" that.
3235 For now implement what forward_propagate_comparison did. */
3237 (bit_not (cmp @0 @1))
3238 (if (VECTOR_TYPE_P (type)
3239 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3240 /* Comparison inversion may be impossible for trapping math,
3241 invert_tree_comparison will tell us. But we can't use
3242 a computed operator in the replacement tree thus we have
3243 to play the trick below. */
3244 (with { enum tree_code ic = invert_tree_comparison
3245 (cmp, HONOR_NANS (@0)); }
3251 (bit_xor (cmp @0 @1) integer_truep)
3252 (with { enum tree_code ic = invert_tree_comparison
3253 (cmp, HONOR_NANS (@0)); }
3259 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3260 ??? The transformation is valid for the other operators if overflow
3261 is undefined for the type, but performing it here badly interacts
3262 with the transformation in fold_cond_expr_with_comparison which
3263 attempts to synthetize ABS_EXPR. */
3265 (for sub (minus pointer_diff)
3267 (cmp (sub@2 @0 @1) integer_zerop)
3268 (if (single_use (@2))
3271 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3272 signed arithmetic case. That form is created by the compiler
3273 often enough for folding it to be of value. One example is in
3274 computing loop trip counts after Operator Strength Reduction. */
3275 (for cmp (simple_comparison)
3276 scmp (swapped_simple_comparison)
3278 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3279 /* Handle unfolded multiplication by zero. */
3280 (if (integer_zerop (@1))
3282 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3283 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3285 /* If @1 is negative we swap the sense of the comparison. */
3286 (if (tree_int_cst_sgn (@1) < 0)
3290 /* Simplify comparison of something with itself. For IEEE
3291 floating-point, we can only do some of these simplifications. */
3295 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3296 || ! HONOR_NANS (@0))
3297 { constant_boolean_node (true, type); }
3298 (if (cmp != EQ_EXPR)
3304 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3305 || ! HONOR_NANS (@0))
3306 { constant_boolean_node (false, type); })))
3307 (for cmp (unle unge uneq)
3310 { constant_boolean_node (true, type); }))
3311 (for cmp (unlt ungt)
3317 (if (!flag_trapping_math)
3318 { constant_boolean_node (false, type); }))
3320 /* Fold ~X op ~Y as Y op X. */
3321 (for cmp (simple_comparison)
3323 (cmp (bit_not@2 @0) (bit_not@3 @1))
3324 (if (single_use (@2) && single_use (@3))
3327 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3328 (for cmp (simple_comparison)
3329 scmp (swapped_simple_comparison)
3331 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3332 (if (single_use (@2)
3333 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3334 (scmp @0 (bit_not @1)))))
3336 (for cmp (simple_comparison)
3337 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3339 (cmp (convert@2 @0) (convert? @1))
3340 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3341 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3342 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3343 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3344 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3347 tree type1 = TREE_TYPE (@1);
3348 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3350 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3351 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3352 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3353 type1 = float_type_node;
3354 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3355 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3356 type1 = double_type_node;
3359 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3360 ? TREE_TYPE (@0) : type1);
3362 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3363 (cmp (convert:newtype @0) (convert:newtype @1))))))
3367 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3369 /* a CMP (-0) -> a CMP 0 */
3370 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3371 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3372 /* x != NaN is always true, other ops are always false. */
3373 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3374 && ! HONOR_SNANS (@1))
3375 { constant_boolean_node (cmp == NE_EXPR, type); })
3376 /* Fold comparisons against infinity. */
3377 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3378 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3381 REAL_VALUE_TYPE max;
3382 enum tree_code code = cmp;
3383 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3385 code = swap_tree_comparison (code);
3388 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3389 (if (code == GT_EXPR
3390 && !(HONOR_NANS (@0) && flag_trapping_math))
3391 { constant_boolean_node (false, type); })
3392 (if (code == LE_EXPR)
3393 /* x <= +Inf is always true, if we don't care about NaNs. */
3394 (if (! HONOR_NANS (@0))
3395 { constant_boolean_node (true, type); }
3396 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3397 an "invalid" exception. */
3398 (if (!flag_trapping_math)
3400 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3401 for == this introduces an exception for x a NaN. */
3402 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3404 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3406 (lt @0 { build_real (TREE_TYPE (@0), max); })
3407 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3408 /* x < +Inf is always equal to x <= DBL_MAX. */
3409 (if (code == LT_EXPR)
3410 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3412 (ge @0 { build_real (TREE_TYPE (@0), max); })
3413 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3414 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3415 an exception for x a NaN so use an unordered comparison. */
3416 (if (code == NE_EXPR)
3417 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3418 (if (! HONOR_NANS (@0))
3420 (ge @0 { build_real (TREE_TYPE (@0), max); })
3421 (le @0 { build_real (TREE_TYPE (@0), max); }))
3423 (unge @0 { build_real (TREE_TYPE (@0), max); })
3424 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3426 /* If this is a comparison of a real constant with a PLUS_EXPR
3427 or a MINUS_EXPR of a real constant, we can convert it into a
3428 comparison with a revised real constant as long as no overflow
3429 occurs when unsafe_math_optimizations are enabled. */
3430 (if (flag_unsafe_math_optimizations)
3431 (for op (plus minus)
3433 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3436 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3437 TREE_TYPE (@1), @2, @1);
3439 (if (tem && !TREE_OVERFLOW (tem))
3440 (cmp @0 { tem; }))))))
3442 /* Likewise, we can simplify a comparison of a real constant with
3443 a MINUS_EXPR whose first operand is also a real constant, i.e.
3444 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3445 floating-point types only if -fassociative-math is set. */
3446 (if (flag_associative_math)
3448 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3449 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3450 (if (tem && !TREE_OVERFLOW (tem))
3451 (cmp { tem; } @1)))))
3453 /* Fold comparisons against built-in math functions. */
3454 (if (flag_unsafe_math_optimizations
3455 && ! flag_errno_math)
3458 (cmp (sq @0) REAL_CST@1)
3460 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3462 /* sqrt(x) < y is always false, if y is negative. */
3463 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3464 { constant_boolean_node (false, type); })
3465 /* sqrt(x) > y is always true, if y is negative and we
3466 don't care about NaNs, i.e. negative values of x. */
3467 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3468 { constant_boolean_node (true, type); })
3469 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3470 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3471 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3473 /* sqrt(x) < 0 is always false. */
3474 (if (cmp == LT_EXPR)
3475 { constant_boolean_node (false, type); })
3476 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3477 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3478 { constant_boolean_node (true, type); })
3479 /* sqrt(x) <= 0 -> x == 0. */
3480 (if (cmp == LE_EXPR)
3482 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3483 == or !=. In the last case:
3485 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3487 if x is negative or NaN. Due to -funsafe-math-optimizations,
3488 the results for other x follow from natural arithmetic. */
3490 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3494 real_arithmetic (&c2, MULT_EXPR,
3495 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3496 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3498 (if (REAL_VALUE_ISINF (c2))
3499 /* sqrt(x) > y is x == +Inf, when y is very large. */
3500 (if (HONOR_INFINITIES (@0))
3501 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3502 { constant_boolean_node (false, type); })
3503 /* sqrt(x) > c is the same as x > c*c. */
3504 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3505 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3509 real_arithmetic (&c2, MULT_EXPR,
3510 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3511 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3513 (if (REAL_VALUE_ISINF (c2))
3515 /* sqrt(x) < y is always true, when y is a very large
3516 value and we don't care about NaNs or Infinities. */
3517 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3518 { constant_boolean_node (true, type); })
3519 /* sqrt(x) < y is x != +Inf when y is very large and we
3520 don't care about NaNs. */
3521 (if (! HONOR_NANS (@0))
3522 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3523 /* sqrt(x) < y is x >= 0 when y is very large and we
3524 don't care about Infinities. */
3525 (if (! HONOR_INFINITIES (@0))
3526 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3527 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3530 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3531 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3532 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3533 (if (! HONOR_NANS (@0))
3534 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3535 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3538 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3539 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3540 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3542 (cmp (sq @0) (sq @1))
3543 (if (! HONOR_NANS (@0))
3546 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
3547 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3548 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
3550 (cmp (float@0 @1) (float @2))
3551 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3552 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3555 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3556 tree type1 = TREE_TYPE (@1);
3557 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3558 tree type2 = TREE_TYPE (@2);
3559 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3561 (if (fmt.can_represent_integral_type_p (type1)
3562 && fmt.can_represent_integral_type_p (type2))
3563 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3564 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3565 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3566 && type1_signed_p >= type2_signed_p)
3567 (icmp @1 (convert @2))
3568 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3569 && type1_signed_p <= type2_signed_p)
3570 (icmp (convert:type2 @1) @2)
3571 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3572 && type1_signed_p == type2_signed_p)
3573 (icmp @1 @2))))))))))
3575 /* Optimize various special cases of (FTYPE) N CMP CST. */
3576 (for cmp (lt le eq ne ge gt)
3577 icmp (le le eq ne ge ge)
3579 (cmp (float @0) REAL_CST@1)
3580 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3581 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3584 tree itype = TREE_TYPE (@0);
3585 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3586 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3587 /* Be careful to preserve any potential exceptions due to
3588 NaNs. qNaNs are ok in == or != context.
3589 TODO: relax under -fno-trapping-math or
3590 -fno-signaling-nans. */
3592 = real_isnan (cst) && (cst->signalling
3593 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3595 /* TODO: allow non-fitting itype and SNaNs when
3596 -fno-trapping-math. */
3597 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
3600 signop isign = TYPE_SIGN (itype);
3601 REAL_VALUE_TYPE imin, imax;
3602 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3603 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3605 REAL_VALUE_TYPE icst;
3606 if (cmp == GT_EXPR || cmp == GE_EXPR)
3607 real_ceil (&icst, fmt, cst);
3608 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3609 real_floor (&icst, fmt, cst);
3611 real_trunc (&icst, fmt, cst);
3613 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3615 bool overflow_p = false;
3617 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3620 /* Optimize cases when CST is outside of ITYPE's range. */
3621 (if (real_compare (LT_EXPR, cst, &imin))
3622 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3624 (if (real_compare (GT_EXPR, cst, &imax))
3625 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3627 /* Remove cast if CST is an integer representable by ITYPE. */
3629 (cmp @0 { gcc_assert (!overflow_p);
3630 wide_int_to_tree (itype, icst_val); })
3632 /* When CST is fractional, optimize
3633 (FTYPE) N == CST -> 0
3634 (FTYPE) N != CST -> 1. */
3635 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3636 { constant_boolean_node (cmp == NE_EXPR, type); })
3637 /* Otherwise replace with sensible integer constant. */
3640 gcc_checking_assert (!overflow_p);
3642 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3644 /* Fold A /[ex] B CMP C to A CMP B * C. */
3647 (cmp (exact_div @0 @1) INTEGER_CST@2)
3648 (if (!integer_zerop (@1))
3649 (if (wi::to_wide (@2) == 0)
3651 (if (TREE_CODE (@1) == INTEGER_CST)
3654 wi::overflow_type ovf;
3655 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3656 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3659 { constant_boolean_node (cmp == NE_EXPR, type); }
3660 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3661 (for cmp (lt le gt ge)
3663 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3664 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3667 wi::overflow_type ovf;
3668 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3669 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3672 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3673 TYPE_SIGN (TREE_TYPE (@2)))
3674 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3675 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3677 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
3679 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
3680 For large C (more than min/B+2^size), this is also true, with the
3681 multiplication computed modulo 2^size.
3682 For intermediate C, this just tests the sign of A. */
3683 (for cmp (lt le gt ge)
3686 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
3687 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
3688 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
3689 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3692 tree utype = TREE_TYPE (@2);
3693 wide_int denom = wi::to_wide (@1);
3694 wide_int right = wi::to_wide (@2);
3695 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
3696 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
3697 bool small = wi::leu_p (right, smax);
3698 bool large = wi::geu_p (right, smin);
3700 (if (small || large)
3701 (cmp (convert:utype @0) (mult @2 (convert @1)))
3702 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
3704 /* Unordered tests if either argument is a NaN. */
3706 (bit_ior (unordered @0 @0) (unordered @1 @1))
3707 (if (types_match (@0, @1))
3710 (bit_and (ordered @0 @0) (ordered @1 @1))
3711 (if (types_match (@0, @1))
3714 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3717 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3720 /* Simple range test simplifications. */
3721 /* A < B || A >= B -> true. */
3722 (for test1 (lt le le le ne ge)
3723 test2 (ge gt ge ne eq ne)
3725 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3726 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3727 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3728 { constant_boolean_node (true, type); })))
3729 /* A < B && A >= B -> false. */
3730 (for test1 (lt lt lt le ne eq)
3731 test2 (ge gt eq gt eq gt)
3733 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3734 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3735 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3736 { constant_boolean_node (false, type); })))
3738 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3739 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3741 Note that comparisons
3742 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3743 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3744 will be canonicalized to above so there's no need to
3751 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3752 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3755 tree ty = TREE_TYPE (@0);
3756 unsigned prec = TYPE_PRECISION (ty);
3757 wide_int mask = wi::to_wide (@2, prec);
3758 wide_int rhs = wi::to_wide (@3, prec);
3759 signop sgn = TYPE_SIGN (ty);
3761 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3762 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3763 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3764 { build_zero_cst (ty); }))))))
3766 /* -A CMP -B -> B CMP A. */
3767 (for cmp (tcc_comparison)
3768 scmp (swapped_tcc_comparison)
3770 (cmp (negate @0) (negate @1))
3771 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3772 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3773 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3776 (cmp (negate @0) CONSTANT_CLASS_P@1)
3777 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3778 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3779 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3780 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3781 (if (tem && !TREE_OVERFLOW (tem))
3782 (scmp @0 { tem; }))))))
3784 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3787 (op (abs @0) zerop@1)
3790 /* From fold_sign_changed_comparison and fold_widened_comparison.
3791 FIXME: the lack of symmetry is disturbing. */
3792 (for cmp (simple_comparison)
3794 (cmp (convert@0 @00) (convert?@1 @10))
3795 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3796 /* Disable this optimization if we're casting a function pointer
3797 type on targets that require function pointer canonicalization. */
3798 && !(targetm.have_canonicalize_funcptr_for_compare ()
3799 && ((POINTER_TYPE_P (TREE_TYPE (@00))
3800 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3801 || (POINTER_TYPE_P (TREE_TYPE (@10))
3802 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
3804 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3805 && (TREE_CODE (@10) == INTEGER_CST
3807 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3810 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3811 /* ??? The special-casing of INTEGER_CST conversion was in the original
3812 code and here to avoid a spurious overflow flag on the resulting
3813 constant which fold_convert produces. */
3814 (if (TREE_CODE (@1) == INTEGER_CST)
3815 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3816 TREE_OVERFLOW (@1)); })
3817 (cmp @00 (convert @1)))
3819 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3820 /* If possible, express the comparison in the shorter mode. */
3821 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3822 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3823 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3824 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3825 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3826 || ((TYPE_PRECISION (TREE_TYPE (@00))
3827 >= TYPE_PRECISION (TREE_TYPE (@10)))
3828 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3829 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3830 || (TREE_CODE (@10) == INTEGER_CST
3831 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3832 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3833 (cmp @00 (convert @10))
3834 (if (TREE_CODE (@10) == INTEGER_CST
3835 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3836 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3839 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3840 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3841 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3842 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3844 (if (above || below)
3845 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3846 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3847 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3848 { constant_boolean_node (above ? true : false, type); }
3849 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3850 { constant_boolean_node (above ? false : true, type); }))))))))))))
3853 /* A local variable can never be pointed to by
3854 the default SSA name of an incoming parameter.
3855 SSA names are canonicalized to 2nd place. */
3857 (cmp addr@0 SSA_NAME@1)
3858 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3859 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3860 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3861 (if (TREE_CODE (base) == VAR_DECL
3862 && auto_var_in_fn_p (base, current_function_decl))
3863 (if (cmp == NE_EXPR)
3864 { constant_boolean_node (true, type); }
3865 { constant_boolean_node (false, type); }))))))
3867 /* Equality compare simplifications from fold_binary */
3870 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3871 Similarly for NE_EXPR. */
3873 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3874 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3875 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3876 { constant_boolean_node (cmp == NE_EXPR, type); }))
3878 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3880 (cmp (bit_xor @0 @1) integer_zerop)
3883 /* (X ^ Y) == Y becomes X == 0.
3884 Likewise (X ^ Y) == X becomes Y == 0. */
3886 (cmp:c (bit_xor:c @0 @1) @0)
3887 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3889 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3891 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3892 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3893 (cmp @0 (bit_xor @1 (convert @2)))))
3896 (cmp (convert? addr@0) integer_zerop)
3897 (if (tree_single_nonzero_warnv_p (@0, NULL))
3898 { constant_boolean_node (cmp == NE_EXPR, type); })))
3900 /* If we have (A & C) == C where C is a power of 2, convert this into
3901 (A & C) != 0. Similarly for NE_EXPR. */
3905 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3906 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3908 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3909 convert this into a shift followed by ANDing with D. */
3912 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3913 INTEGER_CST@2 integer_zerop)
3914 (if (integer_pow2p (@2))
3916 int shift = (wi::exact_log2 (wi::to_wide (@2))
3917 - wi::exact_log2 (wi::to_wide (@1)));
3921 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3923 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3926 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3927 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3931 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3932 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3933 && type_has_mode_precision_p (TREE_TYPE (@0))
3934 && element_precision (@2) >= element_precision (@0)
3935 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3936 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3937 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3939 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3940 this into a right shift or sign extension followed by ANDing with C. */
3943 (lt @0 integer_zerop)
3944 INTEGER_CST@1 integer_zerop)
3945 (if (integer_pow2p (@1)
3946 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3948 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3952 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3954 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3955 sign extension followed by AND with C will achieve the effect. */
3956 (bit_and (convert @0) @1)))))
3958 /* When the addresses are not directly of decls compare base and offset.
3959 This implements some remaining parts of fold_comparison address
3960 comparisons but still no complete part of it. Still it is good
3961 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3962 (for cmp (simple_comparison)
3964 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3967 poly_int64 off0, off1;
3968 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3969 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3970 if (base0 && TREE_CODE (base0) == MEM_REF)
3972 off0 += mem_ref_offset (base0).force_shwi ();
3973 base0 = TREE_OPERAND (base0, 0);
3975 if (base1 && TREE_CODE (base1) == MEM_REF)
3977 off1 += mem_ref_offset (base1).force_shwi ();
3978 base1 = TREE_OPERAND (base1, 0);
3981 (if (base0 && base1)
3985 /* Punt in GENERIC on variables with value expressions;
3986 the value expressions might point to fields/elements
3987 of other vars etc. */
3989 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3990 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3992 else if (decl_in_symtab_p (base0)
3993 && decl_in_symtab_p (base1))
3994 equal = symtab_node::get_create (base0)
3995 ->equal_address_to (symtab_node::get_create (base1));
3996 else if ((DECL_P (base0)
3997 || TREE_CODE (base0) == SSA_NAME
3998 || TREE_CODE (base0) == STRING_CST)
4000 || TREE_CODE (base1) == SSA_NAME
4001 || TREE_CODE (base1) == STRING_CST))
4002 equal = (base0 == base1);
4005 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4006 off0.is_constant (&ioff0);
4007 off1.is_constant (&ioff1);
4008 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4009 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4010 || (TREE_CODE (base0) == STRING_CST
4011 && TREE_CODE (base1) == STRING_CST
4012 && ioff0 >= 0 && ioff1 >= 0
4013 && ioff0 < TREE_STRING_LENGTH (base0)
4014 && ioff1 < TREE_STRING_LENGTH (base1)
4015 /* This is a too conservative test that the STRING_CSTs
4016 will not end up being string-merged. */
4017 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4018 TREE_STRING_POINTER (base1) + ioff1,
4019 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4020 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4022 else if (!DECL_P (base0) || !DECL_P (base1))
4024 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4026 /* If this is a pointer comparison, ignore for now even
4027 valid equalities where one pointer is the offset zero
4028 of one object and the other to one past end of another one. */
4029 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4031 /* Assume that automatic variables can't be adjacent to global
4033 else if (is_global_var (base0) != is_global_var (base1))
4037 tree sz0 = DECL_SIZE_UNIT (base0);
4038 tree sz1 = DECL_SIZE_UNIT (base1);
4039 /* If sizes are unknown, e.g. VLA or not representable,
4041 if (!tree_fits_poly_int64_p (sz0)
4042 || !tree_fits_poly_int64_p (sz1))
4046 poly_int64 size0 = tree_to_poly_int64 (sz0);
4047 poly_int64 size1 = tree_to_poly_int64 (sz1);
4048 /* If one offset is pointing (or could be) to the beginning
4049 of one object and the other is pointing to one past the
4050 last byte of the other object, punt. */
4051 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4053 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4055 /* If both offsets are the same, there are some cases
4056 we know that are ok. Either if we know they aren't
4057 zero, or if we know both sizes are no zero. */
4059 && known_eq (off0, off1)
4060 && (known_ne (off0, 0)
4061 || (known_ne (size0, 0) && known_ne (size1, 0))))
4068 && (cmp == EQ_EXPR || cmp == NE_EXPR
4069 /* If the offsets are equal we can ignore overflow. */
4070 || known_eq (off0, off1)
4071 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4072 /* Or if we compare using pointers to decls or strings. */
4073 || (POINTER_TYPE_P (TREE_TYPE (@2))
4074 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4076 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4077 { constant_boolean_node (known_eq (off0, off1), type); })
4078 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4079 { constant_boolean_node (known_ne (off0, off1), type); })
4080 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4081 { constant_boolean_node (known_lt (off0, off1), type); })
4082 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4083 { constant_boolean_node (known_le (off0, off1), type); })
4084 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4085 { constant_boolean_node (known_ge (off0, off1), type); })
4086 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4087 { constant_boolean_node (known_gt (off0, off1), type); }))
4090 (if (cmp == EQ_EXPR)
4091 { constant_boolean_node (false, type); })
4092 (if (cmp == NE_EXPR)
4093 { constant_boolean_node (true, type); })))))))))
4095 /* Simplify pointer equality compares using PTA. */
4099 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4100 && ptrs_compare_unequal (@0, @1))
4101 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4103 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4104 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4105 Disable the transform if either operand is pointer to function.
4106 This broke pr22051-2.c for arm where function pointer
4107 canonicalizaion is not wanted. */
4111 (cmp (convert @0) INTEGER_CST@1)
4112 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4113 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4114 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4115 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4116 && POINTER_TYPE_P (TREE_TYPE (@1))
4117 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4118 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4119 (cmp @0 (convert @1)))))
4121 /* Non-equality compare simplifications from fold_binary */
4122 (for cmp (lt gt le ge)
4123 /* Comparisons with the highest or lowest possible integer of
4124 the specified precision will have known values. */
4126 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4127 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4128 || POINTER_TYPE_P (TREE_TYPE (@1))
4129 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4130 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4133 tree cst = uniform_integer_cst_p (@1);
4134 tree arg1_type = TREE_TYPE (cst);
4135 unsigned int prec = TYPE_PRECISION (arg1_type);
4136 wide_int max = wi::max_value (arg1_type);
4137 wide_int signed_max = wi::max_value (prec, SIGNED);
4138 wide_int min = wi::min_value (arg1_type);
4141 (if (wi::to_wide (cst) == max)
4143 (if (cmp == GT_EXPR)
4144 { constant_boolean_node (false, type); })
4145 (if (cmp == GE_EXPR)
4147 (if (cmp == LE_EXPR)
4148 { constant_boolean_node (true, type); })
4149 (if (cmp == LT_EXPR)
4151 (if (wi::to_wide (cst) == min)
4153 (if (cmp == LT_EXPR)
4154 { constant_boolean_node (false, type); })
4155 (if (cmp == LE_EXPR)
4157 (if (cmp == GE_EXPR)
4158 { constant_boolean_node (true, type); })
4159 (if (cmp == GT_EXPR)
4161 (if (wi::to_wide (cst) == max - 1)
4163 (if (cmp == GT_EXPR)
4164 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4165 wide_int_to_tree (TREE_TYPE (cst),
4168 (if (cmp == LE_EXPR)
4169 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4170 wide_int_to_tree (TREE_TYPE (cst),
4173 (if (wi::to_wide (cst) == min + 1)
4175 (if (cmp == GE_EXPR)
4176 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4177 wide_int_to_tree (TREE_TYPE (cst),
4180 (if (cmp == LT_EXPR)
4181 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4182 wide_int_to_tree (TREE_TYPE (cst),
4185 (if (wi::to_wide (cst) == signed_max
4186 && TYPE_UNSIGNED (arg1_type)
4187 /* We will flip the signedness of the comparison operator
4188 associated with the mode of @1, so the sign bit is
4189 specified by this mode. Check that @1 is the signed
4190 max associated with this sign bit. */
4191 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4192 /* signed_type does not work on pointer types. */
4193 && INTEGRAL_TYPE_P (arg1_type))
4194 /* The following case also applies to X < signed_max+1
4195 and X >= signed_max+1 because previous transformations. */
4196 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4197 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4199 (if (cst == @1 && cmp == LE_EXPR)
4200 (ge (convert:st @0) { build_zero_cst (st); }))
4201 (if (cst == @1 && cmp == GT_EXPR)
4202 (lt (convert:st @0) { build_zero_cst (st); }))
4203 (if (cmp == LE_EXPR)
4204 (ge (view_convert:st @0) { build_zero_cst (st); }))
4205 (if (cmp == GT_EXPR)
4206 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4208 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4209 /* If the second operand is NaN, the result is constant. */
4212 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4213 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4214 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4215 ? false : true, type); })))
4217 /* bool_var != 0 becomes bool_var. */
4219 (ne @0 integer_zerop)
4220 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4221 && types_match (type, TREE_TYPE (@0)))
4223 /* bool_var == 1 becomes bool_var. */
4225 (eq @0 integer_onep)
4226 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4227 && types_match (type, TREE_TYPE (@0)))
4230 bool_var == 0 becomes !bool_var or
4231 bool_var != 1 becomes !bool_var
4232 here because that only is good in assignment context as long
4233 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4234 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4235 clearly less optimal and which we'll transform again in forwprop. */
4237 /* When one argument is a constant, overflow detection can be simplified.
4238 Currently restricted to single use so as not to interfere too much with
4239 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4240 A + CST CMP A -> A CMP' CST' */
4241 (for cmp (lt le ge gt)
4244 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4245 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4246 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4247 && wi::to_wide (@1) != 0
4249 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4250 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4251 wi::max_value (prec, UNSIGNED)
4252 - wi::to_wide (@1)); })))))
4254 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4255 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4256 expects the long form, so we restrict the transformation for now. */
4259 (cmp:c (minus@2 @0 @1) @0)
4260 (if (single_use (@2)
4261 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4262 && TYPE_UNSIGNED (TREE_TYPE (@0))
4263 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4266 /* Testing for overflow is unnecessary if we already know the result. */
4271 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4272 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4273 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4274 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4279 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4280 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4281 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4282 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4284 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4285 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4289 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4290 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4291 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4292 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4294 /* Simplification of math builtins. These rules must all be optimizations
4295 as well as IL simplifications. If there is a possibility that the new
4296 form could be a pessimization, the rule should go in the canonicalization
4297 section that follows this one.
4299 Rules can generally go in this section if they satisfy one of
4302 - the rule describes an identity
4304 - the rule replaces calls with something as simple as addition or
4307 - the rule contains unary calls only and simplifies the surrounding
4308 arithmetic. (The idea here is to exclude non-unary calls in which
4309 one operand is constant and in which the call is known to be cheap
4310 when the operand has that value.) */
4312 (if (flag_unsafe_math_optimizations)
4313 /* Simplify sqrt(x) * sqrt(x) -> x. */
4315 (mult (SQRT_ALL@1 @0) @1)
4316 (if (!HONOR_SNANS (type))
4319 (for op (plus minus)
4320 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4324 (rdiv (op @0 @2) @1)))
4326 (for cmp (lt le gt ge)
4327 neg_cmp (gt ge lt le)
4328 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4330 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4332 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4334 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4335 || (real_zerop (tem) && !real_zerop (@1))))
4337 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4339 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4340 (neg_cmp @0 { tem; })))))))
4342 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4343 (for root (SQRT CBRT)
4345 (mult (root:s @0) (root:s @1))
4346 (root (mult @0 @1))))
4348 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4349 (for exps (EXP EXP2 EXP10 POW10)
4351 (mult (exps:s @0) (exps:s @1))
4352 (exps (plus @0 @1))))
4354 /* Simplify a/root(b/c) into a*root(c/b). */
4355 (for root (SQRT CBRT)
4357 (rdiv @0 (root:s (rdiv:s @1 @2)))
4358 (mult @0 (root (rdiv @2 @1)))))
4360 /* Simplify x/expN(y) into x*expN(-y). */
4361 (for exps (EXP EXP2 EXP10 POW10)
4363 (rdiv @0 (exps:s @1))
4364 (mult @0 (exps (negate @1)))))
4366 (for logs (LOG LOG2 LOG10 LOG10)
4367 exps (EXP EXP2 EXP10 POW10)
4368 /* logN(expN(x)) -> x. */
4372 /* expN(logN(x)) -> x. */
4377 /* Optimize logN(func()) for various exponential functions. We
4378 want to determine the value "x" and the power "exponent" in
4379 order to transform logN(x**exponent) into exponent*logN(x). */
4380 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4381 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4384 (if (SCALAR_FLOAT_TYPE_P (type))
4390 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4391 x = build_real_truncate (type, dconst_e ());
4394 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4395 x = build_real (type, dconst2);
4399 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4401 REAL_VALUE_TYPE dconst10;
4402 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4403 x = build_real (type, dconst10);
4410 (mult (logs { x; }) @0)))))
4418 (if (SCALAR_FLOAT_TYPE_P (type))
4424 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4425 x = build_real (type, dconsthalf);
4428 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4429 x = build_real_truncate (type, dconst_third ());
4435 (mult { x; } (logs @0))))))
4437 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4438 (for logs (LOG LOG2 LOG10)
4442 (mult @1 (logs @0))))
4444 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4445 or if C is a positive power of 2,
4446 pow(C,x) -> exp2(log2(C)*x). */
4454 (pows REAL_CST@0 @1)
4455 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4456 && real_isfinite (TREE_REAL_CST_PTR (@0))
4457 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4458 the use_exp2 case until after vectorization. It seems actually
4459 beneficial for all constants to postpone this until later,
4460 because exp(log(C)*x), while faster, will have worse precision
4461 and if x folds into a constant too, that is unnecessary
4463 && canonicalize_math_after_vectorization_p ())
4465 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4466 bool use_exp2 = false;
4467 if (targetm.libc_has_function (function_c99_misc)
4468 && value->cl == rvc_normal)
4470 REAL_VALUE_TYPE frac_rvt = *value;
4471 SET_REAL_EXP (&frac_rvt, 1);
4472 if (real_equal (&frac_rvt, &dconst1))
4477 (if (optimize_pow_to_exp (@0, @1))
4478 (exps (mult (logs @0) @1)))
4479 (exp2s (mult (log2s @0) @1)))))))
4482 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4484 exps (EXP EXP2 EXP10 POW10)
4485 logs (LOG LOG2 LOG10 LOG10)
4487 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4488 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4489 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4490 (exps (plus (mult (logs @0) @1) @2)))))
4495 exps (EXP EXP2 EXP10 POW10)
4496 /* sqrt(expN(x)) -> expN(x*0.5). */
4499 (exps (mult @0 { build_real (type, dconsthalf); })))
4500 /* cbrt(expN(x)) -> expN(x/3). */
4503 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4504 /* pow(expN(x), y) -> expN(x*y). */
4507 (exps (mult @0 @1))))
4509 /* tan(atan(x)) -> x. */
4516 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4520 copysigns (COPYSIGN)
4525 REAL_VALUE_TYPE r_cst;
4526 build_sinatan_real (&r_cst, type);
4527 tree t_cst = build_real (type, r_cst);
4528 tree t_one = build_one_cst (type);
4530 (if (SCALAR_FLOAT_TYPE_P (type))
4531 (cond (lt (abs @0) { t_cst; })
4532 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4533 (copysigns { t_one; } @0))))))
4535 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4539 copysigns (COPYSIGN)
4544 REAL_VALUE_TYPE r_cst;
4545 build_sinatan_real (&r_cst, type);
4546 tree t_cst = build_real (type, r_cst);
4547 tree t_one = build_one_cst (type);
4548 tree t_zero = build_zero_cst (type);
4550 (if (SCALAR_FLOAT_TYPE_P (type))
4551 (cond (lt (abs @0) { t_cst; })
4552 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4553 (copysigns { t_zero; } @0))))))
4555 (if (!flag_errno_math)
4556 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4561 (sinhs (atanhs:s @0))
4562 (with { tree t_one = build_one_cst (type); }
4563 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4565 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4570 (coshs (atanhs:s @0))
4571 (with { tree t_one = build_one_cst (type); }
4572 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4574 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4576 (CABS (complex:C @0 real_zerop@1))
4579 /* trunc(trunc(x)) -> trunc(x), etc. */
4580 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4584 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4585 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4587 (fns integer_valued_real_p@0)
4590 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4592 (HYPOT:c @0 real_zerop@1)
4595 /* pow(1,x) -> 1. */
4597 (POW real_onep@0 @1)
4601 /* copysign(x,x) -> x. */
4602 (COPYSIGN_ALL @0 @0)
4606 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4607 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4610 (for scale (LDEXP SCALBN SCALBLN)
4611 /* ldexp(0, x) -> 0. */
4613 (scale real_zerop@0 @1)
4615 /* ldexp(x, 0) -> x. */
4617 (scale @0 integer_zerop@1)
4619 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4621 (scale REAL_CST@0 @1)
4622 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4625 /* Canonicalization of sequences of math builtins. These rules represent
4626 IL simplifications but are not necessarily optimizations.
4628 The sincos pass is responsible for picking "optimal" implementations
4629 of math builtins, which may be more complicated and can sometimes go
4630 the other way, e.g. converting pow into a sequence of sqrts.
4631 We only want to do these canonicalizations before the pass has run. */
4633 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4634 /* Simplify tan(x) * cos(x) -> sin(x). */
4636 (mult:c (TAN:s @0) (COS:s @0))
4639 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4641 (mult:c @0 (POW:s @0 REAL_CST@1))
4642 (if (!TREE_OVERFLOW (@1))
4643 (POW @0 (plus @1 { build_one_cst (type); }))))
4645 /* Simplify sin(x) / cos(x) -> tan(x). */
4647 (rdiv (SIN:s @0) (COS:s @0))
4650 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4652 (rdiv (COS:s @0) (SIN:s @0))
4653 (rdiv { build_one_cst (type); } (TAN @0)))
4655 /* Simplify sin(x) / tan(x) -> cos(x). */
4657 (rdiv (SIN:s @0) (TAN:s @0))
4658 (if (! HONOR_NANS (@0)
4659 && ! HONOR_INFINITIES (@0))
4662 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4664 (rdiv (TAN:s @0) (SIN:s @0))
4665 (if (! HONOR_NANS (@0)
4666 && ! HONOR_INFINITIES (@0))
4667 (rdiv { build_one_cst (type); } (COS @0))))
4669 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4671 (mult (POW:s @0 @1) (POW:s @0 @2))
4672 (POW @0 (plus @1 @2)))
4674 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4676 (mult (POW:s @0 @1) (POW:s @2 @1))
4677 (POW (mult @0 @2) @1))
4679 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4681 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4682 (POWI (mult @0 @2) @1))
4684 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4686 (rdiv (POW:s @0 REAL_CST@1) @0)
4687 (if (!TREE_OVERFLOW (@1))
4688 (POW @0 (minus @1 { build_one_cst (type); }))))
4690 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4692 (rdiv @0 (POW:s @1 @2))
4693 (mult @0 (POW @1 (negate @2))))
4698 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4701 (pows @0 { build_real (type, dconst_quarter ()); }))
4702 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4705 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4706 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4709 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4710 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4712 (cbrts (cbrts tree_expr_nonnegative_p@0))
4713 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4714 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4716 (sqrts (pows @0 @1))
4717 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4718 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4720 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4721 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4722 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4724 (pows (sqrts @0) @1)
4725 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4726 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4728 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4729 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4730 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4732 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4733 (pows @0 (mult @1 @2))))
4735 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4737 (CABS (complex @0 @0))
4738 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4740 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4743 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4745 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4750 (cexps compositional_complex@0)
4751 (if (targetm.libc_has_function (function_c99_math_complex))
4753 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4754 (mult @1 (imagpart @2)))))))
4756 (if (canonicalize_math_p ())
4757 /* floor(x) -> trunc(x) if x is nonnegative. */
4758 (for floors (FLOOR_ALL)
4761 (floors tree_expr_nonnegative_p@0)
4764 (match double_value_p
4766 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4767 (for froms (BUILT_IN_TRUNCL
4779 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4780 (if (optimize && canonicalize_math_p ())
4782 (froms (convert double_value_p@0))
4783 (convert (tos @0)))))
4785 (match float_value_p
4787 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4788 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4789 BUILT_IN_FLOORL BUILT_IN_FLOOR
4790 BUILT_IN_CEILL BUILT_IN_CEIL
4791 BUILT_IN_ROUNDL BUILT_IN_ROUND
4792 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4793 BUILT_IN_RINTL BUILT_IN_RINT)
4794 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4795 BUILT_IN_FLOORF BUILT_IN_FLOORF
4796 BUILT_IN_CEILF BUILT_IN_CEILF
4797 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4798 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4799 BUILT_IN_RINTF BUILT_IN_RINTF)
4800 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4802 (if (optimize && canonicalize_math_p ()
4803 && targetm.libc_has_function (function_c99_misc))
4805 (froms (convert float_value_p@0))
4806 (convert (tos @0)))))
4808 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4809 tos (XFLOOR XCEIL XROUND XRINT)
4810 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4811 (if (optimize && canonicalize_math_p ())
4813 (froms (convert double_value_p@0))
4816 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4817 XFLOOR XCEIL XROUND XRINT)
4818 tos (XFLOORF XCEILF XROUNDF XRINTF)
4819 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4821 (if (optimize && canonicalize_math_p ())
4823 (froms (convert float_value_p@0))
4826 (if (canonicalize_math_p ())
4827 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4828 (for floors (IFLOOR LFLOOR LLFLOOR)
4830 (floors tree_expr_nonnegative_p@0)
4833 (if (canonicalize_math_p ())
4834 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4835 (for fns (IFLOOR LFLOOR LLFLOOR
4837 IROUND LROUND LLROUND)
4839 (fns integer_valued_real_p@0)
4841 (if (!flag_errno_math)
4842 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4843 (for rints (IRINT LRINT LLRINT)
4845 (rints integer_valued_real_p@0)
4848 (if (canonicalize_math_p ())
4849 (for ifn (IFLOOR ICEIL IROUND IRINT)
4850 lfn (LFLOOR LCEIL LROUND LRINT)
4851 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4852 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4853 sizeof (int) == sizeof (long). */
4854 (if (TYPE_PRECISION (integer_type_node)
4855 == TYPE_PRECISION (long_integer_type_node))
4858 (lfn:long_integer_type_node @0)))
4859 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4860 sizeof (long long) == sizeof (long). */
4861 (if (TYPE_PRECISION (long_long_integer_type_node)
4862 == TYPE_PRECISION (long_integer_type_node))
4865 (lfn:long_integer_type_node @0)))))
4867 /* cproj(x) -> x if we're ignoring infinities. */
4870 (if (!HONOR_INFINITIES (type))
4873 /* If the real part is inf and the imag part is known to be
4874 nonnegative, return (inf + 0i). */
4876 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4877 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4878 { build_complex_inf (type, false); }))
4880 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4882 (CPROJ (complex @0 REAL_CST@1))
4883 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4884 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4890 (pows @0 REAL_CST@1)
4892 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4893 REAL_VALUE_TYPE tmp;
4896 /* pow(x,0) -> 1. */
4897 (if (real_equal (value, &dconst0))
4898 { build_real (type, dconst1); })
4899 /* pow(x,1) -> x. */
4900 (if (real_equal (value, &dconst1))
4902 /* pow(x,-1) -> 1/x. */
4903 (if (real_equal (value, &dconstm1))
4904 (rdiv { build_real (type, dconst1); } @0))
4905 /* pow(x,0.5) -> sqrt(x). */
4906 (if (flag_unsafe_math_optimizations
4907 && canonicalize_math_p ()
4908 && real_equal (value, &dconsthalf))
4910 /* pow(x,1/3) -> cbrt(x). */
4911 (if (flag_unsafe_math_optimizations
4912 && canonicalize_math_p ()
4913 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4914 real_equal (value, &tmp)))
4917 /* powi(1,x) -> 1. */
4919 (POWI real_onep@0 @1)
4923 (POWI @0 INTEGER_CST@1)
4925 /* powi(x,0) -> 1. */
4926 (if (wi::to_wide (@1) == 0)
4927 { build_real (type, dconst1); })
4928 /* powi(x,1) -> x. */
4929 (if (wi::to_wide (@1) == 1)
4931 /* powi(x,-1) -> 1/x. */
4932 (if (wi::to_wide (@1) == -1)
4933 (rdiv { build_real (type, dconst1); } @0))))
4935 /* Narrowing of arithmetic and logical operations.
4937 These are conceptually similar to the transformations performed for
4938 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4939 term we want to move all that code out of the front-ends into here. */
4941 /* Convert (outertype)((innertype0)a+(innertype1)b)
4942 into ((newtype)a+(newtype)b) where newtype
4943 is the widest mode from all of these. */
4944 (for op (plus minus mult rdiv)
4946 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
4947 /* If we have a narrowing conversion of an arithmetic operation where
4948 both operands are widening conversions from the same type as the outer
4949 narrowing conversion. Then convert the innermost operands to a
4950 suitable unsigned type (to avoid introducing undefined behavior),
4951 perform the operation and convert the result to the desired type. */
4952 (if (INTEGRAL_TYPE_P (type)
4955 /* We check for type compatibility between @0 and @1 below,
4956 so there's no need to check that @2/@4 are integral types. */
4957 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
4958 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
4959 /* The precision of the type of each operand must match the
4960 precision of the mode of each operand, similarly for the
4962 && type_has_mode_precision_p (TREE_TYPE (@1))
4963 && type_has_mode_precision_p (TREE_TYPE (@2))
4964 && type_has_mode_precision_p (type)
4965 /* The inner conversion must be a widening conversion. */
4966 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
4967 && types_match (@1, type)
4968 && (types_match (@1, @2)
4969 /* Or the second operand is const integer or converted const
4970 integer from valueize. */
4971 || TREE_CODE (@2) == INTEGER_CST))
4972 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
4973 (op @1 (convert @2))
4974 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
4975 (convert (op (convert:utype @1)
4976 (convert:utype @2)))))
4977 (if (FLOAT_TYPE_P (type)
4978 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
4979 == DECIMAL_FLOAT_TYPE_P (type))
4980 (with { tree arg0 = strip_float_extensions (@1);
4981 tree arg1 = strip_float_extensions (@2);
4982 tree itype = TREE_TYPE (@0);
4983 tree ty1 = TREE_TYPE (arg0);
4984 tree ty2 = TREE_TYPE (arg1);
4985 enum tree_code code = TREE_CODE (itype); }
4986 (if (FLOAT_TYPE_P (ty1)
4987 && FLOAT_TYPE_P (ty2))
4988 (with { tree newtype = type;
4989 if (TYPE_MODE (ty1) == SDmode
4990 || TYPE_MODE (ty2) == SDmode
4991 || TYPE_MODE (type) == SDmode)
4992 newtype = dfloat32_type_node;
4993 if (TYPE_MODE (ty1) == DDmode
4994 || TYPE_MODE (ty2) == DDmode
4995 || TYPE_MODE (type) == DDmode)
4996 newtype = dfloat64_type_node;
4997 if (TYPE_MODE (ty1) == TDmode
4998 || TYPE_MODE (ty2) == TDmode
4999 || TYPE_MODE (type) == TDmode)
5000 newtype = dfloat128_type_node; }
5001 (if ((newtype == dfloat32_type_node
5002 || newtype == dfloat64_type_node
5003 || newtype == dfloat128_type_node)
5005 && types_match (newtype, type))
5006 (op (convert:newtype @1) (convert:newtype @2))
5007 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5009 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5011 /* Sometimes this transformation is safe (cannot
5012 change results through affecting double rounding
5013 cases) and sometimes it is not. If NEWTYPE is
5014 wider than TYPE, e.g. (float)((long double)double
5015 + (long double)double) converted to
5016 (float)(double + double), the transformation is
5017 unsafe regardless of the details of the types
5018 involved; double rounding can arise if the result
5019 of NEWTYPE arithmetic is a NEWTYPE value half way
5020 between two representable TYPE values but the
5021 exact value is sufficiently different (in the
5022 right direction) for this difference to be
5023 visible in ITYPE arithmetic. If NEWTYPE is the
5024 same as TYPE, however, the transformation may be
5025 safe depending on the types involved: it is safe
5026 if the ITYPE has strictly more than twice as many
5027 mantissa bits as TYPE, can represent infinities
5028 and NaNs if the TYPE can, and has sufficient
5029 exponent range for the product or ratio of two
5030 values representable in the TYPE to be within the
5031 range of normal values of ITYPE. */
5032 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5033 && (flag_unsafe_math_optimizations
5034 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5035 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5037 && !excess_precision_type (newtype)))
5038 && !types_match (itype, newtype))
5039 (convert:type (op (convert:newtype @1)
5040 (convert:newtype @2)))
5045 /* This is another case of narrowing, specifically when there's an outer
5046 BIT_AND_EXPR which masks off bits outside the type of the innermost
5047 operands. Like the previous case we have to convert the operands
5048 to unsigned types to avoid introducing undefined behavior for the
5049 arithmetic operation. */
5050 (for op (minus plus)
5052 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5053 (if (INTEGRAL_TYPE_P (type)
5054 /* We check for type compatibility between @0 and @1 below,
5055 so there's no need to check that @1/@3 are integral types. */
5056 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5057 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5058 /* The precision of the type of each operand must match the
5059 precision of the mode of each operand, similarly for the
5061 && type_has_mode_precision_p (TREE_TYPE (@0))
5062 && type_has_mode_precision_p (TREE_TYPE (@1))
5063 && type_has_mode_precision_p (type)
5064 /* The inner conversion must be a widening conversion. */
5065 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5066 && types_match (@0, @1)
5067 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5068 <= TYPE_PRECISION (TREE_TYPE (@0)))
5069 && (wi::to_wide (@4)
5070 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5071 true, TYPE_PRECISION (type))) == 0)
5072 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5073 (with { tree ntype = TREE_TYPE (@0); }
5074 (convert (bit_and (op @0 @1) (convert:ntype @4))))
5075 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5076 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5077 (convert:utype @4))))))))
5079 /* Transform (@0 < @1 and @0 < @2) to use min,
5080 (@0 > @1 and @0 > @2) to use max */
5081 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5082 op (lt le gt ge lt le gt ge )
5083 ext (min min max max max max min min )
5085 (logic (op:cs @0 @1) (op:cs @0 @2))
5086 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5087 && TREE_CODE (@0) != INTEGER_CST)
5088 (op @0 (ext @1 @2)))))
5091 /* signbit(x) -> 0 if x is nonnegative. */
5092 (SIGNBIT tree_expr_nonnegative_p@0)
5093 { integer_zero_node; })
5096 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
5098 (if (!HONOR_SIGNED_ZEROS (@0))
5099 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
5101 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
5103 (for op (plus minus)
5106 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5107 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5108 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
5109 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
5110 && !TYPE_SATURATING (TREE_TYPE (@0)))
5111 (with { tree res = int_const_binop (rop, @2, @1); }
5112 (if (TREE_OVERFLOW (res)
5113 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5114 { constant_boolean_node (cmp == NE_EXPR, type); }
5115 (if (single_use (@3))
5116 (cmp @0 { TREE_OVERFLOW (res)
5117 ? drop_tree_overflow (res) : res; }))))))))
5118 (for cmp (lt le gt ge)
5119 (for op (plus minus)
5122 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5123 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5124 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5125 (with { tree res = int_const_binop (rop, @2, @1); }
5126 (if (TREE_OVERFLOW (res))
5128 fold_overflow_warning (("assuming signed overflow does not occur "
5129 "when simplifying conditional to constant"),
5130 WARN_STRICT_OVERFLOW_CONDITIONAL);
5131 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
5132 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
5133 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
5134 TYPE_SIGN (TREE_TYPE (@1)))
5135 != (op == MINUS_EXPR);
5136 constant_boolean_node (less == ovf_high, type);
5138 (if (single_use (@3))
5141 fold_overflow_warning (("assuming signed overflow does not occur "
5142 "when changing X +- C1 cmp C2 to "
5144 WARN_STRICT_OVERFLOW_COMPARISON);
5146 (cmp @0 { res; })))))))))
5148 /* Canonicalizations of BIT_FIELD_REFs. */
5151 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5152 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5155 (BIT_FIELD_REF (view_convert @0) @1 @2)
5156 (BIT_FIELD_REF @0 @1 @2))
5159 (BIT_FIELD_REF @0 @1 integer_zerop)
5160 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5164 (BIT_FIELD_REF @0 @1 @2)
5166 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5167 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5169 (if (integer_zerop (@2))
5170 (view_convert (realpart @0)))
5171 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5172 (view_convert (imagpart @0)))))
5173 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5174 && INTEGRAL_TYPE_P (type)
5175 /* On GIMPLE this should only apply to register arguments. */
5176 && (! GIMPLE || is_gimple_reg (@0))
5177 /* A bit-field-ref that referenced the full argument can be stripped. */
5178 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5179 && integer_zerop (@2))
5180 /* Low-parts can be reduced to integral conversions.
5181 ??? The following doesn't work for PDP endian. */
5182 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5183 /* Don't even think about BITS_BIG_ENDIAN. */
5184 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5185 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5186 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5187 ? (TYPE_PRECISION (TREE_TYPE (@0))
5188 - TYPE_PRECISION (type))
5192 /* Simplify vector extracts. */
5195 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5196 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5197 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5198 || (VECTOR_TYPE_P (type)
5199 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5202 tree ctor = (TREE_CODE (@0) == SSA_NAME
5203 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5204 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5205 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5206 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5207 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5210 && (idx % width) == 0
5212 && known_le ((idx + n) / width,
5213 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5218 /* Constructor elements can be subvectors. */
5220 if (CONSTRUCTOR_NELTS (ctor) != 0)
5222 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5223 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5224 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5226 unsigned HOST_WIDE_INT elt, count, const_k;
5229 /* We keep an exact subset of the constructor elements. */
5230 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5231 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5232 { build_constructor (type, NULL); }
5234 (if (elt < CONSTRUCTOR_NELTS (ctor))
5235 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5236 { build_zero_cst (type); })
5238 vec<constructor_elt, va_gc> *vals;
5239 vec_alloc (vals, count);
5240 for (unsigned i = 0;
5241 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5242 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5243 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5244 build_constructor (type, vals);
5246 /* The bitfield references a single constructor element. */
5247 (if (k.is_constant (&const_k)
5248 && idx + n <= (idx / const_k + 1) * const_k)
5250 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5251 { build_zero_cst (type); })
5253 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5254 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5255 @1 { bitsize_int ((idx % const_k) * width); })))))))))
5257 /* Simplify a bit extraction from a bit insertion for the cases with
5258 the inserted element fully covering the extraction or the insertion
5259 not touching the extraction. */
5261 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5264 unsigned HOST_WIDE_INT isize;
5265 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5266 isize = TYPE_PRECISION (TREE_TYPE (@1));
5268 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5271 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5272 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5273 wi::to_wide (@ipos) + isize))
5274 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
5276 - wi::to_wide (@ipos)); }))
5277 (if (wi::geu_p (wi::to_wide (@ipos),
5278 wi::to_wide (@rpos) + wi::to_wide (@rsize))
5279 || wi::geu_p (wi::to_wide (@rpos),
5280 wi::to_wide (@ipos) + isize))
5281 (BIT_FIELD_REF @0 @rsize @rpos)))))
5283 (if (canonicalize_math_after_vectorization_p ())
5286 (fmas:c (negate @0) @1 @2)
5287 (IFN_FNMA @0 @1 @2))
5289 (fmas @0 @1 (negate @2))
5292 (fmas:c (negate @0) @1 (negate @2))
5293 (IFN_FNMS @0 @1 @2))
5295 (negate (fmas@3 @0 @1 @2))
5296 (if (single_use (@3))
5297 (IFN_FNMS @0 @1 @2))))
5300 (IFN_FMS:c (negate @0) @1 @2)
5301 (IFN_FNMS @0 @1 @2))
5303 (IFN_FMS @0 @1 (negate @2))
5306 (IFN_FMS:c (negate @0) @1 (negate @2))
5307 (IFN_FNMA @0 @1 @2))
5309 (negate (IFN_FMS@3 @0 @1 @2))
5310 (if (single_use (@3))
5311 (IFN_FNMA @0 @1 @2)))
5314 (IFN_FNMA:c (negate @0) @1 @2)
5317 (IFN_FNMA @0 @1 (negate @2))
5318 (IFN_FNMS @0 @1 @2))
5320 (IFN_FNMA:c (negate @0) @1 (negate @2))
5323 (negate (IFN_FNMA@3 @0 @1 @2))
5324 (if (single_use (@3))
5325 (IFN_FMS @0 @1 @2)))
5328 (IFN_FNMS:c (negate @0) @1 @2)
5331 (IFN_FNMS @0 @1 (negate @2))
5332 (IFN_FNMA @0 @1 @2))
5334 (IFN_FNMS:c (negate @0) @1 (negate @2))
5337 (negate (IFN_FNMS@3 @0 @1 @2))
5338 (if (single_use (@3))
5339 (IFN_FMA @0 @1 @2))))
5341 /* POPCOUNT simplifications. */
5342 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5343 BUILT_IN_POPCOUNTIMAX)
5344 /* popcount(X&1) is nop_expr(X&1). */
5347 (if (tree_nonzero_bits (@0) == 1)
5349 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
5351 (plus (popcount:s @0) (popcount:s @1))
5352 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5353 (popcount (bit_ior @0 @1))))
5354 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
5355 (for cmp (le eq ne gt)
5358 (cmp (popcount @0) integer_zerop)
5359 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5368 r = c ? a1 op a2 : b;
5370 if the target can do it in one go. This makes the operation conditional
5371 on c, so could drop potentially-trapping arithmetic, but that's a valid
5372 simplification if the result of the operation isn't needed.
5374 Avoid speculatively generating a stand-alone vector comparison
5375 on targets that might not support them. Any target implementing
5376 conditional internal functions must support the same comparisons
5377 inside and outside a VEC_COND_EXPR. */
5380 (for uncond_op (UNCOND_BINARY)
5381 cond_op (COND_BINARY)
5383 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5384 (with { tree op_type = TREE_TYPE (@4); }
5385 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5386 && element_precision (type) == element_precision (op_type))
5387 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5389 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5390 (with { tree op_type = TREE_TYPE (@4); }
5391 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5392 && element_precision (type) == element_precision (op_type))
5393 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5395 /* Same for ternary operations. */
5396 (for uncond_op (UNCOND_TERNARY)
5397 cond_op (COND_TERNARY)
5399 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5400 (with { tree op_type = TREE_TYPE (@5); }
5401 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5402 && element_precision (type) == element_precision (op_type))
5403 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5405 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5406 (with { tree op_type = TREE_TYPE (@5); }
5407 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5408 && element_precision (type) == element_precision (op_type))
5409 (view_convert (cond_op (bit_not @0) @2 @3 @4
5410 (view_convert:op_type @1)))))))
5413 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5414 "else" value of an IFN_COND_*. */
5415 (for cond_op (COND_BINARY)
5417 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5418 (with { tree op_type = TREE_TYPE (@3); }
5419 (if (element_precision (type) == element_precision (op_type))
5420 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5422 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5423 (with { tree op_type = TREE_TYPE (@5); }
5424 (if (inverse_conditions_p (@0, @2)
5425 && element_precision (type) == element_precision (op_type))
5426 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
5428 /* Same for ternary operations. */
5429 (for cond_op (COND_TERNARY)
5431 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5432 (with { tree op_type = TREE_TYPE (@4); }
5433 (if (element_precision (type) == element_precision (op_type))
5434 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5436 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5437 (with { tree op_type = TREE_TYPE (@6); }
5438 (if (inverse_conditions_p (@0, @2)
5439 && element_precision (type) == element_precision (op_type))
5440 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5442 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5445 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5446 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5448 If pointers are known not to wrap, B checks whether @1 bytes starting
5449 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5450 bytes. A is more efficiently tested as:
5452 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5454 The equivalent expression for B is given by replacing @1 with @1 - 1:
5456 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5458 @0 and @2 can be swapped in both expressions without changing the result.
5460 The folds rely on sizetype's being unsigned (which is always true)
5461 and on its being the same width as the pointer (which we have to check).
5463 The fold replaces two pointer_plus expressions, two comparisons and
5464 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5465 the best case it's a saving of two operations. The A fold retains one
5466 of the original pointer_pluses, so is a win even if both pointer_pluses
5467 are used elsewhere. The B fold is a wash if both pointer_pluses are
5468 used elsewhere, since all we end up doing is replacing a comparison with
5469 a pointer_plus. We do still apply the fold under those circumstances
5470 though, in case applying it to other conditions eventually makes one of the
5471 pointer_pluses dead. */
5472 (for ior (truth_orif truth_or bit_ior)
5475 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5476 (cmp:cs (pointer_plus@4 @2 @1) @0))
5477 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5478 && TYPE_OVERFLOW_WRAPS (sizetype)
5479 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5480 /* Calculate the rhs constant. */
5481 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5482 offset_int rhs = off * 2; }
5483 /* Always fails for negative values. */
5484 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5485 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5486 pick a canonical order. This increases the chances of using the
5487 same pointer_plus in multiple checks. */
5488 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5489 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5490 (if (cmp == LT_EXPR)
5491 (gt (convert:sizetype
5492 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5493 { swap_p ? @0 : @2; }))
5495 (gt (convert:sizetype
5496 (pointer_diff:ssizetype
5497 (pointer_plus { swap_p ? @2 : @0; }
5498 { wide_int_to_tree (sizetype, off); })
5499 { swap_p ? @0 : @2; }))
5500 { rhs_tree; })))))))))
5502 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
5504 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
5505 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
5506 (with { int i = single_nonzero_element (@1); }
5508 (with { tree elt = vector_cst_elt (@1, i);
5509 tree elt_type = TREE_TYPE (elt);
5510 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
5511 tree size = bitsize_int (elt_bits);
5512 tree pos = bitsize_int (elt_bits * i); }
5515 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
5519 (vec_perm @0 @1 VECTOR_CST@2)
5522 tree op0 = @0, op1 = @1, op2 = @2;
5524 /* Build a vector of integers from the tree mask. */
5525 vec_perm_builder builder;
5526 if (!tree_to_vec_perm_builder (&builder, op2))
5529 /* Create a vec_perm_indices for the integer vector. */
5530 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
5531 bool single_arg = (op0 == op1);
5532 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
5534 (if (sel.series_p (0, 1, 0, 1))
5536 (if (sel.series_p (0, 1, nelts, 1))
5542 if (sel.all_from_input_p (0))
5544 else if (sel.all_from_input_p (1))
5547 sel.rotate_inputs (1);
5549 else if (known_ge (poly_uint64 (sel[0]), nelts))
5551 std::swap (op0, op1);
5552 sel.rotate_inputs (1);
5556 tree cop0 = op0, cop1 = op1;
5557 if (TREE_CODE (op0) == SSA_NAME
5558 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
5559 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5560 cop0 = gimple_assign_rhs1 (def);
5561 if (TREE_CODE (op1) == SSA_NAME
5562 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
5563 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5564 cop1 = gimple_assign_rhs1 (def);
5568 (if ((TREE_CODE (cop0) == VECTOR_CST
5569 || TREE_CODE (cop0) == CONSTRUCTOR)
5570 && (TREE_CODE (cop1) == VECTOR_CST
5571 || TREE_CODE (cop1) == CONSTRUCTOR)
5572 && (t = fold_vec_perm (type, cop0, cop1, sel)))
5576 bool changed = (op0 == op1 && !single_arg);
5577 tree ins = NULL_TREE;
5580 /* See if the permutation is performing a single element
5581 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
5582 in that case. But only if the vector mode is supported,
5583 otherwise this is invalid GIMPLE. */
5584 if (TYPE_MODE (type) != BLKmode
5585 && (TREE_CODE (cop0) == VECTOR_CST
5586 || TREE_CODE (cop0) == CONSTRUCTOR
5587 || TREE_CODE (cop1) == VECTOR_CST
5588 || TREE_CODE (cop1) == CONSTRUCTOR))
5590 if (sel.series_p (1, 1, nelts + 1, 1))
5592 /* After canonicalizing the first elt to come from the
5593 first vector we only can insert the first elt from
5594 the first vector. */
5596 if ((ins = fold_read_from_vector (cop0, sel[0])))
5601 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
5602 for (at = 0; at < encoded_nelts; ++at)
5603 if (maybe_ne (sel[at], at))
5605 if (at < encoded_nelts && sel.series_p (at + 1, 1, at + 1, 1))
5607 if (known_lt (at, nelts))
5608 ins = fold_read_from_vector (cop0, sel[at]);
5610 ins = fold_read_from_vector (cop1, sel[at] - nelts);
5615 /* Generate a canonical form of the selector. */
5616 if (!ins && sel.encoding () != builder)
5618 /* Some targets are deficient and fail to expand a single
5619 argument permutation while still allowing an equivalent
5620 2-argument version. */
5622 if (sel.ninputs () == 2
5623 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
5624 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5627 vec_perm_indices sel2 (builder, 2, nelts);
5628 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
5629 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
5631 /* Not directly supported with either encoding,
5632 so use the preferred form. */
5633 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5635 if (!operand_equal_p (op2, oldop2, 0))
5640 (bit_insert { op0; } { ins; }
5641 { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); })
5643 (vec_perm { op0; } { op1; } { op2; }))))))))))
5645 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
5647 (match vec_same_elem_p
5649 (if (uniform_vector_p (@0))))
5651 (match vec_same_elem_p
5655 (vec_perm vec_same_elem_p@0 @0 @1)