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 ^ b) --> a & b */
836 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
839 /* a | ~(a ^ b) --> a | ~b */
841 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
842 (bit_ior @0 (bit_not @1)))
844 /* (a | b) | (a &^ b) --> a | b */
845 (for op (bit_and bit_xor)
847 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
850 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
852 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
855 /* ~(~a & b) --> a | ~b */
857 (bit_not (bit_and:cs (bit_not @0) @1))
858 (bit_ior @0 (bit_not @1)))
860 /* ~(~a | b) --> a & ~b */
862 (bit_not (bit_ior:cs (bit_not @0) @1))
863 (bit_and @0 (bit_not @1)))
865 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
868 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
869 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
870 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
874 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
875 ((A & N) + B) & M -> (A + B) & M
876 Similarly if (N & M) == 0,
877 ((A | N) + B) & M -> (A + B) & M
878 and for - instead of + (or unary - instead of +)
879 and/or ^ instead of |.
880 If B is constant and (B & M) == 0, fold into A & M. */
882 (for bitop (bit_and bit_ior bit_xor)
884 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
887 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
888 @3, @4, @1, ERROR_MARK, NULL_TREE,
891 (convert (bit_and (op (convert:utype { pmop[0]; })
892 (convert:utype { pmop[1]; }))
893 (convert:utype @2))))))
895 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
898 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
899 NULL_TREE, NULL_TREE, @1, bitop, @3,
902 (convert (bit_and (op (convert:utype { pmop[0]; })
903 (convert:utype { pmop[1]; }))
904 (convert:utype @2)))))))
906 (bit_and (op:s @0 @1) INTEGER_CST@2)
909 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
910 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
911 NULL_TREE, NULL_TREE, pmop); }
913 (convert (bit_and (op (convert:utype { pmop[0]; })
914 (convert:utype { pmop[1]; }))
915 (convert:utype @2)))))))
916 (for bitop (bit_and bit_ior bit_xor)
918 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
921 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
922 bitop, @2, @3, NULL_TREE, ERROR_MARK,
923 NULL_TREE, NULL_TREE, pmop); }
925 (convert (bit_and (negate (convert:utype { pmop[0]; }))
926 (convert:utype @1)))))))
928 /* X % Y is smaller than Y. */
931 (cmp (trunc_mod @0 @1) @1)
932 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
933 { constant_boolean_node (cmp == LT_EXPR, type); })))
936 (cmp @1 (trunc_mod @0 @1))
937 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
938 { constant_boolean_node (cmp == GT_EXPR, type); })))
942 (bit_ior @0 integer_all_onesp@1)
947 (bit_ior @0 integer_zerop)
952 (bit_and @0 integer_zerop@1)
958 (for op (bit_ior bit_xor plus)
960 (op:c (convert? @0) (convert? (bit_not @0)))
961 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
966 { build_zero_cst (type); })
968 /* Canonicalize X ^ ~0 to ~X. */
970 (bit_xor @0 integer_all_onesp@1)
975 (bit_and @0 integer_all_onesp)
978 /* x & x -> x, x | x -> x */
979 (for bitop (bit_and bit_ior)
984 /* x & C -> x if we know that x & ~C == 0. */
987 (bit_and SSA_NAME@0 INTEGER_CST@1)
988 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
989 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
993 /* x + (x & 1) -> (x + 1) & ~1 */
995 (plus:c @0 (bit_and:s @0 integer_onep@1))
996 (bit_and (plus @0 @1) (bit_not @1)))
998 /* x & ~(x & y) -> x & ~y */
999 /* x | ~(x | y) -> x | ~y */
1000 (for bitop (bit_and bit_ior)
1002 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1003 (bitop @0 (bit_not @1))))
1005 /* (~x & y) | ~(x | y) -> ~x */
1007 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1010 /* (x | y) ^ (x | ~y) -> ~x */
1012 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1015 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1017 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1018 (bit_not (bit_xor @0 @1)))
1020 /* (~x | y) ^ (x ^ y) -> x | ~y */
1022 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1023 (bit_ior @0 (bit_not @1)))
1025 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1027 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1028 (bit_not (bit_and @0 @1)))
1030 /* (x | y) & ~x -> y & ~x */
1031 /* (x & y) | ~x -> y | ~x */
1032 (for bitop (bit_and bit_ior)
1033 rbitop (bit_ior bit_and)
1035 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1038 /* (x & y) ^ (x | y) -> x ^ y */
1040 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1043 /* (x ^ y) ^ (x | y) -> x & y */
1045 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1048 /* (x & y) + (x ^ y) -> x | y */
1049 /* (x & y) | (x ^ y) -> x | y */
1050 /* (x & y) ^ (x ^ y) -> x | y */
1051 (for op (plus bit_ior bit_xor)
1053 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1056 /* (x & y) + (x | y) -> x + y */
1058 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1061 /* (x + y) - (x | y) -> x & y */
1063 (minus (plus @0 @1) (bit_ior @0 @1))
1064 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1065 && !TYPE_SATURATING (type))
1068 /* (x + y) - (x & y) -> x | y */
1070 (minus (plus @0 @1) (bit_and @0 @1))
1071 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1072 && !TYPE_SATURATING (type))
1075 /* (x | y) - (x ^ y) -> x & y */
1077 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1080 /* (x | y) - (x & y) -> x ^ y */
1082 (minus (bit_ior @0 @1) (bit_and @0 @1))
1085 /* (x | y) & ~(x & y) -> x ^ y */
1087 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1090 /* (x | y) & (~x ^ y) -> x & y */
1092 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1095 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1097 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1098 (bit_not (bit_xor @0 @1)))
1100 /* (~x | y) ^ (x | ~y) -> x ^ y */
1102 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1105 /* ~x & ~y -> ~(x | y)
1106 ~x | ~y -> ~(x & y) */
1107 (for op (bit_and bit_ior)
1108 rop (bit_ior bit_and)
1110 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1111 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1112 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1113 (bit_not (rop (convert @0) (convert @1))))))
1115 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1116 with a constant, and the two constants have no bits in common,
1117 we should treat this as a BIT_IOR_EXPR since this may produce more
1119 (for op (bit_xor plus)
1121 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1122 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1123 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1124 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1125 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1126 (bit_ior (convert @4) (convert @5)))))
1128 /* (X | Y) ^ X -> Y & ~ X*/
1130 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1131 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1132 (convert (bit_and @1 (bit_not @0)))))
1134 /* Convert ~X ^ ~Y to X ^ Y. */
1136 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1137 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1138 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1139 (bit_xor (convert @0) (convert @1))))
1141 /* Convert ~X ^ C to X ^ ~C. */
1143 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1144 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1145 (bit_xor (convert @0) (bit_not @1))))
1147 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1148 (for opo (bit_and bit_xor)
1149 opi (bit_xor bit_and)
1151 (opo:c (opi:cs @0 @1) @1)
1152 (bit_and (bit_not @0) @1)))
1154 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1155 operands are another bit-wise operation with a common input. If so,
1156 distribute the bit operations to save an operation and possibly two if
1157 constants are involved. For example, convert
1158 (A | B) & (A | C) into A | (B & C)
1159 Further simplification will occur if B and C are constants. */
1160 (for op (bit_and bit_ior bit_xor)
1161 rop (bit_ior bit_and bit_and)
1163 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1164 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1165 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1166 (rop (convert @0) (op (convert @1) (convert @2))))))
1168 /* Some simple reassociation for bit operations, also handled in reassoc. */
1169 /* (X & Y) & Y -> X & Y
1170 (X | Y) | Y -> X | Y */
1171 (for op (bit_and bit_ior)
1173 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1175 /* (X ^ Y) ^ Y -> X */
1177 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1179 /* (X & Y) & (X & Z) -> (X & Y) & Z
1180 (X | Y) | (X | Z) -> (X | Y) | Z */
1181 (for op (bit_and bit_ior)
1183 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1184 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1185 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1186 (if (single_use (@5) && single_use (@6))
1187 (op @3 (convert @2))
1188 (if (single_use (@3) && single_use (@4))
1189 (op (convert @1) @5))))))
1190 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1192 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1193 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1194 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1195 (bit_xor (convert @1) (convert @2))))
1197 /* Convert abs (abs (X)) into abs (X).
1198 also absu (absu (X)) into absu (X). */
1204 (absu (convert@2 (absu@1 @0)))
1205 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1208 /* Convert abs[u] (-X) -> abs[u] (X). */
1217 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1219 (abs tree_expr_nonnegative_p@0)
1223 (absu tree_expr_nonnegative_p@0)
1226 /* A few cases of fold-const.c negate_expr_p predicate. */
1227 (match negate_expr_p
1229 (if ((INTEGRAL_TYPE_P (type)
1230 && TYPE_UNSIGNED (type))
1231 || (!TYPE_OVERFLOW_SANITIZED (type)
1232 && may_negate_without_overflow_p (t)))))
1233 (match negate_expr_p
1235 (match negate_expr_p
1237 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1238 (match negate_expr_p
1240 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1241 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1243 (match negate_expr_p
1245 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1246 (match negate_expr_p
1248 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1249 || (FLOAT_TYPE_P (type)
1250 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1251 && !HONOR_SIGNED_ZEROS (type)))))
1253 /* (-A) * (-B) -> A * B */
1255 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1256 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1257 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1258 (mult (convert @0) (convert (negate @1)))))
1260 /* -(A + B) -> (-B) - A. */
1262 (negate (plus:c @0 negate_expr_p@1))
1263 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1264 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1265 (minus (negate @1) @0)))
1267 /* -(A - B) -> B - A. */
1269 (negate (minus @0 @1))
1270 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1271 || (FLOAT_TYPE_P (type)
1272 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1273 && !HONOR_SIGNED_ZEROS (type)))
1276 (negate (pointer_diff @0 @1))
1277 (if (TYPE_OVERFLOW_UNDEFINED (type))
1278 (pointer_diff @1 @0)))
1280 /* A - B -> A + (-B) if B is easily negatable. */
1282 (minus @0 negate_expr_p@1)
1283 (if (!FIXED_POINT_TYPE_P (type))
1284 (plus @0 (negate @1))))
1286 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1288 For bitwise binary operations apply operand conversions to the
1289 binary operation result instead of to the operands. This allows
1290 to combine successive conversions and bitwise binary operations.
1291 We combine the above two cases by using a conditional convert. */
1292 (for bitop (bit_and bit_ior bit_xor)
1294 (bitop (convert @0) (convert? @1))
1295 (if (((TREE_CODE (@1) == INTEGER_CST
1296 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1297 && int_fits_type_p (@1, TREE_TYPE (@0)))
1298 || types_match (@0, @1))
1299 /* ??? This transform conflicts with fold-const.c doing
1300 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1301 constants (if x has signed type, the sign bit cannot be set
1302 in c). This folds extension into the BIT_AND_EXPR.
1303 Restrict it to GIMPLE to avoid endless recursions. */
1304 && (bitop != BIT_AND_EXPR || GIMPLE)
1305 && (/* That's a good idea if the conversion widens the operand, thus
1306 after hoisting the conversion the operation will be narrower. */
1307 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1308 /* It's also a good idea if the conversion is to a non-integer
1310 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1311 /* Or if the precision of TO is not the same as the precision
1313 || !type_has_mode_precision_p (type)))
1314 (convert (bitop @0 (convert @1))))))
1316 (for bitop (bit_and bit_ior)
1317 rbitop (bit_ior bit_and)
1318 /* (x | y) & x -> x */
1319 /* (x & y) | x -> x */
1321 (bitop:c (rbitop:c @0 @1) @0)
1323 /* (~x | y) & x -> x & y */
1324 /* (~x & y) | x -> x | y */
1326 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1329 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1331 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1332 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1334 /* Combine successive equal operations with constants. */
1335 (for bitop (bit_and bit_ior bit_xor)
1337 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1338 (if (!CONSTANT_CLASS_P (@0))
1339 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1340 folded to a constant. */
1341 (bitop @0 (bitop @1 @2))
1342 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1343 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1344 the values involved are such that the operation can't be decided at
1345 compile time. Try folding one of @0 or @1 with @2 to see whether
1346 that combination can be decided at compile time.
1348 Keep the existing form if both folds fail, to avoid endless
1350 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1352 (bitop @1 { cst1; })
1353 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1355 (bitop @0 { cst2; }))))))))
1357 /* Try simple folding for X op !X, and X op X with the help
1358 of the truth_valued_p and logical_inverted_value predicates. */
1359 (match truth_valued_p
1361 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1362 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1363 (match truth_valued_p
1365 (match truth_valued_p
1368 (match (logical_inverted_value @0)
1370 (match (logical_inverted_value @0)
1371 (bit_not truth_valued_p@0))
1372 (match (logical_inverted_value @0)
1373 (eq @0 integer_zerop))
1374 (match (logical_inverted_value @0)
1375 (ne truth_valued_p@0 integer_truep))
1376 (match (logical_inverted_value @0)
1377 (bit_xor truth_valued_p@0 integer_truep))
1381 (bit_and:c @0 (logical_inverted_value @0))
1382 { build_zero_cst (type); })
1383 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1384 (for op (bit_ior bit_xor)
1386 (op:c truth_valued_p@0 (logical_inverted_value @0))
1387 { constant_boolean_node (true, type); }))
1388 /* X ==/!= !X is false/true. */
1391 (op:c truth_valued_p@0 (logical_inverted_value @0))
1392 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1396 (bit_not (bit_not @0))
1399 /* Convert ~ (-A) to A - 1. */
1401 (bit_not (convert? (negate @0)))
1402 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1403 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1404 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1406 /* Convert - (~A) to A + 1. */
1408 (negate (nop_convert (bit_not @0)))
1409 (plus (view_convert @0) { build_each_one_cst (type); }))
1411 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1413 (bit_not (convert? (minus @0 integer_each_onep)))
1414 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1415 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1416 (convert (negate @0))))
1418 (bit_not (convert? (plus @0 integer_all_onesp)))
1419 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1420 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1421 (convert (negate @0))))
1423 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1425 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1426 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1427 (convert (bit_xor @0 (bit_not @1)))))
1429 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1430 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1431 (convert (bit_xor @0 @1))))
1433 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1435 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1436 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1437 (bit_not (bit_xor (view_convert @0) @1))))
1439 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1441 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1442 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1444 /* Fold A - (A & B) into ~B & A. */
1446 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1447 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1448 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1449 (convert (bit_and (bit_not @1) @0))))
1451 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1452 (for cmp (gt lt ge le)
1454 (mult (convert (cmp @0 @1)) @2)
1455 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1457 /* For integral types with undefined overflow and C != 0 fold
1458 x * C EQ/NE y * C into x EQ/NE y. */
1461 (cmp (mult:c @0 @1) (mult:c @2 @1))
1462 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1463 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1464 && tree_expr_nonzero_p (@1))
1467 /* For integral types with wrapping overflow and C odd fold
1468 x * C EQ/NE y * C into x EQ/NE y. */
1471 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1472 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1473 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1474 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1477 /* For integral types with undefined overflow and C != 0 fold
1478 x * C RELOP y * C into:
1480 x RELOP y for nonnegative C
1481 y RELOP x for negative C */
1482 (for cmp (lt gt le ge)
1484 (cmp (mult:c @0 @1) (mult:c @2 @1))
1485 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1486 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1487 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1489 (if (TREE_CODE (@1) == INTEGER_CST
1490 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1493 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1497 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1498 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1499 && TYPE_UNSIGNED (TREE_TYPE (@0))
1500 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1501 && (wi::to_wide (@2)
1502 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1503 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1504 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1506 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1507 (for cmp (simple_comparison)
1509 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1510 (if (element_precision (@3) >= element_precision (@0)
1511 && types_match (@0, @1))
1512 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1513 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1515 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1518 tree utype = unsigned_type_for (TREE_TYPE (@0));
1520 (cmp (convert:utype @1) (convert:utype @0)))))
1521 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1522 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1526 tree utype = unsigned_type_for (TREE_TYPE (@0));
1528 (cmp (convert:utype @0) (convert:utype @1)))))))))
1530 /* X / C1 op C2 into a simple range test. */
1531 (for cmp (simple_comparison)
1533 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1534 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1535 && integer_nonzerop (@1)
1536 && !TREE_OVERFLOW (@1)
1537 && !TREE_OVERFLOW (@2))
1538 (with { tree lo, hi; bool neg_overflow;
1539 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1542 (if (code == LT_EXPR || code == GE_EXPR)
1543 (if (TREE_OVERFLOW (lo))
1544 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1545 (if (code == LT_EXPR)
1548 (if (code == LE_EXPR || code == GT_EXPR)
1549 (if (TREE_OVERFLOW (hi))
1550 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1551 (if (code == LE_EXPR)
1555 { build_int_cst (type, code == NE_EXPR); })
1556 (if (code == EQ_EXPR && !hi)
1558 (if (code == EQ_EXPR && !lo)
1560 (if (code == NE_EXPR && !hi)
1562 (if (code == NE_EXPR && !lo)
1565 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1569 tree etype = range_check_type (TREE_TYPE (@0));
1572 if (! TYPE_UNSIGNED (etype))
1573 etype = unsigned_type_for (etype);
1574 hi = fold_convert (etype, hi);
1575 lo = fold_convert (etype, lo);
1576 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1579 (if (etype && hi && !TREE_OVERFLOW (hi))
1580 (if (code == EQ_EXPR)
1581 (le (minus (convert:etype @0) { lo; }) { hi; })
1582 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1584 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1585 (for op (lt le ge gt)
1587 (op (plus:c @0 @2) (plus:c @1 @2))
1588 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1589 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1591 /* For equality and subtraction, this is also true with wrapping overflow. */
1592 (for op (eq ne minus)
1594 (op (plus:c @0 @2) (plus:c @1 @2))
1595 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1596 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1597 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1600 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1601 (for op (lt le ge gt)
1603 (op (minus @0 @2) (minus @1 @2))
1604 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1605 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1607 /* For equality and subtraction, this is also true with wrapping overflow. */
1608 (for op (eq ne minus)
1610 (op (minus @0 @2) (minus @1 @2))
1611 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1612 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1613 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1615 /* And for pointers... */
1616 (for op (simple_comparison)
1618 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1619 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1622 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1623 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1624 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1625 (pointer_diff @0 @1)))
1627 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1628 (for op (lt le ge gt)
1630 (op (minus @2 @0) (minus @2 @1))
1631 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1632 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1634 /* For equality and subtraction, this is also true with wrapping overflow. */
1635 (for op (eq ne minus)
1637 (op (minus @2 @0) (minus @2 @1))
1638 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1639 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1640 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1642 /* And for pointers... */
1643 (for op (simple_comparison)
1645 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1646 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1649 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1650 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1651 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1652 (pointer_diff @1 @0)))
1654 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1655 (for op (lt le gt ge)
1657 (op:c (plus:c@2 @0 @1) @1)
1658 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1659 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1660 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1661 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1662 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1663 /* For equality, this is also true with wrapping overflow. */
1666 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1667 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1668 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1669 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1670 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1671 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1672 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1673 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1675 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1676 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1677 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1678 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1679 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1681 /* X - Y < X is the same as Y > 0 when there is no overflow.
1682 For equality, this is also true with wrapping overflow. */
1683 (for op (simple_comparison)
1685 (op:c @0 (minus@2 @0 @1))
1686 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1687 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1688 || ((op == EQ_EXPR || op == NE_EXPR)
1689 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1690 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1691 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1694 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1695 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1699 (cmp (trunc_div @0 @1) integer_zerop)
1700 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1701 /* Complex ==/!= is allowed, but not </>=. */
1702 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1703 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1706 /* X == C - X can never be true if C is odd. */
1709 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1710 (if (TREE_INT_CST_LOW (@1) & 1)
1711 { constant_boolean_node (cmp == NE_EXPR, type); })))
1713 /* Arguments on which one can call get_nonzero_bits to get the bits
1715 (match with_possible_nonzero_bits
1717 (match with_possible_nonzero_bits
1719 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1720 /* Slightly extended version, do not make it recursive to keep it cheap. */
1721 (match (with_possible_nonzero_bits2 @0)
1722 with_possible_nonzero_bits@0)
1723 (match (with_possible_nonzero_bits2 @0)
1724 (bit_and:c with_possible_nonzero_bits@0 @2))
1726 /* Same for bits that are known to be set, but we do not have
1727 an equivalent to get_nonzero_bits yet. */
1728 (match (with_certain_nonzero_bits2 @0)
1730 (match (with_certain_nonzero_bits2 @0)
1731 (bit_ior @1 INTEGER_CST@0))
1733 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1736 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1737 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1738 { constant_boolean_node (cmp == NE_EXPR, type); })))
1740 /* ((X inner_op C0) outer_op C1)
1741 With X being a tree where value_range has reasoned certain bits to always be
1742 zero throughout its computed value range,
1743 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1744 where zero_mask has 1's for all bits that are sure to be 0 in
1746 if (inner_op == '^') C0 &= ~C1;
1747 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1748 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1750 (for inner_op (bit_ior bit_xor)
1751 outer_op (bit_xor bit_ior)
1754 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1758 wide_int zero_mask_not;
1762 if (TREE_CODE (@2) == SSA_NAME)
1763 zero_mask_not = get_nonzero_bits (@2);
1767 if (inner_op == BIT_XOR_EXPR)
1769 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1770 cst_emit = C0 | wi::to_wide (@1);
1774 C0 = wi::to_wide (@0);
1775 cst_emit = C0 ^ wi::to_wide (@1);
1778 (if (!fail && (C0 & zero_mask_not) == 0)
1779 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1780 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1781 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1783 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1785 (pointer_plus (pointer_plus:s @0 @1) @3)
1786 (pointer_plus @0 (plus @1 @3)))
1792 tem4 = (unsigned long) tem3;
1797 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1798 /* Conditionally look through a sign-changing conversion. */
1799 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1800 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1801 || (GENERIC && type == TREE_TYPE (@1))))
1804 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1805 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1809 tem = (sizetype) ptr;
1813 and produce the simpler and easier to analyze with respect to alignment
1814 ... = ptr & ~algn; */
1816 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1817 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1818 (bit_and @0 { algn; })))
1820 /* Try folding difference of addresses. */
1822 (minus (convert ADDR_EXPR@0) (convert @1))
1823 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1824 (with { poly_int64 diff; }
1825 (if (ptr_difference_const (@0, @1, &diff))
1826 { build_int_cst_type (type, diff); }))))
1828 (minus (convert @0) (convert ADDR_EXPR@1))
1829 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1830 (with { poly_int64 diff; }
1831 (if (ptr_difference_const (@0, @1, &diff))
1832 { build_int_cst_type (type, diff); }))))
1834 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1835 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1836 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1837 (with { poly_int64 diff; }
1838 (if (ptr_difference_const (@0, @1, &diff))
1839 { build_int_cst_type (type, diff); }))))
1841 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1842 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1843 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1844 (with { poly_int64 diff; }
1845 (if (ptr_difference_const (@0, @1, &diff))
1846 { build_int_cst_type (type, diff); }))))
1848 /* If arg0 is derived from the address of an object or function, we may
1849 be able to fold this expression using the object or function's
1852 (bit_and (convert? @0) INTEGER_CST@1)
1853 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1854 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1858 unsigned HOST_WIDE_INT bitpos;
1859 get_pointer_alignment_1 (@0, &align, &bitpos);
1861 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1862 { wide_int_to_tree (type, (wi::to_wide (@1)
1863 & (bitpos / BITS_PER_UNIT))); }))))
1866 /* We can't reassociate at all for saturating types. */
1867 (if (!TYPE_SATURATING (type))
1869 /* Contract negates. */
1870 /* A + (-B) -> A - B */
1872 (plus:c @0 (convert? (negate @1)))
1873 /* Apply STRIP_NOPS on the negate. */
1874 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1875 && !TYPE_OVERFLOW_SANITIZED (type))
1879 if (INTEGRAL_TYPE_P (type)
1880 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1881 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1883 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1884 /* A - (-B) -> A + B */
1886 (minus @0 (convert? (negate @1)))
1887 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1888 && !TYPE_OVERFLOW_SANITIZED (type))
1892 if (INTEGRAL_TYPE_P (type)
1893 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1894 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1896 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1898 Sign-extension is ok except for INT_MIN, which thankfully cannot
1899 happen without overflow. */
1901 (negate (convert (negate @1)))
1902 (if (INTEGRAL_TYPE_P (type)
1903 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1904 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1905 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1906 && !TYPE_OVERFLOW_SANITIZED (type)
1907 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1910 (negate (convert negate_expr_p@1))
1911 (if (SCALAR_FLOAT_TYPE_P (type)
1912 && ((DECIMAL_FLOAT_TYPE_P (type)
1913 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1914 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1915 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1916 (convert (negate @1))))
1918 (negate (nop_convert (negate @1)))
1919 (if (!TYPE_OVERFLOW_SANITIZED (type)
1920 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1923 /* We can't reassociate floating-point unless -fassociative-math
1924 or fixed-point plus or minus because of saturation to +-Inf. */
1925 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1926 && !FIXED_POINT_TYPE_P (type))
1928 /* Match patterns that allow contracting a plus-minus pair
1929 irrespective of overflow issues. */
1930 /* (A +- B) - A -> +- B */
1931 /* (A +- B) -+ B -> A */
1932 /* A - (A +- B) -> -+ B */
1933 /* A +- (B -+ A) -> +- B */
1935 (minus (plus:c @0 @1) @0)
1938 (minus (minus @0 @1) @0)
1941 (plus:c (minus @0 @1) @1)
1944 (minus @0 (plus:c @0 @1))
1947 (minus @0 (minus @0 @1))
1949 /* (A +- B) + (C - A) -> C +- B */
1950 /* (A + B) - (A - C) -> B + C */
1951 /* More cases are handled with comparisons. */
1953 (plus:c (plus:c @0 @1) (minus @2 @0))
1956 (plus:c (minus @0 @1) (minus @2 @0))
1959 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1960 (if (TYPE_OVERFLOW_UNDEFINED (type)
1961 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1962 (pointer_diff @2 @1)))
1964 (minus (plus:c @0 @1) (minus @0 @2))
1967 /* (A +- CST1) +- CST2 -> A + CST3
1968 Use view_convert because it is safe for vectors and equivalent for
1970 (for outer_op (plus minus)
1971 (for inner_op (plus minus)
1972 neg_inner_op (minus plus)
1974 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1976 /* If one of the types wraps, use that one. */
1977 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1978 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1979 forever if something doesn't simplify into a constant. */
1980 (if (!CONSTANT_CLASS_P (@0))
1981 (if (outer_op == PLUS_EXPR)
1982 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1983 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1984 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1985 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1986 (if (outer_op == PLUS_EXPR)
1987 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1988 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1989 /* If the constant operation overflows we cannot do the transform
1990 directly as we would introduce undefined overflow, for example
1991 with (a - 1) + INT_MIN. */
1992 (if (types_match (type, @0))
1993 (with { tree cst = const_binop (outer_op == inner_op
1994 ? PLUS_EXPR : MINUS_EXPR,
1996 (if (cst && !TREE_OVERFLOW (cst))
1997 (inner_op @0 { cst; } )
1998 /* X+INT_MAX+1 is X-INT_MIN. */
1999 (if (INTEGRAL_TYPE_P (type) && cst
2000 && wi::to_wide (cst) == wi::min_value (type))
2001 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2002 /* Last resort, use some unsigned type. */
2003 (with { tree utype = unsigned_type_for (type); }
2005 (view_convert (inner_op
2006 (view_convert:utype @0)
2008 { drop_tree_overflow (cst); }))))))))))))))
2010 /* (CST1 - A) +- CST2 -> CST3 - A */
2011 (for outer_op (plus minus)
2013 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
2014 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2015 (if (cst && !TREE_OVERFLOW (cst))
2016 (minus { cst; } @0)))))
2018 /* CST1 - (CST2 - A) -> CST3 + A */
2020 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
2021 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2022 (if (cst && !TREE_OVERFLOW (cst))
2023 (plus { cst; } @0))))
2027 (plus:c (bit_not @0) @0)
2028 (if (!TYPE_OVERFLOW_TRAPS (type))
2029 { build_all_ones_cst (type); }))
2033 (plus (convert? (bit_not @0)) integer_each_onep)
2034 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2035 (negate (convert @0))))
2039 (minus (convert? (negate @0)) integer_each_onep)
2040 (if (!TYPE_OVERFLOW_TRAPS (type)
2041 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2042 (bit_not (convert @0))))
2046 (minus integer_all_onesp @0)
2049 /* (T)(P + A) - (T)P -> (T) A */
2051 (minus (convert (plus:c @@0 @1))
2053 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2054 /* For integer types, if A has a smaller type
2055 than T the result depends on the possible
2057 E.g. T=size_t, A=(unsigned)429497295, P>0.
2058 However, if an overflow in P + A would cause
2059 undefined behavior, we can assume that there
2061 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2062 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2065 (minus (convert (pointer_plus @@0 @1))
2067 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2068 /* For pointer types, if the conversion of A to the
2069 final type requires a sign- or zero-extension,
2070 then we have to punt - it is not defined which
2072 || (POINTER_TYPE_P (TREE_TYPE (@0))
2073 && TREE_CODE (@1) == INTEGER_CST
2074 && tree_int_cst_sign_bit (@1) == 0))
2077 (pointer_diff (pointer_plus @@0 @1) @0)
2078 /* The second argument of pointer_plus must be interpreted as signed, and
2079 thus sign-extended if necessary. */
2080 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2081 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2082 second arg is unsigned even when we need to consider it as signed,
2083 we don't want to diagnose overflow here. */
2084 (convert (view_convert:stype @1))))
2086 /* (T)P - (T)(P + A) -> -(T) A */
2088 (minus (convert? @0)
2089 (convert (plus:c @@0 @1)))
2090 (if (INTEGRAL_TYPE_P (type)
2091 && TYPE_OVERFLOW_UNDEFINED (type)
2092 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2093 (with { tree utype = unsigned_type_for (type); }
2094 (convert (negate (convert:utype @1))))
2095 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2096 /* For integer types, if A has a smaller type
2097 than T the result depends on the possible
2099 E.g. T=size_t, A=(unsigned)429497295, P>0.
2100 However, if an overflow in P + A would cause
2101 undefined behavior, we can assume that there
2103 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2104 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2105 (negate (convert @1)))))
2108 (convert (pointer_plus @@0 @1)))
2109 (if (INTEGRAL_TYPE_P (type)
2110 && TYPE_OVERFLOW_UNDEFINED (type)
2111 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2112 (with { tree utype = unsigned_type_for (type); }
2113 (convert (negate (convert:utype @1))))
2114 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2115 /* For pointer types, if the conversion of A to the
2116 final type requires a sign- or zero-extension,
2117 then we have to punt - it is not defined which
2119 || (POINTER_TYPE_P (TREE_TYPE (@0))
2120 && TREE_CODE (@1) == INTEGER_CST
2121 && tree_int_cst_sign_bit (@1) == 0))
2122 (negate (convert @1)))))
2124 (pointer_diff @0 (pointer_plus @@0 @1))
2125 /* The second argument of pointer_plus must be interpreted as signed, and
2126 thus sign-extended if necessary. */
2127 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2128 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2129 second arg is unsigned even when we need to consider it as signed,
2130 we don't want to diagnose overflow here. */
2131 (negate (convert (view_convert:stype @1)))))
2133 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2135 (minus (convert (plus:c @@0 @1))
2136 (convert (plus:c @0 @2)))
2137 (if (INTEGRAL_TYPE_P (type)
2138 && TYPE_OVERFLOW_UNDEFINED (type)
2139 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2140 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2141 (with { tree utype = unsigned_type_for (type); }
2142 (convert (minus (convert:utype @1) (convert:utype @2))))
2143 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2144 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2145 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2146 /* For integer types, if A has a smaller type
2147 than T the result depends on the possible
2149 E.g. T=size_t, A=(unsigned)429497295, P>0.
2150 However, if an overflow in P + A would cause
2151 undefined behavior, we can assume that there
2153 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2154 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2155 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2156 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2157 (minus (convert @1) (convert @2)))))
2159 (minus (convert (pointer_plus @@0 @1))
2160 (convert (pointer_plus @0 @2)))
2161 (if (INTEGRAL_TYPE_P (type)
2162 && TYPE_OVERFLOW_UNDEFINED (type)
2163 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2164 (with { tree utype = unsigned_type_for (type); }
2165 (convert (minus (convert:utype @1) (convert:utype @2))))
2166 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2167 /* For pointer types, if the conversion of A to the
2168 final type requires a sign- or zero-extension,
2169 then we have to punt - it is not defined which
2171 || (POINTER_TYPE_P (TREE_TYPE (@0))
2172 && TREE_CODE (@1) == INTEGER_CST
2173 && tree_int_cst_sign_bit (@1) == 0
2174 && TREE_CODE (@2) == INTEGER_CST
2175 && tree_int_cst_sign_bit (@2) == 0))
2176 (minus (convert @1) (convert @2)))))
2178 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2179 /* The second argument of pointer_plus must be interpreted as signed, and
2180 thus sign-extended if necessary. */
2181 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2182 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2183 second arg is unsigned even when we need to consider it as signed,
2184 we don't want to diagnose overflow here. */
2185 (minus (convert (view_convert:stype @1))
2186 (convert (view_convert:stype @2)))))))
2188 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2189 Modeled after fold_plusminus_mult_expr. */
2190 (if (!TYPE_SATURATING (type)
2191 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2192 (for plusminus (plus minus)
2194 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2195 (if ((!ANY_INTEGRAL_TYPE_P (type)
2196 || TYPE_OVERFLOW_WRAPS (type)
2197 || (INTEGRAL_TYPE_P (type)
2198 && tree_expr_nonzero_p (@0)
2199 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2200 /* If @1 +- @2 is constant require a hard single-use on either
2201 original operand (but not on both). */
2202 && (single_use (@3) || single_use (@4)))
2203 (mult (plusminus @1 @2) @0)))
2204 /* We cannot generate constant 1 for fract. */
2205 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2207 (plusminus @0 (mult:c@3 @0 @2))
2208 (if ((!ANY_INTEGRAL_TYPE_P (type)
2209 || TYPE_OVERFLOW_WRAPS (type)
2210 || (INTEGRAL_TYPE_P (type)
2211 && tree_expr_nonzero_p (@0)
2212 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2214 (mult (plusminus { build_one_cst (type); } @2) @0)))
2216 (plusminus (mult:c@3 @0 @2) @0)
2217 (if ((!ANY_INTEGRAL_TYPE_P (type)
2218 || TYPE_OVERFLOW_WRAPS (type)
2219 || (INTEGRAL_TYPE_P (type)
2220 && tree_expr_nonzero_p (@0)
2221 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2223 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2225 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2227 (for minmax (min max FMIN_ALL FMAX_ALL)
2231 /* min(max(x,y),y) -> y. */
2233 (min:c (max:c @0 @1) @1)
2235 /* max(min(x,y),y) -> y. */
2237 (max:c (min:c @0 @1) @1)
2239 /* max(a,-a) -> abs(a). */
2241 (max:c @0 (negate @0))
2242 (if (TREE_CODE (type) != COMPLEX_TYPE
2243 && (! ANY_INTEGRAL_TYPE_P (type)
2244 || TYPE_OVERFLOW_UNDEFINED (type)))
2246 /* min(a,-a) -> -abs(a). */
2248 (min:c @0 (negate @0))
2249 (if (TREE_CODE (type) != COMPLEX_TYPE
2250 && (! ANY_INTEGRAL_TYPE_P (type)
2251 || TYPE_OVERFLOW_UNDEFINED (type)))
2256 (if (INTEGRAL_TYPE_P (type)
2257 && TYPE_MIN_VALUE (type)
2258 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2260 (if (INTEGRAL_TYPE_P (type)
2261 && TYPE_MAX_VALUE (type)
2262 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2267 (if (INTEGRAL_TYPE_P (type)
2268 && TYPE_MAX_VALUE (type)
2269 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2271 (if (INTEGRAL_TYPE_P (type)
2272 && TYPE_MIN_VALUE (type)
2273 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2276 /* max (a, a + CST) -> a + CST where CST is positive. */
2277 /* max (a, a + CST) -> a where CST is negative. */
2279 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2280 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2281 (if (tree_int_cst_sgn (@1) > 0)
2285 /* min (a, a + CST) -> a where CST is positive. */
2286 /* min (a, a + CST) -> a + CST where CST is negative. */
2288 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2289 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2290 (if (tree_int_cst_sgn (@1) > 0)
2294 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2295 and the outer convert demotes the expression back to x's type. */
2296 (for minmax (min max)
2298 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2299 (if (INTEGRAL_TYPE_P (type)
2300 && types_match (@1, type) && int_fits_type_p (@2, type)
2301 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2302 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2303 (minmax @1 (convert @2)))))
2305 (for minmax (FMIN_ALL FMAX_ALL)
2306 /* If either argument is NaN, return the other one. Avoid the
2307 transformation if we get (and honor) a signalling NaN. */
2309 (minmax:c @0 REAL_CST@1)
2310 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2311 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2313 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2314 functions to return the numeric arg if the other one is NaN.
2315 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2316 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2317 worry about it either. */
2318 (if (flag_finite_math_only)
2325 /* min (-A, -B) -> -max (A, B) */
2326 (for minmax (min max FMIN_ALL FMAX_ALL)
2327 maxmin (max min FMAX_ALL FMIN_ALL)
2329 (minmax (negate:s@2 @0) (negate:s@3 @1))
2330 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2331 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2332 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2333 (negate (maxmin @0 @1)))))
2334 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2335 MAX (~X, ~Y) -> ~MIN (X, Y) */
2336 (for minmax (min max)
2339 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2340 (bit_not (maxmin @0 @1))))
2342 /* MIN (X, Y) == X -> X <= Y */
2343 (for minmax (min min max max)
2347 (cmp:c (minmax:c @0 @1) @0)
2348 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2350 /* MIN (X, 5) == 0 -> X == 0
2351 MIN (X, 5) == 7 -> false */
2354 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2355 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2356 TYPE_SIGN (TREE_TYPE (@0))))
2357 { constant_boolean_node (cmp == NE_EXPR, type); }
2358 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2359 TYPE_SIGN (TREE_TYPE (@0))))
2363 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2364 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2365 TYPE_SIGN (TREE_TYPE (@0))))
2366 { constant_boolean_node (cmp == NE_EXPR, type); }
2367 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2368 TYPE_SIGN (TREE_TYPE (@0))))
2370 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2371 (for minmax (min min max max min min max max )
2372 cmp (lt le gt ge gt ge lt le )
2373 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2375 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2376 (comb (cmp @0 @2) (cmp @1 @2))))
2378 /* Simplifications of shift and rotates. */
2380 (for rotate (lrotate rrotate)
2382 (rotate integer_all_onesp@0 @1)
2385 /* Optimize -1 >> x for arithmetic right shifts. */
2387 (rshift integer_all_onesp@0 @1)
2388 (if (!TYPE_UNSIGNED (type)
2389 && tree_expr_nonnegative_p (@1))
2392 /* Optimize (x >> c) << c into x & (-1<<c). */
2394 (lshift (rshift @0 INTEGER_CST@1) @1)
2395 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2396 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2398 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2401 (rshift (lshift @0 INTEGER_CST@1) @1)
2402 (if (TYPE_UNSIGNED (type)
2403 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2404 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2406 (for shiftrotate (lrotate rrotate lshift rshift)
2408 (shiftrotate @0 integer_zerop)
2411 (shiftrotate integer_zerop@0 @1)
2413 /* Prefer vector1 << scalar to vector1 << vector2
2414 if vector2 is uniform. */
2415 (for vec (VECTOR_CST CONSTRUCTOR)
2417 (shiftrotate @0 vec@1)
2418 (with { tree tem = uniform_vector_p (@1); }
2420 (shiftrotate @0 { tem; }))))))
2422 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2423 Y is 0. Similarly for X >> Y. */
2425 (for shift (lshift rshift)
2427 (shift @0 SSA_NAME@1)
2428 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2430 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2431 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2433 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2437 /* Rewrite an LROTATE_EXPR by a constant into an
2438 RROTATE_EXPR by a new constant. */
2440 (lrotate @0 INTEGER_CST@1)
2441 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2442 build_int_cst (TREE_TYPE (@1),
2443 element_precision (type)), @1); }))
2445 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2446 (for op (lrotate rrotate rshift lshift)
2448 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2449 (with { unsigned int prec = element_precision (type); }
2450 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2451 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2452 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2453 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2454 (with { unsigned int low = (tree_to_uhwi (@1)
2455 + tree_to_uhwi (@2)); }
2456 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2457 being well defined. */
2459 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2460 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2461 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2462 { build_zero_cst (type); }
2463 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2464 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2467 /* ((1 << A) & 1) != 0 -> A == 0
2468 ((1 << A) & 1) == 0 -> A != 0 */
2472 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2473 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2475 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2476 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2480 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2481 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2483 || (!integer_zerop (@2)
2484 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2485 { constant_boolean_node (cmp == NE_EXPR, type); }
2486 (if (!integer_zerop (@2)
2487 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2488 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2490 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2491 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2492 if the new mask might be further optimized. */
2493 (for shift (lshift rshift)
2495 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2497 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2498 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2499 && tree_fits_uhwi_p (@1)
2500 && tree_to_uhwi (@1) > 0
2501 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2504 unsigned int shiftc = tree_to_uhwi (@1);
2505 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2506 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2507 tree shift_type = TREE_TYPE (@3);
2510 if (shift == LSHIFT_EXPR)
2511 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2512 else if (shift == RSHIFT_EXPR
2513 && type_has_mode_precision_p (shift_type))
2515 prec = TYPE_PRECISION (TREE_TYPE (@3));
2517 /* See if more bits can be proven as zero because of
2520 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2522 tree inner_type = TREE_TYPE (@0);
2523 if (type_has_mode_precision_p (inner_type)
2524 && TYPE_PRECISION (inner_type) < prec)
2526 prec = TYPE_PRECISION (inner_type);
2527 /* See if we can shorten the right shift. */
2529 shift_type = inner_type;
2530 /* Otherwise X >> C1 is all zeros, so we'll optimize
2531 it into (X, 0) later on by making sure zerobits
2535 zerobits = HOST_WIDE_INT_M1U;
2538 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2539 zerobits <<= prec - shiftc;
2541 /* For arithmetic shift if sign bit could be set, zerobits
2542 can contain actually sign bits, so no transformation is
2543 possible, unless MASK masks them all away. In that
2544 case the shift needs to be converted into logical shift. */
2545 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2546 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2548 if ((mask & zerobits) == 0)
2549 shift_type = unsigned_type_for (TREE_TYPE (@3));
2555 /* ((X << 16) & 0xff00) is (X, 0). */
2556 (if ((mask & zerobits) == mask)
2557 { build_int_cst (type, 0); }
2558 (with { newmask = mask | zerobits; }
2559 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2562 /* Only do the transformation if NEWMASK is some integer
2564 for (prec = BITS_PER_UNIT;
2565 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2566 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2569 (if (prec < HOST_BITS_PER_WIDE_INT
2570 || newmask == HOST_WIDE_INT_M1U)
2572 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2573 (if (!tree_int_cst_equal (newmaskt, @2))
2574 (if (shift_type != TREE_TYPE (@3))
2575 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2576 (bit_and @4 { newmaskt; })))))))))))))
2578 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2579 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2580 (for shift (lshift rshift)
2581 (for bit_op (bit_and bit_xor bit_ior)
2583 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2584 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2585 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2586 (bit_op (shift (convert @0) @1) { mask; }))))))
2588 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2590 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2591 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2592 && (element_precision (TREE_TYPE (@0))
2593 <= element_precision (TREE_TYPE (@1))
2594 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2596 { tree shift_type = TREE_TYPE (@0); }
2597 (convert (rshift (convert:shift_type @1) @2)))))
2599 /* ~(~X >>r Y) -> X >>r Y
2600 ~(~X <<r Y) -> X <<r Y */
2601 (for rotate (lrotate rrotate)
2603 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2604 (if ((element_precision (TREE_TYPE (@0))
2605 <= element_precision (TREE_TYPE (@1))
2606 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2607 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2608 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2610 { tree rotate_type = TREE_TYPE (@0); }
2611 (convert (rotate (convert:rotate_type @1) @2))))))
2613 /* Simplifications of conversions. */
2615 /* Basic strip-useless-type-conversions / strip_nops. */
2616 (for cvt (convert view_convert float fix_trunc)
2619 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2620 || (GENERIC && type == TREE_TYPE (@0)))
2623 /* Contract view-conversions. */
2625 (view_convert (view_convert @0))
2628 /* For integral conversions with the same precision or pointer
2629 conversions use a NOP_EXPR instead. */
2632 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2633 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2634 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2637 /* Strip inner integral conversions that do not change precision or size, or
2638 zero-extend while keeping the same size (for bool-to-char). */
2640 (view_convert (convert@0 @1))
2641 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2642 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2643 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2644 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2645 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2646 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2649 /* Simplify a view-converted empty constructor. */
2651 (view_convert CONSTRUCTOR@0)
2652 (if (TREE_CODE (@0) != SSA_NAME
2653 && CONSTRUCTOR_NELTS (@0) == 0)
2654 { build_zero_cst (type); }))
2656 /* Re-association barriers around constants and other re-association
2657 barriers can be removed. */
2659 (paren CONSTANT_CLASS_P@0)
2662 (paren (paren@1 @0))
2665 /* Handle cases of two conversions in a row. */
2666 (for ocvt (convert float fix_trunc)
2667 (for icvt (convert float)
2672 tree inside_type = TREE_TYPE (@0);
2673 tree inter_type = TREE_TYPE (@1);
2674 int inside_int = INTEGRAL_TYPE_P (inside_type);
2675 int inside_ptr = POINTER_TYPE_P (inside_type);
2676 int inside_float = FLOAT_TYPE_P (inside_type);
2677 int inside_vec = VECTOR_TYPE_P (inside_type);
2678 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2679 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2680 int inter_int = INTEGRAL_TYPE_P (inter_type);
2681 int inter_ptr = POINTER_TYPE_P (inter_type);
2682 int inter_float = FLOAT_TYPE_P (inter_type);
2683 int inter_vec = VECTOR_TYPE_P (inter_type);
2684 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2685 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2686 int final_int = INTEGRAL_TYPE_P (type);
2687 int final_ptr = POINTER_TYPE_P (type);
2688 int final_float = FLOAT_TYPE_P (type);
2689 int final_vec = VECTOR_TYPE_P (type);
2690 unsigned int final_prec = TYPE_PRECISION (type);
2691 int final_unsignedp = TYPE_UNSIGNED (type);
2694 /* In addition to the cases of two conversions in a row
2695 handled below, if we are converting something to its own
2696 type via an object of identical or wider precision, neither
2697 conversion is needed. */
2698 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2700 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2701 && (((inter_int || inter_ptr) && final_int)
2702 || (inter_float && final_float))
2703 && inter_prec >= final_prec)
2706 /* Likewise, if the intermediate and initial types are either both
2707 float or both integer, we don't need the middle conversion if the
2708 former is wider than the latter and doesn't change the signedness
2709 (for integers). Avoid this if the final type is a pointer since
2710 then we sometimes need the middle conversion. */
2711 (if (((inter_int && inside_int) || (inter_float && inside_float))
2712 && (final_int || final_float)
2713 && inter_prec >= inside_prec
2714 && (inter_float || inter_unsignedp == inside_unsignedp))
2717 /* If we have a sign-extension of a zero-extended value, we can
2718 replace that by a single zero-extension. Likewise if the
2719 final conversion does not change precision we can drop the
2720 intermediate conversion. */
2721 (if (inside_int && inter_int && final_int
2722 && ((inside_prec < inter_prec && inter_prec < final_prec
2723 && inside_unsignedp && !inter_unsignedp)
2724 || final_prec == inter_prec))
2727 /* Two conversions in a row are not needed unless:
2728 - some conversion is floating-point (overstrict for now), or
2729 - some conversion is a vector (overstrict for now), or
2730 - the intermediate type is narrower than both initial and
2732 - the intermediate type and innermost type differ in signedness,
2733 and the outermost type is wider than the intermediate, or
2734 - the initial type is a pointer type and the precisions of the
2735 intermediate and final types differ, or
2736 - the final type is a pointer type and the precisions of the
2737 initial and intermediate types differ. */
2738 (if (! inside_float && ! inter_float && ! final_float
2739 && ! inside_vec && ! inter_vec && ! final_vec
2740 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2741 && ! (inside_int && inter_int
2742 && inter_unsignedp != inside_unsignedp
2743 && inter_prec < final_prec)
2744 && ((inter_unsignedp && inter_prec > inside_prec)
2745 == (final_unsignedp && final_prec > inter_prec))
2746 && ! (inside_ptr && inter_prec != final_prec)
2747 && ! (final_ptr && inside_prec != inter_prec))
2750 /* A truncation to an unsigned type (a zero-extension) should be
2751 canonicalized as bitwise and of a mask. */
2752 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2753 && final_int && inter_int && inside_int
2754 && final_prec == inside_prec
2755 && final_prec > inter_prec
2757 (convert (bit_and @0 { wide_int_to_tree
2759 wi::mask (inter_prec, false,
2760 TYPE_PRECISION (inside_type))); })))
2762 /* If we are converting an integer to a floating-point that can
2763 represent it exactly and back to an integer, we can skip the
2764 floating-point conversion. */
2765 (if (GIMPLE /* PR66211 */
2766 && inside_int && inter_float && final_int &&
2767 (unsigned) significand_size (TYPE_MODE (inter_type))
2768 >= inside_prec - !inside_unsignedp)
2771 /* If we have a narrowing conversion to an integral type that is fed by a
2772 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2773 masks off bits outside the final type (and nothing else). */
2775 (convert (bit_and @0 INTEGER_CST@1))
2776 (if (INTEGRAL_TYPE_P (type)
2777 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2778 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2779 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2780 TYPE_PRECISION (type)), 0))
2784 /* (X /[ex] A) * A -> X. */
2786 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2789 /* Simplify (A / B) * B + (A % B) -> A. */
2790 (for div (trunc_div ceil_div floor_div round_div)
2791 mod (trunc_mod ceil_mod floor_mod round_mod)
2793 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
2796 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
2797 (for op (plus minus)
2799 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2800 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2801 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
2804 wi::overflow_type overflow;
2805 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
2806 TYPE_SIGN (type), &overflow);
2808 (if (types_match (type, TREE_TYPE (@2))
2809 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
2810 (op @0 { wide_int_to_tree (type, mul); })
2811 (with { tree utype = unsigned_type_for (type); }
2812 (convert (op (convert:utype @0)
2813 (mult (convert:utype @1) (convert:utype @2))))))))))
2815 /* Canonicalization of binary operations. */
2817 /* Convert X + -C into X - C. */
2819 (plus @0 REAL_CST@1)
2820 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2821 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2822 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2823 (minus @0 { tem; })))))
2825 /* Convert x+x into x*2. */
2828 (if (SCALAR_FLOAT_TYPE_P (type))
2829 (mult @0 { build_real (type, dconst2); })
2830 (if (INTEGRAL_TYPE_P (type))
2831 (mult @0 { build_int_cst (type, 2); }))))
2835 (minus integer_zerop @1)
2838 (pointer_diff integer_zerop @1)
2839 (negate (convert @1)))
2841 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2842 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2843 (-ARG1 + ARG0) reduces to -ARG1. */
2845 (minus real_zerop@0 @1)
2846 (if (fold_real_zero_addition_p (type, @0, 0))
2849 /* Transform x * -1 into -x. */
2851 (mult @0 integer_minus_onep)
2854 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2855 signed overflow for CST != 0 && CST != -1. */
2857 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2858 (if (TREE_CODE (@2) != INTEGER_CST
2860 && !integer_zerop (@1) && !integer_minus_onep (@1))
2861 (mult (mult @0 @2) @1)))
2863 /* True if we can easily extract the real and imaginary parts of a complex
2865 (match compositional_complex
2866 (convert? (complex @0 @1)))
2868 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2870 (complex (realpart @0) (imagpart @0))
2873 (realpart (complex @0 @1))
2876 (imagpart (complex @0 @1))
2879 /* Sometimes we only care about half of a complex expression. */
2881 (realpart (convert?:s (conj:s @0)))
2882 (convert (realpart @0)))
2884 (imagpart (convert?:s (conj:s @0)))
2885 (convert (negate (imagpart @0))))
2886 (for part (realpart imagpart)
2887 (for op (plus minus)
2889 (part (convert?:s@2 (op:s @0 @1)))
2890 (convert (op (part @0) (part @1))))))
2892 (realpart (convert?:s (CEXPI:s @0)))
2895 (imagpart (convert?:s (CEXPI:s @0)))
2898 /* conj(conj(x)) -> x */
2900 (conj (convert? (conj @0)))
2901 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2904 /* conj({x,y}) -> {x,-y} */
2906 (conj (convert?:s (complex:s @0 @1)))
2907 (with { tree itype = TREE_TYPE (type); }
2908 (complex (convert:itype @0) (negate (convert:itype @1)))))
2910 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2911 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2916 (bswap (bit_not (bswap @0)))
2918 (for bitop (bit_xor bit_ior bit_and)
2920 (bswap (bitop:c (bswap @0) @1))
2921 (bitop @0 (bswap @1)))))
2924 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2926 /* Simplify constant conditions.
2927 Only optimize constant conditions when the selected branch
2928 has the same type as the COND_EXPR. This avoids optimizing
2929 away "c ? x : throw", where the throw has a void type.
2930 Note that we cannot throw away the fold-const.c variant nor
2931 this one as we depend on doing this transform before possibly
2932 A ? B : B -> B triggers and the fold-const.c one can optimize
2933 0 ? A : B to B even if A has side-effects. Something
2934 genmatch cannot handle. */
2936 (cond INTEGER_CST@0 @1 @2)
2937 (if (integer_zerop (@0))
2938 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2940 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2943 (vec_cond VECTOR_CST@0 @1 @2)
2944 (if (integer_all_onesp (@0))
2946 (if (integer_zerop (@0))
2949 /* Sink unary operations to constant branches, but only if we do fold it to
2951 (for op (negate bit_not abs absu)
2953 (op (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2))
2957 cst1 = const_unop (op, type, @1);
2959 cst2 = const_unop (op, type, @2);
2962 (vec_cond @0 { cst1; } { cst2; })))))
2964 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2966 /* This pattern implements two kinds simplification:
2969 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2970 1) Conversions are type widening from smaller type.
2971 2) Const c1 equals to c2 after canonicalizing comparison.
2972 3) Comparison has tree code LT, LE, GT or GE.
2973 This specific pattern is needed when (cmp (convert x) c) may not
2974 be simplified by comparison patterns because of multiple uses of
2975 x. It also makes sense here because simplifying across multiple
2976 referred var is always benefitial for complicated cases.
2979 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2980 (for cmp (lt le gt ge eq)
2982 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2985 tree from_type = TREE_TYPE (@1);
2986 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2987 enum tree_code code = ERROR_MARK;
2989 if (INTEGRAL_TYPE_P (from_type)
2990 && int_fits_type_p (@2, from_type)
2991 && (types_match (c1_type, from_type)
2992 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2993 && (TYPE_UNSIGNED (from_type)
2994 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2995 && (types_match (c2_type, from_type)
2996 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2997 && (TYPE_UNSIGNED (from_type)
2998 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3002 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3004 /* X <= Y - 1 equals to X < Y. */
3007 /* X > Y - 1 equals to X >= Y. */
3011 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3013 /* X < Y + 1 equals to X <= Y. */
3016 /* X >= Y + 1 equals to X > Y. */
3020 if (code != ERROR_MARK
3021 || wi::to_widest (@2) == wi::to_widest (@3))
3023 if (cmp == LT_EXPR || cmp == LE_EXPR)
3025 if (cmp == GT_EXPR || cmp == GE_EXPR)
3029 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3030 else if (int_fits_type_p (@3, from_type))
3034 (if (code == MAX_EXPR)
3035 (convert (max @1 (convert @2)))
3036 (if (code == MIN_EXPR)
3037 (convert (min @1 (convert @2)))
3038 (if (code == EQ_EXPR)
3039 (convert (cond (eq @1 (convert @3))
3040 (convert:from_type @3) (convert:from_type @2)))))))))
3042 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3044 1) OP is PLUS or MINUS.
3045 2) CMP is LT, LE, GT or GE.
3046 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3048 This pattern also handles special cases like:
3050 A) Operand x is a unsigned to signed type conversion and c1 is
3051 integer zero. In this case,
3052 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3053 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3054 B) Const c1 may not equal to (C3 op' C2). In this case we also
3055 check equality for (c1+1) and (c1-1) by adjusting comparison
3058 TODO: Though signed type is handled by this pattern, it cannot be
3059 simplified at the moment because C standard requires additional
3060 type promotion. In order to match&simplify it here, the IR needs
3061 to be cleaned up by other optimizers, i.e, VRP. */
3062 (for op (plus minus)
3063 (for cmp (lt le gt ge)
3065 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3066 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3067 (if (types_match (from_type, to_type)
3068 /* Check if it is special case A). */
3069 || (TYPE_UNSIGNED (from_type)
3070 && !TYPE_UNSIGNED (to_type)
3071 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3072 && integer_zerop (@1)
3073 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3076 wi::overflow_type overflow = wi::OVF_NONE;
3077 enum tree_code code, cmp_code = cmp;
3079 wide_int c1 = wi::to_wide (@1);
3080 wide_int c2 = wi::to_wide (@2);
3081 wide_int c3 = wi::to_wide (@3);
3082 signop sgn = TYPE_SIGN (from_type);
3084 /* Handle special case A), given x of unsigned type:
3085 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3086 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3087 if (!types_match (from_type, to_type))
3089 if (cmp_code == LT_EXPR)
3091 if (cmp_code == GE_EXPR)
3093 c1 = wi::max_value (to_type);
3095 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3096 compute (c3 op' c2) and check if it equals to c1 with op' being
3097 the inverted operator of op. Make sure overflow doesn't happen
3098 if it is undefined. */
3099 if (op == PLUS_EXPR)
3100 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3102 real_c1 = wi::add (c3, c2, sgn, &overflow);
3105 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3107 /* Check if c1 equals to real_c1. Boundary condition is handled
3108 by adjusting comparison operation if necessary. */
3109 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3112 /* X <= Y - 1 equals to X < Y. */
3113 if (cmp_code == LE_EXPR)
3115 /* X > Y - 1 equals to X >= Y. */
3116 if (cmp_code == GT_EXPR)
3119 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3122 /* X < Y + 1 equals to X <= Y. */
3123 if (cmp_code == LT_EXPR)
3125 /* X >= Y + 1 equals to X > Y. */
3126 if (cmp_code == GE_EXPR)
3129 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3131 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3133 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3138 (if (code == MAX_EXPR)
3139 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3140 { wide_int_to_tree (from_type, c2); })
3141 (if (code == MIN_EXPR)
3142 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3143 { wide_int_to_tree (from_type, c2); })))))))))
3145 (for cnd (cond vec_cond)
3146 /* A ? B : (A ? X : C) -> A ? B : C. */
3148 (cnd @0 (cnd @0 @1 @2) @3)
3151 (cnd @0 @1 (cnd @0 @2 @3))
3153 /* A ? B : (!A ? C : X) -> A ? B : C. */
3154 /* ??? This matches embedded conditions open-coded because genmatch
3155 would generate matching code for conditions in separate stmts only.
3156 The following is still important to merge then and else arm cases
3157 from if-conversion. */
3159 (cnd @0 @1 (cnd @2 @3 @4))
3160 (if (inverse_conditions_p (@0, @2))
3163 (cnd @0 (cnd @1 @2 @3) @4)
3164 (if (inverse_conditions_p (@0, @1))
3167 /* A ? B : B -> B. */
3172 /* !A ? B : C -> A ? C : B. */
3174 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3177 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3178 return all -1 or all 0 results. */
3179 /* ??? We could instead convert all instances of the vec_cond to negate,
3180 but that isn't necessarily a win on its own. */
3182 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3183 (if (VECTOR_TYPE_P (type)
3184 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3185 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3186 && (TYPE_MODE (TREE_TYPE (type))
3187 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3188 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3190 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3192 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3193 (if (VECTOR_TYPE_P (type)
3194 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3195 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3196 && (TYPE_MODE (TREE_TYPE (type))
3197 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3198 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3201 /* Simplifications of comparisons. */
3203 /* See if we can reduce the magnitude of a constant involved in a
3204 comparison by changing the comparison code. This is a canonicalization
3205 formerly done by maybe_canonicalize_comparison_1. */
3209 (cmp @0 uniform_integer_cst_p@1)
3210 (with { tree cst = uniform_integer_cst_p (@1); }
3211 (if (tree_int_cst_sgn (cst) == -1)
3212 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3213 wide_int_to_tree (TREE_TYPE (cst),
3219 (cmp @0 uniform_integer_cst_p@1)
3220 (with { tree cst = uniform_integer_cst_p (@1); }
3221 (if (tree_int_cst_sgn (cst) == 1)
3222 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3223 wide_int_to_tree (TREE_TYPE (cst),
3224 wi::to_wide (cst) - 1)); })))))
3226 /* We can simplify a logical negation of a comparison to the
3227 inverted comparison. As we cannot compute an expression
3228 operator using invert_tree_comparison we have to simulate
3229 that with expression code iteration. */
3230 (for cmp (tcc_comparison)
3231 icmp (inverted_tcc_comparison)
3232 ncmp (inverted_tcc_comparison_with_nans)
3233 /* Ideally we'd like to combine the following two patterns
3234 and handle some more cases by using
3235 (logical_inverted_value (cmp @0 @1))
3236 here but for that genmatch would need to "inline" that.
3237 For now implement what forward_propagate_comparison did. */
3239 (bit_not (cmp @0 @1))
3240 (if (VECTOR_TYPE_P (type)
3241 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3242 /* Comparison inversion may be impossible for trapping math,
3243 invert_tree_comparison will tell us. But we can't use
3244 a computed operator in the replacement tree thus we have
3245 to play the trick below. */
3246 (with { enum tree_code ic = invert_tree_comparison
3247 (cmp, HONOR_NANS (@0)); }
3253 (bit_xor (cmp @0 @1) integer_truep)
3254 (with { enum tree_code ic = invert_tree_comparison
3255 (cmp, HONOR_NANS (@0)); }
3261 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3262 ??? The transformation is valid for the other operators if overflow
3263 is undefined for the type, but performing it here badly interacts
3264 with the transformation in fold_cond_expr_with_comparison which
3265 attempts to synthetize ABS_EXPR. */
3267 (for sub (minus pointer_diff)
3269 (cmp (sub@2 @0 @1) integer_zerop)
3270 (if (single_use (@2))
3273 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3274 signed arithmetic case. That form is created by the compiler
3275 often enough for folding it to be of value. One example is in
3276 computing loop trip counts after Operator Strength Reduction. */
3277 (for cmp (simple_comparison)
3278 scmp (swapped_simple_comparison)
3280 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3281 /* Handle unfolded multiplication by zero. */
3282 (if (integer_zerop (@1))
3284 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3285 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3287 /* If @1 is negative we swap the sense of the comparison. */
3288 (if (tree_int_cst_sgn (@1) < 0)
3292 /* Simplify comparison of something with itself. For IEEE
3293 floating-point, we can only do some of these simplifications. */
3297 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3298 || ! HONOR_NANS (@0))
3299 { constant_boolean_node (true, type); }
3300 (if (cmp != EQ_EXPR)
3306 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3307 || ! HONOR_NANS (@0))
3308 { constant_boolean_node (false, type); })))
3309 (for cmp (unle unge uneq)
3312 { constant_boolean_node (true, type); }))
3313 (for cmp (unlt ungt)
3319 (if (!flag_trapping_math)
3320 { constant_boolean_node (false, type); }))
3322 /* Fold ~X op ~Y as Y op X. */
3323 (for cmp (simple_comparison)
3325 (cmp (bit_not@2 @0) (bit_not@3 @1))
3326 (if (single_use (@2) && single_use (@3))
3329 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3330 (for cmp (simple_comparison)
3331 scmp (swapped_simple_comparison)
3333 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3334 (if (single_use (@2)
3335 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3336 (scmp @0 (bit_not @1)))))
3338 (for cmp (simple_comparison)
3339 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3341 (cmp (convert@2 @0) (convert? @1))
3342 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3343 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3344 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3345 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3346 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3349 tree type1 = TREE_TYPE (@1);
3350 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3352 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3353 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3354 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3355 type1 = float_type_node;
3356 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3357 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3358 type1 = double_type_node;
3361 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3362 ? TREE_TYPE (@0) : type1);
3364 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3365 (cmp (convert:newtype @0) (convert:newtype @1))))))
3369 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3371 /* a CMP (-0) -> a CMP 0 */
3372 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3373 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3374 /* x != NaN is always true, other ops are always false. */
3375 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3376 && ! HONOR_SNANS (@1))
3377 { constant_boolean_node (cmp == NE_EXPR, type); })
3378 /* Fold comparisons against infinity. */
3379 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3380 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3383 REAL_VALUE_TYPE max;
3384 enum tree_code code = cmp;
3385 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3387 code = swap_tree_comparison (code);
3390 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3391 (if (code == GT_EXPR
3392 && !(HONOR_NANS (@0) && flag_trapping_math))
3393 { constant_boolean_node (false, type); })
3394 (if (code == LE_EXPR)
3395 /* x <= +Inf is always true, if we don't care about NaNs. */
3396 (if (! HONOR_NANS (@0))
3397 { constant_boolean_node (true, type); }
3398 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3399 an "invalid" exception. */
3400 (if (!flag_trapping_math)
3402 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3403 for == this introduces an exception for x a NaN. */
3404 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3406 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3408 (lt @0 { build_real (TREE_TYPE (@0), max); })
3409 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3410 /* x < +Inf is always equal to x <= DBL_MAX. */
3411 (if (code == LT_EXPR)
3412 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3414 (ge @0 { build_real (TREE_TYPE (@0), max); })
3415 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3416 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3417 an exception for x a NaN so use an unordered comparison. */
3418 (if (code == NE_EXPR)
3419 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3420 (if (! HONOR_NANS (@0))
3422 (ge @0 { build_real (TREE_TYPE (@0), max); })
3423 (le @0 { build_real (TREE_TYPE (@0), max); }))
3425 (unge @0 { build_real (TREE_TYPE (@0), max); })
3426 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3428 /* If this is a comparison of a real constant with a PLUS_EXPR
3429 or a MINUS_EXPR of a real constant, we can convert it into a
3430 comparison with a revised real constant as long as no overflow
3431 occurs when unsafe_math_optimizations are enabled. */
3432 (if (flag_unsafe_math_optimizations)
3433 (for op (plus minus)
3435 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3438 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3439 TREE_TYPE (@1), @2, @1);
3441 (if (tem && !TREE_OVERFLOW (tem))
3442 (cmp @0 { tem; }))))))
3444 /* Likewise, we can simplify a comparison of a real constant with
3445 a MINUS_EXPR whose first operand is also a real constant, i.e.
3446 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3447 floating-point types only if -fassociative-math is set. */
3448 (if (flag_associative_math)
3450 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3451 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3452 (if (tem && !TREE_OVERFLOW (tem))
3453 (cmp { tem; } @1)))))
3455 /* Fold comparisons against built-in math functions. */
3456 (if (flag_unsafe_math_optimizations
3457 && ! flag_errno_math)
3460 (cmp (sq @0) REAL_CST@1)
3462 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3464 /* sqrt(x) < y is always false, if y is negative. */
3465 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3466 { constant_boolean_node (false, type); })
3467 /* sqrt(x) > y is always true, if y is negative and we
3468 don't care about NaNs, i.e. negative values of x. */
3469 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3470 { constant_boolean_node (true, type); })
3471 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3472 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3473 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3475 /* sqrt(x) < 0 is always false. */
3476 (if (cmp == LT_EXPR)
3477 { constant_boolean_node (false, type); })
3478 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3479 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3480 { constant_boolean_node (true, type); })
3481 /* sqrt(x) <= 0 -> x == 0. */
3482 (if (cmp == LE_EXPR)
3484 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3485 == or !=. In the last case:
3487 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3489 if x is negative or NaN. Due to -funsafe-math-optimizations,
3490 the results for other x follow from natural arithmetic. */
3492 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3496 real_arithmetic (&c2, MULT_EXPR,
3497 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3498 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3500 (if (REAL_VALUE_ISINF (c2))
3501 /* sqrt(x) > y is x == +Inf, when y is very large. */
3502 (if (HONOR_INFINITIES (@0))
3503 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3504 { constant_boolean_node (false, type); })
3505 /* sqrt(x) > c is the same as x > c*c. */
3506 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3507 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3511 real_arithmetic (&c2, MULT_EXPR,
3512 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3513 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3515 (if (REAL_VALUE_ISINF (c2))
3517 /* sqrt(x) < y is always true, when y is a very large
3518 value and we don't care about NaNs or Infinities. */
3519 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3520 { constant_boolean_node (true, type); })
3521 /* sqrt(x) < y is x != +Inf when y is very large and we
3522 don't care about NaNs. */
3523 (if (! HONOR_NANS (@0))
3524 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3525 /* sqrt(x) < y is x >= 0 when y is very large and we
3526 don't care about Infinities. */
3527 (if (! HONOR_INFINITIES (@0))
3528 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3529 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3532 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3533 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3534 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3535 (if (! HONOR_NANS (@0))
3536 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3537 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3540 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3541 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3542 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3544 (cmp (sq @0) (sq @1))
3545 (if (! HONOR_NANS (@0))
3548 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
3549 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3550 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
3552 (cmp (float@0 @1) (float @2))
3553 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3554 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3557 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3558 tree type1 = TREE_TYPE (@1);
3559 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3560 tree type2 = TREE_TYPE (@2);
3561 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3563 (if (fmt.can_represent_integral_type_p (type1)
3564 && fmt.can_represent_integral_type_p (type2))
3565 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3566 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3567 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3568 && type1_signed_p >= type2_signed_p)
3569 (icmp @1 (convert @2))
3570 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3571 && type1_signed_p <= type2_signed_p)
3572 (icmp (convert:type2 @1) @2)
3573 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3574 && type1_signed_p == type2_signed_p)
3575 (icmp @1 @2))))))))))
3577 /* Optimize various special cases of (FTYPE) N CMP CST. */
3578 (for cmp (lt le eq ne ge gt)
3579 icmp (le le eq ne ge ge)
3581 (cmp (float @0) REAL_CST@1)
3582 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3583 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3586 tree itype = TREE_TYPE (@0);
3587 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3588 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3589 /* Be careful to preserve any potential exceptions due to
3590 NaNs. qNaNs are ok in == or != context.
3591 TODO: relax under -fno-trapping-math or
3592 -fno-signaling-nans. */
3594 = real_isnan (cst) && (cst->signalling
3595 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3597 /* TODO: allow non-fitting itype and SNaNs when
3598 -fno-trapping-math. */
3599 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
3602 signop isign = TYPE_SIGN (itype);
3603 REAL_VALUE_TYPE imin, imax;
3604 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3605 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3607 REAL_VALUE_TYPE icst;
3608 if (cmp == GT_EXPR || cmp == GE_EXPR)
3609 real_ceil (&icst, fmt, cst);
3610 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3611 real_floor (&icst, fmt, cst);
3613 real_trunc (&icst, fmt, cst);
3615 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3617 bool overflow_p = false;
3619 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3622 /* Optimize cases when CST is outside of ITYPE's range. */
3623 (if (real_compare (LT_EXPR, cst, &imin))
3624 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3626 (if (real_compare (GT_EXPR, cst, &imax))
3627 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3629 /* Remove cast if CST is an integer representable by ITYPE. */
3631 (cmp @0 { gcc_assert (!overflow_p);
3632 wide_int_to_tree (itype, icst_val); })
3634 /* When CST is fractional, optimize
3635 (FTYPE) N == CST -> 0
3636 (FTYPE) N != CST -> 1. */
3637 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3638 { constant_boolean_node (cmp == NE_EXPR, type); })
3639 /* Otherwise replace with sensible integer constant. */
3642 gcc_checking_assert (!overflow_p);
3644 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3646 /* Fold A /[ex] B CMP C to A CMP B * C. */
3649 (cmp (exact_div @0 @1) INTEGER_CST@2)
3650 (if (!integer_zerop (@1))
3651 (if (wi::to_wide (@2) == 0)
3653 (if (TREE_CODE (@1) == INTEGER_CST)
3656 wi::overflow_type ovf;
3657 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3658 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3661 { constant_boolean_node (cmp == NE_EXPR, type); }
3662 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3663 (for cmp (lt le gt ge)
3665 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3666 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3669 wi::overflow_type ovf;
3670 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3671 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3674 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3675 TYPE_SIGN (TREE_TYPE (@2)))
3676 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3677 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3679 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
3681 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
3682 For large C (more than min/B+2^size), this is also true, with the
3683 multiplication computed modulo 2^size.
3684 For intermediate C, this just tests the sign of A. */
3685 (for cmp (lt le gt ge)
3688 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
3689 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
3690 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
3691 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3694 tree utype = TREE_TYPE (@2);
3695 wide_int denom = wi::to_wide (@1);
3696 wide_int right = wi::to_wide (@2);
3697 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
3698 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
3699 bool small = wi::leu_p (right, smax);
3700 bool large = wi::geu_p (right, smin);
3702 (if (small || large)
3703 (cmp (convert:utype @0) (mult @2 (convert @1)))
3704 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
3706 /* Unordered tests if either argument is a NaN. */
3708 (bit_ior (unordered @0 @0) (unordered @1 @1))
3709 (if (types_match (@0, @1))
3712 (bit_and (ordered @0 @0) (ordered @1 @1))
3713 (if (types_match (@0, @1))
3716 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3719 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3722 /* Simple range test simplifications. */
3723 /* A < B || A >= B -> true. */
3724 (for test1 (lt le le le ne ge)
3725 test2 (ge gt ge ne eq ne)
3727 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3728 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3729 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3730 { constant_boolean_node (true, type); })))
3731 /* A < B && A >= B -> false. */
3732 (for test1 (lt lt lt le ne eq)
3733 test2 (ge gt eq gt eq gt)
3735 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3736 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3737 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3738 { constant_boolean_node (false, type); })))
3740 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3741 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3743 Note that comparisons
3744 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3745 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3746 will be canonicalized to above so there's no need to
3753 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3754 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3757 tree ty = TREE_TYPE (@0);
3758 unsigned prec = TYPE_PRECISION (ty);
3759 wide_int mask = wi::to_wide (@2, prec);
3760 wide_int rhs = wi::to_wide (@3, prec);
3761 signop sgn = TYPE_SIGN (ty);
3763 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3764 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3765 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3766 { build_zero_cst (ty); }))))))
3768 /* -A CMP -B -> B CMP A. */
3769 (for cmp (tcc_comparison)
3770 scmp (swapped_tcc_comparison)
3772 (cmp (negate @0) (negate @1))
3773 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3774 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3775 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3778 (cmp (negate @0) CONSTANT_CLASS_P@1)
3779 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3780 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3781 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3782 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3783 (if (tem && !TREE_OVERFLOW (tem))
3784 (scmp @0 { tem; }))))))
3786 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3789 (op (abs @0) zerop@1)
3792 /* From fold_sign_changed_comparison and fold_widened_comparison.
3793 FIXME: the lack of symmetry is disturbing. */
3794 (for cmp (simple_comparison)
3796 (cmp (convert@0 @00) (convert?@1 @10))
3797 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3798 /* Disable this optimization if we're casting a function pointer
3799 type on targets that require function pointer canonicalization. */
3800 && !(targetm.have_canonicalize_funcptr_for_compare ()
3801 && ((POINTER_TYPE_P (TREE_TYPE (@00))
3802 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3803 || (POINTER_TYPE_P (TREE_TYPE (@10))
3804 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
3806 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3807 && (TREE_CODE (@10) == INTEGER_CST
3809 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3812 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3813 /* ??? The special-casing of INTEGER_CST conversion was in the original
3814 code and here to avoid a spurious overflow flag on the resulting
3815 constant which fold_convert produces. */
3816 (if (TREE_CODE (@1) == INTEGER_CST)
3817 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3818 TREE_OVERFLOW (@1)); })
3819 (cmp @00 (convert @1)))
3821 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3822 /* If possible, express the comparison in the shorter mode. */
3823 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3824 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3825 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3826 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3827 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3828 || ((TYPE_PRECISION (TREE_TYPE (@00))
3829 >= TYPE_PRECISION (TREE_TYPE (@10)))
3830 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3831 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3832 || (TREE_CODE (@10) == INTEGER_CST
3833 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3834 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3835 (cmp @00 (convert @10))
3836 (if (TREE_CODE (@10) == INTEGER_CST
3837 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3838 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3841 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3842 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3843 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3844 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3846 (if (above || below)
3847 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3848 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3849 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3850 { constant_boolean_node (above ? true : false, type); }
3851 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3852 { constant_boolean_node (above ? false : true, type); }))))))))))))
3855 /* A local variable can never be pointed to by
3856 the default SSA name of an incoming parameter.
3857 SSA names are canonicalized to 2nd place. */
3859 (cmp addr@0 SSA_NAME@1)
3860 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3861 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3862 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3863 (if (TREE_CODE (base) == VAR_DECL
3864 && auto_var_in_fn_p (base, current_function_decl))
3865 (if (cmp == NE_EXPR)
3866 { constant_boolean_node (true, type); }
3867 { constant_boolean_node (false, type); }))))))
3869 /* Equality compare simplifications from fold_binary */
3872 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3873 Similarly for NE_EXPR. */
3875 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3876 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3877 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3878 { constant_boolean_node (cmp == NE_EXPR, type); }))
3880 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3882 (cmp (bit_xor @0 @1) integer_zerop)
3885 /* (X ^ Y) == Y becomes X == 0.
3886 Likewise (X ^ Y) == X becomes Y == 0. */
3888 (cmp:c (bit_xor:c @0 @1) @0)
3889 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3891 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3893 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3894 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3895 (cmp @0 (bit_xor @1 (convert @2)))))
3898 (cmp (convert? addr@0) integer_zerop)
3899 (if (tree_single_nonzero_warnv_p (@0, NULL))
3900 { constant_boolean_node (cmp == NE_EXPR, type); })))
3902 /* If we have (A & C) == C where C is a power of 2, convert this into
3903 (A & C) != 0. Similarly for NE_EXPR. */
3907 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3908 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3910 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3911 convert this into a shift followed by ANDing with D. */
3914 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3915 INTEGER_CST@2 integer_zerop)
3916 (if (integer_pow2p (@2))
3918 int shift = (wi::exact_log2 (wi::to_wide (@2))
3919 - wi::exact_log2 (wi::to_wide (@1)));
3923 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3925 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3928 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3929 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3933 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3934 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3935 && type_has_mode_precision_p (TREE_TYPE (@0))
3936 && element_precision (@2) >= element_precision (@0)
3937 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3938 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3939 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3941 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3942 this into a right shift or sign extension followed by ANDing with C. */
3945 (lt @0 integer_zerop)
3946 INTEGER_CST@1 integer_zerop)
3947 (if (integer_pow2p (@1)
3948 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3950 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3954 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3956 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3957 sign extension followed by AND with C will achieve the effect. */
3958 (bit_and (convert @0) @1)))))
3960 /* When the addresses are not directly of decls compare base and offset.
3961 This implements some remaining parts of fold_comparison address
3962 comparisons but still no complete part of it. Still it is good
3963 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3964 (for cmp (simple_comparison)
3966 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3969 poly_int64 off0, off1;
3970 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3971 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3972 if (base0 && TREE_CODE (base0) == MEM_REF)
3974 off0 += mem_ref_offset (base0).force_shwi ();
3975 base0 = TREE_OPERAND (base0, 0);
3977 if (base1 && TREE_CODE (base1) == MEM_REF)
3979 off1 += mem_ref_offset (base1).force_shwi ();
3980 base1 = TREE_OPERAND (base1, 0);
3983 (if (base0 && base1)
3987 /* Punt in GENERIC on variables with value expressions;
3988 the value expressions might point to fields/elements
3989 of other vars etc. */
3991 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3992 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3994 else if (decl_in_symtab_p (base0)
3995 && decl_in_symtab_p (base1))
3996 equal = symtab_node::get_create (base0)
3997 ->equal_address_to (symtab_node::get_create (base1));
3998 else if ((DECL_P (base0)
3999 || TREE_CODE (base0) == SSA_NAME
4000 || TREE_CODE (base0) == STRING_CST)
4002 || TREE_CODE (base1) == SSA_NAME
4003 || TREE_CODE (base1) == STRING_CST))
4004 equal = (base0 == base1);
4007 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4008 off0.is_constant (&ioff0);
4009 off1.is_constant (&ioff1);
4010 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4011 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4012 || (TREE_CODE (base0) == STRING_CST
4013 && TREE_CODE (base1) == STRING_CST
4014 && ioff0 >= 0 && ioff1 >= 0
4015 && ioff0 < TREE_STRING_LENGTH (base0)
4016 && ioff1 < TREE_STRING_LENGTH (base1)
4017 /* This is a too conservative test that the STRING_CSTs
4018 will not end up being string-merged. */
4019 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4020 TREE_STRING_POINTER (base1) + ioff1,
4021 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4022 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4024 else if (!DECL_P (base0) || !DECL_P (base1))
4026 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4028 /* If this is a pointer comparison, ignore for now even
4029 valid equalities where one pointer is the offset zero
4030 of one object and the other to one past end of another one. */
4031 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4033 /* Assume that automatic variables can't be adjacent to global
4035 else if (is_global_var (base0) != is_global_var (base1))
4039 tree sz0 = DECL_SIZE_UNIT (base0);
4040 tree sz1 = DECL_SIZE_UNIT (base1);
4041 /* If sizes are unknown, e.g. VLA or not representable,
4043 if (!tree_fits_poly_int64_p (sz0)
4044 || !tree_fits_poly_int64_p (sz1))
4048 poly_int64 size0 = tree_to_poly_int64 (sz0);
4049 poly_int64 size1 = tree_to_poly_int64 (sz1);
4050 /* If one offset is pointing (or could be) to the beginning
4051 of one object and the other is pointing to one past the
4052 last byte of the other object, punt. */
4053 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4055 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4057 /* If both offsets are the same, there are some cases
4058 we know that are ok. Either if we know they aren't
4059 zero, or if we know both sizes are no zero. */
4061 && known_eq (off0, off1)
4062 && (known_ne (off0, 0)
4063 || (known_ne (size0, 0) && known_ne (size1, 0))))
4070 && (cmp == EQ_EXPR || cmp == NE_EXPR
4071 /* If the offsets are equal we can ignore overflow. */
4072 || known_eq (off0, off1)
4073 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4074 /* Or if we compare using pointers to decls or strings. */
4075 || (POINTER_TYPE_P (TREE_TYPE (@2))
4076 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4078 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4079 { constant_boolean_node (known_eq (off0, off1), type); })
4080 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4081 { constant_boolean_node (known_ne (off0, off1), type); })
4082 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4083 { constant_boolean_node (known_lt (off0, off1), type); })
4084 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4085 { constant_boolean_node (known_le (off0, off1), type); })
4086 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4087 { constant_boolean_node (known_ge (off0, off1), type); })
4088 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4089 { constant_boolean_node (known_gt (off0, off1), type); }))
4092 (if (cmp == EQ_EXPR)
4093 { constant_boolean_node (false, type); })
4094 (if (cmp == NE_EXPR)
4095 { constant_boolean_node (true, type); })))))))))
4097 /* Simplify pointer equality compares using PTA. */
4101 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4102 && ptrs_compare_unequal (@0, @1))
4103 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4105 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4106 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4107 Disable the transform if either operand is pointer to function.
4108 This broke pr22051-2.c for arm where function pointer
4109 canonicalizaion is not wanted. */
4113 (cmp (convert @0) INTEGER_CST@1)
4114 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4115 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4116 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4117 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4118 && POINTER_TYPE_P (TREE_TYPE (@1))
4119 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4120 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4121 (cmp @0 (convert @1)))))
4123 /* Non-equality compare simplifications from fold_binary */
4124 (for cmp (lt gt le ge)
4125 /* Comparisons with the highest or lowest possible integer of
4126 the specified precision will have known values. */
4128 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4129 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4130 || POINTER_TYPE_P (TREE_TYPE (@1))
4131 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4132 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4135 tree cst = uniform_integer_cst_p (@1);
4136 tree arg1_type = TREE_TYPE (cst);
4137 unsigned int prec = TYPE_PRECISION (arg1_type);
4138 wide_int max = wi::max_value (arg1_type);
4139 wide_int signed_max = wi::max_value (prec, SIGNED);
4140 wide_int min = wi::min_value (arg1_type);
4143 (if (wi::to_wide (cst) == max)
4145 (if (cmp == GT_EXPR)
4146 { constant_boolean_node (false, type); })
4147 (if (cmp == GE_EXPR)
4149 (if (cmp == LE_EXPR)
4150 { constant_boolean_node (true, type); })
4151 (if (cmp == LT_EXPR)
4153 (if (wi::to_wide (cst) == min)
4155 (if (cmp == LT_EXPR)
4156 { constant_boolean_node (false, type); })
4157 (if (cmp == LE_EXPR)
4159 (if (cmp == GE_EXPR)
4160 { constant_boolean_node (true, type); })
4161 (if (cmp == GT_EXPR)
4163 (if (wi::to_wide (cst) == max - 1)
4165 (if (cmp == GT_EXPR)
4166 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4167 wide_int_to_tree (TREE_TYPE (cst),
4170 (if (cmp == LE_EXPR)
4171 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4172 wide_int_to_tree (TREE_TYPE (cst),
4175 (if (wi::to_wide (cst) == min + 1)
4177 (if (cmp == GE_EXPR)
4178 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4179 wide_int_to_tree (TREE_TYPE (cst),
4182 (if (cmp == LT_EXPR)
4183 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4184 wide_int_to_tree (TREE_TYPE (cst),
4187 (if (wi::to_wide (cst) == signed_max
4188 && TYPE_UNSIGNED (arg1_type)
4189 /* We will flip the signedness of the comparison operator
4190 associated with the mode of @1, so the sign bit is
4191 specified by this mode. Check that @1 is the signed
4192 max associated with this sign bit. */
4193 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4194 /* signed_type does not work on pointer types. */
4195 && INTEGRAL_TYPE_P (arg1_type))
4196 /* The following case also applies to X < signed_max+1
4197 and X >= signed_max+1 because previous transformations. */
4198 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4199 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4201 (if (cst == @1 && cmp == LE_EXPR)
4202 (ge (convert:st @0) { build_zero_cst (st); }))
4203 (if (cst == @1 && cmp == GT_EXPR)
4204 (lt (convert:st @0) { build_zero_cst (st); }))
4205 (if (cmp == LE_EXPR)
4206 (ge (view_convert:st @0) { build_zero_cst (st); }))
4207 (if (cmp == GT_EXPR)
4208 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4210 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4211 /* If the second operand is NaN, the result is constant. */
4214 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4215 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4216 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4217 ? false : true, type); })))
4219 /* bool_var != 0 becomes bool_var. */
4221 (ne @0 integer_zerop)
4222 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4223 && types_match (type, TREE_TYPE (@0)))
4225 /* bool_var == 1 becomes bool_var. */
4227 (eq @0 integer_onep)
4228 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4229 && types_match (type, TREE_TYPE (@0)))
4232 bool_var == 0 becomes !bool_var or
4233 bool_var != 1 becomes !bool_var
4234 here because that only is good in assignment context as long
4235 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4236 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4237 clearly less optimal and which we'll transform again in forwprop. */
4239 /* When one argument is a constant, overflow detection can be simplified.
4240 Currently restricted to single use so as not to interfere too much with
4241 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4242 A + CST CMP A -> A CMP' CST' */
4243 (for cmp (lt le ge gt)
4246 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4247 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4248 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4249 && wi::to_wide (@1) != 0
4251 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4252 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4253 wi::max_value (prec, UNSIGNED)
4254 - wi::to_wide (@1)); })))))
4256 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4257 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4258 expects the long form, so we restrict the transformation for now. */
4261 (cmp:c (minus@2 @0 @1) @0)
4262 (if (single_use (@2)
4263 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4264 && TYPE_UNSIGNED (TREE_TYPE (@0))
4265 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4268 /* Testing for overflow is unnecessary if we already know the result. */
4273 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4274 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4275 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4276 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4281 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4282 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4283 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4284 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4286 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4287 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4291 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4292 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4293 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4294 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4296 /* Simplification of math builtins. These rules must all be optimizations
4297 as well as IL simplifications. If there is a possibility that the new
4298 form could be a pessimization, the rule should go in the canonicalization
4299 section that follows this one.
4301 Rules can generally go in this section if they satisfy one of
4304 - the rule describes an identity
4306 - the rule replaces calls with something as simple as addition or
4309 - the rule contains unary calls only and simplifies the surrounding
4310 arithmetic. (The idea here is to exclude non-unary calls in which
4311 one operand is constant and in which the call is known to be cheap
4312 when the operand has that value.) */
4314 (if (flag_unsafe_math_optimizations)
4315 /* Simplify sqrt(x) * sqrt(x) -> x. */
4317 (mult (SQRT_ALL@1 @0) @1)
4318 (if (!HONOR_SNANS (type))
4321 (for op (plus minus)
4322 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4326 (rdiv (op @0 @2) @1)))
4328 (for cmp (lt le gt ge)
4329 neg_cmp (gt ge lt le)
4330 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4332 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4334 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4336 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4337 || (real_zerop (tem) && !real_zerop (@1))))
4339 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4341 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4342 (neg_cmp @0 { tem; })))))))
4344 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4345 (for root (SQRT CBRT)
4347 (mult (root:s @0) (root:s @1))
4348 (root (mult @0 @1))))
4350 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4351 (for exps (EXP EXP2 EXP10 POW10)
4353 (mult (exps:s @0) (exps:s @1))
4354 (exps (plus @0 @1))))
4356 /* Simplify a/root(b/c) into a*root(c/b). */
4357 (for root (SQRT CBRT)
4359 (rdiv @0 (root:s (rdiv:s @1 @2)))
4360 (mult @0 (root (rdiv @2 @1)))))
4362 /* Simplify x/expN(y) into x*expN(-y). */
4363 (for exps (EXP EXP2 EXP10 POW10)
4365 (rdiv @0 (exps:s @1))
4366 (mult @0 (exps (negate @1)))))
4368 (for logs (LOG LOG2 LOG10 LOG10)
4369 exps (EXP EXP2 EXP10 POW10)
4370 /* logN(expN(x)) -> x. */
4374 /* expN(logN(x)) -> x. */
4379 /* Optimize logN(func()) for various exponential functions. We
4380 want to determine the value "x" and the power "exponent" in
4381 order to transform logN(x**exponent) into exponent*logN(x). */
4382 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4383 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4386 (if (SCALAR_FLOAT_TYPE_P (type))
4392 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4393 x = build_real_truncate (type, dconst_e ());
4396 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4397 x = build_real (type, dconst2);
4401 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4403 REAL_VALUE_TYPE dconst10;
4404 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4405 x = build_real (type, dconst10);
4412 (mult (logs { x; }) @0)))))
4420 (if (SCALAR_FLOAT_TYPE_P (type))
4426 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4427 x = build_real (type, dconsthalf);
4430 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4431 x = build_real_truncate (type, dconst_third ());
4437 (mult { x; } (logs @0))))))
4439 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4440 (for logs (LOG LOG2 LOG10)
4444 (mult @1 (logs @0))))
4446 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4447 or if C is a positive power of 2,
4448 pow(C,x) -> exp2(log2(C)*x). */
4456 (pows REAL_CST@0 @1)
4457 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4458 && real_isfinite (TREE_REAL_CST_PTR (@0))
4459 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4460 the use_exp2 case until after vectorization. It seems actually
4461 beneficial for all constants to postpone this until later,
4462 because exp(log(C)*x), while faster, will have worse precision
4463 and if x folds into a constant too, that is unnecessary
4465 && canonicalize_math_after_vectorization_p ())
4467 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4468 bool use_exp2 = false;
4469 if (targetm.libc_has_function (function_c99_misc)
4470 && value->cl == rvc_normal)
4472 REAL_VALUE_TYPE frac_rvt = *value;
4473 SET_REAL_EXP (&frac_rvt, 1);
4474 if (real_equal (&frac_rvt, &dconst1))
4479 (if (optimize_pow_to_exp (@0, @1))
4480 (exps (mult (logs @0) @1)))
4481 (exp2s (mult (log2s @0) @1)))))))
4484 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4486 exps (EXP EXP2 EXP10 POW10)
4487 logs (LOG LOG2 LOG10 LOG10)
4489 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4490 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4491 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4492 (exps (plus (mult (logs @0) @1) @2)))))
4497 exps (EXP EXP2 EXP10 POW10)
4498 /* sqrt(expN(x)) -> expN(x*0.5). */
4501 (exps (mult @0 { build_real (type, dconsthalf); })))
4502 /* cbrt(expN(x)) -> expN(x/3). */
4505 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4506 /* pow(expN(x), y) -> expN(x*y). */
4509 (exps (mult @0 @1))))
4511 /* tan(atan(x)) -> x. */
4518 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4522 copysigns (COPYSIGN)
4527 REAL_VALUE_TYPE r_cst;
4528 build_sinatan_real (&r_cst, type);
4529 tree t_cst = build_real (type, r_cst);
4530 tree t_one = build_one_cst (type);
4532 (if (SCALAR_FLOAT_TYPE_P (type))
4533 (cond (lt (abs @0) { t_cst; })
4534 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4535 (copysigns { t_one; } @0))))))
4537 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4541 copysigns (COPYSIGN)
4546 REAL_VALUE_TYPE r_cst;
4547 build_sinatan_real (&r_cst, type);
4548 tree t_cst = build_real (type, r_cst);
4549 tree t_one = build_one_cst (type);
4550 tree t_zero = build_zero_cst (type);
4552 (if (SCALAR_FLOAT_TYPE_P (type))
4553 (cond (lt (abs @0) { t_cst; })
4554 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4555 (copysigns { t_zero; } @0))))))
4557 (if (!flag_errno_math)
4558 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4563 (sinhs (atanhs:s @0))
4564 (with { tree t_one = build_one_cst (type); }
4565 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4567 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4572 (coshs (atanhs:s @0))
4573 (with { tree t_one = build_one_cst (type); }
4574 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4576 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4578 (CABS (complex:C @0 real_zerop@1))
4581 /* trunc(trunc(x)) -> trunc(x), etc. */
4582 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4586 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4587 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4589 (fns integer_valued_real_p@0)
4592 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4594 (HYPOT:c @0 real_zerop@1)
4597 /* pow(1,x) -> 1. */
4599 (POW real_onep@0 @1)
4603 /* copysign(x,x) -> x. */
4604 (COPYSIGN_ALL @0 @0)
4608 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4609 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4612 (for scale (LDEXP SCALBN SCALBLN)
4613 /* ldexp(0, x) -> 0. */
4615 (scale real_zerop@0 @1)
4617 /* ldexp(x, 0) -> x. */
4619 (scale @0 integer_zerop@1)
4621 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4623 (scale REAL_CST@0 @1)
4624 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4627 /* Canonicalization of sequences of math builtins. These rules represent
4628 IL simplifications but are not necessarily optimizations.
4630 The sincos pass is responsible for picking "optimal" implementations
4631 of math builtins, which may be more complicated and can sometimes go
4632 the other way, e.g. converting pow into a sequence of sqrts.
4633 We only want to do these canonicalizations before the pass has run. */
4635 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4636 /* Simplify tan(x) * cos(x) -> sin(x). */
4638 (mult:c (TAN:s @0) (COS:s @0))
4641 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4643 (mult:c @0 (POW:s @0 REAL_CST@1))
4644 (if (!TREE_OVERFLOW (@1))
4645 (POW @0 (plus @1 { build_one_cst (type); }))))
4647 /* Simplify sin(x) / cos(x) -> tan(x). */
4649 (rdiv (SIN:s @0) (COS:s @0))
4652 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4654 (rdiv (COS:s @0) (SIN:s @0))
4655 (rdiv { build_one_cst (type); } (TAN @0)))
4657 /* Simplify sin(x) / tan(x) -> cos(x). */
4659 (rdiv (SIN:s @0) (TAN:s @0))
4660 (if (! HONOR_NANS (@0)
4661 && ! HONOR_INFINITIES (@0))
4664 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4666 (rdiv (TAN:s @0) (SIN:s @0))
4667 (if (! HONOR_NANS (@0)
4668 && ! HONOR_INFINITIES (@0))
4669 (rdiv { build_one_cst (type); } (COS @0))))
4671 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4673 (mult (POW:s @0 @1) (POW:s @0 @2))
4674 (POW @0 (plus @1 @2)))
4676 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4678 (mult (POW:s @0 @1) (POW:s @2 @1))
4679 (POW (mult @0 @2) @1))
4681 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4683 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4684 (POWI (mult @0 @2) @1))
4686 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4688 (rdiv (POW:s @0 REAL_CST@1) @0)
4689 (if (!TREE_OVERFLOW (@1))
4690 (POW @0 (minus @1 { build_one_cst (type); }))))
4692 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4694 (rdiv @0 (POW:s @1 @2))
4695 (mult @0 (POW @1 (negate @2))))
4700 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4703 (pows @0 { build_real (type, dconst_quarter ()); }))
4704 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4707 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4708 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4711 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4712 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4714 (cbrts (cbrts tree_expr_nonnegative_p@0))
4715 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4716 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4718 (sqrts (pows @0 @1))
4719 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4720 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4722 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4723 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4724 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4726 (pows (sqrts @0) @1)
4727 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4728 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4730 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4731 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4732 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4734 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4735 (pows @0 (mult @1 @2))))
4737 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4739 (CABS (complex @0 @0))
4740 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4742 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4745 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4747 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4752 (cexps compositional_complex@0)
4753 (if (targetm.libc_has_function (function_c99_math_complex))
4755 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4756 (mult @1 (imagpart @2)))))))
4758 (if (canonicalize_math_p ())
4759 /* floor(x) -> trunc(x) if x is nonnegative. */
4760 (for floors (FLOOR_ALL)
4763 (floors tree_expr_nonnegative_p@0)
4766 (match double_value_p
4768 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4769 (for froms (BUILT_IN_TRUNCL
4781 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4782 (if (optimize && canonicalize_math_p ())
4784 (froms (convert double_value_p@0))
4785 (convert (tos @0)))))
4787 (match float_value_p
4789 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4790 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4791 BUILT_IN_FLOORL BUILT_IN_FLOOR
4792 BUILT_IN_CEILL BUILT_IN_CEIL
4793 BUILT_IN_ROUNDL BUILT_IN_ROUND
4794 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4795 BUILT_IN_RINTL BUILT_IN_RINT)
4796 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4797 BUILT_IN_FLOORF BUILT_IN_FLOORF
4798 BUILT_IN_CEILF BUILT_IN_CEILF
4799 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4800 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4801 BUILT_IN_RINTF BUILT_IN_RINTF)
4802 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4804 (if (optimize && canonicalize_math_p ()
4805 && targetm.libc_has_function (function_c99_misc))
4807 (froms (convert float_value_p@0))
4808 (convert (tos @0)))))
4810 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4811 tos (XFLOOR XCEIL XROUND XRINT)
4812 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4813 (if (optimize && canonicalize_math_p ())
4815 (froms (convert double_value_p@0))
4818 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4819 XFLOOR XCEIL XROUND XRINT)
4820 tos (XFLOORF XCEILF XROUNDF XRINTF)
4821 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4823 (if (optimize && canonicalize_math_p ())
4825 (froms (convert float_value_p@0))
4828 (if (canonicalize_math_p ())
4829 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4830 (for floors (IFLOOR LFLOOR LLFLOOR)
4832 (floors tree_expr_nonnegative_p@0)
4835 (if (canonicalize_math_p ())
4836 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4837 (for fns (IFLOOR LFLOOR LLFLOOR
4839 IROUND LROUND LLROUND)
4841 (fns integer_valued_real_p@0)
4843 (if (!flag_errno_math)
4844 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4845 (for rints (IRINT LRINT LLRINT)
4847 (rints integer_valued_real_p@0)
4850 (if (canonicalize_math_p ())
4851 (for ifn (IFLOOR ICEIL IROUND IRINT)
4852 lfn (LFLOOR LCEIL LROUND LRINT)
4853 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4854 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4855 sizeof (int) == sizeof (long). */
4856 (if (TYPE_PRECISION (integer_type_node)
4857 == TYPE_PRECISION (long_integer_type_node))
4860 (lfn:long_integer_type_node @0)))
4861 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4862 sizeof (long long) == sizeof (long). */
4863 (if (TYPE_PRECISION (long_long_integer_type_node)
4864 == TYPE_PRECISION (long_integer_type_node))
4867 (lfn:long_integer_type_node @0)))))
4869 /* cproj(x) -> x if we're ignoring infinities. */
4872 (if (!HONOR_INFINITIES (type))
4875 /* If the real part is inf and the imag part is known to be
4876 nonnegative, return (inf + 0i). */
4878 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4879 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4880 { build_complex_inf (type, false); }))
4882 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4884 (CPROJ (complex @0 REAL_CST@1))
4885 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4886 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4892 (pows @0 REAL_CST@1)
4894 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4895 REAL_VALUE_TYPE tmp;
4898 /* pow(x,0) -> 1. */
4899 (if (real_equal (value, &dconst0))
4900 { build_real (type, dconst1); })
4901 /* pow(x,1) -> x. */
4902 (if (real_equal (value, &dconst1))
4904 /* pow(x,-1) -> 1/x. */
4905 (if (real_equal (value, &dconstm1))
4906 (rdiv { build_real (type, dconst1); } @0))
4907 /* pow(x,0.5) -> sqrt(x). */
4908 (if (flag_unsafe_math_optimizations
4909 && canonicalize_math_p ()
4910 && real_equal (value, &dconsthalf))
4912 /* pow(x,1/3) -> cbrt(x). */
4913 (if (flag_unsafe_math_optimizations
4914 && canonicalize_math_p ()
4915 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4916 real_equal (value, &tmp)))
4919 /* powi(1,x) -> 1. */
4921 (POWI real_onep@0 @1)
4925 (POWI @0 INTEGER_CST@1)
4927 /* powi(x,0) -> 1. */
4928 (if (wi::to_wide (@1) == 0)
4929 { build_real (type, dconst1); })
4930 /* powi(x,1) -> x. */
4931 (if (wi::to_wide (@1) == 1)
4933 /* powi(x,-1) -> 1/x. */
4934 (if (wi::to_wide (@1) == -1)
4935 (rdiv { build_real (type, dconst1); } @0))))
4937 /* Narrowing of arithmetic and logical operations.
4939 These are conceptually similar to the transformations performed for
4940 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4941 term we want to move all that code out of the front-ends into here. */
4943 /* Convert (outertype)((innertype0)a+(innertype1)b)
4944 into ((newtype)a+(newtype)b) where newtype
4945 is the widest mode from all of these. */
4946 (for op (plus minus mult rdiv)
4948 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
4949 /* If we have a narrowing conversion of an arithmetic operation where
4950 both operands are widening conversions from the same type as the outer
4951 narrowing conversion. Then convert the innermost operands to a
4952 suitable unsigned type (to avoid introducing undefined behavior),
4953 perform the operation and convert the result to the desired type. */
4954 (if (INTEGRAL_TYPE_P (type)
4957 /* We check for type compatibility between @0 and @1 below,
4958 so there's no need to check that @2/@4 are integral types. */
4959 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
4960 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
4961 /* The precision of the type of each operand must match the
4962 precision of the mode of each operand, similarly for the
4964 && type_has_mode_precision_p (TREE_TYPE (@1))
4965 && type_has_mode_precision_p (TREE_TYPE (@2))
4966 && type_has_mode_precision_p (type)
4967 /* The inner conversion must be a widening conversion. */
4968 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
4969 && types_match (@1, type)
4970 && (types_match (@1, @2)
4971 /* Or the second operand is const integer or converted const
4972 integer from valueize. */
4973 || TREE_CODE (@2) == INTEGER_CST))
4974 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
4975 (op @1 (convert @2))
4976 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
4977 (convert (op (convert:utype @1)
4978 (convert:utype @2)))))
4979 (if (FLOAT_TYPE_P (type)
4980 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
4981 == DECIMAL_FLOAT_TYPE_P (type))
4982 (with { tree arg0 = strip_float_extensions (@1);
4983 tree arg1 = strip_float_extensions (@2);
4984 tree itype = TREE_TYPE (@0);
4985 tree ty1 = TREE_TYPE (arg0);
4986 tree ty2 = TREE_TYPE (arg1);
4987 enum tree_code code = TREE_CODE (itype); }
4988 (if (FLOAT_TYPE_P (ty1)
4989 && FLOAT_TYPE_P (ty2))
4990 (with { tree newtype = type;
4991 if (TYPE_MODE (ty1) == SDmode
4992 || TYPE_MODE (ty2) == SDmode
4993 || TYPE_MODE (type) == SDmode)
4994 newtype = dfloat32_type_node;
4995 if (TYPE_MODE (ty1) == DDmode
4996 || TYPE_MODE (ty2) == DDmode
4997 || TYPE_MODE (type) == DDmode)
4998 newtype = dfloat64_type_node;
4999 if (TYPE_MODE (ty1) == TDmode
5000 || TYPE_MODE (ty2) == TDmode
5001 || TYPE_MODE (type) == TDmode)
5002 newtype = dfloat128_type_node; }
5003 (if ((newtype == dfloat32_type_node
5004 || newtype == dfloat64_type_node
5005 || newtype == dfloat128_type_node)
5007 && types_match (newtype, type))
5008 (op (convert:newtype @1) (convert:newtype @2))
5009 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5011 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5013 /* Sometimes this transformation is safe (cannot
5014 change results through affecting double rounding
5015 cases) and sometimes it is not. If NEWTYPE is
5016 wider than TYPE, e.g. (float)((long double)double
5017 + (long double)double) converted to
5018 (float)(double + double), the transformation is
5019 unsafe regardless of the details of the types
5020 involved; double rounding can arise if the result
5021 of NEWTYPE arithmetic is a NEWTYPE value half way
5022 between two representable TYPE values but the
5023 exact value is sufficiently different (in the
5024 right direction) for this difference to be
5025 visible in ITYPE arithmetic. If NEWTYPE is the
5026 same as TYPE, however, the transformation may be
5027 safe depending on the types involved: it is safe
5028 if the ITYPE has strictly more than twice as many
5029 mantissa bits as TYPE, can represent infinities
5030 and NaNs if the TYPE can, and has sufficient
5031 exponent range for the product or ratio of two
5032 values representable in the TYPE to be within the
5033 range of normal values of ITYPE. */
5034 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5035 && (flag_unsafe_math_optimizations
5036 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5037 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5039 && !excess_precision_type (newtype)))
5040 && !types_match (itype, newtype))
5041 (convert:type (op (convert:newtype @1)
5042 (convert:newtype @2)))
5047 /* This is another case of narrowing, specifically when there's an outer
5048 BIT_AND_EXPR which masks off bits outside the type of the innermost
5049 operands. Like the previous case we have to convert the operands
5050 to unsigned types to avoid introducing undefined behavior for the
5051 arithmetic operation. */
5052 (for op (minus plus)
5054 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5055 (if (INTEGRAL_TYPE_P (type)
5056 /* We check for type compatibility between @0 and @1 below,
5057 so there's no need to check that @1/@3 are integral types. */
5058 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5059 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5060 /* The precision of the type of each operand must match the
5061 precision of the mode of each operand, similarly for the
5063 && type_has_mode_precision_p (TREE_TYPE (@0))
5064 && type_has_mode_precision_p (TREE_TYPE (@1))
5065 && type_has_mode_precision_p (type)
5066 /* The inner conversion must be a widening conversion. */
5067 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5068 && types_match (@0, @1)
5069 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5070 <= TYPE_PRECISION (TREE_TYPE (@0)))
5071 && (wi::to_wide (@4)
5072 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5073 true, TYPE_PRECISION (type))) == 0)
5074 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5075 (with { tree ntype = TREE_TYPE (@0); }
5076 (convert (bit_and (op @0 @1) (convert:ntype @4))))
5077 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5078 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5079 (convert:utype @4))))))))
5081 /* Transform (@0 < @1 and @0 < @2) to use min,
5082 (@0 > @1 and @0 > @2) to use max */
5083 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5084 op (lt le gt ge lt le gt ge )
5085 ext (min min max max max max min min )
5087 (logic (op:cs @0 @1) (op:cs @0 @2))
5088 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5089 && TREE_CODE (@0) != INTEGER_CST)
5090 (op @0 (ext @1 @2)))))
5093 /* signbit(x) -> 0 if x is nonnegative. */
5094 (SIGNBIT tree_expr_nonnegative_p@0)
5095 { integer_zero_node; })
5098 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
5100 (if (!HONOR_SIGNED_ZEROS (@0))
5101 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
5103 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
5105 (for op (plus minus)
5108 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5109 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5110 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
5111 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
5112 && !TYPE_SATURATING (TREE_TYPE (@0)))
5113 (with { tree res = int_const_binop (rop, @2, @1); }
5114 (if (TREE_OVERFLOW (res)
5115 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5116 { constant_boolean_node (cmp == NE_EXPR, type); }
5117 (if (single_use (@3))
5118 (cmp @0 { TREE_OVERFLOW (res)
5119 ? drop_tree_overflow (res) : res; }))))))))
5120 (for cmp (lt le gt ge)
5121 (for op (plus minus)
5124 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5125 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5126 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5127 (with { tree res = int_const_binop (rop, @2, @1); }
5128 (if (TREE_OVERFLOW (res))
5130 fold_overflow_warning (("assuming signed overflow does not occur "
5131 "when simplifying conditional to constant"),
5132 WARN_STRICT_OVERFLOW_CONDITIONAL);
5133 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
5134 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
5135 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
5136 TYPE_SIGN (TREE_TYPE (@1)))
5137 != (op == MINUS_EXPR);
5138 constant_boolean_node (less == ovf_high, type);
5140 (if (single_use (@3))
5143 fold_overflow_warning (("assuming signed overflow does not occur "
5144 "when changing X +- C1 cmp C2 to "
5146 WARN_STRICT_OVERFLOW_COMPARISON);
5148 (cmp @0 { res; })))))))))
5150 /* Canonicalizations of BIT_FIELD_REFs. */
5153 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5154 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5157 (BIT_FIELD_REF (view_convert @0) @1 @2)
5158 (BIT_FIELD_REF @0 @1 @2))
5161 (BIT_FIELD_REF @0 @1 integer_zerop)
5162 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5166 (BIT_FIELD_REF @0 @1 @2)
5168 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5169 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5171 (if (integer_zerop (@2))
5172 (view_convert (realpart @0)))
5173 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5174 (view_convert (imagpart @0)))))
5175 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5176 && INTEGRAL_TYPE_P (type)
5177 /* On GIMPLE this should only apply to register arguments. */
5178 && (! GIMPLE || is_gimple_reg (@0))
5179 /* A bit-field-ref that referenced the full argument can be stripped. */
5180 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5181 && integer_zerop (@2))
5182 /* Low-parts can be reduced to integral conversions.
5183 ??? The following doesn't work for PDP endian. */
5184 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5185 /* Don't even think about BITS_BIG_ENDIAN. */
5186 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5187 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5188 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5189 ? (TYPE_PRECISION (TREE_TYPE (@0))
5190 - TYPE_PRECISION (type))
5194 /* Simplify vector extracts. */
5197 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5198 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5199 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5200 || (VECTOR_TYPE_P (type)
5201 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5204 tree ctor = (TREE_CODE (@0) == SSA_NAME
5205 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5206 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5207 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5208 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5209 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5212 && (idx % width) == 0
5214 && known_le ((idx + n) / width,
5215 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5220 /* Constructor elements can be subvectors. */
5222 if (CONSTRUCTOR_NELTS (ctor) != 0)
5224 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5225 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5226 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5228 unsigned HOST_WIDE_INT elt, count, const_k;
5231 /* We keep an exact subset of the constructor elements. */
5232 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5233 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5234 { build_constructor (type, NULL); }
5236 (if (elt < CONSTRUCTOR_NELTS (ctor))
5237 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5238 { build_zero_cst (type); })
5240 vec<constructor_elt, va_gc> *vals;
5241 vec_alloc (vals, count);
5242 for (unsigned i = 0;
5243 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5244 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5245 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5246 build_constructor (type, vals);
5248 /* The bitfield references a single constructor element. */
5249 (if (k.is_constant (&const_k)
5250 && idx + n <= (idx / const_k + 1) * const_k)
5252 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5253 { build_zero_cst (type); })
5255 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5256 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5257 @1 { bitsize_int ((idx % const_k) * width); })))))))))
5259 /* Simplify a bit extraction from a bit insertion for the cases with
5260 the inserted element fully covering the extraction or the insertion
5261 not touching the extraction. */
5263 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5266 unsigned HOST_WIDE_INT isize;
5267 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5268 isize = TYPE_PRECISION (TREE_TYPE (@1));
5270 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5273 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5274 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5275 wi::to_wide (@ipos) + isize))
5276 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
5278 - wi::to_wide (@ipos)); }))
5279 (if (wi::geu_p (wi::to_wide (@ipos),
5280 wi::to_wide (@rpos) + wi::to_wide (@rsize))
5281 || wi::geu_p (wi::to_wide (@rpos),
5282 wi::to_wide (@ipos) + isize))
5283 (BIT_FIELD_REF @0 @rsize @rpos)))))
5285 (if (canonicalize_math_after_vectorization_p ())
5288 (fmas:c (negate @0) @1 @2)
5289 (IFN_FNMA @0 @1 @2))
5291 (fmas @0 @1 (negate @2))
5294 (fmas:c (negate @0) @1 (negate @2))
5295 (IFN_FNMS @0 @1 @2))
5297 (negate (fmas@3 @0 @1 @2))
5298 (if (single_use (@3))
5299 (IFN_FNMS @0 @1 @2))))
5302 (IFN_FMS:c (negate @0) @1 @2)
5303 (IFN_FNMS @0 @1 @2))
5305 (IFN_FMS @0 @1 (negate @2))
5308 (IFN_FMS:c (negate @0) @1 (negate @2))
5309 (IFN_FNMA @0 @1 @2))
5311 (negate (IFN_FMS@3 @0 @1 @2))
5312 (if (single_use (@3))
5313 (IFN_FNMA @0 @1 @2)))
5316 (IFN_FNMA:c (negate @0) @1 @2)
5319 (IFN_FNMA @0 @1 (negate @2))
5320 (IFN_FNMS @0 @1 @2))
5322 (IFN_FNMA:c (negate @0) @1 (negate @2))
5325 (negate (IFN_FNMA@3 @0 @1 @2))
5326 (if (single_use (@3))
5327 (IFN_FMS @0 @1 @2)))
5330 (IFN_FNMS:c (negate @0) @1 @2)
5333 (IFN_FNMS @0 @1 (negate @2))
5334 (IFN_FNMA @0 @1 @2))
5336 (IFN_FNMS:c (negate @0) @1 (negate @2))
5339 (negate (IFN_FNMS@3 @0 @1 @2))
5340 (if (single_use (@3))
5341 (IFN_FMA @0 @1 @2))))
5343 /* POPCOUNT simplifications. */
5344 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5345 BUILT_IN_POPCOUNTIMAX)
5346 /* popcount(X&1) is nop_expr(X&1). */
5349 (if (tree_nonzero_bits (@0) == 1)
5351 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
5353 (plus (popcount:s @0) (popcount:s @1))
5354 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5355 (popcount (bit_ior @0 @1))))
5356 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
5357 (for cmp (le eq ne gt)
5360 (cmp (popcount @0) integer_zerop)
5361 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5370 r = c ? a1 op a2 : b;
5372 if the target can do it in one go. This makes the operation conditional
5373 on c, so could drop potentially-trapping arithmetic, but that's a valid
5374 simplification if the result of the operation isn't needed.
5376 Avoid speculatively generating a stand-alone vector comparison
5377 on targets that might not support them. Any target implementing
5378 conditional internal functions must support the same comparisons
5379 inside and outside a VEC_COND_EXPR. */
5382 (for uncond_op (UNCOND_BINARY)
5383 cond_op (COND_BINARY)
5385 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5386 (with { tree op_type = TREE_TYPE (@4); }
5387 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5388 && element_precision (type) == element_precision (op_type))
5389 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5391 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5392 (with { tree op_type = TREE_TYPE (@4); }
5393 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5394 && element_precision (type) == element_precision (op_type))
5395 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5397 /* Same for ternary operations. */
5398 (for uncond_op (UNCOND_TERNARY)
5399 cond_op (COND_TERNARY)
5401 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5402 (with { tree op_type = TREE_TYPE (@5); }
5403 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5404 && element_precision (type) == element_precision (op_type))
5405 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5407 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5408 (with { tree op_type = TREE_TYPE (@5); }
5409 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5410 && element_precision (type) == element_precision (op_type))
5411 (view_convert (cond_op (bit_not @0) @2 @3 @4
5412 (view_convert:op_type @1)))))))
5415 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5416 "else" value of an IFN_COND_*. */
5417 (for cond_op (COND_BINARY)
5419 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5420 (with { tree op_type = TREE_TYPE (@3); }
5421 (if (element_precision (type) == element_precision (op_type))
5422 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5424 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5425 (with { tree op_type = TREE_TYPE (@5); }
5426 (if (inverse_conditions_p (@0, @2)
5427 && element_precision (type) == element_precision (op_type))
5428 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
5430 /* Same for ternary operations. */
5431 (for cond_op (COND_TERNARY)
5433 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5434 (with { tree op_type = TREE_TYPE (@4); }
5435 (if (element_precision (type) == element_precision (op_type))
5436 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5438 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5439 (with { tree op_type = TREE_TYPE (@6); }
5440 (if (inverse_conditions_p (@0, @2)
5441 && element_precision (type) == element_precision (op_type))
5442 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5444 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5447 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5448 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5450 If pointers are known not to wrap, B checks whether @1 bytes starting
5451 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5452 bytes. A is more efficiently tested as:
5454 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5456 The equivalent expression for B is given by replacing @1 with @1 - 1:
5458 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5460 @0 and @2 can be swapped in both expressions without changing the result.
5462 The folds rely on sizetype's being unsigned (which is always true)
5463 and on its being the same width as the pointer (which we have to check).
5465 The fold replaces two pointer_plus expressions, two comparisons and
5466 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5467 the best case it's a saving of two operations. The A fold retains one
5468 of the original pointer_pluses, so is a win even if both pointer_pluses
5469 are used elsewhere. The B fold is a wash if both pointer_pluses are
5470 used elsewhere, since all we end up doing is replacing a comparison with
5471 a pointer_plus. We do still apply the fold under those circumstances
5472 though, in case applying it to other conditions eventually makes one of the
5473 pointer_pluses dead. */
5474 (for ior (truth_orif truth_or bit_ior)
5477 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5478 (cmp:cs (pointer_plus@4 @2 @1) @0))
5479 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5480 && TYPE_OVERFLOW_WRAPS (sizetype)
5481 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5482 /* Calculate the rhs constant. */
5483 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5484 offset_int rhs = off * 2; }
5485 /* Always fails for negative values. */
5486 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5487 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5488 pick a canonical order. This increases the chances of using the
5489 same pointer_plus in multiple checks. */
5490 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5491 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5492 (if (cmp == LT_EXPR)
5493 (gt (convert:sizetype
5494 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5495 { swap_p ? @0 : @2; }))
5497 (gt (convert:sizetype
5498 (pointer_diff:ssizetype
5499 (pointer_plus { swap_p ? @2 : @0; }
5500 { wide_int_to_tree (sizetype, off); })
5501 { swap_p ? @0 : @2; }))
5502 { rhs_tree; })))))))))
5504 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
5506 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
5507 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
5508 (with { int i = single_nonzero_element (@1); }
5510 (with { tree elt = vector_cst_elt (@1, i);
5511 tree elt_type = TREE_TYPE (elt);
5512 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
5513 tree size = bitsize_int (elt_bits);
5514 tree pos = bitsize_int (elt_bits * i); }
5517 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
5521 (vec_perm @0 @1 VECTOR_CST@2)
5524 tree op0 = @0, op1 = @1, op2 = @2;
5526 /* Build a vector of integers from the tree mask. */
5527 vec_perm_builder builder;
5528 if (!tree_to_vec_perm_builder (&builder, op2))
5531 /* Create a vec_perm_indices for the integer vector. */
5532 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
5533 bool single_arg = (op0 == op1);
5534 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
5536 (if (sel.series_p (0, 1, 0, 1))
5538 (if (sel.series_p (0, 1, nelts, 1))
5544 if (sel.all_from_input_p (0))
5546 else if (sel.all_from_input_p (1))
5549 sel.rotate_inputs (1);
5551 else if (known_ge (poly_uint64 (sel[0]), nelts))
5553 std::swap (op0, op1);
5554 sel.rotate_inputs (1);
5558 tree cop0 = op0, cop1 = op1;
5559 if (TREE_CODE (op0) == SSA_NAME
5560 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
5561 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5562 cop0 = gimple_assign_rhs1 (def);
5563 if (TREE_CODE (op1) == SSA_NAME
5564 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
5565 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5566 cop1 = gimple_assign_rhs1 (def);
5570 (if ((TREE_CODE (cop0) == VECTOR_CST
5571 || TREE_CODE (cop0) == CONSTRUCTOR)
5572 && (TREE_CODE (cop1) == VECTOR_CST
5573 || TREE_CODE (cop1) == CONSTRUCTOR)
5574 && (t = fold_vec_perm (type, cop0, cop1, sel)))
5578 bool changed = (op0 == op1 && !single_arg);
5579 tree ins = NULL_TREE;
5582 /* See if the permutation is performing a single element
5583 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
5584 in that case. But only if the vector mode is supported,
5585 otherwise this is invalid GIMPLE. */
5586 if (TYPE_MODE (type) != BLKmode
5587 && (TREE_CODE (cop0) == VECTOR_CST
5588 || TREE_CODE (cop0) == CONSTRUCTOR
5589 || TREE_CODE (cop1) == VECTOR_CST
5590 || TREE_CODE (cop1) == CONSTRUCTOR))
5592 if (sel.series_p (1, 1, nelts + 1, 1))
5594 /* After canonicalizing the first elt to come from the
5595 first vector we only can insert the first elt from
5596 the first vector. */
5598 if ((ins = fold_read_from_vector (cop0, sel[0])))
5603 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
5604 for (at = 0; at < encoded_nelts; ++at)
5605 if (maybe_ne (sel[at], at))
5607 if (at < encoded_nelts && sel.series_p (at + 1, 1, at + 1, 1))
5609 if (known_lt (at, nelts))
5610 ins = fold_read_from_vector (cop0, sel[at]);
5612 ins = fold_read_from_vector (cop1, sel[at] - nelts);
5617 /* Generate a canonical form of the selector. */
5618 if (!ins && sel.encoding () != builder)
5620 /* Some targets are deficient and fail to expand a single
5621 argument permutation while still allowing an equivalent
5622 2-argument version. */
5624 if (sel.ninputs () == 2
5625 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
5626 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5629 vec_perm_indices sel2 (builder, 2, nelts);
5630 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
5631 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
5633 /* Not directly supported with either encoding,
5634 so use the preferred form. */
5635 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5637 if (!operand_equal_p (op2, oldop2, 0))
5642 (bit_insert { op0; } { ins; }
5643 { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); })
5645 (vec_perm { op0; } { op1; } { op2; }))))))))))
5647 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
5649 (match vec_same_elem_p
5651 (if (uniform_vector_p (@0))))
5653 (match vec_same_elem_p
5657 (vec_perm vec_same_elem_p@0 @0 @1)