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
42 (define_operator_list tcc_comparison
43 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
44 (define_operator_list inverted_tcc_comparison
45 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list inverted_tcc_comparison_with_nans
47 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
48 (define_operator_list swapped_tcc_comparison
49 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
50 (define_operator_list simple_comparison lt le eq ne ge gt)
51 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
53 #include "cfn-operators.pd"
55 /* Define operand lists for math rounding functions {,i,l,ll}FN,
56 where the versions prefixed with "i" return an int, those prefixed with
57 "l" return a long and those prefixed with "ll" return a long long.
59 Also define operand lists:
61 X<FN>F for all float functions, in the order i, l, ll
62 X<FN> for all double functions, in the same order
63 X<FN>L for all long double functions, in the same order. */
64 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
65 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
68 (define_operator_list X##FN BUILT_IN_I##FN \
71 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
75 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
80 /* Binary operations and their associated IFN_COND_* function. */
81 (define_operator_list UNCOND_BINARY
83 mult trunc_div trunc_mod rdiv
85 bit_and bit_ior bit_xor)
86 (define_operator_list COND_BINARY
87 IFN_COND_ADD IFN_COND_SUB
88 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
89 IFN_COND_MIN IFN_COND_MAX
90 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
92 /* Same for ternary operations. */
93 (define_operator_list UNCOND_TERNARY
94 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
95 (define_operator_list COND_TERNARY
96 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
98 /* As opposed to convert?, this still creates a single pattern, so
99 it is not a suitable replacement for convert? in all cases. */
100 (match (nop_convert @0)
102 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
103 (match (nop_convert @0)
105 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
106 && known_eq (TYPE_VECTOR_SUBPARTS (type),
107 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
108 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
109 /* This one has to be last, or it shadows the others. */
110 (match (nop_convert @0)
113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114 ABSU_EXPR returns unsigned absolute value of the operand and the operand
115 of the ABSU_EXPR will have the corresponding signed type. */
116 (simplify (abs (convert @0))
117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118 && !TYPE_UNSIGNED (TREE_TYPE (@0))
119 && element_precision (type) > element_precision (TREE_TYPE (@0)))
120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121 (convert (absu:utype @0)))))
124 /* Simplifications of operations with one constant operand and
125 simplifications to constants or single values. */
127 (for op (plus pointer_plus minus bit_ior bit_xor)
129 (op @0 integer_zerop)
132 /* 0 +p index -> (type)index */
134 (pointer_plus integer_zerop @1)
135 (non_lvalue (convert @1)))
137 /* ptr - 0 -> (type)ptr */
139 (pointer_diff @0 integer_zerop)
142 /* See if ARG1 is zero and X + ARG1 reduces to X.
143 Likewise if the operands are reversed. */
145 (plus:c @0 real_zerop@1)
146 (if (fold_real_zero_addition_p (type, @1, 0))
149 /* See if ARG1 is zero and X - ARG1 reduces to X. */
151 (minus @0 real_zerop@1)
152 (if (fold_real_zero_addition_p (type, @1, 1))
155 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
156 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
157 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
158 if not -frounding-math. For sNaNs the first operation would raise
159 exceptions but turn the result into qNan, so the second operation
160 would not raise it. */
161 (for inner_op (plus minus)
162 (for outer_op (plus minus)
164 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
167 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
168 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
169 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
171 = ((outer_op == PLUS_EXPR)
172 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
173 (if (outer_plus && !inner_plus)
178 This is unsafe for certain floats even in non-IEEE formats.
179 In IEEE, it is unsafe because it does wrong for NaNs.
180 Also note that operand_equal_p is always false if an operand
184 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
185 { build_zero_cst (type); }))
187 (pointer_diff @@0 @0)
188 { build_zero_cst (type); })
191 (mult @0 integer_zerop@1)
194 /* Maybe fold x * 0 to 0. The expressions aren't the same
195 when x is NaN, since x * 0 is also NaN. Nor are they the
196 same in modes with signed zeros, since multiplying a
197 negative value by 0 gives -0, not +0. */
199 (mult @0 real_zerop@1)
200 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
203 /* In IEEE floating point, x*1 is not equivalent to x for snans.
204 Likewise for complex arithmetic with signed zeros. */
207 (if (!HONOR_SNANS (type)
208 && (!HONOR_SIGNED_ZEROS (type)
209 || !COMPLEX_FLOAT_TYPE_P (type)))
212 /* Transform x * -1.0 into -x. */
214 (mult @0 real_minus_onep)
215 (if (!HONOR_SNANS (type)
216 && (!HONOR_SIGNED_ZEROS (type)
217 || !COMPLEX_FLOAT_TYPE_P (type)))
220 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
221 unless the target has native support for the former but not the latter. */
223 (mult @0 VECTOR_CST@1)
224 (if (initializer_each_zero_or_onep (@1)
225 && !HONOR_SNANS (type)
226 && !HONOR_SIGNED_ZEROS (type))
227 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
229 && (!VECTOR_MODE_P (TYPE_MODE (type))
230 || (VECTOR_MODE_P (TYPE_MODE (itype))
231 && optab_handler (and_optab,
232 TYPE_MODE (itype)) != CODE_FOR_nothing)))
233 (view_convert (bit_and:itype (view_convert @0)
234 (ne @1 { build_zero_cst (type); })))))))
236 (for cmp (gt ge lt le)
237 outp (convert convert negate negate)
238 outn (negate negate convert convert)
239 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
240 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
241 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
242 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
244 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
245 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
246 && types_match (type, TREE_TYPE (@0)))
248 (if (types_match (type, float_type_node))
249 (BUILT_IN_COPYSIGNF @1 (outp @0)))
250 (if (types_match (type, double_type_node))
251 (BUILT_IN_COPYSIGN @1 (outp @0)))
252 (if (types_match (type, long_double_type_node))
253 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
254 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
255 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
256 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
257 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
259 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
260 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
261 && types_match (type, TREE_TYPE (@0)))
263 (if (types_match (type, float_type_node))
264 (BUILT_IN_COPYSIGNF @1 (outn @0)))
265 (if (types_match (type, double_type_node))
266 (BUILT_IN_COPYSIGN @1 (outn @0)))
267 (if (types_match (type, long_double_type_node))
268 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
270 /* Transform X * copysign (1.0, X) into abs(X). */
272 (mult:c @0 (COPYSIGN_ALL real_onep @0))
273 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
276 /* Transform X * copysign (1.0, -X) into -abs(X). */
278 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
279 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
282 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
284 (COPYSIGN_ALL REAL_CST@0 @1)
285 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
286 (COPYSIGN_ALL (negate @0) @1)))
288 /* X * 1, X / 1 -> X. */
289 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
294 /* (A / (1 << B)) -> (A >> B).
295 Only for unsigned A. For signed A, this would not preserve rounding
297 For example: (-1 / ( 1 << B)) != -1 >> B. */
299 (trunc_div @0 (lshift integer_onep@1 @2))
300 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
301 && (!VECTOR_TYPE_P (type)
302 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
303 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
306 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
307 undefined behavior in constexpr evaluation, and assuming that the division
308 traps enables better optimizations than these anyway. */
309 (for div (trunc_div ceil_div floor_div round_div exact_div)
310 /* 0 / X is always zero. */
312 (div integer_zerop@0 @1)
313 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
314 (if (!integer_zerop (@1))
318 (div @0 integer_minus_onep@1)
319 (if (!TYPE_UNSIGNED (type))
324 /* But not for 0 / 0 so that we can get the proper warnings and errors.
325 And not for _Fract types where we can't build 1. */
326 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
327 { build_one_cst (type); }))
328 /* X / abs (X) is X < 0 ? -1 : 1. */
331 (if (INTEGRAL_TYPE_P (type)
332 && TYPE_OVERFLOW_UNDEFINED (type))
333 (cond (lt @0 { build_zero_cst (type); })
334 { build_minus_one_cst (type); } { build_one_cst (type); })))
337 (div:C @0 (negate @0))
338 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
339 && TYPE_OVERFLOW_UNDEFINED (type))
340 { build_minus_one_cst (type); })))
342 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
343 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
346 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
347 && TYPE_UNSIGNED (type))
350 /* Combine two successive divisions. Note that combining ceil_div
351 and floor_div is trickier and combining round_div even more so. */
352 (for div (trunc_div exact_div)
354 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
356 wi::overflow_type overflow;
357 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
358 TYPE_SIGN (type), &overflow);
360 (if (div == EXACT_DIV_EXPR
361 || optimize_successive_divisions_p (@2, @3))
363 (div @0 { wide_int_to_tree (type, mul); })
364 (if (TYPE_UNSIGNED (type)
365 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
366 { build_zero_cst (type); }))))))
368 /* Combine successive multiplications. Similar to above, but handling
369 overflow is different. */
371 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
373 wi::overflow_type overflow;
374 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
375 TYPE_SIGN (type), &overflow);
377 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
378 otherwise undefined overflow implies that @0 must be zero. */
379 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
380 (mult @0 { wide_int_to_tree (type, mul); }))))
382 /* Optimize A / A to 1.0 if we don't care about
383 NaNs or Infinities. */
386 (if (FLOAT_TYPE_P (type)
387 && ! HONOR_NANS (type)
388 && ! HONOR_INFINITIES (type))
389 { build_one_cst (type); }))
391 /* Optimize -A / A to -1.0 if we don't care about
392 NaNs or Infinities. */
394 (rdiv:C @0 (negate @0))
395 (if (FLOAT_TYPE_P (type)
396 && ! HONOR_NANS (type)
397 && ! HONOR_INFINITIES (type))
398 { build_minus_one_cst (type); }))
400 /* PR71078: x / abs(x) -> copysign (1.0, x) */
402 (rdiv:C (convert? @0) (convert? (abs @0)))
403 (if (SCALAR_FLOAT_TYPE_P (type)
404 && ! HONOR_NANS (type)
405 && ! HONOR_INFINITIES (type))
407 (if (types_match (type, float_type_node))
408 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
409 (if (types_match (type, double_type_node))
410 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
411 (if (types_match (type, long_double_type_node))
412 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
414 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
417 (if (!HONOR_SNANS (type))
420 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
422 (rdiv @0 real_minus_onep)
423 (if (!HONOR_SNANS (type))
426 (if (flag_reciprocal_math)
427 /* Convert (A/B)/C to A/(B*C). */
429 (rdiv (rdiv:s @0 @1) @2)
430 (rdiv @0 (mult @1 @2)))
432 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
434 (rdiv @0 (mult:s @1 REAL_CST@2))
436 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
438 (rdiv (mult @0 { tem; } ) @1))))
440 /* Convert A/(B/C) to (A/B)*C */
442 (rdiv @0 (rdiv:s @1 @2))
443 (mult (rdiv @0 @1) @2)))
445 /* Simplify x / (- y) to -x / y. */
447 (rdiv @0 (negate @1))
448 (rdiv (negate @0) @1))
450 (if (flag_unsafe_math_optimizations)
451 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
452 Since C / x may underflow to zero, do this only for unsafe math. */
453 (for op (lt le gt ge)
456 (op (rdiv REAL_CST@0 @1) real_zerop@2)
457 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
459 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
461 /* For C < 0, use the inverted operator. */
462 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
465 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
466 (for div (trunc_div ceil_div floor_div round_div exact_div)
468 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
469 (if (integer_pow2p (@2)
470 && tree_int_cst_sgn (@2) > 0
471 && tree_nop_conversion_p (type, TREE_TYPE (@0))
472 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
474 { build_int_cst (integer_type_node,
475 wi::exact_log2 (wi::to_wide (@2))); }))))
477 /* If ARG1 is a constant, we can convert this to a multiply by the
478 reciprocal. This does not have the same rounding properties,
479 so only do this if -freciprocal-math. We can actually
480 always safely do it if ARG1 is a power of two, but it's hard to
481 tell if it is or not in a portable manner. */
482 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
486 (if (flag_reciprocal_math
489 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
491 (mult @0 { tem; } )))
492 (if (cst != COMPLEX_CST)
493 (with { tree inverse = exact_inverse (type, @1); }
495 (mult @0 { inverse; } ))))))))
497 (for mod (ceil_mod floor_mod round_mod trunc_mod)
498 /* 0 % X is always zero. */
500 (mod integer_zerop@0 @1)
501 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
502 (if (!integer_zerop (@1))
504 /* X % 1 is always zero. */
506 (mod @0 integer_onep)
507 { build_zero_cst (type); })
508 /* X % -1 is zero. */
510 (mod @0 integer_minus_onep@1)
511 (if (!TYPE_UNSIGNED (type))
512 { build_zero_cst (type); }))
516 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
517 (if (!integer_zerop (@0))
518 { build_zero_cst (type); }))
519 /* (X % Y) % Y is just X % Y. */
521 (mod (mod@2 @0 @1) @1)
523 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
525 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
526 (if (ANY_INTEGRAL_TYPE_P (type)
527 && TYPE_OVERFLOW_UNDEFINED (type)
528 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
530 { build_zero_cst (type); }))
531 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
532 modulo and comparison, since it is simpler and equivalent. */
535 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
536 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
537 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
538 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
540 /* X % -C is the same as X % C. */
542 (trunc_mod @0 INTEGER_CST@1)
543 (if (TYPE_SIGN (type) == SIGNED
544 && !TREE_OVERFLOW (@1)
545 && wi::neg_p (wi::to_wide (@1))
546 && !TYPE_OVERFLOW_TRAPS (type)
547 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
548 && !sign_bit_p (@1, @1))
549 (trunc_mod @0 (negate @1))))
551 /* X % -Y is the same as X % Y. */
553 (trunc_mod @0 (convert? (negate @1)))
554 (if (INTEGRAL_TYPE_P (type)
555 && !TYPE_UNSIGNED (type)
556 && !TYPE_OVERFLOW_TRAPS (type)
557 && tree_nop_conversion_p (type, TREE_TYPE (@1))
558 /* Avoid this transformation if X might be INT_MIN or
559 Y might be -1, because we would then change valid
560 INT_MIN % -(-1) into invalid INT_MIN % -1. */
561 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
562 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
564 (trunc_mod @0 (convert @1))))
566 /* X - (X / Y) * Y is the same as X % Y. */
568 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
569 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
570 (convert (trunc_mod @0 @1))))
572 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
573 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
574 Also optimize A % (C << N) where C is a power of 2,
575 to A & ((C << N) - 1). */
576 (match (power_of_two_cand @1)
578 (match (power_of_two_cand @1)
579 (lshift INTEGER_CST@1 @2))
580 (for mod (trunc_mod floor_mod)
582 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
583 (if ((TYPE_UNSIGNED (type)
584 || tree_expr_nonnegative_p (@0))
585 && tree_nop_conversion_p (type, TREE_TYPE (@3))
586 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
587 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
589 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
591 (trunc_div (mult @0 integer_pow2p@1) @1)
592 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
593 (bit_and @0 { wide_int_to_tree
594 (type, wi::mask (TYPE_PRECISION (type)
595 - wi::exact_log2 (wi::to_wide (@1)),
596 false, TYPE_PRECISION (type))); })))
598 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
600 (mult (trunc_div @0 integer_pow2p@1) @1)
601 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
602 (bit_and @0 (negate @1))))
604 /* Simplify (t * 2) / 2) -> t. */
605 (for div (trunc_div ceil_div floor_div round_div exact_div)
607 (div (mult:c @0 @1) @1)
608 (if (ANY_INTEGRAL_TYPE_P (type)
609 && TYPE_OVERFLOW_UNDEFINED (type))
613 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
618 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
621 (pows (op @0) REAL_CST@1)
622 (with { HOST_WIDE_INT n; }
623 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
625 /* Likewise for powi. */
628 (pows (op @0) INTEGER_CST@1)
629 (if ((wi::to_wide (@1) & 1) == 0)
631 /* Strip negate and abs from both operands of hypot. */
639 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
640 (for copysigns (COPYSIGN_ALL)
642 (copysigns (op @0) @1)
645 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
650 /* Convert absu(x)*absu(x) -> x*x. */
652 (mult (absu@1 @0) @1)
653 (mult (convert@2 @0) @2))
655 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
659 (coss (copysigns @0 @1))
662 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
666 (pows (copysigns @0 @2) REAL_CST@1)
667 (with { HOST_WIDE_INT n; }
668 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
670 /* Likewise for powi. */
674 (pows (copysigns @0 @2) INTEGER_CST@1)
675 (if ((wi::to_wide (@1) & 1) == 0)
680 /* hypot(copysign(x, y), z) -> hypot(x, z). */
682 (hypots (copysigns @0 @1) @2)
684 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
686 (hypots @0 (copysigns @1 @2))
689 /* copysign(x, CST) -> [-]abs (x). */
690 (for copysigns (COPYSIGN_ALL)
692 (copysigns @0 REAL_CST@1)
693 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
697 /* copysign(copysign(x, y), z) -> copysign(x, z). */
698 (for copysigns (COPYSIGN_ALL)
700 (copysigns (copysigns @0 @1) @2)
703 /* copysign(x,y)*copysign(x,y) -> x*x. */
704 (for copysigns (COPYSIGN_ALL)
706 (mult (copysigns@2 @0 @1) @2)
709 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
710 (for ccoss (CCOS CCOSH)
715 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
716 (for ops (conj negate)
722 /* Fold (a * (1 << b)) into (a << b) */
724 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
725 (if (! FLOAT_TYPE_P (type)
726 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
729 /* Fold (1 << (C - x)) where C = precision(type) - 1
730 into ((1 << C) >> x). */
732 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
733 (if (INTEGRAL_TYPE_P (type)
734 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
736 (if (TYPE_UNSIGNED (type))
737 (rshift (lshift @0 @2) @3)
739 { tree utype = unsigned_type_for (type); }
740 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
742 /* Fold (C1/X)*C2 into (C1*C2)/X. */
744 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
745 (if (flag_associative_math
748 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
750 (rdiv { tem; } @1)))))
752 /* Simplify ~X & X as zero. */
754 (bit_and:c (convert? @0) (convert? (bit_not @0)))
755 { build_zero_cst (type); })
757 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
759 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
760 (if (TYPE_UNSIGNED (type))
761 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
763 (for bitop (bit_and bit_ior)
765 /* PR35691: Transform
766 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
767 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
769 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
770 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
771 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
772 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
773 (cmp (bit_ior @0 (convert @1)) @2)))
775 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
776 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
778 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
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_and @0 (convert @1)) @2))))
784 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
786 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
787 (minus (bit_xor @0 @1) @1))
789 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
790 (if (~wi::to_wide (@2) == wi::to_wide (@1))
791 (minus (bit_xor @0 @1) @1)))
793 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
795 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
796 (minus @1 (bit_xor @0 @1)))
798 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
799 (for op (bit_ior bit_xor plus)
801 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
804 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
805 (if (~wi::to_wide (@2) == wi::to_wide (@1))
808 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
810 (bit_ior:c (bit_xor:c @0 @1) @0)
813 /* (a & ~b) | (a ^ b) --> a ^ b */
815 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
818 /* (a & ~b) ^ ~a --> ~(a & b) */
820 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
821 (bit_not (bit_and @0 @1)))
823 /* (a | b) & ~(a ^ b) --> a & b */
825 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
828 /* a | ~(a ^ b) --> a | ~b */
830 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
831 (bit_ior @0 (bit_not @1)))
833 /* (a | b) | (a &^ b) --> a | b */
834 (for op (bit_and bit_xor)
836 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
839 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
841 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
844 /* ~(~a & b) --> a | ~b */
846 (bit_not (bit_and:cs (bit_not @0) @1))
847 (bit_ior @0 (bit_not @1)))
849 /* ~(~a | b) --> a & ~b */
851 (bit_not (bit_ior:cs (bit_not @0) @1))
852 (bit_and @0 (bit_not @1)))
854 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
857 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
858 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
859 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
863 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
864 ((A & N) + B) & M -> (A + B) & M
865 Similarly if (N & M) == 0,
866 ((A | N) + B) & M -> (A + B) & M
867 and for - instead of + (or unary - instead of +)
868 and/or ^ instead of |.
869 If B is constant and (B & M) == 0, fold into A & M. */
871 (for bitop (bit_and bit_ior bit_xor)
873 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
876 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
877 @3, @4, @1, ERROR_MARK, NULL_TREE,
880 (convert (bit_and (op (convert:utype { pmop[0]; })
881 (convert:utype { pmop[1]; }))
882 (convert:utype @2))))))
884 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
887 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
888 NULL_TREE, NULL_TREE, @1, bitop, @3,
891 (convert (bit_and (op (convert:utype { pmop[0]; })
892 (convert:utype { pmop[1]; }))
893 (convert:utype @2)))))))
895 (bit_and (op:s @0 @1) INTEGER_CST@2)
898 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
899 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
900 NULL_TREE, NULL_TREE, pmop); }
902 (convert (bit_and (op (convert:utype { pmop[0]; })
903 (convert:utype { pmop[1]; }))
904 (convert:utype @2)))))))
905 (for bitop (bit_and bit_ior bit_xor)
907 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
910 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
911 bitop, @2, @3, NULL_TREE, ERROR_MARK,
912 NULL_TREE, NULL_TREE, pmop); }
914 (convert (bit_and (negate (convert:utype { pmop[0]; }))
915 (convert:utype @1)))))))
917 /* X % Y is smaller than Y. */
920 (cmp (trunc_mod @0 @1) @1)
921 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
922 { constant_boolean_node (cmp == LT_EXPR, type); })))
925 (cmp @1 (trunc_mod @0 @1))
926 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
927 { constant_boolean_node (cmp == GT_EXPR, type); })))
931 (bit_ior @0 integer_all_onesp@1)
936 (bit_ior @0 integer_zerop)
941 (bit_and @0 integer_zerop@1)
947 (for op (bit_ior bit_xor plus)
949 (op:c (convert? @0) (convert? (bit_not @0)))
950 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
955 { build_zero_cst (type); })
957 /* Canonicalize X ^ ~0 to ~X. */
959 (bit_xor @0 integer_all_onesp@1)
964 (bit_and @0 integer_all_onesp)
967 /* x & x -> x, x | x -> x */
968 (for bitop (bit_and bit_ior)
973 /* x & C -> x if we know that x & ~C == 0. */
976 (bit_and SSA_NAME@0 INTEGER_CST@1)
977 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
978 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
982 /* x + (x & 1) -> (x + 1) & ~1 */
984 (plus:c @0 (bit_and:s @0 integer_onep@1))
985 (bit_and (plus @0 @1) (bit_not @1)))
987 /* x & ~(x & y) -> x & ~y */
988 /* x | ~(x | y) -> x | ~y */
989 (for bitop (bit_and bit_ior)
991 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
992 (bitop @0 (bit_not @1))))
994 /* (~x & y) | ~(x | y) -> ~x */
996 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
999 /* (x | y) ^ (x | ~y) -> ~x */
1001 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1004 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1006 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1007 (bit_not (bit_xor @0 @1)))
1009 /* (~x | y) ^ (x ^ y) -> x | ~y */
1011 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1012 (bit_ior @0 (bit_not @1)))
1014 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1016 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1017 (bit_not (bit_and @0 @1)))
1019 /* (x | y) & ~x -> y & ~x */
1020 /* (x & y) | ~x -> y | ~x */
1021 (for bitop (bit_and bit_ior)
1022 rbitop (bit_ior bit_and)
1024 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1027 /* (x & y) ^ (x | y) -> x ^ y */
1029 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1032 /* (x ^ y) ^ (x | y) -> x & y */
1034 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1037 /* (x & y) + (x ^ y) -> x | y */
1038 /* (x & y) | (x ^ y) -> x | y */
1039 /* (x & y) ^ (x ^ y) -> x | y */
1040 (for op (plus bit_ior bit_xor)
1042 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1045 /* (x & y) + (x | y) -> x + y */
1047 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1050 /* (x + y) - (x | y) -> x & y */
1052 (minus (plus @0 @1) (bit_ior @0 @1))
1053 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1054 && !TYPE_SATURATING (type))
1057 /* (x + y) - (x & y) -> x | y */
1059 (minus (plus @0 @1) (bit_and @0 @1))
1060 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1061 && !TYPE_SATURATING (type))
1064 /* (x | y) - (x ^ y) -> x & y */
1066 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1069 /* (x | y) - (x & y) -> x ^ y */
1071 (minus (bit_ior @0 @1) (bit_and @0 @1))
1074 /* (x | y) & ~(x & y) -> x ^ y */
1076 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1079 /* (x | y) & (~x ^ y) -> x & y */
1081 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1084 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1086 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1087 (bit_not (bit_xor @0 @1)))
1089 /* (~x | y) ^ (x | ~y) -> x ^ y */
1091 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1094 /* ~x & ~y -> ~(x | y)
1095 ~x | ~y -> ~(x & y) */
1096 (for op (bit_and bit_ior)
1097 rop (bit_ior bit_and)
1099 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1100 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1101 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1102 (bit_not (rop (convert @0) (convert @1))))))
1104 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1105 with a constant, and the two constants have no bits in common,
1106 we should treat this as a BIT_IOR_EXPR since this may produce more
1108 (for op (bit_xor plus)
1110 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1111 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1112 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1113 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1114 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1115 (bit_ior (convert @4) (convert @5)))))
1117 /* (X | Y) ^ X -> Y & ~ X*/
1119 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1120 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1121 (convert (bit_and @1 (bit_not @0)))))
1123 /* Convert ~X ^ ~Y to X ^ Y. */
1125 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1126 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1127 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1128 (bit_xor (convert @0) (convert @1))))
1130 /* Convert ~X ^ C to X ^ ~C. */
1132 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1133 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1134 (bit_xor (convert @0) (bit_not @1))))
1136 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1137 (for opo (bit_and bit_xor)
1138 opi (bit_xor bit_and)
1140 (opo:c (opi:cs @0 @1) @1)
1141 (bit_and (bit_not @0) @1)))
1143 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1144 operands are another bit-wise operation with a common input. If so,
1145 distribute the bit operations to save an operation and possibly two if
1146 constants are involved. For example, convert
1147 (A | B) & (A | C) into A | (B & C)
1148 Further simplification will occur if B and C are constants. */
1149 (for op (bit_and bit_ior bit_xor)
1150 rop (bit_ior bit_and bit_and)
1152 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1153 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1154 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1155 (rop (convert @0) (op (convert @1) (convert @2))))))
1157 /* Some simple reassociation for bit operations, also handled in reassoc. */
1158 /* (X & Y) & Y -> X & Y
1159 (X | Y) | Y -> X | Y */
1160 (for op (bit_and bit_ior)
1162 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1164 /* (X ^ Y) ^ Y -> X */
1166 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1168 /* (X & Y) & (X & Z) -> (X & Y) & Z
1169 (X | Y) | (X | Z) -> (X | Y) | Z */
1170 (for op (bit_and bit_ior)
1172 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1173 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1174 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1175 (if (single_use (@5) && single_use (@6))
1176 (op @3 (convert @2))
1177 (if (single_use (@3) && single_use (@4))
1178 (op (convert @1) @5))))))
1179 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1181 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1182 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1183 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1184 (bit_xor (convert @1) (convert @2))))
1186 /* Convert abs (abs (X)) into abs (X).
1187 also absu (absu (X)) into absu (X). */
1193 (absu (convert@2 (absu@1 @0)))
1194 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1197 /* Convert abs[u] (-X) -> abs[u] (X). */
1206 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1208 (abs tree_expr_nonnegative_p@0)
1212 (absu tree_expr_nonnegative_p@0)
1215 /* A few cases of fold-const.c negate_expr_p predicate. */
1216 (match negate_expr_p
1218 (if ((INTEGRAL_TYPE_P (type)
1219 && TYPE_UNSIGNED (type))
1220 || (!TYPE_OVERFLOW_SANITIZED (type)
1221 && may_negate_without_overflow_p (t)))))
1222 (match negate_expr_p
1224 (match negate_expr_p
1226 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1227 (match negate_expr_p
1229 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1230 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1232 (match negate_expr_p
1234 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1235 (match negate_expr_p
1237 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1238 || (FLOAT_TYPE_P (type)
1239 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1240 && !HONOR_SIGNED_ZEROS (type)))))
1242 /* (-A) * (-B) -> A * B */
1244 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1245 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1246 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1247 (mult (convert @0) (convert (negate @1)))))
1249 /* -(A + B) -> (-B) - A. */
1251 (negate (plus:c @0 negate_expr_p@1))
1252 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1253 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1254 (minus (negate @1) @0)))
1256 /* -(A - B) -> B - A. */
1258 (negate (minus @0 @1))
1259 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1260 || (FLOAT_TYPE_P (type)
1261 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1262 && !HONOR_SIGNED_ZEROS (type)))
1265 (negate (pointer_diff @0 @1))
1266 (if (TYPE_OVERFLOW_UNDEFINED (type))
1267 (pointer_diff @1 @0)))
1269 /* A - B -> A + (-B) if B is easily negatable. */
1271 (minus @0 negate_expr_p@1)
1272 (if (!FIXED_POINT_TYPE_P (type))
1273 (plus @0 (negate @1))))
1275 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1277 For bitwise binary operations apply operand conversions to the
1278 binary operation result instead of to the operands. This allows
1279 to combine successive conversions and bitwise binary operations.
1280 We combine the above two cases by using a conditional convert. */
1281 (for bitop (bit_and bit_ior bit_xor)
1283 (bitop (convert @0) (convert? @1))
1284 (if (((TREE_CODE (@1) == INTEGER_CST
1285 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1286 && int_fits_type_p (@1, TREE_TYPE (@0)))
1287 || types_match (@0, @1))
1288 /* ??? This transform conflicts with fold-const.c doing
1289 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1290 constants (if x has signed type, the sign bit cannot be set
1291 in c). This folds extension into the BIT_AND_EXPR.
1292 Restrict it to GIMPLE to avoid endless recursions. */
1293 && (bitop != BIT_AND_EXPR || GIMPLE)
1294 && (/* That's a good idea if the conversion widens the operand, thus
1295 after hoisting the conversion the operation will be narrower. */
1296 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1297 /* It's also a good idea if the conversion is to a non-integer
1299 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1300 /* Or if the precision of TO is not the same as the precision
1302 || !type_has_mode_precision_p (type)))
1303 (convert (bitop @0 (convert @1))))))
1305 (for bitop (bit_and bit_ior)
1306 rbitop (bit_ior bit_and)
1307 /* (x | y) & x -> x */
1308 /* (x & y) | x -> x */
1310 (bitop:c (rbitop:c @0 @1) @0)
1312 /* (~x | y) & x -> x & y */
1313 /* (~x & y) | x -> x | y */
1315 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1318 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1320 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1321 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1323 /* Combine successive equal operations with constants. */
1324 (for bitop (bit_and bit_ior bit_xor)
1326 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1327 (if (!CONSTANT_CLASS_P (@0))
1328 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1329 folded to a constant. */
1330 (bitop @0 (bitop @1 @2))
1331 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1332 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1333 the values involved are such that the operation can't be decided at
1334 compile time. Try folding one of @0 or @1 with @2 to see whether
1335 that combination can be decided at compile time.
1337 Keep the existing form if both folds fail, to avoid endless
1339 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1341 (bitop @1 { cst1; })
1342 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1344 (bitop @0 { cst2; }))))))))
1346 /* Try simple folding for X op !X, and X op X with the help
1347 of the truth_valued_p and logical_inverted_value predicates. */
1348 (match truth_valued_p
1350 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1351 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1352 (match truth_valued_p
1354 (match truth_valued_p
1357 (match (logical_inverted_value @0)
1359 (match (logical_inverted_value @0)
1360 (bit_not truth_valued_p@0))
1361 (match (logical_inverted_value @0)
1362 (eq @0 integer_zerop))
1363 (match (logical_inverted_value @0)
1364 (ne truth_valued_p@0 integer_truep))
1365 (match (logical_inverted_value @0)
1366 (bit_xor truth_valued_p@0 integer_truep))
1370 (bit_and:c @0 (logical_inverted_value @0))
1371 { build_zero_cst (type); })
1372 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1373 (for op (bit_ior bit_xor)
1375 (op:c truth_valued_p@0 (logical_inverted_value @0))
1376 { constant_boolean_node (true, type); }))
1377 /* X ==/!= !X is false/true. */
1380 (op:c truth_valued_p@0 (logical_inverted_value @0))
1381 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1385 (bit_not (bit_not @0))
1388 /* Convert ~ (-A) to A - 1. */
1390 (bit_not (convert? (negate @0)))
1391 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1392 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1393 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1395 /* Convert - (~A) to A + 1. */
1397 (negate (nop_convert (bit_not @0)))
1398 (plus (view_convert @0) { build_each_one_cst (type); }))
1400 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1402 (bit_not (convert? (minus @0 integer_each_onep)))
1403 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1404 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1405 (convert (negate @0))))
1407 (bit_not (convert? (plus @0 integer_all_onesp)))
1408 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1409 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1410 (convert (negate @0))))
1412 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1414 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1415 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1416 (convert (bit_xor @0 (bit_not @1)))))
1418 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1419 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1420 (convert (bit_xor @0 @1))))
1422 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1424 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1425 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1426 (bit_not (bit_xor (view_convert @0) @1))))
1428 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1430 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1431 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1433 /* Fold A - (A & B) into ~B & A. */
1435 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1436 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1437 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1438 (convert (bit_and (bit_not @1) @0))))
1440 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1441 (for cmp (gt lt ge le)
1443 (mult (convert (cmp @0 @1)) @2)
1444 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1446 /* For integral types with undefined overflow and C != 0 fold
1447 x * C EQ/NE y * C into x EQ/NE y. */
1450 (cmp (mult:c @0 @1) (mult:c @2 @1))
1451 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1452 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1453 && tree_expr_nonzero_p (@1))
1456 /* For integral types with wrapping overflow and C odd fold
1457 x * C EQ/NE y * C into x EQ/NE y. */
1460 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1461 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1462 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1463 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1466 /* For integral types with undefined overflow and C != 0 fold
1467 x * C RELOP y * C into:
1469 x RELOP y for nonnegative C
1470 y RELOP x for negative C */
1471 (for cmp (lt gt le ge)
1473 (cmp (mult:c @0 @1) (mult:c @2 @1))
1474 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1475 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1476 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1478 (if (TREE_CODE (@1) == INTEGER_CST
1479 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1482 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1486 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1487 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1488 && TYPE_UNSIGNED (TREE_TYPE (@0))
1489 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1490 && (wi::to_wide (@2)
1491 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1492 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1493 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1495 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1496 (for cmp (simple_comparison)
1498 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1499 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1502 /* X / C1 op C2 into a simple range test. */
1503 (for cmp (simple_comparison)
1505 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1506 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1507 && integer_nonzerop (@1)
1508 && !TREE_OVERFLOW (@1)
1509 && !TREE_OVERFLOW (@2))
1510 (with { tree lo, hi; bool neg_overflow;
1511 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1514 (if (code == LT_EXPR || code == GE_EXPR)
1515 (if (TREE_OVERFLOW (lo))
1516 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1517 (if (code == LT_EXPR)
1520 (if (code == LE_EXPR || code == GT_EXPR)
1521 (if (TREE_OVERFLOW (hi))
1522 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1523 (if (code == LE_EXPR)
1527 { build_int_cst (type, code == NE_EXPR); })
1528 (if (code == EQ_EXPR && !hi)
1530 (if (code == EQ_EXPR && !lo)
1532 (if (code == NE_EXPR && !hi)
1534 (if (code == NE_EXPR && !lo)
1537 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1541 tree etype = range_check_type (TREE_TYPE (@0));
1544 if (! TYPE_UNSIGNED (etype))
1545 etype = unsigned_type_for (etype);
1546 hi = fold_convert (etype, hi);
1547 lo = fold_convert (etype, lo);
1548 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1551 (if (etype && hi && !TREE_OVERFLOW (hi))
1552 (if (code == EQ_EXPR)
1553 (le (minus (convert:etype @0) { lo; }) { hi; })
1554 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1556 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1557 (for op (lt le ge gt)
1559 (op (plus:c @0 @2) (plus:c @1 @2))
1560 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1561 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1563 /* For equality and subtraction, this is also true with wrapping overflow. */
1564 (for op (eq ne minus)
1566 (op (plus:c @0 @2) (plus:c @1 @2))
1567 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1568 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1569 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1572 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1573 (for op (lt le ge gt)
1575 (op (minus @0 @2) (minus @1 @2))
1576 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1577 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1579 /* For equality and subtraction, this is also true with wrapping overflow. */
1580 (for op (eq ne minus)
1582 (op (minus @0 @2) (minus @1 @2))
1583 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1584 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1585 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1587 /* And for pointers... */
1588 (for op (simple_comparison)
1590 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1591 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1594 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1595 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1596 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1597 (pointer_diff @0 @1)))
1599 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1600 (for op (lt le ge gt)
1602 (op (minus @2 @0) (minus @2 @1))
1603 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1604 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1606 /* For equality and subtraction, this is also true with wrapping overflow. */
1607 (for op (eq ne minus)
1609 (op (minus @2 @0) (minus @2 @1))
1610 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1611 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1612 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1614 /* And for pointers... */
1615 (for op (simple_comparison)
1617 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1618 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1621 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1622 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1623 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1624 (pointer_diff @1 @0)))
1626 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1627 (for op (lt le gt ge)
1629 (op:c (plus:c@2 @0 @1) @1)
1630 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1631 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1632 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1633 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1634 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1635 /* For equality, this is also true with wrapping overflow. */
1638 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1639 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1640 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1641 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1642 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1643 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1644 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1645 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1647 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1648 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1649 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1650 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1651 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1653 /* X - Y < X is the same as Y > 0 when there is no overflow.
1654 For equality, this is also true with wrapping overflow. */
1655 (for op (simple_comparison)
1657 (op:c @0 (minus@2 @0 @1))
1658 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1659 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1660 || ((op == EQ_EXPR || op == NE_EXPR)
1661 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1662 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1663 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1666 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1667 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1671 (cmp (trunc_div @0 @1) integer_zerop)
1672 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1673 /* Complex ==/!= is allowed, but not </>=. */
1674 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1675 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1678 /* X == C - X can never be true if C is odd. */
1681 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1682 (if (TREE_INT_CST_LOW (@1) & 1)
1683 { constant_boolean_node (cmp == NE_EXPR, type); })))
1685 /* Arguments on which one can call get_nonzero_bits to get the bits
1687 (match with_possible_nonzero_bits
1689 (match with_possible_nonzero_bits
1691 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1692 /* Slightly extended version, do not make it recursive to keep it cheap. */
1693 (match (with_possible_nonzero_bits2 @0)
1694 with_possible_nonzero_bits@0)
1695 (match (with_possible_nonzero_bits2 @0)
1696 (bit_and:c with_possible_nonzero_bits@0 @2))
1698 /* Same for bits that are known to be set, but we do not have
1699 an equivalent to get_nonzero_bits yet. */
1700 (match (with_certain_nonzero_bits2 @0)
1702 (match (with_certain_nonzero_bits2 @0)
1703 (bit_ior @1 INTEGER_CST@0))
1705 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1708 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1709 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1710 { constant_boolean_node (cmp == NE_EXPR, type); })))
1712 /* ((X inner_op C0) outer_op C1)
1713 With X being a tree where value_range has reasoned certain bits to always be
1714 zero throughout its computed value range,
1715 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1716 where zero_mask has 1's for all bits that are sure to be 0 in
1718 if (inner_op == '^') C0 &= ~C1;
1719 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1720 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1722 (for inner_op (bit_ior bit_xor)
1723 outer_op (bit_xor bit_ior)
1726 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1730 wide_int zero_mask_not;
1734 if (TREE_CODE (@2) == SSA_NAME)
1735 zero_mask_not = get_nonzero_bits (@2);
1739 if (inner_op == BIT_XOR_EXPR)
1741 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1742 cst_emit = C0 | wi::to_wide (@1);
1746 C0 = wi::to_wide (@0);
1747 cst_emit = C0 ^ wi::to_wide (@1);
1750 (if (!fail && (C0 & zero_mask_not) == 0)
1751 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1752 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1753 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1755 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1757 (pointer_plus (pointer_plus:s @0 @1) @3)
1758 (pointer_plus @0 (plus @1 @3)))
1764 tem4 = (unsigned long) tem3;
1769 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1770 /* Conditionally look through a sign-changing conversion. */
1771 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1772 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1773 || (GENERIC && type == TREE_TYPE (@1))))
1776 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1777 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1781 tem = (sizetype) ptr;
1785 and produce the simpler and easier to analyze with respect to alignment
1786 ... = ptr & ~algn; */
1788 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1789 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1790 (bit_and @0 { algn; })))
1792 /* Try folding difference of addresses. */
1794 (minus (convert ADDR_EXPR@0) (convert @1))
1795 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1796 (with { poly_int64 diff; }
1797 (if (ptr_difference_const (@0, @1, &diff))
1798 { build_int_cst_type (type, diff); }))))
1800 (minus (convert @0) (convert ADDR_EXPR@1))
1801 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1802 (with { poly_int64 diff; }
1803 (if (ptr_difference_const (@0, @1, &diff))
1804 { build_int_cst_type (type, diff); }))))
1806 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1807 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1808 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1809 (with { poly_int64 diff; }
1810 (if (ptr_difference_const (@0, @1, &diff))
1811 { build_int_cst_type (type, diff); }))))
1813 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1814 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1815 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1816 (with { poly_int64 diff; }
1817 (if (ptr_difference_const (@0, @1, &diff))
1818 { build_int_cst_type (type, diff); }))))
1820 /* If arg0 is derived from the address of an object or function, we may
1821 be able to fold this expression using the object or function's
1824 (bit_and (convert? @0) INTEGER_CST@1)
1825 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1826 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1830 unsigned HOST_WIDE_INT bitpos;
1831 get_pointer_alignment_1 (@0, &align, &bitpos);
1833 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1834 { wide_int_to_tree (type, (wi::to_wide (@1)
1835 & (bitpos / BITS_PER_UNIT))); }))))
1838 /* We can't reassociate at all for saturating types. */
1839 (if (!TYPE_SATURATING (type))
1841 /* Contract negates. */
1842 /* A + (-B) -> A - B */
1844 (plus:c @0 (convert? (negate @1)))
1845 /* Apply STRIP_NOPS on the negate. */
1846 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1847 && !TYPE_OVERFLOW_SANITIZED (type))
1851 if (INTEGRAL_TYPE_P (type)
1852 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1853 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1855 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1856 /* A - (-B) -> A + B */
1858 (minus @0 (convert? (negate @1)))
1859 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1860 && !TYPE_OVERFLOW_SANITIZED (type))
1864 if (INTEGRAL_TYPE_P (type)
1865 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1866 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1868 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1870 Sign-extension is ok except for INT_MIN, which thankfully cannot
1871 happen without overflow. */
1873 (negate (convert (negate @1)))
1874 (if (INTEGRAL_TYPE_P (type)
1875 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1876 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1877 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1878 && !TYPE_OVERFLOW_SANITIZED (type)
1879 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1882 (negate (convert negate_expr_p@1))
1883 (if (SCALAR_FLOAT_TYPE_P (type)
1884 && ((DECIMAL_FLOAT_TYPE_P (type)
1885 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1886 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1887 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1888 (convert (negate @1))))
1890 (negate (nop_convert (negate @1)))
1891 (if (!TYPE_OVERFLOW_SANITIZED (type)
1892 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1895 /* We can't reassociate floating-point unless -fassociative-math
1896 or fixed-point plus or minus because of saturation to +-Inf. */
1897 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1898 && !FIXED_POINT_TYPE_P (type))
1900 /* Match patterns that allow contracting a plus-minus pair
1901 irrespective of overflow issues. */
1902 /* (A +- B) - A -> +- B */
1903 /* (A +- B) -+ B -> A */
1904 /* A - (A +- B) -> -+ B */
1905 /* A +- (B -+ A) -> +- B */
1907 (minus (plus:c @0 @1) @0)
1910 (minus (minus @0 @1) @0)
1913 (plus:c (minus @0 @1) @1)
1916 (minus @0 (plus:c @0 @1))
1919 (minus @0 (minus @0 @1))
1921 /* (A +- B) + (C - A) -> C +- B */
1922 /* (A + B) - (A - C) -> B + C */
1923 /* More cases are handled with comparisons. */
1925 (plus:c (plus:c @0 @1) (minus @2 @0))
1928 (plus:c (minus @0 @1) (minus @2 @0))
1931 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1932 (if (TYPE_OVERFLOW_UNDEFINED (type)
1933 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1934 (pointer_diff @2 @1)))
1936 (minus (plus:c @0 @1) (minus @0 @2))
1939 /* (A +- CST1) +- CST2 -> A + CST3
1940 Use view_convert because it is safe for vectors and equivalent for
1942 (for outer_op (plus minus)
1943 (for inner_op (plus minus)
1944 neg_inner_op (minus plus)
1946 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1948 /* If one of the types wraps, use that one. */
1949 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1950 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1951 forever if something doesn't simplify into a constant. */
1952 (if (!CONSTANT_CLASS_P (@0))
1953 (if (outer_op == PLUS_EXPR)
1954 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1955 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1956 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1957 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1958 (if (outer_op == PLUS_EXPR)
1959 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1960 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1961 /* If the constant operation overflows we cannot do the transform
1962 directly as we would introduce undefined overflow, for example
1963 with (a - 1) + INT_MIN. */
1964 (if (types_match (type, @0))
1965 (with { tree cst = const_binop (outer_op == inner_op
1966 ? PLUS_EXPR : MINUS_EXPR,
1968 (if (cst && !TREE_OVERFLOW (cst))
1969 (inner_op @0 { cst; } )
1970 /* X+INT_MAX+1 is X-INT_MIN. */
1971 (if (INTEGRAL_TYPE_P (type) && cst
1972 && wi::to_wide (cst) == wi::min_value (type))
1973 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1974 /* Last resort, use some unsigned type. */
1975 (with { tree utype = unsigned_type_for (type); }
1977 (view_convert (inner_op
1978 (view_convert:utype @0)
1980 { drop_tree_overflow (cst); }))))))))))))))
1982 /* (CST1 - A) +- CST2 -> CST3 - A */
1983 (for outer_op (plus minus)
1985 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1986 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1987 (if (cst && !TREE_OVERFLOW (cst))
1988 (minus { cst; } @0)))))
1990 /* CST1 - (CST2 - A) -> CST3 + A */
1992 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1993 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1994 (if (cst && !TREE_OVERFLOW (cst))
1995 (plus { cst; } @0))))
1999 (plus:c (bit_not @0) @0)
2000 (if (!TYPE_OVERFLOW_TRAPS (type))
2001 { build_all_ones_cst (type); }))
2005 (plus (convert? (bit_not @0)) integer_each_onep)
2006 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2007 (negate (convert @0))))
2011 (minus (convert? (negate @0)) integer_each_onep)
2012 (if (!TYPE_OVERFLOW_TRAPS (type)
2013 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2014 (bit_not (convert @0))))
2018 (minus integer_all_onesp @0)
2021 /* (T)(P + A) - (T)P -> (T) A */
2023 (minus (convert (plus:c @@0 @1))
2025 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2026 /* For integer types, if A has a smaller type
2027 than T the result depends on the possible
2029 E.g. T=size_t, A=(unsigned)429497295, P>0.
2030 However, if an overflow in P + A would cause
2031 undefined behavior, we can assume that there
2033 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2034 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2037 (minus (convert (pointer_plus @@0 @1))
2039 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2040 /* For pointer types, if the conversion of A to the
2041 final type requires a sign- or zero-extension,
2042 then we have to punt - it is not defined which
2044 || (POINTER_TYPE_P (TREE_TYPE (@0))
2045 && TREE_CODE (@1) == INTEGER_CST
2046 && tree_int_cst_sign_bit (@1) == 0))
2049 (pointer_diff (pointer_plus @@0 @1) @0)
2050 /* The second argument of pointer_plus must be interpreted as signed, and
2051 thus sign-extended if necessary. */
2052 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2053 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2054 second arg is unsigned even when we need to consider it as signed,
2055 we don't want to diagnose overflow here. */
2056 (convert (view_convert:stype @1))))
2058 /* (T)P - (T)(P + A) -> -(T) A */
2060 (minus (convert? @0)
2061 (convert (plus:c @@0 @1)))
2062 (if (INTEGRAL_TYPE_P (type)
2063 && TYPE_OVERFLOW_UNDEFINED (type)
2064 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2065 (with { tree utype = unsigned_type_for (type); }
2066 (convert (negate (convert:utype @1))))
2067 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2068 /* For integer types, if A has a smaller type
2069 than T the result depends on the possible
2071 E.g. T=size_t, A=(unsigned)429497295, P>0.
2072 However, if an overflow in P + A would cause
2073 undefined behavior, we can assume that there
2075 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2076 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2077 (negate (convert @1)))))
2080 (convert (pointer_plus @@0 @1)))
2081 (if (INTEGRAL_TYPE_P (type)
2082 && TYPE_OVERFLOW_UNDEFINED (type)
2083 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2084 (with { tree utype = unsigned_type_for (type); }
2085 (convert (negate (convert:utype @1))))
2086 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2087 /* For pointer types, if the conversion of A to the
2088 final type requires a sign- or zero-extension,
2089 then we have to punt - it is not defined which
2091 || (POINTER_TYPE_P (TREE_TYPE (@0))
2092 && TREE_CODE (@1) == INTEGER_CST
2093 && tree_int_cst_sign_bit (@1) == 0))
2094 (negate (convert @1)))))
2096 (pointer_diff @0 (pointer_plus @@0 @1))
2097 /* The second argument of pointer_plus must be interpreted as signed, and
2098 thus sign-extended if necessary. */
2099 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2100 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2101 second arg is unsigned even when we need to consider it as signed,
2102 we don't want to diagnose overflow here. */
2103 (negate (convert (view_convert:stype @1)))))
2105 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2107 (minus (convert (plus:c @@0 @1))
2108 (convert (plus:c @0 @2)))
2109 (if (INTEGRAL_TYPE_P (type)
2110 && TYPE_OVERFLOW_UNDEFINED (type)
2111 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2112 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2113 (with { tree utype = unsigned_type_for (type); }
2114 (convert (minus (convert:utype @1) (convert:utype @2))))
2115 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2116 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2117 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2118 /* For integer types, if A has a smaller type
2119 than T the result depends on the possible
2121 E.g. T=size_t, A=(unsigned)429497295, P>0.
2122 However, if an overflow in P + A would cause
2123 undefined behavior, we can assume that there
2125 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2126 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2127 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2128 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2129 (minus (convert @1) (convert @2)))))
2131 (minus (convert (pointer_plus @@0 @1))
2132 (convert (pointer_plus @0 @2)))
2133 (if (INTEGRAL_TYPE_P (type)
2134 && TYPE_OVERFLOW_UNDEFINED (type)
2135 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2136 (with { tree utype = unsigned_type_for (type); }
2137 (convert (minus (convert:utype @1) (convert:utype @2))))
2138 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2139 /* For pointer types, if the conversion of A to the
2140 final type requires a sign- or zero-extension,
2141 then we have to punt - it is not defined which
2143 || (POINTER_TYPE_P (TREE_TYPE (@0))
2144 && TREE_CODE (@1) == INTEGER_CST
2145 && tree_int_cst_sign_bit (@1) == 0
2146 && TREE_CODE (@2) == INTEGER_CST
2147 && tree_int_cst_sign_bit (@2) == 0))
2148 (minus (convert @1) (convert @2)))))
2150 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2151 /* The second argument of pointer_plus must be interpreted as signed, and
2152 thus sign-extended if necessary. */
2153 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2154 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2155 second arg is unsigned even when we need to consider it as signed,
2156 we don't want to diagnose overflow here. */
2157 (minus (convert (view_convert:stype @1))
2158 (convert (view_convert:stype @2)))))))
2160 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2161 Modeled after fold_plusminus_mult_expr. */
2162 (if (!TYPE_SATURATING (type)
2163 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2164 (for plusminus (plus minus)
2166 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2167 (if ((!ANY_INTEGRAL_TYPE_P (type)
2168 || TYPE_OVERFLOW_WRAPS (type)
2169 || (INTEGRAL_TYPE_P (type)
2170 && tree_expr_nonzero_p (@0)
2171 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2172 /* If @1 +- @2 is constant require a hard single-use on either
2173 original operand (but not on both). */
2174 && (single_use (@3) || single_use (@4)))
2175 (mult (plusminus @1 @2) @0)))
2176 /* We cannot generate constant 1 for fract. */
2177 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2179 (plusminus @0 (mult:c@3 @0 @2))
2180 (if ((!ANY_INTEGRAL_TYPE_P (type)
2181 || TYPE_OVERFLOW_WRAPS (type)
2182 || (INTEGRAL_TYPE_P (type)
2183 && tree_expr_nonzero_p (@0)
2184 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2186 (mult (plusminus { build_one_cst (type); } @2) @0)))
2188 (plusminus (mult:c@3 @0 @2) @0)
2189 (if ((!ANY_INTEGRAL_TYPE_P (type)
2190 || TYPE_OVERFLOW_WRAPS (type)
2191 || (INTEGRAL_TYPE_P (type)
2192 && tree_expr_nonzero_p (@0)
2193 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2195 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2197 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2199 (for minmax (min max FMIN_ALL FMAX_ALL)
2203 /* min(max(x,y),y) -> y. */
2205 (min:c (max:c @0 @1) @1)
2207 /* max(min(x,y),y) -> y. */
2209 (max:c (min:c @0 @1) @1)
2211 /* max(a,-a) -> abs(a). */
2213 (max:c @0 (negate @0))
2214 (if (TREE_CODE (type) != COMPLEX_TYPE
2215 && (! ANY_INTEGRAL_TYPE_P (type)
2216 || TYPE_OVERFLOW_UNDEFINED (type)))
2218 /* min(a,-a) -> -abs(a). */
2220 (min:c @0 (negate @0))
2221 (if (TREE_CODE (type) != COMPLEX_TYPE
2222 && (! ANY_INTEGRAL_TYPE_P (type)
2223 || TYPE_OVERFLOW_UNDEFINED (type)))
2228 (if (INTEGRAL_TYPE_P (type)
2229 && TYPE_MIN_VALUE (type)
2230 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2232 (if (INTEGRAL_TYPE_P (type)
2233 && TYPE_MAX_VALUE (type)
2234 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2239 (if (INTEGRAL_TYPE_P (type)
2240 && TYPE_MAX_VALUE (type)
2241 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2243 (if (INTEGRAL_TYPE_P (type)
2244 && TYPE_MIN_VALUE (type)
2245 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2248 /* max (a, a + CST) -> a + CST where CST is positive. */
2249 /* max (a, a + CST) -> a where CST is negative. */
2251 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2252 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2253 (if (tree_int_cst_sgn (@1) > 0)
2257 /* min (a, a + CST) -> a where CST is positive. */
2258 /* min (a, a + CST) -> a + CST where CST is negative. */
2260 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2261 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2262 (if (tree_int_cst_sgn (@1) > 0)
2266 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2267 and the outer convert demotes the expression back to x's type. */
2268 (for minmax (min max)
2270 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2271 (if (INTEGRAL_TYPE_P (type)
2272 && types_match (@1, type) && int_fits_type_p (@2, type)
2273 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2274 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2275 (minmax @1 (convert @2)))))
2277 (for minmax (FMIN_ALL FMAX_ALL)
2278 /* If either argument is NaN, return the other one. Avoid the
2279 transformation if we get (and honor) a signalling NaN. */
2281 (minmax:c @0 REAL_CST@1)
2282 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2283 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2285 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2286 functions to return the numeric arg if the other one is NaN.
2287 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2288 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2289 worry about it either. */
2290 (if (flag_finite_math_only)
2297 /* min (-A, -B) -> -max (A, B) */
2298 (for minmax (min max FMIN_ALL FMAX_ALL)
2299 maxmin (max min FMAX_ALL FMIN_ALL)
2301 (minmax (negate:s@2 @0) (negate:s@3 @1))
2302 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2303 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2304 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2305 (negate (maxmin @0 @1)))))
2306 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2307 MAX (~X, ~Y) -> ~MIN (X, Y) */
2308 (for minmax (min max)
2311 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2312 (bit_not (maxmin @0 @1))))
2314 /* MIN (X, Y) == X -> X <= Y */
2315 (for minmax (min min max max)
2319 (cmp:c (minmax:c @0 @1) @0)
2320 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2322 /* MIN (X, 5) == 0 -> X == 0
2323 MIN (X, 5) == 7 -> false */
2326 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2327 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2328 TYPE_SIGN (TREE_TYPE (@0))))
2329 { constant_boolean_node (cmp == NE_EXPR, type); }
2330 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2331 TYPE_SIGN (TREE_TYPE (@0))))
2335 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2336 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2337 TYPE_SIGN (TREE_TYPE (@0))))
2338 { constant_boolean_node (cmp == NE_EXPR, type); }
2339 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2340 TYPE_SIGN (TREE_TYPE (@0))))
2342 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2343 (for minmax (min min max max min min max max )
2344 cmp (lt le gt ge gt ge lt le )
2345 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2347 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2348 (comb (cmp @0 @2) (cmp @1 @2))))
2350 /* Simplifications of shift and rotates. */
2352 (for rotate (lrotate rrotate)
2354 (rotate integer_all_onesp@0 @1)
2357 /* Optimize -1 >> x for arithmetic right shifts. */
2359 (rshift integer_all_onesp@0 @1)
2360 (if (!TYPE_UNSIGNED (type)
2361 && tree_expr_nonnegative_p (@1))
2364 /* Optimize (x >> c) << c into x & (-1<<c). */
2366 (lshift (rshift @0 INTEGER_CST@1) @1)
2367 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2368 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2370 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2373 (rshift (lshift @0 INTEGER_CST@1) @1)
2374 (if (TYPE_UNSIGNED (type)
2375 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2376 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2378 (for shiftrotate (lrotate rrotate lshift rshift)
2380 (shiftrotate @0 integer_zerop)
2383 (shiftrotate integer_zerop@0 @1)
2385 /* Prefer vector1 << scalar to vector1 << vector2
2386 if vector2 is uniform. */
2387 (for vec (VECTOR_CST CONSTRUCTOR)
2389 (shiftrotate @0 vec@1)
2390 (with { tree tem = uniform_vector_p (@1); }
2392 (shiftrotate @0 { tem; }))))))
2394 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2395 Y is 0. Similarly for X >> Y. */
2397 (for shift (lshift rshift)
2399 (shift @0 SSA_NAME@1)
2400 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2402 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2403 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2405 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2409 /* Rewrite an LROTATE_EXPR by a constant into an
2410 RROTATE_EXPR by a new constant. */
2412 (lrotate @0 INTEGER_CST@1)
2413 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2414 build_int_cst (TREE_TYPE (@1),
2415 element_precision (type)), @1); }))
2417 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2418 (for op (lrotate rrotate rshift lshift)
2420 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2421 (with { unsigned int prec = element_precision (type); }
2422 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2423 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2424 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2425 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2426 (with { unsigned int low = (tree_to_uhwi (@1)
2427 + tree_to_uhwi (@2)); }
2428 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2429 being well defined. */
2431 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2432 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2433 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2434 { build_zero_cst (type); }
2435 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2436 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2439 /* ((1 << A) & 1) != 0 -> A == 0
2440 ((1 << A) & 1) == 0 -> A != 0 */
2444 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2445 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2447 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2448 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2452 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2453 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2455 || (!integer_zerop (@2)
2456 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2457 { constant_boolean_node (cmp == NE_EXPR, type); }
2458 (if (!integer_zerop (@2)
2459 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2460 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2462 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2463 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2464 if the new mask might be further optimized. */
2465 (for shift (lshift rshift)
2467 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2469 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2470 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2471 && tree_fits_uhwi_p (@1)
2472 && tree_to_uhwi (@1) > 0
2473 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2476 unsigned int shiftc = tree_to_uhwi (@1);
2477 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2478 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2479 tree shift_type = TREE_TYPE (@3);
2482 if (shift == LSHIFT_EXPR)
2483 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2484 else if (shift == RSHIFT_EXPR
2485 && type_has_mode_precision_p (shift_type))
2487 prec = TYPE_PRECISION (TREE_TYPE (@3));
2489 /* See if more bits can be proven as zero because of
2492 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2494 tree inner_type = TREE_TYPE (@0);
2495 if (type_has_mode_precision_p (inner_type)
2496 && TYPE_PRECISION (inner_type) < prec)
2498 prec = TYPE_PRECISION (inner_type);
2499 /* See if we can shorten the right shift. */
2501 shift_type = inner_type;
2502 /* Otherwise X >> C1 is all zeros, so we'll optimize
2503 it into (X, 0) later on by making sure zerobits
2507 zerobits = HOST_WIDE_INT_M1U;
2510 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2511 zerobits <<= prec - shiftc;
2513 /* For arithmetic shift if sign bit could be set, zerobits
2514 can contain actually sign bits, so no transformation is
2515 possible, unless MASK masks them all away. In that
2516 case the shift needs to be converted into logical shift. */
2517 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2518 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2520 if ((mask & zerobits) == 0)
2521 shift_type = unsigned_type_for (TREE_TYPE (@3));
2527 /* ((X << 16) & 0xff00) is (X, 0). */
2528 (if ((mask & zerobits) == mask)
2529 { build_int_cst (type, 0); }
2530 (with { newmask = mask | zerobits; }
2531 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2534 /* Only do the transformation if NEWMASK is some integer
2536 for (prec = BITS_PER_UNIT;
2537 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2538 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2541 (if (prec < HOST_BITS_PER_WIDE_INT
2542 || newmask == HOST_WIDE_INT_M1U)
2544 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2545 (if (!tree_int_cst_equal (newmaskt, @2))
2546 (if (shift_type != TREE_TYPE (@3))
2547 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2548 (bit_and @4 { newmaskt; })))))))))))))
2550 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2551 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2552 (for shift (lshift rshift)
2553 (for bit_op (bit_and bit_xor bit_ior)
2555 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2556 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2557 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2558 (bit_op (shift (convert @0) @1) { mask; }))))))
2560 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2562 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2563 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2564 && (element_precision (TREE_TYPE (@0))
2565 <= element_precision (TREE_TYPE (@1))
2566 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2568 { tree shift_type = TREE_TYPE (@0); }
2569 (convert (rshift (convert:shift_type @1) @2)))))
2571 /* ~(~X >>r Y) -> X >>r Y
2572 ~(~X <<r Y) -> X <<r Y */
2573 (for rotate (lrotate rrotate)
2575 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2576 (if ((element_precision (TREE_TYPE (@0))
2577 <= element_precision (TREE_TYPE (@1))
2578 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2579 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2580 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2582 { tree rotate_type = TREE_TYPE (@0); }
2583 (convert (rotate (convert:rotate_type @1) @2))))))
2585 /* Simplifications of conversions. */
2587 /* Basic strip-useless-type-conversions / strip_nops. */
2588 (for cvt (convert view_convert float fix_trunc)
2591 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2592 || (GENERIC && type == TREE_TYPE (@0)))
2595 /* Contract view-conversions. */
2597 (view_convert (view_convert @0))
2600 /* For integral conversions with the same precision or pointer
2601 conversions use a NOP_EXPR instead. */
2604 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2605 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2606 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2609 /* Strip inner integral conversions that do not change precision or size, or
2610 zero-extend while keeping the same size (for bool-to-char). */
2612 (view_convert (convert@0 @1))
2613 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2614 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2615 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2616 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2617 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2618 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2621 /* Simplify a view-converted empty constructor. */
2623 (view_convert CONSTRUCTOR@0)
2624 (if (TREE_CODE (@0) != SSA_NAME
2625 && CONSTRUCTOR_NELTS (@0) == 0)
2626 { build_zero_cst (type); }))
2628 /* Re-association barriers around constants and other re-association
2629 barriers can be removed. */
2631 (paren CONSTANT_CLASS_P@0)
2634 (paren (paren@1 @0))
2637 /* Handle cases of two conversions in a row. */
2638 (for ocvt (convert float fix_trunc)
2639 (for icvt (convert float)
2644 tree inside_type = TREE_TYPE (@0);
2645 tree inter_type = TREE_TYPE (@1);
2646 int inside_int = INTEGRAL_TYPE_P (inside_type);
2647 int inside_ptr = POINTER_TYPE_P (inside_type);
2648 int inside_float = FLOAT_TYPE_P (inside_type);
2649 int inside_vec = VECTOR_TYPE_P (inside_type);
2650 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2651 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2652 int inter_int = INTEGRAL_TYPE_P (inter_type);
2653 int inter_ptr = POINTER_TYPE_P (inter_type);
2654 int inter_float = FLOAT_TYPE_P (inter_type);
2655 int inter_vec = VECTOR_TYPE_P (inter_type);
2656 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2657 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2658 int final_int = INTEGRAL_TYPE_P (type);
2659 int final_ptr = POINTER_TYPE_P (type);
2660 int final_float = FLOAT_TYPE_P (type);
2661 int final_vec = VECTOR_TYPE_P (type);
2662 unsigned int final_prec = TYPE_PRECISION (type);
2663 int final_unsignedp = TYPE_UNSIGNED (type);
2666 /* In addition to the cases of two conversions in a row
2667 handled below, if we are converting something to its own
2668 type via an object of identical or wider precision, neither
2669 conversion is needed. */
2670 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2672 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2673 && (((inter_int || inter_ptr) && final_int)
2674 || (inter_float && final_float))
2675 && inter_prec >= final_prec)
2678 /* Likewise, if the intermediate and initial types are either both
2679 float or both integer, we don't need the middle conversion if the
2680 former is wider than the latter and doesn't change the signedness
2681 (for integers). Avoid this if the final type is a pointer since
2682 then we sometimes need the middle conversion. */
2683 (if (((inter_int && inside_int) || (inter_float && inside_float))
2684 && (final_int || final_float)
2685 && inter_prec >= inside_prec
2686 && (inter_float || inter_unsignedp == inside_unsignedp))
2689 /* If we have a sign-extension of a zero-extended value, we can
2690 replace that by a single zero-extension. Likewise if the
2691 final conversion does not change precision we can drop the
2692 intermediate conversion. */
2693 (if (inside_int && inter_int && final_int
2694 && ((inside_prec < inter_prec && inter_prec < final_prec
2695 && inside_unsignedp && !inter_unsignedp)
2696 || final_prec == inter_prec))
2699 /* Two conversions in a row are not needed unless:
2700 - some conversion is floating-point (overstrict for now), or
2701 - some conversion is a vector (overstrict for now), or
2702 - the intermediate type is narrower than both initial and
2704 - the intermediate type and innermost type differ in signedness,
2705 and the outermost type is wider than the intermediate, or
2706 - the initial type is a pointer type and the precisions of the
2707 intermediate and final types differ, or
2708 - the final type is a pointer type and the precisions of the
2709 initial and intermediate types differ. */
2710 (if (! inside_float && ! inter_float && ! final_float
2711 && ! inside_vec && ! inter_vec && ! final_vec
2712 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2713 && ! (inside_int && inter_int
2714 && inter_unsignedp != inside_unsignedp
2715 && inter_prec < final_prec)
2716 && ((inter_unsignedp && inter_prec > inside_prec)
2717 == (final_unsignedp && final_prec > inter_prec))
2718 && ! (inside_ptr && inter_prec != final_prec)
2719 && ! (final_ptr && inside_prec != inter_prec))
2722 /* A truncation to an unsigned type (a zero-extension) should be
2723 canonicalized as bitwise and of a mask. */
2724 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2725 && final_int && inter_int && inside_int
2726 && final_prec == inside_prec
2727 && final_prec > inter_prec
2729 (convert (bit_and @0 { wide_int_to_tree
2731 wi::mask (inter_prec, false,
2732 TYPE_PRECISION (inside_type))); })))
2734 /* If we are converting an integer to a floating-point that can
2735 represent it exactly and back to an integer, we can skip the
2736 floating-point conversion. */
2737 (if (GIMPLE /* PR66211 */
2738 && inside_int && inter_float && final_int &&
2739 (unsigned) significand_size (TYPE_MODE (inter_type))
2740 >= inside_prec - !inside_unsignedp)
2743 /* If we have a narrowing conversion to an integral type that is fed by a
2744 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2745 masks off bits outside the final type (and nothing else). */
2747 (convert (bit_and @0 INTEGER_CST@1))
2748 (if (INTEGRAL_TYPE_P (type)
2749 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2750 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2751 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2752 TYPE_PRECISION (type)), 0))
2756 /* (X /[ex] A) * A -> X. */
2758 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2761 /* Simplify (A / B) * B + (A % B) -> A. */
2762 (for div (trunc_div ceil_div floor_div round_div)
2763 mod (trunc_mod ceil_mod floor_mod round_mod)
2765 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
2768 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
2769 (for op (plus minus)
2771 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2772 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2773 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
2776 wi::overflow_type overflow;
2777 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
2778 TYPE_SIGN (type), &overflow);
2780 (if (types_match (type, TREE_TYPE (@2))
2781 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
2782 (op @0 { wide_int_to_tree (type, mul); })
2783 (with { tree utype = unsigned_type_for (type); }
2784 (convert (op (convert:utype @0)
2785 (mult (convert:utype @1) (convert:utype @2))))))))))
2787 /* Canonicalization of binary operations. */
2789 /* Convert X + -C into X - C. */
2791 (plus @0 REAL_CST@1)
2792 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2793 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2794 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2795 (minus @0 { tem; })))))
2797 /* Convert x+x into x*2. */
2800 (if (SCALAR_FLOAT_TYPE_P (type))
2801 (mult @0 { build_real (type, dconst2); })
2802 (if (INTEGRAL_TYPE_P (type))
2803 (mult @0 { build_int_cst (type, 2); }))))
2807 (minus integer_zerop @1)
2810 (pointer_diff integer_zerop @1)
2811 (negate (convert @1)))
2813 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2814 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2815 (-ARG1 + ARG0) reduces to -ARG1. */
2817 (minus real_zerop@0 @1)
2818 (if (fold_real_zero_addition_p (type, @0, 0))
2821 /* Transform x * -1 into -x. */
2823 (mult @0 integer_minus_onep)
2826 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2827 signed overflow for CST != 0 && CST != -1. */
2829 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2830 (if (TREE_CODE (@2) != INTEGER_CST
2832 && !integer_zerop (@1) && !integer_minus_onep (@1))
2833 (mult (mult @0 @2) @1)))
2835 /* True if we can easily extract the real and imaginary parts of a complex
2837 (match compositional_complex
2838 (convert? (complex @0 @1)))
2840 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2842 (complex (realpart @0) (imagpart @0))
2845 (realpart (complex @0 @1))
2848 (imagpart (complex @0 @1))
2851 /* Sometimes we only care about half of a complex expression. */
2853 (realpart (convert?:s (conj:s @0)))
2854 (convert (realpart @0)))
2856 (imagpart (convert?:s (conj:s @0)))
2857 (convert (negate (imagpart @0))))
2858 (for part (realpart imagpart)
2859 (for op (plus minus)
2861 (part (convert?:s@2 (op:s @0 @1)))
2862 (convert (op (part @0) (part @1))))))
2864 (realpart (convert?:s (CEXPI:s @0)))
2867 (imagpart (convert?:s (CEXPI:s @0)))
2870 /* conj(conj(x)) -> x */
2872 (conj (convert? (conj @0)))
2873 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2876 /* conj({x,y}) -> {x,-y} */
2878 (conj (convert?:s (complex:s @0 @1)))
2879 (with { tree itype = TREE_TYPE (type); }
2880 (complex (convert:itype @0) (negate (convert:itype @1)))))
2882 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2883 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2888 (bswap (bit_not (bswap @0)))
2890 (for bitop (bit_xor bit_ior bit_and)
2892 (bswap (bitop:c (bswap @0) @1))
2893 (bitop @0 (bswap @1)))))
2896 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2898 /* Simplify constant conditions.
2899 Only optimize constant conditions when the selected branch
2900 has the same type as the COND_EXPR. This avoids optimizing
2901 away "c ? x : throw", where the throw has a void type.
2902 Note that we cannot throw away the fold-const.c variant nor
2903 this one as we depend on doing this transform before possibly
2904 A ? B : B -> B triggers and the fold-const.c one can optimize
2905 0 ? A : B to B even if A has side-effects. Something
2906 genmatch cannot handle. */
2908 (cond INTEGER_CST@0 @1 @2)
2909 (if (integer_zerop (@0))
2910 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2912 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2915 (vec_cond VECTOR_CST@0 @1 @2)
2916 (if (integer_all_onesp (@0))
2918 (if (integer_zerop (@0))
2921 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2923 /* This pattern implements two kinds simplification:
2926 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2927 1) Conversions are type widening from smaller type.
2928 2) Const c1 equals to c2 after canonicalizing comparison.
2929 3) Comparison has tree code LT, LE, GT or GE.
2930 This specific pattern is needed when (cmp (convert x) c) may not
2931 be simplified by comparison patterns because of multiple uses of
2932 x. It also makes sense here because simplifying across multiple
2933 referred var is always benefitial for complicated cases.
2936 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2937 (for cmp (lt le gt ge eq)
2939 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2942 tree from_type = TREE_TYPE (@1);
2943 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2944 enum tree_code code = ERROR_MARK;
2946 if (INTEGRAL_TYPE_P (from_type)
2947 && int_fits_type_p (@2, from_type)
2948 && (types_match (c1_type, from_type)
2949 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2950 && (TYPE_UNSIGNED (from_type)
2951 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2952 && (types_match (c2_type, from_type)
2953 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2954 && (TYPE_UNSIGNED (from_type)
2955 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2959 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2961 /* X <= Y - 1 equals to X < Y. */
2964 /* X > Y - 1 equals to X >= Y. */
2968 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2970 /* X < Y + 1 equals to X <= Y. */
2973 /* X >= Y + 1 equals to X > Y. */
2977 if (code != ERROR_MARK
2978 || wi::to_widest (@2) == wi::to_widest (@3))
2980 if (cmp == LT_EXPR || cmp == LE_EXPR)
2982 if (cmp == GT_EXPR || cmp == GE_EXPR)
2986 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2987 else if (int_fits_type_p (@3, from_type))
2991 (if (code == MAX_EXPR)
2992 (convert (max @1 (convert @2)))
2993 (if (code == MIN_EXPR)
2994 (convert (min @1 (convert @2)))
2995 (if (code == EQ_EXPR)
2996 (convert (cond (eq @1 (convert @3))
2997 (convert:from_type @3) (convert:from_type @2)))))))))
2999 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3001 1) OP is PLUS or MINUS.
3002 2) CMP is LT, LE, GT or GE.
3003 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3005 This pattern also handles special cases like:
3007 A) Operand x is a unsigned to signed type conversion and c1 is
3008 integer zero. In this case,
3009 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3010 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3011 B) Const c1 may not equal to (C3 op' C2). In this case we also
3012 check equality for (c1+1) and (c1-1) by adjusting comparison
3015 TODO: Though signed type is handled by this pattern, it cannot be
3016 simplified at the moment because C standard requires additional
3017 type promotion. In order to match&simplify it here, the IR needs
3018 to be cleaned up by other optimizers, i.e, VRP. */
3019 (for op (plus minus)
3020 (for cmp (lt le gt ge)
3022 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3023 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3024 (if (types_match (from_type, to_type)
3025 /* Check if it is special case A). */
3026 || (TYPE_UNSIGNED (from_type)
3027 && !TYPE_UNSIGNED (to_type)
3028 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3029 && integer_zerop (@1)
3030 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3033 wi::overflow_type overflow = wi::OVF_NONE;
3034 enum tree_code code, cmp_code = cmp;
3036 wide_int c1 = wi::to_wide (@1);
3037 wide_int c2 = wi::to_wide (@2);
3038 wide_int c3 = wi::to_wide (@3);
3039 signop sgn = TYPE_SIGN (from_type);
3041 /* Handle special case A), given x of unsigned type:
3042 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3043 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3044 if (!types_match (from_type, to_type))
3046 if (cmp_code == LT_EXPR)
3048 if (cmp_code == GE_EXPR)
3050 c1 = wi::max_value (to_type);
3052 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3053 compute (c3 op' c2) and check if it equals to c1 with op' being
3054 the inverted operator of op. Make sure overflow doesn't happen
3055 if it is undefined. */
3056 if (op == PLUS_EXPR)
3057 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3059 real_c1 = wi::add (c3, c2, sgn, &overflow);
3062 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3064 /* Check if c1 equals to real_c1. Boundary condition is handled
3065 by adjusting comparison operation if necessary. */
3066 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3069 /* X <= Y - 1 equals to X < Y. */
3070 if (cmp_code == LE_EXPR)
3072 /* X > Y - 1 equals to X >= Y. */
3073 if (cmp_code == GT_EXPR)
3076 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3079 /* X < Y + 1 equals to X <= Y. */
3080 if (cmp_code == LT_EXPR)
3082 /* X >= Y + 1 equals to X > Y. */
3083 if (cmp_code == GE_EXPR)
3086 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3088 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3090 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3095 (if (code == MAX_EXPR)
3096 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3097 { wide_int_to_tree (from_type, c2); })
3098 (if (code == MIN_EXPR)
3099 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3100 { wide_int_to_tree (from_type, c2); })))))))))
3102 (for cnd (cond vec_cond)
3103 /* A ? B : (A ? X : C) -> A ? B : C. */
3105 (cnd @0 (cnd @0 @1 @2) @3)
3108 (cnd @0 @1 (cnd @0 @2 @3))
3110 /* A ? B : (!A ? C : X) -> A ? B : C. */
3111 /* ??? This matches embedded conditions open-coded because genmatch
3112 would generate matching code for conditions in separate stmts only.
3113 The following is still important to merge then and else arm cases
3114 from if-conversion. */
3116 (cnd @0 @1 (cnd @2 @3 @4))
3117 (if (inverse_conditions_p (@0, @2))
3120 (cnd @0 (cnd @1 @2 @3) @4)
3121 (if (inverse_conditions_p (@0, @1))
3124 /* A ? B : B -> B. */
3129 /* !A ? B : C -> A ? C : B. */
3131 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3134 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3135 return all -1 or all 0 results. */
3136 /* ??? We could instead convert all instances of the vec_cond to negate,
3137 but that isn't necessarily a win on its own. */
3139 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3140 (if (VECTOR_TYPE_P (type)
3141 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3142 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3143 && (TYPE_MODE (TREE_TYPE (type))
3144 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3145 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3147 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3149 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3150 (if (VECTOR_TYPE_P (type)
3151 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3152 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3153 && (TYPE_MODE (TREE_TYPE (type))
3154 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3155 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3158 /* Simplifications of comparisons. */
3160 /* See if we can reduce the magnitude of a constant involved in a
3161 comparison by changing the comparison code. This is a canonicalization
3162 formerly done by maybe_canonicalize_comparison_1. */
3166 (cmp @0 uniform_integer_cst_p@1)
3167 (with { tree cst = uniform_integer_cst_p (@1); }
3168 (if (tree_int_cst_sgn (cst) == -1)
3169 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3170 wide_int_to_tree (TREE_TYPE (cst),
3176 (cmp @0 uniform_integer_cst_p@1)
3177 (with { tree cst = uniform_integer_cst_p (@1); }
3178 (if (tree_int_cst_sgn (cst) == 1)
3179 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3180 wide_int_to_tree (TREE_TYPE (cst),
3181 wi::to_wide (cst) - 1)); })))))
3183 /* We can simplify a logical negation of a comparison to the
3184 inverted comparison. As we cannot compute an expression
3185 operator using invert_tree_comparison we have to simulate
3186 that with expression code iteration. */
3187 (for cmp (tcc_comparison)
3188 icmp (inverted_tcc_comparison)
3189 ncmp (inverted_tcc_comparison_with_nans)
3190 /* Ideally we'd like to combine the following two patterns
3191 and handle some more cases by using
3192 (logical_inverted_value (cmp @0 @1))
3193 here but for that genmatch would need to "inline" that.
3194 For now implement what forward_propagate_comparison did. */
3196 (bit_not (cmp @0 @1))
3197 (if (VECTOR_TYPE_P (type)
3198 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3199 /* Comparison inversion may be impossible for trapping math,
3200 invert_tree_comparison will tell us. But we can't use
3201 a computed operator in the replacement tree thus we have
3202 to play the trick below. */
3203 (with { enum tree_code ic = invert_tree_comparison
3204 (cmp, HONOR_NANS (@0)); }
3210 (bit_xor (cmp @0 @1) integer_truep)
3211 (with { enum tree_code ic = invert_tree_comparison
3212 (cmp, HONOR_NANS (@0)); }
3218 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3219 ??? The transformation is valid for the other operators if overflow
3220 is undefined for the type, but performing it here badly interacts
3221 with the transformation in fold_cond_expr_with_comparison which
3222 attempts to synthetize ABS_EXPR. */
3224 (for sub (minus pointer_diff)
3226 (cmp (sub@2 @0 @1) integer_zerop)
3227 (if (single_use (@2))
3230 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3231 signed arithmetic case. That form is created by the compiler
3232 often enough for folding it to be of value. One example is in
3233 computing loop trip counts after Operator Strength Reduction. */
3234 (for cmp (simple_comparison)
3235 scmp (swapped_simple_comparison)
3237 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3238 /* Handle unfolded multiplication by zero. */
3239 (if (integer_zerop (@1))
3241 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3242 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3244 /* If @1 is negative we swap the sense of the comparison. */
3245 (if (tree_int_cst_sgn (@1) < 0)
3249 /* Simplify comparison of something with itself. For IEEE
3250 floating-point, we can only do some of these simplifications. */
3254 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3255 || ! HONOR_NANS (@0))
3256 { constant_boolean_node (true, type); }
3257 (if (cmp != EQ_EXPR)
3263 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3264 || ! HONOR_NANS (@0))
3265 { constant_boolean_node (false, type); })))
3266 (for cmp (unle unge uneq)
3269 { constant_boolean_node (true, type); }))
3270 (for cmp (unlt ungt)
3276 (if (!flag_trapping_math)
3277 { constant_boolean_node (false, type); }))
3279 /* Fold ~X op ~Y as Y op X. */
3280 (for cmp (simple_comparison)
3282 (cmp (bit_not@2 @0) (bit_not@3 @1))
3283 (if (single_use (@2) && single_use (@3))
3286 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3287 (for cmp (simple_comparison)
3288 scmp (swapped_simple_comparison)
3290 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3291 (if (single_use (@2)
3292 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3293 (scmp @0 (bit_not @1)))))
3295 (for cmp (simple_comparison)
3296 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3298 (cmp (convert@2 @0) (convert? @1))
3299 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3300 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3301 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3302 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3303 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3306 tree type1 = TREE_TYPE (@1);
3307 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3309 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3310 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3311 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3312 type1 = float_type_node;
3313 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3314 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3315 type1 = double_type_node;
3318 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3319 ? TREE_TYPE (@0) : type1);
3321 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3322 (cmp (convert:newtype @0) (convert:newtype @1))))))
3326 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3328 /* a CMP (-0) -> a CMP 0 */
3329 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3330 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3331 /* x != NaN is always true, other ops are always false. */
3332 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3333 && ! HONOR_SNANS (@1))
3334 { constant_boolean_node (cmp == NE_EXPR, type); })
3335 /* Fold comparisons against infinity. */
3336 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3337 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3340 REAL_VALUE_TYPE max;
3341 enum tree_code code = cmp;
3342 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3344 code = swap_tree_comparison (code);
3347 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3348 (if (code == GT_EXPR
3349 && !(HONOR_NANS (@0) && flag_trapping_math))
3350 { constant_boolean_node (false, type); })
3351 (if (code == LE_EXPR)
3352 /* x <= +Inf is always true, if we don't care about NaNs. */
3353 (if (! HONOR_NANS (@0))
3354 { constant_boolean_node (true, type); }
3355 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3356 an "invalid" exception. */
3357 (if (!flag_trapping_math)
3359 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3360 for == this introduces an exception for x a NaN. */
3361 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3363 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3365 (lt @0 { build_real (TREE_TYPE (@0), max); })
3366 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3367 /* x < +Inf is always equal to x <= DBL_MAX. */
3368 (if (code == LT_EXPR)
3369 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3371 (ge @0 { build_real (TREE_TYPE (@0), max); })
3372 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3373 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3374 an exception for x a NaN so use an unordered comparison. */
3375 (if (code == NE_EXPR)
3376 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3377 (if (! HONOR_NANS (@0))
3379 (ge @0 { build_real (TREE_TYPE (@0), max); })
3380 (le @0 { build_real (TREE_TYPE (@0), max); }))
3382 (unge @0 { build_real (TREE_TYPE (@0), max); })
3383 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3385 /* If this is a comparison of a real constant with a PLUS_EXPR
3386 or a MINUS_EXPR of a real constant, we can convert it into a
3387 comparison with a revised real constant as long as no overflow
3388 occurs when unsafe_math_optimizations are enabled. */
3389 (if (flag_unsafe_math_optimizations)
3390 (for op (plus minus)
3392 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3395 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3396 TREE_TYPE (@1), @2, @1);
3398 (if (tem && !TREE_OVERFLOW (tem))
3399 (cmp @0 { tem; }))))))
3401 /* Likewise, we can simplify a comparison of a real constant with
3402 a MINUS_EXPR whose first operand is also a real constant, i.e.
3403 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3404 floating-point types only if -fassociative-math is set. */
3405 (if (flag_associative_math)
3407 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3408 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3409 (if (tem && !TREE_OVERFLOW (tem))
3410 (cmp { tem; } @1)))))
3412 /* Fold comparisons against built-in math functions. */
3413 (if (flag_unsafe_math_optimizations
3414 && ! flag_errno_math)
3417 (cmp (sq @0) REAL_CST@1)
3419 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3421 /* sqrt(x) < y is always false, if y is negative. */
3422 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3423 { constant_boolean_node (false, type); })
3424 /* sqrt(x) > y is always true, if y is negative and we
3425 don't care about NaNs, i.e. negative values of x. */
3426 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3427 { constant_boolean_node (true, type); })
3428 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3429 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3430 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3432 /* sqrt(x) < 0 is always false. */
3433 (if (cmp == LT_EXPR)
3434 { constant_boolean_node (false, type); })
3435 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3436 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3437 { constant_boolean_node (true, type); })
3438 /* sqrt(x) <= 0 -> x == 0. */
3439 (if (cmp == LE_EXPR)
3441 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3442 == or !=. In the last case:
3444 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3446 if x is negative or NaN. Due to -funsafe-math-optimizations,
3447 the results for other x follow from natural arithmetic. */
3449 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3453 real_arithmetic (&c2, MULT_EXPR,
3454 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3455 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3457 (if (REAL_VALUE_ISINF (c2))
3458 /* sqrt(x) > y is x == +Inf, when y is very large. */
3459 (if (HONOR_INFINITIES (@0))
3460 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3461 { constant_boolean_node (false, type); })
3462 /* sqrt(x) > c is the same as x > c*c. */
3463 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3464 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3468 real_arithmetic (&c2, MULT_EXPR,
3469 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3470 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3472 (if (REAL_VALUE_ISINF (c2))
3474 /* sqrt(x) < y is always true, when y is a very large
3475 value and we don't care about NaNs or Infinities. */
3476 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3477 { constant_boolean_node (true, type); })
3478 /* sqrt(x) < y is x != +Inf when y is very large and we
3479 don't care about NaNs. */
3480 (if (! HONOR_NANS (@0))
3481 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3482 /* sqrt(x) < y is x >= 0 when y is very large and we
3483 don't care about Infinities. */
3484 (if (! HONOR_INFINITIES (@0))
3485 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3486 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3489 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3490 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3491 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3492 (if (! HONOR_NANS (@0))
3493 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3494 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3497 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3498 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3499 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3501 (cmp (sq @0) (sq @1))
3502 (if (! HONOR_NANS (@0))
3505 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
3506 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3507 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
3509 (cmp (float@0 @1) (float @2))
3510 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3511 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3514 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3515 tree type1 = TREE_TYPE (@1);
3516 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3517 tree type2 = TREE_TYPE (@2);
3518 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3520 (if (fmt.can_represent_integral_type_p (type1)
3521 && fmt.can_represent_integral_type_p (type2))
3522 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3523 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3524 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3525 && type1_signed_p >= type2_signed_p)
3526 (icmp @1 (convert @2))
3527 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3528 && type1_signed_p <= type2_signed_p)
3529 (icmp (convert:type2 @1) @2)
3530 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3531 && type1_signed_p == type2_signed_p)
3532 (icmp @1 @2))))))))))
3534 /* Optimize various special cases of (FTYPE) N CMP CST. */
3535 (for cmp (lt le eq ne ge gt)
3536 icmp (le le eq ne ge ge)
3538 (cmp (float @0) REAL_CST@1)
3539 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3540 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3543 tree itype = TREE_TYPE (@0);
3544 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3545 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3546 /* Be careful to preserve any potential exceptions due to
3547 NaNs. qNaNs are ok in == or != context.
3548 TODO: relax under -fno-trapping-math or
3549 -fno-signaling-nans. */
3551 = real_isnan (cst) && (cst->signalling
3552 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3554 /* TODO: allow non-fitting itype and SNaNs when
3555 -fno-trapping-math. */
3556 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
3559 signop isign = TYPE_SIGN (itype);
3560 REAL_VALUE_TYPE imin, imax;
3561 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3562 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3564 REAL_VALUE_TYPE icst;
3565 if (cmp == GT_EXPR || cmp == GE_EXPR)
3566 real_ceil (&icst, fmt, cst);
3567 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3568 real_floor (&icst, fmt, cst);
3570 real_trunc (&icst, fmt, cst);
3572 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3574 bool overflow_p = false;
3576 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3579 /* Optimize cases when CST is outside of ITYPE's range. */
3580 (if (real_compare (LT_EXPR, cst, &imin))
3581 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3583 (if (real_compare (GT_EXPR, cst, &imax))
3584 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3586 /* Remove cast if CST is an integer representable by ITYPE. */
3588 (cmp @0 { gcc_assert (!overflow_p);
3589 wide_int_to_tree (itype, icst_val); })
3591 /* When CST is fractional, optimize
3592 (FTYPE) N == CST -> 0
3593 (FTYPE) N != CST -> 1. */
3594 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3595 { constant_boolean_node (cmp == NE_EXPR, type); })
3596 /* Otherwise replace with sensible integer constant. */
3599 gcc_checking_assert (!overflow_p);
3601 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3603 /* Fold A /[ex] B CMP C to A CMP B * C. */
3606 (cmp (exact_div @0 @1) INTEGER_CST@2)
3607 (if (!integer_zerop (@1))
3608 (if (wi::to_wide (@2) == 0)
3610 (if (TREE_CODE (@1) == INTEGER_CST)
3613 wi::overflow_type ovf;
3614 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3615 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3618 { constant_boolean_node (cmp == NE_EXPR, type); }
3619 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3620 (for cmp (lt le gt ge)
3622 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3623 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3626 wi::overflow_type ovf;
3627 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3628 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3631 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3632 TYPE_SIGN (TREE_TYPE (@2)))
3633 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3634 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3636 /* Unordered tests if either argument is a NaN. */
3638 (bit_ior (unordered @0 @0) (unordered @1 @1))
3639 (if (types_match (@0, @1))
3642 (bit_and (ordered @0 @0) (ordered @1 @1))
3643 (if (types_match (@0, @1))
3646 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3649 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3652 /* Simple range test simplifications. */
3653 /* A < B || A >= B -> true. */
3654 (for test1 (lt le le le ne ge)
3655 test2 (ge gt ge ne eq ne)
3657 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3658 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3659 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3660 { constant_boolean_node (true, type); })))
3661 /* A < B && A >= B -> false. */
3662 (for test1 (lt lt lt le ne eq)
3663 test2 (ge gt eq gt eq gt)
3665 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3666 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3667 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3668 { constant_boolean_node (false, type); })))
3670 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3671 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3673 Note that comparisons
3674 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3675 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3676 will be canonicalized to above so there's no need to
3683 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3684 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3687 tree ty = TREE_TYPE (@0);
3688 unsigned prec = TYPE_PRECISION (ty);
3689 wide_int mask = wi::to_wide (@2, prec);
3690 wide_int rhs = wi::to_wide (@3, prec);
3691 signop sgn = TYPE_SIGN (ty);
3693 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3694 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3695 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3696 { build_zero_cst (ty); }))))))
3698 /* -A CMP -B -> B CMP A. */
3699 (for cmp (tcc_comparison)
3700 scmp (swapped_tcc_comparison)
3702 (cmp (negate @0) (negate @1))
3703 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3704 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3705 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3708 (cmp (negate @0) CONSTANT_CLASS_P@1)
3709 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3710 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3711 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3712 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3713 (if (tem && !TREE_OVERFLOW (tem))
3714 (scmp @0 { tem; }))))))
3716 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3719 (op (abs @0) zerop@1)
3722 /* From fold_sign_changed_comparison and fold_widened_comparison.
3723 FIXME: the lack of symmetry is disturbing. */
3724 (for cmp (simple_comparison)
3726 (cmp (convert@0 @00) (convert?@1 @10))
3727 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3728 /* Disable this optimization if we're casting a function pointer
3729 type on targets that require function pointer canonicalization. */
3730 && !(targetm.have_canonicalize_funcptr_for_compare ()
3731 && ((POINTER_TYPE_P (TREE_TYPE (@00))
3732 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3733 || (POINTER_TYPE_P (TREE_TYPE (@10))
3734 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
3736 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3737 && (TREE_CODE (@10) == INTEGER_CST
3739 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3742 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3743 /* ??? The special-casing of INTEGER_CST conversion was in the original
3744 code and here to avoid a spurious overflow flag on the resulting
3745 constant which fold_convert produces. */
3746 (if (TREE_CODE (@1) == INTEGER_CST)
3747 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3748 TREE_OVERFLOW (@1)); })
3749 (cmp @00 (convert @1)))
3751 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3752 /* If possible, express the comparison in the shorter mode. */
3753 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3754 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3755 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3756 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3757 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3758 || ((TYPE_PRECISION (TREE_TYPE (@00))
3759 >= TYPE_PRECISION (TREE_TYPE (@10)))
3760 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3761 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3762 || (TREE_CODE (@10) == INTEGER_CST
3763 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3764 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3765 (cmp @00 (convert @10))
3766 (if (TREE_CODE (@10) == INTEGER_CST
3767 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3768 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3771 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3772 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3773 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3774 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3776 (if (above || below)
3777 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3778 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3779 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3780 { constant_boolean_node (above ? true : false, type); }
3781 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3782 { constant_boolean_node (above ? false : true, type); }))))))))))))
3785 /* A local variable can never be pointed to by
3786 the default SSA name of an incoming parameter.
3787 SSA names are canonicalized to 2nd place. */
3789 (cmp addr@0 SSA_NAME@1)
3790 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3791 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3792 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3793 (if (TREE_CODE (base) == VAR_DECL
3794 && auto_var_in_fn_p (base, current_function_decl))
3795 (if (cmp == NE_EXPR)
3796 { constant_boolean_node (true, type); }
3797 { constant_boolean_node (false, type); }))))))
3799 /* Equality compare simplifications from fold_binary */
3802 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3803 Similarly for NE_EXPR. */
3805 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3806 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3807 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3808 { constant_boolean_node (cmp == NE_EXPR, type); }))
3810 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3812 (cmp (bit_xor @0 @1) integer_zerop)
3815 /* (X ^ Y) == Y becomes X == 0.
3816 Likewise (X ^ Y) == X becomes Y == 0. */
3818 (cmp:c (bit_xor:c @0 @1) @0)
3819 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3821 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3823 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3824 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3825 (cmp @0 (bit_xor @1 (convert @2)))))
3828 (cmp (convert? addr@0) integer_zerop)
3829 (if (tree_single_nonzero_warnv_p (@0, NULL))
3830 { constant_boolean_node (cmp == NE_EXPR, type); })))
3832 /* If we have (A & C) == C where C is a power of 2, convert this into
3833 (A & C) != 0. Similarly for NE_EXPR. */
3837 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3838 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3840 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3841 convert this into a shift followed by ANDing with D. */
3844 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3845 INTEGER_CST@2 integer_zerop)
3846 (if (integer_pow2p (@2))
3848 int shift = (wi::exact_log2 (wi::to_wide (@2))
3849 - wi::exact_log2 (wi::to_wide (@1)));
3853 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3855 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3858 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3859 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3863 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3864 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3865 && type_has_mode_precision_p (TREE_TYPE (@0))
3866 && element_precision (@2) >= element_precision (@0)
3867 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3868 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3869 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3871 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3872 this into a right shift or sign extension followed by ANDing with C. */
3875 (lt @0 integer_zerop)
3876 INTEGER_CST@1 integer_zerop)
3877 (if (integer_pow2p (@1)
3878 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3880 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3884 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3886 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3887 sign extension followed by AND with C will achieve the effect. */
3888 (bit_and (convert @0) @1)))))
3890 /* When the addresses are not directly of decls compare base and offset.
3891 This implements some remaining parts of fold_comparison address
3892 comparisons but still no complete part of it. Still it is good
3893 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3894 (for cmp (simple_comparison)
3896 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3899 poly_int64 off0, off1;
3900 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3901 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3902 if (base0 && TREE_CODE (base0) == MEM_REF)
3904 off0 += mem_ref_offset (base0).force_shwi ();
3905 base0 = TREE_OPERAND (base0, 0);
3907 if (base1 && TREE_CODE (base1) == MEM_REF)
3909 off1 += mem_ref_offset (base1).force_shwi ();
3910 base1 = TREE_OPERAND (base1, 0);
3913 (if (base0 && base1)
3917 /* Punt in GENERIC on variables with value expressions;
3918 the value expressions might point to fields/elements
3919 of other vars etc. */
3921 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3922 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3924 else if (decl_in_symtab_p (base0)
3925 && decl_in_symtab_p (base1))
3926 equal = symtab_node::get_create (base0)
3927 ->equal_address_to (symtab_node::get_create (base1));
3928 else if ((DECL_P (base0)
3929 || TREE_CODE (base0) == SSA_NAME
3930 || TREE_CODE (base0) == STRING_CST)
3932 || TREE_CODE (base1) == SSA_NAME
3933 || TREE_CODE (base1) == STRING_CST))
3934 equal = (base0 == base1);
3937 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
3938 off0.is_constant (&ioff0);
3939 off1.is_constant (&ioff1);
3940 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
3941 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
3942 || (TREE_CODE (base0) == STRING_CST
3943 && TREE_CODE (base1) == STRING_CST
3944 && ioff0 >= 0 && ioff1 >= 0
3945 && ioff0 < TREE_STRING_LENGTH (base0)
3946 && ioff1 < TREE_STRING_LENGTH (base1)
3947 /* This is a too conservative test that the STRING_CSTs
3948 will not end up being string-merged. */
3949 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
3950 TREE_STRING_POINTER (base1) + ioff1,
3951 MIN (TREE_STRING_LENGTH (base0) - ioff0,
3952 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
3954 else if (!DECL_P (base0) || !DECL_P (base1))
3956 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
3958 /* If this is a pointer comparison, ignore for now even
3959 valid equalities where one pointer is the offset zero
3960 of one object and the other to one past end of another one. */
3961 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
3963 /* Assume that automatic variables can't be adjacent to global
3965 else if (is_global_var (base0) != is_global_var (base1))
3969 tree sz0 = DECL_SIZE_UNIT (base0);
3970 tree sz1 = DECL_SIZE_UNIT (base1);
3971 /* If sizes are unknown, e.g. VLA or not representable,
3973 if (!tree_fits_poly_int64_p (sz0)
3974 || !tree_fits_poly_int64_p (sz1))
3978 poly_int64 size0 = tree_to_poly_int64 (sz0);
3979 poly_int64 size1 = tree_to_poly_int64 (sz1);
3980 /* If one offset is pointing (or could be) to the beginning
3981 of one object and the other is pointing to one past the
3982 last byte of the other object, punt. */
3983 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
3985 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
3987 /* If both offsets are the same, there are some cases
3988 we know that are ok. Either if we know they aren't
3989 zero, or if we know both sizes are no zero. */
3991 && known_eq (off0, off1)
3992 && (known_ne (off0, 0)
3993 || (known_ne (size0, 0) && known_ne (size1, 0))))
4000 && (cmp == EQ_EXPR || cmp == NE_EXPR
4001 /* If the offsets are equal we can ignore overflow. */
4002 || known_eq (off0, off1)
4003 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4004 /* Or if we compare using pointers to decls or strings. */
4005 || (POINTER_TYPE_P (TREE_TYPE (@2))
4006 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4008 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4009 { constant_boolean_node (known_eq (off0, off1), type); })
4010 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4011 { constant_boolean_node (known_ne (off0, off1), type); })
4012 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4013 { constant_boolean_node (known_lt (off0, off1), type); })
4014 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4015 { constant_boolean_node (known_le (off0, off1), type); })
4016 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4017 { constant_boolean_node (known_ge (off0, off1), type); })
4018 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4019 { constant_boolean_node (known_gt (off0, off1), type); }))
4022 (if (cmp == EQ_EXPR)
4023 { constant_boolean_node (false, type); })
4024 (if (cmp == NE_EXPR)
4025 { constant_boolean_node (true, type); })))))))))
4027 /* Simplify pointer equality compares using PTA. */
4031 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4032 && ptrs_compare_unequal (@0, @1))
4033 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4035 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4036 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4037 Disable the transform if either operand is pointer to function.
4038 This broke pr22051-2.c for arm where function pointer
4039 canonicalizaion is not wanted. */
4043 (cmp (convert @0) INTEGER_CST@1)
4044 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4045 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4046 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4047 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4048 && POINTER_TYPE_P (TREE_TYPE (@1))
4049 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4050 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4051 (cmp @0 (convert @1)))))
4053 /* Non-equality compare simplifications from fold_binary */
4054 (for cmp (lt gt le ge)
4055 /* Comparisons with the highest or lowest possible integer of
4056 the specified precision will have known values. */
4058 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4059 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4060 || POINTER_TYPE_P (TREE_TYPE (@1))
4061 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4062 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4065 tree cst = uniform_integer_cst_p (@1);
4066 tree arg1_type = TREE_TYPE (cst);
4067 unsigned int prec = TYPE_PRECISION (arg1_type);
4068 wide_int max = wi::max_value (arg1_type);
4069 wide_int signed_max = wi::max_value (prec, SIGNED);
4070 wide_int min = wi::min_value (arg1_type);
4073 (if (wi::to_wide (cst) == max)
4075 (if (cmp == GT_EXPR)
4076 { constant_boolean_node (false, type); })
4077 (if (cmp == GE_EXPR)
4079 (if (cmp == LE_EXPR)
4080 { constant_boolean_node (true, type); })
4081 (if (cmp == LT_EXPR)
4083 (if (wi::to_wide (cst) == min)
4085 (if (cmp == LT_EXPR)
4086 { constant_boolean_node (false, type); })
4087 (if (cmp == LE_EXPR)
4089 (if (cmp == GE_EXPR)
4090 { constant_boolean_node (true, type); })
4091 (if (cmp == GT_EXPR)
4093 (if (wi::to_wide (cst) == max - 1)
4095 (if (cmp == GT_EXPR)
4096 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4097 wide_int_to_tree (TREE_TYPE (cst),
4100 (if (cmp == LE_EXPR)
4101 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4102 wide_int_to_tree (TREE_TYPE (cst),
4105 (if (wi::to_wide (cst) == min + 1)
4107 (if (cmp == GE_EXPR)
4108 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4109 wide_int_to_tree (TREE_TYPE (cst),
4112 (if (cmp == LT_EXPR)
4113 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4114 wide_int_to_tree (TREE_TYPE (cst),
4117 (if (wi::to_wide (cst) == signed_max
4118 && TYPE_UNSIGNED (arg1_type)
4119 /* We will flip the signedness of the comparison operator
4120 associated with the mode of @1, so the sign bit is
4121 specified by this mode. Check that @1 is the signed
4122 max associated with this sign bit. */
4123 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4124 /* signed_type does not work on pointer types. */
4125 && INTEGRAL_TYPE_P (arg1_type))
4126 /* The following case also applies to X < signed_max+1
4127 and X >= signed_max+1 because previous transformations. */
4128 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4129 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4131 (if (cst == @1 && cmp == LE_EXPR)
4132 (ge (convert:st @0) { build_zero_cst (st); }))
4133 (if (cst == @1 && cmp == GT_EXPR)
4134 (lt (convert:st @0) { build_zero_cst (st); }))
4135 (if (cmp == LE_EXPR)
4136 (ge (view_convert:st @0) { build_zero_cst (st); }))
4137 (if (cmp == GT_EXPR)
4138 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4140 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4141 /* If the second operand is NaN, the result is constant. */
4144 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4145 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4146 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4147 ? false : true, type); })))
4149 /* bool_var != 0 becomes bool_var. */
4151 (ne @0 integer_zerop)
4152 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4153 && types_match (type, TREE_TYPE (@0)))
4155 /* bool_var == 1 becomes bool_var. */
4157 (eq @0 integer_onep)
4158 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4159 && types_match (type, TREE_TYPE (@0)))
4162 bool_var == 0 becomes !bool_var or
4163 bool_var != 1 becomes !bool_var
4164 here because that only is good in assignment context as long
4165 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4166 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4167 clearly less optimal and which we'll transform again in forwprop. */
4169 /* When one argument is a constant, overflow detection can be simplified.
4170 Currently restricted to single use so as not to interfere too much with
4171 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4172 A + CST CMP A -> A CMP' CST' */
4173 (for cmp (lt le ge gt)
4176 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4177 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4178 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4179 && wi::to_wide (@1) != 0
4181 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4182 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4183 wi::max_value (prec, UNSIGNED)
4184 - wi::to_wide (@1)); })))))
4186 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4187 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4188 expects the long form, so we restrict the transformation for now. */
4191 (cmp:c (minus@2 @0 @1) @0)
4192 (if (single_use (@2)
4193 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4194 && TYPE_UNSIGNED (TREE_TYPE (@0))
4195 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4198 /* Testing for overflow is unnecessary if we already know the result. */
4203 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4204 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4205 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4206 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4211 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4212 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4213 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4214 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4216 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4217 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4221 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4222 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4223 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4224 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4226 /* Simplification of math builtins. These rules must all be optimizations
4227 as well as IL simplifications. If there is a possibility that the new
4228 form could be a pessimization, the rule should go in the canonicalization
4229 section that follows this one.
4231 Rules can generally go in this section if they satisfy one of
4234 - the rule describes an identity
4236 - the rule replaces calls with something as simple as addition or
4239 - the rule contains unary calls only and simplifies the surrounding
4240 arithmetic. (The idea here is to exclude non-unary calls in which
4241 one operand is constant and in which the call is known to be cheap
4242 when the operand has that value.) */
4244 (if (flag_unsafe_math_optimizations)
4245 /* Simplify sqrt(x) * sqrt(x) -> x. */
4247 (mult (SQRT_ALL@1 @0) @1)
4248 (if (!HONOR_SNANS (type))
4251 (for op (plus minus)
4252 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4256 (rdiv (op @0 @2) @1)))
4258 (for cmp (lt le gt ge)
4259 neg_cmp (gt ge lt le)
4260 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4262 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4264 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4266 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4267 || (real_zerop (tem) && !real_zerop (@1))))
4269 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4271 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4272 (neg_cmp @0 { tem; })))))))
4274 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4275 (for root (SQRT CBRT)
4277 (mult (root:s @0) (root:s @1))
4278 (root (mult @0 @1))))
4280 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4281 (for exps (EXP EXP2 EXP10 POW10)
4283 (mult (exps:s @0) (exps:s @1))
4284 (exps (plus @0 @1))))
4286 /* Simplify a/root(b/c) into a*root(c/b). */
4287 (for root (SQRT CBRT)
4289 (rdiv @0 (root:s (rdiv:s @1 @2)))
4290 (mult @0 (root (rdiv @2 @1)))))
4292 /* Simplify x/expN(y) into x*expN(-y). */
4293 (for exps (EXP EXP2 EXP10 POW10)
4295 (rdiv @0 (exps:s @1))
4296 (mult @0 (exps (negate @1)))))
4298 (for logs (LOG LOG2 LOG10 LOG10)
4299 exps (EXP EXP2 EXP10 POW10)
4300 /* logN(expN(x)) -> x. */
4304 /* expN(logN(x)) -> x. */
4309 /* Optimize logN(func()) for various exponential functions. We
4310 want to determine the value "x" and the power "exponent" in
4311 order to transform logN(x**exponent) into exponent*logN(x). */
4312 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4313 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4316 (if (SCALAR_FLOAT_TYPE_P (type))
4322 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4323 x = build_real_truncate (type, dconst_e ());
4326 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4327 x = build_real (type, dconst2);
4331 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4333 REAL_VALUE_TYPE dconst10;
4334 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4335 x = build_real (type, dconst10);
4342 (mult (logs { x; }) @0)))))
4350 (if (SCALAR_FLOAT_TYPE_P (type))
4356 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4357 x = build_real (type, dconsthalf);
4360 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4361 x = build_real_truncate (type, dconst_third ());
4367 (mult { x; } (logs @0))))))
4369 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4370 (for logs (LOG LOG2 LOG10)
4374 (mult @1 (logs @0))))
4376 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4377 or if C is a positive power of 2,
4378 pow(C,x) -> exp2(log2(C)*x). */
4386 (pows REAL_CST@0 @1)
4387 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4388 && real_isfinite (TREE_REAL_CST_PTR (@0))
4389 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4390 the use_exp2 case until after vectorization. It seems actually
4391 beneficial for all constants to postpone this until later,
4392 because exp(log(C)*x), while faster, will have worse precision
4393 and if x folds into a constant too, that is unnecessary
4395 && canonicalize_math_after_vectorization_p ())
4397 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4398 bool use_exp2 = false;
4399 if (targetm.libc_has_function (function_c99_misc)
4400 && value->cl == rvc_normal)
4402 REAL_VALUE_TYPE frac_rvt = *value;
4403 SET_REAL_EXP (&frac_rvt, 1);
4404 if (real_equal (&frac_rvt, &dconst1))
4409 (if (optimize_pow_to_exp (@0, @1))
4410 (exps (mult (logs @0) @1)))
4411 (exp2s (mult (log2s @0) @1)))))))
4414 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4416 exps (EXP EXP2 EXP10 POW10)
4417 logs (LOG LOG2 LOG10 LOG10)
4419 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4420 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4421 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4422 (exps (plus (mult (logs @0) @1) @2)))))
4427 exps (EXP EXP2 EXP10 POW10)
4428 /* sqrt(expN(x)) -> expN(x*0.5). */
4431 (exps (mult @0 { build_real (type, dconsthalf); })))
4432 /* cbrt(expN(x)) -> expN(x/3). */
4435 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4436 /* pow(expN(x), y) -> expN(x*y). */
4439 (exps (mult @0 @1))))
4441 /* tan(atan(x)) -> x. */
4448 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4452 copysigns (COPYSIGN)
4457 REAL_VALUE_TYPE r_cst;
4458 build_sinatan_real (&r_cst, type);
4459 tree t_cst = build_real (type, r_cst);
4460 tree t_one = build_one_cst (type);
4462 (if (SCALAR_FLOAT_TYPE_P (type))
4463 (cond (lt (abs @0) { t_cst; })
4464 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4465 (copysigns { t_one; } @0))))))
4467 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4471 copysigns (COPYSIGN)
4476 REAL_VALUE_TYPE r_cst;
4477 build_sinatan_real (&r_cst, type);
4478 tree t_cst = build_real (type, r_cst);
4479 tree t_one = build_one_cst (type);
4480 tree t_zero = build_zero_cst (type);
4482 (if (SCALAR_FLOAT_TYPE_P (type))
4483 (cond (lt (abs @0) { t_cst; })
4484 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4485 (copysigns { t_zero; } @0))))))
4487 (if (!flag_errno_math)
4488 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4493 (sinhs (atanhs:s @0))
4494 (with { tree t_one = build_one_cst (type); }
4495 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4497 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4502 (coshs (atanhs:s @0))
4503 (with { tree t_one = build_one_cst (type); }
4504 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4506 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4508 (CABS (complex:C @0 real_zerop@1))
4511 /* trunc(trunc(x)) -> trunc(x), etc. */
4512 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4516 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4517 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4519 (fns integer_valued_real_p@0)
4522 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4524 (HYPOT:c @0 real_zerop@1)
4527 /* pow(1,x) -> 1. */
4529 (POW real_onep@0 @1)
4533 /* copysign(x,x) -> x. */
4534 (COPYSIGN_ALL @0 @0)
4538 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4539 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4542 (for scale (LDEXP SCALBN SCALBLN)
4543 /* ldexp(0, x) -> 0. */
4545 (scale real_zerop@0 @1)
4547 /* ldexp(x, 0) -> x. */
4549 (scale @0 integer_zerop@1)
4551 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4553 (scale REAL_CST@0 @1)
4554 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4557 /* Canonicalization of sequences of math builtins. These rules represent
4558 IL simplifications but are not necessarily optimizations.
4560 The sincos pass is responsible for picking "optimal" implementations
4561 of math builtins, which may be more complicated and can sometimes go
4562 the other way, e.g. converting pow into a sequence of sqrts.
4563 We only want to do these canonicalizations before the pass has run. */
4565 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4566 /* Simplify tan(x) * cos(x) -> sin(x). */
4568 (mult:c (TAN:s @0) (COS:s @0))
4571 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4573 (mult:c @0 (POW:s @0 REAL_CST@1))
4574 (if (!TREE_OVERFLOW (@1))
4575 (POW @0 (plus @1 { build_one_cst (type); }))))
4577 /* Simplify sin(x) / cos(x) -> tan(x). */
4579 (rdiv (SIN:s @0) (COS:s @0))
4582 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4584 (rdiv (COS:s @0) (SIN:s @0))
4585 (rdiv { build_one_cst (type); } (TAN @0)))
4587 /* Simplify sin(x) / tan(x) -> cos(x). */
4589 (rdiv (SIN:s @0) (TAN:s @0))
4590 (if (! HONOR_NANS (@0)
4591 && ! HONOR_INFINITIES (@0))
4594 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4596 (rdiv (TAN:s @0) (SIN:s @0))
4597 (if (! HONOR_NANS (@0)
4598 && ! HONOR_INFINITIES (@0))
4599 (rdiv { build_one_cst (type); } (COS @0))))
4601 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4603 (mult (POW:s @0 @1) (POW:s @0 @2))
4604 (POW @0 (plus @1 @2)))
4606 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4608 (mult (POW:s @0 @1) (POW:s @2 @1))
4609 (POW (mult @0 @2) @1))
4611 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4613 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4614 (POWI (mult @0 @2) @1))
4616 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4618 (rdiv (POW:s @0 REAL_CST@1) @0)
4619 (if (!TREE_OVERFLOW (@1))
4620 (POW @0 (minus @1 { build_one_cst (type); }))))
4622 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4624 (rdiv @0 (POW:s @1 @2))
4625 (mult @0 (POW @1 (negate @2))))
4630 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4633 (pows @0 { build_real (type, dconst_quarter ()); }))
4634 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4637 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4638 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4641 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4642 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4644 (cbrts (cbrts tree_expr_nonnegative_p@0))
4645 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4646 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4648 (sqrts (pows @0 @1))
4649 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4650 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4652 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4653 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4654 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4656 (pows (sqrts @0) @1)
4657 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4658 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4660 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4661 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4662 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4664 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4665 (pows @0 (mult @1 @2))))
4667 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4669 (CABS (complex @0 @0))
4670 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4672 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4675 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4677 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4682 (cexps compositional_complex@0)
4683 (if (targetm.libc_has_function (function_c99_math_complex))
4685 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4686 (mult @1 (imagpart @2)))))))
4688 (if (canonicalize_math_p ())
4689 /* floor(x) -> trunc(x) if x is nonnegative. */
4690 (for floors (FLOOR_ALL)
4693 (floors tree_expr_nonnegative_p@0)
4696 (match double_value_p
4698 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4699 (for froms (BUILT_IN_TRUNCL
4711 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4712 (if (optimize && canonicalize_math_p ())
4714 (froms (convert double_value_p@0))
4715 (convert (tos @0)))))
4717 (match float_value_p
4719 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4720 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4721 BUILT_IN_FLOORL BUILT_IN_FLOOR
4722 BUILT_IN_CEILL BUILT_IN_CEIL
4723 BUILT_IN_ROUNDL BUILT_IN_ROUND
4724 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4725 BUILT_IN_RINTL BUILT_IN_RINT)
4726 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4727 BUILT_IN_FLOORF BUILT_IN_FLOORF
4728 BUILT_IN_CEILF BUILT_IN_CEILF
4729 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4730 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4731 BUILT_IN_RINTF BUILT_IN_RINTF)
4732 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4734 (if (optimize && canonicalize_math_p ()
4735 && targetm.libc_has_function (function_c99_misc))
4737 (froms (convert float_value_p@0))
4738 (convert (tos @0)))))
4740 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4741 tos (XFLOOR XCEIL XROUND XRINT)
4742 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4743 (if (optimize && canonicalize_math_p ())
4745 (froms (convert double_value_p@0))
4748 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4749 XFLOOR XCEIL XROUND XRINT)
4750 tos (XFLOORF XCEILF XROUNDF XRINTF)
4751 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4753 (if (optimize && canonicalize_math_p ())
4755 (froms (convert float_value_p@0))
4758 (if (canonicalize_math_p ())
4759 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4760 (for floors (IFLOOR LFLOOR LLFLOOR)
4762 (floors tree_expr_nonnegative_p@0)
4765 (if (canonicalize_math_p ())
4766 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4767 (for fns (IFLOOR LFLOOR LLFLOOR
4769 IROUND LROUND LLROUND)
4771 (fns integer_valued_real_p@0)
4773 (if (!flag_errno_math)
4774 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4775 (for rints (IRINT LRINT LLRINT)
4777 (rints integer_valued_real_p@0)
4780 (if (canonicalize_math_p ())
4781 (for ifn (IFLOOR ICEIL IROUND IRINT)
4782 lfn (LFLOOR LCEIL LROUND LRINT)
4783 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4784 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4785 sizeof (int) == sizeof (long). */
4786 (if (TYPE_PRECISION (integer_type_node)
4787 == TYPE_PRECISION (long_integer_type_node))
4790 (lfn:long_integer_type_node @0)))
4791 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4792 sizeof (long long) == sizeof (long). */
4793 (if (TYPE_PRECISION (long_long_integer_type_node)
4794 == TYPE_PRECISION (long_integer_type_node))
4797 (lfn:long_integer_type_node @0)))))
4799 /* cproj(x) -> x if we're ignoring infinities. */
4802 (if (!HONOR_INFINITIES (type))
4805 /* If the real part is inf and the imag part is known to be
4806 nonnegative, return (inf + 0i). */
4808 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4809 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4810 { build_complex_inf (type, false); }))
4812 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4814 (CPROJ (complex @0 REAL_CST@1))
4815 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4816 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4822 (pows @0 REAL_CST@1)
4824 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4825 REAL_VALUE_TYPE tmp;
4828 /* pow(x,0) -> 1. */
4829 (if (real_equal (value, &dconst0))
4830 { build_real (type, dconst1); })
4831 /* pow(x,1) -> x. */
4832 (if (real_equal (value, &dconst1))
4834 /* pow(x,-1) -> 1/x. */
4835 (if (real_equal (value, &dconstm1))
4836 (rdiv { build_real (type, dconst1); } @0))
4837 /* pow(x,0.5) -> sqrt(x). */
4838 (if (flag_unsafe_math_optimizations
4839 && canonicalize_math_p ()
4840 && real_equal (value, &dconsthalf))
4842 /* pow(x,1/3) -> cbrt(x). */
4843 (if (flag_unsafe_math_optimizations
4844 && canonicalize_math_p ()
4845 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4846 real_equal (value, &tmp)))
4849 /* powi(1,x) -> 1. */
4851 (POWI real_onep@0 @1)
4855 (POWI @0 INTEGER_CST@1)
4857 /* powi(x,0) -> 1. */
4858 (if (wi::to_wide (@1) == 0)
4859 { build_real (type, dconst1); })
4860 /* powi(x,1) -> x. */
4861 (if (wi::to_wide (@1) == 1)
4863 /* powi(x,-1) -> 1/x. */
4864 (if (wi::to_wide (@1) == -1)
4865 (rdiv { build_real (type, dconst1); } @0))))
4867 /* Narrowing of arithmetic and logical operations.
4869 These are conceptually similar to the transformations performed for
4870 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4871 term we want to move all that code out of the front-ends into here. */
4873 /* If we have a narrowing conversion of an arithmetic operation where
4874 both operands are widening conversions from the same type as the outer
4875 narrowing conversion. Then convert the innermost operands to a suitable
4876 unsigned type (to avoid introducing undefined behavior), perform the
4877 operation and convert the result to the desired type. */
4878 (for op (plus minus)
4880 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4881 (if (INTEGRAL_TYPE_P (type)
4882 /* We check for type compatibility between @0 and @1 below,
4883 so there's no need to check that @1/@3 are integral types. */
4884 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4885 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4886 /* The precision of the type of each operand must match the
4887 precision of the mode of each operand, similarly for the
4889 && type_has_mode_precision_p (TREE_TYPE (@0))
4890 && type_has_mode_precision_p (TREE_TYPE (@1))
4891 && type_has_mode_precision_p (type)
4892 /* The inner conversion must be a widening conversion. */
4893 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4894 && types_match (@0, type)
4895 && (types_match (@0, @1)
4896 /* Or the second operand is const integer or converted const
4897 integer from valueize. */
4898 || TREE_CODE (@1) == INTEGER_CST))
4899 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4900 (op @0 (convert @1))
4901 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4902 (convert (op (convert:utype @0)
4903 (convert:utype @1))))))))
4905 /* This is another case of narrowing, specifically when there's an outer
4906 BIT_AND_EXPR which masks off bits outside the type of the innermost
4907 operands. Like the previous case we have to convert the operands
4908 to unsigned types to avoid introducing undefined behavior for the
4909 arithmetic operation. */
4910 (for op (minus plus)
4912 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4913 (if (INTEGRAL_TYPE_P (type)
4914 /* We check for type compatibility between @0 and @1 below,
4915 so there's no need to check that @1/@3 are integral types. */
4916 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4917 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4918 /* The precision of the type of each operand must match the
4919 precision of the mode of each operand, similarly for the
4921 && type_has_mode_precision_p (TREE_TYPE (@0))
4922 && type_has_mode_precision_p (TREE_TYPE (@1))
4923 && type_has_mode_precision_p (type)
4924 /* The inner conversion must be a widening conversion. */
4925 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4926 && types_match (@0, @1)
4927 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4928 <= TYPE_PRECISION (TREE_TYPE (@0)))
4929 && (wi::to_wide (@4)
4930 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4931 true, TYPE_PRECISION (type))) == 0)
4932 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4933 (with { tree ntype = TREE_TYPE (@0); }
4934 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4935 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4936 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4937 (convert:utype @4))))))))
4939 /* Transform (@0 < @1 and @0 < @2) to use min,
4940 (@0 > @1 and @0 > @2) to use max */
4941 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4942 op (lt le gt ge lt le gt ge )
4943 ext (min min max max max max min min )
4945 (logic (op:cs @0 @1) (op:cs @0 @2))
4946 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4947 && TREE_CODE (@0) != INTEGER_CST)
4948 (op @0 (ext @1 @2)))))
4951 /* signbit(x) -> 0 if x is nonnegative. */
4952 (SIGNBIT tree_expr_nonnegative_p@0)
4953 { integer_zero_node; })
4956 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4958 (if (!HONOR_SIGNED_ZEROS (@0))
4959 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4961 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4963 (for op (plus minus)
4966 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4967 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4968 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4969 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4970 && !TYPE_SATURATING (TREE_TYPE (@0)))
4971 (with { tree res = int_const_binop (rop, @2, @1); }
4972 (if (TREE_OVERFLOW (res)
4973 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4974 { constant_boolean_node (cmp == NE_EXPR, type); }
4975 (if (single_use (@3))
4976 (cmp @0 { TREE_OVERFLOW (res)
4977 ? drop_tree_overflow (res) : res; }))))))))
4978 (for cmp (lt le gt ge)
4979 (for op (plus minus)
4982 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4983 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4984 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4985 (with { tree res = int_const_binop (rop, @2, @1); }
4986 (if (TREE_OVERFLOW (res))
4988 fold_overflow_warning (("assuming signed overflow does not occur "
4989 "when simplifying conditional to constant"),
4990 WARN_STRICT_OVERFLOW_CONDITIONAL);
4991 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4992 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4993 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4994 TYPE_SIGN (TREE_TYPE (@1)))
4995 != (op == MINUS_EXPR);
4996 constant_boolean_node (less == ovf_high, type);
4998 (if (single_use (@3))
5001 fold_overflow_warning (("assuming signed overflow does not occur "
5002 "when changing X +- C1 cmp C2 to "
5004 WARN_STRICT_OVERFLOW_COMPARISON);
5006 (cmp @0 { res; })))))))))
5008 /* Canonicalizations of BIT_FIELD_REFs. */
5011 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5012 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5015 (BIT_FIELD_REF (view_convert @0) @1 @2)
5016 (BIT_FIELD_REF @0 @1 @2))
5019 (BIT_FIELD_REF @0 @1 integer_zerop)
5020 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5024 (BIT_FIELD_REF @0 @1 @2)
5026 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5027 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5029 (if (integer_zerop (@2))
5030 (view_convert (realpart @0)))
5031 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5032 (view_convert (imagpart @0)))))
5033 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5034 && INTEGRAL_TYPE_P (type)
5035 /* On GIMPLE this should only apply to register arguments. */
5036 && (! GIMPLE || is_gimple_reg (@0))
5037 /* A bit-field-ref that referenced the full argument can be stripped. */
5038 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5039 && integer_zerop (@2))
5040 /* Low-parts can be reduced to integral conversions.
5041 ??? The following doesn't work for PDP endian. */
5042 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5043 /* Don't even think about BITS_BIG_ENDIAN. */
5044 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5045 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5046 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5047 ? (TYPE_PRECISION (TREE_TYPE (@0))
5048 - TYPE_PRECISION (type))
5052 /* Simplify vector extracts. */
5055 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5056 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5057 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5058 || (VECTOR_TYPE_P (type)
5059 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5062 tree ctor = (TREE_CODE (@0) == SSA_NAME
5063 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5064 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5065 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5066 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5067 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5070 && (idx % width) == 0
5072 && known_le ((idx + n) / width,
5073 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5078 /* Constructor elements can be subvectors. */
5080 if (CONSTRUCTOR_NELTS (ctor) != 0)
5082 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5083 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5084 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5086 unsigned HOST_WIDE_INT elt, count, const_k;
5089 /* We keep an exact subset of the constructor elements. */
5090 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5091 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5092 { build_constructor (type, NULL); }
5094 (if (elt < CONSTRUCTOR_NELTS (ctor))
5095 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5096 { build_zero_cst (type); })
5098 vec<constructor_elt, va_gc> *vals;
5099 vec_alloc (vals, count);
5100 for (unsigned i = 0;
5101 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5102 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5103 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5104 build_constructor (type, vals);
5106 /* The bitfield references a single constructor element. */
5107 (if (k.is_constant (&const_k)
5108 && idx + n <= (idx / const_k + 1) * const_k)
5110 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5111 { build_zero_cst (type); })
5113 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5114 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5115 @1 { bitsize_int ((idx % const_k) * width); })))))))))
5117 /* Simplify a bit extraction from a bit insertion for the cases with
5118 the inserted element fully covering the extraction or the insertion
5119 not touching the extraction. */
5121 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5124 unsigned HOST_WIDE_INT isize;
5125 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5126 isize = TYPE_PRECISION (TREE_TYPE (@1));
5128 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5131 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5132 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5133 wi::to_wide (@ipos) + isize))
5134 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
5136 - wi::to_wide (@ipos)); }))
5137 (if (wi::geu_p (wi::to_wide (@ipos),
5138 wi::to_wide (@rpos) + wi::to_wide (@rsize))
5139 || wi::geu_p (wi::to_wide (@rpos),
5140 wi::to_wide (@ipos) + isize))
5141 (BIT_FIELD_REF @0 @rsize @rpos)))))
5143 (if (canonicalize_math_after_vectorization_p ())
5146 (fmas:c (negate @0) @1 @2)
5147 (IFN_FNMA @0 @1 @2))
5149 (fmas @0 @1 (negate @2))
5152 (fmas:c (negate @0) @1 (negate @2))
5153 (IFN_FNMS @0 @1 @2))
5155 (negate (fmas@3 @0 @1 @2))
5156 (if (single_use (@3))
5157 (IFN_FNMS @0 @1 @2))))
5160 (IFN_FMS:c (negate @0) @1 @2)
5161 (IFN_FNMS @0 @1 @2))
5163 (IFN_FMS @0 @1 (negate @2))
5166 (IFN_FMS:c (negate @0) @1 (negate @2))
5167 (IFN_FNMA @0 @1 @2))
5169 (negate (IFN_FMS@3 @0 @1 @2))
5170 (if (single_use (@3))
5171 (IFN_FNMA @0 @1 @2)))
5174 (IFN_FNMA:c (negate @0) @1 @2)
5177 (IFN_FNMA @0 @1 (negate @2))
5178 (IFN_FNMS @0 @1 @2))
5180 (IFN_FNMA:c (negate @0) @1 (negate @2))
5183 (negate (IFN_FNMA@3 @0 @1 @2))
5184 (if (single_use (@3))
5185 (IFN_FMS @0 @1 @2)))
5188 (IFN_FNMS:c (negate @0) @1 @2)
5191 (IFN_FNMS @0 @1 (negate @2))
5192 (IFN_FNMA @0 @1 @2))
5194 (IFN_FNMS:c (negate @0) @1 (negate @2))
5197 (negate (IFN_FNMS@3 @0 @1 @2))
5198 (if (single_use (@3))
5199 (IFN_FMA @0 @1 @2))))
5201 /* POPCOUNT simplifications. */
5202 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5203 BUILT_IN_POPCOUNTIMAX)
5204 /* popcount(X&1) is nop_expr(X&1). */
5207 (if (tree_nonzero_bits (@0) == 1)
5209 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
5211 (plus (popcount:s @0) (popcount:s @1))
5212 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5213 (popcount (bit_ior @0 @1))))
5214 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
5215 (for cmp (le eq ne gt)
5218 (cmp (popcount @0) integer_zerop)
5219 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5228 r = c ? a1 op a2 : b;
5230 if the target can do it in one go. This makes the operation conditional
5231 on c, so could drop potentially-trapping arithmetic, but that's a valid
5232 simplification if the result of the operation isn't needed.
5234 Avoid speculatively generating a stand-alone vector comparison
5235 on targets that might not support them. Any target implementing
5236 conditional internal functions must support the same comparisons
5237 inside and outside a VEC_COND_EXPR. */
5240 (for uncond_op (UNCOND_BINARY)
5241 cond_op (COND_BINARY)
5243 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5244 (with { tree op_type = TREE_TYPE (@4); }
5245 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5246 && element_precision (type) == element_precision (op_type))
5247 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5249 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5250 (with { tree op_type = TREE_TYPE (@4); }
5251 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5252 && element_precision (type) == element_precision (op_type))
5253 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5255 /* Same for ternary operations. */
5256 (for uncond_op (UNCOND_TERNARY)
5257 cond_op (COND_TERNARY)
5259 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5260 (with { tree op_type = TREE_TYPE (@5); }
5261 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5262 && element_precision (type) == element_precision (op_type))
5263 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5265 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5266 (with { tree op_type = TREE_TYPE (@5); }
5267 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5268 && element_precision (type) == element_precision (op_type))
5269 (view_convert (cond_op (bit_not @0) @2 @3 @4
5270 (view_convert:op_type @1)))))))
5273 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5274 "else" value of an IFN_COND_*. */
5275 (for cond_op (COND_BINARY)
5277 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5278 (with { tree op_type = TREE_TYPE (@3); }
5279 (if (element_precision (type) == element_precision (op_type))
5280 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5282 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5283 (with { tree op_type = TREE_TYPE (@5); }
5284 (if (inverse_conditions_p (@0, @2)
5285 && element_precision (type) == element_precision (op_type))
5286 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
5288 /* Same for ternary operations. */
5289 (for cond_op (COND_TERNARY)
5291 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5292 (with { tree op_type = TREE_TYPE (@4); }
5293 (if (element_precision (type) == element_precision (op_type))
5294 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5296 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5297 (with { tree op_type = TREE_TYPE (@6); }
5298 (if (inverse_conditions_p (@0, @2)
5299 && element_precision (type) == element_precision (op_type))
5300 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5302 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5305 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5306 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5308 If pointers are known not to wrap, B checks whether @1 bytes starting
5309 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5310 bytes. A is more efficiently tested as:
5312 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5314 The equivalent expression for B is given by replacing @1 with @1 - 1:
5316 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5318 @0 and @2 can be swapped in both expressions without changing the result.
5320 The folds rely on sizetype's being unsigned (which is always true)
5321 and on its being the same width as the pointer (which we have to check).
5323 The fold replaces two pointer_plus expressions, two comparisons and
5324 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5325 the best case it's a saving of two operations. The A fold retains one
5326 of the original pointer_pluses, so is a win even if both pointer_pluses
5327 are used elsewhere. The B fold is a wash if both pointer_pluses are
5328 used elsewhere, since all we end up doing is replacing a comparison with
5329 a pointer_plus. We do still apply the fold under those circumstances
5330 though, in case applying it to other conditions eventually makes one of the
5331 pointer_pluses dead. */
5332 (for ior (truth_orif truth_or bit_ior)
5335 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5336 (cmp:cs (pointer_plus@4 @2 @1) @0))
5337 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5338 && TYPE_OVERFLOW_WRAPS (sizetype)
5339 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5340 /* Calculate the rhs constant. */
5341 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5342 offset_int rhs = off * 2; }
5343 /* Always fails for negative values. */
5344 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5345 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5346 pick a canonical order. This increases the chances of using the
5347 same pointer_plus in multiple checks. */
5348 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5349 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5350 (if (cmp == LT_EXPR)
5351 (gt (convert:sizetype
5352 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5353 { swap_p ? @0 : @2; }))
5355 (gt (convert:sizetype
5356 (pointer_diff:ssizetype
5357 (pointer_plus { swap_p ? @2 : @0; }
5358 { wide_int_to_tree (sizetype, off); })
5359 { swap_p ? @0 : @2; }))
5360 { rhs_tree; })))))))))
5362 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
5364 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
5365 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
5366 (with { int i = single_nonzero_element (@1); }
5368 (with { tree elt = vector_cst_elt (@1, i);
5369 tree elt_type = TREE_TYPE (elt);
5370 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
5371 tree size = bitsize_int (elt_bits);
5372 tree pos = bitsize_int (elt_bits * i); }
5375 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
5379 (vec_perm @0 @1 VECTOR_CST@2)
5382 tree op0 = @0, op1 = @1, op2 = @2;
5384 /* Build a vector of integers from the tree mask. */
5385 vec_perm_builder builder;
5386 if (!tree_to_vec_perm_builder (&builder, op2))
5389 /* Create a vec_perm_indices for the integer vector. */
5390 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
5391 bool single_arg = (op0 == op1);
5392 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
5394 (if (sel.series_p (0, 1, 0, 1))
5396 (if (sel.series_p (0, 1, nelts, 1))
5402 if (sel.all_from_input_p (0))
5404 else if (sel.all_from_input_p (1))
5407 sel.rotate_inputs (1);
5409 else if (known_ge (poly_uint64 (sel[0]), nelts))
5411 std::swap (op0, op1);
5412 sel.rotate_inputs (1);
5416 tree cop0 = op0, cop1 = op1;
5417 if (TREE_CODE (op0) == SSA_NAME
5418 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
5419 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5420 cop0 = gimple_assign_rhs1 (def);
5421 if (TREE_CODE (op1) == SSA_NAME
5422 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
5423 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5424 cop1 = gimple_assign_rhs1 (def);
5428 (if ((TREE_CODE (cop0) == VECTOR_CST
5429 || TREE_CODE (cop0) == CONSTRUCTOR)
5430 && (TREE_CODE (cop1) == VECTOR_CST
5431 || TREE_CODE (cop1) == CONSTRUCTOR)
5432 && (t = fold_vec_perm (type, cop0, cop1, sel)))
5436 bool changed = (op0 == op1 && !single_arg);
5437 tree ins = NULL_TREE;
5440 /* See if the permutation is performing a single element
5441 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
5442 in that case. But only if the vector mode is supported,
5443 otherwise this is invalid GIMPLE. */
5444 if (TYPE_MODE (type) != BLKmode
5445 && (TREE_CODE (cop0) == VECTOR_CST
5446 || TREE_CODE (cop0) == CONSTRUCTOR
5447 || TREE_CODE (cop1) == VECTOR_CST
5448 || TREE_CODE (cop1) == CONSTRUCTOR))
5450 if (sel.series_p (1, 1, nelts + 1, 1))
5452 /* After canonicalizing the first elt to come from the
5453 first vector we only can insert the first elt from
5454 the first vector. */
5456 if ((ins = fold_read_from_vector (cop0, 0)))
5461 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
5462 for (at = 0; at < encoded_nelts; ++at)
5463 if (maybe_ne (sel[at], at))
5465 if (at < encoded_nelts && sel.series_p (at + 1, 1, at + 1, 1))
5467 if (known_lt (at, nelts))
5468 ins = fold_read_from_vector (cop0, sel[at]);
5470 ins = fold_read_from_vector (cop1, sel[at] - nelts);
5475 /* Generate a canonical form of the selector. */
5476 if (!ins && sel.encoding () != builder)
5478 /* Some targets are deficient and fail to expand a single
5479 argument permutation while still allowing an equivalent
5480 2-argument version. */
5482 if (sel.ninputs () == 2
5483 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
5484 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5487 vec_perm_indices sel2 (builder, 2, nelts);
5488 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
5489 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
5491 /* Not directly supported with either encoding,
5492 so use the preferred form. */
5493 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5495 if (!operand_equal_p (op2, oldop2, 0))
5500 (bit_insert { op0; } { ins; }
5501 { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); })
5503 (vec_perm { op0; } { op1; } { op2; }))))))))))