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-2020 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
43 (define_operator_list tcc_comparison
44 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
45 (define_operator_list inverted_tcc_comparison
46 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
47 (define_operator_list inverted_tcc_comparison_with_nans
48 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list swapped_tcc_comparison
50 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
51 (define_operator_list simple_comparison lt le eq ne ge gt)
52 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
54 #include "cfn-operators.pd"
56 /* Define operand lists for math rounding functions {,i,l,ll}FN,
57 where the versions prefixed with "i" return an int, those prefixed with
58 "l" return a long and those prefixed with "ll" return a long long.
60 Also define operand lists:
62 X<FN>F for all float functions, in the order i, l, ll
63 X<FN> for all double functions, in the same order
64 X<FN>L for all long double functions, in the same order. */
65 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
66 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
69 (define_operator_list X##FN BUILT_IN_I##FN \
72 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
76 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
81 /* Binary operations and their associated IFN_COND_* function. */
82 (define_operator_list UNCOND_BINARY
84 mult trunc_div trunc_mod rdiv
86 bit_and bit_ior bit_xor
88 (define_operator_list COND_BINARY
89 IFN_COND_ADD IFN_COND_SUB
90 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
91 IFN_COND_MIN IFN_COND_MAX
92 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
93 IFN_COND_SHL IFN_COND_SHR)
95 /* Same for ternary operations. */
96 (define_operator_list UNCOND_TERNARY
97 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
98 (define_operator_list COND_TERNARY
99 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
101 /* With nop_convert? combine convert? and view_convert? in one pattern
102 plus conditionalize on tree_nop_conversion_p conversions. */
103 (match (nop_convert @0)
105 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
106 (match (nop_convert @0)
108 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
109 && known_eq (TYPE_VECTOR_SUBPARTS (type),
110 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
111 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@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 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
126 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
127 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
128 && !TYPE_UNSIGNED (TREE_TYPE (@0))
129 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
133 /* Simplifications of operations with one constant operand and
134 simplifications to constants or single values. */
136 (for op (plus pointer_plus minus bit_ior bit_xor)
138 (op @0 integer_zerop)
141 /* 0 +p index -> (type)index */
143 (pointer_plus integer_zerop @1)
144 (non_lvalue (convert @1)))
146 /* ptr - 0 -> (type)ptr */
148 (pointer_diff @0 integer_zerop)
151 /* See if ARG1 is zero and X + ARG1 reduces to X.
152 Likewise if the operands are reversed. */
154 (plus:c @0 real_zerop@1)
155 (if (fold_real_zero_addition_p (type, @1, 0))
158 /* See if ARG1 is zero and X - ARG1 reduces to X. */
160 (minus @0 real_zerop@1)
161 (if (fold_real_zero_addition_p (type, @1, 1))
164 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
165 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
166 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
167 if not -frounding-math. For sNaNs the first operation would raise
168 exceptions but turn the result into qNan, so the second operation
169 would not raise it. */
170 (for inner_op (plus minus)
171 (for outer_op (plus minus)
173 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
176 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
177 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
178 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
180 = ((outer_op == PLUS_EXPR)
181 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
182 (if (outer_plus && !inner_plus)
187 This is unsafe for certain floats even in non-IEEE formats.
188 In IEEE, it is unsafe because it does wrong for NaNs.
189 Also note that operand_equal_p is always false if an operand
193 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
194 { build_zero_cst (type); }))
196 (pointer_diff @@0 @0)
197 { build_zero_cst (type); })
200 (mult @0 integer_zerop@1)
203 /* Maybe fold x * 0 to 0. The expressions aren't the same
204 when x is NaN, since x * 0 is also NaN. Nor are they the
205 same in modes with signed zeros, since multiplying a
206 negative value by 0 gives -0, not +0. */
208 (mult @0 real_zerop@1)
209 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
212 /* In IEEE floating point, x*1 is not equivalent to x for snans.
213 Likewise for complex arithmetic with signed zeros. */
216 (if (!HONOR_SNANS (type)
217 && (!HONOR_SIGNED_ZEROS (type)
218 || !COMPLEX_FLOAT_TYPE_P (type)))
221 /* Transform x * -1.0 into -x. */
223 (mult @0 real_minus_onep)
224 (if (!HONOR_SNANS (type)
225 && (!HONOR_SIGNED_ZEROS (type)
226 || !COMPLEX_FLOAT_TYPE_P (type)))
229 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
231 (mult SSA_NAME@1 SSA_NAME@2)
232 (if (INTEGRAL_TYPE_P (type)
233 && get_nonzero_bits (@1) == 1
234 && get_nonzero_bits (@2) == 1)
237 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
238 unless the target has native support for the former but not the latter. */
240 (mult @0 VECTOR_CST@1)
241 (if (initializer_each_zero_or_onep (@1)
242 && !HONOR_SNANS (type)
243 && !HONOR_SIGNED_ZEROS (type))
244 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
246 && (!VECTOR_MODE_P (TYPE_MODE (type))
247 || (VECTOR_MODE_P (TYPE_MODE (itype))
248 && optab_handler (and_optab,
249 TYPE_MODE (itype)) != CODE_FOR_nothing)))
250 (view_convert (bit_and:itype (view_convert @0)
251 (ne @1 { build_zero_cst (type); })))))))
253 (for cmp (gt ge lt le)
254 outp (convert convert negate negate)
255 outn (negate negate convert convert)
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). */
258 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
259 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
261 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
262 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
263 && types_match (type, TREE_TYPE (@0)))
265 (if (types_match (type, float_type_node))
266 (BUILT_IN_COPYSIGNF @1 (outp @0)))
267 (if (types_match (type, double_type_node))
268 (BUILT_IN_COPYSIGN @1 (outp @0)))
269 (if (types_match (type, long_double_type_node))
270 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
271 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
272 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
273 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
274 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
276 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
277 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
278 && types_match (type, TREE_TYPE (@0)))
280 (if (types_match (type, float_type_node))
281 (BUILT_IN_COPYSIGNF @1 (outn @0)))
282 (if (types_match (type, double_type_node))
283 (BUILT_IN_COPYSIGN @1 (outn @0)))
284 (if (types_match (type, long_double_type_node))
285 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
287 /* Transform X * copysign (1.0, X) into abs(X). */
289 (mult:c @0 (COPYSIGN_ALL real_onep @0))
290 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
293 /* Transform X * copysign (1.0, -X) into -abs(X). */
295 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
296 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
299 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
301 (COPYSIGN_ALL REAL_CST@0 @1)
302 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
303 (COPYSIGN_ALL (negate @0) @1)))
305 /* X * 1, X / 1 -> X. */
306 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
311 /* (A / (1 << B)) -> (A >> B).
312 Only for unsigned A. For signed A, this would not preserve rounding
314 For example: (-1 / ( 1 << B)) != -1 >> B.
315 Also also widening conversions, like:
316 (A / (unsigned long long) (1U << B)) -> (A >> B)
318 (A / (unsigned long long) (1 << B)) -> (A >> B).
319 If the left shift is signed, it can be done only if the upper bits
320 of A starting from shift's type sign bit are zero, as
321 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
322 so it is valid only if A >> 31 is zero. */
324 (trunc_div @0 (convert? (lshift integer_onep@1 @2)))
325 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
326 && (!VECTOR_TYPE_P (type)
327 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
328 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
329 && (useless_type_conversion_p (type, TREE_TYPE (@1))
330 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
331 && (TYPE_UNSIGNED (TREE_TYPE (@1))
332 || (element_precision (type)
333 == element_precision (TREE_TYPE (@1)))
334 || (INTEGRAL_TYPE_P (type)
335 && (tree_nonzero_bits (@0)
336 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
338 element_precision (type))) == 0)))))
341 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
342 undefined behavior in constexpr evaluation, and assuming that the division
343 traps enables better optimizations than these anyway. */
344 (for div (trunc_div ceil_div floor_div round_div exact_div)
345 /* 0 / X is always zero. */
347 (div integer_zerop@0 @1)
348 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
349 (if (!integer_zerop (@1))
353 (div @0 integer_minus_onep@1)
354 (if (!TYPE_UNSIGNED (type))
359 /* But not for 0 / 0 so that we can get the proper warnings and errors.
360 And not for _Fract types where we can't build 1. */
361 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
362 { build_one_cst (type); }))
363 /* X / abs (X) is X < 0 ? -1 : 1. */
366 (if (INTEGRAL_TYPE_P (type)
367 && TYPE_OVERFLOW_UNDEFINED (type))
368 (cond (lt @0 { build_zero_cst (type); })
369 { build_minus_one_cst (type); } { build_one_cst (type); })))
372 (div:C @0 (negate @0))
373 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
374 && TYPE_OVERFLOW_UNDEFINED (type))
375 { build_minus_one_cst (type); })))
377 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
378 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
381 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
382 && TYPE_UNSIGNED (type))
385 /* Combine two successive divisions. Note that combining ceil_div
386 and floor_div is trickier and combining round_div even more so. */
387 (for div (trunc_div exact_div)
389 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
391 wi::overflow_type overflow;
392 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
393 TYPE_SIGN (type), &overflow);
395 (if (div == EXACT_DIV_EXPR
396 || optimize_successive_divisions_p (@2, @3))
398 (div @0 { wide_int_to_tree (type, mul); })
399 (if (TYPE_UNSIGNED (type)
400 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
401 { build_zero_cst (type); }))))))
403 /* Combine successive multiplications. Similar to above, but handling
404 overflow is different. */
406 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
408 wi::overflow_type overflow;
409 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
410 TYPE_SIGN (type), &overflow);
412 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
413 otherwise undefined overflow implies that @0 must be zero. */
414 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
415 (mult @0 { wide_int_to_tree (type, mul); }))))
417 /* Optimize A / A to 1.0 if we don't care about
418 NaNs or Infinities. */
421 (if (FLOAT_TYPE_P (type)
422 && ! HONOR_NANS (type)
423 && ! HONOR_INFINITIES (type))
424 { build_one_cst (type); }))
426 /* Optimize -A / A to -1.0 if we don't care about
427 NaNs or Infinities. */
429 (rdiv:C @0 (negate @0))
430 (if (FLOAT_TYPE_P (type)
431 && ! HONOR_NANS (type)
432 && ! HONOR_INFINITIES (type))
433 { build_minus_one_cst (type); }))
435 /* PR71078: x / abs(x) -> copysign (1.0, x) */
437 (rdiv:C (convert? @0) (convert? (abs @0)))
438 (if (SCALAR_FLOAT_TYPE_P (type)
439 && ! HONOR_NANS (type)
440 && ! HONOR_INFINITIES (type))
442 (if (types_match (type, float_type_node))
443 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
444 (if (types_match (type, double_type_node))
445 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
446 (if (types_match (type, long_double_type_node))
447 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
449 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
452 (if (!HONOR_SNANS (type))
455 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
457 (rdiv @0 real_minus_onep)
458 (if (!HONOR_SNANS (type))
461 (if (flag_reciprocal_math)
462 /* Convert (A/B)/C to A/(B*C). */
464 (rdiv (rdiv:s @0 @1) @2)
465 (rdiv @0 (mult @1 @2)))
467 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
469 (rdiv @0 (mult:s @1 REAL_CST@2))
471 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
473 (rdiv (mult @0 { tem; } ) @1))))
475 /* Convert A/(B/C) to (A/B)*C */
477 (rdiv @0 (rdiv:s @1 @2))
478 (mult (rdiv @0 @1) @2)))
480 /* Simplify x / (- y) to -x / y. */
482 (rdiv @0 (negate @1))
483 (rdiv (negate @0) @1))
485 (if (flag_unsafe_math_optimizations)
486 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
487 Since C / x may underflow to zero, do this only for unsafe math. */
488 (for op (lt le gt ge)
491 (op (rdiv REAL_CST@0 @1) real_zerop@2)
492 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
494 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
496 /* For C < 0, use the inverted operator. */
497 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
500 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
501 (for div (trunc_div ceil_div floor_div round_div exact_div)
503 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
504 (if (integer_pow2p (@2)
505 && tree_int_cst_sgn (@2) > 0
506 && tree_nop_conversion_p (type, TREE_TYPE (@0))
507 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
509 { build_int_cst (integer_type_node,
510 wi::exact_log2 (wi::to_wide (@2))); }))))
512 /* If ARG1 is a constant, we can convert this to a multiply by the
513 reciprocal. This does not have the same rounding properties,
514 so only do this if -freciprocal-math. We can actually
515 always safely do it if ARG1 is a power of two, but it's hard to
516 tell if it is or not in a portable manner. */
517 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
521 (if (flag_reciprocal_math
524 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
526 (mult @0 { tem; } )))
527 (if (cst != COMPLEX_CST)
528 (with { tree inverse = exact_inverse (type, @1); }
530 (mult @0 { inverse; } ))))))))
532 (for mod (ceil_mod floor_mod round_mod trunc_mod)
533 /* 0 % X is always zero. */
535 (mod integer_zerop@0 @1)
536 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
537 (if (!integer_zerop (@1))
539 /* X % 1 is always zero. */
541 (mod @0 integer_onep)
542 { build_zero_cst (type); })
543 /* X % -1 is zero. */
545 (mod @0 integer_minus_onep@1)
546 (if (!TYPE_UNSIGNED (type))
547 { build_zero_cst (type); }))
551 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
552 (if (!integer_zerop (@0))
553 { build_zero_cst (type); }))
554 /* (X % Y) % Y is just X % Y. */
556 (mod (mod@2 @0 @1) @1)
558 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
560 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
561 (if (ANY_INTEGRAL_TYPE_P (type)
562 && TYPE_OVERFLOW_UNDEFINED (type)
563 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
565 { build_zero_cst (type); }))
566 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
567 modulo and comparison, since it is simpler and equivalent. */
570 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
571 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
572 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
573 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
575 /* X % -C is the same as X % C. */
577 (trunc_mod @0 INTEGER_CST@1)
578 (if (TYPE_SIGN (type) == SIGNED
579 && !TREE_OVERFLOW (@1)
580 && wi::neg_p (wi::to_wide (@1))
581 && !TYPE_OVERFLOW_TRAPS (type)
582 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
583 && !sign_bit_p (@1, @1))
584 (trunc_mod @0 (negate @1))))
586 /* X % -Y is the same as X % Y. */
588 (trunc_mod @0 (convert? (negate @1)))
589 (if (INTEGRAL_TYPE_P (type)
590 && !TYPE_UNSIGNED (type)
591 && !TYPE_OVERFLOW_TRAPS (type)
592 && tree_nop_conversion_p (type, TREE_TYPE (@1))
593 /* Avoid this transformation if X might be INT_MIN or
594 Y might be -1, because we would then change valid
595 INT_MIN % -(-1) into invalid INT_MIN % -1. */
596 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
597 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
599 (trunc_mod @0 (convert @1))))
601 /* X - (X / Y) * Y is the same as X % Y. */
603 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
604 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
605 (convert (trunc_mod @0 @1))))
607 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
608 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
609 Also optimize A % (C << N) where C is a power of 2,
610 to A & ((C << N) - 1). */
611 (match (power_of_two_cand @1)
613 (match (power_of_two_cand @1)
614 (lshift INTEGER_CST@1 @2))
615 (for mod (trunc_mod floor_mod)
617 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
618 (if ((TYPE_UNSIGNED (type)
619 || tree_expr_nonnegative_p (@0))
620 && tree_nop_conversion_p (type, TREE_TYPE (@3))
621 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
622 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
624 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
626 (trunc_div (mult @0 integer_pow2p@1) @1)
627 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
628 (bit_and @0 { wide_int_to_tree
629 (type, wi::mask (TYPE_PRECISION (type)
630 - wi::exact_log2 (wi::to_wide (@1)),
631 false, TYPE_PRECISION (type))); })))
633 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
635 (mult (trunc_div @0 integer_pow2p@1) @1)
636 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
637 (bit_and @0 (negate @1))))
639 /* Simplify (t * 2) / 2) -> t. */
640 (for div (trunc_div ceil_div floor_div round_div exact_div)
642 (div (mult:c @0 @1) @1)
643 (if (ANY_INTEGRAL_TYPE_P (type)
644 && TYPE_OVERFLOW_UNDEFINED (type))
648 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
653 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
656 (pows (op @0) REAL_CST@1)
657 (with { HOST_WIDE_INT n; }
658 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
660 /* Likewise for powi. */
663 (pows (op @0) INTEGER_CST@1)
664 (if ((wi::to_wide (@1) & 1) == 0)
666 /* Strip negate and abs from both operands of hypot. */
674 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
675 (for copysigns (COPYSIGN_ALL)
677 (copysigns (op @0) @1)
680 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
685 /* Convert absu(x)*absu(x) -> x*x. */
687 (mult (absu@1 @0) @1)
688 (mult (convert@2 @0) @2))
690 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
694 (coss (copysigns @0 @1))
697 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
701 (pows (copysigns @0 @2) REAL_CST@1)
702 (with { HOST_WIDE_INT n; }
703 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
705 /* Likewise for powi. */
709 (pows (copysigns @0 @2) INTEGER_CST@1)
710 (if ((wi::to_wide (@1) & 1) == 0)
715 /* hypot(copysign(x, y), z) -> hypot(x, z). */
717 (hypots (copysigns @0 @1) @2)
719 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
721 (hypots @0 (copysigns @1 @2))
724 /* copysign(x, CST) -> [-]abs (x). */
725 (for copysigns (COPYSIGN_ALL)
727 (copysigns @0 REAL_CST@1)
728 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
732 /* copysign(copysign(x, y), z) -> copysign(x, z). */
733 (for copysigns (COPYSIGN_ALL)
735 (copysigns (copysigns @0 @1) @2)
738 /* copysign(x,y)*copysign(x,y) -> x*x. */
739 (for copysigns (COPYSIGN_ALL)
741 (mult (copysigns@2 @0 @1) @2)
744 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
745 (for ccoss (CCOS CCOSH)
750 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
751 (for ops (conj negate)
757 /* Fold (a * (1 << b)) into (a << b) */
759 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
760 (if (! FLOAT_TYPE_P (type)
761 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
764 /* Fold (1 << (C - x)) where C = precision(type) - 1
765 into ((1 << C) >> x). */
767 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
768 (if (INTEGRAL_TYPE_P (type)
769 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
771 (if (TYPE_UNSIGNED (type))
772 (rshift (lshift @0 @2) @3)
774 { tree utype = unsigned_type_for (type); }
775 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
777 /* Fold (C1/X)*C2 into (C1*C2)/X. */
779 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
780 (if (flag_associative_math
783 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
785 (rdiv { tem; } @1)))))
787 /* Simplify ~X & X as zero. */
789 (bit_and:c (convert? @0) (convert? (bit_not @0)))
790 { build_zero_cst (type); })
792 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
794 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
795 (if (TYPE_UNSIGNED (type))
796 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
798 (for bitop (bit_and bit_ior)
800 /* PR35691: Transform
801 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
802 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
804 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
805 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
806 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
807 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
808 (cmp (bit_ior @0 (convert @1)) @2)))
810 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
811 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
813 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
814 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
815 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
816 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
817 (cmp (bit_and @0 (convert @1)) @2))))
819 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
821 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
822 (minus (bit_xor @0 @1) @1))
824 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
825 (if (~wi::to_wide (@2) == wi::to_wide (@1))
826 (minus (bit_xor @0 @1) @1)))
828 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
830 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
831 (minus @1 (bit_xor @0 @1)))
833 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
834 (for op (bit_ior bit_xor plus)
836 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
839 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
840 (if (~wi::to_wide (@2) == wi::to_wide (@1))
843 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
845 (bit_ior:c (bit_xor:c @0 @1) @0)
848 /* (a & ~b) | (a ^ b) --> a ^ b */
850 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
853 /* (a & ~b) ^ ~a --> ~(a & b) */
855 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
856 (bit_not (bit_and @0 @1)))
858 /* (~a & b) ^ a --> (a | b) */
860 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
863 /* (a | b) & ~(a ^ b) --> a & b */
865 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
868 /* a | ~(a ^ b) --> a | ~b */
870 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
871 (bit_ior @0 (bit_not @1)))
873 /* (a | b) | (a &^ b) --> a | b */
874 (for op (bit_and bit_xor)
876 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
879 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
881 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
884 /* ~(~a & b) --> a | ~b */
886 (bit_not (bit_and:cs (bit_not @0) @1))
887 (bit_ior @0 (bit_not @1)))
889 /* ~(~a | b) --> a & ~b */
891 (bit_not (bit_ior:cs (bit_not @0) @1))
892 (bit_and @0 (bit_not @1)))
894 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
897 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
898 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
899 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
903 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
904 ((A & N) + B) & M -> (A + B) & M
905 Similarly if (N & M) == 0,
906 ((A | N) + B) & M -> (A + B) & M
907 and for - instead of + (or unary - instead of +)
908 and/or ^ instead of |.
909 If B is constant and (B & M) == 0, fold into A & M. */
911 (for bitop (bit_and bit_ior bit_xor)
913 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
916 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
917 @3, @4, @1, ERROR_MARK, NULL_TREE,
920 (convert (bit_and (op (convert:utype { pmop[0]; })
921 (convert:utype { pmop[1]; }))
922 (convert:utype @2))))))
924 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
927 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
928 NULL_TREE, NULL_TREE, @1, bitop, @3,
931 (convert (bit_and (op (convert:utype { pmop[0]; })
932 (convert:utype { pmop[1]; }))
933 (convert:utype @2)))))))
935 (bit_and (op:s @0 @1) INTEGER_CST@2)
938 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
939 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
940 NULL_TREE, NULL_TREE, pmop); }
942 (convert (bit_and (op (convert:utype { pmop[0]; })
943 (convert:utype { pmop[1]; }))
944 (convert:utype @2)))))))
945 (for bitop (bit_and bit_ior bit_xor)
947 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
950 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
951 bitop, @2, @3, NULL_TREE, ERROR_MARK,
952 NULL_TREE, NULL_TREE, pmop); }
954 (convert (bit_and (negate (convert:utype { pmop[0]; }))
955 (convert:utype @1)))))))
957 /* X % Y is smaller than Y. */
960 (cmp (trunc_mod @0 @1) @1)
961 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
962 { constant_boolean_node (cmp == LT_EXPR, type); })))
965 (cmp @1 (trunc_mod @0 @1))
966 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
967 { constant_boolean_node (cmp == GT_EXPR, type); })))
971 (bit_ior @0 integer_all_onesp@1)
976 (bit_ior @0 integer_zerop)
981 (bit_and @0 integer_zerop@1)
987 (for op (bit_ior bit_xor plus)
989 (op:c (convert? @0) (convert? (bit_not @0)))
990 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
995 { build_zero_cst (type); })
997 /* Canonicalize X ^ ~0 to ~X. */
999 (bit_xor @0 integer_all_onesp@1)
1004 (bit_and @0 integer_all_onesp)
1007 /* x & x -> x, x | x -> x */
1008 (for bitop (bit_and bit_ior)
1013 /* x & C -> x if we know that x & ~C == 0. */
1016 (bit_and SSA_NAME@0 INTEGER_CST@1)
1017 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1018 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1022 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1024 (bit_not (minus (bit_not @0) @1))
1027 (bit_not (plus:c (bit_not @0) @1))
1030 /* x + (x & 1) -> (x + 1) & ~1 */
1032 (plus:c @0 (bit_and:s @0 integer_onep@1))
1033 (bit_and (plus @0 @1) (bit_not @1)))
1035 /* x & ~(x & y) -> x & ~y */
1036 /* x | ~(x | y) -> x | ~y */
1037 (for bitop (bit_and bit_ior)
1039 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1040 (bitop @0 (bit_not @1))))
1042 /* (~x & y) | ~(x | y) -> ~x */
1044 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1047 /* (x | y) ^ (x | ~y) -> ~x */
1049 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1052 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1054 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1055 (bit_not (bit_xor @0 @1)))
1057 /* (~x | y) ^ (x ^ y) -> x | ~y */
1059 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1060 (bit_ior @0 (bit_not @1)))
1062 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1064 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1065 (bit_not (bit_and @0 @1)))
1067 /* (x | y) & ~x -> y & ~x */
1068 /* (x & y) | ~x -> y | ~x */
1069 (for bitop (bit_and bit_ior)
1070 rbitop (bit_ior bit_and)
1072 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1075 /* (x & y) ^ (x | y) -> x ^ y */
1077 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1080 /* (x ^ y) ^ (x | y) -> x & y */
1082 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1085 /* (x & y) + (x ^ y) -> x | y */
1086 /* (x & y) | (x ^ y) -> x | y */
1087 /* (x & y) ^ (x ^ y) -> x | y */
1088 (for op (plus bit_ior bit_xor)
1090 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1093 /* (x & y) + (x | y) -> x + y */
1095 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1098 /* (x + y) - (x | y) -> x & y */
1100 (minus (plus @0 @1) (bit_ior @0 @1))
1101 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1102 && !TYPE_SATURATING (type))
1105 /* (x + y) - (x & y) -> x | y */
1107 (minus (plus @0 @1) (bit_and @0 @1))
1108 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1109 && !TYPE_SATURATING (type))
1112 /* (x | y) - y -> (x & ~y) */
1114 (minus (bit_ior:cs @0 @1) @1)
1115 (bit_and @0 (bit_not @1)))
1117 /* (x | y) - (x ^ y) -> x & y */
1119 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1122 /* (x | y) - (x & y) -> x ^ y */
1124 (minus (bit_ior @0 @1) (bit_and @0 @1))
1127 /* (x | y) & ~(x & y) -> x ^ y */
1129 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1132 /* (x | y) & (~x ^ y) -> x & y */
1134 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1137 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1139 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1140 (bit_not (bit_xor @0 @1)))
1142 /* (~x | y) ^ (x | ~y) -> x ^ y */
1144 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1147 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1149 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1150 (nop_convert2? (bit_ior @0 @1))))
1152 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1153 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1154 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1155 && !TYPE_SATURATING (TREE_TYPE (@2)))
1156 (bit_not (convert (bit_xor @0 @1)))))
1158 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1160 (nop_convert3? (bit_ior @0 @1)))
1161 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1162 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1163 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1164 && !TYPE_SATURATING (TREE_TYPE (@2)))
1165 (bit_not (convert (bit_xor @0 @1)))))
1167 (minus (nop_convert1? (bit_and @0 @1))
1168 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1170 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1171 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1172 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1173 && !TYPE_SATURATING (TREE_TYPE (@2)))
1174 (bit_not (convert (bit_xor @0 @1)))))
1176 /* ~x & ~y -> ~(x | y)
1177 ~x | ~y -> ~(x & y) */
1178 (for op (bit_and bit_ior)
1179 rop (bit_ior bit_and)
1181 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1182 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1183 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1184 (bit_not (rop (convert @0) (convert @1))))))
1186 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1187 with a constant, and the two constants have no bits in common,
1188 we should treat this as a BIT_IOR_EXPR since this may produce more
1190 (for op (bit_xor plus)
1192 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1193 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1194 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1195 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1196 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1197 (bit_ior (convert @4) (convert @5)))))
1199 /* (X | Y) ^ X -> Y & ~ X*/
1201 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1202 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1203 (convert (bit_and @1 (bit_not @0)))))
1205 /* Convert ~X ^ ~Y to X ^ Y. */
1207 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1208 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1209 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1210 (bit_xor (convert @0) (convert @1))))
1212 /* Convert ~X ^ C to X ^ ~C. */
1214 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1215 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1216 (bit_xor (convert @0) (bit_not @1))))
1218 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1219 (for opo (bit_and bit_xor)
1220 opi (bit_xor bit_and)
1222 (opo:c (opi:cs @0 @1) @1)
1223 (bit_and (bit_not @0) @1)))
1225 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1226 operands are another bit-wise operation with a common input. If so,
1227 distribute the bit operations to save an operation and possibly two if
1228 constants are involved. For example, convert
1229 (A | B) & (A | C) into A | (B & C)
1230 Further simplification will occur if B and C are constants. */
1231 (for op (bit_and bit_ior bit_xor)
1232 rop (bit_ior bit_and bit_and)
1234 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1235 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1236 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1237 (rop (convert @0) (op (convert @1) (convert @2))))))
1239 /* Some simple reassociation for bit operations, also handled in reassoc. */
1240 /* (X & Y) & Y -> X & Y
1241 (X | Y) | Y -> X | Y */
1242 (for op (bit_and bit_ior)
1244 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1246 /* (X ^ Y) ^ Y -> X */
1248 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1250 /* (X & Y) & (X & Z) -> (X & Y) & Z
1251 (X | Y) | (X | Z) -> (X | Y) | Z */
1252 (for op (bit_and bit_ior)
1254 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1255 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1256 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1257 (if (single_use (@5) && single_use (@6))
1258 (op @3 (convert @2))
1259 (if (single_use (@3) && single_use (@4))
1260 (op (convert @1) @5))))))
1261 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1263 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1264 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1265 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1266 (bit_xor (convert @1) (convert @2))))
1268 /* Convert abs (abs (X)) into abs (X).
1269 also absu (absu (X)) into absu (X). */
1275 (absu (convert@2 (absu@1 @0)))
1276 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1279 /* Convert abs[u] (-X) -> abs[u] (X). */
1288 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1290 (abs tree_expr_nonnegative_p@0)
1294 (absu tree_expr_nonnegative_p@0)
1297 /* A few cases of fold-const.c negate_expr_p predicate. */
1298 (match negate_expr_p
1300 (if ((INTEGRAL_TYPE_P (type)
1301 && TYPE_UNSIGNED (type))
1302 || (!TYPE_OVERFLOW_SANITIZED (type)
1303 && may_negate_without_overflow_p (t)))))
1304 (match negate_expr_p
1306 (match negate_expr_p
1308 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1309 (match negate_expr_p
1311 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1312 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1314 (match negate_expr_p
1316 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1317 (match negate_expr_p
1319 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1320 || (FLOAT_TYPE_P (type)
1321 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1322 && !HONOR_SIGNED_ZEROS (type)))))
1324 /* (-A) * (-B) -> A * B */
1326 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1327 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1328 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1329 (mult (convert @0) (convert (negate @1)))))
1331 /* -(A + B) -> (-B) - A. */
1333 (negate (plus:c @0 negate_expr_p@1))
1334 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1335 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1336 (minus (negate @1) @0)))
1338 /* -(A - B) -> B - A. */
1340 (negate (minus @0 @1))
1341 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1342 || (FLOAT_TYPE_P (type)
1343 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1344 && !HONOR_SIGNED_ZEROS (type)))
1347 (negate (pointer_diff @0 @1))
1348 (if (TYPE_OVERFLOW_UNDEFINED (type))
1349 (pointer_diff @1 @0)))
1351 /* A - B -> A + (-B) if B is easily negatable. */
1353 (minus @0 negate_expr_p@1)
1354 (if (!FIXED_POINT_TYPE_P (type))
1355 (plus @0 (negate @1))))
1357 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1359 For bitwise binary operations apply operand conversions to the
1360 binary operation result instead of to the operands. This allows
1361 to combine successive conversions and bitwise binary operations.
1362 We combine the above two cases by using a conditional convert. */
1363 (for bitop (bit_and bit_ior bit_xor)
1365 (bitop (convert@2 @0) (convert?@3 @1))
1366 (if (((TREE_CODE (@1) == INTEGER_CST
1367 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1368 && int_fits_type_p (@1, TREE_TYPE (@0)))
1369 || types_match (@0, @1))
1370 /* ??? This transform conflicts with fold-const.c doing
1371 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1372 constants (if x has signed type, the sign bit cannot be set
1373 in c). This folds extension into the BIT_AND_EXPR.
1374 Restrict it to GIMPLE to avoid endless recursions. */
1375 && (bitop != BIT_AND_EXPR || GIMPLE)
1376 && (/* That's a good idea if the conversion widens the operand, thus
1377 after hoisting the conversion the operation will be narrower. */
1378 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1379 /* It's also a good idea if the conversion is to a non-integer
1381 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1382 /* Or if the precision of TO is not the same as the precision
1384 || !type_has_mode_precision_p (type)
1385 /* In GIMPLE, getting rid of 2 conversions for one new results
1388 && TREE_CODE (@1) != INTEGER_CST
1389 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1391 && single_use (@3))))
1392 (convert (bitop @0 (convert @1)))))
1393 /* In GIMPLE, getting rid of 2 conversions for one new results
1396 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1398 && TREE_CODE (@1) != INTEGER_CST
1399 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1400 && types_match (type, @0))
1401 (bitop @0 (convert @1)))))
1403 (for bitop (bit_and bit_ior)
1404 rbitop (bit_ior bit_and)
1405 /* (x | y) & x -> x */
1406 /* (x & y) | x -> x */
1408 (bitop:c (rbitop:c @0 @1) @0)
1410 /* (~x | y) & x -> x & y */
1411 /* (~x & y) | x -> x | y */
1413 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1416 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1418 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1419 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1421 /* Combine successive equal operations with constants. */
1422 (for bitop (bit_and bit_ior bit_xor)
1424 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1425 (if (!CONSTANT_CLASS_P (@0))
1426 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1427 folded to a constant. */
1428 (bitop @0 (bitop @1 @2))
1429 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1430 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1431 the values involved are such that the operation can't be decided at
1432 compile time. Try folding one of @0 or @1 with @2 to see whether
1433 that combination can be decided at compile time.
1435 Keep the existing form if both folds fail, to avoid endless
1437 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1439 (bitop @1 { cst1; })
1440 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1442 (bitop @0 { cst2; }))))))))
1444 /* Try simple folding for X op !X, and X op X with the help
1445 of the truth_valued_p and logical_inverted_value predicates. */
1446 (match truth_valued_p
1448 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1449 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1450 (match truth_valued_p
1452 (match truth_valued_p
1455 (match (logical_inverted_value @0)
1457 (match (logical_inverted_value @0)
1458 (bit_not truth_valued_p@0))
1459 (match (logical_inverted_value @0)
1460 (eq @0 integer_zerop))
1461 (match (logical_inverted_value @0)
1462 (ne truth_valued_p@0 integer_truep))
1463 (match (logical_inverted_value @0)
1464 (bit_xor truth_valued_p@0 integer_truep))
1468 (bit_and:c @0 (logical_inverted_value @0))
1469 { build_zero_cst (type); })
1470 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1471 (for op (bit_ior bit_xor)
1473 (op:c truth_valued_p@0 (logical_inverted_value @0))
1474 { constant_boolean_node (true, type); }))
1475 /* X ==/!= !X is false/true. */
1478 (op:c truth_valued_p@0 (logical_inverted_value @0))
1479 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1483 (bit_not (bit_not @0))
1486 /* Convert ~ (-A) to A - 1. */
1488 (bit_not (convert? (negate @0)))
1489 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1490 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1491 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1493 /* Convert - (~A) to A + 1. */
1495 (negate (nop_convert? (bit_not @0)))
1496 (plus (view_convert @0) { build_each_one_cst (type); }))
1498 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1500 (bit_not (convert? (minus @0 integer_each_onep)))
1501 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1502 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1503 (convert (negate @0))))
1505 (bit_not (convert? (plus @0 integer_all_onesp)))
1506 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1507 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1508 (convert (negate @0))))
1510 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1512 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1513 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1514 (convert (bit_xor @0 (bit_not @1)))))
1516 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1517 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1518 (convert (bit_xor @0 @1))))
1520 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1522 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1523 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1524 (bit_not (bit_xor (view_convert @0) @1))))
1526 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1528 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1529 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1531 /* Fold A - (A & B) into ~B & A. */
1533 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1534 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1535 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1536 (convert (bit_and (bit_not @1) @0))))
1538 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1539 (for cmp (gt lt ge le)
1541 (mult (convert (cmp @0 @1)) @2)
1542 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1543 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1545 /* For integral types with undefined overflow and C != 0 fold
1546 x * C EQ/NE y * C into x EQ/NE y. */
1549 (cmp (mult:c @0 @1) (mult:c @2 @1))
1550 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1551 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1552 && tree_expr_nonzero_p (@1))
1555 /* For integral types with wrapping overflow and C odd fold
1556 x * C EQ/NE y * C into x EQ/NE y. */
1559 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1560 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1561 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1562 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1565 /* For integral types with undefined overflow and C != 0 fold
1566 x * C RELOP y * C into:
1568 x RELOP y for nonnegative C
1569 y RELOP x for negative C */
1570 (for cmp (lt gt le ge)
1572 (cmp (mult:c @0 @1) (mult:c @2 @1))
1573 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1574 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1575 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1577 (if (TREE_CODE (@1) == INTEGER_CST
1578 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1581 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1585 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1586 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1587 && TYPE_UNSIGNED (TREE_TYPE (@0))
1588 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1589 && (wi::to_wide (@2)
1590 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1591 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1592 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1594 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1595 (for cmp (simple_comparison)
1597 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1598 (if (element_precision (@3) >= element_precision (@0)
1599 && types_match (@0, @1))
1600 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1601 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1603 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1606 tree utype = unsigned_type_for (TREE_TYPE (@0));
1608 (cmp (convert:utype @1) (convert:utype @0)))))
1609 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1610 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1614 tree utype = unsigned_type_for (TREE_TYPE (@0));
1616 (cmp (convert:utype @0) (convert:utype @1)))))))))
1618 /* X / C1 op C2 into a simple range test. */
1619 (for cmp (simple_comparison)
1621 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1622 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1623 && integer_nonzerop (@1)
1624 && !TREE_OVERFLOW (@1)
1625 && !TREE_OVERFLOW (@2))
1626 (with { tree lo, hi; bool neg_overflow;
1627 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1630 (if (code == LT_EXPR || code == GE_EXPR)
1631 (if (TREE_OVERFLOW (lo))
1632 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1633 (if (code == LT_EXPR)
1636 (if (code == LE_EXPR || code == GT_EXPR)
1637 (if (TREE_OVERFLOW (hi))
1638 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1639 (if (code == LE_EXPR)
1643 { build_int_cst (type, code == NE_EXPR); })
1644 (if (code == EQ_EXPR && !hi)
1646 (if (code == EQ_EXPR && !lo)
1648 (if (code == NE_EXPR && !hi)
1650 (if (code == NE_EXPR && !lo)
1653 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1657 tree etype = range_check_type (TREE_TYPE (@0));
1660 hi = fold_convert (etype, hi);
1661 lo = fold_convert (etype, lo);
1662 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1665 (if (etype && hi && !TREE_OVERFLOW (hi))
1666 (if (code == EQ_EXPR)
1667 (le (minus (convert:etype @0) { lo; }) { hi; })
1668 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1670 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1671 (for op (lt le ge gt)
1673 (op (plus:c @0 @2) (plus:c @1 @2))
1674 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1675 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1677 /* For equality and subtraction, this is also true with wrapping overflow. */
1678 (for op (eq ne minus)
1680 (op (plus:c @0 @2) (plus:c @1 @2))
1681 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1682 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1683 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1686 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1687 (for op (lt le ge gt)
1689 (op (minus @0 @2) (minus @1 @2))
1690 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1691 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1693 /* For equality and subtraction, this is also true with wrapping overflow. */
1694 (for op (eq ne minus)
1696 (op (minus @0 @2) (minus @1 @2))
1697 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1698 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1699 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1701 /* And for pointers... */
1702 (for op (simple_comparison)
1704 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1705 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1708 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1709 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1710 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1711 (pointer_diff @0 @1)))
1713 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1714 (for op (lt le ge gt)
1716 (op (minus @2 @0) (minus @2 @1))
1717 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1718 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1720 /* For equality and subtraction, this is also true with wrapping overflow. */
1721 (for op (eq ne minus)
1723 (op (minus @2 @0) (minus @2 @1))
1724 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1725 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1726 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1728 /* And for pointers... */
1729 (for op (simple_comparison)
1731 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1732 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1735 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1736 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1737 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1738 (pointer_diff @1 @0)))
1740 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1741 (for op (lt le gt ge)
1743 (op:c (plus:c@2 @0 @1) @1)
1744 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1745 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1746 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1747 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1748 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1749 /* For equality, this is also true with wrapping overflow. */
1752 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1753 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1754 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1755 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1756 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1757 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1758 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1759 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1761 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1762 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1763 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1764 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1765 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1767 /* X - Y < X is the same as Y > 0 when there is no overflow.
1768 For equality, this is also true with wrapping overflow. */
1769 (for op (simple_comparison)
1771 (op:c @0 (minus@2 @0 @1))
1772 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1773 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1774 || ((op == EQ_EXPR || op == NE_EXPR)
1775 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1776 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1777 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1780 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1781 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1785 (cmp (trunc_div @0 @1) integer_zerop)
1786 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1787 /* Complex ==/!= is allowed, but not </>=. */
1788 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1789 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1792 /* X == C - X can never be true if C is odd. */
1795 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1796 (if (TREE_INT_CST_LOW (@1) & 1)
1797 { constant_boolean_node (cmp == NE_EXPR, type); })))
1799 /* Arguments on which one can call get_nonzero_bits to get the bits
1801 (match with_possible_nonzero_bits
1803 (match with_possible_nonzero_bits
1805 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1806 /* Slightly extended version, do not make it recursive to keep it cheap. */
1807 (match (with_possible_nonzero_bits2 @0)
1808 with_possible_nonzero_bits@0)
1809 (match (with_possible_nonzero_bits2 @0)
1810 (bit_and:c with_possible_nonzero_bits@0 @2))
1812 /* Same for bits that are known to be set, but we do not have
1813 an equivalent to get_nonzero_bits yet. */
1814 (match (with_certain_nonzero_bits2 @0)
1816 (match (with_certain_nonzero_bits2 @0)
1817 (bit_ior @1 INTEGER_CST@0))
1819 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1822 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1823 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1824 { constant_boolean_node (cmp == NE_EXPR, type); })))
1826 /* ((X inner_op C0) outer_op C1)
1827 With X being a tree where value_range has reasoned certain bits to always be
1828 zero throughout its computed value range,
1829 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1830 where zero_mask has 1's for all bits that are sure to be 0 in
1832 if (inner_op == '^') C0 &= ~C1;
1833 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1834 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1836 (for inner_op (bit_ior bit_xor)
1837 outer_op (bit_xor bit_ior)
1840 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1844 wide_int zero_mask_not;
1848 if (TREE_CODE (@2) == SSA_NAME)
1849 zero_mask_not = get_nonzero_bits (@2);
1853 if (inner_op == BIT_XOR_EXPR)
1855 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1856 cst_emit = C0 | wi::to_wide (@1);
1860 C0 = wi::to_wide (@0);
1861 cst_emit = C0 ^ wi::to_wide (@1);
1864 (if (!fail && (C0 & zero_mask_not) == 0)
1865 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1866 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1867 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1869 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1871 (pointer_plus (pointer_plus:s @0 @1) @3)
1872 (pointer_plus @0 (plus @1 @3)))
1878 tem4 = (unsigned long) tem3;
1883 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1884 /* Conditionally look through a sign-changing conversion. */
1885 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1886 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1887 || (GENERIC && type == TREE_TYPE (@1))))
1890 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1891 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1895 tem = (sizetype) ptr;
1899 and produce the simpler and easier to analyze with respect to alignment
1900 ... = ptr & ~algn; */
1902 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1903 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1904 (bit_and @0 { algn; })))
1906 /* Try folding difference of addresses. */
1908 (minus (convert ADDR_EXPR@0) (convert @1))
1909 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1910 (with { poly_int64 diff; }
1911 (if (ptr_difference_const (@0, @1, &diff))
1912 { build_int_cst_type (type, diff); }))))
1914 (minus (convert @0) (convert ADDR_EXPR@1))
1915 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1916 (with { poly_int64 diff; }
1917 (if (ptr_difference_const (@0, @1, &diff))
1918 { build_int_cst_type (type, diff); }))))
1920 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1921 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1922 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1923 (with { poly_int64 diff; }
1924 (if (ptr_difference_const (@0, @1, &diff))
1925 { build_int_cst_type (type, diff); }))))
1927 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1928 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1929 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1930 (with { poly_int64 diff; }
1931 (if (ptr_difference_const (@0, @1, &diff))
1932 { build_int_cst_type (type, diff); }))))
1934 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
1936 (convert (pointer_diff @0 INTEGER_CST@1))
1937 (if (POINTER_TYPE_P (type))
1938 { build_fold_addr_expr_with_type
1939 (build2 (MEM_REF, char_type_node, @0,
1940 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
1943 /* If arg0 is derived from the address of an object or function, we may
1944 be able to fold this expression using the object or function's
1947 (bit_and (convert? @0) INTEGER_CST@1)
1948 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1949 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1953 unsigned HOST_WIDE_INT bitpos;
1954 get_pointer_alignment_1 (@0, &align, &bitpos);
1956 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1957 { wide_int_to_tree (type, (wi::to_wide (@1)
1958 & (bitpos / BITS_PER_UNIT))); }))))
1962 (if (INTEGRAL_TYPE_P (type)
1963 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
1967 (if (INTEGRAL_TYPE_P (type)
1968 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
1970 /* x > y && x != XXX_MIN --> x > y
1971 x > y && x == XXX_MIN --> false . */
1974 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
1976 (if (eqne == EQ_EXPR)
1977 { constant_boolean_node (false, type); })
1978 (if (eqne == NE_EXPR)
1982 /* x < y && x != XXX_MAX --> x < y
1983 x < y && x == XXX_MAX --> false. */
1986 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
1988 (if (eqne == EQ_EXPR)
1989 { constant_boolean_node (false, type); })
1990 (if (eqne == NE_EXPR)
1994 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
1996 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
1999 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2001 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2004 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2006 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2009 /* x <= y || x != XXX_MIN --> true. */
2011 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2012 { constant_boolean_node (true, type); })
2014 /* x <= y || x == XXX_MIN --> x <= y. */
2016 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2019 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2021 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2024 /* x >= y || x != XXX_MAX --> true
2025 x >= y || x == XXX_MAX --> x >= y. */
2028 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2030 (if (eqne == EQ_EXPR)
2032 (if (eqne == NE_EXPR)
2033 { constant_boolean_node (true, type); }))))
2035 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2036 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2039 (for code2 (eq ne lt gt le ge)
2041 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2044 int cmp = tree_int_cst_compare (@1, @2);
2048 case EQ_EXPR: val = (cmp == 0); break;
2049 case NE_EXPR: val = (cmp != 0); break;
2050 case LT_EXPR: val = (cmp < 0); break;
2051 case GT_EXPR: val = (cmp > 0); break;
2052 case LE_EXPR: val = (cmp <= 0); break;
2053 case GE_EXPR: val = (cmp >= 0); break;
2054 default: gcc_unreachable ();
2058 (if (code1 == EQ_EXPR && val) @3)
2059 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2060 (if (code1 == NE_EXPR && !val) @4))))))
2062 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2064 (for code1 (lt le gt ge)
2065 (for code2 (lt le gt ge)
2067 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2070 int cmp = tree_int_cst_compare (@1, @2);
2073 /* Choose the more restrictive of two < or <= comparisons. */
2074 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2075 && (code2 == LT_EXPR || code2 == LE_EXPR))
2076 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2079 /* Likewise chose the more restrictive of two > or >= comparisons. */
2080 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2081 && (code2 == GT_EXPR || code2 == GE_EXPR))
2082 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2085 /* Check for singleton ranges. */
2087 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2088 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2090 /* Check for disjoint ranges. */
2092 && (code1 == LT_EXPR || code1 == LE_EXPR)
2093 && (code2 == GT_EXPR || code2 == GE_EXPR))
2094 { constant_boolean_node (false, type); })
2096 && (code1 == GT_EXPR || code1 == GE_EXPR)
2097 && (code2 == LT_EXPR || code2 == LE_EXPR))
2098 { constant_boolean_node (false, type); })
2101 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2102 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2105 (for code2 (eq ne lt gt le ge)
2107 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2110 int cmp = tree_int_cst_compare (@1, @2);
2114 case EQ_EXPR: val = (cmp == 0); break;
2115 case NE_EXPR: val = (cmp != 0); break;
2116 case LT_EXPR: val = (cmp < 0); break;
2117 case GT_EXPR: val = (cmp > 0); break;
2118 case LE_EXPR: val = (cmp <= 0); break;
2119 case GE_EXPR: val = (cmp >= 0); break;
2120 default: gcc_unreachable ();
2124 (if (code1 == EQ_EXPR && val) @4)
2125 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2126 (if (code1 == NE_EXPR && !val) @3))))))
2128 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2130 (for code1 (lt le gt ge)
2131 (for code2 (lt le gt ge)
2133 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2136 int cmp = tree_int_cst_compare (@1, @2);
2139 /* Choose the more restrictive of two < or <= comparisons. */
2140 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2141 && (code2 == LT_EXPR || code2 == LE_EXPR))
2142 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2145 /* Likewise chose the more restrictive of two > or >= comparisons. */
2146 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2147 && (code2 == GT_EXPR || code2 == GE_EXPR))
2148 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2151 /* Check for singleton ranges. */
2153 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2154 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2156 /* Check for disjoint ranges. */
2158 && (code1 == LT_EXPR || code1 == LE_EXPR)
2159 && (code2 == GT_EXPR || code2 == GE_EXPR))
2160 { constant_boolean_node (true, type); })
2162 && (code1 == GT_EXPR || code1 == GE_EXPR)
2163 && (code2 == LT_EXPR || code2 == LE_EXPR))
2164 { constant_boolean_node (true, type); })
2167 /* We can't reassociate at all for saturating types. */
2168 (if (!TYPE_SATURATING (type))
2170 /* Contract negates. */
2171 /* A + (-B) -> A - B */
2173 (plus:c @0 (convert? (negate @1)))
2174 /* Apply STRIP_NOPS on the negate. */
2175 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2176 && !TYPE_OVERFLOW_SANITIZED (type))
2180 if (INTEGRAL_TYPE_P (type)
2181 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2182 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2184 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2185 /* A - (-B) -> A + B */
2187 (minus @0 (convert? (negate @1)))
2188 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2189 && !TYPE_OVERFLOW_SANITIZED (type))
2193 if (INTEGRAL_TYPE_P (type)
2194 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2195 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2197 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2199 Sign-extension is ok except for INT_MIN, which thankfully cannot
2200 happen without overflow. */
2202 (negate (convert (negate @1)))
2203 (if (INTEGRAL_TYPE_P (type)
2204 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2205 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2206 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2207 && !TYPE_OVERFLOW_SANITIZED (type)
2208 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2211 (negate (convert negate_expr_p@1))
2212 (if (SCALAR_FLOAT_TYPE_P (type)
2213 && ((DECIMAL_FLOAT_TYPE_P (type)
2214 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2215 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2216 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2217 (convert (negate @1))))
2219 (negate (nop_convert? (negate @1)))
2220 (if (!TYPE_OVERFLOW_SANITIZED (type)
2221 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2224 /* We can't reassociate floating-point unless -fassociative-math
2225 or fixed-point plus or minus because of saturation to +-Inf. */
2226 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2227 && !FIXED_POINT_TYPE_P (type))
2229 /* Match patterns that allow contracting a plus-minus pair
2230 irrespective of overflow issues. */
2231 /* (A +- B) - A -> +- B */
2232 /* (A +- B) -+ B -> A */
2233 /* A - (A +- B) -> -+ B */
2234 /* A +- (B -+ A) -> +- B */
2236 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2239 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2240 (if (!ANY_INTEGRAL_TYPE_P (type)
2241 || TYPE_OVERFLOW_WRAPS (type))
2242 (negate (view_convert @1))
2243 (view_convert (negate @1))))
2245 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2248 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2249 (if (!ANY_INTEGRAL_TYPE_P (type)
2250 || TYPE_OVERFLOW_WRAPS (type))
2251 (negate (view_convert @1))
2252 (view_convert (negate @1))))
2254 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2256 /* (A +- B) + (C - A) -> C +- B */
2257 /* (A + B) - (A - C) -> B + C */
2258 /* More cases are handled with comparisons. */
2260 (plus:c (plus:c @0 @1) (minus @2 @0))
2263 (plus:c (minus @0 @1) (minus @2 @0))
2266 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2267 (if (TYPE_OVERFLOW_UNDEFINED (type)
2268 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2269 (pointer_diff @2 @1)))
2271 (minus (plus:c @0 @1) (minus @0 @2))
2274 /* (A +- CST1) +- CST2 -> A + CST3
2275 Use view_convert because it is safe for vectors and equivalent for
2277 (for outer_op (plus minus)
2278 (for inner_op (plus minus)
2279 neg_inner_op (minus plus)
2281 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2283 /* If one of the types wraps, use that one. */
2284 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2285 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2286 forever if something doesn't simplify into a constant. */
2287 (if (!CONSTANT_CLASS_P (@0))
2288 (if (outer_op == PLUS_EXPR)
2289 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2290 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2291 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2292 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2293 (if (outer_op == PLUS_EXPR)
2294 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2295 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2296 /* If the constant operation overflows we cannot do the transform
2297 directly as we would introduce undefined overflow, for example
2298 with (a - 1) + INT_MIN. */
2299 (if (types_match (type, @0))
2300 (with { tree cst = const_binop (outer_op == inner_op
2301 ? PLUS_EXPR : MINUS_EXPR,
2303 (if (cst && !TREE_OVERFLOW (cst))
2304 (inner_op @0 { cst; } )
2305 /* X+INT_MAX+1 is X-INT_MIN. */
2306 (if (INTEGRAL_TYPE_P (type) && cst
2307 && wi::to_wide (cst) == wi::min_value (type))
2308 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2309 /* Last resort, use some unsigned type. */
2310 (with { tree utype = unsigned_type_for (type); }
2312 (view_convert (inner_op
2313 (view_convert:utype @0)
2315 { drop_tree_overflow (cst); }))))))))))))))
2317 /* (CST1 - A) +- CST2 -> CST3 - A */
2318 (for outer_op (plus minus)
2320 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2321 /* If one of the types wraps, use that one. */
2322 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2323 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2324 forever if something doesn't simplify into a constant. */
2325 (if (!CONSTANT_CLASS_P (@0))
2326 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2327 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2328 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2329 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2330 (if (types_match (type, @0))
2331 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2332 (if (cst && !TREE_OVERFLOW (cst))
2333 (minus { cst; } @0))))))))
2335 /* CST1 - (CST2 - A) -> CST3 + A
2336 Use view_convert because it is safe for vectors and equivalent for
2339 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2340 /* If one of the types wraps, use that one. */
2341 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2342 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2343 forever if something doesn't simplify into a constant. */
2344 (if (!CONSTANT_CLASS_P (@0))
2345 (plus (view_convert @0) (minus @1 (view_convert @2))))
2346 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2347 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2348 (view_convert (plus @0 (minus (view_convert @1) @2)))
2349 (if (types_match (type, @0))
2350 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2351 (if (cst && !TREE_OVERFLOW (cst))
2352 (plus { cst; } @0)))))))
2354 /* ((T)(A)) + CST -> (T)(A + CST) */
2357 (plus (convert SSA_NAME@0) INTEGER_CST@1)
2358 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2359 && TREE_CODE (type) == INTEGER_TYPE
2360 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2361 && int_fits_type_p (@1, TREE_TYPE (@0)))
2362 /* Perform binary operation inside the cast if the constant fits
2363 and (A + CST)'s range does not overflow. */
2366 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2367 max_ovf = wi::OVF_OVERFLOW;
2368 tree inner_type = TREE_TYPE (@0);
2371 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2372 TYPE_SIGN (inner_type));
2374 wide_int wmin0, wmax0;
2375 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2377 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2378 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2381 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2382 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2386 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2388 (for op (plus minus)
2390 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2391 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2392 && TREE_CODE (type) == INTEGER_TYPE
2393 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2394 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2395 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2396 && TYPE_OVERFLOW_WRAPS (type))
2397 (plus (convert @0) (op @2 (convert @1))))))
2400 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2401 to a simple value. */
2403 (for op (plus minus)
2405 (op (convert @0) (convert @1))
2406 (if (INTEGRAL_TYPE_P (type)
2407 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2408 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2409 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2410 && !TYPE_OVERFLOW_TRAPS (type)
2411 && !TYPE_OVERFLOW_SANITIZED (type))
2412 (convert (op! @0 @1)))))
2417 (plus:c (bit_not @0) @0)
2418 (if (!TYPE_OVERFLOW_TRAPS (type))
2419 { build_all_ones_cst (type); }))
2423 (plus (convert? (bit_not @0)) integer_each_onep)
2424 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2425 (negate (convert @0))))
2429 (minus (convert? (negate @0)) integer_each_onep)
2430 (if (!TYPE_OVERFLOW_TRAPS (type)
2431 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2432 (bit_not (convert @0))))
2436 (minus integer_all_onesp @0)
2439 /* (T)(P + A) - (T)P -> (T) A */
2441 (minus (convert (plus:c @@0 @1))
2443 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2444 /* For integer types, if A has a smaller type
2445 than T the result depends on the possible
2447 E.g. T=size_t, A=(unsigned)429497295, P>0.
2448 However, if an overflow in P + A would cause
2449 undefined behavior, we can assume that there
2451 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2452 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2455 (minus (convert (pointer_plus @@0 @1))
2457 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2458 /* For pointer types, if the conversion of A to the
2459 final type requires a sign- or zero-extension,
2460 then we have to punt - it is not defined which
2462 || (POINTER_TYPE_P (TREE_TYPE (@0))
2463 && TREE_CODE (@1) == INTEGER_CST
2464 && tree_int_cst_sign_bit (@1) == 0))
2467 (pointer_diff (pointer_plus @@0 @1) @0)
2468 /* The second argument of pointer_plus must be interpreted as signed, and
2469 thus sign-extended if necessary. */
2470 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2471 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2472 second arg is unsigned even when we need to consider it as signed,
2473 we don't want to diagnose overflow here. */
2474 (convert (view_convert:stype @1))))
2476 /* (T)P - (T)(P + A) -> -(T) A */
2478 (minus (convert? @0)
2479 (convert (plus:c @@0 @1)))
2480 (if (INTEGRAL_TYPE_P (type)
2481 && TYPE_OVERFLOW_UNDEFINED (type)
2482 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2483 (with { tree utype = unsigned_type_for (type); }
2484 (convert (negate (convert:utype @1))))
2485 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2486 /* For integer types, if A has a smaller type
2487 than T the result depends on the possible
2489 E.g. T=size_t, A=(unsigned)429497295, P>0.
2490 However, if an overflow in P + A would cause
2491 undefined behavior, we can assume that there
2493 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2494 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2495 (negate (convert @1)))))
2498 (convert (pointer_plus @@0 @1)))
2499 (if (INTEGRAL_TYPE_P (type)
2500 && TYPE_OVERFLOW_UNDEFINED (type)
2501 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2502 (with { tree utype = unsigned_type_for (type); }
2503 (convert (negate (convert:utype @1))))
2504 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2505 /* For pointer types, if the conversion of A to the
2506 final type requires a sign- or zero-extension,
2507 then we have to punt - it is not defined which
2509 || (POINTER_TYPE_P (TREE_TYPE (@0))
2510 && TREE_CODE (@1) == INTEGER_CST
2511 && tree_int_cst_sign_bit (@1) == 0))
2512 (negate (convert @1)))))
2514 (pointer_diff @0 (pointer_plus @@0 @1))
2515 /* The second argument of pointer_plus must be interpreted as signed, and
2516 thus sign-extended if necessary. */
2517 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2518 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2519 second arg is unsigned even when we need to consider it as signed,
2520 we don't want to diagnose overflow here. */
2521 (negate (convert (view_convert:stype @1)))))
2523 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2525 (minus (convert (plus:c @@0 @1))
2526 (convert (plus:c @0 @2)))
2527 (if (INTEGRAL_TYPE_P (type)
2528 && TYPE_OVERFLOW_UNDEFINED (type)
2529 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2530 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2531 (with { tree utype = unsigned_type_for (type); }
2532 (convert (minus (convert:utype @1) (convert:utype @2))))
2533 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2534 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2535 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2536 /* For integer types, if A has a smaller type
2537 than T the result depends on the possible
2539 E.g. T=size_t, A=(unsigned)429497295, P>0.
2540 However, if an overflow in P + A would cause
2541 undefined behavior, we can assume that there
2543 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2544 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2545 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2546 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2547 (minus (convert @1) (convert @2)))))
2549 (minus (convert (pointer_plus @@0 @1))
2550 (convert (pointer_plus @0 @2)))
2551 (if (INTEGRAL_TYPE_P (type)
2552 && TYPE_OVERFLOW_UNDEFINED (type)
2553 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2554 (with { tree utype = unsigned_type_for (type); }
2555 (convert (minus (convert:utype @1) (convert:utype @2))))
2556 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2557 /* For pointer types, if the conversion of A to the
2558 final type requires a sign- or zero-extension,
2559 then we have to punt - it is not defined which
2561 || (POINTER_TYPE_P (TREE_TYPE (@0))
2562 && TREE_CODE (@1) == INTEGER_CST
2563 && tree_int_cst_sign_bit (@1) == 0
2564 && TREE_CODE (@2) == INTEGER_CST
2565 && tree_int_cst_sign_bit (@2) == 0))
2566 (minus (convert @1) (convert @2)))))
2568 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2569 (pointer_diff @0 @1))
2571 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2572 /* The second argument of pointer_plus must be interpreted as signed, and
2573 thus sign-extended if necessary. */
2574 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2575 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2576 second arg is unsigned even when we need to consider it as signed,
2577 we don't want to diagnose overflow here. */
2578 (minus (convert (view_convert:stype @1))
2579 (convert (view_convert:stype @2)))))))
2581 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2582 Modeled after fold_plusminus_mult_expr. */
2583 (if (!TYPE_SATURATING (type)
2584 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2585 (for plusminus (plus minus)
2587 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2588 (if (!ANY_INTEGRAL_TYPE_P (type)
2589 || TYPE_OVERFLOW_WRAPS (type)
2590 || (INTEGRAL_TYPE_P (type)
2591 && tree_expr_nonzero_p (@0)
2592 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2593 (if (single_use (@3) || single_use (@4))
2594 /* If @1 +- @2 is constant require a hard single-use on either
2595 original operand (but not on both). */
2596 (mult (plusminus @1 @2) @0)
2598 (mult! (plusminus @1 @2) @0)
2601 /* We cannot generate constant 1 for fract. */
2602 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2604 (plusminus @0 (mult:c@3 @0 @2))
2605 (if ((!ANY_INTEGRAL_TYPE_P (type)
2606 || TYPE_OVERFLOW_WRAPS (type)
2607 /* For @0 + @0*@2 this transformation would introduce UB
2608 (where there was none before) for @0 in [-1,0] and @2 max.
2609 For @0 - @0*@2 this transformation would introduce UB
2610 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2611 || (INTEGRAL_TYPE_P (type)
2612 && ((tree_expr_nonzero_p (@0)
2613 && expr_not_equal_to (@0,
2614 wi::minus_one (TYPE_PRECISION (type))))
2615 || (plusminus == PLUS_EXPR
2616 ? expr_not_equal_to (@2,
2617 wi::max_value (TYPE_PRECISION (type), SIGNED))
2618 /* Let's ignore the @0 -1 and @2 min case. */
2619 : (expr_not_equal_to (@2,
2620 wi::min_value (TYPE_PRECISION (type), SIGNED))
2621 && expr_not_equal_to (@2,
2622 wi::min_value (TYPE_PRECISION (type), SIGNED)
2625 (mult (plusminus { build_one_cst (type); } @2) @0)))
2627 (plusminus (mult:c@3 @0 @2) @0)
2628 (if ((!ANY_INTEGRAL_TYPE_P (type)
2629 || TYPE_OVERFLOW_WRAPS (type)
2630 /* For @0*@2 + @0 this transformation would introduce UB
2631 (where there was none before) for @0 in [-1,0] and @2 max.
2632 For @0*@2 - @0 this transformation would introduce UB
2633 for @0 0 and @2 min. */
2634 || (INTEGRAL_TYPE_P (type)
2635 && ((tree_expr_nonzero_p (@0)
2636 && (plusminus == MINUS_EXPR
2637 || expr_not_equal_to (@0,
2638 wi::minus_one (TYPE_PRECISION (type)))))
2639 || expr_not_equal_to (@2,
2640 (plusminus == PLUS_EXPR
2641 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2642 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2644 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2647 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2648 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2650 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2651 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2652 && tree_fits_uhwi_p (@1)
2653 && tree_to_uhwi (@1) < element_precision (type))
2654 (with { tree t = type;
2655 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2656 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2657 element_precision (type));
2659 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2661 cst = build_uniform_cst (t, cst); }
2662 (convert (mult (convert:t @0) { cst; })))))
2664 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2665 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2666 && tree_fits_uhwi_p (@1)
2667 && tree_to_uhwi (@1) < element_precision (type)
2668 && tree_fits_uhwi_p (@2)
2669 && tree_to_uhwi (@2) < element_precision (type))
2670 (with { tree t = type;
2671 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2672 unsigned int prec = element_precision (type);
2673 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2674 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2675 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2677 cst = build_uniform_cst (t, cst); }
2678 (convert (mult (convert:t @0) { cst; })))))
2681 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2683 (for minmax (min max FMIN_ALL FMAX_ALL)
2687 /* min(max(x,y),y) -> y. */
2689 (min:c (max:c @0 @1) @1)
2691 /* max(min(x,y),y) -> y. */
2693 (max:c (min:c @0 @1) @1)
2695 /* max(a,-a) -> abs(a). */
2697 (max:c @0 (negate @0))
2698 (if (TREE_CODE (type) != COMPLEX_TYPE
2699 && (! ANY_INTEGRAL_TYPE_P (type)
2700 || TYPE_OVERFLOW_UNDEFINED (type)))
2702 /* min(a,-a) -> -abs(a). */
2704 (min:c @0 (negate @0))
2705 (if (TREE_CODE (type) != COMPLEX_TYPE
2706 && (! ANY_INTEGRAL_TYPE_P (type)
2707 || TYPE_OVERFLOW_UNDEFINED (type)))
2712 (if (INTEGRAL_TYPE_P (type)
2713 && TYPE_MIN_VALUE (type)
2714 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2716 (if (INTEGRAL_TYPE_P (type)
2717 && TYPE_MAX_VALUE (type)
2718 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2723 (if (INTEGRAL_TYPE_P (type)
2724 && TYPE_MAX_VALUE (type)
2725 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2727 (if (INTEGRAL_TYPE_P (type)
2728 && TYPE_MIN_VALUE (type)
2729 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2732 /* max (a, a + CST) -> a + CST where CST is positive. */
2733 /* max (a, a + CST) -> a where CST is negative. */
2735 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2736 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2737 (if (tree_int_cst_sgn (@1) > 0)
2741 /* min (a, a + CST) -> a where CST is positive. */
2742 /* min (a, a + CST) -> a + CST where CST is negative. */
2744 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2745 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2746 (if (tree_int_cst_sgn (@1) > 0)
2750 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2751 and the outer convert demotes the expression back to x's type. */
2752 (for minmax (min max)
2754 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2755 (if (INTEGRAL_TYPE_P (type)
2756 && types_match (@1, type) && int_fits_type_p (@2, type)
2757 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2758 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2759 (minmax @1 (convert @2)))))
2761 (for minmax (FMIN_ALL FMAX_ALL)
2762 /* If either argument is NaN, return the other one. Avoid the
2763 transformation if we get (and honor) a signalling NaN. */
2765 (minmax:c @0 REAL_CST@1)
2766 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2767 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2769 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2770 functions to return the numeric arg if the other one is NaN.
2771 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2772 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2773 worry about it either. */
2774 (if (flag_finite_math_only)
2781 /* min (-A, -B) -> -max (A, B) */
2782 (for minmax (min max FMIN_ALL FMAX_ALL)
2783 maxmin (max min FMAX_ALL FMIN_ALL)
2785 (minmax (negate:s@2 @0) (negate:s@3 @1))
2786 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2787 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2788 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2789 (negate (maxmin @0 @1)))))
2790 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2791 MAX (~X, ~Y) -> ~MIN (X, Y) */
2792 (for minmax (min max)
2795 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2796 (bit_not (maxmin @0 @1))))
2798 /* MIN (X, Y) == X -> X <= Y */
2799 (for minmax (min min max max)
2803 (cmp:c (minmax:c @0 @1) @0)
2804 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2806 /* MIN (X, 5) == 0 -> X == 0
2807 MIN (X, 5) == 7 -> false */
2810 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2811 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2812 TYPE_SIGN (TREE_TYPE (@0))))
2813 { constant_boolean_node (cmp == NE_EXPR, type); }
2814 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2815 TYPE_SIGN (TREE_TYPE (@0))))
2819 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2820 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2821 TYPE_SIGN (TREE_TYPE (@0))))
2822 { constant_boolean_node (cmp == NE_EXPR, type); }
2823 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2824 TYPE_SIGN (TREE_TYPE (@0))))
2826 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2827 (for minmax (min min max max min min max max )
2828 cmp (lt le gt ge gt ge lt le )
2829 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2831 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2832 (comb (cmp @0 @2) (cmp @1 @2))))
2834 /* Undo fancy way of writing max/min or other ?: expressions,
2835 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2836 People normally use ?: and that is what we actually try to optimize. */
2837 (for cmp (simple_comparison)
2839 (minus @0 (bit_and:c (minus @0 @1)
2840 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2841 (if (INTEGRAL_TYPE_P (type)
2842 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2843 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2844 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2845 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2846 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2847 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2848 (cond (cmp @2 @3) @1 @0)))
2850 (plus:c @0 (bit_and:c (minus @1 @0)
2851 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2852 (if (INTEGRAL_TYPE_P (type)
2853 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2854 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2855 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2856 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2857 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2858 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2859 (cond (cmp @2 @3) @1 @0)))
2860 /* Similarly with ^ instead of - though in that case with :c. */
2862 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
2863 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2864 (if (INTEGRAL_TYPE_P (type)
2865 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2866 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2867 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2868 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2869 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2870 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2871 (cond (cmp @2 @3) @1 @0))))
2873 /* Simplifications of shift and rotates. */
2875 (for rotate (lrotate rrotate)
2877 (rotate integer_all_onesp@0 @1)
2880 /* Optimize -1 >> x for arithmetic right shifts. */
2882 (rshift integer_all_onesp@0 @1)
2883 (if (!TYPE_UNSIGNED (type)
2884 && tree_expr_nonnegative_p (@1))
2887 /* Optimize (x >> c) << c into x & (-1<<c). */
2889 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
2890 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2891 /* It doesn't matter if the right shift is arithmetic or logical. */
2892 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
2895 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
2896 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
2897 /* Allow intermediate conversion to integral type with whatever sign, as
2898 long as the low TYPE_PRECISION (type)
2899 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
2900 && INTEGRAL_TYPE_P (type)
2901 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2902 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2903 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
2904 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
2905 || wi::geu_p (wi::to_wide (@1),
2906 TYPE_PRECISION (type)
2907 - TYPE_PRECISION (TREE_TYPE (@2)))))
2908 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
2910 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2913 (rshift (lshift @0 INTEGER_CST@1) @1)
2914 (if (TYPE_UNSIGNED (type)
2915 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2916 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2918 (for shiftrotate (lrotate rrotate lshift rshift)
2920 (shiftrotate @0 integer_zerop)
2923 (shiftrotate integer_zerop@0 @1)
2925 /* Prefer vector1 << scalar to vector1 << vector2
2926 if vector2 is uniform. */
2927 (for vec (VECTOR_CST CONSTRUCTOR)
2929 (shiftrotate @0 vec@1)
2930 (with { tree tem = uniform_vector_p (@1); }
2932 (shiftrotate @0 { tem; }))))))
2934 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2935 Y is 0. Similarly for X >> Y. */
2937 (for shift (lshift rshift)
2939 (shift @0 SSA_NAME@1)
2940 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2942 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2943 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2945 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2949 /* Rewrite an LROTATE_EXPR by a constant into an
2950 RROTATE_EXPR by a new constant. */
2952 (lrotate @0 INTEGER_CST@1)
2953 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2954 build_int_cst (TREE_TYPE (@1),
2955 element_precision (type)), @1); }))
2957 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2958 (for op (lrotate rrotate rshift lshift)
2960 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2961 (with { unsigned int prec = element_precision (type); }
2962 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2963 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2964 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2965 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2966 (with { unsigned int low = (tree_to_uhwi (@1)
2967 + tree_to_uhwi (@2)); }
2968 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2969 being well defined. */
2971 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2972 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2973 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2974 { build_zero_cst (type); }
2975 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2976 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2979 /* ((1 << A) & 1) != 0 -> A == 0
2980 ((1 << A) & 1) == 0 -> A != 0 */
2984 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2985 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2987 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2988 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2992 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2993 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2995 || (!integer_zerop (@2)
2996 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2997 { constant_boolean_node (cmp == NE_EXPR, type); }
2998 (if (!integer_zerop (@2)
2999 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3000 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3002 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3003 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3004 if the new mask might be further optimized. */
3005 (for shift (lshift rshift)
3007 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3009 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3010 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3011 && tree_fits_uhwi_p (@1)
3012 && tree_to_uhwi (@1) > 0
3013 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3016 unsigned int shiftc = tree_to_uhwi (@1);
3017 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3018 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3019 tree shift_type = TREE_TYPE (@3);
3022 if (shift == LSHIFT_EXPR)
3023 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3024 else if (shift == RSHIFT_EXPR
3025 && type_has_mode_precision_p (shift_type))
3027 prec = TYPE_PRECISION (TREE_TYPE (@3));
3029 /* See if more bits can be proven as zero because of
3032 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3034 tree inner_type = TREE_TYPE (@0);
3035 if (type_has_mode_precision_p (inner_type)
3036 && TYPE_PRECISION (inner_type) < prec)
3038 prec = TYPE_PRECISION (inner_type);
3039 /* See if we can shorten the right shift. */
3041 shift_type = inner_type;
3042 /* Otherwise X >> C1 is all zeros, so we'll optimize
3043 it into (X, 0) later on by making sure zerobits
3047 zerobits = HOST_WIDE_INT_M1U;
3050 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3051 zerobits <<= prec - shiftc;
3053 /* For arithmetic shift if sign bit could be set, zerobits
3054 can contain actually sign bits, so no transformation is
3055 possible, unless MASK masks them all away. In that
3056 case the shift needs to be converted into logical shift. */
3057 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3058 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3060 if ((mask & zerobits) == 0)
3061 shift_type = unsigned_type_for (TREE_TYPE (@3));
3067 /* ((X << 16) & 0xff00) is (X, 0). */
3068 (if ((mask & zerobits) == mask)
3069 { build_int_cst (type, 0); }
3070 (with { newmask = mask | zerobits; }
3071 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3074 /* Only do the transformation if NEWMASK is some integer
3076 for (prec = BITS_PER_UNIT;
3077 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3078 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3081 (if (prec < HOST_BITS_PER_WIDE_INT
3082 || newmask == HOST_WIDE_INT_M1U)
3084 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3085 (if (!tree_int_cst_equal (newmaskt, @2))
3086 (if (shift_type != TREE_TYPE (@3))
3087 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3088 (bit_and @4 { newmaskt; })))))))))))))
3090 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3091 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3092 (for shift (lshift rshift)
3093 (for bit_op (bit_and bit_xor bit_ior)
3095 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3096 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3097 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3098 (bit_op (shift (convert @0) @1) { mask; }))))))
3100 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3102 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3103 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3104 && (element_precision (TREE_TYPE (@0))
3105 <= element_precision (TREE_TYPE (@1))
3106 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3108 { tree shift_type = TREE_TYPE (@0); }
3109 (convert (rshift (convert:shift_type @1) @2)))))
3111 /* ~(~X >>r Y) -> X >>r Y
3112 ~(~X <<r Y) -> X <<r Y */
3113 (for rotate (lrotate rrotate)
3115 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3116 (if ((element_precision (TREE_TYPE (@0))
3117 <= element_precision (TREE_TYPE (@1))
3118 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3119 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3120 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3122 { tree rotate_type = TREE_TYPE (@0); }
3123 (convert (rotate (convert:rotate_type @1) @2))))))
3125 /* Simplifications of conversions. */
3127 /* Basic strip-useless-type-conversions / strip_nops. */
3128 (for cvt (convert view_convert float fix_trunc)
3131 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3132 || (GENERIC && type == TREE_TYPE (@0)))
3135 /* Contract view-conversions. */
3137 (view_convert (view_convert @0))
3140 /* For integral conversions with the same precision or pointer
3141 conversions use a NOP_EXPR instead. */
3144 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3145 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3146 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3149 /* Strip inner integral conversions that do not change precision or size, or
3150 zero-extend while keeping the same size (for bool-to-char). */
3152 (view_convert (convert@0 @1))
3153 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3154 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3155 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3156 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3157 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3158 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3161 /* Simplify a view-converted empty constructor. */
3163 (view_convert CONSTRUCTOR@0)
3164 (if (TREE_CODE (@0) != SSA_NAME
3165 && CONSTRUCTOR_NELTS (@0) == 0)
3166 { build_zero_cst (type); }))
3168 /* Re-association barriers around constants and other re-association
3169 barriers can be removed. */
3171 (paren CONSTANT_CLASS_P@0)
3174 (paren (paren@1 @0))
3177 /* Handle cases of two conversions in a row. */
3178 (for ocvt (convert float fix_trunc)
3179 (for icvt (convert float)
3184 tree inside_type = TREE_TYPE (@0);
3185 tree inter_type = TREE_TYPE (@1);
3186 int inside_int = INTEGRAL_TYPE_P (inside_type);
3187 int inside_ptr = POINTER_TYPE_P (inside_type);
3188 int inside_float = FLOAT_TYPE_P (inside_type);
3189 int inside_vec = VECTOR_TYPE_P (inside_type);
3190 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3191 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3192 int inter_int = INTEGRAL_TYPE_P (inter_type);
3193 int inter_ptr = POINTER_TYPE_P (inter_type);
3194 int inter_float = FLOAT_TYPE_P (inter_type);
3195 int inter_vec = VECTOR_TYPE_P (inter_type);
3196 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3197 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3198 int final_int = INTEGRAL_TYPE_P (type);
3199 int final_ptr = POINTER_TYPE_P (type);
3200 int final_float = FLOAT_TYPE_P (type);
3201 int final_vec = VECTOR_TYPE_P (type);
3202 unsigned int final_prec = TYPE_PRECISION (type);
3203 int final_unsignedp = TYPE_UNSIGNED (type);
3206 /* In addition to the cases of two conversions in a row
3207 handled below, if we are converting something to its own
3208 type via an object of identical or wider precision, neither
3209 conversion is needed. */
3210 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3212 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3213 && (((inter_int || inter_ptr) && final_int)
3214 || (inter_float && final_float))
3215 && inter_prec >= final_prec)
3218 /* Likewise, if the intermediate and initial types are either both
3219 float or both integer, we don't need the middle conversion if the
3220 former is wider than the latter and doesn't change the signedness
3221 (for integers). Avoid this if the final type is a pointer since
3222 then we sometimes need the middle conversion. */
3223 (if (((inter_int && inside_int) || (inter_float && inside_float))
3224 && (final_int || final_float)
3225 && inter_prec >= inside_prec
3226 && (inter_float || inter_unsignedp == inside_unsignedp))
3229 /* If we have a sign-extension of a zero-extended value, we can
3230 replace that by a single zero-extension. Likewise if the
3231 final conversion does not change precision we can drop the
3232 intermediate conversion. */
3233 (if (inside_int && inter_int && final_int
3234 && ((inside_prec < inter_prec && inter_prec < final_prec
3235 && inside_unsignedp && !inter_unsignedp)
3236 || final_prec == inter_prec))
3239 /* Two conversions in a row are not needed unless:
3240 - some conversion is floating-point (overstrict for now), or
3241 - some conversion is a vector (overstrict for now), or
3242 - the intermediate type is narrower than both initial and
3244 - the intermediate type and innermost type differ in signedness,
3245 and the outermost type is wider than the intermediate, or
3246 - the initial type is a pointer type and the precisions of the
3247 intermediate and final types differ, or
3248 - the final type is a pointer type and the precisions of the
3249 initial and intermediate types differ. */
3250 (if (! inside_float && ! inter_float && ! final_float
3251 && ! inside_vec && ! inter_vec && ! final_vec
3252 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3253 && ! (inside_int && inter_int
3254 && inter_unsignedp != inside_unsignedp
3255 && inter_prec < final_prec)
3256 && ((inter_unsignedp && inter_prec > inside_prec)
3257 == (final_unsignedp && final_prec > inter_prec))
3258 && ! (inside_ptr && inter_prec != final_prec)
3259 && ! (final_ptr && inside_prec != inter_prec))
3262 /* A truncation to an unsigned type (a zero-extension) should be
3263 canonicalized as bitwise and of a mask. */
3264 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3265 && final_int && inter_int && inside_int
3266 && final_prec == inside_prec
3267 && final_prec > inter_prec
3269 (convert (bit_and @0 { wide_int_to_tree
3271 wi::mask (inter_prec, false,
3272 TYPE_PRECISION (inside_type))); })))
3274 /* If we are converting an integer to a floating-point that can
3275 represent it exactly and back to an integer, we can skip the
3276 floating-point conversion. */
3277 (if (GIMPLE /* PR66211 */
3278 && inside_int && inter_float && final_int &&
3279 (unsigned) significand_size (TYPE_MODE (inter_type))
3280 >= inside_prec - !inside_unsignedp)
3283 /* If we have a narrowing conversion to an integral type that is fed by a
3284 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3285 masks off bits outside the final type (and nothing else). */
3287 (convert (bit_and @0 INTEGER_CST@1))
3288 (if (INTEGRAL_TYPE_P (type)
3289 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3290 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3291 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3292 TYPE_PRECISION (type)), 0))
3296 /* (X /[ex] A) * A -> X. */
3298 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3301 /* Simplify (A / B) * B + (A % B) -> A. */
3302 (for div (trunc_div ceil_div floor_div round_div)
3303 mod (trunc_mod ceil_mod floor_mod round_mod)
3305 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3308 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3309 (for op (plus minus)
3311 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3312 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3313 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3316 wi::overflow_type overflow;
3317 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3318 TYPE_SIGN (type), &overflow);
3320 (if (types_match (type, TREE_TYPE (@2))
3321 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3322 (op @0 { wide_int_to_tree (type, mul); })
3323 (with { tree utype = unsigned_type_for (type); }
3324 (convert (op (convert:utype @0)
3325 (mult (convert:utype @1) (convert:utype @2))))))))))
3327 /* Canonicalization of binary operations. */
3329 /* Convert X + -C into X - C. */
3331 (plus @0 REAL_CST@1)
3332 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3333 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3334 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3335 (minus @0 { tem; })))))
3337 /* Convert x+x into x*2. */
3340 (if (SCALAR_FLOAT_TYPE_P (type))
3341 (mult @0 { build_real (type, dconst2); })
3342 (if (INTEGRAL_TYPE_P (type))
3343 (mult @0 { build_int_cst (type, 2); }))))
3347 (minus integer_zerop @1)
3350 (pointer_diff integer_zerop @1)
3351 (negate (convert @1)))
3353 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3354 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3355 (-ARG1 + ARG0) reduces to -ARG1. */
3357 (minus real_zerop@0 @1)
3358 (if (fold_real_zero_addition_p (type, @0, 0))
3361 /* Transform x * -1 into -x. */
3363 (mult @0 integer_minus_onep)
3366 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3367 signed overflow for CST != 0 && CST != -1. */
3369 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3370 (if (TREE_CODE (@2) != INTEGER_CST
3372 && !integer_zerop (@1) && !integer_minus_onep (@1))
3373 (mult (mult @0 @2) @1)))
3375 /* True if we can easily extract the real and imaginary parts of a complex
3377 (match compositional_complex
3378 (convert? (complex @0 @1)))
3380 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3382 (complex (realpart @0) (imagpart @0))
3385 (realpart (complex @0 @1))
3388 (imagpart (complex @0 @1))
3391 /* Sometimes we only care about half of a complex expression. */
3393 (realpart (convert?:s (conj:s @0)))
3394 (convert (realpart @0)))
3396 (imagpart (convert?:s (conj:s @0)))
3397 (convert (negate (imagpart @0))))
3398 (for part (realpart imagpart)
3399 (for op (plus minus)
3401 (part (convert?:s@2 (op:s @0 @1)))
3402 (convert (op (part @0) (part @1))))))
3404 (realpart (convert?:s (CEXPI:s @0)))
3407 (imagpart (convert?:s (CEXPI:s @0)))
3410 /* conj(conj(x)) -> x */
3412 (conj (convert? (conj @0)))
3413 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3416 /* conj({x,y}) -> {x,-y} */
3418 (conj (convert?:s (complex:s @0 @1)))
3419 (with { tree itype = TREE_TYPE (type); }
3420 (complex (convert:itype @0) (negate (convert:itype @1)))))
3422 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3423 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3428 (bswap (bit_not (bswap @0)))
3430 (for bitop (bit_xor bit_ior bit_and)
3432 (bswap (bitop:c (bswap @0) @1))
3433 (bitop @0 (bswap @1)))))
3436 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3438 /* Simplify constant conditions.
3439 Only optimize constant conditions when the selected branch
3440 has the same type as the COND_EXPR. This avoids optimizing
3441 away "c ? x : throw", where the throw has a void type.
3442 Note that we cannot throw away the fold-const.c variant nor
3443 this one as we depend on doing this transform before possibly
3444 A ? B : B -> B triggers and the fold-const.c one can optimize
3445 0 ? A : B to B even if A has side-effects. Something
3446 genmatch cannot handle. */
3448 (cond INTEGER_CST@0 @1 @2)
3449 (if (integer_zerop (@0))
3450 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3452 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3455 (vec_cond VECTOR_CST@0 @1 @2)
3456 (if (integer_all_onesp (@0))
3458 (if (integer_zerop (@0))
3462 /* Sink unary operations to branches, but only if we do fold both. */
3463 (for op (negate bit_not abs absu)
3465 (op (vec_cond:s @0 @1 @2))
3466 (vec_cond @0 (op! @1) (op! @2))))
3468 /* Sink binary operation to branches, but only if we can fold it. */
3469 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3470 rdiv trunc_div ceil_div floor_div round_div
3471 trunc_mod ceil_mod floor_mod round_mod min max)
3472 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3474 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3475 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3477 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3479 (op (vec_cond:s @0 @1 @2) @3)
3480 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3482 (op @3 (vec_cond:s @0 @1 @2))
3483 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3486 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3487 Currently disabled after pass lvec because ARM understands
3488 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3490 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3491 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3492 (vec_cond (bit_and @0 @3) @1 @2)))
3494 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3495 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3496 (vec_cond (bit_ior @0 @3) @1 @2)))
3498 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3499 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3500 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3502 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3503 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3504 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3506 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3508 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3509 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3510 (vec_cond (bit_and @0 @1) @2 @3)))
3512 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3513 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3514 (vec_cond (bit_ior @0 @1) @2 @3)))
3516 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3517 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3518 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3520 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3521 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3522 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3524 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3525 types are compatible. */
3527 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3528 (if (VECTOR_BOOLEAN_TYPE_P (type)
3529 && types_match (type, TREE_TYPE (@0)))
3530 (if (integer_zerop (@1) && integer_all_onesp (@2))
3532 (if (integer_all_onesp (@1) && integer_zerop (@2))
3535 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3537 /* This pattern implements two kinds simplification:
3540 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3541 1) Conversions are type widening from smaller type.
3542 2) Const c1 equals to c2 after canonicalizing comparison.
3543 3) Comparison has tree code LT, LE, GT or GE.
3544 This specific pattern is needed when (cmp (convert x) c) may not
3545 be simplified by comparison patterns because of multiple uses of
3546 x. It also makes sense here because simplifying across multiple
3547 referred var is always benefitial for complicated cases.
3550 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3551 (for cmp (lt le gt ge eq)
3553 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3556 tree from_type = TREE_TYPE (@1);
3557 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3558 enum tree_code code = ERROR_MARK;
3560 if (INTEGRAL_TYPE_P (from_type)
3561 && int_fits_type_p (@2, from_type)
3562 && (types_match (c1_type, from_type)
3563 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3564 && (TYPE_UNSIGNED (from_type)
3565 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3566 && (types_match (c2_type, from_type)
3567 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3568 && (TYPE_UNSIGNED (from_type)
3569 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3573 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3575 /* X <= Y - 1 equals to X < Y. */
3578 /* X > Y - 1 equals to X >= Y. */
3582 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3584 /* X < Y + 1 equals to X <= Y. */
3587 /* X >= Y + 1 equals to X > Y. */
3591 if (code != ERROR_MARK
3592 || wi::to_widest (@2) == wi::to_widest (@3))
3594 if (cmp == LT_EXPR || cmp == LE_EXPR)
3596 if (cmp == GT_EXPR || cmp == GE_EXPR)
3600 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3601 else if (int_fits_type_p (@3, from_type))
3605 (if (code == MAX_EXPR)
3606 (convert (max @1 (convert @2)))
3607 (if (code == MIN_EXPR)
3608 (convert (min @1 (convert @2)))
3609 (if (code == EQ_EXPR)
3610 (convert (cond (eq @1 (convert @3))
3611 (convert:from_type @3) (convert:from_type @2)))))))))
3613 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3615 1) OP is PLUS or MINUS.
3616 2) CMP is LT, LE, GT or GE.
3617 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3619 This pattern also handles special cases like:
3621 A) Operand x is a unsigned to signed type conversion and c1 is
3622 integer zero. In this case,
3623 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3624 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3625 B) Const c1 may not equal to (C3 op' C2). In this case we also
3626 check equality for (c1+1) and (c1-1) by adjusting comparison
3629 TODO: Though signed type is handled by this pattern, it cannot be
3630 simplified at the moment because C standard requires additional
3631 type promotion. In order to match&simplify it here, the IR needs
3632 to be cleaned up by other optimizers, i.e, VRP. */
3633 (for op (plus minus)
3634 (for cmp (lt le gt ge)
3636 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3637 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3638 (if (types_match (from_type, to_type)
3639 /* Check if it is special case A). */
3640 || (TYPE_UNSIGNED (from_type)
3641 && !TYPE_UNSIGNED (to_type)
3642 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3643 && integer_zerop (@1)
3644 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3647 wi::overflow_type overflow = wi::OVF_NONE;
3648 enum tree_code code, cmp_code = cmp;
3650 wide_int c1 = wi::to_wide (@1);
3651 wide_int c2 = wi::to_wide (@2);
3652 wide_int c3 = wi::to_wide (@3);
3653 signop sgn = TYPE_SIGN (from_type);
3655 /* Handle special case A), given x of unsigned type:
3656 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3657 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3658 if (!types_match (from_type, to_type))
3660 if (cmp_code == LT_EXPR)
3662 if (cmp_code == GE_EXPR)
3664 c1 = wi::max_value (to_type);
3666 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3667 compute (c3 op' c2) and check if it equals to c1 with op' being
3668 the inverted operator of op. Make sure overflow doesn't happen
3669 if it is undefined. */
3670 if (op == PLUS_EXPR)
3671 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3673 real_c1 = wi::add (c3, c2, sgn, &overflow);
3676 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3678 /* Check if c1 equals to real_c1. Boundary condition is handled
3679 by adjusting comparison operation if necessary. */
3680 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3683 /* X <= Y - 1 equals to X < Y. */
3684 if (cmp_code == LE_EXPR)
3686 /* X > Y - 1 equals to X >= Y. */
3687 if (cmp_code == GT_EXPR)
3690 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3693 /* X < Y + 1 equals to X <= Y. */
3694 if (cmp_code == LT_EXPR)
3696 /* X >= Y + 1 equals to X > Y. */
3697 if (cmp_code == GE_EXPR)
3700 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3702 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3704 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3709 (if (code == MAX_EXPR)
3710 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3711 { wide_int_to_tree (from_type, c2); })
3712 (if (code == MIN_EXPR)
3713 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3714 { wide_int_to_tree (from_type, c2); })))))))))
3716 (for cnd (cond vec_cond)
3717 /* A ? B : (A ? X : C) -> A ? B : C. */
3719 (cnd @0 (cnd @0 @1 @2) @3)
3722 (cnd @0 @1 (cnd @0 @2 @3))
3724 /* A ? B : (!A ? C : X) -> A ? B : C. */
3725 /* ??? This matches embedded conditions open-coded because genmatch
3726 would generate matching code for conditions in separate stmts only.
3727 The following is still important to merge then and else arm cases
3728 from if-conversion. */
3730 (cnd @0 @1 (cnd @2 @3 @4))
3731 (if (inverse_conditions_p (@0, @2))
3734 (cnd @0 (cnd @1 @2 @3) @4)
3735 (if (inverse_conditions_p (@0, @1))
3738 /* A ? B : B -> B. */
3743 /* !A ? B : C -> A ? C : B. */
3745 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3748 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3749 return all -1 or all 0 results. */
3750 /* ??? We could instead convert all instances of the vec_cond to negate,
3751 but that isn't necessarily a win on its own. */
3753 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3754 (if (VECTOR_TYPE_P (type)
3755 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3756 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3757 && (TYPE_MODE (TREE_TYPE (type))
3758 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3759 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3761 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3763 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3764 (if (VECTOR_TYPE_P (type)
3765 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3766 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3767 && (TYPE_MODE (TREE_TYPE (type))
3768 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3769 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3772 /* Simplifications of comparisons. */
3774 /* See if we can reduce the magnitude of a constant involved in a
3775 comparison by changing the comparison code. This is a canonicalization
3776 formerly done by maybe_canonicalize_comparison_1. */
3780 (cmp @0 uniform_integer_cst_p@1)
3781 (with { tree cst = uniform_integer_cst_p (@1); }
3782 (if (tree_int_cst_sgn (cst) == -1)
3783 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3784 wide_int_to_tree (TREE_TYPE (cst),
3790 (cmp @0 uniform_integer_cst_p@1)
3791 (with { tree cst = uniform_integer_cst_p (@1); }
3792 (if (tree_int_cst_sgn (cst) == 1)
3793 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3794 wide_int_to_tree (TREE_TYPE (cst),
3795 wi::to_wide (cst) - 1)); })))))
3797 /* We can simplify a logical negation of a comparison to the
3798 inverted comparison. As we cannot compute an expression
3799 operator using invert_tree_comparison we have to simulate
3800 that with expression code iteration. */
3801 (for cmp (tcc_comparison)
3802 icmp (inverted_tcc_comparison)
3803 ncmp (inverted_tcc_comparison_with_nans)
3804 /* Ideally we'd like to combine the following two patterns
3805 and handle some more cases by using
3806 (logical_inverted_value (cmp @0 @1))
3807 here but for that genmatch would need to "inline" that.
3808 For now implement what forward_propagate_comparison did. */
3810 (bit_not (cmp @0 @1))
3811 (if (VECTOR_TYPE_P (type)
3812 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3813 /* Comparison inversion may be impossible for trapping math,
3814 invert_tree_comparison will tell us. But we can't use
3815 a computed operator in the replacement tree thus we have
3816 to play the trick below. */
3817 (with { enum tree_code ic = invert_tree_comparison
3818 (cmp, HONOR_NANS (@0)); }
3824 (bit_xor (cmp @0 @1) integer_truep)
3825 (with { enum tree_code ic = invert_tree_comparison
3826 (cmp, HONOR_NANS (@0)); }
3832 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3833 ??? The transformation is valid for the other operators if overflow
3834 is undefined for the type, but performing it here badly interacts
3835 with the transformation in fold_cond_expr_with_comparison which
3836 attempts to synthetize ABS_EXPR. */
3838 (for sub (minus pointer_diff)
3840 (cmp (sub@2 @0 @1) integer_zerop)
3841 (if (single_use (@2))
3844 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3845 signed arithmetic case. That form is created by the compiler
3846 often enough for folding it to be of value. One example is in
3847 computing loop trip counts after Operator Strength Reduction. */
3848 (for cmp (simple_comparison)
3849 scmp (swapped_simple_comparison)
3851 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3852 /* Handle unfolded multiplication by zero. */
3853 (if (integer_zerop (@1))
3855 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3856 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3858 /* If @1 is negative we swap the sense of the comparison. */
3859 (if (tree_int_cst_sgn (@1) < 0)
3863 /* For integral types with undefined overflow fold
3864 x * C1 == C2 into x == C2 / C1 or false.
3865 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
3869 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
3870 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3871 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3872 && wi::to_wide (@1) != 0)
3873 (with { widest_int quot; }
3874 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
3875 TYPE_SIGN (TREE_TYPE (@0)), "))
3876 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
3877 { constant_boolean_node (cmp == NE_EXPR, type); }))
3878 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3879 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3880 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
3883 tree itype = TREE_TYPE (@0);
3884 int p = TYPE_PRECISION (itype);
3885 wide_int m = wi::one (p + 1) << p;
3886 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
3887 wide_int i = wide_int::from (wi::mod_inv (a, m),
3888 p, TYPE_SIGN (itype));
3889 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
3892 /* Simplify comparison of something with itself. For IEEE
3893 floating-point, we can only do some of these simplifications. */
3897 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3898 || ! HONOR_NANS (@0))
3899 { constant_boolean_node (true, type); }
3900 (if (cmp != EQ_EXPR)
3906 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3907 || ! HONOR_NANS (@0))
3908 { constant_boolean_node (false, type); })))
3909 (for cmp (unle unge uneq)
3912 { constant_boolean_node (true, type); }))
3913 (for cmp (unlt ungt)
3919 (if (!flag_trapping_math)
3920 { constant_boolean_node (false, type); }))
3922 /* Fold ~X op ~Y as Y op X. */
3923 (for cmp (simple_comparison)
3925 (cmp (bit_not@2 @0) (bit_not@3 @1))
3926 (if (single_use (@2) && single_use (@3))
3929 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3930 (for cmp (simple_comparison)
3931 scmp (swapped_simple_comparison)
3933 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3934 (if (single_use (@2)
3935 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3936 (scmp @0 (bit_not @1)))))
3938 (for cmp (simple_comparison)
3939 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3941 (cmp (convert@2 @0) (convert? @1))
3942 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3943 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3944 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3945 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3946 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3949 tree type1 = TREE_TYPE (@1);
3950 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3952 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3953 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3954 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3955 type1 = float_type_node;
3956 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3957 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3958 type1 = double_type_node;
3961 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3962 ? TREE_TYPE (@0) : type1);
3964 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3965 (cmp (convert:newtype @0) (convert:newtype @1))))))
3969 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3971 /* a CMP (-0) -> a CMP 0 */
3972 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3973 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3974 /* x != NaN is always true, other ops are always false. */
3975 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3976 && ! HONOR_SNANS (@1))
3977 { constant_boolean_node (cmp == NE_EXPR, type); })
3978 /* Fold comparisons against infinity. */
3979 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3980 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3983 REAL_VALUE_TYPE max;
3984 enum tree_code code = cmp;
3985 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3987 code = swap_tree_comparison (code);
3990 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3991 (if (code == GT_EXPR
3992 && !(HONOR_NANS (@0) && flag_trapping_math))
3993 { constant_boolean_node (false, type); })
3994 (if (code == LE_EXPR)
3995 /* x <= +Inf is always true, if we don't care about NaNs. */
3996 (if (! HONOR_NANS (@0))
3997 { constant_boolean_node (true, type); }
3998 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3999 an "invalid" exception. */
4000 (if (!flag_trapping_math)
4002 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4003 for == this introduces an exception for x a NaN. */
4004 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4006 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4008 (lt @0 { build_real (TREE_TYPE (@0), max); })
4009 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4010 /* x < +Inf is always equal to x <= DBL_MAX. */
4011 (if (code == LT_EXPR)
4012 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4014 (ge @0 { build_real (TREE_TYPE (@0), max); })
4015 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4016 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4017 an exception for x a NaN so use an unordered comparison. */
4018 (if (code == NE_EXPR)
4019 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4020 (if (! HONOR_NANS (@0))
4022 (ge @0 { build_real (TREE_TYPE (@0), max); })
4023 (le @0 { build_real (TREE_TYPE (@0), max); }))
4025 (unge @0 { build_real (TREE_TYPE (@0), max); })
4026 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4028 /* If this is a comparison of a real constant with a PLUS_EXPR
4029 or a MINUS_EXPR of a real constant, we can convert it into a
4030 comparison with a revised real constant as long as no overflow
4031 occurs when unsafe_math_optimizations are enabled. */
4032 (if (flag_unsafe_math_optimizations)
4033 (for op (plus minus)
4035 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4038 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4039 TREE_TYPE (@1), @2, @1);
4041 (if (tem && !TREE_OVERFLOW (tem))
4042 (cmp @0 { tem; }))))))
4044 /* Likewise, we can simplify a comparison of a real constant with
4045 a MINUS_EXPR whose first operand is also a real constant, i.e.
4046 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4047 floating-point types only if -fassociative-math is set. */
4048 (if (flag_associative_math)
4050 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4051 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4052 (if (tem && !TREE_OVERFLOW (tem))
4053 (cmp { tem; } @1)))))
4055 /* Fold comparisons against built-in math functions. */
4056 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4059 (cmp (sq @0) REAL_CST@1)
4061 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4063 /* sqrt(x) < y is always false, if y is negative. */
4064 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4065 { constant_boolean_node (false, type); })
4066 /* sqrt(x) > y is always true, if y is negative and we
4067 don't care about NaNs, i.e. negative values of x. */
4068 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4069 { constant_boolean_node (true, type); })
4070 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4071 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4072 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4074 /* sqrt(x) < 0 is always false. */
4075 (if (cmp == LT_EXPR)
4076 { constant_boolean_node (false, type); })
4077 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4078 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4079 { constant_boolean_node (true, type); })
4080 /* sqrt(x) <= 0 -> x == 0. */
4081 (if (cmp == LE_EXPR)
4083 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4084 == or !=. In the last case:
4086 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4088 if x is negative or NaN. Due to -funsafe-math-optimizations,
4089 the results for other x follow from natural arithmetic. */
4091 (if ((cmp == LT_EXPR
4095 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4096 /* Give up for -frounding-math. */
4097 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4101 enum tree_code ncmp = cmp;
4102 const real_format *fmt
4103 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4104 real_arithmetic (&c2, MULT_EXPR,
4105 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4106 real_convert (&c2, fmt, &c2);
4107 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4108 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4109 if (!REAL_VALUE_ISINF (c2))
4111 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4112 build_real (TREE_TYPE (@0), c2));
4113 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4115 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4116 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4117 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4118 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4119 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4120 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4123 /* With rounding to even, sqrt of up to 3 different values
4124 gives the same normal result, so in some cases c2 needs
4126 REAL_VALUE_TYPE c2alt, tow;
4127 if (cmp == LT_EXPR || cmp == GE_EXPR)
4131 real_nextafter (&c2alt, fmt, &c2, &tow);
4132 real_convert (&c2alt, fmt, &c2alt);
4133 if (REAL_VALUE_ISINF (c2alt))
4137 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4138 build_real (TREE_TYPE (@0), c2alt));
4139 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4141 else if (real_equal (&TREE_REAL_CST (c3),
4142 &TREE_REAL_CST (@1)))
4148 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4149 (if (REAL_VALUE_ISINF (c2))
4150 /* sqrt(x) > y is x == +Inf, when y is very large. */
4151 (if (HONOR_INFINITIES (@0))
4152 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4153 { constant_boolean_node (false, type); })
4154 /* sqrt(x) > c is the same as x > c*c. */
4155 (if (ncmp != ERROR_MARK)
4156 (if (ncmp == GE_EXPR)
4157 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4158 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4159 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4160 (if (REAL_VALUE_ISINF (c2))
4162 /* sqrt(x) < y is always true, when y is a very large
4163 value and we don't care about NaNs or Infinities. */
4164 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4165 { constant_boolean_node (true, type); })
4166 /* sqrt(x) < y is x != +Inf when y is very large and we
4167 don't care about NaNs. */
4168 (if (! HONOR_NANS (@0))
4169 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4170 /* sqrt(x) < y is x >= 0 when y is very large and we
4171 don't care about Infinities. */
4172 (if (! HONOR_INFINITIES (@0))
4173 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4174 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4177 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4178 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4179 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4180 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4181 (if (ncmp == LT_EXPR)
4182 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4183 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4184 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4185 (if (ncmp != ERROR_MARK && GENERIC)
4186 (if (ncmp == LT_EXPR)
4188 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4189 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4191 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4192 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4193 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4195 (cmp (sq @0) (sq @1))
4196 (if (! HONOR_NANS (@0))
4199 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4200 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4201 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4203 (cmp (float@0 @1) (float @2))
4204 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4205 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4208 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4209 tree type1 = TREE_TYPE (@1);
4210 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4211 tree type2 = TREE_TYPE (@2);
4212 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4214 (if (fmt.can_represent_integral_type_p (type1)
4215 && fmt.can_represent_integral_type_p (type2))
4216 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4217 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4218 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4219 && type1_signed_p >= type2_signed_p)
4220 (icmp @1 (convert @2))
4221 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4222 && type1_signed_p <= type2_signed_p)
4223 (icmp (convert:type2 @1) @2)
4224 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4225 && type1_signed_p == type2_signed_p)
4226 (icmp @1 @2))))))))))
4228 /* Optimize various special cases of (FTYPE) N CMP CST. */
4229 (for cmp (lt le eq ne ge gt)
4230 icmp (le le eq ne ge ge)
4232 (cmp (float @0) REAL_CST@1)
4233 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4234 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4237 tree itype = TREE_TYPE (@0);
4238 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4239 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4240 /* Be careful to preserve any potential exceptions due to
4241 NaNs. qNaNs are ok in == or != context.
4242 TODO: relax under -fno-trapping-math or
4243 -fno-signaling-nans. */
4245 = real_isnan (cst) && (cst->signalling
4246 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4248 /* TODO: allow non-fitting itype and SNaNs when
4249 -fno-trapping-math. */
4250 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4253 signop isign = TYPE_SIGN (itype);
4254 REAL_VALUE_TYPE imin, imax;
4255 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4256 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4258 REAL_VALUE_TYPE icst;
4259 if (cmp == GT_EXPR || cmp == GE_EXPR)
4260 real_ceil (&icst, fmt, cst);
4261 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4262 real_floor (&icst, fmt, cst);
4264 real_trunc (&icst, fmt, cst);
4266 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4268 bool overflow_p = false;
4270 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4273 /* Optimize cases when CST is outside of ITYPE's range. */
4274 (if (real_compare (LT_EXPR, cst, &imin))
4275 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4277 (if (real_compare (GT_EXPR, cst, &imax))
4278 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4280 /* Remove cast if CST is an integer representable by ITYPE. */
4282 (cmp @0 { gcc_assert (!overflow_p);
4283 wide_int_to_tree (itype, icst_val); })
4285 /* When CST is fractional, optimize
4286 (FTYPE) N == CST -> 0
4287 (FTYPE) N != CST -> 1. */
4288 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4289 { constant_boolean_node (cmp == NE_EXPR, type); })
4290 /* Otherwise replace with sensible integer constant. */
4293 gcc_checking_assert (!overflow_p);
4295 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4297 /* Fold A /[ex] B CMP C to A CMP B * C. */
4300 (cmp (exact_div @0 @1) INTEGER_CST@2)
4301 (if (!integer_zerop (@1))
4302 (if (wi::to_wide (@2) == 0)
4304 (if (TREE_CODE (@1) == INTEGER_CST)
4307 wi::overflow_type ovf;
4308 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4309 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4312 { constant_boolean_node (cmp == NE_EXPR, type); }
4313 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4314 (for cmp (lt le gt ge)
4316 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4317 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4320 wi::overflow_type ovf;
4321 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4322 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4325 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4326 TYPE_SIGN (TREE_TYPE (@2)))
4327 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4328 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4330 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4332 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4333 For large C (more than min/B+2^size), this is also true, with the
4334 multiplication computed modulo 2^size.
4335 For intermediate C, this just tests the sign of A. */
4336 (for cmp (lt le gt ge)
4339 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4340 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4341 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4342 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4345 tree utype = TREE_TYPE (@2);
4346 wide_int denom = wi::to_wide (@1);
4347 wide_int right = wi::to_wide (@2);
4348 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4349 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4350 bool small = wi::leu_p (right, smax);
4351 bool large = wi::geu_p (right, smin);
4353 (if (small || large)
4354 (cmp (convert:utype @0) (mult @2 (convert @1)))
4355 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4357 /* Unordered tests if either argument is a NaN. */
4359 (bit_ior (unordered @0 @0) (unordered @1 @1))
4360 (if (types_match (@0, @1))
4363 (bit_and (ordered @0 @0) (ordered @1 @1))
4364 (if (types_match (@0, @1))
4367 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4370 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4373 /* Simple range test simplifications. */
4374 /* A < B || A >= B -> true. */
4375 (for test1 (lt le le le ne ge)
4376 test2 (ge gt ge ne eq ne)
4378 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4379 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4380 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4381 { constant_boolean_node (true, type); })))
4382 /* A < B && A >= B -> false. */
4383 (for test1 (lt lt lt le ne eq)
4384 test2 (ge gt eq gt eq gt)
4386 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4387 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4388 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4389 { constant_boolean_node (false, type); })))
4391 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4392 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4394 Note that comparisons
4395 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4396 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4397 will be canonicalized to above so there's no need to
4404 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4405 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4408 tree ty = TREE_TYPE (@0);
4409 unsigned prec = TYPE_PRECISION (ty);
4410 wide_int mask = wi::to_wide (@2, prec);
4411 wide_int rhs = wi::to_wide (@3, prec);
4412 signop sgn = TYPE_SIGN (ty);
4414 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4415 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4416 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4417 { build_zero_cst (ty); }))))))
4419 /* -A CMP -B -> B CMP A. */
4420 (for cmp (tcc_comparison)
4421 scmp (swapped_tcc_comparison)
4423 (cmp (negate @0) (negate @1))
4424 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4425 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4426 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4429 (cmp (negate @0) CONSTANT_CLASS_P@1)
4430 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4431 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4432 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4433 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4434 (if (tem && !TREE_OVERFLOW (tem))
4435 (scmp @0 { tem; }))))))
4437 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4440 (op (abs @0) zerop@1)
4443 /* From fold_sign_changed_comparison and fold_widened_comparison.
4444 FIXME: the lack of symmetry is disturbing. */
4445 (for cmp (simple_comparison)
4447 (cmp (convert@0 @00) (convert?@1 @10))
4448 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4449 /* Disable this optimization if we're casting a function pointer
4450 type on targets that require function pointer canonicalization. */
4451 && !(targetm.have_canonicalize_funcptr_for_compare ()
4452 && ((POINTER_TYPE_P (TREE_TYPE (@00))
4453 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4454 || (POINTER_TYPE_P (TREE_TYPE (@10))
4455 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4457 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4458 && (TREE_CODE (@10) == INTEGER_CST
4460 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4463 && !POINTER_TYPE_P (TREE_TYPE (@00)))
4464 /* ??? The special-casing of INTEGER_CST conversion was in the original
4465 code and here to avoid a spurious overflow flag on the resulting
4466 constant which fold_convert produces. */
4467 (if (TREE_CODE (@1) == INTEGER_CST)
4468 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4469 TREE_OVERFLOW (@1)); })
4470 (cmp @00 (convert @1)))
4472 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4473 /* If possible, express the comparison in the shorter mode. */
4474 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4475 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4476 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4477 && TYPE_UNSIGNED (TREE_TYPE (@00))))
4478 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4479 || ((TYPE_PRECISION (TREE_TYPE (@00))
4480 >= TYPE_PRECISION (TREE_TYPE (@10)))
4481 && (TYPE_UNSIGNED (TREE_TYPE (@00))
4482 == TYPE_UNSIGNED (TREE_TYPE (@10))))
4483 || (TREE_CODE (@10) == INTEGER_CST
4484 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4485 && int_fits_type_p (@10, TREE_TYPE (@00)))))
4486 (cmp @00 (convert @10))
4487 (if (TREE_CODE (@10) == INTEGER_CST
4488 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4489 && !int_fits_type_p (@10, TREE_TYPE (@00)))
4492 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4493 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4494 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4495 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4497 (if (above || below)
4498 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4499 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4500 (if (cmp == LT_EXPR || cmp == LE_EXPR)
4501 { constant_boolean_node (above ? true : false, type); }
4502 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4503 { constant_boolean_node (above ? false : true, type); }))))))))))))
4507 /* SSA names are canonicalized to 2nd place. */
4508 (cmp addr@0 SSA_NAME@1)
4510 { poly_int64 off; tree base; }
4511 /* A local variable can never be pointed to by
4512 the default SSA name of an incoming parameter. */
4513 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4514 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4515 && (base = get_base_address (TREE_OPERAND (@0, 0)))
4516 && TREE_CODE (base) == VAR_DECL
4517 && auto_var_in_fn_p (base, current_function_decl))
4518 (if (cmp == NE_EXPR)
4519 { constant_boolean_node (true, type); }
4520 { constant_boolean_node (false, type); })
4521 /* If the address is based on @1 decide using the offset. */
4522 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4523 && TREE_CODE (base) == MEM_REF
4524 && TREE_OPERAND (base, 0) == @1)
4525 (with { off += mem_ref_offset (base).force_shwi (); }
4526 (if (known_ne (off, 0))
4527 { constant_boolean_node (cmp == NE_EXPR, type); }
4528 (if (known_eq (off, 0))
4529 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4531 /* Equality compare simplifications from fold_binary */
4534 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4535 Similarly for NE_EXPR. */
4537 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4538 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4539 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4540 { constant_boolean_node (cmp == NE_EXPR, type); }))
4542 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
4544 (cmp (bit_xor @0 @1) integer_zerop)
4547 /* (X ^ Y) == Y becomes X == 0.
4548 Likewise (X ^ Y) == X becomes Y == 0. */
4550 (cmp:c (bit_xor:c @0 @1) @0)
4551 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4553 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
4555 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4556 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4557 (cmp @0 (bit_xor @1 (convert @2)))))
4560 (cmp (convert? addr@0) integer_zerop)
4561 (if (tree_single_nonzero_warnv_p (@0, NULL))
4562 { constant_boolean_node (cmp == NE_EXPR, type); }))
4564 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
4566 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
4567 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
4569 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
4570 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
4571 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
4572 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
4577 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
4578 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4579 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4580 && types_match (@0, @1))
4581 (ncmp (bit_xor @0 @1) @2)))))
4582 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
4583 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
4587 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
4588 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4589 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4590 && types_match (@0, @1))
4591 (ncmp (bit_xor @0 @1) @2))))
4593 /* If we have (A & C) == C where C is a power of 2, convert this into
4594 (A & C) != 0. Similarly for NE_EXPR. */
4598 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4599 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4601 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4602 convert this into a shift followed by ANDing with D. */
4605 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4606 INTEGER_CST@2 integer_zerop)
4607 (if (integer_pow2p (@2))
4609 int shift = (wi::exact_log2 (wi::to_wide (@2))
4610 - wi::exact_log2 (wi::to_wide (@1)));
4614 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4616 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4619 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4620 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
4624 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4625 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4626 && type_has_mode_precision_p (TREE_TYPE (@0))
4627 && element_precision (@2) >= element_precision (@0)
4628 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4629 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4630 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4632 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4633 this into a right shift or sign extension followed by ANDing with C. */
4636 (lt @0 integer_zerop)
4637 INTEGER_CST@1 integer_zerop)
4638 (if (integer_pow2p (@1)
4639 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4641 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4645 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4647 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4648 sign extension followed by AND with C will achieve the effect. */
4649 (bit_and (convert @0) @1)))))
4651 /* When the addresses are not directly of decls compare base and offset.
4652 This implements some remaining parts of fold_comparison address
4653 comparisons but still no complete part of it. Still it is good
4654 enough to make fold_stmt not regress when not dispatching to fold_binary. */
4655 (for cmp (simple_comparison)
4657 (cmp (convert1?@2 addr@0) (convert2? addr@1))
4660 poly_int64 off0, off1;
4661 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4662 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4663 if (base0 && TREE_CODE (base0) == MEM_REF)
4665 off0 += mem_ref_offset (base0).force_shwi ();
4666 base0 = TREE_OPERAND (base0, 0);
4668 if (base1 && TREE_CODE (base1) == MEM_REF)
4670 off1 += mem_ref_offset (base1).force_shwi ();
4671 base1 = TREE_OPERAND (base1, 0);
4674 (if (base0 && base1)
4678 /* Punt in GENERIC on variables with value expressions;
4679 the value expressions might point to fields/elements
4680 of other vars etc. */
4682 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4683 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4685 else if (decl_in_symtab_p (base0)
4686 && decl_in_symtab_p (base1))
4687 equal = symtab_node::get_create (base0)
4688 ->equal_address_to (symtab_node::get_create (base1));
4689 else if ((DECL_P (base0)
4690 || TREE_CODE (base0) == SSA_NAME
4691 || TREE_CODE (base0) == STRING_CST)
4693 || TREE_CODE (base1) == SSA_NAME
4694 || TREE_CODE (base1) == STRING_CST))
4695 equal = (base0 == base1);
4698 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4699 off0.is_constant (&ioff0);
4700 off1.is_constant (&ioff1);
4701 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4702 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4703 || (TREE_CODE (base0) == STRING_CST
4704 && TREE_CODE (base1) == STRING_CST
4705 && ioff0 >= 0 && ioff1 >= 0
4706 && ioff0 < TREE_STRING_LENGTH (base0)
4707 && ioff1 < TREE_STRING_LENGTH (base1)
4708 /* This is a too conservative test that the STRING_CSTs
4709 will not end up being string-merged. */
4710 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4711 TREE_STRING_POINTER (base1) + ioff1,
4712 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4713 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4715 else if (!DECL_P (base0) || !DECL_P (base1))
4717 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4719 /* If this is a pointer comparison, ignore for now even
4720 valid equalities where one pointer is the offset zero
4721 of one object and the other to one past end of another one. */
4722 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4724 /* Assume that automatic variables can't be adjacent to global
4726 else if (is_global_var (base0) != is_global_var (base1))
4730 tree sz0 = DECL_SIZE_UNIT (base0);
4731 tree sz1 = DECL_SIZE_UNIT (base1);
4732 /* If sizes are unknown, e.g. VLA or not representable,
4734 if (!tree_fits_poly_int64_p (sz0)
4735 || !tree_fits_poly_int64_p (sz1))
4739 poly_int64 size0 = tree_to_poly_int64 (sz0);
4740 poly_int64 size1 = tree_to_poly_int64 (sz1);
4741 /* If one offset is pointing (or could be) to the beginning
4742 of one object and the other is pointing to one past the
4743 last byte of the other object, punt. */
4744 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4746 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4748 /* If both offsets are the same, there are some cases
4749 we know that are ok. Either if we know they aren't
4750 zero, or if we know both sizes are no zero. */
4752 && known_eq (off0, off1)
4753 && (known_ne (off0, 0)
4754 || (known_ne (size0, 0) && known_ne (size1, 0))))
4761 && (cmp == EQ_EXPR || cmp == NE_EXPR
4762 /* If the offsets are equal we can ignore overflow. */
4763 || known_eq (off0, off1)
4764 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4765 /* Or if we compare using pointers to decls or strings. */
4766 || (POINTER_TYPE_P (TREE_TYPE (@2))
4767 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4769 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4770 { constant_boolean_node (known_eq (off0, off1), type); })
4771 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4772 { constant_boolean_node (known_ne (off0, off1), type); })
4773 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4774 { constant_boolean_node (known_lt (off0, off1), type); })
4775 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4776 { constant_boolean_node (known_le (off0, off1), type); })
4777 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4778 { constant_boolean_node (known_ge (off0, off1), type); })
4779 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4780 { constant_boolean_node (known_gt (off0, off1), type); }))
4783 (if (cmp == EQ_EXPR)
4784 { constant_boolean_node (false, type); })
4785 (if (cmp == NE_EXPR)
4786 { constant_boolean_node (true, type); })))))))))
4788 /* Simplify pointer equality compares using PTA. */
4792 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4793 && ptrs_compare_unequal (@0, @1))
4794 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4796 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4797 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4798 Disable the transform if either operand is pointer to function.
4799 This broke pr22051-2.c for arm where function pointer
4800 canonicalizaion is not wanted. */
4804 (cmp (convert @0) INTEGER_CST@1)
4805 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4806 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4807 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4808 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4809 && POINTER_TYPE_P (TREE_TYPE (@1))
4810 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4811 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4812 (cmp @0 (convert @1)))))
4814 /* Non-equality compare simplifications from fold_binary */
4815 (for cmp (lt gt le ge)
4816 /* Comparisons with the highest or lowest possible integer of
4817 the specified precision will have known values. */
4819 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4820 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4821 || POINTER_TYPE_P (TREE_TYPE (@1))
4822 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4823 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4826 tree cst = uniform_integer_cst_p (@1);
4827 tree arg1_type = TREE_TYPE (cst);
4828 unsigned int prec = TYPE_PRECISION (arg1_type);
4829 wide_int max = wi::max_value (arg1_type);
4830 wide_int signed_max = wi::max_value (prec, SIGNED);
4831 wide_int min = wi::min_value (arg1_type);
4834 (if (wi::to_wide (cst) == max)
4836 (if (cmp == GT_EXPR)
4837 { constant_boolean_node (false, type); })
4838 (if (cmp == GE_EXPR)
4840 (if (cmp == LE_EXPR)
4841 { constant_boolean_node (true, type); })
4842 (if (cmp == LT_EXPR)
4844 (if (wi::to_wide (cst) == min)
4846 (if (cmp == LT_EXPR)
4847 { constant_boolean_node (false, type); })
4848 (if (cmp == LE_EXPR)
4850 (if (cmp == GE_EXPR)
4851 { constant_boolean_node (true, type); })
4852 (if (cmp == GT_EXPR)
4854 (if (wi::to_wide (cst) == max - 1)
4856 (if (cmp == GT_EXPR)
4857 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4858 wide_int_to_tree (TREE_TYPE (cst),
4861 (if (cmp == LE_EXPR)
4862 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4863 wide_int_to_tree (TREE_TYPE (cst),
4866 (if (wi::to_wide (cst) == min + 1)
4868 (if (cmp == GE_EXPR)
4869 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4870 wide_int_to_tree (TREE_TYPE (cst),
4873 (if (cmp == LT_EXPR)
4874 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4875 wide_int_to_tree (TREE_TYPE (cst),
4878 (if (wi::to_wide (cst) == signed_max
4879 && TYPE_UNSIGNED (arg1_type)
4880 /* We will flip the signedness of the comparison operator
4881 associated with the mode of @1, so the sign bit is
4882 specified by this mode. Check that @1 is the signed
4883 max associated with this sign bit. */
4884 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4885 /* signed_type does not work on pointer types. */
4886 && INTEGRAL_TYPE_P (arg1_type))
4887 /* The following case also applies to X < signed_max+1
4888 and X >= signed_max+1 because previous transformations. */
4889 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4890 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4892 (if (cst == @1 && cmp == LE_EXPR)
4893 (ge (convert:st @0) { build_zero_cst (st); }))
4894 (if (cst == @1 && cmp == GT_EXPR)
4895 (lt (convert:st @0) { build_zero_cst (st); }))
4896 (if (cmp == LE_EXPR)
4897 (ge (view_convert:st @0) { build_zero_cst (st); }))
4898 (if (cmp == GT_EXPR)
4899 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4901 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4902 /* If the second operand is NaN, the result is constant. */
4905 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4906 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4907 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4908 ? false : true, type); })))
4910 /* bool_var != 0 becomes bool_var. */
4912 (ne @0 integer_zerop)
4913 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4914 && types_match (type, TREE_TYPE (@0)))
4916 /* bool_var == 1 becomes bool_var. */
4918 (eq @0 integer_onep)
4919 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4920 && types_match (type, TREE_TYPE (@0)))
4923 bool_var == 0 becomes !bool_var or
4924 bool_var != 1 becomes !bool_var
4925 here because that only is good in assignment context as long
4926 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4927 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4928 clearly less optimal and which we'll transform again in forwprop. */
4930 /* When one argument is a constant, overflow detection can be simplified.
4931 Currently restricted to single use so as not to interfere too much with
4932 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4933 A + CST CMP A -> A CMP' CST' */
4934 (for cmp (lt le ge gt)
4937 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4938 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4939 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4940 && wi::to_wide (@1) != 0
4942 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4943 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4944 wi::max_value (prec, UNSIGNED)
4945 - wi::to_wide (@1)); })))))
4947 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4948 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4949 expects the long form, so we restrict the transformation for now. */
4952 (cmp:c (minus@2 @0 @1) @0)
4953 (if (single_use (@2)
4954 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4955 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4958 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
4961 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
4962 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4963 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4966 /* Testing for overflow is unnecessary if we already know the result. */
4971 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4972 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4973 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4974 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4979 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4980 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4981 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4982 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4984 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4985 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4989 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4990 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4991 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4992 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4994 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
4995 is at least twice as wide as type of A and B, simplify to
4996 __builtin_mul_overflow (A, B, <unused>). */
4999 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5001 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5002 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5003 && TYPE_UNSIGNED (TREE_TYPE (@0))
5004 && (TYPE_PRECISION (TREE_TYPE (@3))
5005 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5006 && tree_fits_uhwi_p (@2)
5007 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5008 && types_match (@0, @1)
5009 && type_has_mode_precision_p (TREE_TYPE (@0))
5010 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5011 != CODE_FOR_nothing))
5012 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5013 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5015 /* Simplification of math builtins. These rules must all be optimizations
5016 as well as IL simplifications. If there is a possibility that the new
5017 form could be a pessimization, the rule should go in the canonicalization
5018 section that follows this one.
5020 Rules can generally go in this section if they satisfy one of
5023 - the rule describes an identity
5025 - the rule replaces calls with something as simple as addition or
5028 - the rule contains unary calls only and simplifies the surrounding
5029 arithmetic. (The idea here is to exclude non-unary calls in which
5030 one operand is constant and in which the call is known to be cheap
5031 when the operand has that value.) */
5033 (if (flag_unsafe_math_optimizations)
5034 /* Simplify sqrt(x) * sqrt(x) -> x. */
5036 (mult (SQRT_ALL@1 @0) @1)
5037 (if (!HONOR_SNANS (type))
5040 (for op (plus minus)
5041 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5045 (rdiv (op @0 @2) @1)))
5047 (for cmp (lt le gt ge)
5048 neg_cmp (gt ge lt le)
5049 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5051 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5053 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5055 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5056 || (real_zerop (tem) && !real_zerop (@1))))
5058 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5060 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5061 (neg_cmp @0 { tem; })))))))
5063 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5064 (for root (SQRT CBRT)
5066 (mult (root:s @0) (root:s @1))
5067 (root (mult @0 @1))))
5069 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5070 (for exps (EXP EXP2 EXP10 POW10)
5072 (mult (exps:s @0) (exps:s @1))
5073 (exps (plus @0 @1))))
5075 /* Simplify a/root(b/c) into a*root(c/b). */
5076 (for root (SQRT CBRT)
5078 (rdiv @0 (root:s (rdiv:s @1 @2)))
5079 (mult @0 (root (rdiv @2 @1)))))
5081 /* Simplify x/expN(y) into x*expN(-y). */
5082 (for exps (EXP EXP2 EXP10 POW10)
5084 (rdiv @0 (exps:s @1))
5085 (mult @0 (exps (negate @1)))))
5087 (for logs (LOG LOG2 LOG10 LOG10)
5088 exps (EXP EXP2 EXP10 POW10)
5089 /* logN(expN(x)) -> x. */
5093 /* expN(logN(x)) -> x. */
5098 /* Optimize logN(func()) for various exponential functions. We
5099 want to determine the value "x" and the power "exponent" in
5100 order to transform logN(x**exponent) into exponent*logN(x). */
5101 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5102 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5105 (if (SCALAR_FLOAT_TYPE_P (type))
5111 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5112 x = build_real_truncate (type, dconst_e ());
5115 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5116 x = build_real (type, dconst2);
5120 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5122 REAL_VALUE_TYPE dconst10;
5123 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5124 x = build_real (type, dconst10);
5131 (mult (logs { x; }) @0)))))
5139 (if (SCALAR_FLOAT_TYPE_P (type))
5145 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5146 x = build_real (type, dconsthalf);
5149 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5150 x = build_real_truncate (type, dconst_third ());
5156 (mult { x; } (logs @0))))))
5158 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5159 (for logs (LOG LOG2 LOG10)
5163 (mult @1 (logs @0))))
5165 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5166 or if C is a positive power of 2,
5167 pow(C,x) -> exp2(log2(C)*x). */
5175 (pows REAL_CST@0 @1)
5176 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5177 && real_isfinite (TREE_REAL_CST_PTR (@0))
5178 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5179 the use_exp2 case until after vectorization. It seems actually
5180 beneficial for all constants to postpone this until later,
5181 because exp(log(C)*x), while faster, will have worse precision
5182 and if x folds into a constant too, that is unnecessary
5184 && canonicalize_math_after_vectorization_p ())
5186 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5187 bool use_exp2 = false;
5188 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5189 && value->cl == rvc_normal)
5191 REAL_VALUE_TYPE frac_rvt = *value;
5192 SET_REAL_EXP (&frac_rvt, 1);
5193 if (real_equal (&frac_rvt, &dconst1))
5198 (if (optimize_pow_to_exp (@0, @1))
5199 (exps (mult (logs @0) @1)))
5200 (exp2s (mult (log2s @0) @1)))))))
5203 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5205 exps (EXP EXP2 EXP10 POW10)
5206 logs (LOG LOG2 LOG10 LOG10)
5208 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5209 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5210 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5211 (exps (plus (mult (logs @0) @1) @2)))))
5216 exps (EXP EXP2 EXP10 POW10)
5217 /* sqrt(expN(x)) -> expN(x*0.5). */
5220 (exps (mult @0 { build_real (type, dconsthalf); })))
5221 /* cbrt(expN(x)) -> expN(x/3). */
5224 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5225 /* pow(expN(x), y) -> expN(x*y). */
5228 (exps (mult @0 @1))))
5230 /* tan(atan(x)) -> x. */
5237 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5241 copysigns (COPYSIGN)
5246 REAL_VALUE_TYPE r_cst;
5247 build_sinatan_real (&r_cst, type);
5248 tree t_cst = build_real (type, r_cst);
5249 tree t_one = build_one_cst (type);
5251 (if (SCALAR_FLOAT_TYPE_P (type))
5252 (cond (lt (abs @0) { t_cst; })
5253 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5254 (copysigns { t_one; } @0))))))
5256 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5260 copysigns (COPYSIGN)
5265 REAL_VALUE_TYPE r_cst;
5266 build_sinatan_real (&r_cst, type);
5267 tree t_cst = build_real (type, r_cst);
5268 tree t_one = build_one_cst (type);
5269 tree t_zero = build_zero_cst (type);
5271 (if (SCALAR_FLOAT_TYPE_P (type))
5272 (cond (lt (abs @0) { t_cst; })
5273 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5274 (copysigns { t_zero; } @0))))))
5276 (if (!flag_errno_math)
5277 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5282 (sinhs (atanhs:s @0))
5283 (with { tree t_one = build_one_cst (type); }
5284 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5286 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5291 (coshs (atanhs:s @0))
5292 (with { tree t_one = build_one_cst (type); }
5293 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5295 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5297 (CABS (complex:C @0 real_zerop@1))
5300 /* trunc(trunc(x)) -> trunc(x), etc. */
5301 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5305 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5306 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5308 (fns integer_valued_real_p@0)
5311 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5313 (HYPOT:c @0 real_zerop@1)
5316 /* pow(1,x) -> 1. */
5318 (POW real_onep@0 @1)
5322 /* copysign(x,x) -> x. */
5323 (COPYSIGN_ALL @0 @0)
5327 /* copysign(x,-x) -> -x. */
5328 (COPYSIGN_ALL @0 (negate@1 @0))
5332 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5333 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5336 (for scale (LDEXP SCALBN SCALBLN)
5337 /* ldexp(0, x) -> 0. */
5339 (scale real_zerop@0 @1)
5341 /* ldexp(x, 0) -> x. */
5343 (scale @0 integer_zerop@1)
5345 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5347 (scale REAL_CST@0 @1)
5348 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5351 /* Canonicalization of sequences of math builtins. These rules represent
5352 IL simplifications but are not necessarily optimizations.
5354 The sincos pass is responsible for picking "optimal" implementations
5355 of math builtins, which may be more complicated and can sometimes go
5356 the other way, e.g. converting pow into a sequence of sqrts.
5357 We only want to do these canonicalizations before the pass has run. */
5359 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5360 /* Simplify tan(x) * cos(x) -> sin(x). */
5362 (mult:c (TAN:s @0) (COS:s @0))
5365 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5367 (mult:c @0 (POW:s @0 REAL_CST@1))
5368 (if (!TREE_OVERFLOW (@1))
5369 (POW @0 (plus @1 { build_one_cst (type); }))))
5371 /* Simplify sin(x) / cos(x) -> tan(x). */
5373 (rdiv (SIN:s @0) (COS:s @0))
5376 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5378 (rdiv (SINH:s @0) (COSH:s @0))
5381 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5383 (rdiv (TANH:s @0) (SINH:s @0))
5384 (rdiv {build_one_cst (type);} (COSH @0)))
5386 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5388 (rdiv (COS:s @0) (SIN:s @0))
5389 (rdiv { build_one_cst (type); } (TAN @0)))
5391 /* Simplify sin(x) / tan(x) -> cos(x). */
5393 (rdiv (SIN:s @0) (TAN:s @0))
5394 (if (! HONOR_NANS (@0)
5395 && ! HONOR_INFINITIES (@0))
5398 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5400 (rdiv (TAN:s @0) (SIN:s @0))
5401 (if (! HONOR_NANS (@0)
5402 && ! HONOR_INFINITIES (@0))
5403 (rdiv { build_one_cst (type); } (COS @0))))
5405 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5407 (mult (POW:s @0 @1) (POW:s @0 @2))
5408 (POW @0 (plus @1 @2)))
5410 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5412 (mult (POW:s @0 @1) (POW:s @2 @1))
5413 (POW (mult @0 @2) @1))
5415 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5417 (mult (POWI:s @0 @1) (POWI:s @2 @1))
5418 (POWI (mult @0 @2) @1))
5420 /* Simplify pow(x,c) / x -> pow(x,c-1). */
5422 (rdiv (POW:s @0 REAL_CST@1) @0)
5423 (if (!TREE_OVERFLOW (@1))
5424 (POW @0 (minus @1 { build_one_cst (type); }))))
5426 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5428 (rdiv @0 (POW:s @1 @2))
5429 (mult @0 (POW @1 (negate @2))))
5434 /* sqrt(sqrt(x)) -> pow(x,1/4). */
5437 (pows @0 { build_real (type, dconst_quarter ()); }))
5438 /* sqrt(cbrt(x)) -> pow(x,1/6). */
5441 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5442 /* cbrt(sqrt(x)) -> pow(x,1/6). */
5445 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5446 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
5448 (cbrts (cbrts tree_expr_nonnegative_p@0))
5449 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5450 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
5452 (sqrts (pows @0 @1))
5453 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5454 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
5456 (cbrts (pows tree_expr_nonnegative_p@0 @1))
5457 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5458 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
5460 (pows (sqrts @0) @1)
5461 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5462 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
5464 (pows (cbrts tree_expr_nonnegative_p@0) @1)
5465 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5466 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
5468 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5469 (pows @0 (mult @1 @2))))
5471 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
5473 (CABS (complex @0 @0))
5474 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5476 /* hypot(x,x) -> fabs(x)*sqrt(2). */
5479 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5481 /* cexp(x+yi) -> exp(x)*cexpi(y). */
5486 (cexps compositional_complex@0)
5487 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
5489 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5490 (mult @1 (imagpart @2)))))))
5492 (if (canonicalize_math_p ())
5493 /* floor(x) -> trunc(x) if x is nonnegative. */
5494 (for floors (FLOOR_ALL)
5497 (floors tree_expr_nonnegative_p@0)
5500 (match double_value_p
5502 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5503 (for froms (BUILT_IN_TRUNCL
5515 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
5516 (if (optimize && canonicalize_math_p ())
5518 (froms (convert double_value_p@0))
5519 (convert (tos @0)))))
5521 (match float_value_p
5523 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5524 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5525 BUILT_IN_FLOORL BUILT_IN_FLOOR
5526 BUILT_IN_CEILL BUILT_IN_CEIL
5527 BUILT_IN_ROUNDL BUILT_IN_ROUND
5528 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5529 BUILT_IN_RINTL BUILT_IN_RINT)
5530 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5531 BUILT_IN_FLOORF BUILT_IN_FLOORF
5532 BUILT_IN_CEILF BUILT_IN_CEILF
5533 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5534 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5535 BUILT_IN_RINTF BUILT_IN_RINTF)
5536 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5538 (if (optimize && canonicalize_math_p ()
5539 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
5541 (froms (convert float_value_p@0))
5542 (convert (tos @0)))))
5544 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5545 tos (XFLOOR XCEIL XROUND XRINT)
5546 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
5547 (if (optimize && canonicalize_math_p ())
5549 (froms (convert double_value_p@0))
5552 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5553 XFLOOR XCEIL XROUND XRINT)
5554 tos (XFLOORF XCEILF XROUNDF XRINTF)
5555 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5557 (if (optimize && canonicalize_math_p ())
5559 (froms (convert float_value_p@0))
5562 (if (canonicalize_math_p ())
5563 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
5564 (for floors (IFLOOR LFLOOR LLFLOOR)
5566 (floors tree_expr_nonnegative_p@0)
5569 (if (canonicalize_math_p ())
5570 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
5571 (for fns (IFLOOR LFLOOR LLFLOOR
5573 IROUND LROUND LLROUND)
5575 (fns integer_valued_real_p@0)
5577 (if (!flag_errno_math)
5578 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
5579 (for rints (IRINT LRINT LLRINT)
5581 (rints integer_valued_real_p@0)
5584 (if (canonicalize_math_p ())
5585 (for ifn (IFLOOR ICEIL IROUND IRINT)
5586 lfn (LFLOOR LCEIL LROUND LRINT)
5587 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5588 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5589 sizeof (int) == sizeof (long). */
5590 (if (TYPE_PRECISION (integer_type_node)
5591 == TYPE_PRECISION (long_integer_type_node))
5594 (lfn:long_integer_type_node @0)))
5595 /* Canonicalize llround (x) to lround (x) on LP64 targets where
5596 sizeof (long long) == sizeof (long). */
5597 (if (TYPE_PRECISION (long_long_integer_type_node)
5598 == TYPE_PRECISION (long_integer_type_node))
5601 (lfn:long_integer_type_node @0)))))
5603 /* cproj(x) -> x if we're ignoring infinities. */
5606 (if (!HONOR_INFINITIES (type))
5609 /* If the real part is inf and the imag part is known to be
5610 nonnegative, return (inf + 0i). */
5612 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5613 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5614 { build_complex_inf (type, false); }))
5616 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
5618 (CPROJ (complex @0 REAL_CST@1))
5619 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5620 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5626 (pows @0 REAL_CST@1)
5628 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5629 REAL_VALUE_TYPE tmp;
5632 /* pow(x,0) -> 1. */
5633 (if (real_equal (value, &dconst0))
5634 { build_real (type, dconst1); })
5635 /* pow(x,1) -> x. */
5636 (if (real_equal (value, &dconst1))
5638 /* pow(x,-1) -> 1/x. */
5639 (if (real_equal (value, &dconstm1))
5640 (rdiv { build_real (type, dconst1); } @0))
5641 /* pow(x,0.5) -> sqrt(x). */
5642 (if (flag_unsafe_math_optimizations
5643 && canonicalize_math_p ()
5644 && real_equal (value, &dconsthalf))
5646 /* pow(x,1/3) -> cbrt(x). */
5647 (if (flag_unsafe_math_optimizations
5648 && canonicalize_math_p ()
5649 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5650 real_equal (value, &tmp)))
5653 /* powi(1,x) -> 1. */
5655 (POWI real_onep@0 @1)
5659 (POWI @0 INTEGER_CST@1)
5661 /* powi(x,0) -> 1. */
5662 (if (wi::to_wide (@1) == 0)
5663 { build_real (type, dconst1); })
5664 /* powi(x,1) -> x. */
5665 (if (wi::to_wide (@1) == 1)
5667 /* powi(x,-1) -> 1/x. */
5668 (if (wi::to_wide (@1) == -1)
5669 (rdiv { build_real (type, dconst1); } @0))))
5671 /* Narrowing of arithmetic and logical operations.
5673 These are conceptually similar to the transformations performed for
5674 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
5675 term we want to move all that code out of the front-ends into here. */
5677 /* Convert (outertype)((innertype0)a+(innertype1)b)
5678 into ((newtype)a+(newtype)b) where newtype
5679 is the widest mode from all of these. */
5680 (for op (plus minus mult rdiv)
5682 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5683 /* If we have a narrowing conversion of an arithmetic operation where
5684 both operands are widening conversions from the same type as the outer
5685 narrowing conversion. Then convert the innermost operands to a
5686 suitable unsigned type (to avoid introducing undefined behavior),
5687 perform the operation and convert the result to the desired type. */
5688 (if (INTEGRAL_TYPE_P (type)
5691 /* We check for type compatibility between @0 and @1 below,
5692 so there's no need to check that @2/@4 are integral types. */
5693 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5694 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5695 /* The precision of the type of each operand must match the
5696 precision of the mode of each operand, similarly for the
5698 && type_has_mode_precision_p (TREE_TYPE (@1))
5699 && type_has_mode_precision_p (TREE_TYPE (@2))
5700 && type_has_mode_precision_p (type)
5701 /* The inner conversion must be a widening conversion. */
5702 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5703 && types_match (@1, type)
5704 && (types_match (@1, @2)
5705 /* Or the second operand is const integer or converted const
5706 integer from valueize. */
5707 || TREE_CODE (@2) == INTEGER_CST))
5708 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5709 (op @1 (convert @2))
5710 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5711 (convert (op (convert:utype @1)
5712 (convert:utype @2)))))
5713 (if (FLOAT_TYPE_P (type)
5714 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5715 == DECIMAL_FLOAT_TYPE_P (type))
5716 (with { tree arg0 = strip_float_extensions (@1);
5717 tree arg1 = strip_float_extensions (@2);
5718 tree itype = TREE_TYPE (@0);
5719 tree ty1 = TREE_TYPE (arg0);
5720 tree ty2 = TREE_TYPE (arg1);
5721 enum tree_code code = TREE_CODE (itype); }
5722 (if (FLOAT_TYPE_P (ty1)
5723 && FLOAT_TYPE_P (ty2))
5724 (with { tree newtype = type;
5725 if (TYPE_MODE (ty1) == SDmode
5726 || TYPE_MODE (ty2) == SDmode
5727 || TYPE_MODE (type) == SDmode)
5728 newtype = dfloat32_type_node;
5729 if (TYPE_MODE (ty1) == DDmode
5730 || TYPE_MODE (ty2) == DDmode
5731 || TYPE_MODE (type) == DDmode)
5732 newtype = dfloat64_type_node;
5733 if (TYPE_MODE (ty1) == TDmode
5734 || TYPE_MODE (ty2) == TDmode
5735 || TYPE_MODE (type) == TDmode)
5736 newtype = dfloat128_type_node; }
5737 (if ((newtype == dfloat32_type_node
5738 || newtype == dfloat64_type_node
5739 || newtype == dfloat128_type_node)
5741 && types_match (newtype, type))
5742 (op (convert:newtype @1) (convert:newtype @2))
5743 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5745 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5747 /* Sometimes this transformation is safe (cannot
5748 change results through affecting double rounding
5749 cases) and sometimes it is not. If NEWTYPE is
5750 wider than TYPE, e.g. (float)((long double)double
5751 + (long double)double) converted to
5752 (float)(double + double), the transformation is
5753 unsafe regardless of the details of the types
5754 involved; double rounding can arise if the result
5755 of NEWTYPE arithmetic is a NEWTYPE value half way
5756 between two representable TYPE values but the
5757 exact value is sufficiently different (in the
5758 right direction) for this difference to be
5759 visible in ITYPE arithmetic. If NEWTYPE is the
5760 same as TYPE, however, the transformation may be
5761 safe depending on the types involved: it is safe
5762 if the ITYPE has strictly more than twice as many
5763 mantissa bits as TYPE, can represent infinities
5764 and NaNs if the TYPE can, and has sufficient
5765 exponent range for the product or ratio of two
5766 values representable in the TYPE to be within the
5767 range of normal values of ITYPE. */
5768 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5769 && (flag_unsafe_math_optimizations
5770 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5771 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5773 && !excess_precision_type (newtype)))
5774 && !types_match (itype, newtype))
5775 (convert:type (op (convert:newtype @1)
5776 (convert:newtype @2)))
5781 /* This is another case of narrowing, specifically when there's an outer
5782 BIT_AND_EXPR which masks off bits outside the type of the innermost
5783 operands. Like the previous case we have to convert the operands
5784 to unsigned types to avoid introducing undefined behavior for the
5785 arithmetic operation. */
5786 (for op (minus plus)
5788 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5789 (if (INTEGRAL_TYPE_P (type)
5790 /* We check for type compatibility between @0 and @1 below,
5791 so there's no need to check that @1/@3 are integral types. */
5792 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5793 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5794 /* The precision of the type of each operand must match the
5795 precision of the mode of each operand, similarly for the
5797 && type_has_mode_precision_p (TREE_TYPE (@0))
5798 && type_has_mode_precision_p (TREE_TYPE (@1))
5799 && type_has_mode_precision_p (type)
5800 /* The inner conversion must be a widening conversion. */
5801 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5802 && types_match (@0, @1)
5803 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5804 <= TYPE_PRECISION (TREE_TYPE (@0)))
5805 && (wi::to_wide (@4)
5806 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5807 true, TYPE_PRECISION (type))) == 0)
5808 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5809 (with { tree ntype = TREE_TYPE (@0); }
5810 (convert (bit_and (op @0 @1) (convert:ntype @4))))
5811 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5812 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5813 (convert:utype @4))))))))
5815 /* Transform (@0 < @1 and @0 < @2) to use min,
5816 (@0 > @1 and @0 > @2) to use max */
5817 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5818 op (lt le gt ge lt le gt ge )
5819 ext (min min max max max max min min )
5821 (logic (op:cs @0 @1) (op:cs @0 @2))
5822 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5823 && TREE_CODE (@0) != INTEGER_CST)
5824 (op @0 (ext @1 @2)))))
5827 /* signbit(x) -> 0 if x is nonnegative. */
5828 (SIGNBIT tree_expr_nonnegative_p@0)
5829 { integer_zero_node; })
5832 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
5834 (if (!HONOR_SIGNED_ZEROS (@0))
5835 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
5837 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
5839 (for op (plus minus)
5842 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5843 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5844 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
5845 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
5846 && !TYPE_SATURATING (TREE_TYPE (@0)))
5847 (with { tree res = int_const_binop (rop, @2, @1); }
5848 (if (TREE_OVERFLOW (res)
5849 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5850 { constant_boolean_node (cmp == NE_EXPR, type); }
5851 (if (single_use (@3))
5852 (cmp @0 { TREE_OVERFLOW (res)
5853 ? drop_tree_overflow (res) : res; }))))))))
5854 (for cmp (lt le gt ge)
5855 (for op (plus minus)
5858 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5859 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5860 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5861 (with { tree res = int_const_binop (rop, @2, @1); }
5862 (if (TREE_OVERFLOW (res))
5864 fold_overflow_warning (("assuming signed overflow does not occur "
5865 "when simplifying conditional to constant"),
5866 WARN_STRICT_OVERFLOW_CONDITIONAL);
5867 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
5868 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
5869 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
5870 TYPE_SIGN (TREE_TYPE (@1)))
5871 != (op == MINUS_EXPR);
5872 constant_boolean_node (less == ovf_high, type);
5874 (if (single_use (@3))
5877 fold_overflow_warning (("assuming signed overflow does not occur "
5878 "when changing X +- C1 cmp C2 to "
5880 WARN_STRICT_OVERFLOW_COMPARISON);
5882 (cmp @0 { res; })))))))))
5884 /* Canonicalizations of BIT_FIELD_REFs. */
5887 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5888 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5891 (BIT_FIELD_REF (view_convert @0) @1 @2)
5892 (BIT_FIELD_REF @0 @1 @2))
5895 (BIT_FIELD_REF @0 @1 integer_zerop)
5896 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5900 (BIT_FIELD_REF @0 @1 @2)
5902 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5903 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5905 (if (integer_zerop (@2))
5906 (view_convert (realpart @0)))
5907 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5908 (view_convert (imagpart @0)))))
5909 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5910 && INTEGRAL_TYPE_P (type)
5911 /* On GIMPLE this should only apply to register arguments. */
5912 && (! GIMPLE || is_gimple_reg (@0))
5913 /* A bit-field-ref that referenced the full argument can be stripped. */
5914 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5915 && integer_zerop (@2))
5916 /* Low-parts can be reduced to integral conversions.
5917 ??? The following doesn't work for PDP endian. */
5918 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5919 /* Don't even think about BITS_BIG_ENDIAN. */
5920 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5921 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5922 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5923 ? (TYPE_PRECISION (TREE_TYPE (@0))
5924 - TYPE_PRECISION (type))
5928 /* Simplify vector extracts. */
5931 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5932 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5933 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5934 || (VECTOR_TYPE_P (type)
5935 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5938 tree ctor = (TREE_CODE (@0) == SSA_NAME
5939 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5940 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5941 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5942 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5943 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5946 && (idx % width) == 0
5948 && known_le ((idx + n) / width,
5949 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5954 /* Constructor elements can be subvectors. */
5956 if (CONSTRUCTOR_NELTS (ctor) != 0)
5958 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5959 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5960 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5962 unsigned HOST_WIDE_INT elt, count, const_k;
5965 /* We keep an exact subset of the constructor elements. */
5966 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5967 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5968 { build_constructor (type, NULL); }
5970 (if (elt < CONSTRUCTOR_NELTS (ctor))
5971 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5972 { build_zero_cst (type); })
5973 /* We don't want to emit new CTORs unless the old one goes away.
5974 ??? Eventually allow this if the CTOR ends up constant or
5976 (if (single_use (@0))
5978 vec<constructor_elt, va_gc> *vals;
5979 vec_alloc (vals, count);
5980 for (unsigned i = 0;
5981 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5982 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5983 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5984 build_constructor (type, vals);
5986 /* The bitfield references a single constructor element. */
5987 (if (k.is_constant (&const_k)
5988 && idx + n <= (idx / const_k + 1) * const_k)
5990 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5991 { build_zero_cst (type); })
5993 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5994 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5995 @1 { bitsize_int ((idx % const_k) * width); })))))))))
5997 /* Simplify a bit extraction from a bit insertion for the cases with
5998 the inserted element fully covering the extraction or the insertion
5999 not touching the extraction. */
6001 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6004 unsigned HOST_WIDE_INT isize;
6005 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6006 isize = TYPE_PRECISION (TREE_TYPE (@1));
6008 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6011 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6012 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6013 wi::to_wide (@ipos) + isize))
6014 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6016 - wi::to_wide (@ipos)); }))
6017 (if (wi::geu_p (wi::to_wide (@ipos),
6018 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6019 || wi::geu_p (wi::to_wide (@rpos),
6020 wi::to_wide (@ipos) + isize))
6021 (BIT_FIELD_REF @0 @rsize @rpos)))))
6023 (if (canonicalize_math_after_vectorization_p ())
6026 (fmas:c (negate @0) @1 @2)
6027 (IFN_FNMA @0 @1 @2))
6029 (fmas @0 @1 (negate @2))
6032 (fmas:c (negate @0) @1 (negate @2))
6033 (IFN_FNMS @0 @1 @2))
6035 (negate (fmas@3 @0 @1 @2))
6036 (if (single_use (@3))
6037 (IFN_FNMS @0 @1 @2))))
6040 (IFN_FMS:c (negate @0) @1 @2)
6041 (IFN_FNMS @0 @1 @2))
6043 (IFN_FMS @0 @1 (negate @2))
6046 (IFN_FMS:c (negate @0) @1 (negate @2))
6047 (IFN_FNMA @0 @1 @2))
6049 (negate (IFN_FMS@3 @0 @1 @2))
6050 (if (single_use (@3))
6051 (IFN_FNMA @0 @1 @2)))
6054 (IFN_FNMA:c (negate @0) @1 @2)
6057 (IFN_FNMA @0 @1 (negate @2))
6058 (IFN_FNMS @0 @1 @2))
6060 (IFN_FNMA:c (negate @0) @1 (negate @2))
6063 (negate (IFN_FNMA@3 @0 @1 @2))
6064 (if (single_use (@3))
6065 (IFN_FMS @0 @1 @2)))
6068 (IFN_FNMS:c (negate @0) @1 @2)
6071 (IFN_FNMS @0 @1 (negate @2))
6072 (IFN_FNMA @0 @1 @2))
6074 (IFN_FNMS:c (negate @0) @1 (negate @2))
6077 (negate (IFN_FNMS@3 @0 @1 @2))
6078 (if (single_use (@3))
6079 (IFN_FMA @0 @1 @2))))
6081 /* POPCOUNT simplifications. */
6082 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6084 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6085 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6086 (POPCOUNT (bit_ior @0 @1))))
6088 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6089 (for popcount (POPCOUNT)
6090 (for cmp (le eq ne gt)
6093 (cmp (popcount @0) integer_zerop)
6094 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6096 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6098 (bit_and (POPCOUNT @0) integer_onep)
6101 /* PARITY simplifications. */
6102 /* parity(~X) is parity(X). */
6104 (PARITY (bit_not @0))
6107 /* parity(X)^parity(Y) is parity(X^Y). */
6109 (bit_xor (PARITY:s @0) (PARITY:s @1))
6110 (PARITY (bit_xor @0 @1)))
6112 /* Common POPCOUNT/PARITY simplifications. */
6113 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6114 (for pfun (POPCOUNT PARITY)
6117 (with { wide_int nz = tree_nonzero_bits (@0); }
6121 (if (wi::popcount (nz) == 1)
6122 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6123 (convert (rshift:utype (convert:utype @0)
6124 { build_int_cst (integer_type_node,
6125 wi::ctz (nz)); }))))))))
6128 /* 64- and 32-bits branchless implementations of popcount are detected:
6130 int popcount64c (uint64_t x)
6132 x -= (x >> 1) & 0x5555555555555555ULL;
6133 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6134 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6135 return (x * 0x0101010101010101ULL) >> 56;
6138 int popcount32c (uint32_t x)
6140 x -= (x >> 1) & 0x55555555;
6141 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6142 x = (x + (x >> 4)) & 0x0f0f0f0f;
6143 return (x * 0x01010101) >> 24;
6150 (rshift @8 INTEGER_CST@5)
6152 (bit_and @6 INTEGER_CST@7)
6156 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6162 /* Check constants and optab. */
6163 (with { unsigned prec = TYPE_PRECISION (type);
6164 int shift = (64 - prec) & 63;
6165 unsigned HOST_WIDE_INT c1
6166 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6167 unsigned HOST_WIDE_INT c2
6168 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6169 unsigned HOST_WIDE_INT c3
6170 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6171 unsigned HOST_WIDE_INT c4
6172 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6177 && TYPE_UNSIGNED (type)
6178 && integer_onep (@4)
6179 && wi::to_widest (@10) == 2
6180 && wi::to_widest (@5) == 4
6181 && wi::to_widest (@1) == prec - 8
6182 && tree_to_uhwi (@2) == c1
6183 && tree_to_uhwi (@3) == c2
6184 && tree_to_uhwi (@9) == c3
6185 && tree_to_uhwi (@7) == c3
6186 && tree_to_uhwi (@11) == c4
6187 && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6189 (convert (IFN_POPCOUNT:type @0)))))
6191 /* __builtin_ffs needs to deal on many targets with the possible zero
6192 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6193 should lead to better code. */
6195 (FFS tree_expr_nonzero_p@0)
6196 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6197 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6198 OPTIMIZE_FOR_SPEED))
6199 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6200 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6203 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6205 /* __builtin_ffs (X) == 0 -> X == 0.
6206 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6209 (cmp (ffs@2 @0) INTEGER_CST@1)
6210 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6212 (if (integer_zerop (@1))
6213 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6214 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6215 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6216 (if (single_use (@2))
6217 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6218 wi::mask (tree_to_uhwi (@1),
6220 { wide_int_to_tree (TREE_TYPE (@0),
6221 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6222 false, prec)); }))))))
6224 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
6228 bit_op (bit_and bit_ior)
6230 (cmp (ffs@2 @0) INTEGER_CST@1)
6231 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6233 (if (integer_zerop (@1))
6234 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6235 (if (tree_int_cst_sgn (@1) < 0)
6236 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6237 (if (wi::to_widest (@1) >= prec)
6238 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6239 (if (wi::to_widest (@1) == prec - 1)
6240 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
6241 wi::shifted_mask (prec - 1, 1,
6243 (if (single_use (@2))
6244 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
6246 { wide_int_to_tree (TREE_TYPE (@0),
6247 wi::mask (tree_to_uhwi (@1),
6249 { build_zero_cst (TREE_TYPE (@0)); }))))))))
6258 r = c ? a1 op a2 : b;
6260 if the target can do it in one go. This makes the operation conditional
6261 on c, so could drop potentially-trapping arithmetic, but that's a valid
6262 simplification if the result of the operation isn't needed.
6264 Avoid speculatively generating a stand-alone vector comparison
6265 on targets that might not support them. Any target implementing
6266 conditional internal functions must support the same comparisons
6267 inside and outside a VEC_COND_EXPR. */
6270 (for uncond_op (UNCOND_BINARY)
6271 cond_op (COND_BINARY)
6273 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
6274 (with { tree op_type = TREE_TYPE (@4); }
6275 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6276 && element_precision (type) == element_precision (op_type))
6277 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
6279 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
6280 (with { tree op_type = TREE_TYPE (@4); }
6281 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6282 && element_precision (type) == element_precision (op_type))
6283 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6285 /* Same for ternary operations. */
6286 (for uncond_op (UNCOND_TERNARY)
6287 cond_op (COND_TERNARY)
6289 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
6290 (with { tree op_type = TREE_TYPE (@5); }
6291 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6292 && element_precision (type) == element_precision (op_type))
6293 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
6295 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
6296 (with { tree op_type = TREE_TYPE (@5); }
6297 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6298 && element_precision (type) == element_precision (op_type))
6299 (view_convert (cond_op (bit_not @0) @2 @3 @4
6300 (view_convert:op_type @1)))))))
6303 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
6304 "else" value of an IFN_COND_*. */
6305 (for cond_op (COND_BINARY)
6307 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
6308 (with { tree op_type = TREE_TYPE (@3); }
6309 (if (element_precision (type) == element_precision (op_type))
6310 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
6312 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
6313 (with { tree op_type = TREE_TYPE (@5); }
6314 (if (inverse_conditions_p (@0, @2)
6315 && element_precision (type) == element_precision (op_type))
6316 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
6318 /* Same for ternary operations. */
6319 (for cond_op (COND_TERNARY)
6321 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
6322 (with { tree op_type = TREE_TYPE (@4); }
6323 (if (element_precision (type) == element_precision (op_type))
6324 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
6326 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
6327 (with { tree op_type = TREE_TYPE (@6); }
6328 (if (inverse_conditions_p (@0, @2)
6329 && element_precision (type) == element_precision (op_type))
6330 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
6332 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
6335 A: (@0 + @1 < @2) | (@2 + @1 < @0)
6336 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
6338 If pointers are known not to wrap, B checks whether @1 bytes starting
6339 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
6340 bytes. A is more efficiently tested as:
6342 A: (sizetype) (@0 + @1 - @2) > @1 * 2
6344 The equivalent expression for B is given by replacing @1 with @1 - 1:
6346 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
6348 @0 and @2 can be swapped in both expressions without changing the result.
6350 The folds rely on sizetype's being unsigned (which is always true)
6351 and on its being the same width as the pointer (which we have to check).
6353 The fold replaces two pointer_plus expressions, two comparisons and
6354 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
6355 the best case it's a saving of two operations. The A fold retains one
6356 of the original pointer_pluses, so is a win even if both pointer_pluses
6357 are used elsewhere. The B fold is a wash if both pointer_pluses are
6358 used elsewhere, since all we end up doing is replacing a comparison with
6359 a pointer_plus. We do still apply the fold under those circumstances
6360 though, in case applying it to other conditions eventually makes one of the
6361 pointer_pluses dead. */
6362 (for ior (truth_orif truth_or bit_ior)
6365 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
6366 (cmp:cs (pointer_plus@4 @2 @1) @0))
6367 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6368 && TYPE_OVERFLOW_WRAPS (sizetype)
6369 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
6370 /* Calculate the rhs constant. */
6371 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
6372 offset_int rhs = off * 2; }
6373 /* Always fails for negative values. */
6374 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
6375 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
6376 pick a canonical order. This increases the chances of using the
6377 same pointer_plus in multiple checks. */
6378 (with { bool swap_p = tree_swap_operands_p (@0, @2);
6379 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
6380 (if (cmp == LT_EXPR)
6381 (gt (convert:sizetype
6382 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
6383 { swap_p ? @0 : @2; }))
6385 (gt (convert:sizetype
6386 (pointer_diff:ssizetype
6387 (pointer_plus { swap_p ? @2 : @0; }
6388 { wide_int_to_tree (sizetype, off); })
6389 { swap_p ? @0 : @2; }))
6390 { rhs_tree; })))))))))
6392 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6394 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6395 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6396 (with { int i = single_nonzero_element (@1); }
6398 (with { tree elt = vector_cst_elt (@1, i);
6399 tree elt_type = TREE_TYPE (elt);
6400 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6401 tree size = bitsize_int (elt_bits);
6402 tree pos = bitsize_int (elt_bits * i); }
6405 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6409 (vec_perm @0 @1 VECTOR_CST@2)
6412 tree op0 = @0, op1 = @1, op2 = @2;
6414 /* Build a vector of integers from the tree mask. */
6415 vec_perm_builder builder;
6416 if (!tree_to_vec_perm_builder (&builder, op2))
6419 /* Create a vec_perm_indices for the integer vector. */
6420 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6421 bool single_arg = (op0 == op1);
6422 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6424 (if (sel.series_p (0, 1, 0, 1))
6426 (if (sel.series_p (0, 1, nelts, 1))
6432 if (sel.all_from_input_p (0))
6434 else if (sel.all_from_input_p (1))
6437 sel.rotate_inputs (1);
6439 else if (known_ge (poly_uint64 (sel[0]), nelts))
6441 std::swap (op0, op1);
6442 sel.rotate_inputs (1);
6446 tree cop0 = op0, cop1 = op1;
6447 if (TREE_CODE (op0) == SSA_NAME
6448 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6449 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6450 cop0 = gimple_assign_rhs1 (def);
6451 if (TREE_CODE (op1) == SSA_NAME
6452 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6453 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6454 cop1 = gimple_assign_rhs1 (def);
6458 (if ((TREE_CODE (cop0) == VECTOR_CST
6459 || TREE_CODE (cop0) == CONSTRUCTOR)
6460 && (TREE_CODE (cop1) == VECTOR_CST
6461 || TREE_CODE (cop1) == CONSTRUCTOR)
6462 && (t = fold_vec_perm (type, cop0, cop1, sel)))
6466 bool changed = (op0 == op1 && !single_arg);
6467 tree ins = NULL_TREE;
6470 /* See if the permutation is performing a single element
6471 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6472 in that case. But only if the vector mode is supported,
6473 otherwise this is invalid GIMPLE. */
6474 if (TYPE_MODE (type) != BLKmode
6475 && (TREE_CODE (cop0) == VECTOR_CST
6476 || TREE_CODE (cop0) == CONSTRUCTOR
6477 || TREE_CODE (cop1) == VECTOR_CST
6478 || TREE_CODE (cop1) == CONSTRUCTOR))
6480 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6483 /* After canonicalizing the first elt to come from the
6484 first vector we only can insert the first elt from
6485 the first vector. */
6487 if ((ins = fold_read_from_vector (cop0, sel[0])))
6490 /* The above can fail for two-element vectors which always
6491 appear to insert the first element, so try inserting
6492 into the second lane as well. For more than two
6493 elements that's wasted time. */
6494 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6496 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6497 for (at = 0; at < encoded_nelts; ++at)
6498 if (maybe_ne (sel[at], at))
6500 if (at < encoded_nelts
6501 && (known_eq (at + 1, nelts)
6502 || sel.series_p (at + 1, 1, at + 1, 1)))
6504 if (known_lt (poly_uint64 (sel[at]), nelts))
6505 ins = fold_read_from_vector (cop0, sel[at]);
6507 ins = fold_read_from_vector (cop1, sel[at] - nelts);
6512 /* Generate a canonical form of the selector. */
6513 if (!ins && sel.encoding () != builder)
6515 /* Some targets are deficient and fail to expand a single
6516 argument permutation while still allowing an equivalent
6517 2-argument version. */
6519 if (sel.ninputs () == 2
6520 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6521 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6524 vec_perm_indices sel2 (builder, 2, nelts);
6525 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6526 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6528 /* Not directly supported with either encoding,
6529 so use the preferred form. */
6530 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6532 if (!operand_equal_p (op2, oldop2, 0))
6537 (bit_insert { op0; } { ins; }
6538 { bitsize_int (at * vector_element_bits (type)); })
6540 (vec_perm { op0; } { op1; } { op2; }))))))))))
6542 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
6544 (match vec_same_elem_p
6546 (if (uniform_vector_p (@0))))
6548 (match vec_same_elem_p
6552 (vec_perm vec_same_elem_p@0 @0 @1)
6555 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6556 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6557 constant which when multiplied by a power of 2 contains a unique value
6558 in the top 5 or 6 bits. This is then indexed into a table which maps it
6559 to the number of trailing zeroes. */
6560 (match (ctz_table_index @1 @2 @3)
6561 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))