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-2019 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 /* As opposed to convert?, this still creates a single pattern, so
102 it is not a suitable replacement for convert? in all cases. */
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))))))
112 /* This one has to be last, or it shadows the others. */
113 (match (nop_convert @0)
116 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
117 ABSU_EXPR returns unsigned absolute value of the operand and the operand
118 of the ABSU_EXPR will have the corresponding signed type. */
119 (simplify (abs (convert @0))
120 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
121 && !TYPE_UNSIGNED (TREE_TYPE (@0))
122 && element_precision (type) > element_precision (TREE_TYPE (@0)))
123 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
124 (convert (absu:utype @0)))))
127 /* Simplifications of operations with one constant operand and
128 simplifications to constants or single values. */
130 (for op (plus pointer_plus minus bit_ior bit_xor)
132 (op @0 integer_zerop)
135 /* 0 +p index -> (type)index */
137 (pointer_plus integer_zerop @1)
138 (non_lvalue (convert @1)))
140 /* ptr - 0 -> (type)ptr */
142 (pointer_diff @0 integer_zerop)
145 /* See if ARG1 is zero and X + ARG1 reduces to X.
146 Likewise if the operands are reversed. */
148 (plus:c @0 real_zerop@1)
149 (if (fold_real_zero_addition_p (type, @1, 0))
152 /* See if ARG1 is zero and X - ARG1 reduces to X. */
154 (minus @0 real_zerop@1)
155 (if (fold_real_zero_addition_p (type, @1, 1))
158 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
159 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
160 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
161 if not -frounding-math. For sNaNs the first operation would raise
162 exceptions but turn the result into qNan, so the second operation
163 would not raise it. */
164 (for inner_op (plus minus)
165 (for outer_op (plus minus)
167 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
170 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
171 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
172 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
174 = ((outer_op == PLUS_EXPR)
175 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
176 (if (outer_plus && !inner_plus)
181 This is unsafe for certain floats even in non-IEEE formats.
182 In IEEE, it is unsafe because it does wrong for NaNs.
183 Also note that operand_equal_p is always false if an operand
187 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
188 { build_zero_cst (type); }))
190 (pointer_diff @@0 @0)
191 { build_zero_cst (type); })
194 (mult @0 integer_zerop@1)
197 /* Maybe fold x * 0 to 0. The expressions aren't the same
198 when x is NaN, since x * 0 is also NaN. Nor are they the
199 same in modes with signed zeros, since multiplying a
200 negative value by 0 gives -0, not +0. */
202 (mult @0 real_zerop@1)
203 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
206 /* In IEEE floating point, x*1 is not equivalent to x for snans.
207 Likewise for complex arithmetic with signed zeros. */
210 (if (!HONOR_SNANS (type)
211 && (!HONOR_SIGNED_ZEROS (type)
212 || !COMPLEX_FLOAT_TYPE_P (type)))
215 /* Transform x * -1.0 into -x. */
217 (mult @0 real_minus_onep)
218 (if (!HONOR_SNANS (type)
219 && (!HONOR_SIGNED_ZEROS (type)
220 || !COMPLEX_FLOAT_TYPE_P (type)))
223 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
225 (mult SSA_NAME@1 SSA_NAME@2)
226 (if (INTEGRAL_TYPE_P (type)
227 && get_nonzero_bits (@1) == 1
228 && get_nonzero_bits (@2) == 1)
231 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
232 unless the target has native support for the former but not the latter. */
234 (mult @0 VECTOR_CST@1)
235 (if (initializer_each_zero_or_onep (@1)
236 && !HONOR_SNANS (type)
237 && !HONOR_SIGNED_ZEROS (type))
238 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
240 && (!VECTOR_MODE_P (TYPE_MODE (type))
241 || (VECTOR_MODE_P (TYPE_MODE (itype))
242 && optab_handler (and_optab,
243 TYPE_MODE (itype)) != CODE_FOR_nothing)))
244 (view_convert (bit_and:itype (view_convert @0)
245 (ne @1 { build_zero_cst (type); })))))))
247 (for cmp (gt ge lt le)
248 outp (convert convert negate negate)
249 outn (negate negate convert convert)
250 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
251 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
252 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
253 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
255 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
256 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
257 && types_match (type, TREE_TYPE (@0)))
259 (if (types_match (type, float_type_node))
260 (BUILT_IN_COPYSIGNF @1 (outp @0)))
261 (if (types_match (type, double_type_node))
262 (BUILT_IN_COPYSIGN @1 (outp @0)))
263 (if (types_match (type, long_double_type_node))
264 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
265 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
266 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
267 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
268 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
270 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
271 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
272 && types_match (type, TREE_TYPE (@0)))
274 (if (types_match (type, float_type_node))
275 (BUILT_IN_COPYSIGNF @1 (outn @0)))
276 (if (types_match (type, double_type_node))
277 (BUILT_IN_COPYSIGN @1 (outn @0)))
278 (if (types_match (type, long_double_type_node))
279 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
281 /* Transform X * copysign (1.0, X) into abs(X). */
283 (mult:c @0 (COPYSIGN_ALL real_onep @0))
284 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
287 /* Transform X * copysign (1.0, -X) into -abs(X). */
289 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
290 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
293 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
295 (COPYSIGN_ALL REAL_CST@0 @1)
296 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
297 (COPYSIGN_ALL (negate @0) @1)))
299 /* X * 1, X / 1 -> X. */
300 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
305 /* (A / (1 << B)) -> (A >> B).
306 Only for unsigned A. For signed A, this would not preserve rounding
308 For example: (-1 / ( 1 << B)) != -1 >> B. */
310 (trunc_div @0 (lshift integer_onep@1 @2))
311 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
312 && (!VECTOR_TYPE_P (type)
313 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
314 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
317 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
318 undefined behavior in constexpr evaluation, and assuming that the division
319 traps enables better optimizations than these anyway. */
320 (for div (trunc_div ceil_div floor_div round_div exact_div)
321 /* 0 / X is always zero. */
323 (div integer_zerop@0 @1)
324 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
325 (if (!integer_zerop (@1))
329 (div @0 integer_minus_onep@1)
330 (if (!TYPE_UNSIGNED (type))
335 /* But not for 0 / 0 so that we can get the proper warnings and errors.
336 And not for _Fract types where we can't build 1. */
337 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
338 { build_one_cst (type); }))
339 /* X / abs (X) is X < 0 ? -1 : 1. */
342 (if (INTEGRAL_TYPE_P (type)
343 && TYPE_OVERFLOW_UNDEFINED (type))
344 (cond (lt @0 { build_zero_cst (type); })
345 { build_minus_one_cst (type); } { build_one_cst (type); })))
348 (div:C @0 (negate @0))
349 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
350 && TYPE_OVERFLOW_UNDEFINED (type))
351 { build_minus_one_cst (type); })))
353 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
354 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
357 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
358 && TYPE_UNSIGNED (type))
361 /* Combine two successive divisions. Note that combining ceil_div
362 and floor_div is trickier and combining round_div even more so. */
363 (for div (trunc_div exact_div)
365 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
367 wi::overflow_type overflow;
368 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
369 TYPE_SIGN (type), &overflow);
371 (if (div == EXACT_DIV_EXPR
372 || optimize_successive_divisions_p (@2, @3))
374 (div @0 { wide_int_to_tree (type, mul); })
375 (if (TYPE_UNSIGNED (type)
376 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
377 { build_zero_cst (type); }))))))
379 /* Combine successive multiplications. Similar to above, but handling
380 overflow is different. */
382 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
384 wi::overflow_type overflow;
385 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
386 TYPE_SIGN (type), &overflow);
388 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
389 otherwise undefined overflow implies that @0 must be zero. */
390 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
391 (mult @0 { wide_int_to_tree (type, mul); }))))
393 /* Optimize A / A to 1.0 if we don't care about
394 NaNs or Infinities. */
397 (if (FLOAT_TYPE_P (type)
398 && ! HONOR_NANS (type)
399 && ! HONOR_INFINITIES (type))
400 { build_one_cst (type); }))
402 /* Optimize -A / A to -1.0 if we don't care about
403 NaNs or Infinities. */
405 (rdiv:C @0 (negate @0))
406 (if (FLOAT_TYPE_P (type)
407 && ! HONOR_NANS (type)
408 && ! HONOR_INFINITIES (type))
409 { build_minus_one_cst (type); }))
411 /* PR71078: x / abs(x) -> copysign (1.0, x) */
413 (rdiv:C (convert? @0) (convert? (abs @0)))
414 (if (SCALAR_FLOAT_TYPE_P (type)
415 && ! HONOR_NANS (type)
416 && ! HONOR_INFINITIES (type))
418 (if (types_match (type, float_type_node))
419 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
420 (if (types_match (type, double_type_node))
421 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
422 (if (types_match (type, long_double_type_node))
423 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
425 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
428 (if (!HONOR_SNANS (type))
431 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
433 (rdiv @0 real_minus_onep)
434 (if (!HONOR_SNANS (type))
437 (if (flag_reciprocal_math)
438 /* Convert (A/B)/C to A/(B*C). */
440 (rdiv (rdiv:s @0 @1) @2)
441 (rdiv @0 (mult @1 @2)))
443 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
445 (rdiv @0 (mult:s @1 REAL_CST@2))
447 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
449 (rdiv (mult @0 { tem; } ) @1))))
451 /* Convert A/(B/C) to (A/B)*C */
453 (rdiv @0 (rdiv:s @1 @2))
454 (mult (rdiv @0 @1) @2)))
456 /* Simplify x / (- y) to -x / y. */
458 (rdiv @0 (negate @1))
459 (rdiv (negate @0) @1))
461 (if (flag_unsafe_math_optimizations)
462 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
463 Since C / x may underflow to zero, do this only for unsafe math. */
464 (for op (lt le gt ge)
467 (op (rdiv REAL_CST@0 @1) real_zerop@2)
468 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
470 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
472 /* For C < 0, use the inverted operator. */
473 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
476 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
477 (for div (trunc_div ceil_div floor_div round_div exact_div)
479 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
480 (if (integer_pow2p (@2)
481 && tree_int_cst_sgn (@2) > 0
482 && tree_nop_conversion_p (type, TREE_TYPE (@0))
483 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
485 { build_int_cst (integer_type_node,
486 wi::exact_log2 (wi::to_wide (@2))); }))))
488 /* If ARG1 is a constant, we can convert this to a multiply by the
489 reciprocal. This does not have the same rounding properties,
490 so only do this if -freciprocal-math. We can actually
491 always safely do it if ARG1 is a power of two, but it's hard to
492 tell if it is or not in a portable manner. */
493 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
497 (if (flag_reciprocal_math
500 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
502 (mult @0 { tem; } )))
503 (if (cst != COMPLEX_CST)
504 (with { tree inverse = exact_inverse (type, @1); }
506 (mult @0 { inverse; } ))))))))
508 (for mod (ceil_mod floor_mod round_mod trunc_mod)
509 /* 0 % X is always zero. */
511 (mod integer_zerop@0 @1)
512 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
513 (if (!integer_zerop (@1))
515 /* X % 1 is always zero. */
517 (mod @0 integer_onep)
518 { build_zero_cst (type); })
519 /* X % -1 is zero. */
521 (mod @0 integer_minus_onep@1)
522 (if (!TYPE_UNSIGNED (type))
523 { build_zero_cst (type); }))
527 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
528 (if (!integer_zerop (@0))
529 { build_zero_cst (type); }))
530 /* (X % Y) % Y is just X % Y. */
532 (mod (mod@2 @0 @1) @1)
534 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
536 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
537 (if (ANY_INTEGRAL_TYPE_P (type)
538 && TYPE_OVERFLOW_UNDEFINED (type)
539 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
541 { build_zero_cst (type); }))
542 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
543 modulo and comparison, since it is simpler and equivalent. */
546 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
547 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
548 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
549 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
551 /* X % -C is the same as X % C. */
553 (trunc_mod @0 INTEGER_CST@1)
554 (if (TYPE_SIGN (type) == SIGNED
555 && !TREE_OVERFLOW (@1)
556 && wi::neg_p (wi::to_wide (@1))
557 && !TYPE_OVERFLOW_TRAPS (type)
558 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
559 && !sign_bit_p (@1, @1))
560 (trunc_mod @0 (negate @1))))
562 /* X % -Y is the same as X % Y. */
564 (trunc_mod @0 (convert? (negate @1)))
565 (if (INTEGRAL_TYPE_P (type)
566 && !TYPE_UNSIGNED (type)
567 && !TYPE_OVERFLOW_TRAPS (type)
568 && tree_nop_conversion_p (type, TREE_TYPE (@1))
569 /* Avoid this transformation if X might be INT_MIN or
570 Y might be -1, because we would then change valid
571 INT_MIN % -(-1) into invalid INT_MIN % -1. */
572 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
573 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
575 (trunc_mod @0 (convert @1))))
577 /* X - (X / Y) * Y is the same as X % Y. */
579 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
580 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
581 (convert (trunc_mod @0 @1))))
583 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
584 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
585 Also optimize A % (C << N) where C is a power of 2,
586 to A & ((C << N) - 1). */
587 (match (power_of_two_cand @1)
589 (match (power_of_two_cand @1)
590 (lshift INTEGER_CST@1 @2))
591 (for mod (trunc_mod floor_mod)
593 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
594 (if ((TYPE_UNSIGNED (type)
595 || tree_expr_nonnegative_p (@0))
596 && tree_nop_conversion_p (type, TREE_TYPE (@3))
597 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
598 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
600 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
602 (trunc_div (mult @0 integer_pow2p@1) @1)
603 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
604 (bit_and @0 { wide_int_to_tree
605 (type, wi::mask (TYPE_PRECISION (type)
606 - wi::exact_log2 (wi::to_wide (@1)),
607 false, TYPE_PRECISION (type))); })))
609 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
611 (mult (trunc_div @0 integer_pow2p@1) @1)
612 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
613 (bit_and @0 (negate @1))))
615 /* Simplify (t * 2) / 2) -> t. */
616 (for div (trunc_div ceil_div floor_div round_div exact_div)
618 (div (mult:c @0 @1) @1)
619 (if (ANY_INTEGRAL_TYPE_P (type)
620 && TYPE_OVERFLOW_UNDEFINED (type))
624 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
629 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
632 (pows (op @0) REAL_CST@1)
633 (with { HOST_WIDE_INT n; }
634 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
636 /* Likewise for powi. */
639 (pows (op @0) INTEGER_CST@1)
640 (if ((wi::to_wide (@1) & 1) == 0)
642 /* Strip negate and abs from both operands of hypot. */
650 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
651 (for copysigns (COPYSIGN_ALL)
653 (copysigns (op @0) @1)
656 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
661 /* Convert absu(x)*absu(x) -> x*x. */
663 (mult (absu@1 @0) @1)
664 (mult (convert@2 @0) @2))
666 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
670 (coss (copysigns @0 @1))
673 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
677 (pows (copysigns @0 @2) REAL_CST@1)
678 (with { HOST_WIDE_INT n; }
679 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
681 /* Likewise for powi. */
685 (pows (copysigns @0 @2) INTEGER_CST@1)
686 (if ((wi::to_wide (@1) & 1) == 0)
691 /* hypot(copysign(x, y), z) -> hypot(x, z). */
693 (hypots (copysigns @0 @1) @2)
695 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
697 (hypots @0 (copysigns @1 @2))
700 /* copysign(x, CST) -> [-]abs (x). */
701 (for copysigns (COPYSIGN_ALL)
703 (copysigns @0 REAL_CST@1)
704 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
708 /* copysign(copysign(x, y), z) -> copysign(x, z). */
709 (for copysigns (COPYSIGN_ALL)
711 (copysigns (copysigns @0 @1) @2)
714 /* copysign(x,y)*copysign(x,y) -> x*x. */
715 (for copysigns (COPYSIGN_ALL)
717 (mult (copysigns@2 @0 @1) @2)
720 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
721 (for ccoss (CCOS CCOSH)
726 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
727 (for ops (conj negate)
733 /* Fold (a * (1 << b)) into (a << b) */
735 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
736 (if (! FLOAT_TYPE_P (type)
737 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
740 /* Fold (1 << (C - x)) where C = precision(type) - 1
741 into ((1 << C) >> x). */
743 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
744 (if (INTEGRAL_TYPE_P (type)
745 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
747 (if (TYPE_UNSIGNED (type))
748 (rshift (lshift @0 @2) @3)
750 { tree utype = unsigned_type_for (type); }
751 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
753 /* Fold (C1/X)*C2 into (C1*C2)/X. */
755 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
756 (if (flag_associative_math
759 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
761 (rdiv { tem; } @1)))))
763 /* Simplify ~X & X as zero. */
765 (bit_and:c (convert? @0) (convert? (bit_not @0)))
766 { build_zero_cst (type); })
768 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
770 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
771 (if (TYPE_UNSIGNED (type))
772 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
774 (for bitop (bit_and bit_ior)
776 /* PR35691: Transform
777 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
778 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
780 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
781 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
782 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
783 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
784 (cmp (bit_ior @0 (convert @1)) @2)))
786 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
787 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
789 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
790 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
791 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
792 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
793 (cmp (bit_and @0 (convert @1)) @2))))
795 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
797 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
798 (minus (bit_xor @0 @1) @1))
800 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
801 (if (~wi::to_wide (@2) == wi::to_wide (@1))
802 (minus (bit_xor @0 @1) @1)))
804 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
806 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
807 (minus @1 (bit_xor @0 @1)))
809 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
810 (for op (bit_ior bit_xor plus)
812 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
815 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
816 (if (~wi::to_wide (@2) == wi::to_wide (@1))
819 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
821 (bit_ior:c (bit_xor:c @0 @1) @0)
824 /* (a & ~b) | (a ^ b) --> a ^ b */
826 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
829 /* (a & ~b) ^ ~a --> ~(a & b) */
831 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
832 (bit_not (bit_and @0 @1)))
834 /* (~a & b) ^ a --> (a | b) */
836 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
839 /* (a | b) & ~(a ^ b) --> a & b */
841 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
844 /* a | ~(a ^ b) --> a | ~b */
846 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
847 (bit_ior @0 (bit_not @1)))
849 /* (a | b) | (a &^ b) --> a | b */
850 (for op (bit_and bit_xor)
852 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
855 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
857 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
860 /* ~(~a & b) --> a | ~b */
862 (bit_not (bit_and:cs (bit_not @0) @1))
863 (bit_ior @0 (bit_not @1)))
865 /* ~(~a | b) --> a & ~b */
867 (bit_not (bit_ior:cs (bit_not @0) @1))
868 (bit_and @0 (bit_not @1)))
870 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
873 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
874 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
875 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
879 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
880 ((A & N) + B) & M -> (A + B) & M
881 Similarly if (N & M) == 0,
882 ((A | N) + B) & M -> (A + B) & M
883 and for - instead of + (or unary - instead of +)
884 and/or ^ instead of |.
885 If B is constant and (B & M) == 0, fold into A & M. */
887 (for bitop (bit_and bit_ior bit_xor)
889 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
892 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
893 @3, @4, @1, ERROR_MARK, NULL_TREE,
896 (convert (bit_and (op (convert:utype { pmop[0]; })
897 (convert:utype { pmop[1]; }))
898 (convert:utype @2))))))
900 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
903 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
904 NULL_TREE, NULL_TREE, @1, bitop, @3,
907 (convert (bit_and (op (convert:utype { pmop[0]; })
908 (convert:utype { pmop[1]; }))
909 (convert:utype @2)))))))
911 (bit_and (op:s @0 @1) INTEGER_CST@2)
914 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
915 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
916 NULL_TREE, NULL_TREE, pmop); }
918 (convert (bit_and (op (convert:utype { pmop[0]; })
919 (convert:utype { pmop[1]; }))
920 (convert:utype @2)))))))
921 (for bitop (bit_and bit_ior bit_xor)
923 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
926 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
927 bitop, @2, @3, NULL_TREE, ERROR_MARK,
928 NULL_TREE, NULL_TREE, pmop); }
930 (convert (bit_and (negate (convert:utype { pmop[0]; }))
931 (convert:utype @1)))))))
933 /* X % Y is smaller than Y. */
936 (cmp (trunc_mod @0 @1) @1)
937 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
938 { constant_boolean_node (cmp == LT_EXPR, type); })))
941 (cmp @1 (trunc_mod @0 @1))
942 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
943 { constant_boolean_node (cmp == GT_EXPR, type); })))
947 (bit_ior @0 integer_all_onesp@1)
952 (bit_ior @0 integer_zerop)
957 (bit_and @0 integer_zerop@1)
963 (for op (bit_ior bit_xor plus)
965 (op:c (convert? @0) (convert? (bit_not @0)))
966 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
971 { build_zero_cst (type); })
973 /* Canonicalize X ^ ~0 to ~X. */
975 (bit_xor @0 integer_all_onesp@1)
980 (bit_and @0 integer_all_onesp)
983 /* x & x -> x, x | x -> x */
984 (for bitop (bit_and bit_ior)
989 /* x & C -> x if we know that x & ~C == 0. */
992 (bit_and SSA_NAME@0 INTEGER_CST@1)
993 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
994 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
998 /* x + (x & 1) -> (x + 1) & ~1 */
1000 (plus:c @0 (bit_and:s @0 integer_onep@1))
1001 (bit_and (plus @0 @1) (bit_not @1)))
1003 /* x & ~(x & y) -> x & ~y */
1004 /* x | ~(x | y) -> x | ~y */
1005 (for bitop (bit_and bit_ior)
1007 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1008 (bitop @0 (bit_not @1))))
1010 /* (~x & y) | ~(x | y) -> ~x */
1012 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1015 /* (x | y) ^ (x | ~y) -> ~x */
1017 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1020 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1022 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1023 (bit_not (bit_xor @0 @1)))
1025 /* (~x | y) ^ (x ^ y) -> x | ~y */
1027 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1028 (bit_ior @0 (bit_not @1)))
1030 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1032 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1033 (bit_not (bit_and @0 @1)))
1035 /* (x | y) & ~x -> y & ~x */
1036 /* (x & y) | ~x -> y | ~x */
1037 (for bitop (bit_and bit_ior)
1038 rbitop (bit_ior bit_and)
1040 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1043 /* (x & y) ^ (x | y) -> x ^ y */
1045 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1048 /* (x ^ y) ^ (x | y) -> x & y */
1050 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1053 /* (x & y) + (x ^ y) -> x | y */
1054 /* (x & y) | (x ^ y) -> x | y */
1055 /* (x & y) ^ (x ^ y) -> x | y */
1056 (for op (plus bit_ior bit_xor)
1058 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1061 /* (x & y) + (x | y) -> x + y */
1063 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1066 /* (x + y) - (x | y) -> x & y */
1068 (minus (plus @0 @1) (bit_ior @0 @1))
1069 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1070 && !TYPE_SATURATING (type))
1073 /* (x + y) - (x & y) -> x | y */
1075 (minus (plus @0 @1) (bit_and @0 @1))
1076 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1077 && !TYPE_SATURATING (type))
1080 /* (x | y) - (x ^ y) -> x & y */
1082 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1085 /* (x | y) - (x & y) -> x ^ y */
1087 (minus (bit_ior @0 @1) (bit_and @0 @1))
1090 /* (x | y) & ~(x & y) -> x ^ y */
1092 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1095 /* (x | y) & (~x ^ y) -> x & y */
1097 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1100 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1102 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1103 (bit_not (bit_xor @0 @1)))
1105 /* (~x | y) ^ (x | ~y) -> x ^ y */
1107 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1110 /* ~x & ~y -> ~(x | y)
1111 ~x | ~y -> ~(x & y) */
1112 (for op (bit_and bit_ior)
1113 rop (bit_ior bit_and)
1115 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1116 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1117 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1118 (bit_not (rop (convert @0) (convert @1))))))
1120 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1121 with a constant, and the two constants have no bits in common,
1122 we should treat this as a BIT_IOR_EXPR since this may produce more
1124 (for op (bit_xor plus)
1126 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1127 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1128 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1129 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1130 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1131 (bit_ior (convert @4) (convert @5)))))
1133 /* (X | Y) ^ X -> Y & ~ X*/
1135 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1136 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1137 (convert (bit_and @1 (bit_not @0)))))
1139 /* Convert ~X ^ ~Y to X ^ Y. */
1141 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1142 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1143 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1144 (bit_xor (convert @0) (convert @1))))
1146 /* Convert ~X ^ C to X ^ ~C. */
1148 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1149 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1150 (bit_xor (convert @0) (bit_not @1))))
1152 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1153 (for opo (bit_and bit_xor)
1154 opi (bit_xor bit_and)
1156 (opo:c (opi:cs @0 @1) @1)
1157 (bit_and (bit_not @0) @1)))
1159 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1160 operands are another bit-wise operation with a common input. If so,
1161 distribute the bit operations to save an operation and possibly two if
1162 constants are involved. For example, convert
1163 (A | B) & (A | C) into A | (B & C)
1164 Further simplification will occur if B and C are constants. */
1165 (for op (bit_and bit_ior bit_xor)
1166 rop (bit_ior bit_and bit_and)
1168 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1169 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1170 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1171 (rop (convert @0) (op (convert @1) (convert @2))))))
1173 /* Some simple reassociation for bit operations, also handled in reassoc. */
1174 /* (X & Y) & Y -> X & Y
1175 (X | Y) | Y -> X | Y */
1176 (for op (bit_and bit_ior)
1178 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1180 /* (X ^ Y) ^ Y -> X */
1182 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1184 /* (X & Y) & (X & Z) -> (X & Y) & Z
1185 (X | Y) | (X | Z) -> (X | Y) | Z */
1186 (for op (bit_and bit_ior)
1188 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1189 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1190 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1191 (if (single_use (@5) && single_use (@6))
1192 (op @3 (convert @2))
1193 (if (single_use (@3) && single_use (@4))
1194 (op (convert @1) @5))))))
1195 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1197 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1198 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1199 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1200 (bit_xor (convert @1) (convert @2))))
1202 /* Convert abs (abs (X)) into abs (X).
1203 also absu (absu (X)) into absu (X). */
1209 (absu (convert@2 (absu@1 @0)))
1210 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1213 /* Convert abs[u] (-X) -> abs[u] (X). */
1222 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1224 (abs tree_expr_nonnegative_p@0)
1228 (absu tree_expr_nonnegative_p@0)
1231 /* A few cases of fold-const.c negate_expr_p predicate. */
1232 (match negate_expr_p
1234 (if ((INTEGRAL_TYPE_P (type)
1235 && TYPE_UNSIGNED (type))
1236 || (!TYPE_OVERFLOW_SANITIZED (type)
1237 && may_negate_without_overflow_p (t)))))
1238 (match negate_expr_p
1240 (match negate_expr_p
1242 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1243 (match negate_expr_p
1245 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1246 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1248 (match negate_expr_p
1250 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1251 (match negate_expr_p
1253 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1254 || (FLOAT_TYPE_P (type)
1255 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1256 && !HONOR_SIGNED_ZEROS (type)))))
1258 /* (-A) * (-B) -> A * B */
1260 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1261 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1262 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1263 (mult (convert @0) (convert (negate @1)))))
1265 /* -(A + B) -> (-B) - A. */
1267 (negate (plus:c @0 negate_expr_p@1))
1268 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1269 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1270 (minus (negate @1) @0)))
1272 /* -(A - B) -> B - A. */
1274 (negate (minus @0 @1))
1275 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1276 || (FLOAT_TYPE_P (type)
1277 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1278 && !HONOR_SIGNED_ZEROS (type)))
1281 (negate (pointer_diff @0 @1))
1282 (if (TYPE_OVERFLOW_UNDEFINED (type))
1283 (pointer_diff @1 @0)))
1285 /* A - B -> A + (-B) if B is easily negatable. */
1287 (minus @0 negate_expr_p@1)
1288 (if (!FIXED_POINT_TYPE_P (type))
1289 (plus @0 (negate @1))))
1291 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1293 For bitwise binary operations apply operand conversions to the
1294 binary operation result instead of to the operands. This allows
1295 to combine successive conversions and bitwise binary operations.
1296 We combine the above two cases by using a conditional convert. */
1297 (for bitop (bit_and bit_ior bit_xor)
1299 (bitop (convert @0) (convert? @1))
1300 (if (((TREE_CODE (@1) == INTEGER_CST
1301 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1302 && int_fits_type_p (@1, TREE_TYPE (@0)))
1303 || types_match (@0, @1))
1304 /* ??? This transform conflicts with fold-const.c doing
1305 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1306 constants (if x has signed type, the sign bit cannot be set
1307 in c). This folds extension into the BIT_AND_EXPR.
1308 Restrict it to GIMPLE to avoid endless recursions. */
1309 && (bitop != BIT_AND_EXPR || GIMPLE)
1310 && (/* That's a good idea if the conversion widens the operand, thus
1311 after hoisting the conversion the operation will be narrower. */
1312 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1313 /* It's also a good idea if the conversion is to a non-integer
1315 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1316 /* Or if the precision of TO is not the same as the precision
1318 || !type_has_mode_precision_p (type)))
1319 (convert (bitop @0 (convert @1))))))
1321 (for bitop (bit_and bit_ior)
1322 rbitop (bit_ior bit_and)
1323 /* (x | y) & x -> x */
1324 /* (x & y) | x -> x */
1326 (bitop:c (rbitop:c @0 @1) @0)
1328 /* (~x | y) & x -> x & y */
1329 /* (~x & y) | x -> x | y */
1331 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1334 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1336 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1337 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1339 /* Combine successive equal operations with constants. */
1340 (for bitop (bit_and bit_ior bit_xor)
1342 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1343 (if (!CONSTANT_CLASS_P (@0))
1344 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1345 folded to a constant. */
1346 (bitop @0 (bitop @1 @2))
1347 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1348 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1349 the values involved are such that the operation can't be decided at
1350 compile time. Try folding one of @0 or @1 with @2 to see whether
1351 that combination can be decided at compile time.
1353 Keep the existing form if both folds fail, to avoid endless
1355 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1357 (bitop @1 { cst1; })
1358 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1360 (bitop @0 { cst2; }))))))))
1362 /* Try simple folding for X op !X, and X op X with the help
1363 of the truth_valued_p and logical_inverted_value predicates. */
1364 (match truth_valued_p
1366 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1367 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1368 (match truth_valued_p
1370 (match truth_valued_p
1373 (match (logical_inverted_value @0)
1375 (match (logical_inverted_value @0)
1376 (bit_not truth_valued_p@0))
1377 (match (logical_inverted_value @0)
1378 (eq @0 integer_zerop))
1379 (match (logical_inverted_value @0)
1380 (ne truth_valued_p@0 integer_truep))
1381 (match (logical_inverted_value @0)
1382 (bit_xor truth_valued_p@0 integer_truep))
1386 (bit_and:c @0 (logical_inverted_value @0))
1387 { build_zero_cst (type); })
1388 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1389 (for op (bit_ior bit_xor)
1391 (op:c truth_valued_p@0 (logical_inverted_value @0))
1392 { constant_boolean_node (true, type); }))
1393 /* X ==/!= !X is false/true. */
1396 (op:c truth_valued_p@0 (logical_inverted_value @0))
1397 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1401 (bit_not (bit_not @0))
1404 /* Convert ~ (-A) to A - 1. */
1406 (bit_not (convert? (negate @0)))
1407 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1408 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1409 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1411 /* Convert - (~A) to A + 1. */
1413 (negate (nop_convert (bit_not @0)))
1414 (plus (view_convert @0) { build_each_one_cst (type); }))
1416 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1418 (bit_not (convert? (minus @0 integer_each_onep)))
1419 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1420 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1421 (convert (negate @0))))
1423 (bit_not (convert? (plus @0 integer_all_onesp)))
1424 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1425 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1426 (convert (negate @0))))
1428 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1430 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1431 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1432 (convert (bit_xor @0 (bit_not @1)))))
1434 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1435 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1436 (convert (bit_xor @0 @1))))
1438 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1440 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1441 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1442 (bit_not (bit_xor (view_convert @0) @1))))
1444 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1446 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1447 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1449 /* Fold A - (A & B) into ~B & A. */
1451 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1452 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1453 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1454 (convert (bit_and (bit_not @1) @0))))
1456 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1457 (for cmp (gt lt ge le)
1459 (mult (convert (cmp @0 @1)) @2)
1460 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1462 /* For integral types with undefined overflow and C != 0 fold
1463 x * C EQ/NE y * C into x EQ/NE y. */
1466 (cmp (mult:c @0 @1) (mult:c @2 @1))
1467 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1468 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1469 && tree_expr_nonzero_p (@1))
1472 /* For integral types with wrapping overflow and C odd fold
1473 x * C EQ/NE y * C into x EQ/NE y. */
1476 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1477 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1478 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1479 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1482 /* For integral types with undefined overflow and C != 0 fold
1483 x * C RELOP y * C into:
1485 x RELOP y for nonnegative C
1486 y RELOP x for negative C */
1487 (for cmp (lt gt le ge)
1489 (cmp (mult:c @0 @1) (mult:c @2 @1))
1490 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1491 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1492 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1494 (if (TREE_CODE (@1) == INTEGER_CST
1495 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1498 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1502 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1503 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1504 && TYPE_UNSIGNED (TREE_TYPE (@0))
1505 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1506 && (wi::to_wide (@2)
1507 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1508 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1509 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1511 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1512 (for cmp (simple_comparison)
1514 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1515 (if (element_precision (@3) >= element_precision (@0)
1516 && types_match (@0, @1))
1517 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1518 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1520 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1523 tree utype = unsigned_type_for (TREE_TYPE (@0));
1525 (cmp (convert:utype @1) (convert:utype @0)))))
1526 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1527 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1531 tree utype = unsigned_type_for (TREE_TYPE (@0));
1533 (cmp (convert:utype @0) (convert:utype @1)))))))))
1535 /* X / C1 op C2 into a simple range test. */
1536 (for cmp (simple_comparison)
1538 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1539 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1540 && integer_nonzerop (@1)
1541 && !TREE_OVERFLOW (@1)
1542 && !TREE_OVERFLOW (@2))
1543 (with { tree lo, hi; bool neg_overflow;
1544 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1547 (if (code == LT_EXPR || code == GE_EXPR)
1548 (if (TREE_OVERFLOW (lo))
1549 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1550 (if (code == LT_EXPR)
1553 (if (code == LE_EXPR || code == GT_EXPR)
1554 (if (TREE_OVERFLOW (hi))
1555 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1556 (if (code == LE_EXPR)
1560 { build_int_cst (type, code == NE_EXPR); })
1561 (if (code == EQ_EXPR && !hi)
1563 (if (code == EQ_EXPR && !lo)
1565 (if (code == NE_EXPR && !hi)
1567 (if (code == NE_EXPR && !lo)
1570 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1574 tree etype = range_check_type (TREE_TYPE (@0));
1577 hi = fold_convert (etype, hi);
1578 lo = fold_convert (etype, lo);
1579 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1582 (if (etype && hi && !TREE_OVERFLOW (hi))
1583 (if (code == EQ_EXPR)
1584 (le (minus (convert:etype @0) { lo; }) { hi; })
1585 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1587 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1588 (for op (lt le ge gt)
1590 (op (plus:c @0 @2) (plus:c @1 @2))
1591 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1592 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1594 /* For equality and subtraction, this is also true with wrapping overflow. */
1595 (for op (eq ne minus)
1597 (op (plus:c @0 @2) (plus:c @1 @2))
1598 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1599 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1600 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1603 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1604 (for op (lt le ge gt)
1606 (op (minus @0 @2) (minus @1 @2))
1607 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1608 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1610 /* For equality and subtraction, this is also true with wrapping overflow. */
1611 (for op (eq ne minus)
1613 (op (minus @0 @2) (minus @1 @2))
1614 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1615 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1616 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1618 /* And for pointers... */
1619 (for op (simple_comparison)
1621 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1622 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1625 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1626 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1627 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1628 (pointer_diff @0 @1)))
1630 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1631 (for op (lt le ge gt)
1633 (op (minus @2 @0) (minus @2 @1))
1634 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1635 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1637 /* For equality and subtraction, this is also true with wrapping overflow. */
1638 (for op (eq ne minus)
1640 (op (minus @2 @0) (minus @2 @1))
1641 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1642 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1643 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1645 /* And for pointers... */
1646 (for op (simple_comparison)
1648 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1649 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1652 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1653 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1654 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1655 (pointer_diff @1 @0)))
1657 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1658 (for op (lt le gt ge)
1660 (op:c (plus:c@2 @0 @1) @1)
1661 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1662 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1663 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1664 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1665 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1666 /* For equality, this is also true with wrapping overflow. */
1669 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1670 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1671 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1672 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1673 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1674 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1675 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1676 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1678 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1679 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1680 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1681 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1682 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1684 /* X - Y < X is the same as Y > 0 when there is no overflow.
1685 For equality, this is also true with wrapping overflow. */
1686 (for op (simple_comparison)
1688 (op:c @0 (minus@2 @0 @1))
1689 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1690 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1691 || ((op == EQ_EXPR || op == NE_EXPR)
1692 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1693 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1694 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1697 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1698 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1702 (cmp (trunc_div @0 @1) integer_zerop)
1703 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1704 /* Complex ==/!= is allowed, but not </>=. */
1705 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1706 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1709 /* X == C - X can never be true if C is odd. */
1712 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1713 (if (TREE_INT_CST_LOW (@1) & 1)
1714 { constant_boolean_node (cmp == NE_EXPR, type); })))
1716 /* Arguments on which one can call get_nonzero_bits to get the bits
1718 (match with_possible_nonzero_bits
1720 (match with_possible_nonzero_bits
1722 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1723 /* Slightly extended version, do not make it recursive to keep it cheap. */
1724 (match (with_possible_nonzero_bits2 @0)
1725 with_possible_nonzero_bits@0)
1726 (match (with_possible_nonzero_bits2 @0)
1727 (bit_and:c with_possible_nonzero_bits@0 @2))
1729 /* Same for bits that are known to be set, but we do not have
1730 an equivalent to get_nonzero_bits yet. */
1731 (match (with_certain_nonzero_bits2 @0)
1733 (match (with_certain_nonzero_bits2 @0)
1734 (bit_ior @1 INTEGER_CST@0))
1736 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1739 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1740 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1741 { constant_boolean_node (cmp == NE_EXPR, type); })))
1743 /* ((X inner_op C0) outer_op C1)
1744 With X being a tree where value_range has reasoned certain bits to always be
1745 zero throughout its computed value range,
1746 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1747 where zero_mask has 1's for all bits that are sure to be 0 in
1749 if (inner_op == '^') C0 &= ~C1;
1750 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1751 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1753 (for inner_op (bit_ior bit_xor)
1754 outer_op (bit_xor bit_ior)
1757 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1761 wide_int zero_mask_not;
1765 if (TREE_CODE (@2) == SSA_NAME)
1766 zero_mask_not = get_nonzero_bits (@2);
1770 if (inner_op == BIT_XOR_EXPR)
1772 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1773 cst_emit = C0 | wi::to_wide (@1);
1777 C0 = wi::to_wide (@0);
1778 cst_emit = C0 ^ wi::to_wide (@1);
1781 (if (!fail && (C0 & zero_mask_not) == 0)
1782 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1783 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1784 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1786 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1788 (pointer_plus (pointer_plus:s @0 @1) @3)
1789 (pointer_plus @0 (plus @1 @3)))
1795 tem4 = (unsigned long) tem3;
1800 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1801 /* Conditionally look through a sign-changing conversion. */
1802 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1803 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1804 || (GENERIC && type == TREE_TYPE (@1))))
1807 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1808 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1812 tem = (sizetype) ptr;
1816 and produce the simpler and easier to analyze with respect to alignment
1817 ... = ptr & ~algn; */
1819 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1820 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1821 (bit_and @0 { algn; })))
1823 /* Try folding difference of addresses. */
1825 (minus (convert ADDR_EXPR@0) (convert @1))
1826 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1827 (with { poly_int64 diff; }
1828 (if (ptr_difference_const (@0, @1, &diff))
1829 { build_int_cst_type (type, diff); }))))
1831 (minus (convert @0) (convert ADDR_EXPR@1))
1832 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1833 (with { poly_int64 diff; }
1834 (if (ptr_difference_const (@0, @1, &diff))
1835 { build_int_cst_type (type, diff); }))))
1837 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1838 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1839 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1840 (with { poly_int64 diff; }
1841 (if (ptr_difference_const (@0, @1, &diff))
1842 { build_int_cst_type (type, diff); }))))
1844 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1845 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1846 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1847 (with { poly_int64 diff; }
1848 (if (ptr_difference_const (@0, @1, &diff))
1849 { build_int_cst_type (type, diff); }))))
1851 /* If arg0 is derived from the address of an object or function, we may
1852 be able to fold this expression using the object or function's
1855 (bit_and (convert? @0) INTEGER_CST@1)
1856 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1857 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1861 unsigned HOST_WIDE_INT bitpos;
1862 get_pointer_alignment_1 (@0, &align, &bitpos);
1864 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1865 { wide_int_to_tree (type, (wi::to_wide (@1)
1866 & (bitpos / BITS_PER_UNIT))); }))))
1869 /* We can't reassociate at all for saturating types. */
1870 (if (!TYPE_SATURATING (type))
1872 /* Contract negates. */
1873 /* A + (-B) -> A - B */
1875 (plus:c @0 (convert? (negate @1)))
1876 /* Apply STRIP_NOPS on the negate. */
1877 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1878 && !TYPE_OVERFLOW_SANITIZED (type))
1882 if (INTEGRAL_TYPE_P (type)
1883 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1884 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1886 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1887 /* A - (-B) -> A + B */
1889 (minus @0 (convert? (negate @1)))
1890 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1891 && !TYPE_OVERFLOW_SANITIZED (type))
1895 if (INTEGRAL_TYPE_P (type)
1896 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1897 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1899 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1901 Sign-extension is ok except for INT_MIN, which thankfully cannot
1902 happen without overflow. */
1904 (negate (convert (negate @1)))
1905 (if (INTEGRAL_TYPE_P (type)
1906 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1907 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1908 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1909 && !TYPE_OVERFLOW_SANITIZED (type)
1910 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1913 (negate (convert negate_expr_p@1))
1914 (if (SCALAR_FLOAT_TYPE_P (type)
1915 && ((DECIMAL_FLOAT_TYPE_P (type)
1916 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1917 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1918 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1919 (convert (negate @1))))
1921 (negate (nop_convert (negate @1)))
1922 (if (!TYPE_OVERFLOW_SANITIZED (type)
1923 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1926 /* We can't reassociate floating-point unless -fassociative-math
1927 or fixed-point plus or minus because of saturation to +-Inf. */
1928 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1929 && !FIXED_POINT_TYPE_P (type))
1931 /* Match patterns that allow contracting a plus-minus pair
1932 irrespective of overflow issues. */
1933 /* (A +- B) - A -> +- B */
1934 /* (A +- B) -+ B -> A */
1935 /* A - (A +- B) -> -+ B */
1936 /* A +- (B -+ A) -> +- B */
1938 (minus (plus:c @0 @1) @0)
1941 (minus (minus @0 @1) @0)
1944 (plus:c (minus @0 @1) @1)
1947 (minus @0 (plus:c @0 @1))
1950 (minus @0 (minus @0 @1))
1952 /* (A +- B) + (C - A) -> C +- B */
1953 /* (A + B) - (A - C) -> B + C */
1954 /* More cases are handled with comparisons. */
1956 (plus:c (plus:c @0 @1) (minus @2 @0))
1959 (plus:c (minus @0 @1) (minus @2 @0))
1962 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1963 (if (TYPE_OVERFLOW_UNDEFINED (type)
1964 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1965 (pointer_diff @2 @1)))
1967 (minus (plus:c @0 @1) (minus @0 @2))
1970 /* (A +- CST1) +- CST2 -> A + CST3
1971 Use view_convert because it is safe for vectors and equivalent for
1973 (for outer_op (plus minus)
1974 (for inner_op (plus minus)
1975 neg_inner_op (minus plus)
1977 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1979 /* If one of the types wraps, use that one. */
1980 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1981 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1982 forever if something doesn't simplify into a constant. */
1983 (if (!CONSTANT_CLASS_P (@0))
1984 (if (outer_op == PLUS_EXPR)
1985 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1986 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1987 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1988 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1989 (if (outer_op == PLUS_EXPR)
1990 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1991 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1992 /* If the constant operation overflows we cannot do the transform
1993 directly as we would introduce undefined overflow, for example
1994 with (a - 1) + INT_MIN. */
1995 (if (types_match (type, @0))
1996 (with { tree cst = const_binop (outer_op == inner_op
1997 ? PLUS_EXPR : MINUS_EXPR,
1999 (if (cst && !TREE_OVERFLOW (cst))
2000 (inner_op @0 { cst; } )
2001 /* X+INT_MAX+1 is X-INT_MIN. */
2002 (if (INTEGRAL_TYPE_P (type) && cst
2003 && wi::to_wide (cst) == wi::min_value (type))
2004 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2005 /* Last resort, use some unsigned type. */
2006 (with { tree utype = unsigned_type_for (type); }
2008 (view_convert (inner_op
2009 (view_convert:utype @0)
2011 { drop_tree_overflow (cst); }))))))))))))))
2013 /* (CST1 - A) +- CST2 -> CST3 - A */
2014 (for outer_op (plus minus)
2016 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
2017 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2018 (if (cst && !TREE_OVERFLOW (cst))
2019 (minus { cst; } @0)))))
2021 /* CST1 - (CST2 - A) -> CST3 + A */
2023 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
2024 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2025 (if (cst && !TREE_OVERFLOW (cst))
2026 (plus { cst; } @0))))
2028 /* ((T)(A)) + CST -> (T)(A + CST) */
2031 (plus (convert SSA_NAME@0) INTEGER_CST@1)
2032 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2033 && TREE_CODE (type) == INTEGER_TYPE
2034 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2035 && int_fits_type_p (@1, TREE_TYPE (@0)))
2036 /* Perform binary operation inside the cast if the constant fits
2037 and (A + CST)'s range does not overflow. */
2040 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2041 max_ovf = wi::OVF_OVERFLOW;
2042 tree inner_type = TREE_TYPE (@0);
2044 wide_int w1 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2045 TYPE_SIGN (inner_type));
2047 wide_int wmin0, wmax0;
2048 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2050 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2051 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2054 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2055 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2061 (plus:c (bit_not @0) @0)
2062 (if (!TYPE_OVERFLOW_TRAPS (type))
2063 { build_all_ones_cst (type); }))
2067 (plus (convert? (bit_not @0)) integer_each_onep)
2068 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2069 (negate (convert @0))))
2073 (minus (convert? (negate @0)) integer_each_onep)
2074 (if (!TYPE_OVERFLOW_TRAPS (type)
2075 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2076 (bit_not (convert @0))))
2080 (minus integer_all_onesp @0)
2083 /* (T)(P + A) - (T)P -> (T) A */
2085 (minus (convert (plus:c @@0 @1))
2087 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2088 /* For integer types, if A has a smaller type
2089 than T the result depends on the possible
2091 E.g. T=size_t, A=(unsigned)429497295, P>0.
2092 However, if an overflow in P + A would cause
2093 undefined behavior, we can assume that there
2095 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2096 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2099 (minus (convert (pointer_plus @@0 @1))
2101 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2102 /* For pointer types, if the conversion of A to the
2103 final type requires a sign- or zero-extension,
2104 then we have to punt - it is not defined which
2106 || (POINTER_TYPE_P (TREE_TYPE (@0))
2107 && TREE_CODE (@1) == INTEGER_CST
2108 && tree_int_cst_sign_bit (@1) == 0))
2111 (pointer_diff (pointer_plus @@0 @1) @0)
2112 /* The second argument of pointer_plus must be interpreted as signed, and
2113 thus sign-extended if necessary. */
2114 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2115 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2116 second arg is unsigned even when we need to consider it as signed,
2117 we don't want to diagnose overflow here. */
2118 (convert (view_convert:stype @1))))
2120 /* (T)P - (T)(P + A) -> -(T) A */
2122 (minus (convert? @0)
2123 (convert (plus:c @@0 @1)))
2124 (if (INTEGRAL_TYPE_P (type)
2125 && TYPE_OVERFLOW_UNDEFINED (type)
2126 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2127 (with { tree utype = unsigned_type_for (type); }
2128 (convert (negate (convert:utype @1))))
2129 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2130 /* For integer types, if A has a smaller type
2131 than T the result depends on the possible
2133 E.g. T=size_t, A=(unsigned)429497295, P>0.
2134 However, if an overflow in P + A would cause
2135 undefined behavior, we can assume that there
2137 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2138 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2139 (negate (convert @1)))))
2142 (convert (pointer_plus @@0 @1)))
2143 (if (INTEGRAL_TYPE_P (type)
2144 && TYPE_OVERFLOW_UNDEFINED (type)
2145 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2146 (with { tree utype = unsigned_type_for (type); }
2147 (convert (negate (convert:utype @1))))
2148 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2149 /* For pointer types, if the conversion of A to the
2150 final type requires a sign- or zero-extension,
2151 then we have to punt - it is not defined which
2153 || (POINTER_TYPE_P (TREE_TYPE (@0))
2154 && TREE_CODE (@1) == INTEGER_CST
2155 && tree_int_cst_sign_bit (@1) == 0))
2156 (negate (convert @1)))))
2158 (pointer_diff @0 (pointer_plus @@0 @1))
2159 /* The second argument of pointer_plus must be interpreted as signed, and
2160 thus sign-extended if necessary. */
2161 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2162 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2163 second arg is unsigned even when we need to consider it as signed,
2164 we don't want to diagnose overflow here. */
2165 (negate (convert (view_convert:stype @1)))))
2167 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2169 (minus (convert (plus:c @@0 @1))
2170 (convert (plus:c @0 @2)))
2171 (if (INTEGRAL_TYPE_P (type)
2172 && TYPE_OVERFLOW_UNDEFINED (type)
2173 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2174 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2175 (with { tree utype = unsigned_type_for (type); }
2176 (convert (minus (convert:utype @1) (convert:utype @2))))
2177 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2178 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2179 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2180 /* For integer types, if A has a smaller type
2181 than T the result depends on the possible
2183 E.g. T=size_t, A=(unsigned)429497295, P>0.
2184 However, if an overflow in P + A would cause
2185 undefined behavior, we can assume that there
2187 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2188 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2189 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2190 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2191 (minus (convert @1) (convert @2)))))
2193 (minus (convert (pointer_plus @@0 @1))
2194 (convert (pointer_plus @0 @2)))
2195 (if (INTEGRAL_TYPE_P (type)
2196 && TYPE_OVERFLOW_UNDEFINED (type)
2197 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2198 (with { tree utype = unsigned_type_for (type); }
2199 (convert (minus (convert:utype @1) (convert:utype @2))))
2200 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2201 /* For pointer types, if the conversion of A to the
2202 final type requires a sign- or zero-extension,
2203 then we have to punt - it is not defined which
2205 || (POINTER_TYPE_P (TREE_TYPE (@0))
2206 && TREE_CODE (@1) == INTEGER_CST
2207 && tree_int_cst_sign_bit (@1) == 0
2208 && TREE_CODE (@2) == INTEGER_CST
2209 && tree_int_cst_sign_bit (@2) == 0))
2210 (minus (convert @1) (convert @2)))))
2212 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2213 /* The second argument of pointer_plus must be interpreted as signed, and
2214 thus sign-extended if necessary. */
2215 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2216 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2217 second arg is unsigned even when we need to consider it as signed,
2218 we don't want to diagnose overflow here. */
2219 (minus (convert (view_convert:stype @1))
2220 (convert (view_convert:stype @2)))))))
2222 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2223 Modeled after fold_plusminus_mult_expr. */
2224 (if (!TYPE_SATURATING (type)
2225 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2226 (for plusminus (plus minus)
2228 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2229 (if ((!ANY_INTEGRAL_TYPE_P (type)
2230 || TYPE_OVERFLOW_WRAPS (type)
2231 || (INTEGRAL_TYPE_P (type)
2232 && tree_expr_nonzero_p (@0)
2233 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2234 /* If @1 +- @2 is constant require a hard single-use on either
2235 original operand (but not on both). */
2236 && (single_use (@3) || single_use (@4)))
2237 (mult (plusminus @1 @2) @0)))
2238 /* We cannot generate constant 1 for fract. */
2239 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2241 (plusminus @0 (mult:c@3 @0 @2))
2242 (if ((!ANY_INTEGRAL_TYPE_P (type)
2243 || TYPE_OVERFLOW_WRAPS (type)
2244 || (INTEGRAL_TYPE_P (type)
2245 && tree_expr_nonzero_p (@0)
2246 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2248 (mult (plusminus { build_one_cst (type); } @2) @0)))
2250 (plusminus (mult:c@3 @0 @2) @0)
2251 (if ((!ANY_INTEGRAL_TYPE_P (type)
2252 || TYPE_OVERFLOW_WRAPS (type)
2253 || (INTEGRAL_TYPE_P (type)
2254 && tree_expr_nonzero_p (@0)
2255 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2257 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2259 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2261 (for minmax (min max FMIN_ALL FMAX_ALL)
2265 /* min(max(x,y),y) -> y. */
2267 (min:c (max:c @0 @1) @1)
2269 /* max(min(x,y),y) -> y. */
2271 (max:c (min:c @0 @1) @1)
2273 /* max(a,-a) -> abs(a). */
2275 (max:c @0 (negate @0))
2276 (if (TREE_CODE (type) != COMPLEX_TYPE
2277 && (! ANY_INTEGRAL_TYPE_P (type)
2278 || TYPE_OVERFLOW_UNDEFINED (type)))
2280 /* min(a,-a) -> -abs(a). */
2282 (min:c @0 (negate @0))
2283 (if (TREE_CODE (type) != COMPLEX_TYPE
2284 && (! ANY_INTEGRAL_TYPE_P (type)
2285 || TYPE_OVERFLOW_UNDEFINED (type)))
2290 (if (INTEGRAL_TYPE_P (type)
2291 && TYPE_MIN_VALUE (type)
2292 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2294 (if (INTEGRAL_TYPE_P (type)
2295 && TYPE_MAX_VALUE (type)
2296 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2301 (if (INTEGRAL_TYPE_P (type)
2302 && TYPE_MAX_VALUE (type)
2303 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2305 (if (INTEGRAL_TYPE_P (type)
2306 && TYPE_MIN_VALUE (type)
2307 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2310 /* max (a, a + CST) -> a + CST where CST is positive. */
2311 /* max (a, a + CST) -> a where CST is negative. */
2313 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2314 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2315 (if (tree_int_cst_sgn (@1) > 0)
2319 /* min (a, a + CST) -> a where CST is positive. */
2320 /* min (a, a + CST) -> a + CST where CST is negative. */
2322 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2323 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2324 (if (tree_int_cst_sgn (@1) > 0)
2328 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2329 and the outer convert demotes the expression back to x's type. */
2330 (for minmax (min max)
2332 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2333 (if (INTEGRAL_TYPE_P (type)
2334 && types_match (@1, type) && int_fits_type_p (@2, type)
2335 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2336 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2337 (minmax @1 (convert @2)))))
2339 (for minmax (FMIN_ALL FMAX_ALL)
2340 /* If either argument is NaN, return the other one. Avoid the
2341 transformation if we get (and honor) a signalling NaN. */
2343 (minmax:c @0 REAL_CST@1)
2344 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2345 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2347 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2348 functions to return the numeric arg if the other one is NaN.
2349 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2350 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2351 worry about it either. */
2352 (if (flag_finite_math_only)
2359 /* min (-A, -B) -> -max (A, B) */
2360 (for minmax (min max FMIN_ALL FMAX_ALL)
2361 maxmin (max min FMAX_ALL FMIN_ALL)
2363 (minmax (negate:s@2 @0) (negate:s@3 @1))
2364 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2365 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2366 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2367 (negate (maxmin @0 @1)))))
2368 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2369 MAX (~X, ~Y) -> ~MIN (X, Y) */
2370 (for minmax (min max)
2373 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2374 (bit_not (maxmin @0 @1))))
2376 /* MIN (X, Y) == X -> X <= Y */
2377 (for minmax (min min max max)
2381 (cmp:c (minmax:c @0 @1) @0)
2382 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2384 /* MIN (X, 5) == 0 -> X == 0
2385 MIN (X, 5) == 7 -> false */
2388 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2389 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2390 TYPE_SIGN (TREE_TYPE (@0))))
2391 { constant_boolean_node (cmp == NE_EXPR, type); }
2392 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2393 TYPE_SIGN (TREE_TYPE (@0))))
2397 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2398 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2399 TYPE_SIGN (TREE_TYPE (@0))))
2400 { constant_boolean_node (cmp == NE_EXPR, type); }
2401 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2402 TYPE_SIGN (TREE_TYPE (@0))))
2404 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2405 (for minmax (min min max max min min max max )
2406 cmp (lt le gt ge gt ge lt le )
2407 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2409 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2410 (comb (cmp @0 @2) (cmp @1 @2))))
2412 /* Simplifications of shift and rotates. */
2414 (for rotate (lrotate rrotate)
2416 (rotate integer_all_onesp@0 @1)
2419 /* Optimize -1 >> x for arithmetic right shifts. */
2421 (rshift integer_all_onesp@0 @1)
2422 (if (!TYPE_UNSIGNED (type)
2423 && tree_expr_nonnegative_p (@1))
2426 /* Optimize (x >> c) << c into x & (-1<<c). */
2428 (lshift (rshift @0 INTEGER_CST@1) @1)
2429 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2430 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2432 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2435 (rshift (lshift @0 INTEGER_CST@1) @1)
2436 (if (TYPE_UNSIGNED (type)
2437 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2438 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2440 (for shiftrotate (lrotate rrotate lshift rshift)
2442 (shiftrotate @0 integer_zerop)
2445 (shiftrotate integer_zerop@0 @1)
2447 /* Prefer vector1 << scalar to vector1 << vector2
2448 if vector2 is uniform. */
2449 (for vec (VECTOR_CST CONSTRUCTOR)
2451 (shiftrotate @0 vec@1)
2452 (with { tree tem = uniform_vector_p (@1); }
2454 (shiftrotate @0 { tem; }))))))
2456 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2457 Y is 0. Similarly for X >> Y. */
2459 (for shift (lshift rshift)
2461 (shift @0 SSA_NAME@1)
2462 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2464 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2465 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2467 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2471 /* Rewrite an LROTATE_EXPR by a constant into an
2472 RROTATE_EXPR by a new constant. */
2474 (lrotate @0 INTEGER_CST@1)
2475 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2476 build_int_cst (TREE_TYPE (@1),
2477 element_precision (type)), @1); }))
2479 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2480 (for op (lrotate rrotate rshift lshift)
2482 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2483 (with { unsigned int prec = element_precision (type); }
2484 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2485 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2486 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2487 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2488 (with { unsigned int low = (tree_to_uhwi (@1)
2489 + tree_to_uhwi (@2)); }
2490 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2491 being well defined. */
2493 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2494 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2495 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2496 { build_zero_cst (type); }
2497 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2498 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2501 /* ((1 << A) & 1) != 0 -> A == 0
2502 ((1 << A) & 1) == 0 -> A != 0 */
2506 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2507 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2509 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2510 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2514 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2515 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2517 || (!integer_zerop (@2)
2518 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2519 { constant_boolean_node (cmp == NE_EXPR, type); }
2520 (if (!integer_zerop (@2)
2521 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2522 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2524 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2525 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2526 if the new mask might be further optimized. */
2527 (for shift (lshift rshift)
2529 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2531 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2532 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2533 && tree_fits_uhwi_p (@1)
2534 && tree_to_uhwi (@1) > 0
2535 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2538 unsigned int shiftc = tree_to_uhwi (@1);
2539 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2540 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2541 tree shift_type = TREE_TYPE (@3);
2544 if (shift == LSHIFT_EXPR)
2545 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2546 else if (shift == RSHIFT_EXPR
2547 && type_has_mode_precision_p (shift_type))
2549 prec = TYPE_PRECISION (TREE_TYPE (@3));
2551 /* See if more bits can be proven as zero because of
2554 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2556 tree inner_type = TREE_TYPE (@0);
2557 if (type_has_mode_precision_p (inner_type)
2558 && TYPE_PRECISION (inner_type) < prec)
2560 prec = TYPE_PRECISION (inner_type);
2561 /* See if we can shorten the right shift. */
2563 shift_type = inner_type;
2564 /* Otherwise X >> C1 is all zeros, so we'll optimize
2565 it into (X, 0) later on by making sure zerobits
2569 zerobits = HOST_WIDE_INT_M1U;
2572 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2573 zerobits <<= prec - shiftc;
2575 /* For arithmetic shift if sign bit could be set, zerobits
2576 can contain actually sign bits, so no transformation is
2577 possible, unless MASK masks them all away. In that
2578 case the shift needs to be converted into logical shift. */
2579 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2580 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2582 if ((mask & zerobits) == 0)
2583 shift_type = unsigned_type_for (TREE_TYPE (@3));
2589 /* ((X << 16) & 0xff00) is (X, 0). */
2590 (if ((mask & zerobits) == mask)
2591 { build_int_cst (type, 0); }
2592 (with { newmask = mask | zerobits; }
2593 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2596 /* Only do the transformation if NEWMASK is some integer
2598 for (prec = BITS_PER_UNIT;
2599 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2600 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2603 (if (prec < HOST_BITS_PER_WIDE_INT
2604 || newmask == HOST_WIDE_INT_M1U)
2606 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2607 (if (!tree_int_cst_equal (newmaskt, @2))
2608 (if (shift_type != TREE_TYPE (@3))
2609 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2610 (bit_and @4 { newmaskt; })))))))))))))
2612 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2613 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2614 (for shift (lshift rshift)
2615 (for bit_op (bit_and bit_xor bit_ior)
2617 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2618 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2619 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2620 (bit_op (shift (convert @0) @1) { mask; }))))))
2622 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2624 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2625 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2626 && (element_precision (TREE_TYPE (@0))
2627 <= element_precision (TREE_TYPE (@1))
2628 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2630 { tree shift_type = TREE_TYPE (@0); }
2631 (convert (rshift (convert:shift_type @1) @2)))))
2633 /* ~(~X >>r Y) -> X >>r Y
2634 ~(~X <<r Y) -> X <<r Y */
2635 (for rotate (lrotate rrotate)
2637 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2638 (if ((element_precision (TREE_TYPE (@0))
2639 <= element_precision (TREE_TYPE (@1))
2640 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2641 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2642 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2644 { tree rotate_type = TREE_TYPE (@0); }
2645 (convert (rotate (convert:rotate_type @1) @2))))))
2647 /* Simplifications of conversions. */
2649 /* Basic strip-useless-type-conversions / strip_nops. */
2650 (for cvt (convert view_convert float fix_trunc)
2653 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2654 || (GENERIC && type == TREE_TYPE (@0)))
2657 /* Contract view-conversions. */
2659 (view_convert (view_convert @0))
2662 /* For integral conversions with the same precision or pointer
2663 conversions use a NOP_EXPR instead. */
2666 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2667 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2668 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2671 /* Strip inner integral conversions that do not change precision or size, or
2672 zero-extend while keeping the same size (for bool-to-char). */
2674 (view_convert (convert@0 @1))
2675 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2676 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2677 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2678 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2679 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2680 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2683 /* Simplify a view-converted empty constructor. */
2685 (view_convert CONSTRUCTOR@0)
2686 (if (TREE_CODE (@0) != SSA_NAME
2687 && CONSTRUCTOR_NELTS (@0) == 0)
2688 { build_zero_cst (type); }))
2690 /* Re-association barriers around constants and other re-association
2691 barriers can be removed. */
2693 (paren CONSTANT_CLASS_P@0)
2696 (paren (paren@1 @0))
2699 /* Handle cases of two conversions in a row. */
2700 (for ocvt (convert float fix_trunc)
2701 (for icvt (convert float)
2706 tree inside_type = TREE_TYPE (@0);
2707 tree inter_type = TREE_TYPE (@1);
2708 int inside_int = INTEGRAL_TYPE_P (inside_type);
2709 int inside_ptr = POINTER_TYPE_P (inside_type);
2710 int inside_float = FLOAT_TYPE_P (inside_type);
2711 int inside_vec = VECTOR_TYPE_P (inside_type);
2712 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2713 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2714 int inter_int = INTEGRAL_TYPE_P (inter_type);
2715 int inter_ptr = POINTER_TYPE_P (inter_type);
2716 int inter_float = FLOAT_TYPE_P (inter_type);
2717 int inter_vec = VECTOR_TYPE_P (inter_type);
2718 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2719 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2720 int final_int = INTEGRAL_TYPE_P (type);
2721 int final_ptr = POINTER_TYPE_P (type);
2722 int final_float = FLOAT_TYPE_P (type);
2723 int final_vec = VECTOR_TYPE_P (type);
2724 unsigned int final_prec = TYPE_PRECISION (type);
2725 int final_unsignedp = TYPE_UNSIGNED (type);
2728 /* In addition to the cases of two conversions in a row
2729 handled below, if we are converting something to its own
2730 type via an object of identical or wider precision, neither
2731 conversion is needed. */
2732 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2734 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2735 && (((inter_int || inter_ptr) && final_int)
2736 || (inter_float && final_float))
2737 && inter_prec >= final_prec)
2740 /* Likewise, if the intermediate and initial types are either both
2741 float or both integer, we don't need the middle conversion if the
2742 former is wider than the latter and doesn't change the signedness
2743 (for integers). Avoid this if the final type is a pointer since
2744 then we sometimes need the middle conversion. */
2745 (if (((inter_int && inside_int) || (inter_float && inside_float))
2746 && (final_int || final_float)
2747 && inter_prec >= inside_prec
2748 && (inter_float || inter_unsignedp == inside_unsignedp))
2751 /* If we have a sign-extension of a zero-extended value, we can
2752 replace that by a single zero-extension. Likewise if the
2753 final conversion does not change precision we can drop the
2754 intermediate conversion. */
2755 (if (inside_int && inter_int && final_int
2756 && ((inside_prec < inter_prec && inter_prec < final_prec
2757 && inside_unsignedp && !inter_unsignedp)
2758 || final_prec == inter_prec))
2761 /* Two conversions in a row are not needed unless:
2762 - some conversion is floating-point (overstrict for now), or
2763 - some conversion is a vector (overstrict for now), or
2764 - the intermediate type is narrower than both initial and
2766 - the intermediate type and innermost type differ in signedness,
2767 and the outermost type is wider than the intermediate, or
2768 - the initial type is a pointer type and the precisions of the
2769 intermediate and final types differ, or
2770 - the final type is a pointer type and the precisions of the
2771 initial and intermediate types differ. */
2772 (if (! inside_float && ! inter_float && ! final_float
2773 && ! inside_vec && ! inter_vec && ! final_vec
2774 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2775 && ! (inside_int && inter_int
2776 && inter_unsignedp != inside_unsignedp
2777 && inter_prec < final_prec)
2778 && ((inter_unsignedp && inter_prec > inside_prec)
2779 == (final_unsignedp && final_prec > inter_prec))
2780 && ! (inside_ptr && inter_prec != final_prec)
2781 && ! (final_ptr && inside_prec != inter_prec))
2784 /* A truncation to an unsigned type (a zero-extension) should be
2785 canonicalized as bitwise and of a mask. */
2786 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2787 && final_int && inter_int && inside_int
2788 && final_prec == inside_prec
2789 && final_prec > inter_prec
2791 (convert (bit_and @0 { wide_int_to_tree
2793 wi::mask (inter_prec, false,
2794 TYPE_PRECISION (inside_type))); })))
2796 /* If we are converting an integer to a floating-point that can
2797 represent it exactly and back to an integer, we can skip the
2798 floating-point conversion. */
2799 (if (GIMPLE /* PR66211 */
2800 && inside_int && inter_float && final_int &&
2801 (unsigned) significand_size (TYPE_MODE (inter_type))
2802 >= inside_prec - !inside_unsignedp)
2805 /* If we have a narrowing conversion to an integral type that is fed by a
2806 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2807 masks off bits outside the final type (and nothing else). */
2809 (convert (bit_and @0 INTEGER_CST@1))
2810 (if (INTEGRAL_TYPE_P (type)
2811 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2812 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2813 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2814 TYPE_PRECISION (type)), 0))
2818 /* (X /[ex] A) * A -> X. */
2820 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2823 /* Simplify (A / B) * B + (A % B) -> A. */
2824 (for div (trunc_div ceil_div floor_div round_div)
2825 mod (trunc_mod ceil_mod floor_mod round_mod)
2827 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
2830 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
2831 (for op (plus minus)
2833 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2834 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2835 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
2838 wi::overflow_type overflow;
2839 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
2840 TYPE_SIGN (type), &overflow);
2842 (if (types_match (type, TREE_TYPE (@2))
2843 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
2844 (op @0 { wide_int_to_tree (type, mul); })
2845 (with { tree utype = unsigned_type_for (type); }
2846 (convert (op (convert:utype @0)
2847 (mult (convert:utype @1) (convert:utype @2))))))))))
2849 /* Canonicalization of binary operations. */
2851 /* Convert X + -C into X - C. */
2853 (plus @0 REAL_CST@1)
2854 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2855 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2856 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2857 (minus @0 { tem; })))))
2859 /* Convert x+x into x*2. */
2862 (if (SCALAR_FLOAT_TYPE_P (type))
2863 (mult @0 { build_real (type, dconst2); })
2864 (if (INTEGRAL_TYPE_P (type))
2865 (mult @0 { build_int_cst (type, 2); }))))
2869 (minus integer_zerop @1)
2872 (pointer_diff integer_zerop @1)
2873 (negate (convert @1)))
2875 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2876 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2877 (-ARG1 + ARG0) reduces to -ARG1. */
2879 (minus real_zerop@0 @1)
2880 (if (fold_real_zero_addition_p (type, @0, 0))
2883 /* Transform x * -1 into -x. */
2885 (mult @0 integer_minus_onep)
2888 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2889 signed overflow for CST != 0 && CST != -1. */
2891 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2892 (if (TREE_CODE (@2) != INTEGER_CST
2894 && !integer_zerop (@1) && !integer_minus_onep (@1))
2895 (mult (mult @0 @2) @1)))
2897 /* True if we can easily extract the real and imaginary parts of a complex
2899 (match compositional_complex
2900 (convert? (complex @0 @1)))
2902 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2904 (complex (realpart @0) (imagpart @0))
2907 (realpart (complex @0 @1))
2910 (imagpart (complex @0 @1))
2913 /* Sometimes we only care about half of a complex expression. */
2915 (realpart (convert?:s (conj:s @0)))
2916 (convert (realpart @0)))
2918 (imagpart (convert?:s (conj:s @0)))
2919 (convert (negate (imagpart @0))))
2920 (for part (realpart imagpart)
2921 (for op (plus minus)
2923 (part (convert?:s@2 (op:s @0 @1)))
2924 (convert (op (part @0) (part @1))))))
2926 (realpart (convert?:s (CEXPI:s @0)))
2929 (imagpart (convert?:s (CEXPI:s @0)))
2932 /* conj(conj(x)) -> x */
2934 (conj (convert? (conj @0)))
2935 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2938 /* conj({x,y}) -> {x,-y} */
2940 (conj (convert?:s (complex:s @0 @1)))
2941 (with { tree itype = TREE_TYPE (type); }
2942 (complex (convert:itype @0) (negate (convert:itype @1)))))
2944 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2945 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2950 (bswap (bit_not (bswap @0)))
2952 (for bitop (bit_xor bit_ior bit_and)
2954 (bswap (bitop:c (bswap @0) @1))
2955 (bitop @0 (bswap @1)))))
2958 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2960 /* Simplify constant conditions.
2961 Only optimize constant conditions when the selected branch
2962 has the same type as the COND_EXPR. This avoids optimizing
2963 away "c ? x : throw", where the throw has a void type.
2964 Note that we cannot throw away the fold-const.c variant nor
2965 this one as we depend on doing this transform before possibly
2966 A ? B : B -> B triggers and the fold-const.c one can optimize
2967 0 ? A : B to B even if A has side-effects. Something
2968 genmatch cannot handle. */
2970 (cond INTEGER_CST@0 @1 @2)
2971 (if (integer_zerop (@0))
2972 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2974 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2977 (vec_cond VECTOR_CST@0 @1 @2)
2978 (if (integer_all_onesp (@0))
2980 (if (integer_zerop (@0))
2983 /* Sink unary operations to constant branches, but only if we do fold it to
2985 (for op (negate bit_not abs absu)
2987 (op (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2))
2991 cst1 = const_unop (op, type, @1);
2993 cst2 = const_unop (op, type, @2);
2996 (vec_cond @0 { cst1; } { cst2; })))))
2998 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3000 /* This pattern implements two kinds simplification:
3003 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3004 1) Conversions are type widening from smaller type.
3005 2) Const c1 equals to c2 after canonicalizing comparison.
3006 3) Comparison has tree code LT, LE, GT or GE.
3007 This specific pattern is needed when (cmp (convert x) c) may not
3008 be simplified by comparison patterns because of multiple uses of
3009 x. It also makes sense here because simplifying across multiple
3010 referred var is always benefitial for complicated cases.
3013 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3014 (for cmp (lt le gt ge eq)
3016 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3019 tree from_type = TREE_TYPE (@1);
3020 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3021 enum tree_code code = ERROR_MARK;
3023 if (INTEGRAL_TYPE_P (from_type)
3024 && int_fits_type_p (@2, from_type)
3025 && (types_match (c1_type, from_type)
3026 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3027 && (TYPE_UNSIGNED (from_type)
3028 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3029 && (types_match (c2_type, from_type)
3030 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3031 && (TYPE_UNSIGNED (from_type)
3032 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3036 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3038 /* X <= Y - 1 equals to X < Y. */
3041 /* X > Y - 1 equals to X >= Y. */
3045 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3047 /* X < Y + 1 equals to X <= Y. */
3050 /* X >= Y + 1 equals to X > Y. */
3054 if (code != ERROR_MARK
3055 || wi::to_widest (@2) == wi::to_widest (@3))
3057 if (cmp == LT_EXPR || cmp == LE_EXPR)
3059 if (cmp == GT_EXPR || cmp == GE_EXPR)
3063 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3064 else if (int_fits_type_p (@3, from_type))
3068 (if (code == MAX_EXPR)
3069 (convert (max @1 (convert @2)))
3070 (if (code == MIN_EXPR)
3071 (convert (min @1 (convert @2)))
3072 (if (code == EQ_EXPR)
3073 (convert (cond (eq @1 (convert @3))
3074 (convert:from_type @3) (convert:from_type @2)))))))))
3076 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3078 1) OP is PLUS or MINUS.
3079 2) CMP is LT, LE, GT or GE.
3080 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3082 This pattern also handles special cases like:
3084 A) Operand x is a unsigned to signed type conversion and c1 is
3085 integer zero. In this case,
3086 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3087 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3088 B) Const c1 may not equal to (C3 op' C2). In this case we also
3089 check equality for (c1+1) and (c1-1) by adjusting comparison
3092 TODO: Though signed type is handled by this pattern, it cannot be
3093 simplified at the moment because C standard requires additional
3094 type promotion. In order to match&simplify it here, the IR needs
3095 to be cleaned up by other optimizers, i.e, VRP. */
3096 (for op (plus minus)
3097 (for cmp (lt le gt ge)
3099 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3100 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3101 (if (types_match (from_type, to_type)
3102 /* Check if it is special case A). */
3103 || (TYPE_UNSIGNED (from_type)
3104 && !TYPE_UNSIGNED (to_type)
3105 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3106 && integer_zerop (@1)
3107 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3110 wi::overflow_type overflow = wi::OVF_NONE;
3111 enum tree_code code, cmp_code = cmp;
3113 wide_int c1 = wi::to_wide (@1);
3114 wide_int c2 = wi::to_wide (@2);
3115 wide_int c3 = wi::to_wide (@3);
3116 signop sgn = TYPE_SIGN (from_type);
3118 /* Handle special case A), given x of unsigned type:
3119 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3120 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3121 if (!types_match (from_type, to_type))
3123 if (cmp_code == LT_EXPR)
3125 if (cmp_code == GE_EXPR)
3127 c1 = wi::max_value (to_type);
3129 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3130 compute (c3 op' c2) and check if it equals to c1 with op' being
3131 the inverted operator of op. Make sure overflow doesn't happen
3132 if it is undefined. */
3133 if (op == PLUS_EXPR)
3134 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3136 real_c1 = wi::add (c3, c2, sgn, &overflow);
3139 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3141 /* Check if c1 equals to real_c1. Boundary condition is handled
3142 by adjusting comparison operation if necessary. */
3143 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3146 /* X <= Y - 1 equals to X < Y. */
3147 if (cmp_code == LE_EXPR)
3149 /* X > Y - 1 equals to X >= Y. */
3150 if (cmp_code == GT_EXPR)
3153 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3156 /* X < Y + 1 equals to X <= Y. */
3157 if (cmp_code == LT_EXPR)
3159 /* X >= Y + 1 equals to X > Y. */
3160 if (cmp_code == GE_EXPR)
3163 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3165 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3167 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3172 (if (code == MAX_EXPR)
3173 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3174 { wide_int_to_tree (from_type, c2); })
3175 (if (code == MIN_EXPR)
3176 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3177 { wide_int_to_tree (from_type, c2); })))))))))
3179 (for cnd (cond vec_cond)
3180 /* A ? B : (A ? X : C) -> A ? B : C. */
3182 (cnd @0 (cnd @0 @1 @2) @3)
3185 (cnd @0 @1 (cnd @0 @2 @3))
3187 /* A ? B : (!A ? C : X) -> A ? B : C. */
3188 /* ??? This matches embedded conditions open-coded because genmatch
3189 would generate matching code for conditions in separate stmts only.
3190 The following is still important to merge then and else arm cases
3191 from if-conversion. */
3193 (cnd @0 @1 (cnd @2 @3 @4))
3194 (if (inverse_conditions_p (@0, @2))
3197 (cnd @0 (cnd @1 @2 @3) @4)
3198 (if (inverse_conditions_p (@0, @1))
3201 /* A ? B : B -> B. */
3206 /* !A ? B : C -> A ? C : B. */
3208 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3211 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3212 return all -1 or all 0 results. */
3213 /* ??? We could instead convert all instances of the vec_cond to negate,
3214 but that isn't necessarily a win on its own. */
3216 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3217 (if (VECTOR_TYPE_P (type)
3218 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3219 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3220 && (TYPE_MODE (TREE_TYPE (type))
3221 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3222 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3224 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3226 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3227 (if (VECTOR_TYPE_P (type)
3228 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3229 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3230 && (TYPE_MODE (TREE_TYPE (type))
3231 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3232 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3235 /* Simplifications of comparisons. */
3237 /* See if we can reduce the magnitude of a constant involved in a
3238 comparison by changing the comparison code. This is a canonicalization
3239 formerly done by maybe_canonicalize_comparison_1. */
3243 (cmp @0 uniform_integer_cst_p@1)
3244 (with { tree cst = uniform_integer_cst_p (@1); }
3245 (if (tree_int_cst_sgn (cst) == -1)
3246 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3247 wide_int_to_tree (TREE_TYPE (cst),
3253 (cmp @0 uniform_integer_cst_p@1)
3254 (with { tree cst = uniform_integer_cst_p (@1); }
3255 (if (tree_int_cst_sgn (cst) == 1)
3256 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3257 wide_int_to_tree (TREE_TYPE (cst),
3258 wi::to_wide (cst) - 1)); })))))
3260 /* We can simplify a logical negation of a comparison to the
3261 inverted comparison. As we cannot compute an expression
3262 operator using invert_tree_comparison we have to simulate
3263 that with expression code iteration. */
3264 (for cmp (tcc_comparison)
3265 icmp (inverted_tcc_comparison)
3266 ncmp (inverted_tcc_comparison_with_nans)
3267 /* Ideally we'd like to combine the following two patterns
3268 and handle some more cases by using
3269 (logical_inverted_value (cmp @0 @1))
3270 here but for that genmatch would need to "inline" that.
3271 For now implement what forward_propagate_comparison did. */
3273 (bit_not (cmp @0 @1))
3274 (if (VECTOR_TYPE_P (type)
3275 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3276 /* Comparison inversion may be impossible for trapping math,
3277 invert_tree_comparison will tell us. But we can't use
3278 a computed operator in the replacement tree thus we have
3279 to play the trick below. */
3280 (with { enum tree_code ic = invert_tree_comparison
3281 (cmp, HONOR_NANS (@0)); }
3287 (bit_xor (cmp @0 @1) integer_truep)
3288 (with { enum tree_code ic = invert_tree_comparison
3289 (cmp, HONOR_NANS (@0)); }
3295 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3296 ??? The transformation is valid for the other operators if overflow
3297 is undefined for the type, but performing it here badly interacts
3298 with the transformation in fold_cond_expr_with_comparison which
3299 attempts to synthetize ABS_EXPR. */
3301 (for sub (minus pointer_diff)
3303 (cmp (sub@2 @0 @1) integer_zerop)
3304 (if (single_use (@2))
3307 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3308 signed arithmetic case. That form is created by the compiler
3309 often enough for folding it to be of value. One example is in
3310 computing loop trip counts after Operator Strength Reduction. */
3311 (for cmp (simple_comparison)
3312 scmp (swapped_simple_comparison)
3314 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3315 /* Handle unfolded multiplication by zero. */
3316 (if (integer_zerop (@1))
3318 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3319 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3321 /* If @1 is negative we swap the sense of the comparison. */
3322 (if (tree_int_cst_sgn (@1) < 0)
3326 /* Simplify comparison of something with itself. For IEEE
3327 floating-point, we can only do some of these simplifications. */
3331 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3332 || ! HONOR_NANS (@0))
3333 { constant_boolean_node (true, type); }
3334 (if (cmp != EQ_EXPR)
3340 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3341 || ! HONOR_NANS (@0))
3342 { constant_boolean_node (false, type); })))
3343 (for cmp (unle unge uneq)
3346 { constant_boolean_node (true, type); }))
3347 (for cmp (unlt ungt)
3353 (if (!flag_trapping_math)
3354 { constant_boolean_node (false, type); }))
3356 /* Fold ~X op ~Y as Y op X. */
3357 (for cmp (simple_comparison)
3359 (cmp (bit_not@2 @0) (bit_not@3 @1))
3360 (if (single_use (@2) && single_use (@3))
3363 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3364 (for cmp (simple_comparison)
3365 scmp (swapped_simple_comparison)
3367 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3368 (if (single_use (@2)
3369 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3370 (scmp @0 (bit_not @1)))))
3372 (for cmp (simple_comparison)
3373 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3375 (cmp (convert@2 @0) (convert? @1))
3376 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3377 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3378 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3379 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3380 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3383 tree type1 = TREE_TYPE (@1);
3384 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3386 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3387 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3388 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3389 type1 = float_type_node;
3390 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3391 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3392 type1 = double_type_node;
3395 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3396 ? TREE_TYPE (@0) : type1);
3398 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3399 (cmp (convert:newtype @0) (convert:newtype @1))))))
3403 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3405 /* a CMP (-0) -> a CMP 0 */
3406 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3407 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3408 /* x != NaN is always true, other ops are always false. */
3409 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3410 && ! HONOR_SNANS (@1))
3411 { constant_boolean_node (cmp == NE_EXPR, type); })
3412 /* Fold comparisons against infinity. */
3413 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3414 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3417 REAL_VALUE_TYPE max;
3418 enum tree_code code = cmp;
3419 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3421 code = swap_tree_comparison (code);
3424 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3425 (if (code == GT_EXPR
3426 && !(HONOR_NANS (@0) && flag_trapping_math))
3427 { constant_boolean_node (false, type); })
3428 (if (code == LE_EXPR)
3429 /* x <= +Inf is always true, if we don't care about NaNs. */
3430 (if (! HONOR_NANS (@0))
3431 { constant_boolean_node (true, type); }
3432 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3433 an "invalid" exception. */
3434 (if (!flag_trapping_math)
3436 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3437 for == this introduces an exception for x a NaN. */
3438 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3440 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3442 (lt @0 { build_real (TREE_TYPE (@0), max); })
3443 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3444 /* x < +Inf is always equal to x <= DBL_MAX. */
3445 (if (code == LT_EXPR)
3446 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3448 (ge @0 { build_real (TREE_TYPE (@0), max); })
3449 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3450 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3451 an exception for x a NaN so use an unordered comparison. */
3452 (if (code == NE_EXPR)
3453 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3454 (if (! HONOR_NANS (@0))
3456 (ge @0 { build_real (TREE_TYPE (@0), max); })
3457 (le @0 { build_real (TREE_TYPE (@0), max); }))
3459 (unge @0 { build_real (TREE_TYPE (@0), max); })
3460 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3462 /* If this is a comparison of a real constant with a PLUS_EXPR
3463 or a MINUS_EXPR of a real constant, we can convert it into a
3464 comparison with a revised real constant as long as no overflow
3465 occurs when unsafe_math_optimizations are enabled. */
3466 (if (flag_unsafe_math_optimizations)
3467 (for op (plus minus)
3469 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3472 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3473 TREE_TYPE (@1), @2, @1);
3475 (if (tem && !TREE_OVERFLOW (tem))
3476 (cmp @0 { tem; }))))))
3478 /* Likewise, we can simplify a comparison of a real constant with
3479 a MINUS_EXPR whose first operand is also a real constant, i.e.
3480 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3481 floating-point types only if -fassociative-math is set. */
3482 (if (flag_associative_math)
3484 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3485 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3486 (if (tem && !TREE_OVERFLOW (tem))
3487 (cmp { tem; } @1)))))
3489 /* Fold comparisons against built-in math functions. */
3490 (if (flag_unsafe_math_optimizations
3491 && ! flag_errno_math)
3494 (cmp (sq @0) REAL_CST@1)
3496 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3498 /* sqrt(x) < y is always false, if y is negative. */
3499 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3500 { constant_boolean_node (false, type); })
3501 /* sqrt(x) > y is always true, if y is negative and we
3502 don't care about NaNs, i.e. negative values of x. */
3503 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3504 { constant_boolean_node (true, type); })
3505 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3506 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3507 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3509 /* sqrt(x) < 0 is always false. */
3510 (if (cmp == LT_EXPR)
3511 { constant_boolean_node (false, type); })
3512 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3513 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3514 { constant_boolean_node (true, type); })
3515 /* sqrt(x) <= 0 -> x == 0. */
3516 (if (cmp == LE_EXPR)
3518 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3519 == or !=. In the last case:
3521 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3523 if x is negative or NaN. Due to -funsafe-math-optimizations,
3524 the results for other x follow from natural arithmetic. */
3526 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3530 real_arithmetic (&c2, MULT_EXPR,
3531 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3532 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3534 (if (REAL_VALUE_ISINF (c2))
3535 /* sqrt(x) > y is x == +Inf, when y is very large. */
3536 (if (HONOR_INFINITIES (@0))
3537 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3538 { constant_boolean_node (false, type); })
3539 /* sqrt(x) > c is the same as x > c*c. */
3540 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3541 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3545 real_arithmetic (&c2, MULT_EXPR,
3546 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3547 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3549 (if (REAL_VALUE_ISINF (c2))
3551 /* sqrt(x) < y is always true, when y is a very large
3552 value and we don't care about NaNs or Infinities. */
3553 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3554 { constant_boolean_node (true, type); })
3555 /* sqrt(x) < y is x != +Inf when y is very large and we
3556 don't care about NaNs. */
3557 (if (! HONOR_NANS (@0))
3558 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3559 /* sqrt(x) < y is x >= 0 when y is very large and we
3560 don't care about Infinities. */
3561 (if (! HONOR_INFINITIES (@0))
3562 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3563 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3566 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3567 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3568 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3569 (if (! HONOR_NANS (@0))
3570 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3571 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3574 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3575 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3576 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3578 (cmp (sq @0) (sq @1))
3579 (if (! HONOR_NANS (@0))
3582 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
3583 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3584 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
3586 (cmp (float@0 @1) (float @2))
3587 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3588 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3591 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3592 tree type1 = TREE_TYPE (@1);
3593 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3594 tree type2 = TREE_TYPE (@2);
3595 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3597 (if (fmt.can_represent_integral_type_p (type1)
3598 && fmt.can_represent_integral_type_p (type2))
3599 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3600 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3601 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3602 && type1_signed_p >= type2_signed_p)
3603 (icmp @1 (convert @2))
3604 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3605 && type1_signed_p <= type2_signed_p)
3606 (icmp (convert:type2 @1) @2)
3607 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3608 && type1_signed_p == type2_signed_p)
3609 (icmp @1 @2))))))))))
3611 /* Optimize various special cases of (FTYPE) N CMP CST. */
3612 (for cmp (lt le eq ne ge gt)
3613 icmp (le le eq ne ge ge)
3615 (cmp (float @0) REAL_CST@1)
3616 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3617 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3620 tree itype = TREE_TYPE (@0);
3621 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3622 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3623 /* Be careful to preserve any potential exceptions due to
3624 NaNs. qNaNs are ok in == or != context.
3625 TODO: relax under -fno-trapping-math or
3626 -fno-signaling-nans. */
3628 = real_isnan (cst) && (cst->signalling
3629 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3631 /* TODO: allow non-fitting itype and SNaNs when
3632 -fno-trapping-math. */
3633 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
3636 signop isign = TYPE_SIGN (itype);
3637 REAL_VALUE_TYPE imin, imax;
3638 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3639 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3641 REAL_VALUE_TYPE icst;
3642 if (cmp == GT_EXPR || cmp == GE_EXPR)
3643 real_ceil (&icst, fmt, cst);
3644 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3645 real_floor (&icst, fmt, cst);
3647 real_trunc (&icst, fmt, cst);
3649 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3651 bool overflow_p = false;
3653 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3656 /* Optimize cases when CST is outside of ITYPE's range. */
3657 (if (real_compare (LT_EXPR, cst, &imin))
3658 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3660 (if (real_compare (GT_EXPR, cst, &imax))
3661 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3663 /* Remove cast if CST is an integer representable by ITYPE. */
3665 (cmp @0 { gcc_assert (!overflow_p);
3666 wide_int_to_tree (itype, icst_val); })
3668 /* When CST is fractional, optimize
3669 (FTYPE) N == CST -> 0
3670 (FTYPE) N != CST -> 1. */
3671 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3672 { constant_boolean_node (cmp == NE_EXPR, type); })
3673 /* Otherwise replace with sensible integer constant. */
3676 gcc_checking_assert (!overflow_p);
3678 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3680 /* Fold A /[ex] B CMP C to A CMP B * C. */
3683 (cmp (exact_div @0 @1) INTEGER_CST@2)
3684 (if (!integer_zerop (@1))
3685 (if (wi::to_wide (@2) == 0)
3687 (if (TREE_CODE (@1) == INTEGER_CST)
3690 wi::overflow_type ovf;
3691 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3692 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3695 { constant_boolean_node (cmp == NE_EXPR, type); }
3696 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3697 (for cmp (lt le gt ge)
3699 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3700 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3703 wi::overflow_type ovf;
3704 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3705 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3708 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3709 TYPE_SIGN (TREE_TYPE (@2)))
3710 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3711 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3713 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
3715 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
3716 For large C (more than min/B+2^size), this is also true, with the
3717 multiplication computed modulo 2^size.
3718 For intermediate C, this just tests the sign of A. */
3719 (for cmp (lt le gt ge)
3722 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
3723 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
3724 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
3725 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3728 tree utype = TREE_TYPE (@2);
3729 wide_int denom = wi::to_wide (@1);
3730 wide_int right = wi::to_wide (@2);
3731 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
3732 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
3733 bool small = wi::leu_p (right, smax);
3734 bool large = wi::geu_p (right, smin);
3736 (if (small || large)
3737 (cmp (convert:utype @0) (mult @2 (convert @1)))
3738 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
3740 /* Unordered tests if either argument is a NaN. */
3742 (bit_ior (unordered @0 @0) (unordered @1 @1))
3743 (if (types_match (@0, @1))
3746 (bit_and (ordered @0 @0) (ordered @1 @1))
3747 (if (types_match (@0, @1))
3750 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3753 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3756 /* Simple range test simplifications. */
3757 /* A < B || A >= B -> true. */
3758 (for test1 (lt le le le ne ge)
3759 test2 (ge gt ge ne eq ne)
3761 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3762 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3763 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3764 { constant_boolean_node (true, type); })))
3765 /* A < B && A >= B -> false. */
3766 (for test1 (lt lt lt le ne eq)
3767 test2 (ge gt eq gt eq gt)
3769 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3770 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3771 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3772 { constant_boolean_node (false, type); })))
3774 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3775 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3777 Note that comparisons
3778 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3779 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3780 will be canonicalized to above so there's no need to
3787 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3788 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3791 tree ty = TREE_TYPE (@0);
3792 unsigned prec = TYPE_PRECISION (ty);
3793 wide_int mask = wi::to_wide (@2, prec);
3794 wide_int rhs = wi::to_wide (@3, prec);
3795 signop sgn = TYPE_SIGN (ty);
3797 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3798 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3799 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3800 { build_zero_cst (ty); }))))))
3802 /* -A CMP -B -> B CMP A. */
3803 (for cmp (tcc_comparison)
3804 scmp (swapped_tcc_comparison)
3806 (cmp (negate @0) (negate @1))
3807 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3808 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3809 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3812 (cmp (negate @0) CONSTANT_CLASS_P@1)
3813 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3814 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3815 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3816 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3817 (if (tem && !TREE_OVERFLOW (tem))
3818 (scmp @0 { tem; }))))))
3820 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3823 (op (abs @0) zerop@1)
3826 /* From fold_sign_changed_comparison and fold_widened_comparison.
3827 FIXME: the lack of symmetry is disturbing. */
3828 (for cmp (simple_comparison)
3830 (cmp (convert@0 @00) (convert?@1 @10))
3831 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3832 /* Disable this optimization if we're casting a function pointer
3833 type on targets that require function pointer canonicalization. */
3834 && !(targetm.have_canonicalize_funcptr_for_compare ()
3835 && ((POINTER_TYPE_P (TREE_TYPE (@00))
3836 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3837 || (POINTER_TYPE_P (TREE_TYPE (@10))
3838 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
3840 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3841 && (TREE_CODE (@10) == INTEGER_CST
3843 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3846 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3847 /* ??? The special-casing of INTEGER_CST conversion was in the original
3848 code and here to avoid a spurious overflow flag on the resulting
3849 constant which fold_convert produces. */
3850 (if (TREE_CODE (@1) == INTEGER_CST)
3851 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3852 TREE_OVERFLOW (@1)); })
3853 (cmp @00 (convert @1)))
3855 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3856 /* If possible, express the comparison in the shorter mode. */
3857 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3858 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3859 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3860 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3861 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3862 || ((TYPE_PRECISION (TREE_TYPE (@00))
3863 >= TYPE_PRECISION (TREE_TYPE (@10)))
3864 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3865 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3866 || (TREE_CODE (@10) == INTEGER_CST
3867 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3868 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3869 (cmp @00 (convert @10))
3870 (if (TREE_CODE (@10) == INTEGER_CST
3871 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3872 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3875 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3876 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3877 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3878 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3880 (if (above || below)
3881 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3882 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3883 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3884 { constant_boolean_node (above ? true : false, type); }
3885 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3886 { constant_boolean_node (above ? false : true, type); }))))))))))))
3889 /* A local variable can never be pointed to by
3890 the default SSA name of an incoming parameter.
3891 SSA names are canonicalized to 2nd place. */
3893 (cmp addr@0 SSA_NAME@1)
3894 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3895 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3896 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3897 (if (TREE_CODE (base) == VAR_DECL
3898 && auto_var_in_fn_p (base, current_function_decl))
3899 (if (cmp == NE_EXPR)
3900 { constant_boolean_node (true, type); }
3901 { constant_boolean_node (false, type); }))))))
3903 /* Equality compare simplifications from fold_binary */
3906 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3907 Similarly for NE_EXPR. */
3909 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3910 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3911 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3912 { constant_boolean_node (cmp == NE_EXPR, type); }))
3914 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3916 (cmp (bit_xor @0 @1) integer_zerop)
3919 /* (X ^ Y) == Y becomes X == 0.
3920 Likewise (X ^ Y) == X becomes Y == 0. */
3922 (cmp:c (bit_xor:c @0 @1) @0)
3923 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3925 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3927 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3928 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3929 (cmp @0 (bit_xor @1 (convert @2)))))
3932 (cmp (convert? addr@0) integer_zerop)
3933 (if (tree_single_nonzero_warnv_p (@0, NULL))
3934 { constant_boolean_node (cmp == NE_EXPR, type); })))
3936 /* If we have (A & C) == C where C is a power of 2, convert this into
3937 (A & C) != 0. Similarly for NE_EXPR. */
3941 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3942 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3944 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3945 convert this into a shift followed by ANDing with D. */
3948 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3949 INTEGER_CST@2 integer_zerop)
3950 (if (integer_pow2p (@2))
3952 int shift = (wi::exact_log2 (wi::to_wide (@2))
3953 - wi::exact_log2 (wi::to_wide (@1)));
3957 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3959 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3962 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3963 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3967 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3968 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3969 && type_has_mode_precision_p (TREE_TYPE (@0))
3970 && element_precision (@2) >= element_precision (@0)
3971 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3972 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3973 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3975 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3976 this into a right shift or sign extension followed by ANDing with C. */
3979 (lt @0 integer_zerop)
3980 INTEGER_CST@1 integer_zerop)
3981 (if (integer_pow2p (@1)
3982 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3984 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3988 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3990 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3991 sign extension followed by AND with C will achieve the effect. */
3992 (bit_and (convert @0) @1)))))
3994 /* When the addresses are not directly of decls compare base and offset.
3995 This implements some remaining parts of fold_comparison address
3996 comparisons but still no complete part of it. Still it is good
3997 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3998 (for cmp (simple_comparison)
4000 (cmp (convert1?@2 addr@0) (convert2? addr@1))
4003 poly_int64 off0, off1;
4004 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4005 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4006 if (base0 && TREE_CODE (base0) == MEM_REF)
4008 off0 += mem_ref_offset (base0).force_shwi ();
4009 base0 = TREE_OPERAND (base0, 0);
4011 if (base1 && TREE_CODE (base1) == MEM_REF)
4013 off1 += mem_ref_offset (base1).force_shwi ();
4014 base1 = TREE_OPERAND (base1, 0);
4017 (if (base0 && base1)
4021 /* Punt in GENERIC on variables with value expressions;
4022 the value expressions might point to fields/elements
4023 of other vars etc. */
4025 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4026 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4028 else if (decl_in_symtab_p (base0)
4029 && decl_in_symtab_p (base1))
4030 equal = symtab_node::get_create (base0)
4031 ->equal_address_to (symtab_node::get_create (base1));
4032 else if ((DECL_P (base0)
4033 || TREE_CODE (base0) == SSA_NAME
4034 || TREE_CODE (base0) == STRING_CST)
4036 || TREE_CODE (base1) == SSA_NAME
4037 || TREE_CODE (base1) == STRING_CST))
4038 equal = (base0 == base1);
4041 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4042 off0.is_constant (&ioff0);
4043 off1.is_constant (&ioff1);
4044 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4045 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4046 || (TREE_CODE (base0) == STRING_CST
4047 && TREE_CODE (base1) == STRING_CST
4048 && ioff0 >= 0 && ioff1 >= 0
4049 && ioff0 < TREE_STRING_LENGTH (base0)
4050 && ioff1 < TREE_STRING_LENGTH (base1)
4051 /* This is a too conservative test that the STRING_CSTs
4052 will not end up being string-merged. */
4053 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4054 TREE_STRING_POINTER (base1) + ioff1,
4055 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4056 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4058 else if (!DECL_P (base0) || !DECL_P (base1))
4060 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4062 /* If this is a pointer comparison, ignore for now even
4063 valid equalities where one pointer is the offset zero
4064 of one object and the other to one past end of another one. */
4065 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4067 /* Assume that automatic variables can't be adjacent to global
4069 else if (is_global_var (base0) != is_global_var (base1))
4073 tree sz0 = DECL_SIZE_UNIT (base0);
4074 tree sz1 = DECL_SIZE_UNIT (base1);
4075 /* If sizes are unknown, e.g. VLA or not representable,
4077 if (!tree_fits_poly_int64_p (sz0)
4078 || !tree_fits_poly_int64_p (sz1))
4082 poly_int64 size0 = tree_to_poly_int64 (sz0);
4083 poly_int64 size1 = tree_to_poly_int64 (sz1);
4084 /* If one offset is pointing (or could be) to the beginning
4085 of one object and the other is pointing to one past the
4086 last byte of the other object, punt. */
4087 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4089 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4091 /* If both offsets are the same, there are some cases
4092 we know that are ok. Either if we know they aren't
4093 zero, or if we know both sizes are no zero. */
4095 && known_eq (off0, off1)
4096 && (known_ne (off0, 0)
4097 || (known_ne (size0, 0) && known_ne (size1, 0))))
4104 && (cmp == EQ_EXPR || cmp == NE_EXPR
4105 /* If the offsets are equal we can ignore overflow. */
4106 || known_eq (off0, off1)
4107 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4108 /* Or if we compare using pointers to decls or strings. */
4109 || (POINTER_TYPE_P (TREE_TYPE (@2))
4110 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4112 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4113 { constant_boolean_node (known_eq (off0, off1), type); })
4114 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4115 { constant_boolean_node (known_ne (off0, off1), type); })
4116 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4117 { constant_boolean_node (known_lt (off0, off1), type); })
4118 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4119 { constant_boolean_node (known_le (off0, off1), type); })
4120 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4121 { constant_boolean_node (known_ge (off0, off1), type); })
4122 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4123 { constant_boolean_node (known_gt (off0, off1), type); }))
4126 (if (cmp == EQ_EXPR)
4127 { constant_boolean_node (false, type); })
4128 (if (cmp == NE_EXPR)
4129 { constant_boolean_node (true, type); })))))))))
4131 /* Simplify pointer equality compares using PTA. */
4135 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4136 && ptrs_compare_unequal (@0, @1))
4137 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4139 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4140 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4141 Disable the transform if either operand is pointer to function.
4142 This broke pr22051-2.c for arm where function pointer
4143 canonicalizaion is not wanted. */
4147 (cmp (convert @0) INTEGER_CST@1)
4148 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4149 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4150 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4151 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4152 && POINTER_TYPE_P (TREE_TYPE (@1))
4153 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4154 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4155 (cmp @0 (convert @1)))))
4157 /* Non-equality compare simplifications from fold_binary */
4158 (for cmp (lt gt le ge)
4159 /* Comparisons with the highest or lowest possible integer of
4160 the specified precision will have known values. */
4162 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4163 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4164 || POINTER_TYPE_P (TREE_TYPE (@1))
4165 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4166 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4169 tree cst = uniform_integer_cst_p (@1);
4170 tree arg1_type = TREE_TYPE (cst);
4171 unsigned int prec = TYPE_PRECISION (arg1_type);
4172 wide_int max = wi::max_value (arg1_type);
4173 wide_int signed_max = wi::max_value (prec, SIGNED);
4174 wide_int min = wi::min_value (arg1_type);
4177 (if (wi::to_wide (cst) == max)
4179 (if (cmp == GT_EXPR)
4180 { constant_boolean_node (false, type); })
4181 (if (cmp == GE_EXPR)
4183 (if (cmp == LE_EXPR)
4184 { constant_boolean_node (true, type); })
4185 (if (cmp == LT_EXPR)
4187 (if (wi::to_wide (cst) == min)
4189 (if (cmp == LT_EXPR)
4190 { constant_boolean_node (false, type); })
4191 (if (cmp == LE_EXPR)
4193 (if (cmp == GE_EXPR)
4194 { constant_boolean_node (true, type); })
4195 (if (cmp == GT_EXPR)
4197 (if (wi::to_wide (cst) == max - 1)
4199 (if (cmp == GT_EXPR)
4200 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4201 wide_int_to_tree (TREE_TYPE (cst),
4204 (if (cmp == LE_EXPR)
4205 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4206 wide_int_to_tree (TREE_TYPE (cst),
4209 (if (wi::to_wide (cst) == min + 1)
4211 (if (cmp == GE_EXPR)
4212 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4213 wide_int_to_tree (TREE_TYPE (cst),
4216 (if (cmp == LT_EXPR)
4217 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4218 wide_int_to_tree (TREE_TYPE (cst),
4221 (if (wi::to_wide (cst) == signed_max
4222 && TYPE_UNSIGNED (arg1_type)
4223 /* We will flip the signedness of the comparison operator
4224 associated with the mode of @1, so the sign bit is
4225 specified by this mode. Check that @1 is the signed
4226 max associated with this sign bit. */
4227 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4228 /* signed_type does not work on pointer types. */
4229 && INTEGRAL_TYPE_P (arg1_type))
4230 /* The following case also applies to X < signed_max+1
4231 and X >= signed_max+1 because previous transformations. */
4232 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4233 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4235 (if (cst == @1 && cmp == LE_EXPR)
4236 (ge (convert:st @0) { build_zero_cst (st); }))
4237 (if (cst == @1 && cmp == GT_EXPR)
4238 (lt (convert:st @0) { build_zero_cst (st); }))
4239 (if (cmp == LE_EXPR)
4240 (ge (view_convert:st @0) { build_zero_cst (st); }))
4241 (if (cmp == GT_EXPR)
4242 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4244 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4245 /* If the second operand is NaN, the result is constant. */
4248 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4249 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4250 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4251 ? false : true, type); })))
4253 /* bool_var != 0 becomes bool_var. */
4255 (ne @0 integer_zerop)
4256 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4257 && types_match (type, TREE_TYPE (@0)))
4259 /* bool_var == 1 becomes bool_var. */
4261 (eq @0 integer_onep)
4262 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4263 && types_match (type, TREE_TYPE (@0)))
4266 bool_var == 0 becomes !bool_var or
4267 bool_var != 1 becomes !bool_var
4268 here because that only is good in assignment context as long
4269 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4270 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4271 clearly less optimal and which we'll transform again in forwprop. */
4273 /* When one argument is a constant, overflow detection can be simplified.
4274 Currently restricted to single use so as not to interfere too much with
4275 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4276 A + CST CMP A -> A CMP' CST' */
4277 (for cmp (lt le ge gt)
4280 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4281 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4282 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4283 && wi::to_wide (@1) != 0
4285 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4286 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4287 wi::max_value (prec, UNSIGNED)
4288 - wi::to_wide (@1)); })))))
4290 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4291 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4292 expects the long form, so we restrict the transformation for now. */
4295 (cmp:c (minus@2 @0 @1) @0)
4296 (if (single_use (@2)
4297 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4298 && TYPE_UNSIGNED (TREE_TYPE (@0))
4299 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4302 /* Testing for overflow is unnecessary if we already know the result. */
4307 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4308 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4309 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4310 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4315 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4316 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4317 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4318 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4320 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4321 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4325 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4326 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4327 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4328 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4330 /* Simplification of math builtins. These rules must all be optimizations
4331 as well as IL simplifications. If there is a possibility that the new
4332 form could be a pessimization, the rule should go in the canonicalization
4333 section that follows this one.
4335 Rules can generally go in this section if they satisfy one of
4338 - the rule describes an identity
4340 - the rule replaces calls with something as simple as addition or
4343 - the rule contains unary calls only and simplifies the surrounding
4344 arithmetic. (The idea here is to exclude non-unary calls in which
4345 one operand is constant and in which the call is known to be cheap
4346 when the operand has that value.) */
4348 (if (flag_unsafe_math_optimizations)
4349 /* Simplify sqrt(x) * sqrt(x) -> x. */
4351 (mult (SQRT_ALL@1 @0) @1)
4352 (if (!HONOR_SNANS (type))
4355 (for op (plus minus)
4356 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4360 (rdiv (op @0 @2) @1)))
4362 (for cmp (lt le gt ge)
4363 neg_cmp (gt ge lt le)
4364 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4366 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4368 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4370 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4371 || (real_zerop (tem) && !real_zerop (@1))))
4373 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4375 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4376 (neg_cmp @0 { tem; })))))))
4378 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4379 (for root (SQRT CBRT)
4381 (mult (root:s @0) (root:s @1))
4382 (root (mult @0 @1))))
4384 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4385 (for exps (EXP EXP2 EXP10 POW10)
4387 (mult (exps:s @0) (exps:s @1))
4388 (exps (plus @0 @1))))
4390 /* Simplify a/root(b/c) into a*root(c/b). */
4391 (for root (SQRT CBRT)
4393 (rdiv @0 (root:s (rdiv:s @1 @2)))
4394 (mult @0 (root (rdiv @2 @1)))))
4396 /* Simplify x/expN(y) into x*expN(-y). */
4397 (for exps (EXP EXP2 EXP10 POW10)
4399 (rdiv @0 (exps:s @1))
4400 (mult @0 (exps (negate @1)))))
4402 (for logs (LOG LOG2 LOG10 LOG10)
4403 exps (EXP EXP2 EXP10 POW10)
4404 /* logN(expN(x)) -> x. */
4408 /* expN(logN(x)) -> x. */
4413 /* Optimize logN(func()) for various exponential functions. We
4414 want to determine the value "x" and the power "exponent" in
4415 order to transform logN(x**exponent) into exponent*logN(x). */
4416 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4417 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4420 (if (SCALAR_FLOAT_TYPE_P (type))
4426 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4427 x = build_real_truncate (type, dconst_e ());
4430 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4431 x = build_real (type, dconst2);
4435 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4437 REAL_VALUE_TYPE dconst10;
4438 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4439 x = build_real (type, dconst10);
4446 (mult (logs { x; }) @0)))))
4454 (if (SCALAR_FLOAT_TYPE_P (type))
4460 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4461 x = build_real (type, dconsthalf);
4464 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4465 x = build_real_truncate (type, dconst_third ());
4471 (mult { x; } (logs @0))))))
4473 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4474 (for logs (LOG LOG2 LOG10)
4478 (mult @1 (logs @0))))
4480 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4481 or if C is a positive power of 2,
4482 pow(C,x) -> exp2(log2(C)*x). */
4490 (pows REAL_CST@0 @1)
4491 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4492 && real_isfinite (TREE_REAL_CST_PTR (@0))
4493 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4494 the use_exp2 case until after vectorization. It seems actually
4495 beneficial for all constants to postpone this until later,
4496 because exp(log(C)*x), while faster, will have worse precision
4497 and if x folds into a constant too, that is unnecessary
4499 && canonicalize_math_after_vectorization_p ())
4501 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4502 bool use_exp2 = false;
4503 if (targetm.libc_has_function (function_c99_misc)
4504 && value->cl == rvc_normal)
4506 REAL_VALUE_TYPE frac_rvt = *value;
4507 SET_REAL_EXP (&frac_rvt, 1);
4508 if (real_equal (&frac_rvt, &dconst1))
4513 (if (optimize_pow_to_exp (@0, @1))
4514 (exps (mult (logs @0) @1)))
4515 (exp2s (mult (log2s @0) @1)))))))
4518 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4520 exps (EXP EXP2 EXP10 POW10)
4521 logs (LOG LOG2 LOG10 LOG10)
4523 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4524 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4525 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4526 (exps (plus (mult (logs @0) @1) @2)))))
4531 exps (EXP EXP2 EXP10 POW10)
4532 /* sqrt(expN(x)) -> expN(x*0.5). */
4535 (exps (mult @0 { build_real (type, dconsthalf); })))
4536 /* cbrt(expN(x)) -> expN(x/3). */
4539 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4540 /* pow(expN(x), y) -> expN(x*y). */
4543 (exps (mult @0 @1))))
4545 /* tan(atan(x)) -> x. */
4552 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4556 copysigns (COPYSIGN)
4561 REAL_VALUE_TYPE r_cst;
4562 build_sinatan_real (&r_cst, type);
4563 tree t_cst = build_real (type, r_cst);
4564 tree t_one = build_one_cst (type);
4566 (if (SCALAR_FLOAT_TYPE_P (type))
4567 (cond (lt (abs @0) { t_cst; })
4568 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4569 (copysigns { t_one; } @0))))))
4571 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4575 copysigns (COPYSIGN)
4580 REAL_VALUE_TYPE r_cst;
4581 build_sinatan_real (&r_cst, type);
4582 tree t_cst = build_real (type, r_cst);
4583 tree t_one = build_one_cst (type);
4584 tree t_zero = build_zero_cst (type);
4586 (if (SCALAR_FLOAT_TYPE_P (type))
4587 (cond (lt (abs @0) { t_cst; })
4588 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4589 (copysigns { t_zero; } @0))))))
4591 (if (!flag_errno_math)
4592 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4597 (sinhs (atanhs:s @0))
4598 (with { tree t_one = build_one_cst (type); }
4599 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4601 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4606 (coshs (atanhs:s @0))
4607 (with { tree t_one = build_one_cst (type); }
4608 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4610 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4612 (CABS (complex:C @0 real_zerop@1))
4615 /* trunc(trunc(x)) -> trunc(x), etc. */
4616 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4620 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4621 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4623 (fns integer_valued_real_p@0)
4626 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4628 (HYPOT:c @0 real_zerop@1)
4631 /* pow(1,x) -> 1. */
4633 (POW real_onep@0 @1)
4637 /* copysign(x,x) -> x. */
4638 (COPYSIGN_ALL @0 @0)
4642 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4643 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4646 (for scale (LDEXP SCALBN SCALBLN)
4647 /* ldexp(0, x) -> 0. */
4649 (scale real_zerop@0 @1)
4651 /* ldexp(x, 0) -> x. */
4653 (scale @0 integer_zerop@1)
4655 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4657 (scale REAL_CST@0 @1)
4658 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4661 /* Canonicalization of sequences of math builtins. These rules represent
4662 IL simplifications but are not necessarily optimizations.
4664 The sincos pass is responsible for picking "optimal" implementations
4665 of math builtins, which may be more complicated and can sometimes go
4666 the other way, e.g. converting pow into a sequence of sqrts.
4667 We only want to do these canonicalizations before the pass has run. */
4669 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4670 /* Simplify tan(x) * cos(x) -> sin(x). */
4672 (mult:c (TAN:s @0) (COS:s @0))
4675 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4677 (mult:c @0 (POW:s @0 REAL_CST@1))
4678 (if (!TREE_OVERFLOW (@1))
4679 (POW @0 (plus @1 { build_one_cst (type); }))))
4681 /* Simplify sin(x) / cos(x) -> tan(x). */
4683 (rdiv (SIN:s @0) (COS:s @0))
4686 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4688 (rdiv (COS:s @0) (SIN:s @0))
4689 (rdiv { build_one_cst (type); } (TAN @0)))
4691 /* Simplify sin(x) / tan(x) -> cos(x). */
4693 (rdiv (SIN:s @0) (TAN:s @0))
4694 (if (! HONOR_NANS (@0)
4695 && ! HONOR_INFINITIES (@0))
4698 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4700 (rdiv (TAN:s @0) (SIN:s @0))
4701 (if (! HONOR_NANS (@0)
4702 && ! HONOR_INFINITIES (@0))
4703 (rdiv { build_one_cst (type); } (COS @0))))
4705 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4707 (mult (POW:s @0 @1) (POW:s @0 @2))
4708 (POW @0 (plus @1 @2)))
4710 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4712 (mult (POW:s @0 @1) (POW:s @2 @1))
4713 (POW (mult @0 @2) @1))
4715 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4717 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4718 (POWI (mult @0 @2) @1))
4720 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4722 (rdiv (POW:s @0 REAL_CST@1) @0)
4723 (if (!TREE_OVERFLOW (@1))
4724 (POW @0 (minus @1 { build_one_cst (type); }))))
4726 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4728 (rdiv @0 (POW:s @1 @2))
4729 (mult @0 (POW @1 (negate @2))))
4734 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4737 (pows @0 { build_real (type, dconst_quarter ()); }))
4738 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4741 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4742 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4745 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4746 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4748 (cbrts (cbrts tree_expr_nonnegative_p@0))
4749 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4750 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4752 (sqrts (pows @0 @1))
4753 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4754 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4756 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4757 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4758 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4760 (pows (sqrts @0) @1)
4761 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4762 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4764 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4765 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4766 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4768 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4769 (pows @0 (mult @1 @2))))
4771 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4773 (CABS (complex @0 @0))
4774 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4776 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4779 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4781 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4786 (cexps compositional_complex@0)
4787 (if (targetm.libc_has_function (function_c99_math_complex))
4789 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4790 (mult @1 (imagpart @2)))))))
4792 (if (canonicalize_math_p ())
4793 /* floor(x) -> trunc(x) if x is nonnegative. */
4794 (for floors (FLOOR_ALL)
4797 (floors tree_expr_nonnegative_p@0)
4800 (match double_value_p
4802 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4803 (for froms (BUILT_IN_TRUNCL
4815 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4816 (if (optimize && canonicalize_math_p ())
4818 (froms (convert double_value_p@0))
4819 (convert (tos @0)))))
4821 (match float_value_p
4823 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4824 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4825 BUILT_IN_FLOORL BUILT_IN_FLOOR
4826 BUILT_IN_CEILL BUILT_IN_CEIL
4827 BUILT_IN_ROUNDL BUILT_IN_ROUND
4828 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4829 BUILT_IN_RINTL BUILT_IN_RINT)
4830 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4831 BUILT_IN_FLOORF BUILT_IN_FLOORF
4832 BUILT_IN_CEILF BUILT_IN_CEILF
4833 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4834 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4835 BUILT_IN_RINTF BUILT_IN_RINTF)
4836 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4838 (if (optimize && canonicalize_math_p ()
4839 && targetm.libc_has_function (function_c99_misc))
4841 (froms (convert float_value_p@0))
4842 (convert (tos @0)))))
4844 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4845 tos (XFLOOR XCEIL XROUND XRINT)
4846 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4847 (if (optimize && canonicalize_math_p ())
4849 (froms (convert double_value_p@0))
4852 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4853 XFLOOR XCEIL XROUND XRINT)
4854 tos (XFLOORF XCEILF XROUNDF XRINTF)
4855 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4857 (if (optimize && canonicalize_math_p ())
4859 (froms (convert float_value_p@0))
4862 (if (canonicalize_math_p ())
4863 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4864 (for floors (IFLOOR LFLOOR LLFLOOR)
4866 (floors tree_expr_nonnegative_p@0)
4869 (if (canonicalize_math_p ())
4870 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4871 (for fns (IFLOOR LFLOOR LLFLOOR
4873 IROUND LROUND LLROUND)
4875 (fns integer_valued_real_p@0)
4877 (if (!flag_errno_math)
4878 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4879 (for rints (IRINT LRINT LLRINT)
4881 (rints integer_valued_real_p@0)
4884 (if (canonicalize_math_p ())
4885 (for ifn (IFLOOR ICEIL IROUND IRINT)
4886 lfn (LFLOOR LCEIL LROUND LRINT)
4887 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4888 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4889 sizeof (int) == sizeof (long). */
4890 (if (TYPE_PRECISION (integer_type_node)
4891 == TYPE_PRECISION (long_integer_type_node))
4894 (lfn:long_integer_type_node @0)))
4895 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4896 sizeof (long long) == sizeof (long). */
4897 (if (TYPE_PRECISION (long_long_integer_type_node)
4898 == TYPE_PRECISION (long_integer_type_node))
4901 (lfn:long_integer_type_node @0)))))
4903 /* cproj(x) -> x if we're ignoring infinities. */
4906 (if (!HONOR_INFINITIES (type))
4909 /* If the real part is inf and the imag part is known to be
4910 nonnegative, return (inf + 0i). */
4912 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4913 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4914 { build_complex_inf (type, false); }))
4916 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4918 (CPROJ (complex @0 REAL_CST@1))
4919 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4920 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4926 (pows @0 REAL_CST@1)
4928 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4929 REAL_VALUE_TYPE tmp;
4932 /* pow(x,0) -> 1. */
4933 (if (real_equal (value, &dconst0))
4934 { build_real (type, dconst1); })
4935 /* pow(x,1) -> x. */
4936 (if (real_equal (value, &dconst1))
4938 /* pow(x,-1) -> 1/x. */
4939 (if (real_equal (value, &dconstm1))
4940 (rdiv { build_real (type, dconst1); } @0))
4941 /* pow(x,0.5) -> sqrt(x). */
4942 (if (flag_unsafe_math_optimizations
4943 && canonicalize_math_p ()
4944 && real_equal (value, &dconsthalf))
4946 /* pow(x,1/3) -> cbrt(x). */
4947 (if (flag_unsafe_math_optimizations
4948 && canonicalize_math_p ()
4949 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4950 real_equal (value, &tmp)))
4953 /* powi(1,x) -> 1. */
4955 (POWI real_onep@0 @1)
4959 (POWI @0 INTEGER_CST@1)
4961 /* powi(x,0) -> 1. */
4962 (if (wi::to_wide (@1) == 0)
4963 { build_real (type, dconst1); })
4964 /* powi(x,1) -> x. */
4965 (if (wi::to_wide (@1) == 1)
4967 /* powi(x,-1) -> 1/x. */
4968 (if (wi::to_wide (@1) == -1)
4969 (rdiv { build_real (type, dconst1); } @0))))
4971 /* Narrowing of arithmetic and logical operations.
4973 These are conceptually similar to the transformations performed for
4974 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4975 term we want to move all that code out of the front-ends into here. */
4977 /* Convert (outertype)((innertype0)a+(innertype1)b)
4978 into ((newtype)a+(newtype)b) where newtype
4979 is the widest mode from all of these. */
4980 (for op (plus minus mult rdiv)
4982 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
4983 /* If we have a narrowing conversion of an arithmetic operation where
4984 both operands are widening conversions from the same type as the outer
4985 narrowing conversion. Then convert the innermost operands to a
4986 suitable unsigned type (to avoid introducing undefined behavior),
4987 perform the operation and convert the result to the desired type. */
4988 (if (INTEGRAL_TYPE_P (type)
4991 /* We check for type compatibility between @0 and @1 below,
4992 so there's no need to check that @2/@4 are integral types. */
4993 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
4994 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
4995 /* The precision of the type of each operand must match the
4996 precision of the mode of each operand, similarly for the
4998 && type_has_mode_precision_p (TREE_TYPE (@1))
4999 && type_has_mode_precision_p (TREE_TYPE (@2))
5000 && type_has_mode_precision_p (type)
5001 /* The inner conversion must be a widening conversion. */
5002 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5003 && types_match (@1, type)
5004 && (types_match (@1, @2)
5005 /* Or the second operand is const integer or converted const
5006 integer from valueize. */
5007 || TREE_CODE (@2) == INTEGER_CST))
5008 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5009 (op @1 (convert @2))
5010 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5011 (convert (op (convert:utype @1)
5012 (convert:utype @2)))))
5013 (if (FLOAT_TYPE_P (type)
5014 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5015 == DECIMAL_FLOAT_TYPE_P (type))
5016 (with { tree arg0 = strip_float_extensions (@1);
5017 tree arg1 = strip_float_extensions (@2);
5018 tree itype = TREE_TYPE (@0);
5019 tree ty1 = TREE_TYPE (arg0);
5020 tree ty2 = TREE_TYPE (arg1);
5021 enum tree_code code = TREE_CODE (itype); }
5022 (if (FLOAT_TYPE_P (ty1)
5023 && FLOAT_TYPE_P (ty2))
5024 (with { tree newtype = type;
5025 if (TYPE_MODE (ty1) == SDmode
5026 || TYPE_MODE (ty2) == SDmode
5027 || TYPE_MODE (type) == SDmode)
5028 newtype = dfloat32_type_node;
5029 if (TYPE_MODE (ty1) == DDmode
5030 || TYPE_MODE (ty2) == DDmode
5031 || TYPE_MODE (type) == DDmode)
5032 newtype = dfloat64_type_node;
5033 if (TYPE_MODE (ty1) == TDmode
5034 || TYPE_MODE (ty2) == TDmode
5035 || TYPE_MODE (type) == TDmode)
5036 newtype = dfloat128_type_node; }
5037 (if ((newtype == dfloat32_type_node
5038 || newtype == dfloat64_type_node
5039 || newtype == dfloat128_type_node)
5041 && types_match (newtype, type))
5042 (op (convert:newtype @1) (convert:newtype @2))
5045 if (!flag_unsafe_math_optimizations)
5047 if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5050 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5055 /* Sometimes this transformation is safe (cannot
5056 change results through affecting double rounding
5057 cases) and sometimes it is not. If NEWTYPE is
5058 wider than TYPE, e.g. (float)((long double)double
5059 + (long double)double) converted to
5060 (float)(double + double), the transformation is
5061 unsafe regardless of the details of the types
5062 involved; double rounding can arise if the result
5063 of NEWTYPE arithmetic is a NEWTYPE value half way
5064 between two representable TYPE values but the
5065 exact value is sufficiently different (in the
5066 right direction) for this difference to be
5067 visible in ITYPE arithmetic. If NEWTYPE is the
5068 same as TYPE, however, the transformation may be
5069 safe depending on the types involved: it is safe
5070 if the ITYPE has strictly more than twice as many
5071 mantissa bits as TYPE, can represent infinities
5072 and NaNs if the TYPE can, and has sufficient
5073 exponent range for the product or ratio of two
5074 values representable in the TYPE to be within the
5075 range of normal values of ITYPE. */
5076 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5077 && (flag_unsafe_math_optimizations
5078 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5079 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5081 && !excess_precision_type (newtype)))
5082 && !types_match (itype, newtype))
5083 (convert:type (op (convert:newtype @1)
5084 (convert:newtype @2)))
5089 /* This is another case of narrowing, specifically when there's an outer
5090 BIT_AND_EXPR which masks off bits outside the type of the innermost
5091 operands. Like the previous case we have to convert the operands
5092 to unsigned types to avoid introducing undefined behavior for the
5093 arithmetic operation. */
5094 (for op (minus plus)
5096 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5097 (if (INTEGRAL_TYPE_P (type)
5098 /* We check for type compatibility between @0 and @1 below,
5099 so there's no need to check that @1/@3 are integral types. */
5100 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5101 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5102 /* The precision of the type of each operand must match the
5103 precision of the mode of each operand, similarly for the
5105 && type_has_mode_precision_p (TREE_TYPE (@0))
5106 && type_has_mode_precision_p (TREE_TYPE (@1))
5107 && type_has_mode_precision_p (type)
5108 /* The inner conversion must be a widening conversion. */
5109 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5110 && types_match (@0, @1)
5111 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5112 <= TYPE_PRECISION (TREE_TYPE (@0)))
5113 && (wi::to_wide (@4)
5114 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5115 true, TYPE_PRECISION (type))) == 0)
5116 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5117 (with { tree ntype = TREE_TYPE (@0); }
5118 (convert (bit_and (op @0 @1) (convert:ntype @4))))
5119 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5120 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5121 (convert:utype @4))))))))
5123 /* Transform (@0 < @1 and @0 < @2) to use min,
5124 (@0 > @1 and @0 > @2) to use max */
5125 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5126 op (lt le gt ge lt le gt ge )
5127 ext (min min max max max max min min )
5129 (logic (op:cs @0 @1) (op:cs @0 @2))
5130 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5131 && TREE_CODE (@0) != INTEGER_CST)
5132 (op @0 (ext @1 @2)))))
5135 /* signbit(x) -> 0 if x is nonnegative. */
5136 (SIGNBIT tree_expr_nonnegative_p@0)
5137 { integer_zero_node; })
5140 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
5142 (if (!HONOR_SIGNED_ZEROS (@0))
5143 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
5145 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
5147 (for op (plus minus)
5150 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5151 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5152 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
5153 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
5154 && !TYPE_SATURATING (TREE_TYPE (@0)))
5155 (with { tree res = int_const_binop (rop, @2, @1); }
5156 (if (TREE_OVERFLOW (res)
5157 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5158 { constant_boolean_node (cmp == NE_EXPR, type); }
5159 (if (single_use (@3))
5160 (cmp @0 { TREE_OVERFLOW (res)
5161 ? drop_tree_overflow (res) : res; }))))))))
5162 (for cmp (lt le gt ge)
5163 (for op (plus minus)
5166 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5167 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5168 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5169 (with { tree res = int_const_binop (rop, @2, @1); }
5170 (if (TREE_OVERFLOW (res))
5172 fold_overflow_warning (("assuming signed overflow does not occur "
5173 "when simplifying conditional to constant"),
5174 WARN_STRICT_OVERFLOW_CONDITIONAL);
5175 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
5176 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
5177 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
5178 TYPE_SIGN (TREE_TYPE (@1)))
5179 != (op == MINUS_EXPR);
5180 constant_boolean_node (less == ovf_high, type);
5182 (if (single_use (@3))
5185 fold_overflow_warning (("assuming signed overflow does not occur "
5186 "when changing X +- C1 cmp C2 to "
5188 WARN_STRICT_OVERFLOW_COMPARISON);
5190 (cmp @0 { res; })))))))))
5192 /* Canonicalizations of BIT_FIELD_REFs. */
5195 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5196 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5199 (BIT_FIELD_REF (view_convert @0) @1 @2)
5200 (BIT_FIELD_REF @0 @1 @2))
5203 (BIT_FIELD_REF @0 @1 integer_zerop)
5204 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5208 (BIT_FIELD_REF @0 @1 @2)
5210 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5211 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5213 (if (integer_zerop (@2))
5214 (view_convert (realpart @0)))
5215 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5216 (view_convert (imagpart @0)))))
5217 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5218 && INTEGRAL_TYPE_P (type)
5219 /* On GIMPLE this should only apply to register arguments. */
5220 && (! GIMPLE || is_gimple_reg (@0))
5221 /* A bit-field-ref that referenced the full argument can be stripped. */
5222 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5223 && integer_zerop (@2))
5224 /* Low-parts can be reduced to integral conversions.
5225 ??? The following doesn't work for PDP endian. */
5226 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5227 /* Don't even think about BITS_BIG_ENDIAN. */
5228 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5229 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5230 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5231 ? (TYPE_PRECISION (TREE_TYPE (@0))
5232 - TYPE_PRECISION (type))
5236 /* Simplify vector extracts. */
5239 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5240 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5241 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5242 || (VECTOR_TYPE_P (type)
5243 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5246 tree ctor = (TREE_CODE (@0) == SSA_NAME
5247 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5248 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5249 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5250 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5251 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5254 && (idx % width) == 0
5256 && known_le ((idx + n) / width,
5257 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5262 /* Constructor elements can be subvectors. */
5264 if (CONSTRUCTOR_NELTS (ctor) != 0)
5266 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5267 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5268 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5270 unsigned HOST_WIDE_INT elt, count, const_k;
5273 /* We keep an exact subset of the constructor elements. */
5274 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5275 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5276 { build_constructor (type, NULL); }
5278 (if (elt < CONSTRUCTOR_NELTS (ctor))
5279 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5280 { build_zero_cst (type); })
5282 vec<constructor_elt, va_gc> *vals;
5283 vec_alloc (vals, count);
5284 for (unsigned i = 0;
5285 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5286 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5287 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5288 build_constructor (type, vals);
5290 /* The bitfield references a single constructor element. */
5291 (if (k.is_constant (&const_k)
5292 && idx + n <= (idx / const_k + 1) * const_k)
5294 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5295 { build_zero_cst (type); })
5297 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5298 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5299 @1 { bitsize_int ((idx % const_k) * width); })))))))))
5301 /* Simplify a bit extraction from a bit insertion for the cases with
5302 the inserted element fully covering the extraction or the insertion
5303 not touching the extraction. */
5305 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5308 unsigned HOST_WIDE_INT isize;
5309 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5310 isize = TYPE_PRECISION (TREE_TYPE (@1));
5312 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5315 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5316 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5317 wi::to_wide (@ipos) + isize))
5318 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
5320 - wi::to_wide (@ipos)); }))
5321 (if (wi::geu_p (wi::to_wide (@ipos),
5322 wi::to_wide (@rpos) + wi::to_wide (@rsize))
5323 || wi::geu_p (wi::to_wide (@rpos),
5324 wi::to_wide (@ipos) + isize))
5325 (BIT_FIELD_REF @0 @rsize @rpos)))))
5327 (if (canonicalize_math_after_vectorization_p ())
5330 (fmas:c (negate @0) @1 @2)
5331 (IFN_FNMA @0 @1 @2))
5333 (fmas @0 @1 (negate @2))
5336 (fmas:c (negate @0) @1 (negate @2))
5337 (IFN_FNMS @0 @1 @2))
5339 (negate (fmas@3 @0 @1 @2))
5340 (if (single_use (@3))
5341 (IFN_FNMS @0 @1 @2))))
5344 (IFN_FMS:c (negate @0) @1 @2)
5345 (IFN_FNMS @0 @1 @2))
5347 (IFN_FMS @0 @1 (negate @2))
5350 (IFN_FMS:c (negate @0) @1 (negate @2))
5351 (IFN_FNMA @0 @1 @2))
5353 (negate (IFN_FMS@3 @0 @1 @2))
5354 (if (single_use (@3))
5355 (IFN_FNMA @0 @1 @2)))
5358 (IFN_FNMA:c (negate @0) @1 @2)
5361 (IFN_FNMA @0 @1 (negate @2))
5362 (IFN_FNMS @0 @1 @2))
5364 (IFN_FNMA:c (negate @0) @1 (negate @2))
5367 (negate (IFN_FNMA@3 @0 @1 @2))
5368 (if (single_use (@3))
5369 (IFN_FMS @0 @1 @2)))
5372 (IFN_FNMS:c (negate @0) @1 @2)
5375 (IFN_FNMS @0 @1 (negate @2))
5376 (IFN_FNMA @0 @1 @2))
5378 (IFN_FNMS:c (negate @0) @1 (negate @2))
5381 (negate (IFN_FNMS@3 @0 @1 @2))
5382 (if (single_use (@3))
5383 (IFN_FMA @0 @1 @2))))
5385 /* POPCOUNT simplifications. */
5386 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5387 BUILT_IN_POPCOUNTIMAX)
5388 /* popcount(X&1) is nop_expr(X&1). */
5391 (if (tree_nonzero_bits (@0) == 1)
5393 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
5395 (plus (popcount:s @0) (popcount:s @1))
5396 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5397 (popcount (bit_ior @0 @1))))
5398 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
5399 (for cmp (le eq ne gt)
5402 (cmp (popcount @0) integer_zerop)
5403 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5412 r = c ? a1 op a2 : b;
5414 if the target can do it in one go. This makes the operation conditional
5415 on c, so could drop potentially-trapping arithmetic, but that's a valid
5416 simplification if the result of the operation isn't needed.
5418 Avoid speculatively generating a stand-alone vector comparison
5419 on targets that might not support them. Any target implementing
5420 conditional internal functions must support the same comparisons
5421 inside and outside a VEC_COND_EXPR. */
5424 (for uncond_op (UNCOND_BINARY)
5425 cond_op (COND_BINARY)
5427 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5428 (with { tree op_type = TREE_TYPE (@4); }
5429 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5430 && element_precision (type) == element_precision (op_type))
5431 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5433 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5434 (with { tree op_type = TREE_TYPE (@4); }
5435 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5436 && element_precision (type) == element_precision (op_type))
5437 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5439 /* Same for ternary operations. */
5440 (for uncond_op (UNCOND_TERNARY)
5441 cond_op (COND_TERNARY)
5443 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5444 (with { tree op_type = TREE_TYPE (@5); }
5445 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5446 && element_precision (type) == element_precision (op_type))
5447 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5449 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5450 (with { tree op_type = TREE_TYPE (@5); }
5451 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5452 && element_precision (type) == element_precision (op_type))
5453 (view_convert (cond_op (bit_not @0) @2 @3 @4
5454 (view_convert:op_type @1)))))))
5457 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5458 "else" value of an IFN_COND_*. */
5459 (for cond_op (COND_BINARY)
5461 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5462 (with { tree op_type = TREE_TYPE (@3); }
5463 (if (element_precision (type) == element_precision (op_type))
5464 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5466 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5467 (with { tree op_type = TREE_TYPE (@5); }
5468 (if (inverse_conditions_p (@0, @2)
5469 && element_precision (type) == element_precision (op_type))
5470 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
5472 /* Same for ternary operations. */
5473 (for cond_op (COND_TERNARY)
5475 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5476 (with { tree op_type = TREE_TYPE (@4); }
5477 (if (element_precision (type) == element_precision (op_type))
5478 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5480 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5481 (with { tree op_type = TREE_TYPE (@6); }
5482 (if (inverse_conditions_p (@0, @2)
5483 && element_precision (type) == element_precision (op_type))
5484 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5486 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5489 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5490 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5492 If pointers are known not to wrap, B checks whether @1 bytes starting
5493 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5494 bytes. A is more efficiently tested as:
5496 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5498 The equivalent expression for B is given by replacing @1 with @1 - 1:
5500 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5502 @0 and @2 can be swapped in both expressions without changing the result.
5504 The folds rely on sizetype's being unsigned (which is always true)
5505 and on its being the same width as the pointer (which we have to check).
5507 The fold replaces two pointer_plus expressions, two comparisons and
5508 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5509 the best case it's a saving of two operations. The A fold retains one
5510 of the original pointer_pluses, so is a win even if both pointer_pluses
5511 are used elsewhere. The B fold is a wash if both pointer_pluses are
5512 used elsewhere, since all we end up doing is replacing a comparison with
5513 a pointer_plus. We do still apply the fold under those circumstances
5514 though, in case applying it to other conditions eventually makes one of the
5515 pointer_pluses dead. */
5516 (for ior (truth_orif truth_or bit_ior)
5519 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5520 (cmp:cs (pointer_plus@4 @2 @1) @0))
5521 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5522 && TYPE_OVERFLOW_WRAPS (sizetype)
5523 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5524 /* Calculate the rhs constant. */
5525 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5526 offset_int rhs = off * 2; }
5527 /* Always fails for negative values. */
5528 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5529 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5530 pick a canonical order. This increases the chances of using the
5531 same pointer_plus in multiple checks. */
5532 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5533 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5534 (if (cmp == LT_EXPR)
5535 (gt (convert:sizetype
5536 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5537 { swap_p ? @0 : @2; }))
5539 (gt (convert:sizetype
5540 (pointer_diff:ssizetype
5541 (pointer_plus { swap_p ? @2 : @0; }
5542 { wide_int_to_tree (sizetype, off); })
5543 { swap_p ? @0 : @2; }))
5544 { rhs_tree; })))))))))
5546 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
5548 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
5549 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
5550 (with { int i = single_nonzero_element (@1); }
5552 (with { tree elt = vector_cst_elt (@1, i);
5553 tree elt_type = TREE_TYPE (elt);
5554 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
5555 tree size = bitsize_int (elt_bits);
5556 tree pos = bitsize_int (elt_bits * i); }
5559 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
5563 (vec_perm @0 @1 VECTOR_CST@2)
5566 tree op0 = @0, op1 = @1, op2 = @2;
5568 /* Build a vector of integers from the tree mask. */
5569 vec_perm_builder builder;
5570 if (!tree_to_vec_perm_builder (&builder, op2))
5573 /* Create a vec_perm_indices for the integer vector. */
5574 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
5575 bool single_arg = (op0 == op1);
5576 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
5578 (if (sel.series_p (0, 1, 0, 1))
5580 (if (sel.series_p (0, 1, nelts, 1))
5586 if (sel.all_from_input_p (0))
5588 else if (sel.all_from_input_p (1))
5591 sel.rotate_inputs (1);
5593 else if (known_ge (poly_uint64 (sel[0]), nelts))
5595 std::swap (op0, op1);
5596 sel.rotate_inputs (1);
5600 tree cop0 = op0, cop1 = op1;
5601 if (TREE_CODE (op0) == SSA_NAME
5602 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
5603 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5604 cop0 = gimple_assign_rhs1 (def);
5605 if (TREE_CODE (op1) == SSA_NAME
5606 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
5607 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5608 cop1 = gimple_assign_rhs1 (def);
5612 (if ((TREE_CODE (cop0) == VECTOR_CST
5613 || TREE_CODE (cop0) == CONSTRUCTOR)
5614 && (TREE_CODE (cop1) == VECTOR_CST
5615 || TREE_CODE (cop1) == CONSTRUCTOR)
5616 && (t = fold_vec_perm (type, cop0, cop1, sel)))
5620 bool changed = (op0 == op1 && !single_arg);
5621 tree ins = NULL_TREE;
5624 /* See if the permutation is performing a single element
5625 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
5626 in that case. But only if the vector mode is supported,
5627 otherwise this is invalid GIMPLE. */
5628 if (TYPE_MODE (type) != BLKmode
5629 && (TREE_CODE (cop0) == VECTOR_CST
5630 || TREE_CODE (cop0) == CONSTRUCTOR
5631 || TREE_CODE (cop1) == VECTOR_CST
5632 || TREE_CODE (cop1) == CONSTRUCTOR))
5634 if (sel.series_p (1, 1, nelts + 1, 1))
5636 /* After canonicalizing the first elt to come from the
5637 first vector we only can insert the first elt from
5638 the first vector. */
5640 if ((ins = fold_read_from_vector (cop0, sel[0])))
5645 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
5646 for (at = 0; at < encoded_nelts; ++at)
5647 if (maybe_ne (sel[at], at))
5649 if (at < encoded_nelts && sel.series_p (at + 1, 1, at + 1, 1))
5651 if (known_lt (at, nelts))
5652 ins = fold_read_from_vector (cop0, sel[at]);
5654 ins = fold_read_from_vector (cop1, sel[at] - nelts);
5659 /* Generate a canonical form of the selector. */
5660 if (!ins && sel.encoding () != builder)
5662 /* Some targets are deficient and fail to expand a single
5663 argument permutation while still allowing an equivalent
5664 2-argument version. */
5666 if (sel.ninputs () == 2
5667 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
5668 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5671 vec_perm_indices sel2 (builder, 2, nelts);
5672 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
5673 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
5675 /* Not directly supported with either encoding,
5676 so use the preferred form. */
5677 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5679 if (!operand_equal_p (op2, oldop2, 0))
5684 (bit_insert { op0; } { ins; }
5685 { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); })
5687 (vec_perm { op0; } { op1; } { op2; }))))))))))
5689 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
5691 (match vec_same_elem_p
5693 (if (uniform_vector_p (@0))))
5695 (match vec_same_elem_p
5699 (vec_perm vec_same_elem_p@0 @0 @1)