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 Also optimize "A shift (B % C)", if C is a power of 2, to
612 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
613 and assume (B % C) is nonnegative as shifts negative values would
615 (match (power_of_two_cand @1)
617 (match (power_of_two_cand @1)
618 (lshift INTEGER_CST@1 @2))
619 (for mod (trunc_mod floor_mod)
620 (for shift (lshift rshift)
622 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
623 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
624 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
627 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
628 (if ((TYPE_UNSIGNED (type)
629 || tree_expr_nonnegative_p (@0))
630 && tree_nop_conversion_p (type, TREE_TYPE (@3))
631 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
632 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
634 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
636 (trunc_div (mult @0 integer_pow2p@1) @1)
637 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
638 (bit_and @0 { wide_int_to_tree
639 (type, wi::mask (TYPE_PRECISION (type)
640 - wi::exact_log2 (wi::to_wide (@1)),
641 false, TYPE_PRECISION (type))); })))
643 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
645 (mult (trunc_div @0 integer_pow2p@1) @1)
646 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
647 (bit_and @0 (negate @1))))
649 /* Simplify (t * 2) / 2) -> t. */
650 (for div (trunc_div ceil_div floor_div round_div exact_div)
652 (div (mult:c @0 @1) @1)
653 (if (ANY_INTEGRAL_TYPE_P (type)
654 && TYPE_OVERFLOW_UNDEFINED (type))
658 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
663 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
666 (pows (op @0) REAL_CST@1)
667 (with { HOST_WIDE_INT n; }
668 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
670 /* Likewise for powi. */
673 (pows (op @0) INTEGER_CST@1)
674 (if ((wi::to_wide (@1) & 1) == 0)
676 /* Strip negate and abs from both operands of hypot. */
684 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
685 (for copysigns (COPYSIGN_ALL)
687 (copysigns (op @0) @1)
690 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
695 /* Convert absu(x)*absu(x) -> x*x. */
697 (mult (absu@1 @0) @1)
698 (mult (convert@2 @0) @2))
700 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
704 (coss (copysigns @0 @1))
707 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
711 (pows (copysigns @0 @2) REAL_CST@1)
712 (with { HOST_WIDE_INT n; }
713 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
715 /* Likewise for powi. */
719 (pows (copysigns @0 @2) INTEGER_CST@1)
720 (if ((wi::to_wide (@1) & 1) == 0)
725 /* hypot(copysign(x, y), z) -> hypot(x, z). */
727 (hypots (copysigns @0 @1) @2)
729 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
731 (hypots @0 (copysigns @1 @2))
734 /* copysign(x, CST) -> [-]abs (x). */
735 (for copysigns (COPYSIGN_ALL)
737 (copysigns @0 REAL_CST@1)
738 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
742 /* copysign(copysign(x, y), z) -> copysign(x, z). */
743 (for copysigns (COPYSIGN_ALL)
745 (copysigns (copysigns @0 @1) @2)
748 /* copysign(x,y)*copysign(x,y) -> x*x. */
749 (for copysigns (COPYSIGN_ALL)
751 (mult (copysigns@2 @0 @1) @2)
754 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
755 (for ccoss (CCOS CCOSH)
760 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
761 (for ops (conj negate)
767 /* Fold (a * (1 << b)) into (a << b) */
769 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
770 (if (! FLOAT_TYPE_P (type)
771 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
774 /* Fold (1 << (C - x)) where C = precision(type) - 1
775 into ((1 << C) >> x). */
777 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
778 (if (INTEGRAL_TYPE_P (type)
779 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
781 (if (TYPE_UNSIGNED (type))
782 (rshift (lshift @0 @2) @3)
784 { tree utype = unsigned_type_for (type); }
785 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
787 /* Fold (C1/X)*C2 into (C1*C2)/X. */
789 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
790 (if (flag_associative_math
793 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
795 (rdiv { tem; } @1)))))
797 /* Simplify ~X & X as zero. */
799 (bit_and:c (convert? @0) (convert? (bit_not @0)))
800 { build_zero_cst (type); })
802 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
804 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
805 (if (TYPE_UNSIGNED (type))
806 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
808 (for bitop (bit_and bit_ior)
810 /* PR35691: Transform
811 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
812 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
814 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
815 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
816 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
817 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
818 (cmp (bit_ior @0 (convert @1)) @2)))
820 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
821 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
823 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
824 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
825 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
826 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
827 (cmp (bit_and @0 (convert @1)) @2))))
829 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
831 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
832 (minus (bit_xor @0 @1) @1))
834 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
835 (if (~wi::to_wide (@2) == wi::to_wide (@1))
836 (minus (bit_xor @0 @1) @1)))
838 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
840 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
841 (minus @1 (bit_xor @0 @1)))
843 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
844 (for op (bit_ior bit_xor plus)
846 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
849 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
850 (if (~wi::to_wide (@2) == wi::to_wide (@1))
853 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
855 (bit_ior:c (bit_xor:c @0 @1) @0)
858 /* (a & ~b) | (a ^ b) --> a ^ b */
860 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
863 /* (a & ~b) ^ ~a --> ~(a & b) */
865 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
866 (bit_not (bit_and @0 @1)))
868 /* (~a & b) ^ a --> (a | b) */
870 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
873 /* (a | b) & ~(a ^ b) --> a & b */
875 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
878 /* a | ~(a ^ b) --> a | ~b */
880 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
881 (bit_ior @0 (bit_not @1)))
883 /* (a | b) | (a &^ b) --> a | b */
884 (for op (bit_and bit_xor)
886 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
889 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
891 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
894 /* ~(~a & b) --> a | ~b */
896 (bit_not (bit_and:cs (bit_not @0) @1))
897 (bit_ior @0 (bit_not @1)))
899 /* ~(~a | b) --> a & ~b */
901 (bit_not (bit_ior:cs (bit_not @0) @1))
902 (bit_and @0 (bit_not @1)))
904 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
907 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
908 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
909 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
913 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
914 ((A & N) + B) & M -> (A + B) & M
915 Similarly if (N & M) == 0,
916 ((A | N) + B) & M -> (A + B) & M
917 and for - instead of + (or unary - instead of +)
918 and/or ^ instead of |.
919 If B is constant and (B & M) == 0, fold into A & M. */
921 (for bitop (bit_and bit_ior bit_xor)
923 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
926 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
927 @3, @4, @1, ERROR_MARK, NULL_TREE,
930 (convert (bit_and (op (convert:utype { pmop[0]; })
931 (convert:utype { pmop[1]; }))
932 (convert:utype @2))))))
934 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
937 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
938 NULL_TREE, NULL_TREE, @1, bitop, @3,
941 (convert (bit_and (op (convert:utype { pmop[0]; })
942 (convert:utype { pmop[1]; }))
943 (convert:utype @2)))))))
945 (bit_and (op:s @0 @1) INTEGER_CST@2)
948 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
949 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
950 NULL_TREE, NULL_TREE, pmop); }
952 (convert (bit_and (op (convert:utype { pmop[0]; })
953 (convert:utype { pmop[1]; }))
954 (convert:utype @2)))))))
955 (for bitop (bit_and bit_ior bit_xor)
957 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
960 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
961 bitop, @2, @3, NULL_TREE, ERROR_MARK,
962 NULL_TREE, NULL_TREE, pmop); }
964 (convert (bit_and (negate (convert:utype { pmop[0]; }))
965 (convert:utype @1)))))))
967 /* X % Y is smaller than Y. */
970 (cmp (trunc_mod @0 @1) @1)
971 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
972 { constant_boolean_node (cmp == LT_EXPR, type); })))
975 (cmp @1 (trunc_mod @0 @1))
976 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
977 { constant_boolean_node (cmp == GT_EXPR, type); })))
981 (bit_ior @0 integer_all_onesp@1)
986 (bit_ior @0 integer_zerop)
991 (bit_and @0 integer_zerop@1)
997 (for op (bit_ior bit_xor plus)
999 (op:c (convert? @0) (convert? (bit_not @0)))
1000 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1005 { build_zero_cst (type); })
1007 /* Canonicalize X ^ ~0 to ~X. */
1009 (bit_xor @0 integer_all_onesp@1)
1014 (bit_and @0 integer_all_onesp)
1017 /* x & x -> x, x | x -> x */
1018 (for bitop (bit_and bit_ior)
1023 /* x & C -> x if we know that x & ~C == 0. */
1026 (bit_and SSA_NAME@0 INTEGER_CST@1)
1027 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1028 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1032 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1034 (bit_not (minus (bit_not @0) @1))
1037 (bit_not (plus:c (bit_not @0) @1))
1040 /* x + (x & 1) -> (x + 1) & ~1 */
1042 (plus:c @0 (bit_and:s @0 integer_onep@1))
1043 (bit_and (plus @0 @1) (bit_not @1)))
1045 /* x & ~(x & y) -> x & ~y */
1046 /* x | ~(x | y) -> x | ~y */
1047 (for bitop (bit_and bit_ior)
1049 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1050 (bitop @0 (bit_not @1))))
1052 /* (~x & y) | ~(x | y) -> ~x */
1054 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1057 /* (x | y) ^ (x | ~y) -> ~x */
1059 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1062 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1064 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1065 (bit_not (bit_xor @0 @1)))
1067 /* (~x | y) ^ (x ^ y) -> x | ~y */
1069 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1070 (bit_ior @0 (bit_not @1)))
1072 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1074 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1075 (bit_not (bit_and @0 @1)))
1077 /* (x | y) & ~x -> y & ~x */
1078 /* (x & y) | ~x -> y | ~x */
1079 (for bitop (bit_and bit_ior)
1080 rbitop (bit_ior bit_and)
1082 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1085 /* (x & y) ^ (x | y) -> x ^ y */
1087 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1090 /* (x ^ y) ^ (x | y) -> x & y */
1092 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1095 /* (x & y) + (x ^ y) -> x | y */
1096 /* (x & y) | (x ^ y) -> x | y */
1097 /* (x & y) ^ (x ^ y) -> x | y */
1098 (for op (plus bit_ior bit_xor)
1100 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1103 /* (x & y) + (x | y) -> x + y */
1105 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1108 /* (x + y) - (x | y) -> x & y */
1110 (minus (plus @0 @1) (bit_ior @0 @1))
1111 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1112 && !TYPE_SATURATING (type))
1115 /* (x + y) - (x & y) -> x | y */
1117 (minus (plus @0 @1) (bit_and @0 @1))
1118 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1119 && !TYPE_SATURATING (type))
1122 /* (x | y) - y -> (x & ~y) */
1124 (minus (bit_ior:cs @0 @1) @1)
1125 (bit_and @0 (bit_not @1)))
1127 /* (x | y) - (x ^ y) -> x & y */
1129 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1132 /* (x | y) - (x & y) -> x ^ y */
1134 (minus (bit_ior @0 @1) (bit_and @0 @1))
1137 /* (x | y) & ~(x & y) -> x ^ y */
1139 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1142 /* (x | y) & (~x ^ y) -> x & y */
1144 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1147 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1149 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1150 (bit_not (bit_xor @0 @1)))
1152 /* (~x | y) ^ (x | ~y) -> x ^ y */
1154 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1157 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1159 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1160 (nop_convert2? (bit_ior @0 @1))))
1162 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1163 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1164 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1165 && !TYPE_SATURATING (TREE_TYPE (@2)))
1166 (bit_not (convert (bit_xor @0 @1)))))
1168 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1170 (nop_convert3? (bit_ior @0 @1)))
1171 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1172 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1173 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1174 && !TYPE_SATURATING (TREE_TYPE (@2)))
1175 (bit_not (convert (bit_xor @0 @1)))))
1177 (minus (nop_convert1? (bit_and @0 @1))
1178 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1180 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1181 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1182 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1183 && !TYPE_SATURATING (TREE_TYPE (@2)))
1184 (bit_not (convert (bit_xor @0 @1)))))
1186 /* ~x & ~y -> ~(x | y)
1187 ~x | ~y -> ~(x & y) */
1188 (for op (bit_and bit_ior)
1189 rop (bit_ior bit_and)
1191 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1192 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1193 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1194 (bit_not (rop (convert @0) (convert @1))))))
1196 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1197 with a constant, and the two constants have no bits in common,
1198 we should treat this as a BIT_IOR_EXPR since this may produce more
1200 (for op (bit_xor plus)
1202 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1203 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1204 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1205 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1206 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1207 (bit_ior (convert @4) (convert @5)))))
1209 /* (X | Y) ^ X -> Y & ~ X*/
1211 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1212 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1213 (convert (bit_and @1 (bit_not @0)))))
1215 /* Convert ~X ^ ~Y to X ^ Y. */
1217 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1218 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1219 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1220 (bit_xor (convert @0) (convert @1))))
1222 /* Convert ~X ^ C to X ^ ~C. */
1224 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1225 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1226 (bit_xor (convert @0) (bit_not @1))))
1228 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1229 (for opo (bit_and bit_xor)
1230 opi (bit_xor bit_and)
1232 (opo:c (opi:cs @0 @1) @1)
1233 (bit_and (bit_not @0) @1)))
1235 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1236 operands are another bit-wise operation with a common input. If so,
1237 distribute the bit operations to save an operation and possibly two if
1238 constants are involved. For example, convert
1239 (A | B) & (A | C) into A | (B & C)
1240 Further simplification will occur if B and C are constants. */
1241 (for op (bit_and bit_ior bit_xor)
1242 rop (bit_ior bit_and bit_and)
1244 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1245 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1246 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1247 (rop (convert @0) (op (convert @1) (convert @2))))))
1249 /* Some simple reassociation for bit operations, also handled in reassoc. */
1250 /* (X & Y) & Y -> X & Y
1251 (X | Y) | Y -> X | Y */
1252 (for op (bit_and bit_ior)
1254 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1256 /* (X ^ Y) ^ Y -> X */
1258 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1260 /* (X & Y) & (X & Z) -> (X & Y) & Z
1261 (X | Y) | (X | Z) -> (X | Y) | Z */
1262 (for op (bit_and bit_ior)
1264 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1265 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1266 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1267 (if (single_use (@5) && single_use (@6))
1268 (op @3 (convert @2))
1269 (if (single_use (@3) && single_use (@4))
1270 (op (convert @1) @5))))))
1271 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1273 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1274 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1275 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1276 (bit_xor (convert @1) (convert @2))))
1278 /* Convert abs (abs (X)) into abs (X).
1279 also absu (absu (X)) into absu (X). */
1285 (absu (convert@2 (absu@1 @0)))
1286 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1289 /* Convert abs[u] (-X) -> abs[u] (X). */
1298 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1300 (abs tree_expr_nonnegative_p@0)
1304 (absu tree_expr_nonnegative_p@0)
1307 /* A few cases of fold-const.c negate_expr_p predicate. */
1308 (match negate_expr_p
1310 (if ((INTEGRAL_TYPE_P (type)
1311 && TYPE_UNSIGNED (type))
1312 || (!TYPE_OVERFLOW_SANITIZED (type)
1313 && may_negate_without_overflow_p (t)))))
1314 (match negate_expr_p
1316 (match negate_expr_p
1318 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1319 (match negate_expr_p
1321 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1322 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1324 (match negate_expr_p
1326 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1327 (match negate_expr_p
1329 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1330 || (FLOAT_TYPE_P (type)
1331 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1332 && !HONOR_SIGNED_ZEROS (type)))))
1334 /* (-A) * (-B) -> A * B */
1336 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1337 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1338 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1339 (mult (convert @0) (convert (negate @1)))))
1341 /* -(A + B) -> (-B) - A. */
1343 (negate (plus:c @0 negate_expr_p@1))
1344 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1345 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1346 (minus (negate @1) @0)))
1348 /* -(A - B) -> B - A. */
1350 (negate (minus @0 @1))
1351 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1352 || (FLOAT_TYPE_P (type)
1353 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1354 && !HONOR_SIGNED_ZEROS (type)))
1357 (negate (pointer_diff @0 @1))
1358 (if (TYPE_OVERFLOW_UNDEFINED (type))
1359 (pointer_diff @1 @0)))
1361 /* A - B -> A + (-B) if B is easily negatable. */
1363 (minus @0 negate_expr_p@1)
1364 (if (!FIXED_POINT_TYPE_P (type))
1365 (plus @0 (negate @1))))
1367 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1369 For bitwise binary operations apply operand conversions to the
1370 binary operation result instead of to the operands. This allows
1371 to combine successive conversions and bitwise binary operations.
1372 We combine the above two cases by using a conditional convert. */
1373 (for bitop (bit_and bit_ior bit_xor)
1375 (bitop (convert@2 @0) (convert?@3 @1))
1376 (if (((TREE_CODE (@1) == INTEGER_CST
1377 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1378 && int_fits_type_p (@1, TREE_TYPE (@0)))
1379 || types_match (@0, @1))
1380 /* ??? This transform conflicts with fold-const.c doing
1381 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1382 constants (if x has signed type, the sign bit cannot be set
1383 in c). This folds extension into the BIT_AND_EXPR.
1384 Restrict it to GIMPLE to avoid endless recursions. */
1385 && (bitop != BIT_AND_EXPR || GIMPLE)
1386 && (/* That's a good idea if the conversion widens the operand, thus
1387 after hoisting the conversion the operation will be narrower. */
1388 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1389 /* It's also a good idea if the conversion is to a non-integer
1391 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1392 /* Or if the precision of TO is not the same as the precision
1394 || !type_has_mode_precision_p (type)
1395 /* In GIMPLE, getting rid of 2 conversions for one new results
1398 && TREE_CODE (@1) != INTEGER_CST
1399 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1401 && single_use (@3))))
1402 (convert (bitop @0 (convert @1)))))
1403 /* In GIMPLE, getting rid of 2 conversions for one new results
1406 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1408 && TREE_CODE (@1) != INTEGER_CST
1409 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1410 && types_match (type, @0))
1411 (bitop @0 (convert @1)))))
1413 (for bitop (bit_and bit_ior)
1414 rbitop (bit_ior bit_and)
1415 /* (x | y) & x -> x */
1416 /* (x & y) | x -> x */
1418 (bitop:c (rbitop:c @0 @1) @0)
1420 /* (~x | y) & x -> x & y */
1421 /* (~x & y) | x -> x | y */
1423 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1426 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1428 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1429 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1431 /* Combine successive equal operations with constants. */
1432 (for bitop (bit_and bit_ior bit_xor)
1434 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1435 (if (!CONSTANT_CLASS_P (@0))
1436 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1437 folded to a constant. */
1438 (bitop @0 (bitop @1 @2))
1439 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1440 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1441 the values involved are such that the operation can't be decided at
1442 compile time. Try folding one of @0 or @1 with @2 to see whether
1443 that combination can be decided at compile time.
1445 Keep the existing form if both folds fail, to avoid endless
1447 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1449 (bitop @1 { cst1; })
1450 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1452 (bitop @0 { cst2; }))))))))
1454 /* Try simple folding for X op !X, and X op X with the help
1455 of the truth_valued_p and logical_inverted_value predicates. */
1456 (match truth_valued_p
1458 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1459 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1460 (match truth_valued_p
1462 (match truth_valued_p
1465 (match (logical_inverted_value @0)
1467 (match (logical_inverted_value @0)
1468 (bit_not truth_valued_p@0))
1469 (match (logical_inverted_value @0)
1470 (eq @0 integer_zerop))
1471 (match (logical_inverted_value @0)
1472 (ne truth_valued_p@0 integer_truep))
1473 (match (logical_inverted_value @0)
1474 (bit_xor truth_valued_p@0 integer_truep))
1478 (bit_and:c @0 (logical_inverted_value @0))
1479 { build_zero_cst (type); })
1480 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1481 (for op (bit_ior bit_xor)
1483 (op:c truth_valued_p@0 (logical_inverted_value @0))
1484 { constant_boolean_node (true, type); }))
1485 /* X ==/!= !X is false/true. */
1488 (op:c truth_valued_p@0 (logical_inverted_value @0))
1489 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1493 (bit_not (bit_not @0))
1496 /* Convert ~ (-A) to A - 1. */
1498 (bit_not (convert? (negate @0)))
1499 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1500 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1501 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1503 /* Convert - (~A) to A + 1. */
1505 (negate (nop_convert? (bit_not @0)))
1506 (plus (view_convert @0) { build_each_one_cst (type); }))
1508 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1510 (bit_not (convert? (minus @0 integer_each_onep)))
1511 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1512 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1513 (convert (negate @0))))
1515 (bit_not (convert? (plus @0 integer_all_onesp)))
1516 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1517 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1518 (convert (negate @0))))
1520 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1522 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1523 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1524 (convert (bit_xor @0 (bit_not @1)))))
1526 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1527 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1528 (convert (bit_xor @0 @1))))
1530 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1532 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1533 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1534 (bit_not (bit_xor (view_convert @0) @1))))
1536 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1538 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1539 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1541 /* Fold A - (A & B) into ~B & A. */
1543 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1544 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1545 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1546 (convert (bit_and (bit_not @1) @0))))
1548 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1549 (for cmp (gt lt ge le)
1551 (mult (convert (cmp @0 @1)) @2)
1552 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1553 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1555 /* For integral types with undefined overflow and C != 0 fold
1556 x * C EQ/NE y * C into x EQ/NE y. */
1559 (cmp (mult:c @0 @1) (mult:c @2 @1))
1560 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1561 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1562 && tree_expr_nonzero_p (@1))
1565 /* For integral types with wrapping overflow and C odd fold
1566 x * C EQ/NE y * C into x EQ/NE y. */
1569 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1570 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1571 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1572 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1575 /* For integral types with undefined overflow and C != 0 fold
1576 x * C RELOP y * C into:
1578 x RELOP y for nonnegative C
1579 y RELOP x for negative C */
1580 (for cmp (lt gt le ge)
1582 (cmp (mult:c @0 @1) (mult:c @2 @1))
1583 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1584 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1585 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1587 (if (TREE_CODE (@1) == INTEGER_CST
1588 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1591 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1595 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1596 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1597 && TYPE_UNSIGNED (TREE_TYPE (@0))
1598 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1599 && (wi::to_wide (@2)
1600 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1601 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1602 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1604 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1605 (for cmp (simple_comparison)
1607 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1608 (if (element_precision (@3) >= element_precision (@0)
1609 && types_match (@0, @1))
1610 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1611 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1613 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1616 tree utype = unsigned_type_for (TREE_TYPE (@0));
1618 (cmp (convert:utype @1) (convert:utype @0)))))
1619 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1620 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1624 tree utype = unsigned_type_for (TREE_TYPE (@0));
1626 (cmp (convert:utype @0) (convert:utype @1)))))))))
1628 /* X / C1 op C2 into a simple range test. */
1629 (for cmp (simple_comparison)
1631 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1632 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1633 && integer_nonzerop (@1)
1634 && !TREE_OVERFLOW (@1)
1635 && !TREE_OVERFLOW (@2))
1636 (with { tree lo, hi; bool neg_overflow;
1637 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1640 (if (code == LT_EXPR || code == GE_EXPR)
1641 (if (TREE_OVERFLOW (lo))
1642 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1643 (if (code == LT_EXPR)
1646 (if (code == LE_EXPR || code == GT_EXPR)
1647 (if (TREE_OVERFLOW (hi))
1648 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1649 (if (code == LE_EXPR)
1653 { build_int_cst (type, code == NE_EXPR); })
1654 (if (code == EQ_EXPR && !hi)
1656 (if (code == EQ_EXPR && !lo)
1658 (if (code == NE_EXPR && !hi)
1660 (if (code == NE_EXPR && !lo)
1663 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1667 tree etype = range_check_type (TREE_TYPE (@0));
1670 hi = fold_convert (etype, hi);
1671 lo = fold_convert (etype, lo);
1672 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1675 (if (etype && hi && !TREE_OVERFLOW (hi))
1676 (if (code == EQ_EXPR)
1677 (le (minus (convert:etype @0) { lo; }) { hi; })
1678 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1680 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1681 (for op (lt le ge gt)
1683 (op (plus:c @0 @2) (plus:c @1 @2))
1684 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1685 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1687 /* For equality and subtraction, this is also true with wrapping overflow. */
1688 (for op (eq ne minus)
1690 (op (plus:c @0 @2) (plus:c @1 @2))
1691 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1692 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1693 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1696 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1697 (for op (lt le ge gt)
1699 (op (minus @0 @2) (minus @1 @2))
1700 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1701 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1703 /* For equality and subtraction, this is also true with wrapping overflow. */
1704 (for op (eq ne minus)
1706 (op (minus @0 @2) (minus @1 @2))
1707 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1708 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1709 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1711 /* And for pointers... */
1712 (for op (simple_comparison)
1714 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1715 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1718 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1719 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1720 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1721 (pointer_diff @0 @1)))
1723 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1724 (for op (lt le ge gt)
1726 (op (minus @2 @0) (minus @2 @1))
1727 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1728 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1730 /* For equality and subtraction, this is also true with wrapping overflow. */
1731 (for op (eq ne minus)
1733 (op (minus @2 @0) (minus @2 @1))
1734 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1735 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1736 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1738 /* And for pointers... */
1739 (for op (simple_comparison)
1741 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1742 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1745 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1746 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1747 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1748 (pointer_diff @1 @0)))
1750 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1751 (for op (lt le gt ge)
1753 (op:c (plus:c@2 @0 @1) @1)
1754 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1755 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1756 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1757 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1758 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1759 /* For equality, this is also true with wrapping overflow. */
1762 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1763 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1764 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1765 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1766 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1767 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1768 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1769 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1771 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1772 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1773 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1774 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1775 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1777 /* X - Y < X is the same as Y > 0 when there is no overflow.
1778 For equality, this is also true with wrapping overflow. */
1779 (for op (simple_comparison)
1781 (op:c @0 (minus@2 @0 @1))
1782 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1783 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1784 || ((op == EQ_EXPR || op == NE_EXPR)
1785 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1786 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1787 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1790 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1791 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1795 (cmp (trunc_div @0 @1) integer_zerop)
1796 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1797 /* Complex ==/!= is allowed, but not </>=. */
1798 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1799 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1802 /* X == C - X can never be true if C is odd. */
1805 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1806 (if (TREE_INT_CST_LOW (@1) & 1)
1807 { constant_boolean_node (cmp == NE_EXPR, type); })))
1809 /* Arguments on which one can call get_nonzero_bits to get the bits
1811 (match with_possible_nonzero_bits
1813 (match with_possible_nonzero_bits
1815 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1816 /* Slightly extended version, do not make it recursive to keep it cheap. */
1817 (match (with_possible_nonzero_bits2 @0)
1818 with_possible_nonzero_bits@0)
1819 (match (with_possible_nonzero_bits2 @0)
1820 (bit_and:c with_possible_nonzero_bits@0 @2))
1822 /* Same for bits that are known to be set, but we do not have
1823 an equivalent to get_nonzero_bits yet. */
1824 (match (with_certain_nonzero_bits2 @0)
1826 (match (with_certain_nonzero_bits2 @0)
1827 (bit_ior @1 INTEGER_CST@0))
1829 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1832 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1833 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1834 { constant_boolean_node (cmp == NE_EXPR, type); })))
1836 /* ((X inner_op C0) outer_op C1)
1837 With X being a tree where value_range has reasoned certain bits to always be
1838 zero throughout its computed value range,
1839 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1840 where zero_mask has 1's for all bits that are sure to be 0 in
1842 if (inner_op == '^') C0 &= ~C1;
1843 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1844 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1846 (for inner_op (bit_ior bit_xor)
1847 outer_op (bit_xor bit_ior)
1850 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1854 wide_int zero_mask_not;
1858 if (TREE_CODE (@2) == SSA_NAME)
1859 zero_mask_not = get_nonzero_bits (@2);
1863 if (inner_op == BIT_XOR_EXPR)
1865 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1866 cst_emit = C0 | wi::to_wide (@1);
1870 C0 = wi::to_wide (@0);
1871 cst_emit = C0 ^ wi::to_wide (@1);
1874 (if (!fail && (C0 & zero_mask_not) == 0)
1875 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1876 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1877 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1879 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1881 (pointer_plus (pointer_plus:s @0 @1) @3)
1882 (pointer_plus @0 (plus @1 @3)))
1888 tem4 = (unsigned long) tem3;
1893 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1894 /* Conditionally look through a sign-changing conversion. */
1895 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1896 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1897 || (GENERIC && type == TREE_TYPE (@1))))
1900 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1901 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1905 tem = (sizetype) ptr;
1909 and produce the simpler and easier to analyze with respect to alignment
1910 ... = ptr & ~algn; */
1912 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1913 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1914 (bit_and @0 { algn; })))
1916 /* Try folding difference of addresses. */
1918 (minus (convert ADDR_EXPR@0) (convert @1))
1919 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1920 (with { poly_int64 diff; }
1921 (if (ptr_difference_const (@0, @1, &diff))
1922 { build_int_cst_type (type, diff); }))))
1924 (minus (convert @0) (convert ADDR_EXPR@1))
1925 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1926 (with { poly_int64 diff; }
1927 (if (ptr_difference_const (@0, @1, &diff))
1928 { build_int_cst_type (type, diff); }))))
1930 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1931 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1932 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1933 (with { poly_int64 diff; }
1934 (if (ptr_difference_const (@0, @1, &diff))
1935 { build_int_cst_type (type, diff); }))))
1937 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1938 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1939 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1940 (with { poly_int64 diff; }
1941 (if (ptr_difference_const (@0, @1, &diff))
1942 { build_int_cst_type (type, diff); }))))
1944 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
1946 (convert (pointer_diff @0 INTEGER_CST@1))
1947 (if (POINTER_TYPE_P (type))
1948 { build_fold_addr_expr_with_type
1949 (build2 (MEM_REF, char_type_node, @0,
1950 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
1953 /* If arg0 is derived from the address of an object or function, we may
1954 be able to fold this expression using the object or function's
1957 (bit_and (convert? @0) INTEGER_CST@1)
1958 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1959 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1963 unsigned HOST_WIDE_INT bitpos;
1964 get_pointer_alignment_1 (@0, &align, &bitpos);
1966 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1967 { wide_int_to_tree (type, (wi::to_wide (@1)
1968 & (bitpos / BITS_PER_UNIT))); }))))
1972 (if (INTEGRAL_TYPE_P (type)
1973 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
1977 (if (INTEGRAL_TYPE_P (type)
1978 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
1980 /* x > y && x != XXX_MIN --> x > y
1981 x > y && x == XXX_MIN --> false . */
1984 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
1986 (if (eqne == EQ_EXPR)
1987 { constant_boolean_node (false, type); })
1988 (if (eqne == NE_EXPR)
1992 /* x < y && x != XXX_MAX --> x < y
1993 x < y && x == XXX_MAX --> false. */
1996 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
1998 (if (eqne == EQ_EXPR)
1999 { constant_boolean_node (false, type); })
2000 (if (eqne == NE_EXPR)
2004 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2006 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2009 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2011 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2014 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2016 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2019 /* x <= y || x != XXX_MIN --> true. */
2021 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2022 { constant_boolean_node (true, type); })
2024 /* x <= y || x == XXX_MIN --> x <= y. */
2026 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2029 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2031 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2034 /* x >= y || x != XXX_MAX --> true
2035 x >= y || x == XXX_MAX --> x >= y. */
2038 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2040 (if (eqne == EQ_EXPR)
2042 (if (eqne == NE_EXPR)
2043 { constant_boolean_node (true, type); }))))
2045 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2046 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2049 (for code2 (eq ne lt gt le ge)
2051 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2054 int cmp = tree_int_cst_compare (@1, @2);
2058 case EQ_EXPR: val = (cmp == 0); break;
2059 case NE_EXPR: val = (cmp != 0); break;
2060 case LT_EXPR: val = (cmp < 0); break;
2061 case GT_EXPR: val = (cmp > 0); break;
2062 case LE_EXPR: val = (cmp <= 0); break;
2063 case GE_EXPR: val = (cmp >= 0); break;
2064 default: gcc_unreachable ();
2068 (if (code1 == EQ_EXPR && val) @3)
2069 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2070 (if (code1 == NE_EXPR && !val) @4))))))
2072 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2074 (for code1 (lt le gt ge)
2075 (for code2 (lt le gt ge)
2077 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2080 int cmp = tree_int_cst_compare (@1, @2);
2083 /* Choose the more restrictive of two < or <= comparisons. */
2084 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2085 && (code2 == LT_EXPR || code2 == LE_EXPR))
2086 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2089 /* Likewise chose the more restrictive of two > or >= comparisons. */
2090 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2091 && (code2 == GT_EXPR || code2 == GE_EXPR))
2092 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2095 /* Check for singleton ranges. */
2097 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2098 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2100 /* Check for disjoint ranges. */
2102 && (code1 == LT_EXPR || code1 == LE_EXPR)
2103 && (code2 == GT_EXPR || code2 == GE_EXPR))
2104 { constant_boolean_node (false, type); })
2106 && (code1 == GT_EXPR || code1 == GE_EXPR)
2107 && (code2 == LT_EXPR || code2 == LE_EXPR))
2108 { constant_boolean_node (false, type); })
2111 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2112 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2115 (for code2 (eq ne lt gt le ge)
2117 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2120 int cmp = tree_int_cst_compare (@1, @2);
2124 case EQ_EXPR: val = (cmp == 0); break;
2125 case NE_EXPR: val = (cmp != 0); break;
2126 case LT_EXPR: val = (cmp < 0); break;
2127 case GT_EXPR: val = (cmp > 0); break;
2128 case LE_EXPR: val = (cmp <= 0); break;
2129 case GE_EXPR: val = (cmp >= 0); break;
2130 default: gcc_unreachable ();
2134 (if (code1 == EQ_EXPR && val) @4)
2135 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2136 (if (code1 == NE_EXPR && !val) @3))))))
2138 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2140 (for code1 (lt le gt ge)
2141 (for code2 (lt le gt ge)
2143 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2146 int cmp = tree_int_cst_compare (@1, @2);
2149 /* Choose the more restrictive of two < or <= comparisons. */
2150 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2151 && (code2 == LT_EXPR || code2 == LE_EXPR))
2152 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2155 /* Likewise chose the more restrictive of two > or >= comparisons. */
2156 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2157 && (code2 == GT_EXPR || code2 == GE_EXPR))
2158 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2161 /* Check for singleton ranges. */
2163 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2164 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2166 /* Check for disjoint ranges. */
2168 && (code1 == LT_EXPR || code1 == LE_EXPR)
2169 && (code2 == GT_EXPR || code2 == GE_EXPR))
2170 { constant_boolean_node (true, type); })
2172 && (code1 == GT_EXPR || code1 == GE_EXPR)
2173 && (code2 == LT_EXPR || code2 == LE_EXPR))
2174 { constant_boolean_node (true, type); })
2177 /* We can't reassociate at all for saturating types. */
2178 (if (!TYPE_SATURATING (type))
2180 /* Contract negates. */
2181 /* A + (-B) -> A - B */
2183 (plus:c @0 (convert? (negate @1)))
2184 /* Apply STRIP_NOPS on the negate. */
2185 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2186 && !TYPE_OVERFLOW_SANITIZED (type))
2190 if (INTEGRAL_TYPE_P (type)
2191 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2192 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2194 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2195 /* A - (-B) -> A + B */
2197 (minus @0 (convert? (negate @1)))
2198 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2199 && !TYPE_OVERFLOW_SANITIZED (type))
2203 if (INTEGRAL_TYPE_P (type)
2204 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2205 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2207 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2209 Sign-extension is ok except for INT_MIN, which thankfully cannot
2210 happen without overflow. */
2212 (negate (convert (negate @1)))
2213 (if (INTEGRAL_TYPE_P (type)
2214 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2215 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2216 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2217 && !TYPE_OVERFLOW_SANITIZED (type)
2218 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2221 (negate (convert negate_expr_p@1))
2222 (if (SCALAR_FLOAT_TYPE_P (type)
2223 && ((DECIMAL_FLOAT_TYPE_P (type)
2224 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2225 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2226 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2227 (convert (negate @1))))
2229 (negate (nop_convert? (negate @1)))
2230 (if (!TYPE_OVERFLOW_SANITIZED (type)
2231 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2234 /* We can't reassociate floating-point unless -fassociative-math
2235 or fixed-point plus or minus because of saturation to +-Inf. */
2236 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2237 && !FIXED_POINT_TYPE_P (type))
2239 /* Match patterns that allow contracting a plus-minus pair
2240 irrespective of overflow issues. */
2241 /* (A +- B) - A -> +- B */
2242 /* (A +- B) -+ B -> A */
2243 /* A - (A +- B) -> -+ B */
2244 /* A +- (B -+ A) -> +- B */
2246 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2249 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2250 (if (!ANY_INTEGRAL_TYPE_P (type)
2251 || TYPE_OVERFLOW_WRAPS (type))
2252 (negate (view_convert @1))
2253 (view_convert (negate @1))))
2255 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2258 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2259 (if (!ANY_INTEGRAL_TYPE_P (type)
2260 || TYPE_OVERFLOW_WRAPS (type))
2261 (negate (view_convert @1))
2262 (view_convert (negate @1))))
2264 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2266 /* (A +- B) + (C - A) -> C +- B */
2267 /* (A + B) - (A - C) -> B + C */
2268 /* More cases are handled with comparisons. */
2270 (plus:c (plus:c @0 @1) (minus @2 @0))
2273 (plus:c (minus @0 @1) (minus @2 @0))
2276 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2277 (if (TYPE_OVERFLOW_UNDEFINED (type)
2278 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2279 (pointer_diff @2 @1)))
2281 (minus (plus:c @0 @1) (minus @0 @2))
2284 /* (A +- CST1) +- CST2 -> A + CST3
2285 Use view_convert because it is safe for vectors and equivalent for
2287 (for outer_op (plus minus)
2288 (for inner_op (plus minus)
2289 neg_inner_op (minus plus)
2291 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2293 /* If one of the types wraps, use that one. */
2294 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2295 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2296 forever if something doesn't simplify into a constant. */
2297 (if (!CONSTANT_CLASS_P (@0))
2298 (if (outer_op == PLUS_EXPR)
2299 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2300 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2301 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2302 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2303 (if (outer_op == PLUS_EXPR)
2304 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2305 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2306 /* If the constant operation overflows we cannot do the transform
2307 directly as we would introduce undefined overflow, for example
2308 with (a - 1) + INT_MIN. */
2309 (if (types_match (type, @0))
2310 (with { tree cst = const_binop (outer_op == inner_op
2311 ? PLUS_EXPR : MINUS_EXPR,
2313 (if (cst && !TREE_OVERFLOW (cst))
2314 (inner_op @0 { cst; } )
2315 /* X+INT_MAX+1 is X-INT_MIN. */
2316 (if (INTEGRAL_TYPE_P (type) && cst
2317 && wi::to_wide (cst) == wi::min_value (type))
2318 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2319 /* Last resort, use some unsigned type. */
2320 (with { tree utype = unsigned_type_for (type); }
2322 (view_convert (inner_op
2323 (view_convert:utype @0)
2325 { drop_tree_overflow (cst); }))))))))))))))
2327 /* (CST1 - A) +- CST2 -> CST3 - A */
2328 (for outer_op (plus minus)
2330 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2331 /* If one of the types wraps, use that one. */
2332 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2333 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2334 forever if something doesn't simplify into a constant. */
2335 (if (!CONSTANT_CLASS_P (@0))
2336 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2337 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2338 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2339 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2340 (if (types_match (type, @0))
2341 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2342 (if (cst && !TREE_OVERFLOW (cst))
2343 (minus { cst; } @0))))))))
2345 /* CST1 - (CST2 - A) -> CST3 + A
2346 Use view_convert because it is safe for vectors and equivalent for
2349 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2350 /* If one of the types wraps, use that one. */
2351 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2352 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2353 forever if something doesn't simplify into a constant. */
2354 (if (!CONSTANT_CLASS_P (@0))
2355 (plus (view_convert @0) (minus @1 (view_convert @2))))
2356 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2357 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2358 (view_convert (plus @0 (minus (view_convert @1) @2)))
2359 (if (types_match (type, @0))
2360 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2361 (if (cst && !TREE_OVERFLOW (cst))
2362 (plus { cst; } @0)))))))
2364 /* ((T)(A)) + CST -> (T)(A + CST) */
2367 (plus (convert SSA_NAME@0) INTEGER_CST@1)
2368 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2369 && TREE_CODE (type) == INTEGER_TYPE
2370 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2371 && int_fits_type_p (@1, TREE_TYPE (@0)))
2372 /* Perform binary operation inside the cast if the constant fits
2373 and (A + CST)'s range does not overflow. */
2376 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2377 max_ovf = wi::OVF_OVERFLOW;
2378 tree inner_type = TREE_TYPE (@0);
2381 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2382 TYPE_SIGN (inner_type));
2384 wide_int wmin0, wmax0;
2385 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2387 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2388 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2391 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2392 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2396 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2398 (for op (plus minus)
2400 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2401 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2402 && TREE_CODE (type) == INTEGER_TYPE
2403 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2404 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2405 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2406 && TYPE_OVERFLOW_WRAPS (type))
2407 (plus (convert @0) (op @2 (convert @1))))))
2410 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2411 to a simple value. */
2413 (for op (plus minus)
2415 (op (convert @0) (convert @1))
2416 (if (INTEGRAL_TYPE_P (type)
2417 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2418 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2419 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2420 && !TYPE_OVERFLOW_TRAPS (type)
2421 && !TYPE_OVERFLOW_SANITIZED (type))
2422 (convert (op! @0 @1)))))
2427 (plus:c (bit_not @0) @0)
2428 (if (!TYPE_OVERFLOW_TRAPS (type))
2429 { build_all_ones_cst (type); }))
2433 (plus (convert? (bit_not @0)) integer_each_onep)
2434 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2435 (negate (convert @0))))
2439 (minus (convert? (negate @0)) integer_each_onep)
2440 (if (!TYPE_OVERFLOW_TRAPS (type)
2441 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2442 (bit_not (convert @0))))
2446 (minus integer_all_onesp @0)
2449 /* (T)(P + A) - (T)P -> (T) A */
2451 (minus (convert (plus:c @@0 @1))
2453 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2454 /* For integer types, if A has a smaller type
2455 than T the result depends on the possible
2457 E.g. T=size_t, A=(unsigned)429497295, P>0.
2458 However, if an overflow in P + A would cause
2459 undefined behavior, we can assume that there
2461 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2462 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2465 (minus (convert (pointer_plus @@0 @1))
2467 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2468 /* For pointer types, if the conversion of A to the
2469 final type requires a sign- or zero-extension,
2470 then we have to punt - it is not defined which
2472 || (POINTER_TYPE_P (TREE_TYPE (@0))
2473 && TREE_CODE (@1) == INTEGER_CST
2474 && tree_int_cst_sign_bit (@1) == 0))
2477 (pointer_diff (pointer_plus @@0 @1) @0)
2478 /* The second argument of pointer_plus must be interpreted as signed, and
2479 thus sign-extended if necessary. */
2480 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2481 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2482 second arg is unsigned even when we need to consider it as signed,
2483 we don't want to diagnose overflow here. */
2484 (convert (view_convert:stype @1))))
2486 /* (T)P - (T)(P + A) -> -(T) A */
2488 (minus (convert? @0)
2489 (convert (plus:c @@0 @1)))
2490 (if (INTEGRAL_TYPE_P (type)
2491 && TYPE_OVERFLOW_UNDEFINED (type)
2492 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2493 (with { tree utype = unsigned_type_for (type); }
2494 (convert (negate (convert:utype @1))))
2495 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2496 /* For integer types, if A has a smaller type
2497 than T the result depends on the possible
2499 E.g. T=size_t, A=(unsigned)429497295, P>0.
2500 However, if an overflow in P + A would cause
2501 undefined behavior, we can assume that there
2503 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2504 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2505 (negate (convert @1)))))
2508 (convert (pointer_plus @@0 @1)))
2509 (if (INTEGRAL_TYPE_P (type)
2510 && TYPE_OVERFLOW_UNDEFINED (type)
2511 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2512 (with { tree utype = unsigned_type_for (type); }
2513 (convert (negate (convert:utype @1))))
2514 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2515 /* For pointer types, if the conversion of A to the
2516 final type requires a sign- or zero-extension,
2517 then we have to punt - it is not defined which
2519 || (POINTER_TYPE_P (TREE_TYPE (@0))
2520 && TREE_CODE (@1) == INTEGER_CST
2521 && tree_int_cst_sign_bit (@1) == 0))
2522 (negate (convert @1)))))
2524 (pointer_diff @0 (pointer_plus @@0 @1))
2525 /* The second argument of pointer_plus must be interpreted as signed, and
2526 thus sign-extended if necessary. */
2527 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2528 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2529 second arg is unsigned even when we need to consider it as signed,
2530 we don't want to diagnose overflow here. */
2531 (negate (convert (view_convert:stype @1)))))
2533 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2535 (minus (convert (plus:c @@0 @1))
2536 (convert (plus:c @0 @2)))
2537 (if (INTEGRAL_TYPE_P (type)
2538 && TYPE_OVERFLOW_UNDEFINED (type)
2539 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2540 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2541 (with { tree utype = unsigned_type_for (type); }
2542 (convert (minus (convert:utype @1) (convert:utype @2))))
2543 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2544 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2545 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2546 /* For integer types, if A has a smaller type
2547 than T the result depends on the possible
2549 E.g. T=size_t, A=(unsigned)429497295, P>0.
2550 However, if an overflow in P + A would cause
2551 undefined behavior, we can assume that there
2553 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2554 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2555 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2556 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2557 (minus (convert @1) (convert @2)))))
2559 (minus (convert (pointer_plus @@0 @1))
2560 (convert (pointer_plus @0 @2)))
2561 (if (INTEGRAL_TYPE_P (type)
2562 && TYPE_OVERFLOW_UNDEFINED (type)
2563 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2564 (with { tree utype = unsigned_type_for (type); }
2565 (convert (minus (convert:utype @1) (convert:utype @2))))
2566 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2567 /* For pointer types, if the conversion of A to the
2568 final type requires a sign- or zero-extension,
2569 then we have to punt - it is not defined which
2571 || (POINTER_TYPE_P (TREE_TYPE (@0))
2572 && TREE_CODE (@1) == INTEGER_CST
2573 && tree_int_cst_sign_bit (@1) == 0
2574 && TREE_CODE (@2) == INTEGER_CST
2575 && tree_int_cst_sign_bit (@2) == 0))
2576 (minus (convert @1) (convert @2)))))
2578 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2579 (pointer_diff @0 @1))
2581 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2582 /* The second argument of pointer_plus must be interpreted as signed, and
2583 thus sign-extended if necessary. */
2584 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2585 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2586 second arg is unsigned even when we need to consider it as signed,
2587 we don't want to diagnose overflow here. */
2588 (minus (convert (view_convert:stype @1))
2589 (convert (view_convert:stype @2)))))))
2591 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2592 Modeled after fold_plusminus_mult_expr. */
2593 (if (!TYPE_SATURATING (type)
2594 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2595 (for plusminus (plus minus)
2597 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2598 (if (!ANY_INTEGRAL_TYPE_P (type)
2599 || TYPE_OVERFLOW_WRAPS (type)
2600 || (INTEGRAL_TYPE_P (type)
2601 && tree_expr_nonzero_p (@0)
2602 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2603 (if (single_use (@3) || single_use (@4))
2604 /* If @1 +- @2 is constant require a hard single-use on either
2605 original operand (but not on both). */
2606 (mult (plusminus @1 @2) @0)
2608 (mult! (plusminus @1 @2) @0)
2611 /* We cannot generate constant 1 for fract. */
2612 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2614 (plusminus @0 (mult:c@3 @0 @2))
2615 (if ((!ANY_INTEGRAL_TYPE_P (type)
2616 || TYPE_OVERFLOW_WRAPS (type)
2617 /* For @0 + @0*@2 this transformation would introduce UB
2618 (where there was none before) for @0 in [-1,0] and @2 max.
2619 For @0 - @0*@2 this transformation would introduce UB
2620 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2621 || (INTEGRAL_TYPE_P (type)
2622 && ((tree_expr_nonzero_p (@0)
2623 && expr_not_equal_to (@0,
2624 wi::minus_one (TYPE_PRECISION (type))))
2625 || (plusminus == PLUS_EXPR
2626 ? expr_not_equal_to (@2,
2627 wi::max_value (TYPE_PRECISION (type), SIGNED))
2628 /* Let's ignore the @0 -1 and @2 min case. */
2629 : (expr_not_equal_to (@2,
2630 wi::min_value (TYPE_PRECISION (type), SIGNED))
2631 && expr_not_equal_to (@2,
2632 wi::min_value (TYPE_PRECISION (type), SIGNED)
2635 (mult (plusminus { build_one_cst (type); } @2) @0)))
2637 (plusminus (mult:c@3 @0 @2) @0)
2638 (if ((!ANY_INTEGRAL_TYPE_P (type)
2639 || TYPE_OVERFLOW_WRAPS (type)
2640 /* For @0*@2 + @0 this transformation would introduce UB
2641 (where there was none before) for @0 in [-1,0] and @2 max.
2642 For @0*@2 - @0 this transformation would introduce UB
2643 for @0 0 and @2 min. */
2644 || (INTEGRAL_TYPE_P (type)
2645 && ((tree_expr_nonzero_p (@0)
2646 && (plusminus == MINUS_EXPR
2647 || expr_not_equal_to (@0,
2648 wi::minus_one (TYPE_PRECISION (type)))))
2649 || expr_not_equal_to (@2,
2650 (plusminus == PLUS_EXPR
2651 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2652 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2654 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2657 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2658 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2660 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2661 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2662 && tree_fits_uhwi_p (@1)
2663 && tree_to_uhwi (@1) < element_precision (type))
2664 (with { tree t = type;
2665 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2666 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2667 element_precision (type));
2669 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2671 cst = build_uniform_cst (t, cst); }
2672 (convert (mult (convert:t @0) { cst; })))))
2674 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2675 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2676 && tree_fits_uhwi_p (@1)
2677 && tree_to_uhwi (@1) < element_precision (type)
2678 && tree_fits_uhwi_p (@2)
2679 && tree_to_uhwi (@2) < element_precision (type))
2680 (with { tree t = type;
2681 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2682 unsigned int prec = element_precision (type);
2683 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2684 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2685 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2687 cst = build_uniform_cst (t, cst); }
2688 (convert (mult (convert:t @0) { cst; })))))
2691 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2693 (for minmax (min max FMIN_ALL FMAX_ALL)
2697 /* min(max(x,y),y) -> y. */
2699 (min:c (max:c @0 @1) @1)
2701 /* max(min(x,y),y) -> y. */
2703 (max:c (min:c @0 @1) @1)
2705 /* max(a,-a) -> abs(a). */
2707 (max:c @0 (negate @0))
2708 (if (TREE_CODE (type) != COMPLEX_TYPE
2709 && (! ANY_INTEGRAL_TYPE_P (type)
2710 || TYPE_OVERFLOW_UNDEFINED (type)))
2712 /* min(a,-a) -> -abs(a). */
2714 (min:c @0 (negate @0))
2715 (if (TREE_CODE (type) != COMPLEX_TYPE
2716 && (! ANY_INTEGRAL_TYPE_P (type)
2717 || TYPE_OVERFLOW_UNDEFINED (type)))
2722 (if (INTEGRAL_TYPE_P (type)
2723 && TYPE_MIN_VALUE (type)
2724 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2726 (if (INTEGRAL_TYPE_P (type)
2727 && TYPE_MAX_VALUE (type)
2728 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2733 (if (INTEGRAL_TYPE_P (type)
2734 && TYPE_MAX_VALUE (type)
2735 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2737 (if (INTEGRAL_TYPE_P (type)
2738 && TYPE_MIN_VALUE (type)
2739 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2742 /* max (a, a + CST) -> a + CST where CST is positive. */
2743 /* max (a, a + CST) -> a where CST is negative. */
2745 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2746 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2747 (if (tree_int_cst_sgn (@1) > 0)
2751 /* min (a, a + CST) -> a where CST is positive. */
2752 /* min (a, a + CST) -> a + CST where CST is negative. */
2754 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2755 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2756 (if (tree_int_cst_sgn (@1) > 0)
2760 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2761 and the outer convert demotes the expression back to x's type. */
2762 (for minmax (min max)
2764 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2765 (if (INTEGRAL_TYPE_P (type)
2766 && types_match (@1, type) && int_fits_type_p (@2, type)
2767 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2768 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2769 (minmax @1 (convert @2)))))
2771 (for minmax (FMIN_ALL FMAX_ALL)
2772 /* If either argument is NaN, return the other one. Avoid the
2773 transformation if we get (and honor) a signalling NaN. */
2775 (minmax:c @0 REAL_CST@1)
2776 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2777 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2779 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2780 functions to return the numeric arg if the other one is NaN.
2781 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2782 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2783 worry about it either. */
2784 (if (flag_finite_math_only)
2791 /* min (-A, -B) -> -max (A, B) */
2792 (for minmax (min max FMIN_ALL FMAX_ALL)
2793 maxmin (max min FMAX_ALL FMIN_ALL)
2795 (minmax (negate:s@2 @0) (negate:s@3 @1))
2796 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2797 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2798 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2799 (negate (maxmin @0 @1)))))
2800 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2801 MAX (~X, ~Y) -> ~MIN (X, Y) */
2802 (for minmax (min max)
2805 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2806 (bit_not (maxmin @0 @1))))
2808 /* MIN (X, Y) == X -> X <= Y */
2809 (for minmax (min min max max)
2813 (cmp:c (minmax:c @0 @1) @0)
2814 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2816 /* MIN (X, 5) == 0 -> X == 0
2817 MIN (X, 5) == 7 -> false */
2820 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2821 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2822 TYPE_SIGN (TREE_TYPE (@0))))
2823 { constant_boolean_node (cmp == NE_EXPR, type); }
2824 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2825 TYPE_SIGN (TREE_TYPE (@0))))
2829 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2830 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2831 TYPE_SIGN (TREE_TYPE (@0))))
2832 { constant_boolean_node (cmp == NE_EXPR, type); }
2833 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2834 TYPE_SIGN (TREE_TYPE (@0))))
2836 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2837 (for minmax (min min max max min min max max )
2838 cmp (lt le gt ge gt ge lt le )
2839 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2841 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2842 (comb (cmp @0 @2) (cmp @1 @2))))
2844 /* Undo fancy way of writing max/min or other ?: expressions,
2845 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2846 People normally use ?: and that is what we actually try to optimize. */
2847 (for cmp (simple_comparison)
2849 (minus @0 (bit_and:c (minus @0 @1)
2850 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2851 (if (INTEGRAL_TYPE_P (type)
2852 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2853 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2854 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2855 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2856 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2857 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2858 (cond (cmp @2 @3) @1 @0)))
2860 (plus:c @0 (bit_and:c (minus @1 @0)
2861 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2862 (if (INTEGRAL_TYPE_P (type)
2863 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2864 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2865 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2866 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2867 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2868 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2869 (cond (cmp @2 @3) @1 @0)))
2870 /* Similarly with ^ instead of - though in that case with :c. */
2872 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
2873 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2874 (if (INTEGRAL_TYPE_P (type)
2875 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2876 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2877 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2878 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2879 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2880 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2881 (cond (cmp @2 @3) @1 @0))))
2883 /* Simplifications of shift and rotates. */
2885 (for rotate (lrotate rrotate)
2887 (rotate integer_all_onesp@0 @1)
2890 /* Optimize -1 >> x for arithmetic right shifts. */
2892 (rshift integer_all_onesp@0 @1)
2893 (if (!TYPE_UNSIGNED (type)
2894 && tree_expr_nonnegative_p (@1))
2897 /* Optimize (x >> c) << c into x & (-1<<c). */
2899 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
2900 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2901 /* It doesn't matter if the right shift is arithmetic or logical. */
2902 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
2905 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
2906 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
2907 /* Allow intermediate conversion to integral type with whatever sign, as
2908 long as the low TYPE_PRECISION (type)
2909 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
2910 && INTEGRAL_TYPE_P (type)
2911 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2912 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2913 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
2914 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
2915 || wi::geu_p (wi::to_wide (@1),
2916 TYPE_PRECISION (type)
2917 - TYPE_PRECISION (TREE_TYPE (@2)))))
2918 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
2920 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2923 (rshift (lshift @0 INTEGER_CST@1) @1)
2924 (if (TYPE_UNSIGNED (type)
2925 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2926 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2928 /* Optimize x >> x into 0 */
2931 { build_zero_cst (type); })
2933 (for shiftrotate (lrotate rrotate lshift rshift)
2935 (shiftrotate @0 integer_zerop)
2938 (shiftrotate integer_zerop@0 @1)
2940 /* Prefer vector1 << scalar to vector1 << vector2
2941 if vector2 is uniform. */
2942 (for vec (VECTOR_CST CONSTRUCTOR)
2944 (shiftrotate @0 vec@1)
2945 (with { tree tem = uniform_vector_p (@1); }
2947 (shiftrotate @0 { tem; }))))))
2949 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2950 Y is 0. Similarly for X >> Y. */
2952 (for shift (lshift rshift)
2954 (shift @0 SSA_NAME@1)
2955 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2957 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2958 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2960 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2964 /* Rewrite an LROTATE_EXPR by a constant into an
2965 RROTATE_EXPR by a new constant. */
2967 (lrotate @0 INTEGER_CST@1)
2968 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2969 build_int_cst (TREE_TYPE (@1),
2970 element_precision (type)), @1); }))
2972 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2973 (for op (lrotate rrotate rshift lshift)
2975 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2976 (with { unsigned int prec = element_precision (type); }
2977 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2978 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2979 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2980 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2981 (with { unsigned int low = (tree_to_uhwi (@1)
2982 + tree_to_uhwi (@2)); }
2983 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2984 being well defined. */
2986 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2987 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2988 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2989 { build_zero_cst (type); }
2990 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2991 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2994 /* ((1 << A) & 1) != 0 -> A == 0
2995 ((1 << A) & 1) == 0 -> A != 0 */
2999 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
3000 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
3002 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3003 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3007 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3008 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3010 || (!integer_zerop (@2)
3011 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3012 { constant_boolean_node (cmp == NE_EXPR, type); }
3013 (if (!integer_zerop (@2)
3014 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3015 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3017 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3018 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3019 if the new mask might be further optimized. */
3020 (for shift (lshift rshift)
3022 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3024 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3025 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3026 && tree_fits_uhwi_p (@1)
3027 && tree_to_uhwi (@1) > 0
3028 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3031 unsigned int shiftc = tree_to_uhwi (@1);
3032 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3033 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3034 tree shift_type = TREE_TYPE (@3);
3037 if (shift == LSHIFT_EXPR)
3038 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3039 else if (shift == RSHIFT_EXPR
3040 && type_has_mode_precision_p (shift_type))
3042 prec = TYPE_PRECISION (TREE_TYPE (@3));
3044 /* See if more bits can be proven as zero because of
3047 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3049 tree inner_type = TREE_TYPE (@0);
3050 if (type_has_mode_precision_p (inner_type)
3051 && TYPE_PRECISION (inner_type) < prec)
3053 prec = TYPE_PRECISION (inner_type);
3054 /* See if we can shorten the right shift. */
3056 shift_type = inner_type;
3057 /* Otherwise X >> C1 is all zeros, so we'll optimize
3058 it into (X, 0) later on by making sure zerobits
3062 zerobits = HOST_WIDE_INT_M1U;
3065 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3066 zerobits <<= prec - shiftc;
3068 /* For arithmetic shift if sign bit could be set, zerobits
3069 can contain actually sign bits, so no transformation is
3070 possible, unless MASK masks them all away. In that
3071 case the shift needs to be converted into logical shift. */
3072 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3073 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3075 if ((mask & zerobits) == 0)
3076 shift_type = unsigned_type_for (TREE_TYPE (@3));
3082 /* ((X << 16) & 0xff00) is (X, 0). */
3083 (if ((mask & zerobits) == mask)
3084 { build_int_cst (type, 0); }
3085 (with { newmask = mask | zerobits; }
3086 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3089 /* Only do the transformation if NEWMASK is some integer
3091 for (prec = BITS_PER_UNIT;
3092 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3093 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3096 (if (prec < HOST_BITS_PER_WIDE_INT
3097 || newmask == HOST_WIDE_INT_M1U)
3099 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3100 (if (!tree_int_cst_equal (newmaskt, @2))
3101 (if (shift_type != TREE_TYPE (@3))
3102 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3103 (bit_and @4 { newmaskt; })))))))))))))
3105 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3106 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3107 (for shift (lshift rshift)
3108 (for bit_op (bit_and bit_xor bit_ior)
3110 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3111 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3112 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3113 (bit_op (shift (convert @0) @1) { mask; }))))))
3115 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3117 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3118 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3119 && (element_precision (TREE_TYPE (@0))
3120 <= element_precision (TREE_TYPE (@1))
3121 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3123 { tree shift_type = TREE_TYPE (@0); }
3124 (convert (rshift (convert:shift_type @1) @2)))))
3126 /* ~(~X >>r Y) -> X >>r Y
3127 ~(~X <<r Y) -> X <<r Y */
3128 (for rotate (lrotate rrotate)
3130 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3131 (if ((element_precision (TREE_TYPE (@0))
3132 <= element_precision (TREE_TYPE (@1))
3133 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3134 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3135 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3137 { tree rotate_type = TREE_TYPE (@0); }
3138 (convert (rotate (convert:rotate_type @1) @2))))))
3140 /* Simplifications of conversions. */
3142 /* Basic strip-useless-type-conversions / strip_nops. */
3143 (for cvt (convert view_convert float fix_trunc)
3146 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3147 || (GENERIC && type == TREE_TYPE (@0)))
3150 /* Contract view-conversions. */
3152 (view_convert (view_convert @0))
3155 /* For integral conversions with the same precision or pointer
3156 conversions use a NOP_EXPR instead. */
3159 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3160 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3161 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3164 /* Strip inner integral conversions that do not change precision or size, or
3165 zero-extend while keeping the same size (for bool-to-char). */
3167 (view_convert (convert@0 @1))
3168 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3169 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3170 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3171 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3172 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3173 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3176 /* Simplify a view-converted empty constructor. */
3178 (view_convert CONSTRUCTOR@0)
3179 (if (TREE_CODE (@0) != SSA_NAME
3180 && CONSTRUCTOR_NELTS (@0) == 0)
3181 { build_zero_cst (type); }))
3183 /* Re-association barriers around constants and other re-association
3184 barriers can be removed. */
3186 (paren CONSTANT_CLASS_P@0)
3189 (paren (paren@1 @0))
3192 /* Handle cases of two conversions in a row. */
3193 (for ocvt (convert float fix_trunc)
3194 (for icvt (convert float)
3199 tree inside_type = TREE_TYPE (@0);
3200 tree inter_type = TREE_TYPE (@1);
3201 int inside_int = INTEGRAL_TYPE_P (inside_type);
3202 int inside_ptr = POINTER_TYPE_P (inside_type);
3203 int inside_float = FLOAT_TYPE_P (inside_type);
3204 int inside_vec = VECTOR_TYPE_P (inside_type);
3205 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3206 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3207 int inter_int = INTEGRAL_TYPE_P (inter_type);
3208 int inter_ptr = POINTER_TYPE_P (inter_type);
3209 int inter_float = FLOAT_TYPE_P (inter_type);
3210 int inter_vec = VECTOR_TYPE_P (inter_type);
3211 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3212 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3213 int final_int = INTEGRAL_TYPE_P (type);
3214 int final_ptr = POINTER_TYPE_P (type);
3215 int final_float = FLOAT_TYPE_P (type);
3216 int final_vec = VECTOR_TYPE_P (type);
3217 unsigned int final_prec = TYPE_PRECISION (type);
3218 int final_unsignedp = TYPE_UNSIGNED (type);
3221 /* In addition to the cases of two conversions in a row
3222 handled below, if we are converting something to its own
3223 type via an object of identical or wider precision, neither
3224 conversion is needed. */
3225 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3227 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3228 && (((inter_int || inter_ptr) && final_int)
3229 || (inter_float && final_float))
3230 && inter_prec >= final_prec)
3233 /* Likewise, if the intermediate and initial types are either both
3234 float or both integer, we don't need the middle conversion if the
3235 former is wider than the latter and doesn't change the signedness
3236 (for integers). Avoid this if the final type is a pointer since
3237 then we sometimes need the middle conversion. */
3238 (if (((inter_int && inside_int) || (inter_float && inside_float))
3239 && (final_int || final_float)
3240 && inter_prec >= inside_prec
3241 && (inter_float || inter_unsignedp == inside_unsignedp))
3244 /* If we have a sign-extension of a zero-extended value, we can
3245 replace that by a single zero-extension. Likewise if the
3246 final conversion does not change precision we can drop the
3247 intermediate conversion. */
3248 (if (inside_int && inter_int && final_int
3249 && ((inside_prec < inter_prec && inter_prec < final_prec
3250 && inside_unsignedp && !inter_unsignedp)
3251 || final_prec == inter_prec))
3254 /* Two conversions in a row are not needed unless:
3255 - some conversion is floating-point (overstrict for now), or
3256 - some conversion is a vector (overstrict for now), or
3257 - the intermediate type is narrower than both initial and
3259 - the intermediate type and innermost type differ in signedness,
3260 and the outermost type is wider than the intermediate, or
3261 - the initial type is a pointer type and the precisions of the
3262 intermediate and final types differ, or
3263 - the final type is a pointer type and the precisions of the
3264 initial and intermediate types differ. */
3265 (if (! inside_float && ! inter_float && ! final_float
3266 && ! inside_vec && ! inter_vec && ! final_vec
3267 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3268 && ! (inside_int && inter_int
3269 && inter_unsignedp != inside_unsignedp
3270 && inter_prec < final_prec)
3271 && ((inter_unsignedp && inter_prec > inside_prec)
3272 == (final_unsignedp && final_prec > inter_prec))
3273 && ! (inside_ptr && inter_prec != final_prec)
3274 && ! (final_ptr && inside_prec != inter_prec))
3277 /* A truncation to an unsigned type (a zero-extension) should be
3278 canonicalized as bitwise and of a mask. */
3279 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3280 && final_int && inter_int && inside_int
3281 && final_prec == inside_prec
3282 && final_prec > inter_prec
3284 (convert (bit_and @0 { wide_int_to_tree
3286 wi::mask (inter_prec, false,
3287 TYPE_PRECISION (inside_type))); })))
3289 /* If we are converting an integer to a floating-point that can
3290 represent it exactly and back to an integer, we can skip the
3291 floating-point conversion. */
3292 (if (GIMPLE /* PR66211 */
3293 && inside_int && inter_float && final_int &&
3294 (unsigned) significand_size (TYPE_MODE (inter_type))
3295 >= inside_prec - !inside_unsignedp)
3298 /* If we have a narrowing conversion to an integral type that is fed by a
3299 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3300 masks off bits outside the final type (and nothing else). */
3302 (convert (bit_and @0 INTEGER_CST@1))
3303 (if (INTEGRAL_TYPE_P (type)
3304 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3305 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3306 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3307 TYPE_PRECISION (type)), 0))
3311 /* (X /[ex] A) * A -> X. */
3313 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3316 /* Simplify (A / B) * B + (A % B) -> A. */
3317 (for div (trunc_div ceil_div floor_div round_div)
3318 mod (trunc_mod ceil_mod floor_mod round_mod)
3320 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3323 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3324 (for op (plus minus)
3326 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3327 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3328 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3331 wi::overflow_type overflow;
3332 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3333 TYPE_SIGN (type), &overflow);
3335 (if (types_match (type, TREE_TYPE (@2))
3336 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3337 (op @0 { wide_int_to_tree (type, mul); })
3338 (with { tree utype = unsigned_type_for (type); }
3339 (convert (op (convert:utype @0)
3340 (mult (convert:utype @1) (convert:utype @2))))))))))
3342 /* Canonicalization of binary operations. */
3344 /* Convert X + -C into X - C. */
3346 (plus @0 REAL_CST@1)
3347 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3348 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3349 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3350 (minus @0 { tem; })))))
3352 /* Convert x+x into x*2. */
3355 (if (SCALAR_FLOAT_TYPE_P (type))
3356 (mult @0 { build_real (type, dconst2); })
3357 (if (INTEGRAL_TYPE_P (type))
3358 (mult @0 { build_int_cst (type, 2); }))))
3362 (minus integer_zerop @1)
3365 (pointer_diff integer_zerop @1)
3366 (negate (convert @1)))
3368 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3369 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3370 (-ARG1 + ARG0) reduces to -ARG1. */
3372 (minus real_zerop@0 @1)
3373 (if (fold_real_zero_addition_p (type, @0, 0))
3376 /* Transform x * -1 into -x. */
3378 (mult @0 integer_minus_onep)
3381 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3382 signed overflow for CST != 0 && CST != -1. */
3384 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3385 (if (TREE_CODE (@2) != INTEGER_CST
3387 && !integer_zerop (@1) && !integer_minus_onep (@1))
3388 (mult (mult @0 @2) @1)))
3390 /* True if we can easily extract the real and imaginary parts of a complex
3392 (match compositional_complex
3393 (convert? (complex @0 @1)))
3395 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3397 (complex (realpart @0) (imagpart @0))
3400 (realpart (complex @0 @1))
3403 (imagpart (complex @0 @1))
3406 /* Sometimes we only care about half of a complex expression. */
3408 (realpart (convert?:s (conj:s @0)))
3409 (convert (realpart @0)))
3411 (imagpart (convert?:s (conj:s @0)))
3412 (convert (negate (imagpart @0))))
3413 (for part (realpart imagpart)
3414 (for op (plus minus)
3416 (part (convert?:s@2 (op:s @0 @1)))
3417 (convert (op (part @0) (part @1))))))
3419 (realpart (convert?:s (CEXPI:s @0)))
3422 (imagpart (convert?:s (CEXPI:s @0)))
3425 /* conj(conj(x)) -> x */
3427 (conj (convert? (conj @0)))
3428 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3431 /* conj({x,y}) -> {x,-y} */
3433 (conj (convert?:s (complex:s @0 @1)))
3434 (with { tree itype = TREE_TYPE (type); }
3435 (complex (convert:itype @0) (negate (convert:itype @1)))))
3437 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3438 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3443 (bswap (bit_not (bswap @0)))
3445 (for bitop (bit_xor bit_ior bit_and)
3447 (bswap (bitop:c (bswap @0) @1))
3448 (bitop @0 (bswap @1)))))
3451 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3453 /* Simplify constant conditions.
3454 Only optimize constant conditions when the selected branch
3455 has the same type as the COND_EXPR. This avoids optimizing
3456 away "c ? x : throw", where the throw has a void type.
3457 Note that we cannot throw away the fold-const.c variant nor
3458 this one as we depend on doing this transform before possibly
3459 A ? B : B -> B triggers and the fold-const.c one can optimize
3460 0 ? A : B to B even if A has side-effects. Something
3461 genmatch cannot handle. */
3463 (cond INTEGER_CST@0 @1 @2)
3464 (if (integer_zerop (@0))
3465 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3467 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3470 (vec_cond VECTOR_CST@0 @1 @2)
3471 (if (integer_all_onesp (@0))
3473 (if (integer_zerop (@0))
3477 /* Sink unary operations to branches, but only if we do fold both. */
3478 (for op (negate bit_not abs absu)
3480 (op (vec_cond:s @0 @1 @2))
3481 (vec_cond @0 (op! @1) (op! @2))))
3483 /* Sink binary operation to branches, but only if we can fold it. */
3484 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3485 rdiv trunc_div ceil_div floor_div round_div
3486 trunc_mod ceil_mod floor_mod round_mod min max)
3487 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3489 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3490 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3492 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3494 (op (vec_cond:s @0 @1 @2) @3)
3495 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3497 (op @3 (vec_cond:s @0 @1 @2))
3498 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3501 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3502 Currently disabled after pass lvec because ARM understands
3503 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3505 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3506 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3507 (vec_cond (bit_and @0 @3) @1 @2)))
3509 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3510 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3511 (vec_cond (bit_ior @0 @3) @1 @2)))
3513 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3514 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3515 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3517 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3518 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3519 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3521 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3523 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3524 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3525 (vec_cond (bit_and @0 @1) @2 @3)))
3527 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3528 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3529 (vec_cond (bit_ior @0 @1) @2 @3)))
3531 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3532 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3533 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3535 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3536 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3537 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3539 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3540 types are compatible. */
3542 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3543 (if (VECTOR_BOOLEAN_TYPE_P (type)
3544 && types_match (type, TREE_TYPE (@0)))
3545 (if (integer_zerop (@1) && integer_all_onesp (@2))
3547 (if (integer_all_onesp (@1) && integer_zerop (@2))
3550 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3552 /* This pattern implements two kinds simplification:
3555 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3556 1) Conversions are type widening from smaller type.
3557 2) Const c1 equals to c2 after canonicalizing comparison.
3558 3) Comparison has tree code LT, LE, GT or GE.
3559 This specific pattern is needed when (cmp (convert x) c) may not
3560 be simplified by comparison patterns because of multiple uses of
3561 x. It also makes sense here because simplifying across multiple
3562 referred var is always benefitial for complicated cases.
3565 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3566 (for cmp (lt le gt ge eq)
3568 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3571 tree from_type = TREE_TYPE (@1);
3572 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3573 enum tree_code code = ERROR_MARK;
3575 if (INTEGRAL_TYPE_P (from_type)
3576 && int_fits_type_p (@2, from_type)
3577 && (types_match (c1_type, from_type)
3578 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3579 && (TYPE_UNSIGNED (from_type)
3580 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3581 && (types_match (c2_type, from_type)
3582 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3583 && (TYPE_UNSIGNED (from_type)
3584 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3588 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3590 /* X <= Y - 1 equals to X < Y. */
3593 /* X > Y - 1 equals to X >= Y. */
3597 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3599 /* X < Y + 1 equals to X <= Y. */
3602 /* X >= Y + 1 equals to X > Y. */
3606 if (code != ERROR_MARK
3607 || wi::to_widest (@2) == wi::to_widest (@3))
3609 if (cmp == LT_EXPR || cmp == LE_EXPR)
3611 if (cmp == GT_EXPR || cmp == GE_EXPR)
3615 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3616 else if (int_fits_type_p (@3, from_type))
3620 (if (code == MAX_EXPR)
3621 (convert (max @1 (convert @2)))
3622 (if (code == MIN_EXPR)
3623 (convert (min @1 (convert @2)))
3624 (if (code == EQ_EXPR)
3625 (convert (cond (eq @1 (convert @3))
3626 (convert:from_type @3) (convert:from_type @2)))))))))
3628 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3630 1) OP is PLUS or MINUS.
3631 2) CMP is LT, LE, GT or GE.
3632 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3634 This pattern also handles special cases like:
3636 A) Operand x is a unsigned to signed type conversion and c1 is
3637 integer zero. In this case,
3638 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3639 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3640 B) Const c1 may not equal to (C3 op' C2). In this case we also
3641 check equality for (c1+1) and (c1-1) by adjusting comparison
3644 TODO: Though signed type is handled by this pattern, it cannot be
3645 simplified at the moment because C standard requires additional
3646 type promotion. In order to match&simplify it here, the IR needs
3647 to be cleaned up by other optimizers, i.e, VRP. */
3648 (for op (plus minus)
3649 (for cmp (lt le gt ge)
3651 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3652 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3653 (if (types_match (from_type, to_type)
3654 /* Check if it is special case A). */
3655 || (TYPE_UNSIGNED (from_type)
3656 && !TYPE_UNSIGNED (to_type)
3657 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3658 && integer_zerop (@1)
3659 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3662 wi::overflow_type overflow = wi::OVF_NONE;
3663 enum tree_code code, cmp_code = cmp;
3665 wide_int c1 = wi::to_wide (@1);
3666 wide_int c2 = wi::to_wide (@2);
3667 wide_int c3 = wi::to_wide (@3);
3668 signop sgn = TYPE_SIGN (from_type);
3670 /* Handle special case A), given x of unsigned type:
3671 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3672 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3673 if (!types_match (from_type, to_type))
3675 if (cmp_code == LT_EXPR)
3677 if (cmp_code == GE_EXPR)
3679 c1 = wi::max_value (to_type);
3681 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3682 compute (c3 op' c2) and check if it equals to c1 with op' being
3683 the inverted operator of op. Make sure overflow doesn't happen
3684 if it is undefined. */
3685 if (op == PLUS_EXPR)
3686 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3688 real_c1 = wi::add (c3, c2, sgn, &overflow);
3691 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3693 /* Check if c1 equals to real_c1. Boundary condition is handled
3694 by adjusting comparison operation if necessary. */
3695 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3698 /* X <= Y - 1 equals to X < Y. */
3699 if (cmp_code == LE_EXPR)
3701 /* X > Y - 1 equals to X >= Y. */
3702 if (cmp_code == GT_EXPR)
3705 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3708 /* X < Y + 1 equals to X <= Y. */
3709 if (cmp_code == LT_EXPR)
3711 /* X >= Y + 1 equals to X > Y. */
3712 if (cmp_code == GE_EXPR)
3715 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3717 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3719 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3724 (if (code == MAX_EXPR)
3725 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3726 { wide_int_to_tree (from_type, c2); })
3727 (if (code == MIN_EXPR)
3728 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3729 { wide_int_to_tree (from_type, c2); })))))))))
3731 (for cnd (cond vec_cond)
3732 /* A ? B : (A ? X : C) -> A ? B : C. */
3734 (cnd @0 (cnd @0 @1 @2) @3)
3737 (cnd @0 @1 (cnd @0 @2 @3))
3739 /* A ? B : (!A ? C : X) -> A ? B : C. */
3740 /* ??? This matches embedded conditions open-coded because genmatch
3741 would generate matching code for conditions in separate stmts only.
3742 The following is still important to merge then and else arm cases
3743 from if-conversion. */
3745 (cnd @0 @1 (cnd @2 @3 @4))
3746 (if (inverse_conditions_p (@0, @2))
3749 (cnd @0 (cnd @1 @2 @3) @4)
3750 (if (inverse_conditions_p (@0, @1))
3753 /* A ? B : B -> B. */
3758 /* !A ? B : C -> A ? C : B. */
3760 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3763 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3764 return all -1 or all 0 results. */
3765 /* ??? We could instead convert all instances of the vec_cond to negate,
3766 but that isn't necessarily a win on its own. */
3768 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3769 (if (VECTOR_TYPE_P (type)
3770 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3771 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3772 && (TYPE_MODE (TREE_TYPE (type))
3773 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3774 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3776 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3778 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3779 (if (VECTOR_TYPE_P (type)
3780 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3781 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3782 && (TYPE_MODE (TREE_TYPE (type))
3783 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3784 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3787 /* Simplifications of comparisons. */
3789 /* See if we can reduce the magnitude of a constant involved in a
3790 comparison by changing the comparison code. This is a canonicalization
3791 formerly done by maybe_canonicalize_comparison_1. */
3795 (cmp @0 uniform_integer_cst_p@1)
3796 (with { tree cst = uniform_integer_cst_p (@1); }
3797 (if (tree_int_cst_sgn (cst) == -1)
3798 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3799 wide_int_to_tree (TREE_TYPE (cst),
3805 (cmp @0 uniform_integer_cst_p@1)
3806 (with { tree cst = uniform_integer_cst_p (@1); }
3807 (if (tree_int_cst_sgn (cst) == 1)
3808 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3809 wide_int_to_tree (TREE_TYPE (cst),
3810 wi::to_wide (cst) - 1)); })))))
3812 /* We can simplify a logical negation of a comparison to the
3813 inverted comparison. As we cannot compute an expression
3814 operator using invert_tree_comparison we have to simulate
3815 that with expression code iteration. */
3816 (for cmp (tcc_comparison)
3817 icmp (inverted_tcc_comparison)
3818 ncmp (inverted_tcc_comparison_with_nans)
3819 /* Ideally we'd like to combine the following two patterns
3820 and handle some more cases by using
3821 (logical_inverted_value (cmp @0 @1))
3822 here but for that genmatch would need to "inline" that.
3823 For now implement what forward_propagate_comparison did. */
3825 (bit_not (cmp @0 @1))
3826 (if (VECTOR_TYPE_P (type)
3827 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3828 /* Comparison inversion may be impossible for trapping math,
3829 invert_tree_comparison will tell us. But we can't use
3830 a computed operator in the replacement tree thus we have
3831 to play the trick below. */
3832 (with { enum tree_code ic = invert_tree_comparison
3833 (cmp, HONOR_NANS (@0)); }
3839 (bit_xor (cmp @0 @1) integer_truep)
3840 (with { enum tree_code ic = invert_tree_comparison
3841 (cmp, HONOR_NANS (@0)); }
3847 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3848 ??? The transformation is valid for the other operators if overflow
3849 is undefined for the type, but performing it here badly interacts
3850 with the transformation in fold_cond_expr_with_comparison which
3851 attempts to synthetize ABS_EXPR. */
3853 (for sub (minus pointer_diff)
3855 (cmp (sub@2 @0 @1) integer_zerop)
3856 (if (single_use (@2))
3859 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3860 signed arithmetic case. That form is created by the compiler
3861 often enough for folding it to be of value. One example is in
3862 computing loop trip counts after Operator Strength Reduction. */
3863 (for cmp (simple_comparison)
3864 scmp (swapped_simple_comparison)
3866 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3867 /* Handle unfolded multiplication by zero. */
3868 (if (integer_zerop (@1))
3870 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3871 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3873 /* If @1 is negative we swap the sense of the comparison. */
3874 (if (tree_int_cst_sgn (@1) < 0)
3878 /* For integral types with undefined overflow fold
3879 x * C1 == C2 into x == C2 / C1 or false.
3880 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
3884 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
3885 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3886 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3887 && wi::to_wide (@1) != 0)
3888 (with { widest_int quot; }
3889 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
3890 TYPE_SIGN (TREE_TYPE (@0)), "))
3891 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
3892 { constant_boolean_node (cmp == NE_EXPR, type); }))
3893 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3894 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3895 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
3898 tree itype = TREE_TYPE (@0);
3899 int p = TYPE_PRECISION (itype);
3900 wide_int m = wi::one (p + 1) << p;
3901 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
3902 wide_int i = wide_int::from (wi::mod_inv (a, m),
3903 p, TYPE_SIGN (itype));
3904 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
3907 /* Simplify comparison of something with itself. For IEEE
3908 floating-point, we can only do some of these simplifications. */
3912 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3913 || ! HONOR_NANS (@0))
3914 { constant_boolean_node (true, type); }
3915 (if (cmp != EQ_EXPR)
3921 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3922 || ! HONOR_NANS (@0))
3923 { constant_boolean_node (false, type); })))
3924 (for cmp (unle unge uneq)
3927 { constant_boolean_node (true, type); }))
3928 (for cmp (unlt ungt)
3934 (if (!flag_trapping_math)
3935 { constant_boolean_node (false, type); }))
3937 /* Fold ~X op ~Y as Y op X. */
3938 (for cmp (simple_comparison)
3940 (cmp (bit_not@2 @0) (bit_not@3 @1))
3941 (if (single_use (@2) && single_use (@3))
3944 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3945 (for cmp (simple_comparison)
3946 scmp (swapped_simple_comparison)
3948 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3949 (if (single_use (@2)
3950 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3951 (scmp @0 (bit_not @1)))))
3953 (for cmp (simple_comparison)
3954 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3956 (cmp (convert@2 @0) (convert? @1))
3957 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3958 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3959 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3960 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3961 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3964 tree type1 = TREE_TYPE (@1);
3965 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3967 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3968 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3969 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3970 type1 = float_type_node;
3971 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3972 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3973 type1 = double_type_node;
3976 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3977 ? TREE_TYPE (@0) : type1);
3979 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3980 (cmp (convert:newtype @0) (convert:newtype @1))))))
3984 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3986 /* a CMP (-0) -> a CMP 0 */
3987 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3988 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3989 /* x != NaN is always true, other ops are always false. */
3990 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3991 && ! HONOR_SNANS (@1))
3992 { constant_boolean_node (cmp == NE_EXPR, type); })
3993 /* Fold comparisons against infinity. */
3994 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3995 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3998 REAL_VALUE_TYPE max;
3999 enum tree_code code = cmp;
4000 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4002 code = swap_tree_comparison (code);
4005 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4006 (if (code == GT_EXPR
4007 && !(HONOR_NANS (@0) && flag_trapping_math))
4008 { constant_boolean_node (false, type); })
4009 (if (code == LE_EXPR)
4010 /* x <= +Inf is always true, if we don't care about NaNs. */
4011 (if (! HONOR_NANS (@0))
4012 { constant_boolean_node (true, type); }
4013 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4014 an "invalid" exception. */
4015 (if (!flag_trapping_math)
4017 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4018 for == this introduces an exception for x a NaN. */
4019 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4021 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4023 (lt @0 { build_real (TREE_TYPE (@0), max); })
4024 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4025 /* x < +Inf is always equal to x <= DBL_MAX. */
4026 (if (code == LT_EXPR)
4027 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4029 (ge @0 { build_real (TREE_TYPE (@0), max); })
4030 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4031 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4032 an exception for x a NaN so use an unordered comparison. */
4033 (if (code == NE_EXPR)
4034 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4035 (if (! HONOR_NANS (@0))
4037 (ge @0 { build_real (TREE_TYPE (@0), max); })
4038 (le @0 { build_real (TREE_TYPE (@0), max); }))
4040 (unge @0 { build_real (TREE_TYPE (@0), max); })
4041 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4043 /* If this is a comparison of a real constant with a PLUS_EXPR
4044 or a MINUS_EXPR of a real constant, we can convert it into a
4045 comparison with a revised real constant as long as no overflow
4046 occurs when unsafe_math_optimizations are enabled. */
4047 (if (flag_unsafe_math_optimizations)
4048 (for op (plus minus)
4050 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4053 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4054 TREE_TYPE (@1), @2, @1);
4056 (if (tem && !TREE_OVERFLOW (tem))
4057 (cmp @0 { tem; }))))))
4059 /* Likewise, we can simplify a comparison of a real constant with
4060 a MINUS_EXPR whose first operand is also a real constant, i.e.
4061 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4062 floating-point types only if -fassociative-math is set. */
4063 (if (flag_associative_math)
4065 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4066 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4067 (if (tem && !TREE_OVERFLOW (tem))
4068 (cmp { tem; } @1)))))
4070 /* Fold comparisons against built-in math functions. */
4071 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4074 (cmp (sq @0) REAL_CST@1)
4076 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4078 /* sqrt(x) < y is always false, if y is negative. */
4079 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4080 { constant_boolean_node (false, type); })
4081 /* sqrt(x) > y is always true, if y is negative and we
4082 don't care about NaNs, i.e. negative values of x. */
4083 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4084 { constant_boolean_node (true, type); })
4085 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4086 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4087 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4089 /* sqrt(x) < 0 is always false. */
4090 (if (cmp == LT_EXPR)
4091 { constant_boolean_node (false, type); })
4092 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4093 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4094 { constant_boolean_node (true, type); })
4095 /* sqrt(x) <= 0 -> x == 0. */
4096 (if (cmp == LE_EXPR)
4098 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4099 == or !=. In the last case:
4101 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4103 if x is negative or NaN. Due to -funsafe-math-optimizations,
4104 the results for other x follow from natural arithmetic. */
4106 (if ((cmp == LT_EXPR
4110 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4111 /* Give up for -frounding-math. */
4112 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4116 enum tree_code ncmp = cmp;
4117 const real_format *fmt
4118 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4119 real_arithmetic (&c2, MULT_EXPR,
4120 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4121 real_convert (&c2, fmt, &c2);
4122 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4123 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4124 if (!REAL_VALUE_ISINF (c2))
4126 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4127 build_real (TREE_TYPE (@0), c2));
4128 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4130 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4131 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4132 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4133 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4134 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4135 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4138 /* With rounding to even, sqrt of up to 3 different values
4139 gives the same normal result, so in some cases c2 needs
4141 REAL_VALUE_TYPE c2alt, tow;
4142 if (cmp == LT_EXPR || cmp == GE_EXPR)
4146 real_nextafter (&c2alt, fmt, &c2, &tow);
4147 real_convert (&c2alt, fmt, &c2alt);
4148 if (REAL_VALUE_ISINF (c2alt))
4152 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4153 build_real (TREE_TYPE (@0), c2alt));
4154 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4156 else if (real_equal (&TREE_REAL_CST (c3),
4157 &TREE_REAL_CST (@1)))
4163 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4164 (if (REAL_VALUE_ISINF (c2))
4165 /* sqrt(x) > y is x == +Inf, when y is very large. */
4166 (if (HONOR_INFINITIES (@0))
4167 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4168 { constant_boolean_node (false, type); })
4169 /* sqrt(x) > c is the same as x > c*c. */
4170 (if (ncmp != ERROR_MARK)
4171 (if (ncmp == GE_EXPR)
4172 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4173 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4174 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4175 (if (REAL_VALUE_ISINF (c2))
4177 /* sqrt(x) < y is always true, when y is a very large
4178 value and we don't care about NaNs or Infinities. */
4179 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4180 { constant_boolean_node (true, type); })
4181 /* sqrt(x) < y is x != +Inf when y is very large and we
4182 don't care about NaNs. */
4183 (if (! HONOR_NANS (@0))
4184 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4185 /* sqrt(x) < y is x >= 0 when y is very large and we
4186 don't care about Infinities. */
4187 (if (! HONOR_INFINITIES (@0))
4188 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4189 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4192 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4193 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4194 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4195 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4196 (if (ncmp == LT_EXPR)
4197 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4198 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4199 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4200 (if (ncmp != ERROR_MARK && GENERIC)
4201 (if (ncmp == LT_EXPR)
4203 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4204 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4206 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4207 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4208 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4210 (cmp (sq @0) (sq @1))
4211 (if (! HONOR_NANS (@0))
4214 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4215 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4216 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4218 (cmp (float@0 @1) (float @2))
4219 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4220 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4223 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4224 tree type1 = TREE_TYPE (@1);
4225 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4226 tree type2 = TREE_TYPE (@2);
4227 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4229 (if (fmt.can_represent_integral_type_p (type1)
4230 && fmt.can_represent_integral_type_p (type2))
4231 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4232 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4233 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4234 && type1_signed_p >= type2_signed_p)
4235 (icmp @1 (convert @2))
4236 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4237 && type1_signed_p <= type2_signed_p)
4238 (icmp (convert:type2 @1) @2)
4239 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4240 && type1_signed_p == type2_signed_p)
4241 (icmp @1 @2))))))))))
4243 /* Optimize various special cases of (FTYPE) N CMP CST. */
4244 (for cmp (lt le eq ne ge gt)
4245 icmp (le le eq ne ge ge)
4247 (cmp (float @0) REAL_CST@1)
4248 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4249 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4252 tree itype = TREE_TYPE (@0);
4253 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4254 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4255 /* Be careful to preserve any potential exceptions due to
4256 NaNs. qNaNs are ok in == or != context.
4257 TODO: relax under -fno-trapping-math or
4258 -fno-signaling-nans. */
4260 = real_isnan (cst) && (cst->signalling
4261 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4263 /* TODO: allow non-fitting itype and SNaNs when
4264 -fno-trapping-math. */
4265 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4268 signop isign = TYPE_SIGN (itype);
4269 REAL_VALUE_TYPE imin, imax;
4270 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4271 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4273 REAL_VALUE_TYPE icst;
4274 if (cmp == GT_EXPR || cmp == GE_EXPR)
4275 real_ceil (&icst, fmt, cst);
4276 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4277 real_floor (&icst, fmt, cst);
4279 real_trunc (&icst, fmt, cst);
4281 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4283 bool overflow_p = false;
4285 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4288 /* Optimize cases when CST is outside of ITYPE's range. */
4289 (if (real_compare (LT_EXPR, cst, &imin))
4290 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4292 (if (real_compare (GT_EXPR, cst, &imax))
4293 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4295 /* Remove cast if CST is an integer representable by ITYPE. */
4297 (cmp @0 { gcc_assert (!overflow_p);
4298 wide_int_to_tree (itype, icst_val); })
4300 /* When CST is fractional, optimize
4301 (FTYPE) N == CST -> 0
4302 (FTYPE) N != CST -> 1. */
4303 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4304 { constant_boolean_node (cmp == NE_EXPR, type); })
4305 /* Otherwise replace with sensible integer constant. */
4308 gcc_checking_assert (!overflow_p);
4310 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4312 /* Fold A /[ex] B CMP C to A CMP B * C. */
4315 (cmp (exact_div @0 @1) INTEGER_CST@2)
4316 (if (!integer_zerop (@1))
4317 (if (wi::to_wide (@2) == 0)
4319 (if (TREE_CODE (@1) == INTEGER_CST)
4322 wi::overflow_type ovf;
4323 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4324 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4327 { constant_boolean_node (cmp == NE_EXPR, type); }
4328 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4329 (for cmp (lt le gt ge)
4331 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4332 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4335 wi::overflow_type ovf;
4336 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4337 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4340 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4341 TYPE_SIGN (TREE_TYPE (@2)))
4342 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4343 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4345 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4347 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4348 For large C (more than min/B+2^size), this is also true, with the
4349 multiplication computed modulo 2^size.
4350 For intermediate C, this just tests the sign of A. */
4351 (for cmp (lt le gt ge)
4354 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4355 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4356 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4357 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4360 tree utype = TREE_TYPE (@2);
4361 wide_int denom = wi::to_wide (@1);
4362 wide_int right = wi::to_wide (@2);
4363 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4364 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4365 bool small = wi::leu_p (right, smax);
4366 bool large = wi::geu_p (right, smin);
4368 (if (small || large)
4369 (cmp (convert:utype @0) (mult @2 (convert @1)))
4370 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4372 /* Unordered tests if either argument is a NaN. */
4374 (bit_ior (unordered @0 @0) (unordered @1 @1))
4375 (if (types_match (@0, @1))
4378 (bit_and (ordered @0 @0) (ordered @1 @1))
4379 (if (types_match (@0, @1))
4382 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4385 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4388 /* Simple range test simplifications. */
4389 /* A < B || A >= B -> true. */
4390 (for test1 (lt le le le ne ge)
4391 test2 (ge gt ge ne eq ne)
4393 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4394 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4395 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4396 { constant_boolean_node (true, type); })))
4397 /* A < B && A >= B -> false. */
4398 (for test1 (lt lt lt le ne eq)
4399 test2 (ge gt eq gt eq gt)
4401 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4402 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4403 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4404 { constant_boolean_node (false, type); })))
4406 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4407 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4409 Note that comparisons
4410 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4411 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4412 will be canonicalized to above so there's no need to
4419 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4420 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4423 tree ty = TREE_TYPE (@0);
4424 unsigned prec = TYPE_PRECISION (ty);
4425 wide_int mask = wi::to_wide (@2, prec);
4426 wide_int rhs = wi::to_wide (@3, prec);
4427 signop sgn = TYPE_SIGN (ty);
4429 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4430 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4431 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4432 { build_zero_cst (ty); }))))))
4434 /* -A CMP -B -> B CMP A. */
4435 (for cmp (tcc_comparison)
4436 scmp (swapped_tcc_comparison)
4438 (cmp (negate @0) (negate @1))
4439 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4440 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4441 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4444 (cmp (negate @0) CONSTANT_CLASS_P@1)
4445 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4446 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4447 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4448 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4449 (if (tem && !TREE_OVERFLOW (tem))
4450 (scmp @0 { tem; }))))))
4452 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4455 (op (abs @0) zerop@1)
4458 /* From fold_sign_changed_comparison and fold_widened_comparison.
4459 FIXME: the lack of symmetry is disturbing. */
4460 (for cmp (simple_comparison)
4462 (cmp (convert@0 @00) (convert?@1 @10))
4463 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4464 /* Disable this optimization if we're casting a function pointer
4465 type on targets that require function pointer canonicalization. */
4466 && !(targetm.have_canonicalize_funcptr_for_compare ()
4467 && ((POINTER_TYPE_P (TREE_TYPE (@00))
4468 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4469 || (POINTER_TYPE_P (TREE_TYPE (@10))
4470 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4472 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4473 && (TREE_CODE (@10) == INTEGER_CST
4475 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4478 && !POINTER_TYPE_P (TREE_TYPE (@00)))
4479 /* ??? The special-casing of INTEGER_CST conversion was in the original
4480 code and here to avoid a spurious overflow flag on the resulting
4481 constant which fold_convert produces. */
4482 (if (TREE_CODE (@1) == INTEGER_CST)
4483 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4484 TREE_OVERFLOW (@1)); })
4485 (cmp @00 (convert @1)))
4487 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4488 /* If possible, express the comparison in the shorter mode. */
4489 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4490 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4491 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4492 && TYPE_UNSIGNED (TREE_TYPE (@00))))
4493 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4494 || ((TYPE_PRECISION (TREE_TYPE (@00))
4495 >= TYPE_PRECISION (TREE_TYPE (@10)))
4496 && (TYPE_UNSIGNED (TREE_TYPE (@00))
4497 == TYPE_UNSIGNED (TREE_TYPE (@10))))
4498 || (TREE_CODE (@10) == INTEGER_CST
4499 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4500 && int_fits_type_p (@10, TREE_TYPE (@00)))))
4501 (cmp @00 (convert @10))
4502 (if (TREE_CODE (@10) == INTEGER_CST
4503 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4504 && !int_fits_type_p (@10, TREE_TYPE (@00)))
4507 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4508 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4509 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4510 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4512 (if (above || below)
4513 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4514 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4515 (if (cmp == LT_EXPR || cmp == LE_EXPR)
4516 { constant_boolean_node (above ? true : false, type); }
4517 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4518 { constant_boolean_node (above ? false : true, type); }))))))))))))
4522 /* SSA names are canonicalized to 2nd place. */
4523 (cmp addr@0 SSA_NAME@1)
4525 { poly_int64 off; tree base; }
4526 /* A local variable can never be pointed to by
4527 the default SSA name of an incoming parameter. */
4528 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4529 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4530 && (base = get_base_address (TREE_OPERAND (@0, 0)))
4531 && TREE_CODE (base) == VAR_DECL
4532 && auto_var_in_fn_p (base, current_function_decl))
4533 (if (cmp == NE_EXPR)
4534 { constant_boolean_node (true, type); }
4535 { constant_boolean_node (false, type); })
4536 /* If the address is based on @1 decide using the offset. */
4537 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4538 && TREE_CODE (base) == MEM_REF
4539 && TREE_OPERAND (base, 0) == @1)
4540 (with { off += mem_ref_offset (base).force_shwi (); }
4541 (if (known_ne (off, 0))
4542 { constant_boolean_node (cmp == NE_EXPR, type); }
4543 (if (known_eq (off, 0))
4544 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4546 /* Equality compare simplifications from fold_binary */
4549 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4550 Similarly for NE_EXPR. */
4552 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4553 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4554 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4555 { constant_boolean_node (cmp == NE_EXPR, type); }))
4557 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
4559 (cmp (bit_xor @0 @1) integer_zerop)
4562 /* (X ^ Y) == Y becomes X == 0.
4563 Likewise (X ^ Y) == X becomes Y == 0. */
4565 (cmp:c (bit_xor:c @0 @1) @0)
4566 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4568 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
4570 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4571 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4572 (cmp @0 (bit_xor @1 (convert @2)))))
4575 (cmp (convert? addr@0) integer_zerop)
4576 (if (tree_single_nonzero_warnv_p (@0, NULL))
4577 { constant_boolean_node (cmp == NE_EXPR, type); }))
4579 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
4581 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
4582 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
4584 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
4585 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
4586 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
4587 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
4592 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
4593 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4594 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4595 && types_match (@0, @1))
4596 (ncmp (bit_xor @0 @1) @2)))))
4597 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
4598 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
4602 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
4603 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4604 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4605 && types_match (@0, @1))
4606 (ncmp (bit_xor @0 @1) @2))))
4608 /* If we have (A & C) == C where C is a power of 2, convert this into
4609 (A & C) != 0. Similarly for NE_EXPR. */
4613 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4614 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4616 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4617 convert this into a shift followed by ANDing with D. */
4620 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4621 INTEGER_CST@2 integer_zerop)
4622 (if (integer_pow2p (@2))
4624 int shift = (wi::exact_log2 (wi::to_wide (@2))
4625 - wi::exact_log2 (wi::to_wide (@1)));
4629 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4631 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4634 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4635 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
4639 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4640 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4641 && type_has_mode_precision_p (TREE_TYPE (@0))
4642 && element_precision (@2) >= element_precision (@0)
4643 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4644 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4645 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4647 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4648 this into a right shift or sign extension followed by ANDing with C. */
4651 (lt @0 integer_zerop)
4652 INTEGER_CST@1 integer_zerop)
4653 (if (integer_pow2p (@1)
4654 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4656 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4660 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4662 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4663 sign extension followed by AND with C will achieve the effect. */
4664 (bit_and (convert @0) @1)))))
4666 /* When the addresses are not directly of decls compare base and offset.
4667 This implements some remaining parts of fold_comparison address
4668 comparisons but still no complete part of it. Still it is good
4669 enough to make fold_stmt not regress when not dispatching to fold_binary. */
4670 (for cmp (simple_comparison)
4672 (cmp (convert1?@2 addr@0) (convert2? addr@1))
4675 poly_int64 off0, off1;
4676 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4677 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4678 if (base0 && TREE_CODE (base0) == MEM_REF)
4680 off0 += mem_ref_offset (base0).force_shwi ();
4681 base0 = TREE_OPERAND (base0, 0);
4683 if (base1 && TREE_CODE (base1) == MEM_REF)
4685 off1 += mem_ref_offset (base1).force_shwi ();
4686 base1 = TREE_OPERAND (base1, 0);
4689 (if (base0 && base1)
4693 /* Punt in GENERIC on variables with value expressions;
4694 the value expressions might point to fields/elements
4695 of other vars etc. */
4697 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4698 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4700 else if (decl_in_symtab_p (base0)
4701 && decl_in_symtab_p (base1))
4702 equal = symtab_node::get_create (base0)
4703 ->equal_address_to (symtab_node::get_create (base1));
4704 else if ((DECL_P (base0)
4705 || TREE_CODE (base0) == SSA_NAME
4706 || TREE_CODE (base0) == STRING_CST)
4708 || TREE_CODE (base1) == SSA_NAME
4709 || TREE_CODE (base1) == STRING_CST))
4710 equal = (base0 == base1);
4713 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4714 off0.is_constant (&ioff0);
4715 off1.is_constant (&ioff1);
4716 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4717 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4718 || (TREE_CODE (base0) == STRING_CST
4719 && TREE_CODE (base1) == STRING_CST
4720 && ioff0 >= 0 && ioff1 >= 0
4721 && ioff0 < TREE_STRING_LENGTH (base0)
4722 && ioff1 < TREE_STRING_LENGTH (base1)
4723 /* This is a too conservative test that the STRING_CSTs
4724 will not end up being string-merged. */
4725 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4726 TREE_STRING_POINTER (base1) + ioff1,
4727 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4728 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4730 else if (!DECL_P (base0) || !DECL_P (base1))
4732 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4734 /* If this is a pointer comparison, ignore for now even
4735 valid equalities where one pointer is the offset zero
4736 of one object and the other to one past end of another one. */
4737 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4739 /* Assume that automatic variables can't be adjacent to global
4741 else if (is_global_var (base0) != is_global_var (base1))
4745 tree sz0 = DECL_SIZE_UNIT (base0);
4746 tree sz1 = DECL_SIZE_UNIT (base1);
4747 /* If sizes are unknown, e.g. VLA or not representable,
4749 if (!tree_fits_poly_int64_p (sz0)
4750 || !tree_fits_poly_int64_p (sz1))
4754 poly_int64 size0 = tree_to_poly_int64 (sz0);
4755 poly_int64 size1 = tree_to_poly_int64 (sz1);
4756 /* If one offset is pointing (or could be) to the beginning
4757 of one object and the other is pointing to one past the
4758 last byte of the other object, punt. */
4759 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4761 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4763 /* If both offsets are the same, there are some cases
4764 we know that are ok. Either if we know they aren't
4765 zero, or if we know both sizes are no zero. */
4767 && known_eq (off0, off1)
4768 && (known_ne (off0, 0)
4769 || (known_ne (size0, 0) && known_ne (size1, 0))))
4776 && (cmp == EQ_EXPR || cmp == NE_EXPR
4777 /* If the offsets are equal we can ignore overflow. */
4778 || known_eq (off0, off1)
4779 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4780 /* Or if we compare using pointers to decls or strings. */
4781 || (POINTER_TYPE_P (TREE_TYPE (@2))
4782 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4784 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4785 { constant_boolean_node (known_eq (off0, off1), type); })
4786 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4787 { constant_boolean_node (known_ne (off0, off1), type); })
4788 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4789 { constant_boolean_node (known_lt (off0, off1), type); })
4790 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4791 { constant_boolean_node (known_le (off0, off1), type); })
4792 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4793 { constant_boolean_node (known_ge (off0, off1), type); })
4794 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4795 { constant_boolean_node (known_gt (off0, off1), type); }))
4798 (if (cmp == EQ_EXPR)
4799 { constant_boolean_node (false, type); })
4800 (if (cmp == NE_EXPR)
4801 { constant_boolean_node (true, type); })))))))))
4803 /* Simplify pointer equality compares using PTA. */
4807 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4808 && ptrs_compare_unequal (@0, @1))
4809 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4811 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4812 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4813 Disable the transform if either operand is pointer to function.
4814 This broke pr22051-2.c for arm where function pointer
4815 canonicalizaion is not wanted. */
4819 (cmp (convert @0) INTEGER_CST@1)
4820 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4821 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4822 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4823 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4824 && POINTER_TYPE_P (TREE_TYPE (@1))
4825 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4826 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4827 (cmp @0 (convert @1)))))
4829 /* Non-equality compare simplifications from fold_binary */
4830 (for cmp (lt gt le ge)
4831 /* Comparisons with the highest or lowest possible integer of
4832 the specified precision will have known values. */
4834 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4835 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4836 || POINTER_TYPE_P (TREE_TYPE (@1))
4837 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4838 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4841 tree cst = uniform_integer_cst_p (@1);
4842 tree arg1_type = TREE_TYPE (cst);
4843 unsigned int prec = TYPE_PRECISION (arg1_type);
4844 wide_int max = wi::max_value (arg1_type);
4845 wide_int signed_max = wi::max_value (prec, SIGNED);
4846 wide_int min = wi::min_value (arg1_type);
4849 (if (wi::to_wide (cst) == max)
4851 (if (cmp == GT_EXPR)
4852 { constant_boolean_node (false, type); })
4853 (if (cmp == GE_EXPR)
4855 (if (cmp == LE_EXPR)
4856 { constant_boolean_node (true, type); })
4857 (if (cmp == LT_EXPR)
4859 (if (wi::to_wide (cst) == min)
4861 (if (cmp == LT_EXPR)
4862 { constant_boolean_node (false, type); })
4863 (if (cmp == LE_EXPR)
4865 (if (cmp == GE_EXPR)
4866 { constant_boolean_node (true, type); })
4867 (if (cmp == GT_EXPR)
4869 (if (wi::to_wide (cst) == max - 1)
4871 (if (cmp == GT_EXPR)
4872 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4873 wide_int_to_tree (TREE_TYPE (cst),
4876 (if (cmp == LE_EXPR)
4877 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4878 wide_int_to_tree (TREE_TYPE (cst),
4881 (if (wi::to_wide (cst) == min + 1)
4883 (if (cmp == GE_EXPR)
4884 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4885 wide_int_to_tree (TREE_TYPE (cst),
4888 (if (cmp == LT_EXPR)
4889 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4890 wide_int_to_tree (TREE_TYPE (cst),
4893 (if (wi::to_wide (cst) == signed_max
4894 && TYPE_UNSIGNED (arg1_type)
4895 /* We will flip the signedness of the comparison operator
4896 associated with the mode of @1, so the sign bit is
4897 specified by this mode. Check that @1 is the signed
4898 max associated with this sign bit. */
4899 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4900 /* signed_type does not work on pointer types. */
4901 && INTEGRAL_TYPE_P (arg1_type))
4902 /* The following case also applies to X < signed_max+1
4903 and X >= signed_max+1 because previous transformations. */
4904 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4905 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4907 (if (cst == @1 && cmp == LE_EXPR)
4908 (ge (convert:st @0) { build_zero_cst (st); }))
4909 (if (cst == @1 && cmp == GT_EXPR)
4910 (lt (convert:st @0) { build_zero_cst (st); }))
4911 (if (cmp == LE_EXPR)
4912 (ge (view_convert:st @0) { build_zero_cst (st); }))
4913 (if (cmp == GT_EXPR)
4914 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4916 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4917 /* If the second operand is NaN, the result is constant. */
4920 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4921 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4922 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4923 ? false : true, type); })))
4925 /* bool_var != 0 becomes bool_var. */
4927 (ne @0 integer_zerop)
4928 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4929 && types_match (type, TREE_TYPE (@0)))
4931 /* bool_var == 1 becomes bool_var. */
4933 (eq @0 integer_onep)
4934 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4935 && types_match (type, TREE_TYPE (@0)))
4938 bool_var == 0 becomes !bool_var or
4939 bool_var != 1 becomes !bool_var
4940 here because that only is good in assignment context as long
4941 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4942 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4943 clearly less optimal and which we'll transform again in forwprop. */
4945 /* When one argument is a constant, overflow detection can be simplified.
4946 Currently restricted to single use so as not to interfere too much with
4947 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4948 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
4949 (for cmp (lt le ge gt)
4952 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
4953 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
4954 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
4955 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
4956 && wi::to_wide (@1) != 0
4959 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
4960 signop sign = TYPE_SIGN (TREE_TYPE (@0));
4962 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4963 wi::max_value (prec, sign)
4964 - wi::to_wide (@1)); })))))
4966 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4967 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4968 expects the long form, so we restrict the transformation for now. */
4971 (cmp:c (minus@2 @0 @1) @0)
4972 (if (single_use (@2)
4973 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4974 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4977 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
4980 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
4981 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4982 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4985 /* Testing for overflow is unnecessary if we already know the result. */
4990 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4991 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4992 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4993 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4998 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4999 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5000 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5001 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5003 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5004 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5008 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5009 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5010 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5011 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5013 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5014 is at least twice as wide as type of A and B, simplify to
5015 __builtin_mul_overflow (A, B, <unused>). */
5018 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5020 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5021 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5022 && TYPE_UNSIGNED (TREE_TYPE (@0))
5023 && (TYPE_PRECISION (TREE_TYPE (@3))
5024 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5025 && tree_fits_uhwi_p (@2)
5026 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5027 && types_match (@0, @1)
5028 && type_has_mode_precision_p (TREE_TYPE (@0))
5029 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5030 != CODE_FOR_nothing))
5031 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5032 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5034 /* Simplification of math builtins. These rules must all be optimizations
5035 as well as IL simplifications. If there is a possibility that the new
5036 form could be a pessimization, the rule should go in the canonicalization
5037 section that follows this one.
5039 Rules can generally go in this section if they satisfy one of
5042 - the rule describes an identity
5044 - the rule replaces calls with something as simple as addition or
5047 - the rule contains unary calls only and simplifies the surrounding
5048 arithmetic. (The idea here is to exclude non-unary calls in which
5049 one operand is constant and in which the call is known to be cheap
5050 when the operand has that value.) */
5052 (if (flag_unsafe_math_optimizations)
5053 /* Simplify sqrt(x) * sqrt(x) -> x. */
5055 (mult (SQRT_ALL@1 @0) @1)
5056 (if (!HONOR_SNANS (type))
5059 (for op (plus minus)
5060 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5064 (rdiv (op @0 @2) @1)))
5066 (for cmp (lt le gt ge)
5067 neg_cmp (gt ge lt le)
5068 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5070 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5072 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5074 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5075 || (real_zerop (tem) && !real_zerop (@1))))
5077 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5079 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5080 (neg_cmp @0 { tem; })))))))
5082 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5083 (for root (SQRT CBRT)
5085 (mult (root:s @0) (root:s @1))
5086 (root (mult @0 @1))))
5088 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5089 (for exps (EXP EXP2 EXP10 POW10)
5091 (mult (exps:s @0) (exps:s @1))
5092 (exps (plus @0 @1))))
5094 /* Simplify a/root(b/c) into a*root(c/b). */
5095 (for root (SQRT CBRT)
5097 (rdiv @0 (root:s (rdiv:s @1 @2)))
5098 (mult @0 (root (rdiv @2 @1)))))
5100 /* Simplify x/expN(y) into x*expN(-y). */
5101 (for exps (EXP EXP2 EXP10 POW10)
5103 (rdiv @0 (exps:s @1))
5104 (mult @0 (exps (negate @1)))))
5106 (for logs (LOG LOG2 LOG10 LOG10)
5107 exps (EXP EXP2 EXP10 POW10)
5108 /* logN(expN(x)) -> x. */
5112 /* expN(logN(x)) -> x. */
5117 /* Optimize logN(func()) for various exponential functions. We
5118 want to determine the value "x" and the power "exponent" in
5119 order to transform logN(x**exponent) into exponent*logN(x). */
5120 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5121 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5124 (if (SCALAR_FLOAT_TYPE_P (type))
5130 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5131 x = build_real_truncate (type, dconst_e ());
5134 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5135 x = build_real (type, dconst2);
5139 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5141 REAL_VALUE_TYPE dconst10;
5142 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5143 x = build_real (type, dconst10);
5150 (mult (logs { x; }) @0)))))
5158 (if (SCALAR_FLOAT_TYPE_P (type))
5164 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5165 x = build_real (type, dconsthalf);
5168 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5169 x = build_real_truncate (type, dconst_third ());
5175 (mult { x; } (logs @0))))))
5177 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5178 (for logs (LOG LOG2 LOG10)
5182 (mult @1 (logs @0))))
5184 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5185 or if C is a positive power of 2,
5186 pow(C,x) -> exp2(log2(C)*x). */
5194 (pows REAL_CST@0 @1)
5195 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5196 && real_isfinite (TREE_REAL_CST_PTR (@0))
5197 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5198 the use_exp2 case until after vectorization. It seems actually
5199 beneficial for all constants to postpone this until later,
5200 because exp(log(C)*x), while faster, will have worse precision
5201 and if x folds into a constant too, that is unnecessary
5203 && canonicalize_math_after_vectorization_p ())
5205 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5206 bool use_exp2 = false;
5207 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5208 && value->cl == rvc_normal)
5210 REAL_VALUE_TYPE frac_rvt = *value;
5211 SET_REAL_EXP (&frac_rvt, 1);
5212 if (real_equal (&frac_rvt, &dconst1))
5217 (if (optimize_pow_to_exp (@0, @1))
5218 (exps (mult (logs @0) @1)))
5219 (exp2s (mult (log2s @0) @1)))))))
5222 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5224 exps (EXP EXP2 EXP10 POW10)
5225 logs (LOG LOG2 LOG10 LOG10)
5227 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5228 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5229 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5230 (exps (plus (mult (logs @0) @1) @2)))))
5235 exps (EXP EXP2 EXP10 POW10)
5236 /* sqrt(expN(x)) -> expN(x*0.5). */
5239 (exps (mult @0 { build_real (type, dconsthalf); })))
5240 /* cbrt(expN(x)) -> expN(x/3). */
5243 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5244 /* pow(expN(x), y) -> expN(x*y). */
5247 (exps (mult @0 @1))))
5249 /* tan(atan(x)) -> x. */
5256 /* Simplify sin(atan(x)) -> x / 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);
5270 (if (SCALAR_FLOAT_TYPE_P (type))
5271 (cond (lt (abs @0) { t_cst; })
5272 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5273 (copysigns { t_one; } @0))))))
5275 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5279 copysigns (COPYSIGN)
5284 REAL_VALUE_TYPE r_cst;
5285 build_sinatan_real (&r_cst, type);
5286 tree t_cst = build_real (type, r_cst);
5287 tree t_one = build_one_cst (type);
5288 tree t_zero = build_zero_cst (type);
5290 (if (SCALAR_FLOAT_TYPE_P (type))
5291 (cond (lt (abs @0) { t_cst; })
5292 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5293 (copysigns { t_zero; } @0))))))
5295 (if (!flag_errno_math)
5296 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5301 (sinhs (atanhs:s @0))
5302 (with { tree t_one = build_one_cst (type); }
5303 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5305 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5310 (coshs (atanhs:s @0))
5311 (with { tree t_one = build_one_cst (type); }
5312 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5314 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5316 (CABS (complex:C @0 real_zerop@1))
5319 /* trunc(trunc(x)) -> trunc(x), etc. */
5320 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5324 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5325 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5327 (fns integer_valued_real_p@0)
5330 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5332 (HYPOT:c @0 real_zerop@1)
5335 /* pow(1,x) -> 1. */
5337 (POW real_onep@0 @1)
5341 /* copysign(x,x) -> x. */
5342 (COPYSIGN_ALL @0 @0)
5346 /* copysign(x,-x) -> -x. */
5347 (COPYSIGN_ALL @0 (negate@1 @0))
5351 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5352 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5355 (for scale (LDEXP SCALBN SCALBLN)
5356 /* ldexp(0, x) -> 0. */
5358 (scale real_zerop@0 @1)
5360 /* ldexp(x, 0) -> x. */
5362 (scale @0 integer_zerop@1)
5364 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5366 (scale REAL_CST@0 @1)
5367 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5370 /* Canonicalization of sequences of math builtins. These rules represent
5371 IL simplifications but are not necessarily optimizations.
5373 The sincos pass is responsible for picking "optimal" implementations
5374 of math builtins, which may be more complicated and can sometimes go
5375 the other way, e.g. converting pow into a sequence of sqrts.
5376 We only want to do these canonicalizations before the pass has run. */
5378 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5379 /* Simplify tan(x) * cos(x) -> sin(x). */
5381 (mult:c (TAN:s @0) (COS:s @0))
5384 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5386 (mult:c @0 (POW:s @0 REAL_CST@1))
5387 (if (!TREE_OVERFLOW (@1))
5388 (POW @0 (plus @1 { build_one_cst (type); }))))
5390 /* Simplify sin(x) / cos(x) -> tan(x). */
5392 (rdiv (SIN:s @0) (COS:s @0))
5395 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5397 (rdiv (SINH:s @0) (COSH:s @0))
5400 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5402 (rdiv (TANH:s @0) (SINH:s @0))
5403 (rdiv {build_one_cst (type);} (COSH @0)))
5405 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5407 (rdiv (COS:s @0) (SIN:s @0))
5408 (rdiv { build_one_cst (type); } (TAN @0)))
5410 /* Simplify sin(x) / tan(x) -> cos(x). */
5412 (rdiv (SIN:s @0) (TAN:s @0))
5413 (if (! HONOR_NANS (@0)
5414 && ! HONOR_INFINITIES (@0))
5417 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5419 (rdiv (TAN:s @0) (SIN:s @0))
5420 (if (! HONOR_NANS (@0)
5421 && ! HONOR_INFINITIES (@0))
5422 (rdiv { build_one_cst (type); } (COS @0))))
5424 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5426 (mult (POW:s @0 @1) (POW:s @0 @2))
5427 (POW @0 (plus @1 @2)))
5429 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5431 (mult (POW:s @0 @1) (POW:s @2 @1))
5432 (POW (mult @0 @2) @1))
5434 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5436 (mult (POWI:s @0 @1) (POWI:s @2 @1))
5437 (POWI (mult @0 @2) @1))
5439 /* Simplify pow(x,c) / x -> pow(x,c-1). */
5441 (rdiv (POW:s @0 REAL_CST@1) @0)
5442 (if (!TREE_OVERFLOW (@1))
5443 (POW @0 (minus @1 { build_one_cst (type); }))))
5445 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5447 (rdiv @0 (POW:s @1 @2))
5448 (mult @0 (POW @1 (negate @2))))
5453 /* sqrt(sqrt(x)) -> pow(x,1/4). */
5456 (pows @0 { build_real (type, dconst_quarter ()); }))
5457 /* sqrt(cbrt(x)) -> pow(x,1/6). */
5460 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5461 /* cbrt(sqrt(x)) -> pow(x,1/6). */
5464 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5465 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
5467 (cbrts (cbrts tree_expr_nonnegative_p@0))
5468 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5469 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
5471 (sqrts (pows @0 @1))
5472 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5473 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
5475 (cbrts (pows tree_expr_nonnegative_p@0 @1))
5476 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5477 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
5479 (pows (sqrts @0) @1)
5480 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5481 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
5483 (pows (cbrts tree_expr_nonnegative_p@0) @1)
5484 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5485 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
5487 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5488 (pows @0 (mult @1 @2))))
5490 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
5492 (CABS (complex @0 @0))
5493 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5495 /* hypot(x,x) -> fabs(x)*sqrt(2). */
5498 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5500 /* cexp(x+yi) -> exp(x)*cexpi(y). */
5505 (cexps compositional_complex@0)
5506 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
5508 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5509 (mult @1 (imagpart @2)))))))
5511 (if (canonicalize_math_p ())
5512 /* floor(x) -> trunc(x) if x is nonnegative. */
5513 (for floors (FLOOR_ALL)
5516 (floors tree_expr_nonnegative_p@0)
5519 (match double_value_p
5521 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5522 (for froms (BUILT_IN_TRUNCL
5534 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
5535 (if (optimize && canonicalize_math_p ())
5537 (froms (convert double_value_p@0))
5538 (convert (tos @0)))))
5540 (match float_value_p
5542 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5543 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5544 BUILT_IN_FLOORL BUILT_IN_FLOOR
5545 BUILT_IN_CEILL BUILT_IN_CEIL
5546 BUILT_IN_ROUNDL BUILT_IN_ROUND
5547 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5548 BUILT_IN_RINTL BUILT_IN_RINT)
5549 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5550 BUILT_IN_FLOORF BUILT_IN_FLOORF
5551 BUILT_IN_CEILF BUILT_IN_CEILF
5552 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5553 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5554 BUILT_IN_RINTF BUILT_IN_RINTF)
5555 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5557 (if (optimize && canonicalize_math_p ()
5558 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
5560 (froms (convert float_value_p@0))
5561 (convert (tos @0)))))
5563 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5564 tos (XFLOOR XCEIL XROUND XRINT)
5565 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
5566 (if (optimize && canonicalize_math_p ())
5568 (froms (convert double_value_p@0))
5571 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5572 XFLOOR XCEIL XROUND XRINT)
5573 tos (XFLOORF XCEILF XROUNDF XRINTF)
5574 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5576 (if (optimize && canonicalize_math_p ())
5578 (froms (convert float_value_p@0))
5581 (if (canonicalize_math_p ())
5582 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
5583 (for floors (IFLOOR LFLOOR LLFLOOR)
5585 (floors tree_expr_nonnegative_p@0)
5588 (if (canonicalize_math_p ())
5589 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
5590 (for fns (IFLOOR LFLOOR LLFLOOR
5592 IROUND LROUND LLROUND)
5594 (fns integer_valued_real_p@0)
5596 (if (!flag_errno_math)
5597 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
5598 (for rints (IRINT LRINT LLRINT)
5600 (rints integer_valued_real_p@0)
5603 (if (canonicalize_math_p ())
5604 (for ifn (IFLOOR ICEIL IROUND IRINT)
5605 lfn (LFLOOR LCEIL LROUND LRINT)
5606 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5607 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5608 sizeof (int) == sizeof (long). */
5609 (if (TYPE_PRECISION (integer_type_node)
5610 == TYPE_PRECISION (long_integer_type_node))
5613 (lfn:long_integer_type_node @0)))
5614 /* Canonicalize llround (x) to lround (x) on LP64 targets where
5615 sizeof (long long) == sizeof (long). */
5616 (if (TYPE_PRECISION (long_long_integer_type_node)
5617 == TYPE_PRECISION (long_integer_type_node))
5620 (lfn:long_integer_type_node @0)))))
5622 /* cproj(x) -> x if we're ignoring infinities. */
5625 (if (!HONOR_INFINITIES (type))
5628 /* If the real part is inf and the imag part is known to be
5629 nonnegative, return (inf + 0i). */
5631 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5632 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5633 { build_complex_inf (type, false); }))
5635 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
5637 (CPROJ (complex @0 REAL_CST@1))
5638 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5639 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5645 (pows @0 REAL_CST@1)
5647 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5648 REAL_VALUE_TYPE tmp;
5651 /* pow(x,0) -> 1. */
5652 (if (real_equal (value, &dconst0))
5653 { build_real (type, dconst1); })
5654 /* pow(x,1) -> x. */
5655 (if (real_equal (value, &dconst1))
5657 /* pow(x,-1) -> 1/x. */
5658 (if (real_equal (value, &dconstm1))
5659 (rdiv { build_real (type, dconst1); } @0))
5660 /* pow(x,0.5) -> sqrt(x). */
5661 (if (flag_unsafe_math_optimizations
5662 && canonicalize_math_p ()
5663 && real_equal (value, &dconsthalf))
5665 /* pow(x,1/3) -> cbrt(x). */
5666 (if (flag_unsafe_math_optimizations
5667 && canonicalize_math_p ()
5668 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5669 real_equal (value, &tmp)))
5672 /* powi(1,x) -> 1. */
5674 (POWI real_onep@0 @1)
5678 (POWI @0 INTEGER_CST@1)
5680 /* powi(x,0) -> 1. */
5681 (if (wi::to_wide (@1) == 0)
5682 { build_real (type, dconst1); })
5683 /* powi(x,1) -> x. */
5684 (if (wi::to_wide (@1) == 1)
5686 /* powi(x,-1) -> 1/x. */
5687 (if (wi::to_wide (@1) == -1)
5688 (rdiv { build_real (type, dconst1); } @0))))
5690 /* Narrowing of arithmetic and logical operations.
5692 These are conceptually similar to the transformations performed for
5693 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
5694 term we want to move all that code out of the front-ends into here. */
5696 /* Convert (outertype)((innertype0)a+(innertype1)b)
5697 into ((newtype)a+(newtype)b) where newtype
5698 is the widest mode from all of these. */
5699 (for op (plus minus mult rdiv)
5701 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5702 /* If we have a narrowing conversion of an arithmetic operation where
5703 both operands are widening conversions from the same type as the outer
5704 narrowing conversion. Then convert the innermost operands to a
5705 suitable unsigned type (to avoid introducing undefined behavior),
5706 perform the operation and convert the result to the desired type. */
5707 (if (INTEGRAL_TYPE_P (type)
5710 /* We check for type compatibility between @0 and @1 below,
5711 so there's no need to check that @2/@4 are integral types. */
5712 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5713 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5714 /* The precision of the type of each operand must match the
5715 precision of the mode of each operand, similarly for the
5717 && type_has_mode_precision_p (TREE_TYPE (@1))
5718 && type_has_mode_precision_p (TREE_TYPE (@2))
5719 && type_has_mode_precision_p (type)
5720 /* The inner conversion must be a widening conversion. */
5721 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5722 && types_match (@1, type)
5723 && (types_match (@1, @2)
5724 /* Or the second operand is const integer or converted const
5725 integer from valueize. */
5726 || TREE_CODE (@2) == INTEGER_CST))
5727 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5728 (op @1 (convert @2))
5729 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5730 (convert (op (convert:utype @1)
5731 (convert:utype @2)))))
5732 (if (FLOAT_TYPE_P (type)
5733 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5734 == DECIMAL_FLOAT_TYPE_P (type))
5735 (with { tree arg0 = strip_float_extensions (@1);
5736 tree arg1 = strip_float_extensions (@2);
5737 tree itype = TREE_TYPE (@0);
5738 tree ty1 = TREE_TYPE (arg0);
5739 tree ty2 = TREE_TYPE (arg1);
5740 enum tree_code code = TREE_CODE (itype); }
5741 (if (FLOAT_TYPE_P (ty1)
5742 && FLOAT_TYPE_P (ty2))
5743 (with { tree newtype = type;
5744 if (TYPE_MODE (ty1) == SDmode
5745 || TYPE_MODE (ty2) == SDmode
5746 || TYPE_MODE (type) == SDmode)
5747 newtype = dfloat32_type_node;
5748 if (TYPE_MODE (ty1) == DDmode
5749 || TYPE_MODE (ty2) == DDmode
5750 || TYPE_MODE (type) == DDmode)
5751 newtype = dfloat64_type_node;
5752 if (TYPE_MODE (ty1) == TDmode
5753 || TYPE_MODE (ty2) == TDmode
5754 || TYPE_MODE (type) == TDmode)
5755 newtype = dfloat128_type_node; }
5756 (if ((newtype == dfloat32_type_node
5757 || newtype == dfloat64_type_node
5758 || newtype == dfloat128_type_node)
5760 && types_match (newtype, type))
5761 (op (convert:newtype @1) (convert:newtype @2))
5762 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5764 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5766 /* Sometimes this transformation is safe (cannot
5767 change results through affecting double rounding
5768 cases) and sometimes it is not. If NEWTYPE is
5769 wider than TYPE, e.g. (float)((long double)double
5770 + (long double)double) converted to
5771 (float)(double + double), the transformation is
5772 unsafe regardless of the details of the types
5773 involved; double rounding can arise if the result
5774 of NEWTYPE arithmetic is a NEWTYPE value half way
5775 between two representable TYPE values but the
5776 exact value is sufficiently different (in the
5777 right direction) for this difference to be
5778 visible in ITYPE arithmetic. If NEWTYPE is the
5779 same as TYPE, however, the transformation may be
5780 safe depending on the types involved: it is safe
5781 if the ITYPE has strictly more than twice as many
5782 mantissa bits as TYPE, can represent infinities
5783 and NaNs if the TYPE can, and has sufficient
5784 exponent range for the product or ratio of two
5785 values representable in the TYPE to be within the
5786 range of normal values of ITYPE. */
5787 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5788 && (flag_unsafe_math_optimizations
5789 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5790 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5792 && !excess_precision_type (newtype)))
5793 && !types_match (itype, newtype))
5794 (convert:type (op (convert:newtype @1)
5795 (convert:newtype @2)))
5800 /* This is another case of narrowing, specifically when there's an outer
5801 BIT_AND_EXPR which masks off bits outside the type of the innermost
5802 operands. Like the previous case we have to convert the operands
5803 to unsigned types to avoid introducing undefined behavior for the
5804 arithmetic operation. */
5805 (for op (minus plus)
5807 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5808 (if (INTEGRAL_TYPE_P (type)
5809 /* We check for type compatibility between @0 and @1 below,
5810 so there's no need to check that @1/@3 are integral types. */
5811 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5812 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5813 /* The precision of the type of each operand must match the
5814 precision of the mode of each operand, similarly for the
5816 && type_has_mode_precision_p (TREE_TYPE (@0))
5817 && type_has_mode_precision_p (TREE_TYPE (@1))
5818 && type_has_mode_precision_p (type)
5819 /* The inner conversion must be a widening conversion. */
5820 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5821 && types_match (@0, @1)
5822 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5823 <= TYPE_PRECISION (TREE_TYPE (@0)))
5824 && (wi::to_wide (@4)
5825 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5826 true, TYPE_PRECISION (type))) == 0)
5827 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5828 (with { tree ntype = TREE_TYPE (@0); }
5829 (convert (bit_and (op @0 @1) (convert:ntype @4))))
5830 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5831 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5832 (convert:utype @4))))))))
5834 /* Transform (@0 < @1 and @0 < @2) to use min,
5835 (@0 > @1 and @0 > @2) to use max */
5836 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5837 op (lt le gt ge lt le gt ge )
5838 ext (min min max max max max min min )
5840 (logic (op:cs @0 @1) (op:cs @0 @2))
5841 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5842 && TREE_CODE (@0) != INTEGER_CST)
5843 (op @0 (ext @1 @2)))))
5846 /* signbit(x) -> 0 if x is nonnegative. */
5847 (SIGNBIT tree_expr_nonnegative_p@0)
5848 { integer_zero_node; })
5851 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
5853 (if (!HONOR_SIGNED_ZEROS (@0))
5854 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
5856 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
5858 (for op (plus minus)
5861 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5862 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5863 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
5864 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
5865 && !TYPE_SATURATING (TREE_TYPE (@0)))
5866 (with { tree res = int_const_binop (rop, @2, @1); }
5867 (if (TREE_OVERFLOW (res)
5868 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5869 { constant_boolean_node (cmp == NE_EXPR, type); }
5870 (if (single_use (@3))
5871 (cmp @0 { TREE_OVERFLOW (res)
5872 ? drop_tree_overflow (res) : res; }))))))))
5873 (for cmp (lt le gt ge)
5874 (for op (plus minus)
5877 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5878 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5879 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5880 (with { tree res = int_const_binop (rop, @2, @1); }
5881 (if (TREE_OVERFLOW (res))
5883 fold_overflow_warning (("assuming signed overflow does not occur "
5884 "when simplifying conditional to constant"),
5885 WARN_STRICT_OVERFLOW_CONDITIONAL);
5886 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
5887 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
5888 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
5889 TYPE_SIGN (TREE_TYPE (@1)))
5890 != (op == MINUS_EXPR);
5891 constant_boolean_node (less == ovf_high, type);
5893 (if (single_use (@3))
5896 fold_overflow_warning (("assuming signed overflow does not occur "
5897 "when changing X +- C1 cmp C2 to "
5899 WARN_STRICT_OVERFLOW_COMPARISON);
5901 (cmp @0 { res; })))))))))
5903 /* Canonicalizations of BIT_FIELD_REFs. */
5906 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5907 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5910 (BIT_FIELD_REF (view_convert @0) @1 @2)
5911 (BIT_FIELD_REF @0 @1 @2))
5914 (BIT_FIELD_REF @0 @1 integer_zerop)
5915 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5919 (BIT_FIELD_REF @0 @1 @2)
5921 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5922 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5924 (if (integer_zerop (@2))
5925 (view_convert (realpart @0)))
5926 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5927 (view_convert (imagpart @0)))))
5928 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5929 && INTEGRAL_TYPE_P (type)
5930 /* On GIMPLE this should only apply to register arguments. */
5931 && (! GIMPLE || is_gimple_reg (@0))
5932 /* A bit-field-ref that referenced the full argument can be stripped. */
5933 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5934 && integer_zerop (@2))
5935 /* Low-parts can be reduced to integral conversions.
5936 ??? The following doesn't work for PDP endian. */
5937 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5938 /* Don't even think about BITS_BIG_ENDIAN. */
5939 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5940 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5941 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5942 ? (TYPE_PRECISION (TREE_TYPE (@0))
5943 - TYPE_PRECISION (type))
5947 /* Simplify vector extracts. */
5950 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5951 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5952 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5953 || (VECTOR_TYPE_P (type)
5954 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5957 tree ctor = (TREE_CODE (@0) == SSA_NAME
5958 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5959 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5960 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5961 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5962 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5965 && (idx % width) == 0
5967 && known_le ((idx + n) / width,
5968 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5973 /* Constructor elements can be subvectors. */
5975 if (CONSTRUCTOR_NELTS (ctor) != 0)
5977 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5978 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5979 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5981 unsigned HOST_WIDE_INT elt, count, const_k;
5984 /* We keep an exact subset of the constructor elements. */
5985 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5986 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5987 { build_constructor (type, NULL); }
5989 (if (elt < CONSTRUCTOR_NELTS (ctor))
5990 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5991 { build_zero_cst (type); })
5992 /* We don't want to emit new CTORs unless the old one goes away.
5993 ??? Eventually allow this if the CTOR ends up constant or
5995 (if (single_use (@0))
5997 vec<constructor_elt, va_gc> *vals;
5998 vec_alloc (vals, count);
5999 for (unsigned i = 0;
6000 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6001 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
6002 CONSTRUCTOR_ELT (ctor, elt + i)->value);
6003 build_constructor (type, vals);
6005 /* The bitfield references a single constructor element. */
6006 (if (k.is_constant (&const_k)
6007 && idx + n <= (idx / const_k + 1) * const_k)
6009 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6010 { build_zero_cst (type); })
6012 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6013 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6014 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6016 /* Simplify a bit extraction from a bit insertion for the cases with
6017 the inserted element fully covering the extraction or the insertion
6018 not touching the extraction. */
6020 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6023 unsigned HOST_WIDE_INT isize;
6024 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6025 isize = TYPE_PRECISION (TREE_TYPE (@1));
6027 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6030 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6031 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6032 wi::to_wide (@ipos) + isize))
6033 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6035 - wi::to_wide (@ipos)); }))
6036 (if (wi::geu_p (wi::to_wide (@ipos),
6037 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6038 || wi::geu_p (wi::to_wide (@rpos),
6039 wi::to_wide (@ipos) + isize))
6040 (BIT_FIELD_REF @0 @rsize @rpos)))))
6042 (if (canonicalize_math_after_vectorization_p ())
6045 (fmas:c (negate @0) @1 @2)
6046 (IFN_FNMA @0 @1 @2))
6048 (fmas @0 @1 (negate @2))
6051 (fmas:c (negate @0) @1 (negate @2))
6052 (IFN_FNMS @0 @1 @2))
6054 (negate (fmas@3 @0 @1 @2))
6055 (if (single_use (@3))
6056 (IFN_FNMS @0 @1 @2))))
6059 (IFN_FMS:c (negate @0) @1 @2)
6060 (IFN_FNMS @0 @1 @2))
6062 (IFN_FMS @0 @1 (negate @2))
6065 (IFN_FMS:c (negate @0) @1 (negate @2))
6066 (IFN_FNMA @0 @1 @2))
6068 (negate (IFN_FMS@3 @0 @1 @2))
6069 (if (single_use (@3))
6070 (IFN_FNMA @0 @1 @2)))
6073 (IFN_FNMA:c (negate @0) @1 @2)
6076 (IFN_FNMA @0 @1 (negate @2))
6077 (IFN_FNMS @0 @1 @2))
6079 (IFN_FNMA:c (negate @0) @1 (negate @2))
6082 (negate (IFN_FNMA@3 @0 @1 @2))
6083 (if (single_use (@3))
6084 (IFN_FMS @0 @1 @2)))
6087 (IFN_FNMS:c (negate @0) @1 @2)
6090 (IFN_FNMS @0 @1 (negate @2))
6091 (IFN_FNMA @0 @1 @2))
6093 (IFN_FNMS:c (negate @0) @1 (negate @2))
6096 (negate (IFN_FNMS@3 @0 @1 @2))
6097 (if (single_use (@3))
6098 (IFN_FMA @0 @1 @2))))
6100 /* POPCOUNT simplifications. */
6101 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6103 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6104 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6105 (POPCOUNT (bit_ior @0 @1))))
6107 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6108 (for popcount (POPCOUNT)
6109 (for cmp (le eq ne gt)
6112 (cmp (popcount @0) integer_zerop)
6113 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6115 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6117 (bit_and (POPCOUNT @0) integer_onep)
6120 /* PARITY simplifications. */
6121 /* parity(~X) is parity(X). */
6123 (PARITY (bit_not @0))
6126 /* parity(X)^parity(Y) is parity(X^Y). */
6128 (bit_xor (PARITY:s @0) (PARITY:s @1))
6129 (PARITY (bit_xor @0 @1)))
6131 /* Common POPCOUNT/PARITY simplifications. */
6132 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6133 (for pfun (POPCOUNT PARITY)
6136 (with { wide_int nz = tree_nonzero_bits (@0); }
6140 (if (wi::popcount (nz) == 1)
6141 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6142 (convert (rshift:utype (convert:utype @0)
6143 { build_int_cst (integer_type_node,
6144 wi::ctz (nz)); }))))))))
6147 /* 64- and 32-bits branchless implementations of popcount are detected:
6149 int popcount64c (uint64_t x)
6151 x -= (x >> 1) & 0x5555555555555555ULL;
6152 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6153 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6154 return (x * 0x0101010101010101ULL) >> 56;
6157 int popcount32c (uint32_t x)
6159 x -= (x >> 1) & 0x55555555;
6160 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6161 x = (x + (x >> 4)) & 0x0f0f0f0f;
6162 return (x * 0x01010101) >> 24;
6169 (rshift @8 INTEGER_CST@5)
6171 (bit_and @6 INTEGER_CST@7)
6175 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6181 /* Check constants and optab. */
6182 (with { unsigned prec = TYPE_PRECISION (type);
6183 int shift = (64 - prec) & 63;
6184 unsigned HOST_WIDE_INT c1
6185 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6186 unsigned HOST_WIDE_INT c2
6187 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6188 unsigned HOST_WIDE_INT c3
6189 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6190 unsigned HOST_WIDE_INT c4
6191 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6196 && TYPE_UNSIGNED (type)
6197 && integer_onep (@4)
6198 && wi::to_widest (@10) == 2
6199 && wi::to_widest (@5) == 4
6200 && wi::to_widest (@1) == prec - 8
6201 && tree_to_uhwi (@2) == c1
6202 && tree_to_uhwi (@3) == c2
6203 && tree_to_uhwi (@9) == c3
6204 && tree_to_uhwi (@7) == c3
6205 && tree_to_uhwi (@11) == c4
6206 && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6208 (convert (IFN_POPCOUNT:type @0)))))
6210 /* __builtin_ffs needs to deal on many targets with the possible zero
6211 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6212 should lead to better code. */
6214 (FFS tree_expr_nonzero_p@0)
6215 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6216 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6217 OPTIMIZE_FOR_SPEED))
6218 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6219 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6222 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6224 /* __builtin_ffs (X) == 0 -> X == 0.
6225 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6228 (cmp (ffs@2 @0) INTEGER_CST@1)
6229 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6231 (if (integer_zerop (@1))
6232 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6233 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6234 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6235 (if (single_use (@2))
6236 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6237 wi::mask (tree_to_uhwi (@1),
6239 { wide_int_to_tree (TREE_TYPE (@0),
6240 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6241 false, prec)); }))))))
6243 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
6247 bit_op (bit_and bit_ior)
6249 (cmp (ffs@2 @0) INTEGER_CST@1)
6250 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6252 (if (integer_zerop (@1))
6253 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6254 (if (tree_int_cst_sgn (@1) < 0)
6255 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6256 (if (wi::to_widest (@1) >= prec)
6257 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6258 (if (wi::to_widest (@1) == prec - 1)
6259 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
6260 wi::shifted_mask (prec - 1, 1,
6262 (if (single_use (@2))
6263 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
6265 { wide_int_to_tree (TREE_TYPE (@0),
6266 wi::mask (tree_to_uhwi (@1),
6268 { build_zero_cst (TREE_TYPE (@0)); }))))))))
6277 r = c ? a1 op a2 : b;
6279 if the target can do it in one go. This makes the operation conditional
6280 on c, so could drop potentially-trapping arithmetic, but that's a valid
6281 simplification if the result of the operation isn't needed.
6283 Avoid speculatively generating a stand-alone vector comparison
6284 on targets that might not support them. Any target implementing
6285 conditional internal functions must support the same comparisons
6286 inside and outside a VEC_COND_EXPR. */
6289 (for uncond_op (UNCOND_BINARY)
6290 cond_op (COND_BINARY)
6292 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
6293 (with { tree op_type = TREE_TYPE (@4); }
6294 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6295 && element_precision (type) == element_precision (op_type))
6296 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
6298 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
6299 (with { tree op_type = TREE_TYPE (@4); }
6300 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6301 && element_precision (type) == element_precision (op_type))
6302 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6304 /* Same for ternary operations. */
6305 (for uncond_op (UNCOND_TERNARY)
6306 cond_op (COND_TERNARY)
6308 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
6309 (with { tree op_type = TREE_TYPE (@5); }
6310 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6311 && element_precision (type) == element_precision (op_type))
6312 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
6314 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
6315 (with { tree op_type = TREE_TYPE (@5); }
6316 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6317 && element_precision (type) == element_precision (op_type))
6318 (view_convert (cond_op (bit_not @0) @2 @3 @4
6319 (view_convert:op_type @1)))))))
6322 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
6323 "else" value of an IFN_COND_*. */
6324 (for cond_op (COND_BINARY)
6326 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
6327 (with { tree op_type = TREE_TYPE (@3); }
6328 (if (element_precision (type) == element_precision (op_type))
6329 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
6331 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
6332 (with { tree op_type = TREE_TYPE (@5); }
6333 (if (inverse_conditions_p (@0, @2)
6334 && element_precision (type) == element_precision (op_type))
6335 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
6337 /* Same for ternary operations. */
6338 (for cond_op (COND_TERNARY)
6340 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
6341 (with { tree op_type = TREE_TYPE (@4); }
6342 (if (element_precision (type) == element_precision (op_type))
6343 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
6345 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
6346 (with { tree op_type = TREE_TYPE (@6); }
6347 (if (inverse_conditions_p (@0, @2)
6348 && element_precision (type) == element_precision (op_type))
6349 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
6351 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
6354 A: (@0 + @1 < @2) | (@2 + @1 < @0)
6355 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
6357 If pointers are known not to wrap, B checks whether @1 bytes starting
6358 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
6359 bytes. A is more efficiently tested as:
6361 A: (sizetype) (@0 + @1 - @2) > @1 * 2
6363 The equivalent expression for B is given by replacing @1 with @1 - 1:
6365 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
6367 @0 and @2 can be swapped in both expressions without changing the result.
6369 The folds rely on sizetype's being unsigned (which is always true)
6370 and on its being the same width as the pointer (which we have to check).
6372 The fold replaces two pointer_plus expressions, two comparisons and
6373 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
6374 the best case it's a saving of two operations. The A fold retains one
6375 of the original pointer_pluses, so is a win even if both pointer_pluses
6376 are used elsewhere. The B fold is a wash if both pointer_pluses are
6377 used elsewhere, since all we end up doing is replacing a comparison with
6378 a pointer_plus. We do still apply the fold under those circumstances
6379 though, in case applying it to other conditions eventually makes one of the
6380 pointer_pluses dead. */
6381 (for ior (truth_orif truth_or bit_ior)
6384 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
6385 (cmp:cs (pointer_plus@4 @2 @1) @0))
6386 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6387 && TYPE_OVERFLOW_WRAPS (sizetype)
6388 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
6389 /* Calculate the rhs constant. */
6390 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
6391 offset_int rhs = off * 2; }
6392 /* Always fails for negative values. */
6393 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
6394 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
6395 pick a canonical order. This increases the chances of using the
6396 same pointer_plus in multiple checks. */
6397 (with { bool swap_p = tree_swap_operands_p (@0, @2);
6398 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
6399 (if (cmp == LT_EXPR)
6400 (gt (convert:sizetype
6401 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
6402 { swap_p ? @0 : @2; }))
6404 (gt (convert:sizetype
6405 (pointer_diff:ssizetype
6406 (pointer_plus { swap_p ? @2 : @0; }
6407 { wide_int_to_tree (sizetype, off); })
6408 { swap_p ? @0 : @2; }))
6409 { rhs_tree; })))))))))
6411 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6413 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6414 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6415 (with { int i = single_nonzero_element (@1); }
6417 (with { tree elt = vector_cst_elt (@1, i);
6418 tree elt_type = TREE_TYPE (elt);
6419 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6420 tree size = bitsize_int (elt_bits);
6421 tree pos = bitsize_int (elt_bits * i); }
6424 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6428 (vec_perm @0 @1 VECTOR_CST@2)
6431 tree op0 = @0, op1 = @1, op2 = @2;
6433 /* Build a vector of integers from the tree mask. */
6434 vec_perm_builder builder;
6435 if (!tree_to_vec_perm_builder (&builder, op2))
6438 /* Create a vec_perm_indices for the integer vector. */
6439 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6440 bool single_arg = (op0 == op1);
6441 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6443 (if (sel.series_p (0, 1, 0, 1))
6445 (if (sel.series_p (0, 1, nelts, 1))
6451 if (sel.all_from_input_p (0))
6453 else if (sel.all_from_input_p (1))
6456 sel.rotate_inputs (1);
6458 else if (known_ge (poly_uint64 (sel[0]), nelts))
6460 std::swap (op0, op1);
6461 sel.rotate_inputs (1);
6465 tree cop0 = op0, cop1 = op1;
6466 if (TREE_CODE (op0) == SSA_NAME
6467 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6468 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6469 cop0 = gimple_assign_rhs1 (def);
6470 if (TREE_CODE (op1) == SSA_NAME
6471 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6472 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6473 cop1 = gimple_assign_rhs1 (def);
6477 (if ((TREE_CODE (cop0) == VECTOR_CST
6478 || TREE_CODE (cop0) == CONSTRUCTOR)
6479 && (TREE_CODE (cop1) == VECTOR_CST
6480 || TREE_CODE (cop1) == CONSTRUCTOR)
6481 && (t = fold_vec_perm (type, cop0, cop1, sel)))
6485 bool changed = (op0 == op1 && !single_arg);
6486 tree ins = NULL_TREE;
6489 /* See if the permutation is performing a single element
6490 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6491 in that case. But only if the vector mode is supported,
6492 otherwise this is invalid GIMPLE. */
6493 if (TYPE_MODE (type) != BLKmode
6494 && (TREE_CODE (cop0) == VECTOR_CST
6495 || TREE_CODE (cop0) == CONSTRUCTOR
6496 || TREE_CODE (cop1) == VECTOR_CST
6497 || TREE_CODE (cop1) == CONSTRUCTOR))
6499 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6502 /* After canonicalizing the first elt to come from the
6503 first vector we only can insert the first elt from
6504 the first vector. */
6506 if ((ins = fold_read_from_vector (cop0, sel[0])))
6509 /* The above can fail for two-element vectors which always
6510 appear to insert the first element, so try inserting
6511 into the second lane as well. For more than two
6512 elements that's wasted time. */
6513 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6515 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6516 for (at = 0; at < encoded_nelts; ++at)
6517 if (maybe_ne (sel[at], at))
6519 if (at < encoded_nelts
6520 && (known_eq (at + 1, nelts)
6521 || sel.series_p (at + 1, 1, at + 1, 1)))
6523 if (known_lt (poly_uint64 (sel[at]), nelts))
6524 ins = fold_read_from_vector (cop0, sel[at]);
6526 ins = fold_read_from_vector (cop1, sel[at] - nelts);
6531 /* Generate a canonical form of the selector. */
6532 if (!ins && sel.encoding () != builder)
6534 /* Some targets are deficient and fail to expand a single
6535 argument permutation while still allowing an equivalent
6536 2-argument version. */
6538 if (sel.ninputs () == 2
6539 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6540 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6543 vec_perm_indices sel2 (builder, 2, nelts);
6544 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6545 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6547 /* Not directly supported with either encoding,
6548 so use the preferred form. */
6549 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6551 if (!operand_equal_p (op2, oldop2, 0))
6556 (bit_insert { op0; } { ins; }
6557 { bitsize_int (at * vector_element_bits (type)); })
6559 (vec_perm { op0; } { op1; } { op2; }))))))))))
6561 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
6563 (match vec_same_elem_p
6565 (if (uniform_vector_p (@0))))
6567 (match vec_same_elem_p
6571 (vec_perm vec_same_elem_p@0 @0 @1)
6574 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6575 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6576 constant which when multiplied by a power of 2 contains a unique value
6577 in the top 5 or 6 bits. This is then indexed into a table which maps it
6578 to the number of trailing zeroes. */
6579 (match (ctz_table_index @1 @2 @3)
6580 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))