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 > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
257 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
258 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
259 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
261 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
262 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
263 && types_match (type, TREE_TYPE (@0)))
265 (if (types_match (type, float_type_node))
266 (BUILT_IN_COPYSIGNF @1 (outp @0)))
267 (if (types_match (type, double_type_node))
268 (BUILT_IN_COPYSIGN @1 (outp @0)))
269 (if (types_match (type, long_double_type_node))
270 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
271 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
272 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
273 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
274 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
276 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
277 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
278 && types_match (type, TREE_TYPE (@0)))
280 (if (types_match (type, float_type_node))
281 (BUILT_IN_COPYSIGNF @1 (outn @0)))
282 (if (types_match (type, double_type_node))
283 (BUILT_IN_COPYSIGN @1 (outn @0)))
284 (if (types_match (type, long_double_type_node))
285 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
287 /* Transform X * copysign (1.0, X) into abs(X). */
289 (mult:c @0 (COPYSIGN_ALL real_onep @0))
290 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
293 /* Transform X * copysign (1.0, -X) into -abs(X). */
295 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
296 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
299 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
301 (COPYSIGN_ALL REAL_CST@0 @1)
302 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
303 (COPYSIGN_ALL (negate @0) @1)))
305 /* X * 1, X / 1 -> X. */
306 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
311 /* (A / (1 << B)) -> (A >> B).
312 Only for unsigned A. For signed A, this would not preserve rounding
314 For example: (-1 / ( 1 << B)) != -1 >> B.
315 Also also widening conversions, like:
316 (A / (unsigned long long) (1U << B)) -> (A >> B)
318 (A / (unsigned long long) (1 << B)) -> (A >> B).
319 If the left shift is signed, it can be done only if the upper bits
320 of A starting from shift's type sign bit are zero, as
321 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
322 so it is valid only if A >> 31 is zero. */
324 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
325 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
326 && (!VECTOR_TYPE_P (type)
327 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
328 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
329 && (useless_type_conversion_p (type, TREE_TYPE (@1))
330 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
331 && (TYPE_UNSIGNED (TREE_TYPE (@1))
332 || (element_precision (type)
333 == element_precision (TREE_TYPE (@1)))
334 || (INTEGRAL_TYPE_P (type)
335 && (tree_nonzero_bits (@0)
336 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
338 element_precision (type))) == 0)))))
339 (if (!VECTOR_TYPE_P (type)
340 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
341 && element_precision (TREE_TYPE (@3)) < element_precision (type))
342 (convert (rshift @3 @2))
345 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
346 undefined behavior in constexpr evaluation, and assuming that the division
347 traps enables better optimizations than these anyway. */
348 (for div (trunc_div ceil_div floor_div round_div exact_div)
349 /* 0 / X is always zero. */
351 (div integer_zerop@0 @1)
352 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
353 (if (!integer_zerop (@1))
357 (div @0 integer_minus_onep@1)
358 (if (!TYPE_UNSIGNED (type))
360 /* X / bool_range_Y is X. */
363 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
368 /* But not for 0 / 0 so that we can get the proper warnings and errors.
369 And not for _Fract types where we can't build 1. */
370 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
371 { build_one_cst (type); }))
372 /* X / abs (X) is X < 0 ? -1 : 1. */
375 (if (INTEGRAL_TYPE_P (type)
376 && TYPE_OVERFLOW_UNDEFINED (type))
377 (cond (lt @0 { build_zero_cst (type); })
378 { build_minus_one_cst (type); } { build_one_cst (type); })))
381 (div:C @0 (negate @0))
382 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
383 && TYPE_OVERFLOW_UNDEFINED (type))
384 { build_minus_one_cst (type); })))
386 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
387 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
390 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
391 && TYPE_UNSIGNED (type))
394 /* Combine two successive divisions. Note that combining ceil_div
395 and floor_div is trickier and combining round_div even more so. */
396 (for div (trunc_div exact_div)
398 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
400 wi::overflow_type overflow;
401 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
402 TYPE_SIGN (type), &overflow);
404 (if (div == EXACT_DIV_EXPR
405 || optimize_successive_divisions_p (@2, @3))
407 (div @0 { wide_int_to_tree (type, mul); })
408 (if (TYPE_UNSIGNED (type)
409 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
410 { build_zero_cst (type); }))))))
412 /* Combine successive multiplications. Similar to above, but handling
413 overflow is different. */
415 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
417 wi::overflow_type overflow;
418 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
419 TYPE_SIGN (type), &overflow);
421 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
422 otherwise undefined overflow implies that @0 must be zero. */
423 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
424 (mult @0 { wide_int_to_tree (type, mul); }))))
426 /* Optimize A / A to 1.0 if we don't care about
427 NaNs or Infinities. */
430 (if (FLOAT_TYPE_P (type)
431 && ! HONOR_NANS (type)
432 && ! HONOR_INFINITIES (type))
433 { build_one_cst (type); }))
435 /* Optimize -A / A to -1.0 if we don't care about
436 NaNs or Infinities. */
438 (rdiv:C @0 (negate @0))
439 (if (FLOAT_TYPE_P (type)
440 && ! HONOR_NANS (type)
441 && ! HONOR_INFINITIES (type))
442 { build_minus_one_cst (type); }))
444 /* PR71078: x / abs(x) -> copysign (1.0, x) */
446 (rdiv:C (convert? @0) (convert? (abs @0)))
447 (if (SCALAR_FLOAT_TYPE_P (type)
448 && ! HONOR_NANS (type)
449 && ! HONOR_INFINITIES (type))
451 (if (types_match (type, float_type_node))
452 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
453 (if (types_match (type, double_type_node))
454 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
455 (if (types_match (type, long_double_type_node))
456 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
458 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
461 (if (!HONOR_SNANS (type))
464 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
466 (rdiv @0 real_minus_onep)
467 (if (!HONOR_SNANS (type))
470 (if (flag_reciprocal_math)
471 /* Convert (A/B)/C to A/(B*C). */
473 (rdiv (rdiv:s @0 @1) @2)
474 (rdiv @0 (mult @1 @2)))
476 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
478 (rdiv @0 (mult:s @1 REAL_CST@2))
480 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
482 (rdiv (mult @0 { tem; } ) @1))))
484 /* Convert A/(B/C) to (A/B)*C */
486 (rdiv @0 (rdiv:s @1 @2))
487 (mult (rdiv @0 @1) @2)))
489 /* Simplify x / (- y) to -x / y. */
491 (rdiv @0 (negate @1))
492 (rdiv (negate @0) @1))
494 (if (flag_unsafe_math_optimizations)
495 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
496 Since C / x may underflow to zero, do this only for unsafe math. */
497 (for op (lt le gt ge)
500 (op (rdiv REAL_CST@0 @1) real_zerop@2)
501 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
503 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
505 /* For C < 0, use the inverted operator. */
506 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
509 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
510 (for div (trunc_div ceil_div floor_div round_div exact_div)
512 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
513 (if (integer_pow2p (@2)
514 && tree_int_cst_sgn (@2) > 0
515 && tree_nop_conversion_p (type, TREE_TYPE (@0))
516 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
518 { build_int_cst (integer_type_node,
519 wi::exact_log2 (wi::to_wide (@2))); }))))
521 /* If ARG1 is a constant, we can convert this to a multiply by the
522 reciprocal. This does not have the same rounding properties,
523 so only do this if -freciprocal-math. We can actually
524 always safely do it if ARG1 is a power of two, but it's hard to
525 tell if it is or not in a portable manner. */
526 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
530 (if (flag_reciprocal_math
533 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
535 (mult @0 { tem; } )))
536 (if (cst != COMPLEX_CST)
537 (with { tree inverse = exact_inverse (type, @1); }
539 (mult @0 { inverse; } ))))))))
541 (for mod (ceil_mod floor_mod round_mod trunc_mod)
542 /* 0 % X is always zero. */
544 (mod integer_zerop@0 @1)
545 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
546 (if (!integer_zerop (@1))
548 /* X % 1 is always zero. */
550 (mod @0 integer_onep)
551 { build_zero_cst (type); })
552 /* X % -1 is zero. */
554 (mod @0 integer_minus_onep@1)
555 (if (!TYPE_UNSIGNED (type))
556 { build_zero_cst (type); }))
560 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
561 (if (!integer_zerop (@0))
562 { build_zero_cst (type); }))
563 /* (X % Y) % Y is just X % Y. */
565 (mod (mod@2 @0 @1) @1)
567 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
569 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
570 (if (ANY_INTEGRAL_TYPE_P (type)
571 && TYPE_OVERFLOW_UNDEFINED (type)
572 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
574 { build_zero_cst (type); }))
575 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
576 modulo and comparison, since it is simpler and equivalent. */
579 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
580 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
581 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
582 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
584 /* X % -C is the same as X % C. */
586 (trunc_mod @0 INTEGER_CST@1)
587 (if (TYPE_SIGN (type) == SIGNED
588 && !TREE_OVERFLOW (@1)
589 && wi::neg_p (wi::to_wide (@1))
590 && !TYPE_OVERFLOW_TRAPS (type)
591 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
592 && !sign_bit_p (@1, @1))
593 (trunc_mod @0 (negate @1))))
595 /* X % -Y is the same as X % Y. */
597 (trunc_mod @0 (convert? (negate @1)))
598 (if (INTEGRAL_TYPE_P (type)
599 && !TYPE_UNSIGNED (type)
600 && !TYPE_OVERFLOW_TRAPS (type)
601 && tree_nop_conversion_p (type, TREE_TYPE (@1))
602 /* Avoid this transformation if X might be INT_MIN or
603 Y might be -1, because we would then change valid
604 INT_MIN % -(-1) into invalid INT_MIN % -1. */
605 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
606 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
608 (trunc_mod @0 (convert @1))))
610 /* X - (X / Y) * Y is the same as X % Y. */
612 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
613 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
614 (convert (trunc_mod @0 @1))))
616 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
617 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
618 Also optimize A % (C << N) where C is a power of 2,
619 to A & ((C << N) - 1).
620 Also optimize "A shift (B % C)", if C is a power of 2, to
621 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
622 and assume (B % C) is nonnegative as shifts negative values would
624 (match (power_of_two_cand @1)
626 (match (power_of_two_cand @1)
627 (lshift INTEGER_CST@1 @2))
628 (for mod (trunc_mod floor_mod)
629 (for shift (lshift rshift)
631 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
632 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
633 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
636 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
637 (if ((TYPE_UNSIGNED (type)
638 || tree_expr_nonnegative_p (@0))
639 && tree_nop_conversion_p (type, TREE_TYPE (@3))
640 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
641 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
643 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
645 (trunc_div (mult @0 integer_pow2p@1) @1)
646 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
647 (bit_and @0 { wide_int_to_tree
648 (type, wi::mask (TYPE_PRECISION (type)
649 - wi::exact_log2 (wi::to_wide (@1)),
650 false, TYPE_PRECISION (type))); })))
652 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
654 (mult (trunc_div @0 integer_pow2p@1) @1)
655 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
656 (bit_and @0 (negate @1))))
658 /* Simplify (t * 2) / 2) -> t. */
659 (for div (trunc_div ceil_div floor_div round_div exact_div)
661 (div (mult:c @0 @1) @1)
662 (if (ANY_INTEGRAL_TYPE_P (type))
663 (if (TYPE_OVERFLOW_UNDEFINED (type))
668 bool overflowed = true;
669 wide_int wmin0, wmax0, wmin1, wmax1;
670 if (INTEGRAL_TYPE_P (type)
671 && get_range_info (@0, &wmin0, &wmax0) == VR_RANGE
672 && get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
674 /* If the multiplication can't overflow/wrap around, then
675 it can be optimized too. */
676 wi::overflow_type min_ovf, max_ovf;
677 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
678 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
679 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
681 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
682 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
683 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
694 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
699 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
702 (pows (op @0) REAL_CST@1)
703 (with { HOST_WIDE_INT n; }
704 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
706 /* Likewise for powi. */
709 (pows (op @0) INTEGER_CST@1)
710 (if ((wi::to_wide (@1) & 1) == 0)
712 /* Strip negate and abs from both operands of hypot. */
720 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
721 (for copysigns (COPYSIGN_ALL)
723 (copysigns (op @0) @1)
726 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
731 /* Convert absu(x)*absu(x) -> x*x. */
733 (mult (absu@1 @0) @1)
734 (mult (convert@2 @0) @2))
736 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
740 (coss (copysigns @0 @1))
743 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
747 (pows (copysigns @0 @2) REAL_CST@1)
748 (with { HOST_WIDE_INT n; }
749 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
751 /* Likewise for powi. */
755 (pows (copysigns @0 @2) INTEGER_CST@1)
756 (if ((wi::to_wide (@1) & 1) == 0)
761 /* hypot(copysign(x, y), z) -> hypot(x, z). */
763 (hypots (copysigns @0 @1) @2)
765 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
767 (hypots @0 (copysigns @1 @2))
770 /* copysign(x, CST) -> [-]abs (x). */
771 (for copysigns (COPYSIGN_ALL)
773 (copysigns @0 REAL_CST@1)
774 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
778 /* copysign(copysign(x, y), z) -> copysign(x, z). */
779 (for copysigns (COPYSIGN_ALL)
781 (copysigns (copysigns @0 @1) @2)
784 /* copysign(x,y)*copysign(x,y) -> x*x. */
785 (for copysigns (COPYSIGN_ALL)
787 (mult (copysigns@2 @0 @1) @2)
790 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
791 (for ccoss (CCOS CCOSH)
796 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
797 (for ops (conj negate)
803 /* Fold (a * (1 << b)) into (a << b) */
805 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
806 (if (! FLOAT_TYPE_P (type)
807 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
810 /* Fold (1 << (C - x)) where C = precision(type) - 1
811 into ((1 << C) >> x). */
813 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
814 (if (INTEGRAL_TYPE_P (type)
815 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
817 (if (TYPE_UNSIGNED (type))
818 (rshift (lshift @0 @2) @3)
820 { tree utype = unsigned_type_for (type); }
821 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
823 /* Fold (C1/X)*C2 into (C1*C2)/X. */
825 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
826 (if (flag_associative_math
829 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
831 (rdiv { tem; } @1)))))
833 /* Simplify ~X & X as zero. */
835 (bit_and:c (convert? @0) (convert? (bit_not @0)))
836 { build_zero_cst (type); })
838 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
840 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
841 (if (TYPE_UNSIGNED (type))
842 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
844 (for bitop (bit_and bit_ior)
846 /* PR35691: Transform
847 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
848 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
850 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
851 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
852 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
853 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
854 (cmp (bit_ior @0 (convert @1)) @2)))
856 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
857 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
859 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
860 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
861 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
862 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
863 (cmp (bit_and @0 (convert @1)) @2))))
865 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
867 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
868 (minus (bit_xor @0 @1) @1))
870 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
871 (if (~wi::to_wide (@2) == wi::to_wide (@1))
872 (minus (bit_xor @0 @1) @1)))
874 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
876 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
877 (minus @1 (bit_xor @0 @1)))
879 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
880 (for op (bit_ior bit_xor plus)
882 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
885 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
886 (if (~wi::to_wide (@2) == wi::to_wide (@1))
889 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
891 (bit_ior:c (bit_xor:c @0 @1) @0)
894 /* (a & ~b) | (a ^ b) --> a ^ b */
896 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
899 /* (a & ~b) ^ ~a --> ~(a & b) */
901 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
902 (bit_not (bit_and @0 @1)))
904 /* (~a & b) ^ a --> (a | b) */
906 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
909 /* (a | b) & ~(a ^ b) --> a & b */
911 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
914 /* a | ~(a ^ b) --> a | ~b */
916 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
917 (bit_ior @0 (bit_not @1)))
919 /* (a | b) | (a &^ b) --> a | b */
920 (for op (bit_and bit_xor)
922 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
925 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
927 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
930 /* ~(~a & b) --> a | ~b */
932 (bit_not (bit_and:cs (bit_not @0) @1))
933 (bit_ior @0 (bit_not @1)))
935 /* ~(~a | b) --> a & ~b */
937 (bit_not (bit_ior:cs (bit_not @0) @1))
938 (bit_and @0 (bit_not @1)))
940 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
942 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
943 (bit_and @3 (bit_not @2)))
945 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
947 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
951 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
953 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
954 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
956 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
958 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
959 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
961 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
963 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
964 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
965 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
969 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
970 ((A & N) + B) & M -> (A + B) & M
971 Similarly if (N & M) == 0,
972 ((A | N) + B) & M -> (A + B) & M
973 and for - instead of + (or unary - instead of +)
974 and/or ^ instead of |.
975 If B is constant and (B & M) == 0, fold into A & M. */
977 (for bitop (bit_and bit_ior bit_xor)
979 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
982 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
983 @3, @4, @1, ERROR_MARK, NULL_TREE,
986 (convert (bit_and (op (convert:utype { pmop[0]; })
987 (convert:utype { pmop[1]; }))
988 (convert:utype @2))))))
990 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
993 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
994 NULL_TREE, NULL_TREE, @1, bitop, @3,
997 (convert (bit_and (op (convert:utype { pmop[0]; })
998 (convert:utype { pmop[1]; }))
999 (convert:utype @2)))))))
1001 (bit_and (op:s @0 @1) INTEGER_CST@2)
1004 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1005 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1006 NULL_TREE, NULL_TREE, pmop); }
1008 (convert (bit_and (op (convert:utype { pmop[0]; })
1009 (convert:utype { pmop[1]; }))
1010 (convert:utype @2)))))))
1011 (for bitop (bit_and bit_ior bit_xor)
1013 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1016 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1017 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1018 NULL_TREE, NULL_TREE, pmop); }
1020 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1021 (convert:utype @1)))))))
1023 /* X % Y is smaller than Y. */
1026 (cmp (trunc_mod @0 @1) @1)
1027 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1028 { constant_boolean_node (cmp == LT_EXPR, type); })))
1031 (cmp @1 (trunc_mod @0 @1))
1032 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1033 { constant_boolean_node (cmp == GT_EXPR, type); })))
1037 (bit_ior @0 integer_all_onesp@1)
1042 (bit_ior @0 integer_zerop)
1047 (bit_and @0 integer_zerop@1)
1053 (for op (bit_ior bit_xor plus)
1055 (op:c (convert? @0) (convert? (bit_not @0)))
1056 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1061 { build_zero_cst (type); })
1063 /* Canonicalize X ^ ~0 to ~X. */
1065 (bit_xor @0 integer_all_onesp@1)
1070 (bit_and @0 integer_all_onesp)
1073 /* x & x -> x, x | x -> x */
1074 (for bitop (bit_and bit_ior)
1079 /* x & C -> x if we know that x & ~C == 0. */
1082 (bit_and SSA_NAME@0 INTEGER_CST@1)
1083 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1084 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1088 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1090 (bit_not (minus (bit_not @0) @1))
1093 (bit_not (plus:c (bit_not @0) @1))
1096 /* ~(X - Y) -> ~X + Y. */
1098 (bit_not (minus:s @0 @1))
1099 (plus (bit_not @0) @1))
1101 (bit_not (plus:s @0 INTEGER_CST@1))
1102 (if ((INTEGRAL_TYPE_P (type)
1103 && TYPE_UNSIGNED (type))
1104 || (!TYPE_OVERFLOW_SANITIZED (type)
1105 && may_negate_without_overflow_p (@1)))
1106 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1109 /* ~X + Y -> (Y - X) - 1. */
1111 (plus:c (bit_not @0) @1)
1112 (if (ANY_INTEGRAL_TYPE_P (type)
1113 && TYPE_OVERFLOW_WRAPS (type)
1114 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1115 && !integer_all_onesp (@1))
1116 (plus (minus @1 @0) { build_minus_one_cst (type); })
1117 (if (INTEGRAL_TYPE_P (type)
1118 && TREE_CODE (@1) == INTEGER_CST
1119 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1121 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1123 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1125 (bit_not (rshift:s @0 @1))
1126 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1127 (rshift (bit_not! @0) @1)
1128 /* For logical right shifts, this is possible only if @0 doesn't
1129 have MSB set and the logical right shift is changed into
1130 arithmetic shift. */
1131 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1132 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1133 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1136 /* x + (x & 1) -> (x + 1) & ~1 */
1138 (plus:c @0 (bit_and:s @0 integer_onep@1))
1139 (bit_and (plus @0 @1) (bit_not @1)))
1141 /* x & ~(x & y) -> x & ~y */
1142 /* x | ~(x | y) -> x | ~y */
1143 (for bitop (bit_and bit_ior)
1145 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1146 (bitop @0 (bit_not @1))))
1148 /* (~x & y) | ~(x | y) -> ~x */
1150 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1153 /* (x | y) ^ (x | ~y) -> ~x */
1155 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1158 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1160 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1161 (bit_not (bit_xor @0 @1)))
1163 /* (~x | y) ^ (x ^ y) -> x | ~y */
1165 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1166 (bit_ior @0 (bit_not @1)))
1168 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1170 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1171 (bit_not (bit_and @0 @1)))
1173 /* (x | y) & ~x -> y & ~x */
1174 /* (x & y) | ~x -> y | ~x */
1175 (for bitop (bit_and bit_ior)
1176 rbitop (bit_ior bit_and)
1178 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1181 /* (x & y) ^ (x | y) -> x ^ y */
1183 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1186 /* (x ^ y) ^ (x | y) -> x & y */
1188 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1191 /* (x & y) + (x ^ y) -> x | y */
1192 /* (x & y) | (x ^ y) -> x | y */
1193 /* (x & y) ^ (x ^ y) -> x | y */
1194 (for op (plus bit_ior bit_xor)
1196 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1199 /* (x & y) + (x | y) -> x + y */
1201 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1204 /* (x + y) - (x | y) -> x & y */
1206 (minus (plus @0 @1) (bit_ior @0 @1))
1207 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1208 && !TYPE_SATURATING (type))
1211 /* (x + y) - (x & y) -> x | y */
1213 (minus (plus @0 @1) (bit_and @0 @1))
1214 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1215 && !TYPE_SATURATING (type))
1218 /* (x | y) - y -> (x & ~y) */
1220 (minus (bit_ior:cs @0 @1) @1)
1221 (bit_and @0 (bit_not @1)))
1223 /* (x | y) - (x ^ y) -> x & y */
1225 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1228 /* (x | y) - (x & y) -> x ^ y */
1230 (minus (bit_ior @0 @1) (bit_and @0 @1))
1233 /* (x | y) & ~(x & y) -> x ^ y */
1235 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1238 /* (x | y) & (~x ^ y) -> x & y */
1240 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1243 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1245 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1246 (bit_not (bit_xor @0 @1)))
1248 /* (~x | y) ^ (x | ~y) -> x ^ y */
1250 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1253 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1255 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1256 (nop_convert2? (bit_ior @0 @1))))
1258 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1259 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1260 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1261 && !TYPE_SATURATING (TREE_TYPE (@2)))
1262 (bit_not (convert (bit_xor @0 @1)))))
1264 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1266 (nop_convert3? (bit_ior @0 @1)))
1267 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1268 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1269 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1270 && !TYPE_SATURATING (TREE_TYPE (@2)))
1271 (bit_not (convert (bit_xor @0 @1)))))
1273 (minus (nop_convert1? (bit_and @0 @1))
1274 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1276 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1277 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1278 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1279 && !TYPE_SATURATING (TREE_TYPE (@2)))
1280 (bit_not (convert (bit_xor @0 @1)))))
1282 /* ~x & ~y -> ~(x | y)
1283 ~x | ~y -> ~(x & y) */
1284 (for op (bit_and bit_ior)
1285 rop (bit_ior bit_and)
1287 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1288 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1289 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1290 (bit_not (rop (convert @0) (convert @1))))))
1292 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1293 with a constant, and the two constants have no bits in common,
1294 we should treat this as a BIT_IOR_EXPR since this may produce more
1296 (for op (bit_xor plus)
1298 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1299 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1300 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1301 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1302 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1303 (bit_ior (convert @4) (convert @5)))))
1305 /* (X | Y) ^ X -> Y & ~ X*/
1307 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1308 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1309 (convert (bit_and @1 (bit_not @0)))))
1311 /* Convert ~X ^ ~Y to X ^ Y. */
1313 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1314 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1315 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1316 (bit_xor (convert @0) (convert @1))))
1318 /* Convert ~X ^ C to X ^ ~C. */
1320 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1321 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1322 (bit_xor (convert @0) (bit_not @1))))
1324 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1325 (for opo (bit_and bit_xor)
1326 opi (bit_xor bit_and)
1328 (opo:c (opi:cs @0 @1) @1)
1329 (bit_and (bit_not @0) @1)))
1331 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1332 operands are another bit-wise operation with a common input. If so,
1333 distribute the bit operations to save an operation and possibly two if
1334 constants are involved. For example, convert
1335 (A | B) & (A | C) into A | (B & C)
1336 Further simplification will occur if B and C are constants. */
1337 (for op (bit_and bit_ior bit_xor)
1338 rop (bit_ior bit_and bit_and)
1340 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1341 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1342 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1343 (rop (convert @0) (op (convert @1) (convert @2))))))
1345 /* Some simple reassociation for bit operations, also handled in reassoc. */
1346 /* (X & Y) & Y -> X & Y
1347 (X | Y) | Y -> X | Y */
1348 (for op (bit_and bit_ior)
1350 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1352 /* (X ^ Y) ^ Y -> X */
1354 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1356 /* (X & Y) & (X & Z) -> (X & Y) & Z
1357 (X | Y) | (X | Z) -> (X | Y) | Z */
1358 (for op (bit_and bit_ior)
1360 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1361 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1362 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1363 (if (single_use (@5) && single_use (@6))
1364 (op @3 (convert @2))
1365 (if (single_use (@3) && single_use (@4))
1366 (op (convert @1) @5))))))
1367 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1369 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1370 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1371 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1372 (bit_xor (convert @1) (convert @2))))
1374 /* Convert abs (abs (X)) into abs (X).
1375 also absu (absu (X)) into absu (X). */
1381 (absu (convert@2 (absu@1 @0)))
1382 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1385 /* Convert abs[u] (-X) -> abs[u] (X). */
1394 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1396 (abs tree_expr_nonnegative_p@0)
1400 (absu tree_expr_nonnegative_p@0)
1403 /* Simplify (-(X < 0) | 1) * X into abs (X). */
1405 (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
1406 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
1409 /* Similarly (-(X < 0) | 1U) * X into absu (X). */
1411 (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
1412 integer_onep) (nop_convert @0))
1413 (if (INTEGRAL_TYPE_P (type)
1414 && TYPE_UNSIGNED (type)
1415 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1416 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1419 /* A few cases of fold-const.c negate_expr_p predicate. */
1420 (match negate_expr_p
1422 (if ((INTEGRAL_TYPE_P (type)
1423 && TYPE_UNSIGNED (type))
1424 || (!TYPE_OVERFLOW_SANITIZED (type)
1425 && may_negate_without_overflow_p (t)))))
1426 (match negate_expr_p
1428 (match negate_expr_p
1430 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1431 (match negate_expr_p
1433 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1434 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1436 (match negate_expr_p
1438 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1439 (match negate_expr_p
1441 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1442 || (FLOAT_TYPE_P (type)
1443 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1444 && !HONOR_SIGNED_ZEROS (type)))))
1446 /* (-A) * (-B) -> A * B */
1448 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1449 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1450 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1451 (mult (convert @0) (convert (negate @1)))))
1453 /* -(A + B) -> (-B) - A. */
1455 (negate (plus:c @0 negate_expr_p@1))
1456 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1457 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1458 (minus (negate @1) @0)))
1460 /* -(A - B) -> B - A. */
1462 (negate (minus @0 @1))
1463 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1464 || (FLOAT_TYPE_P (type)
1465 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1466 && !HONOR_SIGNED_ZEROS (type)))
1469 (negate (pointer_diff @0 @1))
1470 (if (TYPE_OVERFLOW_UNDEFINED (type))
1471 (pointer_diff @1 @0)))
1473 /* A - B -> A + (-B) if B is easily negatable. */
1475 (minus @0 negate_expr_p@1)
1476 (if (!FIXED_POINT_TYPE_P (type))
1477 (plus @0 (negate @1))))
1479 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1481 For bitwise binary operations apply operand conversions to the
1482 binary operation result instead of to the operands. This allows
1483 to combine successive conversions and bitwise binary operations.
1484 We combine the above two cases by using a conditional convert. */
1485 (for bitop (bit_and bit_ior bit_xor)
1487 (bitop (convert@2 @0) (convert?@3 @1))
1488 (if (((TREE_CODE (@1) == INTEGER_CST
1489 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1490 && int_fits_type_p (@1, TREE_TYPE (@0)))
1491 || types_match (@0, @1))
1492 /* ??? This transform conflicts with fold-const.c doing
1493 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1494 constants (if x has signed type, the sign bit cannot be set
1495 in c). This folds extension into the BIT_AND_EXPR.
1496 Restrict it to GIMPLE to avoid endless recursions. */
1497 && (bitop != BIT_AND_EXPR || GIMPLE)
1498 && (/* That's a good idea if the conversion widens the operand, thus
1499 after hoisting the conversion the operation will be narrower. */
1500 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1501 /* It's also a good idea if the conversion is to a non-integer
1503 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1504 /* Or if the precision of TO is not the same as the precision
1506 || !type_has_mode_precision_p (type)
1507 /* In GIMPLE, getting rid of 2 conversions for one new results
1510 && TREE_CODE (@1) != INTEGER_CST
1511 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1513 && single_use (@3))))
1514 (convert (bitop @0 (convert @1)))))
1515 /* In GIMPLE, getting rid of 2 conversions for one new results
1518 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1520 && TREE_CODE (@1) != INTEGER_CST
1521 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1522 && types_match (type, @0))
1523 (bitop @0 (convert @1)))))
1525 (for bitop (bit_and bit_ior)
1526 rbitop (bit_ior bit_and)
1527 /* (x | y) & x -> x */
1528 /* (x & y) | x -> x */
1530 (bitop:c (rbitop:c @0 @1) @0)
1532 /* (~x | y) & x -> x & y */
1533 /* (~x & y) | x -> x | y */
1535 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1538 /* ((x | y) & z) | x -> (z & y) | x */
1540 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1541 (bit_ior (bit_and @2 @1) @0))
1543 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1545 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1546 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1548 /* Combine successive equal operations with constants. */
1549 (for bitop (bit_and bit_ior bit_xor)
1551 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1552 (if (!CONSTANT_CLASS_P (@0))
1553 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1554 folded to a constant. */
1555 (bitop @0 (bitop @1 @2))
1556 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1557 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1558 the values involved are such that the operation can't be decided at
1559 compile time. Try folding one of @0 or @1 with @2 to see whether
1560 that combination can be decided at compile time.
1562 Keep the existing form if both folds fail, to avoid endless
1564 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1566 (bitop @1 { cst1; })
1567 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1569 (bitop @0 { cst2; }))))))))
1571 /* Try simple folding for X op !X, and X op X with the help
1572 of the truth_valued_p and logical_inverted_value predicates. */
1573 (match truth_valued_p
1575 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1576 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1577 (match truth_valued_p
1579 (match truth_valued_p
1582 (match (logical_inverted_value @0)
1584 (match (logical_inverted_value @0)
1585 (bit_not truth_valued_p@0))
1586 (match (logical_inverted_value @0)
1587 (eq @0 integer_zerop))
1588 (match (logical_inverted_value @0)
1589 (ne truth_valued_p@0 integer_truep))
1590 (match (logical_inverted_value @0)
1591 (bit_xor truth_valued_p@0 integer_truep))
1595 (bit_and:c @0 (logical_inverted_value @0))
1596 { build_zero_cst (type); })
1597 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1598 (for op (bit_ior bit_xor)
1600 (op:c truth_valued_p@0 (logical_inverted_value @0))
1601 { constant_boolean_node (true, type); }))
1602 /* X ==/!= !X is false/true. */
1605 (op:c truth_valued_p@0 (logical_inverted_value @0))
1606 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1610 (bit_not (bit_not @0))
1613 /* Convert ~ (-A) to A - 1. */
1615 (bit_not (convert? (negate @0)))
1616 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1617 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1618 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1620 /* Convert - (~A) to A + 1. */
1622 (negate (nop_convert? (bit_not @0)))
1623 (plus (view_convert @0) { build_each_one_cst (type); }))
1625 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1627 (bit_not (convert? (minus @0 integer_each_onep)))
1628 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1629 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1630 (convert (negate @0))))
1632 (bit_not (convert? (plus @0 integer_all_onesp)))
1633 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1634 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1635 (convert (negate @0))))
1637 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1639 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1640 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1641 (convert (bit_xor @0 (bit_not @1)))))
1643 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1644 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1645 (convert (bit_xor @0 @1))))
1647 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1649 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1650 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1651 (bit_not (bit_xor (view_convert @0) @1))))
1653 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1655 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1656 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1658 /* Fold A - (A & B) into ~B & A. */
1660 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1661 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1662 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1663 (convert (bit_and (bit_not @1) @0))))
1665 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1666 (for cmp (gt lt ge le)
1668 (mult (convert (cmp @0 @1)) @2)
1669 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1670 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1672 /* For integral types with undefined overflow and C != 0 fold
1673 x * C EQ/NE y * C into x EQ/NE y. */
1676 (cmp (mult:c @0 @1) (mult:c @2 @1))
1677 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1678 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1679 && tree_expr_nonzero_p (@1))
1682 /* For integral types with wrapping overflow and C odd fold
1683 x * C EQ/NE y * C into x EQ/NE y. */
1686 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1687 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1688 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1689 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1692 /* For integral types with undefined overflow and C != 0 fold
1693 x * C RELOP y * C into:
1695 x RELOP y for nonnegative C
1696 y RELOP x for negative C */
1697 (for cmp (lt gt le ge)
1699 (cmp (mult:c @0 @1) (mult:c @2 @1))
1700 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1701 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1702 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1704 (if (TREE_CODE (@1) == INTEGER_CST
1705 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1708 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1712 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1713 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1714 && TYPE_UNSIGNED (TREE_TYPE (@0))
1715 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1716 && (wi::to_wide (@2)
1717 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1718 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1719 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1721 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1722 (for cmp (simple_comparison)
1724 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1725 (if (element_precision (@3) >= element_precision (@0)
1726 && types_match (@0, @1))
1727 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1728 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1730 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1733 tree utype = unsigned_type_for (TREE_TYPE (@0));
1735 (cmp (convert:utype @1) (convert:utype @0)))))
1736 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1737 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1741 tree utype = unsigned_type_for (TREE_TYPE (@0));
1743 (cmp (convert:utype @0) (convert:utype @1)))))))))
1745 /* X / C1 op C2 into a simple range test. */
1746 (for cmp (simple_comparison)
1748 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1749 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1750 && integer_nonzerop (@1)
1751 && !TREE_OVERFLOW (@1)
1752 && !TREE_OVERFLOW (@2))
1753 (with { tree lo, hi; bool neg_overflow;
1754 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1757 (if (code == LT_EXPR || code == GE_EXPR)
1758 (if (TREE_OVERFLOW (lo))
1759 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1760 (if (code == LT_EXPR)
1763 (if (code == LE_EXPR || code == GT_EXPR)
1764 (if (TREE_OVERFLOW (hi))
1765 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1766 (if (code == LE_EXPR)
1770 { build_int_cst (type, code == NE_EXPR); })
1771 (if (code == EQ_EXPR && !hi)
1773 (if (code == EQ_EXPR && !lo)
1775 (if (code == NE_EXPR && !hi)
1777 (if (code == NE_EXPR && !lo)
1780 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1784 tree etype = range_check_type (TREE_TYPE (@0));
1787 hi = fold_convert (etype, hi);
1788 lo = fold_convert (etype, lo);
1789 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1792 (if (etype && hi && !TREE_OVERFLOW (hi))
1793 (if (code == EQ_EXPR)
1794 (le (minus (convert:etype @0) { lo; }) { hi; })
1795 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1797 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1798 (for op (lt le ge gt)
1800 (op (plus:c @0 @2) (plus:c @1 @2))
1801 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1802 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1804 /* For equality and subtraction, this is also true with wrapping overflow. */
1805 (for op (eq ne minus)
1807 (op (plus:c @0 @2) (plus:c @1 @2))
1808 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1809 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1810 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1813 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1814 (for op (lt le ge gt)
1816 (op (minus @0 @2) (minus @1 @2))
1817 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1818 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1820 /* For equality and subtraction, this is also true with wrapping overflow. */
1821 (for op (eq ne minus)
1823 (op (minus @0 @2) (minus @1 @2))
1824 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1825 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1826 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1828 /* And for pointers... */
1829 (for op (simple_comparison)
1831 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1832 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1835 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1836 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1837 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1838 (pointer_diff @0 @1)))
1840 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1841 (for op (lt le ge gt)
1843 (op (minus @2 @0) (minus @2 @1))
1844 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1845 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1847 /* For equality and subtraction, this is also true with wrapping overflow. */
1848 (for op (eq ne minus)
1850 (op (minus @2 @0) (minus @2 @1))
1851 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1852 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1853 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1855 /* And for pointers... */
1856 (for op (simple_comparison)
1858 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1859 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1862 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1863 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1864 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1865 (pointer_diff @1 @0)))
1867 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1868 (for op (lt le gt ge)
1870 (op:c (plus:c@2 @0 @1) @1)
1871 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1872 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1873 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1874 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1875 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1876 /* For equality, this is also true with wrapping overflow. */
1879 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1880 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1881 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1882 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1883 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1884 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1885 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1886 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1888 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1889 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1890 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1891 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1892 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1894 /* X - Y < X is the same as Y > 0 when there is no overflow.
1895 For equality, this is also true with wrapping overflow. */
1896 (for op (simple_comparison)
1898 (op:c @0 (minus@2 @0 @1))
1899 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1900 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1901 || ((op == EQ_EXPR || op == NE_EXPR)
1902 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1903 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1904 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1907 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1908 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1912 (cmp (trunc_div @0 @1) integer_zerop)
1913 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1914 /* Complex ==/!= is allowed, but not </>=. */
1915 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1916 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1919 /* X == C - X can never be true if C is odd. */
1922 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1923 (if (TREE_INT_CST_LOW (@1) & 1)
1924 { constant_boolean_node (cmp == NE_EXPR, type); })))
1926 /* Arguments on which one can call get_nonzero_bits to get the bits
1928 (match with_possible_nonzero_bits
1930 (match with_possible_nonzero_bits
1932 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1933 /* Slightly extended version, do not make it recursive to keep it cheap. */
1934 (match (with_possible_nonzero_bits2 @0)
1935 with_possible_nonzero_bits@0)
1936 (match (with_possible_nonzero_bits2 @0)
1937 (bit_and:c with_possible_nonzero_bits@0 @2))
1939 /* Same for bits that are known to be set, but we do not have
1940 an equivalent to get_nonzero_bits yet. */
1941 (match (with_certain_nonzero_bits2 @0)
1943 (match (with_certain_nonzero_bits2 @0)
1944 (bit_ior @1 INTEGER_CST@0))
1946 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1949 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1950 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1951 { constant_boolean_node (cmp == NE_EXPR, type); })))
1953 /* ((X inner_op C0) outer_op C1)
1954 With X being a tree where value_range has reasoned certain bits to always be
1955 zero throughout its computed value range,
1956 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1957 where zero_mask has 1's for all bits that are sure to be 0 in
1959 if (inner_op == '^') C0 &= ~C1;
1960 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1961 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1963 (for inner_op (bit_ior bit_xor)
1964 outer_op (bit_xor bit_ior)
1967 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1971 wide_int zero_mask_not;
1975 if (TREE_CODE (@2) == SSA_NAME)
1976 zero_mask_not = get_nonzero_bits (@2);
1980 if (inner_op == BIT_XOR_EXPR)
1982 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1983 cst_emit = C0 | wi::to_wide (@1);
1987 C0 = wi::to_wide (@0);
1988 cst_emit = C0 ^ wi::to_wide (@1);
1991 (if (!fail && (C0 & zero_mask_not) == 0)
1992 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1993 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1994 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1996 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1998 (pointer_plus (pointer_plus:s @0 @1) @3)
1999 (pointer_plus @0 (plus @1 @3)))
2005 tem4 = (unsigned long) tem3;
2010 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2011 /* Conditionally look through a sign-changing conversion. */
2012 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2013 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2014 || (GENERIC && type == TREE_TYPE (@1))))
2017 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2018 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2022 tem = (sizetype) ptr;
2026 and produce the simpler and easier to analyze with respect to alignment
2027 ... = ptr & ~algn; */
2029 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2030 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2031 (bit_and @0 { algn; })))
2033 /* Try folding difference of addresses. */
2035 (minus (convert ADDR_EXPR@0) (convert @1))
2036 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2037 (with { poly_int64 diff; }
2038 (if (ptr_difference_const (@0, @1, &diff))
2039 { build_int_cst_type (type, diff); }))))
2041 (minus (convert @0) (convert ADDR_EXPR@1))
2042 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2043 (with { poly_int64 diff; }
2044 (if (ptr_difference_const (@0, @1, &diff))
2045 { build_int_cst_type (type, diff); }))))
2047 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2048 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2049 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2050 (with { poly_int64 diff; }
2051 (if (ptr_difference_const (@0, @1, &diff))
2052 { build_int_cst_type (type, diff); }))))
2054 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2055 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2056 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2057 (with { poly_int64 diff; }
2058 (if (ptr_difference_const (@0, @1, &diff))
2059 { build_int_cst_type (type, diff); }))))
2061 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2063 (convert (pointer_diff @0 INTEGER_CST@1))
2064 (if (POINTER_TYPE_P (type))
2065 { build_fold_addr_expr_with_type
2066 (build2 (MEM_REF, char_type_node, @0,
2067 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2070 /* If arg0 is derived from the address of an object or function, we may
2071 be able to fold this expression using the object or function's
2074 (bit_and (convert? @0) INTEGER_CST@1)
2075 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2076 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2080 unsigned HOST_WIDE_INT bitpos;
2081 get_pointer_alignment_1 (@0, &align, &bitpos);
2083 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2084 { wide_int_to_tree (type, (wi::to_wide (@1)
2085 & (bitpos / BITS_PER_UNIT))); }))))
2089 (if (INTEGRAL_TYPE_P (type)
2090 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2094 (if (INTEGRAL_TYPE_P (type)
2095 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2097 /* x > y && x != XXX_MIN --> x > y
2098 x > y && x == XXX_MIN --> false . */
2101 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2103 (if (eqne == EQ_EXPR)
2104 { constant_boolean_node (false, type); })
2105 (if (eqne == NE_EXPR)
2109 /* x < y && x != XXX_MAX --> x < y
2110 x < y && x == XXX_MAX --> false. */
2113 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2115 (if (eqne == EQ_EXPR)
2116 { constant_boolean_node (false, type); })
2117 (if (eqne == NE_EXPR)
2121 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2123 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2126 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2128 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2131 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2133 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2136 /* x <= y || x != XXX_MIN --> true. */
2138 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2139 { constant_boolean_node (true, type); })
2141 /* x <= y || x == XXX_MIN --> x <= y. */
2143 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2146 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2148 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2151 /* x >= y || x != XXX_MAX --> true
2152 x >= y || x == XXX_MAX --> x >= y. */
2155 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2157 (if (eqne == EQ_EXPR)
2159 (if (eqne == NE_EXPR)
2160 { constant_boolean_node (true, type); }))))
2162 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2163 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2166 (for code2 (eq ne lt gt le ge)
2168 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2171 int cmp = tree_int_cst_compare (@1, @2);
2175 case EQ_EXPR: val = (cmp == 0); break;
2176 case NE_EXPR: val = (cmp != 0); break;
2177 case LT_EXPR: val = (cmp < 0); break;
2178 case GT_EXPR: val = (cmp > 0); break;
2179 case LE_EXPR: val = (cmp <= 0); break;
2180 case GE_EXPR: val = (cmp >= 0); break;
2181 default: gcc_unreachable ();
2185 (if (code1 == EQ_EXPR && val) @3)
2186 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2187 (if (code1 == NE_EXPR && !val) @4))))))
2189 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2191 (for code1 (lt le gt ge)
2192 (for code2 (lt le gt ge)
2194 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2197 int cmp = tree_int_cst_compare (@1, @2);
2200 /* Choose the more restrictive of two < or <= comparisons. */
2201 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2202 && (code2 == LT_EXPR || code2 == LE_EXPR))
2203 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2206 /* Likewise chose the more restrictive of two > or >= comparisons. */
2207 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2208 && (code2 == GT_EXPR || code2 == GE_EXPR))
2209 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2212 /* Check for singleton ranges. */
2214 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2215 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2217 /* Check for disjoint ranges. */
2219 && (code1 == LT_EXPR || code1 == LE_EXPR)
2220 && (code2 == GT_EXPR || code2 == GE_EXPR))
2221 { constant_boolean_node (false, type); })
2223 && (code1 == GT_EXPR || code1 == GE_EXPR)
2224 && (code2 == LT_EXPR || code2 == LE_EXPR))
2225 { constant_boolean_node (false, type); })
2228 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2229 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2232 (for code2 (eq ne lt gt le ge)
2234 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2237 int cmp = tree_int_cst_compare (@1, @2);
2241 case EQ_EXPR: val = (cmp == 0); break;
2242 case NE_EXPR: val = (cmp != 0); break;
2243 case LT_EXPR: val = (cmp < 0); break;
2244 case GT_EXPR: val = (cmp > 0); break;
2245 case LE_EXPR: val = (cmp <= 0); break;
2246 case GE_EXPR: val = (cmp >= 0); break;
2247 default: gcc_unreachable ();
2251 (if (code1 == EQ_EXPR && val) @4)
2252 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2253 (if (code1 == NE_EXPR && !val) @3))))))
2255 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2257 (for code1 (lt le gt ge)
2258 (for code2 (lt le gt ge)
2260 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2263 int cmp = tree_int_cst_compare (@1, @2);
2266 /* Choose the more restrictive of two < or <= comparisons. */
2267 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2268 && (code2 == LT_EXPR || code2 == LE_EXPR))
2269 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2272 /* Likewise chose the more restrictive of two > or >= comparisons. */
2273 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2274 && (code2 == GT_EXPR || code2 == GE_EXPR))
2275 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2278 /* Check for singleton ranges. */
2280 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2281 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2283 /* Check for disjoint ranges. */
2285 && (code1 == LT_EXPR || code1 == LE_EXPR)
2286 && (code2 == GT_EXPR || code2 == GE_EXPR))
2287 { constant_boolean_node (true, type); })
2289 && (code1 == GT_EXPR || code1 == GE_EXPR)
2290 && (code2 == LT_EXPR || code2 == LE_EXPR))
2291 { constant_boolean_node (true, type); })
2294 /* We can't reassociate at all for saturating types. */
2295 (if (!TYPE_SATURATING (type))
2297 /* Contract negates. */
2298 /* A + (-B) -> A - B */
2300 (plus:c @0 (convert? (negate @1)))
2301 /* Apply STRIP_NOPS on the negate. */
2302 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2303 && !TYPE_OVERFLOW_SANITIZED (type))
2307 if (INTEGRAL_TYPE_P (type)
2308 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2309 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2311 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2312 /* A - (-B) -> A + B */
2314 (minus @0 (convert? (negate @1)))
2315 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2316 && !TYPE_OVERFLOW_SANITIZED (type))
2320 if (INTEGRAL_TYPE_P (type)
2321 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2322 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2324 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2326 Sign-extension is ok except for INT_MIN, which thankfully cannot
2327 happen without overflow. */
2329 (negate (convert (negate @1)))
2330 (if (INTEGRAL_TYPE_P (type)
2331 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2332 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2333 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2334 && !TYPE_OVERFLOW_SANITIZED (type)
2335 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2338 (negate (convert negate_expr_p@1))
2339 (if (SCALAR_FLOAT_TYPE_P (type)
2340 && ((DECIMAL_FLOAT_TYPE_P (type)
2341 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2342 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2343 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2344 (convert (negate @1))))
2346 (negate (nop_convert? (negate @1)))
2347 (if (!TYPE_OVERFLOW_SANITIZED (type)
2348 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2351 /* We can't reassociate floating-point unless -fassociative-math
2352 or fixed-point plus or minus because of saturation to +-Inf. */
2353 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2354 && !FIXED_POINT_TYPE_P (type))
2356 /* Match patterns that allow contracting a plus-minus pair
2357 irrespective of overflow issues. */
2358 /* (A +- B) - A -> +- B */
2359 /* (A +- B) -+ B -> A */
2360 /* A - (A +- B) -> -+ B */
2361 /* A +- (B -+ A) -> +- B */
2363 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2366 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2367 (if (!ANY_INTEGRAL_TYPE_P (type)
2368 || TYPE_OVERFLOW_WRAPS (type))
2369 (negate (view_convert @1))
2370 (view_convert (negate @1))))
2372 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2375 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2376 (if (!ANY_INTEGRAL_TYPE_P (type)
2377 || TYPE_OVERFLOW_WRAPS (type))
2378 (negate (view_convert @1))
2379 (view_convert (negate @1))))
2381 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2383 /* (A +- B) + (C - A) -> C +- B */
2384 /* (A + B) - (A - C) -> B + C */
2385 /* More cases are handled with comparisons. */
2387 (plus:c (plus:c @0 @1) (minus @2 @0))
2390 (plus:c (minus @0 @1) (minus @2 @0))
2393 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2394 (if (TYPE_OVERFLOW_UNDEFINED (type)
2395 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2396 (pointer_diff @2 @1)))
2398 (minus (plus:c @0 @1) (minus @0 @2))
2401 /* (A +- CST1) +- CST2 -> A + CST3
2402 Use view_convert because it is safe for vectors and equivalent for
2404 (for outer_op (plus minus)
2405 (for inner_op (plus minus)
2406 neg_inner_op (minus plus)
2408 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2410 /* If one of the types wraps, use that one. */
2411 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2412 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2413 forever if something doesn't simplify into a constant. */
2414 (if (!CONSTANT_CLASS_P (@0))
2415 (if (outer_op == PLUS_EXPR)
2416 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2417 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2418 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2419 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2420 (if (outer_op == PLUS_EXPR)
2421 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2422 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2423 /* If the constant operation overflows we cannot do the transform
2424 directly as we would introduce undefined overflow, for example
2425 with (a - 1) + INT_MIN. */
2426 (if (types_match (type, @0))
2427 (with { tree cst = const_binop (outer_op == inner_op
2428 ? PLUS_EXPR : MINUS_EXPR,
2430 (if (cst && !TREE_OVERFLOW (cst))
2431 (inner_op @0 { cst; } )
2432 /* X+INT_MAX+1 is X-INT_MIN. */
2433 (if (INTEGRAL_TYPE_P (type) && cst
2434 && wi::to_wide (cst) == wi::min_value (type))
2435 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2436 /* Last resort, use some unsigned type. */
2437 (with { tree utype = unsigned_type_for (type); }
2439 (view_convert (inner_op
2440 (view_convert:utype @0)
2442 { drop_tree_overflow (cst); }))))))))))))))
2444 /* (CST1 - A) +- CST2 -> CST3 - A */
2445 (for outer_op (plus minus)
2447 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2448 /* If one of the types wraps, use that one. */
2449 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2450 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2451 forever if something doesn't simplify into a constant. */
2452 (if (!CONSTANT_CLASS_P (@0))
2453 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2454 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2455 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2456 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2457 (if (types_match (type, @0))
2458 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2459 (if (cst && !TREE_OVERFLOW (cst))
2460 (minus { cst; } @0))))))))
2462 /* CST1 - (CST2 - A) -> CST3 + A
2463 Use view_convert because it is safe for vectors and equivalent for
2466 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2467 /* If one of the types wraps, use that one. */
2468 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2469 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2470 forever if something doesn't simplify into a constant. */
2471 (if (!CONSTANT_CLASS_P (@0))
2472 (plus (view_convert @0) (minus @1 (view_convert @2))))
2473 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2474 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2475 (view_convert (plus @0 (minus (view_convert @1) @2)))
2476 (if (types_match (type, @0))
2477 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2478 (if (cst && !TREE_OVERFLOW (cst))
2479 (plus { cst; } @0)))))))
2481 /* ((T)(A)) + CST -> (T)(A + CST) */
2484 (plus (convert SSA_NAME@0) INTEGER_CST@1)
2485 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2486 && TREE_CODE (type) == INTEGER_TYPE
2487 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2488 && int_fits_type_p (@1, TREE_TYPE (@0)))
2489 /* Perform binary operation inside the cast if the constant fits
2490 and (A + CST)'s range does not overflow. */
2493 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2494 max_ovf = wi::OVF_OVERFLOW;
2495 tree inner_type = TREE_TYPE (@0);
2498 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2499 TYPE_SIGN (inner_type));
2501 wide_int wmin0, wmax0;
2502 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2504 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2505 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2508 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2509 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2513 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2515 (for op (plus minus)
2517 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2518 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2519 && TREE_CODE (type) == INTEGER_TYPE
2520 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2521 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2522 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2523 && TYPE_OVERFLOW_WRAPS (type))
2524 (plus (convert @0) (op @2 (convert @1))))))
2527 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2528 to a simple value. */
2530 (for op (plus minus)
2532 (op (convert @0) (convert @1))
2533 (if (INTEGRAL_TYPE_P (type)
2534 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2535 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2536 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2537 && !TYPE_OVERFLOW_TRAPS (type)
2538 && !TYPE_OVERFLOW_SANITIZED (type))
2539 (convert (op! @0 @1)))))
2544 (plus:c (bit_not @0) @0)
2545 (if (!TYPE_OVERFLOW_TRAPS (type))
2546 { build_all_ones_cst (type); }))
2550 (plus (convert? (bit_not @0)) integer_each_onep)
2551 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2552 (negate (convert @0))))
2556 (minus (convert? (negate @0)) integer_each_onep)
2557 (if (!TYPE_OVERFLOW_TRAPS (type)
2558 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2559 (bit_not (convert @0))))
2563 (minus integer_all_onesp @0)
2566 /* (T)(P + A) - (T)P -> (T) A */
2568 (minus (convert (plus:c @@0 @1))
2570 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2571 /* For integer types, if A has a smaller type
2572 than T the result depends on the possible
2574 E.g. T=size_t, A=(unsigned)429497295, P>0.
2575 However, if an overflow in P + A would cause
2576 undefined behavior, we can assume that there
2578 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2579 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2582 (minus (convert (pointer_plus @@0 @1))
2584 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2585 /* For pointer types, if the conversion of A to the
2586 final type requires a sign- or zero-extension,
2587 then we have to punt - it is not defined which
2589 || (POINTER_TYPE_P (TREE_TYPE (@0))
2590 && TREE_CODE (@1) == INTEGER_CST
2591 && tree_int_cst_sign_bit (@1) == 0))
2594 (pointer_diff (pointer_plus @@0 @1) @0)
2595 /* The second argument of pointer_plus must be interpreted as signed, and
2596 thus sign-extended if necessary. */
2597 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2598 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2599 second arg is unsigned even when we need to consider it as signed,
2600 we don't want to diagnose overflow here. */
2601 (convert (view_convert:stype @1))))
2603 /* (T)P - (T)(P + A) -> -(T) A */
2605 (minus (convert? @0)
2606 (convert (plus:c @@0 @1)))
2607 (if (INTEGRAL_TYPE_P (type)
2608 && TYPE_OVERFLOW_UNDEFINED (type)
2609 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2610 (with { tree utype = unsigned_type_for (type); }
2611 (convert (negate (convert:utype @1))))
2612 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2613 /* For integer types, if A has a smaller type
2614 than T the result depends on the possible
2616 E.g. T=size_t, A=(unsigned)429497295, P>0.
2617 However, if an overflow in P + A would cause
2618 undefined behavior, we can assume that there
2620 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2621 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2622 (negate (convert @1)))))
2625 (convert (pointer_plus @@0 @1)))
2626 (if (INTEGRAL_TYPE_P (type)
2627 && TYPE_OVERFLOW_UNDEFINED (type)
2628 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2629 (with { tree utype = unsigned_type_for (type); }
2630 (convert (negate (convert:utype @1))))
2631 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2632 /* For pointer types, if the conversion of A to the
2633 final type requires a sign- or zero-extension,
2634 then we have to punt - it is not defined which
2636 || (POINTER_TYPE_P (TREE_TYPE (@0))
2637 && TREE_CODE (@1) == INTEGER_CST
2638 && tree_int_cst_sign_bit (@1) == 0))
2639 (negate (convert @1)))))
2641 (pointer_diff @0 (pointer_plus @@0 @1))
2642 /* The second argument of pointer_plus must be interpreted as signed, and
2643 thus sign-extended if necessary. */
2644 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2645 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2646 second arg is unsigned even when we need to consider it as signed,
2647 we don't want to diagnose overflow here. */
2648 (negate (convert (view_convert:stype @1)))))
2650 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2652 (minus (convert (plus:c @@0 @1))
2653 (convert (plus:c @0 @2)))
2654 (if (INTEGRAL_TYPE_P (type)
2655 && TYPE_OVERFLOW_UNDEFINED (type)
2656 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2657 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2658 (with { tree utype = unsigned_type_for (type); }
2659 (convert (minus (convert:utype @1) (convert:utype @2))))
2660 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2661 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2662 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2663 /* For integer types, if A has a smaller type
2664 than T the result depends on the possible
2666 E.g. T=size_t, A=(unsigned)429497295, P>0.
2667 However, if an overflow in P + A would cause
2668 undefined behavior, we can assume that there
2670 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2671 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2672 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2673 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2674 (minus (convert @1) (convert @2)))))
2676 (minus (convert (pointer_plus @@0 @1))
2677 (convert (pointer_plus @0 @2)))
2678 (if (INTEGRAL_TYPE_P (type)
2679 && TYPE_OVERFLOW_UNDEFINED (type)
2680 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2681 (with { tree utype = unsigned_type_for (type); }
2682 (convert (minus (convert:utype @1) (convert:utype @2))))
2683 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2684 /* For pointer types, if the conversion of A to the
2685 final type requires a sign- or zero-extension,
2686 then we have to punt - it is not defined which
2688 || (POINTER_TYPE_P (TREE_TYPE (@0))
2689 && TREE_CODE (@1) == INTEGER_CST
2690 && tree_int_cst_sign_bit (@1) == 0
2691 && TREE_CODE (@2) == INTEGER_CST
2692 && tree_int_cst_sign_bit (@2) == 0))
2693 (minus (convert @1) (convert @2)))))
2695 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2696 (pointer_diff @0 @1))
2698 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2699 /* The second argument of pointer_plus must be interpreted as signed, and
2700 thus sign-extended if necessary. */
2701 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2702 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2703 second arg is unsigned even when we need to consider it as signed,
2704 we don't want to diagnose overflow here. */
2705 (minus (convert (view_convert:stype @1))
2706 (convert (view_convert:stype @2)))))))
2708 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2709 Modeled after fold_plusminus_mult_expr. */
2710 (if (!TYPE_SATURATING (type)
2711 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2712 (for plusminus (plus minus)
2714 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2715 (if (!ANY_INTEGRAL_TYPE_P (type)
2716 || TYPE_OVERFLOW_WRAPS (type)
2717 || (INTEGRAL_TYPE_P (type)
2718 && tree_expr_nonzero_p (@0)
2719 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2720 (if (single_use (@3) || single_use (@4))
2721 /* If @1 +- @2 is constant require a hard single-use on either
2722 original operand (but not on both). */
2723 (mult (plusminus @1 @2) @0)
2725 (mult! (plusminus @1 @2) @0)
2728 /* We cannot generate constant 1 for fract. */
2729 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2731 (plusminus @0 (mult:c@3 @0 @2))
2732 (if ((!ANY_INTEGRAL_TYPE_P (type)
2733 || TYPE_OVERFLOW_WRAPS (type)
2734 /* For @0 + @0*@2 this transformation would introduce UB
2735 (where there was none before) for @0 in [-1,0] and @2 max.
2736 For @0 - @0*@2 this transformation would introduce UB
2737 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2738 || (INTEGRAL_TYPE_P (type)
2739 && ((tree_expr_nonzero_p (@0)
2740 && expr_not_equal_to (@0,
2741 wi::minus_one (TYPE_PRECISION (type))))
2742 || (plusminus == PLUS_EXPR
2743 ? expr_not_equal_to (@2,
2744 wi::max_value (TYPE_PRECISION (type), SIGNED))
2745 /* Let's ignore the @0 -1 and @2 min case. */
2746 : (expr_not_equal_to (@2,
2747 wi::min_value (TYPE_PRECISION (type), SIGNED))
2748 && expr_not_equal_to (@2,
2749 wi::min_value (TYPE_PRECISION (type), SIGNED)
2752 (mult (plusminus { build_one_cst (type); } @2) @0)))
2754 (plusminus (mult:c@3 @0 @2) @0)
2755 (if ((!ANY_INTEGRAL_TYPE_P (type)
2756 || TYPE_OVERFLOW_WRAPS (type)
2757 /* For @0*@2 + @0 this transformation would introduce UB
2758 (where there was none before) for @0 in [-1,0] and @2 max.
2759 For @0*@2 - @0 this transformation would introduce UB
2760 for @0 0 and @2 min. */
2761 || (INTEGRAL_TYPE_P (type)
2762 && ((tree_expr_nonzero_p (@0)
2763 && (plusminus == MINUS_EXPR
2764 || expr_not_equal_to (@0,
2765 wi::minus_one (TYPE_PRECISION (type)))))
2766 || expr_not_equal_to (@2,
2767 (plusminus == PLUS_EXPR
2768 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2769 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2771 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2774 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2775 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2777 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2778 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2779 && tree_fits_uhwi_p (@1)
2780 && tree_to_uhwi (@1) < element_precision (type))
2781 (with { tree t = type;
2782 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2783 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2784 element_precision (type));
2786 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2788 cst = build_uniform_cst (t, cst); }
2789 (convert (mult (convert:t @0) { cst; })))))
2791 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2792 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2793 && tree_fits_uhwi_p (@1)
2794 && tree_to_uhwi (@1) < element_precision (type)
2795 && tree_fits_uhwi_p (@2)
2796 && tree_to_uhwi (@2) < element_precision (type))
2797 (with { tree t = type;
2798 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2799 unsigned int prec = element_precision (type);
2800 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2801 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2802 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2804 cst = build_uniform_cst (t, cst); }
2805 (convert (mult (convert:t @0) { cst; })))))
2808 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2810 (for minmax (min max FMIN_ALL FMAX_ALL)
2814 /* min(max(x,y),y) -> y. */
2816 (min:c (max:c @0 @1) @1)
2818 /* max(min(x,y),y) -> y. */
2820 (max:c (min:c @0 @1) @1)
2822 /* max(a,-a) -> abs(a). */
2824 (max:c @0 (negate @0))
2825 (if (TREE_CODE (type) != COMPLEX_TYPE
2826 && (! ANY_INTEGRAL_TYPE_P (type)
2827 || TYPE_OVERFLOW_UNDEFINED (type)))
2829 /* min(a,-a) -> -abs(a). */
2831 (min:c @0 (negate @0))
2832 (if (TREE_CODE (type) != COMPLEX_TYPE
2833 && (! ANY_INTEGRAL_TYPE_P (type)
2834 || TYPE_OVERFLOW_UNDEFINED (type)))
2839 (if (INTEGRAL_TYPE_P (type)
2840 && TYPE_MIN_VALUE (type)
2841 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2843 (if (INTEGRAL_TYPE_P (type)
2844 && TYPE_MAX_VALUE (type)
2845 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2850 (if (INTEGRAL_TYPE_P (type)
2851 && TYPE_MAX_VALUE (type)
2852 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2854 (if (INTEGRAL_TYPE_P (type)
2855 && TYPE_MIN_VALUE (type)
2856 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2859 /* max (a, a + CST) -> a + CST where CST is positive. */
2860 /* max (a, a + CST) -> a where CST is negative. */
2862 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2863 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2864 (if (tree_int_cst_sgn (@1) > 0)
2868 /* min (a, a + CST) -> a where CST is positive. */
2869 /* min (a, a + CST) -> a + CST where CST is negative. */
2871 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2872 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2873 (if (tree_int_cst_sgn (@1) > 0)
2877 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2878 and the outer convert demotes the expression back to x's type. */
2879 (for minmax (min max)
2881 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2882 (if (INTEGRAL_TYPE_P (type)
2883 && types_match (@1, type) && int_fits_type_p (@2, type)
2884 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2885 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2886 (minmax @1 (convert @2)))))
2888 (for minmax (FMIN_ALL FMAX_ALL)
2889 /* If either argument is NaN, return the other one. Avoid the
2890 transformation if we get (and honor) a signalling NaN. */
2892 (minmax:c @0 REAL_CST@1)
2893 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2894 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2896 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2897 functions to return the numeric arg if the other one is NaN.
2898 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2899 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2900 worry about it either. */
2901 (if (flag_finite_math_only)
2908 /* min (-A, -B) -> -max (A, B) */
2909 (for minmax (min max FMIN_ALL FMAX_ALL)
2910 maxmin (max min FMAX_ALL FMIN_ALL)
2912 (minmax (negate:s@2 @0) (negate:s@3 @1))
2913 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2914 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2915 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2916 (negate (maxmin @0 @1)))))
2917 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2918 MAX (~X, ~Y) -> ~MIN (X, Y) */
2919 (for minmax (min max)
2922 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2923 (bit_not (maxmin @0 @1))))
2925 /* MIN (X, Y) == X -> X <= Y */
2926 (for minmax (min min max max)
2930 (cmp:c (minmax:c @0 @1) @0)
2931 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2933 /* MIN (X, 5) == 0 -> X == 0
2934 MIN (X, 5) == 7 -> false */
2937 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2938 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2939 TYPE_SIGN (TREE_TYPE (@0))))
2940 { constant_boolean_node (cmp == NE_EXPR, type); }
2941 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2942 TYPE_SIGN (TREE_TYPE (@0))))
2946 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2947 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2948 TYPE_SIGN (TREE_TYPE (@0))))
2949 { constant_boolean_node (cmp == NE_EXPR, type); }
2950 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2951 TYPE_SIGN (TREE_TYPE (@0))))
2953 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2954 (for minmax (min min max max min min max max )
2955 cmp (lt le gt ge gt ge lt le )
2956 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2958 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2959 (comb (cmp @0 @2) (cmp @1 @2))))
2961 /* X <= MAX(X, Y) -> true
2962 X > MAX(X, Y) -> false
2963 X >= MIN(X, Y) -> true
2964 X < MIN(X, Y) -> false */
2965 (for minmax (min min max max )
2968 (cmp @0 (minmax:c @0 @1))
2969 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
2971 /* Undo fancy way of writing max/min or other ?: expressions,
2972 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2973 People normally use ?: and that is what we actually try to optimize. */
2974 (for cmp (simple_comparison)
2976 (minus @0 (bit_and:c (minus @0 @1)
2977 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2978 (if (INTEGRAL_TYPE_P (type)
2979 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2980 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2981 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2982 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2983 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2984 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2985 (cond (cmp @2 @3) @1 @0)))
2987 (plus:c @0 (bit_and:c (minus @1 @0)
2988 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2989 (if (INTEGRAL_TYPE_P (type)
2990 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2991 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2992 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2993 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2994 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2995 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2996 (cond (cmp @2 @3) @1 @0)))
2997 /* Similarly with ^ instead of - though in that case with :c. */
2999 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3000 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3001 (if (INTEGRAL_TYPE_P (type)
3002 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3003 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3004 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3005 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3006 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3007 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3008 (cond (cmp @2 @3) @1 @0))))
3010 /* Simplifications of shift and rotates. */
3012 (for rotate (lrotate rrotate)
3014 (rotate integer_all_onesp@0 @1)
3017 /* Optimize -1 >> x for arithmetic right shifts. */
3019 (rshift integer_all_onesp@0 @1)
3020 (if (!TYPE_UNSIGNED (type))
3023 /* Optimize (x >> c) << c into x & (-1<<c). */
3025 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3026 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3027 /* It doesn't matter if the right shift is arithmetic or logical. */
3028 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3031 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3032 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3033 /* Allow intermediate conversion to integral type with whatever sign, as
3034 long as the low TYPE_PRECISION (type)
3035 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3036 && INTEGRAL_TYPE_P (type)
3037 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3038 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3039 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3040 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3041 || wi::geu_p (wi::to_wide (@1),
3042 TYPE_PRECISION (type)
3043 - TYPE_PRECISION (TREE_TYPE (@2)))))
3044 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3046 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3049 (rshift (lshift @0 INTEGER_CST@1) @1)
3050 (if (TYPE_UNSIGNED (type)
3051 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3052 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3054 /* Optimize x >> x into 0 */
3057 { build_zero_cst (type); })
3059 (for shiftrotate (lrotate rrotate lshift rshift)
3061 (shiftrotate @0 integer_zerop)
3064 (shiftrotate integer_zerop@0 @1)
3066 /* Prefer vector1 << scalar to vector1 << vector2
3067 if vector2 is uniform. */
3068 (for vec (VECTOR_CST CONSTRUCTOR)
3070 (shiftrotate @0 vec@1)
3071 (with { tree tem = uniform_vector_p (@1); }
3073 (shiftrotate @0 { tem; }))))))
3075 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3076 Y is 0. Similarly for X >> Y. */
3078 (for shift (lshift rshift)
3080 (shift @0 SSA_NAME@1)
3081 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3083 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3084 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3086 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3090 /* Rewrite an LROTATE_EXPR by a constant into an
3091 RROTATE_EXPR by a new constant. */
3093 (lrotate @0 INTEGER_CST@1)
3094 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3095 build_int_cst (TREE_TYPE (@1),
3096 element_precision (type)), @1); }))
3098 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3099 (for op (lrotate rrotate rshift lshift)
3101 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3102 (with { unsigned int prec = element_precision (type); }
3103 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3104 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3105 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3106 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3107 (with { unsigned int low = (tree_to_uhwi (@1)
3108 + tree_to_uhwi (@2)); }
3109 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3110 being well defined. */
3112 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3113 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3114 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3115 { build_zero_cst (type); }
3116 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3117 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3120 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3122 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3123 (if ((wi::to_wide (@1) & 1) != 0)
3124 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3125 { build_zero_cst (type); }))
3127 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3128 either to false if D is smaller (unsigned comparison) than C, or to
3129 x == log2 (D) - log2 (C). Similarly for right shifts. */
3133 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3134 (with { int c1 = wi::clz (wi::to_wide (@1));
3135 int c2 = wi::clz (wi::to_wide (@2)); }
3137 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3138 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3140 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3141 (if (tree_int_cst_sgn (@1) > 0)
3142 (with { int c1 = wi::clz (wi::to_wide (@1));
3143 int c2 = wi::clz (wi::to_wide (@2)); }
3145 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3146 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3148 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3149 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3153 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3154 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3156 || (!integer_zerop (@2)
3157 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3158 { constant_boolean_node (cmp == NE_EXPR, type); }
3159 (if (!integer_zerop (@2)
3160 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3161 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3163 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3164 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3165 if the new mask might be further optimized. */
3166 (for shift (lshift rshift)
3168 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3170 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3171 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3172 && tree_fits_uhwi_p (@1)
3173 && tree_to_uhwi (@1) > 0
3174 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3177 unsigned int shiftc = tree_to_uhwi (@1);
3178 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3179 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3180 tree shift_type = TREE_TYPE (@3);
3183 if (shift == LSHIFT_EXPR)
3184 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3185 else if (shift == RSHIFT_EXPR
3186 && type_has_mode_precision_p (shift_type))
3188 prec = TYPE_PRECISION (TREE_TYPE (@3));
3190 /* See if more bits can be proven as zero because of
3193 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3195 tree inner_type = TREE_TYPE (@0);
3196 if (type_has_mode_precision_p (inner_type)
3197 && TYPE_PRECISION (inner_type) < prec)
3199 prec = TYPE_PRECISION (inner_type);
3200 /* See if we can shorten the right shift. */
3202 shift_type = inner_type;
3203 /* Otherwise X >> C1 is all zeros, so we'll optimize
3204 it into (X, 0) later on by making sure zerobits
3208 zerobits = HOST_WIDE_INT_M1U;
3211 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3212 zerobits <<= prec - shiftc;
3214 /* For arithmetic shift if sign bit could be set, zerobits
3215 can contain actually sign bits, so no transformation is
3216 possible, unless MASK masks them all away. In that
3217 case the shift needs to be converted into logical shift. */
3218 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3219 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3221 if ((mask & zerobits) == 0)
3222 shift_type = unsigned_type_for (TREE_TYPE (@3));
3228 /* ((X << 16) & 0xff00) is (X, 0). */
3229 (if ((mask & zerobits) == mask)
3230 { build_int_cst (type, 0); }
3231 (with { newmask = mask | zerobits; }
3232 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3235 /* Only do the transformation if NEWMASK is some integer
3237 for (prec = BITS_PER_UNIT;
3238 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3239 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3242 (if (prec < HOST_BITS_PER_WIDE_INT
3243 || newmask == HOST_WIDE_INT_M1U)
3245 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3246 (if (!tree_int_cst_equal (newmaskt, @2))
3247 (if (shift_type != TREE_TYPE (@3))
3248 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3249 (bit_and @4 { newmaskt; })))))))))))))
3251 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3252 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3253 (for shift (lshift rshift)
3254 (for bit_op (bit_and bit_xor bit_ior)
3256 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3257 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3258 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3260 (bit_op (shift (convert @0) @1) { mask; })))))))
3262 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3264 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3265 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3266 && (element_precision (TREE_TYPE (@0))
3267 <= element_precision (TREE_TYPE (@1))
3268 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3270 { tree shift_type = TREE_TYPE (@0); }
3271 (convert (rshift (convert:shift_type @1) @2)))))
3273 /* ~(~X >>r Y) -> X >>r Y
3274 ~(~X <<r Y) -> X <<r Y */
3275 (for rotate (lrotate rrotate)
3277 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3278 (if ((element_precision (TREE_TYPE (@0))
3279 <= element_precision (TREE_TYPE (@1))
3280 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3281 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3282 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3284 { tree rotate_type = TREE_TYPE (@0); }
3285 (convert (rotate (convert:rotate_type @1) @2))))))
3287 /* Simplifications of conversions. */
3289 /* Basic strip-useless-type-conversions / strip_nops. */
3290 (for cvt (convert view_convert float fix_trunc)
3293 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3294 || (GENERIC && type == TREE_TYPE (@0)))
3297 /* Contract view-conversions. */
3299 (view_convert (view_convert @0))
3302 /* For integral conversions with the same precision or pointer
3303 conversions use a NOP_EXPR instead. */
3306 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3307 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3308 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3311 /* Strip inner integral conversions that do not change precision or size, or
3312 zero-extend while keeping the same size (for bool-to-char). */
3314 (view_convert (convert@0 @1))
3315 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3316 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3317 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3318 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3319 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3320 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3323 /* Simplify a view-converted empty constructor. */
3325 (view_convert CONSTRUCTOR@0)
3326 (if (TREE_CODE (@0) != SSA_NAME
3327 && CONSTRUCTOR_NELTS (@0) == 0)
3328 { build_zero_cst (type); }))
3330 /* Re-association barriers around constants and other re-association
3331 barriers can be removed. */
3333 (paren CONSTANT_CLASS_P@0)
3336 (paren (paren@1 @0))
3339 /* Handle cases of two conversions in a row. */
3340 (for ocvt (convert float fix_trunc)
3341 (for icvt (convert float)
3346 tree inside_type = TREE_TYPE (@0);
3347 tree inter_type = TREE_TYPE (@1);
3348 int inside_int = INTEGRAL_TYPE_P (inside_type);
3349 int inside_ptr = POINTER_TYPE_P (inside_type);
3350 int inside_float = FLOAT_TYPE_P (inside_type);
3351 int inside_vec = VECTOR_TYPE_P (inside_type);
3352 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3353 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3354 int inter_int = INTEGRAL_TYPE_P (inter_type);
3355 int inter_ptr = POINTER_TYPE_P (inter_type);
3356 int inter_float = FLOAT_TYPE_P (inter_type);
3357 int inter_vec = VECTOR_TYPE_P (inter_type);
3358 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3359 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3360 int final_int = INTEGRAL_TYPE_P (type);
3361 int final_ptr = POINTER_TYPE_P (type);
3362 int final_float = FLOAT_TYPE_P (type);
3363 int final_vec = VECTOR_TYPE_P (type);
3364 unsigned int final_prec = TYPE_PRECISION (type);
3365 int final_unsignedp = TYPE_UNSIGNED (type);
3368 /* In addition to the cases of two conversions in a row
3369 handled below, if we are converting something to its own
3370 type via an object of identical or wider precision, neither
3371 conversion is needed. */
3372 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3374 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3375 && (((inter_int || inter_ptr) && final_int)
3376 || (inter_float && final_float))
3377 && inter_prec >= final_prec)
3380 /* Likewise, if the intermediate and initial types are either both
3381 float or both integer, we don't need the middle conversion if the
3382 former is wider than the latter and doesn't change the signedness
3383 (for integers). Avoid this if the final type is a pointer since
3384 then we sometimes need the middle conversion. */
3385 (if (((inter_int && inside_int) || (inter_float && inside_float))
3386 && (final_int || final_float)
3387 && inter_prec >= inside_prec
3388 && (inter_float || inter_unsignedp == inside_unsignedp))
3391 /* If we have a sign-extension of a zero-extended value, we can
3392 replace that by a single zero-extension. Likewise if the
3393 final conversion does not change precision we can drop the
3394 intermediate conversion. */
3395 (if (inside_int && inter_int && final_int
3396 && ((inside_prec < inter_prec && inter_prec < final_prec
3397 && inside_unsignedp && !inter_unsignedp)
3398 || final_prec == inter_prec))
3401 /* Two conversions in a row are not needed unless:
3402 - some conversion is floating-point (overstrict for now), or
3403 - some conversion is a vector (overstrict for now), or
3404 - the intermediate type is narrower than both initial and
3406 - the intermediate type and innermost type differ in signedness,
3407 and the outermost type is wider than the intermediate, or
3408 - the initial type is a pointer type and the precisions of the
3409 intermediate and final types differ, or
3410 - the final type is a pointer type and the precisions of the
3411 initial and intermediate types differ. */
3412 (if (! inside_float && ! inter_float && ! final_float
3413 && ! inside_vec && ! inter_vec && ! final_vec
3414 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3415 && ! (inside_int && inter_int
3416 && inter_unsignedp != inside_unsignedp
3417 && inter_prec < final_prec)
3418 && ((inter_unsignedp && inter_prec > inside_prec)
3419 == (final_unsignedp && final_prec > inter_prec))
3420 && ! (inside_ptr && inter_prec != final_prec)
3421 && ! (final_ptr && inside_prec != inter_prec))
3424 /* A truncation to an unsigned type (a zero-extension) should be
3425 canonicalized as bitwise and of a mask. */
3426 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3427 && final_int && inter_int && inside_int
3428 && final_prec == inside_prec
3429 && final_prec > inter_prec
3431 (convert (bit_and @0 { wide_int_to_tree
3433 wi::mask (inter_prec, false,
3434 TYPE_PRECISION (inside_type))); })))
3436 /* If we are converting an integer to a floating-point that can
3437 represent it exactly and back to an integer, we can skip the
3438 floating-point conversion. */
3439 (if (GIMPLE /* PR66211 */
3440 && inside_int && inter_float && final_int &&
3441 (unsigned) significand_size (TYPE_MODE (inter_type))
3442 >= inside_prec - !inside_unsignedp)
3445 /* If we have a narrowing conversion to an integral type that is fed by a
3446 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3447 masks off bits outside the final type (and nothing else). */
3449 (convert (bit_and @0 INTEGER_CST@1))
3450 (if (INTEGRAL_TYPE_P (type)
3451 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3452 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3453 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3454 TYPE_PRECISION (type)), 0))
3458 /* (X /[ex] A) * A -> X. */
3460 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3463 /* Simplify (A / B) * B + (A % B) -> A. */
3464 (for div (trunc_div ceil_div floor_div round_div)
3465 mod (trunc_mod ceil_mod floor_mod round_mod)
3467 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3470 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3471 (for op (plus minus)
3473 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3474 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3475 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3478 wi::overflow_type overflow;
3479 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3480 TYPE_SIGN (type), &overflow);
3482 (if (types_match (type, TREE_TYPE (@2))
3483 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3484 (op @0 { wide_int_to_tree (type, mul); })
3485 (with { tree utype = unsigned_type_for (type); }
3486 (convert (op (convert:utype @0)
3487 (mult (convert:utype @1) (convert:utype @2))))))))))
3489 /* Canonicalization of binary operations. */
3491 /* Convert X + -C into X - C. */
3493 (plus @0 REAL_CST@1)
3494 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3495 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3496 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3497 (minus @0 { tem; })))))
3499 /* Convert x+x into x*2. */
3502 (if (SCALAR_FLOAT_TYPE_P (type))
3503 (mult @0 { build_real (type, dconst2); })
3504 (if (INTEGRAL_TYPE_P (type))
3505 (mult @0 { build_int_cst (type, 2); }))))
3509 (minus integer_zerop @1)
3512 (pointer_diff integer_zerop @1)
3513 (negate (convert @1)))
3515 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3516 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3517 (-ARG1 + ARG0) reduces to -ARG1. */
3519 (minus real_zerop@0 @1)
3520 (if (fold_real_zero_addition_p (type, @0, 0))
3523 /* Transform x * -1 into -x. */
3525 (mult @0 integer_minus_onep)
3528 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3529 signed overflow for CST != 0 && CST != -1. */
3531 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3532 (if (TREE_CODE (@2) != INTEGER_CST
3534 && !integer_zerop (@1) && !integer_minus_onep (@1))
3535 (mult (mult @0 @2) @1)))
3537 /* True if we can easily extract the real and imaginary parts of a complex
3539 (match compositional_complex
3540 (convert? (complex @0 @1)))
3542 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3544 (complex (realpart @0) (imagpart @0))
3547 (realpart (complex @0 @1))
3550 (imagpart (complex @0 @1))
3553 /* Sometimes we only care about half of a complex expression. */
3555 (realpart (convert?:s (conj:s @0)))
3556 (convert (realpart @0)))
3558 (imagpart (convert?:s (conj:s @0)))
3559 (convert (negate (imagpart @0))))
3560 (for part (realpart imagpart)
3561 (for op (plus minus)
3563 (part (convert?:s@2 (op:s @0 @1)))
3564 (convert (op (part @0) (part @1))))))
3566 (realpart (convert?:s (CEXPI:s @0)))
3569 (imagpart (convert?:s (CEXPI:s @0)))
3572 /* conj(conj(x)) -> x */
3574 (conj (convert? (conj @0)))
3575 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3578 /* conj({x,y}) -> {x,-y} */
3580 (conj (convert?:s (complex:s @0 @1)))
3581 (with { tree itype = TREE_TYPE (type); }
3582 (complex (convert:itype @0) (negate (convert:itype @1)))))
3584 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3585 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3590 (bswap (bit_not (bswap @0)))
3592 (for bitop (bit_xor bit_ior bit_and)
3594 (bswap (bitop:c (bswap @0) @1))
3595 (bitop @0 (bswap @1)))))
3598 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3600 /* Simplify constant conditions.
3601 Only optimize constant conditions when the selected branch
3602 has the same type as the COND_EXPR. This avoids optimizing
3603 away "c ? x : throw", where the throw has a void type.
3604 Note that we cannot throw away the fold-const.c variant nor
3605 this one as we depend on doing this transform before possibly
3606 A ? B : B -> B triggers and the fold-const.c one can optimize
3607 0 ? A : B to B even if A has side-effects. Something
3608 genmatch cannot handle. */
3610 (cond INTEGER_CST@0 @1 @2)
3611 (if (integer_zerop (@0))
3612 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3614 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3617 (vec_cond VECTOR_CST@0 @1 @2)
3618 (if (integer_all_onesp (@0))
3620 (if (integer_zerop (@0))
3624 /* Sink unary operations to branches, but only if we do fold both. */
3625 (for op (negate bit_not abs absu)
3627 (op (vec_cond:s @0 @1 @2))
3628 (vec_cond @0 (op! @1) (op! @2))))
3630 /* Sink binary operation to branches, but only if we can fold it. */
3631 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3632 rdiv trunc_div ceil_div floor_div round_div
3633 trunc_mod ceil_mod floor_mod round_mod min max)
3634 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3636 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3637 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3639 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3641 (op (vec_cond:s @0 @1 @2) @3)
3642 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3644 (op @3 (vec_cond:s @0 @1 @2))
3645 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3648 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3649 Currently disabled after pass lvec because ARM understands
3650 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3652 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3653 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3654 (vec_cond (bit_and @0 @3) @1 @2)))
3656 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3657 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3658 (vec_cond (bit_ior @0 @3) @1 @2)))
3660 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3661 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3662 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3664 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3665 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3666 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3668 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3670 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3671 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3672 (vec_cond (bit_and @0 @1) @2 @3)))
3674 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3675 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3676 (vec_cond (bit_ior @0 @1) @2 @3)))
3678 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3679 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3680 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3682 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3683 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3684 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3686 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3687 types are compatible. */
3689 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3690 (if (VECTOR_BOOLEAN_TYPE_P (type)
3691 && types_match (type, TREE_TYPE (@0)))
3692 (if (integer_zerop (@1) && integer_all_onesp (@2))
3694 (if (integer_all_onesp (@1) && integer_zerop (@2))
3697 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3699 /* This pattern implements two kinds simplification:
3702 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3703 1) Conversions are type widening from smaller type.
3704 2) Const c1 equals to c2 after canonicalizing comparison.
3705 3) Comparison has tree code LT, LE, GT or GE.
3706 This specific pattern is needed when (cmp (convert x) c) may not
3707 be simplified by comparison patterns because of multiple uses of
3708 x. It also makes sense here because simplifying across multiple
3709 referred var is always benefitial for complicated cases.
3712 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3713 (for cmp (lt le gt ge eq)
3715 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3718 tree from_type = TREE_TYPE (@1);
3719 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3720 enum tree_code code = ERROR_MARK;
3722 if (INTEGRAL_TYPE_P (from_type)
3723 && int_fits_type_p (@2, from_type)
3724 && (types_match (c1_type, from_type)
3725 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3726 && (TYPE_UNSIGNED (from_type)
3727 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3728 && (types_match (c2_type, from_type)
3729 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3730 && (TYPE_UNSIGNED (from_type)
3731 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3735 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3737 /* X <= Y - 1 equals to X < Y. */
3740 /* X > Y - 1 equals to X >= Y. */
3744 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3746 /* X < Y + 1 equals to X <= Y. */
3749 /* X >= Y + 1 equals to X > Y. */
3753 if (code != ERROR_MARK
3754 || wi::to_widest (@2) == wi::to_widest (@3))
3756 if (cmp == LT_EXPR || cmp == LE_EXPR)
3758 if (cmp == GT_EXPR || cmp == GE_EXPR)
3762 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3763 else if (int_fits_type_p (@3, from_type))
3767 (if (code == MAX_EXPR)
3768 (convert (max @1 (convert @2)))
3769 (if (code == MIN_EXPR)
3770 (convert (min @1 (convert @2)))
3771 (if (code == EQ_EXPR)
3772 (convert (cond (eq @1 (convert @3))
3773 (convert:from_type @3) (convert:from_type @2)))))))))
3775 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3777 1) OP is PLUS or MINUS.
3778 2) CMP is LT, LE, GT or GE.
3779 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3781 This pattern also handles special cases like:
3783 A) Operand x is a unsigned to signed type conversion and c1 is
3784 integer zero. In this case,
3785 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3786 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3787 B) Const c1 may not equal to (C3 op' C2). In this case we also
3788 check equality for (c1+1) and (c1-1) by adjusting comparison
3791 TODO: Though signed type is handled by this pattern, it cannot be
3792 simplified at the moment because C standard requires additional
3793 type promotion. In order to match&simplify it here, the IR needs
3794 to be cleaned up by other optimizers, i.e, VRP. */
3795 (for op (plus minus)
3796 (for cmp (lt le gt ge)
3798 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3799 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3800 (if (types_match (from_type, to_type)
3801 /* Check if it is special case A). */
3802 || (TYPE_UNSIGNED (from_type)
3803 && !TYPE_UNSIGNED (to_type)
3804 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3805 && integer_zerop (@1)
3806 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3809 wi::overflow_type overflow = wi::OVF_NONE;
3810 enum tree_code code, cmp_code = cmp;
3812 wide_int c1 = wi::to_wide (@1);
3813 wide_int c2 = wi::to_wide (@2);
3814 wide_int c3 = wi::to_wide (@3);
3815 signop sgn = TYPE_SIGN (from_type);
3817 /* Handle special case A), given x of unsigned type:
3818 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3819 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3820 if (!types_match (from_type, to_type))
3822 if (cmp_code == LT_EXPR)
3824 if (cmp_code == GE_EXPR)
3826 c1 = wi::max_value (to_type);
3828 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3829 compute (c3 op' c2) and check if it equals to c1 with op' being
3830 the inverted operator of op. Make sure overflow doesn't happen
3831 if it is undefined. */
3832 if (op == PLUS_EXPR)
3833 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3835 real_c1 = wi::add (c3, c2, sgn, &overflow);
3838 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3840 /* Check if c1 equals to real_c1. Boundary condition is handled
3841 by adjusting comparison operation if necessary. */
3842 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3845 /* X <= Y - 1 equals to X < Y. */
3846 if (cmp_code == LE_EXPR)
3848 /* X > Y - 1 equals to X >= Y. */
3849 if (cmp_code == GT_EXPR)
3852 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3855 /* X < Y + 1 equals to X <= Y. */
3856 if (cmp_code == LT_EXPR)
3858 /* X >= Y + 1 equals to X > Y. */
3859 if (cmp_code == GE_EXPR)
3862 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3864 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3866 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3871 (if (code == MAX_EXPR)
3872 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3873 { wide_int_to_tree (from_type, c2); })
3874 (if (code == MIN_EXPR)
3875 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3876 { wide_int_to_tree (from_type, c2); })))))))))
3878 (for cnd (cond vec_cond)
3879 /* A ? B : (A ? X : C) -> A ? B : C. */
3881 (cnd @0 (cnd @0 @1 @2) @3)
3884 (cnd @0 @1 (cnd @0 @2 @3))
3886 /* A ? B : (!A ? C : X) -> A ? B : C. */
3887 /* ??? This matches embedded conditions open-coded because genmatch
3888 would generate matching code for conditions in separate stmts only.
3889 The following is still important to merge then and else arm cases
3890 from if-conversion. */
3892 (cnd @0 @1 (cnd @2 @3 @4))
3893 (if (inverse_conditions_p (@0, @2))
3896 (cnd @0 (cnd @1 @2 @3) @4)
3897 (if (inverse_conditions_p (@0, @1))
3900 /* A ? B : B -> B. */
3905 /* !A ? B : C -> A ? C : B. */
3907 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3910 /* -(type)!A -> (type)A - 1. */
3912 (negate (convert?:s (logical_inverted_value:s @0)))
3913 (if (INTEGRAL_TYPE_P (type)
3914 && TREE_CODE (type) != BOOLEAN_TYPE
3915 && TYPE_PRECISION (type) > 1
3916 && TREE_CODE (@0) == SSA_NAME
3917 && ssa_name_has_boolean_range (@0))
3918 (plus (convert:type @0) { build_all_ones_cst (type); })))
3920 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3921 return all -1 or all 0 results. */
3922 /* ??? We could instead convert all instances of the vec_cond to negate,
3923 but that isn't necessarily a win on its own. */
3925 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3926 (if (VECTOR_TYPE_P (type)
3927 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3928 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3929 && (TYPE_MODE (TREE_TYPE (type))
3930 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3931 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3933 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3935 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3936 (if (VECTOR_TYPE_P (type)
3937 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3938 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3939 && (TYPE_MODE (TREE_TYPE (type))
3940 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3941 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3944 /* Simplifications of comparisons. */
3946 /* See if we can reduce the magnitude of a constant involved in a
3947 comparison by changing the comparison code. This is a canonicalization
3948 formerly done by maybe_canonicalize_comparison_1. */
3952 (cmp @0 uniform_integer_cst_p@1)
3953 (with { tree cst = uniform_integer_cst_p (@1); }
3954 (if (tree_int_cst_sgn (cst) == -1)
3955 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3956 wide_int_to_tree (TREE_TYPE (cst),
3962 (cmp @0 uniform_integer_cst_p@1)
3963 (with { tree cst = uniform_integer_cst_p (@1); }
3964 (if (tree_int_cst_sgn (cst) == 1)
3965 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3966 wide_int_to_tree (TREE_TYPE (cst),
3967 wi::to_wide (cst) - 1)); })))))
3969 /* We can simplify a logical negation of a comparison to the
3970 inverted comparison. As we cannot compute an expression
3971 operator using invert_tree_comparison we have to simulate
3972 that with expression code iteration. */
3973 (for cmp (tcc_comparison)
3974 icmp (inverted_tcc_comparison)
3975 ncmp (inverted_tcc_comparison_with_nans)
3976 /* Ideally we'd like to combine the following two patterns
3977 and handle some more cases by using
3978 (logical_inverted_value (cmp @0 @1))
3979 here but for that genmatch would need to "inline" that.
3980 For now implement what forward_propagate_comparison did. */
3982 (bit_not (cmp @0 @1))
3983 (if (VECTOR_TYPE_P (type)
3984 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3985 /* Comparison inversion may be impossible for trapping math,
3986 invert_tree_comparison will tell us. But we can't use
3987 a computed operator in the replacement tree thus we have
3988 to play the trick below. */
3989 (with { enum tree_code ic = invert_tree_comparison
3990 (cmp, HONOR_NANS (@0)); }
3996 (bit_xor (cmp @0 @1) integer_truep)
3997 (with { enum tree_code ic = invert_tree_comparison
3998 (cmp, HONOR_NANS (@0)); }
4004 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4005 ??? The transformation is valid for the other operators if overflow
4006 is undefined for the type, but performing it here badly interacts
4007 with the transformation in fold_cond_expr_with_comparison which
4008 attempts to synthetize ABS_EXPR. */
4010 (for sub (minus pointer_diff)
4012 (cmp (sub@2 @0 @1) integer_zerop)
4013 (if (single_use (@2))
4016 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4017 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4020 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4021 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4022 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4023 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4024 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4025 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4026 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4028 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4029 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4030 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4031 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4032 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4034 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4035 signed arithmetic case. That form is created by the compiler
4036 often enough for folding it to be of value. One example is in
4037 computing loop trip counts after Operator Strength Reduction. */
4038 (for cmp (simple_comparison)
4039 scmp (swapped_simple_comparison)
4041 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4042 /* Handle unfolded multiplication by zero. */
4043 (if (integer_zerop (@1))
4045 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4046 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4048 /* If @1 is negative we swap the sense of the comparison. */
4049 (if (tree_int_cst_sgn (@1) < 0)
4053 /* For integral types with undefined overflow fold
4054 x * C1 == C2 into x == C2 / C1 or false.
4055 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4059 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4060 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4061 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4062 && wi::to_wide (@1) != 0)
4063 (with { widest_int quot; }
4064 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4065 TYPE_SIGN (TREE_TYPE (@0)), "))
4066 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4067 { constant_boolean_node (cmp == NE_EXPR, type); }))
4068 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4069 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4070 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4073 tree itype = TREE_TYPE (@0);
4074 int p = TYPE_PRECISION (itype);
4075 wide_int m = wi::one (p + 1) << p;
4076 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4077 wide_int i = wide_int::from (wi::mod_inv (a, m),
4078 p, TYPE_SIGN (itype));
4079 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4082 /* Simplify comparison of something with itself. For IEEE
4083 floating-point, we can only do some of these simplifications. */
4087 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4088 || ! HONOR_NANS (@0))
4089 { constant_boolean_node (true, type); }
4090 (if (cmp != EQ_EXPR)
4096 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4097 || ! HONOR_NANS (@0))
4098 { constant_boolean_node (false, type); })))
4099 (for cmp (unle unge uneq)
4102 { constant_boolean_node (true, type); }))
4103 (for cmp (unlt ungt)
4109 (if (!flag_trapping_math)
4110 { constant_boolean_node (false, type); }))
4112 /* x == ~x -> false */
4113 /* x != ~x -> true */
4116 (cmp:c @0 (bit_not @0))
4117 { constant_boolean_node (cmp == NE_EXPR, type); }))
4119 /* Fold ~X op ~Y as Y op X. */
4120 (for cmp (simple_comparison)
4122 (cmp (bit_not@2 @0) (bit_not@3 @1))
4123 (if (single_use (@2) && single_use (@3))
4126 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4127 (for cmp (simple_comparison)
4128 scmp (swapped_simple_comparison)
4130 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4131 (if (single_use (@2)
4132 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4133 (scmp @0 (bit_not @1)))))
4135 (for cmp (simple_comparison)
4136 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4138 (cmp (convert@2 @0) (convert? @1))
4139 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4140 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4141 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4142 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4143 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4146 tree type1 = TREE_TYPE (@1);
4147 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4149 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4150 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4151 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4152 type1 = float_type_node;
4153 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4154 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4155 type1 = double_type_node;
4158 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4159 ? TREE_TYPE (@0) : type1);
4161 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4162 (cmp (convert:newtype @0) (convert:newtype @1))))))
4166 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4168 /* a CMP (-0) -> a CMP 0 */
4169 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4170 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4171 /* x != NaN is always true, other ops are always false. */
4172 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4173 && ! HONOR_SNANS (@1))
4174 { constant_boolean_node (cmp == NE_EXPR, type); })
4175 /* Fold comparisons against infinity. */
4176 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4177 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4180 REAL_VALUE_TYPE max;
4181 enum tree_code code = cmp;
4182 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4184 code = swap_tree_comparison (code);
4187 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4188 (if (code == GT_EXPR
4189 && !(HONOR_NANS (@0) && flag_trapping_math))
4190 { constant_boolean_node (false, type); })
4191 (if (code == LE_EXPR)
4192 /* x <= +Inf is always true, if we don't care about NaNs. */
4193 (if (! HONOR_NANS (@0))
4194 { constant_boolean_node (true, type); }
4195 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4196 an "invalid" exception. */
4197 (if (!flag_trapping_math)
4199 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4200 for == this introduces an exception for x a NaN. */
4201 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4203 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4205 (lt @0 { build_real (TREE_TYPE (@0), max); })
4206 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4207 /* x < +Inf is always equal to x <= DBL_MAX. */
4208 (if (code == LT_EXPR)
4209 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4211 (ge @0 { build_real (TREE_TYPE (@0), max); })
4212 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4213 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4214 an exception for x a NaN so use an unordered comparison. */
4215 (if (code == NE_EXPR)
4216 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4217 (if (! HONOR_NANS (@0))
4219 (ge @0 { build_real (TREE_TYPE (@0), max); })
4220 (le @0 { build_real (TREE_TYPE (@0), max); }))
4222 (unge @0 { build_real (TREE_TYPE (@0), max); })
4223 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4225 /* If this is a comparison of a real constant with a PLUS_EXPR
4226 or a MINUS_EXPR of a real constant, we can convert it into a
4227 comparison with a revised real constant as long as no overflow
4228 occurs when unsafe_math_optimizations are enabled. */
4229 (if (flag_unsafe_math_optimizations)
4230 (for op (plus minus)
4232 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4235 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4236 TREE_TYPE (@1), @2, @1);
4238 (if (tem && !TREE_OVERFLOW (tem))
4239 (cmp @0 { tem; }))))))
4241 /* Likewise, we can simplify a comparison of a real constant with
4242 a MINUS_EXPR whose first operand is also a real constant, i.e.
4243 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4244 floating-point types only if -fassociative-math is set. */
4245 (if (flag_associative_math)
4247 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4248 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4249 (if (tem && !TREE_OVERFLOW (tem))
4250 (cmp { tem; } @1)))))
4252 /* Fold comparisons against built-in math functions. */
4253 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4256 (cmp (sq @0) REAL_CST@1)
4258 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4260 /* sqrt(x) < y is always false, if y is negative. */
4261 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4262 { constant_boolean_node (false, type); })
4263 /* sqrt(x) > y is always true, if y is negative and we
4264 don't care about NaNs, i.e. negative values of x. */
4265 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4266 { constant_boolean_node (true, type); })
4267 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4268 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4269 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4271 /* sqrt(x) < 0 is always false. */
4272 (if (cmp == LT_EXPR)
4273 { constant_boolean_node (false, type); })
4274 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4275 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4276 { constant_boolean_node (true, type); })
4277 /* sqrt(x) <= 0 -> x == 0. */
4278 (if (cmp == LE_EXPR)
4280 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4281 == or !=. In the last case:
4283 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4285 if x is negative or NaN. Due to -funsafe-math-optimizations,
4286 the results for other x follow from natural arithmetic. */
4288 (if ((cmp == LT_EXPR
4292 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4293 /* Give up for -frounding-math. */
4294 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4298 enum tree_code ncmp = cmp;
4299 const real_format *fmt
4300 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4301 real_arithmetic (&c2, MULT_EXPR,
4302 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4303 real_convert (&c2, fmt, &c2);
4304 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4305 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4306 if (!REAL_VALUE_ISINF (c2))
4308 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4309 build_real (TREE_TYPE (@0), c2));
4310 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4312 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4313 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4314 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4315 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4316 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4317 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4320 /* With rounding to even, sqrt of up to 3 different values
4321 gives the same normal result, so in some cases c2 needs
4323 REAL_VALUE_TYPE c2alt, tow;
4324 if (cmp == LT_EXPR || cmp == GE_EXPR)
4328 real_nextafter (&c2alt, fmt, &c2, &tow);
4329 real_convert (&c2alt, fmt, &c2alt);
4330 if (REAL_VALUE_ISINF (c2alt))
4334 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4335 build_real (TREE_TYPE (@0), c2alt));
4336 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4338 else if (real_equal (&TREE_REAL_CST (c3),
4339 &TREE_REAL_CST (@1)))
4345 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4346 (if (REAL_VALUE_ISINF (c2))
4347 /* sqrt(x) > y is x == +Inf, when y is very large. */
4348 (if (HONOR_INFINITIES (@0))
4349 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4350 { constant_boolean_node (false, type); })
4351 /* sqrt(x) > c is the same as x > c*c. */
4352 (if (ncmp != ERROR_MARK)
4353 (if (ncmp == GE_EXPR)
4354 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4355 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4356 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4357 (if (REAL_VALUE_ISINF (c2))
4359 /* sqrt(x) < y is always true, when y is a very large
4360 value and we don't care about NaNs or Infinities. */
4361 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4362 { constant_boolean_node (true, type); })
4363 /* sqrt(x) < y is x != +Inf when y is very large and we
4364 don't care about NaNs. */
4365 (if (! HONOR_NANS (@0))
4366 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4367 /* sqrt(x) < y is x >= 0 when y is very large and we
4368 don't care about Infinities. */
4369 (if (! HONOR_INFINITIES (@0))
4370 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4371 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4374 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4375 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4376 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4377 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4378 (if (ncmp == LT_EXPR)
4379 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4380 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4381 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4382 (if (ncmp != ERROR_MARK && GENERIC)
4383 (if (ncmp == LT_EXPR)
4385 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4386 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4388 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4389 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4390 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4392 (cmp (sq @0) (sq @1))
4393 (if (! HONOR_NANS (@0))
4396 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4397 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4398 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4400 (cmp (float@0 @1) (float @2))
4401 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4402 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4405 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4406 tree type1 = TREE_TYPE (@1);
4407 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4408 tree type2 = TREE_TYPE (@2);
4409 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4411 (if (fmt.can_represent_integral_type_p (type1)
4412 && fmt.can_represent_integral_type_p (type2))
4413 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4414 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4415 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4416 && type1_signed_p >= type2_signed_p)
4417 (icmp @1 (convert @2))
4418 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4419 && type1_signed_p <= type2_signed_p)
4420 (icmp (convert:type2 @1) @2)
4421 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4422 && type1_signed_p == type2_signed_p)
4423 (icmp @1 @2))))))))))
4425 /* Optimize various special cases of (FTYPE) N CMP CST. */
4426 (for cmp (lt le eq ne ge gt)
4427 icmp (le le eq ne ge ge)
4429 (cmp (float @0) REAL_CST@1)
4430 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4431 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4434 tree itype = TREE_TYPE (@0);
4435 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4436 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4437 /* Be careful to preserve any potential exceptions due to
4438 NaNs. qNaNs are ok in == or != context.
4439 TODO: relax under -fno-trapping-math or
4440 -fno-signaling-nans. */
4442 = real_isnan (cst) && (cst->signalling
4443 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4445 /* TODO: allow non-fitting itype and SNaNs when
4446 -fno-trapping-math. */
4447 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4450 signop isign = TYPE_SIGN (itype);
4451 REAL_VALUE_TYPE imin, imax;
4452 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4453 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4455 REAL_VALUE_TYPE icst;
4456 if (cmp == GT_EXPR || cmp == GE_EXPR)
4457 real_ceil (&icst, fmt, cst);
4458 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4459 real_floor (&icst, fmt, cst);
4461 real_trunc (&icst, fmt, cst);
4463 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4465 bool overflow_p = false;
4467 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4470 /* Optimize cases when CST is outside of ITYPE's range. */
4471 (if (real_compare (LT_EXPR, cst, &imin))
4472 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4474 (if (real_compare (GT_EXPR, cst, &imax))
4475 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4477 /* Remove cast if CST is an integer representable by ITYPE. */
4479 (cmp @0 { gcc_assert (!overflow_p);
4480 wide_int_to_tree (itype, icst_val); })
4482 /* When CST is fractional, optimize
4483 (FTYPE) N == CST -> 0
4484 (FTYPE) N != CST -> 1. */
4485 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4486 { constant_boolean_node (cmp == NE_EXPR, type); })
4487 /* Otherwise replace with sensible integer constant. */
4490 gcc_checking_assert (!overflow_p);
4492 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4494 /* Fold A /[ex] B CMP C to A CMP B * C. */
4497 (cmp (exact_div @0 @1) INTEGER_CST@2)
4498 (if (!integer_zerop (@1))
4499 (if (wi::to_wide (@2) == 0)
4501 (if (TREE_CODE (@1) == INTEGER_CST)
4504 wi::overflow_type ovf;
4505 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4506 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4509 { constant_boolean_node (cmp == NE_EXPR, type); }
4510 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4511 (for cmp (lt le gt ge)
4513 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4514 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4517 wi::overflow_type ovf;
4518 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4519 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4522 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4523 TYPE_SIGN (TREE_TYPE (@2)))
4524 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4525 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4527 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4529 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4530 For large C (more than min/B+2^size), this is also true, with the
4531 multiplication computed modulo 2^size.
4532 For intermediate C, this just tests the sign of A. */
4533 (for cmp (lt le gt ge)
4536 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4537 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4538 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4539 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4542 tree utype = TREE_TYPE (@2);
4543 wide_int denom = wi::to_wide (@1);
4544 wide_int right = wi::to_wide (@2);
4545 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4546 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4547 bool small = wi::leu_p (right, smax);
4548 bool large = wi::geu_p (right, smin);
4550 (if (small || large)
4551 (cmp (convert:utype @0) (mult @2 (convert @1)))
4552 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4554 /* Unordered tests if either argument is a NaN. */
4556 (bit_ior (unordered @0 @0) (unordered @1 @1))
4557 (if (types_match (@0, @1))
4560 (bit_and (ordered @0 @0) (ordered @1 @1))
4561 (if (types_match (@0, @1))
4564 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4567 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4570 /* Simple range test simplifications. */
4571 /* A < B || A >= B -> true. */
4572 (for test1 (lt le le le ne ge)
4573 test2 (ge gt ge ne eq ne)
4575 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4576 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4577 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4578 { constant_boolean_node (true, type); })))
4579 /* A < B && A >= B -> false. */
4580 (for test1 (lt lt lt le ne eq)
4581 test2 (ge gt eq gt eq gt)
4583 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4584 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4585 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4586 { constant_boolean_node (false, type); })))
4588 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4589 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4591 Note that comparisons
4592 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4593 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4594 will be canonicalized to above so there's no need to
4601 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4602 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4605 tree ty = TREE_TYPE (@0);
4606 unsigned prec = TYPE_PRECISION (ty);
4607 wide_int mask = wi::to_wide (@2, prec);
4608 wide_int rhs = wi::to_wide (@3, prec);
4609 signop sgn = TYPE_SIGN (ty);
4611 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4612 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4613 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4614 { build_zero_cst (ty); }))))))
4616 /* -A CMP -B -> B CMP A. */
4617 (for cmp (tcc_comparison)
4618 scmp (swapped_tcc_comparison)
4620 (cmp (negate @0) (negate @1))
4621 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4622 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4623 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4626 (cmp (negate @0) CONSTANT_CLASS_P@1)
4627 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4628 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4629 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4630 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4631 (if (tem && !TREE_OVERFLOW (tem))
4632 (scmp @0 { tem; }))))))
4634 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4637 (op (abs @0) zerop@1)
4640 /* From fold_sign_changed_comparison and fold_widened_comparison.
4641 FIXME: the lack of symmetry is disturbing. */
4642 (for cmp (simple_comparison)
4644 (cmp (convert@0 @00) (convert?@1 @10))
4645 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4646 /* Disable this optimization if we're casting a function pointer
4647 type on targets that require function pointer canonicalization. */
4648 && !(targetm.have_canonicalize_funcptr_for_compare ()
4649 && ((POINTER_TYPE_P (TREE_TYPE (@00))
4650 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4651 || (POINTER_TYPE_P (TREE_TYPE (@10))
4652 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4654 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4655 && (TREE_CODE (@10) == INTEGER_CST
4657 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4660 && !POINTER_TYPE_P (TREE_TYPE (@00)))
4661 /* ??? The special-casing of INTEGER_CST conversion was in the original
4662 code and here to avoid a spurious overflow flag on the resulting
4663 constant which fold_convert produces. */
4664 (if (TREE_CODE (@1) == INTEGER_CST)
4665 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4666 TREE_OVERFLOW (@1)); })
4667 (cmp @00 (convert @1)))
4669 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4670 /* If possible, express the comparison in the shorter mode. */
4671 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4672 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4673 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4674 && TYPE_UNSIGNED (TREE_TYPE (@00))))
4675 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4676 || ((TYPE_PRECISION (TREE_TYPE (@00))
4677 >= TYPE_PRECISION (TREE_TYPE (@10)))
4678 && (TYPE_UNSIGNED (TREE_TYPE (@00))
4679 == TYPE_UNSIGNED (TREE_TYPE (@10))))
4680 || (TREE_CODE (@10) == INTEGER_CST
4681 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4682 && int_fits_type_p (@10, TREE_TYPE (@00)))))
4683 (cmp @00 (convert @10))
4684 (if (TREE_CODE (@10) == INTEGER_CST
4685 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4686 && !int_fits_type_p (@10, TREE_TYPE (@00)))
4689 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4690 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4691 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4692 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4694 (if (above || below)
4695 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4696 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4697 (if (cmp == LT_EXPR || cmp == LE_EXPR)
4698 { constant_boolean_node (above ? true : false, type); }
4699 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4700 { constant_boolean_node (above ? false : true, type); }))))))))))))
4704 /* SSA names are canonicalized to 2nd place. */
4705 (cmp addr@0 SSA_NAME@1)
4707 { poly_int64 off; tree base; }
4708 /* A local variable can never be pointed to by
4709 the default SSA name of an incoming parameter. */
4710 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4711 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4712 && (base = get_base_address (TREE_OPERAND (@0, 0)))
4713 && TREE_CODE (base) == VAR_DECL
4714 && auto_var_in_fn_p (base, current_function_decl))
4715 (if (cmp == NE_EXPR)
4716 { constant_boolean_node (true, type); }
4717 { constant_boolean_node (false, type); })
4718 /* If the address is based on @1 decide using the offset. */
4719 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4720 && TREE_CODE (base) == MEM_REF
4721 && TREE_OPERAND (base, 0) == @1)
4722 (with { off += mem_ref_offset (base).force_shwi (); }
4723 (if (known_ne (off, 0))
4724 { constant_boolean_node (cmp == NE_EXPR, type); }
4725 (if (known_eq (off, 0))
4726 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4728 /* Equality compare simplifications from fold_binary */
4731 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4732 Similarly for NE_EXPR. */
4734 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4735 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4736 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4737 { constant_boolean_node (cmp == NE_EXPR, type); }))
4739 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
4741 (cmp (bit_xor @0 @1) integer_zerop)
4744 /* (X ^ Y) == Y becomes X == 0.
4745 Likewise (X ^ Y) == X becomes Y == 0. */
4747 (cmp:c (bit_xor:c @0 @1) @0)
4748 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4750 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
4752 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4753 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4754 (cmp @0 (bit_xor @1 (convert @2)))))
4757 (cmp (convert? addr@0) integer_zerop)
4758 (if (tree_single_nonzero_warnv_p (@0, NULL))
4759 { constant_boolean_node (cmp == NE_EXPR, type); }))
4761 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
4763 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
4764 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
4766 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
4767 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
4768 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
4769 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
4774 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
4775 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4776 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4777 && types_match (@0, @1))
4778 (ncmp (bit_xor @0 @1) @2)))))
4779 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
4780 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
4784 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
4785 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4786 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4787 && types_match (@0, @1))
4788 (ncmp (bit_xor @0 @1) @2))))
4790 /* If we have (A & C) == C where C is a power of 2, convert this into
4791 (A & C) != 0. Similarly for NE_EXPR. */
4795 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4796 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4798 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4799 convert this into a shift followed by ANDing with D. */
4802 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4803 INTEGER_CST@2 integer_zerop)
4804 (if (integer_pow2p (@2))
4806 int shift = (wi::exact_log2 (wi::to_wide (@2))
4807 - wi::exact_log2 (wi::to_wide (@1)));
4811 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4813 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4816 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4817 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
4821 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4822 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4823 && type_has_mode_precision_p (TREE_TYPE (@0))
4824 && element_precision (@2) >= element_precision (@0)
4825 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4826 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4827 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4829 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4830 this into a right shift or sign extension followed by ANDing with C. */
4833 (lt @0 integer_zerop)
4834 INTEGER_CST@1 integer_zerop)
4835 (if (integer_pow2p (@1)
4836 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4838 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4842 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4844 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4845 sign extension followed by AND with C will achieve the effect. */
4846 (bit_and (convert @0) @1)))))
4848 /* When the addresses are not directly of decls compare base and offset.
4849 This implements some remaining parts of fold_comparison address
4850 comparisons but still no complete part of it. Still it is good
4851 enough to make fold_stmt not regress when not dispatching to fold_binary. */
4852 (for cmp (simple_comparison)
4854 (cmp (convert1?@2 addr@0) (convert2? addr@1))
4857 poly_int64 off0, off1;
4858 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4859 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4860 if (base0 && TREE_CODE (base0) == MEM_REF)
4862 off0 += mem_ref_offset (base0).force_shwi ();
4863 base0 = TREE_OPERAND (base0, 0);
4865 if (base1 && TREE_CODE (base1) == MEM_REF)
4867 off1 += mem_ref_offset (base1).force_shwi ();
4868 base1 = TREE_OPERAND (base1, 0);
4871 (if (base0 && base1)
4875 /* Punt in GENERIC on variables with value expressions;
4876 the value expressions might point to fields/elements
4877 of other vars etc. */
4879 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4880 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4882 else if (decl_in_symtab_p (base0)
4883 && decl_in_symtab_p (base1))
4884 equal = symtab_node::get_create (base0)
4885 ->equal_address_to (symtab_node::get_create (base1));
4886 else if ((DECL_P (base0)
4887 || TREE_CODE (base0) == SSA_NAME
4888 || TREE_CODE (base0) == STRING_CST)
4890 || TREE_CODE (base1) == SSA_NAME
4891 || TREE_CODE (base1) == STRING_CST))
4892 equal = (base0 == base1);
4895 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4896 off0.is_constant (&ioff0);
4897 off1.is_constant (&ioff1);
4898 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4899 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4900 || (TREE_CODE (base0) == STRING_CST
4901 && TREE_CODE (base1) == STRING_CST
4902 && ioff0 >= 0 && ioff1 >= 0
4903 && ioff0 < TREE_STRING_LENGTH (base0)
4904 && ioff1 < TREE_STRING_LENGTH (base1)
4905 /* This is a too conservative test that the STRING_CSTs
4906 will not end up being string-merged. */
4907 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4908 TREE_STRING_POINTER (base1) + ioff1,
4909 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4910 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4912 else if (!DECL_P (base0) || !DECL_P (base1))
4914 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4916 /* If this is a pointer comparison, ignore for now even
4917 valid equalities where one pointer is the offset zero
4918 of one object and the other to one past end of another one. */
4919 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4921 /* Assume that automatic variables can't be adjacent to global
4923 else if (is_global_var (base0) != is_global_var (base1))
4927 tree sz0 = DECL_SIZE_UNIT (base0);
4928 tree sz1 = DECL_SIZE_UNIT (base1);
4929 /* If sizes are unknown, e.g. VLA or not representable,
4931 if (!tree_fits_poly_int64_p (sz0)
4932 || !tree_fits_poly_int64_p (sz1))
4936 poly_int64 size0 = tree_to_poly_int64 (sz0);
4937 poly_int64 size1 = tree_to_poly_int64 (sz1);
4938 /* If one offset is pointing (or could be) to the beginning
4939 of one object and the other is pointing to one past the
4940 last byte of the other object, punt. */
4941 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4943 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4945 /* If both offsets are the same, there are some cases
4946 we know that are ok. Either if we know they aren't
4947 zero, or if we know both sizes are no zero. */
4949 && known_eq (off0, off1)
4950 && (known_ne (off0, 0)
4951 || (known_ne (size0, 0) && known_ne (size1, 0))))
4958 && (cmp == EQ_EXPR || cmp == NE_EXPR
4959 /* If the offsets are equal we can ignore overflow. */
4960 || known_eq (off0, off1)
4961 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4962 /* Or if we compare using pointers to decls or strings. */
4963 || (POINTER_TYPE_P (TREE_TYPE (@2))
4964 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4966 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4967 { constant_boolean_node (known_eq (off0, off1), type); })
4968 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4969 { constant_boolean_node (known_ne (off0, off1), type); })
4970 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4971 { constant_boolean_node (known_lt (off0, off1), type); })
4972 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4973 { constant_boolean_node (known_le (off0, off1), type); })
4974 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4975 { constant_boolean_node (known_ge (off0, off1), type); })
4976 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4977 { constant_boolean_node (known_gt (off0, off1), type); }))
4980 (if (cmp == EQ_EXPR)
4981 { constant_boolean_node (false, type); })
4982 (if (cmp == NE_EXPR)
4983 { constant_boolean_node (true, type); })))))))))
4985 /* Simplify pointer equality compares using PTA. */
4989 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4990 && ptrs_compare_unequal (@0, @1))
4991 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4993 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4994 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4995 Disable the transform if either operand is pointer to function.
4996 This broke pr22051-2.c for arm where function pointer
4997 canonicalizaion is not wanted. */
5001 (cmp (convert @0) INTEGER_CST@1)
5002 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5003 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5004 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5005 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5006 && POINTER_TYPE_P (TREE_TYPE (@1))
5007 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5008 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5009 (cmp @0 (convert @1)))))
5011 /* Non-equality compare simplifications from fold_binary */
5012 (for cmp (lt gt le ge)
5013 /* Comparisons with the highest or lowest possible integer of
5014 the specified precision will have known values. */
5016 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5017 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5018 || POINTER_TYPE_P (TREE_TYPE (@1))
5019 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5020 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5023 tree cst = uniform_integer_cst_p (@1);
5024 tree arg1_type = TREE_TYPE (cst);
5025 unsigned int prec = TYPE_PRECISION (arg1_type);
5026 wide_int max = wi::max_value (arg1_type);
5027 wide_int signed_max = wi::max_value (prec, SIGNED);
5028 wide_int min = wi::min_value (arg1_type);
5031 (if (wi::to_wide (cst) == max)
5033 (if (cmp == GT_EXPR)
5034 { constant_boolean_node (false, type); })
5035 (if (cmp == GE_EXPR)
5037 (if (cmp == LE_EXPR)
5038 { constant_boolean_node (true, type); })
5039 (if (cmp == LT_EXPR)
5041 (if (wi::to_wide (cst) == min)
5043 (if (cmp == LT_EXPR)
5044 { constant_boolean_node (false, type); })
5045 (if (cmp == LE_EXPR)
5047 (if (cmp == GE_EXPR)
5048 { constant_boolean_node (true, type); })
5049 (if (cmp == GT_EXPR)
5051 (if (wi::to_wide (cst) == max - 1)
5053 (if (cmp == GT_EXPR)
5054 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5055 wide_int_to_tree (TREE_TYPE (cst),
5058 (if (cmp == LE_EXPR)
5059 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5060 wide_int_to_tree (TREE_TYPE (cst),
5063 (if (wi::to_wide (cst) == min + 1)
5065 (if (cmp == GE_EXPR)
5066 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5067 wide_int_to_tree (TREE_TYPE (cst),
5070 (if (cmp == LT_EXPR)
5071 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5072 wide_int_to_tree (TREE_TYPE (cst),
5075 (if (wi::to_wide (cst) == signed_max
5076 && TYPE_UNSIGNED (arg1_type)
5077 /* We will flip the signedness of the comparison operator
5078 associated with the mode of @1, so the sign bit is
5079 specified by this mode. Check that @1 is the signed
5080 max associated with this sign bit. */
5081 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5082 /* signed_type does not work on pointer types. */
5083 && INTEGRAL_TYPE_P (arg1_type))
5084 /* The following case also applies to X < signed_max+1
5085 and X >= signed_max+1 because previous transformations. */
5086 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5087 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5089 (if (cst == @1 && cmp == LE_EXPR)
5090 (ge (convert:st @0) { build_zero_cst (st); }))
5091 (if (cst == @1 && cmp == GT_EXPR)
5092 (lt (convert:st @0) { build_zero_cst (st); }))
5093 (if (cmp == LE_EXPR)
5094 (ge (view_convert:st @0) { build_zero_cst (st); }))
5095 (if (cmp == GT_EXPR)
5096 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5098 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5099 /* If the second operand is NaN, the result is constant. */
5102 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5103 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5104 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5105 ? false : true, type); })))
5107 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5111 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5112 { constant_boolean_node (true, type); })
5113 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5114 { constant_boolean_node (false, type); })))
5116 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5120 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5121 { constant_boolean_node (false, type); })
5122 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5123 { constant_boolean_node (true, type); })))
5125 /* bool_var != 0 becomes bool_var. */
5127 (ne @0 integer_zerop)
5128 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5129 && types_match (type, TREE_TYPE (@0)))
5131 /* bool_var == 1 becomes bool_var. */
5133 (eq @0 integer_onep)
5134 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5135 && types_match (type, TREE_TYPE (@0)))
5138 bool_var == 0 becomes !bool_var or
5139 bool_var != 1 becomes !bool_var
5140 here because that only is good in assignment context as long
5141 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5142 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5143 clearly less optimal and which we'll transform again in forwprop. */
5145 /* When one argument is a constant, overflow detection can be simplified.
5146 Currently restricted to single use so as not to interfere too much with
5147 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5148 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5149 (for cmp (lt le ge gt)
5152 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5153 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5154 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5155 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5156 && wi::to_wide (@1) != 0
5159 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5160 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5162 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5163 wi::max_value (prec, sign)
5164 - wi::to_wide (@1)); })))))
5166 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5167 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5168 expects the long form, so we restrict the transformation for now. */
5171 (cmp:c (minus@2 @0 @1) @0)
5172 (if (single_use (@2)
5173 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5174 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5177 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5180 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5181 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5182 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5185 /* Testing for overflow is unnecessary if we already know the result. */
5190 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5191 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5192 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5193 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5198 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5199 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5200 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5201 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5203 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5204 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5208 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5209 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5210 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5211 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5213 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5214 is at least twice as wide as type of A and B, simplify to
5215 __builtin_mul_overflow (A, B, <unused>). */
5218 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5220 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5221 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5222 && TYPE_UNSIGNED (TREE_TYPE (@0))
5223 && (TYPE_PRECISION (TREE_TYPE (@3))
5224 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5225 && tree_fits_uhwi_p (@2)
5226 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5227 && types_match (@0, @1)
5228 && type_has_mode_precision_p (TREE_TYPE (@0))
5229 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5230 != CODE_FOR_nothing))
5231 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5232 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5234 /* Simplification of math builtins. These rules must all be optimizations
5235 as well as IL simplifications. If there is a possibility that the new
5236 form could be a pessimization, the rule should go in the canonicalization
5237 section that follows this one.
5239 Rules can generally go in this section if they satisfy one of
5242 - the rule describes an identity
5244 - the rule replaces calls with something as simple as addition or
5247 - the rule contains unary calls only and simplifies the surrounding
5248 arithmetic. (The idea here is to exclude non-unary calls in which
5249 one operand is constant and in which the call is known to be cheap
5250 when the operand has that value.) */
5252 (if (flag_unsafe_math_optimizations)
5253 /* Simplify sqrt(x) * sqrt(x) -> x. */
5255 (mult (SQRT_ALL@1 @0) @1)
5256 (if (!tree_expr_maybe_signaling_nan_p (@0))
5259 (for op (plus minus)
5260 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5264 (rdiv (op @0 @2) @1)))
5266 (for cmp (lt le gt ge)
5267 neg_cmp (gt ge lt le)
5268 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5270 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5272 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5274 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5275 || (real_zerop (tem) && !real_zerop (@1))))
5277 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5279 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5280 (neg_cmp @0 { tem; })))))))
5282 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5283 (for root (SQRT CBRT)
5285 (mult (root:s @0) (root:s @1))
5286 (root (mult @0 @1))))
5288 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5289 (for exps (EXP EXP2 EXP10 POW10)
5291 (mult (exps:s @0) (exps:s @1))
5292 (exps (plus @0 @1))))
5294 /* Simplify a/root(b/c) into a*root(c/b). */
5295 (for root (SQRT CBRT)
5297 (rdiv @0 (root:s (rdiv:s @1 @2)))
5298 (mult @0 (root (rdiv @2 @1)))))
5300 /* Simplify x/expN(y) into x*expN(-y). */
5301 (for exps (EXP EXP2 EXP10 POW10)
5303 (rdiv @0 (exps:s @1))
5304 (mult @0 (exps (negate @1)))))
5306 (for logs (LOG LOG2 LOG10 LOG10)
5307 exps (EXP EXP2 EXP10 POW10)
5308 /* logN(expN(x)) -> x. */
5312 /* expN(logN(x)) -> x. */
5317 /* Optimize logN(func()) for various exponential functions. We
5318 want to determine the value "x" and the power "exponent" in
5319 order to transform logN(x**exponent) into exponent*logN(x). */
5320 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5321 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5324 (if (SCALAR_FLOAT_TYPE_P (type))
5330 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5331 x = build_real_truncate (type, dconst_e ());
5334 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5335 x = build_real (type, dconst2);
5339 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5341 REAL_VALUE_TYPE dconst10;
5342 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5343 x = build_real (type, dconst10);
5350 (mult (logs { x; }) @0)))))
5358 (if (SCALAR_FLOAT_TYPE_P (type))
5364 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5365 x = build_real (type, dconsthalf);
5368 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5369 x = build_real_truncate (type, dconst_third ());
5375 (mult { x; } (logs @0))))))
5377 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5378 (for logs (LOG LOG2 LOG10)
5382 (mult @1 (logs @0))))
5384 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5385 or if C is a positive power of 2,
5386 pow(C,x) -> exp2(log2(C)*x). */
5394 (pows REAL_CST@0 @1)
5395 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5396 && real_isfinite (TREE_REAL_CST_PTR (@0))
5397 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5398 the use_exp2 case until after vectorization. It seems actually
5399 beneficial for all constants to postpone this until later,
5400 because exp(log(C)*x), while faster, will have worse precision
5401 and if x folds into a constant too, that is unnecessary
5403 && canonicalize_math_after_vectorization_p ())
5405 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5406 bool use_exp2 = false;
5407 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5408 && value->cl == rvc_normal)
5410 REAL_VALUE_TYPE frac_rvt = *value;
5411 SET_REAL_EXP (&frac_rvt, 1);
5412 if (real_equal (&frac_rvt, &dconst1))
5417 (if (optimize_pow_to_exp (@0, @1))
5418 (exps (mult (logs @0) @1)))
5419 (exp2s (mult (log2s @0) @1)))))))
5422 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5424 exps (EXP EXP2 EXP10 POW10)
5425 logs (LOG LOG2 LOG10 LOG10)
5427 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5428 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5429 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5430 (exps (plus (mult (logs @0) @1) @2)))))
5435 exps (EXP EXP2 EXP10 POW10)
5436 /* sqrt(expN(x)) -> expN(x*0.5). */
5439 (exps (mult @0 { build_real (type, dconsthalf); })))
5440 /* cbrt(expN(x)) -> expN(x/3). */
5443 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5444 /* pow(expN(x), y) -> expN(x*y). */
5447 (exps (mult @0 @1))))
5449 /* tan(atan(x)) -> x. */
5456 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5460 copysigns (COPYSIGN)
5465 REAL_VALUE_TYPE r_cst;
5466 build_sinatan_real (&r_cst, type);
5467 tree t_cst = build_real (type, r_cst);
5468 tree t_one = build_one_cst (type);
5470 (if (SCALAR_FLOAT_TYPE_P (type))
5471 (cond (lt (abs @0) { t_cst; })
5472 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5473 (copysigns { t_one; } @0))))))
5475 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5479 copysigns (COPYSIGN)
5484 REAL_VALUE_TYPE r_cst;
5485 build_sinatan_real (&r_cst, type);
5486 tree t_cst = build_real (type, r_cst);
5487 tree t_one = build_one_cst (type);
5488 tree t_zero = build_zero_cst (type);
5490 (if (SCALAR_FLOAT_TYPE_P (type))
5491 (cond (lt (abs @0) { t_cst; })
5492 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5493 (copysigns { t_zero; } @0))))))
5495 (if (!flag_errno_math)
5496 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5501 (sinhs (atanhs:s @0))
5502 (with { tree t_one = build_one_cst (type); }
5503 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5505 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5510 (coshs (atanhs:s @0))
5511 (with { tree t_one = build_one_cst (type); }
5512 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5514 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5516 (CABS (complex:C @0 real_zerop@1))
5519 /* trunc(trunc(x)) -> trunc(x), etc. */
5520 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5524 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5525 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5527 (fns integer_valued_real_p@0)
5530 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5532 (HYPOT:c @0 real_zerop@1)
5535 /* pow(1,x) -> 1. */
5537 (POW real_onep@0 @1)
5541 /* copysign(x,x) -> x. */
5542 (COPYSIGN_ALL @0 @0)
5546 /* copysign(x,-x) -> -x. */
5547 (COPYSIGN_ALL @0 (negate@1 @0))
5551 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5552 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5555 (for scale (LDEXP SCALBN SCALBLN)
5556 /* ldexp(0, x) -> 0. */
5558 (scale real_zerop@0 @1)
5560 /* ldexp(x, 0) -> x. */
5562 (scale @0 integer_zerop@1)
5564 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5566 (scale REAL_CST@0 @1)
5567 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5570 /* Canonicalization of sequences of math builtins. These rules represent
5571 IL simplifications but are not necessarily optimizations.
5573 The sincos pass is responsible for picking "optimal" implementations
5574 of math builtins, which may be more complicated and can sometimes go
5575 the other way, e.g. converting pow into a sequence of sqrts.
5576 We only want to do these canonicalizations before the pass has run. */
5578 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5579 /* Simplify tan(x) * cos(x) -> sin(x). */
5581 (mult:c (TAN:s @0) (COS:s @0))
5584 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5586 (mult:c @0 (POW:s @0 REAL_CST@1))
5587 (if (!TREE_OVERFLOW (@1))
5588 (POW @0 (plus @1 { build_one_cst (type); }))))
5590 /* Simplify sin(x) / cos(x) -> tan(x). */
5592 (rdiv (SIN:s @0) (COS:s @0))
5595 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5597 (rdiv (SINH:s @0) (COSH:s @0))
5600 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5602 (rdiv (TANH:s @0) (SINH:s @0))
5603 (rdiv {build_one_cst (type);} (COSH @0)))
5605 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5607 (rdiv (COS:s @0) (SIN:s @0))
5608 (rdiv { build_one_cst (type); } (TAN @0)))
5610 /* Simplify sin(x) / tan(x) -> cos(x). */
5612 (rdiv (SIN:s @0) (TAN:s @0))
5613 (if (! HONOR_NANS (@0)
5614 && ! HONOR_INFINITIES (@0))
5617 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5619 (rdiv (TAN:s @0) (SIN:s @0))
5620 (if (! HONOR_NANS (@0)
5621 && ! HONOR_INFINITIES (@0))
5622 (rdiv { build_one_cst (type); } (COS @0))))
5624 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5626 (mult (POW:s @0 @1) (POW:s @0 @2))
5627 (POW @0 (plus @1 @2)))
5629 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5631 (mult (POW:s @0 @1) (POW:s @2 @1))
5632 (POW (mult @0 @2) @1))
5634 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5636 (mult (POWI:s @0 @1) (POWI:s @2 @1))
5637 (POWI (mult @0 @2) @1))
5639 /* Simplify pow(x,c) / x -> pow(x,c-1). */
5641 (rdiv (POW:s @0 REAL_CST@1) @0)
5642 (if (!TREE_OVERFLOW (@1))
5643 (POW @0 (minus @1 { build_one_cst (type); }))))
5645 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5647 (rdiv @0 (POW:s @1 @2))
5648 (mult @0 (POW @1 (negate @2))))
5653 /* sqrt(sqrt(x)) -> pow(x,1/4). */
5656 (pows @0 { build_real (type, dconst_quarter ()); }))
5657 /* sqrt(cbrt(x)) -> pow(x,1/6). */
5660 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5661 /* cbrt(sqrt(x)) -> pow(x,1/6). */
5664 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5665 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
5667 (cbrts (cbrts tree_expr_nonnegative_p@0))
5668 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5669 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
5671 (sqrts (pows @0 @1))
5672 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5673 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
5675 (cbrts (pows tree_expr_nonnegative_p@0 @1))
5676 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5677 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
5679 (pows (sqrts @0) @1)
5680 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5681 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
5683 (pows (cbrts tree_expr_nonnegative_p@0) @1)
5684 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5685 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
5687 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5688 (pows @0 (mult @1 @2))))
5690 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
5692 (CABS (complex @0 @0))
5693 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5695 /* hypot(x,x) -> fabs(x)*sqrt(2). */
5698 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5700 /* cexp(x+yi) -> exp(x)*cexpi(y). */
5705 (cexps compositional_complex@0)
5706 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
5708 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5709 (mult @1 (imagpart @2)))))))
5711 (if (canonicalize_math_p ())
5712 /* floor(x) -> trunc(x) if x is nonnegative. */
5713 (for floors (FLOOR_ALL)
5716 (floors tree_expr_nonnegative_p@0)
5719 (match double_value_p
5721 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5722 (for froms (BUILT_IN_TRUNCL
5734 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
5735 (if (optimize && canonicalize_math_p ())
5737 (froms (convert double_value_p@0))
5738 (convert (tos @0)))))
5740 (match float_value_p
5742 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5743 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5744 BUILT_IN_FLOORL BUILT_IN_FLOOR
5745 BUILT_IN_CEILL BUILT_IN_CEIL
5746 BUILT_IN_ROUNDL BUILT_IN_ROUND
5747 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5748 BUILT_IN_RINTL BUILT_IN_RINT)
5749 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5750 BUILT_IN_FLOORF BUILT_IN_FLOORF
5751 BUILT_IN_CEILF BUILT_IN_CEILF
5752 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5753 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5754 BUILT_IN_RINTF BUILT_IN_RINTF)
5755 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5757 (if (optimize && canonicalize_math_p ()
5758 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
5760 (froms (convert float_value_p@0))
5761 (convert (tos @0)))))
5763 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5764 tos (XFLOOR XCEIL XROUND XRINT)
5765 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
5766 (if (optimize && canonicalize_math_p ())
5768 (froms (convert double_value_p@0))
5771 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5772 XFLOOR XCEIL XROUND XRINT)
5773 tos (XFLOORF XCEILF XROUNDF XRINTF)
5774 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5776 (if (optimize && canonicalize_math_p ())
5778 (froms (convert float_value_p@0))
5781 (if (canonicalize_math_p ())
5782 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
5783 (for floors (IFLOOR LFLOOR LLFLOOR)
5785 (floors tree_expr_nonnegative_p@0)
5788 (if (canonicalize_math_p ())
5789 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
5790 (for fns (IFLOOR LFLOOR LLFLOOR
5792 IROUND LROUND LLROUND)
5794 (fns integer_valued_real_p@0)
5796 (if (!flag_errno_math)
5797 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
5798 (for rints (IRINT LRINT LLRINT)
5800 (rints integer_valued_real_p@0)
5803 (if (canonicalize_math_p ())
5804 (for ifn (IFLOOR ICEIL IROUND IRINT)
5805 lfn (LFLOOR LCEIL LROUND LRINT)
5806 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5807 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5808 sizeof (int) == sizeof (long). */
5809 (if (TYPE_PRECISION (integer_type_node)
5810 == TYPE_PRECISION (long_integer_type_node))
5813 (lfn:long_integer_type_node @0)))
5814 /* Canonicalize llround (x) to lround (x) on LP64 targets where
5815 sizeof (long long) == sizeof (long). */
5816 (if (TYPE_PRECISION (long_long_integer_type_node)
5817 == TYPE_PRECISION (long_integer_type_node))
5820 (lfn:long_integer_type_node @0)))))
5822 /* cproj(x) -> x if we're ignoring infinities. */
5825 (if (!HONOR_INFINITIES (type))
5828 /* If the real part is inf and the imag part is known to be
5829 nonnegative, return (inf + 0i). */
5831 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5832 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5833 { build_complex_inf (type, false); }))
5835 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
5837 (CPROJ (complex @0 REAL_CST@1))
5838 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5839 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5845 (pows @0 REAL_CST@1)
5847 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5848 REAL_VALUE_TYPE tmp;
5851 /* pow(x,0) -> 1. */
5852 (if (real_equal (value, &dconst0))
5853 { build_real (type, dconst1); })
5854 /* pow(x,1) -> x. */
5855 (if (real_equal (value, &dconst1))
5857 /* pow(x,-1) -> 1/x. */
5858 (if (real_equal (value, &dconstm1))
5859 (rdiv { build_real (type, dconst1); } @0))
5860 /* pow(x,0.5) -> sqrt(x). */
5861 (if (flag_unsafe_math_optimizations
5862 && canonicalize_math_p ()
5863 && real_equal (value, &dconsthalf))
5865 /* pow(x,1/3) -> cbrt(x). */
5866 (if (flag_unsafe_math_optimizations
5867 && canonicalize_math_p ()
5868 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5869 real_equal (value, &tmp)))
5872 /* powi(1,x) -> 1. */
5874 (POWI real_onep@0 @1)
5878 (POWI @0 INTEGER_CST@1)
5880 /* powi(x,0) -> 1. */
5881 (if (wi::to_wide (@1) == 0)
5882 { build_real (type, dconst1); })
5883 /* powi(x,1) -> x. */
5884 (if (wi::to_wide (@1) == 1)
5886 /* powi(x,-1) -> 1/x. */
5887 (if (wi::to_wide (@1) == -1)
5888 (rdiv { build_real (type, dconst1); } @0))))
5890 /* Narrowing of arithmetic and logical operations.
5892 These are conceptually similar to the transformations performed for
5893 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
5894 term we want to move all that code out of the front-ends into here. */
5896 /* Convert (outertype)((innertype0)a+(innertype1)b)
5897 into ((newtype)a+(newtype)b) where newtype
5898 is the widest mode from all of these. */
5899 (for op (plus minus mult rdiv)
5901 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5902 /* If we have a narrowing conversion of an arithmetic operation where
5903 both operands are widening conversions from the same type as the outer
5904 narrowing conversion. Then convert the innermost operands to a
5905 suitable unsigned type (to avoid introducing undefined behavior),
5906 perform the operation and convert the result to the desired type. */
5907 (if (INTEGRAL_TYPE_P (type)
5910 /* We check for type compatibility between @0 and @1 below,
5911 so there's no need to check that @2/@4 are integral types. */
5912 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5913 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5914 /* The precision of the type of each operand must match the
5915 precision of the mode of each operand, similarly for the
5917 && type_has_mode_precision_p (TREE_TYPE (@1))
5918 && type_has_mode_precision_p (TREE_TYPE (@2))
5919 && type_has_mode_precision_p (type)
5920 /* The inner conversion must be a widening conversion. */
5921 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5922 && types_match (@1, type)
5923 && (types_match (@1, @2)
5924 /* Or the second operand is const integer or converted const
5925 integer from valueize. */
5926 || TREE_CODE (@2) == INTEGER_CST))
5927 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5928 (op @1 (convert @2))
5929 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5930 (convert (op (convert:utype @1)
5931 (convert:utype @2)))))
5932 (if (FLOAT_TYPE_P (type)
5933 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5934 == DECIMAL_FLOAT_TYPE_P (type))
5935 (with { tree arg0 = strip_float_extensions (@1);
5936 tree arg1 = strip_float_extensions (@2);
5937 tree itype = TREE_TYPE (@0);
5938 tree ty1 = TREE_TYPE (arg0);
5939 tree ty2 = TREE_TYPE (arg1);
5940 enum tree_code code = TREE_CODE (itype); }
5941 (if (FLOAT_TYPE_P (ty1)
5942 && FLOAT_TYPE_P (ty2))
5943 (with { tree newtype = type;
5944 if (TYPE_MODE (ty1) == SDmode
5945 || TYPE_MODE (ty2) == SDmode
5946 || TYPE_MODE (type) == SDmode)
5947 newtype = dfloat32_type_node;
5948 if (TYPE_MODE (ty1) == DDmode
5949 || TYPE_MODE (ty2) == DDmode
5950 || TYPE_MODE (type) == DDmode)
5951 newtype = dfloat64_type_node;
5952 if (TYPE_MODE (ty1) == TDmode
5953 || TYPE_MODE (ty2) == TDmode
5954 || TYPE_MODE (type) == TDmode)
5955 newtype = dfloat128_type_node; }
5956 (if ((newtype == dfloat32_type_node
5957 || newtype == dfloat64_type_node
5958 || newtype == dfloat128_type_node)
5960 && types_match (newtype, type))
5961 (op (convert:newtype @1) (convert:newtype @2))
5962 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5964 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5966 /* Sometimes this transformation is safe (cannot
5967 change results through affecting double rounding
5968 cases) and sometimes it is not. If NEWTYPE is
5969 wider than TYPE, e.g. (float)((long double)double
5970 + (long double)double) converted to
5971 (float)(double + double), the transformation is
5972 unsafe regardless of the details of the types
5973 involved; double rounding can arise if the result
5974 of NEWTYPE arithmetic is a NEWTYPE value half way
5975 between two representable TYPE values but the
5976 exact value is sufficiently different (in the
5977 right direction) for this difference to be
5978 visible in ITYPE arithmetic. If NEWTYPE is the
5979 same as TYPE, however, the transformation may be
5980 safe depending on the types involved: it is safe
5981 if the ITYPE has strictly more than twice as many
5982 mantissa bits as TYPE, can represent infinities
5983 and NaNs if the TYPE can, and has sufficient
5984 exponent range for the product or ratio of two
5985 values representable in the TYPE to be within the
5986 range of normal values of ITYPE. */
5987 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5988 && (flag_unsafe_math_optimizations
5989 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5990 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5992 && !excess_precision_type (newtype)))
5993 && !types_match (itype, newtype))
5994 (convert:type (op (convert:newtype @1)
5995 (convert:newtype @2)))
6000 /* This is another case of narrowing, specifically when there's an outer
6001 BIT_AND_EXPR which masks off bits outside the type of the innermost
6002 operands. Like the previous case we have to convert the operands
6003 to unsigned types to avoid introducing undefined behavior for the
6004 arithmetic operation. */
6005 (for op (minus plus)
6007 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6008 (if (INTEGRAL_TYPE_P (type)
6009 /* We check for type compatibility between @0 and @1 below,
6010 so there's no need to check that @1/@3 are integral types. */
6011 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6012 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6013 /* The precision of the type of each operand must match the
6014 precision of the mode of each operand, similarly for the
6016 && type_has_mode_precision_p (TREE_TYPE (@0))
6017 && type_has_mode_precision_p (TREE_TYPE (@1))
6018 && type_has_mode_precision_p (type)
6019 /* The inner conversion must be a widening conversion. */
6020 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6021 && types_match (@0, @1)
6022 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6023 <= TYPE_PRECISION (TREE_TYPE (@0)))
6024 && (wi::to_wide (@4)
6025 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6026 true, TYPE_PRECISION (type))) == 0)
6027 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6028 (with { tree ntype = TREE_TYPE (@0); }
6029 (convert (bit_and (op @0 @1) (convert:ntype @4))))
6030 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6031 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6032 (convert:utype @4))))))))
6034 /* Transform (@0 < @1 and @0 < @2) to use min,
6035 (@0 > @1 and @0 > @2) to use max */
6036 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6037 op (lt le gt ge lt le gt ge )
6038 ext (min min max max max max min min )
6040 (logic (op:cs @0 @1) (op:cs @0 @2))
6041 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6042 && TREE_CODE (@0) != INTEGER_CST)
6043 (op @0 (ext @1 @2)))))
6046 /* signbit(x) -> 0 if x is nonnegative. */
6047 (SIGNBIT tree_expr_nonnegative_p@0)
6048 { integer_zero_node; })
6051 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6053 (if (!HONOR_SIGNED_ZEROS (@0))
6054 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6056 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6058 (for op (plus minus)
6061 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6062 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6063 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6064 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6065 && !TYPE_SATURATING (TREE_TYPE (@0)))
6066 (with { tree res = int_const_binop (rop, @2, @1); }
6067 (if (TREE_OVERFLOW (res)
6068 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6069 { constant_boolean_node (cmp == NE_EXPR, type); }
6070 (if (single_use (@3))
6071 (cmp @0 { TREE_OVERFLOW (res)
6072 ? drop_tree_overflow (res) : res; }))))))))
6073 (for cmp (lt le gt ge)
6074 (for op (plus minus)
6077 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6078 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6079 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6080 (with { tree res = int_const_binop (rop, @2, @1); }
6081 (if (TREE_OVERFLOW (res))
6083 fold_overflow_warning (("assuming signed overflow does not occur "
6084 "when simplifying conditional to constant"),
6085 WARN_STRICT_OVERFLOW_CONDITIONAL);
6086 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6087 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6088 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6089 TYPE_SIGN (TREE_TYPE (@1)))
6090 != (op == MINUS_EXPR);
6091 constant_boolean_node (less == ovf_high, type);
6093 (if (single_use (@3))
6096 fold_overflow_warning (("assuming signed overflow does not occur "
6097 "when changing X +- C1 cmp C2 to "
6099 WARN_STRICT_OVERFLOW_COMPARISON);
6101 (cmp @0 { res; })))))))))
6103 /* Canonicalizations of BIT_FIELD_REFs. */
6106 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6107 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6110 (BIT_FIELD_REF (view_convert @0) @1 @2)
6111 (BIT_FIELD_REF @0 @1 @2))
6114 (BIT_FIELD_REF @0 @1 integer_zerop)
6115 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6119 (BIT_FIELD_REF @0 @1 @2)
6121 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6122 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6124 (if (integer_zerop (@2))
6125 (view_convert (realpart @0)))
6126 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6127 (view_convert (imagpart @0)))))
6128 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6129 && INTEGRAL_TYPE_P (type)
6130 /* On GIMPLE this should only apply to register arguments. */
6131 && (! GIMPLE || is_gimple_reg (@0))
6132 /* A bit-field-ref that referenced the full argument can be stripped. */
6133 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6134 && integer_zerop (@2))
6135 /* Low-parts can be reduced to integral conversions.
6136 ??? The following doesn't work for PDP endian. */
6137 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6138 /* But only do this after vectorization. */
6139 && canonicalize_math_after_vectorization_p ()
6140 /* Don't even think about BITS_BIG_ENDIAN. */
6141 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6142 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6143 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6144 ? (TYPE_PRECISION (TREE_TYPE (@0))
6145 - TYPE_PRECISION (type))
6149 /* Simplify vector extracts. */
6152 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6153 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6154 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
6155 || (VECTOR_TYPE_P (type)
6156 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
6159 tree ctor = (TREE_CODE (@0) == SSA_NAME
6160 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6161 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6162 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6163 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6164 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6167 && (idx % width) == 0
6169 && known_le ((idx + n) / width,
6170 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6175 /* Constructor elements can be subvectors. */
6177 if (CONSTRUCTOR_NELTS (ctor) != 0)
6179 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6180 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6181 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6183 unsigned HOST_WIDE_INT elt, count, const_k;
6186 /* We keep an exact subset of the constructor elements. */
6187 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6188 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6189 { build_constructor (type, NULL); }
6191 (if (elt < CONSTRUCTOR_NELTS (ctor))
6192 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6193 { build_zero_cst (type); })
6194 /* We don't want to emit new CTORs unless the old one goes away.
6195 ??? Eventually allow this if the CTOR ends up constant or
6197 (if (single_use (@0))
6199 vec<constructor_elt, va_gc> *vals;
6200 vec_alloc (vals, count);
6201 for (unsigned i = 0;
6202 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6203 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
6204 CONSTRUCTOR_ELT (ctor, elt + i)->value);
6205 build_constructor (type, vals);
6207 /* The bitfield references a single constructor element. */
6208 (if (k.is_constant (&const_k)
6209 && idx + n <= (idx / const_k + 1) * const_k)
6211 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6212 { build_zero_cst (type); })
6214 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6215 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6216 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6218 /* Simplify a bit extraction from a bit insertion for the cases with
6219 the inserted element fully covering the extraction or the insertion
6220 not touching the extraction. */
6222 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6225 unsigned HOST_WIDE_INT isize;
6226 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6227 isize = TYPE_PRECISION (TREE_TYPE (@1));
6229 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6232 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6233 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6234 wi::to_wide (@ipos) + isize))
6235 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6237 - wi::to_wide (@ipos)); }))
6238 (if (wi::geu_p (wi::to_wide (@ipos),
6239 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6240 || wi::geu_p (wi::to_wide (@rpos),
6241 wi::to_wide (@ipos) + isize))
6242 (BIT_FIELD_REF @0 @rsize @rpos)))))
6244 (if (canonicalize_math_after_vectorization_p ())
6247 (fmas:c (negate @0) @1 @2)
6248 (IFN_FNMA @0 @1 @2))
6250 (fmas @0 @1 (negate @2))
6253 (fmas:c (negate @0) @1 (negate @2))
6254 (IFN_FNMS @0 @1 @2))
6256 (negate (fmas@3 @0 @1 @2))
6257 (if (single_use (@3))
6258 (IFN_FNMS @0 @1 @2))))
6261 (IFN_FMS:c (negate @0) @1 @2)
6262 (IFN_FNMS @0 @1 @2))
6264 (IFN_FMS @0 @1 (negate @2))
6267 (IFN_FMS:c (negate @0) @1 (negate @2))
6268 (IFN_FNMA @0 @1 @2))
6270 (negate (IFN_FMS@3 @0 @1 @2))
6271 (if (single_use (@3))
6272 (IFN_FNMA @0 @1 @2)))
6275 (IFN_FNMA:c (negate @0) @1 @2)
6278 (IFN_FNMA @0 @1 (negate @2))
6279 (IFN_FNMS @0 @1 @2))
6281 (IFN_FNMA:c (negate @0) @1 (negate @2))
6284 (negate (IFN_FNMA@3 @0 @1 @2))
6285 (if (single_use (@3))
6286 (IFN_FMS @0 @1 @2)))
6289 (IFN_FNMS:c (negate @0) @1 @2)
6292 (IFN_FNMS @0 @1 (negate @2))
6293 (IFN_FNMA @0 @1 @2))
6295 (IFN_FNMS:c (negate @0) @1 (negate @2))
6298 (negate (IFN_FNMS@3 @0 @1 @2))
6299 (if (single_use (@3))
6300 (IFN_FMA @0 @1 @2))))
6302 /* CLZ simplifications. */
6307 (op (clz:s @0) INTEGER_CST@1)
6308 (if (integer_zerop (@1))
6309 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6310 (with { tree stype = signed_type_for (TREE_TYPE (@0));
6311 HOST_WIDE_INT val = 0;
6312 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6313 /* Punt on hypothetical weird targets. */
6314 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6321 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6322 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
6323 (with { bool ok = true;
6324 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6325 /* Punt on hypothetical weird targets. */
6326 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6328 && val == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
6332 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (TREE_TYPE (@0)) - 1))
6333 (op @0 { build_one_cst (TREE_TYPE (@0)); })))))))
6335 /* POPCOUNT simplifications. */
6336 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6338 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6339 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6340 (POPCOUNT (bit_ior @0 @1))))
6342 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6343 (for popcount (POPCOUNT)
6344 (for cmp (le eq ne gt)
6347 (cmp (popcount @0) integer_zerop)
6348 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6350 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6352 (bit_and (POPCOUNT @0) integer_onep)
6355 /* PARITY simplifications. */
6356 /* parity(~X) is parity(X). */
6358 (PARITY (bit_not @0))
6361 /* parity(X)^parity(Y) is parity(X^Y). */
6363 (bit_xor (PARITY:s @0) (PARITY:s @1))
6364 (PARITY (bit_xor @0 @1)))
6366 /* Common POPCOUNT/PARITY simplifications. */
6367 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6368 (for pfun (POPCOUNT PARITY)
6371 (with { wide_int nz = tree_nonzero_bits (@0); }
6375 (if (wi::popcount (nz) == 1)
6376 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6377 (convert (rshift:utype (convert:utype @0)
6378 { build_int_cst (integer_type_node,
6379 wi::ctz (nz)); }))))))))
6382 /* 64- and 32-bits branchless implementations of popcount are detected:
6384 int popcount64c (uint64_t x)
6386 x -= (x >> 1) & 0x5555555555555555ULL;
6387 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6388 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6389 return (x * 0x0101010101010101ULL) >> 56;
6392 int popcount32c (uint32_t x)
6394 x -= (x >> 1) & 0x55555555;
6395 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6396 x = (x + (x >> 4)) & 0x0f0f0f0f;
6397 return (x * 0x01010101) >> 24;
6404 (rshift @8 INTEGER_CST@5)
6406 (bit_and @6 INTEGER_CST@7)
6410 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6416 /* Check constants and optab. */
6417 (with { unsigned prec = TYPE_PRECISION (type);
6418 int shift = (64 - prec) & 63;
6419 unsigned HOST_WIDE_INT c1
6420 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6421 unsigned HOST_WIDE_INT c2
6422 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6423 unsigned HOST_WIDE_INT c3
6424 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6425 unsigned HOST_WIDE_INT c4
6426 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6431 && TYPE_UNSIGNED (type)
6432 && integer_onep (@4)
6433 && wi::to_widest (@10) == 2
6434 && wi::to_widest (@5) == 4
6435 && wi::to_widest (@1) == prec - 8
6436 && tree_to_uhwi (@2) == c1
6437 && tree_to_uhwi (@3) == c2
6438 && tree_to_uhwi (@9) == c3
6439 && tree_to_uhwi (@7) == c3
6440 && tree_to_uhwi (@11) == c4
6441 && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6443 (convert (IFN_POPCOUNT:type @0)))))
6445 /* __builtin_ffs needs to deal on many targets with the possible zero
6446 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6447 should lead to better code. */
6449 (FFS tree_expr_nonzero_p@0)
6450 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6451 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6452 OPTIMIZE_FOR_SPEED))
6453 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6454 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6457 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6459 /* __builtin_ffs (X) == 0 -> X == 0.
6460 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6463 (cmp (ffs@2 @0) INTEGER_CST@1)
6464 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6466 (if (integer_zerop (@1))
6467 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6468 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6469 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6470 (if (single_use (@2))
6471 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6472 wi::mask (tree_to_uhwi (@1),
6474 { wide_int_to_tree (TREE_TYPE (@0),
6475 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6476 false, prec)); }))))))
6478 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
6482 bit_op (bit_and bit_ior)
6484 (cmp (ffs@2 @0) INTEGER_CST@1)
6485 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6487 (if (integer_zerop (@1))
6488 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6489 (if (tree_int_cst_sgn (@1) < 0)
6490 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6491 (if (wi::to_widest (@1) >= prec)
6492 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6493 (if (wi::to_widest (@1) == prec - 1)
6494 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
6495 wi::shifted_mask (prec - 1, 1,
6497 (if (single_use (@2))
6498 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
6500 { wide_int_to_tree (TREE_TYPE (@0),
6501 wi::mask (tree_to_uhwi (@1),
6503 { build_zero_cst (TREE_TYPE (@0)); }))))))))
6512 r = c ? a1 op a2 : b;
6514 if the target can do it in one go. This makes the operation conditional
6515 on c, so could drop potentially-trapping arithmetic, but that's a valid
6516 simplification if the result of the operation isn't needed.
6518 Avoid speculatively generating a stand-alone vector comparison
6519 on targets that might not support them. Any target implementing
6520 conditional internal functions must support the same comparisons
6521 inside and outside a VEC_COND_EXPR. */
6524 (for uncond_op (UNCOND_BINARY)
6525 cond_op (COND_BINARY)
6527 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
6528 (with { tree op_type = TREE_TYPE (@4); }
6529 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6530 && element_precision (type) == element_precision (op_type))
6531 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
6533 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
6534 (with { tree op_type = TREE_TYPE (@4); }
6535 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6536 && element_precision (type) == element_precision (op_type))
6537 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6539 /* Same for ternary operations. */
6540 (for uncond_op (UNCOND_TERNARY)
6541 cond_op (COND_TERNARY)
6543 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
6544 (with { tree op_type = TREE_TYPE (@5); }
6545 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6546 && element_precision (type) == element_precision (op_type))
6547 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
6549 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
6550 (with { tree op_type = TREE_TYPE (@5); }
6551 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6552 && element_precision (type) == element_precision (op_type))
6553 (view_convert (cond_op (bit_not @0) @2 @3 @4
6554 (view_convert:op_type @1)))))))
6557 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
6558 "else" value of an IFN_COND_*. */
6559 (for cond_op (COND_BINARY)
6561 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
6562 (with { tree op_type = TREE_TYPE (@3); }
6563 (if (element_precision (type) == element_precision (op_type))
6564 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
6566 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
6567 (with { tree op_type = TREE_TYPE (@5); }
6568 (if (inverse_conditions_p (@0, @2)
6569 && element_precision (type) == element_precision (op_type))
6570 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
6572 /* Same for ternary operations. */
6573 (for cond_op (COND_TERNARY)
6575 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
6576 (with { tree op_type = TREE_TYPE (@4); }
6577 (if (element_precision (type) == element_precision (op_type))
6578 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
6580 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
6581 (with { tree op_type = TREE_TYPE (@6); }
6582 (if (inverse_conditions_p (@0, @2)
6583 && element_precision (type) == element_precision (op_type))
6584 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
6586 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
6589 A: (@0 + @1 < @2) | (@2 + @1 < @0)
6590 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
6592 If pointers are known not to wrap, B checks whether @1 bytes starting
6593 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
6594 bytes. A is more efficiently tested as:
6596 A: (sizetype) (@0 + @1 - @2) > @1 * 2
6598 The equivalent expression for B is given by replacing @1 with @1 - 1:
6600 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
6602 @0 and @2 can be swapped in both expressions without changing the result.
6604 The folds rely on sizetype's being unsigned (which is always true)
6605 and on its being the same width as the pointer (which we have to check).
6607 The fold replaces two pointer_plus expressions, two comparisons and
6608 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
6609 the best case it's a saving of two operations. The A fold retains one
6610 of the original pointer_pluses, so is a win even if both pointer_pluses
6611 are used elsewhere. The B fold is a wash if both pointer_pluses are
6612 used elsewhere, since all we end up doing is replacing a comparison with
6613 a pointer_plus. We do still apply the fold under those circumstances
6614 though, in case applying it to other conditions eventually makes one of the
6615 pointer_pluses dead. */
6616 (for ior (truth_orif truth_or bit_ior)
6619 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
6620 (cmp:cs (pointer_plus@4 @2 @1) @0))
6621 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6622 && TYPE_OVERFLOW_WRAPS (sizetype)
6623 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
6624 /* Calculate the rhs constant. */
6625 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
6626 offset_int rhs = off * 2; }
6627 /* Always fails for negative values. */
6628 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
6629 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
6630 pick a canonical order. This increases the chances of using the
6631 same pointer_plus in multiple checks. */
6632 (with { bool swap_p = tree_swap_operands_p (@0, @2);
6633 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
6634 (if (cmp == LT_EXPR)
6635 (gt (convert:sizetype
6636 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
6637 { swap_p ? @0 : @2; }))
6639 (gt (convert:sizetype
6640 (pointer_diff:ssizetype
6641 (pointer_plus { swap_p ? @2 : @0; }
6642 { wide_int_to_tree (sizetype, off); })
6643 { swap_p ? @0 : @2; }))
6644 { rhs_tree; })))))))))
6646 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6648 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6649 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6650 (with { int i = single_nonzero_element (@1); }
6652 (with { tree elt = vector_cst_elt (@1, i);
6653 tree elt_type = TREE_TYPE (elt);
6654 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6655 tree size = bitsize_int (elt_bits);
6656 tree pos = bitsize_int (elt_bits * i); }
6659 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6663 (vec_perm @0 @1 VECTOR_CST@2)
6666 tree op0 = @0, op1 = @1, op2 = @2;
6668 /* Build a vector of integers from the tree mask. */
6669 vec_perm_builder builder;
6670 if (!tree_to_vec_perm_builder (&builder, op2))
6673 /* Create a vec_perm_indices for the integer vector. */
6674 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6675 bool single_arg = (op0 == op1);
6676 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6678 (if (sel.series_p (0, 1, 0, 1))
6680 (if (sel.series_p (0, 1, nelts, 1))
6686 if (sel.all_from_input_p (0))
6688 else if (sel.all_from_input_p (1))
6691 sel.rotate_inputs (1);
6693 else if (known_ge (poly_uint64 (sel[0]), nelts))
6695 std::swap (op0, op1);
6696 sel.rotate_inputs (1);
6700 tree cop0 = op0, cop1 = op1;
6701 if (TREE_CODE (op0) == SSA_NAME
6702 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6703 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6704 cop0 = gimple_assign_rhs1 (def);
6705 if (TREE_CODE (op1) == SSA_NAME
6706 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6707 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6708 cop1 = gimple_assign_rhs1 (def);
6712 (if ((TREE_CODE (cop0) == VECTOR_CST
6713 || TREE_CODE (cop0) == CONSTRUCTOR)
6714 && (TREE_CODE (cop1) == VECTOR_CST
6715 || TREE_CODE (cop1) == CONSTRUCTOR)
6716 && (t = fold_vec_perm (type, cop0, cop1, sel)))
6720 bool changed = (op0 == op1 && !single_arg);
6721 tree ins = NULL_TREE;
6724 /* See if the permutation is performing a single element
6725 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6726 in that case. But only if the vector mode is supported,
6727 otherwise this is invalid GIMPLE. */
6728 if (TYPE_MODE (type) != BLKmode
6729 && (TREE_CODE (cop0) == VECTOR_CST
6730 || TREE_CODE (cop0) == CONSTRUCTOR
6731 || TREE_CODE (cop1) == VECTOR_CST
6732 || TREE_CODE (cop1) == CONSTRUCTOR))
6734 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6737 /* After canonicalizing the first elt to come from the
6738 first vector we only can insert the first elt from
6739 the first vector. */
6741 if ((ins = fold_read_from_vector (cop0, sel[0])))
6744 /* The above can fail for two-element vectors which always
6745 appear to insert the first element, so try inserting
6746 into the second lane as well. For more than two
6747 elements that's wasted time. */
6748 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6750 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6751 for (at = 0; at < encoded_nelts; ++at)
6752 if (maybe_ne (sel[at], at))
6754 if (at < encoded_nelts
6755 && (known_eq (at + 1, nelts)
6756 || sel.series_p (at + 1, 1, at + 1, 1)))
6758 if (known_lt (poly_uint64 (sel[at]), nelts))
6759 ins = fold_read_from_vector (cop0, sel[at]);
6761 ins = fold_read_from_vector (cop1, sel[at] - nelts);
6766 /* Generate a canonical form of the selector. */
6767 if (!ins && sel.encoding () != builder)
6769 /* Some targets are deficient and fail to expand a single
6770 argument permutation while still allowing an equivalent
6771 2-argument version. */
6773 if (sel.ninputs () == 2
6774 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6775 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6778 vec_perm_indices sel2 (builder, 2, nelts);
6779 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6780 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6782 /* Not directly supported with either encoding,
6783 so use the preferred form. */
6784 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6786 if (!operand_equal_p (op2, oldop2, 0))
6791 (bit_insert { op0; } { ins; }
6792 { bitsize_int (at * vector_element_bits (type)); })
6794 (vec_perm { op0; } { op1; } { op2; }))))))))))
6796 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
6798 (match vec_same_elem_p
6800 (if (uniform_vector_p (@0))))
6802 (match vec_same_elem_p
6806 (vec_perm vec_same_elem_p@0 @0 @1)
6809 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6810 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6811 constant which when multiplied by a power of 2 contains a unique value
6812 in the top 5 or 6 bits. This is then indexed into a table which maps it
6813 to the number of trailing zeroes. */
6814 (match (ctz_table_index @1 @2 @3)
6815 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))