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-2021 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, @0, @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, @0, @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) || !tree_expr_maybe_nan_p (@0))
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 (!tree_expr_maybe_nan_p (@0)
210 && !tree_expr_maybe_real_minus_zero_p (@0)
211 && !tree_expr_maybe_real_minus_zero_p (@1))
214 /* In IEEE floating point, x*1 is not equivalent to x for snans.
215 Likewise for complex arithmetic with signed zeros. */
218 (if (!tree_expr_maybe_signaling_nan_p (@0)
219 && (!HONOR_SIGNED_ZEROS (type)
220 || !COMPLEX_FLOAT_TYPE_P (type)))
223 /* Transform x * -1.0 into -x. */
225 (mult @0 real_minus_onep)
226 (if (!tree_expr_maybe_signaling_nan_p (@0)
227 && (!HONOR_SIGNED_ZEROS (type)
228 || !COMPLEX_FLOAT_TYPE_P (type)))
231 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
233 (mult SSA_NAME@1 SSA_NAME@2)
234 (if (INTEGRAL_TYPE_P (type)
235 && get_nonzero_bits (@1) == 1
236 && get_nonzero_bits (@2) == 1)
239 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
240 unless the target has native support for the former but not the latter. */
242 (mult @0 VECTOR_CST@1)
243 (if (initializer_each_zero_or_onep (@1)
244 && !HONOR_SNANS (type)
245 && !HONOR_SIGNED_ZEROS (type))
246 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
248 && (!VECTOR_MODE_P (TYPE_MODE (type))
249 || (VECTOR_MODE_P (TYPE_MODE (itype))
250 && optab_handler (and_optab,
251 TYPE_MODE (itype)) != CODE_FOR_nothing)))
252 (view_convert (bit_and:itype (view_convert @0)
253 (ne @1 { build_zero_cst (type); })))))))
255 (for cmp (gt ge lt le)
256 outp (convert convert negate negate)
257 outn (negate negate convert convert)
258 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
259 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
260 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
261 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
263 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
264 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
266 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
267 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
268 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
269 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
271 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
272 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
275 /* Transform X * copysign (1.0, X) into abs(X). */
277 (mult:c @0 (COPYSIGN_ALL real_onep @0))
278 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
281 /* Transform X * copysign (1.0, -X) into -abs(X). */
283 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
284 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
287 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
289 (COPYSIGN_ALL REAL_CST@0 @1)
290 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
291 (COPYSIGN_ALL (negate @0) @1)))
293 /* X * 1, X / 1 -> X. */
294 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
299 /* (A / (1 << B)) -> (A >> B).
300 Only for unsigned A. For signed A, this would not preserve rounding
302 For example: (-1 / ( 1 << B)) != -1 >> B.
303 Also also widening conversions, like:
304 (A / (unsigned long long) (1U << B)) -> (A >> B)
306 (A / (unsigned long long) (1 << B)) -> (A >> B).
307 If the left shift is signed, it can be done only if the upper bits
308 of A starting from shift's type sign bit are zero, as
309 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
310 so it is valid only if A >> 31 is zero. */
312 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
313 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
314 && (!VECTOR_TYPE_P (type)
315 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
316 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
317 && (useless_type_conversion_p (type, TREE_TYPE (@1))
318 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
319 && (TYPE_UNSIGNED (TREE_TYPE (@1))
320 || (element_precision (type)
321 == element_precision (TREE_TYPE (@1)))
322 || (INTEGRAL_TYPE_P (type)
323 && (tree_nonzero_bits (@0)
324 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
326 element_precision (type))) == 0)))))
327 (if (!VECTOR_TYPE_P (type)
328 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
329 && element_precision (TREE_TYPE (@3)) < element_precision (type))
330 (convert (rshift @3 @2))
333 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
334 undefined behavior in constexpr evaluation, and assuming that the division
335 traps enables better optimizations than these anyway. */
336 (for div (trunc_div ceil_div floor_div round_div exact_div)
337 /* 0 / X is always zero. */
339 (div integer_zerop@0 @1)
340 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
341 (if (!integer_zerop (@1))
345 (div @0 integer_minus_onep@1)
346 (if (!TYPE_UNSIGNED (type))
348 /* X / bool_range_Y is X. */
351 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
356 /* But not for 0 / 0 so that we can get the proper warnings and errors.
357 And not for _Fract types where we can't build 1. */
358 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
359 { build_one_cst (type); }))
360 /* X / abs (X) is X < 0 ? -1 : 1. */
363 (if (INTEGRAL_TYPE_P (type)
364 && TYPE_OVERFLOW_UNDEFINED (type))
365 (cond (lt @0 { build_zero_cst (type); })
366 { build_minus_one_cst (type); } { build_one_cst (type); })))
369 (div:C @0 (negate @0))
370 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
371 && TYPE_OVERFLOW_UNDEFINED (type))
372 { build_minus_one_cst (type); })))
374 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
375 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
378 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
379 && TYPE_UNSIGNED (type))
382 /* Combine two successive divisions. Note that combining ceil_div
383 and floor_div is trickier and combining round_div even more so. */
384 (for div (trunc_div exact_div)
386 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
388 wi::overflow_type overflow;
389 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
390 TYPE_SIGN (type), &overflow);
392 (if (div == EXACT_DIV_EXPR
393 || optimize_successive_divisions_p (@2, @3))
395 (div @0 { wide_int_to_tree (type, mul); })
396 (if (TYPE_UNSIGNED (type)
397 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
398 { build_zero_cst (type); }))))))
400 /* Combine successive multiplications. Similar to above, but handling
401 overflow is different. */
403 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
405 wi::overflow_type overflow;
406 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
407 TYPE_SIGN (type), &overflow);
409 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
410 otherwise undefined overflow implies that @0 must be zero. */
411 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
412 (mult @0 { wide_int_to_tree (type, mul); }))))
414 /* Optimize A / A to 1.0 if we don't care about
415 NaNs or Infinities. */
418 (if (FLOAT_TYPE_P (type)
419 && ! HONOR_NANS (type)
420 && ! HONOR_INFINITIES (type))
421 { build_one_cst (type); }))
423 /* Optimize -A / A to -1.0 if we don't care about
424 NaNs or Infinities. */
426 (rdiv:C @0 (negate @0))
427 (if (FLOAT_TYPE_P (type)
428 && ! HONOR_NANS (type)
429 && ! HONOR_INFINITIES (type))
430 { build_minus_one_cst (type); }))
432 /* PR71078: x / abs(x) -> copysign (1.0, x) */
434 (rdiv:C (convert? @0) (convert? (abs @0)))
435 (if (SCALAR_FLOAT_TYPE_P (type)
436 && ! HONOR_NANS (type)
437 && ! HONOR_INFINITIES (type))
439 (if (types_match (type, float_type_node))
440 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
441 (if (types_match (type, double_type_node))
442 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
443 (if (types_match (type, long_double_type_node))
444 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
446 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
449 (if (!tree_expr_maybe_signaling_nan_p (@0))
452 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
454 (rdiv @0 real_minus_onep)
455 (if (!tree_expr_maybe_signaling_nan_p (@0))
458 (if (flag_reciprocal_math)
459 /* Convert (A/B)/C to A/(B*C). */
461 (rdiv (rdiv:s @0 @1) @2)
462 (rdiv @0 (mult @1 @2)))
464 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
466 (rdiv @0 (mult:s @1 REAL_CST@2))
468 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
470 (rdiv (mult @0 { tem; } ) @1))))
472 /* Convert A/(B/C) to (A/B)*C */
474 (rdiv @0 (rdiv:s @1 @2))
475 (mult (rdiv @0 @1) @2)))
477 /* Simplify x / (- y) to -x / y. */
479 (rdiv @0 (negate @1))
480 (rdiv (negate @0) @1))
482 (if (flag_unsafe_math_optimizations)
483 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
484 Since C / x may underflow to zero, do this only for unsafe math. */
485 (for op (lt le gt ge)
488 (op (rdiv REAL_CST@0 @1) real_zerop@2)
489 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
491 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
493 /* For C < 0, use the inverted operator. */
494 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
497 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
498 (for div (trunc_div ceil_div floor_div round_div exact_div)
500 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
501 (if (integer_pow2p (@2)
502 && tree_int_cst_sgn (@2) > 0
503 && tree_nop_conversion_p (type, TREE_TYPE (@0))
504 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
506 { build_int_cst (integer_type_node,
507 wi::exact_log2 (wi::to_wide (@2))); }))))
509 /* If ARG1 is a constant, we can convert this to a multiply by the
510 reciprocal. This does not have the same rounding properties,
511 so only do this if -freciprocal-math. We can actually
512 always safely do it if ARG1 is a power of two, but it's hard to
513 tell if it is or not in a portable manner. */
514 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
518 (if (flag_reciprocal_math
521 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
523 (mult @0 { tem; } )))
524 (if (cst != COMPLEX_CST)
525 (with { tree inverse = exact_inverse (type, @1); }
527 (mult @0 { inverse; } ))))))))
529 (for mod (ceil_mod floor_mod round_mod trunc_mod)
530 /* 0 % X is always zero. */
532 (mod integer_zerop@0 @1)
533 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
534 (if (!integer_zerop (@1))
536 /* X % 1 is always zero. */
538 (mod @0 integer_onep)
539 { build_zero_cst (type); })
540 /* X % -1 is zero. */
542 (mod @0 integer_minus_onep@1)
543 (if (!TYPE_UNSIGNED (type))
544 { build_zero_cst (type); }))
548 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
549 (if (!integer_zerop (@0))
550 { build_zero_cst (type); }))
551 /* (X % Y) % Y is just X % Y. */
553 (mod (mod@2 @0 @1) @1)
555 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
557 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
558 (if (ANY_INTEGRAL_TYPE_P (type)
559 && TYPE_OVERFLOW_UNDEFINED (type)
560 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
562 { build_zero_cst (type); }))
563 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
564 modulo and comparison, since it is simpler and equivalent. */
567 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
568 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
569 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
570 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
572 /* X % -C is the same as X % C. */
574 (trunc_mod @0 INTEGER_CST@1)
575 (if (TYPE_SIGN (type) == SIGNED
576 && !TREE_OVERFLOW (@1)
577 && wi::neg_p (wi::to_wide (@1))
578 && !TYPE_OVERFLOW_TRAPS (type)
579 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
580 && !sign_bit_p (@1, @1))
581 (trunc_mod @0 (negate @1))))
583 /* X % -Y is the same as X % Y. */
585 (trunc_mod @0 (convert? (negate @1)))
586 (if (INTEGRAL_TYPE_P (type)
587 && !TYPE_UNSIGNED (type)
588 && !TYPE_OVERFLOW_TRAPS (type)
589 && tree_nop_conversion_p (type, TREE_TYPE (@1))
590 /* Avoid this transformation if X might be INT_MIN or
591 Y might be -1, because we would then change valid
592 INT_MIN % -(-1) into invalid INT_MIN % -1. */
593 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
594 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
596 (trunc_mod @0 (convert @1))))
598 /* X - (X / Y) * Y is the same as X % Y. */
600 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
601 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
602 (convert (trunc_mod @0 @1))))
604 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
605 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
606 Also optimize A % (C << N) where C is a power of 2,
607 to A & ((C << N) - 1).
608 Also optimize "A shift (B % C)", if C is a power of 2, to
609 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
610 and assume (B % C) is nonnegative as shifts negative values would
612 (match (power_of_two_cand @1)
614 (match (power_of_two_cand @1)
615 (lshift INTEGER_CST@1 @2))
616 (for mod (trunc_mod floor_mod)
617 (for shift (lshift rshift)
619 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
620 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
621 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
624 (mod @0 (convert? (power_of_two_cand@1 @2)))
625 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
626 /* Allow any integral conversions of the divisor, except
627 conversion from narrower signed to wider unsigned type
628 where if @1 would be negative power of two, the divisor
629 would not be a power of two. */
630 && INTEGRAL_TYPE_P (type)
631 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
632 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
633 || TYPE_UNSIGNED (TREE_TYPE (@1))
634 || !TYPE_UNSIGNED (type))
635 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
636 (with { tree utype = TREE_TYPE (@1);
637 if (!TYPE_OVERFLOW_WRAPS (utype))
638 utype = unsigned_type_for (utype); }
639 (bit_and @0 (convert (minus (convert:utype @1)
640 { build_one_cst (utype); })))))))
642 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
644 (trunc_div (mult @0 integer_pow2p@1) @1)
645 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
646 (bit_and @0 { wide_int_to_tree
647 (type, wi::mask (TYPE_PRECISION (type)
648 - wi::exact_log2 (wi::to_wide (@1)),
649 false, TYPE_PRECISION (type))); })))
651 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
653 (mult (trunc_div @0 integer_pow2p@1) @1)
654 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
655 (bit_and @0 (negate @1))))
657 /* Simplify (t * 2) / 2) -> t. */
658 (for div (trunc_div ceil_div floor_div round_div exact_div)
660 (div (mult:c @0 @1) @1)
661 (if (ANY_INTEGRAL_TYPE_P (type))
662 (if (TYPE_OVERFLOW_UNDEFINED (type))
667 bool overflowed = true;
668 value_range vr0, vr1;
669 if (INTEGRAL_TYPE_P (type)
670 && get_global_range_query ()->range_of_expr (vr0, @0)
671 && get_global_range_query ()->range_of_expr (vr1, @1)
672 && vr0.kind () == VR_RANGE
673 && vr1.kind () == VR_RANGE)
675 wide_int wmin0 = vr0.lower_bound ();
676 wide_int wmax0 = vr0.upper_bound ();
677 wide_int wmin1 = vr1.lower_bound ();
678 wide_int wmax1 = vr1.upper_bound ();
679 /* If the multiplication can't overflow/wrap around, then
680 it can be optimized too. */
681 wi::overflow_type min_ovf, max_ovf;
682 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
683 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
684 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
686 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
687 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
688 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
699 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
704 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
707 (pows (op @0) REAL_CST@1)
708 (with { HOST_WIDE_INT n; }
709 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
711 /* Likewise for powi. */
714 (pows (op @0) INTEGER_CST@1)
715 (if ((wi::to_wide (@1) & 1) == 0)
717 /* Strip negate and abs from both operands of hypot. */
725 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
726 (for copysigns (COPYSIGN_ALL)
728 (copysigns (op @0) @1)
731 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
736 /* Convert absu(x)*absu(x) -> x*x. */
738 (mult (absu@1 @0) @1)
739 (mult (convert@2 @0) @2))
741 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
745 (coss (copysigns @0 @1))
748 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
752 (pows (copysigns @0 @2) REAL_CST@1)
753 (with { HOST_WIDE_INT n; }
754 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
756 /* Likewise for powi. */
760 (pows (copysigns @0 @2) INTEGER_CST@1)
761 (if ((wi::to_wide (@1) & 1) == 0)
766 /* hypot(copysign(x, y), z) -> hypot(x, z). */
768 (hypots (copysigns @0 @1) @2)
770 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
772 (hypots @0 (copysigns @1 @2))
775 /* copysign(x, CST) -> [-]abs (x). */
776 (for copysigns (COPYSIGN_ALL)
778 (copysigns @0 REAL_CST@1)
779 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
783 /* copysign(copysign(x, y), z) -> copysign(x, z). */
784 (for copysigns (COPYSIGN_ALL)
786 (copysigns (copysigns @0 @1) @2)
789 /* copysign(x,y)*copysign(x,y) -> x*x. */
790 (for copysigns (COPYSIGN_ALL)
792 (mult (copysigns@2 @0 @1) @2)
795 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
796 (for ccoss (CCOS CCOSH)
801 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
802 (for ops (conj negate)
808 /* Fold (a * (1 << b)) into (a << b) */
810 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
811 (if (! FLOAT_TYPE_P (type)
812 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
815 /* Fold (1 << (C - x)) where C = precision(type) - 1
816 into ((1 << C) >> x). */
818 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
819 (if (INTEGRAL_TYPE_P (type)
820 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
822 (if (TYPE_UNSIGNED (type))
823 (rshift (lshift @0 @2) @3)
825 { tree utype = unsigned_type_for (type); }
826 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
828 /* Fold (C1/X)*C2 into (C1*C2)/X. */
830 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
831 (if (flag_associative_math
834 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
836 (rdiv { tem; } @1)))))
838 /* Simplify ~X & X as zero. */
840 (bit_and:c (convert? @0) (convert? (bit_not @0)))
841 { build_zero_cst (type); })
843 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
845 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
846 (if (TYPE_UNSIGNED (type))
847 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
849 (for bitop (bit_and bit_ior)
851 /* PR35691: Transform
852 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
853 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
855 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
856 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
857 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
858 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
859 (cmp (bit_ior @0 (convert @1)) @2)))
861 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
862 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
864 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
865 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
866 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
867 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
868 (cmp (bit_and @0 (convert @1)) @2))))
870 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
872 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
873 (minus (bit_xor @0 @1) @1))
875 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
876 (if (~wi::to_wide (@2) == wi::to_wide (@1))
877 (minus (bit_xor @0 @1) @1)))
879 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
881 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
882 (minus @1 (bit_xor @0 @1)))
884 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
885 (for op (bit_ior bit_xor plus)
887 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
890 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
891 (if (~wi::to_wide (@2) == wi::to_wide (@1))
894 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
896 (bit_ior:c (bit_xor:c @0 @1) @0)
899 /* (a & ~b) | (a ^ b) --> a ^ b */
901 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
904 /* (a & ~b) ^ ~a --> ~(a & b) */
906 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
907 (bit_not (bit_and @0 @1)))
909 /* (~a & b) ^ a --> (a | b) */
911 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
914 /* (a | b) & ~(a ^ b) --> a & b */
916 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
919 /* a | ~(a ^ b) --> a | ~b */
921 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
922 (bit_ior @0 (bit_not @1)))
924 /* (a | b) | (a &^ b) --> a | b */
925 (for op (bit_and bit_xor)
927 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
930 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
932 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
935 /* ~(~a & b) --> a | ~b */
937 (bit_not (bit_and:cs (bit_not @0) @1))
938 (bit_ior @0 (bit_not @1)))
940 /* ~(~a | b) --> a & ~b */
942 (bit_not (bit_ior:cs (bit_not @0) @1))
943 (bit_and @0 (bit_not @1)))
945 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
947 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
948 (bit_and @3 (bit_not @2)))
950 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
952 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
956 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
958 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
959 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
961 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
963 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
964 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
966 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
968 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
969 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
970 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
974 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
975 ((A & N) + B) & M -> (A + B) & M
976 Similarly if (N & M) == 0,
977 ((A | N) + B) & M -> (A + B) & M
978 and for - instead of + (or unary - instead of +)
979 and/or ^ instead of |.
980 If B is constant and (B & M) == 0, fold into A & M. */
982 (for bitop (bit_and bit_ior bit_xor)
984 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
987 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
988 @3, @4, @1, ERROR_MARK, NULL_TREE,
991 (convert (bit_and (op (convert:utype { pmop[0]; })
992 (convert:utype { pmop[1]; }))
993 (convert:utype @2))))))
995 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
998 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
999 NULL_TREE, NULL_TREE, @1, bitop, @3,
1002 (convert (bit_and (op (convert:utype { pmop[0]; })
1003 (convert:utype { pmop[1]; }))
1004 (convert:utype @2)))))))
1006 (bit_and (op:s @0 @1) INTEGER_CST@2)
1009 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1010 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1011 NULL_TREE, NULL_TREE, pmop); }
1013 (convert (bit_and (op (convert:utype { pmop[0]; })
1014 (convert:utype { pmop[1]; }))
1015 (convert:utype @2)))))))
1016 (for bitop (bit_and bit_ior bit_xor)
1018 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1021 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1022 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1023 NULL_TREE, NULL_TREE, pmop); }
1025 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1026 (convert:utype @1)))))))
1028 /* X % Y is smaller than Y. */
1031 (cmp (trunc_mod @0 @1) @1)
1032 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1033 { constant_boolean_node (cmp == LT_EXPR, type); })))
1036 (cmp @1 (trunc_mod @0 @1))
1037 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1038 { constant_boolean_node (cmp == GT_EXPR, type); })))
1042 (bit_ior @0 integer_all_onesp@1)
1047 (bit_ior @0 integer_zerop)
1052 (bit_and @0 integer_zerop@1)
1058 (for op (bit_ior bit_xor plus)
1060 (op:c (convert? @0) (convert? (bit_not @0)))
1061 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1066 { build_zero_cst (type); })
1068 /* Canonicalize X ^ ~0 to ~X. */
1070 (bit_xor @0 integer_all_onesp@1)
1075 (bit_and @0 integer_all_onesp)
1078 /* x & x -> x, x | x -> x */
1079 (for bitop (bit_and bit_ior)
1084 /* x & C -> x if we know that x & ~C == 0. */
1087 (bit_and SSA_NAME@0 INTEGER_CST@1)
1088 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1089 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1093 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1095 (bit_not (minus (bit_not @0) @1))
1098 (bit_not (plus:c (bit_not @0) @1))
1101 /* ~(X - Y) -> ~X + Y. */
1103 (bit_not (minus:s @0 @1))
1104 (plus (bit_not @0) @1))
1106 (bit_not (plus:s @0 INTEGER_CST@1))
1107 (if ((INTEGRAL_TYPE_P (type)
1108 && TYPE_UNSIGNED (type))
1109 || (!TYPE_OVERFLOW_SANITIZED (type)
1110 && may_negate_without_overflow_p (@1)))
1111 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1114 /* ~X + Y -> (Y - X) - 1. */
1116 (plus:c (bit_not @0) @1)
1117 (if (ANY_INTEGRAL_TYPE_P (type)
1118 && TYPE_OVERFLOW_WRAPS (type)
1119 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1120 && !integer_all_onesp (@1))
1121 (plus (minus @1 @0) { build_minus_one_cst (type); })
1122 (if (INTEGRAL_TYPE_P (type)
1123 && TREE_CODE (@1) == INTEGER_CST
1124 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1126 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1128 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1130 (bit_not (rshift:s @0 @1))
1131 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1132 (rshift (bit_not! @0) @1)
1133 /* For logical right shifts, this is possible only if @0 doesn't
1134 have MSB set and the logical right shift is changed into
1135 arithmetic shift. */
1136 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1137 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1138 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1141 /* x + (x & 1) -> (x + 1) & ~1 */
1143 (plus:c @0 (bit_and:s @0 integer_onep@1))
1144 (bit_and (plus @0 @1) (bit_not @1)))
1146 /* x & ~(x & y) -> x & ~y */
1147 /* x | ~(x | y) -> x | ~y */
1148 (for bitop (bit_and bit_ior)
1150 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1151 (bitop @0 (bit_not @1))))
1153 /* (~x & y) | ~(x | y) -> ~x */
1155 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1158 /* (x | y) ^ (x | ~y) -> ~x */
1160 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1163 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1165 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1166 (bit_not (bit_xor @0 @1)))
1168 /* (~x | y) ^ (x ^ y) -> x | ~y */
1170 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1171 (bit_ior @0 (bit_not @1)))
1173 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1175 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1176 (bit_not (bit_and @0 @1)))
1178 /* (x | y) & ~x -> y & ~x */
1179 /* (x & y) | ~x -> y | ~x */
1180 (for bitop (bit_and bit_ior)
1181 rbitop (bit_ior bit_and)
1183 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1186 /* (x & y) ^ (x | y) -> x ^ y */
1188 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1191 /* (x ^ y) ^ (x | y) -> x & y */
1193 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1196 /* (x & y) + (x ^ y) -> x | y */
1197 /* (x & y) | (x ^ y) -> x | y */
1198 /* (x & y) ^ (x ^ y) -> x | y */
1199 (for op (plus bit_ior bit_xor)
1201 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1204 /* (x & y) + (x | y) -> x + y */
1206 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1209 /* (x + y) - (x | y) -> x & y */
1211 (minus (plus @0 @1) (bit_ior @0 @1))
1212 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1213 && !TYPE_SATURATING (type))
1216 /* (x + y) - (x & y) -> x | y */
1218 (minus (plus @0 @1) (bit_and @0 @1))
1219 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1220 && !TYPE_SATURATING (type))
1223 /* (x | y) - y -> (x & ~y) */
1225 (minus (bit_ior:cs @0 @1) @1)
1226 (bit_and @0 (bit_not @1)))
1228 /* (x | y) - (x ^ y) -> x & y */
1230 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1233 /* (x | y) - (x & y) -> x ^ y */
1235 (minus (bit_ior @0 @1) (bit_and @0 @1))
1238 /* (x | y) & ~(x & y) -> x ^ y */
1240 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1243 /* (x | y) & (~x ^ y) -> x & y */
1245 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1248 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1250 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1251 (bit_not (bit_xor @0 @1)))
1253 /* (~x | y) ^ (x | ~y) -> x ^ y */
1255 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1258 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1260 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1261 (nop_convert2? (bit_ior @0 @1))))
1263 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1264 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1265 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1266 && !TYPE_SATURATING (TREE_TYPE (@2)))
1267 (bit_not (convert (bit_xor @0 @1)))))
1269 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1271 (nop_convert3? (bit_ior @0 @1)))
1272 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1273 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1274 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1275 && !TYPE_SATURATING (TREE_TYPE (@2)))
1276 (bit_not (convert (bit_xor @0 @1)))))
1278 (minus (nop_convert1? (bit_and @0 @1))
1279 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1281 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1282 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1283 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1284 && !TYPE_SATURATING (TREE_TYPE (@2)))
1285 (bit_not (convert (bit_xor @0 @1)))))
1287 /* ~x & ~y -> ~(x | y)
1288 ~x | ~y -> ~(x & y) */
1289 (for op (bit_and bit_ior)
1290 rop (bit_ior bit_and)
1292 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1293 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1294 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1295 (bit_not (rop (convert @0) (convert @1))))))
1297 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1298 with a constant, and the two constants have no bits in common,
1299 we should treat this as a BIT_IOR_EXPR since this may produce more
1301 (for op (bit_xor plus)
1303 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1304 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1305 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1306 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1307 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1308 (bit_ior (convert @4) (convert @5)))))
1310 /* (X | Y) ^ X -> Y & ~ X*/
1312 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1313 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1314 (convert (bit_and @1 (bit_not @0)))))
1316 /* Convert ~X ^ ~Y to X ^ Y. */
1318 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1319 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1320 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1321 (bit_xor (convert @0) (convert @1))))
1323 /* Convert ~X ^ C to X ^ ~C. */
1325 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1326 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1327 (bit_xor (convert @0) (bit_not @1))))
1329 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1330 (for opo (bit_and bit_xor)
1331 opi (bit_xor bit_and)
1333 (opo:c (opi:cs @0 @1) @1)
1334 (bit_and (bit_not @0) @1)))
1336 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1337 operands are another bit-wise operation with a common input. If so,
1338 distribute the bit operations to save an operation and possibly two if
1339 constants are involved. For example, convert
1340 (A | B) & (A | C) into A | (B & C)
1341 Further simplification will occur if B and C are constants. */
1342 (for op (bit_and bit_ior bit_xor)
1343 rop (bit_ior bit_and bit_and)
1345 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1346 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1347 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1348 (rop (convert @0) (op (convert @1) (convert @2))))))
1350 /* Some simple reassociation for bit operations, also handled in reassoc. */
1351 /* (X & Y) & Y -> X & Y
1352 (X | Y) | Y -> X | Y */
1353 (for op (bit_and bit_ior)
1355 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1357 /* (X ^ Y) ^ Y -> X */
1359 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1361 /* (X & Y) & (X & Z) -> (X & Y) & Z
1362 (X | Y) | (X | Z) -> (X | Y) | Z */
1363 (for op (bit_and bit_ior)
1365 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1366 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1367 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1368 (if (single_use (@5) && single_use (@6))
1369 (op @3 (convert @2))
1370 (if (single_use (@3) && single_use (@4))
1371 (op (convert @1) @5))))))
1372 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1374 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1375 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1376 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1377 (bit_xor (convert @1) (convert @2))))
1379 /* Convert abs (abs (X)) into abs (X).
1380 also absu (absu (X)) into absu (X). */
1386 (absu (convert@2 (absu@1 @0)))
1387 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1390 /* Convert abs[u] (-X) -> abs[u] (X). */
1399 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1401 (abs tree_expr_nonnegative_p@0)
1405 (absu tree_expr_nonnegative_p@0)
1408 /* Simplify (-(X < 0) | 1) * X into abs (X). */
1410 (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
1411 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
1414 /* Similarly (-(X < 0) | 1U) * X into absu (X). */
1416 (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
1417 integer_onep) (nop_convert @0))
1418 (if (INTEGRAL_TYPE_P (type)
1419 && TYPE_UNSIGNED (type)
1420 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1421 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1424 /* A few cases of fold-const.c negate_expr_p predicate. */
1425 (match negate_expr_p
1427 (if ((INTEGRAL_TYPE_P (type)
1428 && TYPE_UNSIGNED (type))
1429 || (!TYPE_OVERFLOW_SANITIZED (type)
1430 && may_negate_without_overflow_p (t)))))
1431 (match negate_expr_p
1433 (match negate_expr_p
1435 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1436 (match negate_expr_p
1438 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1439 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1441 (match negate_expr_p
1443 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1444 (match negate_expr_p
1446 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1447 || (FLOAT_TYPE_P (type)
1448 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1449 && !HONOR_SIGNED_ZEROS (type)))))
1451 /* (-A) * (-B) -> A * B */
1453 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1454 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1455 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1456 (mult (convert @0) (convert (negate @1)))))
1458 /* -(A + B) -> (-B) - A. */
1460 (negate (plus:c @0 negate_expr_p@1))
1461 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1462 && !HONOR_SIGNED_ZEROS (type))
1463 (minus (negate @1) @0)))
1465 /* -(A - B) -> B - A. */
1467 (negate (minus @0 @1))
1468 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1469 || (FLOAT_TYPE_P (type)
1470 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1471 && !HONOR_SIGNED_ZEROS (type)))
1474 (negate (pointer_diff @0 @1))
1475 (if (TYPE_OVERFLOW_UNDEFINED (type))
1476 (pointer_diff @1 @0)))
1478 /* A - B -> A + (-B) if B is easily negatable. */
1480 (minus @0 negate_expr_p@1)
1481 (if (!FIXED_POINT_TYPE_P (type))
1482 (plus @0 (negate @1))))
1484 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1486 (negate (mult:c@0 @1 negate_expr_p@2))
1487 (if (! TYPE_UNSIGNED (type)
1488 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1490 (mult @1 (negate @2))))
1493 (negate (rdiv@0 @1 negate_expr_p@2))
1494 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1496 (rdiv @1 (negate @2))))
1499 (negate (rdiv@0 negate_expr_p@1 @2))
1500 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1502 (rdiv (negate @1) @2)))
1504 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1506 (negate (convert? (rshift @0 INTEGER_CST@1)))
1507 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1508 && wi::to_wide (@1) == element_precision (type) - 1)
1509 (with { tree stype = TREE_TYPE (@0);
1510 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1511 : unsigned_type_for (stype); }
1512 (convert (rshift:ntype (convert:ntype @0) @1)))))
1514 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1516 For bitwise binary operations apply operand conversions to the
1517 binary operation result instead of to the operands. This allows
1518 to combine successive conversions and bitwise binary operations.
1519 We combine the above two cases by using a conditional convert. */
1520 (for bitop (bit_and bit_ior bit_xor)
1522 (bitop (convert@2 @0) (convert?@3 @1))
1523 (if (((TREE_CODE (@1) == INTEGER_CST
1524 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1525 && int_fits_type_p (@1, TREE_TYPE (@0)))
1526 || types_match (@0, @1))
1527 /* ??? This transform conflicts with fold-const.c doing
1528 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1529 constants (if x has signed type, the sign bit cannot be set
1530 in c). This folds extension into the BIT_AND_EXPR.
1531 Restrict it to GIMPLE to avoid endless recursions. */
1532 && (bitop != BIT_AND_EXPR || GIMPLE)
1533 && (/* That's a good idea if the conversion widens the operand, thus
1534 after hoisting the conversion the operation will be narrower. */
1535 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1536 /* It's also a good idea if the conversion is to a non-integer
1538 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1539 /* Or if the precision of TO is not the same as the precision
1541 || !type_has_mode_precision_p (type)
1542 /* In GIMPLE, getting rid of 2 conversions for one new results
1545 && TREE_CODE (@1) != INTEGER_CST
1546 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1548 && single_use (@3))))
1549 (convert (bitop @0 (convert @1)))))
1550 /* In GIMPLE, getting rid of 2 conversions for one new results
1553 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1555 && TREE_CODE (@1) != INTEGER_CST
1556 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1557 && types_match (type, @0))
1558 (bitop @0 (convert @1)))))
1560 (for bitop (bit_and bit_ior)
1561 rbitop (bit_ior bit_and)
1562 /* (x | y) & x -> x */
1563 /* (x & y) | x -> x */
1565 (bitop:c (rbitop:c @0 @1) @0)
1567 /* (~x | y) & x -> x & y */
1568 /* (~x & y) | x -> x | y */
1570 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1573 /* ((x | y) & z) | x -> (z & y) | x */
1575 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1576 (bit_ior (bit_and @2 @1) @0))
1578 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1580 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1581 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1583 /* Combine successive equal operations with constants. */
1584 (for bitop (bit_and bit_ior bit_xor)
1586 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1587 (if (!CONSTANT_CLASS_P (@0))
1588 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1589 folded to a constant. */
1590 (bitop @0 (bitop @1 @2))
1591 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1592 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1593 the values involved are such that the operation can't be decided at
1594 compile time. Try folding one of @0 or @1 with @2 to see whether
1595 that combination can be decided at compile time.
1597 Keep the existing form if both folds fail, to avoid endless
1599 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1601 (bitop @1 { cst1; })
1602 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1604 (bitop @0 { cst2; }))))))))
1606 /* Try simple folding for X op !X, and X op X with the help
1607 of the truth_valued_p and logical_inverted_value predicates. */
1608 (match truth_valued_p
1610 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1611 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1612 (match truth_valued_p
1614 (match truth_valued_p
1617 (match (logical_inverted_value @0)
1619 (match (logical_inverted_value @0)
1620 (bit_not truth_valued_p@0))
1621 (match (logical_inverted_value @0)
1622 (eq @0 integer_zerop))
1623 (match (logical_inverted_value @0)
1624 (ne truth_valued_p@0 integer_truep))
1625 (match (logical_inverted_value @0)
1626 (bit_xor truth_valued_p@0 integer_truep))
1630 (bit_and:c @0 (logical_inverted_value @0))
1631 { build_zero_cst (type); })
1632 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1633 (for op (bit_ior bit_xor)
1635 (op:c truth_valued_p@0 (logical_inverted_value @0))
1636 { constant_boolean_node (true, type); }))
1637 /* X ==/!= !X is false/true. */
1640 (op:c truth_valued_p@0 (logical_inverted_value @0))
1641 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1645 (bit_not (bit_not @0))
1648 /* Convert ~ (-A) to A - 1. */
1650 (bit_not (convert? (negate @0)))
1651 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1652 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1653 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1655 /* Convert - (~A) to A + 1. */
1657 (negate (nop_convert? (bit_not @0)))
1658 (plus (view_convert @0) { build_each_one_cst (type); }))
1660 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1662 (bit_not (convert? (minus @0 integer_each_onep)))
1663 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1664 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1665 (convert (negate @0))))
1667 (bit_not (convert? (plus @0 integer_all_onesp)))
1668 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1669 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1670 (convert (negate @0))))
1672 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1674 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1675 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1676 (convert (bit_xor @0 (bit_not @1)))))
1678 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1679 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1680 (convert (bit_xor @0 @1))))
1682 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1684 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1685 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1686 (bit_not (bit_xor (view_convert @0) @1))))
1688 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1690 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1691 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1693 /* Fold A - (A & B) into ~B & A. */
1695 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1696 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1697 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1698 (convert (bit_and (bit_not @1) @0))))
1700 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1701 (for cmp (gt lt ge le)
1703 (mult (convert (cmp @0 @1)) @2)
1704 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1705 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1707 /* For integral types with undefined overflow and C != 0 fold
1708 x * C EQ/NE y * C into x EQ/NE y. */
1711 (cmp (mult:c @0 @1) (mult:c @2 @1))
1712 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1713 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1714 && tree_expr_nonzero_p (@1))
1717 /* For integral types with wrapping overflow and C odd fold
1718 x * C EQ/NE y * C into x EQ/NE y. */
1721 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1722 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1723 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1724 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1727 /* For integral types with undefined overflow and C != 0 fold
1728 x * C RELOP y * C into:
1730 x RELOP y for nonnegative C
1731 y RELOP x for negative C */
1732 (for cmp (lt gt le ge)
1734 (cmp (mult:c @0 @1) (mult:c @2 @1))
1735 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1736 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1737 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1739 (if (TREE_CODE (@1) == INTEGER_CST
1740 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1743 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1747 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1748 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1749 && TYPE_UNSIGNED (TREE_TYPE (@0))
1750 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1751 && (wi::to_wide (@2)
1752 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1753 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1754 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1756 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1757 (for cmp (simple_comparison)
1759 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1760 (if (element_precision (@3) >= element_precision (@0)
1761 && types_match (@0, @1))
1762 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1763 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1765 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1768 tree utype = unsigned_type_for (TREE_TYPE (@0));
1770 (cmp (convert:utype @1) (convert:utype @0)))))
1771 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1772 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1776 tree utype = unsigned_type_for (TREE_TYPE (@0));
1778 (cmp (convert:utype @0) (convert:utype @1)))))))))
1780 /* X / C1 op C2 into a simple range test. */
1781 (for cmp (simple_comparison)
1783 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1784 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1785 && integer_nonzerop (@1)
1786 && !TREE_OVERFLOW (@1)
1787 && !TREE_OVERFLOW (@2))
1788 (with { tree lo, hi; bool neg_overflow;
1789 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1792 (if (code == LT_EXPR || code == GE_EXPR)
1793 (if (TREE_OVERFLOW (lo))
1794 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1795 (if (code == LT_EXPR)
1798 (if (code == LE_EXPR || code == GT_EXPR)
1799 (if (TREE_OVERFLOW (hi))
1800 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1801 (if (code == LE_EXPR)
1805 { build_int_cst (type, code == NE_EXPR); })
1806 (if (code == EQ_EXPR && !hi)
1808 (if (code == EQ_EXPR && !lo)
1810 (if (code == NE_EXPR && !hi)
1812 (if (code == NE_EXPR && !lo)
1815 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1819 tree etype = range_check_type (TREE_TYPE (@0));
1822 hi = fold_convert (etype, hi);
1823 lo = fold_convert (etype, lo);
1824 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1827 (if (etype && hi && !TREE_OVERFLOW (hi))
1828 (if (code == EQ_EXPR)
1829 (le (minus (convert:etype @0) { lo; }) { hi; })
1830 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1832 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1833 (for op (lt le ge gt)
1835 (op (plus:c @0 @2) (plus:c @1 @2))
1836 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1837 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1839 /* For equality and subtraction, this is also true with wrapping overflow. */
1840 (for op (eq ne minus)
1842 (op (plus:c @0 @2) (plus:c @1 @2))
1843 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1844 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1845 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1848 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1849 (for op (lt le ge gt)
1851 (op (minus @0 @2) (minus @1 @2))
1852 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1853 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1855 /* For equality and subtraction, this is also true with wrapping overflow. */
1856 (for op (eq ne minus)
1858 (op (minus @0 @2) (minus @1 @2))
1859 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1860 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1861 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1863 /* And for pointers... */
1864 (for op (simple_comparison)
1866 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1867 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1870 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1871 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1872 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1873 (pointer_diff @0 @1)))
1875 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1876 (for op (lt le ge gt)
1878 (op (minus @2 @0) (minus @2 @1))
1879 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1880 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1882 /* For equality and subtraction, this is also true with wrapping overflow. */
1883 (for op (eq ne minus)
1885 (op (minus @2 @0) (minus @2 @1))
1886 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1887 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1888 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1890 /* And for pointers... */
1891 (for op (simple_comparison)
1893 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1894 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1897 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1898 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1899 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1900 (pointer_diff @1 @0)))
1902 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1903 (for op (lt le gt ge)
1905 (op:c (plus:c@2 @0 @1) @1)
1906 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1907 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1908 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1909 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1910 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1911 /* For equality, this is also true with wrapping overflow. */
1914 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1915 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1916 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1917 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1918 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1919 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1920 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1921 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1923 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1924 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1925 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1926 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1927 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1929 /* X - Y < X is the same as Y > 0 when there is no overflow.
1930 For equality, this is also true with wrapping overflow. */
1931 (for op (simple_comparison)
1933 (op:c @0 (minus@2 @0 @1))
1934 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1935 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1936 || ((op == EQ_EXPR || op == NE_EXPR)
1937 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1938 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1939 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1942 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1943 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1947 (cmp (trunc_div @0 @1) integer_zerop)
1948 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1949 /* Complex ==/!= is allowed, but not </>=. */
1950 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1951 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1954 /* X == C - X can never be true if C is odd. */
1957 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1958 (if (TREE_INT_CST_LOW (@1) & 1)
1959 { constant_boolean_node (cmp == NE_EXPR, type); })))
1961 /* Arguments on which one can call get_nonzero_bits to get the bits
1963 (match with_possible_nonzero_bits
1965 (match with_possible_nonzero_bits
1967 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1968 /* Slightly extended version, do not make it recursive to keep it cheap. */
1969 (match (with_possible_nonzero_bits2 @0)
1970 with_possible_nonzero_bits@0)
1971 (match (with_possible_nonzero_bits2 @0)
1972 (bit_and:c with_possible_nonzero_bits@0 @2))
1974 /* Same for bits that are known to be set, but we do not have
1975 an equivalent to get_nonzero_bits yet. */
1976 (match (with_certain_nonzero_bits2 @0)
1978 (match (with_certain_nonzero_bits2 @0)
1979 (bit_ior @1 INTEGER_CST@0))
1981 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1984 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1985 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1986 { constant_boolean_node (cmp == NE_EXPR, type); })))
1988 /* ((X inner_op C0) outer_op C1)
1989 With X being a tree where value_range has reasoned certain bits to always be
1990 zero throughout its computed value range,
1991 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1992 where zero_mask has 1's for all bits that are sure to be 0 in
1994 if (inner_op == '^') C0 &= ~C1;
1995 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1996 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1998 (for inner_op (bit_ior bit_xor)
1999 outer_op (bit_xor bit_ior)
2002 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2006 wide_int zero_mask_not;
2010 if (TREE_CODE (@2) == SSA_NAME)
2011 zero_mask_not = get_nonzero_bits (@2);
2015 if (inner_op == BIT_XOR_EXPR)
2017 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2018 cst_emit = C0 | wi::to_wide (@1);
2022 C0 = wi::to_wide (@0);
2023 cst_emit = C0 ^ wi::to_wide (@1);
2026 (if (!fail && (C0 & zero_mask_not) == 0)
2027 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2028 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2029 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2031 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2033 (pointer_plus (pointer_plus:s @0 @1) @3)
2034 (pointer_plus @0 (plus @1 @3)))
2040 tem4 = (unsigned long) tem3;
2045 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2046 /* Conditionally look through a sign-changing conversion. */
2047 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2048 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2049 || (GENERIC && type == TREE_TYPE (@1))))
2052 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2053 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2057 tem = (sizetype) ptr;
2061 and produce the simpler and easier to analyze with respect to alignment
2062 ... = ptr & ~algn; */
2064 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2065 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2066 (bit_and @0 { algn; })))
2068 /* Try folding difference of addresses. */
2070 (minus (convert ADDR_EXPR@0) (convert @1))
2071 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2072 (with { poly_int64 diff; }
2073 (if (ptr_difference_const (@0, @1, &diff))
2074 { build_int_cst_type (type, diff); }))))
2076 (minus (convert @0) (convert ADDR_EXPR@1))
2077 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2078 (with { poly_int64 diff; }
2079 (if (ptr_difference_const (@0, @1, &diff))
2080 { build_int_cst_type (type, diff); }))))
2082 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2083 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2084 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2085 (with { poly_int64 diff; }
2086 (if (ptr_difference_const (@0, @1, &diff))
2087 { build_int_cst_type (type, diff); }))))
2089 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2090 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2091 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2092 (with { poly_int64 diff; }
2093 (if (ptr_difference_const (@0, @1, &diff))
2094 { build_int_cst_type (type, diff); }))))
2096 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2098 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2099 (with { poly_int64 diff; }
2100 (if (ptr_difference_const (@0, @2, &diff))
2101 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2103 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2106 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2107 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2108 (if (ptr_difference_const (@0, @2, &diff))
2109 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2111 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2113 (convert (pointer_diff @0 INTEGER_CST@1))
2114 (if (POINTER_TYPE_P (type))
2115 { build_fold_addr_expr_with_type
2116 (build2 (MEM_REF, char_type_node, @0,
2117 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2120 /* If arg0 is derived from the address of an object or function, we may
2121 be able to fold this expression using the object or function's
2124 (bit_and (convert? @0) INTEGER_CST@1)
2125 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2126 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2130 unsigned HOST_WIDE_INT bitpos;
2131 get_pointer_alignment_1 (@0, &align, &bitpos);
2133 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2134 { wide_int_to_tree (type, (wi::to_wide (@1)
2135 & (bitpos / BITS_PER_UNIT))); }))))
2139 (if (INTEGRAL_TYPE_P (type)
2140 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2144 (if (INTEGRAL_TYPE_P (type)
2145 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2147 /* x > y && x != XXX_MIN --> x > y
2148 x > y && x == XXX_MIN --> false . */
2151 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2153 (if (eqne == EQ_EXPR)
2154 { constant_boolean_node (false, type); })
2155 (if (eqne == NE_EXPR)
2159 /* x < y && x != XXX_MAX --> x < y
2160 x < y && x == XXX_MAX --> false. */
2163 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2165 (if (eqne == EQ_EXPR)
2166 { constant_boolean_node (false, type); })
2167 (if (eqne == NE_EXPR)
2171 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2173 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2176 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2178 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2181 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2183 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2186 /* x <= y || x != XXX_MIN --> true. */
2188 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2189 { constant_boolean_node (true, type); })
2191 /* x <= y || x == XXX_MIN --> x <= y. */
2193 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2196 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2198 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2201 /* x >= y || x != XXX_MAX --> true
2202 x >= y || x == XXX_MAX --> x >= y. */
2205 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2207 (if (eqne == EQ_EXPR)
2209 (if (eqne == NE_EXPR)
2210 { constant_boolean_node (true, type); }))))
2212 /* y == XXX_MIN || x < y --> x <= y - 1 */
2214 (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
2215 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2216 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2217 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2219 /* y != XXX_MIN && x >= y --> x > y - 1 */
2221 (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
2222 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2223 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2224 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2226 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2227 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2230 (for code2 (eq ne lt gt le ge)
2232 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2235 int cmp = tree_int_cst_compare (@1, @2);
2239 case EQ_EXPR: val = (cmp == 0); break;
2240 case NE_EXPR: val = (cmp != 0); break;
2241 case LT_EXPR: val = (cmp < 0); break;
2242 case GT_EXPR: val = (cmp > 0); break;
2243 case LE_EXPR: val = (cmp <= 0); break;
2244 case GE_EXPR: val = (cmp >= 0); break;
2245 default: gcc_unreachable ();
2249 (if (code1 == EQ_EXPR && val) @3)
2250 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2251 (if (code1 == NE_EXPR && !val) @4))))))
2253 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2255 (for code1 (lt le gt ge)
2256 (for code2 (lt le gt ge)
2258 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2261 int cmp = tree_int_cst_compare (@1, @2);
2264 /* Choose the more restrictive of two < or <= comparisons. */
2265 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2266 && (code2 == LT_EXPR || code2 == LE_EXPR))
2267 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2270 /* Likewise chose the more restrictive of two > or >= comparisons. */
2271 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2272 && (code2 == GT_EXPR || code2 == GE_EXPR))
2273 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2276 /* Check for singleton ranges. */
2278 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2279 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2281 /* Check for disjoint ranges. */
2283 && (code1 == LT_EXPR || code1 == LE_EXPR)
2284 && (code2 == GT_EXPR || code2 == GE_EXPR))
2285 { constant_boolean_node (false, type); })
2287 && (code1 == GT_EXPR || code1 == GE_EXPR)
2288 && (code2 == LT_EXPR || code2 == LE_EXPR))
2289 { constant_boolean_node (false, type); })
2292 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2293 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2296 (for code2 (eq ne lt gt le ge)
2298 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2301 int cmp = tree_int_cst_compare (@1, @2);
2305 case EQ_EXPR: val = (cmp == 0); break;
2306 case NE_EXPR: val = (cmp != 0); break;
2307 case LT_EXPR: val = (cmp < 0); break;
2308 case GT_EXPR: val = (cmp > 0); break;
2309 case LE_EXPR: val = (cmp <= 0); break;
2310 case GE_EXPR: val = (cmp >= 0); break;
2311 default: gcc_unreachable ();
2315 (if (code1 == EQ_EXPR && val) @4)
2316 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2317 (if (code1 == NE_EXPR && !val) @3))))))
2319 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2321 (for code1 (lt le gt ge)
2322 (for code2 (lt le gt ge)
2324 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2327 int cmp = tree_int_cst_compare (@1, @2);
2330 /* Choose the more restrictive of two < or <= comparisons. */
2331 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2332 && (code2 == LT_EXPR || code2 == LE_EXPR))
2333 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2336 /* Likewise chose the more restrictive of two > or >= comparisons. */
2337 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2338 && (code2 == GT_EXPR || code2 == GE_EXPR))
2339 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2342 /* Check for singleton ranges. */
2344 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2345 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2347 /* Check for disjoint ranges. */
2349 && (code1 == LT_EXPR || code1 == LE_EXPR)
2350 && (code2 == GT_EXPR || code2 == GE_EXPR))
2351 { constant_boolean_node (true, type); })
2353 && (code1 == GT_EXPR || code1 == GE_EXPR)
2354 && (code2 == LT_EXPR || code2 == LE_EXPR))
2355 { constant_boolean_node (true, type); })
2358 /* We can't reassociate at all for saturating types. */
2359 (if (!TYPE_SATURATING (type))
2361 /* Contract negates. */
2362 /* A + (-B) -> A - B */
2364 (plus:c @0 (convert? (negate @1)))
2365 /* Apply STRIP_NOPS on the negate. */
2366 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2367 && !TYPE_OVERFLOW_SANITIZED (type))
2371 if (INTEGRAL_TYPE_P (type)
2372 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2373 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2375 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2376 /* A - (-B) -> A + B */
2378 (minus @0 (convert? (negate @1)))
2379 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2380 && !TYPE_OVERFLOW_SANITIZED (type))
2384 if (INTEGRAL_TYPE_P (type)
2385 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2386 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2388 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2390 Sign-extension is ok except for INT_MIN, which thankfully cannot
2391 happen without overflow. */
2393 (negate (convert (negate @1)))
2394 (if (INTEGRAL_TYPE_P (type)
2395 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2396 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2397 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2398 && !TYPE_OVERFLOW_SANITIZED (type)
2399 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2402 (negate (convert negate_expr_p@1))
2403 (if (SCALAR_FLOAT_TYPE_P (type)
2404 && ((DECIMAL_FLOAT_TYPE_P (type)
2405 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2406 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2407 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2408 (convert (negate @1))))
2410 (negate (nop_convert? (negate @1)))
2411 (if (!TYPE_OVERFLOW_SANITIZED (type)
2412 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2415 /* We can't reassociate floating-point unless -fassociative-math
2416 or fixed-point plus or minus because of saturation to +-Inf. */
2417 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2418 && !FIXED_POINT_TYPE_P (type))
2420 /* Match patterns that allow contracting a plus-minus pair
2421 irrespective of overflow issues. */
2422 /* (A +- B) - A -> +- B */
2423 /* (A +- B) -+ B -> A */
2424 /* A - (A +- B) -> -+ B */
2425 /* A +- (B -+ A) -> +- B */
2427 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2430 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2431 (if (!ANY_INTEGRAL_TYPE_P (type)
2432 || TYPE_OVERFLOW_WRAPS (type))
2433 (negate (view_convert @1))
2434 (view_convert (negate @1))))
2436 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2439 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2440 (if (!ANY_INTEGRAL_TYPE_P (type)
2441 || TYPE_OVERFLOW_WRAPS (type))
2442 (negate (view_convert @1))
2443 (view_convert (negate @1))))
2445 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2447 /* (A +- B) + (C - A) -> C +- B */
2448 /* (A + B) - (A - C) -> B + C */
2449 /* More cases are handled with comparisons. */
2451 (plus:c (plus:c @0 @1) (minus @2 @0))
2454 (plus:c (minus @0 @1) (minus @2 @0))
2457 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2458 (if (TYPE_OVERFLOW_UNDEFINED (type)
2459 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2460 (pointer_diff @2 @1)))
2462 (minus (plus:c @0 @1) (minus @0 @2))
2465 /* (A +- CST1) +- CST2 -> A + CST3
2466 Use view_convert because it is safe for vectors and equivalent for
2468 (for outer_op (plus minus)
2469 (for inner_op (plus minus)
2470 neg_inner_op (minus plus)
2472 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2474 /* If one of the types wraps, use that one. */
2475 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2476 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2477 forever if something doesn't simplify into a constant. */
2478 (if (!CONSTANT_CLASS_P (@0))
2479 (if (outer_op == PLUS_EXPR)
2480 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2481 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2482 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2483 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2484 (if (outer_op == PLUS_EXPR)
2485 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2486 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2487 /* If the constant operation overflows we cannot do the transform
2488 directly as we would introduce undefined overflow, for example
2489 with (a - 1) + INT_MIN. */
2490 (if (types_match (type, @0))
2491 (with { tree cst = const_binop (outer_op == inner_op
2492 ? PLUS_EXPR : MINUS_EXPR,
2494 (if (cst && !TREE_OVERFLOW (cst))
2495 (inner_op @0 { cst; } )
2496 /* X+INT_MAX+1 is X-INT_MIN. */
2497 (if (INTEGRAL_TYPE_P (type) && cst
2498 && wi::to_wide (cst) == wi::min_value (type))
2499 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2500 /* Last resort, use some unsigned type. */
2501 (with { tree utype = unsigned_type_for (type); }
2503 (view_convert (inner_op
2504 (view_convert:utype @0)
2506 { drop_tree_overflow (cst); }))))))))))))))
2508 /* (CST1 - A) +- CST2 -> CST3 - A */
2509 (for outer_op (plus minus)
2511 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2512 /* If one of the types wraps, use that one. */
2513 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2514 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2515 forever if something doesn't simplify into a constant. */
2516 (if (!CONSTANT_CLASS_P (@0))
2517 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2518 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2519 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2520 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2521 (if (types_match (type, @0))
2522 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2523 (if (cst && !TREE_OVERFLOW (cst))
2524 (minus { cst; } @0))))))))
2526 /* CST1 - (CST2 - A) -> CST3 + A
2527 Use view_convert because it is safe for vectors and equivalent for
2530 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2531 /* If one of the types wraps, use that one. */
2532 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2533 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2534 forever if something doesn't simplify into a constant. */
2535 (if (!CONSTANT_CLASS_P (@0))
2536 (plus (view_convert @0) (minus @1 (view_convert @2))))
2537 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2538 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2539 (view_convert (plus @0 (minus (view_convert @1) @2)))
2540 (if (types_match (type, @0))
2541 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2542 (if (cst && !TREE_OVERFLOW (cst))
2543 (plus { cst; } @0)))))))
2545 /* ((T)(A)) + CST -> (T)(A + CST) */
2548 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2549 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2550 && TREE_CODE (type) == INTEGER_TYPE
2551 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2552 && int_fits_type_p (@1, TREE_TYPE (@0)))
2553 /* Perform binary operation inside the cast if the constant fits
2554 and (A + CST)'s range does not overflow. */
2557 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2558 max_ovf = wi::OVF_OVERFLOW;
2559 tree inner_type = TREE_TYPE (@0);
2562 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2563 TYPE_SIGN (inner_type));
2566 if (get_global_range_query ()->range_of_expr (vr, @0)
2567 && vr.kind () == VR_RANGE)
2569 wide_int wmin0 = vr.lower_bound ();
2570 wide_int wmax0 = vr.upper_bound ();
2571 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2572 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2575 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2576 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2580 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2582 (for op (plus minus)
2584 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2585 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2586 && TREE_CODE (type) == INTEGER_TYPE
2587 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2588 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2589 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2590 && TYPE_OVERFLOW_WRAPS (type))
2591 (plus (convert @0) (op @2 (convert @1))))))
2594 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2595 to a simple value. */
2597 (for op (plus minus)
2599 (op (convert @0) (convert @1))
2600 (if (INTEGRAL_TYPE_P (type)
2601 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2602 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2603 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2604 && !TYPE_OVERFLOW_TRAPS (type)
2605 && !TYPE_OVERFLOW_SANITIZED (type))
2606 (convert (op! @0 @1)))))
2611 (plus:c (bit_not @0) @0)
2612 (if (!TYPE_OVERFLOW_TRAPS (type))
2613 { build_all_ones_cst (type); }))
2617 (plus (convert? (bit_not @0)) integer_each_onep)
2618 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2619 (negate (convert @0))))
2623 (minus (convert? (negate @0)) integer_each_onep)
2624 (if (!TYPE_OVERFLOW_TRAPS (type)
2625 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2626 (bit_not (convert @0))))
2630 (minus integer_all_onesp @0)
2633 /* (T)(P + A) - (T)P -> (T) A */
2635 (minus (convert (plus:c @@0 @1))
2637 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2638 /* For integer types, if A has a smaller type
2639 than T the result depends on the possible
2641 E.g. T=size_t, A=(unsigned)429497295, P>0.
2642 However, if an overflow in P + A would cause
2643 undefined behavior, we can assume that there
2645 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2646 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2649 (minus (convert (pointer_plus @@0 @1))
2651 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2652 /* For pointer types, if the conversion of A to the
2653 final type requires a sign- or zero-extension,
2654 then we have to punt - it is not defined which
2656 || (POINTER_TYPE_P (TREE_TYPE (@0))
2657 && TREE_CODE (@1) == INTEGER_CST
2658 && tree_int_cst_sign_bit (@1) == 0))
2661 (pointer_diff (pointer_plus @@0 @1) @0)
2662 /* The second argument of pointer_plus must be interpreted as signed, and
2663 thus sign-extended if necessary. */
2664 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2665 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2666 second arg is unsigned even when we need to consider it as signed,
2667 we don't want to diagnose overflow here. */
2668 (convert (view_convert:stype @1))))
2670 /* (T)P - (T)(P + A) -> -(T) A */
2672 (minus (convert? @0)
2673 (convert (plus:c @@0 @1)))
2674 (if (INTEGRAL_TYPE_P (type)
2675 && TYPE_OVERFLOW_UNDEFINED (type)
2676 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2677 (with { tree utype = unsigned_type_for (type); }
2678 (convert (negate (convert:utype @1))))
2679 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2680 /* For integer types, if A has a smaller type
2681 than T the result depends on the possible
2683 E.g. T=size_t, A=(unsigned)429497295, P>0.
2684 However, if an overflow in P + A would cause
2685 undefined behavior, we can assume that there
2687 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2688 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2689 (negate (convert @1)))))
2692 (convert (pointer_plus @@0 @1)))
2693 (if (INTEGRAL_TYPE_P (type)
2694 && TYPE_OVERFLOW_UNDEFINED (type)
2695 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2696 (with { tree utype = unsigned_type_for (type); }
2697 (convert (negate (convert:utype @1))))
2698 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2699 /* For pointer types, if the conversion of A to the
2700 final type requires a sign- or zero-extension,
2701 then we have to punt - it is not defined which
2703 || (POINTER_TYPE_P (TREE_TYPE (@0))
2704 && TREE_CODE (@1) == INTEGER_CST
2705 && tree_int_cst_sign_bit (@1) == 0))
2706 (negate (convert @1)))))
2708 (pointer_diff @0 (pointer_plus @@0 @1))
2709 /* The second argument of pointer_plus must be interpreted as signed, and
2710 thus sign-extended if necessary. */
2711 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2712 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2713 second arg is unsigned even when we need to consider it as signed,
2714 we don't want to diagnose overflow here. */
2715 (negate (convert (view_convert:stype @1)))))
2717 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2719 (minus (convert (plus:c @@0 @1))
2720 (convert (plus:c @0 @2)))
2721 (if (INTEGRAL_TYPE_P (type)
2722 && TYPE_OVERFLOW_UNDEFINED (type)
2723 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2724 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2725 (with { tree utype = unsigned_type_for (type); }
2726 (convert (minus (convert:utype @1) (convert:utype @2))))
2727 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2728 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2729 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2730 /* For integer types, if A has a smaller type
2731 than T the result depends on the possible
2733 E.g. T=size_t, A=(unsigned)429497295, P>0.
2734 However, if an overflow in P + A would cause
2735 undefined behavior, we can assume that there
2737 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2738 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2739 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2740 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2741 (minus (convert @1) (convert @2)))))
2743 (minus (convert (pointer_plus @@0 @1))
2744 (convert (pointer_plus @0 @2)))
2745 (if (INTEGRAL_TYPE_P (type)
2746 && TYPE_OVERFLOW_UNDEFINED (type)
2747 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2748 (with { tree utype = unsigned_type_for (type); }
2749 (convert (minus (convert:utype @1) (convert:utype @2))))
2750 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2751 /* For pointer types, if the conversion of A to the
2752 final type requires a sign- or zero-extension,
2753 then we have to punt - it is not defined which
2755 || (POINTER_TYPE_P (TREE_TYPE (@0))
2756 && TREE_CODE (@1) == INTEGER_CST
2757 && tree_int_cst_sign_bit (@1) == 0
2758 && TREE_CODE (@2) == INTEGER_CST
2759 && tree_int_cst_sign_bit (@2) == 0))
2760 (minus (convert @1) (convert @2)))))
2762 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2763 (pointer_diff @0 @1))
2765 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2766 /* The second argument of pointer_plus must be interpreted as signed, and
2767 thus sign-extended if necessary. */
2768 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2769 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2770 second arg is unsigned even when we need to consider it as signed,
2771 we don't want to diagnose overflow here. */
2772 (minus (convert (view_convert:stype @1))
2773 (convert (view_convert:stype @2)))))))
2775 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2776 Modeled after fold_plusminus_mult_expr. */
2777 (if (!TYPE_SATURATING (type)
2778 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2779 (for plusminus (plus minus)
2781 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2782 (if (!ANY_INTEGRAL_TYPE_P (type)
2783 || TYPE_OVERFLOW_WRAPS (type)
2784 || (INTEGRAL_TYPE_P (type)
2785 && tree_expr_nonzero_p (@0)
2786 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2787 (if (single_use (@3) || single_use (@4))
2788 /* If @1 +- @2 is constant require a hard single-use on either
2789 original operand (but not on both). */
2790 (mult (plusminus @1 @2) @0)
2792 (mult! (plusminus @1 @2) @0)
2795 /* We cannot generate constant 1 for fract. */
2796 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2798 (plusminus @0 (mult:c@3 @0 @2))
2799 (if ((!ANY_INTEGRAL_TYPE_P (type)
2800 || TYPE_OVERFLOW_WRAPS (type)
2801 /* For @0 + @0*@2 this transformation would introduce UB
2802 (where there was none before) for @0 in [-1,0] and @2 max.
2803 For @0 - @0*@2 this transformation would introduce UB
2804 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2805 || (INTEGRAL_TYPE_P (type)
2806 && ((tree_expr_nonzero_p (@0)
2807 && expr_not_equal_to (@0,
2808 wi::minus_one (TYPE_PRECISION (type))))
2809 || (plusminus == PLUS_EXPR
2810 ? expr_not_equal_to (@2,
2811 wi::max_value (TYPE_PRECISION (type), SIGNED))
2812 /* Let's ignore the @0 -1 and @2 min case. */
2813 : (expr_not_equal_to (@2,
2814 wi::min_value (TYPE_PRECISION (type), SIGNED))
2815 && expr_not_equal_to (@2,
2816 wi::min_value (TYPE_PRECISION (type), SIGNED)
2819 (mult (plusminus { build_one_cst (type); } @2) @0)))
2821 (plusminus (mult:c@3 @0 @2) @0)
2822 (if ((!ANY_INTEGRAL_TYPE_P (type)
2823 || TYPE_OVERFLOW_WRAPS (type)
2824 /* For @0*@2 + @0 this transformation would introduce UB
2825 (where there was none before) for @0 in [-1,0] and @2 max.
2826 For @0*@2 - @0 this transformation would introduce UB
2827 for @0 0 and @2 min. */
2828 || (INTEGRAL_TYPE_P (type)
2829 && ((tree_expr_nonzero_p (@0)
2830 && (plusminus == MINUS_EXPR
2831 || expr_not_equal_to (@0,
2832 wi::minus_one (TYPE_PRECISION (type)))))
2833 || expr_not_equal_to (@2,
2834 (plusminus == PLUS_EXPR
2835 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2836 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2838 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2841 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2842 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2844 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2845 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2846 && tree_fits_uhwi_p (@1)
2847 && tree_to_uhwi (@1) < element_precision (type)
2848 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2849 || optab_handler (smul_optab,
2850 TYPE_MODE (type)) != CODE_FOR_nothing))
2851 (with { tree t = type;
2852 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2853 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2854 element_precision (type));
2856 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2858 cst = build_uniform_cst (t, cst); }
2859 (convert (mult (convert:t @0) { cst; })))))
2861 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2862 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2863 && tree_fits_uhwi_p (@1)
2864 && tree_to_uhwi (@1) < element_precision (type)
2865 && tree_fits_uhwi_p (@2)
2866 && tree_to_uhwi (@2) < element_precision (type)
2867 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2868 || optab_handler (smul_optab,
2869 TYPE_MODE (type)) != CODE_FOR_nothing))
2870 (with { tree t = type;
2871 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2872 unsigned int prec = element_precision (type);
2873 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2874 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2875 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2877 cst = build_uniform_cst (t, cst); }
2878 (convert (mult (convert:t @0) { cst; })))))
2881 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
2882 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
2883 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
2884 (for op (bit_ior bit_xor)
2886 (op (mult:s@0 @1 INTEGER_CST@2)
2887 (mult:s@3 @1 INTEGER_CST@4))
2888 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2889 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2891 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
2893 (op:c (mult:s@0 @1 INTEGER_CST@2)
2894 (lshift:s@3 @1 INTEGER_CST@4))
2895 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2896 && tree_int_cst_sgn (@4) > 0
2897 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2898 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
2899 wide_int c = wi::add (wi::to_wide (@2),
2900 wi::lshift (wone, wi::to_wide (@4))); }
2901 (mult @1 { wide_int_to_tree (type, c); }))))
2903 (op:c (mult:s@0 @1 INTEGER_CST@2)
2905 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2906 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
2908 { wide_int_to_tree (type,
2909 wi::add (wi::to_wide (@2), 1)); })))
2911 (op (lshift:s@0 @1 INTEGER_CST@2)
2912 (lshift:s@3 @1 INTEGER_CST@4))
2913 (if (INTEGRAL_TYPE_P (type)
2914 && tree_int_cst_sgn (@2) > 0
2915 && tree_int_cst_sgn (@4) > 0
2916 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2917 (with { tree t = type;
2918 if (!TYPE_OVERFLOW_WRAPS (t))
2919 t = unsigned_type_for (t);
2920 wide_int wone = wi::one (TYPE_PRECISION (t));
2921 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
2922 wi::lshift (wone, wi::to_wide (@4))); }
2923 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
2925 (op:c (lshift:s@0 @1 INTEGER_CST@2)
2927 (if (INTEGRAL_TYPE_P (type)
2928 && tree_int_cst_sgn (@2) > 0
2929 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
2930 (with { tree t = type;
2931 if (!TYPE_OVERFLOW_WRAPS (t))
2932 t = unsigned_type_for (t);
2933 wide_int wone = wi::one (TYPE_PRECISION (t));
2934 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
2935 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
2937 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2939 (for minmax (min max FMIN_ALL FMAX_ALL)
2943 /* min(max(x,y),y) -> y. */
2945 (min:c (max:c @0 @1) @1)
2947 /* max(min(x,y),y) -> y. */
2949 (max:c (min:c @0 @1) @1)
2951 /* max(a,-a) -> abs(a). */
2953 (max:c @0 (negate @0))
2954 (if (TREE_CODE (type) != COMPLEX_TYPE
2955 && (! ANY_INTEGRAL_TYPE_P (type)
2956 || TYPE_OVERFLOW_UNDEFINED (type)))
2958 /* min(a,-a) -> -abs(a). */
2960 (min:c @0 (negate @0))
2961 (if (TREE_CODE (type) != COMPLEX_TYPE
2962 && (! ANY_INTEGRAL_TYPE_P (type)
2963 || TYPE_OVERFLOW_UNDEFINED (type)))
2968 (if (INTEGRAL_TYPE_P (type)
2969 && TYPE_MIN_VALUE (type)
2970 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2972 (if (INTEGRAL_TYPE_P (type)
2973 && TYPE_MAX_VALUE (type)
2974 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2979 (if (INTEGRAL_TYPE_P (type)
2980 && TYPE_MAX_VALUE (type)
2981 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2983 (if (INTEGRAL_TYPE_P (type)
2984 && TYPE_MIN_VALUE (type)
2985 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2988 /* max (a, a + CST) -> a + CST where CST is positive. */
2989 /* max (a, a + CST) -> a where CST is negative. */
2991 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2992 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2993 (if (tree_int_cst_sgn (@1) > 0)
2997 /* min (a, a + CST) -> a where CST is positive. */
2998 /* min (a, a + CST) -> a + CST where CST is negative. */
3000 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3001 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3002 (if (tree_int_cst_sgn (@1) > 0)
3006 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3007 and the outer convert demotes the expression back to x's type. */
3008 (for minmax (min max)
3010 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3011 (if (INTEGRAL_TYPE_P (type)
3012 && types_match (@1, type) && int_fits_type_p (@2, type)
3013 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3014 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3015 (minmax @1 (convert @2)))))
3017 (for minmax (FMIN_ALL FMAX_ALL)
3018 /* If either argument is NaN, return the other one. Avoid the
3019 transformation if we get (and honor) a signalling NaN. */
3021 (minmax:c @0 REAL_CST@1)
3022 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3023 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
3025 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3026 functions to return the numeric arg if the other one is NaN.
3027 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3028 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3029 worry about it either. */
3030 (if (flag_finite_math_only)
3037 /* min (-A, -B) -> -max (A, B) */
3038 (for minmax (min max FMIN_ALL FMAX_ALL)
3039 maxmin (max min FMAX_ALL FMIN_ALL)
3041 (minmax (negate:s@2 @0) (negate:s@3 @1))
3042 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3043 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3044 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3045 (negate (maxmin @0 @1)))))
3046 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3047 MAX (~X, ~Y) -> ~MIN (X, Y) */
3048 (for minmax (min max)
3051 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3052 (bit_not (maxmin @0 @1))))
3054 /* MIN (X, Y) == X -> X <= Y */
3055 (for minmax (min min max max)
3059 (cmp:c (minmax:c @0 @1) @0)
3060 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3062 /* MIN (X, 5) == 0 -> X == 0
3063 MIN (X, 5) == 7 -> false */
3066 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3067 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3068 TYPE_SIGN (TREE_TYPE (@0))))
3069 { constant_boolean_node (cmp == NE_EXPR, type); }
3070 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3071 TYPE_SIGN (TREE_TYPE (@0))))
3075 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3076 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3077 TYPE_SIGN (TREE_TYPE (@0))))
3078 { constant_boolean_node (cmp == NE_EXPR, type); }
3079 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3080 TYPE_SIGN (TREE_TYPE (@0))))
3082 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3083 (for minmax (min min max max min min max max )
3084 cmp (lt le gt ge gt ge lt le )
3085 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3087 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3088 (comb (cmp @0 @2) (cmp @1 @2))))
3090 /* X <= MAX(X, Y) -> true
3091 X > MAX(X, Y) -> false
3092 X >= MIN(X, Y) -> true
3093 X < MIN(X, Y) -> false */
3094 (for minmax (min min max max )
3097 (cmp @0 (minmax:c @0 @1))
3098 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3100 /* Undo fancy way of writing max/min or other ?: expressions,
3101 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
3102 People normally use ?: and that is what we actually try to optimize. */
3103 (for cmp (simple_comparison)
3105 (minus @0 (bit_and:c (minus @0 @1)
3106 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3107 (if (INTEGRAL_TYPE_P (type)
3108 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3109 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3110 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3111 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3112 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3113 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3114 (cond (cmp @2 @3) @1 @0)))
3116 (plus:c @0 (bit_and:c (minus @1 @0)
3117 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3118 (if (INTEGRAL_TYPE_P (type)
3119 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3120 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3121 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3122 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3123 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3124 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3125 (cond (cmp @2 @3) @1 @0)))
3126 /* Similarly with ^ instead of - though in that case with :c. */
3128 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3129 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3130 (if (INTEGRAL_TYPE_P (type)
3131 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3132 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3133 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3134 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3135 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3136 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3137 (cond (cmp @2 @3) @1 @0))))
3139 /* Simplifications of shift and rotates. */
3141 (for rotate (lrotate rrotate)
3143 (rotate integer_all_onesp@0 @1)
3146 /* Optimize -1 >> x for arithmetic right shifts. */
3148 (rshift integer_all_onesp@0 @1)
3149 (if (!TYPE_UNSIGNED (type))
3152 /* Optimize (x >> c) << c into x & (-1<<c). */
3154 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3155 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3156 /* It doesn't matter if the right shift is arithmetic or logical. */
3157 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3160 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3161 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3162 /* Allow intermediate conversion to integral type with whatever sign, as
3163 long as the low TYPE_PRECISION (type)
3164 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3165 && INTEGRAL_TYPE_P (type)
3166 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3167 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3168 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3169 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3170 || wi::geu_p (wi::to_wide (@1),
3171 TYPE_PRECISION (type)
3172 - TYPE_PRECISION (TREE_TYPE (@2)))))
3173 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3175 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3178 (rshift (lshift @0 INTEGER_CST@1) @1)
3179 (if (TYPE_UNSIGNED (type)
3180 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3181 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3183 /* Optimize x >> x into 0 */
3186 { build_zero_cst (type); })
3188 (for shiftrotate (lrotate rrotate lshift rshift)
3190 (shiftrotate @0 integer_zerop)
3193 (shiftrotate integer_zerop@0 @1)
3195 /* Prefer vector1 << scalar to vector1 << vector2
3196 if vector2 is uniform. */
3197 (for vec (VECTOR_CST CONSTRUCTOR)
3199 (shiftrotate @0 vec@1)
3200 (with { tree tem = uniform_vector_p (@1); }
3202 (shiftrotate @0 { tem; }))))))
3204 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3205 Y is 0. Similarly for X >> Y. */
3207 (for shift (lshift rshift)
3209 (shift @0 SSA_NAME@1)
3210 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3212 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3213 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3215 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3219 /* Rewrite an LROTATE_EXPR by a constant into an
3220 RROTATE_EXPR by a new constant. */
3222 (lrotate @0 INTEGER_CST@1)
3223 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3224 build_int_cst (TREE_TYPE (@1),
3225 element_precision (type)), @1); }))
3227 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3228 (for op (lrotate rrotate rshift lshift)
3230 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3231 (with { unsigned int prec = element_precision (type); }
3232 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3233 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3234 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3235 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3236 (with { unsigned int low = (tree_to_uhwi (@1)
3237 + tree_to_uhwi (@2)); }
3238 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3239 being well defined. */
3241 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3242 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3243 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3244 { build_zero_cst (type); }
3245 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3246 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3249 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3251 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3252 (if ((wi::to_wide (@1) & 1) != 0)
3253 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3254 { build_zero_cst (type); }))
3256 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3257 either to false if D is smaller (unsigned comparison) than C, or to
3258 x == log2 (D) - log2 (C). Similarly for right shifts. */
3262 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3263 (with { int c1 = wi::clz (wi::to_wide (@1));
3264 int c2 = wi::clz (wi::to_wide (@2)); }
3266 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3267 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3269 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3270 (if (tree_int_cst_sgn (@1) > 0)
3271 (with { int c1 = wi::clz (wi::to_wide (@1));
3272 int c2 = wi::clz (wi::to_wide (@2)); }
3274 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3275 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3277 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3278 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3282 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3283 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3285 || (!integer_zerop (@2)
3286 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3287 { constant_boolean_node (cmp == NE_EXPR, type); }
3288 (if (!integer_zerop (@2)
3289 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3290 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3292 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3293 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3294 if the new mask might be further optimized. */
3295 (for shift (lshift rshift)
3297 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3299 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3300 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3301 && tree_fits_uhwi_p (@1)
3302 && tree_to_uhwi (@1) > 0
3303 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3306 unsigned int shiftc = tree_to_uhwi (@1);
3307 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3308 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3309 tree shift_type = TREE_TYPE (@3);
3312 if (shift == LSHIFT_EXPR)
3313 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3314 else if (shift == RSHIFT_EXPR
3315 && type_has_mode_precision_p (shift_type))
3317 prec = TYPE_PRECISION (TREE_TYPE (@3));
3319 /* See if more bits can be proven as zero because of
3322 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3324 tree inner_type = TREE_TYPE (@0);
3325 if (type_has_mode_precision_p (inner_type)
3326 && TYPE_PRECISION (inner_type) < prec)
3328 prec = TYPE_PRECISION (inner_type);
3329 /* See if we can shorten the right shift. */
3331 shift_type = inner_type;
3332 /* Otherwise X >> C1 is all zeros, so we'll optimize
3333 it into (X, 0) later on by making sure zerobits
3337 zerobits = HOST_WIDE_INT_M1U;
3340 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3341 zerobits <<= prec - shiftc;
3343 /* For arithmetic shift if sign bit could be set, zerobits
3344 can contain actually sign bits, so no transformation is
3345 possible, unless MASK masks them all away. In that
3346 case the shift needs to be converted into logical shift. */
3347 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3348 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3350 if ((mask & zerobits) == 0)
3351 shift_type = unsigned_type_for (TREE_TYPE (@3));
3357 /* ((X << 16) & 0xff00) is (X, 0). */
3358 (if ((mask & zerobits) == mask)
3359 { build_int_cst (type, 0); }
3360 (with { newmask = mask | zerobits; }
3361 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3364 /* Only do the transformation if NEWMASK is some integer
3366 for (prec = BITS_PER_UNIT;
3367 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3368 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3371 (if (prec < HOST_BITS_PER_WIDE_INT
3372 || newmask == HOST_WIDE_INT_M1U)
3374 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3375 (if (!tree_int_cst_equal (newmaskt, @2))
3376 (if (shift_type != TREE_TYPE (@3))
3377 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3378 (bit_and @4 { newmaskt; })))))))))))))
3380 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3381 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3382 (for shift (lshift rshift)
3383 (for bit_op (bit_and bit_xor bit_ior)
3385 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3386 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3387 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3389 (bit_op (shift (convert @0) @1) { mask; })))))))
3391 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3393 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3394 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3395 && (element_precision (TREE_TYPE (@0))
3396 <= element_precision (TREE_TYPE (@1))
3397 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3399 { tree shift_type = TREE_TYPE (@0); }
3400 (convert (rshift (convert:shift_type @1) @2)))))
3402 /* ~(~X >>r Y) -> X >>r Y
3403 ~(~X <<r Y) -> X <<r Y */
3404 (for rotate (lrotate rrotate)
3406 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3407 (if ((element_precision (TREE_TYPE (@0))
3408 <= element_precision (TREE_TYPE (@1))
3409 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3410 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3411 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3413 { tree rotate_type = TREE_TYPE (@0); }
3414 (convert (rotate (convert:rotate_type @1) @2))))))
3417 (for rotate (lrotate rrotate)
3418 invrot (rrotate lrotate)
3419 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3421 (cmp (rotate @1 @0) (rotate @2 @0))
3423 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3425 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3426 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3427 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3429 (cmp (rotate @0 @1) INTEGER_CST@2)
3430 (if (integer_zerop (@2) || integer_all_onesp (@2))
3433 /* Both signed and unsigned lshift produce the same result, so use
3434 the form that minimizes the number of conversions. Postpone this
3435 transformation until after shifts by zero have been folded. */
3437 (convert (lshift:s@0 (convert:s@1 @2) INTEGER_CST@3))
3438 (if (INTEGRAL_TYPE_P (type)
3439 && tree_nop_conversion_p (type, TREE_TYPE (@0))
3440 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3441 && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
3442 && !integer_zerop (@3))
3443 (lshift (convert @2) @3)))
3445 /* Simplifications of conversions. */
3447 /* Basic strip-useless-type-conversions / strip_nops. */
3448 (for cvt (convert view_convert float fix_trunc)
3451 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3452 || (GENERIC && type == TREE_TYPE (@0)))
3455 /* Contract view-conversions. */
3457 (view_convert (view_convert @0))
3460 /* For integral conversions with the same precision or pointer
3461 conversions use a NOP_EXPR instead. */
3464 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3465 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3466 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3469 /* Strip inner integral conversions that do not change precision or size, or
3470 zero-extend while keeping the same size (for bool-to-char). */
3472 (view_convert (convert@0 @1))
3473 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3474 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3475 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3476 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3477 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3478 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3481 /* Simplify a view-converted empty constructor. */
3483 (view_convert CONSTRUCTOR@0)
3484 (if (TREE_CODE (@0) != SSA_NAME
3485 && CONSTRUCTOR_NELTS (@0) == 0)
3486 { build_zero_cst (type); }))
3488 /* Re-association barriers around constants and other re-association
3489 barriers can be removed. */
3491 (paren CONSTANT_CLASS_P@0)
3494 (paren (paren@1 @0))
3497 /* Handle cases of two conversions in a row. */
3498 (for ocvt (convert float fix_trunc)
3499 (for icvt (convert float)
3504 tree inside_type = TREE_TYPE (@0);
3505 tree inter_type = TREE_TYPE (@1);
3506 int inside_int = INTEGRAL_TYPE_P (inside_type);
3507 int inside_ptr = POINTER_TYPE_P (inside_type);
3508 int inside_float = FLOAT_TYPE_P (inside_type);
3509 int inside_vec = VECTOR_TYPE_P (inside_type);
3510 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3511 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3512 int inter_int = INTEGRAL_TYPE_P (inter_type);
3513 int inter_ptr = POINTER_TYPE_P (inter_type);
3514 int inter_float = FLOAT_TYPE_P (inter_type);
3515 int inter_vec = VECTOR_TYPE_P (inter_type);
3516 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3517 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3518 int final_int = INTEGRAL_TYPE_P (type);
3519 int final_ptr = POINTER_TYPE_P (type);
3520 int final_float = FLOAT_TYPE_P (type);
3521 int final_vec = VECTOR_TYPE_P (type);
3522 unsigned int final_prec = TYPE_PRECISION (type);
3523 int final_unsignedp = TYPE_UNSIGNED (type);
3526 /* In addition to the cases of two conversions in a row
3527 handled below, if we are converting something to its own
3528 type via an object of identical or wider precision, neither
3529 conversion is needed. */
3530 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3532 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3533 && (((inter_int || inter_ptr) && final_int)
3534 || (inter_float && final_float))
3535 && inter_prec >= final_prec)
3538 /* Likewise, if the intermediate and initial types are either both
3539 float or both integer, we don't need the middle conversion if the
3540 former is wider than the latter and doesn't change the signedness
3541 (for integers). Avoid this if the final type is a pointer since
3542 then we sometimes need the middle conversion. */
3543 (if (((inter_int && inside_int) || (inter_float && inside_float))
3544 && (final_int || final_float)
3545 && inter_prec >= inside_prec
3546 && (inter_float || inter_unsignedp == inside_unsignedp))
3549 /* If we have a sign-extension of a zero-extended value, we can
3550 replace that by a single zero-extension. Likewise if the
3551 final conversion does not change precision we can drop the
3552 intermediate conversion. */
3553 (if (inside_int && inter_int && final_int
3554 && ((inside_prec < inter_prec && inter_prec < final_prec
3555 && inside_unsignedp && !inter_unsignedp)
3556 || final_prec == inter_prec))
3559 /* Two conversions in a row are not needed unless:
3560 - some conversion is floating-point (overstrict for now), or
3561 - some conversion is a vector (overstrict for now), or
3562 - the intermediate type is narrower than both initial and
3564 - the intermediate type and innermost type differ in signedness,
3565 and the outermost type is wider than the intermediate, or
3566 - the initial type is a pointer type and the precisions of the
3567 intermediate and final types differ, or
3568 - the final type is a pointer type and the precisions of the
3569 initial and intermediate types differ. */
3570 (if (! inside_float && ! inter_float && ! final_float
3571 && ! inside_vec && ! inter_vec && ! final_vec
3572 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3573 && ! (inside_int && inter_int
3574 && inter_unsignedp != inside_unsignedp
3575 && inter_prec < final_prec)
3576 && ((inter_unsignedp && inter_prec > inside_prec)
3577 == (final_unsignedp && final_prec > inter_prec))
3578 && ! (inside_ptr && inter_prec != final_prec)
3579 && ! (final_ptr && inside_prec != inter_prec))
3582 /* A truncation to an unsigned type (a zero-extension) should be
3583 canonicalized as bitwise and of a mask. */
3584 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3585 && final_int && inter_int && inside_int
3586 && final_prec == inside_prec
3587 && final_prec > inter_prec
3589 (convert (bit_and @0 { wide_int_to_tree
3591 wi::mask (inter_prec, false,
3592 TYPE_PRECISION (inside_type))); })))
3594 /* If we are converting an integer to a floating-point that can
3595 represent it exactly and back to an integer, we can skip the
3596 floating-point conversion. */
3597 (if (GIMPLE /* PR66211 */
3598 && inside_int && inter_float && final_int &&
3599 (unsigned) significand_size (TYPE_MODE (inter_type))
3600 >= inside_prec - !inside_unsignedp)
3603 /* If we have a narrowing conversion to an integral type that is fed by a
3604 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3605 masks off bits outside the final type (and nothing else). */
3607 (convert (bit_and @0 INTEGER_CST@1))
3608 (if (INTEGRAL_TYPE_P (type)
3609 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3610 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3611 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3612 TYPE_PRECISION (type)), 0))
3616 /* (X /[ex] A) * A -> X. */
3618 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3621 /* Simplify (A / B) * B + (A % B) -> A. */
3622 (for div (trunc_div ceil_div floor_div round_div)
3623 mod (trunc_mod ceil_mod floor_mod round_mod)
3625 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3628 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3629 (for op (plus minus)
3631 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3632 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3633 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3636 wi::overflow_type overflow;
3637 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3638 TYPE_SIGN (type), &overflow);
3640 (if (types_match (type, TREE_TYPE (@2))
3641 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3642 (op @0 { wide_int_to_tree (type, mul); })
3643 (with { tree utype = unsigned_type_for (type); }
3644 (convert (op (convert:utype @0)
3645 (mult (convert:utype @1) (convert:utype @2))))))))))
3647 /* Canonicalization of binary operations. */
3649 /* Convert X + -C into X - C. */
3651 (plus @0 REAL_CST@1)
3652 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3653 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3654 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3655 (minus @0 { tem; })))))
3657 /* Convert x+x into x*2. */
3660 (if (SCALAR_FLOAT_TYPE_P (type))
3661 (mult @0 { build_real (type, dconst2); })
3662 (if (INTEGRAL_TYPE_P (type))
3663 (mult @0 { build_int_cst (type, 2); }))))
3667 (minus integer_zerop @1)
3670 (pointer_diff integer_zerop @1)
3671 (negate (convert @1)))
3673 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3674 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3675 (-ARG1 + ARG0) reduces to -ARG1. */
3677 (minus real_zerop@0 @1)
3678 (if (fold_real_zero_addition_p (type, @1, @0, 0))
3681 /* Transform x * -1 into -x. */
3683 (mult @0 integer_minus_onep)
3686 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3687 signed overflow for CST != 0 && CST != -1. */
3689 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3690 (if (TREE_CODE (@2) != INTEGER_CST
3692 && !integer_zerop (@1) && !integer_minus_onep (@1))
3693 (mult (mult @0 @2) @1)))
3695 /* True if we can easily extract the real and imaginary parts of a complex
3697 (match compositional_complex
3698 (convert? (complex @0 @1)))
3700 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3702 (complex (realpart @0) (imagpart @0))
3705 (realpart (complex @0 @1))
3708 (imagpart (complex @0 @1))
3711 /* Sometimes we only care about half of a complex expression. */
3713 (realpart (convert?:s (conj:s @0)))
3714 (convert (realpart @0)))
3716 (imagpart (convert?:s (conj:s @0)))
3717 (convert (negate (imagpart @0))))
3718 (for part (realpart imagpart)
3719 (for op (plus minus)
3721 (part (convert?:s@2 (op:s @0 @1)))
3722 (convert (op (part @0) (part @1))))))
3724 (realpart (convert?:s (CEXPI:s @0)))
3727 (imagpart (convert?:s (CEXPI:s @0)))
3730 /* conj(conj(x)) -> x */
3732 (conj (convert? (conj @0)))
3733 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3736 /* conj({x,y}) -> {x,-y} */
3738 (conj (convert?:s (complex:s @0 @1)))
3739 (with { tree itype = TREE_TYPE (type); }
3740 (complex (convert:itype @0) (negate (convert:itype @1)))))
3742 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3743 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
3744 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
3749 (bswap (bit_not (bswap @0)))
3751 (for bitop (bit_xor bit_ior bit_and)
3753 (bswap (bitop:c (bswap @0) @1))
3754 (bitop @0 (bswap @1))))
3757 (cmp (bswap@2 @0) (bswap @1))
3758 (with { tree ctype = TREE_TYPE (@2); }
3759 (cmp (convert:ctype @0) (convert:ctype @1))))
3761 (cmp (bswap @0) INTEGER_CST@1)
3762 (with { tree ctype = TREE_TYPE (@1); }
3763 (cmp (convert:ctype @0) (bswap @1)))))
3764 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
3766 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
3768 (if (BITS_PER_UNIT == 8
3769 && tree_fits_uhwi_p (@2)
3770 && tree_fits_uhwi_p (@3))
3773 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
3774 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
3775 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
3776 unsigned HOST_WIDE_INT lo = bits & 7;
3777 unsigned HOST_WIDE_INT hi = bits - lo;
3780 && mask < (256u>>lo)
3781 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
3782 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
3784 (bit_and (convert @1) @3)
3787 tree utype = unsigned_type_for (TREE_TYPE (@1));
3788 tree nst = build_int_cst (integer_type_node, ns);
3790 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
3791 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
3793 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
3794 (if (BITS_PER_UNIT == 8
3795 && CHAR_TYPE_SIZE == 8
3796 && tree_fits_uhwi_p (@1))
3799 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3800 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
3801 /* If the bswap was extended before the original shift, this
3802 byte (shift) has the sign of the extension, not the sign of
3803 the original shift. */
3804 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
3806 /* Special case: logical right shift of sign-extended bswap.
3807 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
3808 (if (TYPE_PRECISION (type) > prec
3809 && !TYPE_UNSIGNED (TREE_TYPE (@2))
3810 && TYPE_UNSIGNED (type)
3811 && bits < prec && bits + 8 >= prec)
3812 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
3813 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
3814 (if (bits + 8 == prec)
3815 (if (TYPE_UNSIGNED (st))
3816 (convert (convert:unsigned_char_type_node @0))
3817 (convert (convert:signed_char_type_node @0)))
3818 (if (bits < prec && bits + 8 > prec)
3821 tree nst = build_int_cst (integer_type_node, bits & 7);
3822 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
3823 : signed_char_type_node;
3825 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
3826 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
3828 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
3829 (if (BITS_PER_UNIT == 8
3830 && tree_fits_uhwi_p (@1)
3831 && tree_to_uhwi (@1) < 256)
3834 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3835 tree utype = unsigned_type_for (TREE_TYPE (@0));
3836 tree nst = build_int_cst (integer_type_node, prec - 8);
3838 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
3841 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3843 /* Simplify constant conditions.
3844 Only optimize constant conditions when the selected branch
3845 has the same type as the COND_EXPR. This avoids optimizing
3846 away "c ? x : throw", where the throw has a void type.
3847 Note that we cannot throw away the fold-const.c variant nor
3848 this one as we depend on doing this transform before possibly
3849 A ? B : B -> B triggers and the fold-const.c one can optimize
3850 0 ? A : B to B even if A has side-effects. Something
3851 genmatch cannot handle. */
3853 (cond INTEGER_CST@0 @1 @2)
3854 (if (integer_zerop (@0))
3855 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3857 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3860 (vec_cond VECTOR_CST@0 @1 @2)
3861 (if (integer_all_onesp (@0))
3863 (if (integer_zerop (@0))
3867 /* Sink unary operations to branches, but only if we do fold both. */
3868 (for op (negate bit_not abs absu)
3870 (op (vec_cond:s @0 @1 @2))
3871 (vec_cond @0 (op! @1) (op! @2))))
3873 /* Sink binary operation to branches, but only if we can fold it. */
3874 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3875 lshift rshift rdiv trunc_div ceil_div floor_div round_div
3876 trunc_mod ceil_mod floor_mod round_mod min max)
3877 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3879 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3880 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3882 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3884 (op (vec_cond:s @0 @1 @2) @3)
3885 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3887 (op @3 (vec_cond:s @0 @1 @2))
3888 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3891 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3892 Currently disabled after pass lvec because ARM understands
3893 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3895 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3896 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3897 (vec_cond (bit_and @0 @3) @1 @2)))
3899 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3900 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3901 (vec_cond (bit_ior @0 @3) @1 @2)))
3903 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3904 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3905 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3907 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3908 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3909 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3911 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3913 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3914 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3915 (vec_cond (bit_and @0 @1) @2 @3)))
3917 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3918 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3919 (vec_cond (bit_ior @0 @1) @2 @3)))
3921 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3922 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3923 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3925 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3926 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3927 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3929 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3930 types are compatible. */
3932 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3933 (if (VECTOR_BOOLEAN_TYPE_P (type)
3934 && types_match (type, TREE_TYPE (@0)))
3935 (if (integer_zerop (@1) && integer_all_onesp (@2))
3937 (if (integer_all_onesp (@1) && integer_zerop (@2))
3940 /* A few simplifications of "a ? CST1 : CST2". */
3941 /* NOTE: Only do this on gimple as the if-chain-to-switch
3942 optimization depends on the gimple to have if statements in it. */
3945 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
3947 (if (integer_zerop (@2))
3949 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
3950 (if (integer_onep (@1))
3951 (convert (convert:boolean_type_node @0)))
3952 /* a ? -1 : 0 -> -a. */
3953 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
3954 (negate (convert (convert:boolean_type_node @0))))
3955 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
3956 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
3958 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
3960 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
3961 (if (integer_zerop (@1))
3963 tree booltrue = constant_boolean_node (true, boolean_type_node);
3966 /* a ? 0 : 1 -> !a. */
3967 (if (integer_onep (@2))
3968 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
3969 /* a ? -1 : 0 -> -(!a). */
3970 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
3971 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
3972 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
3973 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
3975 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
3977 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
3981 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3983 /* This pattern implements two kinds simplification:
3986 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3987 1) Conversions are type widening from smaller type.
3988 2) Const c1 equals to c2 after canonicalizing comparison.
3989 3) Comparison has tree code LT, LE, GT or GE.
3990 This specific pattern is needed when (cmp (convert x) c) may not
3991 be simplified by comparison patterns because of multiple uses of
3992 x. It also makes sense here because simplifying across multiple
3993 referred var is always benefitial for complicated cases.
3996 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3997 (for cmp (lt le gt ge eq)
3999 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4002 tree from_type = TREE_TYPE (@1);
4003 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4004 enum tree_code code = ERROR_MARK;
4006 if (INTEGRAL_TYPE_P (from_type)
4007 && int_fits_type_p (@2, from_type)
4008 && (types_match (c1_type, from_type)
4009 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4010 && (TYPE_UNSIGNED (from_type)
4011 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4012 && (types_match (c2_type, from_type)
4013 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4014 && (TYPE_UNSIGNED (from_type)
4015 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4019 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4021 /* X <= Y - 1 equals to X < Y. */
4024 /* X > Y - 1 equals to X >= Y. */
4028 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4030 /* X < Y + 1 equals to X <= Y. */
4033 /* X >= Y + 1 equals to X > Y. */
4037 if (code != ERROR_MARK
4038 || wi::to_widest (@2) == wi::to_widest (@3))
4040 if (cmp == LT_EXPR || cmp == LE_EXPR)
4042 if (cmp == GT_EXPR || cmp == GE_EXPR)
4046 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4047 else if (int_fits_type_p (@3, from_type))
4051 (if (code == MAX_EXPR)
4052 (convert (max @1 (convert @2)))
4053 (if (code == MIN_EXPR)
4054 (convert (min @1 (convert @2)))
4055 (if (code == EQ_EXPR)
4056 (convert (cond (eq @1 (convert @3))
4057 (convert:from_type @3) (convert:from_type @2)))))))))
4059 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4061 1) OP is PLUS or MINUS.
4062 2) CMP is LT, LE, GT or GE.
4063 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4065 This pattern also handles special cases like:
4067 A) Operand x is a unsigned to signed type conversion and c1 is
4068 integer zero. In this case,
4069 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4070 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4071 B) Const c1 may not equal to (C3 op' C2). In this case we also
4072 check equality for (c1+1) and (c1-1) by adjusting comparison
4075 TODO: Though signed type is handled by this pattern, it cannot be
4076 simplified at the moment because C standard requires additional
4077 type promotion. In order to match&simplify it here, the IR needs
4078 to be cleaned up by other optimizers, i.e, VRP. */
4079 (for op (plus minus)
4080 (for cmp (lt le gt ge)
4082 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4083 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4084 (if (types_match (from_type, to_type)
4085 /* Check if it is special case A). */
4086 || (TYPE_UNSIGNED (from_type)
4087 && !TYPE_UNSIGNED (to_type)
4088 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4089 && integer_zerop (@1)
4090 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4093 wi::overflow_type overflow = wi::OVF_NONE;
4094 enum tree_code code, cmp_code = cmp;
4096 wide_int c1 = wi::to_wide (@1);
4097 wide_int c2 = wi::to_wide (@2);
4098 wide_int c3 = wi::to_wide (@3);
4099 signop sgn = TYPE_SIGN (from_type);
4101 /* Handle special case A), given x of unsigned type:
4102 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4103 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4104 if (!types_match (from_type, to_type))
4106 if (cmp_code == LT_EXPR)
4108 if (cmp_code == GE_EXPR)
4110 c1 = wi::max_value (to_type);
4112 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4113 compute (c3 op' c2) and check if it equals to c1 with op' being
4114 the inverted operator of op. Make sure overflow doesn't happen
4115 if it is undefined. */
4116 if (op == PLUS_EXPR)
4117 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4119 real_c1 = wi::add (c3, c2, sgn, &overflow);
4122 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4124 /* Check if c1 equals to real_c1. Boundary condition is handled
4125 by adjusting comparison operation if necessary. */
4126 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4129 /* X <= Y - 1 equals to X < Y. */
4130 if (cmp_code == LE_EXPR)
4132 /* X > Y - 1 equals to X >= Y. */
4133 if (cmp_code == GT_EXPR)
4136 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4139 /* X < Y + 1 equals to X <= Y. */
4140 if (cmp_code == LT_EXPR)
4142 /* X >= Y + 1 equals to X > Y. */
4143 if (cmp_code == GE_EXPR)
4146 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4148 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4150 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4155 (if (code == MAX_EXPR)
4156 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4157 { wide_int_to_tree (from_type, c2); })
4158 (if (code == MIN_EXPR)
4159 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4160 { wide_int_to_tree (from_type, c2); })))))))))
4162 (for cnd (cond vec_cond)
4163 /* A ? B : (A ? X : C) -> A ? B : C. */
4165 (cnd @0 (cnd @0 @1 @2) @3)
4168 (cnd @0 @1 (cnd @0 @2 @3))
4170 /* A ? B : (!A ? C : X) -> A ? B : C. */
4171 /* ??? This matches embedded conditions open-coded because genmatch
4172 would generate matching code for conditions in separate stmts only.
4173 The following is still important to merge then and else arm cases
4174 from if-conversion. */
4176 (cnd @0 @1 (cnd @2 @3 @4))
4177 (if (inverse_conditions_p (@0, @2))
4180 (cnd @0 (cnd @1 @2 @3) @4)
4181 (if (inverse_conditions_p (@0, @1))
4184 /* A ? B : B -> B. */
4189 /* !A ? B : C -> A ? C : B. */
4191 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4194 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4195 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4196 Need to handle UN* comparisons.
4198 None of these transformations work for modes with signed
4199 zeros. If A is +/-0, the first two transformations will
4200 change the sign of the result (from +0 to -0, or vice
4201 versa). The last four will fix the sign of the result,
4202 even though the original expressions could be positive or
4203 negative, depending on the sign of A.
4205 Note that all these transformations are correct if A is
4206 NaN, since the two alternatives (A and -A) are also NaNs. */
4208 (for cnd (cond vec_cond)
4209 /* A == 0 ? A : -A same as -A */
4212 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4213 (if (!HONOR_SIGNED_ZEROS (type))
4216 (cnd (cmp @0 zerop) integer_zerop (negate@1 @0))
4217 (if (!HONOR_SIGNED_ZEROS (type))
4220 /* A != 0 ? A : -A same as A */
4223 (cnd (cmp @0 zerop) @0 (negate @0))
4224 (if (!HONOR_SIGNED_ZEROS (type))
4227 (cnd (cmp @0 zerop) @0 integer_zerop)
4228 (if (!HONOR_SIGNED_ZEROS (type))
4231 /* A >=/> 0 ? A : -A same as abs (A) */
4234 (cnd (cmp @0 zerop) @0 (negate @0))
4235 (if (!HONOR_SIGNED_ZEROS (type)
4236 && !TYPE_UNSIGNED (type))
4238 /* A <=/< 0 ? A : -A same as -abs (A) */
4241 (cnd (cmp @0 zerop) @0 (negate @0))
4242 (if (!HONOR_SIGNED_ZEROS (type)
4243 && !TYPE_UNSIGNED (type))
4244 (if (ANY_INTEGRAL_TYPE_P (type)
4245 && !TYPE_OVERFLOW_WRAPS (type))
4247 tree utype = unsigned_type_for (type);
4249 (convert (negate (absu:utype @0))))
4250 (negate (abs @0)))))
4254 /* -(type)!A -> (type)A - 1. */
4256 (negate (convert?:s (logical_inverted_value:s @0)))
4257 (if (INTEGRAL_TYPE_P (type)
4258 && TREE_CODE (type) != BOOLEAN_TYPE
4259 && TYPE_PRECISION (type) > 1
4260 && TREE_CODE (@0) == SSA_NAME
4261 && ssa_name_has_boolean_range (@0))
4262 (plus (convert:type @0) { build_all_ones_cst (type); })))
4264 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4265 return all -1 or all 0 results. */
4266 /* ??? We could instead convert all instances of the vec_cond to negate,
4267 but that isn't necessarily a win on its own. */
4269 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4270 (if (VECTOR_TYPE_P (type)
4271 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4272 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4273 && (TYPE_MODE (TREE_TYPE (type))
4274 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4275 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4277 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4279 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4280 (if (VECTOR_TYPE_P (type)
4281 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4282 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4283 && (TYPE_MODE (TREE_TYPE (type))
4284 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4285 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4288 /* Simplifications of comparisons. */
4290 /* See if we can reduce the magnitude of a constant involved in a
4291 comparison by changing the comparison code. This is a canonicalization
4292 formerly done by maybe_canonicalize_comparison_1. */
4296 (cmp @0 uniform_integer_cst_p@1)
4297 (with { tree cst = uniform_integer_cst_p (@1); }
4298 (if (tree_int_cst_sgn (cst) == -1)
4299 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4300 wide_int_to_tree (TREE_TYPE (cst),
4306 (cmp @0 uniform_integer_cst_p@1)
4307 (with { tree cst = uniform_integer_cst_p (@1); }
4308 (if (tree_int_cst_sgn (cst) == 1)
4309 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4310 wide_int_to_tree (TREE_TYPE (cst),
4311 wi::to_wide (cst) - 1)); })))))
4313 /* We can simplify a logical negation of a comparison to the
4314 inverted comparison. As we cannot compute an expression
4315 operator using invert_tree_comparison we have to simulate
4316 that with expression code iteration. */
4317 (for cmp (tcc_comparison)
4318 icmp (inverted_tcc_comparison)
4319 ncmp (inverted_tcc_comparison_with_nans)
4320 /* Ideally we'd like to combine the following two patterns
4321 and handle some more cases by using
4322 (logical_inverted_value (cmp @0 @1))
4323 here but for that genmatch would need to "inline" that.
4324 For now implement what forward_propagate_comparison did. */
4326 (bit_not (cmp @0 @1))
4327 (if (VECTOR_TYPE_P (type)
4328 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4329 /* Comparison inversion may be impossible for trapping math,
4330 invert_tree_comparison will tell us. But we can't use
4331 a computed operator in the replacement tree thus we have
4332 to play the trick below. */
4333 (with { enum tree_code ic = invert_tree_comparison
4334 (cmp, HONOR_NANS (@0)); }
4340 (bit_xor (cmp @0 @1) integer_truep)
4341 (with { enum tree_code ic = invert_tree_comparison
4342 (cmp, HONOR_NANS (@0)); }
4348 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4349 ??? The transformation is valid for the other operators if overflow
4350 is undefined for the type, but performing it here badly interacts
4351 with the transformation in fold_cond_expr_with_comparison which
4352 attempts to synthetize ABS_EXPR. */
4354 (for sub (minus pointer_diff)
4356 (cmp (sub@2 @0 @1) integer_zerop)
4357 (if (single_use (@2))
4360 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4361 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4364 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4365 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4366 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4367 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4368 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4369 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4370 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4372 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4373 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4374 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4375 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4376 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4378 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4379 signed arithmetic case. That form is created by the compiler
4380 often enough for folding it to be of value. One example is in
4381 computing loop trip counts after Operator Strength Reduction. */
4382 (for cmp (simple_comparison)
4383 scmp (swapped_simple_comparison)
4385 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4386 /* Handle unfolded multiplication by zero. */
4387 (if (integer_zerop (@1))
4389 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4390 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4392 /* If @1 is negative we swap the sense of the comparison. */
4393 (if (tree_int_cst_sgn (@1) < 0)
4397 /* For integral types with undefined overflow fold
4398 x * C1 == C2 into x == C2 / C1 or false.
4399 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4403 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4404 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4405 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4406 && wi::to_wide (@1) != 0)
4407 (with { widest_int quot; }
4408 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4409 TYPE_SIGN (TREE_TYPE (@0)), "))
4410 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4411 { constant_boolean_node (cmp == NE_EXPR, type); }))
4412 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4413 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4414 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4417 tree itype = TREE_TYPE (@0);
4418 int p = TYPE_PRECISION (itype);
4419 wide_int m = wi::one (p + 1) << p;
4420 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4421 wide_int i = wide_int::from (wi::mod_inv (a, m),
4422 p, TYPE_SIGN (itype));
4423 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4426 /* Simplify comparison of something with itself. For IEEE
4427 floating-point, we can only do some of these simplifications. */
4431 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4432 || ! HONOR_NANS (@0))
4433 { constant_boolean_node (true, type); }
4434 (if (cmp != EQ_EXPR)
4440 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4441 || ! HONOR_NANS (@0))
4442 { constant_boolean_node (false, type); })))
4443 (for cmp (unle unge uneq)
4446 { constant_boolean_node (true, type); }))
4447 (for cmp (unlt ungt)
4453 (if (!flag_trapping_math)
4454 { constant_boolean_node (false, type); }))
4456 /* x == ~x -> false */
4457 /* x != ~x -> true */
4460 (cmp:c @0 (bit_not @0))
4461 { constant_boolean_node (cmp == NE_EXPR, type); }))
4463 /* Fold ~X op ~Y as Y op X. */
4464 (for cmp (simple_comparison)
4466 (cmp (bit_not@2 @0) (bit_not@3 @1))
4467 (if (single_use (@2) && single_use (@3))
4470 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4471 (for cmp (simple_comparison)
4472 scmp (swapped_simple_comparison)
4474 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4475 (if (single_use (@2)
4476 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4477 (scmp @0 (bit_not @1)))))
4479 (for cmp (simple_comparison)
4480 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4482 (cmp (convert@2 @0) (convert? @1))
4483 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4484 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4485 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4486 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4487 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4490 tree type1 = TREE_TYPE (@1);
4491 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4493 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4494 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4495 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4496 type1 = float_type_node;
4497 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4498 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4499 type1 = double_type_node;
4502 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4503 ? TREE_TYPE (@0) : type1);
4505 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4506 (cmp (convert:newtype @0) (convert:newtype @1))))))
4510 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4512 /* a CMP (-0) -> a CMP 0 */
4513 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4514 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4515 /* (-0) CMP b -> 0 CMP b. */
4516 (if (TREE_CODE (@0) == REAL_CST
4517 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
4518 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
4519 /* x != NaN is always true, other ops are always false. */
4520 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4521 && !tree_expr_signaling_nan_p (@1)
4522 && !tree_expr_maybe_signaling_nan_p (@0))
4523 { constant_boolean_node (cmp == NE_EXPR, type); })
4524 /* NaN != y is always true, other ops are always false. */
4525 (if (TREE_CODE (@0) == REAL_CST
4526 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
4527 && !tree_expr_signaling_nan_p (@0)
4528 && !tree_expr_signaling_nan_p (@1))
4529 { constant_boolean_node (cmp == NE_EXPR, type); })
4530 /* Fold comparisons against infinity. */
4531 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4532 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4535 REAL_VALUE_TYPE max;
4536 enum tree_code code = cmp;
4537 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4539 code = swap_tree_comparison (code);
4542 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4543 (if (code == GT_EXPR
4544 && !(HONOR_NANS (@0) && flag_trapping_math))
4545 { constant_boolean_node (false, type); })
4546 (if (code == LE_EXPR)
4547 /* x <= +Inf is always true, if we don't care about NaNs. */
4548 (if (! HONOR_NANS (@0))
4549 { constant_boolean_node (true, type); }
4550 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4551 an "invalid" exception. */
4552 (if (!flag_trapping_math)
4554 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4555 for == this introduces an exception for x a NaN. */
4556 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4558 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4560 (lt @0 { build_real (TREE_TYPE (@0), max); })
4561 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4562 /* x < +Inf is always equal to x <= DBL_MAX. */
4563 (if (code == LT_EXPR)
4564 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4566 (ge @0 { build_real (TREE_TYPE (@0), max); })
4567 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4568 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4569 an exception for x a NaN so use an unordered comparison. */
4570 (if (code == NE_EXPR)
4571 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4572 (if (! HONOR_NANS (@0))
4574 (ge @0 { build_real (TREE_TYPE (@0), max); })
4575 (le @0 { build_real (TREE_TYPE (@0), max); }))
4577 (unge @0 { build_real (TREE_TYPE (@0), max); })
4578 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4580 /* If this is a comparison of a real constant with a PLUS_EXPR
4581 or a MINUS_EXPR of a real constant, we can convert it into a
4582 comparison with a revised real constant as long as no overflow
4583 occurs when unsafe_math_optimizations are enabled. */
4584 (if (flag_unsafe_math_optimizations)
4585 (for op (plus minus)
4587 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4590 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4591 TREE_TYPE (@1), @2, @1);
4593 (if (tem && !TREE_OVERFLOW (tem))
4594 (cmp @0 { tem; }))))))
4596 /* Likewise, we can simplify a comparison of a real constant with
4597 a MINUS_EXPR whose first operand is also a real constant, i.e.
4598 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4599 floating-point types only if -fassociative-math is set. */
4600 (if (flag_associative_math)
4602 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4603 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4604 (if (tem && !TREE_OVERFLOW (tem))
4605 (cmp { tem; } @1)))))
4607 /* Fold comparisons against built-in math functions. */
4608 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4611 (cmp (sq @0) REAL_CST@1)
4613 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4615 /* sqrt(x) < y is always false, if y is negative. */
4616 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4617 { constant_boolean_node (false, type); })
4618 /* sqrt(x) > y is always true, if y is negative and we
4619 don't care about NaNs, i.e. negative values of x. */
4620 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4621 { constant_boolean_node (true, type); })
4622 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4623 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4624 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4626 /* sqrt(x) < 0 is always false. */
4627 (if (cmp == LT_EXPR)
4628 { constant_boolean_node (false, type); })
4629 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4630 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4631 { constant_boolean_node (true, type); })
4632 /* sqrt(x) <= 0 -> x == 0. */
4633 (if (cmp == LE_EXPR)
4635 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4636 == or !=. In the last case:
4638 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4640 if x is negative or NaN. Due to -funsafe-math-optimizations,
4641 the results for other x follow from natural arithmetic. */
4643 (if ((cmp == LT_EXPR
4647 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4648 /* Give up for -frounding-math. */
4649 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4653 enum tree_code ncmp = cmp;
4654 const real_format *fmt
4655 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4656 real_arithmetic (&c2, MULT_EXPR,
4657 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4658 real_convert (&c2, fmt, &c2);
4659 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4660 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4661 if (!REAL_VALUE_ISINF (c2))
4663 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4664 build_real (TREE_TYPE (@0), c2));
4665 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4667 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4668 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4669 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4670 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4671 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4672 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4675 /* With rounding to even, sqrt of up to 3 different values
4676 gives the same normal result, so in some cases c2 needs
4678 REAL_VALUE_TYPE c2alt, tow;
4679 if (cmp == LT_EXPR || cmp == GE_EXPR)
4683 real_nextafter (&c2alt, fmt, &c2, &tow);
4684 real_convert (&c2alt, fmt, &c2alt);
4685 if (REAL_VALUE_ISINF (c2alt))
4689 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4690 build_real (TREE_TYPE (@0), c2alt));
4691 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4693 else if (real_equal (&TREE_REAL_CST (c3),
4694 &TREE_REAL_CST (@1)))
4700 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4701 (if (REAL_VALUE_ISINF (c2))
4702 /* sqrt(x) > y is x == +Inf, when y is very large. */
4703 (if (HONOR_INFINITIES (@0))
4704 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4705 { constant_boolean_node (false, type); })
4706 /* sqrt(x) > c is the same as x > c*c. */
4707 (if (ncmp != ERROR_MARK)
4708 (if (ncmp == GE_EXPR)
4709 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4710 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4711 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4712 (if (REAL_VALUE_ISINF (c2))
4714 /* sqrt(x) < y is always true, when y is a very large
4715 value and we don't care about NaNs or Infinities. */
4716 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4717 { constant_boolean_node (true, type); })
4718 /* sqrt(x) < y is x != +Inf when y is very large and we
4719 don't care about NaNs. */
4720 (if (! HONOR_NANS (@0))
4721 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4722 /* sqrt(x) < y is x >= 0 when y is very large and we
4723 don't care about Infinities. */
4724 (if (! HONOR_INFINITIES (@0))
4725 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4726 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4729 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4730 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4731 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4732 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4733 (if (ncmp == LT_EXPR)
4734 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4735 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4736 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4737 (if (ncmp != ERROR_MARK && GENERIC)
4738 (if (ncmp == LT_EXPR)
4740 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4741 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4743 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4744 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4745 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4747 (cmp (sq @0) (sq @1))
4748 (if (! HONOR_NANS (@0))
4751 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4752 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4753 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4755 (cmp (float@0 @1) (float @2))
4756 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4757 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4760 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4761 tree type1 = TREE_TYPE (@1);
4762 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4763 tree type2 = TREE_TYPE (@2);
4764 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4766 (if (fmt.can_represent_integral_type_p (type1)
4767 && fmt.can_represent_integral_type_p (type2))
4768 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4769 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4770 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4771 && type1_signed_p >= type2_signed_p)
4772 (icmp @1 (convert @2))
4773 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4774 && type1_signed_p <= type2_signed_p)
4775 (icmp (convert:type2 @1) @2)
4776 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4777 && type1_signed_p == type2_signed_p)
4778 (icmp @1 @2))))))))))
4780 /* Optimize various special cases of (FTYPE) N CMP CST. */
4781 (for cmp (lt le eq ne ge gt)
4782 icmp (le le eq ne ge ge)
4784 (cmp (float @0) REAL_CST@1)
4785 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4786 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4789 tree itype = TREE_TYPE (@0);
4790 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4791 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4792 /* Be careful to preserve any potential exceptions due to
4793 NaNs. qNaNs are ok in == or != context.
4794 TODO: relax under -fno-trapping-math or
4795 -fno-signaling-nans. */
4797 = real_isnan (cst) && (cst->signalling
4798 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4800 /* TODO: allow non-fitting itype and SNaNs when
4801 -fno-trapping-math. */
4802 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4805 signop isign = TYPE_SIGN (itype);
4806 REAL_VALUE_TYPE imin, imax;
4807 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4808 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4810 REAL_VALUE_TYPE icst;
4811 if (cmp == GT_EXPR || cmp == GE_EXPR)
4812 real_ceil (&icst, fmt, cst);
4813 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4814 real_floor (&icst, fmt, cst);
4816 real_trunc (&icst, fmt, cst);
4818 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4820 bool overflow_p = false;
4822 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4825 /* Optimize cases when CST is outside of ITYPE's range. */
4826 (if (real_compare (LT_EXPR, cst, &imin))
4827 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4829 (if (real_compare (GT_EXPR, cst, &imax))
4830 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4832 /* Remove cast if CST is an integer representable by ITYPE. */
4834 (cmp @0 { gcc_assert (!overflow_p);
4835 wide_int_to_tree (itype, icst_val); })
4837 /* When CST is fractional, optimize
4838 (FTYPE) N == CST -> 0
4839 (FTYPE) N != CST -> 1. */
4840 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4841 { constant_boolean_node (cmp == NE_EXPR, type); })
4842 /* Otherwise replace with sensible integer constant. */
4845 gcc_checking_assert (!overflow_p);
4847 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4849 /* Fold A /[ex] B CMP C to A CMP B * C. */
4852 (cmp (exact_div @0 @1) INTEGER_CST@2)
4853 (if (!integer_zerop (@1))
4854 (if (wi::to_wide (@2) == 0)
4856 (if (TREE_CODE (@1) == INTEGER_CST)
4859 wi::overflow_type ovf;
4860 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4861 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4864 { constant_boolean_node (cmp == NE_EXPR, type); }
4865 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4866 (for cmp (lt le gt ge)
4868 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4869 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4872 wi::overflow_type ovf;
4873 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4874 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4877 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4878 TYPE_SIGN (TREE_TYPE (@2)))
4879 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4880 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4882 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4884 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4885 For large C (more than min/B+2^size), this is also true, with the
4886 multiplication computed modulo 2^size.
4887 For intermediate C, this just tests the sign of A. */
4888 (for cmp (lt le gt ge)
4891 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4892 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4893 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4894 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4897 tree utype = TREE_TYPE (@2);
4898 wide_int denom = wi::to_wide (@1);
4899 wide_int right = wi::to_wide (@2);
4900 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4901 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4902 bool small = wi::leu_p (right, smax);
4903 bool large = wi::geu_p (right, smin);
4905 (if (small || large)
4906 (cmp (convert:utype @0) (mult @2 (convert @1)))
4907 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4909 /* Unordered tests if either argument is a NaN. */
4911 (bit_ior (unordered @0 @0) (unordered @1 @1))
4912 (if (types_match (@0, @1))
4915 (bit_and (ordered @0 @0) (ordered @1 @1))
4916 (if (types_match (@0, @1))
4919 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4922 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4925 /* Simple range test simplifications. */
4926 /* A < B || A >= B -> true. */
4927 (for test1 (lt le le le ne ge)
4928 test2 (ge gt ge ne eq ne)
4930 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4931 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4932 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4933 { constant_boolean_node (true, type); })))
4934 /* A < B && A >= B -> false. */
4935 (for test1 (lt lt lt le ne eq)
4936 test2 (ge gt eq gt eq gt)
4938 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4939 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4940 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4941 { constant_boolean_node (false, type); })))
4943 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4944 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4946 Note that comparisons
4947 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4948 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4949 will be canonicalized to above so there's no need to
4956 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4957 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4960 tree ty = TREE_TYPE (@0);
4961 unsigned prec = TYPE_PRECISION (ty);
4962 wide_int mask = wi::to_wide (@2, prec);
4963 wide_int rhs = wi::to_wide (@3, prec);
4964 signop sgn = TYPE_SIGN (ty);
4966 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4967 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4968 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4969 { build_zero_cst (ty); }))))))
4971 /* -A CMP -B -> B CMP A. */
4972 (for cmp (tcc_comparison)
4973 scmp (swapped_tcc_comparison)
4975 (cmp (negate @0) (negate @1))
4976 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4977 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4978 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4981 (cmp (negate @0) CONSTANT_CLASS_P@1)
4982 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4983 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4984 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4985 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4986 (if (tem && !TREE_OVERFLOW (tem))
4987 (scmp @0 { tem; }))))))
4989 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4992 (op (abs @0) zerop@1)
4995 /* From fold_sign_changed_comparison and fold_widened_comparison.
4996 FIXME: the lack of symmetry is disturbing. */
4997 (for cmp (simple_comparison)
4999 (cmp (convert@0 @00) (convert?@1 @10))
5000 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5001 /* Disable this optimization if we're casting a function pointer
5002 type on targets that require function pointer canonicalization. */
5003 && !(targetm.have_canonicalize_funcptr_for_compare ()
5004 && ((POINTER_TYPE_P (TREE_TYPE (@00))
5005 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5006 || (POINTER_TYPE_P (TREE_TYPE (@10))
5007 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5009 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5010 && (TREE_CODE (@10) == INTEGER_CST
5012 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5015 && !POINTER_TYPE_P (TREE_TYPE (@00)))
5016 /* ??? The special-casing of INTEGER_CST conversion was in the original
5017 code and here to avoid a spurious overflow flag on the resulting
5018 constant which fold_convert produces. */
5019 (if (TREE_CODE (@1) == INTEGER_CST)
5020 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5021 TREE_OVERFLOW (@1)); })
5022 (cmp @00 (convert @1)))
5024 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5025 /* If possible, express the comparison in the shorter mode. */
5026 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5027 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5028 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5029 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5030 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5031 || ((TYPE_PRECISION (TREE_TYPE (@00))
5032 >= TYPE_PRECISION (TREE_TYPE (@10)))
5033 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5034 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5035 || (TREE_CODE (@10) == INTEGER_CST
5036 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5037 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5038 (cmp @00 (convert @10))
5039 (if (TREE_CODE (@10) == INTEGER_CST
5040 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5041 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5044 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5045 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5046 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5047 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5049 (if (above || below)
5050 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5051 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5052 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5053 { constant_boolean_node (above ? true : false, type); }
5054 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5055 { constant_boolean_node (above ? false : true, type); }))))))))))))
5059 /* SSA names are canonicalized to 2nd place. */
5060 (cmp addr@0 SSA_NAME@1)
5062 { poly_int64 off; tree base; }
5063 /* A local variable can never be pointed to by
5064 the default SSA name of an incoming parameter. */
5065 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5066 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5067 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5068 && TREE_CODE (base) == VAR_DECL
5069 && auto_var_in_fn_p (base, current_function_decl))
5070 (if (cmp == NE_EXPR)
5071 { constant_boolean_node (true, type); }
5072 { constant_boolean_node (false, type); })
5073 /* If the address is based on @1 decide using the offset. */
5074 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5075 && TREE_CODE (base) == MEM_REF
5076 && TREE_OPERAND (base, 0) == @1)
5077 (with { off += mem_ref_offset (base).force_shwi (); }
5078 (if (known_ne (off, 0))
5079 { constant_boolean_node (cmp == NE_EXPR, type); }
5080 (if (known_eq (off, 0))
5081 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5083 /* Equality compare simplifications from fold_binary */
5086 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5087 Similarly for NE_EXPR. */
5089 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5090 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5091 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5092 { constant_boolean_node (cmp == NE_EXPR, type); }))
5094 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5096 (cmp (bit_xor @0 @1) integer_zerop)
5099 /* (X ^ Y) == Y becomes X == 0.
5100 Likewise (X ^ Y) == X becomes Y == 0. */
5102 (cmp:c (bit_xor:c @0 @1) @0)
5103 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5106 /* (X & Y) == X becomes (X & ~Y) == 0. */
5108 (cmp:c (bit_and:c @0 @1) @0)
5109 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5111 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5112 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5113 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5114 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5115 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5116 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5117 && !wi::neg_p (wi::to_wide (@1)))
5118 (cmp (bit_and @0 (convert (bit_not @1)))
5119 { build_zero_cst (TREE_TYPE (@0)); })))
5121 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5123 (cmp:c (bit_ior:c @0 @1) @1)
5124 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5127 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5129 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5130 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5131 (cmp @0 (bit_xor @1 (convert @2)))))
5134 (cmp (convert? addr@0) integer_zerop)
5135 (if (tree_single_nonzero_warnv_p (@0, NULL))
5136 { constant_boolean_node (cmp == NE_EXPR, type); }))
5138 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5140 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5141 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5143 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5144 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5145 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5146 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5151 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5152 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5153 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5154 && types_match (@0, @1))
5155 (ncmp (bit_xor @0 @1) @2)))))
5156 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5157 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5161 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5162 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5163 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5164 && types_match (@0, @1))
5165 (ncmp (bit_xor @0 @1) @2))))
5167 /* If we have (A & C) == C where C is a power of 2, convert this into
5168 (A & C) != 0. Similarly for NE_EXPR. */
5172 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5173 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5176 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5177 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5179 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5180 (if (INTEGRAL_TYPE_P (type)
5181 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5182 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5183 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5186 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5188 (if (cmp == LT_EXPR)
5189 (bit_xor (convert (rshift @0 {shifter;})) @1)
5190 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5191 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5192 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5194 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5195 (if (INTEGRAL_TYPE_P (type)
5196 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5197 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5198 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5201 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5203 (if (cmp == GE_EXPR)
5204 (bit_xor (convert (rshift @0 {shifter;})) @1)
5205 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5207 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5208 convert this into a shift followed by ANDing with D. */
5211 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5212 INTEGER_CST@2 integer_zerop)
5213 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5215 int shift = (wi::exact_log2 (wi::to_wide (@2))
5216 - wi::exact_log2 (wi::to_wide (@1)));
5220 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5222 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5225 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5226 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5230 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5231 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5232 && type_has_mode_precision_p (TREE_TYPE (@0))
5233 && element_precision (@2) >= element_precision (@0)
5234 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5235 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5236 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5238 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5239 this into a right shift or sign extension followed by ANDing with C. */
5242 (lt @0 integer_zerop)
5243 INTEGER_CST@1 integer_zerop)
5244 (if (integer_pow2p (@1)
5245 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5247 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5251 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5253 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5254 sign extension followed by AND with C will achieve the effect. */
5255 (bit_and (convert @0) @1)))))
5257 /* When the addresses are not directly of decls compare base and offset.
5258 This implements some remaining parts of fold_comparison address
5259 comparisons but still no complete part of it. Still it is good
5260 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5261 (for cmp (simple_comparison)
5263 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5266 poly_int64 off0, off1;
5267 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
5268 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
5269 if (base0 && TREE_CODE (base0) == MEM_REF)
5271 off0 += mem_ref_offset (base0).force_shwi ();
5272 base0 = TREE_OPERAND (base0, 0);
5274 if (base1 && TREE_CODE (base1) == MEM_REF)
5276 off1 += mem_ref_offset (base1).force_shwi ();
5277 base1 = TREE_OPERAND (base1, 0);
5280 (if (base0 && base1)
5284 /* Punt in GENERIC on variables with value expressions;
5285 the value expressions might point to fields/elements
5286 of other vars etc. */
5288 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
5289 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
5291 else if (decl_in_symtab_p (base0)
5292 && decl_in_symtab_p (base1))
5293 equal = symtab_node::get_create (base0)
5294 ->equal_address_to (symtab_node::get_create (base1));
5295 else if ((DECL_P (base0)
5296 || TREE_CODE (base0) == SSA_NAME
5297 || TREE_CODE (base0) == STRING_CST)
5299 || TREE_CODE (base1) == SSA_NAME
5300 || TREE_CODE (base1) == STRING_CST))
5301 equal = (base0 == base1);
5304 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
5305 off0.is_constant (&ioff0);
5306 off1.is_constant (&ioff1);
5307 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
5308 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
5309 || (TREE_CODE (base0) == STRING_CST
5310 && TREE_CODE (base1) == STRING_CST
5311 && ioff0 >= 0 && ioff1 >= 0
5312 && ioff0 < TREE_STRING_LENGTH (base0)
5313 && ioff1 < TREE_STRING_LENGTH (base1)
5314 /* This is a too conservative test that the STRING_CSTs
5315 will not end up being string-merged. */
5316 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
5317 TREE_STRING_POINTER (base1) + ioff1,
5318 MIN (TREE_STRING_LENGTH (base0) - ioff0,
5319 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
5321 else if (!DECL_P (base0) || !DECL_P (base1))
5323 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
5325 /* If this is a pointer comparison, ignore for now even
5326 valid equalities where one pointer is the offset zero
5327 of one object and the other to one past end of another one. */
5328 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
5330 /* Assume that automatic variables can't be adjacent to global
5332 else if (is_global_var (base0) != is_global_var (base1))
5336 tree sz0 = DECL_SIZE_UNIT (base0);
5337 tree sz1 = DECL_SIZE_UNIT (base1);
5338 /* If sizes are unknown, e.g. VLA or not representable,
5340 if (!tree_fits_poly_int64_p (sz0)
5341 || !tree_fits_poly_int64_p (sz1))
5345 poly_int64 size0 = tree_to_poly_int64 (sz0);
5346 poly_int64 size1 = tree_to_poly_int64 (sz1);
5347 /* If one offset is pointing (or could be) to the beginning
5348 of one object and the other is pointing to one past the
5349 last byte of the other object, punt. */
5350 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
5352 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
5354 /* If both offsets are the same, there are some cases
5355 we know that are ok. Either if we know they aren't
5356 zero, or if we know both sizes are no zero. */
5358 && known_eq (off0, off1)
5359 && (known_ne (off0, 0)
5360 || (known_ne (size0, 0) && known_ne (size1, 0))))
5367 && (cmp == EQ_EXPR || cmp == NE_EXPR
5368 /* If the offsets are equal we can ignore overflow. */
5369 || known_eq (off0, off1)
5370 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5371 /* Or if we compare using pointers to decls or strings. */
5372 || (POINTER_TYPE_P (TREE_TYPE (@2))
5373 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
5375 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5376 { constant_boolean_node (known_eq (off0, off1), type); })
5377 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5378 { constant_boolean_node (known_ne (off0, off1), type); })
5379 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5380 { constant_boolean_node (known_lt (off0, off1), type); })
5381 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5382 { constant_boolean_node (known_le (off0, off1), type); })
5383 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5384 { constant_boolean_node (known_ge (off0, off1), type); })
5385 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5386 { constant_boolean_node (known_gt (off0, off1), type); }))
5389 (if (cmp == EQ_EXPR)
5390 { constant_boolean_node (false, type); })
5391 (if (cmp == NE_EXPR)
5392 { constant_boolean_node (true, type); })))))))))
5394 /* Simplify pointer equality compares using PTA. */
5398 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5399 && ptrs_compare_unequal (@0, @1))
5400 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5402 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5403 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5404 Disable the transform if either operand is pointer to function.
5405 This broke pr22051-2.c for arm where function pointer
5406 canonicalizaion is not wanted. */
5410 (cmp (convert @0) INTEGER_CST@1)
5411 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5412 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5413 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5414 /* Don't perform this optimization in GENERIC if @0 has reference
5415 type when sanitizing. See PR101210. */
5417 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5418 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5419 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5420 && POINTER_TYPE_P (TREE_TYPE (@1))
5421 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5422 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5423 (cmp @0 (convert @1)))))
5425 /* Non-equality compare simplifications from fold_binary */
5426 (for cmp (lt gt le ge)
5427 /* Comparisons with the highest or lowest possible integer of
5428 the specified precision will have known values. */
5430 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5431 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5432 || POINTER_TYPE_P (TREE_TYPE (@1))
5433 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5434 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5437 tree cst = uniform_integer_cst_p (@1);
5438 tree arg1_type = TREE_TYPE (cst);
5439 unsigned int prec = TYPE_PRECISION (arg1_type);
5440 wide_int max = wi::max_value (arg1_type);
5441 wide_int signed_max = wi::max_value (prec, SIGNED);
5442 wide_int min = wi::min_value (arg1_type);
5445 (if (wi::to_wide (cst) == max)
5447 (if (cmp == GT_EXPR)
5448 { constant_boolean_node (false, type); })
5449 (if (cmp == GE_EXPR)
5451 (if (cmp == LE_EXPR)
5452 { constant_boolean_node (true, type); })
5453 (if (cmp == LT_EXPR)
5455 (if (wi::to_wide (cst) == min)
5457 (if (cmp == LT_EXPR)
5458 { constant_boolean_node (false, type); })
5459 (if (cmp == LE_EXPR)
5461 (if (cmp == GE_EXPR)
5462 { constant_boolean_node (true, type); })
5463 (if (cmp == GT_EXPR)
5465 (if (wi::to_wide (cst) == max - 1)
5467 (if (cmp == GT_EXPR)
5468 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5469 wide_int_to_tree (TREE_TYPE (cst),
5472 (if (cmp == LE_EXPR)
5473 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5474 wide_int_to_tree (TREE_TYPE (cst),
5477 (if (wi::to_wide (cst) == min + 1)
5479 (if (cmp == GE_EXPR)
5480 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5481 wide_int_to_tree (TREE_TYPE (cst),
5484 (if (cmp == LT_EXPR)
5485 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5486 wide_int_to_tree (TREE_TYPE (cst),
5489 (if (wi::to_wide (cst) == signed_max
5490 && TYPE_UNSIGNED (arg1_type)
5491 /* We will flip the signedness of the comparison operator
5492 associated with the mode of @1, so the sign bit is
5493 specified by this mode. Check that @1 is the signed
5494 max associated with this sign bit. */
5495 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5496 /* signed_type does not work on pointer types. */
5497 && INTEGRAL_TYPE_P (arg1_type))
5498 /* The following case also applies to X < signed_max+1
5499 and X >= signed_max+1 because previous transformations. */
5500 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5501 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5503 (if (cst == @1 && cmp == LE_EXPR)
5504 (ge (convert:st @0) { build_zero_cst (st); }))
5505 (if (cst == @1 && cmp == GT_EXPR)
5506 (lt (convert:st @0) { build_zero_cst (st); }))
5507 (if (cmp == LE_EXPR)
5508 (ge (view_convert:st @0) { build_zero_cst (st); }))
5509 (if (cmp == GT_EXPR)
5510 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5512 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5513 /* If the second operand is NaN, the result is constant. */
5516 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5517 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5518 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5519 ? false : true, type); })))
5521 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5525 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5526 { constant_boolean_node (true, type); })
5527 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5528 { constant_boolean_node (false, type); })))
5530 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5534 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5535 { constant_boolean_node (false, type); })
5536 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5537 { constant_boolean_node (true, type); })))
5539 /* bool_var != 0 becomes bool_var. */
5541 (ne @0 integer_zerop)
5542 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5543 && types_match (type, TREE_TYPE (@0)))
5545 /* bool_var == 1 becomes bool_var. */
5547 (eq @0 integer_onep)
5548 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5549 && types_match (type, TREE_TYPE (@0)))
5552 bool_var == 0 becomes !bool_var or
5553 bool_var != 1 becomes !bool_var
5554 here because that only is good in assignment context as long
5555 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5556 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5557 clearly less optimal and which we'll transform again in forwprop. */
5559 /* When one argument is a constant, overflow detection can be simplified.
5560 Currently restricted to single use so as not to interfere too much with
5561 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5562 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5563 (for cmp (lt le ge gt)
5566 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5567 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5568 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5569 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5570 && wi::to_wide (@1) != 0
5573 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5574 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5576 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5577 wi::max_value (prec, sign)
5578 - wi::to_wide (@1)); })))))
5580 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5581 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5582 expects the long form, so we restrict the transformation for now. */
5585 (cmp:c (minus@2 @0 @1) @0)
5586 (if (single_use (@2)
5587 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5588 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5591 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5594 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5595 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5596 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5599 /* Testing for overflow is unnecessary if we already know the result. */
5604 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5605 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5606 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5607 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5612 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5613 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5614 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5615 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5617 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5618 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5622 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5623 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5624 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5625 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5627 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5628 is at least twice as wide as type of A and B, simplify to
5629 __builtin_mul_overflow (A, B, <unused>). */
5632 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5634 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5635 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5636 && TYPE_UNSIGNED (TREE_TYPE (@0))
5637 && (TYPE_PRECISION (TREE_TYPE (@3))
5638 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5639 && tree_fits_uhwi_p (@2)
5640 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5641 && types_match (@0, @1)
5642 && type_has_mode_precision_p (TREE_TYPE (@0))
5643 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5644 != CODE_FOR_nothing))
5645 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5646 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5648 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
5649 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
5651 (ovf (convert@2 @0) @1)
5652 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5653 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5654 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5655 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5658 (ovf @1 (convert@2 @0))
5659 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5660 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5661 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5662 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5665 /* Simplification of math builtins. These rules must all be optimizations
5666 as well as IL simplifications. If there is a possibility that the new
5667 form could be a pessimization, the rule should go in the canonicalization
5668 section that follows this one.
5670 Rules can generally go in this section if they satisfy one of
5673 - the rule describes an identity
5675 - the rule replaces calls with something as simple as addition or
5678 - the rule contains unary calls only and simplifies the surrounding
5679 arithmetic. (The idea here is to exclude non-unary calls in which
5680 one operand is constant and in which the call is known to be cheap
5681 when the operand has that value.) */
5683 (if (flag_unsafe_math_optimizations)
5684 /* Simplify sqrt(x) * sqrt(x) -> x. */
5686 (mult (SQRT_ALL@1 @0) @1)
5687 (if (!tree_expr_maybe_signaling_nan_p (@0))
5690 (for op (plus minus)
5691 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5695 (rdiv (op @0 @2) @1)))
5697 (for cmp (lt le gt ge)
5698 neg_cmp (gt ge lt le)
5699 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5701 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5703 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5705 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5706 || (real_zerop (tem) && !real_zerop (@1))))
5708 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5710 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5711 (neg_cmp @0 { tem; })))))))
5713 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5714 (for root (SQRT CBRT)
5716 (mult (root:s @0) (root:s @1))
5717 (root (mult @0 @1))))
5719 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5720 (for exps (EXP EXP2 EXP10 POW10)
5722 (mult (exps:s @0) (exps:s @1))
5723 (exps (plus @0 @1))))
5725 /* Simplify a/root(b/c) into a*root(c/b). */
5726 (for root (SQRT CBRT)
5728 (rdiv @0 (root:s (rdiv:s @1 @2)))
5729 (mult @0 (root (rdiv @2 @1)))))
5731 /* Simplify x/expN(y) into x*expN(-y). */
5732 (for exps (EXP EXP2 EXP10 POW10)
5734 (rdiv @0 (exps:s @1))
5735 (mult @0 (exps (negate @1)))))
5737 (for logs (LOG LOG2 LOG10 LOG10)
5738 exps (EXP EXP2 EXP10 POW10)
5739 /* logN(expN(x)) -> x. */
5743 /* expN(logN(x)) -> x. */
5748 /* Optimize logN(func()) for various exponential functions. We
5749 want to determine the value "x" and the power "exponent" in
5750 order to transform logN(x**exponent) into exponent*logN(x). */
5751 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5752 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5755 (if (SCALAR_FLOAT_TYPE_P (type))
5761 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5762 x = build_real_truncate (type, dconst_e ());
5765 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5766 x = build_real (type, dconst2);
5770 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5772 REAL_VALUE_TYPE dconst10;
5773 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5774 x = build_real (type, dconst10);
5781 (mult (logs { x; }) @0)))))
5789 (if (SCALAR_FLOAT_TYPE_P (type))
5795 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5796 x = build_real (type, dconsthalf);
5799 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5800 x = build_real_truncate (type, dconst_third ());
5806 (mult { x; } (logs @0))))))
5808 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5809 (for logs (LOG LOG2 LOG10)
5813 (mult @1 (logs @0))))
5815 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5816 or if C is a positive power of 2,
5817 pow(C,x) -> exp2(log2(C)*x). */
5825 (pows REAL_CST@0 @1)
5826 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5827 && real_isfinite (TREE_REAL_CST_PTR (@0))
5828 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5829 the use_exp2 case until after vectorization. It seems actually
5830 beneficial for all constants to postpone this until later,
5831 because exp(log(C)*x), while faster, will have worse precision
5832 and if x folds into a constant too, that is unnecessary
5834 && canonicalize_math_after_vectorization_p ())
5836 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5837 bool use_exp2 = false;
5838 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5839 && value->cl == rvc_normal)
5841 REAL_VALUE_TYPE frac_rvt = *value;
5842 SET_REAL_EXP (&frac_rvt, 1);
5843 if (real_equal (&frac_rvt, &dconst1))
5848 (if (optimize_pow_to_exp (@0, @1))
5849 (exps (mult (logs @0) @1)))
5850 (exp2s (mult (log2s @0) @1)))))))
5853 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5855 exps (EXP EXP2 EXP10 POW10)
5856 logs (LOG LOG2 LOG10 LOG10)
5858 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5859 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5860 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5861 (exps (plus (mult (logs @0) @1) @2)))))
5866 exps (EXP EXP2 EXP10 POW10)
5867 /* sqrt(expN(x)) -> expN(x*0.5). */
5870 (exps (mult @0 { build_real (type, dconsthalf); })))
5871 /* cbrt(expN(x)) -> expN(x/3). */
5874 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5875 /* pow(expN(x), y) -> expN(x*y). */
5878 (exps (mult @0 @1))))
5880 /* tan(atan(x)) -> x. */
5887 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5891 copysigns (COPYSIGN)
5896 REAL_VALUE_TYPE r_cst;
5897 build_sinatan_real (&r_cst, type);
5898 tree t_cst = build_real (type, r_cst);
5899 tree t_one = build_one_cst (type);
5901 (if (SCALAR_FLOAT_TYPE_P (type))
5902 (cond (lt (abs @0) { t_cst; })
5903 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5904 (copysigns { t_one; } @0))))))
5906 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5910 copysigns (COPYSIGN)
5915 REAL_VALUE_TYPE r_cst;
5916 build_sinatan_real (&r_cst, type);
5917 tree t_cst = build_real (type, r_cst);
5918 tree t_one = build_one_cst (type);
5919 tree t_zero = build_zero_cst (type);
5921 (if (SCALAR_FLOAT_TYPE_P (type))
5922 (cond (lt (abs @0) { t_cst; })
5923 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5924 (copysigns { t_zero; } @0))))))
5926 (if (!flag_errno_math)
5927 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5932 (sinhs (atanhs:s @0))
5933 (with { tree t_one = build_one_cst (type); }
5934 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5936 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5941 (coshs (atanhs:s @0))
5942 (with { tree t_one = build_one_cst (type); }
5943 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5945 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5947 (CABS (complex:C @0 real_zerop@1))
5950 /* trunc(trunc(x)) -> trunc(x), etc. */
5951 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5955 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5956 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5958 (fns integer_valued_real_p@0)
5961 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5963 (HYPOT:c @0 real_zerop@1)
5966 /* pow(1,x) -> 1. */
5968 (POW real_onep@0 @1)
5972 /* copysign(x,x) -> x. */
5973 (COPYSIGN_ALL @0 @0)
5977 /* copysign(x,-x) -> -x. */
5978 (COPYSIGN_ALL @0 (negate@1 @0))
5982 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5983 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5986 (for scale (LDEXP SCALBN SCALBLN)
5987 /* ldexp(0, x) -> 0. */
5989 (scale real_zerop@0 @1)
5991 /* ldexp(x, 0) -> x. */
5993 (scale @0 integer_zerop@1)
5995 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5997 (scale REAL_CST@0 @1)
5998 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6001 /* Canonicalization of sequences of math builtins. These rules represent
6002 IL simplifications but are not necessarily optimizations.
6004 The sincos pass is responsible for picking "optimal" implementations
6005 of math builtins, which may be more complicated and can sometimes go
6006 the other way, e.g. converting pow into a sequence of sqrts.
6007 We only want to do these canonicalizations before the pass has run. */
6009 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6010 /* Simplify tan(x) * cos(x) -> sin(x). */
6012 (mult:c (TAN:s @0) (COS:s @0))
6015 /* Simplify x * pow(x,c) -> pow(x,c+1). */
6017 (mult:c @0 (POW:s @0 REAL_CST@1))
6018 (if (!TREE_OVERFLOW (@1))
6019 (POW @0 (plus @1 { build_one_cst (type); }))))
6021 /* Simplify sin(x) / cos(x) -> tan(x). */
6023 (rdiv (SIN:s @0) (COS:s @0))
6026 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6028 (rdiv (SINH:s @0) (COSH:s @0))
6031 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6033 (rdiv (TANH:s @0) (SINH:s @0))
6034 (rdiv {build_one_cst (type);} (COSH @0)))
6036 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6038 (rdiv (COS:s @0) (SIN:s @0))
6039 (rdiv { build_one_cst (type); } (TAN @0)))
6041 /* Simplify sin(x) / tan(x) -> cos(x). */
6043 (rdiv (SIN:s @0) (TAN:s @0))
6044 (if (! HONOR_NANS (@0)
6045 && ! HONOR_INFINITIES (@0))
6048 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6050 (rdiv (TAN:s @0) (SIN:s @0))
6051 (if (! HONOR_NANS (@0)
6052 && ! HONOR_INFINITIES (@0))
6053 (rdiv { build_one_cst (type); } (COS @0))))
6055 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6057 (mult (POW:s @0 @1) (POW:s @0 @2))
6058 (POW @0 (plus @1 @2)))
6060 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6062 (mult (POW:s @0 @1) (POW:s @2 @1))
6063 (POW (mult @0 @2) @1))
6065 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6067 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6068 (POWI (mult @0 @2) @1))
6070 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6072 (rdiv (POW:s @0 REAL_CST@1) @0)
6073 (if (!TREE_OVERFLOW (@1))
6074 (POW @0 (minus @1 { build_one_cst (type); }))))
6076 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6078 (rdiv @0 (POW:s @1 @2))
6079 (mult @0 (POW @1 (negate @2))))
6084 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6087 (pows @0 { build_real (type, dconst_quarter ()); }))
6088 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6091 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6092 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6095 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6096 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6098 (cbrts (cbrts tree_expr_nonnegative_p@0))
6099 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6100 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6102 (sqrts (pows @0 @1))
6103 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6104 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6106 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6107 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6108 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6110 (pows (sqrts @0) @1)
6111 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6112 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6114 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6115 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6116 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6118 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6119 (pows @0 (mult @1 @2))))
6121 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6123 (CABS (complex @0 @0))
6124 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6126 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6129 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6131 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6136 (cexps compositional_complex@0)
6137 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6139 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6140 (mult @1 (imagpart @2)))))))
6142 (if (canonicalize_math_p ())
6143 /* floor(x) -> trunc(x) if x is nonnegative. */
6144 (for floors (FLOOR_ALL)
6147 (floors tree_expr_nonnegative_p@0)
6150 (match double_value_p
6152 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6153 (for froms (BUILT_IN_TRUNCL
6165 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6166 (if (optimize && canonicalize_math_p ())
6168 (froms (convert double_value_p@0))
6169 (convert (tos @0)))))
6171 (match float_value_p
6173 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6174 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6175 BUILT_IN_FLOORL BUILT_IN_FLOOR
6176 BUILT_IN_CEILL BUILT_IN_CEIL
6177 BUILT_IN_ROUNDL BUILT_IN_ROUND
6178 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6179 BUILT_IN_RINTL BUILT_IN_RINT)
6180 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6181 BUILT_IN_FLOORF BUILT_IN_FLOORF
6182 BUILT_IN_CEILF BUILT_IN_CEILF
6183 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6184 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6185 BUILT_IN_RINTF BUILT_IN_RINTF)
6186 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6188 (if (optimize && canonicalize_math_p ()
6189 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6191 (froms (convert float_value_p@0))
6192 (convert (tos @0)))))
6194 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6195 tos (XFLOOR XCEIL XROUND XRINT)
6196 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6197 (if (optimize && canonicalize_math_p ())
6199 (froms (convert double_value_p@0))
6202 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6203 XFLOOR XCEIL XROUND XRINT)
6204 tos (XFLOORF XCEILF XROUNDF XRINTF)
6205 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6207 (if (optimize && canonicalize_math_p ())
6209 (froms (convert float_value_p@0))
6212 (if (canonicalize_math_p ())
6213 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6214 (for floors (IFLOOR LFLOOR LLFLOOR)
6216 (floors tree_expr_nonnegative_p@0)
6219 (if (canonicalize_math_p ())
6220 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6221 (for fns (IFLOOR LFLOOR LLFLOOR
6223 IROUND LROUND LLROUND)
6225 (fns integer_valued_real_p@0)
6227 (if (!flag_errno_math)
6228 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6229 (for rints (IRINT LRINT LLRINT)
6231 (rints integer_valued_real_p@0)
6234 (if (canonicalize_math_p ())
6235 (for ifn (IFLOOR ICEIL IROUND IRINT)
6236 lfn (LFLOOR LCEIL LROUND LRINT)
6237 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6238 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6239 sizeof (int) == sizeof (long). */
6240 (if (TYPE_PRECISION (integer_type_node)
6241 == TYPE_PRECISION (long_integer_type_node))
6244 (lfn:long_integer_type_node @0)))
6245 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6246 sizeof (long long) == sizeof (long). */
6247 (if (TYPE_PRECISION (long_long_integer_type_node)
6248 == TYPE_PRECISION (long_integer_type_node))
6251 (lfn:long_integer_type_node @0)))))
6253 /* cproj(x) -> x if we're ignoring infinities. */
6256 (if (!HONOR_INFINITIES (type))
6259 /* If the real part is inf and the imag part is known to be
6260 nonnegative, return (inf + 0i). */
6262 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6263 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6264 { build_complex_inf (type, false); }))
6266 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6268 (CPROJ (complex @0 REAL_CST@1))
6269 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6270 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6276 (pows @0 REAL_CST@1)
6278 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6279 REAL_VALUE_TYPE tmp;
6282 /* pow(x,0) -> 1. */
6283 (if (real_equal (value, &dconst0))
6284 { build_real (type, dconst1); })
6285 /* pow(x,1) -> x. */
6286 (if (real_equal (value, &dconst1))
6288 /* pow(x,-1) -> 1/x. */
6289 (if (real_equal (value, &dconstm1))
6290 (rdiv { build_real (type, dconst1); } @0))
6291 /* pow(x,0.5) -> sqrt(x). */
6292 (if (flag_unsafe_math_optimizations
6293 && canonicalize_math_p ()
6294 && real_equal (value, &dconsthalf))
6296 /* pow(x,1/3) -> cbrt(x). */
6297 (if (flag_unsafe_math_optimizations
6298 && canonicalize_math_p ()
6299 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6300 real_equal (value, &tmp)))
6303 /* powi(1,x) -> 1. */
6305 (POWI real_onep@0 @1)
6309 (POWI @0 INTEGER_CST@1)
6311 /* powi(x,0) -> 1. */
6312 (if (wi::to_wide (@1) == 0)
6313 { build_real (type, dconst1); })
6314 /* powi(x,1) -> x. */
6315 (if (wi::to_wide (@1) == 1)
6317 /* powi(x,-1) -> 1/x. */
6318 (if (wi::to_wide (@1) == -1)
6319 (rdiv { build_real (type, dconst1); } @0))))
6321 /* Narrowing of arithmetic and logical operations.
6323 These are conceptually similar to the transformations performed for
6324 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6325 term we want to move all that code out of the front-ends into here. */
6327 /* Convert (outertype)((innertype0)a+(innertype1)b)
6328 into ((newtype)a+(newtype)b) where newtype
6329 is the widest mode from all of these. */
6330 (for op (plus minus mult rdiv)
6332 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6333 /* If we have a narrowing conversion of an arithmetic operation where
6334 both operands are widening conversions from the same type as the outer
6335 narrowing conversion. Then convert the innermost operands to a
6336 suitable unsigned type (to avoid introducing undefined behavior),
6337 perform the operation and convert the result to the desired type. */
6338 (if (INTEGRAL_TYPE_P (type)
6341 /* We check for type compatibility between @0 and @1 below,
6342 so there's no need to check that @2/@4 are integral types. */
6343 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6344 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6345 /* The precision of the type of each operand must match the
6346 precision of the mode of each operand, similarly for the
6348 && type_has_mode_precision_p (TREE_TYPE (@1))
6349 && type_has_mode_precision_p (TREE_TYPE (@2))
6350 && type_has_mode_precision_p (type)
6351 /* The inner conversion must be a widening conversion. */
6352 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
6353 && types_match (@1, type)
6354 && (types_match (@1, @2)
6355 /* Or the second operand is const integer or converted const
6356 integer from valueize. */
6357 || poly_int_tree_p (@4)))
6358 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
6359 (op @1 (convert @2))
6360 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6361 (convert (op (convert:utype @1)
6362 (convert:utype @2)))))
6363 (if (FLOAT_TYPE_P (type)
6364 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6365 == DECIMAL_FLOAT_TYPE_P (type))
6366 (with { tree arg0 = strip_float_extensions (@1);
6367 tree arg1 = strip_float_extensions (@2);
6368 tree itype = TREE_TYPE (@0);
6369 tree ty1 = TREE_TYPE (arg0);
6370 tree ty2 = TREE_TYPE (arg1);
6371 enum tree_code code = TREE_CODE (itype); }
6372 (if (FLOAT_TYPE_P (ty1)
6373 && FLOAT_TYPE_P (ty2))
6374 (with { tree newtype = type;
6375 if (TYPE_MODE (ty1) == SDmode
6376 || TYPE_MODE (ty2) == SDmode
6377 || TYPE_MODE (type) == SDmode)
6378 newtype = dfloat32_type_node;
6379 if (TYPE_MODE (ty1) == DDmode
6380 || TYPE_MODE (ty2) == DDmode
6381 || TYPE_MODE (type) == DDmode)
6382 newtype = dfloat64_type_node;
6383 if (TYPE_MODE (ty1) == TDmode
6384 || TYPE_MODE (ty2) == TDmode
6385 || TYPE_MODE (type) == TDmode)
6386 newtype = dfloat128_type_node; }
6387 (if ((newtype == dfloat32_type_node
6388 || newtype == dfloat64_type_node
6389 || newtype == dfloat128_type_node)
6391 && types_match (newtype, type))
6392 (op (convert:newtype @1) (convert:newtype @2))
6393 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
6395 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
6397 /* Sometimes this transformation is safe (cannot
6398 change results through affecting double rounding
6399 cases) and sometimes it is not. If NEWTYPE is
6400 wider than TYPE, e.g. (float)((long double)double
6401 + (long double)double) converted to
6402 (float)(double + double), the transformation is
6403 unsafe regardless of the details of the types
6404 involved; double rounding can arise if the result
6405 of NEWTYPE arithmetic is a NEWTYPE value half way
6406 between two representable TYPE values but the
6407 exact value is sufficiently different (in the
6408 right direction) for this difference to be
6409 visible in ITYPE arithmetic. If NEWTYPE is the
6410 same as TYPE, however, the transformation may be
6411 safe depending on the types involved: it is safe
6412 if the ITYPE has strictly more than twice as many
6413 mantissa bits as TYPE, can represent infinities
6414 and NaNs if the TYPE can, and has sufficient
6415 exponent range for the product or ratio of two
6416 values representable in the TYPE to be within the
6417 range of normal values of ITYPE. */
6418 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6419 && (flag_unsafe_math_optimizations
6420 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6421 && real_can_shorten_arithmetic (TYPE_MODE (itype),
6423 && !excess_precision_type (newtype)))
6424 && !types_match (itype, newtype))
6425 (convert:type (op (convert:newtype @1)
6426 (convert:newtype @2)))
6431 /* This is another case of narrowing, specifically when there's an outer
6432 BIT_AND_EXPR which masks off bits outside the type of the innermost
6433 operands. Like the previous case we have to convert the operands
6434 to unsigned types to avoid introducing undefined behavior for the
6435 arithmetic operation. */
6436 (for op (minus plus)
6438 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6439 (if (INTEGRAL_TYPE_P (type)
6440 /* We check for type compatibility between @0 and @1 below,
6441 so there's no need to check that @1/@3 are integral types. */
6442 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6443 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6444 /* The precision of the type of each operand must match the
6445 precision of the mode of each operand, similarly for the
6447 && type_has_mode_precision_p (TREE_TYPE (@0))
6448 && type_has_mode_precision_p (TREE_TYPE (@1))
6449 && type_has_mode_precision_p (type)
6450 /* The inner conversion must be a widening conversion. */
6451 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6452 && types_match (@0, @1)
6453 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6454 <= TYPE_PRECISION (TREE_TYPE (@0)))
6455 && (wi::to_wide (@4)
6456 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6457 true, TYPE_PRECISION (type))) == 0)
6458 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6459 (with { tree ntype = TREE_TYPE (@0); }
6460 (convert (bit_and (op @0 @1) (convert:ntype @4))))
6461 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6462 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6463 (convert:utype @4))))))))
6465 /* Transform (@0 < @1 and @0 < @2) to use min,
6466 (@0 > @1 and @0 > @2) to use max */
6467 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6468 op (lt le gt ge lt le gt ge )
6469 ext (min min max max max max min min )
6471 (logic (op:cs @0 @1) (op:cs @0 @2))
6472 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6473 && TREE_CODE (@0) != INTEGER_CST)
6474 (op @0 (ext @1 @2)))))
6477 /* signbit(x) -> 0 if x is nonnegative. */
6478 (SIGNBIT tree_expr_nonnegative_p@0)
6479 { integer_zero_node; })
6482 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6484 (if (!HONOR_SIGNED_ZEROS (@0))
6485 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6487 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6489 (for op (plus minus)
6492 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6493 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6494 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6495 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6496 && !TYPE_SATURATING (TREE_TYPE (@0)))
6497 (with { tree res = int_const_binop (rop, @2, @1); }
6498 (if (TREE_OVERFLOW (res)
6499 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6500 { constant_boolean_node (cmp == NE_EXPR, type); }
6501 (if (single_use (@3))
6502 (cmp @0 { TREE_OVERFLOW (res)
6503 ? drop_tree_overflow (res) : res; }))))))))
6504 (for cmp (lt le gt ge)
6505 (for op (plus minus)
6508 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6509 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6510 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6511 (with { tree res = int_const_binop (rop, @2, @1); }
6512 (if (TREE_OVERFLOW (res))
6514 fold_overflow_warning (("assuming signed overflow does not occur "
6515 "when simplifying conditional to constant"),
6516 WARN_STRICT_OVERFLOW_CONDITIONAL);
6517 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6518 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6519 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6520 TYPE_SIGN (TREE_TYPE (@1)))
6521 != (op == MINUS_EXPR);
6522 constant_boolean_node (less == ovf_high, type);
6524 (if (single_use (@3))
6527 fold_overflow_warning (("assuming signed overflow does not occur "
6528 "when changing X +- C1 cmp C2 to "
6530 WARN_STRICT_OVERFLOW_COMPARISON);
6532 (cmp @0 { res; })))))))))
6534 /* Canonicalizations of BIT_FIELD_REFs. */
6537 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6538 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6541 (BIT_FIELD_REF (view_convert @0) @1 @2)
6542 (BIT_FIELD_REF @0 @1 @2))
6545 (BIT_FIELD_REF @0 @1 integer_zerop)
6546 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6550 (BIT_FIELD_REF @0 @1 @2)
6552 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6553 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6555 (if (integer_zerop (@2))
6556 (view_convert (realpart @0)))
6557 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6558 (view_convert (imagpart @0)))))
6559 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6560 && INTEGRAL_TYPE_P (type)
6561 /* On GIMPLE this should only apply to register arguments. */
6562 && (! GIMPLE || is_gimple_reg (@0))
6563 /* A bit-field-ref that referenced the full argument can be stripped. */
6564 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6565 && integer_zerop (@2))
6566 /* Low-parts can be reduced to integral conversions.
6567 ??? The following doesn't work for PDP endian. */
6568 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6569 /* But only do this after vectorization. */
6570 && canonicalize_math_after_vectorization_p ()
6571 /* Don't even think about BITS_BIG_ENDIAN. */
6572 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6573 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6574 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6575 ? (TYPE_PRECISION (TREE_TYPE (@0))
6576 - TYPE_PRECISION (type))
6580 /* Simplify vector extracts. */
6583 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6584 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6585 && tree_fits_uhwi_p (TYPE_SIZE (type))
6586 && ((tree_to_uhwi (TYPE_SIZE (type))
6587 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6588 || (VECTOR_TYPE_P (type)
6589 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
6590 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
6593 tree ctor = (TREE_CODE (@0) == SSA_NAME
6594 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6595 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6596 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6597 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6598 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6601 && (idx % width) == 0
6603 && known_le ((idx + n) / width,
6604 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6609 /* Constructor elements can be subvectors. */
6611 if (CONSTRUCTOR_NELTS (ctor) != 0)
6613 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6614 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6615 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6617 unsigned HOST_WIDE_INT elt, count, const_k;
6620 /* We keep an exact subset of the constructor elements. */
6621 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6622 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6623 { build_zero_cst (type); }
6625 (if (elt < CONSTRUCTOR_NELTS (ctor))
6626 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6627 { build_zero_cst (type); })
6628 /* We don't want to emit new CTORs unless the old one goes away.
6629 ??? Eventually allow this if the CTOR ends up constant or
6631 (if (single_use (@0))
6634 vec<constructor_elt, va_gc> *vals;
6635 vec_alloc (vals, count);
6636 bool constant_p = true;
6638 for (unsigned i = 0;
6639 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6641 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
6642 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
6643 if (!CONSTANT_CLASS_P (e))
6646 tree evtype = (types_match (TREE_TYPE (type),
6647 TREE_TYPE (TREE_TYPE (ctor)))
6649 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
6651 res = (constant_p ? build_vector_from_ctor (evtype, vals)
6652 : build_constructor (evtype, vals));
6654 (view_convert { res; }))))))
6655 /* The bitfield references a single constructor element. */
6656 (if (k.is_constant (&const_k)
6657 && idx + n <= (idx / const_k + 1) * const_k)
6659 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6660 { build_zero_cst (type); })
6662 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6663 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6664 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6666 /* Simplify a bit extraction from a bit insertion for the cases with
6667 the inserted element fully covering the extraction or the insertion
6668 not touching the extraction. */
6670 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6673 unsigned HOST_WIDE_INT isize;
6674 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6675 isize = TYPE_PRECISION (TREE_TYPE (@1));
6677 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6680 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6681 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6682 wi::to_wide (@ipos) + isize))
6683 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6685 - wi::to_wide (@ipos)); }))
6686 (if (wi::geu_p (wi::to_wide (@ipos),
6687 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6688 || wi::geu_p (wi::to_wide (@rpos),
6689 wi::to_wide (@ipos) + isize))
6690 (BIT_FIELD_REF @0 @rsize @rpos)))))
6692 (if (canonicalize_math_after_vectorization_p ())
6695 (fmas:c (negate @0) @1 @2)
6696 (IFN_FNMA @0 @1 @2))
6698 (fmas @0 @1 (negate @2))
6701 (fmas:c (negate @0) @1 (negate @2))
6702 (IFN_FNMS @0 @1 @2))
6704 (negate (fmas@3 @0 @1 @2))
6705 (if (single_use (@3))
6706 (IFN_FNMS @0 @1 @2))))
6709 (IFN_FMS:c (negate @0) @1 @2)
6710 (IFN_FNMS @0 @1 @2))
6712 (IFN_FMS @0 @1 (negate @2))
6715 (IFN_FMS:c (negate @0) @1 (negate @2))
6716 (IFN_FNMA @0 @1 @2))
6718 (negate (IFN_FMS@3 @0 @1 @2))
6719 (if (single_use (@3))
6720 (IFN_FNMA @0 @1 @2)))
6723 (IFN_FNMA:c (negate @0) @1 @2)
6726 (IFN_FNMA @0 @1 (negate @2))
6727 (IFN_FNMS @0 @1 @2))
6729 (IFN_FNMA:c (negate @0) @1 (negate @2))
6732 (negate (IFN_FNMA@3 @0 @1 @2))
6733 (if (single_use (@3))
6734 (IFN_FMS @0 @1 @2)))
6737 (IFN_FNMS:c (negate @0) @1 @2)
6740 (IFN_FNMS @0 @1 (negate @2))
6741 (IFN_FNMA @0 @1 @2))
6743 (IFN_FNMS:c (negate @0) @1 (negate @2))
6746 (negate (IFN_FNMS@3 @0 @1 @2))
6747 (if (single_use (@3))
6748 (IFN_FMA @0 @1 @2))))
6750 /* CLZ simplifications. */
6755 (op (clz:s@2 @0) INTEGER_CST@1)
6756 (if (integer_zerop (@1) && single_use (@2))
6757 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6758 (with { tree type0 = TREE_TYPE (@0);
6759 tree stype = signed_type_for (type0);
6760 HOST_WIDE_INT val = 0;
6761 /* Punt on hypothetical weird targets. */
6763 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6769 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6770 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
6771 (with { bool ok = true;
6772 HOST_WIDE_INT val = 0;
6773 tree type0 = TREE_TYPE (@0);
6774 /* Punt on hypothetical weird targets. */
6776 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6778 && val == TYPE_PRECISION (type0) - 1)
6781 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
6782 (op @0 { build_one_cst (type0); })))))))
6784 /* CTZ simplifications. */
6786 (for op (ge gt le lt)
6789 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
6790 (op (ctz:s @0) INTEGER_CST@1)
6791 (with { bool ok = true;
6792 HOST_WIDE_INT val = 0;
6793 if (!tree_fits_shwi_p (@1))
6797 val = tree_to_shwi (@1);
6798 /* Canonicalize to >= or <. */
6799 if (op == GT_EXPR || op == LE_EXPR)
6801 if (val == HOST_WIDE_INT_MAX)
6807 bool zero_res = false;
6808 HOST_WIDE_INT zero_val = 0;
6809 tree type0 = TREE_TYPE (@0);
6810 int prec = TYPE_PRECISION (type0);
6812 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6817 (if (ok && (!zero_res || zero_val >= val))
6818 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
6820 (if (ok && (!zero_res || zero_val < val))
6821 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
6822 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
6823 (cmp (bit_and @0 { wide_int_to_tree (type0,
6824 wi::mask (val, false, prec)); })
6825 { build_zero_cst (type0); })))))))
6828 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
6829 (op (ctz:s @0) INTEGER_CST@1)
6830 (with { bool zero_res = false;
6831 HOST_WIDE_INT zero_val = 0;
6832 tree type0 = TREE_TYPE (@0);
6833 int prec = TYPE_PRECISION (type0);
6835 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6839 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
6840 (if (!zero_res || zero_val != wi::to_widest (@1))
6841 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
6842 (if (!zero_res || zero_val < 0 || zero_val >= prec)
6843 (op (bit_and @0 { wide_int_to_tree (type0,
6844 wi::mask (tree_to_uhwi (@1) + 1,
6846 { wide_int_to_tree (type0,
6847 wi::shifted_mask (tree_to_uhwi (@1), 1,
6848 false, prec)); })))))))
6850 /* POPCOUNT simplifications. */
6851 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6853 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6854 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6855 (POPCOUNT (bit_ior @0 @1))))
6857 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6858 (for popcount (POPCOUNT)
6859 (for cmp (le eq ne gt)
6862 (cmp (popcount @0) integer_zerop)
6863 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6865 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6867 (bit_and (POPCOUNT @0) integer_onep)
6870 /* PARITY simplifications. */
6871 /* parity(~X) is parity(X). */
6873 (PARITY (bit_not @0))
6876 /* parity(X)^parity(Y) is parity(X^Y). */
6878 (bit_xor (PARITY:s @0) (PARITY:s @1))
6879 (PARITY (bit_xor @0 @1)))
6881 /* Common POPCOUNT/PARITY simplifications. */
6882 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6883 (for pfun (POPCOUNT PARITY)
6886 (with { wide_int nz = tree_nonzero_bits (@0); }
6890 (if (wi::popcount (nz) == 1)
6891 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6892 (convert (rshift:utype (convert:utype @0)
6893 { build_int_cst (integer_type_node,
6894 wi::ctz (nz)); }))))))))
6897 /* 64- and 32-bits branchless implementations of popcount are detected:
6899 int popcount64c (uint64_t x)
6901 x -= (x >> 1) & 0x5555555555555555ULL;
6902 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6903 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6904 return (x * 0x0101010101010101ULL) >> 56;
6907 int popcount32c (uint32_t x)
6909 x -= (x >> 1) & 0x55555555;
6910 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6911 x = (x + (x >> 4)) & 0x0f0f0f0f;
6912 return (x * 0x01010101) >> 24;
6919 (rshift @8 INTEGER_CST@5)
6921 (bit_and @6 INTEGER_CST@7)
6925 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6931 /* Check constants and optab. */
6932 (with { unsigned prec = TYPE_PRECISION (type);
6933 int shift = (64 - prec) & 63;
6934 unsigned HOST_WIDE_INT c1
6935 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6936 unsigned HOST_WIDE_INT c2
6937 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6938 unsigned HOST_WIDE_INT c3
6939 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6940 unsigned HOST_WIDE_INT c4
6941 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6946 && TYPE_UNSIGNED (type)
6947 && integer_onep (@4)
6948 && wi::to_widest (@10) == 2
6949 && wi::to_widest (@5) == 4
6950 && wi::to_widest (@1) == prec - 8
6951 && tree_to_uhwi (@2) == c1
6952 && tree_to_uhwi (@3) == c2
6953 && tree_to_uhwi (@9) == c3
6954 && tree_to_uhwi (@7) == c3
6955 && tree_to_uhwi (@11) == c4)
6956 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6958 (convert (IFN_POPCOUNT:type @0))
6959 /* Try to do popcount in two halves. PREC must be at least
6960 five bits for this to work without extension before adding. */
6962 tree half_type = NULL_TREE;
6963 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
6966 && m.require () != TYPE_MODE (type))
6968 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
6969 half_type = build_nonstandard_integer_type (half_prec, 1);
6971 gcc_assert (half_prec > 2);
6973 (if (half_type != NULL_TREE
6974 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
6977 (IFN_POPCOUNT:half_type (convert @0))
6978 (IFN_POPCOUNT:half_type (convert (rshift @0
6979 { build_int_cst (integer_type_node, half_prec); } )))))))))))
6981 /* __builtin_ffs needs to deal on many targets with the possible zero
6982 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6983 should lead to better code. */
6985 (FFS tree_expr_nonzero_p@0)
6986 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6987 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6988 OPTIMIZE_FOR_SPEED))
6989 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6990 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6993 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6995 /* __builtin_ffs (X) == 0 -> X == 0.
6996 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6999 (cmp (ffs@2 @0) INTEGER_CST@1)
7000 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7002 (if (integer_zerop (@1))
7003 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7004 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7005 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7006 (if (single_use (@2))
7007 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7008 wi::mask (tree_to_uhwi (@1),
7010 { wide_int_to_tree (TREE_TYPE (@0),
7011 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7012 false, prec)); }))))))
7014 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
7018 bit_op (bit_and bit_ior)
7020 (cmp (ffs@2 @0) INTEGER_CST@1)
7021 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7023 (if (integer_zerop (@1))
7024 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7025 (if (tree_int_cst_sgn (@1) < 0)
7026 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7027 (if (wi::to_widest (@1) >= prec)
7028 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7029 (if (wi::to_widest (@1) == prec - 1)
7030 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7031 wi::shifted_mask (prec - 1, 1,
7033 (if (single_use (@2))
7034 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7036 { wide_int_to_tree (TREE_TYPE (@0),
7037 wi::mask (tree_to_uhwi (@1),
7039 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7048 r = c ? a1 op a2 : b;
7050 if the target can do it in one go. This makes the operation conditional
7051 on c, so could drop potentially-trapping arithmetic, but that's a valid
7052 simplification if the result of the operation isn't needed.
7054 Avoid speculatively generating a stand-alone vector comparison
7055 on targets that might not support them. Any target implementing
7056 conditional internal functions must support the same comparisons
7057 inside and outside a VEC_COND_EXPR. */
7060 (for uncond_op (UNCOND_BINARY)
7061 cond_op (COND_BINARY)
7063 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7064 (with { tree op_type = TREE_TYPE (@4); }
7065 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7066 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7067 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7069 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7070 (with { tree op_type = TREE_TYPE (@4); }
7071 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7072 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7073 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7075 /* Same for ternary operations. */
7076 (for uncond_op (UNCOND_TERNARY)
7077 cond_op (COND_TERNARY)
7079 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7080 (with { tree op_type = TREE_TYPE (@5); }
7081 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7082 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7083 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7085 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7086 (with { tree op_type = TREE_TYPE (@5); }
7087 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7088 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7089 (view_convert (cond_op (bit_not @0) @2 @3 @4
7090 (view_convert:op_type @1)))))))
7093 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7094 "else" value of an IFN_COND_*. */
7095 (for cond_op (COND_BINARY)
7097 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7098 (with { tree op_type = TREE_TYPE (@3); }
7099 (if (element_precision (type) == element_precision (op_type))
7100 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7102 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7103 (with { tree op_type = TREE_TYPE (@5); }
7104 (if (inverse_conditions_p (@0, @2)
7105 && element_precision (type) == element_precision (op_type))
7106 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7108 /* Same for ternary operations. */
7109 (for cond_op (COND_TERNARY)
7111 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7112 (with { tree op_type = TREE_TYPE (@4); }
7113 (if (element_precision (type) == element_precision (op_type))
7114 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7116 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7117 (with { tree op_type = TREE_TYPE (@6); }
7118 (if (inverse_conditions_p (@0, @2)
7119 && element_precision (type) == element_precision (op_type))
7120 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7122 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7125 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7126 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7128 If pointers are known not to wrap, B checks whether @1 bytes starting
7129 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7130 bytes. A is more efficiently tested as:
7132 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7134 The equivalent expression for B is given by replacing @1 with @1 - 1:
7136 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7138 @0 and @2 can be swapped in both expressions without changing the result.
7140 The folds rely on sizetype's being unsigned (which is always true)
7141 and on its being the same width as the pointer (which we have to check).
7143 The fold replaces two pointer_plus expressions, two comparisons and
7144 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7145 the best case it's a saving of two operations. The A fold retains one
7146 of the original pointer_pluses, so is a win even if both pointer_pluses
7147 are used elsewhere. The B fold is a wash if both pointer_pluses are
7148 used elsewhere, since all we end up doing is replacing a comparison with
7149 a pointer_plus. We do still apply the fold under those circumstances
7150 though, in case applying it to other conditions eventually makes one of the
7151 pointer_pluses dead. */
7152 (for ior (truth_orif truth_or bit_ior)
7155 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7156 (cmp:cs (pointer_plus@4 @2 @1) @0))
7157 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7158 && TYPE_OVERFLOW_WRAPS (sizetype)
7159 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7160 /* Calculate the rhs constant. */
7161 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7162 offset_int rhs = off * 2; }
7163 /* Always fails for negative values. */
7164 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7165 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7166 pick a canonical order. This increases the chances of using the
7167 same pointer_plus in multiple checks. */
7168 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7169 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7170 (if (cmp == LT_EXPR)
7171 (gt (convert:sizetype
7172 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7173 { swap_p ? @0 : @2; }))
7175 (gt (convert:sizetype
7176 (pointer_diff:ssizetype
7177 (pointer_plus { swap_p ? @2 : @0; }
7178 { wide_int_to_tree (sizetype, off); })
7179 { swap_p ? @0 : @2; }))
7180 { rhs_tree; })))))))))
7182 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7184 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7185 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7186 (with { int i = single_nonzero_element (@1); }
7188 (with { tree elt = vector_cst_elt (@1, i);
7189 tree elt_type = TREE_TYPE (elt);
7190 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7191 tree size = bitsize_int (elt_bits);
7192 tree pos = bitsize_int (elt_bits * i); }
7195 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7199 (vec_perm @0 @1 VECTOR_CST@2)
7202 tree op0 = @0, op1 = @1, op2 = @2;
7204 /* Build a vector of integers from the tree mask. */
7205 vec_perm_builder builder;
7206 if (!tree_to_vec_perm_builder (&builder, op2))
7209 /* Create a vec_perm_indices for the integer vector. */
7210 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7211 bool single_arg = (op0 == op1);
7212 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7214 (if (sel.series_p (0, 1, 0, 1))
7216 (if (sel.series_p (0, 1, nelts, 1))
7222 if (sel.all_from_input_p (0))
7224 else if (sel.all_from_input_p (1))
7227 sel.rotate_inputs (1);
7229 else if (known_ge (poly_uint64 (sel[0]), nelts))
7231 std::swap (op0, op1);
7232 sel.rotate_inputs (1);
7236 tree cop0 = op0, cop1 = op1;
7237 if (TREE_CODE (op0) == SSA_NAME
7238 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7239 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7240 cop0 = gimple_assign_rhs1 (def);
7241 if (TREE_CODE (op1) == SSA_NAME
7242 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7243 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7244 cop1 = gimple_assign_rhs1 (def);
7248 (if ((TREE_CODE (cop0) == VECTOR_CST
7249 || TREE_CODE (cop0) == CONSTRUCTOR)
7250 && (TREE_CODE (cop1) == VECTOR_CST
7251 || TREE_CODE (cop1) == CONSTRUCTOR)
7252 && (t = fold_vec_perm (type, cop0, cop1, sel)))
7256 bool changed = (op0 == op1 && !single_arg);
7257 tree ins = NULL_TREE;
7260 /* See if the permutation is performing a single element
7261 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7262 in that case. But only if the vector mode is supported,
7263 otherwise this is invalid GIMPLE. */
7264 if (TYPE_MODE (type) != BLKmode
7265 && (TREE_CODE (cop0) == VECTOR_CST
7266 || TREE_CODE (cop0) == CONSTRUCTOR
7267 || TREE_CODE (cop1) == VECTOR_CST
7268 || TREE_CODE (cop1) == CONSTRUCTOR))
7270 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7273 /* After canonicalizing the first elt to come from the
7274 first vector we only can insert the first elt from
7275 the first vector. */
7277 if ((ins = fold_read_from_vector (cop0, sel[0])))
7280 /* The above can fail for two-element vectors which always
7281 appear to insert the first element, so try inserting
7282 into the second lane as well. For more than two
7283 elements that's wasted time. */
7284 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
7286 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
7287 for (at = 0; at < encoded_nelts; ++at)
7288 if (maybe_ne (sel[at], at))
7290 if (at < encoded_nelts
7291 && (known_eq (at + 1, nelts)
7292 || sel.series_p (at + 1, 1, at + 1, 1)))
7294 if (known_lt (poly_uint64 (sel[at]), nelts))
7295 ins = fold_read_from_vector (cop0, sel[at]);
7297 ins = fold_read_from_vector (cop1, sel[at] - nelts);
7302 /* Generate a canonical form of the selector. */
7303 if (!ins && sel.encoding () != builder)
7305 /* Some targets are deficient and fail to expand a single
7306 argument permutation while still allowing an equivalent
7307 2-argument version. */
7309 if (sel.ninputs () == 2
7310 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
7311 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7314 vec_perm_indices sel2 (builder, 2, nelts);
7315 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
7316 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
7318 /* Not directly supported with either encoding,
7319 so use the preferred form. */
7320 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7322 if (!operand_equal_p (op2, oldop2, 0))
7327 (bit_insert { op0; } { ins; }
7328 { bitsize_int (at * vector_element_bits (type)); })
7330 (vec_perm { op0; } { op1; } { op2; }))))))))))
7332 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
7334 (match vec_same_elem_p
7336 (if (uniform_vector_p (@0))))
7338 (match vec_same_elem_p
7342 (vec_perm vec_same_elem_p@0 @0 @1)
7345 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
7346 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
7347 constant which when multiplied by a power of 2 contains a unique value
7348 in the top 5 or 6 bits. This is then indexed into a table which maps it
7349 to the number of trailing zeroes. */
7350 (match (ctz_table_index @1 @2 @3)
7351 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))