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 /* Unary operations and their associated IFN_COND_* function. */
82 (define_operator_list UNCOND_UNARY
84 (define_operator_list COND_UNARY
87 /* Binary operations and their associated IFN_COND_* function. */
88 (define_operator_list UNCOND_BINARY
90 mult trunc_div trunc_mod rdiv
92 bit_and bit_ior bit_xor
94 (define_operator_list COND_BINARY
95 IFN_COND_ADD IFN_COND_SUB
96 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
97 IFN_COND_MIN IFN_COND_MAX
98 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
99 IFN_COND_SHL IFN_COND_SHR)
101 /* Same for ternary operations. */
102 (define_operator_list UNCOND_TERNARY
103 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
104 (define_operator_list COND_TERNARY
105 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
107 /* With nop_convert? combine convert? and view_convert? in one pattern
108 plus conditionalize on tree_nop_conversion_p conversions. */
109 (match (nop_convert @0)
111 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
112 (match (nop_convert @0)
114 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
115 && known_eq (TYPE_VECTOR_SUBPARTS (type),
116 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
117 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
119 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
120 ABSU_EXPR returns unsigned absolute value of the operand and the operand
121 of the ABSU_EXPR will have the corresponding signed type. */
122 (simplify (abs (convert @0))
123 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
124 && !TYPE_UNSIGNED (TREE_TYPE (@0))
125 && element_precision (type) > element_precision (TREE_TYPE (@0)))
126 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
127 (convert (absu:utype @0)))))
130 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
132 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
133 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
134 && !TYPE_UNSIGNED (TREE_TYPE (@0))
135 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
139 /* Simplifications of operations with one constant operand and
140 simplifications to constants or single values. */
142 (for op (plus pointer_plus minus bit_ior bit_xor)
144 (op @0 integer_zerop)
147 /* 0 +p index -> (type)index */
149 (pointer_plus integer_zerop @1)
150 (non_lvalue (convert @1)))
152 /* ptr - 0 -> (type)ptr */
154 (pointer_diff @0 integer_zerop)
157 /* See if ARG1 is zero and X + ARG1 reduces to X.
158 Likewise if the operands are reversed. */
160 (plus:c @0 real_zerop@1)
161 (if (fold_real_zero_addition_p (type, @0, @1, 0))
164 /* See if ARG1 is zero and X - ARG1 reduces to X. */
166 (minus @0 real_zerop@1)
167 (if (fold_real_zero_addition_p (type, @0, @1, 1))
170 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
171 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
172 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
173 if not -frounding-math. For sNaNs the first operation would raise
174 exceptions but turn the result into qNan, so the second operation
175 would not raise it. */
176 (for inner_op (plus minus)
177 (for outer_op (plus minus)
179 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
182 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
183 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
184 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
186 = ((outer_op == PLUS_EXPR)
187 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
188 (if (outer_plus && !inner_plus)
193 This is unsafe for certain floats even in non-IEEE formats.
194 In IEEE, it is unsafe because it does wrong for NaNs.
195 Also note that operand_equal_p is always false if an operand
199 (if (!FLOAT_TYPE_P (type) || !tree_expr_maybe_nan_p (@0))
200 { build_zero_cst (type); }))
202 (pointer_diff @@0 @0)
203 { build_zero_cst (type); })
206 (mult @0 integer_zerop@1)
209 /* Maybe fold x * 0 to 0. The expressions aren't the same
210 when x is NaN, since x * 0 is also NaN. Nor are they the
211 same in modes with signed zeros, since multiplying a
212 negative value by 0 gives -0, not +0. */
214 (mult @0 real_zerop@1)
215 (if (!tree_expr_maybe_nan_p (@0)
216 && !tree_expr_maybe_real_minus_zero_p (@0)
217 && !tree_expr_maybe_real_minus_zero_p (@1))
220 /* In IEEE floating point, x*1 is not equivalent to x for snans.
221 Likewise for complex arithmetic with signed zeros. */
224 (if (!tree_expr_maybe_signaling_nan_p (@0)
225 && (!HONOR_SIGNED_ZEROS (type)
226 || !COMPLEX_FLOAT_TYPE_P (type)))
229 /* Transform x * -1.0 into -x. */
231 (mult @0 real_minus_onep)
232 (if (!tree_expr_maybe_signaling_nan_p (@0)
233 && (!HONOR_SIGNED_ZEROS (type)
234 || !COMPLEX_FLOAT_TYPE_P (type)))
237 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
239 (mult SSA_NAME@1 SSA_NAME@2)
240 (if (INTEGRAL_TYPE_P (type)
241 && get_nonzero_bits (@1) == 1
242 && get_nonzero_bits (@2) == 1)
245 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
246 unless the target has native support for the former but not the latter. */
248 (mult @0 VECTOR_CST@1)
249 (if (initializer_each_zero_or_onep (@1)
250 && !HONOR_SNANS (type)
251 && !HONOR_SIGNED_ZEROS (type))
252 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
254 && (!VECTOR_MODE_P (TYPE_MODE (type))
255 || (VECTOR_MODE_P (TYPE_MODE (itype))
256 && optab_handler (and_optab,
257 TYPE_MODE (itype)) != CODE_FOR_nothing)))
258 (view_convert (bit_and:itype (view_convert @0)
259 (ne @1 { build_zero_cst (type); })))))))
261 (for cmp (gt ge lt le)
262 outp (convert convert negate negate)
263 outn (negate negate convert convert)
264 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
265 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
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). */
269 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
270 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
272 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
273 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
274 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
275 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
277 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
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 @0))
284 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
287 /* Transform X * copysign (1.0, -X) into -abs(X). */
289 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
290 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
293 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
295 (COPYSIGN_ALL REAL_CST@0 @1)
296 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
297 (COPYSIGN_ALL (negate @0) @1)))
299 /* X * 1, X / 1 -> X. */
300 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
305 /* (A / (1 << B)) -> (A >> B).
306 Only for unsigned A. For signed A, this would not preserve rounding
308 For example: (-1 / ( 1 << B)) != -1 >> B.
309 Also also widening conversions, like:
310 (A / (unsigned long long) (1U << B)) -> (A >> B)
312 (A / (unsigned long long) (1 << B)) -> (A >> B).
313 If the left shift is signed, it can be done only if the upper bits
314 of A starting from shift's type sign bit are zero, as
315 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
316 so it is valid only if A >> 31 is zero. */
318 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
319 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
320 && (!VECTOR_TYPE_P (type)
321 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
322 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
323 && (useless_type_conversion_p (type, TREE_TYPE (@1))
324 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
325 && (TYPE_UNSIGNED (TREE_TYPE (@1))
326 || (element_precision (type)
327 == element_precision (TREE_TYPE (@1)))
328 || (INTEGRAL_TYPE_P (type)
329 && (tree_nonzero_bits (@0)
330 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
332 element_precision (type))) == 0)))))
333 (if (!VECTOR_TYPE_P (type)
334 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
335 && element_precision (TREE_TYPE (@3)) < element_precision (type))
336 (convert (rshift @3 @2))
339 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
340 undefined behavior in constexpr evaluation, and assuming that the division
341 traps enables better optimizations than these anyway. */
342 (for div (trunc_div ceil_div floor_div round_div exact_div)
343 /* 0 / X is always zero. */
345 (div integer_zerop@0 @1)
346 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
347 (if (!integer_zerop (@1))
351 (div @0 integer_minus_onep@1)
352 (if (!TYPE_UNSIGNED (type))
354 /* X / bool_range_Y is X. */
357 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
362 /* But not for 0 / 0 so that we can get the proper warnings and errors.
363 And not for _Fract types where we can't build 1. */
364 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
365 { build_one_cst (type); }))
366 /* X / abs (X) is X < 0 ? -1 : 1. */
369 (if (INTEGRAL_TYPE_P (type)
370 && TYPE_OVERFLOW_UNDEFINED (type))
371 (cond (lt @0 { build_zero_cst (type); })
372 { build_minus_one_cst (type); } { build_one_cst (type); })))
375 (div:C @0 (negate @0))
376 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
377 && TYPE_OVERFLOW_UNDEFINED (type))
378 { build_minus_one_cst (type); })))
380 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
381 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
384 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
385 && TYPE_UNSIGNED (type))
388 /* Combine two successive divisions. Note that combining ceil_div
389 and floor_div is trickier and combining round_div even more so. */
390 (for div (trunc_div exact_div)
392 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
394 wi::overflow_type overflow;
395 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
396 TYPE_SIGN (type), &overflow);
398 (if (div == EXACT_DIV_EXPR
399 || optimize_successive_divisions_p (@2, @3))
401 (div @0 { wide_int_to_tree (type, mul); })
402 (if (TYPE_UNSIGNED (type)
403 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
404 { build_zero_cst (type); }))))))
406 /* Combine successive multiplications. Similar to above, but handling
407 overflow is different. */
409 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
411 wi::overflow_type overflow;
412 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
413 TYPE_SIGN (type), &overflow);
415 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
416 otherwise undefined overflow implies that @0 must be zero. */
417 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
418 (mult @0 { wide_int_to_tree (type, mul); }))))
420 /* Optimize A / A to 1.0 if we don't care about
421 NaNs or Infinities. */
424 (if (FLOAT_TYPE_P (type)
425 && ! HONOR_NANS (type)
426 && ! HONOR_INFINITIES (type))
427 { build_one_cst (type); }))
429 /* Optimize -A / A to -1.0 if we don't care about
430 NaNs or Infinities. */
432 (rdiv:C @0 (negate @0))
433 (if (FLOAT_TYPE_P (type)
434 && ! HONOR_NANS (type)
435 && ! HONOR_INFINITIES (type))
436 { build_minus_one_cst (type); }))
438 /* PR71078: x / abs(x) -> copysign (1.0, x) */
440 (rdiv:C (convert? @0) (convert? (abs @0)))
441 (if (SCALAR_FLOAT_TYPE_P (type)
442 && ! HONOR_NANS (type)
443 && ! HONOR_INFINITIES (type))
445 (if (types_match (type, float_type_node))
446 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
447 (if (types_match (type, double_type_node))
448 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
449 (if (types_match (type, long_double_type_node))
450 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
452 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
455 (if (!tree_expr_maybe_signaling_nan_p (@0))
458 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
460 (rdiv @0 real_minus_onep)
461 (if (!tree_expr_maybe_signaling_nan_p (@0))
464 (if (flag_reciprocal_math)
465 /* Convert (A/B)/C to A/(B*C). */
467 (rdiv (rdiv:s @0 @1) @2)
468 (rdiv @0 (mult @1 @2)))
470 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
472 (rdiv @0 (mult:s @1 REAL_CST@2))
474 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
476 (rdiv (mult @0 { tem; } ) @1))))
478 /* Convert A/(B/C) to (A/B)*C */
480 (rdiv @0 (rdiv:s @1 @2))
481 (mult (rdiv @0 @1) @2)))
483 /* Simplify x / (- y) to -x / y. */
485 (rdiv @0 (negate @1))
486 (rdiv (negate @0) @1))
488 (if (flag_unsafe_math_optimizations)
489 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
490 Since C / x may underflow to zero, do this only for unsafe math. */
491 (for op (lt le gt ge)
494 (op (rdiv REAL_CST@0 @1) real_zerop@2)
495 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
497 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
499 /* For C < 0, use the inverted operator. */
500 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
503 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
504 (for div (trunc_div ceil_div floor_div round_div exact_div)
506 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
507 (if (integer_pow2p (@2)
508 && tree_int_cst_sgn (@2) > 0
509 && tree_nop_conversion_p (type, TREE_TYPE (@0))
510 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
512 { build_int_cst (integer_type_node,
513 wi::exact_log2 (wi::to_wide (@2))); }))))
515 /* If ARG1 is a constant, we can convert this to a multiply by the
516 reciprocal. This does not have the same rounding properties,
517 so only do this if -freciprocal-math. We can actually
518 always safely do it if ARG1 is a power of two, but it's hard to
519 tell if it is or not in a portable manner. */
520 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
524 (if (flag_reciprocal_math
527 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
529 (mult @0 { tem; } )))
530 (if (cst != COMPLEX_CST)
531 (with { tree inverse = exact_inverse (type, @1); }
533 (mult @0 { inverse; } ))))))))
535 (for mod (ceil_mod floor_mod round_mod trunc_mod)
536 /* 0 % X is always zero. */
538 (mod integer_zerop@0 @1)
539 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
540 (if (!integer_zerop (@1))
542 /* X % 1 is always zero. */
544 (mod @0 integer_onep)
545 { build_zero_cst (type); })
546 /* X % -1 is zero. */
548 (mod @0 integer_minus_onep@1)
549 (if (!TYPE_UNSIGNED (type))
550 { build_zero_cst (type); }))
554 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
555 (if (!integer_zerop (@0))
556 { build_zero_cst (type); }))
557 /* (X % Y) % Y is just X % Y. */
559 (mod (mod@2 @0 @1) @1)
561 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
563 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
564 (if (ANY_INTEGRAL_TYPE_P (type)
565 && TYPE_OVERFLOW_UNDEFINED (type)
566 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
568 { build_zero_cst (type); }))
569 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
570 modulo and comparison, since it is simpler and equivalent. */
573 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
574 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
575 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
576 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
578 /* X % -C is the same as X % C. */
580 (trunc_mod @0 INTEGER_CST@1)
581 (if (TYPE_SIGN (type) == SIGNED
582 && !TREE_OVERFLOW (@1)
583 && wi::neg_p (wi::to_wide (@1))
584 && !TYPE_OVERFLOW_TRAPS (type)
585 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
586 && !sign_bit_p (@1, @1))
587 (trunc_mod @0 (negate @1))))
589 /* X % -Y is the same as X % Y. */
591 (trunc_mod @0 (convert? (negate @1)))
592 (if (INTEGRAL_TYPE_P (type)
593 && !TYPE_UNSIGNED (type)
594 && !TYPE_OVERFLOW_TRAPS (type)
595 && tree_nop_conversion_p (type, TREE_TYPE (@1))
596 /* Avoid this transformation if X might be INT_MIN or
597 Y might be -1, because we would then change valid
598 INT_MIN % -(-1) into invalid INT_MIN % -1. */
599 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
600 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
602 (trunc_mod @0 (convert @1))))
604 /* X - (X / Y) * Y is the same as X % Y. */
606 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
607 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
608 (convert (trunc_mod @0 @1))))
610 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
611 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
612 Also optimize A % (C << N) where C is a power of 2,
613 to A & ((C << N) - 1).
614 Also optimize "A shift (B % C)", if C is a power of 2, to
615 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
616 and assume (B % C) is nonnegative as shifts negative values would
618 (match (power_of_two_cand @1)
620 (match (power_of_two_cand @1)
621 (lshift INTEGER_CST@1 @2))
622 (for mod (trunc_mod floor_mod)
623 (for shift (lshift rshift)
625 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
626 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
627 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
630 (mod @0 (convert? (power_of_two_cand@1 @2)))
631 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
632 /* Allow any integral conversions of the divisor, except
633 conversion from narrower signed to wider unsigned type
634 where if @1 would be negative power of two, the divisor
635 would not be a power of two. */
636 && INTEGRAL_TYPE_P (type)
637 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
638 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
639 || TYPE_UNSIGNED (TREE_TYPE (@1))
640 || !TYPE_UNSIGNED (type))
641 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
642 (with { tree utype = TREE_TYPE (@1);
643 if (!TYPE_OVERFLOW_WRAPS (utype))
644 utype = unsigned_type_for (utype); }
645 (bit_and @0 (convert (minus (convert:utype @1)
646 { build_one_cst (utype); })))))))
648 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
650 (trunc_div (mult @0 integer_pow2p@1) @1)
651 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
652 (bit_and @0 { wide_int_to_tree
653 (type, wi::mask (TYPE_PRECISION (type)
654 - wi::exact_log2 (wi::to_wide (@1)),
655 false, TYPE_PRECISION (type))); })))
657 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
659 (mult (trunc_div @0 integer_pow2p@1) @1)
660 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
661 (bit_and @0 (negate @1))))
663 /* Simplify (t * 2) / 2) -> t. */
664 (for div (trunc_div ceil_div floor_div round_div exact_div)
666 (div (mult:c @0 @1) @1)
667 (if (ANY_INTEGRAL_TYPE_P (type))
668 (if (TYPE_OVERFLOW_UNDEFINED (type))
673 bool overflowed = true;
674 value_range vr0, vr1;
675 if (INTEGRAL_TYPE_P (type)
676 && get_global_range_query ()->range_of_expr (vr0, @0)
677 && get_global_range_query ()->range_of_expr (vr1, @1)
678 && vr0.kind () == VR_RANGE
679 && vr1.kind () == VR_RANGE)
681 wide_int wmin0 = vr0.lower_bound ();
682 wide_int wmax0 = vr0.upper_bound ();
683 wide_int wmin1 = vr1.lower_bound ();
684 wide_int wmax1 = vr1.upper_bound ();
685 /* If the multiplication can't overflow/wrap around, then
686 it can be optimized too. */
687 wi::overflow_type min_ovf, max_ovf;
688 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
689 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
690 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
692 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
693 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
694 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
705 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
710 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
713 (pows (op @0) REAL_CST@1)
714 (with { HOST_WIDE_INT n; }
715 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
717 /* Likewise for powi. */
720 (pows (op @0) INTEGER_CST@1)
721 (if ((wi::to_wide (@1) & 1) == 0)
723 /* Strip negate and abs from both operands of hypot. */
731 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
732 (for copysigns (COPYSIGN_ALL)
734 (copysigns (op @0) @1)
737 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
742 /* Convert absu(x)*absu(x) -> x*x. */
744 (mult (absu@1 @0) @1)
745 (mult (convert@2 @0) @2))
747 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
751 (coss (copysigns @0 @1))
754 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
758 (pows (copysigns @0 @2) REAL_CST@1)
759 (with { HOST_WIDE_INT n; }
760 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
762 /* Likewise for powi. */
766 (pows (copysigns @0 @2) INTEGER_CST@1)
767 (if ((wi::to_wide (@1) & 1) == 0)
772 /* hypot(copysign(x, y), z) -> hypot(x, z). */
774 (hypots (copysigns @0 @1) @2)
776 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
778 (hypots @0 (copysigns @1 @2))
781 /* copysign(x, CST) -> [-]abs (x). */
782 (for copysigns (COPYSIGN_ALL)
784 (copysigns @0 REAL_CST@1)
785 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
789 /* copysign(copysign(x, y), z) -> copysign(x, z). */
790 (for copysigns (COPYSIGN_ALL)
792 (copysigns (copysigns @0 @1) @2)
795 /* copysign(x,y)*copysign(x,y) -> x*x. */
796 (for copysigns (COPYSIGN_ALL)
798 (mult (copysigns@2 @0 @1) @2)
801 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
802 (for ccoss (CCOS CCOSH)
807 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
808 (for ops (conj negate)
814 /* Fold (a * (1 << b)) into (a << b) */
816 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
817 (if (! FLOAT_TYPE_P (type)
818 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
821 /* Fold (1 << (C - x)) where C = precision(type) - 1
822 into ((1 << C) >> x). */
824 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
825 (if (INTEGRAL_TYPE_P (type)
826 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
828 (if (TYPE_UNSIGNED (type))
829 (rshift (lshift @0 @2) @3)
831 { tree utype = unsigned_type_for (type); }
832 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
834 /* Fold (C1/X)*C2 into (C1*C2)/X. */
836 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
837 (if (flag_associative_math
840 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
842 (rdiv { tem; } @1)))))
844 /* Simplify ~X & X as zero. */
846 (bit_and:c (convert? @0) (convert? (bit_not @0)))
847 { build_zero_cst (type); })
849 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
851 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
852 (if (TYPE_UNSIGNED (type))
853 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
855 (for bitop (bit_and bit_ior)
857 /* PR35691: Transform
858 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
859 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
861 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
862 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
863 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
864 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
865 (cmp (bit_ior @0 (convert @1)) @2)))
867 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
868 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
870 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
871 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
872 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
873 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
874 (cmp (bit_and @0 (convert @1)) @2))))
876 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
878 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
879 (minus (bit_xor @0 @1) @1))
881 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
882 (if (~wi::to_wide (@2) == wi::to_wide (@1))
883 (minus (bit_xor @0 @1) @1)))
885 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
887 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
888 (minus @1 (bit_xor @0 @1)))
890 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
891 (for op (bit_ior bit_xor plus)
893 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
896 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
897 (if (~wi::to_wide (@2) == wi::to_wide (@1))
900 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
902 (bit_ior:c (bit_xor:c @0 @1) @0)
905 /* (a & ~b) | (a ^ b) --> a ^ b */
907 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
910 /* (a & ~b) ^ ~a --> ~(a & b) */
912 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
913 (bit_not (bit_and @0 @1)))
915 /* (~a & b) ^ a --> (a | b) */
917 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
920 /* (a | b) & ~(a ^ b) --> a & b */
922 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
925 /* a | ~(a ^ b) --> a | ~b */
927 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
928 (bit_ior @0 (bit_not @1)))
930 /* (a | b) | (a &^ b) --> a | b */
931 (for op (bit_and bit_xor)
933 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
936 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
938 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
941 /* ~(~a & b) --> a | ~b */
943 (bit_not (bit_and:cs (bit_not @0) @1))
944 (bit_ior @0 (bit_not @1)))
946 /* ~(~a | b) --> a & ~b */
948 (bit_not (bit_ior:cs (bit_not @0) @1))
949 (bit_and @0 (bit_not @1)))
951 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
953 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
954 (bit_and @3 (bit_not @2)))
956 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
958 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
962 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
964 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
965 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
967 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
969 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
970 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
972 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
974 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
975 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
976 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
980 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
981 ((A & N) + B) & M -> (A + B) & M
982 Similarly if (N & M) == 0,
983 ((A | N) + B) & M -> (A + B) & M
984 and for - instead of + (or unary - instead of +)
985 and/or ^ instead of |.
986 If B is constant and (B & M) == 0, fold into A & M. */
988 (for bitop (bit_and bit_ior bit_xor)
990 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
993 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
994 @3, @4, @1, ERROR_MARK, NULL_TREE,
997 (convert (bit_and (op (convert:utype { pmop[0]; })
998 (convert:utype { pmop[1]; }))
999 (convert:utype @2))))))
1001 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1004 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1005 NULL_TREE, NULL_TREE, @1, bitop, @3,
1008 (convert (bit_and (op (convert:utype { pmop[0]; })
1009 (convert:utype { pmop[1]; }))
1010 (convert:utype @2)))))))
1012 (bit_and (op:s @0 @1) INTEGER_CST@2)
1015 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1016 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1017 NULL_TREE, NULL_TREE, pmop); }
1019 (convert (bit_and (op (convert:utype { pmop[0]; })
1020 (convert:utype { pmop[1]; }))
1021 (convert:utype @2)))))))
1022 (for bitop (bit_and bit_ior bit_xor)
1024 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1027 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1028 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1029 NULL_TREE, NULL_TREE, pmop); }
1031 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1032 (convert:utype @1)))))))
1034 /* X % Y is smaller than Y. */
1037 (cmp (trunc_mod @0 @1) @1)
1038 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1039 { constant_boolean_node (cmp == LT_EXPR, type); })))
1042 (cmp @1 (trunc_mod @0 @1))
1043 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1044 { constant_boolean_node (cmp == GT_EXPR, type); })))
1048 (bit_ior @0 integer_all_onesp@1)
1053 (bit_ior @0 integer_zerop)
1058 (bit_and @0 integer_zerop@1)
1064 (for op (bit_ior bit_xor plus)
1066 (op:c (convert? @0) (convert? (bit_not @0)))
1067 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1072 { build_zero_cst (type); })
1074 /* Canonicalize X ^ ~0 to ~X. */
1076 (bit_xor @0 integer_all_onesp@1)
1081 (bit_and @0 integer_all_onesp)
1084 /* x & x -> x, x | x -> x */
1085 (for bitop (bit_and bit_ior)
1090 /* x & C -> x if we know that x & ~C == 0. */
1093 (bit_and SSA_NAME@0 INTEGER_CST@1)
1094 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1095 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1099 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1101 (bit_not (minus (bit_not @0) @1))
1104 (bit_not (plus:c (bit_not @0) @1))
1107 /* ~(X - Y) -> ~X + Y. */
1109 (bit_not (minus:s @0 @1))
1110 (plus (bit_not @0) @1))
1112 (bit_not (plus:s @0 INTEGER_CST@1))
1113 (if ((INTEGRAL_TYPE_P (type)
1114 && TYPE_UNSIGNED (type))
1115 || (!TYPE_OVERFLOW_SANITIZED (type)
1116 && may_negate_without_overflow_p (@1)))
1117 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1120 /* ~X + Y -> (Y - X) - 1. */
1122 (plus:c (bit_not @0) @1)
1123 (if (ANY_INTEGRAL_TYPE_P (type)
1124 && TYPE_OVERFLOW_WRAPS (type)
1125 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1126 && !integer_all_onesp (@1))
1127 (plus (minus @1 @0) { build_minus_one_cst (type); })
1128 (if (INTEGRAL_TYPE_P (type)
1129 && TREE_CODE (@1) == INTEGER_CST
1130 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1132 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1134 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1136 (bit_not (rshift:s @0 @1))
1137 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1138 (rshift (bit_not! @0) @1)
1139 /* For logical right shifts, this is possible only if @0 doesn't
1140 have MSB set and the logical right shift is changed into
1141 arithmetic shift. */
1142 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1143 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1144 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1147 /* x + (x & 1) -> (x + 1) & ~1 */
1149 (plus:c @0 (bit_and:s @0 integer_onep@1))
1150 (bit_and (plus @0 @1) (bit_not @1)))
1152 /* x & ~(x & y) -> x & ~y */
1153 /* x | ~(x | y) -> x | ~y */
1154 (for bitop (bit_and bit_ior)
1156 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1157 (bitop @0 (bit_not @1))))
1159 /* (~x & y) | ~(x | y) -> ~x */
1161 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1164 /* (x | y) ^ (x | ~y) -> ~x */
1166 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1169 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1171 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1172 (bit_not (bit_xor @0 @1)))
1174 /* (~x | y) ^ (x ^ y) -> x | ~y */
1176 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1177 (bit_ior @0 (bit_not @1)))
1179 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1181 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1182 (bit_not (bit_and @0 @1)))
1184 /* (x | y) & ~x -> y & ~x */
1185 /* (x & y) | ~x -> y | ~x */
1186 (for bitop (bit_and bit_ior)
1187 rbitop (bit_ior bit_and)
1189 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1192 /* (x & y) ^ (x | y) -> x ^ y */
1194 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1197 /* (x ^ y) ^ (x | y) -> x & y */
1199 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1202 /* (x & y) + (x ^ y) -> x | y */
1203 /* (x & y) | (x ^ y) -> x | y */
1204 /* (x & y) ^ (x ^ y) -> x | y */
1205 (for op (plus bit_ior bit_xor)
1207 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1210 /* (x & y) + (x | y) -> x + y */
1212 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1215 /* (x + y) - (x | y) -> x & y */
1217 (minus (plus @0 @1) (bit_ior @0 @1))
1218 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1219 && !TYPE_SATURATING (type))
1222 /* (x + y) - (x & y) -> x | y */
1224 (minus (plus @0 @1) (bit_and @0 @1))
1225 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1226 && !TYPE_SATURATING (type))
1229 /* (x | y) - y -> (x & ~y) */
1231 (minus (bit_ior:cs @0 @1) @1)
1232 (bit_and @0 (bit_not @1)))
1234 /* (x | y) - (x ^ y) -> x & y */
1236 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1239 /* (x | y) - (x & y) -> x ^ y */
1241 (minus (bit_ior @0 @1) (bit_and @0 @1))
1244 /* (x | y) & ~(x & y) -> x ^ y */
1246 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1249 /* (x | y) & (~x ^ y) -> x & y */
1251 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1254 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1256 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1257 (bit_not (bit_xor @0 @1)))
1259 /* (~x | y) ^ (x | ~y) -> x ^ y */
1261 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1264 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1266 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1267 (nop_convert2? (bit_ior @0 @1))))
1269 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1270 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1271 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1272 && !TYPE_SATURATING (TREE_TYPE (@2)))
1273 (bit_not (convert (bit_xor @0 @1)))))
1275 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1277 (nop_convert3? (bit_ior @0 @1)))
1278 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1279 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1280 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1281 && !TYPE_SATURATING (TREE_TYPE (@2)))
1282 (bit_not (convert (bit_xor @0 @1)))))
1284 (minus (nop_convert1? (bit_and @0 @1))
1285 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1287 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1288 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1289 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1290 && !TYPE_SATURATING (TREE_TYPE (@2)))
1291 (bit_not (convert (bit_xor @0 @1)))))
1293 /* ~x & ~y -> ~(x | y)
1294 ~x | ~y -> ~(x & y) */
1295 (for op (bit_and bit_ior)
1296 rop (bit_ior bit_and)
1298 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1299 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1300 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1301 (bit_not (rop (convert @0) (convert @1))))))
1303 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1304 with a constant, and the two constants have no bits in common,
1305 we should treat this as a BIT_IOR_EXPR since this may produce more
1307 (for op (bit_xor plus)
1309 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1310 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1311 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1312 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1313 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1314 (bit_ior (convert @4) (convert @5)))))
1316 /* (X | Y) ^ X -> Y & ~ X*/
1318 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1319 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1320 (convert (bit_and @1 (bit_not @0)))))
1322 /* Convert ~X ^ ~Y to X ^ Y. */
1324 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1325 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1326 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1327 (bit_xor (convert @0) (convert @1))))
1329 /* Convert ~X ^ C to X ^ ~C. */
1331 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1332 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1333 (bit_xor (convert @0) (bit_not @1))))
1335 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1336 (for opo (bit_and bit_xor)
1337 opi (bit_xor bit_and)
1339 (opo:c (opi:cs @0 @1) @1)
1340 (bit_and (bit_not @0) @1)))
1342 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1343 operands are another bit-wise operation with a common input. If so,
1344 distribute the bit operations to save an operation and possibly two if
1345 constants are involved. For example, convert
1346 (A | B) & (A | C) into A | (B & C)
1347 Further simplification will occur if B and C are constants. */
1348 (for op (bit_and bit_ior bit_xor)
1349 rop (bit_ior bit_and bit_and)
1351 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1352 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1353 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1354 (rop (convert @0) (op (convert @1) (convert @2))))))
1356 /* Some simple reassociation for bit operations, also handled in reassoc. */
1357 /* (X & Y) & Y -> X & Y
1358 (X | Y) | Y -> X | Y */
1359 (for op (bit_and bit_ior)
1361 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1363 /* (X ^ Y) ^ Y -> X */
1365 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1367 /* (X & Y) & (X & Z) -> (X & Y) & Z
1368 (X | Y) | (X | Z) -> (X | Y) | Z */
1369 (for op (bit_and bit_ior)
1371 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1372 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1373 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1374 (if (single_use (@5) && single_use (@6))
1375 (op @3 (convert @2))
1376 (if (single_use (@3) && single_use (@4))
1377 (op (convert @1) @5))))))
1378 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1380 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1381 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1382 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1383 (bit_xor (convert @1) (convert @2))))
1385 /* Convert abs (abs (X)) into abs (X).
1386 also absu (absu (X)) into absu (X). */
1392 (absu (convert@2 (absu@1 @0)))
1393 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1396 /* Convert abs[u] (-X) -> abs[u] (X). */
1405 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1407 (abs tree_expr_nonnegative_p@0)
1411 (absu tree_expr_nonnegative_p@0)
1414 /* Simplify (-(X < 0) | 1) * X into abs (X). */
1416 (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
1417 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
1420 /* Similarly (-(X < 0) | 1U) * X into absu (X). */
1422 (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
1423 integer_onep) (nop_convert @0))
1424 (if (INTEGRAL_TYPE_P (type)
1425 && TYPE_UNSIGNED (type)
1426 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1427 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1430 /* A few cases of fold-const.c negate_expr_p predicate. */
1431 (match negate_expr_p
1433 (if ((INTEGRAL_TYPE_P (type)
1434 && TYPE_UNSIGNED (type))
1435 || (!TYPE_OVERFLOW_SANITIZED (type)
1436 && may_negate_without_overflow_p (t)))))
1437 (match negate_expr_p
1439 (match negate_expr_p
1441 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1442 (match negate_expr_p
1444 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1445 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1447 (match negate_expr_p
1449 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1450 (match negate_expr_p
1452 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1453 || (FLOAT_TYPE_P (type)
1454 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1455 && !HONOR_SIGNED_ZEROS (type)))))
1457 /* (-A) * (-B) -> A * B */
1459 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1460 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1461 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1462 (mult (convert @0) (convert (negate @1)))))
1464 /* -(A + B) -> (-B) - A. */
1466 (negate (plus:c @0 negate_expr_p@1))
1467 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1468 && !HONOR_SIGNED_ZEROS (type))
1469 (minus (negate @1) @0)))
1471 /* -(A - B) -> B - A. */
1473 (negate (minus @0 @1))
1474 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1475 || (FLOAT_TYPE_P (type)
1476 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1477 && !HONOR_SIGNED_ZEROS (type)))
1480 (negate (pointer_diff @0 @1))
1481 (if (TYPE_OVERFLOW_UNDEFINED (type))
1482 (pointer_diff @1 @0)))
1484 /* A - B -> A + (-B) if B is easily negatable. */
1486 (minus @0 negate_expr_p@1)
1487 (if (!FIXED_POINT_TYPE_P (type))
1488 (plus @0 (negate @1))))
1490 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1492 (negate (mult:c@0 @1 negate_expr_p@2))
1493 (if (! TYPE_UNSIGNED (type)
1494 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1496 (mult @1 (negate @2))))
1499 (negate (rdiv@0 @1 negate_expr_p@2))
1500 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1502 (rdiv @1 (negate @2))))
1505 (negate (rdiv@0 negate_expr_p@1 @2))
1506 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1508 (rdiv (negate @1) @2)))
1510 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1512 (negate (convert? (rshift @0 INTEGER_CST@1)))
1513 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1514 && wi::to_wide (@1) == element_precision (type) - 1)
1515 (with { tree stype = TREE_TYPE (@0);
1516 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1517 : unsigned_type_for (stype); }
1518 (convert (rshift:ntype (convert:ntype @0) @1)))))
1520 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1522 For bitwise binary operations apply operand conversions to the
1523 binary operation result instead of to the operands. This allows
1524 to combine successive conversions and bitwise binary operations.
1525 We combine the above two cases by using a conditional convert. */
1526 (for bitop (bit_and bit_ior bit_xor)
1528 (bitop (convert@2 @0) (convert?@3 @1))
1529 (if (((TREE_CODE (@1) == INTEGER_CST
1530 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1531 && int_fits_type_p (@1, TREE_TYPE (@0)))
1532 || types_match (@0, @1))
1533 /* ??? This transform conflicts with fold-const.c doing
1534 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1535 constants (if x has signed type, the sign bit cannot be set
1536 in c). This folds extension into the BIT_AND_EXPR.
1537 Restrict it to GIMPLE to avoid endless recursions. */
1538 && (bitop != BIT_AND_EXPR || GIMPLE)
1539 && (/* That's a good idea if the conversion widens the operand, thus
1540 after hoisting the conversion the operation will be narrower. */
1541 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1542 /* It's also a good idea if the conversion is to a non-integer
1544 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1545 /* Or if the precision of TO is not the same as the precision
1547 || !type_has_mode_precision_p (type)
1548 /* In GIMPLE, getting rid of 2 conversions for one new results
1551 && TREE_CODE (@1) != INTEGER_CST
1552 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1554 && single_use (@3))))
1555 (convert (bitop @0 (convert @1)))))
1556 /* In GIMPLE, getting rid of 2 conversions for one new results
1559 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1561 && TREE_CODE (@1) != INTEGER_CST
1562 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1563 && types_match (type, @0))
1564 (bitop @0 (convert @1)))))
1566 (for bitop (bit_and bit_ior)
1567 rbitop (bit_ior bit_and)
1568 /* (x | y) & x -> x */
1569 /* (x & y) | x -> x */
1571 (bitop:c (rbitop:c @0 @1) @0)
1573 /* (~x | y) & x -> x & y */
1574 /* (~x & y) | x -> x | y */
1576 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1579 /* ((x | y) & z) | x -> (z & y) | x */
1581 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1582 (bit_ior (bit_and @2 @1) @0))
1584 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1586 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1587 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1589 /* Combine successive equal operations with constants. */
1590 (for bitop (bit_and bit_ior bit_xor)
1592 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1593 (if (!CONSTANT_CLASS_P (@0))
1594 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1595 folded to a constant. */
1596 (bitop @0 (bitop @1 @2))
1597 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1598 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1599 the values involved are such that the operation can't be decided at
1600 compile time. Try folding one of @0 or @1 with @2 to see whether
1601 that combination can be decided at compile time.
1603 Keep the existing form if both folds fail, to avoid endless
1605 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1607 (bitop @1 { cst1; })
1608 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1610 (bitop @0 { cst2; }))))))))
1612 /* Try simple folding for X op !X, and X op X with the help
1613 of the truth_valued_p and logical_inverted_value predicates. */
1614 (match truth_valued_p
1616 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1617 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1618 (match truth_valued_p
1620 (match truth_valued_p
1623 (match (logical_inverted_value @0)
1625 (match (logical_inverted_value @0)
1626 (bit_not truth_valued_p@0))
1627 (match (logical_inverted_value @0)
1628 (eq @0 integer_zerop))
1629 (match (logical_inverted_value @0)
1630 (ne truth_valued_p@0 integer_truep))
1631 (match (logical_inverted_value @0)
1632 (bit_xor truth_valued_p@0 integer_truep))
1636 (bit_and:c @0 (logical_inverted_value @0))
1637 { build_zero_cst (type); })
1638 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1639 (for op (bit_ior bit_xor)
1641 (op:c truth_valued_p@0 (logical_inverted_value @0))
1642 { constant_boolean_node (true, type); }))
1643 /* X ==/!= !X is false/true. */
1646 (op:c truth_valued_p@0 (logical_inverted_value @0))
1647 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1651 (bit_not (bit_not @0))
1654 /* Convert ~ (-A) to A - 1. */
1656 (bit_not (convert? (negate @0)))
1657 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1658 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1659 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1661 /* Convert - (~A) to A + 1. */
1663 (negate (nop_convert? (bit_not @0)))
1664 (plus (view_convert @0) { build_each_one_cst (type); }))
1666 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1668 (bit_not (convert? (minus @0 integer_each_onep)))
1669 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1670 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1671 (convert (negate @0))))
1673 (bit_not (convert? (plus @0 integer_all_onesp)))
1674 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1675 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1676 (convert (negate @0))))
1678 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1680 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1681 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1682 (convert (bit_xor @0 (bit_not @1)))))
1684 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1685 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1686 (convert (bit_xor @0 @1))))
1688 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1690 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1691 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1692 (bit_not (bit_xor (view_convert @0) @1))))
1694 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1696 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1697 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1699 /* Fold A - (A & B) into ~B & A. */
1701 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1702 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1703 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1704 (convert (bit_and (bit_not @1) @0))))
1706 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1707 (for cmp (gt lt ge le)
1709 (mult (convert (cmp @0 @1)) @2)
1710 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1711 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1713 /* For integral types with undefined overflow and C != 0 fold
1714 x * C EQ/NE y * C into x EQ/NE y. */
1717 (cmp (mult:c @0 @1) (mult:c @2 @1))
1718 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1719 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1720 && tree_expr_nonzero_p (@1))
1723 /* For integral types with wrapping overflow and C odd fold
1724 x * C EQ/NE y * C into x EQ/NE y. */
1727 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1728 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1729 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1730 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1733 /* For integral types with undefined overflow and C != 0 fold
1734 x * C RELOP y * C into:
1736 x RELOP y for nonnegative C
1737 y RELOP x for negative C */
1738 (for cmp (lt gt le ge)
1740 (cmp (mult:c @0 @1) (mult:c @2 @1))
1741 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1742 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1743 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1745 (if (TREE_CODE (@1) == INTEGER_CST
1746 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1749 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1753 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1754 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1755 && TYPE_UNSIGNED (TREE_TYPE (@0))
1756 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1757 && (wi::to_wide (@2)
1758 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1759 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1760 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1762 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1763 (for cmp (simple_comparison)
1765 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1766 (if (element_precision (@3) >= element_precision (@0)
1767 && types_match (@0, @1))
1768 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1769 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1771 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1774 tree utype = unsigned_type_for (TREE_TYPE (@0));
1776 (cmp (convert:utype @1) (convert:utype @0)))))
1777 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1778 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1782 tree utype = unsigned_type_for (TREE_TYPE (@0));
1784 (cmp (convert:utype @0) (convert:utype @1)))))))))
1786 /* X / C1 op C2 into a simple range test. */
1787 (for cmp (simple_comparison)
1789 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1790 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1791 && integer_nonzerop (@1)
1792 && !TREE_OVERFLOW (@1)
1793 && !TREE_OVERFLOW (@2))
1794 (with { tree lo, hi; bool neg_overflow;
1795 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1798 (if (code == LT_EXPR || code == GE_EXPR)
1799 (if (TREE_OVERFLOW (lo))
1800 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1801 (if (code == LT_EXPR)
1804 (if (code == LE_EXPR || code == GT_EXPR)
1805 (if (TREE_OVERFLOW (hi))
1806 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1807 (if (code == LE_EXPR)
1811 { build_int_cst (type, code == NE_EXPR); })
1812 (if (code == EQ_EXPR && !hi)
1814 (if (code == EQ_EXPR && !lo)
1816 (if (code == NE_EXPR && !hi)
1818 (if (code == NE_EXPR && !lo)
1821 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1825 tree etype = range_check_type (TREE_TYPE (@0));
1828 hi = fold_convert (etype, hi);
1829 lo = fold_convert (etype, lo);
1830 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1833 (if (etype && hi && !TREE_OVERFLOW (hi))
1834 (if (code == EQ_EXPR)
1835 (le (minus (convert:etype @0) { lo; }) { hi; })
1836 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1838 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1839 (for op (lt le ge gt)
1841 (op (plus:c @0 @2) (plus:c @1 @2))
1842 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1843 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1845 /* For equality and subtraction, this is also true with wrapping overflow. */
1846 (for op (eq ne minus)
1848 (op (plus:c @0 @2) (plus:c @1 @2))
1849 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1850 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1851 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1854 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1855 (for op (lt le ge gt)
1857 (op (minus @0 @2) (minus @1 @2))
1858 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1859 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1861 /* For equality and subtraction, this is also true with wrapping overflow. */
1862 (for op (eq ne minus)
1864 (op (minus @0 @2) (minus @1 @2))
1865 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1866 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1867 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1869 /* And for pointers... */
1870 (for op (simple_comparison)
1872 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1873 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1876 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1877 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1878 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1879 (pointer_diff @0 @1)))
1881 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1882 (for op (lt le ge gt)
1884 (op (minus @2 @0) (minus @2 @1))
1885 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1886 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1888 /* For equality and subtraction, this is also true with wrapping overflow. */
1889 (for op (eq ne minus)
1891 (op (minus @2 @0) (minus @2 @1))
1892 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1893 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1894 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1896 /* And for pointers... */
1897 (for op (simple_comparison)
1899 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1900 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1903 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1904 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1905 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1906 (pointer_diff @1 @0)))
1908 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1909 (for op (lt le gt ge)
1911 (op:c (plus:c@2 @0 @1) @1)
1912 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1913 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1914 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1915 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1916 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1917 /* For equality, this is also true with wrapping overflow. */
1920 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1921 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1922 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1923 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1924 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1925 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1926 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1927 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1929 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1930 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1931 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1932 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1933 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1935 /* X - Y < X is the same as Y > 0 when there is no overflow.
1936 For equality, this is also true with wrapping overflow. */
1937 (for op (simple_comparison)
1939 (op:c @0 (minus@2 @0 @1))
1940 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1941 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1942 || ((op == EQ_EXPR || op == NE_EXPR)
1943 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1944 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1945 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1948 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1949 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1953 (cmp (trunc_div @0 @1) integer_zerop)
1954 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1955 /* Complex ==/!= is allowed, but not </>=. */
1956 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1957 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1960 /* X == C - X can never be true if C is odd. */
1963 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1964 (if (TREE_INT_CST_LOW (@1) & 1)
1965 { constant_boolean_node (cmp == NE_EXPR, type); })))
1967 /* Arguments on which one can call get_nonzero_bits to get the bits
1969 (match with_possible_nonzero_bits
1971 (match with_possible_nonzero_bits
1973 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1974 /* Slightly extended version, do not make it recursive to keep it cheap. */
1975 (match (with_possible_nonzero_bits2 @0)
1976 with_possible_nonzero_bits@0)
1977 (match (with_possible_nonzero_bits2 @0)
1978 (bit_and:c with_possible_nonzero_bits@0 @2))
1980 /* Same for bits that are known to be set, but we do not have
1981 an equivalent to get_nonzero_bits yet. */
1982 (match (with_certain_nonzero_bits2 @0)
1984 (match (with_certain_nonzero_bits2 @0)
1985 (bit_ior @1 INTEGER_CST@0))
1987 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1990 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1991 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1992 { constant_boolean_node (cmp == NE_EXPR, type); })))
1994 /* ((X inner_op C0) outer_op C1)
1995 With X being a tree where value_range has reasoned certain bits to always be
1996 zero throughout its computed value range,
1997 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1998 where zero_mask has 1's for all bits that are sure to be 0 in
2000 if (inner_op == '^') C0 &= ~C1;
2001 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2002 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2004 (for inner_op (bit_ior bit_xor)
2005 outer_op (bit_xor bit_ior)
2008 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2012 wide_int zero_mask_not;
2016 if (TREE_CODE (@2) == SSA_NAME)
2017 zero_mask_not = get_nonzero_bits (@2);
2021 if (inner_op == BIT_XOR_EXPR)
2023 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2024 cst_emit = C0 | wi::to_wide (@1);
2028 C0 = wi::to_wide (@0);
2029 cst_emit = C0 ^ wi::to_wide (@1);
2032 (if (!fail && (C0 & zero_mask_not) == 0)
2033 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2034 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2035 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2037 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2039 (pointer_plus (pointer_plus:s @0 @1) @3)
2040 (pointer_plus @0 (plus @1 @3)))
2046 tem4 = (unsigned long) tem3;
2051 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2052 /* Conditionally look through a sign-changing conversion. */
2053 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2054 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2055 || (GENERIC && type == TREE_TYPE (@1))))
2058 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2059 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2063 tem = (sizetype) ptr;
2067 and produce the simpler and easier to analyze with respect to alignment
2068 ... = ptr & ~algn; */
2070 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2071 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2072 (bit_and @0 { algn; })))
2074 /* Try folding difference of addresses. */
2076 (minus (convert ADDR_EXPR@0) (convert @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 (minus (convert @0) (convert ADDR_EXPR@1))
2083 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2084 (with { poly_int64 diff; }
2085 (if (ptr_difference_const (@0, @1, &diff))
2086 { build_int_cst_type (type, diff); }))))
2088 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2089 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2090 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2091 (with { poly_int64 diff; }
2092 (if (ptr_difference_const (@0, @1, &diff))
2093 { build_int_cst_type (type, diff); }))))
2095 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2096 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2097 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2098 (with { poly_int64 diff; }
2099 (if (ptr_difference_const (@0, @1, &diff))
2100 { build_int_cst_type (type, diff); }))))
2102 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2104 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2105 (with { poly_int64 diff; }
2106 (if (ptr_difference_const (@0, @2, &diff))
2107 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2109 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2112 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2113 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2114 (if (ptr_difference_const (@0, @2, &diff))
2115 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2117 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2119 (convert (pointer_diff @0 INTEGER_CST@1))
2120 (if (POINTER_TYPE_P (type))
2121 { build_fold_addr_expr_with_type
2122 (build2 (MEM_REF, char_type_node, @0,
2123 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2126 /* If arg0 is derived from the address of an object or function, we may
2127 be able to fold this expression using the object or function's
2130 (bit_and (convert? @0) INTEGER_CST@1)
2131 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2132 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2136 unsigned HOST_WIDE_INT bitpos;
2137 get_pointer_alignment_1 (@0, &align, &bitpos);
2139 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2140 { wide_int_to_tree (type, (wi::to_wide (@1)
2141 & (bitpos / BITS_PER_UNIT))); }))))
2145 (if (INTEGRAL_TYPE_P (type)
2146 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2150 (if (INTEGRAL_TYPE_P (type)
2151 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2153 /* x > y && x != XXX_MIN --> x > y
2154 x > y && x == XXX_MIN --> false . */
2157 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2159 (if (eqne == EQ_EXPR)
2160 { constant_boolean_node (false, type); })
2161 (if (eqne == NE_EXPR)
2165 /* x < y && x != XXX_MAX --> x < y
2166 x < y && x == XXX_MAX --> false. */
2169 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2171 (if (eqne == EQ_EXPR)
2172 { constant_boolean_node (false, type); })
2173 (if (eqne == NE_EXPR)
2177 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2179 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2182 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2184 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2187 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2189 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2192 /* x <= y || x != XXX_MIN --> true. */
2194 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2195 { constant_boolean_node (true, type); })
2197 /* x <= y || x == XXX_MIN --> x <= y. */
2199 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2202 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2204 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2207 /* x >= y || x != XXX_MAX --> true
2208 x >= y || x == XXX_MAX --> x >= y. */
2211 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2213 (if (eqne == EQ_EXPR)
2215 (if (eqne == NE_EXPR)
2216 { constant_boolean_node (true, type); }))))
2218 /* y == XXX_MIN || x < y --> x <= y - 1 */
2220 (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
2221 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2222 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2223 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2225 /* y != XXX_MIN && x >= y --> x > y - 1 */
2227 (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
2228 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2229 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2230 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2232 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2233 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2236 (for code2 (eq ne lt gt le ge)
2238 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2241 int cmp = tree_int_cst_compare (@1, @2);
2245 case EQ_EXPR: val = (cmp == 0); break;
2246 case NE_EXPR: val = (cmp != 0); break;
2247 case LT_EXPR: val = (cmp < 0); break;
2248 case GT_EXPR: val = (cmp > 0); break;
2249 case LE_EXPR: val = (cmp <= 0); break;
2250 case GE_EXPR: val = (cmp >= 0); break;
2251 default: gcc_unreachable ();
2255 (if (code1 == EQ_EXPR && val) @3)
2256 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2257 (if (code1 == NE_EXPR && !val) @4))))))
2259 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2261 (for code1 (lt le gt ge)
2262 (for code2 (lt le gt ge)
2264 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2267 int cmp = tree_int_cst_compare (@1, @2);
2270 /* Choose the more restrictive of two < or <= comparisons. */
2271 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2272 && (code2 == LT_EXPR || code2 == LE_EXPR))
2273 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2276 /* Likewise chose the more restrictive of two > or >= comparisons. */
2277 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2278 && (code2 == GT_EXPR || code2 == GE_EXPR))
2279 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2282 /* Check for singleton ranges. */
2284 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2285 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2287 /* Check for disjoint ranges. */
2289 && (code1 == LT_EXPR || code1 == LE_EXPR)
2290 && (code2 == GT_EXPR || code2 == GE_EXPR))
2291 { constant_boolean_node (false, type); })
2293 && (code1 == GT_EXPR || code1 == GE_EXPR)
2294 && (code2 == LT_EXPR || code2 == LE_EXPR))
2295 { constant_boolean_node (false, type); })
2298 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2299 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2302 (for code2 (eq ne lt gt le ge)
2304 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2307 int cmp = tree_int_cst_compare (@1, @2);
2311 case EQ_EXPR: val = (cmp == 0); break;
2312 case NE_EXPR: val = (cmp != 0); break;
2313 case LT_EXPR: val = (cmp < 0); break;
2314 case GT_EXPR: val = (cmp > 0); break;
2315 case LE_EXPR: val = (cmp <= 0); break;
2316 case GE_EXPR: val = (cmp >= 0); break;
2317 default: gcc_unreachable ();
2321 (if (code1 == EQ_EXPR && val) @4)
2322 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2323 (if (code1 == NE_EXPR && !val) @3))))))
2325 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2327 (for code1 (lt le gt ge)
2328 (for code2 (lt le gt ge)
2330 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2333 int cmp = tree_int_cst_compare (@1, @2);
2336 /* Choose the more restrictive of two < or <= comparisons. */
2337 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2338 && (code2 == LT_EXPR || code2 == LE_EXPR))
2339 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2342 /* Likewise chose the more restrictive of two > or >= comparisons. */
2343 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2344 && (code2 == GT_EXPR || code2 == GE_EXPR))
2345 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2348 /* Check for singleton ranges. */
2350 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2351 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2353 /* Check for disjoint ranges. */
2355 && (code1 == LT_EXPR || code1 == LE_EXPR)
2356 && (code2 == GT_EXPR || code2 == GE_EXPR))
2357 { constant_boolean_node (true, type); })
2359 && (code1 == GT_EXPR || code1 == GE_EXPR)
2360 && (code2 == LT_EXPR || code2 == LE_EXPR))
2361 { constant_boolean_node (true, type); })
2364 /* We can't reassociate at all for saturating types. */
2365 (if (!TYPE_SATURATING (type))
2367 /* Contract negates. */
2368 /* A + (-B) -> A - B */
2370 (plus:c @0 (convert? (negate @1)))
2371 /* Apply STRIP_NOPS on the negate. */
2372 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2373 && !TYPE_OVERFLOW_SANITIZED (type))
2377 if (INTEGRAL_TYPE_P (type)
2378 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2379 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2381 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2382 /* A - (-B) -> A + B */
2384 (minus @0 (convert? (negate @1)))
2385 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2386 && !TYPE_OVERFLOW_SANITIZED (type))
2390 if (INTEGRAL_TYPE_P (type)
2391 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2392 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2394 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2396 Sign-extension is ok except for INT_MIN, which thankfully cannot
2397 happen without overflow. */
2399 (negate (convert (negate @1)))
2400 (if (INTEGRAL_TYPE_P (type)
2401 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2402 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2403 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2404 && !TYPE_OVERFLOW_SANITIZED (type)
2405 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2408 (negate (convert negate_expr_p@1))
2409 (if (SCALAR_FLOAT_TYPE_P (type)
2410 && ((DECIMAL_FLOAT_TYPE_P (type)
2411 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2412 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2413 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2414 (convert (negate @1))))
2416 (negate (nop_convert? (negate @1)))
2417 (if (!TYPE_OVERFLOW_SANITIZED (type)
2418 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2421 /* We can't reassociate floating-point unless -fassociative-math
2422 or fixed-point plus or minus because of saturation to +-Inf. */
2423 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2424 && !FIXED_POINT_TYPE_P (type))
2426 /* Match patterns that allow contracting a plus-minus pair
2427 irrespective of overflow issues. */
2428 /* (A +- B) - A -> +- B */
2429 /* (A +- B) -+ B -> A */
2430 /* A - (A +- B) -> -+ B */
2431 /* A +- (B -+ A) -> +- B */
2433 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2436 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2437 (if (!ANY_INTEGRAL_TYPE_P (type)
2438 || TYPE_OVERFLOW_WRAPS (type))
2439 (negate (view_convert @1))
2440 (view_convert (negate @1))))
2442 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2445 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2446 (if (!ANY_INTEGRAL_TYPE_P (type)
2447 || TYPE_OVERFLOW_WRAPS (type))
2448 (negate (view_convert @1))
2449 (view_convert (negate @1))))
2451 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2453 /* (A +- B) + (C - A) -> C +- B */
2454 /* (A + B) - (A - C) -> B + C */
2455 /* More cases are handled with comparisons. */
2457 (plus:c (plus:c @0 @1) (minus @2 @0))
2460 (plus:c (minus @0 @1) (minus @2 @0))
2463 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2464 (if (TYPE_OVERFLOW_UNDEFINED (type)
2465 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2466 (pointer_diff @2 @1)))
2468 (minus (plus:c @0 @1) (minus @0 @2))
2471 /* (A +- CST1) +- CST2 -> A + CST3
2472 Use view_convert because it is safe for vectors and equivalent for
2474 (for outer_op (plus minus)
2475 (for inner_op (plus minus)
2476 neg_inner_op (minus plus)
2478 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2480 /* If one of the types wraps, use that one. */
2481 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2482 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2483 forever if something doesn't simplify into a constant. */
2484 (if (!CONSTANT_CLASS_P (@0))
2485 (if (outer_op == PLUS_EXPR)
2486 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2487 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2488 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2489 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2490 (if (outer_op == PLUS_EXPR)
2491 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2492 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2493 /* If the constant operation overflows we cannot do the transform
2494 directly as we would introduce undefined overflow, for example
2495 with (a - 1) + INT_MIN. */
2496 (if (types_match (type, @0))
2497 (with { tree cst = const_binop (outer_op == inner_op
2498 ? PLUS_EXPR : MINUS_EXPR,
2500 (if (cst && !TREE_OVERFLOW (cst))
2501 (inner_op @0 { cst; } )
2502 /* X+INT_MAX+1 is X-INT_MIN. */
2503 (if (INTEGRAL_TYPE_P (type) && cst
2504 && wi::to_wide (cst) == wi::min_value (type))
2505 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2506 /* Last resort, use some unsigned type. */
2507 (with { tree utype = unsigned_type_for (type); }
2509 (view_convert (inner_op
2510 (view_convert:utype @0)
2512 { drop_tree_overflow (cst); }))))))))))))))
2514 /* (CST1 - A) +- CST2 -> CST3 - A */
2515 (for outer_op (plus minus)
2517 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2518 /* If one of the types wraps, use that one. */
2519 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2520 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2521 forever if something doesn't simplify into a constant. */
2522 (if (!CONSTANT_CLASS_P (@0))
2523 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2524 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2525 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2526 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2527 (if (types_match (type, @0))
2528 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2529 (if (cst && !TREE_OVERFLOW (cst))
2530 (minus { cst; } @0))))))))
2532 /* CST1 - (CST2 - A) -> CST3 + A
2533 Use view_convert because it is safe for vectors and equivalent for
2536 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2537 /* If one of the types wraps, use that one. */
2538 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2539 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2540 forever if something doesn't simplify into a constant. */
2541 (if (!CONSTANT_CLASS_P (@0))
2542 (plus (view_convert @0) (minus @1 (view_convert @2))))
2543 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2544 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2545 (view_convert (plus @0 (minus (view_convert @1) @2)))
2546 (if (types_match (type, @0))
2547 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2548 (if (cst && !TREE_OVERFLOW (cst))
2549 (plus { cst; } @0)))))))
2551 /* ((T)(A)) + CST -> (T)(A + CST) */
2554 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2555 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2556 && TREE_CODE (type) == INTEGER_TYPE
2557 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2558 && int_fits_type_p (@1, TREE_TYPE (@0)))
2559 /* Perform binary operation inside the cast if the constant fits
2560 and (A + CST)'s range does not overflow. */
2563 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2564 max_ovf = wi::OVF_OVERFLOW;
2565 tree inner_type = TREE_TYPE (@0);
2568 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2569 TYPE_SIGN (inner_type));
2572 if (get_global_range_query ()->range_of_expr (vr, @0)
2573 && vr.kind () == VR_RANGE)
2575 wide_int wmin0 = vr.lower_bound ();
2576 wide_int wmax0 = vr.upper_bound ();
2577 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2578 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2581 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2582 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2586 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2588 (for op (plus minus)
2590 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2591 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2592 && TREE_CODE (type) == INTEGER_TYPE
2593 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2594 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2595 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2596 && TYPE_OVERFLOW_WRAPS (type))
2597 (plus (convert @0) (op @2 (convert @1))))))
2600 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2601 to a simple value. */
2603 (for op (plus minus)
2605 (op (convert @0) (convert @1))
2606 (if (INTEGRAL_TYPE_P (type)
2607 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2608 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2609 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2610 && !TYPE_OVERFLOW_TRAPS (type)
2611 && !TYPE_OVERFLOW_SANITIZED (type))
2612 (convert (op! @0 @1)))))
2617 (plus:c (bit_not @0) @0)
2618 (if (!TYPE_OVERFLOW_TRAPS (type))
2619 { build_all_ones_cst (type); }))
2623 (plus (convert? (bit_not @0)) integer_each_onep)
2624 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2625 (negate (convert @0))))
2629 (minus (convert? (negate @0)) integer_each_onep)
2630 (if (!TYPE_OVERFLOW_TRAPS (type)
2631 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2632 (bit_not (convert @0))))
2636 (minus integer_all_onesp @0)
2639 /* (T)(P + A) - (T)P -> (T) A */
2641 (minus (convert (plus:c @@0 @1))
2643 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2644 /* For integer types, if A has a smaller type
2645 than T the result depends on the possible
2647 E.g. T=size_t, A=(unsigned)429497295, P>0.
2648 However, if an overflow in P + A would cause
2649 undefined behavior, we can assume that there
2651 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2652 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2655 (minus (convert (pointer_plus @@0 @1))
2657 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2658 /* For pointer types, if the conversion of A to the
2659 final type requires a sign- or zero-extension,
2660 then we have to punt - it is not defined which
2662 || (POINTER_TYPE_P (TREE_TYPE (@0))
2663 && TREE_CODE (@1) == INTEGER_CST
2664 && tree_int_cst_sign_bit (@1) == 0))
2667 (pointer_diff (pointer_plus @@0 @1) @0)
2668 /* The second argument of pointer_plus must be interpreted as signed, and
2669 thus sign-extended if necessary. */
2670 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2671 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2672 second arg is unsigned even when we need to consider it as signed,
2673 we don't want to diagnose overflow here. */
2674 (convert (view_convert:stype @1))))
2676 /* (T)P - (T)(P + A) -> -(T) A */
2678 (minus (convert? @0)
2679 (convert (plus:c @@0 @1)))
2680 (if (INTEGRAL_TYPE_P (type)
2681 && TYPE_OVERFLOW_UNDEFINED (type)
2682 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2683 (with { tree utype = unsigned_type_for (type); }
2684 (convert (negate (convert:utype @1))))
2685 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2686 /* For integer types, if A has a smaller type
2687 than T the result depends on the possible
2689 E.g. T=size_t, A=(unsigned)429497295, P>0.
2690 However, if an overflow in P + A would cause
2691 undefined behavior, we can assume that there
2693 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2694 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2695 (negate (convert @1)))))
2698 (convert (pointer_plus @@0 @1)))
2699 (if (INTEGRAL_TYPE_P (type)
2700 && TYPE_OVERFLOW_UNDEFINED (type)
2701 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2702 (with { tree utype = unsigned_type_for (type); }
2703 (convert (negate (convert:utype @1))))
2704 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2705 /* For pointer types, if the conversion of A to the
2706 final type requires a sign- or zero-extension,
2707 then we have to punt - it is not defined which
2709 || (POINTER_TYPE_P (TREE_TYPE (@0))
2710 && TREE_CODE (@1) == INTEGER_CST
2711 && tree_int_cst_sign_bit (@1) == 0))
2712 (negate (convert @1)))))
2714 (pointer_diff @0 (pointer_plus @@0 @1))
2715 /* The second argument of pointer_plus must be interpreted as signed, and
2716 thus sign-extended if necessary. */
2717 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2718 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2719 second arg is unsigned even when we need to consider it as signed,
2720 we don't want to diagnose overflow here. */
2721 (negate (convert (view_convert:stype @1)))))
2723 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2725 (minus (convert (plus:c @@0 @1))
2726 (convert (plus:c @0 @2)))
2727 (if (INTEGRAL_TYPE_P (type)
2728 && TYPE_OVERFLOW_UNDEFINED (type)
2729 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2730 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2731 (with { tree utype = unsigned_type_for (type); }
2732 (convert (minus (convert:utype @1) (convert:utype @2))))
2733 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2734 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2735 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2736 /* For integer types, if A has a smaller type
2737 than T the result depends on the possible
2739 E.g. T=size_t, A=(unsigned)429497295, P>0.
2740 However, if an overflow in P + A would cause
2741 undefined behavior, we can assume that there
2743 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2744 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2745 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2746 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2747 (minus (convert @1) (convert @2)))))
2749 (minus (convert (pointer_plus @@0 @1))
2750 (convert (pointer_plus @0 @2)))
2751 (if (INTEGRAL_TYPE_P (type)
2752 && TYPE_OVERFLOW_UNDEFINED (type)
2753 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2754 (with { tree utype = unsigned_type_for (type); }
2755 (convert (minus (convert:utype @1) (convert:utype @2))))
2756 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2757 /* For pointer types, if the conversion of A to the
2758 final type requires a sign- or zero-extension,
2759 then we have to punt - it is not defined which
2761 || (POINTER_TYPE_P (TREE_TYPE (@0))
2762 && TREE_CODE (@1) == INTEGER_CST
2763 && tree_int_cst_sign_bit (@1) == 0
2764 && TREE_CODE (@2) == INTEGER_CST
2765 && tree_int_cst_sign_bit (@2) == 0))
2766 (minus (convert @1) (convert @2)))))
2768 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2769 (pointer_diff @0 @1))
2771 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2772 /* The second argument of pointer_plus must be interpreted as signed, and
2773 thus sign-extended if necessary. */
2774 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2775 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2776 second arg is unsigned even when we need to consider it as signed,
2777 we don't want to diagnose overflow here. */
2778 (minus (convert (view_convert:stype @1))
2779 (convert (view_convert:stype @2)))))))
2781 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2782 Modeled after fold_plusminus_mult_expr. */
2783 (if (!TYPE_SATURATING (type)
2784 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2785 (for plusminus (plus minus)
2787 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2788 (if (!ANY_INTEGRAL_TYPE_P (type)
2789 || TYPE_OVERFLOW_WRAPS (type)
2790 || (INTEGRAL_TYPE_P (type)
2791 && tree_expr_nonzero_p (@0)
2792 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2793 (if (single_use (@3) || single_use (@4))
2794 /* If @1 +- @2 is constant require a hard single-use on either
2795 original operand (but not on both). */
2796 (mult (plusminus @1 @2) @0)
2798 (mult! (plusminus @1 @2) @0)
2801 /* We cannot generate constant 1 for fract. */
2802 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2804 (plusminus @0 (mult:c@3 @0 @2))
2805 (if ((!ANY_INTEGRAL_TYPE_P (type)
2806 || TYPE_OVERFLOW_WRAPS (type)
2807 /* For @0 + @0*@2 this transformation would introduce UB
2808 (where there was none before) for @0 in [-1,0] and @2 max.
2809 For @0 - @0*@2 this transformation would introduce UB
2810 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2811 || (INTEGRAL_TYPE_P (type)
2812 && ((tree_expr_nonzero_p (@0)
2813 && expr_not_equal_to (@0,
2814 wi::minus_one (TYPE_PRECISION (type))))
2815 || (plusminus == PLUS_EXPR
2816 ? expr_not_equal_to (@2,
2817 wi::max_value (TYPE_PRECISION (type), SIGNED))
2818 /* Let's ignore the @0 -1 and @2 min case. */
2819 : (expr_not_equal_to (@2,
2820 wi::min_value (TYPE_PRECISION (type), SIGNED))
2821 && expr_not_equal_to (@2,
2822 wi::min_value (TYPE_PRECISION (type), SIGNED)
2825 (mult (plusminus { build_one_cst (type); } @2) @0)))
2827 (plusminus (mult:c@3 @0 @2) @0)
2828 (if ((!ANY_INTEGRAL_TYPE_P (type)
2829 || TYPE_OVERFLOW_WRAPS (type)
2830 /* For @0*@2 + @0 this transformation would introduce UB
2831 (where there was none before) for @0 in [-1,0] and @2 max.
2832 For @0*@2 - @0 this transformation would introduce UB
2833 for @0 0 and @2 min. */
2834 || (INTEGRAL_TYPE_P (type)
2835 && ((tree_expr_nonzero_p (@0)
2836 && (plusminus == MINUS_EXPR
2837 || expr_not_equal_to (@0,
2838 wi::minus_one (TYPE_PRECISION (type)))))
2839 || expr_not_equal_to (@2,
2840 (plusminus == PLUS_EXPR
2841 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2842 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2844 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2847 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2848 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2850 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2851 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2852 && tree_fits_uhwi_p (@1)
2853 && tree_to_uhwi (@1) < element_precision (type)
2854 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2855 || optab_handler (smul_optab,
2856 TYPE_MODE (type)) != CODE_FOR_nothing))
2857 (with { tree t = type;
2858 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2859 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2860 element_precision (type));
2862 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2864 cst = build_uniform_cst (t, cst); }
2865 (convert (mult (convert:t @0) { cst; })))))
2867 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2868 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2869 && tree_fits_uhwi_p (@1)
2870 && tree_to_uhwi (@1) < element_precision (type)
2871 && tree_fits_uhwi_p (@2)
2872 && tree_to_uhwi (@2) < element_precision (type)
2873 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2874 || optab_handler (smul_optab,
2875 TYPE_MODE (type)) != CODE_FOR_nothing))
2876 (with { tree t = type;
2877 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2878 unsigned int prec = element_precision (type);
2879 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2880 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2881 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2883 cst = build_uniform_cst (t, cst); }
2884 (convert (mult (convert:t @0) { cst; })))))
2887 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
2888 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
2889 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
2890 (for op (bit_ior bit_xor)
2892 (op (mult:s@0 @1 INTEGER_CST@2)
2893 (mult:s@3 @1 INTEGER_CST@4))
2894 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2895 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2897 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
2899 (op:c (mult:s@0 @1 INTEGER_CST@2)
2900 (lshift:s@3 @1 INTEGER_CST@4))
2901 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2902 && tree_int_cst_sgn (@4) > 0
2903 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2904 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
2905 wide_int c = wi::add (wi::to_wide (@2),
2906 wi::lshift (wone, wi::to_wide (@4))); }
2907 (mult @1 { wide_int_to_tree (type, c); }))))
2909 (op:c (mult:s@0 @1 INTEGER_CST@2)
2911 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2912 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
2914 { wide_int_to_tree (type,
2915 wi::add (wi::to_wide (@2), 1)); })))
2917 (op (lshift:s@0 @1 INTEGER_CST@2)
2918 (lshift:s@3 @1 INTEGER_CST@4))
2919 (if (INTEGRAL_TYPE_P (type)
2920 && tree_int_cst_sgn (@2) > 0
2921 && tree_int_cst_sgn (@4) > 0
2922 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2923 (with { tree t = type;
2924 if (!TYPE_OVERFLOW_WRAPS (t))
2925 t = unsigned_type_for (t);
2926 wide_int wone = wi::one (TYPE_PRECISION (t));
2927 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
2928 wi::lshift (wone, wi::to_wide (@4))); }
2929 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
2931 (op:c (lshift:s@0 @1 INTEGER_CST@2)
2933 (if (INTEGRAL_TYPE_P (type)
2934 && tree_int_cst_sgn (@2) > 0
2935 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
2936 (with { tree t = type;
2937 if (!TYPE_OVERFLOW_WRAPS (t))
2938 t = unsigned_type_for (t);
2939 wide_int wone = wi::one (TYPE_PRECISION (t));
2940 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
2941 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
2943 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2945 (for minmax (min max FMIN_ALL FMAX_ALL)
2949 /* min(max(x,y),y) -> y. */
2951 (min:c (max:c @0 @1) @1)
2953 /* max(min(x,y),y) -> y. */
2955 (max:c (min:c @0 @1) @1)
2957 /* max(a,-a) -> abs(a). */
2959 (max:c @0 (negate @0))
2960 (if (TREE_CODE (type) != COMPLEX_TYPE
2961 && (! ANY_INTEGRAL_TYPE_P (type)
2962 || TYPE_OVERFLOW_UNDEFINED (type)))
2964 /* min(a,-a) -> -abs(a). */
2966 (min:c @0 (negate @0))
2967 (if (TREE_CODE (type) != COMPLEX_TYPE
2968 && (! ANY_INTEGRAL_TYPE_P (type)
2969 || TYPE_OVERFLOW_UNDEFINED (type)))
2974 (if (INTEGRAL_TYPE_P (type)
2975 && TYPE_MIN_VALUE (type)
2976 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2978 (if (INTEGRAL_TYPE_P (type)
2979 && TYPE_MAX_VALUE (type)
2980 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2985 (if (INTEGRAL_TYPE_P (type)
2986 && TYPE_MAX_VALUE (type)
2987 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2989 (if (INTEGRAL_TYPE_P (type)
2990 && TYPE_MIN_VALUE (type)
2991 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2994 /* max (a, a + CST) -> a + CST where CST is positive. */
2995 /* max (a, a + CST) -> a where CST is negative. */
2997 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2998 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2999 (if (tree_int_cst_sgn (@1) > 0)
3003 /* min (a, a + CST) -> a where CST is positive. */
3004 /* min (a, a + CST) -> a + CST where CST is negative. */
3006 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3007 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3008 (if (tree_int_cst_sgn (@1) > 0)
3012 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3013 the addresses are known to be less, equal or greater. */
3014 (for minmax (min max)
3017 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3020 poly_int64 off0, off1;
3022 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3023 off0, off1, GENERIC);
3026 (if (minmax == MIN_EXPR)
3027 (if (known_le (off0, off1))
3029 (if (known_gt (off0, off1))
3031 (if (known_ge (off0, off1))
3033 (if (known_lt (off0, off1))
3036 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3037 and the outer convert demotes the expression back to x's type. */
3038 (for minmax (min max)
3040 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3041 (if (INTEGRAL_TYPE_P (type)
3042 && types_match (@1, type) && int_fits_type_p (@2, type)
3043 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3044 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3045 (minmax @1 (convert @2)))))
3047 (for minmax (FMIN_ALL FMAX_ALL)
3048 /* If either argument is NaN, return the other one. Avoid the
3049 transformation if we get (and honor) a signalling NaN. */
3051 (minmax:c @0 REAL_CST@1)
3052 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3053 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
3055 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3056 functions to return the numeric arg if the other one is NaN.
3057 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3058 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3059 worry about it either. */
3060 (if (flag_finite_math_only)
3067 /* min (-A, -B) -> -max (A, B) */
3068 (for minmax (min max FMIN_ALL FMAX_ALL)
3069 maxmin (max min FMAX_ALL FMIN_ALL)
3071 (minmax (negate:s@2 @0) (negate:s@3 @1))
3072 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3073 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3074 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3075 (negate (maxmin @0 @1)))))
3076 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3077 MAX (~X, ~Y) -> ~MIN (X, Y) */
3078 (for minmax (min max)
3081 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3082 (bit_not (maxmin @0 @1))))
3084 /* MIN (X, Y) == X -> X <= Y */
3085 (for minmax (min min max max)
3089 (cmp:c (minmax:c @0 @1) @0)
3090 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3092 /* MIN (X, 5) == 0 -> X == 0
3093 MIN (X, 5) == 7 -> false */
3096 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3097 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3098 TYPE_SIGN (TREE_TYPE (@0))))
3099 { constant_boolean_node (cmp == NE_EXPR, type); }
3100 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3101 TYPE_SIGN (TREE_TYPE (@0))))
3105 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3106 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3107 TYPE_SIGN (TREE_TYPE (@0))))
3108 { constant_boolean_node (cmp == NE_EXPR, type); }
3109 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3110 TYPE_SIGN (TREE_TYPE (@0))))
3112 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3113 (for minmax (min min max max min min max max )
3114 cmp (lt le gt ge gt ge lt le )
3115 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3117 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3118 (comb (cmp @0 @2) (cmp @1 @2))))
3120 /* X <= MAX(X, Y) -> true
3121 X > MAX(X, Y) -> false
3122 X >= MIN(X, Y) -> true
3123 X < MIN(X, Y) -> false */
3124 (for minmax (min min max max )
3127 (cmp @0 (minmax:c @0 @1))
3128 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3130 /* Undo fancy way of writing max/min or other ?: expressions,
3131 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
3132 People normally use ?: and that is what we actually try to optimize. */
3133 (for cmp (simple_comparison)
3135 (minus @0 (bit_and:c (minus @0 @1)
3136 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3137 (if (INTEGRAL_TYPE_P (type)
3138 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3139 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3140 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3141 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3142 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3143 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3144 (cond (cmp @2 @3) @1 @0)))
3146 (plus:c @0 (bit_and:c (minus @1 @0)
3147 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3148 (if (INTEGRAL_TYPE_P (type)
3149 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3150 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3151 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3152 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3153 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3154 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3155 (cond (cmp @2 @3) @1 @0)))
3156 /* Similarly with ^ instead of - though in that case with :c. */
3158 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3159 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3160 (if (INTEGRAL_TYPE_P (type)
3161 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3162 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3163 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3164 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3165 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3166 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3167 (cond (cmp @2 @3) @1 @0))))
3169 /* Simplifications of shift and rotates. */
3171 (for rotate (lrotate rrotate)
3173 (rotate integer_all_onesp@0 @1)
3176 /* Optimize -1 >> x for arithmetic right shifts. */
3178 (rshift integer_all_onesp@0 @1)
3179 (if (!TYPE_UNSIGNED (type))
3182 /* Optimize (x >> c) << c into x & (-1<<c). */
3184 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3185 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3186 /* It doesn't matter if the right shift is arithmetic or logical. */
3187 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3190 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3191 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3192 /* Allow intermediate conversion to integral type with whatever sign, as
3193 long as the low TYPE_PRECISION (type)
3194 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3195 && INTEGRAL_TYPE_P (type)
3196 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3197 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3198 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3199 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3200 || wi::geu_p (wi::to_wide (@1),
3201 TYPE_PRECISION (type)
3202 - TYPE_PRECISION (TREE_TYPE (@2)))))
3203 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3205 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3208 (rshift (lshift @0 INTEGER_CST@1) @1)
3209 (if (TYPE_UNSIGNED (type)
3210 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3211 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3213 /* Optimize x >> x into 0 */
3216 { build_zero_cst (type); })
3218 (for shiftrotate (lrotate rrotate lshift rshift)
3220 (shiftrotate @0 integer_zerop)
3223 (shiftrotate integer_zerop@0 @1)
3225 /* Prefer vector1 << scalar to vector1 << vector2
3226 if vector2 is uniform. */
3227 (for vec (VECTOR_CST CONSTRUCTOR)
3229 (shiftrotate @0 vec@1)
3230 (with { tree tem = uniform_vector_p (@1); }
3232 (shiftrotate @0 { tem; }))))))
3234 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3235 Y is 0. Similarly for X >> Y. */
3237 (for shift (lshift rshift)
3239 (shift @0 SSA_NAME@1)
3240 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3242 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3243 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3245 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3249 /* Rewrite an LROTATE_EXPR by a constant into an
3250 RROTATE_EXPR by a new constant. */
3252 (lrotate @0 INTEGER_CST@1)
3253 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3254 build_int_cst (TREE_TYPE (@1),
3255 element_precision (type)), @1); }))
3257 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3258 (for op (lrotate rrotate rshift lshift)
3260 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3261 (with { unsigned int prec = element_precision (type); }
3262 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3263 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3264 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3265 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3266 (with { unsigned int low = (tree_to_uhwi (@1)
3267 + tree_to_uhwi (@2)); }
3268 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3269 being well defined. */
3271 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3272 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3273 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3274 { build_zero_cst (type); }
3275 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3276 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3279 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3281 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3282 (if ((wi::to_wide (@1) & 1) != 0)
3283 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3284 { build_zero_cst (type); }))
3286 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3287 either to false if D is smaller (unsigned comparison) than C, or to
3288 x == log2 (D) - log2 (C). Similarly for right shifts. */
3292 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3293 (with { int c1 = wi::clz (wi::to_wide (@1));
3294 int c2 = wi::clz (wi::to_wide (@2)); }
3296 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3297 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3299 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3300 (if (tree_int_cst_sgn (@1) > 0)
3301 (with { int c1 = wi::clz (wi::to_wide (@1));
3302 int c2 = wi::clz (wi::to_wide (@2)); }
3304 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3305 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3307 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3308 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3312 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3313 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3315 || (!integer_zerop (@2)
3316 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3317 { constant_boolean_node (cmp == NE_EXPR, type); }
3318 (if (!integer_zerop (@2)
3319 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3320 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3322 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3323 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3324 if the new mask might be further optimized. */
3325 (for shift (lshift rshift)
3327 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3329 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3330 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3331 && tree_fits_uhwi_p (@1)
3332 && tree_to_uhwi (@1) > 0
3333 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3336 unsigned int shiftc = tree_to_uhwi (@1);
3337 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3338 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3339 tree shift_type = TREE_TYPE (@3);
3342 if (shift == LSHIFT_EXPR)
3343 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3344 else if (shift == RSHIFT_EXPR
3345 && type_has_mode_precision_p (shift_type))
3347 prec = TYPE_PRECISION (TREE_TYPE (@3));
3349 /* See if more bits can be proven as zero because of
3352 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3354 tree inner_type = TREE_TYPE (@0);
3355 if (type_has_mode_precision_p (inner_type)
3356 && TYPE_PRECISION (inner_type) < prec)
3358 prec = TYPE_PRECISION (inner_type);
3359 /* See if we can shorten the right shift. */
3361 shift_type = inner_type;
3362 /* Otherwise X >> C1 is all zeros, so we'll optimize
3363 it into (X, 0) later on by making sure zerobits
3367 zerobits = HOST_WIDE_INT_M1U;
3370 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3371 zerobits <<= prec - shiftc;
3373 /* For arithmetic shift if sign bit could be set, zerobits
3374 can contain actually sign bits, so no transformation is
3375 possible, unless MASK masks them all away. In that
3376 case the shift needs to be converted into logical shift. */
3377 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3378 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3380 if ((mask & zerobits) == 0)
3381 shift_type = unsigned_type_for (TREE_TYPE (@3));
3387 /* ((X << 16) & 0xff00) is (X, 0). */
3388 (if ((mask & zerobits) == mask)
3389 { build_int_cst (type, 0); }
3390 (with { newmask = mask | zerobits; }
3391 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3394 /* Only do the transformation if NEWMASK is some integer
3396 for (prec = BITS_PER_UNIT;
3397 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3398 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3401 (if (prec < HOST_BITS_PER_WIDE_INT
3402 || newmask == HOST_WIDE_INT_M1U)
3404 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3405 (if (!tree_int_cst_equal (newmaskt, @2))
3406 (if (shift_type != TREE_TYPE (@3))
3407 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3408 (bit_and @4 { newmaskt; })))))))))))))
3410 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3411 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3412 (for shift (lshift rshift)
3413 (for bit_op (bit_and bit_xor bit_ior)
3415 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3416 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3417 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3419 (bit_op (shift (convert @0) @1) { mask; })))))))
3421 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3423 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3424 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3425 && (element_precision (TREE_TYPE (@0))
3426 <= element_precision (TREE_TYPE (@1))
3427 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3429 { tree shift_type = TREE_TYPE (@0); }
3430 (convert (rshift (convert:shift_type @1) @2)))))
3432 /* ~(~X >>r Y) -> X >>r Y
3433 ~(~X <<r Y) -> X <<r Y */
3434 (for rotate (lrotate rrotate)
3436 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3437 (if ((element_precision (TREE_TYPE (@0))
3438 <= element_precision (TREE_TYPE (@1))
3439 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3440 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3441 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3443 { tree rotate_type = TREE_TYPE (@0); }
3444 (convert (rotate (convert:rotate_type @1) @2))))))
3447 (for rotate (lrotate rrotate)
3448 invrot (rrotate lrotate)
3449 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3451 (cmp (rotate @1 @0) (rotate @2 @0))
3453 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3455 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3456 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3457 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3459 (cmp (rotate @0 @1) INTEGER_CST@2)
3460 (if (integer_zerop (@2) || integer_all_onesp (@2))
3463 /* Both signed and unsigned lshift produce the same result, so use
3464 the form that minimizes the number of conversions. Postpone this
3465 transformation until after shifts by zero have been folded. */
3467 (convert (lshift:s@0 (convert:s@1 @2) INTEGER_CST@3))
3468 (if (INTEGRAL_TYPE_P (type)
3469 && tree_nop_conversion_p (type, TREE_TYPE (@0))
3470 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3471 && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
3472 && !integer_zerop (@3))
3473 (lshift (convert @2) @3)))
3475 /* Simplifications of conversions. */
3477 /* Basic strip-useless-type-conversions / strip_nops. */
3478 (for cvt (convert view_convert float fix_trunc)
3481 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3482 || (GENERIC && type == TREE_TYPE (@0)))
3485 /* Contract view-conversions. */
3487 (view_convert (view_convert @0))
3490 /* For integral conversions with the same precision or pointer
3491 conversions use a NOP_EXPR instead. */
3494 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3495 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3496 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3499 /* Strip inner integral conversions that do not change precision or size, or
3500 zero-extend while keeping the same size (for bool-to-char). */
3502 (view_convert (convert@0 @1))
3503 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3504 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3505 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3506 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3507 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3508 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3511 /* Simplify a view-converted empty constructor. */
3513 (view_convert CONSTRUCTOR@0)
3514 (if (TREE_CODE (@0) != SSA_NAME
3515 && CONSTRUCTOR_NELTS (@0) == 0)
3516 { build_zero_cst (type); }))
3518 /* Re-association barriers around constants and other re-association
3519 barriers can be removed. */
3521 (paren CONSTANT_CLASS_P@0)
3524 (paren (paren@1 @0))
3527 /* Handle cases of two conversions in a row. */
3528 (for ocvt (convert float fix_trunc)
3529 (for icvt (convert float)
3534 tree inside_type = TREE_TYPE (@0);
3535 tree inter_type = TREE_TYPE (@1);
3536 int inside_int = INTEGRAL_TYPE_P (inside_type);
3537 int inside_ptr = POINTER_TYPE_P (inside_type);
3538 int inside_float = FLOAT_TYPE_P (inside_type);
3539 int inside_vec = VECTOR_TYPE_P (inside_type);
3540 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3541 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3542 int inter_int = INTEGRAL_TYPE_P (inter_type);
3543 int inter_ptr = POINTER_TYPE_P (inter_type);
3544 int inter_float = FLOAT_TYPE_P (inter_type);
3545 int inter_vec = VECTOR_TYPE_P (inter_type);
3546 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3547 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3548 int final_int = INTEGRAL_TYPE_P (type);
3549 int final_ptr = POINTER_TYPE_P (type);
3550 int final_float = FLOAT_TYPE_P (type);
3551 int final_vec = VECTOR_TYPE_P (type);
3552 unsigned int final_prec = TYPE_PRECISION (type);
3553 int final_unsignedp = TYPE_UNSIGNED (type);
3556 /* In addition to the cases of two conversions in a row
3557 handled below, if we are converting something to its own
3558 type via an object of identical or wider precision, neither
3559 conversion is needed. */
3560 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3562 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3563 && (((inter_int || inter_ptr) && final_int)
3564 || (inter_float && final_float))
3565 && inter_prec >= final_prec)
3568 /* Likewise, if the intermediate and initial types are either both
3569 float or both integer, we don't need the middle conversion if the
3570 former is wider than the latter and doesn't change the signedness
3571 (for integers). Avoid this if the final type is a pointer since
3572 then we sometimes need the middle conversion. */
3573 (if (((inter_int && inside_int) || (inter_float && inside_float))
3574 && (final_int || final_float)
3575 && inter_prec >= inside_prec
3576 && (inter_float || inter_unsignedp == inside_unsignedp))
3579 /* If we have a sign-extension of a zero-extended value, we can
3580 replace that by a single zero-extension. Likewise if the
3581 final conversion does not change precision we can drop the
3582 intermediate conversion. */
3583 (if (inside_int && inter_int && final_int
3584 && ((inside_prec < inter_prec && inter_prec < final_prec
3585 && inside_unsignedp && !inter_unsignedp)
3586 || final_prec == inter_prec))
3589 /* Two conversions in a row are not needed unless:
3590 - some conversion is floating-point (overstrict for now), or
3591 - some conversion is a vector (overstrict for now), or
3592 - the intermediate type is narrower than both initial and
3594 - the intermediate type and innermost type differ in signedness,
3595 and the outermost type is wider than the intermediate, or
3596 - the initial type is a pointer type and the precisions of the
3597 intermediate and final types differ, or
3598 - the final type is a pointer type and the precisions of the
3599 initial and intermediate types differ. */
3600 (if (! inside_float && ! inter_float && ! final_float
3601 && ! inside_vec && ! inter_vec && ! final_vec
3602 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3603 && ! (inside_int && inter_int
3604 && inter_unsignedp != inside_unsignedp
3605 && inter_prec < final_prec)
3606 && ((inter_unsignedp && inter_prec > inside_prec)
3607 == (final_unsignedp && final_prec > inter_prec))
3608 && ! (inside_ptr && inter_prec != final_prec)
3609 && ! (final_ptr && inside_prec != inter_prec))
3612 /* A truncation to an unsigned type (a zero-extension) should be
3613 canonicalized as bitwise and of a mask. */
3614 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3615 && final_int && inter_int && inside_int
3616 && final_prec == inside_prec
3617 && final_prec > inter_prec
3619 (convert (bit_and @0 { wide_int_to_tree
3621 wi::mask (inter_prec, false,
3622 TYPE_PRECISION (inside_type))); })))
3624 /* If we are converting an integer to a floating-point that can
3625 represent it exactly and back to an integer, we can skip the
3626 floating-point conversion. */
3627 (if (GIMPLE /* PR66211 */
3628 && inside_int && inter_float && final_int &&
3629 (unsigned) significand_size (TYPE_MODE (inter_type))
3630 >= inside_prec - !inside_unsignedp)
3633 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
3634 float_type. Only do the transformation if we do not need to preserve
3635 trapping behaviour, so require !flag_trapping_math. */
3638 (float (fix_trunc @0))
3639 (if (!flag_trapping_math
3640 && types_match (type, TREE_TYPE (@0))
3641 && direct_internal_fn_supported_p (IFN_TRUNC, type,
3646 /* If we have a narrowing conversion to an integral type that is fed by a
3647 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3648 masks off bits outside the final type (and nothing else). */
3650 (convert (bit_and @0 INTEGER_CST@1))
3651 (if (INTEGRAL_TYPE_P (type)
3652 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3653 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3654 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3655 TYPE_PRECISION (type)), 0))
3659 /* (X /[ex] A) * A -> X. */
3661 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3664 /* Simplify (A / B) * B + (A % B) -> A. */
3665 (for div (trunc_div ceil_div floor_div round_div)
3666 mod (trunc_mod ceil_mod floor_mod round_mod)
3668 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3671 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3672 (for op (plus minus)
3674 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3675 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3676 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3679 wi::overflow_type overflow;
3680 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3681 TYPE_SIGN (type), &overflow);
3683 (if (types_match (type, TREE_TYPE (@2))
3684 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3685 (op @0 { wide_int_to_tree (type, mul); })
3686 (with { tree utype = unsigned_type_for (type); }
3687 (convert (op (convert:utype @0)
3688 (mult (convert:utype @1) (convert:utype @2))))))))))
3690 /* Canonicalization of binary operations. */
3692 /* Convert X + -C into X - C. */
3694 (plus @0 REAL_CST@1)
3695 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3696 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3697 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3698 (minus @0 { tem; })))))
3700 /* Convert x+x into x*2. */
3703 (if (SCALAR_FLOAT_TYPE_P (type))
3704 (mult @0 { build_real (type, dconst2); })
3705 (if (INTEGRAL_TYPE_P (type))
3706 (mult @0 { build_int_cst (type, 2); }))))
3710 (minus integer_zerop @1)
3713 (pointer_diff integer_zerop @1)
3714 (negate (convert @1)))
3716 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3717 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3718 (-ARG1 + ARG0) reduces to -ARG1. */
3720 (minus real_zerop@0 @1)
3721 (if (fold_real_zero_addition_p (type, @1, @0, 0))
3724 /* Transform x * -1 into -x. */
3726 (mult @0 integer_minus_onep)
3729 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3730 signed overflow for CST != 0 && CST != -1. */
3732 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3733 (if (TREE_CODE (@2) != INTEGER_CST
3735 && !integer_zerop (@1) && !integer_minus_onep (@1))
3736 (mult (mult @0 @2) @1)))
3738 /* True if we can easily extract the real and imaginary parts of a complex
3740 (match compositional_complex
3741 (convert? (complex @0 @1)))
3743 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3745 (complex (realpart @0) (imagpart @0))
3748 (realpart (complex @0 @1))
3751 (imagpart (complex @0 @1))
3754 /* Sometimes we only care about half of a complex expression. */
3756 (realpart (convert?:s (conj:s @0)))
3757 (convert (realpart @0)))
3759 (imagpart (convert?:s (conj:s @0)))
3760 (convert (negate (imagpart @0))))
3761 (for part (realpart imagpart)
3762 (for op (plus minus)
3764 (part (convert?:s@2 (op:s @0 @1)))
3765 (convert (op (part @0) (part @1))))))
3767 (realpart (convert?:s (CEXPI:s @0)))
3770 (imagpart (convert?:s (CEXPI:s @0)))
3773 /* conj(conj(x)) -> x */
3775 (conj (convert? (conj @0)))
3776 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3779 /* conj({x,y}) -> {x,-y} */
3781 (conj (convert?:s (complex:s @0 @1)))
3782 (with { tree itype = TREE_TYPE (type); }
3783 (complex (convert:itype @0) (negate (convert:itype @1)))))
3785 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3786 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
3787 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
3792 (bswap (bit_not (bswap @0)))
3794 (for bitop (bit_xor bit_ior bit_and)
3796 (bswap (bitop:c (bswap @0) @1))
3797 (bitop @0 (bswap @1))))
3800 (cmp (bswap@2 @0) (bswap @1))
3801 (with { tree ctype = TREE_TYPE (@2); }
3802 (cmp (convert:ctype @0) (convert:ctype @1))))
3804 (cmp (bswap @0) INTEGER_CST@1)
3805 (with { tree ctype = TREE_TYPE (@1); }
3806 (cmp (convert:ctype @0) (bswap @1)))))
3807 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
3809 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
3811 (if (BITS_PER_UNIT == 8
3812 && tree_fits_uhwi_p (@2)
3813 && tree_fits_uhwi_p (@3))
3816 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
3817 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
3818 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
3819 unsigned HOST_WIDE_INT lo = bits & 7;
3820 unsigned HOST_WIDE_INT hi = bits - lo;
3823 && mask < (256u>>lo)
3824 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
3825 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
3827 (bit_and (convert @1) @3)
3830 tree utype = unsigned_type_for (TREE_TYPE (@1));
3831 tree nst = build_int_cst (integer_type_node, ns);
3833 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
3834 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
3836 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
3837 (if (BITS_PER_UNIT == 8
3838 && CHAR_TYPE_SIZE == 8
3839 && tree_fits_uhwi_p (@1))
3842 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3843 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
3844 /* If the bswap was extended before the original shift, this
3845 byte (shift) has the sign of the extension, not the sign of
3846 the original shift. */
3847 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
3849 /* Special case: logical right shift of sign-extended bswap.
3850 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
3851 (if (TYPE_PRECISION (type) > prec
3852 && !TYPE_UNSIGNED (TREE_TYPE (@2))
3853 && TYPE_UNSIGNED (type)
3854 && bits < prec && bits + 8 >= prec)
3855 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
3856 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
3857 (if (bits + 8 == prec)
3858 (if (TYPE_UNSIGNED (st))
3859 (convert (convert:unsigned_char_type_node @0))
3860 (convert (convert:signed_char_type_node @0)))
3861 (if (bits < prec && bits + 8 > prec)
3864 tree nst = build_int_cst (integer_type_node, bits & 7);
3865 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
3866 : signed_char_type_node;
3868 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
3869 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
3871 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
3872 (if (BITS_PER_UNIT == 8
3873 && tree_fits_uhwi_p (@1)
3874 && tree_to_uhwi (@1) < 256)
3877 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3878 tree utype = unsigned_type_for (TREE_TYPE (@0));
3879 tree nst = build_int_cst (integer_type_node, prec - 8);
3881 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
3884 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3886 /* Simplify constant conditions.
3887 Only optimize constant conditions when the selected branch
3888 has the same type as the COND_EXPR. This avoids optimizing
3889 away "c ? x : throw", where the throw has a void type.
3890 Note that we cannot throw away the fold-const.c variant nor
3891 this one as we depend on doing this transform before possibly
3892 A ? B : B -> B triggers and the fold-const.c one can optimize
3893 0 ? A : B to B even if A has side-effects. Something
3894 genmatch cannot handle. */
3896 (cond INTEGER_CST@0 @1 @2)
3897 (if (integer_zerop (@0))
3898 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3900 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3903 (vec_cond VECTOR_CST@0 @1 @2)
3904 (if (integer_all_onesp (@0))
3906 (if (integer_zerop (@0))
3910 /* Sink unary operations to branches, but only if we do fold both. */
3911 (for op (negate bit_not abs absu)
3913 (op (vec_cond:s @0 @1 @2))
3914 (vec_cond @0 (op! @1) (op! @2))))
3916 /* Sink binary operation to branches, but only if we can fold it. */
3917 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3918 lshift rshift rdiv trunc_div ceil_div floor_div round_div
3919 trunc_mod ceil_mod floor_mod round_mod min max)
3920 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3922 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3923 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3925 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3927 (op (vec_cond:s @0 @1 @2) @3)
3928 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3930 (op @3 (vec_cond:s @0 @1 @2))
3931 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3934 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3935 Currently disabled after pass lvec because ARM understands
3936 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3938 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3939 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3940 (vec_cond (bit_and @0 @3) @1 @2)))
3942 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3943 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3944 (vec_cond (bit_ior @0 @3) @1 @2)))
3946 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3947 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3948 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3950 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3951 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3952 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3954 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3956 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3957 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3958 (vec_cond (bit_and @0 @1) @2 @3)))
3960 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3961 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3962 (vec_cond (bit_ior @0 @1) @2 @3)))
3964 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3965 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3966 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3968 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3969 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3970 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3972 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3973 types are compatible. */
3975 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3976 (if (VECTOR_BOOLEAN_TYPE_P (type)
3977 && types_match (type, TREE_TYPE (@0)))
3978 (if (integer_zerop (@1) && integer_all_onesp (@2))
3980 (if (integer_all_onesp (@1) && integer_zerop (@2))
3983 /* A few simplifications of "a ? CST1 : CST2". */
3984 /* NOTE: Only do this on gimple as the if-chain-to-switch
3985 optimization depends on the gimple to have if statements in it. */
3988 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
3990 (if (integer_zerop (@2))
3992 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
3993 (if (integer_onep (@1))
3994 (convert (convert:boolean_type_node @0)))
3995 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
3996 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
3998 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4000 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4001 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
4002 here as the powerof2cst case above will handle that case correctly. */
4003 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4004 (negate (convert (convert:boolean_type_node @0))))))
4005 (if (integer_zerop (@1))
4007 tree booltrue = constant_boolean_node (true, boolean_type_node);
4010 /* a ? 0 : 1 -> !a. */
4011 (if (integer_onep (@2))
4012 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4013 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4014 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2)
4015 && TYPE_PRECISION (type) != 1)
4017 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4019 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4021 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
4022 here as the powerof2cst case above will handle that case correctly. */
4023 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4024 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4032 /* Simplification moved from fold_cond_expr_with_comparison. It may also
4034 /* This pattern implements two kinds simplification:
4037 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4038 1) Conversions are type widening from smaller type.
4039 2) Const c1 equals to c2 after canonicalizing comparison.
4040 3) Comparison has tree code LT, LE, GT or GE.
4041 This specific pattern is needed when (cmp (convert x) c) may not
4042 be simplified by comparison patterns because of multiple uses of
4043 x. It also makes sense here because simplifying across multiple
4044 referred var is always benefitial for complicated cases.
4047 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
4048 (for cmp (lt le gt ge eq)
4050 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4053 tree from_type = TREE_TYPE (@1);
4054 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4055 enum tree_code code = ERROR_MARK;
4057 if (INTEGRAL_TYPE_P (from_type)
4058 && int_fits_type_p (@2, from_type)
4059 && (types_match (c1_type, from_type)
4060 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4061 && (TYPE_UNSIGNED (from_type)
4062 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4063 && (types_match (c2_type, from_type)
4064 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4065 && (TYPE_UNSIGNED (from_type)
4066 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4070 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4072 /* X <= Y - 1 equals to X < Y. */
4075 /* X > Y - 1 equals to X >= Y. */
4079 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4081 /* X < Y + 1 equals to X <= Y. */
4084 /* X >= Y + 1 equals to X > Y. */
4088 if (code != ERROR_MARK
4089 || wi::to_widest (@2) == wi::to_widest (@3))
4091 if (cmp == LT_EXPR || cmp == LE_EXPR)
4093 if (cmp == GT_EXPR || cmp == GE_EXPR)
4097 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4098 else if (int_fits_type_p (@3, from_type))
4102 (if (code == MAX_EXPR)
4103 (convert (max @1 (convert @2)))
4104 (if (code == MIN_EXPR)
4105 (convert (min @1 (convert @2)))
4106 (if (code == EQ_EXPR)
4107 (convert (cond (eq @1 (convert @3))
4108 (convert:from_type @3) (convert:from_type @2)))))))))
4110 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4112 1) OP is PLUS or MINUS.
4113 2) CMP is LT, LE, GT or GE.
4114 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4116 This pattern also handles special cases like:
4118 A) Operand x is a unsigned to signed type conversion and c1 is
4119 integer zero. In this case,
4120 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4121 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4122 B) Const c1 may not equal to (C3 op' C2). In this case we also
4123 check equality for (c1+1) and (c1-1) by adjusting comparison
4126 TODO: Though signed type is handled by this pattern, it cannot be
4127 simplified at the moment because C standard requires additional
4128 type promotion. In order to match&simplify it here, the IR needs
4129 to be cleaned up by other optimizers, i.e, VRP. */
4130 (for op (plus minus)
4131 (for cmp (lt le gt ge)
4133 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4134 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4135 (if (types_match (from_type, to_type)
4136 /* Check if it is special case A). */
4137 || (TYPE_UNSIGNED (from_type)
4138 && !TYPE_UNSIGNED (to_type)
4139 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4140 && integer_zerop (@1)
4141 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4144 wi::overflow_type overflow = wi::OVF_NONE;
4145 enum tree_code code, cmp_code = cmp;
4147 wide_int c1 = wi::to_wide (@1);
4148 wide_int c2 = wi::to_wide (@2);
4149 wide_int c3 = wi::to_wide (@3);
4150 signop sgn = TYPE_SIGN (from_type);
4152 /* Handle special case A), given x of unsigned type:
4153 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4154 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4155 if (!types_match (from_type, to_type))
4157 if (cmp_code == LT_EXPR)
4159 if (cmp_code == GE_EXPR)
4161 c1 = wi::max_value (to_type);
4163 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4164 compute (c3 op' c2) and check if it equals to c1 with op' being
4165 the inverted operator of op. Make sure overflow doesn't happen
4166 if it is undefined. */
4167 if (op == PLUS_EXPR)
4168 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4170 real_c1 = wi::add (c3, c2, sgn, &overflow);
4173 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4175 /* Check if c1 equals to real_c1. Boundary condition is handled
4176 by adjusting comparison operation if necessary. */
4177 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4180 /* X <= Y - 1 equals to X < Y. */
4181 if (cmp_code == LE_EXPR)
4183 /* X > Y - 1 equals to X >= Y. */
4184 if (cmp_code == GT_EXPR)
4187 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4190 /* X < Y + 1 equals to X <= Y. */
4191 if (cmp_code == LT_EXPR)
4193 /* X >= Y + 1 equals to X > Y. */
4194 if (cmp_code == GE_EXPR)
4197 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4199 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4201 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4206 (if (code == MAX_EXPR)
4207 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4208 { wide_int_to_tree (from_type, c2); })
4209 (if (code == MIN_EXPR)
4210 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4211 { wide_int_to_tree (from_type, c2); })))))))))
4213 (for cnd (cond vec_cond)
4214 /* A ? B : (A ? X : C) -> A ? B : C. */
4216 (cnd @0 (cnd @0 @1 @2) @3)
4219 (cnd @0 @1 (cnd @0 @2 @3))
4221 /* A ? B : (!A ? C : X) -> A ? B : C. */
4222 /* ??? This matches embedded conditions open-coded because genmatch
4223 would generate matching code for conditions in separate stmts only.
4224 The following is still important to merge then and else arm cases
4225 from if-conversion. */
4227 (cnd @0 @1 (cnd @2 @3 @4))
4228 (if (inverse_conditions_p (@0, @2))
4231 (cnd @0 (cnd @1 @2 @3) @4)
4232 (if (inverse_conditions_p (@0, @1))
4235 /* A ? B : B -> B. */
4240 /* !A ? B : C -> A ? C : B. */
4242 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4245 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4246 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4247 Need to handle UN* comparisons.
4249 None of these transformations work for modes with signed
4250 zeros. If A is +/-0, the first two transformations will
4251 change the sign of the result (from +0 to -0, or vice
4252 versa). The last four will fix the sign of the result,
4253 even though the original expressions could be positive or
4254 negative, depending on the sign of A.
4256 Note that all these transformations are correct if A is
4257 NaN, since the two alternatives (A and -A) are also NaNs. */
4259 (for cnd (cond vec_cond)
4260 /* A == 0 ? A : -A same as -A */
4263 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4264 (if (!HONOR_SIGNED_ZEROS (type))
4267 (cnd (cmp @0 zerop) integer_zerop (negate@1 @0))
4268 (if (!HONOR_SIGNED_ZEROS (type))
4271 /* A != 0 ? A : -A same as A */
4274 (cnd (cmp @0 zerop) @0 (negate @0))
4275 (if (!HONOR_SIGNED_ZEROS (type))
4278 (cnd (cmp @0 zerop) @0 integer_zerop)
4279 (if (!HONOR_SIGNED_ZEROS (type))
4282 /* A >=/> 0 ? A : -A same as abs (A) */
4285 (cnd (cmp @0 zerop) @0 (negate @0))
4286 (if (!HONOR_SIGNED_ZEROS (type)
4287 && !TYPE_UNSIGNED (type))
4289 /* A <=/< 0 ? A : -A same as -abs (A) */
4292 (cnd (cmp @0 zerop) @0 (negate @0))
4293 (if (!HONOR_SIGNED_ZEROS (type)
4294 && !TYPE_UNSIGNED (type))
4295 (if (ANY_INTEGRAL_TYPE_P (type)
4296 && !TYPE_OVERFLOW_WRAPS (type))
4298 tree utype = unsigned_type_for (type);
4300 (convert (negate (absu:utype @0))))
4301 (negate (abs @0)))))
4305 /* -(type)!A -> (type)A - 1. */
4307 (negate (convert?:s (logical_inverted_value:s @0)))
4308 (if (INTEGRAL_TYPE_P (type)
4309 && TREE_CODE (type) != BOOLEAN_TYPE
4310 && TYPE_PRECISION (type) > 1
4311 && TREE_CODE (@0) == SSA_NAME
4312 && ssa_name_has_boolean_range (@0))
4313 (plus (convert:type @0) { build_all_ones_cst (type); })))
4315 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4316 return all -1 or all 0 results. */
4317 /* ??? We could instead convert all instances of the vec_cond to negate,
4318 but that isn't necessarily a win on its own. */
4320 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4321 (if (VECTOR_TYPE_P (type)
4322 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4323 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4324 && (TYPE_MODE (TREE_TYPE (type))
4325 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4326 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4328 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4330 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4331 (if (VECTOR_TYPE_P (type)
4332 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4333 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4334 && (TYPE_MODE (TREE_TYPE (type))
4335 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4336 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4339 /* Simplifications of comparisons. */
4341 /* See if we can reduce the magnitude of a constant involved in a
4342 comparison by changing the comparison code. This is a canonicalization
4343 formerly done by maybe_canonicalize_comparison_1. */
4347 (cmp @0 uniform_integer_cst_p@1)
4348 (with { tree cst = uniform_integer_cst_p (@1); }
4349 (if (tree_int_cst_sgn (cst) == -1)
4350 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4351 wide_int_to_tree (TREE_TYPE (cst),
4357 (cmp @0 uniform_integer_cst_p@1)
4358 (with { tree cst = uniform_integer_cst_p (@1); }
4359 (if (tree_int_cst_sgn (cst) == 1)
4360 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4361 wide_int_to_tree (TREE_TYPE (cst),
4362 wi::to_wide (cst) - 1)); })))))
4364 /* We can simplify a logical negation of a comparison to the
4365 inverted comparison. As we cannot compute an expression
4366 operator using invert_tree_comparison we have to simulate
4367 that with expression code iteration. */
4368 (for cmp (tcc_comparison)
4369 icmp (inverted_tcc_comparison)
4370 ncmp (inverted_tcc_comparison_with_nans)
4371 /* Ideally we'd like to combine the following two patterns
4372 and handle some more cases by using
4373 (logical_inverted_value (cmp @0 @1))
4374 here but for that genmatch would need to "inline" that.
4375 For now implement what forward_propagate_comparison did. */
4377 (bit_not (cmp @0 @1))
4378 (if (VECTOR_TYPE_P (type)
4379 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4380 /* Comparison inversion may be impossible for trapping math,
4381 invert_tree_comparison will tell us. But we can't use
4382 a computed operator in the replacement tree thus we have
4383 to play the trick below. */
4384 (with { enum tree_code ic = invert_tree_comparison
4385 (cmp, HONOR_NANS (@0)); }
4391 (bit_xor (cmp @0 @1) integer_truep)
4392 (with { enum tree_code ic = invert_tree_comparison
4393 (cmp, HONOR_NANS (@0)); }
4399 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4400 ??? The transformation is valid for the other operators if overflow
4401 is undefined for the type, but performing it here badly interacts
4402 with the transformation in fold_cond_expr_with_comparison which
4403 attempts to synthetize ABS_EXPR. */
4405 (for sub (minus pointer_diff)
4407 (cmp (sub@2 @0 @1) integer_zerop)
4408 (if (single_use (@2))
4411 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4412 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4415 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4416 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4417 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4418 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4419 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4420 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4421 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4423 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4424 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4425 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4426 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4427 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4429 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4430 signed arithmetic case. That form is created by the compiler
4431 often enough for folding it to be of value. One example is in
4432 computing loop trip counts after Operator Strength Reduction. */
4433 (for cmp (simple_comparison)
4434 scmp (swapped_simple_comparison)
4436 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4437 /* Handle unfolded multiplication by zero. */
4438 (if (integer_zerop (@1))
4440 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4441 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4443 /* If @1 is negative we swap the sense of the comparison. */
4444 (if (tree_int_cst_sgn (@1) < 0)
4448 /* For integral types with undefined overflow fold
4449 x * C1 == C2 into x == C2 / C1 or false.
4450 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4454 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4455 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4456 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4457 && wi::to_wide (@1) != 0)
4458 (with { widest_int quot; }
4459 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4460 TYPE_SIGN (TREE_TYPE (@0)), "))
4461 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4462 { constant_boolean_node (cmp == NE_EXPR, type); }))
4463 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4464 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4465 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4468 tree itype = TREE_TYPE (@0);
4469 int p = TYPE_PRECISION (itype);
4470 wide_int m = wi::one (p + 1) << p;
4471 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4472 wide_int i = wide_int::from (wi::mod_inv (a, m),
4473 p, TYPE_SIGN (itype));
4474 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4477 /* Simplify comparison of something with itself. For IEEE
4478 floating-point, we can only do some of these simplifications. */
4482 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4483 || ! HONOR_NANS (@0))
4484 { constant_boolean_node (true, type); }
4485 (if (cmp != EQ_EXPR)
4491 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4492 || ! HONOR_NANS (@0))
4493 { constant_boolean_node (false, type); })))
4494 (for cmp (unle unge uneq)
4497 { constant_boolean_node (true, type); }))
4498 (for cmp (unlt ungt)
4504 (if (!flag_trapping_math)
4505 { constant_boolean_node (false, type); }))
4507 /* x == ~x -> false */
4508 /* x != ~x -> true */
4511 (cmp:c @0 (bit_not @0))
4512 { constant_boolean_node (cmp == NE_EXPR, type); }))
4514 /* Fold ~X op ~Y as Y op X. */
4515 (for cmp (simple_comparison)
4517 (cmp (bit_not@2 @0) (bit_not@3 @1))
4518 (if (single_use (@2) && single_use (@3))
4521 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4522 (for cmp (simple_comparison)
4523 scmp (swapped_simple_comparison)
4525 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4526 (if (single_use (@2)
4527 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4528 (scmp @0 (bit_not @1)))))
4530 (for cmp (simple_comparison)
4531 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4533 (cmp (convert@2 @0) (convert? @1))
4534 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4535 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4536 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4537 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4538 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4541 tree type1 = TREE_TYPE (@1);
4542 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4544 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4545 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4546 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4547 type1 = float_type_node;
4548 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4549 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4550 type1 = double_type_node;
4553 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4554 ? TREE_TYPE (@0) : type1);
4556 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4557 (cmp (convert:newtype @0) (convert:newtype @1))))))
4561 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4563 /* a CMP (-0) -> a CMP 0 */
4564 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4565 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4566 /* (-0) CMP b -> 0 CMP b. */
4567 (if (TREE_CODE (@0) == REAL_CST
4568 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
4569 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
4570 /* x != NaN is always true, other ops are always false. */
4571 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4572 && !tree_expr_signaling_nan_p (@1)
4573 && !tree_expr_maybe_signaling_nan_p (@0))
4574 { constant_boolean_node (cmp == NE_EXPR, type); })
4575 /* NaN != y is always true, other ops are always false. */
4576 (if (TREE_CODE (@0) == REAL_CST
4577 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
4578 && !tree_expr_signaling_nan_p (@0)
4579 && !tree_expr_signaling_nan_p (@1))
4580 { constant_boolean_node (cmp == NE_EXPR, type); })
4581 /* Fold comparisons against infinity. */
4582 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4583 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4586 REAL_VALUE_TYPE max;
4587 enum tree_code code = cmp;
4588 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4590 code = swap_tree_comparison (code);
4593 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4594 (if (code == GT_EXPR
4595 && !(HONOR_NANS (@0) && flag_trapping_math))
4596 { constant_boolean_node (false, type); })
4597 (if (code == LE_EXPR)
4598 /* x <= +Inf is always true, if we don't care about NaNs. */
4599 (if (! HONOR_NANS (@0))
4600 { constant_boolean_node (true, type); }
4601 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4602 an "invalid" exception. */
4603 (if (!flag_trapping_math)
4605 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4606 for == this introduces an exception for x a NaN. */
4607 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4609 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4611 (lt @0 { build_real (TREE_TYPE (@0), max); })
4612 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4613 /* x < +Inf is always equal to x <= DBL_MAX. */
4614 (if (code == LT_EXPR)
4615 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4617 (ge @0 { build_real (TREE_TYPE (@0), max); })
4618 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4619 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4620 an exception for x a NaN so use an unordered comparison. */
4621 (if (code == NE_EXPR)
4622 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4623 (if (! HONOR_NANS (@0))
4625 (ge @0 { build_real (TREE_TYPE (@0), max); })
4626 (le @0 { build_real (TREE_TYPE (@0), max); }))
4628 (unge @0 { build_real (TREE_TYPE (@0), max); })
4629 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4631 /* If this is a comparison of a real constant with a PLUS_EXPR
4632 or a MINUS_EXPR of a real constant, we can convert it into a
4633 comparison with a revised real constant as long as no overflow
4634 occurs when unsafe_math_optimizations are enabled. */
4635 (if (flag_unsafe_math_optimizations)
4636 (for op (plus minus)
4638 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4641 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4642 TREE_TYPE (@1), @2, @1);
4644 (if (tem && !TREE_OVERFLOW (tem))
4645 (cmp @0 { tem; }))))))
4647 /* Likewise, we can simplify a comparison of a real constant with
4648 a MINUS_EXPR whose first operand is also a real constant, i.e.
4649 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4650 floating-point types only if -fassociative-math is set. */
4651 (if (flag_associative_math)
4653 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4654 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4655 (if (tem && !TREE_OVERFLOW (tem))
4656 (cmp { tem; } @1)))))
4658 /* Fold comparisons against built-in math functions. */
4659 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4662 (cmp (sq @0) REAL_CST@1)
4664 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4666 /* sqrt(x) < y is always false, if y is negative. */
4667 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4668 { constant_boolean_node (false, type); })
4669 /* sqrt(x) > y is always true, if y is negative and we
4670 don't care about NaNs, i.e. negative values of x. */
4671 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4672 { constant_boolean_node (true, type); })
4673 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4674 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4675 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4677 /* sqrt(x) < 0 is always false. */
4678 (if (cmp == LT_EXPR)
4679 { constant_boolean_node (false, type); })
4680 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4681 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4682 { constant_boolean_node (true, type); })
4683 /* sqrt(x) <= 0 -> x == 0. */
4684 (if (cmp == LE_EXPR)
4686 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4687 == or !=. In the last case:
4689 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4691 if x is negative or NaN. Due to -funsafe-math-optimizations,
4692 the results for other x follow from natural arithmetic. */
4694 (if ((cmp == LT_EXPR
4698 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4699 /* Give up for -frounding-math. */
4700 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4704 enum tree_code ncmp = cmp;
4705 const real_format *fmt
4706 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4707 real_arithmetic (&c2, MULT_EXPR,
4708 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4709 real_convert (&c2, fmt, &c2);
4710 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4711 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4712 if (!REAL_VALUE_ISINF (c2))
4714 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4715 build_real (TREE_TYPE (@0), c2));
4716 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4718 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4719 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4720 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4721 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4722 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4723 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4726 /* With rounding to even, sqrt of up to 3 different values
4727 gives the same normal result, so in some cases c2 needs
4729 REAL_VALUE_TYPE c2alt, tow;
4730 if (cmp == LT_EXPR || cmp == GE_EXPR)
4734 real_nextafter (&c2alt, fmt, &c2, &tow);
4735 real_convert (&c2alt, fmt, &c2alt);
4736 if (REAL_VALUE_ISINF (c2alt))
4740 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4741 build_real (TREE_TYPE (@0), c2alt));
4742 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4744 else if (real_equal (&TREE_REAL_CST (c3),
4745 &TREE_REAL_CST (@1)))
4751 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4752 (if (REAL_VALUE_ISINF (c2))
4753 /* sqrt(x) > y is x == +Inf, when y is very large. */
4754 (if (HONOR_INFINITIES (@0))
4755 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4756 { constant_boolean_node (false, type); })
4757 /* sqrt(x) > c is the same as x > c*c. */
4758 (if (ncmp != ERROR_MARK)
4759 (if (ncmp == GE_EXPR)
4760 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4761 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4762 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4763 (if (REAL_VALUE_ISINF (c2))
4765 /* sqrt(x) < y is always true, when y is a very large
4766 value and we don't care about NaNs or Infinities. */
4767 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4768 { constant_boolean_node (true, type); })
4769 /* sqrt(x) < y is x != +Inf when y is very large and we
4770 don't care about NaNs. */
4771 (if (! HONOR_NANS (@0))
4772 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4773 /* sqrt(x) < y is x >= 0 when y is very large and we
4774 don't care about Infinities. */
4775 (if (! HONOR_INFINITIES (@0))
4776 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4777 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4780 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4781 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4782 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4783 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4784 (if (ncmp == LT_EXPR)
4785 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4786 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4787 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4788 (if (ncmp != ERROR_MARK && GENERIC)
4789 (if (ncmp == LT_EXPR)
4791 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4792 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4794 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4795 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4796 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4798 (cmp (sq @0) (sq @1))
4799 (if (! HONOR_NANS (@0))
4802 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4803 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4804 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4806 (cmp (float@0 @1) (float @2))
4807 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4808 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4811 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4812 tree type1 = TREE_TYPE (@1);
4813 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4814 tree type2 = TREE_TYPE (@2);
4815 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4817 (if (fmt.can_represent_integral_type_p (type1)
4818 && fmt.can_represent_integral_type_p (type2))
4819 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4820 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4821 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4822 && type1_signed_p >= type2_signed_p)
4823 (icmp @1 (convert @2))
4824 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4825 && type1_signed_p <= type2_signed_p)
4826 (icmp (convert:type2 @1) @2)
4827 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4828 && type1_signed_p == type2_signed_p)
4829 (icmp @1 @2))))))))))
4831 /* Optimize various special cases of (FTYPE) N CMP CST. */
4832 (for cmp (lt le eq ne ge gt)
4833 icmp (le le eq ne ge ge)
4835 (cmp (float @0) REAL_CST@1)
4836 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4837 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4840 tree itype = TREE_TYPE (@0);
4841 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4842 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4843 /* Be careful to preserve any potential exceptions due to
4844 NaNs. qNaNs are ok in == or != context.
4845 TODO: relax under -fno-trapping-math or
4846 -fno-signaling-nans. */
4848 = real_isnan (cst) && (cst->signalling
4849 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4851 /* TODO: allow non-fitting itype and SNaNs when
4852 -fno-trapping-math. */
4853 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4856 signop isign = TYPE_SIGN (itype);
4857 REAL_VALUE_TYPE imin, imax;
4858 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4859 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4861 REAL_VALUE_TYPE icst;
4862 if (cmp == GT_EXPR || cmp == GE_EXPR)
4863 real_ceil (&icst, fmt, cst);
4864 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4865 real_floor (&icst, fmt, cst);
4867 real_trunc (&icst, fmt, cst);
4869 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4871 bool overflow_p = false;
4873 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4876 /* Optimize cases when CST is outside of ITYPE's range. */
4877 (if (real_compare (LT_EXPR, cst, &imin))
4878 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4880 (if (real_compare (GT_EXPR, cst, &imax))
4881 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4883 /* Remove cast if CST is an integer representable by ITYPE. */
4885 (cmp @0 { gcc_assert (!overflow_p);
4886 wide_int_to_tree (itype, icst_val); })
4888 /* When CST is fractional, optimize
4889 (FTYPE) N == CST -> 0
4890 (FTYPE) N != CST -> 1. */
4891 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4892 { constant_boolean_node (cmp == NE_EXPR, type); })
4893 /* Otherwise replace with sensible integer constant. */
4896 gcc_checking_assert (!overflow_p);
4898 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4900 /* Fold A /[ex] B CMP C to A CMP B * C. */
4903 (cmp (exact_div @0 @1) INTEGER_CST@2)
4904 (if (!integer_zerop (@1))
4905 (if (wi::to_wide (@2) == 0)
4907 (if (TREE_CODE (@1) == INTEGER_CST)
4910 wi::overflow_type ovf;
4911 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4912 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4915 { constant_boolean_node (cmp == NE_EXPR, type); }
4916 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4917 (for cmp (lt le gt ge)
4919 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4920 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4923 wi::overflow_type ovf;
4924 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4925 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4928 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4929 TYPE_SIGN (TREE_TYPE (@2)))
4930 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4931 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4933 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4935 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4936 For large C (more than min/B+2^size), this is also true, with the
4937 multiplication computed modulo 2^size.
4938 For intermediate C, this just tests the sign of A. */
4939 (for cmp (lt le gt ge)
4942 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4943 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4944 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4945 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4948 tree utype = TREE_TYPE (@2);
4949 wide_int denom = wi::to_wide (@1);
4950 wide_int right = wi::to_wide (@2);
4951 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4952 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4953 bool small = wi::leu_p (right, smax);
4954 bool large = wi::geu_p (right, smin);
4956 (if (small || large)
4957 (cmp (convert:utype @0) (mult @2 (convert @1)))
4958 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4960 /* Unordered tests if either argument is a NaN. */
4962 (bit_ior (unordered @0 @0) (unordered @1 @1))
4963 (if (types_match (@0, @1))
4966 (bit_and (ordered @0 @0) (ordered @1 @1))
4967 (if (types_match (@0, @1))
4970 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4973 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4976 /* Simple range test simplifications. */
4977 /* A < B || A >= B -> true. */
4978 (for test1 (lt le le le ne ge)
4979 test2 (ge gt ge ne eq ne)
4981 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4982 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4983 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4984 { constant_boolean_node (true, type); })))
4985 /* A < B && A >= B -> false. */
4986 (for test1 (lt lt lt le ne eq)
4987 test2 (ge gt eq gt eq gt)
4989 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4990 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4991 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4992 { constant_boolean_node (false, type); })))
4994 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4995 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4997 Note that comparisons
4998 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4999 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
5000 will be canonicalized to above so there's no need to
5007 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5008 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5011 tree ty = TREE_TYPE (@0);
5012 unsigned prec = TYPE_PRECISION (ty);
5013 wide_int mask = wi::to_wide (@2, prec);
5014 wide_int rhs = wi::to_wide (@3, prec);
5015 signop sgn = TYPE_SIGN (ty);
5017 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5018 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5019 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5020 { build_zero_cst (ty); }))))))
5022 /* -A CMP -B -> B CMP A. */
5023 (for cmp (tcc_comparison)
5024 scmp (swapped_tcc_comparison)
5026 (cmp (negate @0) (negate @1))
5027 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5028 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5029 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5032 (cmp (negate @0) CONSTANT_CLASS_P@1)
5033 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5034 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5035 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5036 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5037 (if (tem && !TREE_OVERFLOW (tem))
5038 (scmp @0 { tem; }))))))
5040 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
5043 (op (abs @0) zerop@1)
5046 /* From fold_sign_changed_comparison and fold_widened_comparison.
5047 FIXME: the lack of symmetry is disturbing. */
5048 (for cmp (simple_comparison)
5050 (cmp (convert@0 @00) (convert?@1 @10))
5051 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5052 /* Disable this optimization if we're casting a function pointer
5053 type on targets that require function pointer canonicalization. */
5054 && !(targetm.have_canonicalize_funcptr_for_compare ()
5055 && ((POINTER_TYPE_P (TREE_TYPE (@00))
5056 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5057 || (POINTER_TYPE_P (TREE_TYPE (@10))
5058 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5060 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5061 && (TREE_CODE (@10) == INTEGER_CST
5063 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5066 && !POINTER_TYPE_P (TREE_TYPE (@00)))
5067 /* ??? The special-casing of INTEGER_CST conversion was in the original
5068 code and here to avoid a spurious overflow flag on the resulting
5069 constant which fold_convert produces. */
5070 (if (TREE_CODE (@1) == INTEGER_CST)
5071 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5072 TREE_OVERFLOW (@1)); })
5073 (cmp @00 (convert @1)))
5075 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5076 /* If possible, express the comparison in the shorter mode. */
5077 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5078 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5079 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5080 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5081 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5082 || ((TYPE_PRECISION (TREE_TYPE (@00))
5083 >= TYPE_PRECISION (TREE_TYPE (@10)))
5084 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5085 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5086 || (TREE_CODE (@10) == INTEGER_CST
5087 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5088 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5089 (cmp @00 (convert @10))
5090 (if (TREE_CODE (@10) == INTEGER_CST
5091 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5092 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5095 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5096 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5097 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5098 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5100 (if (above || below)
5101 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5102 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5103 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5104 { constant_boolean_node (above ? true : false, type); }
5105 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5106 { constant_boolean_node (above ? false : true, type); }))))))))))))
5110 /* SSA names are canonicalized to 2nd place. */
5111 (cmp addr@0 SSA_NAME@1)
5113 { poly_int64 off; tree base; }
5114 /* A local variable can never be pointed to by
5115 the default SSA name of an incoming parameter. */
5116 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5117 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5118 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5119 && TREE_CODE (base) == VAR_DECL
5120 && auto_var_in_fn_p (base, current_function_decl))
5121 (if (cmp == NE_EXPR)
5122 { constant_boolean_node (true, type); }
5123 { constant_boolean_node (false, type); })
5124 /* If the address is based on @1 decide using the offset. */
5125 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5126 && TREE_CODE (base) == MEM_REF
5127 && TREE_OPERAND (base, 0) == @1)
5128 (with { off += mem_ref_offset (base).force_shwi (); }
5129 (if (known_ne (off, 0))
5130 { constant_boolean_node (cmp == NE_EXPR, type); }
5131 (if (known_eq (off, 0))
5132 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5134 /* Equality compare simplifications from fold_binary */
5137 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5138 Similarly for NE_EXPR. */
5140 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5141 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5142 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5143 { constant_boolean_node (cmp == NE_EXPR, type); }))
5145 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5147 (cmp (bit_xor @0 @1) integer_zerop)
5150 /* (X ^ Y) == Y becomes X == 0.
5151 Likewise (X ^ Y) == X becomes Y == 0. */
5153 (cmp:c (bit_xor:c @0 @1) @0)
5154 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5157 /* (X & Y) == X becomes (X & ~Y) == 0. */
5159 (cmp:c (bit_and:c @0 @1) @0)
5160 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5162 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5163 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5164 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5165 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5166 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5167 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5168 && !wi::neg_p (wi::to_wide (@1)))
5169 (cmp (bit_and @0 (convert (bit_not @1)))
5170 { build_zero_cst (TREE_TYPE (@0)); })))
5172 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5174 (cmp:c (bit_ior:c @0 @1) @1)
5175 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5178 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5180 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5181 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5182 (cmp @0 (bit_xor @1 (convert @2)))))
5185 (cmp (convert? addr@0) integer_zerop)
5186 (if (tree_single_nonzero_warnv_p (@0, NULL))
5187 { constant_boolean_node (cmp == NE_EXPR, type); }))
5189 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5191 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5192 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5194 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5195 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5196 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5197 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5202 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5203 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5204 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5205 && types_match (@0, @1))
5206 (ncmp (bit_xor @0 @1) @2)))))
5207 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5208 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5212 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5213 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5214 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5215 && types_match (@0, @1))
5216 (ncmp (bit_xor @0 @1) @2))))
5218 /* If we have (A & C) == C where C is a power of 2, convert this into
5219 (A & C) != 0. Similarly for NE_EXPR. */
5223 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5224 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5227 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5228 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5230 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5231 (if (INTEGRAL_TYPE_P (type)
5232 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5233 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5234 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5237 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5239 (if (cmp == LT_EXPR)
5240 (bit_xor (convert (rshift @0 {shifter;})) @1)
5241 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5242 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5243 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5245 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5246 (if (INTEGRAL_TYPE_P (type)
5247 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5248 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5249 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5252 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5254 (if (cmp == GE_EXPR)
5255 (bit_xor (convert (rshift @0 {shifter;})) @1)
5256 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5258 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5259 convert this into a shift followed by ANDing with D. */
5262 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5263 INTEGER_CST@2 integer_zerop)
5264 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5266 int shift = (wi::exact_log2 (wi::to_wide (@2))
5267 - wi::exact_log2 (wi::to_wide (@1)));
5271 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5273 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5276 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5277 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5281 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5282 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5283 && type_has_mode_precision_p (TREE_TYPE (@0))
5284 && element_precision (@2) >= element_precision (@0)
5285 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5286 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5287 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5289 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5290 this into a right shift or sign extension followed by ANDing with C. */
5293 (lt @0 integer_zerop)
5294 INTEGER_CST@1 integer_zerop)
5295 (if (integer_pow2p (@1)
5296 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5298 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5302 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5304 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5305 sign extension followed by AND with C will achieve the effect. */
5306 (bit_and (convert @0) @1)))))
5308 /* When the addresses are not directly of decls compare base and offset.
5309 This implements some remaining parts of fold_comparison address
5310 comparisons but still no complete part of it. Still it is good
5311 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5312 (for cmp (simple_comparison)
5314 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5317 poly_int64 off0, off1;
5319 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
5320 off0, off1, GENERIC);
5324 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5325 { constant_boolean_node (known_eq (off0, off1), type); })
5326 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5327 { constant_boolean_node (known_ne (off0, off1), type); })
5328 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5329 { constant_boolean_node (known_lt (off0, off1), type); })
5330 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5331 { constant_boolean_node (known_le (off0, off1), type); })
5332 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5333 { constant_boolean_node (known_ge (off0, off1), type); })
5334 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5335 { constant_boolean_node (known_gt (off0, off1), type); }))
5338 (if (cmp == EQ_EXPR)
5339 { constant_boolean_node (false, type); })
5340 (if (cmp == NE_EXPR)
5341 { constant_boolean_node (true, type); })))))))
5343 /* Simplify pointer equality compares using PTA. */
5347 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5348 && ptrs_compare_unequal (@0, @1))
5349 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5351 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5352 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5353 Disable the transform if either operand is pointer to function.
5354 This broke pr22051-2.c for arm where function pointer
5355 canonicalizaion is not wanted. */
5359 (cmp (convert @0) INTEGER_CST@1)
5360 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5361 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5362 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5363 /* Don't perform this optimization in GENERIC if @0 has reference
5364 type when sanitizing. See PR101210. */
5366 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5367 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5368 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5369 && POINTER_TYPE_P (TREE_TYPE (@1))
5370 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5371 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5372 (cmp @0 (convert @1)))))
5374 /* Non-equality compare simplifications from fold_binary */
5375 (for cmp (lt gt le ge)
5376 /* Comparisons with the highest or lowest possible integer of
5377 the specified precision will have known values. */
5379 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5380 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5381 || POINTER_TYPE_P (TREE_TYPE (@1))
5382 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5383 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5386 tree cst = uniform_integer_cst_p (@1);
5387 tree arg1_type = TREE_TYPE (cst);
5388 unsigned int prec = TYPE_PRECISION (arg1_type);
5389 wide_int max = wi::max_value (arg1_type);
5390 wide_int signed_max = wi::max_value (prec, SIGNED);
5391 wide_int min = wi::min_value (arg1_type);
5394 (if (wi::to_wide (cst) == max)
5396 (if (cmp == GT_EXPR)
5397 { constant_boolean_node (false, type); })
5398 (if (cmp == GE_EXPR)
5400 (if (cmp == LE_EXPR)
5401 { constant_boolean_node (true, type); })
5402 (if (cmp == LT_EXPR)
5404 (if (wi::to_wide (cst) == min)
5406 (if (cmp == LT_EXPR)
5407 { constant_boolean_node (false, type); })
5408 (if (cmp == LE_EXPR)
5410 (if (cmp == GE_EXPR)
5411 { constant_boolean_node (true, type); })
5412 (if (cmp == GT_EXPR)
5414 (if (wi::to_wide (cst) == max - 1)
5416 (if (cmp == GT_EXPR)
5417 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5418 wide_int_to_tree (TREE_TYPE (cst),
5421 (if (cmp == LE_EXPR)
5422 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5423 wide_int_to_tree (TREE_TYPE (cst),
5426 (if (wi::to_wide (cst) == min + 1)
5428 (if (cmp == GE_EXPR)
5429 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5430 wide_int_to_tree (TREE_TYPE (cst),
5433 (if (cmp == LT_EXPR)
5434 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5435 wide_int_to_tree (TREE_TYPE (cst),
5438 (if (wi::to_wide (cst) == signed_max
5439 && TYPE_UNSIGNED (arg1_type)
5440 /* We will flip the signedness of the comparison operator
5441 associated with the mode of @1, so the sign bit is
5442 specified by this mode. Check that @1 is the signed
5443 max associated with this sign bit. */
5444 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5445 /* signed_type does not work on pointer types. */
5446 && INTEGRAL_TYPE_P (arg1_type))
5447 /* The following case also applies to X < signed_max+1
5448 and X >= signed_max+1 because previous transformations. */
5449 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5450 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5452 (if (cst == @1 && cmp == LE_EXPR)
5453 (ge (convert:st @0) { build_zero_cst (st); }))
5454 (if (cst == @1 && cmp == GT_EXPR)
5455 (lt (convert:st @0) { build_zero_cst (st); }))
5456 (if (cmp == LE_EXPR)
5457 (ge (view_convert:st @0) { build_zero_cst (st); }))
5458 (if (cmp == GT_EXPR)
5459 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5461 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5462 /* If the second operand is NaN, the result is constant. */
5465 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5466 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5467 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5468 ? false : true, type); })))
5470 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5474 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5475 { constant_boolean_node (true, type); })
5476 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5477 { constant_boolean_node (false, type); })))
5479 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5483 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5484 { constant_boolean_node (false, type); })
5485 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5486 { constant_boolean_node (true, type); })))
5488 /* bool_var != 0 becomes bool_var. */
5490 (ne @0 integer_zerop)
5491 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5492 && types_match (type, TREE_TYPE (@0)))
5494 /* bool_var == 1 becomes bool_var. */
5496 (eq @0 integer_onep)
5497 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5498 && types_match (type, TREE_TYPE (@0)))
5501 bool_var == 0 becomes !bool_var or
5502 bool_var != 1 becomes !bool_var
5503 here because that only is good in assignment context as long
5504 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5505 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5506 clearly less optimal and which we'll transform again in forwprop. */
5508 /* When one argument is a constant, overflow detection can be simplified.
5509 Currently restricted to single use so as not to interfere too much with
5510 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5511 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5512 (for cmp (lt le ge gt)
5515 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5516 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5517 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5518 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5519 && wi::to_wide (@1) != 0
5522 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5523 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5525 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5526 wi::max_value (prec, sign)
5527 - wi::to_wide (@1)); })))))
5529 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5530 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5531 expects the long form, so we restrict the transformation for now. */
5534 (cmp:c (minus@2 @0 @1) @0)
5535 (if (single_use (@2)
5536 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5537 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5540 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5543 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5544 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5545 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5548 /* Testing for overflow is unnecessary if we already know the result. */
5553 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5554 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5555 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5556 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5561 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5562 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5563 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5564 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5566 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5567 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5571 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5572 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5573 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5574 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5576 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5577 is at least twice as wide as type of A and B, simplify to
5578 __builtin_mul_overflow (A, B, <unused>). */
5581 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5583 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5584 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5585 && TYPE_UNSIGNED (TREE_TYPE (@0))
5586 && (TYPE_PRECISION (TREE_TYPE (@3))
5587 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5588 && tree_fits_uhwi_p (@2)
5589 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5590 && types_match (@0, @1)
5591 && type_has_mode_precision_p (TREE_TYPE (@0))
5592 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5593 != CODE_FOR_nothing))
5594 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5595 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5597 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
5598 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
5600 (ovf (convert@2 @0) @1)
5601 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5602 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5603 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5604 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5607 (ovf @1 (convert@2 @0))
5608 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5609 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5610 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5611 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5614 /* Simplification of math builtins. These rules must all be optimizations
5615 as well as IL simplifications. If there is a possibility that the new
5616 form could be a pessimization, the rule should go in the canonicalization
5617 section that follows this one.
5619 Rules can generally go in this section if they satisfy one of
5622 - the rule describes an identity
5624 - the rule replaces calls with something as simple as addition or
5627 - the rule contains unary calls only and simplifies the surrounding
5628 arithmetic. (The idea here is to exclude non-unary calls in which
5629 one operand is constant and in which the call is known to be cheap
5630 when the operand has that value.) */
5632 (if (flag_unsafe_math_optimizations)
5633 /* Simplify sqrt(x) * sqrt(x) -> x. */
5635 (mult (SQRT_ALL@1 @0) @1)
5636 (if (!tree_expr_maybe_signaling_nan_p (@0))
5639 (for op (plus minus)
5640 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5644 (rdiv (op @0 @2) @1)))
5646 (for cmp (lt le gt ge)
5647 neg_cmp (gt ge lt le)
5648 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5650 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5652 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5654 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5655 || (real_zerop (tem) && !real_zerop (@1))))
5657 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5659 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5660 (neg_cmp @0 { tem; })))))))
5662 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5663 (for root (SQRT CBRT)
5665 (mult (root:s @0) (root:s @1))
5666 (root (mult @0 @1))))
5668 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5669 (for exps (EXP EXP2 EXP10 POW10)
5671 (mult (exps:s @0) (exps:s @1))
5672 (exps (plus @0 @1))))
5674 /* Simplify a/root(b/c) into a*root(c/b). */
5675 (for root (SQRT CBRT)
5677 (rdiv @0 (root:s (rdiv:s @1 @2)))
5678 (mult @0 (root (rdiv @2 @1)))))
5680 /* Simplify x/expN(y) into x*expN(-y). */
5681 (for exps (EXP EXP2 EXP10 POW10)
5683 (rdiv @0 (exps:s @1))
5684 (mult @0 (exps (negate @1)))))
5686 (for logs (LOG LOG2 LOG10 LOG10)
5687 exps (EXP EXP2 EXP10 POW10)
5688 /* logN(expN(x)) -> x. */
5692 /* expN(logN(x)) -> x. */
5697 /* Optimize logN(func()) for various exponential functions. We
5698 want to determine the value "x" and the power "exponent" in
5699 order to transform logN(x**exponent) into exponent*logN(x). */
5700 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5701 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5704 (if (SCALAR_FLOAT_TYPE_P (type))
5710 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5711 x = build_real_truncate (type, dconst_e ());
5714 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5715 x = build_real (type, dconst2);
5719 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5721 REAL_VALUE_TYPE dconst10;
5722 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5723 x = build_real (type, dconst10);
5730 (mult (logs { x; }) @0)))))
5738 (if (SCALAR_FLOAT_TYPE_P (type))
5744 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5745 x = build_real (type, dconsthalf);
5748 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5749 x = build_real_truncate (type, dconst_third ());
5755 (mult { x; } (logs @0))))))
5757 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5758 (for logs (LOG LOG2 LOG10)
5762 (mult @1 (logs @0))))
5764 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5765 or if C is a positive power of 2,
5766 pow(C,x) -> exp2(log2(C)*x). */
5774 (pows REAL_CST@0 @1)
5775 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5776 && real_isfinite (TREE_REAL_CST_PTR (@0))
5777 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5778 the use_exp2 case until after vectorization. It seems actually
5779 beneficial for all constants to postpone this until later,
5780 because exp(log(C)*x), while faster, will have worse precision
5781 and if x folds into a constant too, that is unnecessary
5783 && canonicalize_math_after_vectorization_p ())
5785 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5786 bool use_exp2 = false;
5787 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5788 && value->cl == rvc_normal)
5790 REAL_VALUE_TYPE frac_rvt = *value;
5791 SET_REAL_EXP (&frac_rvt, 1);
5792 if (real_equal (&frac_rvt, &dconst1))
5797 (if (optimize_pow_to_exp (@0, @1))
5798 (exps (mult (logs @0) @1)))
5799 (exp2s (mult (log2s @0) @1)))))))
5802 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5804 exps (EXP EXP2 EXP10 POW10)
5805 logs (LOG LOG2 LOG10 LOG10)
5807 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5808 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5809 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5810 (exps (plus (mult (logs @0) @1) @2)))))
5815 exps (EXP EXP2 EXP10 POW10)
5816 /* sqrt(expN(x)) -> expN(x*0.5). */
5819 (exps (mult @0 { build_real (type, dconsthalf); })))
5820 /* cbrt(expN(x)) -> expN(x/3). */
5823 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5824 /* pow(expN(x), y) -> expN(x*y). */
5827 (exps (mult @0 @1))))
5829 /* tan(atan(x)) -> x. */
5836 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5840 copysigns (COPYSIGN)
5845 REAL_VALUE_TYPE r_cst;
5846 build_sinatan_real (&r_cst, type);
5847 tree t_cst = build_real (type, r_cst);
5848 tree t_one = build_one_cst (type);
5850 (if (SCALAR_FLOAT_TYPE_P (type))
5851 (cond (lt (abs @0) { t_cst; })
5852 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5853 (copysigns { t_one; } @0))))))
5855 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5859 copysigns (COPYSIGN)
5864 REAL_VALUE_TYPE r_cst;
5865 build_sinatan_real (&r_cst, type);
5866 tree t_cst = build_real (type, r_cst);
5867 tree t_one = build_one_cst (type);
5868 tree t_zero = build_zero_cst (type);
5870 (if (SCALAR_FLOAT_TYPE_P (type))
5871 (cond (lt (abs @0) { t_cst; })
5872 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5873 (copysigns { t_zero; } @0))))))
5875 (if (!flag_errno_math)
5876 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5881 (sinhs (atanhs:s @0))
5882 (with { tree t_one = build_one_cst (type); }
5883 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5885 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5890 (coshs (atanhs:s @0))
5891 (with { tree t_one = build_one_cst (type); }
5892 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5894 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5896 (CABS (complex:C @0 real_zerop@1))
5899 /* trunc(trunc(x)) -> trunc(x), etc. */
5900 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5904 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5905 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5907 (fns integer_valued_real_p@0)
5910 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5912 (HYPOT:c @0 real_zerop@1)
5915 /* pow(1,x) -> 1. */
5917 (POW real_onep@0 @1)
5921 /* copysign(x,x) -> x. */
5922 (COPYSIGN_ALL @0 @0)
5926 /* copysign(x,-x) -> -x. */
5927 (COPYSIGN_ALL @0 (negate@1 @0))
5931 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5932 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5935 (for scale (LDEXP SCALBN SCALBLN)
5936 /* ldexp(0, x) -> 0. */
5938 (scale real_zerop@0 @1)
5940 /* ldexp(x, 0) -> x. */
5942 (scale @0 integer_zerop@1)
5944 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5946 (scale REAL_CST@0 @1)
5947 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5950 /* Canonicalization of sequences of math builtins. These rules represent
5951 IL simplifications but are not necessarily optimizations.
5953 The sincos pass is responsible for picking "optimal" implementations
5954 of math builtins, which may be more complicated and can sometimes go
5955 the other way, e.g. converting pow into a sequence of sqrts.
5956 We only want to do these canonicalizations before the pass has run. */
5958 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5959 /* Simplify tan(x) * cos(x) -> sin(x). */
5961 (mult:c (TAN:s @0) (COS:s @0))
5964 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5966 (mult:c @0 (POW:s @0 REAL_CST@1))
5967 (if (!TREE_OVERFLOW (@1))
5968 (POW @0 (plus @1 { build_one_cst (type); }))))
5970 /* Simplify sin(x) / cos(x) -> tan(x). */
5972 (rdiv (SIN:s @0) (COS:s @0))
5975 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5977 (rdiv (SINH:s @0) (COSH:s @0))
5980 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5982 (rdiv (TANH:s @0) (SINH:s @0))
5983 (rdiv {build_one_cst (type);} (COSH @0)))
5985 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5987 (rdiv (COS:s @0) (SIN:s @0))
5988 (rdiv { build_one_cst (type); } (TAN @0)))
5990 /* Simplify sin(x) / tan(x) -> cos(x). */
5992 (rdiv (SIN:s @0) (TAN:s @0))
5993 (if (! HONOR_NANS (@0)
5994 && ! HONOR_INFINITIES (@0))
5997 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5999 (rdiv (TAN:s @0) (SIN:s @0))
6000 (if (! HONOR_NANS (@0)
6001 && ! HONOR_INFINITIES (@0))
6002 (rdiv { build_one_cst (type); } (COS @0))))
6004 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6006 (mult (POW:s @0 @1) (POW:s @0 @2))
6007 (POW @0 (plus @1 @2)))
6009 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6011 (mult (POW:s @0 @1) (POW:s @2 @1))
6012 (POW (mult @0 @2) @1))
6014 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6016 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6017 (POWI (mult @0 @2) @1))
6019 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6021 (rdiv (POW:s @0 REAL_CST@1) @0)
6022 (if (!TREE_OVERFLOW (@1))
6023 (POW @0 (minus @1 { build_one_cst (type); }))))
6025 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6027 (rdiv @0 (POW:s @1 @2))
6028 (mult @0 (POW @1 (negate @2))))
6033 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6036 (pows @0 { build_real (type, dconst_quarter ()); }))
6037 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6040 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6041 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6044 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6045 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6047 (cbrts (cbrts tree_expr_nonnegative_p@0))
6048 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6049 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6051 (sqrts (pows @0 @1))
6052 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6053 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6055 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6056 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6057 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6059 (pows (sqrts @0) @1)
6060 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6061 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6063 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6064 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6065 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6067 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6068 (pows @0 (mult @1 @2))))
6070 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6072 (CABS (complex @0 @0))
6073 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6075 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6078 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6080 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6085 (cexps compositional_complex@0)
6086 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6088 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6089 (mult @1 (imagpart @2)))))))
6091 (if (canonicalize_math_p ())
6092 /* floor(x) -> trunc(x) if x is nonnegative. */
6093 (for floors (FLOOR_ALL)
6096 (floors tree_expr_nonnegative_p@0)
6099 (match double_value_p
6101 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6102 (for froms (BUILT_IN_TRUNCL
6114 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6115 (if (optimize && canonicalize_math_p ())
6117 (froms (convert double_value_p@0))
6118 (convert (tos @0)))))
6120 (match float_value_p
6122 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6123 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6124 BUILT_IN_FLOORL BUILT_IN_FLOOR
6125 BUILT_IN_CEILL BUILT_IN_CEIL
6126 BUILT_IN_ROUNDL BUILT_IN_ROUND
6127 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6128 BUILT_IN_RINTL BUILT_IN_RINT)
6129 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6130 BUILT_IN_FLOORF BUILT_IN_FLOORF
6131 BUILT_IN_CEILF BUILT_IN_CEILF
6132 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6133 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6134 BUILT_IN_RINTF BUILT_IN_RINTF)
6135 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6137 (if (optimize && canonicalize_math_p ()
6138 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6140 (froms (convert float_value_p@0))
6141 (convert (tos @0)))))
6144 (match float16_value_p
6146 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
6147 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
6148 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
6149 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
6150 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
6151 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
6152 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
6153 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
6154 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
6155 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
6156 IFN_FLOOR IFN_FLOOR IFN_FLOOR
6157 IFN_CEIL IFN_CEIL IFN_CEIL
6158 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
6159 IFN_ROUND IFN_ROUND IFN_ROUND
6160 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
6161 IFN_RINT IFN_RINT IFN_RINT
6162 IFN_SQRT IFN_SQRT IFN_SQRT)
6163 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
6164 if x is a _Float16. */
6166 (convert (froms (convert float16_value_p@0)))
6168 && types_match (type, TREE_TYPE (@0))
6169 && direct_internal_fn_supported_p (as_internal_fn (tos),
6170 type, OPTIMIZE_FOR_BOTH))
6174 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6175 tos (XFLOOR XCEIL XROUND XRINT)
6176 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6177 (if (optimize && canonicalize_math_p ())
6179 (froms (convert double_value_p@0))
6182 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6183 XFLOOR XCEIL XROUND XRINT)
6184 tos (XFLOORF XCEILF XROUNDF XRINTF)
6185 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6187 (if (optimize && canonicalize_math_p ())
6189 (froms (convert float_value_p@0))
6192 (if (canonicalize_math_p ())
6193 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6194 (for floors (IFLOOR LFLOOR LLFLOOR)
6196 (floors tree_expr_nonnegative_p@0)
6199 (if (canonicalize_math_p ())
6200 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6201 (for fns (IFLOOR LFLOOR LLFLOOR
6203 IROUND LROUND LLROUND)
6205 (fns integer_valued_real_p@0)
6207 (if (!flag_errno_math)
6208 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6209 (for rints (IRINT LRINT LLRINT)
6211 (rints integer_valued_real_p@0)
6214 (if (canonicalize_math_p ())
6215 (for ifn (IFLOOR ICEIL IROUND IRINT)
6216 lfn (LFLOOR LCEIL LROUND LRINT)
6217 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6218 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6219 sizeof (int) == sizeof (long). */
6220 (if (TYPE_PRECISION (integer_type_node)
6221 == TYPE_PRECISION (long_integer_type_node))
6224 (lfn:long_integer_type_node @0)))
6225 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6226 sizeof (long long) == sizeof (long). */
6227 (if (TYPE_PRECISION (long_long_integer_type_node)
6228 == TYPE_PRECISION (long_integer_type_node))
6231 (lfn:long_integer_type_node @0)))))
6233 /* cproj(x) -> x if we're ignoring infinities. */
6236 (if (!HONOR_INFINITIES (type))
6239 /* If the real part is inf and the imag part is known to be
6240 nonnegative, return (inf + 0i). */
6242 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6243 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6244 { build_complex_inf (type, false); }))
6246 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6248 (CPROJ (complex @0 REAL_CST@1))
6249 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6250 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6256 (pows @0 REAL_CST@1)
6258 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6259 REAL_VALUE_TYPE tmp;
6262 /* pow(x,0) -> 1. */
6263 (if (real_equal (value, &dconst0))
6264 { build_real (type, dconst1); })
6265 /* pow(x,1) -> x. */
6266 (if (real_equal (value, &dconst1))
6268 /* pow(x,-1) -> 1/x. */
6269 (if (real_equal (value, &dconstm1))
6270 (rdiv { build_real (type, dconst1); } @0))
6271 /* pow(x,0.5) -> sqrt(x). */
6272 (if (flag_unsafe_math_optimizations
6273 && canonicalize_math_p ()
6274 && real_equal (value, &dconsthalf))
6276 /* pow(x,1/3) -> cbrt(x). */
6277 (if (flag_unsafe_math_optimizations
6278 && canonicalize_math_p ()
6279 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6280 real_equal (value, &tmp)))
6283 /* powi(1,x) -> 1. */
6285 (POWI real_onep@0 @1)
6289 (POWI @0 INTEGER_CST@1)
6291 /* powi(x,0) -> 1. */
6292 (if (wi::to_wide (@1) == 0)
6293 { build_real (type, dconst1); })
6294 /* powi(x,1) -> x. */
6295 (if (wi::to_wide (@1) == 1)
6297 /* powi(x,-1) -> 1/x. */
6298 (if (wi::to_wide (@1) == -1)
6299 (rdiv { build_real (type, dconst1); } @0))))
6301 /* Narrowing of arithmetic and logical operations.
6303 These are conceptually similar to the transformations performed for
6304 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6305 term we want to move all that code out of the front-ends into here. */
6307 /* Convert (outertype)((innertype0)a+(innertype1)b)
6308 into ((newtype)a+(newtype)b) where newtype
6309 is the widest mode from all of these. */
6310 (for op (plus minus mult rdiv)
6312 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6313 /* If we have a narrowing conversion of an arithmetic operation where
6314 both operands are widening conversions from the same type as the outer
6315 narrowing conversion. Then convert the innermost operands to a
6316 suitable unsigned type (to avoid introducing undefined behavior),
6317 perform the operation and convert the result to the desired type. */
6318 (if (INTEGRAL_TYPE_P (type)
6321 /* We check for type compatibility between @0 and @1 below,
6322 so there's no need to check that @2/@4 are integral types. */
6323 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6324 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6325 /* The precision of the type of each operand must match the
6326 precision of the mode of each operand, similarly for the
6328 && type_has_mode_precision_p (TREE_TYPE (@1))
6329 && type_has_mode_precision_p (TREE_TYPE (@2))
6330 && type_has_mode_precision_p (type)
6331 /* The inner conversion must be a widening conversion. */
6332 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
6333 && types_match (@1, type)
6334 && (types_match (@1, @2)
6335 /* Or the second operand is const integer or converted const
6336 integer from valueize. */
6337 || poly_int_tree_p (@4)))
6338 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
6339 (op @1 (convert @2))
6340 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6341 (convert (op (convert:utype @1)
6342 (convert:utype @2)))))
6343 (if (FLOAT_TYPE_P (type)
6344 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6345 == DECIMAL_FLOAT_TYPE_P (type))
6346 (with { tree arg0 = strip_float_extensions (@1);
6347 tree arg1 = strip_float_extensions (@2);
6348 tree itype = TREE_TYPE (@0);
6349 tree ty1 = TREE_TYPE (arg0);
6350 tree ty2 = TREE_TYPE (arg1);
6351 enum tree_code code = TREE_CODE (itype); }
6352 (if (FLOAT_TYPE_P (ty1)
6353 && FLOAT_TYPE_P (ty2))
6354 (with { tree newtype = type;
6355 if (TYPE_MODE (ty1) == SDmode
6356 || TYPE_MODE (ty2) == SDmode
6357 || TYPE_MODE (type) == SDmode)
6358 newtype = dfloat32_type_node;
6359 if (TYPE_MODE (ty1) == DDmode
6360 || TYPE_MODE (ty2) == DDmode
6361 || TYPE_MODE (type) == DDmode)
6362 newtype = dfloat64_type_node;
6363 if (TYPE_MODE (ty1) == TDmode
6364 || TYPE_MODE (ty2) == TDmode
6365 || TYPE_MODE (type) == TDmode)
6366 newtype = dfloat128_type_node; }
6367 (if ((newtype == dfloat32_type_node
6368 || newtype == dfloat64_type_node
6369 || newtype == dfloat128_type_node)
6371 && types_match (newtype, type))
6372 (op (convert:newtype @1) (convert:newtype @2))
6373 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
6375 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
6377 /* Sometimes this transformation is safe (cannot
6378 change results through affecting double rounding
6379 cases) and sometimes it is not. If NEWTYPE is
6380 wider than TYPE, e.g. (float)((long double)double
6381 + (long double)double) converted to
6382 (float)(double + double), the transformation is
6383 unsafe regardless of the details of the types
6384 involved; double rounding can arise if the result
6385 of NEWTYPE arithmetic is a NEWTYPE value half way
6386 between two representable TYPE values but the
6387 exact value is sufficiently different (in the
6388 right direction) for this difference to be
6389 visible in ITYPE arithmetic. If NEWTYPE is the
6390 same as TYPE, however, the transformation may be
6391 safe depending on the types involved: it is safe
6392 if the ITYPE has strictly more than twice as many
6393 mantissa bits as TYPE, can represent infinities
6394 and NaNs if the TYPE can, and has sufficient
6395 exponent range for the product or ratio of two
6396 values representable in the TYPE to be within the
6397 range of normal values of ITYPE. */
6398 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6399 && (flag_unsafe_math_optimizations
6400 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6401 && real_can_shorten_arithmetic (TYPE_MODE (itype),
6403 && !excess_precision_type (newtype)))
6404 && !types_match (itype, newtype))
6405 (convert:type (op (convert:newtype @1)
6406 (convert:newtype @2)))
6411 /* This is another case of narrowing, specifically when there's an outer
6412 BIT_AND_EXPR which masks off bits outside the type of the innermost
6413 operands. Like the previous case we have to convert the operands
6414 to unsigned types to avoid introducing undefined behavior for the
6415 arithmetic operation. */
6416 (for op (minus plus)
6418 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6419 (if (INTEGRAL_TYPE_P (type)
6420 /* We check for type compatibility between @0 and @1 below,
6421 so there's no need to check that @1/@3 are integral types. */
6422 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6423 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6424 /* The precision of the type of each operand must match the
6425 precision of the mode of each operand, similarly for the
6427 && type_has_mode_precision_p (TREE_TYPE (@0))
6428 && type_has_mode_precision_p (TREE_TYPE (@1))
6429 && type_has_mode_precision_p (type)
6430 /* The inner conversion must be a widening conversion. */
6431 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6432 && types_match (@0, @1)
6433 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6434 <= TYPE_PRECISION (TREE_TYPE (@0)))
6435 && (wi::to_wide (@4)
6436 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6437 true, TYPE_PRECISION (type))) == 0)
6438 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6439 (with { tree ntype = TREE_TYPE (@0); }
6440 (convert (bit_and (op @0 @1) (convert:ntype @4))))
6441 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6442 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6443 (convert:utype @4))))))))
6445 /* Transform (@0 < @1 and @0 < @2) to use min,
6446 (@0 > @1 and @0 > @2) to use max */
6447 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6448 op (lt le gt ge lt le gt ge )
6449 ext (min min max max max max min min )
6451 (logic (op:cs @0 @1) (op:cs @0 @2))
6452 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6453 && TREE_CODE (@0) != INTEGER_CST)
6454 (op @0 (ext @1 @2)))))
6457 /* signbit(x) -> 0 if x is nonnegative. */
6458 (SIGNBIT tree_expr_nonnegative_p@0)
6459 { integer_zero_node; })
6462 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6464 (if (!HONOR_SIGNED_ZEROS (@0))
6465 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6467 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6469 (for op (plus minus)
6472 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6473 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6474 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6475 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6476 && !TYPE_SATURATING (TREE_TYPE (@0)))
6477 (with { tree res = int_const_binop (rop, @2, @1); }
6478 (if (TREE_OVERFLOW (res)
6479 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6480 { constant_boolean_node (cmp == NE_EXPR, type); }
6481 (if (single_use (@3))
6482 (cmp @0 { TREE_OVERFLOW (res)
6483 ? drop_tree_overflow (res) : res; }))))))))
6484 (for cmp (lt le gt ge)
6485 (for op (plus minus)
6488 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6489 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6490 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6491 (with { tree res = int_const_binop (rop, @2, @1); }
6492 (if (TREE_OVERFLOW (res))
6494 fold_overflow_warning (("assuming signed overflow does not occur "
6495 "when simplifying conditional to constant"),
6496 WARN_STRICT_OVERFLOW_CONDITIONAL);
6497 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6498 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6499 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6500 TYPE_SIGN (TREE_TYPE (@1)))
6501 != (op == MINUS_EXPR);
6502 constant_boolean_node (less == ovf_high, type);
6504 (if (single_use (@3))
6507 fold_overflow_warning (("assuming signed overflow does not occur "
6508 "when changing X +- C1 cmp C2 to "
6510 WARN_STRICT_OVERFLOW_COMPARISON);
6512 (cmp @0 { res; })))))))))
6514 /* Canonicalizations of BIT_FIELD_REFs. */
6517 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6518 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6521 (BIT_FIELD_REF (view_convert @0) @1 @2)
6522 (BIT_FIELD_REF @0 @1 @2))
6525 (BIT_FIELD_REF @0 @1 integer_zerop)
6526 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6530 (BIT_FIELD_REF @0 @1 @2)
6532 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6533 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6535 (if (integer_zerop (@2))
6536 (view_convert (realpart @0)))
6537 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6538 (view_convert (imagpart @0)))))
6539 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6540 && INTEGRAL_TYPE_P (type)
6541 /* On GIMPLE this should only apply to register arguments. */
6542 && (! GIMPLE || is_gimple_reg (@0))
6543 /* A bit-field-ref that referenced the full argument can be stripped. */
6544 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6545 && integer_zerop (@2))
6546 /* Low-parts can be reduced to integral conversions.
6547 ??? The following doesn't work for PDP endian. */
6548 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6549 /* But only do this after vectorization. */
6550 && canonicalize_math_after_vectorization_p ()
6551 /* Don't even think about BITS_BIG_ENDIAN. */
6552 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6553 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6554 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6555 ? (TYPE_PRECISION (TREE_TYPE (@0))
6556 - TYPE_PRECISION (type))
6560 /* Simplify vector extracts. */
6563 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6564 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6565 && tree_fits_uhwi_p (TYPE_SIZE (type))
6566 && ((tree_to_uhwi (TYPE_SIZE (type))
6567 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6568 || (VECTOR_TYPE_P (type)
6569 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
6570 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
6573 tree ctor = (TREE_CODE (@0) == SSA_NAME
6574 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6575 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6576 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6577 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6578 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6581 && (idx % width) == 0
6583 && known_le ((idx + n) / width,
6584 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6589 /* Constructor elements can be subvectors. */
6591 if (CONSTRUCTOR_NELTS (ctor) != 0)
6593 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6594 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6595 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6597 unsigned HOST_WIDE_INT elt, count, const_k;
6600 /* We keep an exact subset of the constructor elements. */
6601 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6602 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6603 { build_zero_cst (type); }
6605 (if (elt < CONSTRUCTOR_NELTS (ctor))
6606 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6607 { build_zero_cst (type); })
6608 /* We don't want to emit new CTORs unless the old one goes away.
6609 ??? Eventually allow this if the CTOR ends up constant or
6611 (if (single_use (@0))
6614 vec<constructor_elt, va_gc> *vals;
6615 vec_alloc (vals, count);
6616 bool constant_p = true;
6618 for (unsigned i = 0;
6619 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6621 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
6622 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
6623 if (!CONSTANT_CLASS_P (e))
6626 tree evtype = (types_match (TREE_TYPE (type),
6627 TREE_TYPE (TREE_TYPE (ctor)))
6629 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
6631 res = (constant_p ? build_vector_from_ctor (evtype, vals)
6632 : build_constructor (evtype, vals));
6634 (view_convert { res; }))))))
6635 /* The bitfield references a single constructor element. */
6636 (if (k.is_constant (&const_k)
6637 && idx + n <= (idx / const_k + 1) * const_k)
6639 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6640 { build_zero_cst (type); })
6642 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6643 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6644 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6646 /* Simplify a bit extraction from a bit insertion for the cases with
6647 the inserted element fully covering the extraction or the insertion
6648 not touching the extraction. */
6650 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6653 unsigned HOST_WIDE_INT isize;
6654 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6655 isize = TYPE_PRECISION (TREE_TYPE (@1));
6657 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6660 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6661 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6662 wi::to_wide (@ipos) + isize))
6663 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6665 - wi::to_wide (@ipos)); }))
6666 (if (wi::geu_p (wi::to_wide (@ipos),
6667 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6668 || wi::geu_p (wi::to_wide (@rpos),
6669 wi::to_wide (@ipos) + isize))
6670 (BIT_FIELD_REF @0 @rsize @rpos)))))
6672 (if (canonicalize_math_after_vectorization_p ())
6675 (fmas:c (negate @0) @1 @2)
6676 (IFN_FNMA @0 @1 @2))
6678 (fmas @0 @1 (negate @2))
6681 (fmas:c (negate @0) @1 (negate @2))
6682 (IFN_FNMS @0 @1 @2))
6684 (negate (fmas@3 @0 @1 @2))
6685 (if (single_use (@3))
6686 (IFN_FNMS @0 @1 @2))))
6689 (IFN_FMS:c (negate @0) @1 @2)
6690 (IFN_FNMS @0 @1 @2))
6692 (IFN_FMS @0 @1 (negate @2))
6695 (IFN_FMS:c (negate @0) @1 (negate @2))
6696 (IFN_FNMA @0 @1 @2))
6698 (negate (IFN_FMS@3 @0 @1 @2))
6699 (if (single_use (@3))
6700 (IFN_FNMA @0 @1 @2)))
6703 (IFN_FNMA:c (negate @0) @1 @2)
6706 (IFN_FNMA @0 @1 (negate @2))
6707 (IFN_FNMS @0 @1 @2))
6709 (IFN_FNMA:c (negate @0) @1 (negate @2))
6712 (negate (IFN_FNMA@3 @0 @1 @2))
6713 (if (single_use (@3))
6714 (IFN_FMS @0 @1 @2)))
6717 (IFN_FNMS:c (negate @0) @1 @2)
6720 (IFN_FNMS @0 @1 (negate @2))
6721 (IFN_FNMA @0 @1 @2))
6723 (IFN_FNMS:c (negate @0) @1 (negate @2))
6726 (negate (IFN_FNMS@3 @0 @1 @2))
6727 (if (single_use (@3))
6728 (IFN_FMA @0 @1 @2))))
6730 /* CLZ simplifications. */
6735 (op (clz:s@2 @0) INTEGER_CST@1)
6736 (if (integer_zerop (@1) && single_use (@2))
6737 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6738 (with { tree type0 = TREE_TYPE (@0);
6739 tree stype = signed_type_for (type0);
6740 HOST_WIDE_INT val = 0;
6741 /* Punt on hypothetical weird targets. */
6743 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6749 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6750 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
6751 (with { bool ok = true;
6752 HOST_WIDE_INT val = 0;
6753 tree type0 = TREE_TYPE (@0);
6754 /* Punt on hypothetical weird targets. */
6756 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6758 && val == TYPE_PRECISION (type0) - 1)
6761 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
6762 (op @0 { build_one_cst (type0); })))))))
6764 /* CTZ simplifications. */
6766 (for op (ge gt le lt)
6769 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
6770 (op (ctz:s @0) INTEGER_CST@1)
6771 (with { bool ok = true;
6772 HOST_WIDE_INT val = 0;
6773 if (!tree_fits_shwi_p (@1))
6777 val = tree_to_shwi (@1);
6778 /* Canonicalize to >= or <. */
6779 if (op == GT_EXPR || op == LE_EXPR)
6781 if (val == HOST_WIDE_INT_MAX)
6787 bool zero_res = false;
6788 HOST_WIDE_INT zero_val = 0;
6789 tree type0 = TREE_TYPE (@0);
6790 int prec = TYPE_PRECISION (type0);
6792 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6797 (if (ok && (!zero_res || zero_val >= val))
6798 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
6800 (if (ok && (!zero_res || zero_val < val))
6801 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
6802 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
6803 (cmp (bit_and @0 { wide_int_to_tree (type0,
6804 wi::mask (val, false, prec)); })
6805 { build_zero_cst (type0); })))))))
6808 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
6809 (op (ctz:s @0) INTEGER_CST@1)
6810 (with { bool zero_res = false;
6811 HOST_WIDE_INT zero_val = 0;
6812 tree type0 = TREE_TYPE (@0);
6813 int prec = TYPE_PRECISION (type0);
6815 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6819 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
6820 (if (!zero_res || zero_val != wi::to_widest (@1))
6821 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
6822 (if (!zero_res || zero_val < 0 || zero_val >= prec)
6823 (op (bit_and @0 { wide_int_to_tree (type0,
6824 wi::mask (tree_to_uhwi (@1) + 1,
6826 { wide_int_to_tree (type0,
6827 wi::shifted_mask (tree_to_uhwi (@1), 1,
6828 false, prec)); })))))))
6830 /* POPCOUNT simplifications. */
6831 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6833 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6834 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6835 (POPCOUNT (bit_ior @0 @1))))
6837 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6838 (for popcount (POPCOUNT)
6839 (for cmp (le eq ne gt)
6842 (cmp (popcount @0) integer_zerop)
6843 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6845 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6847 (bit_and (POPCOUNT @0) integer_onep)
6850 /* PARITY simplifications. */
6851 /* parity(~X) is parity(X). */
6853 (PARITY (bit_not @0))
6856 /* parity(X)^parity(Y) is parity(X^Y). */
6858 (bit_xor (PARITY:s @0) (PARITY:s @1))
6859 (PARITY (bit_xor @0 @1)))
6861 /* Common POPCOUNT/PARITY simplifications. */
6862 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6863 (for pfun (POPCOUNT PARITY)
6866 (with { wide_int nz = tree_nonzero_bits (@0); }
6870 (if (wi::popcount (nz) == 1)
6871 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6872 (convert (rshift:utype (convert:utype @0)
6873 { build_int_cst (integer_type_node,
6874 wi::ctz (nz)); }))))))))
6877 /* 64- and 32-bits branchless implementations of popcount are detected:
6879 int popcount64c (uint64_t x)
6881 x -= (x >> 1) & 0x5555555555555555ULL;
6882 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6883 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6884 return (x * 0x0101010101010101ULL) >> 56;
6887 int popcount32c (uint32_t x)
6889 x -= (x >> 1) & 0x55555555;
6890 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6891 x = (x + (x >> 4)) & 0x0f0f0f0f;
6892 return (x * 0x01010101) >> 24;
6899 (rshift @8 INTEGER_CST@5)
6901 (bit_and @6 INTEGER_CST@7)
6905 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6911 /* Check constants and optab. */
6912 (with { unsigned prec = TYPE_PRECISION (type);
6913 int shift = (64 - prec) & 63;
6914 unsigned HOST_WIDE_INT c1
6915 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6916 unsigned HOST_WIDE_INT c2
6917 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6918 unsigned HOST_WIDE_INT c3
6919 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6920 unsigned HOST_WIDE_INT c4
6921 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6926 && TYPE_UNSIGNED (type)
6927 && integer_onep (@4)
6928 && wi::to_widest (@10) == 2
6929 && wi::to_widest (@5) == 4
6930 && wi::to_widest (@1) == prec - 8
6931 && tree_to_uhwi (@2) == c1
6932 && tree_to_uhwi (@3) == c2
6933 && tree_to_uhwi (@9) == c3
6934 && tree_to_uhwi (@7) == c3
6935 && tree_to_uhwi (@11) == c4)
6936 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6938 (convert (IFN_POPCOUNT:type @0))
6939 /* Try to do popcount in two halves. PREC must be at least
6940 five bits for this to work without extension before adding. */
6942 tree half_type = NULL_TREE;
6943 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
6946 && m.require () != TYPE_MODE (type))
6948 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
6949 half_type = build_nonstandard_integer_type (half_prec, 1);
6951 gcc_assert (half_prec > 2);
6953 (if (half_type != NULL_TREE
6954 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
6957 (IFN_POPCOUNT:half_type (convert @0))
6958 (IFN_POPCOUNT:half_type (convert (rshift @0
6959 { build_int_cst (integer_type_node, half_prec); } )))))))))))
6961 /* __builtin_ffs needs to deal on many targets with the possible zero
6962 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6963 should lead to better code. */
6965 (FFS tree_expr_nonzero_p@0)
6966 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6967 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6968 OPTIMIZE_FOR_SPEED))
6969 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6970 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6973 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6975 /* __builtin_ffs (X) == 0 -> X == 0.
6976 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6979 (cmp (ffs@2 @0) INTEGER_CST@1)
6980 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6982 (if (integer_zerop (@1))
6983 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6984 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6985 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6986 (if (single_use (@2))
6987 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6988 wi::mask (tree_to_uhwi (@1),
6990 { wide_int_to_tree (TREE_TYPE (@0),
6991 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6992 false, prec)); }))))))
6994 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
6998 bit_op (bit_and bit_ior)
7000 (cmp (ffs@2 @0) INTEGER_CST@1)
7001 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7003 (if (integer_zerop (@1))
7004 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7005 (if (tree_int_cst_sgn (@1) < 0)
7006 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7007 (if (wi::to_widest (@1) >= prec)
7008 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7009 (if (wi::to_widest (@1) == prec - 1)
7010 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7011 wi::shifted_mask (prec - 1, 1,
7013 (if (single_use (@2))
7014 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7016 { wide_int_to_tree (TREE_TYPE (@0),
7017 wi::mask (tree_to_uhwi (@1),
7019 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7026 --> r = .COND_FN (cond, a, b)
7030 --> r = .COND_FN (~cond, b, a). */
7032 (for uncond_op (UNCOND_UNARY)
7033 cond_op (COND_UNARY)
7035 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
7036 (with { tree op_type = TREE_TYPE (@3); }
7037 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7038 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7039 (cond_op @0 @1 @2))))
7041 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
7042 (with { tree op_type = TREE_TYPE (@3); }
7043 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7044 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7045 (cond_op (bit_not @0) @2 @1)))))
7054 r = c ? a1 op a2 : b;
7056 if the target can do it in one go. This makes the operation conditional
7057 on c, so could drop potentially-trapping arithmetic, but that's a valid
7058 simplification if the result of the operation isn't needed.
7060 Avoid speculatively generating a stand-alone vector comparison
7061 on targets that might not support them. Any target implementing
7062 conditional internal functions must support the same comparisons
7063 inside and outside a VEC_COND_EXPR. */
7065 (for uncond_op (UNCOND_BINARY)
7066 cond_op (COND_BINARY)
7068 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7069 (with { tree op_type = TREE_TYPE (@4); }
7070 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7071 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7072 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7074 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7075 (with { tree op_type = TREE_TYPE (@4); }
7076 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7077 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7078 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7080 /* Same for ternary operations. */
7081 (for uncond_op (UNCOND_TERNARY)
7082 cond_op (COND_TERNARY)
7084 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7085 (with { tree op_type = TREE_TYPE (@5); }
7086 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7087 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7088 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7090 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7091 (with { tree op_type = TREE_TYPE (@5); }
7092 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7093 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7094 (view_convert (cond_op (bit_not @0) @2 @3 @4
7095 (view_convert:op_type @1)))))))
7098 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7099 "else" value of an IFN_COND_*. */
7100 (for cond_op (COND_BINARY)
7102 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7103 (with { tree op_type = TREE_TYPE (@3); }
7104 (if (element_precision (type) == element_precision (op_type))
7105 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7107 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7108 (with { tree op_type = TREE_TYPE (@5); }
7109 (if (inverse_conditions_p (@0, @2)
7110 && element_precision (type) == element_precision (op_type))
7111 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7113 /* Same for ternary operations. */
7114 (for cond_op (COND_TERNARY)
7116 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7117 (with { tree op_type = TREE_TYPE (@4); }
7118 (if (element_precision (type) == element_precision (op_type))
7119 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7121 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7122 (with { tree op_type = TREE_TYPE (@6); }
7123 (if (inverse_conditions_p (@0, @2)
7124 && element_precision (type) == element_precision (op_type))
7125 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7127 /* Detect simplication for a conditional reduction where
7130 c = mask2 ? d + a : d
7134 c = mask1 && mask2 ? d + b : d. */
7136 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
7137 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
7139 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7142 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7143 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7145 If pointers are known not to wrap, B checks whether @1 bytes starting
7146 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7147 bytes. A is more efficiently tested as:
7149 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7151 The equivalent expression for B is given by replacing @1 with @1 - 1:
7153 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7155 @0 and @2 can be swapped in both expressions without changing the result.
7157 The folds rely on sizetype's being unsigned (which is always true)
7158 and on its being the same width as the pointer (which we have to check).
7160 The fold replaces two pointer_plus expressions, two comparisons and
7161 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7162 the best case it's a saving of two operations. The A fold retains one
7163 of the original pointer_pluses, so is a win even if both pointer_pluses
7164 are used elsewhere. The B fold is a wash if both pointer_pluses are
7165 used elsewhere, since all we end up doing is replacing a comparison with
7166 a pointer_plus. We do still apply the fold under those circumstances
7167 though, in case applying it to other conditions eventually makes one of the
7168 pointer_pluses dead. */
7169 (for ior (truth_orif truth_or bit_ior)
7172 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7173 (cmp:cs (pointer_plus@4 @2 @1) @0))
7174 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7175 && TYPE_OVERFLOW_WRAPS (sizetype)
7176 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7177 /* Calculate the rhs constant. */
7178 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7179 offset_int rhs = off * 2; }
7180 /* Always fails for negative values. */
7181 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7182 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7183 pick a canonical order. This increases the chances of using the
7184 same pointer_plus in multiple checks. */
7185 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7186 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7187 (if (cmp == LT_EXPR)
7188 (gt (convert:sizetype
7189 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7190 { swap_p ? @0 : @2; }))
7192 (gt (convert:sizetype
7193 (pointer_diff:ssizetype
7194 (pointer_plus { swap_p ? @2 : @0; }
7195 { wide_int_to_tree (sizetype, off); })
7196 { swap_p ? @0 : @2; }))
7197 { rhs_tree; })))))))))
7199 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7201 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7202 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7203 (with { int i = single_nonzero_element (@1); }
7205 (with { tree elt = vector_cst_elt (@1, i);
7206 tree elt_type = TREE_TYPE (elt);
7207 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7208 tree size = bitsize_int (elt_bits);
7209 tree pos = bitsize_int (elt_bits * i); }
7212 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7216 (vec_perm @0 @1 VECTOR_CST@2)
7219 tree op0 = @0, op1 = @1, op2 = @2;
7221 /* Build a vector of integers from the tree mask. */
7222 vec_perm_builder builder;
7223 if (!tree_to_vec_perm_builder (&builder, op2))
7226 /* Create a vec_perm_indices for the integer vector. */
7227 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7228 bool single_arg = (op0 == op1);
7229 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7231 (if (sel.series_p (0, 1, 0, 1))
7233 (if (sel.series_p (0, 1, nelts, 1))
7239 if (sel.all_from_input_p (0))
7241 else if (sel.all_from_input_p (1))
7244 sel.rotate_inputs (1);
7246 else if (known_ge (poly_uint64 (sel[0]), nelts))
7248 std::swap (op0, op1);
7249 sel.rotate_inputs (1);
7253 tree cop0 = op0, cop1 = op1;
7254 if (TREE_CODE (op0) == SSA_NAME
7255 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7256 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7257 cop0 = gimple_assign_rhs1 (def);
7258 if (TREE_CODE (op1) == SSA_NAME
7259 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7260 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7261 cop1 = gimple_assign_rhs1 (def);
7265 (if ((TREE_CODE (cop0) == VECTOR_CST
7266 || TREE_CODE (cop0) == CONSTRUCTOR)
7267 && (TREE_CODE (cop1) == VECTOR_CST
7268 || TREE_CODE (cop1) == CONSTRUCTOR)
7269 && (t = fold_vec_perm (type, cop0, cop1, sel)))
7273 bool changed = (op0 == op1 && !single_arg);
7274 tree ins = NULL_TREE;
7277 /* See if the permutation is performing a single element
7278 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7279 in that case. But only if the vector mode is supported,
7280 otherwise this is invalid GIMPLE. */
7281 if (TYPE_MODE (type) != BLKmode
7282 && (TREE_CODE (cop0) == VECTOR_CST
7283 || TREE_CODE (cop0) == CONSTRUCTOR
7284 || TREE_CODE (cop1) == VECTOR_CST
7285 || TREE_CODE (cop1) == CONSTRUCTOR))
7287 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7290 /* After canonicalizing the first elt to come from the
7291 first vector we only can insert the first elt from
7292 the first vector. */
7294 if ((ins = fold_read_from_vector (cop0, sel[0])))
7297 /* The above can fail for two-element vectors which always
7298 appear to insert the first element, so try inserting
7299 into the second lane as well. For more than two
7300 elements that's wasted time. */
7301 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
7303 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
7304 for (at = 0; at < encoded_nelts; ++at)
7305 if (maybe_ne (sel[at], at))
7307 if (at < encoded_nelts
7308 && (known_eq (at + 1, nelts)
7309 || sel.series_p (at + 1, 1, at + 1, 1)))
7311 if (known_lt (poly_uint64 (sel[at]), nelts))
7312 ins = fold_read_from_vector (cop0, sel[at]);
7314 ins = fold_read_from_vector (cop1, sel[at] - nelts);
7319 /* Generate a canonical form of the selector. */
7320 if (!ins && sel.encoding () != builder)
7322 /* Some targets are deficient and fail to expand a single
7323 argument permutation while still allowing an equivalent
7324 2-argument version. */
7326 if (sel.ninputs () == 2
7327 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
7328 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7331 vec_perm_indices sel2 (builder, 2, nelts);
7332 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
7333 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
7335 /* Not directly supported with either encoding,
7336 so use the preferred form. */
7337 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7339 if (!operand_equal_p (op2, oldop2, 0))
7344 (bit_insert { op0; } { ins; }
7345 { bitsize_int (at * vector_element_bits (type)); })
7347 (vec_perm { op0; } { op1; } { op2; }))))))))))
7349 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
7351 (match vec_same_elem_p
7353 (if (uniform_vector_p (@0))))
7355 (match vec_same_elem_p
7359 (vec_perm vec_same_elem_p@0 @0 @1)
7362 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
7363 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
7364 constant which when multiplied by a power of 2 contains a unique value
7365 in the top 5 or 6 bits. This is then indexed into a table which maps it
7366 to the number of trailing zeroes. */
7367 (match (ctz_table_index @1 @2 @3)
7368 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))