1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2021 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
43 (define_operator_list tcc_comparison
44 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
45 (define_operator_list inverted_tcc_comparison
46 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
47 (define_operator_list inverted_tcc_comparison_with_nans
48 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list swapped_tcc_comparison
50 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
51 (define_operator_list simple_comparison lt le eq ne ge gt)
52 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
54 #include "cfn-operators.pd"
56 /* Define operand lists for math rounding functions {,i,l,ll}FN,
57 where the versions prefixed with "i" return an int, those prefixed with
58 "l" return a long and those prefixed with "ll" return a long long.
60 Also define operand lists:
62 X<FN>F for all float functions, in the order i, l, ll
63 X<FN> for all double functions, in the same order
64 X<FN>L for all long double functions, in the same order. */
65 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
66 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
69 (define_operator_list X##FN BUILT_IN_I##FN \
72 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
76 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
81 /* Binary operations and their associated IFN_COND_* function. */
82 (define_operator_list UNCOND_BINARY
84 mult trunc_div trunc_mod rdiv
86 bit_and bit_ior bit_xor
88 (define_operator_list COND_BINARY
89 IFN_COND_ADD IFN_COND_SUB
90 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
91 IFN_COND_MIN IFN_COND_MAX
92 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
93 IFN_COND_SHL IFN_COND_SHR)
95 /* Same for ternary operations. */
96 (define_operator_list UNCOND_TERNARY
97 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
98 (define_operator_list COND_TERNARY
99 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
101 /* With nop_convert? combine convert? and view_convert? in one pattern
102 plus conditionalize on tree_nop_conversion_p conversions. */
103 (match (nop_convert @0)
105 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
106 (match (nop_convert @0)
108 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
109 && known_eq (TYPE_VECTOR_SUBPARTS (type),
110 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
111 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114 ABSU_EXPR returns unsigned absolute value of the operand and the operand
115 of the ABSU_EXPR will have the corresponding signed type. */
116 (simplify (abs (convert @0))
117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118 && !TYPE_UNSIGNED (TREE_TYPE (@0))
119 && element_precision (type) > element_precision (TREE_TYPE (@0)))
120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121 (convert (absu:utype @0)))))
124 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
126 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
127 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
128 && !TYPE_UNSIGNED (TREE_TYPE (@0))
129 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
133 /* Simplifications of operations with one constant operand and
134 simplifications to constants or single values. */
136 (for op (plus pointer_plus minus bit_ior bit_xor)
138 (op @0 integer_zerop)
141 /* 0 +p index -> (type)index */
143 (pointer_plus integer_zerop @1)
144 (non_lvalue (convert @1)))
146 /* ptr - 0 -> (type)ptr */
148 (pointer_diff @0 integer_zerop)
151 /* See if ARG1 is zero and X + ARG1 reduces to X.
152 Likewise if the operands are reversed. */
154 (plus:c @0 real_zerop@1)
155 (if (fold_real_zero_addition_p (type, @1, 0))
158 /* See if ARG1 is zero and X - ARG1 reduces to X. */
160 (minus @0 real_zerop@1)
161 (if (fold_real_zero_addition_p (type, @1, 1))
164 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
165 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
166 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
167 if not -frounding-math. For sNaNs the first operation would raise
168 exceptions but turn the result into qNan, so the second operation
169 would not raise it. */
170 (for inner_op (plus minus)
171 (for outer_op (plus minus)
173 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
176 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
177 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
178 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
180 = ((outer_op == PLUS_EXPR)
181 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
182 (if (outer_plus && !inner_plus)
187 This is unsafe for certain floats even in non-IEEE formats.
188 In IEEE, it is unsafe because it does wrong for NaNs.
189 Also note that operand_equal_p is always false if an operand
193 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
194 { build_zero_cst (type); }))
196 (pointer_diff @@0 @0)
197 { build_zero_cst (type); })
200 (mult @0 integer_zerop@1)
203 /* Maybe fold x * 0 to 0. The expressions aren't the same
204 when x is NaN, since x * 0 is also NaN. Nor are they the
205 same in modes with signed zeros, since multiplying a
206 negative value by 0 gives -0, not +0. */
208 (mult @0 real_zerop@1)
209 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
212 /* In IEEE floating point, x*1 is not equivalent to x for snans.
213 Likewise for complex arithmetic with signed zeros. */
216 (if (!HONOR_SNANS (type)
217 && (!HONOR_SIGNED_ZEROS (type)
218 || !COMPLEX_FLOAT_TYPE_P (type)))
221 /* Transform x * -1.0 into -x. */
223 (mult @0 real_minus_onep)
224 (if (!HONOR_SNANS (type)
225 && (!HONOR_SIGNED_ZEROS (type)
226 || !COMPLEX_FLOAT_TYPE_P (type)))
229 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
231 (mult SSA_NAME@1 SSA_NAME@2)
232 (if (INTEGRAL_TYPE_P (type)
233 && get_nonzero_bits (@1) == 1
234 && get_nonzero_bits (@2) == 1)
237 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
238 unless the target has native support for the former but not the latter. */
240 (mult @0 VECTOR_CST@1)
241 (if (initializer_each_zero_or_onep (@1)
242 && !HONOR_SNANS (type)
243 && !HONOR_SIGNED_ZEROS (type))
244 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
246 && (!VECTOR_MODE_P (TYPE_MODE (type))
247 || (VECTOR_MODE_P (TYPE_MODE (itype))
248 && optab_handler (and_optab,
249 TYPE_MODE (itype)) != CODE_FOR_nothing)))
250 (view_convert (bit_and:itype (view_convert @0)
251 (ne @1 { build_zero_cst (type); })))))))
253 (for cmp (gt ge lt le)
254 outp (convert convert negate negate)
255 outn (negate negate convert convert)
256 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
257 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
258 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
259 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
261 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
262 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
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_minus_onep real_onep@1))
270 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
273 /* Transform X * copysign (1.0, X) into abs(X). */
275 (mult:c @0 (COPYSIGN_ALL real_onep @0))
276 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
279 /* Transform X * copysign (1.0, -X) into -abs(X). */
281 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
282 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
285 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
287 (COPYSIGN_ALL REAL_CST@0 @1)
288 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
289 (COPYSIGN_ALL (negate @0) @1)))
291 /* X * 1, X / 1 -> X. */
292 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
297 /* (A / (1 << B)) -> (A >> B).
298 Only for unsigned A. For signed A, this would not preserve rounding
300 For example: (-1 / ( 1 << B)) != -1 >> B.
301 Also also widening conversions, like:
302 (A / (unsigned long long) (1U << B)) -> (A >> B)
304 (A / (unsigned long long) (1 << B)) -> (A >> B).
305 If the left shift is signed, it can be done only if the upper bits
306 of A starting from shift's type sign bit are zero, as
307 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
308 so it is valid only if A >> 31 is zero. */
310 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
311 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
312 && (!VECTOR_TYPE_P (type)
313 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
314 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
315 && (useless_type_conversion_p (type, TREE_TYPE (@1))
316 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
317 && (TYPE_UNSIGNED (TREE_TYPE (@1))
318 || (element_precision (type)
319 == element_precision (TREE_TYPE (@1)))
320 || (INTEGRAL_TYPE_P (type)
321 && (tree_nonzero_bits (@0)
322 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
324 element_precision (type))) == 0)))))
325 (if (!VECTOR_TYPE_P (type)
326 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
327 && element_precision (TREE_TYPE (@3)) < element_precision (type))
328 (convert (rshift @3 @2))
331 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
332 undefined behavior in constexpr evaluation, and assuming that the division
333 traps enables better optimizations than these anyway. */
334 (for div (trunc_div ceil_div floor_div round_div exact_div)
335 /* 0 / X is always zero. */
337 (div integer_zerop@0 @1)
338 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
339 (if (!integer_zerop (@1))
343 (div @0 integer_minus_onep@1)
344 (if (!TYPE_UNSIGNED (type))
346 /* X / bool_range_Y is X. */
349 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
354 /* But not for 0 / 0 so that we can get the proper warnings and errors.
355 And not for _Fract types where we can't build 1. */
356 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
357 { build_one_cst (type); }))
358 /* X / abs (X) is X < 0 ? -1 : 1. */
361 (if (INTEGRAL_TYPE_P (type)
362 && TYPE_OVERFLOW_UNDEFINED (type))
363 (cond (lt @0 { build_zero_cst (type); })
364 { build_minus_one_cst (type); } { build_one_cst (type); })))
367 (div:C @0 (negate @0))
368 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
369 && TYPE_OVERFLOW_UNDEFINED (type))
370 { build_minus_one_cst (type); })))
372 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
373 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
376 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
377 && TYPE_UNSIGNED (type))
380 /* Combine two successive divisions. Note that combining ceil_div
381 and floor_div is trickier and combining round_div even more so. */
382 (for div (trunc_div exact_div)
384 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
386 wi::overflow_type overflow;
387 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
388 TYPE_SIGN (type), &overflow);
390 (if (div == EXACT_DIV_EXPR
391 || optimize_successive_divisions_p (@2, @3))
393 (div @0 { wide_int_to_tree (type, mul); })
394 (if (TYPE_UNSIGNED (type)
395 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
396 { build_zero_cst (type); }))))))
398 /* Combine successive multiplications. Similar to above, but handling
399 overflow is different. */
401 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
403 wi::overflow_type overflow;
404 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
405 TYPE_SIGN (type), &overflow);
407 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
408 otherwise undefined overflow implies that @0 must be zero. */
409 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
410 (mult @0 { wide_int_to_tree (type, mul); }))))
412 /* Optimize A / A to 1.0 if we don't care about
413 NaNs or Infinities. */
416 (if (FLOAT_TYPE_P (type)
417 && ! HONOR_NANS (type)
418 && ! HONOR_INFINITIES (type))
419 { build_one_cst (type); }))
421 /* Optimize -A / A to -1.0 if we don't care about
422 NaNs or Infinities. */
424 (rdiv:C @0 (negate @0))
425 (if (FLOAT_TYPE_P (type)
426 && ! HONOR_NANS (type)
427 && ! HONOR_INFINITIES (type))
428 { build_minus_one_cst (type); }))
430 /* PR71078: x / abs(x) -> copysign (1.0, x) */
432 (rdiv:C (convert? @0) (convert? (abs @0)))
433 (if (SCALAR_FLOAT_TYPE_P (type)
434 && ! HONOR_NANS (type)
435 && ! HONOR_INFINITIES (type))
437 (if (types_match (type, float_type_node))
438 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
439 (if (types_match (type, double_type_node))
440 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
441 (if (types_match (type, long_double_type_node))
442 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
444 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
447 (if (!HONOR_SNANS (type))
450 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
452 (rdiv @0 real_minus_onep)
453 (if (!HONOR_SNANS (type))
456 (if (flag_reciprocal_math)
457 /* Convert (A/B)/C to A/(B*C). */
459 (rdiv (rdiv:s @0 @1) @2)
460 (rdiv @0 (mult @1 @2)))
462 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
464 (rdiv @0 (mult:s @1 REAL_CST@2))
466 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
468 (rdiv (mult @0 { tem; } ) @1))))
470 /* Convert A/(B/C) to (A/B)*C */
472 (rdiv @0 (rdiv:s @1 @2))
473 (mult (rdiv @0 @1) @2)))
475 /* Simplify x / (- y) to -x / y. */
477 (rdiv @0 (negate @1))
478 (rdiv (negate @0) @1))
480 (if (flag_unsafe_math_optimizations)
481 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
482 Since C / x may underflow to zero, do this only for unsafe math. */
483 (for op (lt le gt ge)
486 (op (rdiv REAL_CST@0 @1) real_zerop@2)
487 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
489 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
491 /* For C < 0, use the inverted operator. */
492 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
495 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
496 (for div (trunc_div ceil_div floor_div round_div exact_div)
498 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
499 (if (integer_pow2p (@2)
500 && tree_int_cst_sgn (@2) > 0
501 && tree_nop_conversion_p (type, TREE_TYPE (@0))
502 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
504 { build_int_cst (integer_type_node,
505 wi::exact_log2 (wi::to_wide (@2))); }))))
507 /* If ARG1 is a constant, we can convert this to a multiply by the
508 reciprocal. This does not have the same rounding properties,
509 so only do this if -freciprocal-math. We can actually
510 always safely do it if ARG1 is a power of two, but it's hard to
511 tell if it is or not in a portable manner. */
512 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
516 (if (flag_reciprocal_math
519 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
521 (mult @0 { tem; } )))
522 (if (cst != COMPLEX_CST)
523 (with { tree inverse = exact_inverse (type, @1); }
525 (mult @0 { inverse; } ))))))))
527 (for mod (ceil_mod floor_mod round_mod trunc_mod)
528 /* 0 % X is always zero. */
530 (mod integer_zerop@0 @1)
531 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
532 (if (!integer_zerop (@1))
534 /* X % 1 is always zero. */
536 (mod @0 integer_onep)
537 { build_zero_cst (type); })
538 /* X % -1 is zero. */
540 (mod @0 integer_minus_onep@1)
541 (if (!TYPE_UNSIGNED (type))
542 { build_zero_cst (type); }))
546 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
547 (if (!integer_zerop (@0))
548 { build_zero_cst (type); }))
549 /* (X % Y) % Y is just X % Y. */
551 (mod (mod@2 @0 @1) @1)
553 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
555 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
556 (if (ANY_INTEGRAL_TYPE_P (type)
557 && TYPE_OVERFLOW_UNDEFINED (type)
558 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
560 { build_zero_cst (type); }))
561 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
562 modulo and comparison, since it is simpler and equivalent. */
565 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
566 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
567 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
568 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
570 /* X % -C is the same as X % C. */
572 (trunc_mod @0 INTEGER_CST@1)
573 (if (TYPE_SIGN (type) == SIGNED
574 && !TREE_OVERFLOW (@1)
575 && wi::neg_p (wi::to_wide (@1))
576 && !TYPE_OVERFLOW_TRAPS (type)
577 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
578 && !sign_bit_p (@1, @1))
579 (trunc_mod @0 (negate @1))))
581 /* X % -Y is the same as X % Y. */
583 (trunc_mod @0 (convert? (negate @1)))
584 (if (INTEGRAL_TYPE_P (type)
585 && !TYPE_UNSIGNED (type)
586 && !TYPE_OVERFLOW_TRAPS (type)
587 && tree_nop_conversion_p (type, TREE_TYPE (@1))
588 /* Avoid this transformation if X might be INT_MIN or
589 Y might be -1, because we would then change valid
590 INT_MIN % -(-1) into invalid INT_MIN % -1. */
591 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
592 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
594 (trunc_mod @0 (convert @1))))
596 /* X - (X / Y) * Y is the same as X % Y. */
598 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
599 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
600 (convert (trunc_mod @0 @1))))
602 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
603 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
604 Also optimize A % (C << N) where C is a power of 2,
605 to A & ((C << N) - 1).
606 Also optimize "A shift (B % C)", if C is a power of 2, to
607 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
608 and assume (B % C) is nonnegative as shifts negative values would
610 (match (power_of_two_cand @1)
612 (match (power_of_two_cand @1)
613 (lshift INTEGER_CST@1 @2))
614 (for mod (trunc_mod floor_mod)
615 (for shift (lshift rshift)
617 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
618 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
619 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
622 (mod @0 (convert? (power_of_two_cand@1 @2)))
623 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
624 /* Allow any integral conversions of the divisor, except
625 conversion from narrower signed to wider unsigned type
626 where if @1 would be negative power of two, the divisor
627 would not be a power of two. */
628 && INTEGRAL_TYPE_P (type)
629 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
630 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
631 || TYPE_UNSIGNED (TREE_TYPE (@1))
632 || !TYPE_UNSIGNED (type))
633 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
634 (with { tree utype = TREE_TYPE (@1);
635 if (!TYPE_OVERFLOW_WRAPS (utype))
636 utype = unsigned_type_for (utype); }
637 (bit_and @0 (convert (minus (convert:utype @1)
638 { build_one_cst (utype); })))))))
640 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
642 (trunc_div (mult @0 integer_pow2p@1) @1)
643 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
644 (bit_and @0 { wide_int_to_tree
645 (type, wi::mask (TYPE_PRECISION (type)
646 - wi::exact_log2 (wi::to_wide (@1)),
647 false, TYPE_PRECISION (type))); })))
649 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
651 (mult (trunc_div @0 integer_pow2p@1) @1)
652 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
653 (bit_and @0 (negate @1))))
655 /* Simplify (t * 2) / 2) -> t. */
656 (for div (trunc_div ceil_div floor_div round_div exact_div)
658 (div (mult:c @0 @1) @1)
659 (if (ANY_INTEGRAL_TYPE_P (type))
660 (if (TYPE_OVERFLOW_UNDEFINED (type))
665 bool overflowed = true;
666 wide_int wmin0, wmax0, wmin1, wmax1;
667 if (INTEGRAL_TYPE_P (type)
668 && get_range_info (@0, &wmin0, &wmax0) == VR_RANGE
669 && get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
671 /* If the multiplication can't overflow/wrap around, then
672 it can be optimized too. */
673 wi::overflow_type min_ovf, max_ovf;
674 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
675 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
676 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
678 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
679 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
680 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
691 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
696 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
699 (pows (op @0) REAL_CST@1)
700 (with { HOST_WIDE_INT n; }
701 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
703 /* Likewise for powi. */
706 (pows (op @0) INTEGER_CST@1)
707 (if ((wi::to_wide (@1) & 1) == 0)
709 /* Strip negate and abs from both operands of hypot. */
717 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
718 (for copysigns (COPYSIGN_ALL)
720 (copysigns (op @0) @1)
723 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
728 /* Convert absu(x)*absu(x) -> x*x. */
730 (mult (absu@1 @0) @1)
731 (mult (convert@2 @0) @2))
733 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
737 (coss (copysigns @0 @1))
740 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
744 (pows (copysigns @0 @2) REAL_CST@1)
745 (with { HOST_WIDE_INT n; }
746 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
748 /* Likewise for powi. */
752 (pows (copysigns @0 @2) INTEGER_CST@1)
753 (if ((wi::to_wide (@1) & 1) == 0)
758 /* hypot(copysign(x, y), z) -> hypot(x, z). */
760 (hypots (copysigns @0 @1) @2)
762 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
764 (hypots @0 (copysigns @1 @2))
767 /* copysign(x, CST) -> [-]abs (x). */
768 (for copysigns (COPYSIGN_ALL)
770 (copysigns @0 REAL_CST@1)
771 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
775 /* copysign(copysign(x, y), z) -> copysign(x, z). */
776 (for copysigns (COPYSIGN_ALL)
778 (copysigns (copysigns @0 @1) @2)
781 /* copysign(x,y)*copysign(x,y) -> x*x. */
782 (for copysigns (COPYSIGN_ALL)
784 (mult (copysigns@2 @0 @1) @2)
787 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
788 (for ccoss (CCOS CCOSH)
793 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
794 (for ops (conj negate)
800 /* Fold (a * (1 << b)) into (a << b) */
802 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
803 (if (! FLOAT_TYPE_P (type)
804 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
807 /* Fold (1 << (C - x)) where C = precision(type) - 1
808 into ((1 << C) >> x). */
810 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
811 (if (INTEGRAL_TYPE_P (type)
812 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
814 (if (TYPE_UNSIGNED (type))
815 (rshift (lshift @0 @2) @3)
817 { tree utype = unsigned_type_for (type); }
818 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
820 /* Fold (C1/X)*C2 into (C1*C2)/X. */
822 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
823 (if (flag_associative_math
826 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
828 (rdiv { tem; } @1)))))
830 /* Simplify ~X & X as zero. */
832 (bit_and:c (convert? @0) (convert? (bit_not @0)))
833 { build_zero_cst (type); })
835 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
837 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
838 (if (TYPE_UNSIGNED (type))
839 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
841 (for bitop (bit_and bit_ior)
843 /* PR35691: Transform
844 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
845 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
847 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
848 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
849 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
850 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
851 (cmp (bit_ior @0 (convert @1)) @2)))
853 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
854 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
856 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
857 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
858 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
859 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
860 (cmp (bit_and @0 (convert @1)) @2))))
862 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
864 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
865 (minus (bit_xor @0 @1) @1))
867 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
868 (if (~wi::to_wide (@2) == wi::to_wide (@1))
869 (minus (bit_xor @0 @1) @1)))
871 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
873 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
874 (minus @1 (bit_xor @0 @1)))
876 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
877 (for op (bit_ior bit_xor plus)
879 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
882 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
883 (if (~wi::to_wide (@2) == wi::to_wide (@1))
886 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
888 (bit_ior:c (bit_xor:c @0 @1) @0)
891 /* (a & ~b) | (a ^ b) --> a ^ b */
893 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
896 /* (a & ~b) ^ ~a --> ~(a & b) */
898 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
899 (bit_not (bit_and @0 @1)))
901 /* (~a & b) ^ a --> (a | b) */
903 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
906 /* (a | b) & ~(a ^ b) --> a & b */
908 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
911 /* a | ~(a ^ b) --> a | ~b */
913 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
914 (bit_ior @0 (bit_not @1)))
916 /* (a | b) | (a &^ b) --> a | b */
917 (for op (bit_and bit_xor)
919 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
922 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
924 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
927 /* ~(~a & b) --> a | ~b */
929 (bit_not (bit_and:cs (bit_not @0) @1))
930 (bit_ior @0 (bit_not @1)))
932 /* ~(~a | b) --> a & ~b */
934 (bit_not (bit_ior:cs (bit_not @0) @1))
935 (bit_and @0 (bit_not @1)))
937 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
939 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
940 (bit_and @3 (bit_not @2)))
942 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
944 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
948 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
950 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
951 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
953 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
955 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
956 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
958 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
960 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
961 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
962 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
966 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
967 ((A & N) + B) & M -> (A + B) & M
968 Similarly if (N & M) == 0,
969 ((A | N) + B) & M -> (A + B) & M
970 and for - instead of + (or unary - instead of +)
971 and/or ^ instead of |.
972 If B is constant and (B & M) == 0, fold into A & M. */
974 (for bitop (bit_and bit_ior bit_xor)
976 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
979 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
980 @3, @4, @1, ERROR_MARK, NULL_TREE,
983 (convert (bit_and (op (convert:utype { pmop[0]; })
984 (convert:utype { pmop[1]; }))
985 (convert:utype @2))))))
987 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
990 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
991 NULL_TREE, NULL_TREE, @1, bitop, @3,
994 (convert (bit_and (op (convert:utype { pmop[0]; })
995 (convert:utype { pmop[1]; }))
996 (convert:utype @2)))))))
998 (bit_and (op:s @0 @1) INTEGER_CST@2)
1001 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1002 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1003 NULL_TREE, NULL_TREE, pmop); }
1005 (convert (bit_and (op (convert:utype { pmop[0]; })
1006 (convert:utype { pmop[1]; }))
1007 (convert:utype @2)))))))
1008 (for bitop (bit_and bit_ior bit_xor)
1010 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1013 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1014 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1015 NULL_TREE, NULL_TREE, pmop); }
1017 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1018 (convert:utype @1)))))))
1020 /* X % Y is smaller than Y. */
1023 (cmp (trunc_mod @0 @1) @1)
1024 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1025 { constant_boolean_node (cmp == LT_EXPR, type); })))
1028 (cmp @1 (trunc_mod @0 @1))
1029 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1030 { constant_boolean_node (cmp == GT_EXPR, type); })))
1034 (bit_ior @0 integer_all_onesp@1)
1039 (bit_ior @0 integer_zerop)
1044 (bit_and @0 integer_zerop@1)
1050 (for op (bit_ior bit_xor plus)
1052 (op:c (convert? @0) (convert? (bit_not @0)))
1053 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1058 { build_zero_cst (type); })
1060 /* Canonicalize X ^ ~0 to ~X. */
1062 (bit_xor @0 integer_all_onesp@1)
1067 (bit_and @0 integer_all_onesp)
1070 /* x & x -> x, x | x -> x */
1071 (for bitop (bit_and bit_ior)
1076 /* x & C -> x if we know that x & ~C == 0. */
1079 (bit_and SSA_NAME@0 INTEGER_CST@1)
1080 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1081 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1085 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1087 (bit_not (minus (bit_not @0) @1))
1090 (bit_not (plus:c (bit_not @0) @1))
1093 /* ~(X - Y) -> ~X + Y. */
1095 (bit_not (minus:s @0 @1))
1096 (plus (bit_not @0) @1))
1098 (bit_not (plus:s @0 INTEGER_CST@1))
1099 (if ((INTEGRAL_TYPE_P (type)
1100 && TYPE_UNSIGNED (type))
1101 || (!TYPE_OVERFLOW_SANITIZED (type)
1102 && may_negate_without_overflow_p (@1)))
1103 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1106 /* ~X + Y -> (Y - X) - 1. */
1108 (plus:c (bit_not @0) @1)
1109 (if (ANY_INTEGRAL_TYPE_P (type)
1110 && TYPE_OVERFLOW_WRAPS (type)
1111 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1112 && !integer_all_onesp (@1))
1113 (plus (minus @1 @0) { build_minus_one_cst (type); })
1114 (if (INTEGRAL_TYPE_P (type)
1115 && TREE_CODE (@1) == INTEGER_CST
1116 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1118 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1120 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1122 (bit_not (rshift:s @0 @1))
1123 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1124 (rshift (bit_not! @0) @1)
1125 /* For logical right shifts, this is possible only if @0 doesn't
1126 have MSB set and the logical right shift is changed into
1127 arithmetic shift. */
1128 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1129 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1130 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1133 /* x + (x & 1) -> (x + 1) & ~1 */
1135 (plus:c @0 (bit_and:s @0 integer_onep@1))
1136 (bit_and (plus @0 @1) (bit_not @1)))
1138 /* x & ~(x & y) -> x & ~y */
1139 /* x | ~(x | y) -> x | ~y */
1140 (for bitop (bit_and bit_ior)
1142 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1143 (bitop @0 (bit_not @1))))
1145 /* (~x & y) | ~(x | y) -> ~x */
1147 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1150 /* (x | y) ^ (x | ~y) -> ~x */
1152 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1155 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1157 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1158 (bit_not (bit_xor @0 @1)))
1160 /* (~x | y) ^ (x ^ y) -> x | ~y */
1162 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1163 (bit_ior @0 (bit_not @1)))
1165 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1167 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1168 (bit_not (bit_and @0 @1)))
1170 /* (x | y) & ~x -> y & ~x */
1171 /* (x & y) | ~x -> y | ~x */
1172 (for bitop (bit_and bit_ior)
1173 rbitop (bit_ior bit_and)
1175 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1178 /* (x & y) ^ (x | y) -> x ^ y */
1180 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1183 /* (x ^ y) ^ (x | y) -> x & y */
1185 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1188 /* (x & y) + (x ^ y) -> x | y */
1189 /* (x & y) | (x ^ y) -> x | y */
1190 /* (x & y) ^ (x ^ y) -> x | y */
1191 (for op (plus bit_ior bit_xor)
1193 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1196 /* (x & y) + (x | y) -> x + y */
1198 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1201 /* (x + y) - (x | y) -> x & y */
1203 (minus (plus @0 @1) (bit_ior @0 @1))
1204 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1205 && !TYPE_SATURATING (type))
1208 /* (x + y) - (x & y) -> x | y */
1210 (minus (plus @0 @1) (bit_and @0 @1))
1211 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1212 && !TYPE_SATURATING (type))
1215 /* (x | y) - y -> (x & ~y) */
1217 (minus (bit_ior:cs @0 @1) @1)
1218 (bit_and @0 (bit_not @1)))
1220 /* (x | y) - (x ^ y) -> x & y */
1222 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1225 /* (x | y) - (x & y) -> x ^ y */
1227 (minus (bit_ior @0 @1) (bit_and @0 @1))
1230 /* (x | y) & ~(x & y) -> x ^ y */
1232 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1235 /* (x | y) & (~x ^ y) -> x & y */
1237 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1240 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1242 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1243 (bit_not (bit_xor @0 @1)))
1245 /* (~x | y) ^ (x | ~y) -> x ^ y */
1247 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1250 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1252 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1253 (nop_convert2? (bit_ior @0 @1))))
1255 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1256 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1257 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1258 && !TYPE_SATURATING (TREE_TYPE (@2)))
1259 (bit_not (convert (bit_xor @0 @1)))))
1261 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1263 (nop_convert3? (bit_ior @0 @1)))
1264 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1265 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1266 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1267 && !TYPE_SATURATING (TREE_TYPE (@2)))
1268 (bit_not (convert (bit_xor @0 @1)))))
1270 (minus (nop_convert1? (bit_and @0 @1))
1271 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1273 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1274 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1275 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1276 && !TYPE_SATURATING (TREE_TYPE (@2)))
1277 (bit_not (convert (bit_xor @0 @1)))))
1279 /* ~x & ~y -> ~(x | y)
1280 ~x | ~y -> ~(x & y) */
1281 (for op (bit_and bit_ior)
1282 rop (bit_ior bit_and)
1284 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1285 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1286 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1287 (bit_not (rop (convert @0) (convert @1))))))
1289 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1290 with a constant, and the two constants have no bits in common,
1291 we should treat this as a BIT_IOR_EXPR since this may produce more
1293 (for op (bit_xor plus)
1295 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1296 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1297 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1298 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1299 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1300 (bit_ior (convert @4) (convert @5)))))
1302 /* (X | Y) ^ X -> Y & ~ X*/
1304 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1305 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1306 (convert (bit_and @1 (bit_not @0)))))
1308 /* Convert ~X ^ ~Y to X ^ Y. */
1310 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1311 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1312 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1313 (bit_xor (convert @0) (convert @1))))
1315 /* Convert ~X ^ C to X ^ ~C. */
1317 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1318 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1319 (bit_xor (convert @0) (bit_not @1))))
1321 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1322 (for opo (bit_and bit_xor)
1323 opi (bit_xor bit_and)
1325 (opo:c (opi:cs @0 @1) @1)
1326 (bit_and (bit_not @0) @1)))
1328 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1329 operands are another bit-wise operation with a common input. If so,
1330 distribute the bit operations to save an operation and possibly two if
1331 constants are involved. For example, convert
1332 (A | B) & (A | C) into A | (B & C)
1333 Further simplification will occur if B and C are constants. */
1334 (for op (bit_and bit_ior bit_xor)
1335 rop (bit_ior bit_and bit_and)
1337 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1338 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1339 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1340 (rop (convert @0) (op (convert @1) (convert @2))))))
1342 /* Some simple reassociation for bit operations, also handled in reassoc. */
1343 /* (X & Y) & Y -> X & Y
1344 (X | Y) | Y -> X | Y */
1345 (for op (bit_and bit_ior)
1347 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1349 /* (X ^ Y) ^ Y -> X */
1351 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1353 /* (X & Y) & (X & Z) -> (X & Y) & Z
1354 (X | Y) | (X | Z) -> (X | Y) | Z */
1355 (for op (bit_and bit_ior)
1357 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1358 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1359 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1360 (if (single_use (@5) && single_use (@6))
1361 (op @3 (convert @2))
1362 (if (single_use (@3) && single_use (@4))
1363 (op (convert @1) @5))))))
1364 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1366 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1367 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1368 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1369 (bit_xor (convert @1) (convert @2))))
1371 /* Convert abs (abs (X)) into abs (X).
1372 also absu (absu (X)) into absu (X). */
1378 (absu (convert@2 (absu@1 @0)))
1379 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1382 /* Convert abs[u] (-X) -> abs[u] (X). */
1391 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1393 (abs tree_expr_nonnegative_p@0)
1397 (absu tree_expr_nonnegative_p@0)
1400 /* Simplify (-(X < 0) | 1) * X into abs (X). */
1402 (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
1403 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
1406 /* Similarly (-(X < 0) | 1U) * X into absu (X). */
1408 (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
1409 integer_onep) (nop_convert @0))
1410 (if (INTEGRAL_TYPE_P (type)
1411 && TYPE_UNSIGNED (type)
1412 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1413 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1416 /* A few cases of fold-const.c negate_expr_p predicate. */
1417 (match negate_expr_p
1419 (if ((INTEGRAL_TYPE_P (type)
1420 && TYPE_UNSIGNED (type))
1421 || (!TYPE_OVERFLOW_SANITIZED (type)
1422 && may_negate_without_overflow_p (t)))))
1423 (match negate_expr_p
1425 (match negate_expr_p
1427 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1428 (match negate_expr_p
1430 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1431 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1433 (match negate_expr_p
1435 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1436 (match negate_expr_p
1438 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1439 || (FLOAT_TYPE_P (type)
1440 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1441 && !HONOR_SIGNED_ZEROS (type)))))
1443 /* (-A) * (-B) -> A * B */
1445 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1446 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1447 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1448 (mult (convert @0) (convert (negate @1)))))
1450 /* -(A + B) -> (-B) - A. */
1452 (negate (plus:c @0 negate_expr_p@1))
1453 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1454 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1455 (minus (negate @1) @0)))
1457 /* -(A - B) -> B - A. */
1459 (negate (minus @0 @1))
1460 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1461 || (FLOAT_TYPE_P (type)
1462 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1463 && !HONOR_SIGNED_ZEROS (type)))
1466 (negate (pointer_diff @0 @1))
1467 (if (TYPE_OVERFLOW_UNDEFINED (type))
1468 (pointer_diff @1 @0)))
1470 /* A - B -> A + (-B) if B is easily negatable. */
1472 (minus @0 negate_expr_p@1)
1473 (if (!FIXED_POINT_TYPE_P (type))
1474 (plus @0 (negate @1))))
1476 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1478 For bitwise binary operations apply operand conversions to the
1479 binary operation result instead of to the operands. This allows
1480 to combine successive conversions and bitwise binary operations.
1481 We combine the above two cases by using a conditional convert. */
1482 (for bitop (bit_and bit_ior bit_xor)
1484 (bitop (convert@2 @0) (convert?@3 @1))
1485 (if (((TREE_CODE (@1) == INTEGER_CST
1486 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1487 && int_fits_type_p (@1, TREE_TYPE (@0)))
1488 || types_match (@0, @1))
1489 /* ??? This transform conflicts with fold-const.c doing
1490 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1491 constants (if x has signed type, the sign bit cannot be set
1492 in c). This folds extension into the BIT_AND_EXPR.
1493 Restrict it to GIMPLE to avoid endless recursions. */
1494 && (bitop != BIT_AND_EXPR || GIMPLE)
1495 && (/* That's a good idea if the conversion widens the operand, thus
1496 after hoisting the conversion the operation will be narrower. */
1497 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1498 /* It's also a good idea if the conversion is to a non-integer
1500 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1501 /* Or if the precision of TO is not the same as the precision
1503 || !type_has_mode_precision_p (type)
1504 /* In GIMPLE, getting rid of 2 conversions for one new results
1507 && TREE_CODE (@1) != INTEGER_CST
1508 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1510 && single_use (@3))))
1511 (convert (bitop @0 (convert @1)))))
1512 /* In GIMPLE, getting rid of 2 conversions for one new results
1515 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1517 && TREE_CODE (@1) != INTEGER_CST
1518 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1519 && types_match (type, @0))
1520 (bitop @0 (convert @1)))))
1522 (for bitop (bit_and bit_ior)
1523 rbitop (bit_ior bit_and)
1524 /* (x | y) & x -> x */
1525 /* (x & y) | x -> x */
1527 (bitop:c (rbitop:c @0 @1) @0)
1529 /* (~x | y) & x -> x & y */
1530 /* (~x & y) | x -> x | y */
1532 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1535 /* ((x | y) & z) | x -> (z & y) | x */
1537 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1538 (bit_ior (bit_and @2 @1) @0))
1540 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1542 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1543 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1545 /* Combine successive equal operations with constants. */
1546 (for bitop (bit_and bit_ior bit_xor)
1548 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1549 (if (!CONSTANT_CLASS_P (@0))
1550 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1551 folded to a constant. */
1552 (bitop @0 (bitop @1 @2))
1553 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1554 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1555 the values involved are such that the operation can't be decided at
1556 compile time. Try folding one of @0 or @1 with @2 to see whether
1557 that combination can be decided at compile time.
1559 Keep the existing form if both folds fail, to avoid endless
1561 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1563 (bitop @1 { cst1; })
1564 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1566 (bitop @0 { cst2; }))))))))
1568 /* Try simple folding for X op !X, and X op X with the help
1569 of the truth_valued_p and logical_inverted_value predicates. */
1570 (match truth_valued_p
1572 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1573 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1574 (match truth_valued_p
1576 (match truth_valued_p
1579 (match (logical_inverted_value @0)
1581 (match (logical_inverted_value @0)
1582 (bit_not truth_valued_p@0))
1583 (match (logical_inverted_value @0)
1584 (eq @0 integer_zerop))
1585 (match (logical_inverted_value @0)
1586 (ne truth_valued_p@0 integer_truep))
1587 (match (logical_inverted_value @0)
1588 (bit_xor truth_valued_p@0 integer_truep))
1592 (bit_and:c @0 (logical_inverted_value @0))
1593 { build_zero_cst (type); })
1594 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1595 (for op (bit_ior bit_xor)
1597 (op:c truth_valued_p@0 (logical_inverted_value @0))
1598 { constant_boolean_node (true, type); }))
1599 /* X ==/!= !X is false/true. */
1602 (op:c truth_valued_p@0 (logical_inverted_value @0))
1603 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1607 (bit_not (bit_not @0))
1610 /* Convert ~ (-A) to A - 1. */
1612 (bit_not (convert? (negate @0)))
1613 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1614 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1615 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1617 /* Convert - (~A) to A + 1. */
1619 (negate (nop_convert? (bit_not @0)))
1620 (plus (view_convert @0) { build_each_one_cst (type); }))
1622 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1624 (bit_not (convert? (minus @0 integer_each_onep)))
1625 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1626 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1627 (convert (negate @0))))
1629 (bit_not (convert? (plus @0 integer_all_onesp)))
1630 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1631 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1632 (convert (negate @0))))
1634 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1636 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1637 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1638 (convert (bit_xor @0 (bit_not @1)))))
1640 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1641 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1642 (convert (bit_xor @0 @1))))
1644 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1646 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1647 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1648 (bit_not (bit_xor (view_convert @0) @1))))
1650 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1652 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1653 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1655 /* Fold A - (A & B) into ~B & A. */
1657 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1658 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1659 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1660 (convert (bit_and (bit_not @1) @0))))
1662 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1663 (for cmp (gt lt ge le)
1665 (mult (convert (cmp @0 @1)) @2)
1666 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1667 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1669 /* For integral types with undefined overflow and C != 0 fold
1670 x * C EQ/NE y * C into x EQ/NE y. */
1673 (cmp (mult:c @0 @1) (mult:c @2 @1))
1674 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1675 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1676 && tree_expr_nonzero_p (@1))
1679 /* For integral types with wrapping overflow and C odd fold
1680 x * C EQ/NE y * C into x EQ/NE y. */
1683 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1684 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1685 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1686 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1689 /* For integral types with undefined overflow and C != 0 fold
1690 x * C RELOP y * C into:
1692 x RELOP y for nonnegative C
1693 y RELOP x for negative C */
1694 (for cmp (lt gt le ge)
1696 (cmp (mult:c @0 @1) (mult:c @2 @1))
1697 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1698 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1699 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1701 (if (TREE_CODE (@1) == INTEGER_CST
1702 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1705 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1709 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1710 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1711 && TYPE_UNSIGNED (TREE_TYPE (@0))
1712 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1713 && (wi::to_wide (@2)
1714 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1715 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1716 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1718 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1719 (for cmp (simple_comparison)
1721 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1722 (if (element_precision (@3) >= element_precision (@0)
1723 && types_match (@0, @1))
1724 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1725 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1727 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1730 tree utype = unsigned_type_for (TREE_TYPE (@0));
1732 (cmp (convert:utype @1) (convert:utype @0)))))
1733 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1734 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1738 tree utype = unsigned_type_for (TREE_TYPE (@0));
1740 (cmp (convert:utype @0) (convert:utype @1)))))))))
1742 /* X / C1 op C2 into a simple range test. */
1743 (for cmp (simple_comparison)
1745 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1746 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1747 && integer_nonzerop (@1)
1748 && !TREE_OVERFLOW (@1)
1749 && !TREE_OVERFLOW (@2))
1750 (with { tree lo, hi; bool neg_overflow;
1751 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1754 (if (code == LT_EXPR || code == GE_EXPR)
1755 (if (TREE_OVERFLOW (lo))
1756 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1757 (if (code == LT_EXPR)
1760 (if (code == LE_EXPR || code == GT_EXPR)
1761 (if (TREE_OVERFLOW (hi))
1762 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1763 (if (code == LE_EXPR)
1767 { build_int_cst (type, code == NE_EXPR); })
1768 (if (code == EQ_EXPR && !hi)
1770 (if (code == EQ_EXPR && !lo)
1772 (if (code == NE_EXPR && !hi)
1774 (if (code == NE_EXPR && !lo)
1777 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1781 tree etype = range_check_type (TREE_TYPE (@0));
1784 hi = fold_convert (etype, hi);
1785 lo = fold_convert (etype, lo);
1786 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1789 (if (etype && hi && !TREE_OVERFLOW (hi))
1790 (if (code == EQ_EXPR)
1791 (le (minus (convert:etype @0) { lo; }) { hi; })
1792 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1794 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1795 (for op (lt le ge gt)
1797 (op (plus:c @0 @2) (plus:c @1 @2))
1798 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1799 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1801 /* For equality and subtraction, this is also true with wrapping overflow. */
1802 (for op (eq ne minus)
1804 (op (plus:c @0 @2) (plus:c @1 @2))
1805 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1806 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1807 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1810 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1811 (for op (lt le ge gt)
1813 (op (minus @0 @2) (minus @1 @2))
1814 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1815 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1817 /* For equality and subtraction, this is also true with wrapping overflow. */
1818 (for op (eq ne minus)
1820 (op (minus @0 @2) (minus @1 @2))
1821 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1822 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1823 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1825 /* And for pointers... */
1826 (for op (simple_comparison)
1828 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1829 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1832 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1833 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1834 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1835 (pointer_diff @0 @1)))
1837 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1838 (for op (lt le ge gt)
1840 (op (minus @2 @0) (minus @2 @1))
1841 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1842 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1844 /* For equality and subtraction, this is also true with wrapping overflow. */
1845 (for op (eq ne minus)
1847 (op (minus @2 @0) (minus @2 @1))
1848 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1849 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1850 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1852 /* And for pointers... */
1853 (for op (simple_comparison)
1855 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1856 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1859 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1860 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1861 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1862 (pointer_diff @1 @0)))
1864 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1865 (for op (lt le gt ge)
1867 (op:c (plus:c@2 @0 @1) @1)
1868 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1869 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1870 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1871 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1872 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1873 /* For equality, this is also true with wrapping overflow. */
1876 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1877 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1878 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1879 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1880 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1881 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1882 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1883 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1885 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1886 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1887 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1888 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1889 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1891 /* X - Y < X is the same as Y > 0 when there is no overflow.
1892 For equality, this is also true with wrapping overflow. */
1893 (for op (simple_comparison)
1895 (op:c @0 (minus@2 @0 @1))
1896 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1897 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1898 || ((op == EQ_EXPR || op == NE_EXPR)
1899 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1900 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1901 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1904 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1905 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1909 (cmp (trunc_div @0 @1) integer_zerop)
1910 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1911 /* Complex ==/!= is allowed, but not </>=. */
1912 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1913 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1916 /* X == C - X can never be true if C is odd. */
1919 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1920 (if (TREE_INT_CST_LOW (@1) & 1)
1921 { constant_boolean_node (cmp == NE_EXPR, type); })))
1923 /* Arguments on which one can call get_nonzero_bits to get the bits
1925 (match with_possible_nonzero_bits
1927 (match with_possible_nonzero_bits
1929 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1930 /* Slightly extended version, do not make it recursive to keep it cheap. */
1931 (match (with_possible_nonzero_bits2 @0)
1932 with_possible_nonzero_bits@0)
1933 (match (with_possible_nonzero_bits2 @0)
1934 (bit_and:c with_possible_nonzero_bits@0 @2))
1936 /* Same for bits that are known to be set, but we do not have
1937 an equivalent to get_nonzero_bits yet. */
1938 (match (with_certain_nonzero_bits2 @0)
1940 (match (with_certain_nonzero_bits2 @0)
1941 (bit_ior @1 INTEGER_CST@0))
1943 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1946 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1947 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1948 { constant_boolean_node (cmp == NE_EXPR, type); })))
1950 /* ((X inner_op C0) outer_op C1)
1951 With X being a tree where value_range has reasoned certain bits to always be
1952 zero throughout its computed value range,
1953 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1954 where zero_mask has 1's for all bits that are sure to be 0 in
1956 if (inner_op == '^') C0 &= ~C1;
1957 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1958 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1960 (for inner_op (bit_ior bit_xor)
1961 outer_op (bit_xor bit_ior)
1964 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1968 wide_int zero_mask_not;
1972 if (TREE_CODE (@2) == SSA_NAME)
1973 zero_mask_not = get_nonzero_bits (@2);
1977 if (inner_op == BIT_XOR_EXPR)
1979 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1980 cst_emit = C0 | wi::to_wide (@1);
1984 C0 = wi::to_wide (@0);
1985 cst_emit = C0 ^ wi::to_wide (@1);
1988 (if (!fail && (C0 & zero_mask_not) == 0)
1989 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1990 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1991 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1993 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1995 (pointer_plus (pointer_plus:s @0 @1) @3)
1996 (pointer_plus @0 (plus @1 @3)))
2002 tem4 = (unsigned long) tem3;
2007 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2008 /* Conditionally look through a sign-changing conversion. */
2009 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2010 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2011 || (GENERIC && type == TREE_TYPE (@1))))
2014 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2015 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2019 tem = (sizetype) ptr;
2023 and produce the simpler and easier to analyze with respect to alignment
2024 ... = ptr & ~algn; */
2026 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2027 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2028 (bit_and @0 { algn; })))
2030 /* Try folding difference of addresses. */
2032 (minus (convert ADDR_EXPR@0) (convert @1))
2033 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2034 (with { poly_int64 diff; }
2035 (if (ptr_difference_const (@0, @1, &diff))
2036 { build_int_cst_type (type, diff); }))))
2038 (minus (convert @0) (convert ADDR_EXPR@1))
2039 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2040 (with { poly_int64 diff; }
2041 (if (ptr_difference_const (@0, @1, &diff))
2042 { build_int_cst_type (type, diff); }))))
2044 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2045 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2046 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2047 (with { poly_int64 diff; }
2048 (if (ptr_difference_const (@0, @1, &diff))
2049 { build_int_cst_type (type, diff); }))))
2051 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2052 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2053 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2054 (with { poly_int64 diff; }
2055 (if (ptr_difference_const (@0, @1, &diff))
2056 { build_int_cst_type (type, diff); }))))
2058 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2060 (convert (pointer_diff @0 INTEGER_CST@1))
2061 (if (POINTER_TYPE_P (type))
2062 { build_fold_addr_expr_with_type
2063 (build2 (MEM_REF, char_type_node, @0,
2064 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2067 /* If arg0 is derived from the address of an object or function, we may
2068 be able to fold this expression using the object or function's
2071 (bit_and (convert? @0) INTEGER_CST@1)
2072 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2073 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2077 unsigned HOST_WIDE_INT bitpos;
2078 get_pointer_alignment_1 (@0, &align, &bitpos);
2080 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2081 { wide_int_to_tree (type, (wi::to_wide (@1)
2082 & (bitpos / BITS_PER_UNIT))); }))))
2086 (if (INTEGRAL_TYPE_P (type)
2087 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2091 (if (INTEGRAL_TYPE_P (type)
2092 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2094 /* x > y && x != XXX_MIN --> x > y
2095 x > y && x == XXX_MIN --> false . */
2098 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2100 (if (eqne == EQ_EXPR)
2101 { constant_boolean_node (false, type); })
2102 (if (eqne == NE_EXPR)
2106 /* x < y && x != XXX_MAX --> x < y
2107 x < y && x == XXX_MAX --> false. */
2110 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2112 (if (eqne == EQ_EXPR)
2113 { constant_boolean_node (false, type); })
2114 (if (eqne == NE_EXPR)
2118 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2120 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2123 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2125 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2128 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2130 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2133 /* x <= y || x != XXX_MIN --> true. */
2135 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2136 { constant_boolean_node (true, type); })
2138 /* x <= y || x == XXX_MIN --> x <= y. */
2140 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2143 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2145 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2148 /* x >= y || x != XXX_MAX --> true
2149 x >= y || x == XXX_MAX --> x >= y. */
2152 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2154 (if (eqne == EQ_EXPR)
2156 (if (eqne == NE_EXPR)
2157 { constant_boolean_node (true, type); }))))
2159 /* y == XXX_MIN || x < y --> x <= y - 1 */
2161 (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
2162 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2163 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2164 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2166 /* y != XXX_MIN && x >= y --> x > y - 1 */
2168 (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
2169 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2170 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2171 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2173 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2174 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2177 (for code2 (eq ne lt gt le ge)
2179 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2182 int cmp = tree_int_cst_compare (@1, @2);
2186 case EQ_EXPR: val = (cmp == 0); break;
2187 case NE_EXPR: val = (cmp != 0); break;
2188 case LT_EXPR: val = (cmp < 0); break;
2189 case GT_EXPR: val = (cmp > 0); break;
2190 case LE_EXPR: val = (cmp <= 0); break;
2191 case GE_EXPR: val = (cmp >= 0); break;
2192 default: gcc_unreachable ();
2196 (if (code1 == EQ_EXPR && val) @3)
2197 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2198 (if (code1 == NE_EXPR && !val) @4))))))
2200 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2202 (for code1 (lt le gt ge)
2203 (for code2 (lt le gt ge)
2205 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2208 int cmp = tree_int_cst_compare (@1, @2);
2211 /* Choose the more restrictive of two < or <= comparisons. */
2212 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2213 && (code2 == LT_EXPR || code2 == LE_EXPR))
2214 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2217 /* Likewise chose the more restrictive of two > or >= comparisons. */
2218 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2219 && (code2 == GT_EXPR || code2 == GE_EXPR))
2220 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2223 /* Check for singleton ranges. */
2225 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2226 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2228 /* Check for disjoint ranges. */
2230 && (code1 == LT_EXPR || code1 == LE_EXPR)
2231 && (code2 == GT_EXPR || code2 == GE_EXPR))
2232 { constant_boolean_node (false, type); })
2234 && (code1 == GT_EXPR || code1 == GE_EXPR)
2235 && (code2 == LT_EXPR || code2 == LE_EXPR))
2236 { constant_boolean_node (false, type); })
2239 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2240 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2243 (for code2 (eq ne lt gt le ge)
2245 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2248 int cmp = tree_int_cst_compare (@1, @2);
2252 case EQ_EXPR: val = (cmp == 0); break;
2253 case NE_EXPR: val = (cmp != 0); break;
2254 case LT_EXPR: val = (cmp < 0); break;
2255 case GT_EXPR: val = (cmp > 0); break;
2256 case LE_EXPR: val = (cmp <= 0); break;
2257 case GE_EXPR: val = (cmp >= 0); break;
2258 default: gcc_unreachable ();
2262 (if (code1 == EQ_EXPR && val) @4)
2263 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2264 (if (code1 == NE_EXPR && !val) @3))))))
2266 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2268 (for code1 (lt le gt ge)
2269 (for code2 (lt le gt ge)
2271 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2274 int cmp = tree_int_cst_compare (@1, @2);
2277 /* Choose the more restrictive of two < or <= comparisons. */
2278 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2279 && (code2 == LT_EXPR || code2 == LE_EXPR))
2280 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2283 /* Likewise chose the more restrictive of two > or >= comparisons. */
2284 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2285 && (code2 == GT_EXPR || code2 == GE_EXPR))
2286 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2289 /* Check for singleton ranges. */
2291 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2292 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2294 /* Check for disjoint ranges. */
2296 && (code1 == LT_EXPR || code1 == LE_EXPR)
2297 && (code2 == GT_EXPR || code2 == GE_EXPR))
2298 { constant_boolean_node (true, type); })
2300 && (code1 == GT_EXPR || code1 == GE_EXPR)
2301 && (code2 == LT_EXPR || code2 == LE_EXPR))
2302 { constant_boolean_node (true, type); })
2305 /* We can't reassociate at all for saturating types. */
2306 (if (!TYPE_SATURATING (type))
2308 /* Contract negates. */
2309 /* A + (-B) -> A - B */
2311 (plus:c @0 (convert? (negate @1)))
2312 /* Apply STRIP_NOPS on the negate. */
2313 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2314 && !TYPE_OVERFLOW_SANITIZED (type))
2318 if (INTEGRAL_TYPE_P (type)
2319 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2320 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2322 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2323 /* A - (-B) -> A + B */
2325 (minus @0 (convert? (negate @1)))
2326 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2327 && !TYPE_OVERFLOW_SANITIZED (type))
2331 if (INTEGRAL_TYPE_P (type)
2332 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2333 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2335 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2337 Sign-extension is ok except for INT_MIN, which thankfully cannot
2338 happen without overflow. */
2340 (negate (convert (negate @1)))
2341 (if (INTEGRAL_TYPE_P (type)
2342 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2343 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2344 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2345 && !TYPE_OVERFLOW_SANITIZED (type)
2346 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2349 (negate (convert negate_expr_p@1))
2350 (if (SCALAR_FLOAT_TYPE_P (type)
2351 && ((DECIMAL_FLOAT_TYPE_P (type)
2352 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2353 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2354 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2355 (convert (negate @1))))
2357 (negate (nop_convert? (negate @1)))
2358 (if (!TYPE_OVERFLOW_SANITIZED (type)
2359 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2362 /* We can't reassociate floating-point unless -fassociative-math
2363 or fixed-point plus or minus because of saturation to +-Inf. */
2364 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2365 && !FIXED_POINT_TYPE_P (type))
2367 /* Match patterns that allow contracting a plus-minus pair
2368 irrespective of overflow issues. */
2369 /* (A +- B) - A -> +- B */
2370 /* (A +- B) -+ B -> A */
2371 /* A - (A +- B) -> -+ B */
2372 /* A +- (B -+ A) -> +- B */
2374 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2377 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2378 (if (!ANY_INTEGRAL_TYPE_P (type)
2379 || TYPE_OVERFLOW_WRAPS (type))
2380 (negate (view_convert @1))
2381 (view_convert (negate @1))))
2383 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2386 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2387 (if (!ANY_INTEGRAL_TYPE_P (type)
2388 || TYPE_OVERFLOW_WRAPS (type))
2389 (negate (view_convert @1))
2390 (view_convert (negate @1))))
2392 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2394 /* (A +- B) + (C - A) -> C +- B */
2395 /* (A + B) - (A - C) -> B + C */
2396 /* More cases are handled with comparisons. */
2398 (plus:c (plus:c @0 @1) (minus @2 @0))
2401 (plus:c (minus @0 @1) (minus @2 @0))
2404 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2405 (if (TYPE_OVERFLOW_UNDEFINED (type)
2406 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2407 (pointer_diff @2 @1)))
2409 (minus (plus:c @0 @1) (minus @0 @2))
2412 /* (A +- CST1) +- CST2 -> A + CST3
2413 Use view_convert because it is safe for vectors and equivalent for
2415 (for outer_op (plus minus)
2416 (for inner_op (plus minus)
2417 neg_inner_op (minus plus)
2419 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2421 /* If one of the types wraps, use that one. */
2422 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2423 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2424 forever if something doesn't simplify into a constant. */
2425 (if (!CONSTANT_CLASS_P (@0))
2426 (if (outer_op == PLUS_EXPR)
2427 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2428 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2429 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2430 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2431 (if (outer_op == PLUS_EXPR)
2432 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2433 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2434 /* If the constant operation overflows we cannot do the transform
2435 directly as we would introduce undefined overflow, for example
2436 with (a - 1) + INT_MIN. */
2437 (if (types_match (type, @0))
2438 (with { tree cst = const_binop (outer_op == inner_op
2439 ? PLUS_EXPR : MINUS_EXPR,
2441 (if (cst && !TREE_OVERFLOW (cst))
2442 (inner_op @0 { cst; } )
2443 /* X+INT_MAX+1 is X-INT_MIN. */
2444 (if (INTEGRAL_TYPE_P (type) && cst
2445 && wi::to_wide (cst) == wi::min_value (type))
2446 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2447 /* Last resort, use some unsigned type. */
2448 (with { tree utype = unsigned_type_for (type); }
2450 (view_convert (inner_op
2451 (view_convert:utype @0)
2453 { drop_tree_overflow (cst); }))))))))))))))
2455 /* (CST1 - A) +- CST2 -> CST3 - A */
2456 (for outer_op (plus minus)
2458 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2459 /* If one of the types wraps, use that one. */
2460 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2461 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2462 forever if something doesn't simplify into a constant. */
2463 (if (!CONSTANT_CLASS_P (@0))
2464 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2465 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2466 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2467 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2468 (if (types_match (type, @0))
2469 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2470 (if (cst && !TREE_OVERFLOW (cst))
2471 (minus { cst; } @0))))))))
2473 /* CST1 - (CST2 - A) -> CST3 + A
2474 Use view_convert because it is safe for vectors and equivalent for
2477 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2478 /* If one of the types wraps, use that one. */
2479 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2480 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2481 forever if something doesn't simplify into a constant. */
2482 (if (!CONSTANT_CLASS_P (@0))
2483 (plus (view_convert @0) (minus @1 (view_convert @2))))
2484 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2485 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2486 (view_convert (plus @0 (minus (view_convert @1) @2)))
2487 (if (types_match (type, @0))
2488 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2489 (if (cst && !TREE_OVERFLOW (cst))
2490 (plus { cst; } @0)))))))
2492 /* ((T)(A)) + CST -> (T)(A + CST) */
2495 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2496 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2497 && TREE_CODE (type) == INTEGER_TYPE
2498 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2499 && int_fits_type_p (@1, TREE_TYPE (@0)))
2500 /* Perform binary operation inside the cast if the constant fits
2501 and (A + CST)'s range does not overflow. */
2504 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2505 max_ovf = wi::OVF_OVERFLOW;
2506 tree inner_type = TREE_TYPE (@0);
2509 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2510 TYPE_SIGN (inner_type));
2512 wide_int wmin0, wmax0;
2513 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2515 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2516 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2519 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2520 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2524 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2526 (for op (plus minus)
2528 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2529 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2530 && TREE_CODE (type) == INTEGER_TYPE
2531 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2532 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2533 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2534 && TYPE_OVERFLOW_WRAPS (type))
2535 (plus (convert @0) (op @2 (convert @1))))))
2538 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2539 to a simple value. */
2541 (for op (plus minus)
2543 (op (convert @0) (convert @1))
2544 (if (INTEGRAL_TYPE_P (type)
2545 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2546 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2547 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2548 && !TYPE_OVERFLOW_TRAPS (type)
2549 && !TYPE_OVERFLOW_SANITIZED (type))
2550 (convert (op! @0 @1)))))
2555 (plus:c (bit_not @0) @0)
2556 (if (!TYPE_OVERFLOW_TRAPS (type))
2557 { build_all_ones_cst (type); }))
2561 (plus (convert? (bit_not @0)) integer_each_onep)
2562 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2563 (negate (convert @0))))
2567 (minus (convert? (negate @0)) integer_each_onep)
2568 (if (!TYPE_OVERFLOW_TRAPS (type)
2569 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2570 (bit_not (convert @0))))
2574 (minus integer_all_onesp @0)
2577 /* (T)(P + A) - (T)P -> (T) A */
2579 (minus (convert (plus:c @@0 @1))
2581 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2582 /* For integer types, if A has a smaller type
2583 than T the result depends on the possible
2585 E.g. T=size_t, A=(unsigned)429497295, P>0.
2586 However, if an overflow in P + A would cause
2587 undefined behavior, we can assume that there
2589 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2590 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2593 (minus (convert (pointer_plus @@0 @1))
2595 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2596 /* For pointer types, if the conversion of A to the
2597 final type requires a sign- or zero-extension,
2598 then we have to punt - it is not defined which
2600 || (POINTER_TYPE_P (TREE_TYPE (@0))
2601 && TREE_CODE (@1) == INTEGER_CST
2602 && tree_int_cst_sign_bit (@1) == 0))
2605 (pointer_diff (pointer_plus @@0 @1) @0)
2606 /* The second argument of pointer_plus must be interpreted as signed, and
2607 thus sign-extended if necessary. */
2608 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2609 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2610 second arg is unsigned even when we need to consider it as signed,
2611 we don't want to diagnose overflow here. */
2612 (convert (view_convert:stype @1))))
2614 /* (T)P - (T)(P + A) -> -(T) A */
2616 (minus (convert? @0)
2617 (convert (plus:c @@0 @1)))
2618 (if (INTEGRAL_TYPE_P (type)
2619 && TYPE_OVERFLOW_UNDEFINED (type)
2620 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2621 (with { tree utype = unsigned_type_for (type); }
2622 (convert (negate (convert:utype @1))))
2623 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2624 /* For integer types, if A has a smaller type
2625 than T the result depends on the possible
2627 E.g. T=size_t, A=(unsigned)429497295, P>0.
2628 However, if an overflow in P + A would cause
2629 undefined behavior, we can assume that there
2631 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2632 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2633 (negate (convert @1)))))
2636 (convert (pointer_plus @@0 @1)))
2637 (if (INTEGRAL_TYPE_P (type)
2638 && TYPE_OVERFLOW_UNDEFINED (type)
2639 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2640 (with { tree utype = unsigned_type_for (type); }
2641 (convert (negate (convert:utype @1))))
2642 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2643 /* For pointer types, if the conversion of A to the
2644 final type requires a sign- or zero-extension,
2645 then we have to punt - it is not defined which
2647 || (POINTER_TYPE_P (TREE_TYPE (@0))
2648 && TREE_CODE (@1) == INTEGER_CST
2649 && tree_int_cst_sign_bit (@1) == 0))
2650 (negate (convert @1)))))
2652 (pointer_diff @0 (pointer_plus @@0 @1))
2653 /* The second argument of pointer_plus must be interpreted as signed, and
2654 thus sign-extended if necessary. */
2655 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2656 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2657 second arg is unsigned even when we need to consider it as signed,
2658 we don't want to diagnose overflow here. */
2659 (negate (convert (view_convert:stype @1)))))
2661 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2663 (minus (convert (plus:c @@0 @1))
2664 (convert (plus:c @0 @2)))
2665 (if (INTEGRAL_TYPE_P (type)
2666 && TYPE_OVERFLOW_UNDEFINED (type)
2667 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2668 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2669 (with { tree utype = unsigned_type_for (type); }
2670 (convert (minus (convert:utype @1) (convert:utype @2))))
2671 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2672 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2673 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2674 /* For integer types, if A has a smaller type
2675 than T the result depends on the possible
2677 E.g. T=size_t, A=(unsigned)429497295, P>0.
2678 However, if an overflow in P + A would cause
2679 undefined behavior, we can assume that there
2681 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2682 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2683 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2684 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2685 (minus (convert @1) (convert @2)))))
2687 (minus (convert (pointer_plus @@0 @1))
2688 (convert (pointer_plus @0 @2)))
2689 (if (INTEGRAL_TYPE_P (type)
2690 && TYPE_OVERFLOW_UNDEFINED (type)
2691 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2692 (with { tree utype = unsigned_type_for (type); }
2693 (convert (minus (convert:utype @1) (convert:utype @2))))
2694 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2695 /* For pointer types, if the conversion of A to the
2696 final type requires a sign- or zero-extension,
2697 then we have to punt - it is not defined which
2699 || (POINTER_TYPE_P (TREE_TYPE (@0))
2700 && TREE_CODE (@1) == INTEGER_CST
2701 && tree_int_cst_sign_bit (@1) == 0
2702 && TREE_CODE (@2) == INTEGER_CST
2703 && tree_int_cst_sign_bit (@2) == 0))
2704 (minus (convert @1) (convert @2)))))
2706 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2707 (pointer_diff @0 @1))
2709 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2710 /* The second argument of pointer_plus must be interpreted as signed, and
2711 thus sign-extended if necessary. */
2712 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2713 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2714 second arg is unsigned even when we need to consider it as signed,
2715 we don't want to diagnose overflow here. */
2716 (minus (convert (view_convert:stype @1))
2717 (convert (view_convert:stype @2)))))))
2719 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2720 Modeled after fold_plusminus_mult_expr. */
2721 (if (!TYPE_SATURATING (type)
2722 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2723 (for plusminus (plus minus)
2725 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2726 (if (!ANY_INTEGRAL_TYPE_P (type)
2727 || TYPE_OVERFLOW_WRAPS (type)
2728 || (INTEGRAL_TYPE_P (type)
2729 && tree_expr_nonzero_p (@0)
2730 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2731 (if (single_use (@3) || single_use (@4))
2732 /* If @1 +- @2 is constant require a hard single-use on either
2733 original operand (but not on both). */
2734 (mult (plusminus @1 @2) @0)
2736 (mult! (plusminus @1 @2) @0)
2739 /* We cannot generate constant 1 for fract. */
2740 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2742 (plusminus @0 (mult:c@3 @0 @2))
2743 (if ((!ANY_INTEGRAL_TYPE_P (type)
2744 || TYPE_OVERFLOW_WRAPS (type)
2745 /* For @0 + @0*@2 this transformation would introduce UB
2746 (where there was none before) for @0 in [-1,0] and @2 max.
2747 For @0 - @0*@2 this transformation would introduce UB
2748 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2749 || (INTEGRAL_TYPE_P (type)
2750 && ((tree_expr_nonzero_p (@0)
2751 && expr_not_equal_to (@0,
2752 wi::minus_one (TYPE_PRECISION (type))))
2753 || (plusminus == PLUS_EXPR
2754 ? expr_not_equal_to (@2,
2755 wi::max_value (TYPE_PRECISION (type), SIGNED))
2756 /* Let's ignore the @0 -1 and @2 min case. */
2757 : (expr_not_equal_to (@2,
2758 wi::min_value (TYPE_PRECISION (type), SIGNED))
2759 && expr_not_equal_to (@2,
2760 wi::min_value (TYPE_PRECISION (type), SIGNED)
2763 (mult (plusminus { build_one_cst (type); } @2) @0)))
2765 (plusminus (mult:c@3 @0 @2) @0)
2766 (if ((!ANY_INTEGRAL_TYPE_P (type)
2767 || TYPE_OVERFLOW_WRAPS (type)
2768 /* For @0*@2 + @0 this transformation would introduce UB
2769 (where there was none before) for @0 in [-1,0] and @2 max.
2770 For @0*@2 - @0 this transformation would introduce UB
2771 for @0 0 and @2 min. */
2772 || (INTEGRAL_TYPE_P (type)
2773 && ((tree_expr_nonzero_p (@0)
2774 && (plusminus == MINUS_EXPR
2775 || expr_not_equal_to (@0,
2776 wi::minus_one (TYPE_PRECISION (type)))))
2777 || expr_not_equal_to (@2,
2778 (plusminus == PLUS_EXPR
2779 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2780 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2782 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2785 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2786 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2788 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2789 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2790 && tree_fits_uhwi_p (@1)
2791 && tree_to_uhwi (@1) < element_precision (type)
2792 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2793 || optab_handler (smul_optab,
2794 TYPE_MODE (type)) != CODE_FOR_nothing))
2795 (with { tree t = type;
2796 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2797 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2798 element_precision (type));
2800 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2802 cst = build_uniform_cst (t, cst); }
2803 (convert (mult (convert:t @0) { cst; })))))
2805 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2806 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2807 && tree_fits_uhwi_p (@1)
2808 && tree_to_uhwi (@1) < element_precision (type)
2809 && tree_fits_uhwi_p (@2)
2810 && tree_to_uhwi (@2) < element_precision (type)
2811 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2812 || optab_handler (smul_optab,
2813 TYPE_MODE (type)) != CODE_FOR_nothing))
2814 (with { tree t = type;
2815 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2816 unsigned int prec = element_precision (type);
2817 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2818 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2819 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2821 cst = build_uniform_cst (t, cst); }
2822 (convert (mult (convert:t @0) { cst; })))))
2825 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2827 (for minmax (min max FMIN_ALL FMAX_ALL)
2831 /* min(max(x,y),y) -> y. */
2833 (min:c (max:c @0 @1) @1)
2835 /* max(min(x,y),y) -> y. */
2837 (max:c (min:c @0 @1) @1)
2839 /* max(a,-a) -> abs(a). */
2841 (max:c @0 (negate @0))
2842 (if (TREE_CODE (type) != COMPLEX_TYPE
2843 && (! ANY_INTEGRAL_TYPE_P (type)
2844 || TYPE_OVERFLOW_UNDEFINED (type)))
2846 /* min(a,-a) -> -abs(a). */
2848 (min:c @0 (negate @0))
2849 (if (TREE_CODE (type) != COMPLEX_TYPE
2850 && (! ANY_INTEGRAL_TYPE_P (type)
2851 || TYPE_OVERFLOW_UNDEFINED (type)))
2856 (if (INTEGRAL_TYPE_P (type)
2857 && TYPE_MIN_VALUE (type)
2858 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2860 (if (INTEGRAL_TYPE_P (type)
2861 && TYPE_MAX_VALUE (type)
2862 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2867 (if (INTEGRAL_TYPE_P (type)
2868 && TYPE_MAX_VALUE (type)
2869 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2871 (if (INTEGRAL_TYPE_P (type)
2872 && TYPE_MIN_VALUE (type)
2873 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2876 /* max (a, a + CST) -> a + CST where CST is positive. */
2877 /* max (a, a + CST) -> a where CST is negative. */
2879 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2880 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2881 (if (tree_int_cst_sgn (@1) > 0)
2885 /* min (a, a + CST) -> a where CST is positive. */
2886 /* min (a, a + CST) -> a + CST where CST is negative. */
2888 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2889 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2890 (if (tree_int_cst_sgn (@1) > 0)
2894 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2895 and the outer convert demotes the expression back to x's type. */
2896 (for minmax (min max)
2898 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2899 (if (INTEGRAL_TYPE_P (type)
2900 && types_match (@1, type) && int_fits_type_p (@2, type)
2901 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2902 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2903 (minmax @1 (convert @2)))))
2905 (for minmax (FMIN_ALL FMAX_ALL)
2906 /* If either argument is NaN, return the other one. Avoid the
2907 transformation if we get (and honor) a signalling NaN. */
2909 (minmax:c @0 REAL_CST@1)
2910 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2911 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2913 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2914 functions to return the numeric arg if the other one is NaN.
2915 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2916 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2917 worry about it either. */
2918 (if (flag_finite_math_only)
2925 /* min (-A, -B) -> -max (A, B) */
2926 (for minmax (min max FMIN_ALL FMAX_ALL)
2927 maxmin (max min FMAX_ALL FMIN_ALL)
2929 (minmax (negate:s@2 @0) (negate:s@3 @1))
2930 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2931 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2932 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2933 (negate (maxmin @0 @1)))))
2934 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2935 MAX (~X, ~Y) -> ~MIN (X, Y) */
2936 (for minmax (min max)
2939 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2940 (bit_not (maxmin @0 @1))))
2942 /* MIN (X, Y) == X -> X <= Y */
2943 (for minmax (min min max max)
2947 (cmp:c (minmax:c @0 @1) @0)
2948 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2950 /* MIN (X, 5) == 0 -> X == 0
2951 MIN (X, 5) == 7 -> false */
2954 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2955 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2956 TYPE_SIGN (TREE_TYPE (@0))))
2957 { constant_boolean_node (cmp == NE_EXPR, type); }
2958 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2959 TYPE_SIGN (TREE_TYPE (@0))))
2963 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2964 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2965 TYPE_SIGN (TREE_TYPE (@0))))
2966 { constant_boolean_node (cmp == NE_EXPR, type); }
2967 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2968 TYPE_SIGN (TREE_TYPE (@0))))
2970 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2971 (for minmax (min min max max min min max max )
2972 cmp (lt le gt ge gt ge lt le )
2973 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2975 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2976 (comb (cmp @0 @2) (cmp @1 @2))))
2978 /* X <= MAX(X, Y) -> true
2979 X > MAX(X, Y) -> false
2980 X >= MIN(X, Y) -> true
2981 X < MIN(X, Y) -> false */
2982 (for minmax (min min max max )
2985 (cmp @0 (minmax:c @0 @1))
2986 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
2988 /* Undo fancy way of writing max/min or other ?: expressions,
2989 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2990 People normally use ?: and that is what we actually try to optimize. */
2991 (for cmp (simple_comparison)
2993 (minus @0 (bit_and:c (minus @0 @1)
2994 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2995 (if (INTEGRAL_TYPE_P (type)
2996 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2997 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2998 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2999 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3000 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3001 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3002 (cond (cmp @2 @3) @1 @0)))
3004 (plus:c @0 (bit_and:c (minus @1 @0)
3005 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3006 (if (INTEGRAL_TYPE_P (type)
3007 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3008 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3009 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3010 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3011 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3012 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3013 (cond (cmp @2 @3) @1 @0)))
3014 /* Similarly with ^ instead of - though in that case with :c. */
3016 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3017 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3018 (if (INTEGRAL_TYPE_P (type)
3019 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3020 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3021 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3022 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3023 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3024 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3025 (cond (cmp @2 @3) @1 @0))))
3027 /* Simplifications of shift and rotates. */
3029 (for rotate (lrotate rrotate)
3031 (rotate integer_all_onesp@0 @1)
3034 /* Optimize -1 >> x for arithmetic right shifts. */
3036 (rshift integer_all_onesp@0 @1)
3037 (if (!TYPE_UNSIGNED (type))
3040 /* Optimize (x >> c) << c into x & (-1<<c). */
3042 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3043 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3044 /* It doesn't matter if the right shift is arithmetic or logical. */
3045 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3048 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3049 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3050 /* Allow intermediate conversion to integral type with whatever sign, as
3051 long as the low TYPE_PRECISION (type)
3052 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3053 && INTEGRAL_TYPE_P (type)
3054 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3055 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3056 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3057 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3058 || wi::geu_p (wi::to_wide (@1),
3059 TYPE_PRECISION (type)
3060 - TYPE_PRECISION (TREE_TYPE (@2)))))
3061 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3063 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3066 (rshift (lshift @0 INTEGER_CST@1) @1)
3067 (if (TYPE_UNSIGNED (type)
3068 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3069 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3071 /* Optimize x >> x into 0 */
3074 { build_zero_cst (type); })
3076 (for shiftrotate (lrotate rrotate lshift rshift)
3078 (shiftrotate @0 integer_zerop)
3081 (shiftrotate integer_zerop@0 @1)
3083 /* Prefer vector1 << scalar to vector1 << vector2
3084 if vector2 is uniform. */
3085 (for vec (VECTOR_CST CONSTRUCTOR)
3087 (shiftrotate @0 vec@1)
3088 (with { tree tem = uniform_vector_p (@1); }
3090 (shiftrotate @0 { tem; }))))))
3092 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3093 Y is 0. Similarly for X >> Y. */
3095 (for shift (lshift rshift)
3097 (shift @0 SSA_NAME@1)
3098 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3100 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3101 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3103 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3107 /* Rewrite an LROTATE_EXPR by a constant into an
3108 RROTATE_EXPR by a new constant. */
3110 (lrotate @0 INTEGER_CST@1)
3111 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3112 build_int_cst (TREE_TYPE (@1),
3113 element_precision (type)), @1); }))
3115 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3116 (for op (lrotate rrotate rshift lshift)
3118 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3119 (with { unsigned int prec = element_precision (type); }
3120 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3121 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3122 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3123 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3124 (with { unsigned int low = (tree_to_uhwi (@1)
3125 + tree_to_uhwi (@2)); }
3126 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3127 being well defined. */
3129 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3130 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3131 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3132 { build_zero_cst (type); }
3133 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3134 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3137 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3139 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3140 (if ((wi::to_wide (@1) & 1) != 0)
3141 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3142 { build_zero_cst (type); }))
3144 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3145 either to false if D is smaller (unsigned comparison) than C, or to
3146 x == log2 (D) - log2 (C). Similarly for right shifts. */
3150 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3151 (with { int c1 = wi::clz (wi::to_wide (@1));
3152 int c2 = wi::clz (wi::to_wide (@2)); }
3154 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3155 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3157 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3158 (if (tree_int_cst_sgn (@1) > 0)
3159 (with { int c1 = wi::clz (wi::to_wide (@1));
3160 int c2 = wi::clz (wi::to_wide (@2)); }
3162 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3163 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3165 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3166 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3170 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3171 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3173 || (!integer_zerop (@2)
3174 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3175 { constant_boolean_node (cmp == NE_EXPR, type); }
3176 (if (!integer_zerop (@2)
3177 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3178 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3180 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3181 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3182 if the new mask might be further optimized. */
3183 (for shift (lshift rshift)
3185 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3187 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3188 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3189 && tree_fits_uhwi_p (@1)
3190 && tree_to_uhwi (@1) > 0
3191 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3194 unsigned int shiftc = tree_to_uhwi (@1);
3195 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3196 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3197 tree shift_type = TREE_TYPE (@3);
3200 if (shift == LSHIFT_EXPR)
3201 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3202 else if (shift == RSHIFT_EXPR
3203 && type_has_mode_precision_p (shift_type))
3205 prec = TYPE_PRECISION (TREE_TYPE (@3));
3207 /* See if more bits can be proven as zero because of
3210 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3212 tree inner_type = TREE_TYPE (@0);
3213 if (type_has_mode_precision_p (inner_type)
3214 && TYPE_PRECISION (inner_type) < prec)
3216 prec = TYPE_PRECISION (inner_type);
3217 /* See if we can shorten the right shift. */
3219 shift_type = inner_type;
3220 /* Otherwise X >> C1 is all zeros, so we'll optimize
3221 it into (X, 0) later on by making sure zerobits
3225 zerobits = HOST_WIDE_INT_M1U;
3228 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3229 zerobits <<= prec - shiftc;
3231 /* For arithmetic shift if sign bit could be set, zerobits
3232 can contain actually sign bits, so no transformation is
3233 possible, unless MASK masks them all away. In that
3234 case the shift needs to be converted into logical shift. */
3235 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3236 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3238 if ((mask & zerobits) == 0)
3239 shift_type = unsigned_type_for (TREE_TYPE (@3));
3245 /* ((X << 16) & 0xff00) is (X, 0). */
3246 (if ((mask & zerobits) == mask)
3247 { build_int_cst (type, 0); }
3248 (with { newmask = mask | zerobits; }
3249 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3252 /* Only do the transformation if NEWMASK is some integer
3254 for (prec = BITS_PER_UNIT;
3255 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3256 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3259 (if (prec < HOST_BITS_PER_WIDE_INT
3260 || newmask == HOST_WIDE_INT_M1U)
3262 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3263 (if (!tree_int_cst_equal (newmaskt, @2))
3264 (if (shift_type != TREE_TYPE (@3))
3265 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3266 (bit_and @4 { newmaskt; })))))))))))))
3268 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3269 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3270 (for shift (lshift rshift)
3271 (for bit_op (bit_and bit_xor bit_ior)
3273 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3274 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3275 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3277 (bit_op (shift (convert @0) @1) { mask; })))))))
3279 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3281 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3282 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3283 && (element_precision (TREE_TYPE (@0))
3284 <= element_precision (TREE_TYPE (@1))
3285 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3287 { tree shift_type = TREE_TYPE (@0); }
3288 (convert (rshift (convert:shift_type @1) @2)))))
3290 /* ~(~X >>r Y) -> X >>r Y
3291 ~(~X <<r Y) -> X <<r Y */
3292 (for rotate (lrotate rrotate)
3294 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3295 (if ((element_precision (TREE_TYPE (@0))
3296 <= element_precision (TREE_TYPE (@1))
3297 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3298 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3299 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3301 { tree rotate_type = TREE_TYPE (@0); }
3302 (convert (rotate (convert:rotate_type @1) @2))))))
3304 /* Simplifications of conversions. */
3306 /* Basic strip-useless-type-conversions / strip_nops. */
3307 (for cvt (convert view_convert float fix_trunc)
3310 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3311 || (GENERIC && type == TREE_TYPE (@0)))
3314 /* Contract view-conversions. */
3316 (view_convert (view_convert @0))
3319 /* For integral conversions with the same precision or pointer
3320 conversions use a NOP_EXPR instead. */
3323 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3324 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3325 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3328 /* Strip inner integral conversions that do not change precision or size, or
3329 zero-extend while keeping the same size (for bool-to-char). */
3331 (view_convert (convert@0 @1))
3332 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3333 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3334 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3335 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3336 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3337 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3340 /* Simplify a view-converted empty constructor. */
3342 (view_convert CONSTRUCTOR@0)
3343 (if (TREE_CODE (@0) != SSA_NAME
3344 && CONSTRUCTOR_NELTS (@0) == 0)
3345 { build_zero_cst (type); }))
3347 /* Re-association barriers around constants and other re-association
3348 barriers can be removed. */
3350 (paren CONSTANT_CLASS_P@0)
3353 (paren (paren@1 @0))
3356 /* Handle cases of two conversions in a row. */
3357 (for ocvt (convert float fix_trunc)
3358 (for icvt (convert float)
3363 tree inside_type = TREE_TYPE (@0);
3364 tree inter_type = TREE_TYPE (@1);
3365 int inside_int = INTEGRAL_TYPE_P (inside_type);
3366 int inside_ptr = POINTER_TYPE_P (inside_type);
3367 int inside_float = FLOAT_TYPE_P (inside_type);
3368 int inside_vec = VECTOR_TYPE_P (inside_type);
3369 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3370 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3371 int inter_int = INTEGRAL_TYPE_P (inter_type);
3372 int inter_ptr = POINTER_TYPE_P (inter_type);
3373 int inter_float = FLOAT_TYPE_P (inter_type);
3374 int inter_vec = VECTOR_TYPE_P (inter_type);
3375 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3376 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3377 int final_int = INTEGRAL_TYPE_P (type);
3378 int final_ptr = POINTER_TYPE_P (type);
3379 int final_float = FLOAT_TYPE_P (type);
3380 int final_vec = VECTOR_TYPE_P (type);
3381 unsigned int final_prec = TYPE_PRECISION (type);
3382 int final_unsignedp = TYPE_UNSIGNED (type);
3385 /* In addition to the cases of two conversions in a row
3386 handled below, if we are converting something to its own
3387 type via an object of identical or wider precision, neither
3388 conversion is needed. */
3389 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3391 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3392 && (((inter_int || inter_ptr) && final_int)
3393 || (inter_float && final_float))
3394 && inter_prec >= final_prec)
3397 /* Likewise, if the intermediate and initial types are either both
3398 float or both integer, we don't need the middle conversion if the
3399 former is wider than the latter and doesn't change the signedness
3400 (for integers). Avoid this if the final type is a pointer since
3401 then we sometimes need the middle conversion. */
3402 (if (((inter_int && inside_int) || (inter_float && inside_float))
3403 && (final_int || final_float)
3404 && inter_prec >= inside_prec
3405 && (inter_float || inter_unsignedp == inside_unsignedp))
3408 /* If we have a sign-extension of a zero-extended value, we can
3409 replace that by a single zero-extension. Likewise if the
3410 final conversion does not change precision we can drop the
3411 intermediate conversion. */
3412 (if (inside_int && inter_int && final_int
3413 && ((inside_prec < inter_prec && inter_prec < final_prec
3414 && inside_unsignedp && !inter_unsignedp)
3415 || final_prec == inter_prec))
3418 /* Two conversions in a row are not needed unless:
3419 - some conversion is floating-point (overstrict for now), or
3420 - some conversion is a vector (overstrict for now), or
3421 - the intermediate type is narrower than both initial and
3423 - the intermediate type and innermost type differ in signedness,
3424 and the outermost type is wider than the intermediate, or
3425 - the initial type is a pointer type and the precisions of the
3426 intermediate and final types differ, or
3427 - the final type is a pointer type and the precisions of the
3428 initial and intermediate types differ. */
3429 (if (! inside_float && ! inter_float && ! final_float
3430 && ! inside_vec && ! inter_vec && ! final_vec
3431 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3432 && ! (inside_int && inter_int
3433 && inter_unsignedp != inside_unsignedp
3434 && inter_prec < final_prec)
3435 && ((inter_unsignedp && inter_prec > inside_prec)
3436 == (final_unsignedp && final_prec > inter_prec))
3437 && ! (inside_ptr && inter_prec != final_prec)
3438 && ! (final_ptr && inside_prec != inter_prec))
3441 /* A truncation to an unsigned type (a zero-extension) should be
3442 canonicalized as bitwise and of a mask. */
3443 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3444 && final_int && inter_int && inside_int
3445 && final_prec == inside_prec
3446 && final_prec > inter_prec
3448 (convert (bit_and @0 { wide_int_to_tree
3450 wi::mask (inter_prec, false,
3451 TYPE_PRECISION (inside_type))); })))
3453 /* If we are converting an integer to a floating-point that can
3454 represent it exactly and back to an integer, we can skip the
3455 floating-point conversion. */
3456 (if (GIMPLE /* PR66211 */
3457 && inside_int && inter_float && final_int &&
3458 (unsigned) significand_size (TYPE_MODE (inter_type))
3459 >= inside_prec - !inside_unsignedp)
3462 /* If we have a narrowing conversion to an integral type that is fed by a
3463 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3464 masks off bits outside the final type (and nothing else). */
3466 (convert (bit_and @0 INTEGER_CST@1))
3467 (if (INTEGRAL_TYPE_P (type)
3468 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3469 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3470 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3471 TYPE_PRECISION (type)), 0))
3475 /* (X /[ex] A) * A -> X. */
3477 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3480 /* Simplify (A / B) * B + (A % B) -> A. */
3481 (for div (trunc_div ceil_div floor_div round_div)
3482 mod (trunc_mod ceil_mod floor_mod round_mod)
3484 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3487 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3488 (for op (plus minus)
3490 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3491 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3492 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3495 wi::overflow_type overflow;
3496 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3497 TYPE_SIGN (type), &overflow);
3499 (if (types_match (type, TREE_TYPE (@2))
3500 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3501 (op @0 { wide_int_to_tree (type, mul); })
3502 (with { tree utype = unsigned_type_for (type); }
3503 (convert (op (convert:utype @0)
3504 (mult (convert:utype @1) (convert:utype @2))))))))))
3506 /* Canonicalization of binary operations. */
3508 /* Convert X + -C into X - C. */
3510 (plus @0 REAL_CST@1)
3511 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3512 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3513 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3514 (minus @0 { tem; })))))
3516 /* Convert x+x into x*2. */
3519 (if (SCALAR_FLOAT_TYPE_P (type))
3520 (mult @0 { build_real (type, dconst2); })
3521 (if (INTEGRAL_TYPE_P (type))
3522 (mult @0 { build_int_cst (type, 2); }))))
3526 (minus integer_zerop @1)
3529 (pointer_diff integer_zerop @1)
3530 (negate (convert @1)))
3532 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3533 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3534 (-ARG1 + ARG0) reduces to -ARG1. */
3536 (minus real_zerop@0 @1)
3537 (if (fold_real_zero_addition_p (type, @0, 0))
3540 /* Transform x * -1 into -x. */
3542 (mult @0 integer_minus_onep)
3545 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3546 signed overflow for CST != 0 && CST != -1. */
3548 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3549 (if (TREE_CODE (@2) != INTEGER_CST
3551 && !integer_zerop (@1) && !integer_minus_onep (@1))
3552 (mult (mult @0 @2) @1)))
3554 /* True if we can easily extract the real and imaginary parts of a complex
3556 (match compositional_complex
3557 (convert? (complex @0 @1)))
3559 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3561 (complex (realpart @0) (imagpart @0))
3564 (realpart (complex @0 @1))
3567 (imagpart (complex @0 @1))
3570 /* Sometimes we only care about half of a complex expression. */
3572 (realpart (convert?:s (conj:s @0)))
3573 (convert (realpart @0)))
3575 (imagpart (convert?:s (conj:s @0)))
3576 (convert (negate (imagpart @0))))
3577 (for part (realpart imagpart)
3578 (for op (plus minus)
3580 (part (convert?:s@2 (op:s @0 @1)))
3581 (convert (op (part @0) (part @1))))))
3583 (realpart (convert?:s (CEXPI:s @0)))
3586 (imagpart (convert?:s (CEXPI:s @0)))
3589 /* conj(conj(x)) -> x */
3591 (conj (convert? (conj @0)))
3592 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3595 /* conj({x,y}) -> {x,-y} */
3597 (conj (convert?:s (complex:s @0 @1)))
3598 (with { tree itype = TREE_TYPE (type); }
3599 (complex (convert:itype @0) (negate (convert:itype @1)))))
3601 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3602 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3607 (bswap (bit_not (bswap @0)))
3609 (for bitop (bit_xor bit_ior bit_and)
3611 (bswap (bitop:c (bswap @0) @1))
3612 (bitop @0 (bswap @1)))))
3615 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3617 /* Simplify constant conditions.
3618 Only optimize constant conditions when the selected branch
3619 has the same type as the COND_EXPR. This avoids optimizing
3620 away "c ? x : throw", where the throw has a void type.
3621 Note that we cannot throw away the fold-const.c variant nor
3622 this one as we depend on doing this transform before possibly
3623 A ? B : B -> B triggers and the fold-const.c one can optimize
3624 0 ? A : B to B even if A has side-effects. Something
3625 genmatch cannot handle. */
3627 (cond INTEGER_CST@0 @1 @2)
3628 (if (integer_zerop (@0))
3629 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3631 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3634 (vec_cond VECTOR_CST@0 @1 @2)
3635 (if (integer_all_onesp (@0))
3637 (if (integer_zerop (@0))
3641 /* Sink unary operations to branches, but only if we do fold both. */
3642 (for op (negate bit_not abs absu)
3644 (op (vec_cond:s @0 @1 @2))
3645 (vec_cond @0 (op! @1) (op! @2))))
3647 /* Sink binary operation to branches, but only if we can fold it. */
3648 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3649 rdiv trunc_div ceil_div floor_div round_div
3650 trunc_mod ceil_mod floor_mod round_mod min max)
3651 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3653 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3654 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3656 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3658 (op (vec_cond:s @0 @1 @2) @3)
3659 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3661 (op @3 (vec_cond:s @0 @1 @2))
3662 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3665 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3666 Currently disabled after pass lvec because ARM understands
3667 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3669 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3670 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3671 (vec_cond (bit_and @0 @3) @1 @2)))
3673 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3674 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3675 (vec_cond (bit_ior @0 @3) @1 @2)))
3677 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3678 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3679 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3681 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3682 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3683 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3685 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3687 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3688 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3689 (vec_cond (bit_and @0 @1) @2 @3)))
3691 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3692 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3693 (vec_cond (bit_ior @0 @1) @2 @3)))
3695 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3696 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3697 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3699 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3700 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3701 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3703 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3704 types are compatible. */
3706 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3707 (if (VECTOR_BOOLEAN_TYPE_P (type)
3708 && types_match (type, TREE_TYPE (@0)))
3709 (if (integer_zerop (@1) && integer_all_onesp (@2))
3711 (if (integer_all_onesp (@1) && integer_zerop (@2))
3714 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3716 /* This pattern implements two kinds simplification:
3719 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3720 1) Conversions are type widening from smaller type.
3721 2) Const c1 equals to c2 after canonicalizing comparison.
3722 3) Comparison has tree code LT, LE, GT or GE.
3723 This specific pattern is needed when (cmp (convert x) c) may not
3724 be simplified by comparison patterns because of multiple uses of
3725 x. It also makes sense here because simplifying across multiple
3726 referred var is always benefitial for complicated cases.
3729 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3730 (for cmp (lt le gt ge eq)
3732 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3735 tree from_type = TREE_TYPE (@1);
3736 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3737 enum tree_code code = ERROR_MARK;
3739 if (INTEGRAL_TYPE_P (from_type)
3740 && int_fits_type_p (@2, from_type)
3741 && (types_match (c1_type, from_type)
3742 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3743 && (TYPE_UNSIGNED (from_type)
3744 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3745 && (types_match (c2_type, from_type)
3746 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3747 && (TYPE_UNSIGNED (from_type)
3748 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3752 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3754 /* X <= Y - 1 equals to X < Y. */
3757 /* X > Y - 1 equals to X >= Y. */
3761 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3763 /* X < Y + 1 equals to X <= Y. */
3766 /* X >= Y + 1 equals to X > Y. */
3770 if (code != ERROR_MARK
3771 || wi::to_widest (@2) == wi::to_widest (@3))
3773 if (cmp == LT_EXPR || cmp == LE_EXPR)
3775 if (cmp == GT_EXPR || cmp == GE_EXPR)
3779 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3780 else if (int_fits_type_p (@3, from_type))
3784 (if (code == MAX_EXPR)
3785 (convert (max @1 (convert @2)))
3786 (if (code == MIN_EXPR)
3787 (convert (min @1 (convert @2)))
3788 (if (code == EQ_EXPR)
3789 (convert (cond (eq @1 (convert @3))
3790 (convert:from_type @3) (convert:from_type @2)))))))))
3792 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3794 1) OP is PLUS or MINUS.
3795 2) CMP is LT, LE, GT or GE.
3796 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3798 This pattern also handles special cases like:
3800 A) Operand x is a unsigned to signed type conversion and c1 is
3801 integer zero. In this case,
3802 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3803 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3804 B) Const c1 may not equal to (C3 op' C2). In this case we also
3805 check equality for (c1+1) and (c1-1) by adjusting comparison
3808 TODO: Though signed type is handled by this pattern, it cannot be
3809 simplified at the moment because C standard requires additional
3810 type promotion. In order to match&simplify it here, the IR needs
3811 to be cleaned up by other optimizers, i.e, VRP. */
3812 (for op (plus minus)
3813 (for cmp (lt le gt ge)
3815 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3816 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3817 (if (types_match (from_type, to_type)
3818 /* Check if it is special case A). */
3819 || (TYPE_UNSIGNED (from_type)
3820 && !TYPE_UNSIGNED (to_type)
3821 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3822 && integer_zerop (@1)
3823 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3826 wi::overflow_type overflow = wi::OVF_NONE;
3827 enum tree_code code, cmp_code = cmp;
3829 wide_int c1 = wi::to_wide (@1);
3830 wide_int c2 = wi::to_wide (@2);
3831 wide_int c3 = wi::to_wide (@3);
3832 signop sgn = TYPE_SIGN (from_type);
3834 /* Handle special case A), given x of unsigned type:
3835 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3836 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3837 if (!types_match (from_type, to_type))
3839 if (cmp_code == LT_EXPR)
3841 if (cmp_code == GE_EXPR)
3843 c1 = wi::max_value (to_type);
3845 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3846 compute (c3 op' c2) and check if it equals to c1 with op' being
3847 the inverted operator of op. Make sure overflow doesn't happen
3848 if it is undefined. */
3849 if (op == PLUS_EXPR)
3850 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3852 real_c1 = wi::add (c3, c2, sgn, &overflow);
3855 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3857 /* Check if c1 equals to real_c1. Boundary condition is handled
3858 by adjusting comparison operation if necessary. */
3859 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3862 /* X <= Y - 1 equals to X < Y. */
3863 if (cmp_code == LE_EXPR)
3865 /* X > Y - 1 equals to X >= Y. */
3866 if (cmp_code == GT_EXPR)
3869 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3872 /* X < Y + 1 equals to X <= Y. */
3873 if (cmp_code == LT_EXPR)
3875 /* X >= Y + 1 equals to X > Y. */
3876 if (cmp_code == GE_EXPR)
3879 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3881 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3883 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3888 (if (code == MAX_EXPR)
3889 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3890 { wide_int_to_tree (from_type, c2); })
3891 (if (code == MIN_EXPR)
3892 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3893 { wide_int_to_tree (from_type, c2); })))))))))
3895 (for cnd (cond vec_cond)
3896 /* A ? B : (A ? X : C) -> A ? B : C. */
3898 (cnd @0 (cnd @0 @1 @2) @3)
3901 (cnd @0 @1 (cnd @0 @2 @3))
3903 /* A ? B : (!A ? C : X) -> A ? B : C. */
3904 /* ??? This matches embedded conditions open-coded because genmatch
3905 would generate matching code for conditions in separate stmts only.
3906 The following is still important to merge then and else arm cases
3907 from if-conversion. */
3909 (cnd @0 @1 (cnd @2 @3 @4))
3910 (if (inverse_conditions_p (@0, @2))
3913 (cnd @0 (cnd @1 @2 @3) @4)
3914 (if (inverse_conditions_p (@0, @1))
3917 /* A ? B : B -> B. */
3922 /* !A ? B : C -> A ? C : B. */
3924 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3927 /* -(type)!A -> (type)A - 1. */
3929 (negate (convert?:s (logical_inverted_value:s @0)))
3930 (if (INTEGRAL_TYPE_P (type)
3931 && TREE_CODE (type) != BOOLEAN_TYPE
3932 && TYPE_PRECISION (type) > 1
3933 && TREE_CODE (@0) == SSA_NAME
3934 && ssa_name_has_boolean_range (@0))
3935 (plus (convert:type @0) { build_all_ones_cst (type); })))
3937 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3938 return all -1 or all 0 results. */
3939 /* ??? We could instead convert all instances of the vec_cond to negate,
3940 but that isn't necessarily a win on its own. */
3942 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3943 (if (VECTOR_TYPE_P (type)
3944 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3945 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3946 && (TYPE_MODE (TREE_TYPE (type))
3947 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3948 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3950 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3952 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3953 (if (VECTOR_TYPE_P (type)
3954 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3955 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3956 && (TYPE_MODE (TREE_TYPE (type))
3957 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3958 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3961 /* Simplifications of comparisons. */
3963 /* See if we can reduce the magnitude of a constant involved in a
3964 comparison by changing the comparison code. This is a canonicalization
3965 formerly done by maybe_canonicalize_comparison_1. */
3969 (cmp @0 uniform_integer_cst_p@1)
3970 (with { tree cst = uniform_integer_cst_p (@1); }
3971 (if (tree_int_cst_sgn (cst) == -1)
3972 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3973 wide_int_to_tree (TREE_TYPE (cst),
3979 (cmp @0 uniform_integer_cst_p@1)
3980 (with { tree cst = uniform_integer_cst_p (@1); }
3981 (if (tree_int_cst_sgn (cst) == 1)
3982 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3983 wide_int_to_tree (TREE_TYPE (cst),
3984 wi::to_wide (cst) - 1)); })))))
3986 /* We can simplify a logical negation of a comparison to the
3987 inverted comparison. As we cannot compute an expression
3988 operator using invert_tree_comparison we have to simulate
3989 that with expression code iteration. */
3990 (for cmp (tcc_comparison)
3991 icmp (inverted_tcc_comparison)
3992 ncmp (inverted_tcc_comparison_with_nans)
3993 /* Ideally we'd like to combine the following two patterns
3994 and handle some more cases by using
3995 (logical_inverted_value (cmp @0 @1))
3996 here but for that genmatch would need to "inline" that.
3997 For now implement what forward_propagate_comparison did. */
3999 (bit_not (cmp @0 @1))
4000 (if (VECTOR_TYPE_P (type)
4001 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4002 /* Comparison inversion may be impossible for trapping math,
4003 invert_tree_comparison will tell us. But we can't use
4004 a computed operator in the replacement tree thus we have
4005 to play the trick below. */
4006 (with { enum tree_code ic = invert_tree_comparison
4007 (cmp, HONOR_NANS (@0)); }
4013 (bit_xor (cmp @0 @1) integer_truep)
4014 (with { enum tree_code ic = invert_tree_comparison
4015 (cmp, HONOR_NANS (@0)); }
4021 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4022 ??? The transformation is valid for the other operators if overflow
4023 is undefined for the type, but performing it here badly interacts
4024 with the transformation in fold_cond_expr_with_comparison which
4025 attempts to synthetize ABS_EXPR. */
4027 (for sub (minus pointer_diff)
4029 (cmp (sub@2 @0 @1) integer_zerop)
4030 (if (single_use (@2))
4033 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4034 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4037 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4038 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4039 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4040 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4041 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4042 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4043 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4045 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4046 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4047 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4048 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4049 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4051 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4052 signed arithmetic case. That form is created by the compiler
4053 often enough for folding it to be of value. One example is in
4054 computing loop trip counts after Operator Strength Reduction. */
4055 (for cmp (simple_comparison)
4056 scmp (swapped_simple_comparison)
4058 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4059 /* Handle unfolded multiplication by zero. */
4060 (if (integer_zerop (@1))
4062 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4063 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4065 /* If @1 is negative we swap the sense of the comparison. */
4066 (if (tree_int_cst_sgn (@1) < 0)
4070 /* For integral types with undefined overflow fold
4071 x * C1 == C2 into x == C2 / C1 or false.
4072 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4076 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4077 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4078 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4079 && wi::to_wide (@1) != 0)
4080 (with { widest_int quot; }
4081 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4082 TYPE_SIGN (TREE_TYPE (@0)), "))
4083 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4084 { constant_boolean_node (cmp == NE_EXPR, type); }))
4085 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4086 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4087 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4090 tree itype = TREE_TYPE (@0);
4091 int p = TYPE_PRECISION (itype);
4092 wide_int m = wi::one (p + 1) << p;
4093 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4094 wide_int i = wide_int::from (wi::mod_inv (a, m),
4095 p, TYPE_SIGN (itype));
4096 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4099 /* Simplify comparison of something with itself. For IEEE
4100 floating-point, we can only do some of these simplifications. */
4104 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4105 || ! HONOR_NANS (@0))
4106 { constant_boolean_node (true, type); }
4107 (if (cmp != EQ_EXPR)
4113 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4114 || ! HONOR_NANS (@0))
4115 { constant_boolean_node (false, type); })))
4116 (for cmp (unle unge uneq)
4119 { constant_boolean_node (true, type); }))
4120 (for cmp (unlt ungt)
4126 (if (!flag_trapping_math)
4127 { constant_boolean_node (false, type); }))
4129 /* x == ~x -> false */
4130 /* x != ~x -> true */
4133 (cmp:c @0 (bit_not @0))
4134 { constant_boolean_node (cmp == NE_EXPR, type); }))
4136 /* Fold ~X op ~Y as Y op X. */
4137 (for cmp (simple_comparison)
4139 (cmp (bit_not@2 @0) (bit_not@3 @1))
4140 (if (single_use (@2) && single_use (@3))
4143 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4144 (for cmp (simple_comparison)
4145 scmp (swapped_simple_comparison)
4147 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4148 (if (single_use (@2)
4149 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4150 (scmp @0 (bit_not @1)))))
4152 (for cmp (simple_comparison)
4153 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4155 (cmp (convert@2 @0) (convert? @1))
4156 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4157 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4158 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4159 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4160 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4163 tree type1 = TREE_TYPE (@1);
4164 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4166 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4167 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4168 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4169 type1 = float_type_node;
4170 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4171 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4172 type1 = double_type_node;
4175 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4176 ? TREE_TYPE (@0) : type1);
4178 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4179 (cmp (convert:newtype @0) (convert:newtype @1))))))
4183 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4185 /* a CMP (-0) -> a CMP 0 */
4186 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4187 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4188 /* x != NaN is always true, other ops are always false. */
4189 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4190 && ! HONOR_SNANS (@1))
4191 { constant_boolean_node (cmp == NE_EXPR, type); })
4192 /* Fold comparisons against infinity. */
4193 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4194 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4197 REAL_VALUE_TYPE max;
4198 enum tree_code code = cmp;
4199 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4201 code = swap_tree_comparison (code);
4204 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4205 (if (code == GT_EXPR
4206 && !(HONOR_NANS (@0) && flag_trapping_math))
4207 { constant_boolean_node (false, type); })
4208 (if (code == LE_EXPR)
4209 /* x <= +Inf is always true, if we don't care about NaNs. */
4210 (if (! HONOR_NANS (@0))
4211 { constant_boolean_node (true, type); }
4212 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4213 an "invalid" exception. */
4214 (if (!flag_trapping_math)
4216 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4217 for == this introduces an exception for x a NaN. */
4218 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4220 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4222 (lt @0 { build_real (TREE_TYPE (@0), max); })
4223 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4224 /* x < +Inf is always equal to x <= DBL_MAX. */
4225 (if (code == LT_EXPR)
4226 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4228 (ge @0 { build_real (TREE_TYPE (@0), max); })
4229 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4230 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4231 an exception for x a NaN so use an unordered comparison. */
4232 (if (code == NE_EXPR)
4233 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4234 (if (! HONOR_NANS (@0))
4236 (ge @0 { build_real (TREE_TYPE (@0), max); })
4237 (le @0 { build_real (TREE_TYPE (@0), max); }))
4239 (unge @0 { build_real (TREE_TYPE (@0), max); })
4240 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4242 /* If this is a comparison of a real constant with a PLUS_EXPR
4243 or a MINUS_EXPR of a real constant, we can convert it into a
4244 comparison with a revised real constant as long as no overflow
4245 occurs when unsafe_math_optimizations are enabled. */
4246 (if (flag_unsafe_math_optimizations)
4247 (for op (plus minus)
4249 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4252 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4253 TREE_TYPE (@1), @2, @1);
4255 (if (tem && !TREE_OVERFLOW (tem))
4256 (cmp @0 { tem; }))))))
4258 /* Likewise, we can simplify a comparison of a real constant with
4259 a MINUS_EXPR whose first operand is also a real constant, i.e.
4260 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4261 floating-point types only if -fassociative-math is set. */
4262 (if (flag_associative_math)
4264 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4265 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4266 (if (tem && !TREE_OVERFLOW (tem))
4267 (cmp { tem; } @1)))))
4269 /* Fold comparisons against built-in math functions. */
4270 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4273 (cmp (sq @0) REAL_CST@1)
4275 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4277 /* sqrt(x) < y is always false, if y is negative. */
4278 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4279 { constant_boolean_node (false, type); })
4280 /* sqrt(x) > y is always true, if y is negative and we
4281 don't care about NaNs, i.e. negative values of x. */
4282 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4283 { constant_boolean_node (true, type); })
4284 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4285 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4286 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4288 /* sqrt(x) < 0 is always false. */
4289 (if (cmp == LT_EXPR)
4290 { constant_boolean_node (false, type); })
4291 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4292 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4293 { constant_boolean_node (true, type); })
4294 /* sqrt(x) <= 0 -> x == 0. */
4295 (if (cmp == LE_EXPR)
4297 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4298 == or !=. In the last case:
4300 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4302 if x is negative or NaN. Due to -funsafe-math-optimizations,
4303 the results for other x follow from natural arithmetic. */
4305 (if ((cmp == LT_EXPR
4309 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4310 /* Give up for -frounding-math. */
4311 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4315 enum tree_code ncmp = cmp;
4316 const real_format *fmt
4317 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4318 real_arithmetic (&c2, MULT_EXPR,
4319 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4320 real_convert (&c2, fmt, &c2);
4321 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4322 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4323 if (!REAL_VALUE_ISINF (c2))
4325 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4326 build_real (TREE_TYPE (@0), c2));
4327 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4329 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4330 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4331 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4332 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4333 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4334 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4337 /* With rounding to even, sqrt of up to 3 different values
4338 gives the same normal result, so in some cases c2 needs
4340 REAL_VALUE_TYPE c2alt, tow;
4341 if (cmp == LT_EXPR || cmp == GE_EXPR)
4345 real_nextafter (&c2alt, fmt, &c2, &tow);
4346 real_convert (&c2alt, fmt, &c2alt);
4347 if (REAL_VALUE_ISINF (c2alt))
4351 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4352 build_real (TREE_TYPE (@0), c2alt));
4353 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4355 else if (real_equal (&TREE_REAL_CST (c3),
4356 &TREE_REAL_CST (@1)))
4362 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4363 (if (REAL_VALUE_ISINF (c2))
4364 /* sqrt(x) > y is x == +Inf, when y is very large. */
4365 (if (HONOR_INFINITIES (@0))
4366 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4367 { constant_boolean_node (false, type); })
4368 /* sqrt(x) > c is the same as x > c*c. */
4369 (if (ncmp != ERROR_MARK)
4370 (if (ncmp == GE_EXPR)
4371 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4372 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4373 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4374 (if (REAL_VALUE_ISINF (c2))
4376 /* sqrt(x) < y is always true, when y is a very large
4377 value and we don't care about NaNs or Infinities. */
4378 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4379 { constant_boolean_node (true, type); })
4380 /* sqrt(x) < y is x != +Inf when y is very large and we
4381 don't care about NaNs. */
4382 (if (! HONOR_NANS (@0))
4383 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4384 /* sqrt(x) < y is x >= 0 when y is very large and we
4385 don't care about Infinities. */
4386 (if (! HONOR_INFINITIES (@0))
4387 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4388 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4391 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4392 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4393 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4394 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4395 (if (ncmp == LT_EXPR)
4396 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4397 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4398 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4399 (if (ncmp != ERROR_MARK && GENERIC)
4400 (if (ncmp == LT_EXPR)
4402 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4403 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4405 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4406 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4407 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4409 (cmp (sq @0) (sq @1))
4410 (if (! HONOR_NANS (@0))
4413 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4414 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4415 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4417 (cmp (float@0 @1) (float @2))
4418 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4419 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4422 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4423 tree type1 = TREE_TYPE (@1);
4424 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4425 tree type2 = TREE_TYPE (@2);
4426 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4428 (if (fmt.can_represent_integral_type_p (type1)
4429 && fmt.can_represent_integral_type_p (type2))
4430 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4431 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4432 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4433 && type1_signed_p >= type2_signed_p)
4434 (icmp @1 (convert @2))
4435 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4436 && type1_signed_p <= type2_signed_p)
4437 (icmp (convert:type2 @1) @2)
4438 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4439 && type1_signed_p == type2_signed_p)
4440 (icmp @1 @2))))))))))
4442 /* Optimize various special cases of (FTYPE) N CMP CST. */
4443 (for cmp (lt le eq ne ge gt)
4444 icmp (le le eq ne ge ge)
4446 (cmp (float @0) REAL_CST@1)
4447 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4448 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4451 tree itype = TREE_TYPE (@0);
4452 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4453 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4454 /* Be careful to preserve any potential exceptions due to
4455 NaNs. qNaNs are ok in == or != context.
4456 TODO: relax under -fno-trapping-math or
4457 -fno-signaling-nans. */
4459 = real_isnan (cst) && (cst->signalling
4460 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4462 /* TODO: allow non-fitting itype and SNaNs when
4463 -fno-trapping-math. */
4464 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4467 signop isign = TYPE_SIGN (itype);
4468 REAL_VALUE_TYPE imin, imax;
4469 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4470 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4472 REAL_VALUE_TYPE icst;
4473 if (cmp == GT_EXPR || cmp == GE_EXPR)
4474 real_ceil (&icst, fmt, cst);
4475 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4476 real_floor (&icst, fmt, cst);
4478 real_trunc (&icst, fmt, cst);
4480 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4482 bool overflow_p = false;
4484 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4487 /* Optimize cases when CST is outside of ITYPE's range. */
4488 (if (real_compare (LT_EXPR, cst, &imin))
4489 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4491 (if (real_compare (GT_EXPR, cst, &imax))
4492 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4494 /* Remove cast if CST is an integer representable by ITYPE. */
4496 (cmp @0 { gcc_assert (!overflow_p);
4497 wide_int_to_tree (itype, icst_val); })
4499 /* When CST is fractional, optimize
4500 (FTYPE) N == CST -> 0
4501 (FTYPE) N != CST -> 1. */
4502 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4503 { constant_boolean_node (cmp == NE_EXPR, type); })
4504 /* Otherwise replace with sensible integer constant. */
4507 gcc_checking_assert (!overflow_p);
4509 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4511 /* Fold A /[ex] B CMP C to A CMP B * C. */
4514 (cmp (exact_div @0 @1) INTEGER_CST@2)
4515 (if (!integer_zerop (@1))
4516 (if (wi::to_wide (@2) == 0)
4518 (if (TREE_CODE (@1) == INTEGER_CST)
4521 wi::overflow_type ovf;
4522 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4523 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4526 { constant_boolean_node (cmp == NE_EXPR, type); }
4527 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4528 (for cmp (lt le gt ge)
4530 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4531 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4534 wi::overflow_type ovf;
4535 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4536 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4539 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4540 TYPE_SIGN (TREE_TYPE (@2)))
4541 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4542 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4544 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4546 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4547 For large C (more than min/B+2^size), this is also true, with the
4548 multiplication computed modulo 2^size.
4549 For intermediate C, this just tests the sign of A. */
4550 (for cmp (lt le gt ge)
4553 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4554 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4555 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4556 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4559 tree utype = TREE_TYPE (@2);
4560 wide_int denom = wi::to_wide (@1);
4561 wide_int right = wi::to_wide (@2);
4562 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4563 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4564 bool small = wi::leu_p (right, smax);
4565 bool large = wi::geu_p (right, smin);
4567 (if (small || large)
4568 (cmp (convert:utype @0) (mult @2 (convert @1)))
4569 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4571 /* Unordered tests if either argument is a NaN. */
4573 (bit_ior (unordered @0 @0) (unordered @1 @1))
4574 (if (types_match (@0, @1))
4577 (bit_and (ordered @0 @0) (ordered @1 @1))
4578 (if (types_match (@0, @1))
4581 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4584 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4587 /* Simple range test simplifications. */
4588 /* A < B || A >= B -> true. */
4589 (for test1 (lt le le le ne ge)
4590 test2 (ge gt ge ne eq ne)
4592 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4593 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4594 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4595 { constant_boolean_node (true, type); })))
4596 /* A < B && A >= B -> false. */
4597 (for test1 (lt lt lt le ne eq)
4598 test2 (ge gt eq gt eq gt)
4600 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4601 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4602 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4603 { constant_boolean_node (false, type); })))
4605 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4606 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4608 Note that comparisons
4609 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4610 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4611 will be canonicalized to above so there's no need to
4618 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4619 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4622 tree ty = TREE_TYPE (@0);
4623 unsigned prec = TYPE_PRECISION (ty);
4624 wide_int mask = wi::to_wide (@2, prec);
4625 wide_int rhs = wi::to_wide (@3, prec);
4626 signop sgn = TYPE_SIGN (ty);
4628 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4629 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4630 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4631 { build_zero_cst (ty); }))))))
4633 /* -A CMP -B -> B CMP A. */
4634 (for cmp (tcc_comparison)
4635 scmp (swapped_tcc_comparison)
4637 (cmp (negate @0) (negate @1))
4638 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4639 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4640 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4643 (cmp (negate @0) CONSTANT_CLASS_P@1)
4644 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4645 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4646 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4647 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4648 (if (tem && !TREE_OVERFLOW (tem))
4649 (scmp @0 { tem; }))))))
4651 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4654 (op (abs @0) zerop@1)
4657 /* From fold_sign_changed_comparison and fold_widened_comparison.
4658 FIXME: the lack of symmetry is disturbing. */
4659 (for cmp (simple_comparison)
4661 (cmp (convert@0 @00) (convert?@1 @10))
4662 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4663 /* Disable this optimization if we're casting a function pointer
4664 type on targets that require function pointer canonicalization. */
4665 && !(targetm.have_canonicalize_funcptr_for_compare ()
4666 && ((POINTER_TYPE_P (TREE_TYPE (@00))
4667 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4668 || (POINTER_TYPE_P (TREE_TYPE (@10))
4669 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4671 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4672 && (TREE_CODE (@10) == INTEGER_CST
4674 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4677 && !POINTER_TYPE_P (TREE_TYPE (@00)))
4678 /* ??? The special-casing of INTEGER_CST conversion was in the original
4679 code and here to avoid a spurious overflow flag on the resulting
4680 constant which fold_convert produces. */
4681 (if (TREE_CODE (@1) == INTEGER_CST)
4682 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4683 TREE_OVERFLOW (@1)); })
4684 (cmp @00 (convert @1)))
4686 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4687 /* If possible, express the comparison in the shorter mode. */
4688 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4689 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4690 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4691 && TYPE_UNSIGNED (TREE_TYPE (@00))))
4692 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4693 || ((TYPE_PRECISION (TREE_TYPE (@00))
4694 >= TYPE_PRECISION (TREE_TYPE (@10)))
4695 && (TYPE_UNSIGNED (TREE_TYPE (@00))
4696 == TYPE_UNSIGNED (TREE_TYPE (@10))))
4697 || (TREE_CODE (@10) == INTEGER_CST
4698 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4699 && int_fits_type_p (@10, TREE_TYPE (@00)))))
4700 (cmp @00 (convert @10))
4701 (if (TREE_CODE (@10) == INTEGER_CST
4702 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4703 && !int_fits_type_p (@10, TREE_TYPE (@00)))
4706 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4707 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4708 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4709 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4711 (if (above || below)
4712 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4713 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4714 (if (cmp == LT_EXPR || cmp == LE_EXPR)
4715 { constant_boolean_node (above ? true : false, type); }
4716 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4717 { constant_boolean_node (above ? false : true, type); }))))))))))))
4721 /* SSA names are canonicalized to 2nd place. */
4722 (cmp addr@0 SSA_NAME@1)
4724 { poly_int64 off; tree base; }
4725 /* A local variable can never be pointed to by
4726 the default SSA name of an incoming parameter. */
4727 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4728 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4729 && (base = get_base_address (TREE_OPERAND (@0, 0)))
4730 && TREE_CODE (base) == VAR_DECL
4731 && auto_var_in_fn_p (base, current_function_decl))
4732 (if (cmp == NE_EXPR)
4733 { constant_boolean_node (true, type); }
4734 { constant_boolean_node (false, type); })
4735 /* If the address is based on @1 decide using the offset. */
4736 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4737 && TREE_CODE (base) == MEM_REF
4738 && TREE_OPERAND (base, 0) == @1)
4739 (with { off += mem_ref_offset (base).force_shwi (); }
4740 (if (known_ne (off, 0))
4741 { constant_boolean_node (cmp == NE_EXPR, type); }
4742 (if (known_eq (off, 0))
4743 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4745 /* Equality compare simplifications from fold_binary */
4748 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4749 Similarly for NE_EXPR. */
4751 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4752 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4753 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4754 { constant_boolean_node (cmp == NE_EXPR, type); }))
4756 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
4758 (cmp (bit_xor @0 @1) integer_zerop)
4761 /* (X ^ Y) == Y becomes X == 0.
4762 Likewise (X ^ Y) == X becomes Y == 0. */
4764 (cmp:c (bit_xor:c @0 @1) @0)
4765 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4767 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
4769 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4770 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4771 (cmp @0 (bit_xor @1 (convert @2)))))
4774 (cmp (convert? addr@0) integer_zerop)
4775 (if (tree_single_nonzero_warnv_p (@0, NULL))
4776 { constant_boolean_node (cmp == NE_EXPR, type); }))
4778 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
4780 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
4781 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
4783 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
4784 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
4785 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
4786 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
4791 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
4792 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4793 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4794 && types_match (@0, @1))
4795 (ncmp (bit_xor @0 @1) @2)))))
4796 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
4797 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
4801 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
4802 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4803 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4804 && types_match (@0, @1))
4805 (ncmp (bit_xor @0 @1) @2))))
4807 /* If we have (A & C) == C where C is a power of 2, convert this into
4808 (A & C) != 0. Similarly for NE_EXPR. */
4812 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4813 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4815 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4816 convert this into a shift followed by ANDing with D. */
4819 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4820 INTEGER_CST@2 integer_zerop)
4821 (if (integer_pow2p (@2))
4823 int shift = (wi::exact_log2 (wi::to_wide (@2))
4824 - wi::exact_log2 (wi::to_wide (@1)));
4828 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4830 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4833 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4834 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
4838 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4839 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4840 && type_has_mode_precision_p (TREE_TYPE (@0))
4841 && element_precision (@2) >= element_precision (@0)
4842 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4843 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4844 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4846 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4847 this into a right shift or sign extension followed by ANDing with C. */
4850 (lt @0 integer_zerop)
4851 INTEGER_CST@1 integer_zerop)
4852 (if (integer_pow2p (@1)
4853 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4855 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4859 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4861 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4862 sign extension followed by AND with C will achieve the effect. */
4863 (bit_and (convert @0) @1)))))
4865 /* When the addresses are not directly of decls compare base and offset.
4866 This implements some remaining parts of fold_comparison address
4867 comparisons but still no complete part of it. Still it is good
4868 enough to make fold_stmt not regress when not dispatching to fold_binary. */
4869 (for cmp (simple_comparison)
4871 (cmp (convert1?@2 addr@0) (convert2? addr@1))
4874 poly_int64 off0, off1;
4875 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4876 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4877 if (base0 && TREE_CODE (base0) == MEM_REF)
4879 off0 += mem_ref_offset (base0).force_shwi ();
4880 base0 = TREE_OPERAND (base0, 0);
4882 if (base1 && TREE_CODE (base1) == MEM_REF)
4884 off1 += mem_ref_offset (base1).force_shwi ();
4885 base1 = TREE_OPERAND (base1, 0);
4888 (if (base0 && base1)
4892 /* Punt in GENERIC on variables with value expressions;
4893 the value expressions might point to fields/elements
4894 of other vars etc. */
4896 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4897 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4899 else if (decl_in_symtab_p (base0)
4900 && decl_in_symtab_p (base1))
4901 equal = symtab_node::get_create (base0)
4902 ->equal_address_to (symtab_node::get_create (base1));
4903 else if ((DECL_P (base0)
4904 || TREE_CODE (base0) == SSA_NAME
4905 || TREE_CODE (base0) == STRING_CST)
4907 || TREE_CODE (base1) == SSA_NAME
4908 || TREE_CODE (base1) == STRING_CST))
4909 equal = (base0 == base1);
4912 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4913 off0.is_constant (&ioff0);
4914 off1.is_constant (&ioff1);
4915 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4916 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4917 || (TREE_CODE (base0) == STRING_CST
4918 && TREE_CODE (base1) == STRING_CST
4919 && ioff0 >= 0 && ioff1 >= 0
4920 && ioff0 < TREE_STRING_LENGTH (base0)
4921 && ioff1 < TREE_STRING_LENGTH (base1)
4922 /* This is a too conservative test that the STRING_CSTs
4923 will not end up being string-merged. */
4924 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4925 TREE_STRING_POINTER (base1) + ioff1,
4926 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4927 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4929 else if (!DECL_P (base0) || !DECL_P (base1))
4931 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4933 /* If this is a pointer comparison, ignore for now even
4934 valid equalities where one pointer is the offset zero
4935 of one object and the other to one past end of another one. */
4936 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4938 /* Assume that automatic variables can't be adjacent to global
4940 else if (is_global_var (base0) != is_global_var (base1))
4944 tree sz0 = DECL_SIZE_UNIT (base0);
4945 tree sz1 = DECL_SIZE_UNIT (base1);
4946 /* If sizes are unknown, e.g. VLA or not representable,
4948 if (!tree_fits_poly_int64_p (sz0)
4949 || !tree_fits_poly_int64_p (sz1))
4953 poly_int64 size0 = tree_to_poly_int64 (sz0);
4954 poly_int64 size1 = tree_to_poly_int64 (sz1);
4955 /* If one offset is pointing (or could be) to the beginning
4956 of one object and the other is pointing to one past the
4957 last byte of the other object, punt. */
4958 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4960 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4962 /* If both offsets are the same, there are some cases
4963 we know that are ok. Either if we know they aren't
4964 zero, or if we know both sizes are no zero. */
4966 && known_eq (off0, off1)
4967 && (known_ne (off0, 0)
4968 || (known_ne (size0, 0) && known_ne (size1, 0))))
4975 && (cmp == EQ_EXPR || cmp == NE_EXPR
4976 /* If the offsets are equal we can ignore overflow. */
4977 || known_eq (off0, off1)
4978 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4979 /* Or if we compare using pointers to decls or strings. */
4980 || (POINTER_TYPE_P (TREE_TYPE (@2))
4981 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4983 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4984 { constant_boolean_node (known_eq (off0, off1), type); })
4985 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4986 { constant_boolean_node (known_ne (off0, off1), type); })
4987 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4988 { constant_boolean_node (known_lt (off0, off1), type); })
4989 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4990 { constant_boolean_node (known_le (off0, off1), type); })
4991 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4992 { constant_boolean_node (known_ge (off0, off1), type); })
4993 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4994 { constant_boolean_node (known_gt (off0, off1), type); }))
4997 (if (cmp == EQ_EXPR)
4998 { constant_boolean_node (false, type); })
4999 (if (cmp == NE_EXPR)
5000 { constant_boolean_node (true, type); })))))))))
5002 /* Simplify pointer equality compares using PTA. */
5006 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5007 && ptrs_compare_unequal (@0, @1))
5008 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5010 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5011 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5012 Disable the transform if either operand is pointer to function.
5013 This broke pr22051-2.c for arm where function pointer
5014 canonicalizaion is not wanted. */
5018 (cmp (convert @0) INTEGER_CST@1)
5019 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5020 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5021 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5022 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5023 && POINTER_TYPE_P (TREE_TYPE (@1))
5024 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5025 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5026 (cmp @0 (convert @1)))))
5028 /* Non-equality compare simplifications from fold_binary */
5029 (for cmp (lt gt le ge)
5030 /* Comparisons with the highest or lowest possible integer of
5031 the specified precision will have known values. */
5033 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5034 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5035 || POINTER_TYPE_P (TREE_TYPE (@1))
5036 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5037 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5040 tree cst = uniform_integer_cst_p (@1);
5041 tree arg1_type = TREE_TYPE (cst);
5042 unsigned int prec = TYPE_PRECISION (arg1_type);
5043 wide_int max = wi::max_value (arg1_type);
5044 wide_int signed_max = wi::max_value (prec, SIGNED);
5045 wide_int min = wi::min_value (arg1_type);
5048 (if (wi::to_wide (cst) == max)
5050 (if (cmp == GT_EXPR)
5051 { constant_boolean_node (false, type); })
5052 (if (cmp == GE_EXPR)
5054 (if (cmp == LE_EXPR)
5055 { constant_boolean_node (true, type); })
5056 (if (cmp == LT_EXPR)
5058 (if (wi::to_wide (cst) == min)
5060 (if (cmp == LT_EXPR)
5061 { constant_boolean_node (false, type); })
5062 (if (cmp == LE_EXPR)
5064 (if (cmp == GE_EXPR)
5065 { constant_boolean_node (true, type); })
5066 (if (cmp == GT_EXPR)
5068 (if (wi::to_wide (cst) == max - 1)
5070 (if (cmp == GT_EXPR)
5071 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5072 wide_int_to_tree (TREE_TYPE (cst),
5075 (if (cmp == LE_EXPR)
5076 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5077 wide_int_to_tree (TREE_TYPE (cst),
5080 (if (wi::to_wide (cst) == min + 1)
5082 (if (cmp == GE_EXPR)
5083 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5084 wide_int_to_tree (TREE_TYPE (cst),
5087 (if (cmp == LT_EXPR)
5088 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5089 wide_int_to_tree (TREE_TYPE (cst),
5092 (if (wi::to_wide (cst) == signed_max
5093 && TYPE_UNSIGNED (arg1_type)
5094 /* We will flip the signedness of the comparison operator
5095 associated with the mode of @1, so the sign bit is
5096 specified by this mode. Check that @1 is the signed
5097 max associated with this sign bit. */
5098 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5099 /* signed_type does not work on pointer types. */
5100 && INTEGRAL_TYPE_P (arg1_type))
5101 /* The following case also applies to X < signed_max+1
5102 and X >= signed_max+1 because previous transformations. */
5103 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5104 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5106 (if (cst == @1 && cmp == LE_EXPR)
5107 (ge (convert:st @0) { build_zero_cst (st); }))
5108 (if (cst == @1 && cmp == GT_EXPR)
5109 (lt (convert:st @0) { build_zero_cst (st); }))
5110 (if (cmp == LE_EXPR)
5111 (ge (view_convert:st @0) { build_zero_cst (st); }))
5112 (if (cmp == GT_EXPR)
5113 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5115 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5116 /* If the second operand is NaN, the result is constant. */
5119 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5120 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5121 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5122 ? false : true, type); })))
5124 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5128 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5129 { constant_boolean_node (true, type); })
5130 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5131 { constant_boolean_node (false, type); })))
5133 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5137 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5138 { constant_boolean_node (false, type); })
5139 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5140 { constant_boolean_node (true, type); })))
5142 /* bool_var != 0 becomes bool_var. */
5144 (ne @0 integer_zerop)
5145 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5146 && types_match (type, TREE_TYPE (@0)))
5148 /* bool_var == 1 becomes bool_var. */
5150 (eq @0 integer_onep)
5151 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5152 && types_match (type, TREE_TYPE (@0)))
5155 bool_var == 0 becomes !bool_var or
5156 bool_var != 1 becomes !bool_var
5157 here because that only is good in assignment context as long
5158 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5159 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5160 clearly less optimal and which we'll transform again in forwprop. */
5162 /* When one argument is a constant, overflow detection can be simplified.
5163 Currently restricted to single use so as not to interfere too much with
5164 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5165 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5166 (for cmp (lt le ge gt)
5169 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5170 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5171 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5172 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5173 && wi::to_wide (@1) != 0
5176 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5177 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5179 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5180 wi::max_value (prec, sign)
5181 - wi::to_wide (@1)); })))))
5183 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5184 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5185 expects the long form, so we restrict the transformation for now. */
5188 (cmp:c (minus@2 @0 @1) @0)
5189 (if (single_use (@2)
5190 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5191 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5194 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5197 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5198 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5199 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5202 /* Testing for overflow is unnecessary if we already know the result. */
5207 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5208 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5209 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5210 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5215 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5216 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5217 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5218 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5220 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5221 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5225 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5226 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5227 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5228 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5230 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5231 is at least twice as wide as type of A and B, simplify to
5232 __builtin_mul_overflow (A, B, <unused>). */
5235 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5237 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5238 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5239 && TYPE_UNSIGNED (TREE_TYPE (@0))
5240 && (TYPE_PRECISION (TREE_TYPE (@3))
5241 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5242 && tree_fits_uhwi_p (@2)
5243 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5244 && types_match (@0, @1)
5245 && type_has_mode_precision_p (TREE_TYPE (@0))
5246 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5247 != CODE_FOR_nothing))
5248 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5249 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5251 /* Simplification of math builtins. These rules must all be optimizations
5252 as well as IL simplifications. If there is a possibility that the new
5253 form could be a pessimization, the rule should go in the canonicalization
5254 section that follows this one.
5256 Rules can generally go in this section if they satisfy one of
5259 - the rule describes an identity
5261 - the rule replaces calls with something as simple as addition or
5264 - the rule contains unary calls only and simplifies the surrounding
5265 arithmetic. (The idea here is to exclude non-unary calls in which
5266 one operand is constant and in which the call is known to be cheap
5267 when the operand has that value.) */
5269 (if (flag_unsafe_math_optimizations)
5270 /* Simplify sqrt(x) * sqrt(x) -> x. */
5272 (mult (SQRT_ALL@1 @0) @1)
5273 (if (!tree_expr_maybe_signaling_nan_p (@0))
5276 (for op (plus minus)
5277 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5281 (rdiv (op @0 @2) @1)))
5283 (for cmp (lt le gt ge)
5284 neg_cmp (gt ge lt le)
5285 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5287 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5289 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5291 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5292 || (real_zerop (tem) && !real_zerop (@1))))
5294 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5296 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5297 (neg_cmp @0 { tem; })))))))
5299 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5300 (for root (SQRT CBRT)
5302 (mult (root:s @0) (root:s @1))
5303 (root (mult @0 @1))))
5305 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5306 (for exps (EXP EXP2 EXP10 POW10)
5308 (mult (exps:s @0) (exps:s @1))
5309 (exps (plus @0 @1))))
5311 /* Simplify a/root(b/c) into a*root(c/b). */
5312 (for root (SQRT CBRT)
5314 (rdiv @0 (root:s (rdiv:s @1 @2)))
5315 (mult @0 (root (rdiv @2 @1)))))
5317 /* Simplify x/expN(y) into x*expN(-y). */
5318 (for exps (EXP EXP2 EXP10 POW10)
5320 (rdiv @0 (exps:s @1))
5321 (mult @0 (exps (negate @1)))))
5323 (for logs (LOG LOG2 LOG10 LOG10)
5324 exps (EXP EXP2 EXP10 POW10)
5325 /* logN(expN(x)) -> x. */
5329 /* expN(logN(x)) -> x. */
5334 /* Optimize logN(func()) for various exponential functions. We
5335 want to determine the value "x" and the power "exponent" in
5336 order to transform logN(x**exponent) into exponent*logN(x). */
5337 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5338 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5341 (if (SCALAR_FLOAT_TYPE_P (type))
5347 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5348 x = build_real_truncate (type, dconst_e ());
5351 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5352 x = build_real (type, dconst2);
5356 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5358 REAL_VALUE_TYPE dconst10;
5359 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5360 x = build_real (type, dconst10);
5367 (mult (logs { x; }) @0)))))
5375 (if (SCALAR_FLOAT_TYPE_P (type))
5381 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5382 x = build_real (type, dconsthalf);
5385 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5386 x = build_real_truncate (type, dconst_third ());
5392 (mult { x; } (logs @0))))))
5394 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5395 (for logs (LOG LOG2 LOG10)
5399 (mult @1 (logs @0))))
5401 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5402 or if C is a positive power of 2,
5403 pow(C,x) -> exp2(log2(C)*x). */
5411 (pows REAL_CST@0 @1)
5412 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5413 && real_isfinite (TREE_REAL_CST_PTR (@0))
5414 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5415 the use_exp2 case until after vectorization. It seems actually
5416 beneficial for all constants to postpone this until later,
5417 because exp(log(C)*x), while faster, will have worse precision
5418 and if x folds into a constant too, that is unnecessary
5420 && canonicalize_math_after_vectorization_p ())
5422 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5423 bool use_exp2 = false;
5424 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5425 && value->cl == rvc_normal)
5427 REAL_VALUE_TYPE frac_rvt = *value;
5428 SET_REAL_EXP (&frac_rvt, 1);
5429 if (real_equal (&frac_rvt, &dconst1))
5434 (if (optimize_pow_to_exp (@0, @1))
5435 (exps (mult (logs @0) @1)))
5436 (exp2s (mult (log2s @0) @1)))))))
5439 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5441 exps (EXP EXP2 EXP10 POW10)
5442 logs (LOG LOG2 LOG10 LOG10)
5444 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5445 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5446 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5447 (exps (plus (mult (logs @0) @1) @2)))))
5452 exps (EXP EXP2 EXP10 POW10)
5453 /* sqrt(expN(x)) -> expN(x*0.5). */
5456 (exps (mult @0 { build_real (type, dconsthalf); })))
5457 /* cbrt(expN(x)) -> expN(x/3). */
5460 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5461 /* pow(expN(x), y) -> expN(x*y). */
5464 (exps (mult @0 @1))))
5466 /* tan(atan(x)) -> x. */
5473 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5477 copysigns (COPYSIGN)
5482 REAL_VALUE_TYPE r_cst;
5483 build_sinatan_real (&r_cst, type);
5484 tree t_cst = build_real (type, r_cst);
5485 tree t_one = build_one_cst (type);
5487 (if (SCALAR_FLOAT_TYPE_P (type))
5488 (cond (lt (abs @0) { t_cst; })
5489 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5490 (copysigns { t_one; } @0))))))
5492 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5496 copysigns (COPYSIGN)
5501 REAL_VALUE_TYPE r_cst;
5502 build_sinatan_real (&r_cst, type);
5503 tree t_cst = build_real (type, r_cst);
5504 tree t_one = build_one_cst (type);
5505 tree t_zero = build_zero_cst (type);
5507 (if (SCALAR_FLOAT_TYPE_P (type))
5508 (cond (lt (abs @0) { t_cst; })
5509 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5510 (copysigns { t_zero; } @0))))))
5512 (if (!flag_errno_math)
5513 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5518 (sinhs (atanhs:s @0))
5519 (with { tree t_one = build_one_cst (type); }
5520 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5522 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5527 (coshs (atanhs:s @0))
5528 (with { tree t_one = build_one_cst (type); }
5529 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5531 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5533 (CABS (complex:C @0 real_zerop@1))
5536 /* trunc(trunc(x)) -> trunc(x), etc. */
5537 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5541 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5542 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5544 (fns integer_valued_real_p@0)
5547 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5549 (HYPOT:c @0 real_zerop@1)
5552 /* pow(1,x) -> 1. */
5554 (POW real_onep@0 @1)
5558 /* copysign(x,x) -> x. */
5559 (COPYSIGN_ALL @0 @0)
5563 /* copysign(x,-x) -> -x. */
5564 (COPYSIGN_ALL @0 (negate@1 @0))
5568 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5569 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5572 (for scale (LDEXP SCALBN SCALBLN)
5573 /* ldexp(0, x) -> 0. */
5575 (scale real_zerop@0 @1)
5577 /* ldexp(x, 0) -> x. */
5579 (scale @0 integer_zerop@1)
5581 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5583 (scale REAL_CST@0 @1)
5584 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5587 /* Canonicalization of sequences of math builtins. These rules represent
5588 IL simplifications but are not necessarily optimizations.
5590 The sincos pass is responsible for picking "optimal" implementations
5591 of math builtins, which may be more complicated and can sometimes go
5592 the other way, e.g. converting pow into a sequence of sqrts.
5593 We only want to do these canonicalizations before the pass has run. */
5595 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5596 /* Simplify tan(x) * cos(x) -> sin(x). */
5598 (mult:c (TAN:s @0) (COS:s @0))
5601 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5603 (mult:c @0 (POW:s @0 REAL_CST@1))
5604 (if (!TREE_OVERFLOW (@1))
5605 (POW @0 (plus @1 { build_one_cst (type); }))))
5607 /* Simplify sin(x) / cos(x) -> tan(x). */
5609 (rdiv (SIN:s @0) (COS:s @0))
5612 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5614 (rdiv (SINH:s @0) (COSH:s @0))
5617 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5619 (rdiv (TANH:s @0) (SINH:s @0))
5620 (rdiv {build_one_cst (type);} (COSH @0)))
5622 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5624 (rdiv (COS:s @0) (SIN:s @0))
5625 (rdiv { build_one_cst (type); } (TAN @0)))
5627 /* Simplify sin(x) / tan(x) -> cos(x). */
5629 (rdiv (SIN:s @0) (TAN:s @0))
5630 (if (! HONOR_NANS (@0)
5631 && ! HONOR_INFINITIES (@0))
5634 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5636 (rdiv (TAN:s @0) (SIN:s @0))
5637 (if (! HONOR_NANS (@0)
5638 && ! HONOR_INFINITIES (@0))
5639 (rdiv { build_one_cst (type); } (COS @0))))
5641 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5643 (mult (POW:s @0 @1) (POW:s @0 @2))
5644 (POW @0 (plus @1 @2)))
5646 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5648 (mult (POW:s @0 @1) (POW:s @2 @1))
5649 (POW (mult @0 @2) @1))
5651 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5653 (mult (POWI:s @0 @1) (POWI:s @2 @1))
5654 (POWI (mult @0 @2) @1))
5656 /* Simplify pow(x,c) / x -> pow(x,c-1). */
5658 (rdiv (POW:s @0 REAL_CST@1) @0)
5659 (if (!TREE_OVERFLOW (@1))
5660 (POW @0 (minus @1 { build_one_cst (type); }))))
5662 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5664 (rdiv @0 (POW:s @1 @2))
5665 (mult @0 (POW @1 (negate @2))))
5670 /* sqrt(sqrt(x)) -> pow(x,1/4). */
5673 (pows @0 { build_real (type, dconst_quarter ()); }))
5674 /* sqrt(cbrt(x)) -> pow(x,1/6). */
5677 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5678 /* cbrt(sqrt(x)) -> pow(x,1/6). */
5681 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5682 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
5684 (cbrts (cbrts tree_expr_nonnegative_p@0))
5685 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5686 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
5688 (sqrts (pows @0 @1))
5689 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5690 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
5692 (cbrts (pows tree_expr_nonnegative_p@0 @1))
5693 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5694 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
5696 (pows (sqrts @0) @1)
5697 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5698 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
5700 (pows (cbrts tree_expr_nonnegative_p@0) @1)
5701 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5702 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
5704 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5705 (pows @0 (mult @1 @2))))
5707 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
5709 (CABS (complex @0 @0))
5710 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5712 /* hypot(x,x) -> fabs(x)*sqrt(2). */
5715 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5717 /* cexp(x+yi) -> exp(x)*cexpi(y). */
5722 (cexps compositional_complex@0)
5723 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
5725 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5726 (mult @1 (imagpart @2)))))))
5728 (if (canonicalize_math_p ())
5729 /* floor(x) -> trunc(x) if x is nonnegative. */
5730 (for floors (FLOOR_ALL)
5733 (floors tree_expr_nonnegative_p@0)
5736 (match double_value_p
5738 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5739 (for froms (BUILT_IN_TRUNCL
5751 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
5752 (if (optimize && canonicalize_math_p ())
5754 (froms (convert double_value_p@0))
5755 (convert (tos @0)))))
5757 (match float_value_p
5759 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5760 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5761 BUILT_IN_FLOORL BUILT_IN_FLOOR
5762 BUILT_IN_CEILL BUILT_IN_CEIL
5763 BUILT_IN_ROUNDL BUILT_IN_ROUND
5764 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5765 BUILT_IN_RINTL BUILT_IN_RINT)
5766 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5767 BUILT_IN_FLOORF BUILT_IN_FLOORF
5768 BUILT_IN_CEILF BUILT_IN_CEILF
5769 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5770 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5771 BUILT_IN_RINTF BUILT_IN_RINTF)
5772 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5774 (if (optimize && canonicalize_math_p ()
5775 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
5777 (froms (convert float_value_p@0))
5778 (convert (tos @0)))))
5780 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5781 tos (XFLOOR XCEIL XROUND XRINT)
5782 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
5783 (if (optimize && canonicalize_math_p ())
5785 (froms (convert double_value_p@0))
5788 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5789 XFLOOR XCEIL XROUND XRINT)
5790 tos (XFLOORF XCEILF XROUNDF XRINTF)
5791 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5793 (if (optimize && canonicalize_math_p ())
5795 (froms (convert float_value_p@0))
5798 (if (canonicalize_math_p ())
5799 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
5800 (for floors (IFLOOR LFLOOR LLFLOOR)
5802 (floors tree_expr_nonnegative_p@0)
5805 (if (canonicalize_math_p ())
5806 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
5807 (for fns (IFLOOR LFLOOR LLFLOOR
5809 IROUND LROUND LLROUND)
5811 (fns integer_valued_real_p@0)
5813 (if (!flag_errno_math)
5814 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
5815 (for rints (IRINT LRINT LLRINT)
5817 (rints integer_valued_real_p@0)
5820 (if (canonicalize_math_p ())
5821 (for ifn (IFLOOR ICEIL IROUND IRINT)
5822 lfn (LFLOOR LCEIL LROUND LRINT)
5823 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5824 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5825 sizeof (int) == sizeof (long). */
5826 (if (TYPE_PRECISION (integer_type_node)
5827 == TYPE_PRECISION (long_integer_type_node))
5830 (lfn:long_integer_type_node @0)))
5831 /* Canonicalize llround (x) to lround (x) on LP64 targets where
5832 sizeof (long long) == sizeof (long). */
5833 (if (TYPE_PRECISION (long_long_integer_type_node)
5834 == TYPE_PRECISION (long_integer_type_node))
5837 (lfn:long_integer_type_node @0)))))
5839 /* cproj(x) -> x if we're ignoring infinities. */
5842 (if (!HONOR_INFINITIES (type))
5845 /* If the real part is inf and the imag part is known to be
5846 nonnegative, return (inf + 0i). */
5848 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5849 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5850 { build_complex_inf (type, false); }))
5852 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
5854 (CPROJ (complex @0 REAL_CST@1))
5855 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5856 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5862 (pows @0 REAL_CST@1)
5864 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5865 REAL_VALUE_TYPE tmp;
5868 /* pow(x,0) -> 1. */
5869 (if (real_equal (value, &dconst0))
5870 { build_real (type, dconst1); })
5871 /* pow(x,1) -> x. */
5872 (if (real_equal (value, &dconst1))
5874 /* pow(x,-1) -> 1/x. */
5875 (if (real_equal (value, &dconstm1))
5876 (rdiv { build_real (type, dconst1); } @0))
5877 /* pow(x,0.5) -> sqrt(x). */
5878 (if (flag_unsafe_math_optimizations
5879 && canonicalize_math_p ()
5880 && real_equal (value, &dconsthalf))
5882 /* pow(x,1/3) -> cbrt(x). */
5883 (if (flag_unsafe_math_optimizations
5884 && canonicalize_math_p ()
5885 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5886 real_equal (value, &tmp)))
5889 /* powi(1,x) -> 1. */
5891 (POWI real_onep@0 @1)
5895 (POWI @0 INTEGER_CST@1)
5897 /* powi(x,0) -> 1. */
5898 (if (wi::to_wide (@1) == 0)
5899 { build_real (type, dconst1); })
5900 /* powi(x,1) -> x. */
5901 (if (wi::to_wide (@1) == 1)
5903 /* powi(x,-1) -> 1/x. */
5904 (if (wi::to_wide (@1) == -1)
5905 (rdiv { build_real (type, dconst1); } @0))))
5907 /* Narrowing of arithmetic and logical operations.
5909 These are conceptually similar to the transformations performed for
5910 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
5911 term we want to move all that code out of the front-ends into here. */
5913 /* Convert (outertype)((innertype0)a+(innertype1)b)
5914 into ((newtype)a+(newtype)b) where newtype
5915 is the widest mode from all of these. */
5916 (for op (plus minus mult rdiv)
5918 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5919 /* If we have a narrowing conversion of an arithmetic operation where
5920 both operands are widening conversions from the same type as the outer
5921 narrowing conversion. Then convert the innermost operands to a
5922 suitable unsigned type (to avoid introducing undefined behavior),
5923 perform the operation and convert the result to the desired type. */
5924 (if (INTEGRAL_TYPE_P (type)
5927 /* We check for type compatibility between @0 and @1 below,
5928 so there's no need to check that @2/@4 are integral types. */
5929 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5930 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5931 /* The precision of the type of each operand must match the
5932 precision of the mode of each operand, similarly for the
5934 && type_has_mode_precision_p (TREE_TYPE (@1))
5935 && type_has_mode_precision_p (TREE_TYPE (@2))
5936 && type_has_mode_precision_p (type)
5937 /* The inner conversion must be a widening conversion. */
5938 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5939 && types_match (@1, type)
5940 && (types_match (@1, @2)
5941 /* Or the second operand is const integer or converted const
5942 integer from valueize. */
5943 || TREE_CODE (@2) == INTEGER_CST))
5944 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5945 (op @1 (convert @2))
5946 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5947 (convert (op (convert:utype @1)
5948 (convert:utype @2)))))
5949 (if (FLOAT_TYPE_P (type)
5950 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5951 == DECIMAL_FLOAT_TYPE_P (type))
5952 (with { tree arg0 = strip_float_extensions (@1);
5953 tree arg1 = strip_float_extensions (@2);
5954 tree itype = TREE_TYPE (@0);
5955 tree ty1 = TREE_TYPE (arg0);
5956 tree ty2 = TREE_TYPE (arg1);
5957 enum tree_code code = TREE_CODE (itype); }
5958 (if (FLOAT_TYPE_P (ty1)
5959 && FLOAT_TYPE_P (ty2))
5960 (with { tree newtype = type;
5961 if (TYPE_MODE (ty1) == SDmode
5962 || TYPE_MODE (ty2) == SDmode
5963 || TYPE_MODE (type) == SDmode)
5964 newtype = dfloat32_type_node;
5965 if (TYPE_MODE (ty1) == DDmode
5966 || TYPE_MODE (ty2) == DDmode
5967 || TYPE_MODE (type) == DDmode)
5968 newtype = dfloat64_type_node;
5969 if (TYPE_MODE (ty1) == TDmode
5970 || TYPE_MODE (ty2) == TDmode
5971 || TYPE_MODE (type) == TDmode)
5972 newtype = dfloat128_type_node; }
5973 (if ((newtype == dfloat32_type_node
5974 || newtype == dfloat64_type_node
5975 || newtype == dfloat128_type_node)
5977 && types_match (newtype, type))
5978 (op (convert:newtype @1) (convert:newtype @2))
5979 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5981 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5983 /* Sometimes this transformation is safe (cannot
5984 change results through affecting double rounding
5985 cases) and sometimes it is not. If NEWTYPE is
5986 wider than TYPE, e.g. (float)((long double)double
5987 + (long double)double) converted to
5988 (float)(double + double), the transformation is
5989 unsafe regardless of the details of the types
5990 involved; double rounding can arise if the result
5991 of NEWTYPE arithmetic is a NEWTYPE value half way
5992 between two representable TYPE values but the
5993 exact value is sufficiently different (in the
5994 right direction) for this difference to be
5995 visible in ITYPE arithmetic. If NEWTYPE is the
5996 same as TYPE, however, the transformation may be
5997 safe depending on the types involved: it is safe
5998 if the ITYPE has strictly more than twice as many
5999 mantissa bits as TYPE, can represent infinities
6000 and NaNs if the TYPE can, and has sufficient
6001 exponent range for the product or ratio of two
6002 values representable in the TYPE to be within the
6003 range of normal values of ITYPE. */
6004 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6005 && (flag_unsafe_math_optimizations
6006 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6007 && real_can_shorten_arithmetic (TYPE_MODE (itype),
6009 && !excess_precision_type (newtype)))
6010 && !types_match (itype, newtype))
6011 (convert:type (op (convert:newtype @1)
6012 (convert:newtype @2)))
6017 /* This is another case of narrowing, specifically when there's an outer
6018 BIT_AND_EXPR which masks off bits outside the type of the innermost
6019 operands. Like the previous case we have to convert the operands
6020 to unsigned types to avoid introducing undefined behavior for the
6021 arithmetic operation. */
6022 (for op (minus plus)
6024 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6025 (if (INTEGRAL_TYPE_P (type)
6026 /* We check for type compatibility between @0 and @1 below,
6027 so there's no need to check that @1/@3 are integral types. */
6028 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6029 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6030 /* The precision of the type of each operand must match the
6031 precision of the mode of each operand, similarly for the
6033 && type_has_mode_precision_p (TREE_TYPE (@0))
6034 && type_has_mode_precision_p (TREE_TYPE (@1))
6035 && type_has_mode_precision_p (type)
6036 /* The inner conversion must be a widening conversion. */
6037 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6038 && types_match (@0, @1)
6039 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6040 <= TYPE_PRECISION (TREE_TYPE (@0)))
6041 && (wi::to_wide (@4)
6042 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6043 true, TYPE_PRECISION (type))) == 0)
6044 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6045 (with { tree ntype = TREE_TYPE (@0); }
6046 (convert (bit_and (op @0 @1) (convert:ntype @4))))
6047 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6048 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6049 (convert:utype @4))))))))
6051 /* Transform (@0 < @1 and @0 < @2) to use min,
6052 (@0 > @1 and @0 > @2) to use max */
6053 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6054 op (lt le gt ge lt le gt ge )
6055 ext (min min max max max max min min )
6057 (logic (op:cs @0 @1) (op:cs @0 @2))
6058 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6059 && TREE_CODE (@0) != INTEGER_CST)
6060 (op @0 (ext @1 @2)))))
6063 /* signbit(x) -> 0 if x is nonnegative. */
6064 (SIGNBIT tree_expr_nonnegative_p@0)
6065 { integer_zero_node; })
6068 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6070 (if (!HONOR_SIGNED_ZEROS (@0))
6071 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6073 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6075 (for op (plus minus)
6078 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6079 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6080 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6081 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6082 && !TYPE_SATURATING (TREE_TYPE (@0)))
6083 (with { tree res = int_const_binop (rop, @2, @1); }
6084 (if (TREE_OVERFLOW (res)
6085 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6086 { constant_boolean_node (cmp == NE_EXPR, type); }
6087 (if (single_use (@3))
6088 (cmp @0 { TREE_OVERFLOW (res)
6089 ? drop_tree_overflow (res) : res; }))))))))
6090 (for cmp (lt le gt ge)
6091 (for op (plus minus)
6094 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6095 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6096 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6097 (with { tree res = int_const_binop (rop, @2, @1); }
6098 (if (TREE_OVERFLOW (res))
6100 fold_overflow_warning (("assuming signed overflow does not occur "
6101 "when simplifying conditional to constant"),
6102 WARN_STRICT_OVERFLOW_CONDITIONAL);
6103 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6104 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6105 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6106 TYPE_SIGN (TREE_TYPE (@1)))
6107 != (op == MINUS_EXPR);
6108 constant_boolean_node (less == ovf_high, type);
6110 (if (single_use (@3))
6113 fold_overflow_warning (("assuming signed overflow does not occur "
6114 "when changing X +- C1 cmp C2 to "
6116 WARN_STRICT_OVERFLOW_COMPARISON);
6118 (cmp @0 { res; })))))))))
6120 /* Canonicalizations of BIT_FIELD_REFs. */
6123 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6124 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6127 (BIT_FIELD_REF (view_convert @0) @1 @2)
6128 (BIT_FIELD_REF @0 @1 @2))
6131 (BIT_FIELD_REF @0 @1 integer_zerop)
6132 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6136 (BIT_FIELD_REF @0 @1 @2)
6138 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6139 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6141 (if (integer_zerop (@2))
6142 (view_convert (realpart @0)))
6143 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6144 (view_convert (imagpart @0)))))
6145 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6146 && INTEGRAL_TYPE_P (type)
6147 /* On GIMPLE this should only apply to register arguments. */
6148 && (! GIMPLE || is_gimple_reg (@0))
6149 /* A bit-field-ref that referenced the full argument can be stripped. */
6150 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6151 && integer_zerop (@2))
6152 /* Low-parts can be reduced to integral conversions.
6153 ??? The following doesn't work for PDP endian. */
6154 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6155 /* But only do this after vectorization. */
6156 && canonicalize_math_after_vectorization_p ()
6157 /* Don't even think about BITS_BIG_ENDIAN. */
6158 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6159 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6160 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6161 ? (TYPE_PRECISION (TREE_TYPE (@0))
6162 - TYPE_PRECISION (type))
6166 /* Simplify vector extracts. */
6169 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6170 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6171 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
6172 || (VECTOR_TYPE_P (type)
6173 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
6176 tree ctor = (TREE_CODE (@0) == SSA_NAME
6177 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6178 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6179 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6180 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6181 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6184 && (idx % width) == 0
6186 && known_le ((idx + n) / width,
6187 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6192 /* Constructor elements can be subvectors. */
6194 if (CONSTRUCTOR_NELTS (ctor) != 0)
6196 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6197 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6198 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6200 unsigned HOST_WIDE_INT elt, count, const_k;
6203 /* We keep an exact subset of the constructor elements. */
6204 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6205 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6206 { build_constructor (type, NULL); }
6208 (if (elt < CONSTRUCTOR_NELTS (ctor))
6209 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6210 { build_zero_cst (type); })
6211 /* We don't want to emit new CTORs unless the old one goes away.
6212 ??? Eventually allow this if the CTOR ends up constant or
6214 (if (single_use (@0))
6216 vec<constructor_elt, va_gc> *vals;
6217 vec_alloc (vals, count);
6218 for (unsigned i = 0;
6219 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6220 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
6221 CONSTRUCTOR_ELT (ctor, elt + i)->value);
6222 build_constructor (type, vals);
6224 /* The bitfield references a single constructor element. */
6225 (if (k.is_constant (&const_k)
6226 && idx + n <= (idx / const_k + 1) * const_k)
6228 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6229 { build_zero_cst (type); })
6231 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6232 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6233 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6235 /* Simplify a bit extraction from a bit insertion for the cases with
6236 the inserted element fully covering the extraction or the insertion
6237 not touching the extraction. */
6239 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6242 unsigned HOST_WIDE_INT isize;
6243 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6244 isize = TYPE_PRECISION (TREE_TYPE (@1));
6246 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6249 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6250 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6251 wi::to_wide (@ipos) + isize))
6252 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6254 - wi::to_wide (@ipos)); }))
6255 (if (wi::geu_p (wi::to_wide (@ipos),
6256 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6257 || wi::geu_p (wi::to_wide (@rpos),
6258 wi::to_wide (@ipos) + isize))
6259 (BIT_FIELD_REF @0 @rsize @rpos)))))
6261 (if (canonicalize_math_after_vectorization_p ())
6264 (fmas:c (negate @0) @1 @2)
6265 (IFN_FNMA @0 @1 @2))
6267 (fmas @0 @1 (negate @2))
6270 (fmas:c (negate @0) @1 (negate @2))
6271 (IFN_FNMS @0 @1 @2))
6273 (negate (fmas@3 @0 @1 @2))
6274 (if (single_use (@3))
6275 (IFN_FNMS @0 @1 @2))))
6278 (IFN_FMS:c (negate @0) @1 @2)
6279 (IFN_FNMS @0 @1 @2))
6281 (IFN_FMS @0 @1 (negate @2))
6284 (IFN_FMS:c (negate @0) @1 (negate @2))
6285 (IFN_FNMA @0 @1 @2))
6287 (negate (IFN_FMS@3 @0 @1 @2))
6288 (if (single_use (@3))
6289 (IFN_FNMA @0 @1 @2)))
6292 (IFN_FNMA:c (negate @0) @1 @2)
6295 (IFN_FNMA @0 @1 (negate @2))
6296 (IFN_FNMS @0 @1 @2))
6298 (IFN_FNMA:c (negate @0) @1 (negate @2))
6301 (negate (IFN_FNMA@3 @0 @1 @2))
6302 (if (single_use (@3))
6303 (IFN_FMS @0 @1 @2)))
6306 (IFN_FNMS:c (negate @0) @1 @2)
6309 (IFN_FNMS @0 @1 (negate @2))
6310 (IFN_FNMA @0 @1 @2))
6312 (IFN_FNMS:c (negate @0) @1 (negate @2))
6315 (negate (IFN_FNMS@3 @0 @1 @2))
6316 (if (single_use (@3))
6317 (IFN_FMA @0 @1 @2))))
6319 /* CLZ simplifications. */
6324 (op (clz:s@2 @0) INTEGER_CST@1)
6325 (if (integer_zerop (@1) && single_use (@2))
6326 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6327 (with { tree stype = signed_type_for (TREE_TYPE (@0));
6328 HOST_WIDE_INT val = 0;
6329 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6330 /* Punt on hypothetical weird targets. */
6331 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6338 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6339 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
6340 (with { bool ok = true;
6341 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6342 /* Punt on hypothetical weird targets. */
6343 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6345 && val == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
6349 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (TREE_TYPE (@0)) - 1))
6350 (op @0 { build_one_cst (TREE_TYPE (@0)); })))))))
6352 /* POPCOUNT simplifications. */
6353 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6355 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6356 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6357 (POPCOUNT (bit_ior @0 @1))))
6359 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6360 (for popcount (POPCOUNT)
6361 (for cmp (le eq ne gt)
6364 (cmp (popcount @0) integer_zerop)
6365 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6367 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6369 (bit_and (POPCOUNT @0) integer_onep)
6372 /* PARITY simplifications. */
6373 /* parity(~X) is parity(X). */
6375 (PARITY (bit_not @0))
6378 /* parity(X)^parity(Y) is parity(X^Y). */
6380 (bit_xor (PARITY:s @0) (PARITY:s @1))
6381 (PARITY (bit_xor @0 @1)))
6383 /* Common POPCOUNT/PARITY simplifications. */
6384 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6385 (for pfun (POPCOUNT PARITY)
6388 (with { wide_int nz = tree_nonzero_bits (@0); }
6392 (if (wi::popcount (nz) == 1)
6393 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6394 (convert (rshift:utype (convert:utype @0)
6395 { build_int_cst (integer_type_node,
6396 wi::ctz (nz)); }))))))))
6399 /* 64- and 32-bits branchless implementations of popcount are detected:
6401 int popcount64c (uint64_t x)
6403 x -= (x >> 1) & 0x5555555555555555ULL;
6404 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6405 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6406 return (x * 0x0101010101010101ULL) >> 56;
6409 int popcount32c (uint32_t x)
6411 x -= (x >> 1) & 0x55555555;
6412 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6413 x = (x + (x >> 4)) & 0x0f0f0f0f;
6414 return (x * 0x01010101) >> 24;
6421 (rshift @8 INTEGER_CST@5)
6423 (bit_and @6 INTEGER_CST@7)
6427 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6433 /* Check constants and optab. */
6434 (with { unsigned prec = TYPE_PRECISION (type);
6435 int shift = (64 - prec) & 63;
6436 unsigned HOST_WIDE_INT c1
6437 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6438 unsigned HOST_WIDE_INT c2
6439 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6440 unsigned HOST_WIDE_INT c3
6441 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6442 unsigned HOST_WIDE_INT c4
6443 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6448 && TYPE_UNSIGNED (type)
6449 && integer_onep (@4)
6450 && wi::to_widest (@10) == 2
6451 && wi::to_widest (@5) == 4
6452 && wi::to_widest (@1) == prec - 8
6453 && tree_to_uhwi (@2) == c1
6454 && tree_to_uhwi (@3) == c2
6455 && tree_to_uhwi (@9) == c3
6456 && tree_to_uhwi (@7) == c3
6457 && tree_to_uhwi (@11) == c4
6458 && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6460 (convert (IFN_POPCOUNT:type @0)))))
6462 /* __builtin_ffs needs to deal on many targets with the possible zero
6463 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6464 should lead to better code. */
6466 (FFS tree_expr_nonzero_p@0)
6467 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6468 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6469 OPTIMIZE_FOR_SPEED))
6470 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6471 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6474 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6476 /* __builtin_ffs (X) == 0 -> X == 0.
6477 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6480 (cmp (ffs@2 @0) INTEGER_CST@1)
6481 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6483 (if (integer_zerop (@1))
6484 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6485 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6486 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6487 (if (single_use (@2))
6488 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6489 wi::mask (tree_to_uhwi (@1),
6491 { wide_int_to_tree (TREE_TYPE (@0),
6492 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6493 false, prec)); }))))))
6495 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
6499 bit_op (bit_and bit_ior)
6501 (cmp (ffs@2 @0) INTEGER_CST@1)
6502 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6504 (if (integer_zerop (@1))
6505 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6506 (if (tree_int_cst_sgn (@1) < 0)
6507 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6508 (if (wi::to_widest (@1) >= prec)
6509 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6510 (if (wi::to_widest (@1) == prec - 1)
6511 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
6512 wi::shifted_mask (prec - 1, 1,
6514 (if (single_use (@2))
6515 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
6517 { wide_int_to_tree (TREE_TYPE (@0),
6518 wi::mask (tree_to_uhwi (@1),
6520 { build_zero_cst (TREE_TYPE (@0)); }))))))))
6529 r = c ? a1 op a2 : b;
6531 if the target can do it in one go. This makes the operation conditional
6532 on c, so could drop potentially-trapping arithmetic, but that's a valid
6533 simplification if the result of the operation isn't needed.
6535 Avoid speculatively generating a stand-alone vector comparison
6536 on targets that might not support them. Any target implementing
6537 conditional internal functions must support the same comparisons
6538 inside and outside a VEC_COND_EXPR. */
6541 (for uncond_op (UNCOND_BINARY)
6542 cond_op (COND_BINARY)
6544 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
6545 (with { tree op_type = TREE_TYPE (@4); }
6546 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6547 && element_precision (type) == element_precision (op_type))
6548 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
6550 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
6551 (with { tree op_type = TREE_TYPE (@4); }
6552 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6553 && element_precision (type) == element_precision (op_type))
6554 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6556 /* Same for ternary operations. */
6557 (for uncond_op (UNCOND_TERNARY)
6558 cond_op (COND_TERNARY)
6560 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
6561 (with { tree op_type = TREE_TYPE (@5); }
6562 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6563 && element_precision (type) == element_precision (op_type))
6564 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
6566 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
6567 (with { tree op_type = TREE_TYPE (@5); }
6568 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6569 && element_precision (type) == element_precision (op_type))
6570 (view_convert (cond_op (bit_not @0) @2 @3 @4
6571 (view_convert:op_type @1)))))))
6574 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
6575 "else" value of an IFN_COND_*. */
6576 (for cond_op (COND_BINARY)
6578 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
6579 (with { tree op_type = TREE_TYPE (@3); }
6580 (if (element_precision (type) == element_precision (op_type))
6581 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
6583 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
6584 (with { tree op_type = TREE_TYPE (@5); }
6585 (if (inverse_conditions_p (@0, @2)
6586 && element_precision (type) == element_precision (op_type))
6587 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
6589 /* Same for ternary operations. */
6590 (for cond_op (COND_TERNARY)
6592 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
6593 (with { tree op_type = TREE_TYPE (@4); }
6594 (if (element_precision (type) == element_precision (op_type))
6595 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
6597 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
6598 (with { tree op_type = TREE_TYPE (@6); }
6599 (if (inverse_conditions_p (@0, @2)
6600 && element_precision (type) == element_precision (op_type))
6601 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
6603 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
6606 A: (@0 + @1 < @2) | (@2 + @1 < @0)
6607 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
6609 If pointers are known not to wrap, B checks whether @1 bytes starting
6610 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
6611 bytes. A is more efficiently tested as:
6613 A: (sizetype) (@0 + @1 - @2) > @1 * 2
6615 The equivalent expression for B is given by replacing @1 with @1 - 1:
6617 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
6619 @0 and @2 can be swapped in both expressions without changing the result.
6621 The folds rely on sizetype's being unsigned (which is always true)
6622 and on its being the same width as the pointer (which we have to check).
6624 The fold replaces two pointer_plus expressions, two comparisons and
6625 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
6626 the best case it's a saving of two operations. The A fold retains one
6627 of the original pointer_pluses, so is a win even if both pointer_pluses
6628 are used elsewhere. The B fold is a wash if both pointer_pluses are
6629 used elsewhere, since all we end up doing is replacing a comparison with
6630 a pointer_plus. We do still apply the fold under those circumstances
6631 though, in case applying it to other conditions eventually makes one of the
6632 pointer_pluses dead. */
6633 (for ior (truth_orif truth_or bit_ior)
6636 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
6637 (cmp:cs (pointer_plus@4 @2 @1) @0))
6638 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6639 && TYPE_OVERFLOW_WRAPS (sizetype)
6640 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
6641 /* Calculate the rhs constant. */
6642 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
6643 offset_int rhs = off * 2; }
6644 /* Always fails for negative values. */
6645 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
6646 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
6647 pick a canonical order. This increases the chances of using the
6648 same pointer_plus in multiple checks. */
6649 (with { bool swap_p = tree_swap_operands_p (@0, @2);
6650 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
6651 (if (cmp == LT_EXPR)
6652 (gt (convert:sizetype
6653 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
6654 { swap_p ? @0 : @2; }))
6656 (gt (convert:sizetype
6657 (pointer_diff:ssizetype
6658 (pointer_plus { swap_p ? @2 : @0; }
6659 { wide_int_to_tree (sizetype, off); })
6660 { swap_p ? @0 : @2; }))
6661 { rhs_tree; })))))))))
6663 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6665 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6666 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6667 (with { int i = single_nonzero_element (@1); }
6669 (with { tree elt = vector_cst_elt (@1, i);
6670 tree elt_type = TREE_TYPE (elt);
6671 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6672 tree size = bitsize_int (elt_bits);
6673 tree pos = bitsize_int (elt_bits * i); }
6676 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6680 (vec_perm @0 @1 VECTOR_CST@2)
6683 tree op0 = @0, op1 = @1, op2 = @2;
6685 /* Build a vector of integers from the tree mask. */
6686 vec_perm_builder builder;
6687 if (!tree_to_vec_perm_builder (&builder, op2))
6690 /* Create a vec_perm_indices for the integer vector. */
6691 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6692 bool single_arg = (op0 == op1);
6693 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6695 (if (sel.series_p (0, 1, 0, 1))
6697 (if (sel.series_p (0, 1, nelts, 1))
6703 if (sel.all_from_input_p (0))
6705 else if (sel.all_from_input_p (1))
6708 sel.rotate_inputs (1);
6710 else if (known_ge (poly_uint64 (sel[0]), nelts))
6712 std::swap (op0, op1);
6713 sel.rotate_inputs (1);
6717 tree cop0 = op0, cop1 = op1;
6718 if (TREE_CODE (op0) == SSA_NAME
6719 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6720 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6721 cop0 = gimple_assign_rhs1 (def);
6722 if (TREE_CODE (op1) == SSA_NAME
6723 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6724 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6725 cop1 = gimple_assign_rhs1 (def);
6729 (if ((TREE_CODE (cop0) == VECTOR_CST
6730 || TREE_CODE (cop0) == CONSTRUCTOR)
6731 && (TREE_CODE (cop1) == VECTOR_CST
6732 || TREE_CODE (cop1) == CONSTRUCTOR)
6733 && (t = fold_vec_perm (type, cop0, cop1, sel)))
6737 bool changed = (op0 == op1 && !single_arg);
6738 tree ins = NULL_TREE;
6741 /* See if the permutation is performing a single element
6742 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6743 in that case. But only if the vector mode is supported,
6744 otherwise this is invalid GIMPLE. */
6745 if (TYPE_MODE (type) != BLKmode
6746 && (TREE_CODE (cop0) == VECTOR_CST
6747 || TREE_CODE (cop0) == CONSTRUCTOR
6748 || TREE_CODE (cop1) == VECTOR_CST
6749 || TREE_CODE (cop1) == CONSTRUCTOR))
6751 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6754 /* After canonicalizing the first elt to come from the
6755 first vector we only can insert the first elt from
6756 the first vector. */
6758 if ((ins = fold_read_from_vector (cop0, sel[0])))
6761 /* The above can fail for two-element vectors which always
6762 appear to insert the first element, so try inserting
6763 into the second lane as well. For more than two
6764 elements that's wasted time. */
6765 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6767 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6768 for (at = 0; at < encoded_nelts; ++at)
6769 if (maybe_ne (sel[at], at))
6771 if (at < encoded_nelts
6772 && (known_eq (at + 1, nelts)
6773 || sel.series_p (at + 1, 1, at + 1, 1)))
6775 if (known_lt (poly_uint64 (sel[at]), nelts))
6776 ins = fold_read_from_vector (cop0, sel[at]);
6778 ins = fold_read_from_vector (cop1, sel[at] - nelts);
6783 /* Generate a canonical form of the selector. */
6784 if (!ins && sel.encoding () != builder)
6786 /* Some targets are deficient and fail to expand a single
6787 argument permutation while still allowing an equivalent
6788 2-argument version. */
6790 if (sel.ninputs () == 2
6791 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6792 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6795 vec_perm_indices sel2 (builder, 2, nelts);
6796 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6797 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6799 /* Not directly supported with either encoding,
6800 so use the preferred form. */
6801 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6803 if (!operand_equal_p (op2, oldop2, 0))
6808 (bit_insert { op0; } { ins; }
6809 { bitsize_int (at * vector_element_bits (type)); })
6811 (vec_perm { op0; } { op1; } { op2; }))))))))))
6813 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
6815 (match vec_same_elem_p
6817 (if (uniform_vector_p (@0))))
6819 (match vec_same_elem_p
6823 (vec_perm vec_same_elem_p@0 @0 @1)
6826 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6827 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6828 constant which when multiplied by a power of 2 contains a unique value
6829 in the top 5 or 6 bits. This is then indexed into a table which maps it
6830 to the number of trailing zeroes. */
6831 (match (ctz_table_index @1 @2 @3)
6832 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))