1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.cc
3 and generic-match.cc from it.
5 Copyright (C) 2014-2022 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
42 bitmask_inv_cst_vector_p)
45 (define_operator_list tcc_comparison
46 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison lt le eq ne ge gt)
54 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
56 #include "cfn-operators.pd"
58 /* Define operand lists for math rounding functions {,i,l,ll}FN,
59 where the versions prefixed with "i" return an int, those prefixed with
60 "l" return a long and those prefixed with "ll" return a long long.
62 Also define operand lists:
64 X<FN>F for all float functions, in the order i, l, ll
65 X<FN> for all double functions, in the same order
66 X<FN>L for all long double functions, in the same order. */
67 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
68 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71 (define_operator_list X##FN BUILT_IN_I##FN \
74 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
78 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
83 /* Unary operations and their associated IFN_COND_* function. */
84 (define_operator_list UNCOND_UNARY
86 (define_operator_list COND_UNARY
89 /* Binary operations and their associated IFN_COND_* function. */
90 (define_operator_list UNCOND_BINARY
92 mult trunc_div trunc_mod rdiv
95 bit_and bit_ior bit_xor
97 (define_operator_list COND_BINARY
98 IFN_COND_ADD IFN_COND_SUB
99 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
100 IFN_COND_MIN IFN_COND_MAX
101 IFN_COND_FMIN IFN_COND_FMAX
102 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
103 IFN_COND_SHL IFN_COND_SHR)
105 /* Same for ternary operations. */
106 (define_operator_list UNCOND_TERNARY
107 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
108 (define_operator_list COND_TERNARY
109 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
111 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
112 (define_operator_list ATOMIC_FETCH_OR_XOR_N
113 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
114 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
115 BUILT_IN_ATOMIC_FETCH_OR_16
116 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
117 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
118 BUILT_IN_ATOMIC_FETCH_XOR_16
119 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
120 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
121 BUILT_IN_ATOMIC_XOR_FETCH_16)
122 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
123 (define_operator_list SYNC_FETCH_OR_XOR_N
124 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
125 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
126 BUILT_IN_SYNC_FETCH_AND_OR_16
127 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
128 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
129 BUILT_IN_SYNC_FETCH_AND_XOR_16
130 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
131 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
132 BUILT_IN_SYNC_XOR_AND_FETCH_16)
133 /* __atomic_fetch_and_*. */
134 (define_operator_list ATOMIC_FETCH_AND_N
135 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
136 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
137 BUILT_IN_ATOMIC_FETCH_AND_16)
138 /* __sync_fetch_and_and_*. */
139 (define_operator_list SYNC_FETCH_AND_AND_N
140 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
141 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
142 BUILT_IN_SYNC_FETCH_AND_AND_16)
144 /* With nop_convert? combine convert? and view_convert? in one pattern
145 plus conditionalize on tree_nop_conversion_p conversions. */
146 (match (nop_convert @0)
148 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
149 (match (nop_convert @0)
151 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
152 && known_eq (TYPE_VECTOR_SUBPARTS (type),
153 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
154 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
156 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
157 ABSU_EXPR returns unsigned absolute value of the operand and the operand
158 of the ABSU_EXPR will have the corresponding signed type. */
159 (simplify (abs (convert @0))
160 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
161 && !TYPE_UNSIGNED (TREE_TYPE (@0))
162 && element_precision (type) > element_precision (TREE_TYPE (@0)))
163 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
164 (convert (absu:utype @0)))))
167 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
169 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
170 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
171 && !TYPE_UNSIGNED (TREE_TYPE (@0))
172 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
176 /* Simplifications of operations with one constant operand and
177 simplifications to constants or single values. */
179 (for op (plus pointer_plus minus bit_ior bit_xor)
181 (op @0 integer_zerop)
184 /* 0 +p index -> (type)index */
186 (pointer_plus integer_zerop @1)
187 (non_lvalue (convert @1)))
189 /* ptr - 0 -> (type)ptr */
191 (pointer_diff @0 integer_zerop)
194 /* See if ARG1 is zero and X + ARG1 reduces to X.
195 Likewise if the operands are reversed. */
197 (plus:c @0 real_zerop@1)
198 (if (fold_real_zero_addition_p (type, @0, @1, 0))
201 /* See if ARG1 is zero and X - ARG1 reduces to X. */
203 (minus @0 real_zerop@1)
204 (if (fold_real_zero_addition_p (type, @0, @1, 1))
207 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
208 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
209 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
210 if not -frounding-math. For sNaNs the first operation would raise
211 exceptions but turn the result into qNan, so the second operation
212 would not raise it. */
213 (for inner_op (plus minus)
214 (for outer_op (plus minus)
216 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
219 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
220 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
221 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
223 = ((outer_op == PLUS_EXPR)
224 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
225 (if (outer_plus && !inner_plus)
230 This is unsafe for certain floats even in non-IEEE formats.
231 In IEEE, it is unsafe because it does wrong for NaNs.
232 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
233 Also note that operand_equal_p is always false if an operand
237 (if (!FLOAT_TYPE_P (type)
238 || (!tree_expr_maybe_nan_p (@0)
239 && !tree_expr_maybe_infinite_p (@0)
240 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
241 || !HONOR_SIGNED_ZEROS (type))))
242 { build_zero_cst (type); }))
244 (pointer_diff @@0 @0)
245 { build_zero_cst (type); })
248 (mult @0 integer_zerop@1)
251 /* -x == x -> x == 0 */
254 (cmp:c @0 (negate @0))
255 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
256 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
257 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
259 /* Maybe fold x * 0 to 0. The expressions aren't the same
260 when x is NaN, since x * 0 is also NaN. Nor are they the
261 same in modes with signed zeros, since multiplying a
262 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
263 since x * 0 is NaN. */
265 (mult @0 real_zerop@1)
266 (if (!tree_expr_maybe_nan_p (@0)
267 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
268 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
271 /* In IEEE floating point, x*1 is not equivalent to x for snans.
272 Likewise for complex arithmetic with signed zeros. */
275 (if (!tree_expr_maybe_signaling_nan_p (@0)
276 && (!HONOR_SIGNED_ZEROS (type)
277 || !COMPLEX_FLOAT_TYPE_P (type)))
280 /* Transform x * -1.0 into -x. */
282 (mult @0 real_minus_onep)
283 (if (!tree_expr_maybe_signaling_nan_p (@0)
284 && (!HONOR_SIGNED_ZEROS (type)
285 || !COMPLEX_FLOAT_TYPE_P (type)))
288 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
289 unless the target has native support for the former but not the latter. */
291 (mult @0 VECTOR_CST@1)
292 (if (initializer_each_zero_or_onep (@1)
293 && !HONOR_SNANS (type)
294 && !HONOR_SIGNED_ZEROS (type))
295 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
297 && (!VECTOR_MODE_P (TYPE_MODE (type))
298 || (VECTOR_MODE_P (TYPE_MODE (itype))
299 && optab_handler (and_optab,
300 TYPE_MODE (itype)) != CODE_FOR_nothing)))
301 (view_convert (bit_and:itype (view_convert @0)
302 (ne @1 { build_zero_cst (type); })))))))
304 (for cmp (gt ge lt le)
305 outp (convert convert negate negate)
306 outn (negate negate convert convert)
307 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
308 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
309 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
310 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
312 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
313 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
315 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
316 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
317 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
318 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
320 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
321 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
324 /* Transform X * copysign (1.0, X) into abs(X). */
326 (mult:c @0 (COPYSIGN_ALL real_onep @0))
327 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
330 /* Transform X * copysign (1.0, -X) into -abs(X). */
332 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
333 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
336 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
338 (COPYSIGN_ALL REAL_CST@0 @1)
339 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
340 (COPYSIGN_ALL (negate @0) @1)))
342 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
344 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
347 /* X * 1, X / 1 -> X. */
348 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
353 /* (A / (1 << B)) -> (A >> B).
354 Only for unsigned A. For signed A, this would not preserve rounding
356 For example: (-1 / ( 1 << B)) != -1 >> B.
357 Also handle widening conversions, like:
358 (A / (unsigned long long) (1U << B)) -> (A >> B)
360 (A / (unsigned long long) (1 << B)) -> (A >> B).
361 If the left shift is signed, it can be done only if the upper bits
362 of A starting from shift's type sign bit are zero, as
363 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
364 so it is valid only if A >> 31 is zero. */
366 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
367 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
368 && (!VECTOR_TYPE_P (type)
369 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
370 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
371 && (useless_type_conversion_p (type, TREE_TYPE (@1))
372 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
373 && (TYPE_UNSIGNED (TREE_TYPE (@1))
374 || (element_precision (type)
375 == element_precision (TREE_TYPE (@1)))
376 || (INTEGRAL_TYPE_P (type)
377 && (tree_nonzero_bits (@0)
378 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
380 element_precision (type))) == 0)))))
381 (if (!VECTOR_TYPE_P (type)
382 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
383 && element_precision (TREE_TYPE (@3)) < element_precision (type))
384 (convert (rshift @3 @2))
387 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
388 undefined behavior in constexpr evaluation, and assuming that the division
389 traps enables better optimizations than these anyway. */
390 (for div (trunc_div ceil_div floor_div round_div exact_div)
391 /* 0 / X is always zero. */
393 (div integer_zerop@0 @1)
394 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
395 (if (!integer_zerop (@1))
399 (div @0 integer_minus_onep@1)
400 (if (!TYPE_UNSIGNED (type))
402 /* X / bool_range_Y is X. */
405 (if (INTEGRAL_TYPE_P (type)
406 && ssa_name_has_boolean_range (@1)
407 && !flag_non_call_exceptions)
412 /* But not for 0 / 0 so that we can get the proper warnings and errors.
413 And not for _Fract types where we can't build 1. */
414 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
415 && !integer_zerop (@0)
416 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
417 { build_one_cst (type); }))
418 /* X / abs (X) is X < 0 ? -1 : 1. */
421 (if (INTEGRAL_TYPE_P (type)
422 && TYPE_OVERFLOW_UNDEFINED (type)
423 && !integer_zerop (@0)
424 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
425 (cond (lt @0 { build_zero_cst (type); })
426 { build_minus_one_cst (type); } { build_one_cst (type); })))
429 (div:C @0 (negate @0))
430 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
431 && TYPE_OVERFLOW_UNDEFINED (type)
432 && !integer_zerop (@0)
433 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
434 { build_minus_one_cst (type); })))
436 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
437 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
438 for MOD instead of DIV. */
439 (for floor_divmod (floor_div floor_mod)
440 trunc_divmod (trunc_div trunc_mod)
443 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
444 && TYPE_UNSIGNED (type))
445 (trunc_divmod @0 @1))))
447 /* 1 / X -> X == 1 for unsigned integer X.
448 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
449 But not for 1 / 0 so that we can get proper warnings and errors,
450 and not for 1-bit integers as they are edge cases better handled
453 (trunc_div integer_onep@0 @1)
454 (if (INTEGRAL_TYPE_P (type)
455 && TYPE_PRECISION (type) > 1
456 && !integer_zerop (@1)
457 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
458 (if (TYPE_UNSIGNED (type))
459 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
460 (with { tree utype = unsigned_type_for (type); }
461 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
462 { build_int_cst (utype, 2); })
463 @1 { build_zero_cst (type); })))))
465 /* Combine two successive divisions. Note that combining ceil_div
466 and floor_div is trickier and combining round_div even more so. */
467 (for div (trunc_div exact_div)
469 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
471 wi::overflow_type overflow;
472 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
473 TYPE_SIGN (type), &overflow);
475 (if (div == EXACT_DIV_EXPR
476 || optimize_successive_divisions_p (@2, @3))
478 (div @0 { wide_int_to_tree (type, mul); })
479 (if (TYPE_UNSIGNED (type)
480 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
481 { build_zero_cst (type); }))))))
483 /* Combine successive multiplications. Similar to above, but handling
484 overflow is different. */
486 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
488 wi::overflow_type overflow;
489 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
490 TYPE_SIGN (type), &overflow);
492 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
493 otherwise undefined overflow implies that @0 must be zero. */
494 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
495 (mult @0 { wide_int_to_tree (type, mul); }))))
497 /* Similar to above, but there could be an extra add/sub between
498 successive multuiplications. */
500 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
502 bool overflowed = true;
503 wi::overflow_type ovf1, ovf2;
504 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
505 TYPE_SIGN (type), &ovf1);
506 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
507 TYPE_SIGN (type), &ovf2);
508 if (TYPE_OVERFLOW_UNDEFINED (type))
512 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
513 && get_global_range_query ()->range_of_expr (vr0, @4)
514 && vr0.kind () == VR_RANGE)
516 wide_int wmin0 = vr0.lower_bound ();
517 wide_int wmax0 = vr0.upper_bound ();
518 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
519 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
520 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
522 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
523 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
524 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
533 /* Skip folding on overflow. */
535 (plus (mult @0 { wide_int_to_tree (type, mul); })
536 { wide_int_to_tree (type, add); }))))
538 /* Similar to above, but a multiplication between successive additions. */
540 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
542 bool overflowed = true;
543 wi::overflow_type ovf1;
544 wi::overflow_type ovf2;
545 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
546 TYPE_SIGN (type), &ovf1);
547 wide_int add = wi::add (mul, wi::to_wide (@3),
548 TYPE_SIGN (type), &ovf2);
549 if (TYPE_OVERFLOW_UNDEFINED (type))
553 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
554 && get_global_range_query ()->range_of_expr (vr0, @0)
555 && vr0.kind () == VR_RANGE)
557 wide_int wmin0 = vr0.lower_bound ();
558 wide_int wmax0 = vr0.upper_bound ();
559 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
560 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
561 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
563 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
564 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
565 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
574 /* Skip folding on overflow. */
576 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
578 /* Optimize A / A to 1.0 if we don't care about
579 NaNs or Infinities. */
582 (if (FLOAT_TYPE_P (type)
583 && ! HONOR_NANS (type)
584 && ! HONOR_INFINITIES (type))
585 { build_one_cst (type); }))
587 /* Optimize -A / A to -1.0 if we don't care about
588 NaNs or Infinities. */
590 (rdiv:C @0 (negate @0))
591 (if (FLOAT_TYPE_P (type)
592 && ! HONOR_NANS (type)
593 && ! HONOR_INFINITIES (type))
594 { build_minus_one_cst (type); }))
596 /* PR71078: x / abs(x) -> copysign (1.0, x) */
598 (rdiv:C (convert? @0) (convert? (abs @0)))
599 (if (SCALAR_FLOAT_TYPE_P (type)
600 && ! HONOR_NANS (type)
601 && ! HONOR_INFINITIES (type))
603 (if (types_match (type, float_type_node))
604 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
605 (if (types_match (type, double_type_node))
606 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
607 (if (types_match (type, long_double_type_node))
608 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
610 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
613 (if (!tree_expr_maybe_signaling_nan_p (@0))
616 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
618 (rdiv @0 real_minus_onep)
619 (if (!tree_expr_maybe_signaling_nan_p (@0))
622 (if (flag_reciprocal_math)
623 /* Convert (A/B)/C to A/(B*C). */
625 (rdiv (rdiv:s @0 @1) @2)
626 (rdiv @0 (mult @1 @2)))
628 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
630 (rdiv @0 (mult:s @1 REAL_CST@2))
632 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
634 (rdiv (mult @0 { tem; } ) @1))))
636 /* Convert A/(B/C) to (A/B)*C */
638 (rdiv @0 (rdiv:s @1 @2))
639 (mult (rdiv @0 @1) @2)))
641 /* Simplify x / (- y) to -x / y. */
643 (rdiv @0 (negate @1))
644 (rdiv (negate @0) @1))
646 (if (flag_unsafe_math_optimizations)
647 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
648 Since C / x may underflow to zero, do this only for unsafe math. */
649 (for op (lt le gt ge)
652 (op (rdiv REAL_CST@0 @1) real_zerop@2)
653 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
655 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
657 /* For C < 0, use the inverted operator. */
658 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
661 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
662 (for div (trunc_div ceil_div floor_div round_div exact_div)
664 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
665 (if (integer_pow2p (@2)
666 && tree_int_cst_sgn (@2) > 0
667 && tree_nop_conversion_p (type, TREE_TYPE (@0))
668 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
670 { build_int_cst (integer_type_node,
671 wi::exact_log2 (wi::to_wide (@2))); }))))
673 /* If ARG1 is a constant, we can convert this to a multiply by the
674 reciprocal. This does not have the same rounding properties,
675 so only do this if -freciprocal-math. We can actually
676 always safely do it if ARG1 is a power of two, but it's hard to
677 tell if it is or not in a portable manner. */
678 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
682 (if (flag_reciprocal_math
685 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
687 (mult @0 { tem; } )))
688 (if (cst != COMPLEX_CST)
689 (with { tree inverse = exact_inverse (type, @1); }
691 (mult @0 { inverse; } ))))))))
693 (for mod (ceil_mod floor_mod round_mod trunc_mod)
694 /* 0 % X is always zero. */
696 (mod integer_zerop@0 @1)
697 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
698 (if (!integer_zerop (@1))
700 /* X % 1 is always zero. */
702 (mod @0 integer_onep)
703 { build_zero_cst (type); })
704 /* X % -1 is zero. */
706 (mod @0 integer_minus_onep@1)
707 (if (!TYPE_UNSIGNED (type))
708 { build_zero_cst (type); }))
712 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
713 (if (!integer_zerop (@0))
714 { build_zero_cst (type); }))
715 /* (X % Y) % Y is just X % Y. */
717 (mod (mod@2 @0 @1) @1)
719 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
721 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
722 (if (ANY_INTEGRAL_TYPE_P (type)
723 && TYPE_OVERFLOW_UNDEFINED (type)
724 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
726 { build_zero_cst (type); }))
727 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
728 modulo and comparison, since it is simpler and equivalent. */
731 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
732 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
733 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
734 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
736 /* X % -C is the same as X % C. */
738 (trunc_mod @0 INTEGER_CST@1)
739 (if (TYPE_SIGN (type) == SIGNED
740 && !TREE_OVERFLOW (@1)
741 && wi::neg_p (wi::to_wide (@1))
742 && !TYPE_OVERFLOW_TRAPS (type)
743 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
744 && !sign_bit_p (@1, @1))
745 (trunc_mod @0 (negate @1))))
747 /* X % -Y is the same as X % Y. */
749 (trunc_mod @0 (convert? (negate @1)))
750 (if (INTEGRAL_TYPE_P (type)
751 && !TYPE_UNSIGNED (type)
752 && !TYPE_OVERFLOW_TRAPS (type)
753 && tree_nop_conversion_p (type, TREE_TYPE (@1))
754 /* Avoid this transformation if X might be INT_MIN or
755 Y might be -1, because we would then change valid
756 INT_MIN % -(-1) into invalid INT_MIN % -1. */
757 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
758 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
760 (trunc_mod @0 (convert @1))))
762 /* X - (X / Y) * Y is the same as X % Y. */
764 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
765 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
766 (convert (trunc_mod @0 @1))))
768 /* x * (1 + y / x) - y -> x - y % x */
770 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
771 (if (INTEGRAL_TYPE_P (type))
772 (minus @0 (trunc_mod @1 @0))))
774 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
775 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
776 Also optimize A % (C << N) where C is a power of 2,
777 to A & ((C << N) - 1).
778 Also optimize "A shift (B % C)", if C is a power of 2, to
779 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
780 and assume (B % C) is nonnegative as shifts negative values would
782 (match (power_of_two_cand @1)
784 (match (power_of_two_cand @1)
785 (lshift INTEGER_CST@1 @2))
786 (for mod (trunc_mod floor_mod)
787 (for shift (lshift rshift)
789 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
790 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
791 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
794 (mod @0 (convert? (power_of_two_cand@1 @2)))
795 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
796 /* Allow any integral conversions of the divisor, except
797 conversion from narrower signed to wider unsigned type
798 where if @1 would be negative power of two, the divisor
799 would not be a power of two. */
800 && INTEGRAL_TYPE_P (type)
801 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
802 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
803 || TYPE_UNSIGNED (TREE_TYPE (@1))
804 || !TYPE_UNSIGNED (type))
805 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
806 (with { tree utype = TREE_TYPE (@1);
807 if (!TYPE_OVERFLOW_WRAPS (utype))
808 utype = unsigned_type_for (utype); }
809 (bit_and @0 (convert (minus (convert:utype @1)
810 { build_one_cst (utype); })))))))
812 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
814 (trunc_div (mult @0 integer_pow2p@1) @1)
815 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
816 (bit_and @0 { wide_int_to_tree
817 (type, wi::mask (TYPE_PRECISION (type)
818 - wi::exact_log2 (wi::to_wide (@1)),
819 false, TYPE_PRECISION (type))); })))
821 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
823 (mult (trunc_div @0 integer_pow2p@1) @1)
824 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
825 (bit_and @0 (negate @1))))
827 /* Simplify (t * 2) / 2) -> t. */
828 (for div (trunc_div ceil_div floor_div round_div exact_div)
830 (div (mult:c @0 @1) @1)
831 (if (ANY_INTEGRAL_TYPE_P (type))
832 (if (TYPE_OVERFLOW_UNDEFINED (type))
837 bool overflowed = true;
838 value_range vr0, vr1;
839 if (INTEGRAL_TYPE_P (type)
840 && get_global_range_query ()->range_of_expr (vr0, @0)
841 && get_global_range_query ()->range_of_expr (vr1, @1)
842 && vr0.kind () == VR_RANGE
843 && vr1.kind () == VR_RANGE)
845 wide_int wmin0 = vr0.lower_bound ();
846 wide_int wmax0 = vr0.upper_bound ();
847 wide_int wmin1 = vr1.lower_bound ();
848 wide_int wmax1 = vr1.upper_bound ();
849 /* If the multiplication can't overflow/wrap around, then
850 it can be optimized too. */
851 wi::overflow_type min_ovf, max_ovf;
852 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
853 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
854 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
856 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
857 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
858 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
869 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
874 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
877 (pows (op @0) REAL_CST@1)
878 (with { HOST_WIDE_INT n; }
879 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
881 /* Likewise for powi. */
884 (pows (op @0) INTEGER_CST@1)
885 (if ((wi::to_wide (@1) & 1) == 0)
887 /* Strip negate and abs from both operands of hypot. */
895 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
896 (for copysigns (COPYSIGN_ALL)
898 (copysigns (op @0) @1)
901 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
906 /* Convert absu(x)*absu(x) -> x*x. */
908 (mult (absu@1 @0) @1)
909 (mult (convert@2 @0) @2))
911 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
915 (coss (copysigns @0 @1))
918 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
922 (pows (copysigns @0 @2) REAL_CST@1)
923 (with { HOST_WIDE_INT n; }
924 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
926 /* Likewise for powi. */
930 (pows (copysigns @0 @2) INTEGER_CST@1)
931 (if ((wi::to_wide (@1) & 1) == 0)
936 /* hypot(copysign(x, y), z) -> hypot(x, z). */
938 (hypots (copysigns @0 @1) @2)
940 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
942 (hypots @0 (copysigns @1 @2))
945 /* copysign(x, CST) -> [-]abs (x). */
946 (for copysigns (COPYSIGN_ALL)
948 (copysigns @0 REAL_CST@1)
949 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
953 /* copysign(copysign(x, y), z) -> copysign(x, z). */
954 (for copysigns (COPYSIGN_ALL)
956 (copysigns (copysigns @0 @1) @2)
959 /* copysign(x,y)*copysign(x,y) -> x*x. */
960 (for copysigns (COPYSIGN_ALL)
962 (mult (copysigns@2 @0 @1) @2)
965 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
966 (for ccoss (CCOS CCOSH)
971 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
972 (for ops (conj negate)
978 /* Fold (a * (1 << b)) into (a << b) */
980 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
981 (if (! FLOAT_TYPE_P (type)
982 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
985 /* Shifts by constants distribute over several binary operations,
986 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
989 (op (lshift:s @0 @1) (lshift:s @2 @1))
990 (if (INTEGRAL_TYPE_P (type)
991 && TYPE_OVERFLOW_WRAPS (type)
992 && !TYPE_SATURATING (type))
993 (lshift (op @0 @2) @1))))
995 (for op (bit_and bit_ior bit_xor)
997 (op (lshift:s @0 @1) (lshift:s @2 @1))
998 (if (INTEGRAL_TYPE_P (type))
999 (lshift (op @0 @2) @1)))
1001 (op (rshift:s @0 @1) (rshift:s @2 @1))
1002 (if (INTEGRAL_TYPE_P (type))
1003 (rshift (op @0 @2) @1))))
1005 /* Fold (1 << (C - x)) where C = precision(type) - 1
1006 into ((1 << C) >> x). */
1008 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1009 (if (INTEGRAL_TYPE_P (type)
1010 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1012 (if (TYPE_UNSIGNED (type))
1013 (rshift (lshift @0 @2) @3)
1015 { tree utype = unsigned_type_for (type); }
1016 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1018 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1020 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1021 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1022 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1023 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1024 (bit_and (convert @0)
1025 { wide_int_to_tree (type,
1026 wi::lshift (wone, wi::to_wide (@2))); }))))
1028 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1029 (for cst (INTEGER_CST VECTOR_CST)
1031 (rshift (negate:s @0) cst@1)
1032 (if (!TYPE_UNSIGNED (type)
1033 && TYPE_OVERFLOW_UNDEFINED (type))
1034 (with { tree stype = TREE_TYPE (@1);
1035 tree bt = truth_type_for (type);
1036 tree zeros = build_zero_cst (type);
1037 tree cst = NULL_TREE; }
1039 /* Handle scalar case. */
1040 (if (INTEGRAL_TYPE_P (type)
1041 /* If we apply the rule to the scalar type before vectorization
1042 we will enforce the result of the comparison being a bool
1043 which will require an extra AND on the result that will be
1044 indistinguishable from when the user did actually want 0
1045 or 1 as the result so it can't be removed. */
1046 && canonicalize_math_after_vectorization_p ()
1047 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1048 (negate (convert (gt @0 { zeros; }))))
1049 /* Handle vector case. */
1050 (if (VECTOR_INTEGER_TYPE_P (type)
1051 /* First check whether the target has the same mode for vector
1052 comparison results as it's operands do. */
1053 && TYPE_MODE (bt) == TYPE_MODE (type)
1054 /* Then check to see if the target is able to expand the comparison
1055 with the given type later on, otherwise we may ICE. */
1056 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1057 && (cst = uniform_integer_cst_p (@1)) != NULL
1058 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1059 (view_convert (gt:bt @0 { zeros; }))))))))
1061 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1063 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1064 (if (flag_associative_math
1067 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1069 (rdiv { tem; } @1)))))
1071 /* Simplify ~X & X as zero. */
1073 (bit_and:c (convert? @0) (convert? (bit_not @0)))
1074 { build_zero_cst (type); })
1076 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1078 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1079 (if (TYPE_UNSIGNED (type))
1080 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1082 (for bitop (bit_and bit_ior)
1084 /* PR35691: Transform
1085 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1086 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1088 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1089 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1090 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1091 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1092 (cmp (bit_ior @0 (convert @1)) @2)))
1094 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1095 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1097 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1098 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1099 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1100 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1101 (cmp (bit_and @0 (convert @1)) @2))))
1103 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1105 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1106 (minus (bit_xor @0 @1) @1))
1108 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1109 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1110 (minus (bit_xor @0 @1) @1)))
1112 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1114 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1115 (minus @1 (bit_xor @0 @1)))
1117 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1118 (for op (bit_ior bit_xor plus)
1120 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
1123 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
1124 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1127 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1129 (bit_ior:c (bit_xor:c @0 @1) @0)
1132 /* (a & ~b) | (a ^ b) --> a ^ b */
1134 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1137 /* (a & ~b) ^ ~a --> ~(a & b) */
1139 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1140 (bit_not (bit_and @0 @1)))
1142 /* (~a & b) ^ a --> (a | b) */
1144 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1147 /* (a | b) & ~(a ^ b) --> a & b */
1149 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1152 /* a | ~(a ^ b) --> a | ~b */
1154 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1155 (bit_ior @0 (bit_not @1)))
1157 /* (a | b) | (a &^ b) --> a | b */
1158 (for op (bit_and bit_xor)
1160 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1163 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1165 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1168 /* ~(~a & b) --> a | ~b */
1170 (bit_not (bit_and:cs (bit_not @0) @1))
1171 (bit_ior @0 (bit_not @1)))
1173 /* ~(~a | b) --> a & ~b */
1175 (bit_not (bit_ior:cs (bit_not @0) @1))
1176 (bit_and @0 (bit_not @1)))
1178 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1180 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1181 (bit_and @3 (bit_not @2)))
1183 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1185 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1188 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1190 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1191 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1193 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1195 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1196 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1198 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1200 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1201 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1202 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1205 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1206 ((A & N) + B) & M -> (A + B) & M
1207 Similarly if (N & M) == 0,
1208 ((A | N) + B) & M -> (A + B) & M
1209 and for - instead of + (or unary - instead of +)
1210 and/or ^ instead of |.
1211 If B is constant and (B & M) == 0, fold into A & M. */
1212 (for op (plus minus)
1213 (for bitop (bit_and bit_ior bit_xor)
1215 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1218 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1219 @3, @4, @1, ERROR_MARK, NULL_TREE,
1222 (convert (bit_and (op (convert:utype { pmop[0]; })
1223 (convert:utype { pmop[1]; }))
1224 (convert:utype @2))))))
1226 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1229 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1230 NULL_TREE, NULL_TREE, @1, bitop, @3,
1233 (convert (bit_and (op (convert:utype { pmop[0]; })
1234 (convert:utype { pmop[1]; }))
1235 (convert:utype @2)))))))
1237 (bit_and (op:s @0 @1) INTEGER_CST@2)
1240 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1241 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1242 NULL_TREE, NULL_TREE, pmop); }
1244 (convert (bit_and (op (convert:utype { pmop[0]; })
1245 (convert:utype { pmop[1]; }))
1246 (convert:utype @2)))))))
1247 (for bitop (bit_and bit_ior bit_xor)
1249 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1252 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1253 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1254 NULL_TREE, NULL_TREE, pmop); }
1256 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1257 (convert:utype @1)))))))
1259 /* X % Y is smaller than Y. */
1262 (cmp (trunc_mod @0 @1) @1)
1263 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1264 { constant_boolean_node (cmp == LT_EXPR, type); })))
1267 (cmp @1 (trunc_mod @0 @1))
1268 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1269 { constant_boolean_node (cmp == GT_EXPR, type); })))
1273 (bit_ior @0 integer_all_onesp@1)
1278 (bit_ior @0 integer_zerop)
1283 (bit_and @0 integer_zerop@1)
1288 (for op (bit_ior bit_xor)
1290 (op:c (convert? @0) (convert? (bit_not @0)))
1291 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1296 { build_zero_cst (type); })
1298 /* Canonicalize X ^ ~0 to ~X. */
1300 (bit_xor @0 integer_all_onesp@1)
1305 (bit_and @0 integer_all_onesp)
1308 /* x & x -> x, x | x -> x */
1309 (for bitop (bit_and bit_ior)
1314 /* x & C -> x if we know that x & ~C == 0. */
1317 (bit_and SSA_NAME@0 INTEGER_CST@1)
1318 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1319 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1323 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1325 (bit_not (minus (bit_not @0) @1))
1328 (bit_not (plus:c (bit_not @0) @1))
1330 /* (~X - ~Y) -> Y - X. */
1332 (minus (bit_not @0) (bit_not @1))
1333 (if (!TYPE_OVERFLOW_SANITIZED (type))
1334 (with { tree utype = unsigned_type_for (type); }
1335 (convert (minus (convert:utype @1) (convert:utype @0))))))
1337 /* ~(X - Y) -> ~X + Y. */
1339 (bit_not (minus:s @0 @1))
1340 (plus (bit_not @0) @1))
1342 (bit_not (plus:s @0 INTEGER_CST@1))
1343 (if ((INTEGRAL_TYPE_P (type)
1344 && TYPE_UNSIGNED (type))
1345 || (!TYPE_OVERFLOW_SANITIZED (type)
1346 && may_negate_without_overflow_p (@1)))
1347 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1350 /* ~X + Y -> (Y - X) - 1. */
1352 (plus:c (bit_not @0) @1)
1353 (if (ANY_INTEGRAL_TYPE_P (type)
1354 && TYPE_OVERFLOW_WRAPS (type)
1355 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1356 && !integer_all_onesp (@1))
1357 (plus (minus @1 @0) { build_minus_one_cst (type); })
1358 (if (INTEGRAL_TYPE_P (type)
1359 && TREE_CODE (@1) == INTEGER_CST
1360 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1362 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1365 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1367 (bit_not (rshift:s @0 @1))
1368 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1369 (rshift (bit_not! @0) @1)
1370 /* For logical right shifts, this is possible only if @0 doesn't
1371 have MSB set and the logical right shift is changed into
1372 arithmetic shift. */
1373 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1374 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1375 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1377 /* x + (x & 1) -> (x + 1) & ~1 */
1379 (plus:c @0 (bit_and:s @0 integer_onep@1))
1380 (bit_and (plus @0 @1) (bit_not @1)))
1382 /* x & ~(x & y) -> x & ~y */
1383 /* x | ~(x | y) -> x | ~y */
1384 (for bitop (bit_and bit_ior)
1386 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1387 (bitop @0 (bit_not @1))))
1389 /* (~x & y) | ~(x | y) -> ~x */
1391 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1394 /* (x | y) ^ (x | ~y) -> ~x */
1396 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1399 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1401 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1402 (bit_not (bit_xor @0 @1)))
1404 /* (~x | y) ^ (x ^ y) -> x | ~y */
1406 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1407 (bit_ior @0 (bit_not @1)))
1409 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1411 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1412 (bit_not (bit_and @0 @1)))
1414 /* (x | y) & ~x -> y & ~x */
1415 /* (x & y) | ~x -> y | ~x */
1416 (for bitop (bit_and bit_ior)
1417 rbitop (bit_ior bit_and)
1419 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1422 /* (x & y) ^ (x | y) -> x ^ y */
1424 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1427 /* (x ^ y) ^ (x | y) -> x & y */
1429 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1432 /* (x & y) + (x ^ y) -> x | y */
1433 /* (x & y) | (x ^ y) -> x | y */
1434 /* (x & y) ^ (x ^ y) -> x | y */
1435 (for op (plus bit_ior bit_xor)
1437 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1440 /* (x & y) + (x | y) -> x + y */
1442 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1445 /* (x + y) - (x | y) -> x & y */
1447 (minus (plus @0 @1) (bit_ior @0 @1))
1448 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1449 && !TYPE_SATURATING (type))
1452 /* (x + y) - (x & y) -> x | y */
1454 (minus (plus @0 @1) (bit_and @0 @1))
1455 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1456 && !TYPE_SATURATING (type))
1459 /* (x | y) - y -> (x & ~y) */
1461 (minus (bit_ior:cs @0 @1) @1)
1462 (bit_and @0 (bit_not @1)))
1464 /* (x | y) - (x ^ y) -> x & y */
1466 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1469 /* (x | y) - (x & y) -> x ^ y */
1471 (minus (bit_ior @0 @1) (bit_and @0 @1))
1474 /* (x | y) & ~(x & y) -> x ^ y */
1476 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1479 /* (x | y) & (~x ^ y) -> x & y */
1481 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1484 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1486 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1487 (bit_not (bit_xor @0 @1)))
1489 /* (~x | y) ^ (x | ~y) -> x ^ y */
1491 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1494 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1496 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1497 (nop_convert2? (bit_ior @0 @1))))
1499 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1500 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1501 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1502 && !TYPE_SATURATING (TREE_TYPE (@2)))
1503 (bit_not (convert (bit_xor @0 @1)))))
1505 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1507 (nop_convert3? (bit_ior @0 @1)))
1508 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1509 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1510 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1511 && !TYPE_SATURATING (TREE_TYPE (@2)))
1512 (bit_not (convert (bit_xor @0 @1)))))
1514 (minus (nop_convert1? (bit_and @0 @1))
1515 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1517 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1518 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1519 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1520 && !TYPE_SATURATING (TREE_TYPE (@2)))
1521 (bit_not (convert (bit_xor @0 @1)))))
1523 /* ~x & ~y -> ~(x | y)
1524 ~x | ~y -> ~(x & y) */
1525 (for op (bit_and bit_ior)
1526 rop (bit_ior bit_and)
1528 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1529 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1530 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1531 (bit_not (rop (convert @0) (convert @1))))))
1533 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1534 with a constant, and the two constants have no bits in common,
1535 we should treat this as a BIT_IOR_EXPR since this may produce more
1537 (for op (bit_xor plus)
1539 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1540 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1541 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1542 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1543 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1544 (bit_ior (convert @4) (convert @5)))))
1546 /* (X | Y) ^ X -> Y & ~ X*/
1548 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1549 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1550 (convert (bit_and @1 (bit_not @0)))))
1552 /* Convert ~X ^ ~Y to X ^ Y. */
1554 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1555 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1556 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1557 (bit_xor (convert @0) (convert @1))))
1559 /* Convert ~X ^ C to X ^ ~C. */
1561 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1562 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1563 (bit_xor (convert @0) (bit_not @1))))
1565 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1566 (for opo (bit_and bit_xor)
1567 opi (bit_xor bit_and)
1569 (opo:c (opi:cs @0 @1) @1)
1570 (bit_and (bit_not @0) @1)))
1572 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1573 operands are another bit-wise operation with a common input. If so,
1574 distribute the bit operations to save an operation and possibly two if
1575 constants are involved. For example, convert
1576 (A | B) & (A | C) into A | (B & C)
1577 Further simplification will occur if B and C are constants. */
1578 (for op (bit_and bit_ior bit_xor)
1579 rop (bit_ior bit_and bit_and)
1581 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1582 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1583 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1584 (rop (convert @0) (op (convert @1) (convert @2))))))
1586 /* Some simple reassociation for bit operations, also handled in reassoc. */
1587 /* (X & Y) & Y -> X & Y
1588 (X | Y) | Y -> X | Y */
1589 (for op (bit_and bit_ior)
1591 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1593 /* (X ^ Y) ^ Y -> X */
1595 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1597 /* (X & Y) & (X & Z) -> (X & Y) & Z
1598 (X | Y) | (X | Z) -> (X | Y) | Z */
1599 (for op (bit_and bit_ior)
1601 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1602 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1603 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1604 (if (single_use (@5) && single_use (@6))
1605 (op @3 (convert @2))
1606 (if (single_use (@3) && single_use (@4))
1607 (op (convert @1) @5))))))
1608 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1610 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1611 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1612 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1613 (bit_xor (convert @1) (convert @2))))
1615 /* Convert abs (abs (X)) into abs (X).
1616 also absu (absu (X)) into absu (X). */
1622 (absu (convert@2 (absu@1 @0)))
1623 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1626 /* Convert abs[u] (-X) -> abs[u] (X). */
1635 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1637 (abs tree_expr_nonnegative_p@0)
1641 (absu tree_expr_nonnegative_p@0)
1644 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1646 (mult:c (nop_convert1?
1647 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1650 (if (INTEGRAL_TYPE_P (type)
1651 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1652 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1653 (if (TYPE_UNSIGNED (type))
1660 /* A few cases of fold-const.cc negate_expr_p predicate. */
1661 (match negate_expr_p
1663 (if ((INTEGRAL_TYPE_P (type)
1664 && TYPE_UNSIGNED (type))
1665 || (!TYPE_OVERFLOW_SANITIZED (type)
1666 && may_negate_without_overflow_p (t)))))
1667 (match negate_expr_p
1669 (match negate_expr_p
1671 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1672 (match negate_expr_p
1674 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1675 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1677 (match negate_expr_p
1679 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1680 (match negate_expr_p
1682 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1683 || (FLOAT_TYPE_P (type)
1684 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1685 && !HONOR_SIGNED_ZEROS (type)))))
1687 /* (-A) * (-B) -> A * B */
1689 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1690 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1691 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1692 (mult (convert @0) (convert (negate @1)))))
1694 /* -(A + B) -> (-B) - A. */
1696 (negate (plus:c @0 negate_expr_p@1))
1697 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1698 && !HONOR_SIGNED_ZEROS (type))
1699 (minus (negate @1) @0)))
1701 /* -(A - B) -> B - A. */
1703 (negate (minus @0 @1))
1704 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1705 || (FLOAT_TYPE_P (type)
1706 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1707 && !HONOR_SIGNED_ZEROS (type)))
1710 (negate (pointer_diff @0 @1))
1711 (if (TYPE_OVERFLOW_UNDEFINED (type))
1712 (pointer_diff @1 @0)))
1714 /* A - B -> A + (-B) if B is easily negatable. */
1716 (minus @0 negate_expr_p@1)
1717 (if (!FIXED_POINT_TYPE_P (type))
1718 (plus @0 (negate @1))))
1720 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1722 (negate (mult:c@0 @1 negate_expr_p@2))
1723 (if (! TYPE_UNSIGNED (type)
1724 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1726 (mult @1 (negate @2))))
1729 (negate (rdiv@0 @1 negate_expr_p@2))
1730 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1732 (rdiv @1 (negate @2))))
1735 (negate (rdiv@0 negate_expr_p@1 @2))
1736 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1738 (rdiv (negate @1) @2)))
1740 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1742 (negate (convert? (rshift @0 INTEGER_CST@1)))
1743 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1744 && wi::to_wide (@1) == element_precision (type) - 1)
1745 (with { tree stype = TREE_TYPE (@0);
1746 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1747 : unsigned_type_for (stype); }
1748 (if (VECTOR_TYPE_P (type))
1749 (view_convert (rshift (view_convert:ntype @0) @1))
1750 (convert (rshift (convert:ntype @0) @1))))))
1752 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1754 For bitwise binary operations apply operand conversions to the
1755 binary operation result instead of to the operands. This allows
1756 to combine successive conversions and bitwise binary operations.
1757 We combine the above two cases by using a conditional convert. */
1758 (for bitop (bit_and bit_ior bit_xor)
1760 (bitop (convert@2 @0) (convert?@3 @1))
1761 (if (((TREE_CODE (@1) == INTEGER_CST
1762 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1763 && (int_fits_type_p (@1, TREE_TYPE (@0))
1764 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1765 || types_match (@0, @1))
1766 && !POINTER_TYPE_P (TREE_TYPE (@0))
1767 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
1768 /* ??? This transform conflicts with fold-const.cc doing
1769 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1770 constants (if x has signed type, the sign bit cannot be set
1771 in c). This folds extension into the BIT_AND_EXPR.
1772 Restrict it to GIMPLE to avoid endless recursions. */
1773 && (bitop != BIT_AND_EXPR || GIMPLE)
1774 && (/* That's a good idea if the conversion widens the operand, thus
1775 after hoisting the conversion the operation will be narrower.
1776 It is also a good if the conversion is a nop as moves the
1777 conversion to one side; allowing for combining of the conversions. */
1778 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1779 /* The conversion check for being a nop can only be done at the gimple
1780 level as fold_binary has some re-association code which can conflict
1781 with this if there is a "constant" which is not a full INTEGER_CST. */
1782 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1783 /* It's also a good idea if the conversion is to a non-integer
1785 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1786 /* Or if the precision of TO is not the same as the precision
1788 || !type_has_mode_precision_p (type)
1789 /* In GIMPLE, getting rid of 2 conversions for one new results
1792 && TREE_CODE (@1) != INTEGER_CST
1793 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1795 && single_use (@3))))
1796 (convert (bitop @0 (convert @1)))))
1797 /* In GIMPLE, getting rid of 2 conversions for one new results
1800 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1802 && TREE_CODE (@1) != INTEGER_CST
1803 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1804 && types_match (type, @0)
1805 && !POINTER_TYPE_P (TREE_TYPE (@0))
1806 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
1807 (bitop @0 (convert @1)))))
1809 (for bitop (bit_and bit_ior)
1810 rbitop (bit_ior bit_and)
1811 /* (x | y) & x -> x */
1812 /* (x & y) | x -> x */
1814 (bitop:c (rbitop:c @0 @1) @0)
1816 /* (~x | y) & x -> x & y */
1817 /* (~x & y) | x -> x | y */
1819 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1822 /* ((x | y) & z) | x -> (z & y) | x */
1824 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1825 (bit_ior (bit_and @2 @1) @0))
1827 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1829 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1830 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1832 /* Combine successive equal operations with constants. */
1833 (for bitop (bit_and bit_ior bit_xor)
1835 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1836 (if (!CONSTANT_CLASS_P (@0))
1837 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1838 folded to a constant. */
1839 (bitop @0 (bitop @1 @2))
1840 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1841 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1842 the values involved are such that the operation can't be decided at
1843 compile time. Try folding one of @0 or @1 with @2 to see whether
1844 that combination can be decided at compile time.
1846 Keep the existing form if both folds fail, to avoid endless
1848 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1850 (bitop @1 { cst1; })
1851 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1853 (bitop @0 { cst2; }))))))))
1855 /* Try simple folding for X op !X, and X op X with the help
1856 of the truth_valued_p and logical_inverted_value predicates. */
1857 (match truth_valued_p
1859 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1860 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1861 (match truth_valued_p
1863 (match truth_valued_p
1866 (match (logical_inverted_value @0)
1868 (match (logical_inverted_value @0)
1869 (bit_not truth_valued_p@0))
1870 (match (logical_inverted_value @0)
1871 (eq @0 integer_zerop))
1872 (match (logical_inverted_value @0)
1873 (ne truth_valued_p@0 integer_truep))
1874 (match (logical_inverted_value @0)
1875 (bit_xor truth_valued_p@0 integer_truep))
1879 (bit_and:c @0 (logical_inverted_value @0))
1880 { build_zero_cst (type); })
1881 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1882 (for op (bit_ior bit_xor)
1884 (op:c truth_valued_p@0 (logical_inverted_value @0))
1885 { constant_boolean_node (true, type); }))
1886 /* X ==/!= !X is false/true. */
1889 (op:c truth_valued_p@0 (logical_inverted_value @0))
1890 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1894 (bit_not (bit_not @0))
1897 (match zero_one_valued_p
1899 (if (INTEGRAL_TYPE_P (type) && tree_nonzero_bits (@0) == 1)))
1900 (match zero_one_valued_p
1903 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
1905 (mult zero_one_valued_p@0 zero_one_valued_p@1)
1906 (if (INTEGRAL_TYPE_P (type))
1909 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
1911 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
1912 (if (INTEGRAL_TYPE_P (type)
1913 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1914 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
1915 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1916 (mult (convert @0) @1)))
1918 /* Narrow integer multiplication by a zero_one_valued_p operand.
1919 Multiplication by [0,1] is guaranteed not to overflow. */
1921 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
1922 (if (INTEGRAL_TYPE_P (type)
1923 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1924 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
1925 (mult (convert @1) (convert @2))))
1927 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
1928 Check that the shift is well-defined (C is less than TYPE_PRECISION)
1929 as some targets (such as x86's SSE) may return zero for larger C. */
1931 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
1932 (if (tree_fits_shwi_p (@1)
1933 && tree_to_shwi (@1) > 0
1934 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
1937 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
1938 Check that the shift is well-defined (C is less than TYPE_PRECISION)
1939 as some targets (such as x86's SSE) may return zero for larger C. */
1941 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
1942 (if (tree_fits_shwi_p (@1)
1943 && tree_to_shwi (@1) > 0
1944 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
1947 /* Convert ~ (-A) to A - 1. */
1949 (bit_not (convert? (negate @0)))
1950 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1951 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1952 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1954 /* Convert - (~A) to A + 1. */
1956 (negate (nop_convert? (bit_not @0)))
1957 (plus (view_convert @0) { build_each_one_cst (type); }))
1959 /* (a & b) ^ (a == b) -> !(a | b) */
1960 /* (a & b) == (a ^ b) -> !(a | b) */
1961 (for first_op (bit_xor eq)
1962 second_op (eq bit_xor)
1964 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
1965 (bit_not (bit_ior @0 @1))))
1967 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1969 (bit_not (convert? (minus @0 integer_each_onep)))
1970 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1971 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1972 (convert (negate @0))))
1974 (bit_not (convert? (plus @0 integer_all_onesp)))
1975 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1976 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1977 (convert (negate @0))))
1979 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1981 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1982 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1983 (convert (bit_xor @0 (bit_not @1)))))
1985 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1986 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1987 (convert (bit_xor @0 @1))))
1989 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1991 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1992 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1993 (bit_not (bit_xor (view_convert @0) @1))))
1995 /* ~(a ^ b) is a == b for truth valued a and b. */
1997 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
1998 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1999 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2000 (convert (eq @0 @1))))
2002 /* (~a) == b is a ^ b for truth valued a and b. */
2004 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2005 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2006 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2007 (convert (bit_xor @0 @1))))
2009 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2011 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2012 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2014 /* Fold A - (A & B) into ~B & A. */
2016 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2017 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2018 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2019 (convert (bit_and (bit_not @1) @0))))
2021 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2022 (if (!canonicalize_math_p ())
2023 (for cmp (gt lt ge le)
2025 (mult (convert (cmp @0 @1)) @2)
2026 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
2028 /* For integral types with undefined overflow and C != 0 fold
2029 x * C EQ/NE y * C into x EQ/NE y. */
2032 (cmp (mult:c @0 @1) (mult:c @2 @1))
2033 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2034 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2035 && tree_expr_nonzero_p (@1))
2038 /* For integral types with wrapping overflow and C odd fold
2039 x * C EQ/NE y * C into x EQ/NE y. */
2042 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2043 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2044 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2045 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2048 /* For integral types with undefined overflow and C != 0 fold
2049 x * C RELOP y * C into:
2051 x RELOP y for nonnegative C
2052 y RELOP x for negative C */
2053 (for cmp (lt gt le ge)
2055 (cmp (mult:c @0 @1) (mult:c @2 @1))
2056 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2057 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2058 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2060 (if (TREE_CODE (@1) == INTEGER_CST
2061 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2064 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2068 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2069 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2070 && TYPE_UNSIGNED (TREE_TYPE (@0))
2071 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2072 && (wi::to_wide (@2)
2073 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2074 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2075 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2077 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2078 (for cmp (simple_comparison)
2080 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2081 (if (element_precision (@3) >= element_precision (@0)
2082 && types_match (@0, @1))
2083 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2084 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2086 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2089 tree utype = unsigned_type_for (TREE_TYPE (@0));
2091 (cmp (convert:utype @1) (convert:utype @0)))))
2092 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2093 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2097 tree utype = unsigned_type_for (TREE_TYPE (@0));
2099 (cmp (convert:utype @0) (convert:utype @1)))))))))
2101 /* X / C1 op C2 into a simple range test. */
2102 (for cmp (simple_comparison)
2104 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2105 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2106 && integer_nonzerop (@1)
2107 && !TREE_OVERFLOW (@1)
2108 && !TREE_OVERFLOW (@2))
2109 (with { tree lo, hi; bool neg_overflow;
2110 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2113 (if (code == LT_EXPR || code == GE_EXPR)
2114 (if (TREE_OVERFLOW (lo))
2115 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2116 (if (code == LT_EXPR)
2119 (if (code == LE_EXPR || code == GT_EXPR)
2120 (if (TREE_OVERFLOW (hi))
2121 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2122 (if (code == LE_EXPR)
2126 { build_int_cst (type, code == NE_EXPR); })
2127 (if (code == EQ_EXPR && !hi)
2129 (if (code == EQ_EXPR && !lo)
2131 (if (code == NE_EXPR && !hi)
2133 (if (code == NE_EXPR && !lo)
2136 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2140 tree etype = range_check_type (TREE_TYPE (@0));
2143 hi = fold_convert (etype, hi);
2144 lo = fold_convert (etype, lo);
2145 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2148 (if (etype && hi && !TREE_OVERFLOW (hi))
2149 (if (code == EQ_EXPR)
2150 (le (minus (convert:etype @0) { lo; }) { hi; })
2151 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2153 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2154 (for op (lt le ge gt)
2156 (op (plus:c @0 @2) (plus:c @1 @2))
2157 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2158 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2161 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2162 when C is an unsigned integer constant with only the MSB set, and X and
2163 Y have types of equal or lower integer conversion rank than C's. */
2164 (for op (lt le ge gt)
2166 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2167 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2168 && TYPE_UNSIGNED (TREE_TYPE (@0))
2169 && wi::only_sign_bit_p (wi::to_wide (@0)))
2170 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2171 (op (convert:stype @1) (convert:stype @2))))))
2173 /* For equality and subtraction, this is also true with wrapping overflow. */
2174 (for op (eq ne minus)
2176 (op (plus:c @0 @2) (plus:c @1 @2))
2177 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2178 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2179 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2182 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2183 (for op (lt le ge gt)
2185 (op (minus @0 @2) (minus @1 @2))
2186 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2187 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2189 /* For equality and subtraction, this is also true with wrapping overflow. */
2190 (for op (eq ne minus)
2192 (op (minus @0 @2) (minus @1 @2))
2193 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2194 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2195 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2197 /* And for pointers... */
2198 (for op (simple_comparison)
2200 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2201 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2204 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2205 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2206 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2207 (pointer_diff @0 @1)))
2209 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2210 (for op (lt le ge gt)
2212 (op (minus @2 @0) (minus @2 @1))
2213 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2214 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2216 /* For equality and subtraction, this is also true with wrapping overflow. */
2217 (for op (eq ne minus)
2219 (op (minus @2 @0) (minus @2 @1))
2220 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2221 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2222 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2224 /* And for pointers... */
2225 (for op (simple_comparison)
2227 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2228 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2231 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2232 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2233 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2234 (pointer_diff @1 @0)))
2236 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2237 (for op (lt le gt ge)
2239 (op:c (plus:c@2 @0 @1) @1)
2240 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2241 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2242 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2243 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2244 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2245 /* For equality, this is also true with wrapping overflow. */
2248 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2249 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2250 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2251 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2252 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2253 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2254 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2255 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2257 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2258 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2259 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2260 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2261 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2263 /* X - Y < X is the same as Y > 0 when there is no overflow.
2264 For equality, this is also true with wrapping overflow. */
2265 (for op (simple_comparison)
2267 (op:c @0 (minus@2 @0 @1))
2268 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2269 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2270 || ((op == EQ_EXPR || op == NE_EXPR)
2271 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2272 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2273 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2276 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2277 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2281 (cmp (trunc_div @0 @1) integer_zerop)
2282 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2283 /* Complex ==/!= is allowed, but not </>=. */
2284 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2285 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2288 /* X == C - X can never be true if C is odd. */
2291 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2292 (if (TREE_INT_CST_LOW (@1) & 1)
2293 { constant_boolean_node (cmp == NE_EXPR, type); })))
2295 /* Arguments on which one can call get_nonzero_bits to get the bits
2297 (match with_possible_nonzero_bits
2299 (match with_possible_nonzero_bits
2301 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2302 /* Slightly extended version, do not make it recursive to keep it cheap. */
2303 (match (with_possible_nonzero_bits2 @0)
2304 with_possible_nonzero_bits@0)
2305 (match (with_possible_nonzero_bits2 @0)
2306 (bit_and:c with_possible_nonzero_bits@0 @2))
2308 /* Same for bits that are known to be set, but we do not have
2309 an equivalent to get_nonzero_bits yet. */
2310 (match (with_certain_nonzero_bits2 @0)
2312 (match (with_certain_nonzero_bits2 @0)
2313 (bit_ior @1 INTEGER_CST@0))
2315 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2318 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2319 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2320 { constant_boolean_node (cmp == NE_EXPR, type); })))
2322 /* ((X inner_op C0) outer_op C1)
2323 With X being a tree where value_range has reasoned certain bits to always be
2324 zero throughout its computed value range,
2325 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2326 where zero_mask has 1's for all bits that are sure to be 0 in
2328 if (inner_op == '^') C0 &= ~C1;
2329 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2330 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2332 (for inner_op (bit_ior bit_xor)
2333 outer_op (bit_xor bit_ior)
2336 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2340 wide_int zero_mask_not;
2344 if (TREE_CODE (@2) == SSA_NAME)
2345 zero_mask_not = get_nonzero_bits (@2);
2349 if (inner_op == BIT_XOR_EXPR)
2351 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2352 cst_emit = C0 | wi::to_wide (@1);
2356 C0 = wi::to_wide (@0);
2357 cst_emit = C0 ^ wi::to_wide (@1);
2360 (if (!fail && (C0 & zero_mask_not) == 0)
2361 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2362 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2363 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2365 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2367 (pointer_plus (pointer_plus:s @0 @1) @3)
2368 (pointer_plus @0 (plus @1 @3)))
2371 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2372 (convert:type (pointer_plus @0 (plus @1 @3))))
2379 tem4 = (unsigned long) tem3;
2384 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2385 /* Conditionally look through a sign-changing conversion. */
2386 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2387 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2388 || (GENERIC && type == TREE_TYPE (@1))))
2391 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2392 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2396 tem = (sizetype) ptr;
2400 and produce the simpler and easier to analyze with respect to alignment
2401 ... = ptr & ~algn; */
2403 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2404 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2405 (bit_and @0 { algn; })))
2407 /* Try folding difference of addresses. */
2409 (minus (convert ADDR_EXPR@0) (convert @1))
2410 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2411 (with { poly_int64 diff; }
2412 (if (ptr_difference_const (@0, @1, &diff))
2413 { build_int_cst_type (type, diff); }))))
2415 (minus (convert @0) (convert ADDR_EXPR@1))
2416 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2417 (with { poly_int64 diff; }
2418 (if (ptr_difference_const (@0, @1, &diff))
2419 { build_int_cst_type (type, diff); }))))
2421 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2422 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2423 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2424 (with { poly_int64 diff; }
2425 (if (ptr_difference_const (@0, @1, &diff))
2426 { build_int_cst_type (type, diff); }))))
2428 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2429 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2430 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2431 (with { poly_int64 diff; }
2432 (if (ptr_difference_const (@0, @1, &diff))
2433 { build_int_cst_type (type, diff); }))))
2435 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2437 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2438 (with { poly_int64 diff; }
2439 (if (ptr_difference_const (@0, @2, &diff))
2440 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2442 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2445 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2446 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2447 (if (ptr_difference_const (@0, @2, &diff))
2448 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2450 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2452 (convert (pointer_diff @0 INTEGER_CST@1))
2453 (if (POINTER_TYPE_P (type))
2454 { build_fold_addr_expr_with_type
2455 (build2 (MEM_REF, char_type_node, @0,
2456 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2459 /* If arg0 is derived from the address of an object or function, we may
2460 be able to fold this expression using the object or function's
2463 (bit_and (convert? @0) INTEGER_CST@1)
2464 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2465 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2469 unsigned HOST_WIDE_INT bitpos;
2470 get_pointer_alignment_1 (@0, &align, &bitpos);
2472 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2473 { wide_int_to_tree (type, (wi::to_wide (@1)
2474 & (bitpos / BITS_PER_UNIT))); }))))
2478 (if (INTEGRAL_TYPE_P (type)
2479 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2483 (if (INTEGRAL_TYPE_P (type)
2484 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2486 /* x > y && x != XXX_MIN --> x > y
2487 x > y && x == XXX_MIN --> false . */
2490 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2492 (if (eqne == EQ_EXPR)
2493 { constant_boolean_node (false, type); })
2494 (if (eqne == NE_EXPR)
2498 /* x < y && x != XXX_MAX --> x < y
2499 x < y && x == XXX_MAX --> false. */
2502 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2504 (if (eqne == EQ_EXPR)
2505 { constant_boolean_node (false, type); })
2506 (if (eqne == NE_EXPR)
2510 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2512 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2515 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2517 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2520 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2522 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2525 /* x <= y || x != XXX_MIN --> true. */
2527 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2528 { constant_boolean_node (true, type); })
2530 /* x <= y || x == XXX_MIN --> x <= y. */
2532 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2535 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2537 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2540 /* x >= y || x != XXX_MAX --> true
2541 x >= y || x == XXX_MAX --> x >= y. */
2544 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2546 (if (eqne == EQ_EXPR)
2548 (if (eqne == NE_EXPR)
2549 { constant_boolean_node (true, type); }))))
2551 /* y == XXX_MIN || x < y --> x <= y - 1 */
2553 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2554 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2555 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2556 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2558 /* y != XXX_MIN && x >= y --> x > y - 1 */
2560 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2561 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2562 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2563 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2565 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2566 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2569 (for code2 (eq ne lt gt le ge)
2571 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2574 int cmp = tree_int_cst_compare (@1, @2);
2578 case EQ_EXPR: val = (cmp == 0); break;
2579 case NE_EXPR: val = (cmp != 0); break;
2580 case LT_EXPR: val = (cmp < 0); break;
2581 case GT_EXPR: val = (cmp > 0); break;
2582 case LE_EXPR: val = (cmp <= 0); break;
2583 case GE_EXPR: val = (cmp >= 0); break;
2584 default: gcc_unreachable ();
2588 (if (code1 == EQ_EXPR && val) @3)
2589 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2590 (if (code1 == NE_EXPR && !val) @4))))))
2592 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2594 (for code1 (lt le gt ge)
2595 (for code2 (lt le gt ge)
2597 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2600 int cmp = tree_int_cst_compare (@1, @2);
2603 /* Choose the more restrictive of two < or <= comparisons. */
2604 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2605 && (code2 == LT_EXPR || code2 == LE_EXPR))
2606 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2609 /* Likewise chose the more restrictive of two > or >= comparisons. */
2610 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2611 && (code2 == GT_EXPR || code2 == GE_EXPR))
2612 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2615 /* Check for singleton ranges. */
2617 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2618 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2620 /* Check for disjoint ranges. */
2622 && (code1 == LT_EXPR || code1 == LE_EXPR)
2623 && (code2 == GT_EXPR || code2 == GE_EXPR))
2624 { constant_boolean_node (false, type); })
2626 && (code1 == GT_EXPR || code1 == GE_EXPR)
2627 && (code2 == LT_EXPR || code2 == LE_EXPR))
2628 { constant_boolean_node (false, type); })
2631 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2632 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2635 (for code2 (eq ne lt gt le ge)
2637 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2640 int cmp = tree_int_cst_compare (@1, @2);
2644 case EQ_EXPR: val = (cmp == 0); break;
2645 case NE_EXPR: val = (cmp != 0); break;
2646 case LT_EXPR: val = (cmp < 0); break;
2647 case GT_EXPR: val = (cmp > 0); break;
2648 case LE_EXPR: val = (cmp <= 0); break;
2649 case GE_EXPR: val = (cmp >= 0); break;
2650 default: gcc_unreachable ();
2654 (if (code1 == EQ_EXPR && val) @4)
2655 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2656 (if (code1 == NE_EXPR && !val) @3))))))
2658 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2660 (for code1 (lt le gt ge)
2661 (for code2 (lt le gt ge)
2663 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2666 int cmp = tree_int_cst_compare (@1, @2);
2669 /* Choose the more restrictive of two < or <= comparisons. */
2670 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2671 && (code2 == LT_EXPR || code2 == LE_EXPR))
2672 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2675 /* Likewise chose the more restrictive of two > or >= comparisons. */
2676 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2677 && (code2 == GT_EXPR || code2 == GE_EXPR))
2678 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2681 /* Check for singleton ranges. */
2683 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2684 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2686 /* Check for disjoint ranges. */
2688 && (code1 == LT_EXPR || code1 == LE_EXPR)
2689 && (code2 == GT_EXPR || code2 == GE_EXPR))
2690 { constant_boolean_node (true, type); })
2692 && (code1 == GT_EXPR || code1 == GE_EXPR)
2693 && (code2 == LT_EXPR || code2 == LE_EXPR))
2694 { constant_boolean_node (true, type); })
2697 /* We can't reassociate at all for saturating types. */
2698 (if (!TYPE_SATURATING (type))
2700 /* Contract negates. */
2701 /* A + (-B) -> A - B */
2703 (plus:c @0 (convert? (negate @1)))
2704 /* Apply STRIP_NOPS on the negate. */
2705 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2706 && !TYPE_OVERFLOW_SANITIZED (type))
2710 if (INTEGRAL_TYPE_P (type)
2711 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2712 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2714 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2715 /* A - (-B) -> A + B */
2717 (minus @0 (convert? (negate @1)))
2718 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2719 && !TYPE_OVERFLOW_SANITIZED (type))
2723 if (INTEGRAL_TYPE_P (type)
2724 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2725 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2727 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2729 Sign-extension is ok except for INT_MIN, which thankfully cannot
2730 happen without overflow. */
2732 (negate (convert (negate @1)))
2733 (if (INTEGRAL_TYPE_P (type)
2734 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2735 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2736 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2737 && !TYPE_OVERFLOW_SANITIZED (type)
2738 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2741 (negate (convert negate_expr_p@1))
2742 (if (SCALAR_FLOAT_TYPE_P (type)
2743 && ((DECIMAL_FLOAT_TYPE_P (type)
2744 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2745 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2746 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2747 (convert (negate @1))))
2749 (negate (nop_convert? (negate @1)))
2750 (if (!TYPE_OVERFLOW_SANITIZED (type)
2751 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2754 /* We can't reassociate floating-point unless -fassociative-math
2755 or fixed-point plus or minus because of saturation to +-Inf. */
2756 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2757 && !FIXED_POINT_TYPE_P (type))
2759 /* Match patterns that allow contracting a plus-minus pair
2760 irrespective of overflow issues. */
2761 /* (A +- B) - A -> +- B */
2762 /* (A +- B) -+ B -> A */
2763 /* A - (A +- B) -> -+ B */
2764 /* A +- (B -+ A) -> +- B */
2766 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2769 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2770 (if (!ANY_INTEGRAL_TYPE_P (type)
2771 || TYPE_OVERFLOW_WRAPS (type))
2772 (negate (view_convert @1))
2773 (view_convert (negate @1))))
2775 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2778 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2779 (if (!ANY_INTEGRAL_TYPE_P (type)
2780 || TYPE_OVERFLOW_WRAPS (type))
2781 (negate (view_convert @1))
2782 (view_convert (negate @1))))
2784 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2786 /* (A +- B) + (C - A) -> C +- B */
2787 /* (A + B) - (A - C) -> B + C */
2788 /* More cases are handled with comparisons. */
2790 (plus:c (plus:c @0 @1) (minus @2 @0))
2793 (plus:c (minus @0 @1) (minus @2 @0))
2796 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2797 (if (TYPE_OVERFLOW_UNDEFINED (type)
2798 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2799 (pointer_diff @2 @1)))
2801 (minus (plus:c @0 @1) (minus @0 @2))
2804 /* (A +- CST1) +- CST2 -> A + CST3
2805 Use view_convert because it is safe for vectors and equivalent for
2807 (for outer_op (plus minus)
2808 (for inner_op (plus minus)
2809 neg_inner_op (minus plus)
2811 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2813 /* If one of the types wraps, use that one. */
2814 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2815 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2816 forever if something doesn't simplify into a constant. */
2817 (if (!CONSTANT_CLASS_P (@0))
2818 (if (outer_op == PLUS_EXPR)
2819 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2820 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2821 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2822 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2823 (if (outer_op == PLUS_EXPR)
2824 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2825 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2826 /* If the constant operation overflows we cannot do the transform
2827 directly as we would introduce undefined overflow, for example
2828 with (a - 1) + INT_MIN. */
2829 (if (types_match (type, @0))
2830 (with { tree cst = const_binop (outer_op == inner_op
2831 ? PLUS_EXPR : MINUS_EXPR,
2833 (if (cst && !TREE_OVERFLOW (cst))
2834 (inner_op @0 { cst; } )
2835 /* X+INT_MAX+1 is X-INT_MIN. */
2836 (if (INTEGRAL_TYPE_P (type) && cst
2837 && wi::to_wide (cst) == wi::min_value (type))
2838 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2839 /* Last resort, use some unsigned type. */
2840 (with { tree utype = unsigned_type_for (type); }
2842 (view_convert (inner_op
2843 (view_convert:utype @0)
2845 { drop_tree_overflow (cst); }))))))))))))))
2847 /* (CST1 - A) +- CST2 -> CST3 - A */
2848 (for outer_op (plus minus)
2850 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2851 /* If one of the types wraps, use that one. */
2852 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2853 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2854 forever if something doesn't simplify into a constant. */
2855 (if (!CONSTANT_CLASS_P (@0))
2856 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2857 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2858 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2859 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2860 (if (types_match (type, @0))
2861 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2862 (if (cst && !TREE_OVERFLOW (cst))
2863 (minus { cst; } @0))))))))
2865 /* CST1 - (CST2 - A) -> CST3 + A
2866 Use view_convert because it is safe for vectors and equivalent for
2869 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2870 /* If one of the types wraps, use that one. */
2871 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2872 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2873 forever if something doesn't simplify into a constant. */
2874 (if (!CONSTANT_CLASS_P (@0))
2875 (plus (view_convert @0) (minus @1 (view_convert @2))))
2876 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2877 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2878 (view_convert (plus @0 (minus (view_convert @1) @2)))
2879 (if (types_match (type, @0))
2880 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2881 (if (cst && !TREE_OVERFLOW (cst))
2882 (plus { cst; } @0)))))))
2884 /* ((T)(A)) + CST -> (T)(A + CST) */
2887 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2888 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2889 && TREE_CODE (type) == INTEGER_TYPE
2890 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2891 && int_fits_type_p (@1, TREE_TYPE (@0)))
2892 /* Perform binary operation inside the cast if the constant fits
2893 and (A + CST)'s range does not overflow. */
2896 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2897 max_ovf = wi::OVF_OVERFLOW;
2898 tree inner_type = TREE_TYPE (@0);
2901 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2902 TYPE_SIGN (inner_type));
2905 if (get_global_range_query ()->range_of_expr (vr, @0)
2906 && vr.kind () == VR_RANGE)
2908 wide_int wmin0 = vr.lower_bound ();
2909 wide_int wmax0 = vr.upper_bound ();
2910 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2911 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2914 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2915 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2919 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2921 (for op (plus minus)
2923 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2924 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2925 && TREE_CODE (type) == INTEGER_TYPE
2926 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2927 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2928 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2929 && TYPE_OVERFLOW_WRAPS (type))
2930 (plus (convert @0) (op @2 (convert @1))))))
2933 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2934 to a simple value. */
2935 (for op (plus minus)
2937 (op (convert @0) (convert @1))
2938 (if (INTEGRAL_TYPE_P (type)
2939 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2940 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2941 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2942 && !TYPE_OVERFLOW_TRAPS (type)
2943 && !TYPE_OVERFLOW_SANITIZED (type))
2944 (convert (op! @0 @1)))))
2948 (plus:c (convert? (bit_not @0)) (convert? @0))
2949 (if (!TYPE_OVERFLOW_TRAPS (type))
2950 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
2954 (plus (convert? (bit_not @0)) integer_each_onep)
2955 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2956 (negate (convert @0))))
2960 (minus (convert? (negate @0)) integer_each_onep)
2961 (if (!TYPE_OVERFLOW_TRAPS (type)
2962 && TREE_CODE (type) != COMPLEX_TYPE
2963 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2964 (bit_not (convert @0))))
2968 (minus integer_all_onesp @0)
2969 (if (TREE_CODE (type) != COMPLEX_TYPE)
2972 /* (T)(P + A) - (T)P -> (T) A */
2974 (minus (convert (plus:c @@0 @1))
2976 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2977 /* For integer types, if A has a smaller type
2978 than T the result depends on the possible
2980 E.g. T=size_t, A=(unsigned)429497295, P>0.
2981 However, if an overflow in P + A would cause
2982 undefined behavior, we can assume that there
2984 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2985 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2988 (minus (convert (pointer_plus @@0 @1))
2990 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2991 /* For pointer types, if the conversion of A to the
2992 final type requires a sign- or zero-extension,
2993 then we have to punt - it is not defined which
2995 || (POINTER_TYPE_P (TREE_TYPE (@0))
2996 && TREE_CODE (@1) == INTEGER_CST
2997 && tree_int_cst_sign_bit (@1) == 0))
3000 (pointer_diff (pointer_plus @@0 @1) @0)
3001 /* The second argument of pointer_plus must be interpreted as signed, and
3002 thus sign-extended if necessary. */
3003 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3004 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3005 second arg is unsigned even when we need to consider it as signed,
3006 we don't want to diagnose overflow here. */
3007 (convert (view_convert:stype @1))))
3009 /* (T)P - (T)(P + A) -> -(T) A */
3011 (minus (convert? @0)
3012 (convert (plus:c @@0 @1)))
3013 (if (INTEGRAL_TYPE_P (type)
3014 && TYPE_OVERFLOW_UNDEFINED (type)
3015 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3016 (with { tree utype = unsigned_type_for (type); }
3017 (convert (negate (convert:utype @1))))
3018 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3019 /* For integer types, if A has a smaller type
3020 than T the result depends on the possible
3022 E.g. T=size_t, A=(unsigned)429497295, P>0.
3023 However, if an overflow in P + A would cause
3024 undefined behavior, we can assume that there
3026 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3027 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3028 (negate (convert @1)))))
3031 (convert (pointer_plus @@0 @1)))
3032 (if (INTEGRAL_TYPE_P (type)
3033 && TYPE_OVERFLOW_UNDEFINED (type)
3034 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3035 (with { tree utype = unsigned_type_for (type); }
3036 (convert (negate (convert:utype @1))))
3037 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3038 /* For pointer types, if the conversion of A to the
3039 final type requires a sign- or zero-extension,
3040 then we have to punt - it is not defined which
3042 || (POINTER_TYPE_P (TREE_TYPE (@0))
3043 && TREE_CODE (@1) == INTEGER_CST
3044 && tree_int_cst_sign_bit (@1) == 0))
3045 (negate (convert @1)))))
3047 (pointer_diff @0 (pointer_plus @@0 @1))
3048 /* The second argument of pointer_plus must be interpreted as signed, and
3049 thus sign-extended if necessary. */
3050 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3051 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3052 second arg is unsigned even when we need to consider it as signed,
3053 we don't want to diagnose overflow here. */
3054 (negate (convert (view_convert:stype @1)))))
3056 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3058 (minus (convert (plus:c @@0 @1))
3059 (convert (plus:c @0 @2)))
3060 (if (INTEGRAL_TYPE_P (type)
3061 && TYPE_OVERFLOW_UNDEFINED (type)
3062 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3063 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3064 (with { tree utype = unsigned_type_for (type); }
3065 (convert (minus (convert:utype @1) (convert:utype @2))))
3066 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3067 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3068 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3069 /* For integer types, if A has a smaller type
3070 than T the result depends on the possible
3072 E.g. T=size_t, A=(unsigned)429497295, P>0.
3073 However, if an overflow in P + A would cause
3074 undefined behavior, we can assume that there
3076 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3077 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3078 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3079 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3080 (minus (convert @1) (convert @2)))))
3082 (minus (convert (pointer_plus @@0 @1))
3083 (convert (pointer_plus @0 @2)))
3084 (if (INTEGRAL_TYPE_P (type)
3085 && TYPE_OVERFLOW_UNDEFINED (type)
3086 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3087 (with { tree utype = unsigned_type_for (type); }
3088 (convert (minus (convert:utype @1) (convert:utype @2))))
3089 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3090 /* For pointer types, if the conversion of A to the
3091 final type requires a sign- or zero-extension,
3092 then we have to punt - it is not defined which
3094 || (POINTER_TYPE_P (TREE_TYPE (@0))
3095 && TREE_CODE (@1) == INTEGER_CST
3096 && tree_int_cst_sign_bit (@1) == 0
3097 && TREE_CODE (@2) == INTEGER_CST
3098 && tree_int_cst_sign_bit (@2) == 0))
3099 (minus (convert @1) (convert @2)))))
3101 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3102 (pointer_diff @0 @1))
3104 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3105 /* The second argument of pointer_plus must be interpreted as signed, and
3106 thus sign-extended if necessary. */
3107 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3108 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3109 second arg is unsigned even when we need to consider it as signed,
3110 we don't want to diagnose overflow here. */
3111 (minus (convert (view_convert:stype @1))
3112 (convert (view_convert:stype @2)))))))
3114 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3115 Modeled after fold_plusminus_mult_expr. */
3116 (if (!TYPE_SATURATING (type)
3117 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3118 (for plusminus (plus minus)
3120 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3121 (if (!ANY_INTEGRAL_TYPE_P (type)
3122 || TYPE_OVERFLOW_WRAPS (type)
3123 || (INTEGRAL_TYPE_P (type)
3124 && tree_expr_nonzero_p (@0)
3125 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3126 (if (single_use (@3) || single_use (@4))
3127 /* If @1 +- @2 is constant require a hard single-use on either
3128 original operand (but not on both). */
3129 (mult (plusminus @1 @2) @0)
3130 (mult! (plusminus @1 @2) @0)
3132 /* We cannot generate constant 1 for fract. */
3133 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3135 (plusminus @0 (mult:c@3 @0 @2))
3136 (if ((!ANY_INTEGRAL_TYPE_P (type)
3137 || TYPE_OVERFLOW_WRAPS (type)
3138 /* For @0 + @0*@2 this transformation would introduce UB
3139 (where there was none before) for @0 in [-1,0] and @2 max.
3140 For @0 - @0*@2 this transformation would introduce UB
3141 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3142 || (INTEGRAL_TYPE_P (type)
3143 && ((tree_expr_nonzero_p (@0)
3144 && expr_not_equal_to (@0,
3145 wi::minus_one (TYPE_PRECISION (type))))
3146 || (plusminus == PLUS_EXPR
3147 ? expr_not_equal_to (@2,
3148 wi::max_value (TYPE_PRECISION (type), SIGNED))
3149 /* Let's ignore the @0 -1 and @2 min case. */
3150 : (expr_not_equal_to (@2,
3151 wi::min_value (TYPE_PRECISION (type), SIGNED))
3152 && expr_not_equal_to (@2,
3153 wi::min_value (TYPE_PRECISION (type), SIGNED)
3156 (mult (plusminus { build_one_cst (type); } @2) @0)))
3158 (plusminus (mult:c@3 @0 @2) @0)
3159 (if ((!ANY_INTEGRAL_TYPE_P (type)
3160 || TYPE_OVERFLOW_WRAPS (type)
3161 /* For @0*@2 + @0 this transformation would introduce UB
3162 (where there was none before) for @0 in [-1,0] and @2 max.
3163 For @0*@2 - @0 this transformation would introduce UB
3164 for @0 0 and @2 min. */
3165 || (INTEGRAL_TYPE_P (type)
3166 && ((tree_expr_nonzero_p (@0)
3167 && (plusminus == MINUS_EXPR
3168 || expr_not_equal_to (@0,
3169 wi::minus_one (TYPE_PRECISION (type)))))
3170 || expr_not_equal_to (@2,
3171 (plusminus == PLUS_EXPR
3172 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3173 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3175 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3178 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3179 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3181 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3182 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3183 && tree_fits_uhwi_p (@1)
3184 && tree_to_uhwi (@1) < element_precision (type)
3185 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3186 || optab_handler (smul_optab,
3187 TYPE_MODE (type)) != CODE_FOR_nothing))
3188 (with { tree t = type;
3189 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3190 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3191 element_precision (type));
3193 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3195 cst = build_uniform_cst (t, cst); }
3196 (convert (mult (convert:t @0) { cst; })))))
3198 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3199 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3200 && tree_fits_uhwi_p (@1)
3201 && tree_to_uhwi (@1) < element_precision (type)
3202 && tree_fits_uhwi_p (@2)
3203 && tree_to_uhwi (@2) < element_precision (type)
3204 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3205 || optab_handler (smul_optab,
3206 TYPE_MODE (type)) != CODE_FOR_nothing))
3207 (with { tree t = type;
3208 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3209 unsigned int prec = element_precision (type);
3210 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3211 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3212 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3214 cst = build_uniform_cst (t, cst); }
3215 (convert (mult (convert:t @0) { cst; })))))
3218 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3219 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3220 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3221 (for op (bit_ior bit_xor)
3223 (op (mult:s@0 @1 INTEGER_CST@2)
3224 (mult:s@3 @1 INTEGER_CST@4))
3225 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3226 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3228 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3230 (op:c (mult:s@0 @1 INTEGER_CST@2)
3231 (lshift:s@3 @1 INTEGER_CST@4))
3232 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3233 && tree_int_cst_sgn (@4) > 0
3234 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3235 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3236 wide_int c = wi::add (wi::to_wide (@2),
3237 wi::lshift (wone, wi::to_wide (@4))); }
3238 (mult @1 { wide_int_to_tree (type, c); }))))
3240 (op:c (mult:s@0 @1 INTEGER_CST@2)
3242 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3243 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3245 { wide_int_to_tree (type,
3246 wi::add (wi::to_wide (@2), 1)); })))
3248 (op (lshift:s@0 @1 INTEGER_CST@2)
3249 (lshift:s@3 @1 INTEGER_CST@4))
3250 (if (INTEGRAL_TYPE_P (type)
3251 && tree_int_cst_sgn (@2) > 0
3252 && tree_int_cst_sgn (@4) > 0
3253 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3254 (with { tree t = type;
3255 if (!TYPE_OVERFLOW_WRAPS (t))
3256 t = unsigned_type_for (t);
3257 wide_int wone = wi::one (TYPE_PRECISION (t));
3258 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3259 wi::lshift (wone, wi::to_wide (@4))); }
3260 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3262 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3264 (if (INTEGRAL_TYPE_P (type)
3265 && tree_int_cst_sgn (@2) > 0
3266 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3267 (with { tree t = type;
3268 if (!TYPE_OVERFLOW_WRAPS (t))
3269 t = unsigned_type_for (t);
3270 wide_int wone = wi::one (TYPE_PRECISION (t));
3271 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3272 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3274 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3276 (for minmax (min max)
3280 /* For fmin() and fmax(), skip folding when both are sNaN. */
3281 (for minmax (FMIN_ALL FMAX_ALL)
3284 (if (!tree_expr_maybe_signaling_nan_p (@0))
3286 /* min(max(x,y),y) -> y. */
3288 (min:c (max:c @0 @1) @1)
3290 /* max(min(x,y),y) -> y. */
3292 (max:c (min:c @0 @1) @1)
3294 /* max(a,-a) -> abs(a). */
3296 (max:c @0 (negate @0))
3297 (if (TREE_CODE (type) != COMPLEX_TYPE
3298 && (! ANY_INTEGRAL_TYPE_P (type)
3299 || TYPE_OVERFLOW_UNDEFINED (type)))
3301 /* min(a,-a) -> -abs(a). */
3303 (min:c @0 (negate @0))
3304 (if (TREE_CODE (type) != COMPLEX_TYPE
3305 && (! ANY_INTEGRAL_TYPE_P (type)
3306 || TYPE_OVERFLOW_UNDEFINED (type)))
3311 (if (INTEGRAL_TYPE_P (type)
3312 && TYPE_MIN_VALUE (type)
3313 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3315 (if (INTEGRAL_TYPE_P (type)
3316 && TYPE_MAX_VALUE (type)
3317 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3322 (if (INTEGRAL_TYPE_P (type)
3323 && TYPE_MAX_VALUE (type)
3324 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3326 (if (INTEGRAL_TYPE_P (type)
3327 && TYPE_MIN_VALUE (type)
3328 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3331 /* max (a, a + CST) -> a + CST where CST is positive. */
3332 /* max (a, a + CST) -> a where CST is negative. */
3334 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3335 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3336 (if (tree_int_cst_sgn (@1) > 0)
3340 /* min (a, a + CST) -> a where CST is positive. */
3341 /* min (a, a + CST) -> a + CST where CST is negative. */
3343 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3344 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3345 (if (tree_int_cst_sgn (@1) > 0)
3349 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3350 the addresses are known to be less, equal or greater. */
3351 (for minmax (min max)
3354 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3357 poly_int64 off0, off1;
3359 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3360 off0, off1, GENERIC);
3363 (if (minmax == MIN_EXPR)
3364 (if (known_le (off0, off1))
3366 (if (known_gt (off0, off1))
3368 (if (known_ge (off0, off1))
3370 (if (known_lt (off0, off1))
3373 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3374 and the outer convert demotes the expression back to x's type. */
3375 (for minmax (min max)
3377 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3378 (if (INTEGRAL_TYPE_P (type)
3379 && types_match (@1, type) && int_fits_type_p (@2, type)
3380 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3381 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3382 (minmax @1 (convert @2)))))
3384 (for minmax (FMIN_ALL FMAX_ALL)
3385 /* If either argument is NaN and other one is not sNaN, return the other
3386 one. Avoid the transformation if we get (and honor) a signalling NaN. */
3388 (minmax:c @0 REAL_CST@1)
3389 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3390 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3391 && !tree_expr_maybe_signaling_nan_p (@0))
3393 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3394 functions to return the numeric arg if the other one is NaN.
3395 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3396 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3397 worry about it either. */
3398 (if (flag_finite_math_only)
3405 /* min (-A, -B) -> -max (A, B) */
3406 (for minmax (min max FMIN_ALL FMAX_ALL)
3407 maxmin (max min FMAX_ALL FMIN_ALL)
3409 (minmax (negate:s@2 @0) (negate:s@3 @1))
3410 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3411 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3412 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3413 (negate (maxmin @0 @1)))))
3414 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3415 MAX (~X, ~Y) -> ~MIN (X, Y) */
3416 (for minmax (min max)
3419 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3420 (bit_not (maxmin @0 @1))))
3422 /* MIN (X, Y) == X -> X <= Y */
3423 (for minmax (min min max max)
3427 (cmp:c (minmax:c @0 @1) @0)
3428 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3430 /* MIN (X, 5) == 0 -> X == 0
3431 MIN (X, 5) == 7 -> false */
3434 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3435 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3436 TYPE_SIGN (TREE_TYPE (@0))))
3437 { constant_boolean_node (cmp == NE_EXPR, type); }
3438 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3439 TYPE_SIGN (TREE_TYPE (@0))))
3443 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3444 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3445 TYPE_SIGN (TREE_TYPE (@0))))
3446 { constant_boolean_node (cmp == NE_EXPR, type); }
3447 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3448 TYPE_SIGN (TREE_TYPE (@0))))
3450 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3451 (for minmax (min min max max min min max max )
3452 cmp (lt le gt ge gt ge lt le )
3453 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3455 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3456 (comb (cmp @0 @2) (cmp @1 @2))))
3458 /* X <= MAX(X, Y) -> true
3459 X > MAX(X, Y) -> false
3460 X >= MIN(X, Y) -> true
3461 X < MIN(X, Y) -> false */
3462 (for minmax (min min max max )
3465 (cmp @0 (minmax:c @0 @1))
3466 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3468 /* Undo fancy ways of writing max/min or other ?: expressions, like
3469 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
3470 People normally use ?: and that is what we actually try to optimize. */
3471 /* Transform A + (B-A)*cmp into cmp ? B : A. */
3473 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3474 (if (INTEGRAL_TYPE_P (type)
3475 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3476 (cond (convert:boolean_type_node @2) @1 @0)))
3477 /* Transform A - (A-B)*cmp into cmp ? B : A. */
3479 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3480 (if (INTEGRAL_TYPE_P (type)
3481 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3482 (cond (convert:boolean_type_node @2) @1 @0)))
3483 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
3485 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3486 (if (INTEGRAL_TYPE_P (type)
3487 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3488 (cond (convert:boolean_type_node @2) @1 @0)))
3490 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
3492 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3495 /* Simplifications of shift and rotates. */
3497 (for rotate (lrotate rrotate)
3499 (rotate integer_all_onesp@0 @1)
3502 /* Optimize -1 >> x for arithmetic right shifts. */
3504 (rshift integer_all_onesp@0 @1)
3505 (if (!TYPE_UNSIGNED (type))
3508 /* Optimize (x >> c) << c into x & (-1<<c). */
3510 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3511 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3512 /* It doesn't matter if the right shift is arithmetic or logical. */
3513 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3516 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3517 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3518 /* Allow intermediate conversion to integral type with whatever sign, as
3519 long as the low TYPE_PRECISION (type)
3520 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3521 && INTEGRAL_TYPE_P (type)
3522 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3523 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3524 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3525 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3526 || wi::geu_p (wi::to_wide (@1),
3527 TYPE_PRECISION (type)
3528 - TYPE_PRECISION (TREE_TYPE (@2)))))
3529 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3531 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3534 (rshift (lshift @0 INTEGER_CST@1) @1)
3535 (if (TYPE_UNSIGNED (type)
3536 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3537 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3539 /* Optimize x >> x into 0 */
3542 { build_zero_cst (type); })
3544 (for shiftrotate (lrotate rrotate lshift rshift)
3546 (shiftrotate @0 integer_zerop)
3549 (shiftrotate integer_zerop@0 @1)
3551 /* Prefer vector1 << scalar to vector1 << vector2
3552 if vector2 is uniform. */
3553 (for vec (VECTOR_CST CONSTRUCTOR)
3555 (shiftrotate @0 vec@1)
3556 (with { tree tem = uniform_vector_p (@1); }
3558 (shiftrotate @0 { tem; }))))))
3560 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3561 Y is 0. Similarly for X >> Y. */
3563 (for shift (lshift rshift)
3565 (shift @0 SSA_NAME@1)
3566 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3568 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3569 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3571 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3575 /* Rewrite an LROTATE_EXPR by a constant into an
3576 RROTATE_EXPR by a new constant. */
3578 (lrotate @0 INTEGER_CST@1)
3579 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3580 build_int_cst (TREE_TYPE (@1),
3581 element_precision (type)), @1); }))
3583 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3584 (for op (lrotate rrotate rshift lshift)
3586 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3587 (with { unsigned int prec = element_precision (type); }
3588 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3589 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3590 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3591 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3592 (with { unsigned int low = (tree_to_uhwi (@1)
3593 + tree_to_uhwi (@2)); }
3594 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3595 being well defined. */
3597 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3598 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3599 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3600 { build_zero_cst (type); }
3601 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3602 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3605 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3607 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3608 (if ((wi::to_wide (@1) & 1) != 0)
3609 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3610 { build_zero_cst (type); }))
3612 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3613 either to false if D is smaller (unsigned comparison) than C, or to
3614 x == log2 (D) - log2 (C). Similarly for right shifts. */
3618 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3619 (with { int c1 = wi::clz (wi::to_wide (@1));
3620 int c2 = wi::clz (wi::to_wide (@2)); }
3622 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3623 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3625 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3626 (if (tree_int_cst_sgn (@1) > 0)
3627 (with { int c1 = wi::clz (wi::to_wide (@1));
3628 int c2 = wi::clz (wi::to_wide (@2)); }
3630 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3631 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3633 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3634 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3638 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3639 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3641 || (!integer_zerop (@2)
3642 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3643 { constant_boolean_node (cmp == NE_EXPR, type); }
3644 (if (!integer_zerop (@2)
3645 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3646 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3648 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
3649 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
3652 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3653 (if (tree_fits_shwi_p (@1)
3654 && tree_to_shwi (@1) > 0
3655 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3656 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
3657 { constant_boolean_node (cmp == NE_EXPR, type); }
3658 (with { wide_int c1 = wi::to_wide (@1);
3659 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
3660 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
3661 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
3662 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
3664 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3665 (if (tree_fits_shwi_p (@1)
3666 && tree_to_shwi (@1) > 0
3667 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3668 (with { tree t0 = TREE_TYPE (@0);
3669 unsigned int prec = TYPE_PRECISION (t0);
3670 wide_int c1 = wi::to_wide (@1);
3671 wide_int c2 = wi::to_wide (@2);
3672 wide_int c3 = wi::to_wide (@3);
3673 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
3674 (if ((c2 & c3) != c3)
3675 { constant_boolean_node (cmp == NE_EXPR, type); }
3676 (if (TYPE_UNSIGNED (t0))
3677 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
3678 { constant_boolean_node (cmp == NE_EXPR, type); }
3679 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3680 { wide_int_to_tree (t0, c3 << c1); }))
3681 (with { wide_int smask = wi::arshift (sb, c1); }
3683 (if ((c2 & smask) == 0)
3684 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3685 { wide_int_to_tree (t0, c3 << c1); }))
3686 (if ((c3 & smask) == 0)
3687 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3688 { wide_int_to_tree (t0, c3 << c1); }))
3689 (if ((c2 & smask) != (c3 & smask))
3690 { constant_boolean_node (cmp == NE_EXPR, type); })
3691 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3692 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
3694 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3695 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3696 if the new mask might be further optimized. */
3697 (for shift (lshift rshift)
3699 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3701 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3702 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3703 && tree_fits_uhwi_p (@1)
3704 && tree_to_uhwi (@1) > 0
3705 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3708 unsigned int shiftc = tree_to_uhwi (@1);
3709 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3710 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3711 tree shift_type = TREE_TYPE (@3);
3714 if (shift == LSHIFT_EXPR)
3715 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3716 else if (shift == RSHIFT_EXPR
3717 && type_has_mode_precision_p (shift_type))
3719 prec = TYPE_PRECISION (TREE_TYPE (@3));
3721 /* See if more bits can be proven as zero because of
3724 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3726 tree inner_type = TREE_TYPE (@0);
3727 if (type_has_mode_precision_p (inner_type)
3728 && TYPE_PRECISION (inner_type) < prec)
3730 prec = TYPE_PRECISION (inner_type);
3731 /* See if we can shorten the right shift. */
3733 shift_type = inner_type;
3734 /* Otherwise X >> C1 is all zeros, so we'll optimize
3735 it into (X, 0) later on by making sure zerobits
3739 zerobits = HOST_WIDE_INT_M1U;
3742 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3743 zerobits <<= prec - shiftc;
3745 /* For arithmetic shift if sign bit could be set, zerobits
3746 can contain actually sign bits, so no transformation is
3747 possible, unless MASK masks them all away. In that
3748 case the shift needs to be converted into logical shift. */
3749 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3750 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3752 if ((mask & zerobits) == 0)
3753 shift_type = unsigned_type_for (TREE_TYPE (@3));
3759 /* ((X << 16) & 0xff00) is (X, 0). */
3760 (if ((mask & zerobits) == mask)
3761 { build_int_cst (type, 0); }
3762 (with { newmask = mask | zerobits; }
3763 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3766 /* Only do the transformation if NEWMASK is some integer
3768 for (prec = BITS_PER_UNIT;
3769 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3770 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3773 (if (prec < HOST_BITS_PER_WIDE_INT
3774 || newmask == HOST_WIDE_INT_M1U)
3776 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3777 (if (!tree_int_cst_equal (newmaskt, @2))
3778 (if (shift_type != TREE_TYPE (@3))
3779 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3780 (bit_and @4 { newmaskt; })))))))))))))
3782 /* ((1 << n) & M) != 0 -> n == log2 (M) */
3788 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3789 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3790 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3791 wi::exact_log2 (wi::to_wide (@1))); }))))
3793 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3794 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3795 (for shift (lshift rshift)
3796 (for bit_op (bit_and bit_xor bit_ior)
3798 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3799 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3800 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3802 (bit_op (shift (convert @0) @1) { mask; })))))))
3804 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3806 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3807 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3808 && (element_precision (TREE_TYPE (@0))
3809 <= element_precision (TREE_TYPE (@1))
3810 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3812 { tree shift_type = TREE_TYPE (@0); }
3813 (convert (rshift (convert:shift_type @1) @2)))))
3815 /* ~(~X >>r Y) -> X >>r Y
3816 ~(~X <<r Y) -> X <<r Y */
3817 (for rotate (lrotate rrotate)
3819 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3820 (if ((element_precision (TREE_TYPE (@0))
3821 <= element_precision (TREE_TYPE (@1))
3822 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3823 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3824 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3826 { tree rotate_type = TREE_TYPE (@0); }
3827 (convert (rotate (convert:rotate_type @1) @2))))))
3830 (for rotate (lrotate rrotate)
3831 invrot (rrotate lrotate)
3832 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3834 (cmp (rotate @1 @0) (rotate @2 @0))
3836 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3838 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3839 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3840 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3842 (cmp (rotate @0 @1) INTEGER_CST@2)
3843 (if (integer_zerop (@2) || integer_all_onesp (@2))
3846 /* Narrow a lshift by constant. */
3848 (convert (lshift:s@0 @1 INTEGER_CST@2))
3849 (if (INTEGRAL_TYPE_P (type)
3850 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3851 && !integer_zerop (@2)
3852 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
3853 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3854 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
3855 (lshift (convert @1) @2)
3856 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
3857 { build_zero_cst (type); }))))
3859 /* Simplifications of conversions. */
3861 /* Basic strip-useless-type-conversions / strip_nops. */
3862 (for cvt (convert view_convert float fix_trunc)
3865 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3866 || (GENERIC && type == TREE_TYPE (@0)))
3869 /* Contract view-conversions. */
3871 (view_convert (view_convert @0))
3874 /* For integral conversions with the same precision or pointer
3875 conversions use a NOP_EXPR instead. */
3878 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3879 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3880 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3883 /* Strip inner integral conversions that do not change precision or size, or
3884 zero-extend while keeping the same size (for bool-to-char). */
3886 (view_convert (convert@0 @1))
3887 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3888 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3889 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3890 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3891 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3892 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3895 /* Simplify a view-converted empty or single-element constructor. */
3897 (view_convert CONSTRUCTOR@0)
3899 { tree ctor = (TREE_CODE (@0) == SSA_NAME
3900 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
3902 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3903 { build_zero_cst (type); })
3904 (if (CONSTRUCTOR_NELTS (ctor) == 1
3905 && VECTOR_TYPE_P (TREE_TYPE (ctor))
3906 && operand_equal_p (TYPE_SIZE (type),
3907 TYPE_SIZE (TREE_TYPE
3908 (CONSTRUCTOR_ELT (ctor, 0)->value))))
3909 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
3911 /* Re-association barriers around constants and other re-association
3912 barriers can be removed. */
3914 (paren CONSTANT_CLASS_P@0)
3917 (paren (paren@1 @0))
3920 /* Handle cases of two conversions in a row. */
3921 (for ocvt (convert float fix_trunc)
3922 (for icvt (convert float)
3927 tree inside_type = TREE_TYPE (@0);
3928 tree inter_type = TREE_TYPE (@1);
3929 int inside_int = INTEGRAL_TYPE_P (inside_type);
3930 int inside_ptr = POINTER_TYPE_P (inside_type);
3931 int inside_float = FLOAT_TYPE_P (inside_type);
3932 int inside_vec = VECTOR_TYPE_P (inside_type);
3933 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3934 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3935 int inter_int = INTEGRAL_TYPE_P (inter_type);
3936 int inter_ptr = POINTER_TYPE_P (inter_type);
3937 int inter_float = FLOAT_TYPE_P (inter_type);
3938 int inter_vec = VECTOR_TYPE_P (inter_type);
3939 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3940 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3941 int final_int = INTEGRAL_TYPE_P (type);
3942 int final_ptr = POINTER_TYPE_P (type);
3943 int final_float = FLOAT_TYPE_P (type);
3944 int final_vec = VECTOR_TYPE_P (type);
3945 unsigned int final_prec = TYPE_PRECISION (type);
3946 int final_unsignedp = TYPE_UNSIGNED (type);
3949 /* In addition to the cases of two conversions in a row
3950 handled below, if we are converting something to its own
3951 type via an object of identical or wider precision, neither
3952 conversion is needed. */
3953 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3955 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3956 && (((inter_int || inter_ptr) && final_int)
3957 || (inter_float && final_float))
3958 && inter_prec >= final_prec)
3961 /* Likewise, if the intermediate and initial types are either both
3962 float or both integer, we don't need the middle conversion if the
3963 former is wider than the latter and doesn't change the signedness
3964 (for integers). Avoid this if the final type is a pointer since
3965 then we sometimes need the middle conversion. */
3966 (if (((inter_int && inside_int) || (inter_float && inside_float))
3967 && (final_int || final_float)
3968 && inter_prec >= inside_prec
3969 && (inter_float || inter_unsignedp == inside_unsignedp))
3972 /* If we have a sign-extension of a zero-extended value, we can
3973 replace that by a single zero-extension. Likewise if the
3974 final conversion does not change precision we can drop the
3975 intermediate conversion. */
3976 (if (inside_int && inter_int && final_int
3977 && ((inside_prec < inter_prec && inter_prec < final_prec
3978 && inside_unsignedp && !inter_unsignedp)
3979 || final_prec == inter_prec))
3982 /* Two conversions in a row are not needed unless:
3983 - some conversion is floating-point (overstrict for now), or
3984 - some conversion is a vector (overstrict for now), or
3985 - the intermediate type is narrower than both initial and
3987 - the intermediate type and innermost type differ in signedness,
3988 and the outermost type is wider than the intermediate, or
3989 - the initial type is a pointer type and the precisions of the
3990 intermediate and final types differ, or
3991 - the final type is a pointer type and the precisions of the
3992 initial and intermediate types differ. */
3993 (if (! inside_float && ! inter_float && ! final_float
3994 && ! inside_vec && ! inter_vec && ! final_vec
3995 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3996 && ! (inside_int && inter_int
3997 && inter_unsignedp != inside_unsignedp
3998 && inter_prec < final_prec)
3999 && ((inter_unsignedp && inter_prec > inside_prec)
4000 == (final_unsignedp && final_prec > inter_prec))
4001 && ! (inside_ptr && inter_prec != final_prec)
4002 && ! (final_ptr && inside_prec != inter_prec))
4005 /* A truncation to an unsigned type (a zero-extension) should be
4006 canonicalized as bitwise and of a mask. */
4007 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4008 && final_int && inter_int && inside_int
4009 && final_prec == inside_prec
4010 && final_prec > inter_prec
4012 (convert (bit_and @0 { wide_int_to_tree
4014 wi::mask (inter_prec, false,
4015 TYPE_PRECISION (inside_type))); })))
4017 /* If we are converting an integer to a floating-point that can
4018 represent it exactly and back to an integer, we can skip the
4019 floating-point conversion. */
4020 (if (GIMPLE /* PR66211 */
4021 && inside_int && inter_float && final_int &&
4022 (unsigned) significand_size (TYPE_MODE (inter_type))
4023 >= inside_prec - !inside_unsignedp)
4026 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4027 float_type. Only do the transformation if we do not need to preserve
4028 trapping behaviour, so require !flag_trapping_math. */
4031 (float (fix_trunc @0))
4032 (if (!flag_trapping_math
4033 && types_match (type, TREE_TYPE (@0))
4034 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4039 /* If we have a narrowing conversion to an integral type that is fed by a
4040 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4041 masks off bits outside the final type (and nothing else). */
4043 (convert (bit_and @0 INTEGER_CST@1))
4044 (if (INTEGRAL_TYPE_P (type)
4045 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4046 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4047 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4048 TYPE_PRECISION (type)), 0))
4052 /* (X /[ex] A) * A -> X. */
4054 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4057 /* Simplify (A / B) * B + (A % B) -> A. */
4058 (for div (trunc_div ceil_div floor_div round_div)
4059 mod (trunc_mod ceil_mod floor_mod round_mod)
4061 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4064 /* x / y * y == x -> x % y == 0. */
4066 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4067 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4068 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4070 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4071 (for op (plus minus)
4073 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4074 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4075 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4078 wi::overflow_type overflow;
4079 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4080 TYPE_SIGN (type), &overflow);
4082 (if (types_match (type, TREE_TYPE (@2))
4083 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4084 (op @0 { wide_int_to_tree (type, mul); })
4085 (with { tree utype = unsigned_type_for (type); }
4086 (convert (op (convert:utype @0)
4087 (mult (convert:utype @1) (convert:utype @2))))))))))
4089 /* Canonicalization of binary operations. */
4091 /* Convert X + -C into X - C. */
4093 (plus @0 REAL_CST@1)
4094 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4095 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4096 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4097 (minus @0 { tem; })))))
4099 /* Convert x+x into x*2. */
4102 (if (SCALAR_FLOAT_TYPE_P (type))
4103 (mult @0 { build_real (type, dconst2); })
4104 (if (INTEGRAL_TYPE_P (type))
4105 (mult @0 { build_int_cst (type, 2); }))))
4109 (minus integer_zerop @1)
4112 (pointer_diff integer_zerop @1)
4113 (negate (convert @1)))
4115 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4116 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4117 (-ARG1 + ARG0) reduces to -ARG1. */
4119 (minus real_zerop@0 @1)
4120 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4123 /* Transform x * -1 into -x. */
4125 (mult @0 integer_minus_onep)
4128 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4129 signed overflow for CST != 0 && CST != -1. */
4131 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4132 (if (TREE_CODE (@2) != INTEGER_CST
4134 && !integer_zerop (@1) && !integer_minus_onep (@1))
4135 (mult (mult @0 @2) @1)))
4137 /* True if we can easily extract the real and imaginary parts of a complex
4139 (match compositional_complex
4140 (convert? (complex @0 @1)))
4142 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4144 (complex (realpart @0) (imagpart @0))
4147 (realpart (complex @0 @1))
4150 (imagpart (complex @0 @1))
4153 /* Sometimes we only care about half of a complex expression. */
4155 (realpart (convert?:s (conj:s @0)))
4156 (convert (realpart @0)))
4158 (imagpart (convert?:s (conj:s @0)))
4159 (convert (negate (imagpart @0))))
4160 (for part (realpart imagpart)
4161 (for op (plus minus)
4163 (part (convert?:s@2 (op:s @0 @1)))
4164 (convert (op (part @0) (part @1))))))
4166 (realpart (convert?:s (CEXPI:s @0)))
4169 (imagpart (convert?:s (CEXPI:s @0)))
4172 /* conj(conj(x)) -> x */
4174 (conj (convert? (conj @0)))
4175 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4178 /* conj({x,y}) -> {x,-y} */
4180 (conj (convert?:s (complex:s @0 @1)))
4181 (with { tree itype = TREE_TYPE (type); }
4182 (complex (convert:itype @0) (negate (convert:itype @1)))))
4184 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4185 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
4186 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
4191 (bswap (bit_not (bswap @0)))
4193 (for bitop (bit_xor bit_ior bit_and)
4195 (bswap (bitop:c (bswap @0) @1))
4196 (bitop @0 (bswap @1))))
4199 (cmp (bswap@2 @0) (bswap @1))
4200 (with { tree ctype = TREE_TYPE (@2); }
4201 (cmp (convert:ctype @0) (convert:ctype @1))))
4203 (cmp (bswap @0) INTEGER_CST@1)
4204 (with { tree ctype = TREE_TYPE (@1); }
4205 (cmp (convert:ctype @0) (bswap! @1)))))
4206 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4208 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4210 (if (BITS_PER_UNIT == 8
4211 && tree_fits_uhwi_p (@2)
4212 && tree_fits_uhwi_p (@3))
4215 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4216 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4217 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4218 unsigned HOST_WIDE_INT lo = bits & 7;
4219 unsigned HOST_WIDE_INT hi = bits - lo;
4222 && mask < (256u>>lo)
4223 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4224 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4226 (bit_and (convert @1) @3)
4229 tree utype = unsigned_type_for (TREE_TYPE (@1));
4230 tree nst = build_int_cst (integer_type_node, ns);
4232 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4233 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4235 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4236 (if (BITS_PER_UNIT == 8
4237 && CHAR_TYPE_SIZE == 8
4238 && tree_fits_uhwi_p (@1))
4241 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4242 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4243 /* If the bswap was extended before the original shift, this
4244 byte (shift) has the sign of the extension, not the sign of
4245 the original shift. */
4246 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4248 /* Special case: logical right shift of sign-extended bswap.
4249 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4250 (if (TYPE_PRECISION (type) > prec
4251 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4252 && TYPE_UNSIGNED (type)
4253 && bits < prec && bits + 8 >= prec)
4254 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4255 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4256 (if (bits + 8 == prec)
4257 (if (TYPE_UNSIGNED (st))
4258 (convert (convert:unsigned_char_type_node @0))
4259 (convert (convert:signed_char_type_node @0)))
4260 (if (bits < prec && bits + 8 > prec)
4263 tree nst = build_int_cst (integer_type_node, bits & 7);
4264 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4265 : signed_char_type_node;
4267 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4268 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4270 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4271 (if (BITS_PER_UNIT == 8
4272 && tree_fits_uhwi_p (@1)
4273 && tree_to_uhwi (@1) < 256)
4276 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4277 tree utype = unsigned_type_for (TREE_TYPE (@0));
4278 tree nst = build_int_cst (integer_type_node, prec - 8);
4280 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4283 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
4285 /* Simplify constant conditions.
4286 Only optimize constant conditions when the selected branch
4287 has the same type as the COND_EXPR. This avoids optimizing
4288 away "c ? x : throw", where the throw has a void type.
4289 Note that we cannot throw away the fold-const.cc variant nor
4290 this one as we depend on doing this transform before possibly
4291 A ? B : B -> B triggers and the fold-const.cc one can optimize
4292 0 ? A : B to B even if A has side-effects. Something
4293 genmatch cannot handle. */
4295 (cond INTEGER_CST@0 @1 @2)
4296 (if (integer_zerop (@0))
4297 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4299 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4302 (vec_cond VECTOR_CST@0 @1 @2)
4303 (if (integer_all_onesp (@0))
4305 (if (integer_zerop (@0))
4308 /* Sink unary operations to branches, but only if we do fold both. */
4309 (for op (negate bit_not abs absu)
4311 (op (vec_cond:s @0 @1 @2))
4312 (vec_cond @0 (op! @1) (op! @2))))
4314 /* Sink binary operation to branches, but only if we can fold it. */
4315 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4316 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4317 trunc_mod ceil_mod floor_mod round_mod min max)
4318 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4320 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4321 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4323 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4325 (op (vec_cond:s @0 @1 @2) @3)
4326 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4328 (op @3 (vec_cond:s @0 @1 @2))
4329 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4332 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4333 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4336 int ibit = tree_log2 (@0);
4337 int ibit2 = tree_log2 (@1);
4341 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4343 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4344 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4347 int ibit = tree_log2 (@0);
4348 int ibit2 = tree_log2 (@1);
4352 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4354 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4357 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4359 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4361 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4364 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4366 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4368 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4369 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4372 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4373 TYPE_PRECISION(type)));
4374 int ibit2 = tree_log2 (@1);
4378 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4380 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4382 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4385 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4386 TYPE_PRECISION(type)));
4387 int ibit2 = tree_log2 (@1);
4391 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4393 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4396 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4398 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4400 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4403 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4405 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4409 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4410 Currently disabled after pass lvec because ARM understands
4411 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
4413 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4414 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4415 (vec_cond (bit_and @0 @3) @1 @2)))
4417 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4418 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4419 (vec_cond (bit_ior @0 @3) @1 @2)))
4421 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4422 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4423 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4425 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4426 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4427 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4429 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
4431 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4432 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4433 (vec_cond (bit_and @0 @1) @2 @3)))
4435 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4436 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4437 (vec_cond (bit_ior @0 @1) @2 @3)))
4439 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4440 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4441 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4443 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4444 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4445 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4447 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4448 types are compatible. */
4450 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4451 (if (VECTOR_BOOLEAN_TYPE_P (type)
4452 && types_match (type, TREE_TYPE (@0)))
4453 (if (integer_zerop (@1) && integer_all_onesp (@2))
4455 (if (integer_all_onesp (@1) && integer_zerop (@2))
4458 /* A few simplifications of "a ? CST1 : CST2". */
4459 /* NOTE: Only do this on gimple as the if-chain-to-switch
4460 optimization depends on the gimple to have if statements in it. */
4463 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4465 (if (integer_zerop (@2))
4467 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4468 (if (integer_onep (@1))
4469 (convert (convert:boolean_type_node @0)))
4470 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4471 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4473 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4475 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4476 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
4477 here as the powerof2cst case above will handle that case correctly. */
4478 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4479 (negate (convert (convert:boolean_type_node @0))))))
4480 (if (integer_zerop (@1))
4482 tree booltrue = constant_boolean_node (true, boolean_type_node);
4485 /* a ? 0 : 1 -> !a. */
4486 (if (integer_onep (@2))
4487 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4488 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4489 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
4491 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4493 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4495 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
4496 here as the powerof2cst case above will handle that case correctly. */
4497 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4498 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4507 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
4508 (if (INTEGRAL_TYPE_P (type)
4509 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4510 (cond @1 (convert @2) (convert @3))))
4512 /* Simplification moved from fold_cond_expr_with_comparison. It may also
4514 /* This pattern implements two kinds simplification:
4517 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4518 1) Conversions are type widening from smaller type.
4519 2) Const c1 equals to c2 after canonicalizing comparison.
4520 3) Comparison has tree code LT, LE, GT or GE.
4521 This specific pattern is needed when (cmp (convert x) c) may not
4522 be simplified by comparison patterns because of multiple uses of
4523 x. It also makes sense here because simplifying across multiple
4524 referred var is always benefitial for complicated cases.
4527 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
4528 (for cmp (lt le gt ge eq)
4530 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4533 tree from_type = TREE_TYPE (@1);
4534 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4535 enum tree_code code = ERROR_MARK;
4537 if (INTEGRAL_TYPE_P (from_type)
4538 && int_fits_type_p (@2, from_type)
4539 && (types_match (c1_type, from_type)
4540 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4541 && (TYPE_UNSIGNED (from_type)
4542 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4543 && (types_match (c2_type, from_type)
4544 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4545 && (TYPE_UNSIGNED (from_type)
4546 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4550 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4552 /* X <= Y - 1 equals to X < Y. */
4555 /* X > Y - 1 equals to X >= Y. */
4559 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4561 /* X < Y + 1 equals to X <= Y. */
4564 /* X >= Y + 1 equals to X > Y. */
4568 if (code != ERROR_MARK
4569 || wi::to_widest (@2) == wi::to_widest (@3))
4571 if (cmp == LT_EXPR || cmp == LE_EXPR)
4573 if (cmp == GT_EXPR || cmp == GE_EXPR)
4577 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4578 else if (int_fits_type_p (@3, from_type))
4582 (if (code == MAX_EXPR)
4583 (convert (max @1 (convert @2)))
4584 (if (code == MIN_EXPR)
4585 (convert (min @1 (convert @2)))
4586 (if (code == EQ_EXPR)
4587 (convert (cond (eq @1 (convert @3))
4588 (convert:from_type @3) (convert:from_type @2)))))))))
4590 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4592 1) OP is PLUS or MINUS.
4593 2) CMP is LT, LE, GT or GE.
4594 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4596 This pattern also handles special cases like:
4598 A) Operand x is a unsigned to signed type conversion and c1 is
4599 integer zero. In this case,
4600 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4601 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4602 B) Const c1 may not equal to (C3 op' C2). In this case we also
4603 check equality for (c1+1) and (c1-1) by adjusting comparison
4606 TODO: Though signed type is handled by this pattern, it cannot be
4607 simplified at the moment because C standard requires additional
4608 type promotion. In order to match&simplify it here, the IR needs
4609 to be cleaned up by other optimizers, i.e, VRP. */
4610 (for op (plus minus)
4611 (for cmp (lt le gt ge)
4613 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4614 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4615 (if (types_match (from_type, to_type)
4616 /* Check if it is special case A). */
4617 || (TYPE_UNSIGNED (from_type)
4618 && !TYPE_UNSIGNED (to_type)
4619 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4620 && integer_zerop (@1)
4621 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4624 wi::overflow_type overflow = wi::OVF_NONE;
4625 enum tree_code code, cmp_code = cmp;
4627 wide_int c1 = wi::to_wide (@1);
4628 wide_int c2 = wi::to_wide (@2);
4629 wide_int c3 = wi::to_wide (@3);
4630 signop sgn = TYPE_SIGN (from_type);
4632 /* Handle special case A), given x of unsigned type:
4633 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4634 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4635 if (!types_match (from_type, to_type))
4637 if (cmp_code == LT_EXPR)
4639 if (cmp_code == GE_EXPR)
4641 c1 = wi::max_value (to_type);
4643 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4644 compute (c3 op' c2) and check if it equals to c1 with op' being
4645 the inverted operator of op. Make sure overflow doesn't happen
4646 if it is undefined. */
4647 if (op == PLUS_EXPR)
4648 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4650 real_c1 = wi::add (c3, c2, sgn, &overflow);
4653 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4655 /* Check if c1 equals to real_c1. Boundary condition is handled
4656 by adjusting comparison operation if necessary. */
4657 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4660 /* X <= Y - 1 equals to X < Y. */
4661 if (cmp_code == LE_EXPR)
4663 /* X > Y - 1 equals to X >= Y. */
4664 if (cmp_code == GT_EXPR)
4667 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4670 /* X < Y + 1 equals to X <= Y. */
4671 if (cmp_code == LT_EXPR)
4673 /* X >= Y + 1 equals to X > Y. */
4674 if (cmp_code == GE_EXPR)
4677 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4679 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4681 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4686 (if (code == MAX_EXPR)
4687 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4688 { wide_int_to_tree (from_type, c2); })
4689 (if (code == MIN_EXPR)
4690 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4691 { wide_int_to_tree (from_type, c2); })))))))))
4694 /* A >= B ? A : B -> max (A, B) and friends. The code is still
4695 in fold_cond_expr_with_comparison for GENERIC folding with
4696 some extra constraints. */
4697 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
4699 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
4700 (convert3? @0) (convert4? @1))
4701 (if (!HONOR_SIGNED_ZEROS (type)
4702 && (/* Allow widening conversions of the compare operands as data. */
4703 (INTEGRAL_TYPE_P (type)
4704 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
4705 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
4706 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
4707 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
4708 /* Or sign conversions for the comparison. */
4709 || (types_match (type, TREE_TYPE (@0))
4710 && types_match (type, TREE_TYPE (@1)))))
4712 (if (cmp == EQ_EXPR)
4713 (if (VECTOR_TYPE_P (type))
4716 (if (cmp == NE_EXPR)
4717 (if (VECTOR_TYPE_P (type))
4720 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
4721 (if (!HONOR_NANS (type))
4722 (if (VECTOR_TYPE_P (type))
4723 (view_convert (min @c0 @c1))
4724 (convert (min @c0 @c1)))))
4725 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
4726 (if (!HONOR_NANS (type))
4727 (if (VECTOR_TYPE_P (type))
4728 (view_convert (max @c0 @c1))
4729 (convert (max @c0 @c1)))))
4730 (if (cmp == UNEQ_EXPR)
4731 (if (!HONOR_NANS (type))
4732 (if (VECTOR_TYPE_P (type))
4735 (if (cmp == LTGT_EXPR)
4736 (if (!HONOR_NANS (type))
4737 (if (VECTOR_TYPE_P (type))
4739 (convert @c0))))))))
4742 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
4744 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
4745 (if (!TYPE_SATURATING (type)
4746 && (TYPE_OVERFLOW_WRAPS (type)
4747 || !wi::only_sign_bit_p (wi::to_wide (@1)))
4748 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
4751 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
4753 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
4754 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
4757 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
4758 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
4760 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
4761 (if (TYPE_UNSIGNED (type))
4762 (cond (ge @0 @1) (negate @0) @2)))
4764 (for cnd (cond vec_cond)
4765 /* A ? B : (A ? X : C) -> A ? B : C. */
4767 (cnd @0 (cnd @0 @1 @2) @3)
4770 (cnd @0 @1 (cnd @0 @2 @3))
4772 /* A ? B : (!A ? C : X) -> A ? B : C. */
4773 /* ??? This matches embedded conditions open-coded because genmatch
4774 would generate matching code for conditions in separate stmts only.
4775 The following is still important to merge then and else arm cases
4776 from if-conversion. */
4778 (cnd @0 @1 (cnd @2 @3 @4))
4779 (if (inverse_conditions_p (@0, @2))
4782 (cnd @0 (cnd @1 @2 @3) @4)
4783 (if (inverse_conditions_p (@0, @1))
4786 /* A ? B : B -> B. */
4791 /* !A ? B : C -> A ? C : B. */
4793 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4796 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4797 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4798 Need to handle UN* comparisons.
4800 None of these transformations work for modes with signed
4801 zeros. If A is +/-0, the first two transformations will
4802 change the sign of the result (from +0 to -0, or vice
4803 versa). The last four will fix the sign of the result,
4804 even though the original expressions could be positive or
4805 negative, depending on the sign of A.
4807 Note that all these transformations are correct if A is
4808 NaN, since the two alternatives (A and -A) are also NaNs. */
4810 (for cnd (cond vec_cond)
4811 /* A == 0 ? A : -A same as -A */
4814 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4815 (if (!HONOR_SIGNED_ZEROS (type))
4818 (cnd (cmp @0 zerop) zerop (negate@1 @0))
4819 (if (!HONOR_SIGNED_ZEROS (type))
4822 /* A != 0 ? A : -A same as A */
4825 (cnd (cmp @0 zerop) @0 (negate @0))
4826 (if (!HONOR_SIGNED_ZEROS (type))
4829 (cnd (cmp @0 zerop) @0 integer_zerop)
4830 (if (!HONOR_SIGNED_ZEROS (type))
4833 /* A >=/> 0 ? A : -A same as abs (A) */
4836 (cnd (cmp @0 zerop) @0 (negate @0))
4837 (if (!HONOR_SIGNED_ZEROS (type)
4838 && !TYPE_UNSIGNED (type))
4840 /* A <=/< 0 ? A : -A same as -abs (A) */
4843 (cnd (cmp @0 zerop) @0 (negate @0))
4844 (if (!HONOR_SIGNED_ZEROS (type)
4845 && !TYPE_UNSIGNED (type))
4846 (if (ANY_INTEGRAL_TYPE_P (type)
4847 && !TYPE_OVERFLOW_WRAPS (type))
4849 tree utype = unsigned_type_for (type);
4851 (convert (negate (absu:utype @0))))
4852 (negate (abs @0)))))
4856 /* -(type)!A -> (type)A - 1. */
4858 (negate (convert?:s (logical_inverted_value:s @0)))
4859 (if (INTEGRAL_TYPE_P (type)
4860 && TREE_CODE (type) != BOOLEAN_TYPE
4861 && TYPE_PRECISION (type) > 1
4862 && TREE_CODE (@0) == SSA_NAME
4863 && ssa_name_has_boolean_range (@0))
4864 (plus (convert:type @0) { build_all_ones_cst (type); })))
4866 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4867 return all -1 or all 0 results. */
4868 /* ??? We could instead convert all instances of the vec_cond to negate,
4869 but that isn't necessarily a win on its own. */
4871 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4872 (if (VECTOR_TYPE_P (type)
4873 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4874 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4875 && (TYPE_MODE (TREE_TYPE (type))
4876 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4877 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4879 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4881 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4882 (if (VECTOR_TYPE_P (type)
4883 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4884 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4885 && (TYPE_MODE (TREE_TYPE (type))
4886 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4887 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4890 /* Simplifications of comparisons. */
4892 /* See if we can reduce the magnitude of a constant involved in a
4893 comparison by changing the comparison code. This is a canonicalization
4894 formerly done by maybe_canonicalize_comparison_1. */
4898 (cmp @0 uniform_integer_cst_p@1)
4899 (with { tree cst = uniform_integer_cst_p (@1); }
4900 (if (tree_int_cst_sgn (cst) == -1)
4901 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4902 wide_int_to_tree (TREE_TYPE (cst),
4908 (cmp @0 uniform_integer_cst_p@1)
4909 (with { tree cst = uniform_integer_cst_p (@1); }
4910 (if (tree_int_cst_sgn (cst) == 1)
4911 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4912 wide_int_to_tree (TREE_TYPE (cst),
4913 wi::to_wide (cst) - 1)); })))))
4915 /* We can simplify a logical negation of a comparison to the
4916 inverted comparison. As we cannot compute an expression
4917 operator using invert_tree_comparison we have to simulate
4918 that with expression code iteration. */
4919 (for cmp (tcc_comparison)
4920 icmp (inverted_tcc_comparison)
4921 ncmp (inverted_tcc_comparison_with_nans)
4922 /* Ideally we'd like to combine the following two patterns
4923 and handle some more cases by using
4924 (logical_inverted_value (cmp @0 @1))
4925 here but for that genmatch would need to "inline" that.
4926 For now implement what forward_propagate_comparison did. */
4928 (bit_not (cmp @0 @1))
4929 (if (VECTOR_TYPE_P (type)
4930 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4931 /* Comparison inversion may be impossible for trapping math,
4932 invert_tree_comparison will tell us. But we can't use
4933 a computed operator in the replacement tree thus we have
4934 to play the trick below. */
4935 (with { enum tree_code ic = invert_tree_comparison
4936 (cmp, HONOR_NANS (@0)); }
4942 (bit_xor (cmp @0 @1) integer_truep)
4943 (with { enum tree_code ic = invert_tree_comparison
4944 (cmp, HONOR_NANS (@0)); }
4949 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
4951 (ne (cmp@2 @0 @1) integer_zerop)
4952 (if (types_match (type, TREE_TYPE (@2)))
4955 (eq (cmp@2 @0 @1) integer_truep)
4956 (if (types_match (type, TREE_TYPE (@2)))
4959 (ne (cmp@2 @0 @1) integer_truep)
4960 (if (types_match (type, TREE_TYPE (@2)))
4961 (with { enum tree_code ic = invert_tree_comparison
4962 (cmp, HONOR_NANS (@0)); }
4968 (eq (cmp@2 @0 @1) integer_zerop)
4969 (if (types_match (type, TREE_TYPE (@2)))
4970 (with { enum tree_code ic = invert_tree_comparison
4971 (cmp, HONOR_NANS (@0)); }
4977 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4978 ??? The transformation is valid for the other operators if overflow
4979 is undefined for the type, but performing it here badly interacts
4980 with the transformation in fold_cond_expr_with_comparison which
4981 attempts to synthetize ABS_EXPR. */
4983 (for sub (minus pointer_diff)
4985 (cmp (sub@2 @0 @1) integer_zerop)
4986 (if (single_use (@2))
4989 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4990 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4993 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4994 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4995 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4996 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4997 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4998 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4999 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
5001 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5002 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5003 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5004 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5005 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5007 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5008 signed arithmetic case. That form is created by the compiler
5009 often enough for folding it to be of value. One example is in
5010 computing loop trip counts after Operator Strength Reduction. */
5011 (for cmp (simple_comparison)
5012 scmp (swapped_simple_comparison)
5014 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5015 /* Handle unfolded multiplication by zero. */
5016 (if (integer_zerop (@1))
5018 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5019 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5021 /* If @1 is negative we swap the sense of the comparison. */
5022 (if (tree_int_cst_sgn (@1) < 0)
5026 /* For integral types with undefined overflow fold
5027 x * C1 == C2 into x == C2 / C1 or false.
5028 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5032 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5033 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5034 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5035 && wi::to_wide (@1) != 0)
5036 (with { widest_int quot; }
5037 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5038 TYPE_SIGN (TREE_TYPE (@0)), "))
5039 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5040 { constant_boolean_node (cmp == NE_EXPR, type); }))
5041 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5042 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5043 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5046 tree itype = TREE_TYPE (@0);
5047 int p = TYPE_PRECISION (itype);
5048 wide_int m = wi::one (p + 1) << p;
5049 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5050 wide_int i = wide_int::from (wi::mod_inv (a, m),
5051 p, TYPE_SIGN (itype));
5052 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5055 /* Simplify comparison of something with itself. For IEEE
5056 floating-point, we can only do some of these simplifications. */
5060 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5061 || ! tree_expr_maybe_nan_p (@0))
5062 { constant_boolean_node (true, type); }
5064 /* With -ftrapping-math conversion to EQ loses an exception. */
5065 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5066 || ! flag_trapping_math))
5072 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5073 || ! tree_expr_maybe_nan_p (@0))
5074 { constant_boolean_node (false, type); })))
5075 (for cmp (unle unge uneq)
5078 { constant_boolean_node (true, type); }))
5079 (for cmp (unlt ungt)
5085 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5086 { constant_boolean_node (false, type); }))
5088 /* x == ~x -> false */
5089 /* x != ~x -> true */
5092 (cmp:c @0 (bit_not @0))
5093 { constant_boolean_node (cmp == NE_EXPR, type); }))
5095 /* Fold ~X op ~Y as Y op X. */
5096 (for cmp (simple_comparison)
5098 (cmp (bit_not@2 @0) (bit_not@3 @1))
5099 (if (single_use (@2) && single_use (@3))
5102 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
5103 (for cmp (simple_comparison)
5104 scmp (swapped_simple_comparison)
5106 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5107 (if (single_use (@2)
5108 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5109 (scmp @0 (bit_not @1)))))
5111 (for cmp (simple_comparison)
5114 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
5116 /* a CMP (-0) -> a CMP 0 */
5117 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5118 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5119 /* (-0) CMP b -> 0 CMP b. */
5120 (if (TREE_CODE (@0) == REAL_CST
5121 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5122 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5123 /* x != NaN is always true, other ops are always false. */
5124 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5125 && !tree_expr_signaling_nan_p (@1)
5126 && !tree_expr_maybe_signaling_nan_p (@0))
5127 { constant_boolean_node (cmp == NE_EXPR, type); })
5128 /* NaN != y is always true, other ops are always false. */
5129 (if (TREE_CODE (@0) == REAL_CST
5130 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5131 && !tree_expr_signaling_nan_p (@0)
5132 && !tree_expr_signaling_nan_p (@1))
5133 { constant_boolean_node (cmp == NE_EXPR, type); })
5134 /* Fold comparisons against infinity. */
5135 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5136 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5139 REAL_VALUE_TYPE max;
5140 enum tree_code code = cmp;
5141 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5143 code = swap_tree_comparison (code);
5146 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
5147 (if (code == GT_EXPR
5148 && !(HONOR_NANS (@0) && flag_trapping_math))
5149 { constant_boolean_node (false, type); })
5150 (if (code == LE_EXPR)
5151 /* x <= +Inf is always true, if we don't care about NaNs. */
5152 (if (! HONOR_NANS (@0))
5153 { constant_boolean_node (true, type); }
5154 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5155 an "invalid" exception. */
5156 (if (!flag_trapping_math)
5158 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5159 for == this introduces an exception for x a NaN. */
5160 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5162 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5164 (lt @0 { build_real (TREE_TYPE (@0), max); })
5165 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5166 /* x < +Inf is always equal to x <= DBL_MAX. */
5167 (if (code == LT_EXPR)
5168 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5170 (ge @0 { build_real (TREE_TYPE (@0), max); })
5171 (le @0 { build_real (TREE_TYPE (@0), max); }))))
5172 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5173 an exception for x a NaN so use an unordered comparison. */
5174 (if (code == NE_EXPR)
5175 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5176 (if (! HONOR_NANS (@0))
5178 (ge @0 { build_real (TREE_TYPE (@0), max); })
5179 (le @0 { build_real (TREE_TYPE (@0), max); }))
5181 (unge @0 { build_real (TREE_TYPE (@0), max); })
5182 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5184 /* If this is a comparison of a real constant with a PLUS_EXPR
5185 or a MINUS_EXPR of a real constant, we can convert it into a
5186 comparison with a revised real constant as long as no overflow
5187 occurs when unsafe_math_optimizations are enabled. */
5188 (if (flag_unsafe_math_optimizations)
5189 (for op (plus minus)
5191 (cmp (op @0 REAL_CST@1) REAL_CST@2)
5194 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5195 TREE_TYPE (@1), @2, @1);
5197 (if (tem && !TREE_OVERFLOW (tem))
5198 (cmp @0 { tem; }))))))
5200 /* Likewise, we can simplify a comparison of a real constant with
5201 a MINUS_EXPR whose first operand is also a real constant, i.e.
5202 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
5203 floating-point types only if -fassociative-math is set. */
5204 (if (flag_associative_math)
5206 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5207 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5208 (if (tem && !TREE_OVERFLOW (tem))
5209 (cmp { tem; } @1)))))
5211 /* Fold comparisons against built-in math functions. */
5212 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5215 (cmp (sq @0) REAL_CST@1)
5217 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5219 /* sqrt(x) < y is always false, if y is negative. */
5220 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5221 { constant_boolean_node (false, type); })
5222 /* sqrt(x) > y is always true, if y is negative and we
5223 don't care about NaNs, i.e. negative values of x. */
5224 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5225 { constant_boolean_node (true, type); })
5226 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5227 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5228 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5230 /* sqrt(x) < 0 is always false. */
5231 (if (cmp == LT_EXPR)
5232 { constant_boolean_node (false, type); })
5233 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
5234 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5235 { constant_boolean_node (true, type); })
5236 /* sqrt(x) <= 0 -> x == 0. */
5237 (if (cmp == LE_EXPR)
5239 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
5240 == or !=. In the last case:
5242 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5244 if x is negative or NaN. Due to -funsafe-math-optimizations,
5245 the results for other x follow from natural arithmetic. */
5247 (if ((cmp == LT_EXPR
5251 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5252 /* Give up for -frounding-math. */
5253 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5257 enum tree_code ncmp = cmp;
5258 const real_format *fmt
5259 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5260 real_arithmetic (&c2, MULT_EXPR,
5261 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5262 real_convert (&c2, fmt, &c2);
5263 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5264 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
5265 if (!REAL_VALUE_ISINF (c2))
5267 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5268 build_real (TREE_TYPE (@0), c2));
5269 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5271 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5272 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5273 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5274 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5275 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5276 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5279 /* With rounding to even, sqrt of up to 3 different values
5280 gives the same normal result, so in some cases c2 needs
5282 REAL_VALUE_TYPE c2alt, tow;
5283 if (cmp == LT_EXPR || cmp == GE_EXPR)
5287 real_nextafter (&c2alt, fmt, &c2, &tow);
5288 real_convert (&c2alt, fmt, &c2alt);
5289 if (REAL_VALUE_ISINF (c2alt))
5293 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5294 build_real (TREE_TYPE (@0), c2alt));
5295 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5297 else if (real_equal (&TREE_REAL_CST (c3),
5298 &TREE_REAL_CST (@1)))
5304 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5305 (if (REAL_VALUE_ISINF (c2))
5306 /* sqrt(x) > y is x == +Inf, when y is very large. */
5307 (if (HONOR_INFINITIES (@0))
5308 (eq @0 { build_real (TREE_TYPE (@0), c2); })
5309 { constant_boolean_node (false, type); })
5310 /* sqrt(x) > c is the same as x > c*c. */
5311 (if (ncmp != ERROR_MARK)
5312 (if (ncmp == GE_EXPR)
5313 (ge @0 { build_real (TREE_TYPE (@0), c2); })
5314 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5315 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
5316 (if (REAL_VALUE_ISINF (c2))
5318 /* sqrt(x) < y is always true, when y is a very large
5319 value and we don't care about NaNs or Infinities. */
5320 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5321 { constant_boolean_node (true, type); })
5322 /* sqrt(x) < y is x != +Inf when y is very large and we
5323 don't care about NaNs. */
5324 (if (! HONOR_NANS (@0))
5325 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5326 /* sqrt(x) < y is x >= 0 when y is very large and we
5327 don't care about Infinities. */
5328 (if (! HONOR_INFINITIES (@0))
5329 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5330 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5333 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5334 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5335 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5336 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5337 (if (ncmp == LT_EXPR)
5338 (lt @0 { build_real (TREE_TYPE (@0), c2); })
5339 (le @0 { build_real (TREE_TYPE (@0), c2); }))
5340 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5341 (if (ncmp != ERROR_MARK && GENERIC)
5342 (if (ncmp == LT_EXPR)
5344 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5345 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5347 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5348 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5349 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
5351 (cmp (sq @0) (sq @1))
5352 (if (! HONOR_NANS (@0))
5355 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
5356 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5357 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
5359 (cmp (float@0 @1) (float @2))
5360 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5361 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5364 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5365 tree type1 = TREE_TYPE (@1);
5366 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5367 tree type2 = TREE_TYPE (@2);
5368 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5370 (if (fmt.can_represent_integral_type_p (type1)
5371 && fmt.can_represent_integral_type_p (type2))
5372 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5373 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5374 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5375 && type1_signed_p >= type2_signed_p)
5376 (icmp @1 (convert @2))
5377 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5378 && type1_signed_p <= type2_signed_p)
5379 (icmp (convert:type2 @1) @2)
5380 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5381 && type1_signed_p == type2_signed_p)
5382 (icmp @1 @2))))))))))
5384 /* Optimize various special cases of (FTYPE) N CMP CST. */
5385 (for cmp (lt le eq ne ge gt)
5386 icmp (le le eq ne ge ge)
5388 (cmp (float @0) REAL_CST@1)
5389 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5390 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5393 tree itype = TREE_TYPE (@0);
5394 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5395 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5396 /* Be careful to preserve any potential exceptions due to
5397 NaNs. qNaNs are ok in == or != context.
5398 TODO: relax under -fno-trapping-math or
5399 -fno-signaling-nans. */
5401 = real_isnan (cst) && (cst->signalling
5402 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5404 /* TODO: allow non-fitting itype and SNaNs when
5405 -fno-trapping-math. */
5406 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5409 signop isign = TYPE_SIGN (itype);
5410 REAL_VALUE_TYPE imin, imax;
5411 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5412 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5414 REAL_VALUE_TYPE icst;
5415 if (cmp == GT_EXPR || cmp == GE_EXPR)
5416 real_ceil (&icst, fmt, cst);
5417 else if (cmp == LT_EXPR || cmp == LE_EXPR)
5418 real_floor (&icst, fmt, cst);
5420 real_trunc (&icst, fmt, cst);
5422 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5424 bool overflow_p = false;
5426 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5429 /* Optimize cases when CST is outside of ITYPE's range. */
5430 (if (real_compare (LT_EXPR, cst, &imin))
5431 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5433 (if (real_compare (GT_EXPR, cst, &imax))
5434 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5436 /* Remove cast if CST is an integer representable by ITYPE. */
5438 (cmp @0 { gcc_assert (!overflow_p);
5439 wide_int_to_tree (itype, icst_val); })
5441 /* When CST is fractional, optimize
5442 (FTYPE) N == CST -> 0
5443 (FTYPE) N != CST -> 1. */
5444 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5445 { constant_boolean_node (cmp == NE_EXPR, type); })
5446 /* Otherwise replace with sensible integer constant. */
5449 gcc_checking_assert (!overflow_p);
5451 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5453 /* Fold A /[ex] B CMP C to A CMP B * C. */
5456 (cmp (exact_div @0 @1) INTEGER_CST@2)
5457 (if (!integer_zerop (@1))
5458 (if (wi::to_wide (@2) == 0)
5460 (if (TREE_CODE (@1) == INTEGER_CST)
5463 wi::overflow_type ovf;
5464 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5465 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5468 { constant_boolean_node (cmp == NE_EXPR, type); }
5469 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5470 (for cmp (lt le gt ge)
5472 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5473 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5476 wi::overflow_type ovf;
5477 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5478 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5481 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5482 TYPE_SIGN (TREE_TYPE (@2)))
5483 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5484 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5486 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5488 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5489 For large C (more than min/B+2^size), this is also true, with the
5490 multiplication computed modulo 2^size.
5491 For intermediate C, this just tests the sign of A. */
5492 (for cmp (lt le gt ge)
5495 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5496 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5497 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5498 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5501 tree utype = TREE_TYPE (@2);
5502 wide_int denom = wi::to_wide (@1);
5503 wide_int right = wi::to_wide (@2);
5504 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5505 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5506 bool small = wi::leu_p (right, smax);
5507 bool large = wi::geu_p (right, smin);
5509 (if (small || large)
5510 (cmp (convert:utype @0) (mult @2 (convert @1)))
5511 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5513 /* Unordered tests if either argument is a NaN. */
5515 (bit_ior (unordered @0 @0) (unordered @1 @1))
5516 (if (types_match (@0, @1))
5519 (bit_and (ordered @0 @0) (ordered @1 @1))
5520 (if (types_match (@0, @1))
5523 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5526 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5529 /* Simple range test simplifications. */
5530 /* A < B || A >= B -> true. */
5531 (for test1 (lt le le le ne ge)
5532 test2 (ge gt ge ne eq ne)
5534 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5535 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5536 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5537 { constant_boolean_node (true, type); })))
5538 /* A < B && A >= B -> false. */
5539 (for test1 (lt lt lt le ne eq)
5540 test2 (ge gt eq gt eq gt)
5542 (bit_and:c (test1 @0 @1) (test2 @0 @1))
5543 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5544 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5545 { constant_boolean_node (false, type); })))
5547 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5548 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
5550 Note that comparisons
5551 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
5552 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
5553 will be canonicalized to above so there's no need to
5560 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5561 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5564 tree ty = TREE_TYPE (@0);
5565 unsigned prec = TYPE_PRECISION (ty);
5566 wide_int mask = wi::to_wide (@2, prec);
5567 wide_int rhs = wi::to_wide (@3, prec);
5568 signop sgn = TYPE_SIGN (ty);
5570 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5571 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5572 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5573 { build_zero_cst (ty); }))))))
5575 /* -A CMP -B -> B CMP A. */
5576 (for cmp (tcc_comparison)
5577 scmp (swapped_tcc_comparison)
5579 (cmp (negate @0) (negate @1))
5580 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5581 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5582 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5585 (cmp (negate @0) CONSTANT_CLASS_P@1)
5586 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5587 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5588 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5589 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5590 (if (tem && !TREE_OVERFLOW (tem))
5591 (scmp @0 { tem; }))))))
5593 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
5596 (op (abs @0) zerop@1)
5599 /* From fold_sign_changed_comparison and fold_widened_comparison.
5600 FIXME: the lack of symmetry is disturbing. */
5601 (for cmp (simple_comparison)
5603 (cmp (convert@0 @00) (convert?@1 @10))
5604 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5605 /* Disable this optimization if we're casting a function pointer
5606 type on targets that require function pointer canonicalization. */
5607 && !(targetm.have_canonicalize_funcptr_for_compare ()
5608 && ((POINTER_TYPE_P (TREE_TYPE (@00))
5609 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5610 || (POINTER_TYPE_P (TREE_TYPE (@10))
5611 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5613 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5614 && (TREE_CODE (@10) == INTEGER_CST
5616 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5619 && !POINTER_TYPE_P (TREE_TYPE (@00))
5620 /* (int)bool:32 != (int)uint is not the same as
5621 bool:32 != (bool:32)uint since boolean types only have two valid
5622 values independent of their precision. */
5623 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
5624 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
5625 /* ??? The special-casing of INTEGER_CST conversion was in the original
5626 code and here to avoid a spurious overflow flag on the resulting
5627 constant which fold_convert produces. */
5628 (if (TREE_CODE (@1) == INTEGER_CST)
5629 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5630 TREE_OVERFLOW (@1)); })
5631 (cmp @00 (convert @1)))
5633 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5634 /* If possible, express the comparison in the shorter mode. */
5635 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5636 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5637 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5638 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5639 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5640 || ((TYPE_PRECISION (TREE_TYPE (@00))
5641 >= TYPE_PRECISION (TREE_TYPE (@10)))
5642 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5643 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5644 || (TREE_CODE (@10) == INTEGER_CST
5645 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5646 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5647 (cmp @00 (convert @10))
5648 (if (TREE_CODE (@10) == INTEGER_CST
5649 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5650 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5653 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5654 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5655 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5656 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5658 (if (above || below)
5659 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5660 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5661 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5662 { constant_boolean_node (above ? true : false, type); }
5663 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5664 { constant_boolean_node (above ? false : true, type); })))))))))
5665 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
5666 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
5667 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5668 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
5669 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5670 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
5673 tree type1 = TREE_TYPE (@10);
5674 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
5676 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
5677 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
5678 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
5679 type1 = float_type_node;
5680 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
5681 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
5682 type1 = double_type_node;
5685 = (TYPE_PRECISION (TREE_TYPE (@00)) > TYPE_PRECISION (type1)
5686 ? TREE_TYPE (@00) : type1);
5688 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (newtype))
5689 (cmp (convert:newtype @00) (convert:newtype @10))))))))
5694 /* SSA names are canonicalized to 2nd place. */
5695 (cmp addr@0 SSA_NAME@1)
5697 { poly_int64 off; tree base; }
5698 /* A local variable can never be pointed to by
5699 the default SSA name of an incoming parameter. */
5700 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5701 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5702 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5703 && TREE_CODE (base) == VAR_DECL
5704 && auto_var_in_fn_p (base, current_function_decl))
5705 (if (cmp == NE_EXPR)
5706 { constant_boolean_node (true, type); }
5707 { constant_boolean_node (false, type); })
5708 /* If the address is based on @1 decide using the offset. */
5709 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5710 && TREE_CODE (base) == MEM_REF
5711 && TREE_OPERAND (base, 0) == @1)
5712 (with { off += mem_ref_offset (base).force_shwi (); }
5713 (if (known_ne (off, 0))
5714 { constant_boolean_node (cmp == NE_EXPR, type); }
5715 (if (known_eq (off, 0))
5716 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5718 /* Equality compare simplifications from fold_binary */
5721 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5722 Similarly for NE_EXPR. */
5724 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5725 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5726 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5727 { constant_boolean_node (cmp == NE_EXPR, type); }))
5729 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5731 (cmp (bit_xor @0 @1) integer_zerop)
5734 /* (X ^ Y) == Y becomes X == 0.
5735 Likewise (X ^ Y) == X becomes Y == 0. */
5737 (cmp:c (bit_xor:c @0 @1) @0)
5738 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5740 /* (X & Y) == X becomes (X & ~Y) == 0. */
5742 (cmp:c (bit_and:c @0 @1) @0)
5743 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5745 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5746 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5747 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5748 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5749 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5750 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5751 && !wi::neg_p (wi::to_wide (@1)))
5752 (cmp (bit_and @0 (convert (bit_not @1)))
5753 { build_zero_cst (TREE_TYPE (@0)); })))
5755 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5757 (cmp:c (bit_ior:c @0 @1) @1)
5758 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5760 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5762 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5763 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5764 (cmp @0 (bit_xor @1 (convert @2)))))
5767 (cmp (convert? addr@0) integer_zerop)
5768 (if (tree_single_nonzero_warnv_p (@0, NULL))
5769 { constant_boolean_node (cmp == NE_EXPR, type); }))
5771 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5773 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5774 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5776 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5777 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5778 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5779 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5784 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5785 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5786 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5787 && types_match (@0, @1))
5788 (ncmp (bit_xor @0 @1) @2)))))
5789 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5790 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5794 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5795 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5796 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5797 && types_match (@0, @1))
5798 (ncmp (bit_xor @0 @1) @2))))
5800 /* If we have (A & C) == C where C is a power of 2, convert this into
5801 (A & C) != 0. Similarly for NE_EXPR. */
5805 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5806 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5809 /* From fold_binary_op_with_conditional_arg handle the case of
5810 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
5811 compares simplify. */
5812 (for cmp (simple_comparison)
5814 (cmp:c (cond @0 @1 @2) @3)
5815 /* Do not move possibly trapping operations into the conditional as this
5816 pessimizes code and causes gimplification issues when applied late. */
5817 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
5818 || !operation_could_trap_p (cmp, true, false, @3))
5819 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
5823 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5824 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5826 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5827 (if (INTEGRAL_TYPE_P (type)
5828 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5829 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5830 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5833 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5835 (if (cmp == LT_EXPR)
5836 (bit_xor (convert (rshift @0 {shifter;})) @1)
5837 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5838 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5839 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5841 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5842 (if (INTEGRAL_TYPE_P (type)
5843 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5844 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5845 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5848 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5850 (if (cmp == GE_EXPR)
5851 (bit_xor (convert (rshift @0 {shifter;})) @1)
5852 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5854 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5855 convert this into a shift followed by ANDing with D. */
5858 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5859 INTEGER_CST@2 integer_zerop)
5860 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5862 int shift = (wi::exact_log2 (wi::to_wide (@2))
5863 - wi::exact_log2 (wi::to_wide (@1)));
5867 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5869 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5872 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5873 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5877 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5878 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5879 && type_has_mode_precision_p (TREE_TYPE (@0))
5880 && element_precision (@2) >= element_precision (@0)
5881 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5882 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5883 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5885 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5886 this into a right shift or sign extension followed by ANDing with C. */
5889 (lt @0 integer_zerop)
5890 INTEGER_CST@1 integer_zerop)
5891 (if (integer_pow2p (@1)
5892 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5894 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5898 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5900 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5901 sign extension followed by AND with C will achieve the effect. */
5902 (bit_and (convert @0) @1)))))
5904 /* When the addresses are not directly of decls compare base and offset.
5905 This implements some remaining parts of fold_comparison address
5906 comparisons but still no complete part of it. Still it is good
5907 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5908 (for cmp (simple_comparison)
5910 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5913 poly_int64 off0, off1;
5915 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
5916 off0, off1, GENERIC);
5920 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5921 { constant_boolean_node (known_eq (off0, off1), type); })
5922 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5923 { constant_boolean_node (known_ne (off0, off1), type); })
5924 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5925 { constant_boolean_node (known_lt (off0, off1), type); })
5926 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5927 { constant_boolean_node (known_le (off0, off1), type); })
5928 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5929 { constant_boolean_node (known_ge (off0, off1), type); })
5930 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5931 { constant_boolean_node (known_gt (off0, off1), type); }))
5934 (if (cmp == EQ_EXPR)
5935 { constant_boolean_node (false, type); })
5936 (if (cmp == NE_EXPR)
5937 { constant_boolean_node (true, type); })))))))
5939 /* Simplify pointer equality compares using PTA. */
5943 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5944 && ptrs_compare_unequal (@0, @1))
5945 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5947 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5948 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5949 Disable the transform if either operand is pointer to function.
5950 This broke pr22051-2.c for arm where function pointer
5951 canonicalizaion is not wanted. */
5955 (cmp (convert @0) INTEGER_CST@1)
5956 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5957 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5958 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5959 /* Don't perform this optimization in GENERIC if @0 has reference
5960 type when sanitizing. See PR101210. */
5962 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5963 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5964 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5965 && POINTER_TYPE_P (TREE_TYPE (@1))
5966 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5967 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5968 (cmp @0 (convert @1)))))
5970 /* Non-equality compare simplifications from fold_binary */
5971 (for cmp (lt gt le ge)
5972 /* Comparisons with the highest or lowest possible integer of
5973 the specified precision will have known values. */
5975 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5976 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5977 || POINTER_TYPE_P (TREE_TYPE (@1))
5978 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5979 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5982 tree cst = uniform_integer_cst_p (@1);
5983 tree arg1_type = TREE_TYPE (cst);
5984 unsigned int prec = TYPE_PRECISION (arg1_type);
5985 wide_int max = wi::max_value (arg1_type);
5986 wide_int signed_max = wi::max_value (prec, SIGNED);
5987 wide_int min = wi::min_value (arg1_type);
5990 (if (wi::to_wide (cst) == max)
5992 (if (cmp == GT_EXPR)
5993 { constant_boolean_node (false, type); })
5994 (if (cmp == GE_EXPR)
5996 (if (cmp == LE_EXPR)
5997 { constant_boolean_node (true, type); })
5998 (if (cmp == LT_EXPR)
6000 (if (wi::to_wide (cst) == min)
6002 (if (cmp == LT_EXPR)
6003 { constant_boolean_node (false, type); })
6004 (if (cmp == LE_EXPR)
6006 (if (cmp == GE_EXPR)
6007 { constant_boolean_node (true, type); })
6008 (if (cmp == GT_EXPR)
6010 (if (wi::to_wide (cst) == max - 1)
6012 (if (cmp == GT_EXPR)
6013 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6014 wide_int_to_tree (TREE_TYPE (cst),
6017 (if (cmp == LE_EXPR)
6018 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6019 wide_int_to_tree (TREE_TYPE (cst),
6022 (if (wi::to_wide (cst) == min + 1)
6024 (if (cmp == GE_EXPR)
6025 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6026 wide_int_to_tree (TREE_TYPE (cst),
6029 (if (cmp == LT_EXPR)
6030 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6031 wide_int_to_tree (TREE_TYPE (cst),
6034 (if (wi::to_wide (cst) == signed_max
6035 && TYPE_UNSIGNED (arg1_type)
6036 /* We will flip the signedness of the comparison operator
6037 associated with the mode of @1, so the sign bit is
6038 specified by this mode. Check that @1 is the signed
6039 max associated with this sign bit. */
6040 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6041 /* signed_type does not work on pointer types. */
6042 && INTEGRAL_TYPE_P (arg1_type))
6043 /* The following case also applies to X < signed_max+1
6044 and X >= signed_max+1 because previous transformations. */
6045 (if (cmp == LE_EXPR || cmp == GT_EXPR)
6046 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6048 (if (cst == @1 && cmp == LE_EXPR)
6049 (ge (convert:st @0) { build_zero_cst (st); }))
6050 (if (cst == @1 && cmp == GT_EXPR)
6051 (lt (convert:st @0) { build_zero_cst (st); }))
6052 (if (cmp == LE_EXPR)
6053 (ge (view_convert:st @0) { build_zero_cst (st); }))
6054 (if (cmp == GT_EXPR)
6055 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6057 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6058 /* If the second operand is NaN, the result is constant. */
6061 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6062 && (cmp != LTGT_EXPR || ! flag_trapping_math))
6063 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6064 ? false : true, type); })))
6066 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
6070 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6071 { constant_boolean_node (true, type); })
6072 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6073 { constant_boolean_node (false, type); })))
6075 /* Fold ORDERED if either operand must be NaN, or neither can be. */
6079 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6080 { constant_boolean_node (false, type); })
6081 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6082 { constant_boolean_node (true, type); })))
6084 /* bool_var != 0 becomes bool_var. */
6086 (ne @0 integer_zerop)
6087 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6088 && types_match (type, TREE_TYPE (@0)))
6090 /* bool_var == 1 becomes bool_var. */
6092 (eq @0 integer_onep)
6093 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6094 && types_match (type, TREE_TYPE (@0)))
6097 bool_var == 0 becomes !bool_var or
6098 bool_var != 1 becomes !bool_var
6099 here because that only is good in assignment context as long
6100 as we require a tcc_comparison in GIMPLE_CONDs where we'd
6101 replace if (x == 0) with tem = ~x; if (tem != 0) which is
6102 clearly less optimal and which we'll transform again in forwprop. */
6104 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6105 where ~Y + 1 == pow2 and Z = ~Y. */
6106 (for cst (VECTOR_CST INTEGER_CST)
6110 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6111 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6112 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6113 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6114 ? optab_vector : optab_default;
6115 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6116 (if (target_supports_op_p (utype, icmp, optab)
6117 || (optimize_vectors_before_lowering_p ()
6118 && (!target_supports_op_p (type, cmp, optab)
6119 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6120 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6122 (icmp (view_convert:utype @0) { csts; })))))))))
6124 /* When one argument is a constant, overflow detection can be simplified.
6125 Currently restricted to single use so as not to interfere too much with
6126 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6127 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
6128 (for cmp (lt le ge gt)
6131 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6132 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6133 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6134 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6135 && wi::to_wide (@1) != 0
6138 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6139 signop sign = TYPE_SIGN (TREE_TYPE (@0));
6141 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6142 wi::max_value (prec, sign)
6143 - wi::to_wide (@1)); })))))
6145 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6146 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6147 expects the long form, so we restrict the transformation for now. */
6150 (cmp:c (minus@2 @0 @1) @0)
6151 (if (single_use (@2)
6152 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6153 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6156 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
6159 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6160 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6161 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6164 /* Testing for overflow is unnecessary if we already know the result. */
6169 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6170 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6171 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6172 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6177 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6178 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6179 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6180 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6182 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6183 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
6187 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6188 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6189 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6190 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6192 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6193 is at least twice as wide as type of A and B, simplify to
6194 __builtin_mul_overflow (A, B, <unused>). */
6197 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6199 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6200 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6201 && TYPE_UNSIGNED (TREE_TYPE (@0))
6202 && (TYPE_PRECISION (TREE_TYPE (@3))
6203 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6204 && tree_fits_uhwi_p (@2)
6205 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6206 && types_match (@0, @1)
6207 && type_has_mode_precision_p (TREE_TYPE (@0))
6208 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6209 != CODE_FOR_nothing))
6210 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6211 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6213 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
6214 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6216 (ovf (convert@2 @0) @1)
6217 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6218 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6219 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6220 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6223 (ovf @1 (convert@2 @0))
6224 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6225 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6226 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6227 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6230 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6231 are unsigned to x > (umax / cst). Similarly for signed type, but
6232 in that case it needs to be outside of a range. */
6234 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6235 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6236 && TYPE_MAX_VALUE (TREE_TYPE (@0))
6237 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6238 && int_fits_type_p (@1, TREE_TYPE (@0)))
6239 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6240 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6241 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6242 (if (integer_minus_onep (@1))
6243 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6246 tree div = fold_convert (TREE_TYPE (@0), @1);
6247 tree lo = int_const_binop (TRUNC_DIV_EXPR,
6248 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6249 tree hi = int_const_binop (TRUNC_DIV_EXPR,
6250 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6251 tree etype = range_check_type (TREE_TYPE (@0));
6254 if (wi::neg_p (wi::to_wide (div)))
6256 lo = fold_convert (etype, lo);
6257 hi = fold_convert (etype, hi);
6258 hi = int_const_binop (MINUS_EXPR, hi, lo);
6262 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
6264 /* Simplification of math builtins. These rules must all be optimizations
6265 as well as IL simplifications. If there is a possibility that the new
6266 form could be a pessimization, the rule should go in the canonicalization
6267 section that follows this one.
6269 Rules can generally go in this section if they satisfy one of
6272 - the rule describes an identity
6274 - the rule replaces calls with something as simple as addition or
6277 - the rule contains unary calls only and simplifies the surrounding
6278 arithmetic. (The idea here is to exclude non-unary calls in which
6279 one operand is constant and in which the call is known to be cheap
6280 when the operand has that value.) */
6282 (if (flag_unsafe_math_optimizations)
6283 /* Simplify sqrt(x) * sqrt(x) -> x. */
6285 (mult (SQRT_ALL@1 @0) @1)
6286 (if (!tree_expr_maybe_signaling_nan_p (@0))
6289 (for op (plus minus)
6290 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
6294 (rdiv (op @0 @2) @1)))
6296 (for cmp (lt le gt ge)
6297 neg_cmp (gt ge lt le)
6298 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
6300 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6302 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6304 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6305 || (real_zerop (tem) && !real_zerop (@1))))
6307 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6309 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6310 (neg_cmp @0 { tem; })))))))
6312 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
6313 (for root (SQRT CBRT)
6315 (mult (root:s @0) (root:s @1))
6316 (root (mult @0 @1))))
6318 /* Simplify expN(x) * expN(y) -> expN(x+y). */
6319 (for exps (EXP EXP2 EXP10 POW10)
6321 (mult (exps:s @0) (exps:s @1))
6322 (exps (plus @0 @1))))
6324 /* Simplify a/root(b/c) into a*root(c/b). */
6325 (for root (SQRT CBRT)
6327 (rdiv @0 (root:s (rdiv:s @1 @2)))
6328 (mult @0 (root (rdiv @2 @1)))))
6330 /* Simplify x/expN(y) into x*expN(-y). */
6331 (for exps (EXP EXP2 EXP10 POW10)
6333 (rdiv @0 (exps:s @1))
6334 (mult @0 (exps (negate @1)))))
6336 (for logs (LOG LOG2 LOG10 LOG10)
6337 exps (EXP EXP2 EXP10 POW10)
6338 /* logN(expN(x)) -> x. */
6342 /* expN(logN(x)) -> x. */
6347 /* Optimize logN(func()) for various exponential functions. We
6348 want to determine the value "x" and the power "exponent" in
6349 order to transform logN(x**exponent) into exponent*logN(x). */
6350 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
6351 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
6354 (if (SCALAR_FLOAT_TYPE_P (type))
6360 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
6361 x = build_real_truncate (type, dconst_e ());
6364 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
6365 x = build_real (type, dconst2);
6369 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
6371 REAL_VALUE_TYPE dconst10;
6372 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6373 x = build_real (type, dconst10);
6380 (mult (logs { x; }) @0)))))
6388 (if (SCALAR_FLOAT_TYPE_P (type))
6394 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
6395 x = build_real (type, dconsthalf);
6398 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
6399 x = build_real_truncate (type, dconst_third ());
6405 (mult { x; } (logs @0))))))
6407 /* logN(pow(x,exponent)) -> exponent*logN(x). */
6408 (for logs (LOG LOG2 LOG10)
6412 (mult @1 (logs @0))))
6414 /* pow(C,x) -> exp(log(C)*x) if C > 0,
6415 or if C is a positive power of 2,
6416 pow(C,x) -> exp2(log2(C)*x). */
6424 (pows REAL_CST@0 @1)
6425 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6426 && real_isfinite (TREE_REAL_CST_PTR (@0))
6427 /* As libmvec doesn't have a vectorized exp2, defer optimizing
6428 the use_exp2 case until after vectorization. It seems actually
6429 beneficial for all constants to postpone this until later,
6430 because exp(log(C)*x), while faster, will have worse precision
6431 and if x folds into a constant too, that is unnecessary
6433 && canonicalize_math_after_vectorization_p ())
6435 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
6436 bool use_exp2 = false;
6437 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
6438 && value->cl == rvc_normal)
6440 REAL_VALUE_TYPE frac_rvt = *value;
6441 SET_REAL_EXP (&frac_rvt, 1);
6442 if (real_equal (&frac_rvt, &dconst1))
6447 (if (optimize_pow_to_exp (@0, @1))
6448 (exps (mult (logs @0) @1)))
6449 (exp2s (mult (log2s @0) @1)))))))
6452 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
6454 exps (EXP EXP2 EXP10 POW10)
6455 logs (LOG LOG2 LOG10 LOG10)
6457 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6458 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6459 && real_isfinite (TREE_REAL_CST_PTR (@0)))
6460 (exps (plus (mult (logs @0) @1) @2)))))
6465 exps (EXP EXP2 EXP10 POW10)
6466 /* sqrt(expN(x)) -> expN(x*0.5). */
6469 (exps (mult @0 { build_real (type, dconsthalf); })))
6470 /* cbrt(expN(x)) -> expN(x/3). */
6473 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6474 /* pow(expN(x), y) -> expN(x*y). */
6477 (exps (mult @0 @1))))
6479 /* tan(atan(x)) -> x. */
6486 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6490 copysigns (COPYSIGN)
6495 REAL_VALUE_TYPE r_cst;
6496 build_sinatan_real (&r_cst, type);
6497 tree t_cst = build_real (type, r_cst);
6498 tree t_one = build_one_cst (type);
6500 (if (SCALAR_FLOAT_TYPE_P (type))
6501 (cond (lt (abs @0) { t_cst; })
6502 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6503 (copysigns { t_one; } @0))))))
6505 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6509 copysigns (COPYSIGN)
6514 REAL_VALUE_TYPE r_cst;
6515 build_sinatan_real (&r_cst, type);
6516 tree t_cst = build_real (type, r_cst);
6517 tree t_one = build_one_cst (type);
6518 tree t_zero = build_zero_cst (type);
6520 (if (SCALAR_FLOAT_TYPE_P (type))
6521 (cond (lt (abs @0) { t_cst; })
6522 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6523 (copysigns { t_zero; } @0))))))
6525 (if (!flag_errno_math)
6526 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6531 (sinhs (atanhs:s @0))
6532 (with { tree t_one = build_one_cst (type); }
6533 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6535 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6540 (coshs (atanhs:s @0))
6541 (with { tree t_one = build_one_cst (type); }
6542 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6544 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
6546 (CABS (complex:C @0 real_zerop@1))
6549 /* trunc(trunc(x)) -> trunc(x), etc. */
6550 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6554 /* f(x) -> x if x is integer valued and f does nothing for such values. */
6555 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6557 (fns integer_valued_real_p@0)
6560 /* hypot(x,0) and hypot(0,x) -> abs(x). */
6562 (HYPOT:c @0 real_zerop@1)
6565 /* pow(1,x) -> 1. */
6567 (POW real_onep@0 @1)
6571 /* copysign(x,x) -> x. */
6572 (COPYSIGN_ALL @0 @0)
6576 /* copysign(x,-x) -> -x. */
6577 (COPYSIGN_ALL @0 (negate@1 @0))
6581 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
6582 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6585 (for scale (LDEXP SCALBN SCALBLN)
6586 /* ldexp(0, x) -> 0. */
6588 (scale real_zerop@0 @1)
6590 /* ldexp(x, 0) -> x. */
6592 (scale @0 integer_zerop@1)
6594 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
6596 (scale REAL_CST@0 @1)
6597 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6600 /* Canonicalization of sequences of math builtins. These rules represent
6601 IL simplifications but are not necessarily optimizations.
6603 The sincos pass is responsible for picking "optimal" implementations
6604 of math builtins, which may be more complicated and can sometimes go
6605 the other way, e.g. converting pow into a sequence of sqrts.
6606 We only want to do these canonicalizations before the pass has run. */
6608 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6609 /* Simplify tan(x) * cos(x) -> sin(x). */
6611 (mult:c (TAN:s @0) (COS:s @0))
6614 /* Simplify x * pow(x,c) -> pow(x,c+1). */
6616 (mult:c @0 (POW:s @0 REAL_CST@1))
6617 (if (!TREE_OVERFLOW (@1))
6618 (POW @0 (plus @1 { build_one_cst (type); }))))
6620 /* Simplify sin(x) / cos(x) -> tan(x). */
6622 (rdiv (SIN:s @0) (COS:s @0))
6625 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6627 (rdiv (SINH:s @0) (COSH:s @0))
6630 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6632 (rdiv (TANH:s @0) (SINH:s @0))
6633 (rdiv {build_one_cst (type);} (COSH @0)))
6635 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6637 (rdiv (COS:s @0) (SIN:s @0))
6638 (rdiv { build_one_cst (type); } (TAN @0)))
6640 /* Simplify sin(x) / tan(x) -> cos(x). */
6642 (rdiv (SIN:s @0) (TAN:s @0))
6643 (if (! HONOR_NANS (@0)
6644 && ! HONOR_INFINITIES (@0))
6647 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6649 (rdiv (TAN:s @0) (SIN:s @0))
6650 (if (! HONOR_NANS (@0)
6651 && ! HONOR_INFINITIES (@0))
6652 (rdiv { build_one_cst (type); } (COS @0))))
6654 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6656 (mult (POW:s @0 @1) (POW:s @0 @2))
6657 (POW @0 (plus @1 @2)))
6659 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6661 (mult (POW:s @0 @1) (POW:s @2 @1))
6662 (POW (mult @0 @2) @1))
6664 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6666 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6667 (POWI (mult @0 @2) @1))
6669 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6671 (rdiv (POW:s @0 REAL_CST@1) @0)
6672 (if (!TREE_OVERFLOW (@1))
6673 (POW @0 (minus @1 { build_one_cst (type); }))))
6675 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6677 (rdiv @0 (POW:s @1 @2))
6678 (mult @0 (POW @1 (negate @2))))
6683 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6686 (pows @0 { build_real (type, dconst_quarter ()); }))
6687 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6690 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6691 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6694 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6695 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6697 (cbrts (cbrts tree_expr_nonnegative_p@0))
6698 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6699 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6701 (sqrts (pows @0 @1))
6702 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6703 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6705 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6706 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6707 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6709 (pows (sqrts @0) @1)
6710 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6711 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6713 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6714 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6715 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6717 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6718 (pows @0 (mult @1 @2))))
6720 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6722 (CABS (complex @0 @0))
6723 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6725 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6728 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6730 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6735 (cexps compositional_complex@0)
6736 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6738 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6739 (mult @1 (imagpart @2)))))))
6741 (if (canonicalize_math_p ())
6742 /* floor(x) -> trunc(x) if x is nonnegative. */
6743 (for floors (FLOOR_ALL)
6746 (floors tree_expr_nonnegative_p@0)
6749 (match double_value_p
6751 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6752 (for froms (BUILT_IN_TRUNCL
6764 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6765 (if (optimize && canonicalize_math_p ())
6767 (froms (convert double_value_p@0))
6768 (convert (tos @0)))))
6770 (match float_value_p
6772 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6773 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6774 BUILT_IN_FLOORL BUILT_IN_FLOOR
6775 BUILT_IN_CEILL BUILT_IN_CEIL
6776 BUILT_IN_ROUNDL BUILT_IN_ROUND
6777 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6778 BUILT_IN_RINTL BUILT_IN_RINT)
6779 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6780 BUILT_IN_FLOORF BUILT_IN_FLOORF
6781 BUILT_IN_CEILF BUILT_IN_CEILF
6782 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6783 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6784 BUILT_IN_RINTF BUILT_IN_RINTF)
6785 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6787 (if (optimize && canonicalize_math_p ()
6788 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6790 (froms (convert float_value_p@0))
6791 (convert (tos @0)))))
6794 (match float16_value_p
6796 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
6797 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
6798 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
6799 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
6800 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
6801 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
6802 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
6803 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
6804 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
6805 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
6806 IFN_FLOOR IFN_FLOOR IFN_FLOOR
6807 IFN_CEIL IFN_CEIL IFN_CEIL
6808 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
6809 IFN_ROUND IFN_ROUND IFN_ROUND
6810 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
6811 IFN_RINT IFN_RINT IFN_RINT
6812 IFN_SQRT IFN_SQRT IFN_SQRT)
6813 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
6814 if x is a _Float16. */
6816 (convert (froms (convert float16_value_p@0)))
6818 && types_match (type, TREE_TYPE (@0))
6819 && direct_internal_fn_supported_p (as_internal_fn (tos),
6820 type, OPTIMIZE_FOR_BOTH))
6823 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
6824 x,y is float value, similar for _Float16/double. */
6825 (for copysigns (COPYSIGN_ALL)
6827 (convert (copysigns (convert@2 @0) (convert @1)))
6829 && !HONOR_SNANS (@2)
6830 && types_match (type, TREE_TYPE (@0))
6831 && types_match (type, TREE_TYPE (@1))
6832 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
6833 && direct_internal_fn_supported_p (IFN_COPYSIGN,
6834 type, OPTIMIZE_FOR_BOTH))
6835 (IFN_COPYSIGN @0 @1))))
6837 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
6838 tos (IFN_FMA IFN_FMA IFN_FMA)
6840 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
6841 (if (flag_unsafe_math_optimizations
6843 && FLOAT_TYPE_P (type)
6844 && FLOAT_TYPE_P (TREE_TYPE (@3))
6845 && types_match (type, TREE_TYPE (@0))
6846 && types_match (type, TREE_TYPE (@1))
6847 && types_match (type, TREE_TYPE (@2))
6848 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
6849 && direct_internal_fn_supported_p (as_internal_fn (tos),
6850 type, OPTIMIZE_FOR_BOTH))
6853 (for maxmin (max min)
6855 (convert (maxmin (convert@2 @0) (convert @1)))
6857 && FLOAT_TYPE_P (type)
6858 && FLOAT_TYPE_P (TREE_TYPE (@2))
6859 && types_match (type, TREE_TYPE (@0))
6860 && types_match (type, TREE_TYPE (@1))
6861 && element_precision (type) < element_precision (TREE_TYPE (@2)))
6865 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6866 tos (XFLOOR XCEIL XROUND XRINT)
6867 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6868 (if (optimize && canonicalize_math_p ())
6870 (froms (convert double_value_p@0))
6873 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6874 XFLOOR XCEIL XROUND XRINT)
6875 tos (XFLOORF XCEILF XROUNDF XRINTF)
6876 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6878 (if (optimize && canonicalize_math_p ())
6880 (froms (convert float_value_p@0))
6883 (if (canonicalize_math_p ())
6884 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6885 (for floors (IFLOOR LFLOOR LLFLOOR)
6887 (floors tree_expr_nonnegative_p@0)
6890 (if (canonicalize_math_p ())
6891 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6892 (for fns (IFLOOR LFLOOR LLFLOOR
6894 IROUND LROUND LLROUND)
6896 (fns integer_valued_real_p@0)
6898 (if (!flag_errno_math)
6899 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6900 (for rints (IRINT LRINT LLRINT)
6902 (rints integer_valued_real_p@0)
6905 (if (canonicalize_math_p ())
6906 (for ifn (IFLOOR ICEIL IROUND IRINT)
6907 lfn (LFLOOR LCEIL LROUND LRINT)
6908 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6909 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6910 sizeof (int) == sizeof (long). */
6911 (if (TYPE_PRECISION (integer_type_node)
6912 == TYPE_PRECISION (long_integer_type_node))
6915 (lfn:long_integer_type_node @0)))
6916 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6917 sizeof (long long) == sizeof (long). */
6918 (if (TYPE_PRECISION (long_long_integer_type_node)
6919 == TYPE_PRECISION (long_integer_type_node))
6922 (lfn:long_integer_type_node @0)))))
6924 /* cproj(x) -> x if we're ignoring infinities. */
6927 (if (!HONOR_INFINITIES (type))
6930 /* If the real part is inf and the imag part is known to be
6931 nonnegative, return (inf + 0i). */
6933 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6934 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6935 { build_complex_inf (type, false); }))
6937 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6939 (CPROJ (complex @0 REAL_CST@1))
6940 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6941 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6947 (pows @0 REAL_CST@1)
6949 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6950 REAL_VALUE_TYPE tmp;
6953 /* pow(x,0) -> 1. */
6954 (if (real_equal (value, &dconst0))
6955 { build_real (type, dconst1); })
6956 /* pow(x,1) -> x. */
6957 (if (real_equal (value, &dconst1))
6959 /* pow(x,-1) -> 1/x. */
6960 (if (real_equal (value, &dconstm1))
6961 (rdiv { build_real (type, dconst1); } @0))
6962 /* pow(x,0.5) -> sqrt(x). */
6963 (if (flag_unsafe_math_optimizations
6964 && canonicalize_math_p ()
6965 && real_equal (value, &dconsthalf))
6967 /* pow(x,1/3) -> cbrt(x). */
6968 (if (flag_unsafe_math_optimizations
6969 && canonicalize_math_p ()
6970 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6971 real_equal (value, &tmp)))
6974 /* powi(1,x) -> 1. */
6976 (POWI real_onep@0 @1)
6980 (POWI @0 INTEGER_CST@1)
6982 /* powi(x,0) -> 1. */
6983 (if (wi::to_wide (@1) == 0)
6984 { build_real (type, dconst1); })
6985 /* powi(x,1) -> x. */
6986 (if (wi::to_wide (@1) == 1)
6988 /* powi(x,-1) -> 1/x. */
6989 (if (wi::to_wide (@1) == -1)
6990 (rdiv { build_real (type, dconst1); } @0))))
6992 /* Narrowing of arithmetic and logical operations.
6994 These are conceptually similar to the transformations performed for
6995 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6996 term we want to move all that code out of the front-ends into here. */
6998 /* Convert (outertype)((innertype0)a+(innertype1)b)
6999 into ((newtype)a+(newtype)b) where newtype
7000 is the widest mode from all of these. */
7001 (for op (plus minus mult rdiv)
7003 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7004 /* If we have a narrowing conversion of an arithmetic operation where
7005 both operands are widening conversions from the same type as the outer
7006 narrowing conversion. Then convert the innermost operands to a
7007 suitable unsigned type (to avoid introducing undefined behavior),
7008 perform the operation and convert the result to the desired type. */
7009 (if (INTEGRAL_TYPE_P (type)
7012 /* We check for type compatibility between @0 and @1 below,
7013 so there's no need to check that @2/@4 are integral types. */
7014 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7015 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7016 /* The precision of the type of each operand must match the
7017 precision of the mode of each operand, similarly for the
7019 && type_has_mode_precision_p (TREE_TYPE (@1))
7020 && type_has_mode_precision_p (TREE_TYPE (@2))
7021 && type_has_mode_precision_p (type)
7022 /* The inner conversion must be a widening conversion. */
7023 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7024 && types_match (@1, type)
7025 && (types_match (@1, @2)
7026 /* Or the second operand is const integer or converted const
7027 integer from valueize. */
7028 || poly_int_tree_p (@4)))
7029 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7030 (op @1 (convert @2))
7031 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7032 (convert (op (convert:utype @1)
7033 (convert:utype @2)))))
7034 (if (FLOAT_TYPE_P (type)
7035 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7036 == DECIMAL_FLOAT_TYPE_P (type))
7037 (with { tree arg0 = strip_float_extensions (@1);
7038 tree arg1 = strip_float_extensions (@2);
7039 tree itype = TREE_TYPE (@0);
7040 tree ty1 = TREE_TYPE (arg0);
7041 tree ty2 = TREE_TYPE (arg1);
7042 enum tree_code code = TREE_CODE (itype); }
7043 (if (FLOAT_TYPE_P (ty1)
7044 && FLOAT_TYPE_P (ty2))
7045 (with { tree newtype = type;
7046 if (TYPE_MODE (ty1) == SDmode
7047 || TYPE_MODE (ty2) == SDmode
7048 || TYPE_MODE (type) == SDmode)
7049 newtype = dfloat32_type_node;
7050 if (TYPE_MODE (ty1) == DDmode
7051 || TYPE_MODE (ty2) == DDmode
7052 || TYPE_MODE (type) == DDmode)
7053 newtype = dfloat64_type_node;
7054 if (TYPE_MODE (ty1) == TDmode
7055 || TYPE_MODE (ty2) == TDmode
7056 || TYPE_MODE (type) == TDmode)
7057 newtype = dfloat128_type_node; }
7058 (if ((newtype == dfloat32_type_node
7059 || newtype == dfloat64_type_node
7060 || newtype == dfloat128_type_node)
7062 && types_match (newtype, type))
7063 (op (convert:newtype @1) (convert:newtype @2))
7064 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
7066 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
7068 /* Sometimes this transformation is safe (cannot
7069 change results through affecting double rounding
7070 cases) and sometimes it is not. If NEWTYPE is
7071 wider than TYPE, e.g. (float)((long double)double
7072 + (long double)double) converted to
7073 (float)(double + double), the transformation is
7074 unsafe regardless of the details of the types
7075 involved; double rounding can arise if the result
7076 of NEWTYPE arithmetic is a NEWTYPE value half way
7077 between two representable TYPE values but the
7078 exact value is sufficiently different (in the
7079 right direction) for this difference to be
7080 visible in ITYPE arithmetic. If NEWTYPE is the
7081 same as TYPE, however, the transformation may be
7082 safe depending on the types involved: it is safe
7083 if the ITYPE has strictly more than twice as many
7084 mantissa bits as TYPE, can represent infinities
7085 and NaNs if the TYPE can, and has sufficient
7086 exponent range for the product or ratio of two
7087 values representable in the TYPE to be within the
7088 range of normal values of ITYPE. */
7089 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
7090 && (flag_unsafe_math_optimizations
7091 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
7092 && real_can_shorten_arithmetic (TYPE_MODE (itype),
7094 && !excess_precision_type (newtype)))
7095 && !types_match (itype, newtype))
7096 (convert:type (op (convert:newtype @1)
7097 (convert:newtype @2)))
7102 /* This is another case of narrowing, specifically when there's an outer
7103 BIT_AND_EXPR which masks off bits outside the type of the innermost
7104 operands. Like the previous case we have to convert the operands
7105 to unsigned types to avoid introducing undefined behavior for the
7106 arithmetic operation. */
7107 (for op (minus plus)
7109 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7110 (if (INTEGRAL_TYPE_P (type)
7111 /* We check for type compatibility between @0 and @1 below,
7112 so there's no need to check that @1/@3 are integral types. */
7113 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7114 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7115 /* The precision of the type of each operand must match the
7116 precision of the mode of each operand, similarly for the
7118 && type_has_mode_precision_p (TREE_TYPE (@0))
7119 && type_has_mode_precision_p (TREE_TYPE (@1))
7120 && type_has_mode_precision_p (type)
7121 /* The inner conversion must be a widening conversion. */
7122 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7123 && types_match (@0, @1)
7124 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7125 <= TYPE_PRECISION (TREE_TYPE (@0)))
7126 && (wi::to_wide (@4)
7127 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7128 true, TYPE_PRECISION (type))) == 0)
7129 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7130 (with { tree ntype = TREE_TYPE (@0); }
7131 (convert (bit_and (op @0 @1) (convert:ntype @4))))
7132 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7133 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7134 (convert:utype @4))))))))
7136 /* Transform (@0 < @1 and @0 < @2) to use min,
7137 (@0 > @1 and @0 > @2) to use max */
7138 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7139 op (lt le gt ge lt le gt ge )
7140 ext (min min max max max max min min )
7142 (logic (op:cs @0 @1) (op:cs @0 @2))
7143 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7144 && TREE_CODE (@0) != INTEGER_CST)
7145 (op @0 (ext @1 @2)))))
7148 /* signbit(x) -> 0 if x is nonnegative. */
7149 (SIGNBIT tree_expr_nonnegative_p@0)
7150 { integer_zero_node; })
7153 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
7155 (if (!HONOR_SIGNED_ZEROS (@0))
7156 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7158 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
7160 (for op (plus minus)
7163 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7164 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7165 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7166 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7167 && !TYPE_SATURATING (TREE_TYPE (@0)))
7168 (with { tree res = int_const_binop (rop, @2, @1); }
7169 (if (TREE_OVERFLOW (res)
7170 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7171 { constant_boolean_node (cmp == NE_EXPR, type); }
7172 (if (single_use (@3))
7173 (cmp @0 { TREE_OVERFLOW (res)
7174 ? drop_tree_overflow (res) : res; }))))))))
7175 (for cmp (lt le gt ge)
7176 (for op (plus minus)
7179 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7180 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7181 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7182 (with { tree res = int_const_binop (rop, @2, @1); }
7183 (if (TREE_OVERFLOW (res))
7185 fold_overflow_warning (("assuming signed overflow does not occur "
7186 "when simplifying conditional to constant"),
7187 WARN_STRICT_OVERFLOW_CONDITIONAL);
7188 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7189 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
7190 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7191 TYPE_SIGN (TREE_TYPE (@1)))
7192 != (op == MINUS_EXPR);
7193 constant_boolean_node (less == ovf_high, type);
7195 (if (single_use (@3))
7198 fold_overflow_warning (("assuming signed overflow does not occur "
7199 "when changing X +- C1 cmp C2 to "
7201 WARN_STRICT_OVERFLOW_COMPARISON);
7203 (cmp @0 { res; })))))))))
7205 /* Canonicalizations of BIT_FIELD_REFs. */
7208 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7209 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7212 (BIT_FIELD_REF (view_convert @0) @1 @2)
7213 (BIT_FIELD_REF @0 @1 @2))
7216 (BIT_FIELD_REF @0 @1 integer_zerop)
7217 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7221 (BIT_FIELD_REF @0 @1 @2)
7223 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7224 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7226 (if (integer_zerop (@2))
7227 (view_convert (realpart @0)))
7228 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7229 (view_convert (imagpart @0)))))
7230 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7231 && INTEGRAL_TYPE_P (type)
7232 /* On GIMPLE this should only apply to register arguments. */
7233 && (! GIMPLE || is_gimple_reg (@0))
7234 /* A bit-field-ref that referenced the full argument can be stripped. */
7235 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
7236 && integer_zerop (@2))
7237 /* Low-parts can be reduced to integral conversions.
7238 ??? The following doesn't work for PDP endian. */
7239 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7240 /* But only do this after vectorization. */
7241 && canonicalize_math_after_vectorization_p ()
7242 /* Don't even think about BITS_BIG_ENDIAN. */
7243 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
7244 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
7245 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
7246 ? (TYPE_PRECISION (TREE_TYPE (@0))
7247 - TYPE_PRECISION (type))
7251 /* Simplify vector extracts. */
7254 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
7255 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
7256 && tree_fits_uhwi_p (TYPE_SIZE (type))
7257 && ((tree_to_uhwi (TYPE_SIZE (type))
7258 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7259 || (VECTOR_TYPE_P (type)
7260 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
7261 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
7264 tree ctor = (TREE_CODE (@0) == SSA_NAME
7265 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7266 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
7267 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
7268 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
7269 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
7272 && (idx % width) == 0
7274 && known_le ((idx + n) / width,
7275 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
7280 /* Constructor elements can be subvectors. */
7282 if (CONSTRUCTOR_NELTS (ctor) != 0)
7284 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7285 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7286 k = TYPE_VECTOR_SUBPARTS (cons_elem);
7288 unsigned HOST_WIDE_INT elt, count, const_k;
7291 /* We keep an exact subset of the constructor elements. */
7292 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7293 (if (CONSTRUCTOR_NELTS (ctor) == 0)
7294 { build_zero_cst (type); }
7296 (if (elt < CONSTRUCTOR_NELTS (ctor))
7297 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7298 { build_zero_cst (type); })
7299 /* We don't want to emit new CTORs unless the old one goes away.
7300 ??? Eventually allow this if the CTOR ends up constant or
7302 (if (single_use (@0))
7305 vec<constructor_elt, va_gc> *vals;
7306 vec_alloc (vals, count);
7307 bool constant_p = true;
7309 for (unsigned i = 0;
7310 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7312 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7313 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7314 if (!CONSTANT_CLASS_P (e))
7317 tree evtype = (types_match (TREE_TYPE (type),
7318 TREE_TYPE (TREE_TYPE (ctor)))
7320 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7322 res = (constant_p ? build_vector_from_ctor (evtype, vals)
7323 : build_constructor (evtype, vals));
7325 (view_convert { res; }))))))
7326 /* The bitfield references a single constructor element. */
7327 (if (k.is_constant (&const_k)
7328 && idx + n <= (idx / const_k + 1) * const_k)
7330 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7331 { build_zero_cst (type); })
7333 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7334 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7335 @1 { bitsize_int ((idx % const_k) * width); })))))))))
7337 /* Simplify a bit extraction from a bit insertion for the cases with
7338 the inserted element fully covering the extraction or the insertion
7339 not touching the extraction. */
7341 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7344 unsigned HOST_WIDE_INT isize;
7345 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7346 isize = TYPE_PRECISION (TREE_TYPE (@1));
7348 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7351 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7352 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
7353 wi::to_wide (@ipos) + isize))
7354 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
7356 - wi::to_wide (@ipos)); }))
7357 (if (wi::geu_p (wi::to_wide (@ipos),
7358 wi::to_wide (@rpos) + wi::to_wide (@rsize))
7359 || wi::geu_p (wi::to_wide (@rpos),
7360 wi::to_wide (@ipos) + isize))
7361 (BIT_FIELD_REF @0 @rsize @rpos)))))
7363 (if (canonicalize_math_after_vectorization_p ())
7366 (fmas:c (negate @0) @1 @2)
7367 (IFN_FNMA @0 @1 @2))
7369 (fmas @0 @1 (negate @2))
7372 (fmas:c (negate @0) @1 (negate @2))
7373 (IFN_FNMS @0 @1 @2))
7375 (negate (fmas@3 @0 @1 @2))
7376 (if (single_use (@3))
7377 (IFN_FNMS @0 @1 @2))))
7380 (IFN_FMS:c (negate @0) @1 @2)
7381 (IFN_FNMS @0 @1 @2))
7383 (IFN_FMS @0 @1 (negate @2))
7386 (IFN_FMS:c (negate @0) @1 (negate @2))
7387 (IFN_FNMA @0 @1 @2))
7389 (negate (IFN_FMS@3 @0 @1 @2))
7390 (if (single_use (@3))
7391 (IFN_FNMA @0 @1 @2)))
7394 (IFN_FNMA:c (negate @0) @1 @2)
7397 (IFN_FNMA @0 @1 (negate @2))
7398 (IFN_FNMS @0 @1 @2))
7400 (IFN_FNMA:c (negate @0) @1 (negate @2))
7403 (negate (IFN_FNMA@3 @0 @1 @2))
7404 (if (single_use (@3))
7405 (IFN_FMS @0 @1 @2)))
7408 (IFN_FNMS:c (negate @0) @1 @2)
7411 (IFN_FNMS @0 @1 (negate @2))
7412 (IFN_FNMA @0 @1 @2))
7414 (IFN_FNMS:c (negate @0) @1 (negate @2))
7417 (negate (IFN_FNMS@3 @0 @1 @2))
7418 (if (single_use (@3))
7419 (IFN_FMA @0 @1 @2))))
7421 /* CLZ simplifications. */
7426 (op (clz:s@2 @0) INTEGER_CST@1)
7427 (if (integer_zerop (@1) && single_use (@2))
7428 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
7429 (with { tree type0 = TREE_TYPE (@0);
7430 tree stype = signed_type_for (type0);
7431 HOST_WIDE_INT val = 0;
7432 /* Punt on hypothetical weird targets. */
7434 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7440 (cmp (convert:stype @0) { build_zero_cst (stype); })))
7441 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
7442 (with { bool ok = true;
7443 HOST_WIDE_INT val = 0;
7444 tree type0 = TREE_TYPE (@0);
7445 /* Punt on hypothetical weird targets. */
7447 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7449 && val == TYPE_PRECISION (type0) - 1)
7452 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7453 (op @0 { build_one_cst (type0); })))))))
7455 /* CTZ simplifications. */
7457 (for op (ge gt le lt)
7460 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
7461 (op (ctz:s @0) INTEGER_CST@1)
7462 (with { bool ok = true;
7463 HOST_WIDE_INT val = 0;
7464 if (!tree_fits_shwi_p (@1))
7468 val = tree_to_shwi (@1);
7469 /* Canonicalize to >= or <. */
7470 if (op == GT_EXPR || op == LE_EXPR)
7472 if (val == HOST_WIDE_INT_MAX)
7478 bool zero_res = false;
7479 HOST_WIDE_INT zero_val = 0;
7480 tree type0 = TREE_TYPE (@0);
7481 int prec = TYPE_PRECISION (type0);
7483 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7488 (if (ok && (!zero_res || zero_val >= val))
7489 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7491 (if (ok && (!zero_res || zero_val < val))
7492 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7493 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7494 (cmp (bit_and @0 { wide_int_to_tree (type0,
7495 wi::mask (val, false, prec)); })
7496 { build_zero_cst (type0); })))))))
7499 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
7500 (op (ctz:s @0) INTEGER_CST@1)
7501 (with { bool zero_res = false;
7502 HOST_WIDE_INT zero_val = 0;
7503 tree type0 = TREE_TYPE (@0);
7504 int prec = TYPE_PRECISION (type0);
7506 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7510 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7511 (if (!zero_res || zero_val != wi::to_widest (@1))
7512 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7513 (if (!zero_res || zero_val < 0 || zero_val >= prec)
7514 (op (bit_and @0 { wide_int_to_tree (type0,
7515 wi::mask (tree_to_uhwi (@1) + 1,
7517 { wide_int_to_tree (type0,
7518 wi::shifted_mask (tree_to_uhwi (@1), 1,
7519 false, prec)); })))))))
7521 /* POPCOUNT simplifications. */
7522 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
7524 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7525 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7526 (POPCOUNT (bit_ior @0 @1))))
7528 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
7529 (for popcount (POPCOUNT)
7530 (for cmp (le eq ne gt)
7533 (cmp (popcount @0) integer_zerop)
7534 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7536 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
7538 (bit_and (POPCOUNT @0) integer_onep)
7541 /* PARITY simplifications. */
7542 /* parity(~X) is parity(X). */
7544 (PARITY (bit_not @0))
7547 /* parity(X)^parity(Y) is parity(X^Y). */
7549 (bit_xor (PARITY:s @0) (PARITY:s @1))
7550 (PARITY (bit_xor @0 @1)))
7552 /* Common POPCOUNT/PARITY simplifications. */
7553 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
7554 (for pfun (POPCOUNT PARITY)
7557 (with { wide_int nz = tree_nonzero_bits (@0); }
7561 (if (wi::popcount (nz) == 1)
7562 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7563 (convert (rshift:utype (convert:utype @0)
7564 { build_int_cst (integer_type_node,
7565 wi::ctz (nz)); }))))))))
7568 /* 64- and 32-bits branchless implementations of popcount are detected:
7570 int popcount64c (uint64_t x)
7572 x -= (x >> 1) & 0x5555555555555555ULL;
7573 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7574 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
7575 return (x * 0x0101010101010101ULL) >> 56;
7578 int popcount32c (uint32_t x)
7580 x -= (x >> 1) & 0x55555555;
7581 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
7582 x = (x + (x >> 4)) & 0x0f0f0f0f;
7583 return (x * 0x01010101) >> 24;
7590 (rshift @8 INTEGER_CST@5)
7592 (bit_and @6 INTEGER_CST@7)
7596 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
7602 /* Check constants and optab. */
7603 (with { unsigned prec = TYPE_PRECISION (type);
7604 int shift = (64 - prec) & 63;
7605 unsigned HOST_WIDE_INT c1
7606 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
7607 unsigned HOST_WIDE_INT c2
7608 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
7609 unsigned HOST_WIDE_INT c3
7610 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
7611 unsigned HOST_WIDE_INT c4
7612 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
7617 && TYPE_UNSIGNED (type)
7618 && integer_onep (@4)
7619 && wi::to_widest (@10) == 2
7620 && wi::to_widest (@5) == 4
7621 && wi::to_widest (@1) == prec - 8
7622 && tree_to_uhwi (@2) == c1
7623 && tree_to_uhwi (@3) == c2
7624 && tree_to_uhwi (@9) == c3
7625 && tree_to_uhwi (@7) == c3
7626 && tree_to_uhwi (@11) == c4)
7627 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
7629 (convert (IFN_POPCOUNT:type @0))
7630 /* Try to do popcount in two halves. PREC must be at least
7631 five bits for this to work without extension before adding. */
7633 tree half_type = NULL_TREE;
7634 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
7637 && m.require () != TYPE_MODE (type))
7639 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
7640 half_type = build_nonstandard_integer_type (half_prec, 1);
7642 gcc_assert (half_prec > 2);
7644 (if (half_type != NULL_TREE
7645 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
7648 (IFN_POPCOUNT:half_type (convert @0))
7649 (IFN_POPCOUNT:half_type (convert (rshift @0
7650 { build_int_cst (integer_type_node, half_prec); } )))))))))))
7652 /* __builtin_ffs needs to deal on many targets with the possible zero
7653 argument. If we know the argument is always non-zero, __builtin_ctz + 1
7654 should lead to better code. */
7656 (FFS tree_expr_nonzero_p@0)
7657 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7658 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
7659 OPTIMIZE_FOR_SPEED))
7660 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7661 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
7664 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
7666 /* __builtin_ffs (X) == 0 -> X == 0.
7667 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
7670 (cmp (ffs@2 @0) INTEGER_CST@1)
7671 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7673 (if (integer_zerop (@1))
7674 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7675 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7676 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7677 (if (single_use (@2))
7678 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7679 wi::mask (tree_to_uhwi (@1),
7681 { wide_int_to_tree (TREE_TYPE (@0),
7682 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7683 false, prec)); }))))))
7685 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
7689 bit_op (bit_and bit_ior)
7691 (cmp (ffs@2 @0) INTEGER_CST@1)
7692 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7694 (if (integer_zerop (@1))
7695 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7696 (if (tree_int_cst_sgn (@1) < 0)
7697 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7698 (if (wi::to_widest (@1) >= prec)
7699 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7700 (if (wi::to_widest (@1) == prec - 1)
7701 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7702 wi::shifted_mask (prec - 1, 1,
7704 (if (single_use (@2))
7705 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7707 { wide_int_to_tree (TREE_TYPE (@0),
7708 wi::mask (tree_to_uhwi (@1),
7710 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7717 --> r = .COND_FN (cond, a, b)
7721 --> r = .COND_FN (~cond, b, a). */
7723 (for uncond_op (UNCOND_UNARY)
7724 cond_op (COND_UNARY)
7726 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
7727 (with { tree op_type = TREE_TYPE (@3); }
7728 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7729 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7730 (cond_op @0 @1 @2))))
7732 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
7733 (with { tree op_type = TREE_TYPE (@3); }
7734 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7735 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7736 (cond_op (bit_not @0) @2 @1)))))
7745 r = c ? a1 op a2 : b;
7747 if the target can do it in one go. This makes the operation conditional
7748 on c, so could drop potentially-trapping arithmetic, but that's a valid
7749 simplification if the result of the operation isn't needed.
7751 Avoid speculatively generating a stand-alone vector comparison
7752 on targets that might not support them. Any target implementing
7753 conditional internal functions must support the same comparisons
7754 inside and outside a VEC_COND_EXPR. */
7756 (for uncond_op (UNCOND_BINARY)
7757 cond_op (COND_BINARY)
7759 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7760 (with { tree op_type = TREE_TYPE (@4); }
7761 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7762 && is_truth_type_for (op_type, TREE_TYPE (@0))
7764 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7766 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7767 (with { tree op_type = TREE_TYPE (@4); }
7768 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7769 && is_truth_type_for (op_type, TREE_TYPE (@0))
7771 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7773 /* Same for ternary operations. */
7774 (for uncond_op (UNCOND_TERNARY)
7775 cond_op (COND_TERNARY)
7777 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7778 (with { tree op_type = TREE_TYPE (@5); }
7779 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7780 && is_truth_type_for (op_type, TREE_TYPE (@0))
7782 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7784 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7785 (with { tree op_type = TREE_TYPE (@5); }
7786 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7787 && is_truth_type_for (op_type, TREE_TYPE (@0))
7789 (view_convert (cond_op (bit_not @0) @2 @3 @4
7790 (view_convert:op_type @1)))))))
7793 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7794 "else" value of an IFN_COND_*. */
7795 (for cond_op (COND_BINARY)
7797 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7798 (with { tree op_type = TREE_TYPE (@3); }
7799 (if (element_precision (type) == element_precision (op_type))
7800 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7802 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7803 (with { tree op_type = TREE_TYPE (@5); }
7804 (if (inverse_conditions_p (@0, @2)
7805 && element_precision (type) == element_precision (op_type))
7806 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7808 /* Same for ternary operations. */
7809 (for cond_op (COND_TERNARY)
7811 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7812 (with { tree op_type = TREE_TYPE (@4); }
7813 (if (element_precision (type) == element_precision (op_type))
7814 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7816 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7817 (with { tree op_type = TREE_TYPE (@6); }
7818 (if (inverse_conditions_p (@0, @2)
7819 && element_precision (type) == element_precision (op_type))
7820 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7822 /* Detect simplication for a conditional reduction where
7825 c = mask2 ? d + a : d
7829 c = mask1 && mask2 ? d + b : d. */
7831 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
7832 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
7834 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7837 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7838 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7840 If pointers are known not to wrap, B checks whether @1 bytes starting
7841 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7842 bytes. A is more efficiently tested as:
7844 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7846 The equivalent expression for B is given by replacing @1 with @1 - 1:
7848 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7850 @0 and @2 can be swapped in both expressions without changing the result.
7852 The folds rely on sizetype's being unsigned (which is always true)
7853 and on its being the same width as the pointer (which we have to check).
7855 The fold replaces two pointer_plus expressions, two comparisons and
7856 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7857 the best case it's a saving of two operations. The A fold retains one
7858 of the original pointer_pluses, so is a win even if both pointer_pluses
7859 are used elsewhere. The B fold is a wash if both pointer_pluses are
7860 used elsewhere, since all we end up doing is replacing a comparison with
7861 a pointer_plus. We do still apply the fold under those circumstances
7862 though, in case applying it to other conditions eventually makes one of the
7863 pointer_pluses dead. */
7864 (for ior (truth_orif truth_or bit_ior)
7867 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7868 (cmp:cs (pointer_plus@4 @2 @1) @0))
7869 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7870 && TYPE_OVERFLOW_WRAPS (sizetype)
7871 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7872 /* Calculate the rhs constant. */
7873 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7874 offset_int rhs = off * 2; }
7875 /* Always fails for negative values. */
7876 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7877 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7878 pick a canonical order. This increases the chances of using the
7879 same pointer_plus in multiple checks. */
7880 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7881 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7882 (if (cmp == LT_EXPR)
7883 (gt (convert:sizetype
7884 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7885 { swap_p ? @0 : @2; }))
7887 (gt (convert:sizetype
7888 (pointer_diff:ssizetype
7889 (pointer_plus { swap_p ? @2 : @0; }
7890 { wide_int_to_tree (sizetype, off); })
7891 { swap_p ? @0 : @2; }))
7892 { rhs_tree; })))))))))
7894 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7896 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7897 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7898 (with { int i = single_nonzero_element (@1); }
7900 (with { tree elt = vector_cst_elt (@1, i);
7901 tree elt_type = TREE_TYPE (elt);
7902 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7903 tree size = bitsize_int (elt_bits);
7904 tree pos = bitsize_int (elt_bits * i); }
7907 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7910 /* Fold reduction of a single nonzero element constructor. */
7911 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7912 (simplify (reduc (CONSTRUCTOR@0))
7913 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
7914 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7915 tree elt = ctor_single_nonzero_element (ctor); }
7917 && !HONOR_SNANS (type)
7918 && !HONOR_SIGNED_ZEROS (type))
7921 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
7922 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
7923 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
7924 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
7925 (simplify (reduc (op @0 VECTOR_CST@1))
7926 (op (reduc:type @0) (reduc:type @1))))
7928 /* Simplify vector floating point operations of alternating sub/add pairs
7929 into using an fneg of a wider element type followed by a normal add.
7930 under IEEE 754 the fneg of the wider type will negate every even entry
7931 and when doing an add we get a sub of the even and add of every odd
7934 (vec_perm (plus:c @0 @1) (minus @0 @1) VECTOR_CST@2)
7935 (if (!VECTOR_INTEGER_TYPE_P (type)
7936 && !FLOAT_WORDS_BIG_ENDIAN)
7939 /* Build a vector of integers from the tree mask. */
7940 vec_perm_builder builder;
7942 (if (tree_to_vec_perm_builder (&builder, @2))
7945 /* Create a vec_perm_indices for the integer vector. */
7946 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7947 vec_perm_indices sel (builder, 2, nelts);
7948 machine_mode vec_mode = TYPE_MODE (type);
7949 machine_mode wide_mode;
7950 scalar_mode wide_elt_mode;
7951 poly_uint64 wide_nunits;
7952 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
7954 (if (sel.series_p (0, 2, 0, 2)
7955 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
7956 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
7957 && related_vector_mode (vec_mode, wide_elt_mode,
7958 wide_nunits).exists (&wide_mode))
7962 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
7963 TYPE_UNSIGNED (type));
7964 tree ntype = build_vector_type_for_mode (stype, wide_mode);
7966 /* The format has to be a non-extended ieee format. */
7967 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
7968 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
7970 (if (TYPE_MODE (stype) != BLKmode
7971 && VECTOR_TYPE_P (ntype)
7976 /* If the target doesn't support v1xx vectors, try using
7977 scalar mode xx instead. */
7978 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
7979 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
7982 (if (fmt_new->signbit_rw
7983 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
7984 && fmt_new->signbit_rw == fmt_new->signbit_ro
7985 && targetm.can_change_mode_class (TYPE_MODE (ntype), TYPE_MODE (type), ALL_REGS)
7986 && ((optimize_vectors_before_lowering_p () && VECTOR_TYPE_P (ntype))
7987 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
7988 (plus (view_convert:type (negate (view_convert:ntype @1))) @0)))))))))))
7991 (vec_perm @0 @1 VECTOR_CST@2)
7994 tree op0 = @0, op1 = @1, op2 = @2;
7995 machine_mode result_mode = TYPE_MODE (type);
7996 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
7998 /* Build a vector of integers from the tree mask. */
7999 vec_perm_builder builder;
8001 (if (tree_to_vec_perm_builder (&builder, op2))
8004 /* Create a vec_perm_indices for the integer vector. */
8005 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8006 bool single_arg = (op0 == op1);
8007 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
8009 (if (sel.series_p (0, 1, 0, 1))
8011 (if (sel.series_p (0, 1, nelts, 1))
8017 if (sel.all_from_input_p (0))
8019 else if (sel.all_from_input_p (1))
8022 sel.rotate_inputs (1);
8024 else if (known_ge (poly_uint64 (sel[0]), nelts))
8026 std::swap (op0, op1);
8027 sel.rotate_inputs (1);
8031 tree cop0 = op0, cop1 = op1;
8032 if (TREE_CODE (op0) == SSA_NAME
8033 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
8034 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8035 cop0 = gimple_assign_rhs1 (def);
8036 if (TREE_CODE (op1) == SSA_NAME
8037 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
8038 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8039 cop1 = gimple_assign_rhs1 (def);
8042 (if ((TREE_CODE (cop0) == VECTOR_CST
8043 || TREE_CODE (cop0) == CONSTRUCTOR)
8044 && (TREE_CODE (cop1) == VECTOR_CST
8045 || TREE_CODE (cop1) == CONSTRUCTOR)
8046 && (t = fold_vec_perm (type, cop0, cop1, sel)))
8050 bool changed = (op0 == op1 && !single_arg);
8051 tree ins = NULL_TREE;
8054 /* See if the permutation is performing a single element
8055 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
8056 in that case. But only if the vector mode is supported,
8057 otherwise this is invalid GIMPLE. */
8058 if (op_mode != BLKmode
8059 && (TREE_CODE (cop0) == VECTOR_CST
8060 || TREE_CODE (cop0) == CONSTRUCTOR
8061 || TREE_CODE (cop1) == VECTOR_CST
8062 || TREE_CODE (cop1) == CONSTRUCTOR))
8064 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
8067 /* After canonicalizing the first elt to come from the
8068 first vector we only can insert the first elt from
8069 the first vector. */
8071 if ((ins = fold_read_from_vector (cop0, sel[0])))
8074 /* The above can fail for two-element vectors which always
8075 appear to insert the first element, so try inserting
8076 into the second lane as well. For more than two
8077 elements that's wasted time. */
8078 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
8080 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
8081 for (at = 0; at < encoded_nelts; ++at)
8082 if (maybe_ne (sel[at], at))
8084 if (at < encoded_nelts
8085 && (known_eq (at + 1, nelts)
8086 || sel.series_p (at + 1, 1, at + 1, 1)))
8088 if (known_lt (poly_uint64 (sel[at]), nelts))
8089 ins = fold_read_from_vector (cop0, sel[at]);
8091 ins = fold_read_from_vector (cop1, sel[at] - nelts);
8096 /* Generate a canonical form of the selector. */
8097 if (!ins && sel.encoding () != builder)
8099 /* Some targets are deficient and fail to expand a single
8100 argument permutation while still allowing an equivalent
8101 2-argument version. */
8103 if (sel.ninputs () == 2
8104 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
8105 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8108 vec_perm_indices sel2 (builder, 2, nelts);
8109 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
8110 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
8112 /* Not directly supported with either encoding,
8113 so use the preferred form. */
8114 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8116 if (!operand_equal_p (op2, oldop2, 0))
8121 (bit_insert { op0; } { ins; }
8122 { bitsize_int (at * vector_element_bits (type)); })
8124 (vec_perm { op0; } { op1; } { op2; }))))))))))))
8126 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
8128 (match vec_same_elem_p
8131 (match vec_same_elem_p
8133 (if (TREE_CODE (@0) == SSA_NAME
8134 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
8136 (match vec_same_elem_p
8138 (if (uniform_vector_p (@0))))
8142 (vec_perm vec_same_elem_p@0 @0 @1)
8145 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
8147 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8148 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8149 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
8151 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8152 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8153 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
8157 c = VEC_PERM_EXPR <a, b, VCST0>;
8158 d = VEC_PERM_EXPR <c, c, VCST1>;
8160 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
8163 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
8164 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
8167 machine_mode result_mode = TYPE_MODE (type);
8168 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
8169 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8170 vec_perm_builder builder0;
8171 vec_perm_builder builder1;
8172 vec_perm_builder builder2 (nelts, nelts, 1);
8174 (if (tree_to_vec_perm_builder (&builder0, @3)
8175 && tree_to_vec_perm_builder (&builder1, @4))
8178 vec_perm_indices sel0 (builder0, 2, nelts);
8179 vec_perm_indices sel1 (builder1, 1, nelts);
8181 for (int i = 0; i < nelts; i++)
8182 builder2.quick_push (sel0[sel1[i].to_constant ()]);
8184 vec_perm_indices sel2 (builder2, 2, nelts);
8186 tree op0 = NULL_TREE;
8187 /* If the new VEC_PERM_EXPR can't be handled but both
8188 original VEC_PERM_EXPRs can, punt.
8189 If one or both of the original VEC_PERM_EXPRs can't be
8190 handled and the new one can't be either, don't increase
8191 number of VEC_PERM_EXPRs that can't be handled. */
8192 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
8194 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
8195 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
8196 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
8197 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
8200 (vec_perm @1 @2 { op0; })))))))
8203 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
8204 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
8205 constant which when multiplied by a power of 2 contains a unique value
8206 in the top 5 or 6 bits. This is then indexed into a table which maps it
8207 to the number of trailing zeroes. */
8208 (match (ctz_table_index @1 @2 @3)
8209 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
8211 (match (cond_expr_convert_p @0 @2 @3 @6)
8212 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
8213 (if (INTEGRAL_TYPE_P (type)
8214 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8215 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8216 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8217 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
8218 && TYPE_PRECISION (TREE_TYPE (@0))
8219 == TYPE_PRECISION (TREE_TYPE (@2))
8220 && TYPE_PRECISION (TREE_TYPE (@0))
8221 == TYPE_PRECISION (TREE_TYPE (@3))
8222 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
8223 signess when convert is truncation, but not ok for extension since
8224 it's sign_extend vs zero_extend. */
8225 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
8226 || (TYPE_UNSIGNED (TREE_TYPE (@2))
8227 == TYPE_UNSIGNED (TREE_TYPE (@3))))
8229 && single_use (@5))))
8231 (for bit_op (bit_and bit_ior bit_xor)
8232 (match (bitwise_induction_p @0 @2 @3)
8234 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
8237 (match (bitwise_induction_p @0 @2 @3)
8239 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
8241 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
8242 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
8244 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
8245 (with { auto i = wi::neg (wi::to_wide (@2)); }
8246 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
8247 (if (wi::popcount (i) == 1
8248 && (wi::to_wide (@1)) == (i - 1))
8249 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
8251 (cond (le @0 @1) @0 (bit_and @0 @1))))))
8253 /* -x & 1 -> x & 1. */
8255 (bit_and (negate @0) integer_onep@1)
8256 (if (!TYPE_OVERFLOW_SANITIZED (type))
8260 c1 = VEC_PERM_EXPR (a, a, mask)
8261 c2 = VEC_PERM_EXPR (b, b, mask)
8265 c3 = VEC_PERM_EXPR (c, c, mask)
8266 For all integer non-div operations. */
8267 (for op (plus minus mult bit_and bit_ior bit_xor
8270 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
8271 (if (VECTOR_INTEGER_TYPE_P (type))
8272 (vec_perm (op@3 @0 @1) @3 @2))))
8274 /* Similar for float arithmetic when permutation constant covers
8275 all vector elements. */
8276 (for op (plus minus mult)
8278 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
8279 (if (VECTOR_FLOAT_TYPE_P (type)
8280 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8284 vec_perm_builder builder;
8285 bool full_perm_p = false;
8286 if (tree_to_vec_perm_builder (&builder, perm_cst))
8288 unsigned HOST_WIDE_INT nelts;
8290 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8291 /* Create a vec_perm_indices for the VECTOR_CST. */
8292 vec_perm_indices sel (builder, 1, nelts);
8294 /* Check if perm indices covers all vector elements. */
8295 if (sel.encoding ().encoded_full_vector_p ())
8297 auto_sbitmap seen (nelts);
8298 bitmap_clear (seen);
8300 unsigned HOST_WIDE_INT count = 0, i;
8302 for (i = 0; i < nelts; i++)
8304 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
8308 full_perm_p = count == nelts;
8313 (vec_perm (op@3 @0 @1) @3 @2))))))