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.
309 Also also widening conversions, like:
310 (A / (unsigned long long) (1U << B)) -> (A >> B)
312 (A / (unsigned long long) (1 << B)) -> (A >> B).
313 If the left shift is signed, it can be done only if the upper bits
314 of A starting from shift's type sign bit are zero, as
315 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
316 so it is valid only if A >> 31 is zero. */
318 (trunc_div @0 (convert? (lshift integer_onep@1 @2)))
319 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
320 && (!VECTOR_TYPE_P (type)
321 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
322 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
323 && (useless_type_conversion_p (type, TREE_TYPE (@1))
324 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
325 && (TYPE_UNSIGNED (TREE_TYPE (@1))
326 || (element_precision (type)
327 == element_precision (TREE_TYPE (@1)))
328 || (INTEGRAL_TYPE_P (type)
329 && (tree_nonzero_bits (@0)
330 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
332 element_precision (type))) == 0)))))
335 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
336 undefined behavior in constexpr evaluation, and assuming that the division
337 traps enables better optimizations than these anyway. */
338 (for div (trunc_div ceil_div floor_div round_div exact_div)
339 /* 0 / X is always zero. */
341 (div integer_zerop@0 @1)
342 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
343 (if (!integer_zerop (@1))
347 (div @0 integer_minus_onep@1)
348 (if (!TYPE_UNSIGNED (type))
353 /* But not for 0 / 0 so that we can get the proper warnings and errors.
354 And not for _Fract types where we can't build 1. */
355 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
356 { build_one_cst (type); }))
357 /* X / abs (X) is X < 0 ? -1 : 1. */
360 (if (INTEGRAL_TYPE_P (type)
361 && TYPE_OVERFLOW_UNDEFINED (type))
362 (cond (lt @0 { build_zero_cst (type); })
363 { build_minus_one_cst (type); } { build_one_cst (type); })))
366 (div:C @0 (negate @0))
367 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
368 && TYPE_OVERFLOW_UNDEFINED (type))
369 { build_minus_one_cst (type); })))
371 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
372 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
375 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
376 && TYPE_UNSIGNED (type))
379 /* Combine two successive divisions. Note that combining ceil_div
380 and floor_div is trickier and combining round_div even more so. */
381 (for div (trunc_div exact_div)
383 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
385 wi::overflow_type overflow;
386 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
387 TYPE_SIGN (type), &overflow);
389 (if (div == EXACT_DIV_EXPR
390 || optimize_successive_divisions_p (@2, @3))
392 (div @0 { wide_int_to_tree (type, mul); })
393 (if (TYPE_UNSIGNED (type)
394 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
395 { build_zero_cst (type); }))))))
397 /* Combine successive multiplications. Similar to above, but handling
398 overflow is different. */
400 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
402 wi::overflow_type overflow;
403 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
404 TYPE_SIGN (type), &overflow);
406 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
407 otherwise undefined overflow implies that @0 must be zero. */
408 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
409 (mult @0 { wide_int_to_tree (type, mul); }))))
411 /* Optimize A / A to 1.0 if we don't care about
412 NaNs or Infinities. */
415 (if (FLOAT_TYPE_P (type)
416 && ! HONOR_NANS (type)
417 && ! HONOR_INFINITIES (type))
418 { build_one_cst (type); }))
420 /* Optimize -A / A to -1.0 if we don't care about
421 NaNs or Infinities. */
423 (rdiv:C @0 (negate @0))
424 (if (FLOAT_TYPE_P (type)
425 && ! HONOR_NANS (type)
426 && ! HONOR_INFINITIES (type))
427 { build_minus_one_cst (type); }))
429 /* PR71078: x / abs(x) -> copysign (1.0, x) */
431 (rdiv:C (convert? @0) (convert? (abs @0)))
432 (if (SCALAR_FLOAT_TYPE_P (type)
433 && ! HONOR_NANS (type)
434 && ! HONOR_INFINITIES (type))
436 (if (types_match (type, float_type_node))
437 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
438 (if (types_match (type, double_type_node))
439 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
440 (if (types_match (type, long_double_type_node))
441 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
443 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
446 (if (!HONOR_SNANS (type))
449 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
451 (rdiv @0 real_minus_onep)
452 (if (!HONOR_SNANS (type))
455 (if (flag_reciprocal_math)
456 /* Convert (A/B)/C to A/(B*C). */
458 (rdiv (rdiv:s @0 @1) @2)
459 (rdiv @0 (mult @1 @2)))
461 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
463 (rdiv @0 (mult:s @1 REAL_CST@2))
465 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
467 (rdiv (mult @0 { tem; } ) @1))))
469 /* Convert A/(B/C) to (A/B)*C */
471 (rdiv @0 (rdiv:s @1 @2))
472 (mult (rdiv @0 @1) @2)))
474 /* Simplify x / (- y) to -x / y. */
476 (rdiv @0 (negate @1))
477 (rdiv (negate @0) @1))
479 (if (flag_unsafe_math_optimizations)
480 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
481 Since C / x may underflow to zero, do this only for unsafe math. */
482 (for op (lt le gt ge)
485 (op (rdiv REAL_CST@0 @1) real_zerop@2)
486 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
488 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
490 /* For C < 0, use the inverted operator. */
491 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
494 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
495 (for div (trunc_div ceil_div floor_div round_div exact_div)
497 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
498 (if (integer_pow2p (@2)
499 && tree_int_cst_sgn (@2) > 0
500 && tree_nop_conversion_p (type, TREE_TYPE (@0))
501 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
503 { build_int_cst (integer_type_node,
504 wi::exact_log2 (wi::to_wide (@2))); }))))
506 /* If ARG1 is a constant, we can convert this to a multiply by the
507 reciprocal. This does not have the same rounding properties,
508 so only do this if -freciprocal-math. We can actually
509 always safely do it if ARG1 is a power of two, but it's hard to
510 tell if it is or not in a portable manner. */
511 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
515 (if (flag_reciprocal_math
518 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
520 (mult @0 { tem; } )))
521 (if (cst != COMPLEX_CST)
522 (with { tree inverse = exact_inverse (type, @1); }
524 (mult @0 { inverse; } ))))))))
526 (for mod (ceil_mod floor_mod round_mod trunc_mod)
527 /* 0 % X is always zero. */
529 (mod integer_zerop@0 @1)
530 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
531 (if (!integer_zerop (@1))
533 /* X % 1 is always zero. */
535 (mod @0 integer_onep)
536 { build_zero_cst (type); })
537 /* X % -1 is zero. */
539 (mod @0 integer_minus_onep@1)
540 (if (!TYPE_UNSIGNED (type))
541 { build_zero_cst (type); }))
545 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
546 (if (!integer_zerop (@0))
547 { build_zero_cst (type); }))
548 /* (X % Y) % Y is just X % Y. */
550 (mod (mod@2 @0 @1) @1)
552 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
554 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
555 (if (ANY_INTEGRAL_TYPE_P (type)
556 && TYPE_OVERFLOW_UNDEFINED (type)
557 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
559 { build_zero_cst (type); }))
560 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
561 modulo and comparison, since it is simpler and equivalent. */
564 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
565 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
566 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
567 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
569 /* X % -C is the same as X % C. */
571 (trunc_mod @0 INTEGER_CST@1)
572 (if (TYPE_SIGN (type) == SIGNED
573 && !TREE_OVERFLOW (@1)
574 && wi::neg_p (wi::to_wide (@1))
575 && !TYPE_OVERFLOW_TRAPS (type)
576 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
577 && !sign_bit_p (@1, @1))
578 (trunc_mod @0 (negate @1))))
580 /* X % -Y is the same as X % Y. */
582 (trunc_mod @0 (convert? (negate @1)))
583 (if (INTEGRAL_TYPE_P (type)
584 && !TYPE_UNSIGNED (type)
585 && !TYPE_OVERFLOW_TRAPS (type)
586 && tree_nop_conversion_p (type, TREE_TYPE (@1))
587 /* Avoid this transformation if X might be INT_MIN or
588 Y might be -1, because we would then change valid
589 INT_MIN % -(-1) into invalid INT_MIN % -1. */
590 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
591 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
593 (trunc_mod @0 (convert @1))))
595 /* X - (X / Y) * Y is the same as X % Y. */
597 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
598 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
599 (convert (trunc_mod @0 @1))))
601 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
602 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
603 Also optimize A % (C << N) where C is a power of 2,
604 to A & ((C << N) - 1). */
605 (match (power_of_two_cand @1)
607 (match (power_of_two_cand @1)
608 (lshift INTEGER_CST@1 @2))
609 (for mod (trunc_mod floor_mod)
611 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
612 (if ((TYPE_UNSIGNED (type)
613 || tree_expr_nonnegative_p (@0))
614 && tree_nop_conversion_p (type, TREE_TYPE (@3))
615 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
616 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
618 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
620 (trunc_div (mult @0 integer_pow2p@1) @1)
621 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
622 (bit_and @0 { wide_int_to_tree
623 (type, wi::mask (TYPE_PRECISION (type)
624 - wi::exact_log2 (wi::to_wide (@1)),
625 false, TYPE_PRECISION (type))); })))
627 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
629 (mult (trunc_div @0 integer_pow2p@1) @1)
630 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
631 (bit_and @0 (negate @1))))
633 /* Simplify (t * 2) / 2) -> t. */
634 (for div (trunc_div ceil_div floor_div round_div exact_div)
636 (div (mult:c @0 @1) @1)
637 (if (ANY_INTEGRAL_TYPE_P (type)
638 && TYPE_OVERFLOW_UNDEFINED (type))
642 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
647 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
650 (pows (op @0) REAL_CST@1)
651 (with { HOST_WIDE_INT n; }
652 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
654 /* Likewise for powi. */
657 (pows (op @0) INTEGER_CST@1)
658 (if ((wi::to_wide (@1) & 1) == 0)
660 /* Strip negate and abs from both operands of hypot. */
668 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
669 (for copysigns (COPYSIGN_ALL)
671 (copysigns (op @0) @1)
674 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
679 /* Convert absu(x)*absu(x) -> x*x. */
681 (mult (absu@1 @0) @1)
682 (mult (convert@2 @0) @2))
684 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
688 (coss (copysigns @0 @1))
691 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
695 (pows (copysigns @0 @2) REAL_CST@1)
696 (with { HOST_WIDE_INT n; }
697 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
699 /* Likewise for powi. */
703 (pows (copysigns @0 @2) INTEGER_CST@1)
704 (if ((wi::to_wide (@1) & 1) == 0)
709 /* hypot(copysign(x, y), z) -> hypot(x, z). */
711 (hypots (copysigns @0 @1) @2)
713 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
715 (hypots @0 (copysigns @1 @2))
718 /* copysign(x, CST) -> [-]abs (x). */
719 (for copysigns (COPYSIGN_ALL)
721 (copysigns @0 REAL_CST@1)
722 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
726 /* copysign(copysign(x, y), z) -> copysign(x, z). */
727 (for copysigns (COPYSIGN_ALL)
729 (copysigns (copysigns @0 @1) @2)
732 /* copysign(x,y)*copysign(x,y) -> x*x. */
733 (for copysigns (COPYSIGN_ALL)
735 (mult (copysigns@2 @0 @1) @2)
738 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
739 (for ccoss (CCOS CCOSH)
744 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
745 (for ops (conj negate)
751 /* Fold (a * (1 << b)) into (a << b) */
753 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
754 (if (! FLOAT_TYPE_P (type)
755 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
758 /* Fold (1 << (C - x)) where C = precision(type) - 1
759 into ((1 << C) >> x). */
761 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
762 (if (INTEGRAL_TYPE_P (type)
763 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
765 (if (TYPE_UNSIGNED (type))
766 (rshift (lshift @0 @2) @3)
768 { tree utype = unsigned_type_for (type); }
769 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
771 /* Fold (C1/X)*C2 into (C1*C2)/X. */
773 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
774 (if (flag_associative_math
777 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
779 (rdiv { tem; } @1)))))
781 /* Simplify ~X & X as zero. */
783 (bit_and:c (convert? @0) (convert? (bit_not @0)))
784 { build_zero_cst (type); })
786 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
788 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
789 (if (TYPE_UNSIGNED (type))
790 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
792 (for bitop (bit_and bit_ior)
794 /* PR35691: Transform
795 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
796 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
798 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
799 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
800 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
801 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
802 (cmp (bit_ior @0 (convert @1)) @2)))
804 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
805 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
807 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
808 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
809 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
810 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
811 (cmp (bit_and @0 (convert @1)) @2))))
813 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
815 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
816 (minus (bit_xor @0 @1) @1))
818 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
819 (if (~wi::to_wide (@2) == wi::to_wide (@1))
820 (minus (bit_xor @0 @1) @1)))
822 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
824 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
825 (minus @1 (bit_xor @0 @1)))
827 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
828 (for op (bit_ior bit_xor plus)
830 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
833 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
834 (if (~wi::to_wide (@2) == wi::to_wide (@1))
837 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
839 (bit_ior:c (bit_xor:c @0 @1) @0)
842 /* (a & ~b) | (a ^ b) --> a ^ b */
844 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
847 /* (a & ~b) ^ ~a --> ~(a & b) */
849 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
850 (bit_not (bit_and @0 @1)))
852 /* (~a & b) ^ a --> (a | b) */
854 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
857 /* (a | b) & ~(a ^ b) --> a & b */
859 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
862 /* a | ~(a ^ b) --> a | ~b */
864 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
865 (bit_ior @0 (bit_not @1)))
867 /* (a | b) | (a &^ b) --> a | b */
868 (for op (bit_and bit_xor)
870 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
873 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
875 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
878 /* ~(~a & b) --> a | ~b */
880 (bit_not (bit_and:cs (bit_not @0) @1))
881 (bit_ior @0 (bit_not @1)))
883 /* ~(~a | b) --> a & ~b */
885 (bit_not (bit_ior:cs (bit_not @0) @1))
886 (bit_and @0 (bit_not @1)))
888 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
891 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
892 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
893 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
897 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
898 ((A & N) + B) & M -> (A + B) & M
899 Similarly if (N & M) == 0,
900 ((A | N) + B) & M -> (A + B) & M
901 and for - instead of + (or unary - instead of +)
902 and/or ^ instead of |.
903 If B is constant and (B & M) == 0, fold into A & M. */
905 (for bitop (bit_and bit_ior bit_xor)
907 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
910 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
911 @3, @4, @1, ERROR_MARK, NULL_TREE,
914 (convert (bit_and (op (convert:utype { pmop[0]; })
915 (convert:utype { pmop[1]; }))
916 (convert:utype @2))))))
918 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
921 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
922 NULL_TREE, NULL_TREE, @1, bitop, @3,
925 (convert (bit_and (op (convert:utype { pmop[0]; })
926 (convert:utype { pmop[1]; }))
927 (convert:utype @2)))))))
929 (bit_and (op:s @0 @1) INTEGER_CST@2)
932 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
933 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
934 NULL_TREE, NULL_TREE, pmop); }
936 (convert (bit_and (op (convert:utype { pmop[0]; })
937 (convert:utype { pmop[1]; }))
938 (convert:utype @2)))))))
939 (for bitop (bit_and bit_ior bit_xor)
941 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
944 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
945 bitop, @2, @3, NULL_TREE, ERROR_MARK,
946 NULL_TREE, NULL_TREE, pmop); }
948 (convert (bit_and (negate (convert:utype { pmop[0]; }))
949 (convert:utype @1)))))))
951 /* X % Y is smaller than Y. */
954 (cmp (trunc_mod @0 @1) @1)
955 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
956 { constant_boolean_node (cmp == LT_EXPR, type); })))
959 (cmp @1 (trunc_mod @0 @1))
960 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
961 { constant_boolean_node (cmp == GT_EXPR, type); })))
965 (bit_ior @0 integer_all_onesp@1)
970 (bit_ior @0 integer_zerop)
975 (bit_and @0 integer_zerop@1)
981 (for op (bit_ior bit_xor plus)
983 (op:c (convert? @0) (convert? (bit_not @0)))
984 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
989 { build_zero_cst (type); })
991 /* Canonicalize X ^ ~0 to ~X. */
993 (bit_xor @0 integer_all_onesp@1)
998 (bit_and @0 integer_all_onesp)
1001 /* x & x -> x, x | x -> x */
1002 (for bitop (bit_and bit_ior)
1007 /* x & C -> x if we know that x & ~C == 0. */
1010 (bit_and SSA_NAME@0 INTEGER_CST@1)
1011 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1012 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1016 /* x + (x & 1) -> (x + 1) & ~1 */
1018 (plus:c @0 (bit_and:s @0 integer_onep@1))
1019 (bit_and (plus @0 @1) (bit_not @1)))
1021 /* x & ~(x & y) -> x & ~y */
1022 /* x | ~(x | y) -> x | ~y */
1023 (for bitop (bit_and bit_ior)
1025 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1026 (bitop @0 (bit_not @1))))
1028 /* (~x & y) | ~(x | y) -> ~x */
1030 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1033 /* (x | y) ^ (x | ~y) -> ~x */
1035 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1038 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1040 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1041 (bit_not (bit_xor @0 @1)))
1043 /* (~x | y) ^ (x ^ y) -> x | ~y */
1045 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1046 (bit_ior @0 (bit_not @1)))
1048 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1050 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1051 (bit_not (bit_and @0 @1)))
1053 /* (x | y) & ~x -> y & ~x */
1054 /* (x & y) | ~x -> y | ~x */
1055 (for bitop (bit_and bit_ior)
1056 rbitop (bit_ior bit_and)
1058 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1061 /* (x & y) ^ (x | y) -> x ^ y */
1063 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1066 /* (x ^ y) ^ (x | y) -> x & y */
1068 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1071 /* (x & y) + (x ^ y) -> x | y */
1072 /* (x & y) | (x ^ y) -> x | y */
1073 /* (x & y) ^ (x ^ y) -> x | y */
1074 (for op (plus bit_ior bit_xor)
1076 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1079 /* (x & y) + (x | y) -> x + y */
1081 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1084 /* (x + y) - (x | y) -> x & y */
1086 (minus (plus @0 @1) (bit_ior @0 @1))
1087 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1088 && !TYPE_SATURATING (type))
1091 /* (x + y) - (x & y) -> x | y */
1093 (minus (plus @0 @1) (bit_and @0 @1))
1094 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1095 && !TYPE_SATURATING (type))
1098 /* (x | y) - (x ^ y) -> x & y */
1100 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1103 /* (x | y) - (x & y) -> x ^ y */
1105 (minus (bit_ior @0 @1) (bit_and @0 @1))
1108 /* (x | y) & ~(x & y) -> x ^ y */
1110 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1113 /* (x | y) & (~x ^ y) -> x & y */
1115 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1118 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1120 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1121 (bit_not (bit_xor @0 @1)))
1123 /* (~x | y) ^ (x | ~y) -> x ^ y */
1125 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1128 /* ~x & ~y -> ~(x | y)
1129 ~x | ~y -> ~(x & y) */
1130 (for op (bit_and bit_ior)
1131 rop (bit_ior bit_and)
1133 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1134 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1135 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1136 (bit_not (rop (convert @0) (convert @1))))))
1138 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1139 with a constant, and the two constants have no bits in common,
1140 we should treat this as a BIT_IOR_EXPR since this may produce more
1142 (for op (bit_xor plus)
1144 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1145 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1146 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1147 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1148 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1149 (bit_ior (convert @4) (convert @5)))))
1151 /* (X | Y) ^ X -> Y & ~ X*/
1153 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1154 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1155 (convert (bit_and @1 (bit_not @0)))))
1157 /* Convert ~X ^ ~Y to X ^ Y. */
1159 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1160 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1161 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1162 (bit_xor (convert @0) (convert @1))))
1164 /* Convert ~X ^ C to X ^ ~C. */
1166 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1167 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1168 (bit_xor (convert @0) (bit_not @1))))
1170 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1171 (for opo (bit_and bit_xor)
1172 opi (bit_xor bit_and)
1174 (opo:c (opi:cs @0 @1) @1)
1175 (bit_and (bit_not @0) @1)))
1177 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1178 operands are another bit-wise operation with a common input. If so,
1179 distribute the bit operations to save an operation and possibly two if
1180 constants are involved. For example, convert
1181 (A | B) & (A | C) into A | (B & C)
1182 Further simplification will occur if B and C are constants. */
1183 (for op (bit_and bit_ior bit_xor)
1184 rop (bit_ior bit_and bit_and)
1186 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1187 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1188 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1189 (rop (convert @0) (op (convert @1) (convert @2))))))
1191 /* Some simple reassociation for bit operations, also handled in reassoc. */
1192 /* (X & Y) & Y -> X & Y
1193 (X | Y) | Y -> X | Y */
1194 (for op (bit_and bit_ior)
1196 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1198 /* (X ^ Y) ^ Y -> X */
1200 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1202 /* (X & Y) & (X & Z) -> (X & Y) & Z
1203 (X | Y) | (X | Z) -> (X | Y) | Z */
1204 (for op (bit_and bit_ior)
1206 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1207 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1208 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1209 (if (single_use (@5) && single_use (@6))
1210 (op @3 (convert @2))
1211 (if (single_use (@3) && single_use (@4))
1212 (op (convert @1) @5))))))
1213 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1215 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1216 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1217 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1218 (bit_xor (convert @1) (convert @2))))
1220 /* Convert abs (abs (X)) into abs (X).
1221 also absu (absu (X)) into absu (X). */
1227 (absu (convert@2 (absu@1 @0)))
1228 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1231 /* Convert abs[u] (-X) -> abs[u] (X). */
1240 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1242 (abs tree_expr_nonnegative_p@0)
1246 (absu tree_expr_nonnegative_p@0)
1249 /* A few cases of fold-const.c negate_expr_p predicate. */
1250 (match negate_expr_p
1252 (if ((INTEGRAL_TYPE_P (type)
1253 && TYPE_UNSIGNED (type))
1254 || (!TYPE_OVERFLOW_SANITIZED (type)
1255 && may_negate_without_overflow_p (t)))))
1256 (match negate_expr_p
1258 (match negate_expr_p
1260 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1261 (match negate_expr_p
1263 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1264 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1266 (match negate_expr_p
1268 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1269 (match negate_expr_p
1271 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1272 || (FLOAT_TYPE_P (type)
1273 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1274 && !HONOR_SIGNED_ZEROS (type)))))
1276 /* (-A) * (-B) -> A * B */
1278 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1279 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1280 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1281 (mult (convert @0) (convert (negate @1)))))
1283 /* -(A + B) -> (-B) - A. */
1285 (negate (plus:c @0 negate_expr_p@1))
1286 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1287 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1288 (minus (negate @1) @0)))
1290 /* -(A - B) -> B - A. */
1292 (negate (minus @0 @1))
1293 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1294 || (FLOAT_TYPE_P (type)
1295 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1296 && !HONOR_SIGNED_ZEROS (type)))
1299 (negate (pointer_diff @0 @1))
1300 (if (TYPE_OVERFLOW_UNDEFINED (type))
1301 (pointer_diff @1 @0)))
1303 /* A - B -> A + (-B) if B is easily negatable. */
1305 (minus @0 negate_expr_p@1)
1306 (if (!FIXED_POINT_TYPE_P (type))
1307 (plus @0 (negate @1))))
1309 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1311 For bitwise binary operations apply operand conversions to the
1312 binary operation result instead of to the operands. This allows
1313 to combine successive conversions and bitwise binary operations.
1314 We combine the above two cases by using a conditional convert. */
1315 (for bitop (bit_and bit_ior bit_xor)
1317 (bitop (convert @0) (convert? @1))
1318 (if (((TREE_CODE (@1) == INTEGER_CST
1319 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1320 && int_fits_type_p (@1, TREE_TYPE (@0)))
1321 || types_match (@0, @1))
1322 /* ??? This transform conflicts with fold-const.c doing
1323 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1324 constants (if x has signed type, the sign bit cannot be set
1325 in c). This folds extension into the BIT_AND_EXPR.
1326 Restrict it to GIMPLE to avoid endless recursions. */
1327 && (bitop != BIT_AND_EXPR || GIMPLE)
1328 && (/* That's a good idea if the conversion widens the operand, thus
1329 after hoisting the conversion the operation will be narrower. */
1330 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1331 /* It's also a good idea if the conversion is to a non-integer
1333 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1334 /* Or if the precision of TO is not the same as the precision
1336 || !type_has_mode_precision_p (type)))
1337 (convert (bitop @0 (convert @1))))))
1339 (for bitop (bit_and bit_ior)
1340 rbitop (bit_ior bit_and)
1341 /* (x | y) & x -> x */
1342 /* (x & y) | x -> x */
1344 (bitop:c (rbitop:c @0 @1) @0)
1346 /* (~x | y) & x -> x & y */
1347 /* (~x & y) | x -> x | y */
1349 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1352 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1354 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1355 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1357 /* Combine successive equal operations with constants. */
1358 (for bitop (bit_and bit_ior bit_xor)
1360 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1361 (if (!CONSTANT_CLASS_P (@0))
1362 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1363 folded to a constant. */
1364 (bitop @0 (bitop @1 @2))
1365 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1366 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1367 the values involved are such that the operation can't be decided at
1368 compile time. Try folding one of @0 or @1 with @2 to see whether
1369 that combination can be decided at compile time.
1371 Keep the existing form if both folds fail, to avoid endless
1373 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1375 (bitop @1 { cst1; })
1376 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1378 (bitop @0 { cst2; }))))))))
1380 /* Try simple folding for X op !X, and X op X with the help
1381 of the truth_valued_p and logical_inverted_value predicates. */
1382 (match truth_valued_p
1384 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1385 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1386 (match truth_valued_p
1388 (match truth_valued_p
1391 (match (logical_inverted_value @0)
1393 (match (logical_inverted_value @0)
1394 (bit_not truth_valued_p@0))
1395 (match (logical_inverted_value @0)
1396 (eq @0 integer_zerop))
1397 (match (logical_inverted_value @0)
1398 (ne truth_valued_p@0 integer_truep))
1399 (match (logical_inverted_value @0)
1400 (bit_xor truth_valued_p@0 integer_truep))
1404 (bit_and:c @0 (logical_inverted_value @0))
1405 { build_zero_cst (type); })
1406 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1407 (for op (bit_ior bit_xor)
1409 (op:c truth_valued_p@0 (logical_inverted_value @0))
1410 { constant_boolean_node (true, type); }))
1411 /* X ==/!= !X is false/true. */
1414 (op:c truth_valued_p@0 (logical_inverted_value @0))
1415 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1419 (bit_not (bit_not @0))
1422 /* Convert ~ (-A) to A - 1. */
1424 (bit_not (convert? (negate @0)))
1425 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1426 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1427 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1429 /* Convert - (~A) to A + 1. */
1431 (negate (nop_convert (bit_not @0)))
1432 (plus (view_convert @0) { build_each_one_cst (type); }))
1434 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1436 (bit_not (convert? (minus @0 integer_each_onep)))
1437 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1438 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1439 (convert (negate @0))))
1441 (bit_not (convert? (plus @0 integer_all_onesp)))
1442 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1443 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1444 (convert (negate @0))))
1446 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1448 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1449 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1450 (convert (bit_xor @0 (bit_not @1)))))
1452 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1453 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1454 (convert (bit_xor @0 @1))))
1456 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1458 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1459 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1460 (bit_not (bit_xor (view_convert @0) @1))))
1462 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1464 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1465 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1467 /* Fold A - (A & B) into ~B & A. */
1469 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1470 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1471 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1472 (convert (bit_and (bit_not @1) @0))))
1474 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1475 (for cmp (gt lt ge le)
1477 (mult (convert (cmp @0 @1)) @2)
1478 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1480 /* For integral types with undefined overflow and C != 0 fold
1481 x * C EQ/NE y * C into x EQ/NE y. */
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 && tree_expr_nonzero_p (@1))
1490 /* For integral types with wrapping overflow and C odd fold
1491 x * C EQ/NE y * C into x EQ/NE y. */
1494 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1495 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1496 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1497 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1500 /* For integral types with undefined overflow and C != 0 fold
1501 x * C RELOP y * C into:
1503 x RELOP y for nonnegative C
1504 y RELOP x for negative C */
1505 (for cmp (lt gt le ge)
1507 (cmp (mult:c @0 @1) (mult:c @2 @1))
1508 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1509 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1510 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1512 (if (TREE_CODE (@1) == INTEGER_CST
1513 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1516 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1520 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1521 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1522 && TYPE_UNSIGNED (TREE_TYPE (@0))
1523 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1524 && (wi::to_wide (@2)
1525 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1526 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1527 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1529 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1530 (for cmp (simple_comparison)
1532 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1533 (if (element_precision (@3) >= element_precision (@0)
1534 && types_match (@0, @1))
1535 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1536 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1538 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1541 tree utype = unsigned_type_for (TREE_TYPE (@0));
1543 (cmp (convert:utype @1) (convert:utype @0)))))
1544 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1545 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1549 tree utype = unsigned_type_for (TREE_TYPE (@0));
1551 (cmp (convert:utype @0) (convert:utype @1)))))))))
1553 /* X / C1 op C2 into a simple range test. */
1554 (for cmp (simple_comparison)
1556 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1557 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1558 && integer_nonzerop (@1)
1559 && !TREE_OVERFLOW (@1)
1560 && !TREE_OVERFLOW (@2))
1561 (with { tree lo, hi; bool neg_overflow;
1562 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1565 (if (code == LT_EXPR || code == GE_EXPR)
1566 (if (TREE_OVERFLOW (lo))
1567 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1568 (if (code == LT_EXPR)
1571 (if (code == LE_EXPR || code == GT_EXPR)
1572 (if (TREE_OVERFLOW (hi))
1573 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1574 (if (code == LE_EXPR)
1578 { build_int_cst (type, code == NE_EXPR); })
1579 (if (code == EQ_EXPR && !hi)
1581 (if (code == EQ_EXPR && !lo)
1583 (if (code == NE_EXPR && !hi)
1585 (if (code == NE_EXPR && !lo)
1588 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1592 tree etype = range_check_type (TREE_TYPE (@0));
1595 hi = fold_convert (etype, hi);
1596 lo = fold_convert (etype, lo);
1597 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1600 (if (etype && hi && !TREE_OVERFLOW (hi))
1601 (if (code == EQ_EXPR)
1602 (le (minus (convert:etype @0) { lo; }) { hi; })
1603 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1605 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1606 (for op (lt le ge gt)
1608 (op (plus:c @0 @2) (plus:c @1 @2))
1609 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1610 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1612 /* For equality and subtraction, this is also true with wrapping overflow. */
1613 (for op (eq ne minus)
1615 (op (plus:c @0 @2) (plus:c @1 @2))
1616 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1617 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1618 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1621 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1622 (for op (lt le ge gt)
1624 (op (minus @0 @2) (minus @1 @2))
1625 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1626 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1628 /* For equality and subtraction, this is also true with wrapping overflow. */
1629 (for op (eq ne minus)
1631 (op (minus @0 @2) (minus @1 @2))
1632 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1633 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1634 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1636 /* And for pointers... */
1637 (for op (simple_comparison)
1639 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1640 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1643 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1644 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1645 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1646 (pointer_diff @0 @1)))
1648 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1649 (for op (lt le ge gt)
1651 (op (minus @2 @0) (minus @2 @1))
1652 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1653 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1655 /* For equality and subtraction, this is also true with wrapping overflow. */
1656 (for op (eq ne minus)
1658 (op (minus @2 @0) (minus @2 @1))
1659 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1660 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1661 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1663 /* And for pointers... */
1664 (for op (simple_comparison)
1666 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1667 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1670 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1671 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1672 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1673 (pointer_diff @1 @0)))
1675 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1676 (for op (lt le gt ge)
1678 (op:c (plus:c@2 @0 @1) @1)
1679 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1680 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1681 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1682 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1683 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1684 /* For equality, this is also true with wrapping overflow. */
1687 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1688 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1689 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1690 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1691 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1692 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1693 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1694 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1696 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1697 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1698 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1699 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1700 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1702 /* X - Y < X is the same as Y > 0 when there is no overflow.
1703 For equality, this is also true with wrapping overflow. */
1704 (for op (simple_comparison)
1706 (op:c @0 (minus@2 @0 @1))
1707 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1708 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1709 || ((op == EQ_EXPR || op == NE_EXPR)
1710 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1711 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1712 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1715 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1716 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1720 (cmp (trunc_div @0 @1) integer_zerop)
1721 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1722 /* Complex ==/!= is allowed, but not </>=. */
1723 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1724 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1727 /* X == C - X can never be true if C is odd. */
1730 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1731 (if (TREE_INT_CST_LOW (@1) & 1)
1732 { constant_boolean_node (cmp == NE_EXPR, type); })))
1734 /* Arguments on which one can call get_nonzero_bits to get the bits
1736 (match with_possible_nonzero_bits
1738 (match with_possible_nonzero_bits
1740 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1741 /* Slightly extended version, do not make it recursive to keep it cheap. */
1742 (match (with_possible_nonzero_bits2 @0)
1743 with_possible_nonzero_bits@0)
1744 (match (with_possible_nonzero_bits2 @0)
1745 (bit_and:c with_possible_nonzero_bits@0 @2))
1747 /* Same for bits that are known to be set, but we do not have
1748 an equivalent to get_nonzero_bits yet. */
1749 (match (with_certain_nonzero_bits2 @0)
1751 (match (with_certain_nonzero_bits2 @0)
1752 (bit_ior @1 INTEGER_CST@0))
1754 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1757 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1758 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1759 { constant_boolean_node (cmp == NE_EXPR, type); })))
1761 /* ((X inner_op C0) outer_op C1)
1762 With X being a tree where value_range has reasoned certain bits to always be
1763 zero throughout its computed value range,
1764 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1765 where zero_mask has 1's for all bits that are sure to be 0 in
1767 if (inner_op == '^') C0 &= ~C1;
1768 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1769 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1771 (for inner_op (bit_ior bit_xor)
1772 outer_op (bit_xor bit_ior)
1775 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1779 wide_int zero_mask_not;
1783 if (TREE_CODE (@2) == SSA_NAME)
1784 zero_mask_not = get_nonzero_bits (@2);
1788 if (inner_op == BIT_XOR_EXPR)
1790 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1791 cst_emit = C0 | wi::to_wide (@1);
1795 C0 = wi::to_wide (@0);
1796 cst_emit = C0 ^ wi::to_wide (@1);
1799 (if (!fail && (C0 & zero_mask_not) == 0)
1800 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1801 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1802 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1804 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1806 (pointer_plus (pointer_plus:s @0 @1) @3)
1807 (pointer_plus @0 (plus @1 @3)))
1813 tem4 = (unsigned long) tem3;
1818 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1819 /* Conditionally look through a sign-changing conversion. */
1820 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1821 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1822 || (GENERIC && type == TREE_TYPE (@1))))
1825 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1826 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1830 tem = (sizetype) ptr;
1834 and produce the simpler and easier to analyze with respect to alignment
1835 ... = ptr & ~algn; */
1837 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1838 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1839 (bit_and @0 { algn; })))
1841 /* Try folding difference of addresses. */
1843 (minus (convert ADDR_EXPR@0) (convert @1))
1844 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1845 (with { poly_int64 diff; }
1846 (if (ptr_difference_const (@0, @1, &diff))
1847 { build_int_cst_type (type, diff); }))))
1849 (minus (convert @0) (convert ADDR_EXPR@1))
1850 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1851 (with { poly_int64 diff; }
1852 (if (ptr_difference_const (@0, @1, &diff))
1853 { build_int_cst_type (type, diff); }))))
1855 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1856 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1857 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1858 (with { poly_int64 diff; }
1859 (if (ptr_difference_const (@0, @1, &diff))
1860 { build_int_cst_type (type, diff); }))))
1862 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1863 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1864 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1865 (with { poly_int64 diff; }
1866 (if (ptr_difference_const (@0, @1, &diff))
1867 { build_int_cst_type (type, diff); }))))
1869 /* If arg0 is derived from the address of an object or function, we may
1870 be able to fold this expression using the object or function's
1873 (bit_and (convert? @0) INTEGER_CST@1)
1874 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1875 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1879 unsigned HOST_WIDE_INT bitpos;
1880 get_pointer_alignment_1 (@0, &align, &bitpos);
1882 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1883 { wide_int_to_tree (type, (wi::to_wide (@1)
1884 & (bitpos / BITS_PER_UNIT))); }))))
1888 (if (INTEGRAL_TYPE_P (type)
1889 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
1893 (if (INTEGRAL_TYPE_P (type)
1894 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
1896 /* x > y && x != XXX_MIN --> x > y
1897 x > y && x == XXX_MIN --> false . */
1900 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
1902 (if (eqne == EQ_EXPR)
1903 { constant_boolean_node (false, type); })
1904 (if (eqne == NE_EXPR)
1908 /* x < y && x != XXX_MAX --> x < y
1909 x < y && x == XXX_MAX --> false. */
1912 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
1914 (if (eqne == EQ_EXPR)
1915 { constant_boolean_node (false, type); })
1916 (if (eqne == NE_EXPR)
1920 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
1922 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
1925 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
1927 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
1930 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
1932 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
1935 /* x <= y || x != XXX_MIN --> true. */
1937 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
1938 { constant_boolean_node (true, type); })
1940 /* x <= y || x == XXX_MIN --> x <= y. */
1942 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
1945 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
1947 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
1950 /* x >= y || x != XXX_MAX --> true
1951 x >= y || x == XXX_MAX --> x >= y. */
1954 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
1956 (if (eqne == EQ_EXPR)
1958 (if (eqne == NE_EXPR)
1959 { constant_boolean_node (true, type); }))))
1961 /* Convert (X == CST1) && (X OP2 CST2) to a known value
1962 based on CST1 OP2 CST2. Similarly for (X != CST1). */
1965 (for code2 (eq ne lt gt le ge)
1967 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
1970 int cmp = tree_int_cst_compare (@1, @2);
1974 case EQ_EXPR: val = (cmp == 0); break;
1975 case NE_EXPR: val = (cmp != 0); break;
1976 case LT_EXPR: val = (cmp < 0); break;
1977 case GT_EXPR: val = (cmp > 0); break;
1978 case LE_EXPR: val = (cmp <= 0); break;
1979 case GE_EXPR: val = (cmp >= 0); break;
1980 default: gcc_unreachable ();
1984 (if (code1 == EQ_EXPR && val) @3)
1985 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
1986 (if (code1 == NE_EXPR && !val) @4))))))
1988 /* Convert (X OP1 CST1) && (X OP2 CST2). */
1990 (for code1 (lt le gt ge)
1991 (for code2 (lt le gt ge)
1993 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
1996 int cmp = tree_int_cst_compare (@1, @2);
1999 /* Choose the more restrictive of two < or <= comparisons. */
2000 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2001 && (code2 == LT_EXPR || code2 == LE_EXPR))
2002 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2005 /* Likewise chose the more restrictive of two > or >= comparisons. */
2006 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2007 && (code2 == GT_EXPR || code2 == GE_EXPR))
2008 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2011 /* Check for singleton ranges. */
2013 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2014 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2016 /* Check for disjoint ranges. */
2018 && (code1 == LT_EXPR || code1 == LE_EXPR)
2019 && (code2 == GT_EXPR || code2 == GE_EXPR))
2020 { constant_boolean_node (false, type); })
2022 && (code1 == GT_EXPR || code1 == GE_EXPR)
2023 && (code2 == LT_EXPR || code2 == LE_EXPR))
2024 { constant_boolean_node (false, type); })
2027 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2028 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2031 (for code2 (eq ne lt gt le ge)
2033 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2036 int cmp = tree_int_cst_compare (@1, @2);
2040 case EQ_EXPR: val = (cmp == 0); break;
2041 case NE_EXPR: val = (cmp != 0); break;
2042 case LT_EXPR: val = (cmp < 0); break;
2043 case GT_EXPR: val = (cmp > 0); break;
2044 case LE_EXPR: val = (cmp <= 0); break;
2045 case GE_EXPR: val = (cmp >= 0); break;
2046 default: gcc_unreachable ();
2050 (if (code1 == EQ_EXPR && val) @4)
2051 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2052 (if (code1 == NE_EXPR && !val) @3))))))
2054 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2056 (for code1 (lt le gt ge)
2057 (for code2 (lt le gt ge)
2059 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2062 int cmp = tree_int_cst_compare (@1, @2);
2065 /* Choose the more restrictive of two < or <= comparisons. */
2066 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2067 && (code2 == LT_EXPR || code2 == LE_EXPR))
2068 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2071 /* Likewise chose the more restrictive of two > or >= comparisons. */
2072 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2073 && (code2 == GT_EXPR || code2 == GE_EXPR))
2074 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2077 /* Check for singleton ranges. */
2079 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2080 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2082 /* Check for disjoint ranges. */
2084 && (code1 == LT_EXPR || code1 == LE_EXPR)
2085 && (code2 == GT_EXPR || code2 == GE_EXPR))
2086 { constant_boolean_node (true, type); })
2088 && (code1 == GT_EXPR || code1 == GE_EXPR)
2089 && (code2 == LT_EXPR || code2 == LE_EXPR))
2090 { constant_boolean_node (true, type); })
2093 /* We can't reassociate at all for saturating types. */
2094 (if (!TYPE_SATURATING (type))
2096 /* Contract negates. */
2097 /* A + (-B) -> A - B */
2099 (plus:c @0 (convert? (negate @1)))
2100 /* Apply STRIP_NOPS on the negate. */
2101 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2102 && !TYPE_OVERFLOW_SANITIZED (type))
2106 if (INTEGRAL_TYPE_P (type)
2107 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2108 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2110 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2111 /* A - (-B) -> A + B */
2113 (minus @0 (convert? (negate @1)))
2114 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2115 && !TYPE_OVERFLOW_SANITIZED (type))
2119 if (INTEGRAL_TYPE_P (type)
2120 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2121 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2123 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2125 Sign-extension is ok except for INT_MIN, which thankfully cannot
2126 happen without overflow. */
2128 (negate (convert (negate @1)))
2129 (if (INTEGRAL_TYPE_P (type)
2130 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2131 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2132 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2133 && !TYPE_OVERFLOW_SANITIZED (type)
2134 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2137 (negate (convert negate_expr_p@1))
2138 (if (SCALAR_FLOAT_TYPE_P (type)
2139 && ((DECIMAL_FLOAT_TYPE_P (type)
2140 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2141 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2142 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2143 (convert (negate @1))))
2145 (negate (nop_convert (negate @1)))
2146 (if (!TYPE_OVERFLOW_SANITIZED (type)
2147 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2150 /* We can't reassociate floating-point unless -fassociative-math
2151 or fixed-point plus or minus because of saturation to +-Inf. */
2152 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2153 && !FIXED_POINT_TYPE_P (type))
2155 /* Match patterns that allow contracting a plus-minus pair
2156 irrespective of overflow issues. */
2157 /* (A +- B) - A -> +- B */
2158 /* (A +- B) -+ B -> A */
2159 /* A - (A +- B) -> -+ B */
2160 /* A +- (B -+ A) -> +- B */
2162 (minus (nop_convert (plus:c (nop_convert @0) @1)) @0)
2165 (minus (nop_convert (minus (nop_convert @0) @1)) @0)
2166 (if (!ANY_INTEGRAL_TYPE_P (type)
2167 || TYPE_OVERFLOW_WRAPS (type))
2168 (negate (view_convert @1))
2169 (view_convert (negate @1))))
2171 (plus:c (nop_convert (minus @0 (nop_convert @1))) @1)
2174 (minus @0 (nop_convert (plus:c (nop_convert @0) @1)))
2175 (if (!ANY_INTEGRAL_TYPE_P (type)
2176 || TYPE_OVERFLOW_WRAPS (type))
2177 (negate (view_convert @1))
2178 (view_convert (negate @1))))
2180 (minus @0 (nop_convert (minus (nop_convert @0) @1)))
2182 /* (A +- B) + (C - A) -> C +- B */
2183 /* (A + B) - (A - C) -> B + C */
2184 /* More cases are handled with comparisons. */
2186 (plus:c (plus:c @0 @1) (minus @2 @0))
2189 (plus:c (minus @0 @1) (minus @2 @0))
2192 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2193 (if (TYPE_OVERFLOW_UNDEFINED (type)
2194 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2195 (pointer_diff @2 @1)))
2197 (minus (plus:c @0 @1) (minus @0 @2))
2200 /* (A +- CST1) +- CST2 -> A + CST3
2201 Use view_convert because it is safe for vectors and equivalent for
2203 (for outer_op (plus minus)
2204 (for inner_op (plus minus)
2205 neg_inner_op (minus plus)
2207 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
2209 /* If one of the types wraps, use that one. */
2210 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2211 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2212 forever if something doesn't simplify into a constant. */
2213 (if (!CONSTANT_CLASS_P (@0))
2214 (if (outer_op == PLUS_EXPR)
2215 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2216 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2217 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2218 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2219 (if (outer_op == PLUS_EXPR)
2220 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2221 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2222 /* If the constant operation overflows we cannot do the transform
2223 directly as we would introduce undefined overflow, for example
2224 with (a - 1) + INT_MIN. */
2225 (if (types_match (type, @0))
2226 (with { tree cst = const_binop (outer_op == inner_op
2227 ? PLUS_EXPR : MINUS_EXPR,
2229 (if (cst && !TREE_OVERFLOW (cst))
2230 (inner_op @0 { cst; } )
2231 /* X+INT_MAX+1 is X-INT_MIN. */
2232 (if (INTEGRAL_TYPE_P (type) && cst
2233 && wi::to_wide (cst) == wi::min_value (type))
2234 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2235 /* Last resort, use some unsigned type. */
2236 (with { tree utype = unsigned_type_for (type); }
2238 (view_convert (inner_op
2239 (view_convert:utype @0)
2241 { drop_tree_overflow (cst); }))))))))))))))
2243 /* (CST1 - A) +- CST2 -> CST3 - A */
2244 (for outer_op (plus minus)
2246 (outer_op (nop_convert (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2247 /* If one of the types wraps, use that one. */
2248 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2249 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2250 forever if something doesn't simplify into a constant. */
2251 (if (!CONSTANT_CLASS_P (@0))
2252 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2253 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2254 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2255 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2256 (if (types_match (type, @0))
2257 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2258 (if (cst && !TREE_OVERFLOW (cst))
2259 (minus { cst; } @0))))))))
2261 /* CST1 - (CST2 - A) -> CST3 + A
2262 Use view_convert because it is safe for vectors and equivalent for
2265 (minus CONSTANT_CLASS_P@1 (nop_convert (minus CONSTANT_CLASS_P@2 @0)))
2266 /* If one of the types wraps, use that one. */
2267 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2268 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2269 forever if something doesn't simplify into a constant. */
2270 (if (!CONSTANT_CLASS_P (@0))
2271 (plus (view_convert @0) (minus @1 (view_convert @2))))
2272 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2273 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2274 (view_convert (plus @0 (minus (view_convert @1) @2)))
2275 (if (types_match (type, @0))
2276 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2277 (if (cst && !TREE_OVERFLOW (cst))
2278 (plus { cst; } @0)))))))
2280 /* ((T)(A)) + CST -> (T)(A + CST) */
2283 (plus (convert SSA_NAME@0) INTEGER_CST@1)
2284 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2285 && TREE_CODE (type) == INTEGER_TYPE
2286 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2287 && int_fits_type_p (@1, TREE_TYPE (@0)))
2288 /* Perform binary operation inside the cast if the constant fits
2289 and (A + CST)'s range does not overflow. */
2292 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2293 max_ovf = wi::OVF_OVERFLOW;
2294 tree inner_type = TREE_TYPE (@0);
2297 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2298 TYPE_SIGN (inner_type));
2300 wide_int wmin0, wmax0;
2301 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2303 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2304 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2307 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2308 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2312 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2314 (for op (plus minus)
2316 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2317 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2318 && TREE_CODE (type) == INTEGER_TYPE
2319 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2320 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2321 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2322 && TYPE_OVERFLOW_WRAPS (type))
2323 (plus (convert @0) (op @2 (convert @1))))))
2328 (plus:c (bit_not @0) @0)
2329 (if (!TYPE_OVERFLOW_TRAPS (type))
2330 { build_all_ones_cst (type); }))
2334 (plus (convert? (bit_not @0)) integer_each_onep)
2335 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2336 (negate (convert @0))))
2340 (minus (convert? (negate @0)) integer_each_onep)
2341 (if (!TYPE_OVERFLOW_TRAPS (type)
2342 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2343 (bit_not (convert @0))))
2347 (minus integer_all_onesp @0)
2350 /* (T)(P + A) - (T)P -> (T) A */
2352 (minus (convert (plus:c @@0 @1))
2354 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2355 /* For integer types, if A has a smaller type
2356 than T the result depends on the possible
2358 E.g. T=size_t, A=(unsigned)429497295, P>0.
2359 However, if an overflow in P + A would cause
2360 undefined behavior, we can assume that there
2362 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2363 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2366 (minus (convert (pointer_plus @@0 @1))
2368 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2369 /* For pointer types, if the conversion of A to the
2370 final type requires a sign- or zero-extension,
2371 then we have to punt - it is not defined which
2373 || (POINTER_TYPE_P (TREE_TYPE (@0))
2374 && TREE_CODE (@1) == INTEGER_CST
2375 && tree_int_cst_sign_bit (@1) == 0))
2378 (pointer_diff (pointer_plus @@0 @1) @0)
2379 /* The second argument of pointer_plus must be interpreted as signed, and
2380 thus sign-extended if necessary. */
2381 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2382 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2383 second arg is unsigned even when we need to consider it as signed,
2384 we don't want to diagnose overflow here. */
2385 (convert (view_convert:stype @1))))
2387 /* (T)P - (T)(P + A) -> -(T) A */
2389 (minus (convert? @0)
2390 (convert (plus:c @@0 @1)))
2391 (if (INTEGRAL_TYPE_P (type)
2392 && TYPE_OVERFLOW_UNDEFINED (type)
2393 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2394 (with { tree utype = unsigned_type_for (type); }
2395 (convert (negate (convert:utype @1))))
2396 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2397 /* For integer types, if A has a smaller type
2398 than T the result depends on the possible
2400 E.g. T=size_t, A=(unsigned)429497295, P>0.
2401 However, if an overflow in P + A would cause
2402 undefined behavior, we can assume that there
2404 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2405 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2406 (negate (convert @1)))))
2409 (convert (pointer_plus @@0 @1)))
2410 (if (INTEGRAL_TYPE_P (type)
2411 && TYPE_OVERFLOW_UNDEFINED (type)
2412 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2413 (with { tree utype = unsigned_type_for (type); }
2414 (convert (negate (convert:utype @1))))
2415 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2416 /* For pointer types, if the conversion of A to the
2417 final type requires a sign- or zero-extension,
2418 then we have to punt - it is not defined which
2420 || (POINTER_TYPE_P (TREE_TYPE (@0))
2421 && TREE_CODE (@1) == INTEGER_CST
2422 && tree_int_cst_sign_bit (@1) == 0))
2423 (negate (convert @1)))))
2425 (pointer_diff @0 (pointer_plus @@0 @1))
2426 /* The second argument of pointer_plus must be interpreted as signed, and
2427 thus sign-extended if necessary. */
2428 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2429 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2430 second arg is unsigned even when we need to consider it as signed,
2431 we don't want to diagnose overflow here. */
2432 (negate (convert (view_convert:stype @1)))))
2434 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2436 (minus (convert (plus:c @@0 @1))
2437 (convert (plus:c @0 @2)))
2438 (if (INTEGRAL_TYPE_P (type)
2439 && TYPE_OVERFLOW_UNDEFINED (type)
2440 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2441 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2442 (with { tree utype = unsigned_type_for (type); }
2443 (convert (minus (convert:utype @1) (convert:utype @2))))
2444 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2445 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2446 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2447 /* For integer types, if A has a smaller type
2448 than T the result depends on the possible
2450 E.g. T=size_t, A=(unsigned)429497295, P>0.
2451 However, if an overflow in P + A would cause
2452 undefined behavior, we can assume that there
2454 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2455 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2456 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2457 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2458 (minus (convert @1) (convert @2)))))
2460 (minus (convert (pointer_plus @@0 @1))
2461 (convert (pointer_plus @0 @2)))
2462 (if (INTEGRAL_TYPE_P (type)
2463 && TYPE_OVERFLOW_UNDEFINED (type)
2464 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2465 (with { tree utype = unsigned_type_for (type); }
2466 (convert (minus (convert:utype @1) (convert:utype @2))))
2467 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2468 /* For pointer types, if the conversion of A to the
2469 final type requires a sign- or zero-extension,
2470 then we have to punt - it is not defined which
2472 || (POINTER_TYPE_P (TREE_TYPE (@0))
2473 && TREE_CODE (@1) == INTEGER_CST
2474 && tree_int_cst_sign_bit (@1) == 0
2475 && TREE_CODE (@2) == INTEGER_CST
2476 && tree_int_cst_sign_bit (@2) == 0))
2477 (minus (convert @1) (convert @2)))))
2479 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2480 /* The second argument of pointer_plus must be interpreted as signed, and
2481 thus sign-extended if necessary. */
2482 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2483 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2484 second arg is unsigned even when we need to consider it as signed,
2485 we don't want to diagnose overflow here. */
2486 (minus (convert (view_convert:stype @1))
2487 (convert (view_convert:stype @2)))))))
2489 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2490 Modeled after fold_plusminus_mult_expr. */
2491 (if (!TYPE_SATURATING (type)
2492 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2493 (for plusminus (plus minus)
2495 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2496 (if ((!ANY_INTEGRAL_TYPE_P (type)
2497 || TYPE_OVERFLOW_WRAPS (type)
2498 || (INTEGRAL_TYPE_P (type)
2499 && tree_expr_nonzero_p (@0)
2500 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2501 /* If @1 +- @2 is constant require a hard single-use on either
2502 original operand (but not on both). */
2503 && (single_use (@3) || single_use (@4)))
2504 (mult (plusminus @1 @2) @0)))
2505 /* We cannot generate constant 1 for fract. */
2506 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2508 (plusminus @0 (mult:c@3 @0 @2))
2509 (if ((!ANY_INTEGRAL_TYPE_P (type)
2510 || TYPE_OVERFLOW_WRAPS (type)
2511 /* For @0 + @0*@2 this transformation would introduce UB
2512 (where there was none before) for @0 in [-1,0] and @2 max.
2513 For @0 - @0*@2 this transformation would introduce UB
2514 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2515 || (INTEGRAL_TYPE_P (type)
2516 && ((tree_expr_nonzero_p (@0)
2517 && expr_not_equal_to (@0,
2518 wi::minus_one (TYPE_PRECISION (type))))
2519 || (plusminus == PLUS_EXPR
2520 ? expr_not_equal_to (@2,
2521 wi::max_value (TYPE_PRECISION (type), SIGNED))
2522 /* Let's ignore the @0 -1 and @2 min case. */
2523 : (expr_not_equal_to (@2,
2524 wi::min_value (TYPE_PRECISION (type), SIGNED))
2525 && expr_not_equal_to (@2,
2526 wi::min_value (TYPE_PRECISION (type), SIGNED)
2529 (mult (plusminus { build_one_cst (type); } @2) @0)))
2531 (plusminus (mult:c@3 @0 @2) @0)
2532 (if ((!ANY_INTEGRAL_TYPE_P (type)
2533 || TYPE_OVERFLOW_WRAPS (type)
2534 /* For @0*@2 + @0 this transformation would introduce UB
2535 (where there was none before) for @0 in [-1,0] and @2 max.
2536 For @0*@2 - @0 this transformation would introduce UB
2537 for @0 0 and @2 min. */
2538 || (INTEGRAL_TYPE_P (type)
2539 && ((tree_expr_nonzero_p (@0)
2540 && (plusminus == MINUS_EXPR
2541 || expr_not_equal_to (@0,
2542 wi::minus_one (TYPE_PRECISION (type)))))
2543 || expr_not_equal_to (@2,
2544 (plusminus == PLUS_EXPR
2545 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2546 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2548 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2550 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2552 (for minmax (min max FMIN_ALL FMAX_ALL)
2556 /* min(max(x,y),y) -> y. */
2558 (min:c (max:c @0 @1) @1)
2560 /* max(min(x,y),y) -> y. */
2562 (max:c (min:c @0 @1) @1)
2564 /* max(a,-a) -> abs(a). */
2566 (max:c @0 (negate @0))
2567 (if (TREE_CODE (type) != COMPLEX_TYPE
2568 && (! ANY_INTEGRAL_TYPE_P (type)
2569 || TYPE_OVERFLOW_UNDEFINED (type)))
2571 /* min(a,-a) -> -abs(a). */
2573 (min:c @0 (negate @0))
2574 (if (TREE_CODE (type) != COMPLEX_TYPE
2575 && (! ANY_INTEGRAL_TYPE_P (type)
2576 || TYPE_OVERFLOW_UNDEFINED (type)))
2581 (if (INTEGRAL_TYPE_P (type)
2582 && TYPE_MIN_VALUE (type)
2583 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2585 (if (INTEGRAL_TYPE_P (type)
2586 && TYPE_MAX_VALUE (type)
2587 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2592 (if (INTEGRAL_TYPE_P (type)
2593 && TYPE_MAX_VALUE (type)
2594 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2596 (if (INTEGRAL_TYPE_P (type)
2597 && TYPE_MIN_VALUE (type)
2598 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2601 /* max (a, a + CST) -> a + CST where CST is positive. */
2602 /* max (a, a + CST) -> a where CST is negative. */
2604 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2605 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2606 (if (tree_int_cst_sgn (@1) > 0)
2610 /* min (a, a + CST) -> a where CST is positive. */
2611 /* min (a, a + CST) -> a + CST where CST is negative. */
2613 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2614 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2615 (if (tree_int_cst_sgn (@1) > 0)
2619 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2620 and the outer convert demotes the expression back to x's type. */
2621 (for minmax (min max)
2623 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2624 (if (INTEGRAL_TYPE_P (type)
2625 && types_match (@1, type) && int_fits_type_p (@2, type)
2626 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2627 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2628 (minmax @1 (convert @2)))))
2630 (for minmax (FMIN_ALL FMAX_ALL)
2631 /* If either argument is NaN, return the other one. Avoid the
2632 transformation if we get (and honor) a signalling NaN. */
2634 (minmax:c @0 REAL_CST@1)
2635 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2636 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2638 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2639 functions to return the numeric arg if the other one is NaN.
2640 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2641 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2642 worry about it either. */
2643 (if (flag_finite_math_only)
2650 /* min (-A, -B) -> -max (A, B) */
2651 (for minmax (min max FMIN_ALL FMAX_ALL)
2652 maxmin (max min FMAX_ALL FMIN_ALL)
2654 (minmax (negate:s@2 @0) (negate:s@3 @1))
2655 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2656 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2657 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2658 (negate (maxmin @0 @1)))))
2659 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2660 MAX (~X, ~Y) -> ~MIN (X, Y) */
2661 (for minmax (min max)
2664 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2665 (bit_not (maxmin @0 @1))))
2667 /* MIN (X, Y) == X -> X <= Y */
2668 (for minmax (min min max max)
2672 (cmp:c (minmax:c @0 @1) @0)
2673 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2675 /* MIN (X, 5) == 0 -> X == 0
2676 MIN (X, 5) == 7 -> false */
2679 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2680 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2681 TYPE_SIGN (TREE_TYPE (@0))))
2682 { constant_boolean_node (cmp == NE_EXPR, type); }
2683 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2684 TYPE_SIGN (TREE_TYPE (@0))))
2688 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2689 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2690 TYPE_SIGN (TREE_TYPE (@0))))
2691 { constant_boolean_node (cmp == NE_EXPR, type); }
2692 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2693 TYPE_SIGN (TREE_TYPE (@0))))
2695 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2696 (for minmax (min min max max min min max max )
2697 cmp (lt le gt ge gt ge lt le )
2698 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2700 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2701 (comb (cmp @0 @2) (cmp @1 @2))))
2703 /* Simplifications of shift and rotates. */
2705 (for rotate (lrotate rrotate)
2707 (rotate integer_all_onesp@0 @1)
2710 /* Optimize -1 >> x for arithmetic right shifts. */
2712 (rshift integer_all_onesp@0 @1)
2713 (if (!TYPE_UNSIGNED (type)
2714 && tree_expr_nonnegative_p (@1))
2717 /* Optimize (x >> c) << c into x & (-1<<c). */
2719 (lshift (rshift @0 INTEGER_CST@1) @1)
2720 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2721 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2723 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2726 (rshift (lshift @0 INTEGER_CST@1) @1)
2727 (if (TYPE_UNSIGNED (type)
2728 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2729 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2731 (for shiftrotate (lrotate rrotate lshift rshift)
2733 (shiftrotate @0 integer_zerop)
2736 (shiftrotate integer_zerop@0 @1)
2738 /* Prefer vector1 << scalar to vector1 << vector2
2739 if vector2 is uniform. */
2740 (for vec (VECTOR_CST CONSTRUCTOR)
2742 (shiftrotate @0 vec@1)
2743 (with { tree tem = uniform_vector_p (@1); }
2745 (shiftrotate @0 { tem; }))))))
2747 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2748 Y is 0. Similarly for X >> Y. */
2750 (for shift (lshift rshift)
2752 (shift @0 SSA_NAME@1)
2753 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2755 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2756 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2758 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2762 /* Rewrite an LROTATE_EXPR by a constant into an
2763 RROTATE_EXPR by a new constant. */
2765 (lrotate @0 INTEGER_CST@1)
2766 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2767 build_int_cst (TREE_TYPE (@1),
2768 element_precision (type)), @1); }))
2770 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2771 (for op (lrotate rrotate rshift lshift)
2773 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2774 (with { unsigned int prec = element_precision (type); }
2775 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2776 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2777 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2778 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2779 (with { unsigned int low = (tree_to_uhwi (@1)
2780 + tree_to_uhwi (@2)); }
2781 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2782 being well defined. */
2784 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2785 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2786 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2787 { build_zero_cst (type); }
2788 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2789 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2792 /* ((1 << A) & 1) != 0 -> A == 0
2793 ((1 << A) & 1) == 0 -> A != 0 */
2797 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2798 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2800 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2801 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2805 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2806 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2808 || (!integer_zerop (@2)
2809 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2810 { constant_boolean_node (cmp == NE_EXPR, type); }
2811 (if (!integer_zerop (@2)
2812 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2813 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2815 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2816 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2817 if the new mask might be further optimized. */
2818 (for shift (lshift rshift)
2820 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2822 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2823 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2824 && tree_fits_uhwi_p (@1)
2825 && tree_to_uhwi (@1) > 0
2826 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2829 unsigned int shiftc = tree_to_uhwi (@1);
2830 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2831 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2832 tree shift_type = TREE_TYPE (@3);
2835 if (shift == LSHIFT_EXPR)
2836 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2837 else if (shift == RSHIFT_EXPR
2838 && type_has_mode_precision_p (shift_type))
2840 prec = TYPE_PRECISION (TREE_TYPE (@3));
2842 /* See if more bits can be proven as zero because of
2845 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2847 tree inner_type = TREE_TYPE (@0);
2848 if (type_has_mode_precision_p (inner_type)
2849 && TYPE_PRECISION (inner_type) < prec)
2851 prec = TYPE_PRECISION (inner_type);
2852 /* See if we can shorten the right shift. */
2854 shift_type = inner_type;
2855 /* Otherwise X >> C1 is all zeros, so we'll optimize
2856 it into (X, 0) later on by making sure zerobits
2860 zerobits = HOST_WIDE_INT_M1U;
2863 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2864 zerobits <<= prec - shiftc;
2866 /* For arithmetic shift if sign bit could be set, zerobits
2867 can contain actually sign bits, so no transformation is
2868 possible, unless MASK masks them all away. In that
2869 case the shift needs to be converted into logical shift. */
2870 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2871 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2873 if ((mask & zerobits) == 0)
2874 shift_type = unsigned_type_for (TREE_TYPE (@3));
2880 /* ((X << 16) & 0xff00) is (X, 0). */
2881 (if ((mask & zerobits) == mask)
2882 { build_int_cst (type, 0); }
2883 (with { newmask = mask | zerobits; }
2884 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2887 /* Only do the transformation if NEWMASK is some integer
2889 for (prec = BITS_PER_UNIT;
2890 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2891 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2894 (if (prec < HOST_BITS_PER_WIDE_INT
2895 || newmask == HOST_WIDE_INT_M1U)
2897 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2898 (if (!tree_int_cst_equal (newmaskt, @2))
2899 (if (shift_type != TREE_TYPE (@3))
2900 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2901 (bit_and @4 { newmaskt; })))))))))))))
2903 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2904 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2905 (for shift (lshift rshift)
2906 (for bit_op (bit_and bit_xor bit_ior)
2908 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2909 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2910 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2911 (bit_op (shift (convert @0) @1) { mask; }))))))
2913 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2915 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2916 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2917 && (element_precision (TREE_TYPE (@0))
2918 <= element_precision (TREE_TYPE (@1))
2919 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2921 { tree shift_type = TREE_TYPE (@0); }
2922 (convert (rshift (convert:shift_type @1) @2)))))
2924 /* ~(~X >>r Y) -> X >>r Y
2925 ~(~X <<r Y) -> X <<r Y */
2926 (for rotate (lrotate rrotate)
2928 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2929 (if ((element_precision (TREE_TYPE (@0))
2930 <= element_precision (TREE_TYPE (@1))
2931 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2932 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2933 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2935 { tree rotate_type = TREE_TYPE (@0); }
2936 (convert (rotate (convert:rotate_type @1) @2))))))
2938 /* Simplifications of conversions. */
2940 /* Basic strip-useless-type-conversions / strip_nops. */
2941 (for cvt (convert view_convert float fix_trunc)
2944 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2945 || (GENERIC && type == TREE_TYPE (@0)))
2948 /* Contract view-conversions. */
2950 (view_convert (view_convert @0))
2953 /* For integral conversions with the same precision or pointer
2954 conversions use a NOP_EXPR instead. */
2957 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2958 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2959 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2962 /* Strip inner integral conversions that do not change precision or size, or
2963 zero-extend while keeping the same size (for bool-to-char). */
2965 (view_convert (convert@0 @1))
2966 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2967 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2968 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2969 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2970 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2971 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2974 /* Simplify a view-converted empty constructor. */
2976 (view_convert CONSTRUCTOR@0)
2977 (if (TREE_CODE (@0) != SSA_NAME
2978 && CONSTRUCTOR_NELTS (@0) == 0)
2979 { build_zero_cst (type); }))
2981 /* Re-association barriers around constants and other re-association
2982 barriers can be removed. */
2984 (paren CONSTANT_CLASS_P@0)
2987 (paren (paren@1 @0))
2990 /* Handle cases of two conversions in a row. */
2991 (for ocvt (convert float fix_trunc)
2992 (for icvt (convert float)
2997 tree inside_type = TREE_TYPE (@0);
2998 tree inter_type = TREE_TYPE (@1);
2999 int inside_int = INTEGRAL_TYPE_P (inside_type);
3000 int inside_ptr = POINTER_TYPE_P (inside_type);
3001 int inside_float = FLOAT_TYPE_P (inside_type);
3002 int inside_vec = VECTOR_TYPE_P (inside_type);
3003 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3004 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3005 int inter_int = INTEGRAL_TYPE_P (inter_type);
3006 int inter_ptr = POINTER_TYPE_P (inter_type);
3007 int inter_float = FLOAT_TYPE_P (inter_type);
3008 int inter_vec = VECTOR_TYPE_P (inter_type);
3009 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3010 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3011 int final_int = INTEGRAL_TYPE_P (type);
3012 int final_ptr = POINTER_TYPE_P (type);
3013 int final_float = FLOAT_TYPE_P (type);
3014 int final_vec = VECTOR_TYPE_P (type);
3015 unsigned int final_prec = TYPE_PRECISION (type);
3016 int final_unsignedp = TYPE_UNSIGNED (type);
3019 /* In addition to the cases of two conversions in a row
3020 handled below, if we are converting something to its own
3021 type via an object of identical or wider precision, neither
3022 conversion is needed. */
3023 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3025 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3026 && (((inter_int || inter_ptr) && final_int)
3027 || (inter_float && final_float))
3028 && inter_prec >= final_prec)
3031 /* Likewise, if the intermediate and initial types are either both
3032 float or both integer, we don't need the middle conversion if the
3033 former is wider than the latter and doesn't change the signedness
3034 (for integers). Avoid this if the final type is a pointer since
3035 then we sometimes need the middle conversion. */
3036 (if (((inter_int && inside_int) || (inter_float && inside_float))
3037 && (final_int || final_float)
3038 && inter_prec >= inside_prec
3039 && (inter_float || inter_unsignedp == inside_unsignedp))
3042 /* If we have a sign-extension of a zero-extended value, we can
3043 replace that by a single zero-extension. Likewise if the
3044 final conversion does not change precision we can drop the
3045 intermediate conversion. */
3046 (if (inside_int && inter_int && final_int
3047 && ((inside_prec < inter_prec && inter_prec < final_prec
3048 && inside_unsignedp && !inter_unsignedp)
3049 || final_prec == inter_prec))
3052 /* Two conversions in a row are not needed unless:
3053 - some conversion is floating-point (overstrict for now), or
3054 - some conversion is a vector (overstrict for now), or
3055 - the intermediate type is narrower than both initial and
3057 - the intermediate type and innermost type differ in signedness,
3058 and the outermost type is wider than the intermediate, or
3059 - the initial type is a pointer type and the precisions of the
3060 intermediate and final types differ, or
3061 - the final type is a pointer type and the precisions of the
3062 initial and intermediate types differ. */
3063 (if (! inside_float && ! inter_float && ! final_float
3064 && ! inside_vec && ! inter_vec && ! final_vec
3065 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3066 && ! (inside_int && inter_int
3067 && inter_unsignedp != inside_unsignedp
3068 && inter_prec < final_prec)
3069 && ((inter_unsignedp && inter_prec > inside_prec)
3070 == (final_unsignedp && final_prec > inter_prec))
3071 && ! (inside_ptr && inter_prec != final_prec)
3072 && ! (final_ptr && inside_prec != inter_prec))
3075 /* A truncation to an unsigned type (a zero-extension) should be
3076 canonicalized as bitwise and of a mask. */
3077 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3078 && final_int && inter_int && inside_int
3079 && final_prec == inside_prec
3080 && final_prec > inter_prec
3082 (convert (bit_and @0 { wide_int_to_tree
3084 wi::mask (inter_prec, false,
3085 TYPE_PRECISION (inside_type))); })))
3087 /* If we are converting an integer to a floating-point that can
3088 represent it exactly and back to an integer, we can skip the
3089 floating-point conversion. */
3090 (if (GIMPLE /* PR66211 */
3091 && inside_int && inter_float && final_int &&
3092 (unsigned) significand_size (TYPE_MODE (inter_type))
3093 >= inside_prec - !inside_unsignedp)
3096 /* If we have a narrowing conversion to an integral type that is fed by a
3097 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3098 masks off bits outside the final type (and nothing else). */
3100 (convert (bit_and @0 INTEGER_CST@1))
3101 (if (INTEGRAL_TYPE_P (type)
3102 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3103 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3104 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3105 TYPE_PRECISION (type)), 0))
3109 /* (X /[ex] A) * A -> X. */
3111 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3114 /* Simplify (A / B) * B + (A % B) -> A. */
3115 (for div (trunc_div ceil_div floor_div round_div)
3116 mod (trunc_mod ceil_mod floor_mod round_mod)
3118 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3121 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3122 (for op (plus minus)
3124 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3125 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3126 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3129 wi::overflow_type overflow;
3130 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3131 TYPE_SIGN (type), &overflow);
3133 (if (types_match (type, TREE_TYPE (@2))
3134 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3135 (op @0 { wide_int_to_tree (type, mul); })
3136 (with { tree utype = unsigned_type_for (type); }
3137 (convert (op (convert:utype @0)
3138 (mult (convert:utype @1) (convert:utype @2))))))))))
3140 /* Canonicalization of binary operations. */
3142 /* Convert X + -C into X - C. */
3144 (plus @0 REAL_CST@1)
3145 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3146 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3147 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3148 (minus @0 { tem; })))))
3150 /* Convert x+x into x*2. */
3153 (if (SCALAR_FLOAT_TYPE_P (type))
3154 (mult @0 { build_real (type, dconst2); })
3155 (if (INTEGRAL_TYPE_P (type))
3156 (mult @0 { build_int_cst (type, 2); }))))
3160 (minus integer_zerop @1)
3163 (pointer_diff integer_zerop @1)
3164 (negate (convert @1)))
3166 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3167 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3168 (-ARG1 + ARG0) reduces to -ARG1. */
3170 (minus real_zerop@0 @1)
3171 (if (fold_real_zero_addition_p (type, @0, 0))
3174 /* Transform x * -1 into -x. */
3176 (mult @0 integer_minus_onep)
3179 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3180 signed overflow for CST != 0 && CST != -1. */
3182 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3183 (if (TREE_CODE (@2) != INTEGER_CST
3185 && !integer_zerop (@1) && !integer_minus_onep (@1))
3186 (mult (mult @0 @2) @1)))
3188 /* True if we can easily extract the real and imaginary parts of a complex
3190 (match compositional_complex
3191 (convert? (complex @0 @1)))
3193 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3195 (complex (realpart @0) (imagpart @0))
3198 (realpart (complex @0 @1))
3201 (imagpart (complex @0 @1))
3204 /* Sometimes we only care about half of a complex expression. */
3206 (realpart (convert?:s (conj:s @0)))
3207 (convert (realpart @0)))
3209 (imagpart (convert?:s (conj:s @0)))
3210 (convert (negate (imagpart @0))))
3211 (for part (realpart imagpart)
3212 (for op (plus minus)
3214 (part (convert?:s@2 (op:s @0 @1)))
3215 (convert (op (part @0) (part @1))))))
3217 (realpart (convert?:s (CEXPI:s @0)))
3220 (imagpart (convert?:s (CEXPI:s @0)))
3223 /* conj(conj(x)) -> x */
3225 (conj (convert? (conj @0)))
3226 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3229 /* conj({x,y}) -> {x,-y} */
3231 (conj (convert?:s (complex:s @0 @1)))
3232 (with { tree itype = TREE_TYPE (type); }
3233 (complex (convert:itype @0) (negate (convert:itype @1)))))
3235 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3236 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3241 (bswap (bit_not (bswap @0)))
3243 (for bitop (bit_xor bit_ior bit_and)
3245 (bswap (bitop:c (bswap @0) @1))
3246 (bitop @0 (bswap @1)))))
3249 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3251 /* Simplify constant conditions.
3252 Only optimize constant conditions when the selected branch
3253 has the same type as the COND_EXPR. This avoids optimizing
3254 away "c ? x : throw", where the throw has a void type.
3255 Note that we cannot throw away the fold-const.c variant nor
3256 this one as we depend on doing this transform before possibly
3257 A ? B : B -> B triggers and the fold-const.c one can optimize
3258 0 ? A : B to B even if A has side-effects. Something
3259 genmatch cannot handle. */
3261 (cond INTEGER_CST@0 @1 @2)
3262 (if (integer_zerop (@0))
3263 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3265 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3268 (vec_cond VECTOR_CST@0 @1 @2)
3269 (if (integer_all_onesp (@0))
3271 (if (integer_zerop (@0))
3274 /* Sink unary operations to constant branches, but only if we do fold it to
3276 (for op (negate bit_not abs absu)
3278 (op (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2))
3282 cst1 = const_unop (op, type, @1);
3284 cst2 = const_unop (op, type, @2);
3287 (vec_cond @0 { cst1; } { cst2; })))))
3289 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3291 /* This pattern implements two kinds simplification:
3294 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3295 1) Conversions are type widening from smaller type.
3296 2) Const c1 equals to c2 after canonicalizing comparison.
3297 3) Comparison has tree code LT, LE, GT or GE.
3298 This specific pattern is needed when (cmp (convert x) c) may not
3299 be simplified by comparison patterns because of multiple uses of
3300 x. It also makes sense here because simplifying across multiple
3301 referred var is always benefitial for complicated cases.
3304 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3305 (for cmp (lt le gt ge eq)
3307 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3310 tree from_type = TREE_TYPE (@1);
3311 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3312 enum tree_code code = ERROR_MARK;
3314 if (INTEGRAL_TYPE_P (from_type)
3315 && int_fits_type_p (@2, from_type)
3316 && (types_match (c1_type, from_type)
3317 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3318 && (TYPE_UNSIGNED (from_type)
3319 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3320 && (types_match (c2_type, from_type)
3321 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3322 && (TYPE_UNSIGNED (from_type)
3323 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3327 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3329 /* X <= Y - 1 equals to X < Y. */
3332 /* X > Y - 1 equals to X >= Y. */
3336 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3338 /* X < Y + 1 equals to X <= Y. */
3341 /* X >= Y + 1 equals to X > Y. */
3345 if (code != ERROR_MARK
3346 || wi::to_widest (@2) == wi::to_widest (@3))
3348 if (cmp == LT_EXPR || cmp == LE_EXPR)
3350 if (cmp == GT_EXPR || cmp == GE_EXPR)
3354 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3355 else if (int_fits_type_p (@3, from_type))
3359 (if (code == MAX_EXPR)
3360 (convert (max @1 (convert @2)))
3361 (if (code == MIN_EXPR)
3362 (convert (min @1 (convert @2)))
3363 (if (code == EQ_EXPR)
3364 (convert (cond (eq @1 (convert @3))
3365 (convert:from_type @3) (convert:from_type @2)))))))))
3367 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3369 1) OP is PLUS or MINUS.
3370 2) CMP is LT, LE, GT or GE.
3371 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3373 This pattern also handles special cases like:
3375 A) Operand x is a unsigned to signed type conversion and c1 is
3376 integer zero. In this case,
3377 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3378 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3379 B) Const c1 may not equal to (C3 op' C2). In this case we also
3380 check equality for (c1+1) and (c1-1) by adjusting comparison
3383 TODO: Though signed type is handled by this pattern, it cannot be
3384 simplified at the moment because C standard requires additional
3385 type promotion. In order to match&simplify it here, the IR needs
3386 to be cleaned up by other optimizers, i.e, VRP. */
3387 (for op (plus minus)
3388 (for cmp (lt le gt ge)
3390 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3391 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3392 (if (types_match (from_type, to_type)
3393 /* Check if it is special case A). */
3394 || (TYPE_UNSIGNED (from_type)
3395 && !TYPE_UNSIGNED (to_type)
3396 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3397 && integer_zerop (@1)
3398 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3401 wi::overflow_type overflow = wi::OVF_NONE;
3402 enum tree_code code, cmp_code = cmp;
3404 wide_int c1 = wi::to_wide (@1);
3405 wide_int c2 = wi::to_wide (@2);
3406 wide_int c3 = wi::to_wide (@3);
3407 signop sgn = TYPE_SIGN (from_type);
3409 /* Handle special case A), given x of unsigned type:
3410 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3411 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3412 if (!types_match (from_type, to_type))
3414 if (cmp_code == LT_EXPR)
3416 if (cmp_code == GE_EXPR)
3418 c1 = wi::max_value (to_type);
3420 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3421 compute (c3 op' c2) and check if it equals to c1 with op' being
3422 the inverted operator of op. Make sure overflow doesn't happen
3423 if it is undefined. */
3424 if (op == PLUS_EXPR)
3425 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3427 real_c1 = wi::add (c3, c2, sgn, &overflow);
3430 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3432 /* Check if c1 equals to real_c1. Boundary condition is handled
3433 by adjusting comparison operation if necessary. */
3434 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3437 /* X <= Y - 1 equals to X < Y. */
3438 if (cmp_code == LE_EXPR)
3440 /* X > Y - 1 equals to X >= Y. */
3441 if (cmp_code == GT_EXPR)
3444 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3447 /* X < Y + 1 equals to X <= Y. */
3448 if (cmp_code == LT_EXPR)
3450 /* X >= Y + 1 equals to X > Y. */
3451 if (cmp_code == GE_EXPR)
3454 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3456 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3458 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3463 (if (code == MAX_EXPR)
3464 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3465 { wide_int_to_tree (from_type, c2); })
3466 (if (code == MIN_EXPR)
3467 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3468 { wide_int_to_tree (from_type, c2); })))))))))
3470 (for cnd (cond vec_cond)
3471 /* A ? B : (A ? X : C) -> A ? B : C. */
3473 (cnd @0 (cnd @0 @1 @2) @3)
3476 (cnd @0 @1 (cnd @0 @2 @3))
3478 /* A ? B : (!A ? C : X) -> A ? B : C. */
3479 /* ??? This matches embedded conditions open-coded because genmatch
3480 would generate matching code for conditions in separate stmts only.
3481 The following is still important to merge then and else arm cases
3482 from if-conversion. */
3484 (cnd @0 @1 (cnd @2 @3 @4))
3485 (if (inverse_conditions_p (@0, @2))
3488 (cnd @0 (cnd @1 @2 @3) @4)
3489 (if (inverse_conditions_p (@0, @1))
3492 /* A ? B : B -> B. */
3497 /* !A ? B : C -> A ? C : B. */
3499 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3502 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3503 return all -1 or all 0 results. */
3504 /* ??? We could instead convert all instances of the vec_cond to negate,
3505 but that isn't necessarily a win on its own. */
3507 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3508 (if (VECTOR_TYPE_P (type)
3509 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3510 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3511 && (TYPE_MODE (TREE_TYPE (type))
3512 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3513 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3515 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3517 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3518 (if (VECTOR_TYPE_P (type)
3519 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3520 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3521 && (TYPE_MODE (TREE_TYPE (type))
3522 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3523 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3526 /* Simplifications of comparisons. */
3528 /* See if we can reduce the magnitude of a constant involved in a
3529 comparison by changing the comparison code. This is a canonicalization
3530 formerly done by maybe_canonicalize_comparison_1. */
3534 (cmp @0 uniform_integer_cst_p@1)
3535 (with { tree cst = uniform_integer_cst_p (@1); }
3536 (if (tree_int_cst_sgn (cst) == -1)
3537 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3538 wide_int_to_tree (TREE_TYPE (cst),
3544 (cmp @0 uniform_integer_cst_p@1)
3545 (with { tree cst = uniform_integer_cst_p (@1); }
3546 (if (tree_int_cst_sgn (cst) == 1)
3547 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3548 wide_int_to_tree (TREE_TYPE (cst),
3549 wi::to_wide (cst) - 1)); })))))
3551 /* We can simplify a logical negation of a comparison to the
3552 inverted comparison. As we cannot compute an expression
3553 operator using invert_tree_comparison we have to simulate
3554 that with expression code iteration. */
3555 (for cmp (tcc_comparison)
3556 icmp (inverted_tcc_comparison)
3557 ncmp (inverted_tcc_comparison_with_nans)
3558 /* Ideally we'd like to combine the following two patterns
3559 and handle some more cases by using
3560 (logical_inverted_value (cmp @0 @1))
3561 here but for that genmatch would need to "inline" that.
3562 For now implement what forward_propagate_comparison did. */
3564 (bit_not (cmp @0 @1))
3565 (if (VECTOR_TYPE_P (type)
3566 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3567 /* Comparison inversion may be impossible for trapping math,
3568 invert_tree_comparison will tell us. But we can't use
3569 a computed operator in the replacement tree thus we have
3570 to play the trick below. */
3571 (with { enum tree_code ic = invert_tree_comparison
3572 (cmp, HONOR_NANS (@0)); }
3578 (bit_xor (cmp @0 @1) integer_truep)
3579 (with { enum tree_code ic = invert_tree_comparison
3580 (cmp, HONOR_NANS (@0)); }
3586 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3587 ??? The transformation is valid for the other operators if overflow
3588 is undefined for the type, but performing it here badly interacts
3589 with the transformation in fold_cond_expr_with_comparison which
3590 attempts to synthetize ABS_EXPR. */
3592 (for sub (minus pointer_diff)
3594 (cmp (sub@2 @0 @1) integer_zerop)
3595 (if (single_use (@2))
3598 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3599 signed arithmetic case. That form is created by the compiler
3600 often enough for folding it to be of value. One example is in
3601 computing loop trip counts after Operator Strength Reduction. */
3602 (for cmp (simple_comparison)
3603 scmp (swapped_simple_comparison)
3605 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3606 /* Handle unfolded multiplication by zero. */
3607 (if (integer_zerop (@1))
3609 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3610 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3612 /* If @1 is negative we swap the sense of the comparison. */
3613 (if (tree_int_cst_sgn (@1) < 0)
3617 /* Simplify comparison of something with itself. For IEEE
3618 floating-point, we can only do some of these simplifications. */
3622 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3623 || ! HONOR_NANS (@0))
3624 { constant_boolean_node (true, type); }
3625 (if (cmp != EQ_EXPR)
3631 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3632 || ! HONOR_NANS (@0))
3633 { constant_boolean_node (false, type); })))
3634 (for cmp (unle unge uneq)
3637 { constant_boolean_node (true, type); }))
3638 (for cmp (unlt ungt)
3644 (if (!flag_trapping_math)
3645 { constant_boolean_node (false, type); }))
3647 /* Fold ~X op ~Y as Y op X. */
3648 (for cmp (simple_comparison)
3650 (cmp (bit_not@2 @0) (bit_not@3 @1))
3651 (if (single_use (@2) && single_use (@3))
3654 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3655 (for cmp (simple_comparison)
3656 scmp (swapped_simple_comparison)
3658 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3659 (if (single_use (@2)
3660 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3661 (scmp @0 (bit_not @1)))))
3663 (for cmp (simple_comparison)
3664 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3666 (cmp (convert@2 @0) (convert? @1))
3667 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3668 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3669 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3670 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3671 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3674 tree type1 = TREE_TYPE (@1);
3675 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3677 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3678 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3679 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3680 type1 = float_type_node;
3681 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3682 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3683 type1 = double_type_node;
3686 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3687 ? TREE_TYPE (@0) : type1);
3689 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3690 (cmp (convert:newtype @0) (convert:newtype @1))))))
3694 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3696 /* a CMP (-0) -> a CMP 0 */
3697 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3698 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3699 /* x != NaN is always true, other ops are always false. */
3700 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3701 && ! HONOR_SNANS (@1))
3702 { constant_boolean_node (cmp == NE_EXPR, type); })
3703 /* Fold comparisons against infinity. */
3704 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3705 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3708 REAL_VALUE_TYPE max;
3709 enum tree_code code = cmp;
3710 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3712 code = swap_tree_comparison (code);
3715 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3716 (if (code == GT_EXPR
3717 && !(HONOR_NANS (@0) && flag_trapping_math))
3718 { constant_boolean_node (false, type); })
3719 (if (code == LE_EXPR)
3720 /* x <= +Inf is always true, if we don't care about NaNs. */
3721 (if (! HONOR_NANS (@0))
3722 { constant_boolean_node (true, type); }
3723 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3724 an "invalid" exception. */
3725 (if (!flag_trapping_math)
3727 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3728 for == this introduces an exception for x a NaN. */
3729 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3731 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3733 (lt @0 { build_real (TREE_TYPE (@0), max); })
3734 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3735 /* x < +Inf is always equal to x <= DBL_MAX. */
3736 (if (code == LT_EXPR)
3737 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3739 (ge @0 { build_real (TREE_TYPE (@0), max); })
3740 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3741 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3742 an exception for x a NaN so use an unordered comparison. */
3743 (if (code == NE_EXPR)
3744 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3745 (if (! HONOR_NANS (@0))
3747 (ge @0 { build_real (TREE_TYPE (@0), max); })
3748 (le @0 { build_real (TREE_TYPE (@0), max); }))
3750 (unge @0 { build_real (TREE_TYPE (@0), max); })
3751 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3753 /* If this is a comparison of a real constant with a PLUS_EXPR
3754 or a MINUS_EXPR of a real constant, we can convert it into a
3755 comparison with a revised real constant as long as no overflow
3756 occurs when unsafe_math_optimizations are enabled. */
3757 (if (flag_unsafe_math_optimizations)
3758 (for op (plus minus)
3760 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3763 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3764 TREE_TYPE (@1), @2, @1);
3766 (if (tem && !TREE_OVERFLOW (tem))
3767 (cmp @0 { tem; }))))))
3769 /* Likewise, we can simplify a comparison of a real constant with
3770 a MINUS_EXPR whose first operand is also a real constant, i.e.
3771 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3772 floating-point types only if -fassociative-math is set. */
3773 (if (flag_associative_math)
3775 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3776 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3777 (if (tem && !TREE_OVERFLOW (tem))
3778 (cmp { tem; } @1)))))
3780 /* Fold comparisons against built-in math functions. */
3781 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
3784 (cmp (sq @0) REAL_CST@1)
3786 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3788 /* sqrt(x) < y is always false, if y is negative. */
3789 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3790 { constant_boolean_node (false, type); })
3791 /* sqrt(x) > y is always true, if y is negative and we
3792 don't care about NaNs, i.e. negative values of x. */
3793 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3794 { constant_boolean_node (true, type); })
3795 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3796 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3797 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3799 /* sqrt(x) < 0 is always false. */
3800 (if (cmp == LT_EXPR)
3801 { constant_boolean_node (false, type); })
3802 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3803 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3804 { constant_boolean_node (true, type); })
3805 /* sqrt(x) <= 0 -> x == 0. */
3806 (if (cmp == LE_EXPR)
3808 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3809 == or !=. In the last case:
3811 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3813 if x is negative or NaN. Due to -funsafe-math-optimizations,
3814 the results for other x follow from natural arithmetic. */
3816 (if ((cmp == LT_EXPR
3820 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3821 /* Give up for -frounding-math. */
3822 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
3826 enum tree_code ncmp = cmp;
3827 const real_format *fmt
3828 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
3829 real_arithmetic (&c2, MULT_EXPR,
3830 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3831 real_convert (&c2, fmt, &c2);
3832 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
3833 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
3834 if (!REAL_VALUE_ISINF (c2))
3836 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
3837 build_real (TREE_TYPE (@0), c2));
3838 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
3840 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
3841 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
3842 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
3843 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
3844 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
3845 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
3848 /* With rounding to even, sqrt of up to 3 different values
3849 gives the same normal result, so in some cases c2 needs
3851 REAL_VALUE_TYPE c2alt, tow;
3852 if (cmp == LT_EXPR || cmp == GE_EXPR)
3856 real_nextafter (&c2alt, fmt, &c2, &tow);
3857 real_convert (&c2alt, fmt, &c2alt);
3858 if (REAL_VALUE_ISINF (c2alt))
3862 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
3863 build_real (TREE_TYPE (@0), c2alt));
3864 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
3866 else if (real_equal (&TREE_REAL_CST (c3),
3867 &TREE_REAL_CST (@1)))
3873 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3874 (if (REAL_VALUE_ISINF (c2))
3875 /* sqrt(x) > y is x == +Inf, when y is very large. */
3876 (if (HONOR_INFINITIES (@0))
3877 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3878 { constant_boolean_node (false, type); })
3879 /* sqrt(x) > c is the same as x > c*c. */
3880 (if (ncmp != ERROR_MARK)
3881 (if (ncmp == GE_EXPR)
3882 (ge @0 { build_real (TREE_TYPE (@0), c2); })
3883 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
3884 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
3885 (if (REAL_VALUE_ISINF (c2))
3887 /* sqrt(x) < y is always true, when y is a very large
3888 value and we don't care about NaNs or Infinities. */
3889 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3890 { constant_boolean_node (true, type); })
3891 /* sqrt(x) < y is x != +Inf when y is very large and we
3892 don't care about NaNs. */
3893 (if (! HONOR_NANS (@0))
3894 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3895 /* sqrt(x) < y is x >= 0 when y is very large and we
3896 don't care about Infinities. */
3897 (if (! HONOR_INFINITIES (@0))
3898 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3899 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3902 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3903 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3904 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3905 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
3906 (if (ncmp == LT_EXPR)
3907 (lt @0 { build_real (TREE_TYPE (@0), c2); })
3908 (le @0 { build_real (TREE_TYPE (@0), c2); }))
3909 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3910 (if (ncmp != ERROR_MARK && GENERIC)
3911 (if (ncmp == LT_EXPR)
3913 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3914 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
3916 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3917 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
3918 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3920 (cmp (sq @0) (sq @1))
3921 (if (! HONOR_NANS (@0))
3924 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
3925 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3926 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
3928 (cmp (float@0 @1) (float @2))
3929 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3930 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3933 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3934 tree type1 = TREE_TYPE (@1);
3935 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3936 tree type2 = TREE_TYPE (@2);
3937 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3939 (if (fmt.can_represent_integral_type_p (type1)
3940 && fmt.can_represent_integral_type_p (type2))
3941 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3942 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3943 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3944 && type1_signed_p >= type2_signed_p)
3945 (icmp @1 (convert @2))
3946 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3947 && type1_signed_p <= type2_signed_p)
3948 (icmp (convert:type2 @1) @2)
3949 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3950 && type1_signed_p == type2_signed_p)
3951 (icmp @1 @2))))))))))
3953 /* Optimize various special cases of (FTYPE) N CMP CST. */
3954 (for cmp (lt le eq ne ge gt)
3955 icmp (le le eq ne ge ge)
3957 (cmp (float @0) REAL_CST@1)
3958 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3959 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3962 tree itype = TREE_TYPE (@0);
3963 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3964 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3965 /* Be careful to preserve any potential exceptions due to
3966 NaNs. qNaNs are ok in == or != context.
3967 TODO: relax under -fno-trapping-math or
3968 -fno-signaling-nans. */
3970 = real_isnan (cst) && (cst->signalling
3971 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3973 /* TODO: allow non-fitting itype and SNaNs when
3974 -fno-trapping-math. */
3975 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
3978 signop isign = TYPE_SIGN (itype);
3979 REAL_VALUE_TYPE imin, imax;
3980 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3981 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3983 REAL_VALUE_TYPE icst;
3984 if (cmp == GT_EXPR || cmp == GE_EXPR)
3985 real_ceil (&icst, fmt, cst);
3986 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3987 real_floor (&icst, fmt, cst);
3989 real_trunc (&icst, fmt, cst);
3991 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3993 bool overflow_p = false;
3995 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3998 /* Optimize cases when CST is outside of ITYPE's range. */
3999 (if (real_compare (LT_EXPR, cst, &imin))
4000 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4002 (if (real_compare (GT_EXPR, cst, &imax))
4003 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4005 /* Remove cast if CST is an integer representable by ITYPE. */
4007 (cmp @0 { gcc_assert (!overflow_p);
4008 wide_int_to_tree (itype, icst_val); })
4010 /* When CST is fractional, optimize
4011 (FTYPE) N == CST -> 0
4012 (FTYPE) N != CST -> 1. */
4013 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4014 { constant_boolean_node (cmp == NE_EXPR, type); })
4015 /* Otherwise replace with sensible integer constant. */
4018 gcc_checking_assert (!overflow_p);
4020 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4022 /* Fold A /[ex] B CMP C to A CMP B * C. */
4025 (cmp (exact_div @0 @1) INTEGER_CST@2)
4026 (if (!integer_zerop (@1))
4027 (if (wi::to_wide (@2) == 0)
4029 (if (TREE_CODE (@1) == INTEGER_CST)
4032 wi::overflow_type ovf;
4033 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4034 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4037 { constant_boolean_node (cmp == NE_EXPR, type); }
4038 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4039 (for cmp (lt le gt ge)
4041 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4042 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4045 wi::overflow_type ovf;
4046 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4047 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4050 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4051 TYPE_SIGN (TREE_TYPE (@2)))
4052 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4053 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4055 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4057 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4058 For large C (more than min/B+2^size), this is also true, with the
4059 multiplication computed modulo 2^size.
4060 For intermediate C, this just tests the sign of A. */
4061 (for cmp (lt le gt ge)
4064 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4065 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4066 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4067 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4070 tree utype = TREE_TYPE (@2);
4071 wide_int denom = wi::to_wide (@1);
4072 wide_int right = wi::to_wide (@2);
4073 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4074 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4075 bool small = wi::leu_p (right, smax);
4076 bool large = wi::geu_p (right, smin);
4078 (if (small || large)
4079 (cmp (convert:utype @0) (mult @2 (convert @1)))
4080 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4082 /* Unordered tests if either argument is a NaN. */
4084 (bit_ior (unordered @0 @0) (unordered @1 @1))
4085 (if (types_match (@0, @1))
4088 (bit_and (ordered @0 @0) (ordered @1 @1))
4089 (if (types_match (@0, @1))
4092 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4095 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4098 /* Simple range test simplifications. */
4099 /* A < B || A >= B -> true. */
4100 (for test1 (lt le le le ne ge)
4101 test2 (ge gt ge ne eq ne)
4103 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4104 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4105 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4106 { constant_boolean_node (true, type); })))
4107 /* A < B && A >= B -> false. */
4108 (for test1 (lt lt lt le ne eq)
4109 test2 (ge gt eq gt eq gt)
4111 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4112 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4113 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4114 { constant_boolean_node (false, type); })))
4116 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4117 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4119 Note that comparisons
4120 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4121 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4122 will be canonicalized to above so there's no need to
4129 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4130 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4133 tree ty = TREE_TYPE (@0);
4134 unsigned prec = TYPE_PRECISION (ty);
4135 wide_int mask = wi::to_wide (@2, prec);
4136 wide_int rhs = wi::to_wide (@3, prec);
4137 signop sgn = TYPE_SIGN (ty);
4139 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4140 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4141 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4142 { build_zero_cst (ty); }))))))
4144 /* -A CMP -B -> B CMP A. */
4145 (for cmp (tcc_comparison)
4146 scmp (swapped_tcc_comparison)
4148 (cmp (negate @0) (negate @1))
4149 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4150 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4151 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4154 (cmp (negate @0) CONSTANT_CLASS_P@1)
4155 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4156 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4157 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4158 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4159 (if (tem && !TREE_OVERFLOW (tem))
4160 (scmp @0 { tem; }))))))
4162 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4165 (op (abs @0) zerop@1)
4168 /* From fold_sign_changed_comparison and fold_widened_comparison.
4169 FIXME: the lack of symmetry is disturbing. */
4170 (for cmp (simple_comparison)
4172 (cmp (convert@0 @00) (convert?@1 @10))
4173 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4174 /* Disable this optimization if we're casting a function pointer
4175 type on targets that require function pointer canonicalization. */
4176 && !(targetm.have_canonicalize_funcptr_for_compare ()
4177 && ((POINTER_TYPE_P (TREE_TYPE (@00))
4178 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4179 || (POINTER_TYPE_P (TREE_TYPE (@10))
4180 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4182 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4183 && (TREE_CODE (@10) == INTEGER_CST
4185 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4188 && !POINTER_TYPE_P (TREE_TYPE (@00)))
4189 /* ??? The special-casing of INTEGER_CST conversion was in the original
4190 code and here to avoid a spurious overflow flag on the resulting
4191 constant which fold_convert produces. */
4192 (if (TREE_CODE (@1) == INTEGER_CST)
4193 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4194 TREE_OVERFLOW (@1)); })
4195 (cmp @00 (convert @1)))
4197 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4198 /* If possible, express the comparison in the shorter mode. */
4199 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4200 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4201 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4202 && TYPE_UNSIGNED (TREE_TYPE (@00))))
4203 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4204 || ((TYPE_PRECISION (TREE_TYPE (@00))
4205 >= TYPE_PRECISION (TREE_TYPE (@10)))
4206 && (TYPE_UNSIGNED (TREE_TYPE (@00))
4207 == TYPE_UNSIGNED (TREE_TYPE (@10))))
4208 || (TREE_CODE (@10) == INTEGER_CST
4209 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4210 && int_fits_type_p (@10, TREE_TYPE (@00)))))
4211 (cmp @00 (convert @10))
4212 (if (TREE_CODE (@10) == INTEGER_CST
4213 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4214 && !int_fits_type_p (@10, TREE_TYPE (@00)))
4217 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4218 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4219 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4220 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4222 (if (above || below)
4223 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4224 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4225 (if (cmp == LT_EXPR || cmp == LE_EXPR)
4226 { constant_boolean_node (above ? true : false, type); }
4227 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4228 { constant_boolean_node (above ? false : true, type); }))))))))))))
4231 /* A local variable can never be pointed to by
4232 the default SSA name of an incoming parameter.
4233 SSA names are canonicalized to 2nd place. */
4235 (cmp addr@0 SSA_NAME@1)
4236 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4237 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
4238 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
4239 (if (TREE_CODE (base) == VAR_DECL
4240 && auto_var_in_fn_p (base, current_function_decl))
4241 (if (cmp == NE_EXPR)
4242 { constant_boolean_node (true, type); }
4243 { constant_boolean_node (false, type); }))))))
4245 /* Equality compare simplifications from fold_binary */
4248 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4249 Similarly for NE_EXPR. */
4251 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4252 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4253 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4254 { constant_boolean_node (cmp == NE_EXPR, type); }))
4256 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
4258 (cmp (bit_xor @0 @1) integer_zerop)
4261 /* (X ^ Y) == Y becomes X == 0.
4262 Likewise (X ^ Y) == X becomes Y == 0. */
4264 (cmp:c (bit_xor:c @0 @1) @0)
4265 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4267 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
4269 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4270 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4271 (cmp @0 (bit_xor @1 (convert @2)))))
4274 (cmp (convert? addr@0) integer_zerop)
4275 (if (tree_single_nonzero_warnv_p (@0, NULL))
4276 { constant_boolean_node (cmp == NE_EXPR, type); })))
4278 /* If we have (A & C) == C where C is a power of 2, convert this into
4279 (A & C) != 0. Similarly for NE_EXPR. */
4283 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4284 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4286 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4287 convert this into a shift followed by ANDing with D. */
4290 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4291 INTEGER_CST@2 integer_zerop)
4292 (if (integer_pow2p (@2))
4294 int shift = (wi::exact_log2 (wi::to_wide (@2))
4295 - wi::exact_log2 (wi::to_wide (@1)));
4299 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4301 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4304 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4305 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
4309 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4310 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4311 && type_has_mode_precision_p (TREE_TYPE (@0))
4312 && element_precision (@2) >= element_precision (@0)
4313 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4314 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4315 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4317 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4318 this into a right shift or sign extension followed by ANDing with C. */
4321 (lt @0 integer_zerop)
4322 INTEGER_CST@1 integer_zerop)
4323 (if (integer_pow2p (@1)
4324 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4326 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4330 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4332 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4333 sign extension followed by AND with C will achieve the effect. */
4334 (bit_and (convert @0) @1)))))
4336 /* When the addresses are not directly of decls compare base and offset.
4337 This implements some remaining parts of fold_comparison address
4338 comparisons but still no complete part of it. Still it is good
4339 enough to make fold_stmt not regress when not dispatching to fold_binary. */
4340 (for cmp (simple_comparison)
4342 (cmp (convert1?@2 addr@0) (convert2? addr@1))
4345 poly_int64 off0, off1;
4346 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4347 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4348 if (base0 && TREE_CODE (base0) == MEM_REF)
4350 off0 += mem_ref_offset (base0).force_shwi ();
4351 base0 = TREE_OPERAND (base0, 0);
4353 if (base1 && TREE_CODE (base1) == MEM_REF)
4355 off1 += mem_ref_offset (base1).force_shwi ();
4356 base1 = TREE_OPERAND (base1, 0);
4359 (if (base0 && base1)
4363 /* Punt in GENERIC on variables with value expressions;
4364 the value expressions might point to fields/elements
4365 of other vars etc. */
4367 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4368 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4370 else if (decl_in_symtab_p (base0)
4371 && decl_in_symtab_p (base1))
4372 equal = symtab_node::get_create (base0)
4373 ->equal_address_to (symtab_node::get_create (base1));
4374 else if ((DECL_P (base0)
4375 || TREE_CODE (base0) == SSA_NAME
4376 || TREE_CODE (base0) == STRING_CST)
4378 || TREE_CODE (base1) == SSA_NAME
4379 || TREE_CODE (base1) == STRING_CST))
4380 equal = (base0 == base1);
4383 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4384 off0.is_constant (&ioff0);
4385 off1.is_constant (&ioff1);
4386 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4387 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4388 || (TREE_CODE (base0) == STRING_CST
4389 && TREE_CODE (base1) == STRING_CST
4390 && ioff0 >= 0 && ioff1 >= 0
4391 && ioff0 < TREE_STRING_LENGTH (base0)
4392 && ioff1 < TREE_STRING_LENGTH (base1)
4393 /* This is a too conservative test that the STRING_CSTs
4394 will not end up being string-merged. */
4395 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4396 TREE_STRING_POINTER (base1) + ioff1,
4397 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4398 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4400 else if (!DECL_P (base0) || !DECL_P (base1))
4402 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4404 /* If this is a pointer comparison, ignore for now even
4405 valid equalities where one pointer is the offset zero
4406 of one object and the other to one past end of another one. */
4407 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4409 /* Assume that automatic variables can't be adjacent to global
4411 else if (is_global_var (base0) != is_global_var (base1))
4415 tree sz0 = DECL_SIZE_UNIT (base0);
4416 tree sz1 = DECL_SIZE_UNIT (base1);
4417 /* If sizes are unknown, e.g. VLA or not representable,
4419 if (!tree_fits_poly_int64_p (sz0)
4420 || !tree_fits_poly_int64_p (sz1))
4424 poly_int64 size0 = tree_to_poly_int64 (sz0);
4425 poly_int64 size1 = tree_to_poly_int64 (sz1);
4426 /* If one offset is pointing (or could be) to the beginning
4427 of one object and the other is pointing to one past the
4428 last byte of the other object, punt. */
4429 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4431 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4433 /* If both offsets are the same, there are some cases
4434 we know that are ok. Either if we know they aren't
4435 zero, or if we know both sizes are no zero. */
4437 && known_eq (off0, off1)
4438 && (known_ne (off0, 0)
4439 || (known_ne (size0, 0) && known_ne (size1, 0))))
4446 && (cmp == EQ_EXPR || cmp == NE_EXPR
4447 /* If the offsets are equal we can ignore overflow. */
4448 || known_eq (off0, off1)
4449 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4450 /* Or if we compare using pointers to decls or strings. */
4451 || (POINTER_TYPE_P (TREE_TYPE (@2))
4452 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4454 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4455 { constant_boolean_node (known_eq (off0, off1), type); })
4456 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4457 { constant_boolean_node (known_ne (off0, off1), type); })
4458 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4459 { constant_boolean_node (known_lt (off0, off1), type); })
4460 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4461 { constant_boolean_node (known_le (off0, off1), type); })
4462 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4463 { constant_boolean_node (known_ge (off0, off1), type); })
4464 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4465 { constant_boolean_node (known_gt (off0, off1), type); }))
4468 (if (cmp == EQ_EXPR)
4469 { constant_boolean_node (false, type); })
4470 (if (cmp == NE_EXPR)
4471 { constant_boolean_node (true, type); })))))))))
4473 /* Simplify pointer equality compares using PTA. */
4477 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4478 && ptrs_compare_unequal (@0, @1))
4479 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4481 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4482 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4483 Disable the transform if either operand is pointer to function.
4484 This broke pr22051-2.c for arm where function pointer
4485 canonicalizaion is not wanted. */
4489 (cmp (convert @0) INTEGER_CST@1)
4490 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4491 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4492 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4493 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4494 && POINTER_TYPE_P (TREE_TYPE (@1))
4495 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4496 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4497 (cmp @0 (convert @1)))))
4499 /* Non-equality compare simplifications from fold_binary */
4500 (for cmp (lt gt le ge)
4501 /* Comparisons with the highest or lowest possible integer of
4502 the specified precision will have known values. */
4504 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4505 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4506 || POINTER_TYPE_P (TREE_TYPE (@1))
4507 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4508 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4511 tree cst = uniform_integer_cst_p (@1);
4512 tree arg1_type = TREE_TYPE (cst);
4513 unsigned int prec = TYPE_PRECISION (arg1_type);
4514 wide_int max = wi::max_value (arg1_type);
4515 wide_int signed_max = wi::max_value (prec, SIGNED);
4516 wide_int min = wi::min_value (arg1_type);
4519 (if (wi::to_wide (cst) == max)
4521 (if (cmp == GT_EXPR)
4522 { constant_boolean_node (false, type); })
4523 (if (cmp == GE_EXPR)
4525 (if (cmp == LE_EXPR)
4526 { constant_boolean_node (true, type); })
4527 (if (cmp == LT_EXPR)
4529 (if (wi::to_wide (cst) == min)
4531 (if (cmp == LT_EXPR)
4532 { constant_boolean_node (false, type); })
4533 (if (cmp == LE_EXPR)
4535 (if (cmp == GE_EXPR)
4536 { constant_boolean_node (true, type); })
4537 (if (cmp == GT_EXPR)
4539 (if (wi::to_wide (cst) == max - 1)
4541 (if (cmp == GT_EXPR)
4542 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4543 wide_int_to_tree (TREE_TYPE (cst),
4546 (if (cmp == LE_EXPR)
4547 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4548 wide_int_to_tree (TREE_TYPE (cst),
4551 (if (wi::to_wide (cst) == min + 1)
4553 (if (cmp == GE_EXPR)
4554 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4555 wide_int_to_tree (TREE_TYPE (cst),
4558 (if (cmp == LT_EXPR)
4559 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4560 wide_int_to_tree (TREE_TYPE (cst),
4563 (if (wi::to_wide (cst) == signed_max
4564 && TYPE_UNSIGNED (arg1_type)
4565 /* We will flip the signedness of the comparison operator
4566 associated with the mode of @1, so the sign bit is
4567 specified by this mode. Check that @1 is the signed
4568 max associated with this sign bit. */
4569 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4570 /* signed_type does not work on pointer types. */
4571 && INTEGRAL_TYPE_P (arg1_type))
4572 /* The following case also applies to X < signed_max+1
4573 and X >= signed_max+1 because previous transformations. */
4574 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4575 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4577 (if (cst == @1 && cmp == LE_EXPR)
4578 (ge (convert:st @0) { build_zero_cst (st); }))
4579 (if (cst == @1 && cmp == GT_EXPR)
4580 (lt (convert:st @0) { build_zero_cst (st); }))
4581 (if (cmp == LE_EXPR)
4582 (ge (view_convert:st @0) { build_zero_cst (st); }))
4583 (if (cmp == GT_EXPR)
4584 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4586 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4587 /* If the second operand is NaN, the result is constant. */
4590 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4591 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4592 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4593 ? false : true, type); })))
4595 /* bool_var != 0 becomes bool_var. */
4597 (ne @0 integer_zerop)
4598 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4599 && types_match (type, TREE_TYPE (@0)))
4601 /* bool_var == 1 becomes bool_var. */
4603 (eq @0 integer_onep)
4604 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4605 && types_match (type, TREE_TYPE (@0)))
4608 bool_var == 0 becomes !bool_var or
4609 bool_var != 1 becomes !bool_var
4610 here because that only is good in assignment context as long
4611 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4612 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4613 clearly less optimal and which we'll transform again in forwprop. */
4615 /* When one argument is a constant, overflow detection can be simplified.
4616 Currently restricted to single use so as not to interfere too much with
4617 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4618 A + CST CMP A -> A CMP' CST' */
4619 (for cmp (lt le ge gt)
4622 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4623 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4624 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4625 && wi::to_wide (@1) != 0
4627 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4628 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4629 wi::max_value (prec, UNSIGNED)
4630 - wi::to_wide (@1)); })))))
4632 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4633 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4634 expects the long form, so we restrict the transformation for now. */
4637 (cmp:c (minus@2 @0 @1) @0)
4638 (if (single_use (@2)
4639 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4640 && TYPE_UNSIGNED (TREE_TYPE (@0))
4641 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4644 /* Testing for overflow is unnecessary if we already know the result. */
4649 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4650 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4651 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4652 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4657 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4658 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4659 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4660 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4662 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4663 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4667 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4668 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4669 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4670 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4672 /* Simplification of math builtins. These rules must all be optimizations
4673 as well as IL simplifications. If there is a possibility that the new
4674 form could be a pessimization, the rule should go in the canonicalization
4675 section that follows this one.
4677 Rules can generally go in this section if they satisfy one of
4680 - the rule describes an identity
4682 - the rule replaces calls with something as simple as addition or
4685 - the rule contains unary calls only and simplifies the surrounding
4686 arithmetic. (The idea here is to exclude non-unary calls in which
4687 one operand is constant and in which the call is known to be cheap
4688 when the operand has that value.) */
4690 (if (flag_unsafe_math_optimizations)
4691 /* Simplify sqrt(x) * sqrt(x) -> x. */
4693 (mult (SQRT_ALL@1 @0) @1)
4694 (if (!HONOR_SNANS (type))
4697 (for op (plus minus)
4698 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4702 (rdiv (op @0 @2) @1)))
4704 (for cmp (lt le gt ge)
4705 neg_cmp (gt ge lt le)
4706 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4708 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4710 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4712 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4713 || (real_zerop (tem) && !real_zerop (@1))))
4715 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4717 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4718 (neg_cmp @0 { tem; })))))))
4720 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4721 (for root (SQRT CBRT)
4723 (mult (root:s @0) (root:s @1))
4724 (root (mult @0 @1))))
4726 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4727 (for exps (EXP EXP2 EXP10 POW10)
4729 (mult (exps:s @0) (exps:s @1))
4730 (exps (plus @0 @1))))
4732 /* Simplify a/root(b/c) into a*root(c/b). */
4733 (for root (SQRT CBRT)
4735 (rdiv @0 (root:s (rdiv:s @1 @2)))
4736 (mult @0 (root (rdiv @2 @1)))))
4738 /* Simplify x/expN(y) into x*expN(-y). */
4739 (for exps (EXP EXP2 EXP10 POW10)
4741 (rdiv @0 (exps:s @1))
4742 (mult @0 (exps (negate @1)))))
4744 (for logs (LOG LOG2 LOG10 LOG10)
4745 exps (EXP EXP2 EXP10 POW10)
4746 /* logN(expN(x)) -> x. */
4750 /* expN(logN(x)) -> x. */
4755 /* Optimize logN(func()) for various exponential functions. We
4756 want to determine the value "x" and the power "exponent" in
4757 order to transform logN(x**exponent) into exponent*logN(x). */
4758 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4759 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4762 (if (SCALAR_FLOAT_TYPE_P (type))
4768 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4769 x = build_real_truncate (type, dconst_e ());
4772 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4773 x = build_real (type, dconst2);
4777 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4779 REAL_VALUE_TYPE dconst10;
4780 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4781 x = build_real (type, dconst10);
4788 (mult (logs { x; }) @0)))))
4796 (if (SCALAR_FLOAT_TYPE_P (type))
4802 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4803 x = build_real (type, dconsthalf);
4806 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4807 x = build_real_truncate (type, dconst_third ());
4813 (mult { x; } (logs @0))))))
4815 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4816 (for logs (LOG LOG2 LOG10)
4820 (mult @1 (logs @0))))
4822 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4823 or if C is a positive power of 2,
4824 pow(C,x) -> exp2(log2(C)*x). */
4832 (pows REAL_CST@0 @1)
4833 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4834 && real_isfinite (TREE_REAL_CST_PTR (@0))
4835 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4836 the use_exp2 case until after vectorization. It seems actually
4837 beneficial for all constants to postpone this until later,
4838 because exp(log(C)*x), while faster, will have worse precision
4839 and if x folds into a constant too, that is unnecessary
4841 && canonicalize_math_after_vectorization_p ())
4843 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4844 bool use_exp2 = false;
4845 if (targetm.libc_has_function (function_c99_misc)
4846 && value->cl == rvc_normal)
4848 REAL_VALUE_TYPE frac_rvt = *value;
4849 SET_REAL_EXP (&frac_rvt, 1);
4850 if (real_equal (&frac_rvt, &dconst1))
4855 (if (optimize_pow_to_exp (@0, @1))
4856 (exps (mult (logs @0) @1)))
4857 (exp2s (mult (log2s @0) @1)))))))
4860 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4862 exps (EXP EXP2 EXP10 POW10)
4863 logs (LOG LOG2 LOG10 LOG10)
4865 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4866 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4867 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4868 (exps (plus (mult (logs @0) @1) @2)))))
4873 exps (EXP EXP2 EXP10 POW10)
4874 /* sqrt(expN(x)) -> expN(x*0.5). */
4877 (exps (mult @0 { build_real (type, dconsthalf); })))
4878 /* cbrt(expN(x)) -> expN(x/3). */
4881 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4882 /* pow(expN(x), y) -> expN(x*y). */
4885 (exps (mult @0 @1))))
4887 /* tan(atan(x)) -> x. */
4894 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4898 copysigns (COPYSIGN)
4903 REAL_VALUE_TYPE r_cst;
4904 build_sinatan_real (&r_cst, type);
4905 tree t_cst = build_real (type, r_cst);
4906 tree t_one = build_one_cst (type);
4908 (if (SCALAR_FLOAT_TYPE_P (type))
4909 (cond (lt (abs @0) { t_cst; })
4910 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4911 (copysigns { t_one; } @0))))))
4913 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4917 copysigns (COPYSIGN)
4922 REAL_VALUE_TYPE r_cst;
4923 build_sinatan_real (&r_cst, type);
4924 tree t_cst = build_real (type, r_cst);
4925 tree t_one = build_one_cst (type);
4926 tree t_zero = build_zero_cst (type);
4928 (if (SCALAR_FLOAT_TYPE_P (type))
4929 (cond (lt (abs @0) { t_cst; })
4930 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4931 (copysigns { t_zero; } @0))))))
4933 (if (!flag_errno_math)
4934 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4939 (sinhs (atanhs:s @0))
4940 (with { tree t_one = build_one_cst (type); }
4941 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4943 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4948 (coshs (atanhs:s @0))
4949 (with { tree t_one = build_one_cst (type); }
4950 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4952 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4954 (CABS (complex:C @0 real_zerop@1))
4957 /* trunc(trunc(x)) -> trunc(x), etc. */
4958 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4962 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4963 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4965 (fns integer_valued_real_p@0)
4968 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4970 (HYPOT:c @0 real_zerop@1)
4973 /* pow(1,x) -> 1. */
4975 (POW real_onep@0 @1)
4979 /* copysign(x,x) -> x. */
4980 (COPYSIGN_ALL @0 @0)
4984 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4985 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4988 (for scale (LDEXP SCALBN SCALBLN)
4989 /* ldexp(0, x) -> 0. */
4991 (scale real_zerop@0 @1)
4993 /* ldexp(x, 0) -> x. */
4995 (scale @0 integer_zerop@1)
4997 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4999 (scale REAL_CST@0 @1)
5000 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5003 /* Canonicalization of sequences of math builtins. These rules represent
5004 IL simplifications but are not necessarily optimizations.
5006 The sincos pass is responsible for picking "optimal" implementations
5007 of math builtins, which may be more complicated and can sometimes go
5008 the other way, e.g. converting pow into a sequence of sqrts.
5009 We only want to do these canonicalizations before the pass has run. */
5011 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5012 /* Simplify tan(x) * cos(x) -> sin(x). */
5014 (mult:c (TAN:s @0) (COS:s @0))
5017 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5019 (mult:c @0 (POW:s @0 REAL_CST@1))
5020 (if (!TREE_OVERFLOW (@1))
5021 (POW @0 (plus @1 { build_one_cst (type); }))))
5023 /* Simplify sin(x) / cos(x) -> tan(x). */
5025 (rdiv (SIN:s @0) (COS:s @0))
5028 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5030 (rdiv (SINH:s @0) (COSH:s @0))
5033 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5035 (rdiv (COS:s @0) (SIN:s @0))
5036 (rdiv { build_one_cst (type); } (TAN @0)))
5038 /* Simplify sin(x) / tan(x) -> cos(x). */
5040 (rdiv (SIN:s @0) (TAN:s @0))
5041 (if (! HONOR_NANS (@0)
5042 && ! HONOR_INFINITIES (@0))
5045 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5047 (rdiv (TAN:s @0) (SIN:s @0))
5048 (if (! HONOR_NANS (@0)
5049 && ! HONOR_INFINITIES (@0))
5050 (rdiv { build_one_cst (type); } (COS @0))))
5052 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5054 (mult (POW:s @0 @1) (POW:s @0 @2))
5055 (POW @0 (plus @1 @2)))
5057 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5059 (mult (POW:s @0 @1) (POW:s @2 @1))
5060 (POW (mult @0 @2) @1))
5062 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5064 (mult (POWI:s @0 @1) (POWI:s @2 @1))
5065 (POWI (mult @0 @2) @1))
5067 /* Simplify pow(x,c) / x -> pow(x,c-1). */
5069 (rdiv (POW:s @0 REAL_CST@1) @0)
5070 (if (!TREE_OVERFLOW (@1))
5071 (POW @0 (minus @1 { build_one_cst (type); }))))
5073 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5075 (rdiv @0 (POW:s @1 @2))
5076 (mult @0 (POW @1 (negate @2))))
5081 /* sqrt(sqrt(x)) -> pow(x,1/4). */
5084 (pows @0 { build_real (type, dconst_quarter ()); }))
5085 /* sqrt(cbrt(x)) -> pow(x,1/6). */
5088 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5089 /* cbrt(sqrt(x)) -> pow(x,1/6). */
5092 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5093 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
5095 (cbrts (cbrts tree_expr_nonnegative_p@0))
5096 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5097 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
5099 (sqrts (pows @0 @1))
5100 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5101 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
5103 (cbrts (pows tree_expr_nonnegative_p@0 @1))
5104 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5105 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
5107 (pows (sqrts @0) @1)
5108 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5109 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
5111 (pows (cbrts tree_expr_nonnegative_p@0) @1)
5112 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5113 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
5115 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5116 (pows @0 (mult @1 @2))))
5118 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
5120 (CABS (complex @0 @0))
5121 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5123 /* hypot(x,x) -> fabs(x)*sqrt(2). */
5126 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5128 /* cexp(x+yi) -> exp(x)*cexpi(y). */
5133 (cexps compositional_complex@0)
5134 (if (targetm.libc_has_function (function_c99_math_complex))
5136 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5137 (mult @1 (imagpart @2)))))))
5139 (if (canonicalize_math_p ())
5140 /* floor(x) -> trunc(x) if x is nonnegative. */
5141 (for floors (FLOOR_ALL)
5144 (floors tree_expr_nonnegative_p@0)
5147 (match double_value_p
5149 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5150 (for froms (BUILT_IN_TRUNCL
5162 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
5163 (if (optimize && canonicalize_math_p ())
5165 (froms (convert double_value_p@0))
5166 (convert (tos @0)))))
5168 (match float_value_p
5170 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5171 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5172 BUILT_IN_FLOORL BUILT_IN_FLOOR
5173 BUILT_IN_CEILL BUILT_IN_CEIL
5174 BUILT_IN_ROUNDL BUILT_IN_ROUND
5175 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5176 BUILT_IN_RINTL BUILT_IN_RINT)
5177 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5178 BUILT_IN_FLOORF BUILT_IN_FLOORF
5179 BUILT_IN_CEILF BUILT_IN_CEILF
5180 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5181 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5182 BUILT_IN_RINTF BUILT_IN_RINTF)
5183 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5185 (if (optimize && canonicalize_math_p ()
5186 && targetm.libc_has_function (function_c99_misc))
5188 (froms (convert float_value_p@0))
5189 (convert (tos @0)))))
5191 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5192 tos (XFLOOR XCEIL XROUND XRINT)
5193 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
5194 (if (optimize && canonicalize_math_p ())
5196 (froms (convert double_value_p@0))
5199 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5200 XFLOOR XCEIL XROUND XRINT)
5201 tos (XFLOORF XCEILF XROUNDF XRINTF)
5202 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5204 (if (optimize && canonicalize_math_p ())
5206 (froms (convert float_value_p@0))
5209 (if (canonicalize_math_p ())
5210 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
5211 (for floors (IFLOOR LFLOOR LLFLOOR)
5213 (floors tree_expr_nonnegative_p@0)
5216 (if (canonicalize_math_p ())
5217 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
5218 (for fns (IFLOOR LFLOOR LLFLOOR
5220 IROUND LROUND LLROUND)
5222 (fns integer_valued_real_p@0)
5224 (if (!flag_errno_math)
5225 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
5226 (for rints (IRINT LRINT LLRINT)
5228 (rints integer_valued_real_p@0)
5231 (if (canonicalize_math_p ())
5232 (for ifn (IFLOOR ICEIL IROUND IRINT)
5233 lfn (LFLOOR LCEIL LROUND LRINT)
5234 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5235 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5236 sizeof (int) == sizeof (long). */
5237 (if (TYPE_PRECISION (integer_type_node)
5238 == TYPE_PRECISION (long_integer_type_node))
5241 (lfn:long_integer_type_node @0)))
5242 /* Canonicalize llround (x) to lround (x) on LP64 targets where
5243 sizeof (long long) == sizeof (long). */
5244 (if (TYPE_PRECISION (long_long_integer_type_node)
5245 == TYPE_PRECISION (long_integer_type_node))
5248 (lfn:long_integer_type_node @0)))))
5250 /* cproj(x) -> x if we're ignoring infinities. */
5253 (if (!HONOR_INFINITIES (type))
5256 /* If the real part is inf and the imag part is known to be
5257 nonnegative, return (inf + 0i). */
5259 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5260 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5261 { build_complex_inf (type, false); }))
5263 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
5265 (CPROJ (complex @0 REAL_CST@1))
5266 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5267 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5273 (pows @0 REAL_CST@1)
5275 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5276 REAL_VALUE_TYPE tmp;
5279 /* pow(x,0) -> 1. */
5280 (if (real_equal (value, &dconst0))
5281 { build_real (type, dconst1); })
5282 /* pow(x,1) -> x. */
5283 (if (real_equal (value, &dconst1))
5285 /* pow(x,-1) -> 1/x. */
5286 (if (real_equal (value, &dconstm1))
5287 (rdiv { build_real (type, dconst1); } @0))
5288 /* pow(x,0.5) -> sqrt(x). */
5289 (if (flag_unsafe_math_optimizations
5290 && canonicalize_math_p ()
5291 && real_equal (value, &dconsthalf))
5293 /* pow(x,1/3) -> cbrt(x). */
5294 (if (flag_unsafe_math_optimizations
5295 && canonicalize_math_p ()
5296 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5297 real_equal (value, &tmp)))
5300 /* powi(1,x) -> 1. */
5302 (POWI real_onep@0 @1)
5306 (POWI @0 INTEGER_CST@1)
5308 /* powi(x,0) -> 1. */
5309 (if (wi::to_wide (@1) == 0)
5310 { build_real (type, dconst1); })
5311 /* powi(x,1) -> x. */
5312 (if (wi::to_wide (@1) == 1)
5314 /* powi(x,-1) -> 1/x. */
5315 (if (wi::to_wide (@1) == -1)
5316 (rdiv { build_real (type, dconst1); } @0))))
5318 /* Narrowing of arithmetic and logical operations.
5320 These are conceptually similar to the transformations performed for
5321 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
5322 term we want to move all that code out of the front-ends into here. */
5324 /* Convert (outertype)((innertype0)a+(innertype1)b)
5325 into ((newtype)a+(newtype)b) where newtype
5326 is the widest mode from all of these. */
5327 (for op (plus minus mult rdiv)
5329 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5330 /* If we have a narrowing conversion of an arithmetic operation where
5331 both operands are widening conversions from the same type as the outer
5332 narrowing conversion. Then convert the innermost operands to a
5333 suitable unsigned type (to avoid introducing undefined behavior),
5334 perform the operation and convert the result to the desired type. */
5335 (if (INTEGRAL_TYPE_P (type)
5338 /* We check for type compatibility between @0 and @1 below,
5339 so there's no need to check that @2/@4 are integral types. */
5340 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5341 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5342 /* The precision of the type of each operand must match the
5343 precision of the mode of each operand, similarly for the
5345 && type_has_mode_precision_p (TREE_TYPE (@1))
5346 && type_has_mode_precision_p (TREE_TYPE (@2))
5347 && type_has_mode_precision_p (type)
5348 /* The inner conversion must be a widening conversion. */
5349 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5350 && types_match (@1, type)
5351 && (types_match (@1, @2)
5352 /* Or the second operand is const integer or converted const
5353 integer from valueize. */
5354 || TREE_CODE (@2) == INTEGER_CST))
5355 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5356 (op @1 (convert @2))
5357 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5358 (convert (op (convert:utype @1)
5359 (convert:utype @2)))))
5360 (if (FLOAT_TYPE_P (type)
5361 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5362 == DECIMAL_FLOAT_TYPE_P (type))
5363 (with { tree arg0 = strip_float_extensions (@1);
5364 tree arg1 = strip_float_extensions (@2);
5365 tree itype = TREE_TYPE (@0);
5366 tree ty1 = TREE_TYPE (arg0);
5367 tree ty2 = TREE_TYPE (arg1);
5368 enum tree_code code = TREE_CODE (itype); }
5369 (if (FLOAT_TYPE_P (ty1)
5370 && FLOAT_TYPE_P (ty2))
5371 (with { tree newtype = type;
5372 if (TYPE_MODE (ty1) == SDmode
5373 || TYPE_MODE (ty2) == SDmode
5374 || TYPE_MODE (type) == SDmode)
5375 newtype = dfloat32_type_node;
5376 if (TYPE_MODE (ty1) == DDmode
5377 || TYPE_MODE (ty2) == DDmode
5378 || TYPE_MODE (type) == DDmode)
5379 newtype = dfloat64_type_node;
5380 if (TYPE_MODE (ty1) == TDmode
5381 || TYPE_MODE (ty2) == TDmode
5382 || TYPE_MODE (type) == TDmode)
5383 newtype = dfloat128_type_node; }
5384 (if ((newtype == dfloat32_type_node
5385 || newtype == dfloat64_type_node
5386 || newtype == dfloat128_type_node)
5388 && types_match (newtype, type))
5389 (op (convert:newtype @1) (convert:newtype @2))
5390 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5392 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5394 /* Sometimes this transformation is safe (cannot
5395 change results through affecting double rounding
5396 cases) and sometimes it is not. If NEWTYPE is
5397 wider than TYPE, e.g. (float)((long double)double
5398 + (long double)double) converted to
5399 (float)(double + double), the transformation is
5400 unsafe regardless of the details of the types
5401 involved; double rounding can arise if the result
5402 of NEWTYPE arithmetic is a NEWTYPE value half way
5403 between two representable TYPE values but the
5404 exact value is sufficiently different (in the
5405 right direction) for this difference to be
5406 visible in ITYPE arithmetic. If NEWTYPE is the
5407 same as TYPE, however, the transformation may be
5408 safe depending on the types involved: it is safe
5409 if the ITYPE has strictly more than twice as many
5410 mantissa bits as TYPE, can represent infinities
5411 and NaNs if the TYPE can, and has sufficient
5412 exponent range for the product or ratio of two
5413 values representable in the TYPE to be within the
5414 range of normal values of ITYPE. */
5415 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5416 && (flag_unsafe_math_optimizations
5417 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5418 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5420 && !excess_precision_type (newtype)))
5421 && !types_match (itype, newtype))
5422 (convert:type (op (convert:newtype @1)
5423 (convert:newtype @2)))
5428 /* This is another case of narrowing, specifically when there's an outer
5429 BIT_AND_EXPR which masks off bits outside the type of the innermost
5430 operands. Like the previous case we have to convert the operands
5431 to unsigned types to avoid introducing undefined behavior for the
5432 arithmetic operation. */
5433 (for op (minus plus)
5435 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5436 (if (INTEGRAL_TYPE_P (type)
5437 /* We check for type compatibility between @0 and @1 below,
5438 so there's no need to check that @1/@3 are integral types. */
5439 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5440 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5441 /* The precision of the type of each operand must match the
5442 precision of the mode of each operand, similarly for the
5444 && type_has_mode_precision_p (TREE_TYPE (@0))
5445 && type_has_mode_precision_p (TREE_TYPE (@1))
5446 && type_has_mode_precision_p (type)
5447 /* The inner conversion must be a widening conversion. */
5448 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5449 && types_match (@0, @1)
5450 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5451 <= TYPE_PRECISION (TREE_TYPE (@0)))
5452 && (wi::to_wide (@4)
5453 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5454 true, TYPE_PRECISION (type))) == 0)
5455 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5456 (with { tree ntype = TREE_TYPE (@0); }
5457 (convert (bit_and (op @0 @1) (convert:ntype @4))))
5458 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5459 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5460 (convert:utype @4))))))))
5462 /* Transform (@0 < @1 and @0 < @2) to use min,
5463 (@0 > @1 and @0 > @2) to use max */
5464 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5465 op (lt le gt ge lt le gt ge )
5466 ext (min min max max max max min min )
5468 (logic (op:cs @0 @1) (op:cs @0 @2))
5469 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5470 && TREE_CODE (@0) != INTEGER_CST)
5471 (op @0 (ext @1 @2)))))
5474 /* signbit(x) -> 0 if x is nonnegative. */
5475 (SIGNBIT tree_expr_nonnegative_p@0)
5476 { integer_zero_node; })
5479 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
5481 (if (!HONOR_SIGNED_ZEROS (@0))
5482 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
5484 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
5486 (for op (plus minus)
5489 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5490 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5491 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
5492 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
5493 && !TYPE_SATURATING (TREE_TYPE (@0)))
5494 (with { tree res = int_const_binop (rop, @2, @1); }
5495 (if (TREE_OVERFLOW (res)
5496 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5497 { constant_boolean_node (cmp == NE_EXPR, type); }
5498 (if (single_use (@3))
5499 (cmp @0 { TREE_OVERFLOW (res)
5500 ? drop_tree_overflow (res) : res; }))))))))
5501 (for cmp (lt le gt ge)
5502 (for op (plus minus)
5505 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5506 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5507 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5508 (with { tree res = int_const_binop (rop, @2, @1); }
5509 (if (TREE_OVERFLOW (res))
5511 fold_overflow_warning (("assuming signed overflow does not occur "
5512 "when simplifying conditional to constant"),
5513 WARN_STRICT_OVERFLOW_CONDITIONAL);
5514 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
5515 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
5516 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
5517 TYPE_SIGN (TREE_TYPE (@1)))
5518 != (op == MINUS_EXPR);
5519 constant_boolean_node (less == ovf_high, type);
5521 (if (single_use (@3))
5524 fold_overflow_warning (("assuming signed overflow does not occur "
5525 "when changing X +- C1 cmp C2 to "
5527 WARN_STRICT_OVERFLOW_COMPARISON);
5529 (cmp @0 { res; })))))))))
5531 /* Canonicalizations of BIT_FIELD_REFs. */
5534 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5535 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5538 (BIT_FIELD_REF (view_convert @0) @1 @2)
5539 (BIT_FIELD_REF @0 @1 @2))
5542 (BIT_FIELD_REF @0 @1 integer_zerop)
5543 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5547 (BIT_FIELD_REF @0 @1 @2)
5549 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5550 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5552 (if (integer_zerop (@2))
5553 (view_convert (realpart @0)))
5554 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5555 (view_convert (imagpart @0)))))
5556 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5557 && INTEGRAL_TYPE_P (type)
5558 /* On GIMPLE this should only apply to register arguments. */
5559 && (! GIMPLE || is_gimple_reg (@0))
5560 /* A bit-field-ref that referenced the full argument can be stripped. */
5561 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5562 && integer_zerop (@2))
5563 /* Low-parts can be reduced to integral conversions.
5564 ??? The following doesn't work for PDP endian. */
5565 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5566 /* Don't even think about BITS_BIG_ENDIAN. */
5567 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5568 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5569 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5570 ? (TYPE_PRECISION (TREE_TYPE (@0))
5571 - TYPE_PRECISION (type))
5575 /* Simplify vector extracts. */
5578 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5579 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5580 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5581 || (VECTOR_TYPE_P (type)
5582 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5585 tree ctor = (TREE_CODE (@0) == SSA_NAME
5586 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5587 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5588 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5589 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5590 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5593 && (idx % width) == 0
5595 && known_le ((idx + n) / width,
5596 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5601 /* Constructor elements can be subvectors. */
5603 if (CONSTRUCTOR_NELTS (ctor) != 0)
5605 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5606 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5607 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5609 unsigned HOST_WIDE_INT elt, count, const_k;
5612 /* We keep an exact subset of the constructor elements. */
5613 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5614 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5615 { build_constructor (type, NULL); }
5617 (if (elt < CONSTRUCTOR_NELTS (ctor))
5618 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5619 { build_zero_cst (type); })
5620 /* We don't want to emit new CTORs unless the old one goes away.
5621 ??? Eventually allow this if the CTOR ends up constant or
5623 (if (single_use (@0))
5625 vec<constructor_elt, va_gc> *vals;
5626 vec_alloc (vals, count);
5627 for (unsigned i = 0;
5628 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5629 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5630 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5631 build_constructor (type, vals);
5633 /* The bitfield references a single constructor element. */
5634 (if (k.is_constant (&const_k)
5635 && idx + n <= (idx / const_k + 1) * const_k)
5637 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5638 { build_zero_cst (type); })
5640 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5641 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5642 @1 { bitsize_int ((idx % const_k) * width); })))))))))
5644 /* Simplify a bit extraction from a bit insertion for the cases with
5645 the inserted element fully covering the extraction or the insertion
5646 not touching the extraction. */
5648 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5651 unsigned HOST_WIDE_INT isize;
5652 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5653 isize = TYPE_PRECISION (TREE_TYPE (@1));
5655 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5658 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5659 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5660 wi::to_wide (@ipos) + isize))
5661 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
5663 - wi::to_wide (@ipos)); }))
5664 (if (wi::geu_p (wi::to_wide (@ipos),
5665 wi::to_wide (@rpos) + wi::to_wide (@rsize))
5666 || wi::geu_p (wi::to_wide (@rpos),
5667 wi::to_wide (@ipos) + isize))
5668 (BIT_FIELD_REF @0 @rsize @rpos)))))
5670 (if (canonicalize_math_after_vectorization_p ())
5673 (fmas:c (negate @0) @1 @2)
5674 (IFN_FNMA @0 @1 @2))
5676 (fmas @0 @1 (negate @2))
5679 (fmas:c (negate @0) @1 (negate @2))
5680 (IFN_FNMS @0 @1 @2))
5682 (negate (fmas@3 @0 @1 @2))
5683 (if (single_use (@3))
5684 (IFN_FNMS @0 @1 @2))))
5687 (IFN_FMS:c (negate @0) @1 @2)
5688 (IFN_FNMS @0 @1 @2))
5690 (IFN_FMS @0 @1 (negate @2))
5693 (IFN_FMS:c (negate @0) @1 (negate @2))
5694 (IFN_FNMA @0 @1 @2))
5696 (negate (IFN_FMS@3 @0 @1 @2))
5697 (if (single_use (@3))
5698 (IFN_FNMA @0 @1 @2)))
5701 (IFN_FNMA:c (negate @0) @1 @2)
5704 (IFN_FNMA @0 @1 (negate @2))
5705 (IFN_FNMS @0 @1 @2))
5707 (IFN_FNMA:c (negate @0) @1 (negate @2))
5710 (negate (IFN_FNMA@3 @0 @1 @2))
5711 (if (single_use (@3))
5712 (IFN_FMS @0 @1 @2)))
5715 (IFN_FNMS:c (negate @0) @1 @2)
5718 (IFN_FNMS @0 @1 (negate @2))
5719 (IFN_FNMA @0 @1 @2))
5721 (IFN_FNMS:c (negate @0) @1 (negate @2))
5724 (negate (IFN_FNMS@3 @0 @1 @2))
5725 (if (single_use (@3))
5726 (IFN_FMA @0 @1 @2))))
5728 /* POPCOUNT simplifications. */
5729 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5730 BUILT_IN_POPCOUNTIMAX)
5731 /* popcount(X&1) is nop_expr(X&1). */
5734 (if (tree_nonzero_bits (@0) == 1)
5736 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
5738 (plus (popcount:s @0) (popcount:s @1))
5739 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5740 (popcount (bit_ior @0 @1))))
5741 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
5742 (for cmp (le eq ne gt)
5745 (cmp (popcount @0) integer_zerop)
5746 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5749 /* 64- and 32-bits branchless implementations of popcount are detected:
5751 int popcount64c (uint64_t x)
5753 x -= (x >> 1) & 0x5555555555555555ULL;
5754 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
5755 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
5756 return (x * 0x0101010101010101ULL) >> 56;
5759 int popcount32c (uint32_t x)
5761 x -= (x >> 1) & 0x55555555;
5762 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
5763 x = (x + (x >> 4)) & 0x0f0f0f0f;
5764 return (x * 0x01010101) >> 24;
5771 (rshift @8 INTEGER_CST@5)
5773 (bit_and @6 INTEGER_CST@7)
5779 (rshift @0 INTEGER_CST@4)
5786 /* Check constants and optab. */
5789 unsigned prec = TYPE_PRECISION (type);
5790 int shift = 64 - prec;
5791 const unsigned HOST_WIDE_INT c1 = 0x0101010101010101ULL >> shift,
5792 c2 = 0x0F0F0F0F0F0F0F0FULL >> shift,
5793 c3 = 0x3333333333333333ULL >> shift,
5794 c4 = 0x5555555555555555ULL >> shift;
5796 (if (prec <= 64 && TYPE_UNSIGNED (type) && tree_to_uhwi (@4) == 1
5797 && tree_to_uhwi (@10) == 2 && tree_to_uhwi (@5) == 4
5798 && tree_to_uhwi (@1) == prec - 8 && tree_to_uhwi (@2) == c1
5799 && tree_to_uhwi (@3) == c2 && tree_to_uhwi (@9) == c3
5800 && tree_to_uhwi (@7) == c3 && tree_to_uhwi (@11) == c4
5801 && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
5803 (convert (IFN_POPCOUNT:type @0)))))
5813 r = c ? a1 op a2 : b;
5815 if the target can do it in one go. This makes the operation conditional
5816 on c, so could drop potentially-trapping arithmetic, but that's a valid
5817 simplification if the result of the operation isn't needed.
5819 Avoid speculatively generating a stand-alone vector comparison
5820 on targets that might not support them. Any target implementing
5821 conditional internal functions must support the same comparisons
5822 inside and outside a VEC_COND_EXPR. */
5825 (for uncond_op (UNCOND_BINARY)
5826 cond_op (COND_BINARY)
5828 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5829 (with { tree op_type = TREE_TYPE (@4); }
5830 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5831 && element_precision (type) == element_precision (op_type))
5832 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5834 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5835 (with { tree op_type = TREE_TYPE (@4); }
5836 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5837 && element_precision (type) == element_precision (op_type))
5838 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5840 /* Same for ternary operations. */
5841 (for uncond_op (UNCOND_TERNARY)
5842 cond_op (COND_TERNARY)
5844 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5845 (with { tree op_type = TREE_TYPE (@5); }
5846 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5847 && element_precision (type) == element_precision (op_type))
5848 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5850 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5851 (with { tree op_type = TREE_TYPE (@5); }
5852 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5853 && element_precision (type) == element_precision (op_type))
5854 (view_convert (cond_op (bit_not @0) @2 @3 @4
5855 (view_convert:op_type @1)))))))
5858 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5859 "else" value of an IFN_COND_*. */
5860 (for cond_op (COND_BINARY)
5862 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5863 (with { tree op_type = TREE_TYPE (@3); }
5864 (if (element_precision (type) == element_precision (op_type))
5865 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5867 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5868 (with { tree op_type = TREE_TYPE (@5); }
5869 (if (inverse_conditions_p (@0, @2)
5870 && element_precision (type) == element_precision (op_type))
5871 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
5873 /* Same for ternary operations. */
5874 (for cond_op (COND_TERNARY)
5876 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5877 (with { tree op_type = TREE_TYPE (@4); }
5878 (if (element_precision (type) == element_precision (op_type))
5879 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5881 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5882 (with { tree op_type = TREE_TYPE (@6); }
5883 (if (inverse_conditions_p (@0, @2)
5884 && element_precision (type) == element_precision (op_type))
5885 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5887 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5890 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5891 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5893 If pointers are known not to wrap, B checks whether @1 bytes starting
5894 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5895 bytes. A is more efficiently tested as:
5897 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5899 The equivalent expression for B is given by replacing @1 with @1 - 1:
5901 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5903 @0 and @2 can be swapped in both expressions without changing the result.
5905 The folds rely on sizetype's being unsigned (which is always true)
5906 and on its being the same width as the pointer (which we have to check).
5908 The fold replaces two pointer_plus expressions, two comparisons and
5909 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5910 the best case it's a saving of two operations. The A fold retains one
5911 of the original pointer_pluses, so is a win even if both pointer_pluses
5912 are used elsewhere. The B fold is a wash if both pointer_pluses are
5913 used elsewhere, since all we end up doing is replacing a comparison with
5914 a pointer_plus. We do still apply the fold under those circumstances
5915 though, in case applying it to other conditions eventually makes one of the
5916 pointer_pluses dead. */
5917 (for ior (truth_orif truth_or bit_ior)
5920 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5921 (cmp:cs (pointer_plus@4 @2 @1) @0))
5922 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5923 && TYPE_OVERFLOW_WRAPS (sizetype)
5924 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5925 /* Calculate the rhs constant. */
5926 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5927 offset_int rhs = off * 2; }
5928 /* Always fails for negative values. */
5929 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5930 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5931 pick a canonical order. This increases the chances of using the
5932 same pointer_plus in multiple checks. */
5933 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5934 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5935 (if (cmp == LT_EXPR)
5936 (gt (convert:sizetype
5937 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5938 { swap_p ? @0 : @2; }))
5940 (gt (convert:sizetype
5941 (pointer_diff:ssizetype
5942 (pointer_plus { swap_p ? @2 : @0; }
5943 { wide_int_to_tree (sizetype, off); })
5944 { swap_p ? @0 : @2; }))
5945 { rhs_tree; })))))))))
5947 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
5949 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
5950 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
5951 (with { int i = single_nonzero_element (@1); }
5953 (with { tree elt = vector_cst_elt (@1, i);
5954 tree elt_type = TREE_TYPE (elt);
5955 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
5956 tree size = bitsize_int (elt_bits);
5957 tree pos = bitsize_int (elt_bits * i); }
5960 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
5964 (vec_perm @0 @1 VECTOR_CST@2)
5967 tree op0 = @0, op1 = @1, op2 = @2;
5969 /* Build a vector of integers from the tree mask. */
5970 vec_perm_builder builder;
5971 if (!tree_to_vec_perm_builder (&builder, op2))
5974 /* Create a vec_perm_indices for the integer vector. */
5975 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
5976 bool single_arg = (op0 == op1);
5977 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
5979 (if (sel.series_p (0, 1, 0, 1))
5981 (if (sel.series_p (0, 1, nelts, 1))
5987 if (sel.all_from_input_p (0))
5989 else if (sel.all_from_input_p (1))
5992 sel.rotate_inputs (1);
5994 else if (known_ge (poly_uint64 (sel[0]), nelts))
5996 std::swap (op0, op1);
5997 sel.rotate_inputs (1);
6001 tree cop0 = op0, cop1 = op1;
6002 if (TREE_CODE (op0) == SSA_NAME
6003 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6004 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6005 cop0 = gimple_assign_rhs1 (def);
6006 if (TREE_CODE (op1) == SSA_NAME
6007 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6008 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6009 cop1 = gimple_assign_rhs1 (def);
6013 (if ((TREE_CODE (cop0) == VECTOR_CST
6014 || TREE_CODE (cop0) == CONSTRUCTOR)
6015 && (TREE_CODE (cop1) == VECTOR_CST
6016 || TREE_CODE (cop1) == CONSTRUCTOR)
6017 && (t = fold_vec_perm (type, cop0, cop1, sel)))
6021 bool changed = (op0 == op1 && !single_arg);
6022 tree ins = NULL_TREE;
6025 /* See if the permutation is performing a single element
6026 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6027 in that case. But only if the vector mode is supported,
6028 otherwise this is invalid GIMPLE. */
6029 if (TYPE_MODE (type) != BLKmode
6030 && (TREE_CODE (cop0) == VECTOR_CST
6031 || TREE_CODE (cop0) == CONSTRUCTOR
6032 || TREE_CODE (cop1) == VECTOR_CST
6033 || TREE_CODE (cop1) == CONSTRUCTOR))
6035 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6038 /* After canonicalizing the first elt to come from the
6039 first vector we only can insert the first elt from
6040 the first vector. */
6042 if ((ins = fold_read_from_vector (cop0, sel[0])))
6045 /* The above can fail for two-element vectors which always
6046 appear to insert the first element, so try inserting
6047 into the second lane as well. For more than two
6048 elements that's wasted time. */
6049 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6051 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6052 for (at = 0; at < encoded_nelts; ++at)
6053 if (maybe_ne (sel[at], at))
6055 if (at < encoded_nelts
6056 && (known_eq (at + 1, nelts)
6057 || sel.series_p (at + 1, 1, at + 1, 1)))
6059 if (known_lt (poly_uint64 (sel[at]), nelts))
6060 ins = fold_read_from_vector (cop0, sel[at]);
6062 ins = fold_read_from_vector (cop1, sel[at] - nelts);
6067 /* Generate a canonical form of the selector. */
6068 if (!ins && sel.encoding () != builder)
6070 /* Some targets are deficient and fail to expand a single
6071 argument permutation while still allowing an equivalent
6072 2-argument version. */
6074 if (sel.ninputs () == 2
6075 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6076 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6079 vec_perm_indices sel2 (builder, 2, nelts);
6080 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6081 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6083 /* Not directly supported with either encoding,
6084 so use the preferred form. */
6085 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6087 if (!operand_equal_p (op2, oldop2, 0))
6092 (bit_insert { op0; } { ins; }
6093 { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); })
6095 (vec_perm { op0; } { op1; } { op2; }))))))))))
6097 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
6099 (match vec_same_elem_p
6101 (if (uniform_vector_p (@0))))
6103 (match vec_same_elem_p
6107 (vec_perm vec_same_elem_p@0 @0 @1)