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 also 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 /* Fold (1 << (C - x)) where C = precision(type) - 1
986 into ((1 << C) >> x). */
988 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
989 (if (INTEGRAL_TYPE_P (type)
990 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
992 (if (TYPE_UNSIGNED (type))
993 (rshift (lshift @0 @2) @3)
995 { tree utype = unsigned_type_for (type); }
996 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
998 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1000 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1001 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1002 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1003 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1004 (bit_and (convert @0)
1005 { wide_int_to_tree (type,
1006 wi::lshift (wone, wi::to_wide (@2))); }))))
1008 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1009 (for cst (INTEGER_CST VECTOR_CST)
1011 (rshift (negate:s @0) cst@1)
1012 (if (!TYPE_UNSIGNED (type)
1013 && TYPE_OVERFLOW_UNDEFINED (type))
1014 (with { tree stype = TREE_TYPE (@1);
1015 tree bt = truth_type_for (type);
1016 tree zeros = build_zero_cst (type);
1017 tree cst = NULL_TREE; }
1019 /* Handle scalar case. */
1020 (if (INTEGRAL_TYPE_P (type)
1021 /* If we apply the rule to the scalar type before vectorization
1022 we will enforce the result of the comparison being a bool
1023 which will require an extra AND on the result that will be
1024 indistinguishable from when the user did actually want 0
1025 or 1 as the result so it can't be removed. */
1026 && canonicalize_math_after_vectorization_p ()
1027 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1028 (negate (convert (gt @0 { zeros; }))))
1029 /* Handle vector case. */
1030 (if (VECTOR_INTEGER_TYPE_P (type)
1031 /* First check whether the target has the same mode for vector
1032 comparison results as it's operands do. */
1033 && TYPE_MODE (bt) == TYPE_MODE (type)
1034 /* Then check to see if the target is able to expand the comparison
1035 with the given type later on, otherwise we may ICE. */
1036 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1037 && (cst = uniform_integer_cst_p (@1)) != NULL
1038 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1039 (view_convert (gt:bt @0 { zeros; }))))))))
1041 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1043 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1044 (if (flag_associative_math
1047 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1049 (rdiv { tem; } @1)))))
1051 /* Simplify ~X & X as zero. */
1053 (bit_and:c (convert? @0) (convert? (bit_not @0)))
1054 { build_zero_cst (type); })
1056 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1058 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1059 (if (TYPE_UNSIGNED (type))
1060 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1062 (for bitop (bit_and bit_ior)
1064 /* PR35691: Transform
1065 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1066 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1068 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1069 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1070 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1071 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1072 (cmp (bit_ior @0 (convert @1)) @2)))
1074 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1075 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1077 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1078 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1079 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1080 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1081 (cmp (bit_and @0 (convert @1)) @2))))
1083 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1085 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1086 (minus (bit_xor @0 @1) @1))
1088 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1089 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1090 (minus (bit_xor @0 @1) @1)))
1092 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1094 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1095 (minus @1 (bit_xor @0 @1)))
1097 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1098 (for op (bit_ior bit_xor plus)
1100 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
1103 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
1104 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1107 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1109 (bit_ior:c (bit_xor:c @0 @1) @0)
1112 /* (a & ~b) | (a ^ b) --> a ^ b */
1114 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1117 /* (a & ~b) ^ ~a --> ~(a & b) */
1119 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1120 (bit_not (bit_and @0 @1)))
1122 /* (~a & b) ^ a --> (a | b) */
1124 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1127 /* (a | b) & ~(a ^ b) --> a & b */
1129 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1132 /* a | ~(a ^ b) --> a | ~b */
1134 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1135 (bit_ior @0 (bit_not @1)))
1137 /* (a | b) | (a &^ b) --> a | b */
1138 (for op (bit_and bit_xor)
1140 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1143 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1145 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1148 /* ~(~a & b) --> a | ~b */
1150 (bit_not (bit_and:cs (bit_not @0) @1))
1151 (bit_ior @0 (bit_not @1)))
1153 /* ~(~a | b) --> a & ~b */
1155 (bit_not (bit_ior:cs (bit_not @0) @1))
1156 (bit_and @0 (bit_not @1)))
1158 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1160 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1161 (bit_and @3 (bit_not @2)))
1163 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1165 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1168 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1170 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1171 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1173 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1175 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1176 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1178 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1180 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1181 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1182 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1185 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1186 ((A & N) + B) & M -> (A + B) & M
1187 Similarly if (N & M) == 0,
1188 ((A | N) + B) & M -> (A + B) & M
1189 and for - instead of + (or unary - instead of +)
1190 and/or ^ instead of |.
1191 If B is constant and (B & M) == 0, fold into A & M. */
1192 (for op (plus minus)
1193 (for bitop (bit_and bit_ior bit_xor)
1195 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1198 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1199 @3, @4, @1, ERROR_MARK, NULL_TREE,
1202 (convert (bit_and (op (convert:utype { pmop[0]; })
1203 (convert:utype { pmop[1]; }))
1204 (convert:utype @2))))))
1206 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1209 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1210 NULL_TREE, NULL_TREE, @1, bitop, @3,
1213 (convert (bit_and (op (convert:utype { pmop[0]; })
1214 (convert:utype { pmop[1]; }))
1215 (convert:utype @2)))))))
1217 (bit_and (op:s @0 @1) INTEGER_CST@2)
1220 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1221 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1222 NULL_TREE, NULL_TREE, pmop); }
1224 (convert (bit_and (op (convert:utype { pmop[0]; })
1225 (convert:utype { pmop[1]; }))
1226 (convert:utype @2)))))))
1227 (for bitop (bit_and bit_ior bit_xor)
1229 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1232 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1233 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1234 NULL_TREE, NULL_TREE, pmop); }
1236 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1237 (convert:utype @1)))))))
1239 /* X % Y is smaller than Y. */
1242 (cmp (trunc_mod @0 @1) @1)
1243 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1244 { constant_boolean_node (cmp == LT_EXPR, type); })))
1247 (cmp @1 (trunc_mod @0 @1))
1248 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1249 { constant_boolean_node (cmp == GT_EXPR, type); })))
1253 (bit_ior @0 integer_all_onesp@1)
1258 (bit_ior @0 integer_zerop)
1263 (bit_and @0 integer_zerop@1)
1269 (for op (bit_ior bit_xor plus)
1271 (op:c (convert? @0) (convert? (bit_not @0)))
1272 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1277 { build_zero_cst (type); })
1279 /* Canonicalize X ^ ~0 to ~X. */
1281 (bit_xor @0 integer_all_onesp@1)
1286 (bit_and @0 integer_all_onesp)
1289 /* x & x -> x, x | x -> x */
1290 (for bitop (bit_and bit_ior)
1295 /* x & C -> x if we know that x & ~C == 0. */
1298 (bit_and SSA_NAME@0 INTEGER_CST@1)
1299 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1300 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1304 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1306 (bit_not (minus (bit_not @0) @1))
1309 (bit_not (plus:c (bit_not @0) @1))
1312 /* ~(X - Y) -> ~X + Y. */
1314 (bit_not (minus:s @0 @1))
1315 (plus (bit_not @0) @1))
1317 (bit_not (plus:s @0 INTEGER_CST@1))
1318 (if ((INTEGRAL_TYPE_P (type)
1319 && TYPE_UNSIGNED (type))
1320 || (!TYPE_OVERFLOW_SANITIZED (type)
1321 && may_negate_without_overflow_p (@1)))
1322 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1325 /* ~X + Y -> (Y - X) - 1. */
1327 (plus:c (bit_not @0) @1)
1328 (if (ANY_INTEGRAL_TYPE_P (type)
1329 && TYPE_OVERFLOW_WRAPS (type)
1330 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1331 && !integer_all_onesp (@1))
1332 (plus (minus @1 @0) { build_minus_one_cst (type); })
1333 (if (INTEGRAL_TYPE_P (type)
1334 && TREE_CODE (@1) == INTEGER_CST
1335 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1337 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1340 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1342 (bit_not (rshift:s @0 @1))
1343 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1344 (rshift (bit_not! @0) @1)
1345 /* For logical right shifts, this is possible only if @0 doesn't
1346 have MSB set and the logical right shift is changed into
1347 arithmetic shift. */
1348 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1349 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1350 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1352 /* x + (x & 1) -> (x + 1) & ~1 */
1354 (plus:c @0 (bit_and:s @0 integer_onep@1))
1355 (bit_and (plus @0 @1) (bit_not @1)))
1357 /* x & ~(x & y) -> x & ~y */
1358 /* x | ~(x | y) -> x | ~y */
1359 (for bitop (bit_and bit_ior)
1361 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1362 (bitop @0 (bit_not @1))))
1364 /* (~x & y) | ~(x | y) -> ~x */
1366 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1369 /* (x | y) ^ (x | ~y) -> ~x */
1371 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1374 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1376 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1377 (bit_not (bit_xor @0 @1)))
1379 /* (~x | y) ^ (x ^ y) -> x | ~y */
1381 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1382 (bit_ior @0 (bit_not @1)))
1384 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1386 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1387 (bit_not (bit_and @0 @1)))
1389 /* (x | y) & ~x -> y & ~x */
1390 /* (x & y) | ~x -> y | ~x */
1391 (for bitop (bit_and bit_ior)
1392 rbitop (bit_ior bit_and)
1394 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1397 /* (x & y) ^ (x | y) -> x ^ y */
1399 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1402 /* (x ^ y) ^ (x | y) -> x & y */
1404 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1407 /* (x & y) + (x ^ y) -> x | y */
1408 /* (x & y) | (x ^ y) -> x | y */
1409 /* (x & y) ^ (x ^ y) -> x | y */
1410 (for op (plus bit_ior bit_xor)
1412 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1415 /* (x & y) + (x | y) -> x + y */
1417 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1420 /* (x + y) - (x | y) -> x & y */
1422 (minus (plus @0 @1) (bit_ior @0 @1))
1423 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1424 && !TYPE_SATURATING (type))
1427 /* (x + y) - (x & y) -> x | y */
1429 (minus (plus @0 @1) (bit_and @0 @1))
1430 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1431 && !TYPE_SATURATING (type))
1434 /* (x | y) - y -> (x & ~y) */
1436 (minus (bit_ior:cs @0 @1) @1)
1437 (bit_and @0 (bit_not @1)))
1439 /* (x | y) - (x ^ y) -> x & y */
1441 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1444 /* (x | y) - (x & y) -> x ^ y */
1446 (minus (bit_ior @0 @1) (bit_and @0 @1))
1449 /* (x | y) & ~(x & y) -> x ^ y */
1451 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1454 /* (x | y) & (~x ^ y) -> x & y */
1456 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1459 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1461 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1462 (bit_not (bit_xor @0 @1)))
1464 /* (~x | y) ^ (x | ~y) -> x ^ y */
1466 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1469 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1471 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1472 (nop_convert2? (bit_ior @0 @1))))
1474 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1475 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1476 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1477 && !TYPE_SATURATING (TREE_TYPE (@2)))
1478 (bit_not (convert (bit_xor @0 @1)))))
1480 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1482 (nop_convert3? (bit_ior @0 @1)))
1483 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1484 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1485 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1486 && !TYPE_SATURATING (TREE_TYPE (@2)))
1487 (bit_not (convert (bit_xor @0 @1)))))
1489 (minus (nop_convert1? (bit_and @0 @1))
1490 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1492 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1493 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1494 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1495 && !TYPE_SATURATING (TREE_TYPE (@2)))
1496 (bit_not (convert (bit_xor @0 @1)))))
1498 /* ~x & ~y -> ~(x | y)
1499 ~x | ~y -> ~(x & y) */
1500 (for op (bit_and bit_ior)
1501 rop (bit_ior bit_and)
1503 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1504 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1505 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1506 (bit_not (rop (convert @0) (convert @1))))))
1508 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1509 with a constant, and the two constants have no bits in common,
1510 we should treat this as a BIT_IOR_EXPR since this may produce more
1512 (for op (bit_xor plus)
1514 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1515 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1516 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1517 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1518 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1519 (bit_ior (convert @4) (convert @5)))))
1521 /* (X | Y) ^ X -> Y & ~ X*/
1523 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1524 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1525 (convert (bit_and @1 (bit_not @0)))))
1527 /* Convert ~X ^ ~Y to X ^ Y. */
1529 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1530 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1531 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1532 (bit_xor (convert @0) (convert @1))))
1534 /* Convert ~X ^ C to X ^ ~C. */
1536 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1537 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1538 (bit_xor (convert @0) (bit_not @1))))
1540 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1541 (for opo (bit_and bit_xor)
1542 opi (bit_xor bit_and)
1544 (opo:c (opi:cs @0 @1) @1)
1545 (bit_and (bit_not @0) @1)))
1547 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1548 operands are another bit-wise operation with a common input. If so,
1549 distribute the bit operations to save an operation and possibly two if
1550 constants are involved. For example, convert
1551 (A | B) & (A | C) into A | (B & C)
1552 Further simplification will occur if B and C are constants. */
1553 (for op (bit_and bit_ior bit_xor)
1554 rop (bit_ior bit_and bit_and)
1556 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1557 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1558 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1559 (rop (convert @0) (op (convert @1) (convert @2))))))
1561 /* Some simple reassociation for bit operations, also handled in reassoc. */
1562 /* (X & Y) & Y -> X & Y
1563 (X | Y) | Y -> X | Y */
1564 (for op (bit_and bit_ior)
1566 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1568 /* (X ^ Y) ^ Y -> X */
1570 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1572 /* (X & Y) & (X & Z) -> (X & Y) & Z
1573 (X | Y) | (X | Z) -> (X | Y) | Z */
1574 (for op (bit_and bit_ior)
1576 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1577 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1578 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1579 (if (single_use (@5) && single_use (@6))
1580 (op @3 (convert @2))
1581 (if (single_use (@3) && single_use (@4))
1582 (op (convert @1) @5))))))
1583 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1585 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1586 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1587 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1588 (bit_xor (convert @1) (convert @2))))
1590 /* Convert abs (abs (X)) into abs (X).
1591 also absu (absu (X)) into absu (X). */
1597 (absu (convert@2 (absu@1 @0)))
1598 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1601 /* Convert abs[u] (-X) -> abs[u] (X). */
1610 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1612 (abs tree_expr_nonnegative_p@0)
1616 (absu tree_expr_nonnegative_p@0)
1619 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1621 (mult:c (nop_convert1?
1622 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1625 (if (INTEGRAL_TYPE_P (type)
1626 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1627 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1628 (if (TYPE_UNSIGNED (type))
1635 /* A few cases of fold-const.cc negate_expr_p predicate. */
1636 (match negate_expr_p
1638 (if ((INTEGRAL_TYPE_P (type)
1639 && TYPE_UNSIGNED (type))
1640 || (!TYPE_OVERFLOW_SANITIZED (type)
1641 && may_negate_without_overflow_p (t)))))
1642 (match negate_expr_p
1644 (match negate_expr_p
1646 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1647 (match negate_expr_p
1649 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1650 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1652 (match negate_expr_p
1654 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1655 (match negate_expr_p
1657 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1658 || (FLOAT_TYPE_P (type)
1659 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1660 && !HONOR_SIGNED_ZEROS (type)))))
1662 /* (-A) * (-B) -> A * B */
1664 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1665 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1666 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1667 (mult (convert @0) (convert (negate @1)))))
1669 /* -(A + B) -> (-B) - A. */
1671 (negate (plus:c @0 negate_expr_p@1))
1672 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1673 && !HONOR_SIGNED_ZEROS (type))
1674 (minus (negate @1) @0)))
1676 /* -(A - B) -> B - A. */
1678 (negate (minus @0 @1))
1679 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1680 || (FLOAT_TYPE_P (type)
1681 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1682 && !HONOR_SIGNED_ZEROS (type)))
1685 (negate (pointer_diff @0 @1))
1686 (if (TYPE_OVERFLOW_UNDEFINED (type))
1687 (pointer_diff @1 @0)))
1689 /* A - B -> A + (-B) if B is easily negatable. */
1691 (minus @0 negate_expr_p@1)
1692 (if (!FIXED_POINT_TYPE_P (type))
1693 (plus @0 (negate @1))))
1695 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1697 (negate (mult:c@0 @1 negate_expr_p@2))
1698 (if (! TYPE_UNSIGNED (type)
1699 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1701 (mult @1 (negate @2))))
1704 (negate (rdiv@0 @1 negate_expr_p@2))
1705 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1707 (rdiv @1 (negate @2))))
1710 (negate (rdiv@0 negate_expr_p@1 @2))
1711 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1713 (rdiv (negate @1) @2)))
1715 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1717 (negate (convert? (rshift @0 INTEGER_CST@1)))
1718 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1719 && wi::to_wide (@1) == element_precision (type) - 1)
1720 (with { tree stype = TREE_TYPE (@0);
1721 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1722 : unsigned_type_for (stype); }
1723 (if (VECTOR_TYPE_P (type))
1724 (view_convert (rshift (view_convert:ntype @0) @1))
1725 (convert (rshift (convert:ntype @0) @1))))))
1727 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1729 For bitwise binary operations apply operand conversions to the
1730 binary operation result instead of to the operands. This allows
1731 to combine successive conversions and bitwise binary operations.
1732 We combine the above two cases by using a conditional convert. */
1733 (for bitop (bit_and bit_ior bit_xor)
1735 (bitop (convert@2 @0) (convert?@3 @1))
1736 (if (((TREE_CODE (@1) == INTEGER_CST
1737 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1738 && (int_fits_type_p (@1, TREE_TYPE (@0))
1739 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1740 || types_match (@0, @1))
1741 /* ??? This transform conflicts with fold-const.cc doing
1742 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1743 constants (if x has signed type, the sign bit cannot be set
1744 in c). This folds extension into the BIT_AND_EXPR.
1745 Restrict it to GIMPLE to avoid endless recursions. */
1746 && (bitop != BIT_AND_EXPR || GIMPLE)
1747 && (/* That's a good idea if the conversion widens the operand, thus
1748 after hoisting the conversion the operation will be narrower.
1749 It is also a good if the conversion is a nop as moves the
1750 conversion to one side; allowing for combining of the conversions. */
1751 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1752 /* The conversion check for being a nop can only be done at the gimple
1753 level as fold_binary has some re-association code which can conflict
1754 with this if there is a "constant" which is not a full INTEGER_CST. */
1755 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1756 /* It's also a good idea if the conversion is to a non-integer
1758 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1759 /* Or if the precision of TO is not the same as the precision
1761 || !type_has_mode_precision_p (type)
1762 /* In GIMPLE, getting rid of 2 conversions for one new results
1765 && TREE_CODE (@1) != INTEGER_CST
1766 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1768 && single_use (@3))))
1769 (convert (bitop @0 (convert @1)))))
1770 /* In GIMPLE, getting rid of 2 conversions for one new results
1773 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1775 && TREE_CODE (@1) != INTEGER_CST
1776 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1777 && types_match (type, @0))
1778 (bitop @0 (convert @1)))))
1780 (for bitop (bit_and bit_ior)
1781 rbitop (bit_ior bit_and)
1782 /* (x | y) & x -> x */
1783 /* (x & y) | x -> x */
1785 (bitop:c (rbitop:c @0 @1) @0)
1787 /* (~x | y) & x -> x & y */
1788 /* (~x & y) | x -> x | y */
1790 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1793 /* ((x | y) & z) | x -> (z & y) | x */
1795 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1796 (bit_ior (bit_and @2 @1) @0))
1798 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1800 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1801 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1803 /* Combine successive equal operations with constants. */
1804 (for bitop (bit_and bit_ior bit_xor)
1806 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1807 (if (!CONSTANT_CLASS_P (@0))
1808 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1809 folded to a constant. */
1810 (bitop @0 (bitop @1 @2))
1811 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1812 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1813 the values involved are such that the operation can't be decided at
1814 compile time. Try folding one of @0 or @1 with @2 to see whether
1815 that combination can be decided at compile time.
1817 Keep the existing form if both folds fail, to avoid endless
1819 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1821 (bitop @1 { cst1; })
1822 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1824 (bitop @0 { cst2; }))))))))
1826 /* Try simple folding for X op !X, and X op X with the help
1827 of the truth_valued_p and logical_inverted_value predicates. */
1828 (match truth_valued_p
1830 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1831 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1832 (match truth_valued_p
1834 (match truth_valued_p
1837 (match (logical_inverted_value @0)
1839 (match (logical_inverted_value @0)
1840 (bit_not truth_valued_p@0))
1841 (match (logical_inverted_value @0)
1842 (eq @0 integer_zerop))
1843 (match (logical_inverted_value @0)
1844 (ne truth_valued_p@0 integer_truep))
1845 (match (logical_inverted_value @0)
1846 (bit_xor truth_valued_p@0 integer_truep))
1850 (bit_and:c @0 (logical_inverted_value @0))
1851 { build_zero_cst (type); })
1852 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1853 (for op (bit_ior bit_xor)
1855 (op:c truth_valued_p@0 (logical_inverted_value @0))
1856 { constant_boolean_node (true, type); }))
1857 /* X ==/!= !X is false/true. */
1860 (op:c truth_valued_p@0 (logical_inverted_value @0))
1861 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1865 (bit_not (bit_not @0))
1868 (match zero_one_valued_p
1870 (if (INTEGRAL_TYPE_P (type) && tree_nonzero_bits (@0) == 1)))
1871 (match zero_one_valued_p
1874 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
1876 (mult zero_one_valued_p@0 zero_one_valued_p@1)
1877 (if (INTEGRAL_TYPE_P (type))
1880 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
1882 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
1883 (if (INTEGRAL_TYPE_P (type)
1884 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1885 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
1886 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1887 (mult (convert @0) @1)))
1889 /* Narrow integer multiplication by a zero_one_valued_p operand.
1890 Multiplication by [0,1] is guaranteed not to overflow. */
1892 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
1893 (if (INTEGRAL_TYPE_P (type)
1894 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1895 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
1896 (mult (convert @1) (convert @2))))
1898 /* Convert ~ (-A) to A - 1. */
1900 (bit_not (convert? (negate @0)))
1901 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1902 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1903 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1905 /* Convert - (~A) to A + 1. */
1907 (negate (nop_convert? (bit_not @0)))
1908 (plus (view_convert @0) { build_each_one_cst (type); }))
1910 /* (a & b) ^ (a == b) -> !(a | b) */
1911 /* (a & b) == (a ^ b) -> !(a | b) */
1912 (for first_op (bit_xor eq)
1913 second_op (eq bit_xor)
1915 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
1916 (bit_not (bit_ior @0 @1))))
1918 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1920 (bit_not (convert? (minus @0 integer_each_onep)))
1921 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1922 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1923 (convert (negate @0))))
1925 (bit_not (convert? (plus @0 integer_all_onesp)))
1926 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1927 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1928 (convert (negate @0))))
1930 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1932 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1933 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1934 (convert (bit_xor @0 (bit_not @1)))))
1936 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1937 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1938 (convert (bit_xor @0 @1))))
1940 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1942 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1943 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1944 (bit_not (bit_xor (view_convert @0) @1))))
1946 /* ~(a ^ b) is a == b for truth valued a and b. */
1948 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
1949 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1950 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1951 (convert (eq @0 @1))))
1953 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1955 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1956 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1958 /* Fold A - (A & B) into ~B & A. */
1960 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1961 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1962 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1963 (convert (bit_and (bit_not @1) @0))))
1965 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1966 (if (!canonicalize_math_p ())
1967 (for cmp (gt lt ge le)
1969 (mult (convert (cmp @0 @1)) @2)
1970 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1972 /* For integral types with undefined overflow and C != 0 fold
1973 x * C EQ/NE y * C into x EQ/NE y. */
1976 (cmp (mult:c @0 @1) (mult:c @2 @1))
1977 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1978 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1979 && tree_expr_nonzero_p (@1))
1982 /* For integral types with wrapping overflow and C odd fold
1983 x * C EQ/NE y * C into x EQ/NE y. */
1986 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1987 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1988 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1989 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1992 /* For integral types with undefined overflow and C != 0 fold
1993 x * C RELOP y * C into:
1995 x RELOP y for nonnegative C
1996 y RELOP x for negative C */
1997 (for cmp (lt gt le ge)
1999 (cmp (mult:c @0 @1) (mult:c @2 @1))
2000 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2001 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2002 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2004 (if (TREE_CODE (@1) == INTEGER_CST
2005 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2008 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2012 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2013 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2014 && TYPE_UNSIGNED (TREE_TYPE (@0))
2015 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2016 && (wi::to_wide (@2)
2017 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2018 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2019 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2021 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2022 (for cmp (simple_comparison)
2024 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2025 (if (element_precision (@3) >= element_precision (@0)
2026 && types_match (@0, @1))
2027 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2028 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2030 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2033 tree utype = unsigned_type_for (TREE_TYPE (@0));
2035 (cmp (convert:utype @1) (convert:utype @0)))))
2036 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2037 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2041 tree utype = unsigned_type_for (TREE_TYPE (@0));
2043 (cmp (convert:utype @0) (convert:utype @1)))))))))
2045 /* X / C1 op C2 into a simple range test. */
2046 (for cmp (simple_comparison)
2048 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2049 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2050 && integer_nonzerop (@1)
2051 && !TREE_OVERFLOW (@1)
2052 && !TREE_OVERFLOW (@2))
2053 (with { tree lo, hi; bool neg_overflow;
2054 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2057 (if (code == LT_EXPR || code == GE_EXPR)
2058 (if (TREE_OVERFLOW (lo))
2059 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2060 (if (code == LT_EXPR)
2063 (if (code == LE_EXPR || code == GT_EXPR)
2064 (if (TREE_OVERFLOW (hi))
2065 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2066 (if (code == LE_EXPR)
2070 { build_int_cst (type, code == NE_EXPR); })
2071 (if (code == EQ_EXPR && !hi)
2073 (if (code == EQ_EXPR && !lo)
2075 (if (code == NE_EXPR && !hi)
2077 (if (code == NE_EXPR && !lo)
2080 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2084 tree etype = range_check_type (TREE_TYPE (@0));
2087 hi = fold_convert (etype, hi);
2088 lo = fold_convert (etype, lo);
2089 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2092 (if (etype && hi && !TREE_OVERFLOW (hi))
2093 (if (code == EQ_EXPR)
2094 (le (minus (convert:etype @0) { lo; }) { hi; })
2095 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2097 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2098 (for op (lt le ge gt)
2100 (op (plus:c @0 @2) (plus:c @1 @2))
2101 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2102 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2105 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2106 when C is an unsigned integer constant with only the MSB set, and X and
2107 Y have types of equal or lower integer conversion rank than C's. */
2108 (for op (lt le ge gt)
2110 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2111 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2112 && TYPE_UNSIGNED (TREE_TYPE (@0))
2113 && wi::only_sign_bit_p (wi::to_wide (@0)))
2114 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2115 (op (convert:stype @1) (convert:stype @2))))))
2117 /* For equality and subtraction, this is also true with wrapping overflow. */
2118 (for op (eq ne minus)
2120 (op (plus:c @0 @2) (plus:c @1 @2))
2121 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2122 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2123 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2126 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2127 (for op (lt le ge gt)
2129 (op (minus @0 @2) (minus @1 @2))
2130 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2131 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2133 /* For equality and subtraction, this is also true with wrapping overflow. */
2134 (for op (eq ne minus)
2136 (op (minus @0 @2) (minus @1 @2))
2137 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2138 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2139 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2141 /* And for pointers... */
2142 (for op (simple_comparison)
2144 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2145 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2148 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2149 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2150 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2151 (pointer_diff @0 @1)))
2153 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2154 (for op (lt le ge gt)
2156 (op (minus @2 @0) (minus @2 @1))
2157 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2158 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2160 /* For equality and subtraction, this is also true with wrapping overflow. */
2161 (for op (eq ne minus)
2163 (op (minus @2 @0) (minus @2 @1))
2164 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2165 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2166 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2168 /* And for pointers... */
2169 (for op (simple_comparison)
2171 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2172 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2175 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2176 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2177 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2178 (pointer_diff @1 @0)))
2180 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2181 (for op (lt le gt ge)
2183 (op:c (plus:c@2 @0 @1) @1)
2184 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2185 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2186 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2187 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2188 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2189 /* For equality, this is also true with wrapping overflow. */
2192 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2193 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2194 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2195 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2196 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2197 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2198 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2199 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2201 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2202 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2203 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2204 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2205 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2207 /* X - Y < X is the same as Y > 0 when there is no overflow.
2208 For equality, this is also true with wrapping overflow. */
2209 (for op (simple_comparison)
2211 (op:c @0 (minus@2 @0 @1))
2212 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2213 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2214 || ((op == EQ_EXPR || op == NE_EXPR)
2215 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2216 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2217 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2220 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2221 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2225 (cmp (trunc_div @0 @1) integer_zerop)
2226 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2227 /* Complex ==/!= is allowed, but not </>=. */
2228 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2229 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2232 /* X == C - X can never be true if C is odd. */
2235 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2236 (if (TREE_INT_CST_LOW (@1) & 1)
2237 { constant_boolean_node (cmp == NE_EXPR, type); })))
2239 /* Arguments on which one can call get_nonzero_bits to get the bits
2241 (match with_possible_nonzero_bits
2243 (match with_possible_nonzero_bits
2245 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2246 /* Slightly extended version, do not make it recursive to keep it cheap. */
2247 (match (with_possible_nonzero_bits2 @0)
2248 with_possible_nonzero_bits@0)
2249 (match (with_possible_nonzero_bits2 @0)
2250 (bit_and:c with_possible_nonzero_bits@0 @2))
2252 /* Same for bits that are known to be set, but we do not have
2253 an equivalent to get_nonzero_bits yet. */
2254 (match (with_certain_nonzero_bits2 @0)
2256 (match (with_certain_nonzero_bits2 @0)
2257 (bit_ior @1 INTEGER_CST@0))
2259 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2262 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2263 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2264 { constant_boolean_node (cmp == NE_EXPR, type); })))
2266 /* ((X inner_op C0) outer_op C1)
2267 With X being a tree where value_range has reasoned certain bits to always be
2268 zero throughout its computed value range,
2269 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2270 where zero_mask has 1's for all bits that are sure to be 0 in
2272 if (inner_op == '^') C0 &= ~C1;
2273 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2274 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2276 (for inner_op (bit_ior bit_xor)
2277 outer_op (bit_xor bit_ior)
2280 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2284 wide_int zero_mask_not;
2288 if (TREE_CODE (@2) == SSA_NAME)
2289 zero_mask_not = get_nonzero_bits (@2);
2293 if (inner_op == BIT_XOR_EXPR)
2295 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2296 cst_emit = C0 | wi::to_wide (@1);
2300 C0 = wi::to_wide (@0);
2301 cst_emit = C0 ^ wi::to_wide (@1);
2304 (if (!fail && (C0 & zero_mask_not) == 0)
2305 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2306 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2307 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2309 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2311 (pointer_plus (pointer_plus:s @0 @1) @3)
2312 (pointer_plus @0 (plus @1 @3)))
2315 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2316 (convert:type (pointer_plus @0 (plus @1 @3))))
2323 tem4 = (unsigned long) tem3;
2328 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2329 /* Conditionally look through a sign-changing conversion. */
2330 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2331 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2332 || (GENERIC && type == TREE_TYPE (@1))))
2335 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2336 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2340 tem = (sizetype) ptr;
2344 and produce the simpler and easier to analyze with respect to alignment
2345 ... = ptr & ~algn; */
2347 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2348 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2349 (bit_and @0 { algn; })))
2351 /* Try folding difference of addresses. */
2353 (minus (convert ADDR_EXPR@0) (convert @1))
2354 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2355 (with { poly_int64 diff; }
2356 (if (ptr_difference_const (@0, @1, &diff))
2357 { build_int_cst_type (type, diff); }))))
2359 (minus (convert @0) (convert ADDR_EXPR@1))
2360 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2361 (with { poly_int64 diff; }
2362 (if (ptr_difference_const (@0, @1, &diff))
2363 { build_int_cst_type (type, diff); }))))
2365 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2366 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2367 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2368 (with { poly_int64 diff; }
2369 (if (ptr_difference_const (@0, @1, &diff))
2370 { build_int_cst_type (type, diff); }))))
2372 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2373 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2374 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2375 (with { poly_int64 diff; }
2376 (if (ptr_difference_const (@0, @1, &diff))
2377 { build_int_cst_type (type, diff); }))))
2379 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2381 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2382 (with { poly_int64 diff; }
2383 (if (ptr_difference_const (@0, @2, &diff))
2384 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2386 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2389 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2390 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2391 (if (ptr_difference_const (@0, @2, &diff))
2392 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2394 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2396 (convert (pointer_diff @0 INTEGER_CST@1))
2397 (if (POINTER_TYPE_P (type))
2398 { build_fold_addr_expr_with_type
2399 (build2 (MEM_REF, char_type_node, @0,
2400 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2403 /* If arg0 is derived from the address of an object or function, we may
2404 be able to fold this expression using the object or function's
2407 (bit_and (convert? @0) INTEGER_CST@1)
2408 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2409 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2413 unsigned HOST_WIDE_INT bitpos;
2414 get_pointer_alignment_1 (@0, &align, &bitpos);
2416 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2417 { wide_int_to_tree (type, (wi::to_wide (@1)
2418 & (bitpos / BITS_PER_UNIT))); }))))
2422 (if (INTEGRAL_TYPE_P (type)
2423 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2427 (if (INTEGRAL_TYPE_P (type)
2428 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2430 /* x > y && x != XXX_MIN --> x > y
2431 x > y && x == XXX_MIN --> false . */
2434 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2436 (if (eqne == EQ_EXPR)
2437 { constant_boolean_node (false, type); })
2438 (if (eqne == NE_EXPR)
2442 /* x < y && x != XXX_MAX --> x < y
2443 x < y && x == XXX_MAX --> false. */
2446 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2448 (if (eqne == EQ_EXPR)
2449 { constant_boolean_node (false, type); })
2450 (if (eqne == NE_EXPR)
2454 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2456 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2459 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2461 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2464 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2466 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2469 /* x <= y || x != XXX_MIN --> true. */
2471 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2472 { constant_boolean_node (true, type); })
2474 /* x <= y || x == XXX_MIN --> x <= y. */
2476 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2479 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2481 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2484 /* x >= y || x != XXX_MAX --> true
2485 x >= y || x == XXX_MAX --> x >= y. */
2488 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2490 (if (eqne == EQ_EXPR)
2492 (if (eqne == NE_EXPR)
2493 { constant_boolean_node (true, type); }))))
2495 /* y == XXX_MIN || x < y --> x <= y - 1 */
2497 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2498 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2499 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2500 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2502 /* y != XXX_MIN && x >= y --> x > y - 1 */
2504 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2505 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2506 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2507 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2509 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2510 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2513 (for code2 (eq ne lt gt le ge)
2515 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2518 int cmp = tree_int_cst_compare (@1, @2);
2522 case EQ_EXPR: val = (cmp == 0); break;
2523 case NE_EXPR: val = (cmp != 0); break;
2524 case LT_EXPR: val = (cmp < 0); break;
2525 case GT_EXPR: val = (cmp > 0); break;
2526 case LE_EXPR: val = (cmp <= 0); break;
2527 case GE_EXPR: val = (cmp >= 0); break;
2528 default: gcc_unreachable ();
2532 (if (code1 == EQ_EXPR && val) @3)
2533 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2534 (if (code1 == NE_EXPR && !val) @4))))))
2536 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2538 (for code1 (lt le gt ge)
2539 (for code2 (lt le gt ge)
2541 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2544 int cmp = tree_int_cst_compare (@1, @2);
2547 /* Choose the more restrictive of two < or <= comparisons. */
2548 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2549 && (code2 == LT_EXPR || code2 == LE_EXPR))
2550 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2553 /* Likewise chose the more restrictive of two > or >= comparisons. */
2554 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2555 && (code2 == GT_EXPR || code2 == GE_EXPR))
2556 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2559 /* Check for singleton ranges. */
2561 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2562 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2564 /* Check for disjoint ranges. */
2566 && (code1 == LT_EXPR || code1 == LE_EXPR)
2567 && (code2 == GT_EXPR || code2 == GE_EXPR))
2568 { constant_boolean_node (false, type); })
2570 && (code1 == GT_EXPR || code1 == GE_EXPR)
2571 && (code2 == LT_EXPR || code2 == LE_EXPR))
2572 { constant_boolean_node (false, type); })
2575 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2576 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2579 (for code2 (eq ne lt gt le ge)
2581 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2584 int cmp = tree_int_cst_compare (@1, @2);
2588 case EQ_EXPR: val = (cmp == 0); break;
2589 case NE_EXPR: val = (cmp != 0); break;
2590 case LT_EXPR: val = (cmp < 0); break;
2591 case GT_EXPR: val = (cmp > 0); break;
2592 case LE_EXPR: val = (cmp <= 0); break;
2593 case GE_EXPR: val = (cmp >= 0); break;
2594 default: gcc_unreachable ();
2598 (if (code1 == EQ_EXPR && val) @4)
2599 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2600 (if (code1 == NE_EXPR && !val) @3))))))
2602 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2604 (for code1 (lt le gt ge)
2605 (for code2 (lt le gt ge)
2607 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2610 int cmp = tree_int_cst_compare (@1, @2);
2613 /* Choose the more restrictive of two < or <= comparisons. */
2614 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2615 && (code2 == LT_EXPR || code2 == LE_EXPR))
2616 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2619 /* Likewise chose the more restrictive of two > or >= comparisons. */
2620 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2621 && (code2 == GT_EXPR || code2 == GE_EXPR))
2622 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2625 /* Check for singleton ranges. */
2627 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2628 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2630 /* Check for disjoint ranges. */
2632 && (code1 == LT_EXPR || code1 == LE_EXPR)
2633 && (code2 == GT_EXPR || code2 == GE_EXPR))
2634 { constant_boolean_node (true, type); })
2636 && (code1 == GT_EXPR || code1 == GE_EXPR)
2637 && (code2 == LT_EXPR || code2 == LE_EXPR))
2638 { constant_boolean_node (true, type); })
2641 /* We can't reassociate at all for saturating types. */
2642 (if (!TYPE_SATURATING (type))
2644 /* Contract negates. */
2645 /* A + (-B) -> A - B */
2647 (plus:c @0 (convert? (negate @1)))
2648 /* Apply STRIP_NOPS on the negate. */
2649 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2650 && !TYPE_OVERFLOW_SANITIZED (type))
2654 if (INTEGRAL_TYPE_P (type)
2655 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2656 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2658 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2659 /* A - (-B) -> A + B */
2661 (minus @0 (convert? (negate @1)))
2662 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2663 && !TYPE_OVERFLOW_SANITIZED (type))
2667 if (INTEGRAL_TYPE_P (type)
2668 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2669 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2671 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2673 Sign-extension is ok except for INT_MIN, which thankfully cannot
2674 happen without overflow. */
2676 (negate (convert (negate @1)))
2677 (if (INTEGRAL_TYPE_P (type)
2678 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2679 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2680 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2681 && !TYPE_OVERFLOW_SANITIZED (type)
2682 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2685 (negate (convert negate_expr_p@1))
2686 (if (SCALAR_FLOAT_TYPE_P (type)
2687 && ((DECIMAL_FLOAT_TYPE_P (type)
2688 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2689 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2690 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2691 (convert (negate @1))))
2693 (negate (nop_convert? (negate @1)))
2694 (if (!TYPE_OVERFLOW_SANITIZED (type)
2695 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2698 /* We can't reassociate floating-point unless -fassociative-math
2699 or fixed-point plus or minus because of saturation to +-Inf. */
2700 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2701 && !FIXED_POINT_TYPE_P (type))
2703 /* Match patterns that allow contracting a plus-minus pair
2704 irrespective of overflow issues. */
2705 /* (A +- B) - A -> +- B */
2706 /* (A +- B) -+ B -> A */
2707 /* A - (A +- B) -> -+ B */
2708 /* A +- (B -+ A) -> +- B */
2710 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2713 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2714 (if (!ANY_INTEGRAL_TYPE_P (type)
2715 || TYPE_OVERFLOW_WRAPS (type))
2716 (negate (view_convert @1))
2717 (view_convert (negate @1))))
2719 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2722 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2723 (if (!ANY_INTEGRAL_TYPE_P (type)
2724 || TYPE_OVERFLOW_WRAPS (type))
2725 (negate (view_convert @1))
2726 (view_convert (negate @1))))
2728 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2730 /* (A +- B) + (C - A) -> C +- B */
2731 /* (A + B) - (A - C) -> B + C */
2732 /* More cases are handled with comparisons. */
2734 (plus:c (plus:c @0 @1) (minus @2 @0))
2737 (plus:c (minus @0 @1) (minus @2 @0))
2740 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2741 (if (TYPE_OVERFLOW_UNDEFINED (type)
2742 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2743 (pointer_diff @2 @1)))
2745 (minus (plus:c @0 @1) (minus @0 @2))
2748 /* (A +- CST1) +- CST2 -> A + CST3
2749 Use view_convert because it is safe for vectors and equivalent for
2751 (for outer_op (plus minus)
2752 (for inner_op (plus minus)
2753 neg_inner_op (minus plus)
2755 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2757 /* If one of the types wraps, use that one. */
2758 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2759 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2760 forever if something doesn't simplify into a constant. */
2761 (if (!CONSTANT_CLASS_P (@0))
2762 (if (outer_op == PLUS_EXPR)
2763 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2764 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2765 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2766 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2767 (if (outer_op == PLUS_EXPR)
2768 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2769 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2770 /* If the constant operation overflows we cannot do the transform
2771 directly as we would introduce undefined overflow, for example
2772 with (a - 1) + INT_MIN. */
2773 (if (types_match (type, @0))
2774 (with { tree cst = const_binop (outer_op == inner_op
2775 ? PLUS_EXPR : MINUS_EXPR,
2777 (if (cst && !TREE_OVERFLOW (cst))
2778 (inner_op @0 { cst; } )
2779 /* X+INT_MAX+1 is X-INT_MIN. */
2780 (if (INTEGRAL_TYPE_P (type) && cst
2781 && wi::to_wide (cst) == wi::min_value (type))
2782 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2783 /* Last resort, use some unsigned type. */
2784 (with { tree utype = unsigned_type_for (type); }
2786 (view_convert (inner_op
2787 (view_convert:utype @0)
2789 { drop_tree_overflow (cst); }))))))))))))))
2791 /* (CST1 - A) +- CST2 -> CST3 - A */
2792 (for outer_op (plus minus)
2794 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2795 /* If one of the types wraps, use that one. */
2796 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2797 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2798 forever if something doesn't simplify into a constant. */
2799 (if (!CONSTANT_CLASS_P (@0))
2800 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2801 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2802 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2803 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2804 (if (types_match (type, @0))
2805 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2806 (if (cst && !TREE_OVERFLOW (cst))
2807 (minus { cst; } @0))))))))
2809 /* CST1 - (CST2 - A) -> CST3 + A
2810 Use view_convert because it is safe for vectors and equivalent for
2813 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2814 /* If one of the types wraps, use that one. */
2815 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2816 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2817 forever if something doesn't simplify into a constant. */
2818 (if (!CONSTANT_CLASS_P (@0))
2819 (plus (view_convert @0) (minus @1 (view_convert @2))))
2820 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2821 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2822 (view_convert (plus @0 (minus (view_convert @1) @2)))
2823 (if (types_match (type, @0))
2824 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2825 (if (cst && !TREE_OVERFLOW (cst))
2826 (plus { cst; } @0)))))))
2828 /* ((T)(A)) + CST -> (T)(A + CST) */
2831 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2832 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2833 && TREE_CODE (type) == INTEGER_TYPE
2834 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2835 && int_fits_type_p (@1, TREE_TYPE (@0)))
2836 /* Perform binary operation inside the cast if the constant fits
2837 and (A + CST)'s range does not overflow. */
2840 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2841 max_ovf = wi::OVF_OVERFLOW;
2842 tree inner_type = TREE_TYPE (@0);
2845 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2846 TYPE_SIGN (inner_type));
2849 if (get_global_range_query ()->range_of_expr (vr, @0)
2850 && vr.kind () == VR_RANGE)
2852 wide_int wmin0 = vr.lower_bound ();
2853 wide_int wmax0 = vr.upper_bound ();
2854 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2855 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2858 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2859 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2863 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2865 (for op (plus minus)
2867 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2868 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2869 && TREE_CODE (type) == INTEGER_TYPE
2870 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2871 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2872 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2873 && TYPE_OVERFLOW_WRAPS (type))
2874 (plus (convert @0) (op @2 (convert @1))))))
2877 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2878 to a simple value. */
2879 (for op (plus minus)
2881 (op (convert @0) (convert @1))
2882 (if (INTEGRAL_TYPE_P (type)
2883 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2884 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2885 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2886 && !TYPE_OVERFLOW_TRAPS (type)
2887 && !TYPE_OVERFLOW_SANITIZED (type))
2888 (convert (op! @0 @1)))))
2892 (plus:c (bit_not @0) @0)
2893 (if (!TYPE_OVERFLOW_TRAPS (type))
2894 { build_all_ones_cst (type); }))
2898 (plus (convert? (bit_not @0)) integer_each_onep)
2899 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2900 (negate (convert @0))))
2904 (minus (convert? (negate @0)) integer_each_onep)
2905 (if (!TYPE_OVERFLOW_TRAPS (type)
2906 && TREE_CODE (type) != COMPLEX_TYPE
2907 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2908 (bit_not (convert @0))))
2912 (minus integer_all_onesp @0)
2913 (if (TREE_CODE (type) != COMPLEX_TYPE)
2916 /* (T)(P + A) - (T)P -> (T) A */
2918 (minus (convert (plus:c @@0 @1))
2920 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2921 /* For integer types, if A has a smaller type
2922 than T the result depends on the possible
2924 E.g. T=size_t, A=(unsigned)429497295, P>0.
2925 However, if an overflow in P + A would cause
2926 undefined behavior, we can assume that there
2928 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2929 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2932 (minus (convert (pointer_plus @@0 @1))
2934 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2935 /* For pointer types, if the conversion of A to the
2936 final type requires a sign- or zero-extension,
2937 then we have to punt - it is not defined which
2939 || (POINTER_TYPE_P (TREE_TYPE (@0))
2940 && TREE_CODE (@1) == INTEGER_CST
2941 && tree_int_cst_sign_bit (@1) == 0))
2944 (pointer_diff (pointer_plus @@0 @1) @0)
2945 /* The second argument of pointer_plus must be interpreted as signed, and
2946 thus sign-extended if necessary. */
2947 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2948 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2949 second arg is unsigned even when we need to consider it as signed,
2950 we don't want to diagnose overflow here. */
2951 (convert (view_convert:stype @1))))
2953 /* (T)P - (T)(P + A) -> -(T) A */
2955 (minus (convert? @0)
2956 (convert (plus:c @@0 @1)))
2957 (if (INTEGRAL_TYPE_P (type)
2958 && TYPE_OVERFLOW_UNDEFINED (type)
2959 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2960 (with { tree utype = unsigned_type_for (type); }
2961 (convert (negate (convert:utype @1))))
2962 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2963 /* For integer types, if A has a smaller type
2964 than T the result depends on the possible
2966 E.g. T=size_t, A=(unsigned)429497295, P>0.
2967 However, if an overflow in P + A would cause
2968 undefined behavior, we can assume that there
2970 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2971 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2972 (negate (convert @1)))))
2975 (convert (pointer_plus @@0 @1)))
2976 (if (INTEGRAL_TYPE_P (type)
2977 && TYPE_OVERFLOW_UNDEFINED (type)
2978 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2979 (with { tree utype = unsigned_type_for (type); }
2980 (convert (negate (convert:utype @1))))
2981 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2982 /* For pointer types, if the conversion of A to the
2983 final type requires a sign- or zero-extension,
2984 then we have to punt - it is not defined which
2986 || (POINTER_TYPE_P (TREE_TYPE (@0))
2987 && TREE_CODE (@1) == INTEGER_CST
2988 && tree_int_cst_sign_bit (@1) == 0))
2989 (negate (convert @1)))))
2991 (pointer_diff @0 (pointer_plus @@0 @1))
2992 /* The second argument of pointer_plus must be interpreted as signed, and
2993 thus sign-extended if necessary. */
2994 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2995 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2996 second arg is unsigned even when we need to consider it as signed,
2997 we don't want to diagnose overflow here. */
2998 (negate (convert (view_convert:stype @1)))))
3000 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3002 (minus (convert (plus:c @@0 @1))
3003 (convert (plus:c @0 @2)))
3004 (if (INTEGRAL_TYPE_P (type)
3005 && TYPE_OVERFLOW_UNDEFINED (type)
3006 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3007 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3008 (with { tree utype = unsigned_type_for (type); }
3009 (convert (minus (convert:utype @1) (convert:utype @2))))
3010 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3011 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3012 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3013 /* For integer types, if A has a smaller type
3014 than T the result depends on the possible
3016 E.g. T=size_t, A=(unsigned)429497295, P>0.
3017 However, if an overflow in P + A would cause
3018 undefined behavior, we can assume that there
3020 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3021 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3022 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3023 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3024 (minus (convert @1) (convert @2)))))
3026 (minus (convert (pointer_plus @@0 @1))
3027 (convert (pointer_plus @0 @2)))
3028 (if (INTEGRAL_TYPE_P (type)
3029 && TYPE_OVERFLOW_UNDEFINED (type)
3030 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3031 (with { tree utype = unsigned_type_for (type); }
3032 (convert (minus (convert:utype @1) (convert:utype @2))))
3033 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3034 /* For pointer types, if the conversion of A to the
3035 final type requires a sign- or zero-extension,
3036 then we have to punt - it is not defined which
3038 || (POINTER_TYPE_P (TREE_TYPE (@0))
3039 && TREE_CODE (@1) == INTEGER_CST
3040 && tree_int_cst_sign_bit (@1) == 0
3041 && TREE_CODE (@2) == INTEGER_CST
3042 && tree_int_cst_sign_bit (@2) == 0))
3043 (minus (convert @1) (convert @2)))))
3045 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3046 (pointer_diff @0 @1))
3048 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3049 /* The second argument of pointer_plus must be interpreted as signed, and
3050 thus sign-extended if necessary. */
3051 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3052 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3053 second arg is unsigned even when we need to consider it as signed,
3054 we don't want to diagnose overflow here. */
3055 (minus (convert (view_convert:stype @1))
3056 (convert (view_convert:stype @2)))))))
3058 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3059 Modeled after fold_plusminus_mult_expr. */
3060 (if (!TYPE_SATURATING (type)
3061 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3062 (for plusminus (plus minus)
3064 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3065 (if (!ANY_INTEGRAL_TYPE_P (type)
3066 || TYPE_OVERFLOW_WRAPS (type)
3067 || (INTEGRAL_TYPE_P (type)
3068 && tree_expr_nonzero_p (@0)
3069 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3070 (if (single_use (@3) || single_use (@4))
3071 /* If @1 +- @2 is constant require a hard single-use on either
3072 original operand (but not on both). */
3073 (mult (plusminus @1 @2) @0)
3074 (mult! (plusminus @1 @2) @0)
3076 /* We cannot generate constant 1 for fract. */
3077 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3079 (plusminus @0 (mult:c@3 @0 @2))
3080 (if ((!ANY_INTEGRAL_TYPE_P (type)
3081 || TYPE_OVERFLOW_WRAPS (type)
3082 /* For @0 + @0*@2 this transformation would introduce UB
3083 (where there was none before) for @0 in [-1,0] and @2 max.
3084 For @0 - @0*@2 this transformation would introduce UB
3085 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3086 || (INTEGRAL_TYPE_P (type)
3087 && ((tree_expr_nonzero_p (@0)
3088 && expr_not_equal_to (@0,
3089 wi::minus_one (TYPE_PRECISION (type))))
3090 || (plusminus == PLUS_EXPR
3091 ? expr_not_equal_to (@2,
3092 wi::max_value (TYPE_PRECISION (type), SIGNED))
3093 /* Let's ignore the @0 -1 and @2 min case. */
3094 : (expr_not_equal_to (@2,
3095 wi::min_value (TYPE_PRECISION (type), SIGNED))
3096 && expr_not_equal_to (@2,
3097 wi::min_value (TYPE_PRECISION (type), SIGNED)
3100 (mult (plusminus { build_one_cst (type); } @2) @0)))
3102 (plusminus (mult:c@3 @0 @2) @0)
3103 (if ((!ANY_INTEGRAL_TYPE_P (type)
3104 || TYPE_OVERFLOW_WRAPS (type)
3105 /* For @0*@2 + @0 this transformation would introduce UB
3106 (where there was none before) for @0 in [-1,0] and @2 max.
3107 For @0*@2 - @0 this transformation would introduce UB
3108 for @0 0 and @2 min. */
3109 || (INTEGRAL_TYPE_P (type)
3110 && ((tree_expr_nonzero_p (@0)
3111 && (plusminus == MINUS_EXPR
3112 || expr_not_equal_to (@0,
3113 wi::minus_one (TYPE_PRECISION (type)))))
3114 || expr_not_equal_to (@2,
3115 (plusminus == PLUS_EXPR
3116 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3117 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3119 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3122 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3123 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3125 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3126 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3127 && tree_fits_uhwi_p (@1)
3128 && tree_to_uhwi (@1) < element_precision (type)
3129 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3130 || optab_handler (smul_optab,
3131 TYPE_MODE (type)) != CODE_FOR_nothing))
3132 (with { tree t = type;
3133 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3134 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3135 element_precision (type));
3137 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3139 cst = build_uniform_cst (t, cst); }
3140 (convert (mult (convert:t @0) { cst; })))))
3142 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3143 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3144 && tree_fits_uhwi_p (@1)
3145 && tree_to_uhwi (@1) < element_precision (type)
3146 && tree_fits_uhwi_p (@2)
3147 && tree_to_uhwi (@2) < element_precision (type)
3148 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3149 || optab_handler (smul_optab,
3150 TYPE_MODE (type)) != CODE_FOR_nothing))
3151 (with { tree t = type;
3152 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3153 unsigned int prec = element_precision (type);
3154 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3155 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3156 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3158 cst = build_uniform_cst (t, cst); }
3159 (convert (mult (convert:t @0) { cst; })))))
3162 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3163 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3164 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3165 (for op (bit_ior bit_xor)
3167 (op (mult:s@0 @1 INTEGER_CST@2)
3168 (mult:s@3 @1 INTEGER_CST@4))
3169 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3170 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3172 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3174 (op:c (mult:s@0 @1 INTEGER_CST@2)
3175 (lshift:s@3 @1 INTEGER_CST@4))
3176 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3177 && tree_int_cst_sgn (@4) > 0
3178 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3179 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3180 wide_int c = wi::add (wi::to_wide (@2),
3181 wi::lshift (wone, wi::to_wide (@4))); }
3182 (mult @1 { wide_int_to_tree (type, c); }))))
3184 (op:c (mult:s@0 @1 INTEGER_CST@2)
3186 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3187 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3189 { wide_int_to_tree (type,
3190 wi::add (wi::to_wide (@2), 1)); })))
3192 (op (lshift:s@0 @1 INTEGER_CST@2)
3193 (lshift:s@3 @1 INTEGER_CST@4))
3194 (if (INTEGRAL_TYPE_P (type)
3195 && tree_int_cst_sgn (@2) > 0
3196 && tree_int_cst_sgn (@4) > 0
3197 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3198 (with { tree t = type;
3199 if (!TYPE_OVERFLOW_WRAPS (t))
3200 t = unsigned_type_for (t);
3201 wide_int wone = wi::one (TYPE_PRECISION (t));
3202 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3203 wi::lshift (wone, wi::to_wide (@4))); }
3204 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3206 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3208 (if (INTEGRAL_TYPE_P (type)
3209 && tree_int_cst_sgn (@2) > 0
3210 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3211 (with { tree t = type;
3212 if (!TYPE_OVERFLOW_WRAPS (t))
3213 t = unsigned_type_for (t);
3214 wide_int wone = wi::one (TYPE_PRECISION (t));
3215 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3216 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3218 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3220 (for minmax (min max)
3224 /* For fmin() and fmax(), skip folding when both are sNaN. */
3225 (for minmax (FMIN_ALL FMAX_ALL)
3228 (if (!tree_expr_maybe_signaling_nan_p (@0))
3230 /* min(max(x,y),y) -> y. */
3232 (min:c (max:c @0 @1) @1)
3234 /* max(min(x,y),y) -> y. */
3236 (max:c (min:c @0 @1) @1)
3238 /* max(a,-a) -> abs(a). */
3240 (max:c @0 (negate @0))
3241 (if (TREE_CODE (type) != COMPLEX_TYPE
3242 && (! ANY_INTEGRAL_TYPE_P (type)
3243 || TYPE_OVERFLOW_UNDEFINED (type)))
3245 /* min(a,-a) -> -abs(a). */
3247 (min:c @0 (negate @0))
3248 (if (TREE_CODE (type) != COMPLEX_TYPE
3249 && (! ANY_INTEGRAL_TYPE_P (type)
3250 || TYPE_OVERFLOW_UNDEFINED (type)))
3255 (if (INTEGRAL_TYPE_P (type)
3256 && TYPE_MIN_VALUE (type)
3257 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3259 (if (INTEGRAL_TYPE_P (type)
3260 && TYPE_MAX_VALUE (type)
3261 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3266 (if (INTEGRAL_TYPE_P (type)
3267 && TYPE_MAX_VALUE (type)
3268 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3270 (if (INTEGRAL_TYPE_P (type)
3271 && TYPE_MIN_VALUE (type)
3272 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3275 /* max (a, a + CST) -> a + CST where CST is positive. */
3276 /* max (a, a + CST) -> a where CST is negative. */
3278 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3279 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3280 (if (tree_int_cst_sgn (@1) > 0)
3284 /* min (a, a + CST) -> a where CST is positive. */
3285 /* min (a, a + CST) -> a + CST where CST is negative. */
3287 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3288 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3289 (if (tree_int_cst_sgn (@1) > 0)
3293 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3294 the addresses are known to be less, equal or greater. */
3295 (for minmax (min max)
3298 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3301 poly_int64 off0, off1;
3303 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3304 off0, off1, GENERIC);
3307 (if (minmax == MIN_EXPR)
3308 (if (known_le (off0, off1))
3310 (if (known_gt (off0, off1))
3312 (if (known_ge (off0, off1))
3314 (if (known_lt (off0, off1))
3317 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3318 and the outer convert demotes the expression back to x's type. */
3319 (for minmax (min max)
3321 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3322 (if (INTEGRAL_TYPE_P (type)
3323 && types_match (@1, type) && int_fits_type_p (@2, type)
3324 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3325 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3326 (minmax @1 (convert @2)))))
3328 (for minmax (FMIN_ALL FMAX_ALL)
3329 /* If either argument is NaN and other one is not sNaN, return the other
3330 one. Avoid the transformation if we get (and honor) a signalling NaN. */
3332 (minmax:c @0 REAL_CST@1)
3333 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3334 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3335 && !tree_expr_maybe_signaling_nan_p (@0))
3337 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3338 functions to return the numeric arg if the other one is NaN.
3339 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3340 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3341 worry about it either. */
3342 (if (flag_finite_math_only)
3349 /* min (-A, -B) -> -max (A, B) */
3350 (for minmax (min max FMIN_ALL FMAX_ALL)
3351 maxmin (max min FMAX_ALL FMIN_ALL)
3353 (minmax (negate:s@2 @0) (negate:s@3 @1))
3354 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3355 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3356 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3357 (negate (maxmin @0 @1)))))
3358 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3359 MAX (~X, ~Y) -> ~MIN (X, Y) */
3360 (for minmax (min max)
3363 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3364 (bit_not (maxmin @0 @1))))
3366 /* MIN (X, Y) == X -> X <= Y */
3367 (for minmax (min min max max)
3371 (cmp:c (minmax:c @0 @1) @0)
3372 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3374 /* MIN (X, 5) == 0 -> X == 0
3375 MIN (X, 5) == 7 -> false */
3378 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3379 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3380 TYPE_SIGN (TREE_TYPE (@0))))
3381 { constant_boolean_node (cmp == NE_EXPR, type); }
3382 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3383 TYPE_SIGN (TREE_TYPE (@0))))
3387 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3388 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3389 TYPE_SIGN (TREE_TYPE (@0))))
3390 { constant_boolean_node (cmp == NE_EXPR, type); }
3391 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3392 TYPE_SIGN (TREE_TYPE (@0))))
3394 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3395 (for minmax (min min max max min min max max )
3396 cmp (lt le gt ge gt ge lt le )
3397 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3399 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3400 (comb (cmp @0 @2) (cmp @1 @2))))
3402 /* X <= MAX(X, Y) -> true
3403 X > MAX(X, Y) -> false
3404 X >= MIN(X, Y) -> true
3405 X < MIN(X, Y) -> false */
3406 (for minmax (min min max max )
3409 (cmp @0 (minmax:c @0 @1))
3410 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3412 /* Undo fancy ways of writing max/min or other ?: expressions, like
3413 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
3414 People normally use ?: and that is what we actually try to optimize. */
3415 /* Transform A + (B-A)*cmp into cmp ? B : A. */
3417 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3418 (if (INTEGRAL_TYPE_P (type)
3419 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3420 (cond (convert:boolean_type_node @2) @1 @0)))
3421 /* Transform A - (A-B)*cmp into cmp ? B : A. */
3423 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3424 (if (INTEGRAL_TYPE_P (type)
3425 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3426 (cond (convert:boolean_type_node @2) @1 @0)))
3427 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
3429 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3430 (if (INTEGRAL_TYPE_P (type)
3431 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3432 (cond (convert:boolean_type_node @2) @1 @0)))
3434 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
3436 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3439 /* Simplifications of shift and rotates. */
3441 (for rotate (lrotate rrotate)
3443 (rotate integer_all_onesp@0 @1)
3446 /* Optimize -1 >> x for arithmetic right shifts. */
3448 (rshift integer_all_onesp@0 @1)
3449 (if (!TYPE_UNSIGNED (type))
3452 /* Optimize (x >> c) << c into x & (-1<<c). */
3454 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3455 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3456 /* It doesn't matter if the right shift is arithmetic or logical. */
3457 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3460 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3461 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3462 /* Allow intermediate conversion to integral type with whatever sign, as
3463 long as the low TYPE_PRECISION (type)
3464 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3465 && INTEGRAL_TYPE_P (type)
3466 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3467 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3468 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3469 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3470 || wi::geu_p (wi::to_wide (@1),
3471 TYPE_PRECISION (type)
3472 - TYPE_PRECISION (TREE_TYPE (@2)))))
3473 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3475 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3478 (rshift (lshift @0 INTEGER_CST@1) @1)
3479 (if (TYPE_UNSIGNED (type)
3480 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3481 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3483 /* Optimize x >> x into 0 */
3486 { build_zero_cst (type); })
3488 (for shiftrotate (lrotate rrotate lshift rshift)
3490 (shiftrotate @0 integer_zerop)
3493 (shiftrotate integer_zerop@0 @1)
3495 /* Prefer vector1 << scalar to vector1 << vector2
3496 if vector2 is uniform. */
3497 (for vec (VECTOR_CST CONSTRUCTOR)
3499 (shiftrotate @0 vec@1)
3500 (with { tree tem = uniform_vector_p (@1); }
3502 (shiftrotate @0 { tem; }))))))
3504 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3505 Y is 0. Similarly for X >> Y. */
3507 (for shift (lshift rshift)
3509 (shift @0 SSA_NAME@1)
3510 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3512 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3513 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3515 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3519 /* Rewrite an LROTATE_EXPR by a constant into an
3520 RROTATE_EXPR by a new constant. */
3522 (lrotate @0 INTEGER_CST@1)
3523 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3524 build_int_cst (TREE_TYPE (@1),
3525 element_precision (type)), @1); }))
3527 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3528 (for op (lrotate rrotate rshift lshift)
3530 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3531 (with { unsigned int prec = element_precision (type); }
3532 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3533 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3534 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3535 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3536 (with { unsigned int low = (tree_to_uhwi (@1)
3537 + tree_to_uhwi (@2)); }
3538 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3539 being well defined. */
3541 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3542 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3543 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3544 { build_zero_cst (type); }
3545 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3546 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3549 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3551 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3552 (if ((wi::to_wide (@1) & 1) != 0)
3553 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3554 { build_zero_cst (type); }))
3556 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3557 either to false if D is smaller (unsigned comparison) than C, or to
3558 x == log2 (D) - log2 (C). Similarly for right shifts. */
3562 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3563 (with { int c1 = wi::clz (wi::to_wide (@1));
3564 int c2 = wi::clz (wi::to_wide (@2)); }
3566 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3567 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3569 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3570 (if (tree_int_cst_sgn (@1) > 0)
3571 (with { int c1 = wi::clz (wi::to_wide (@1));
3572 int c2 = wi::clz (wi::to_wide (@2)); }
3574 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3575 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3577 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3578 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3582 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3583 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3585 || (!integer_zerop (@2)
3586 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3587 { constant_boolean_node (cmp == NE_EXPR, type); }
3588 (if (!integer_zerop (@2)
3589 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3590 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3592 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
3593 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
3596 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3597 (if (tree_fits_shwi_p (@1)
3598 && tree_to_shwi (@1) > 0
3599 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))
3600 && tree_to_shwi (@1) <= wi::ctz (wi::to_wide (@3)))
3601 (with { wide_int c1 = wi::to_wide (@1);
3602 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
3603 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
3604 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
3605 { wide_int_to_tree (TREE_TYPE (@0), c3); }))))
3607 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3608 (if (tree_fits_shwi_p (@1)
3609 && tree_to_shwi (@1) > 0
3610 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))
3611 && tree_to_shwi (@1) <= wi::clz (wi::to_wide (@2))
3612 && tree_to_shwi (@1) <= wi::clz (wi::to_wide (@3)))
3613 (cmp (bit_and @0 (lshift @2 @1)) (lshift @3 @1)))))
3615 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3616 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3617 if the new mask might be further optimized. */
3618 (for shift (lshift rshift)
3620 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3622 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3623 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3624 && tree_fits_uhwi_p (@1)
3625 && tree_to_uhwi (@1) > 0
3626 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3629 unsigned int shiftc = tree_to_uhwi (@1);
3630 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3631 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3632 tree shift_type = TREE_TYPE (@3);
3635 if (shift == LSHIFT_EXPR)
3636 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3637 else if (shift == RSHIFT_EXPR
3638 && type_has_mode_precision_p (shift_type))
3640 prec = TYPE_PRECISION (TREE_TYPE (@3));
3642 /* See if more bits can be proven as zero because of
3645 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3647 tree inner_type = TREE_TYPE (@0);
3648 if (type_has_mode_precision_p (inner_type)
3649 && TYPE_PRECISION (inner_type) < prec)
3651 prec = TYPE_PRECISION (inner_type);
3652 /* See if we can shorten the right shift. */
3654 shift_type = inner_type;
3655 /* Otherwise X >> C1 is all zeros, so we'll optimize
3656 it into (X, 0) later on by making sure zerobits
3660 zerobits = HOST_WIDE_INT_M1U;
3663 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3664 zerobits <<= prec - shiftc;
3666 /* For arithmetic shift if sign bit could be set, zerobits
3667 can contain actually sign bits, so no transformation is
3668 possible, unless MASK masks them all away. In that
3669 case the shift needs to be converted into logical shift. */
3670 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3671 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3673 if ((mask & zerobits) == 0)
3674 shift_type = unsigned_type_for (TREE_TYPE (@3));
3680 /* ((X << 16) & 0xff00) is (X, 0). */
3681 (if ((mask & zerobits) == mask)
3682 { build_int_cst (type, 0); }
3683 (with { newmask = mask | zerobits; }
3684 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3687 /* Only do the transformation if NEWMASK is some integer
3689 for (prec = BITS_PER_UNIT;
3690 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3691 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3694 (if (prec < HOST_BITS_PER_WIDE_INT
3695 || newmask == HOST_WIDE_INT_M1U)
3697 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3698 (if (!tree_int_cst_equal (newmaskt, @2))
3699 (if (shift_type != TREE_TYPE (@3))
3700 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3701 (bit_and @4 { newmaskt; })))))))))))))
3703 /* ((1 << n) & M) != 0 -> n == log2 (M) */
3709 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3710 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3711 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3712 wi::exact_log2 (wi::to_wide (@1))); }))))
3714 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3715 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3716 (for shift (lshift rshift)
3717 (for bit_op (bit_and bit_xor bit_ior)
3719 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3720 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3721 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3723 (bit_op (shift (convert @0) @1) { mask; })))))))
3725 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3727 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3728 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3729 && (element_precision (TREE_TYPE (@0))
3730 <= element_precision (TREE_TYPE (@1))
3731 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3733 { tree shift_type = TREE_TYPE (@0); }
3734 (convert (rshift (convert:shift_type @1) @2)))))
3736 /* ~(~X >>r Y) -> X >>r Y
3737 ~(~X <<r Y) -> X <<r Y */
3738 (for rotate (lrotate rrotate)
3740 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3741 (if ((element_precision (TREE_TYPE (@0))
3742 <= element_precision (TREE_TYPE (@1))
3743 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3744 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3745 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3747 { tree rotate_type = TREE_TYPE (@0); }
3748 (convert (rotate (convert:rotate_type @1) @2))))))
3751 (for rotate (lrotate rrotate)
3752 invrot (rrotate lrotate)
3753 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3755 (cmp (rotate @1 @0) (rotate @2 @0))
3757 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3759 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3760 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3761 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3763 (cmp (rotate @0 @1) INTEGER_CST@2)
3764 (if (integer_zerop (@2) || integer_all_onesp (@2))
3767 /* Narrow a lshift by constant. */
3769 (convert (lshift:s@0 @1 INTEGER_CST@2))
3770 (if (INTEGRAL_TYPE_P (type)
3771 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3772 && !integer_zerop (@2)
3773 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
3774 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3775 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
3776 (lshift (convert @1) @2)
3777 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
3778 { build_zero_cst (type); }))))
3780 /* Simplifications of conversions. */
3782 /* Basic strip-useless-type-conversions / strip_nops. */
3783 (for cvt (convert view_convert float fix_trunc)
3786 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3787 || (GENERIC && type == TREE_TYPE (@0)))
3790 /* Contract view-conversions. */
3792 (view_convert (view_convert @0))
3795 /* For integral conversions with the same precision or pointer
3796 conversions use a NOP_EXPR instead. */
3799 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3800 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3801 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3804 /* Strip inner integral conversions that do not change precision or size, or
3805 zero-extend while keeping the same size (for bool-to-char). */
3807 (view_convert (convert@0 @1))
3808 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3809 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3810 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3811 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3812 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3813 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3816 /* Simplify a view-converted empty or single-element constructor. */
3818 (view_convert CONSTRUCTOR@0)
3820 { tree ctor = (TREE_CODE (@0) == SSA_NAME
3821 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
3823 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3824 { build_zero_cst (type); })
3825 (if (CONSTRUCTOR_NELTS (ctor) == 1
3826 && VECTOR_TYPE_P (TREE_TYPE (ctor))
3827 && operand_equal_p (TYPE_SIZE (type),
3828 TYPE_SIZE (TREE_TYPE
3829 (CONSTRUCTOR_ELT (ctor, 0)->value))))
3830 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
3832 /* Re-association barriers around constants and other re-association
3833 barriers can be removed. */
3835 (paren CONSTANT_CLASS_P@0)
3838 (paren (paren@1 @0))
3841 /* Handle cases of two conversions in a row. */
3842 (for ocvt (convert float fix_trunc)
3843 (for icvt (convert float)
3848 tree inside_type = TREE_TYPE (@0);
3849 tree inter_type = TREE_TYPE (@1);
3850 int inside_int = INTEGRAL_TYPE_P (inside_type);
3851 int inside_ptr = POINTER_TYPE_P (inside_type);
3852 int inside_float = FLOAT_TYPE_P (inside_type);
3853 int inside_vec = VECTOR_TYPE_P (inside_type);
3854 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3855 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3856 int inter_int = INTEGRAL_TYPE_P (inter_type);
3857 int inter_ptr = POINTER_TYPE_P (inter_type);
3858 int inter_float = FLOAT_TYPE_P (inter_type);
3859 int inter_vec = VECTOR_TYPE_P (inter_type);
3860 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3861 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3862 int final_int = INTEGRAL_TYPE_P (type);
3863 int final_ptr = POINTER_TYPE_P (type);
3864 int final_float = FLOAT_TYPE_P (type);
3865 int final_vec = VECTOR_TYPE_P (type);
3866 unsigned int final_prec = TYPE_PRECISION (type);
3867 int final_unsignedp = TYPE_UNSIGNED (type);
3870 /* In addition to the cases of two conversions in a row
3871 handled below, if we are converting something to its own
3872 type via an object of identical or wider precision, neither
3873 conversion is needed. */
3874 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3876 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3877 && (((inter_int || inter_ptr) && final_int)
3878 || (inter_float && final_float))
3879 && inter_prec >= final_prec)
3882 /* Likewise, if the intermediate and initial types are either both
3883 float or both integer, we don't need the middle conversion if the
3884 former is wider than the latter and doesn't change the signedness
3885 (for integers). Avoid this if the final type is a pointer since
3886 then we sometimes need the middle conversion. */
3887 (if (((inter_int && inside_int) || (inter_float && inside_float))
3888 && (final_int || final_float)
3889 && inter_prec >= inside_prec
3890 && (inter_float || inter_unsignedp == inside_unsignedp))
3893 /* If we have a sign-extension of a zero-extended value, we can
3894 replace that by a single zero-extension. Likewise if the
3895 final conversion does not change precision we can drop the
3896 intermediate conversion. */
3897 (if (inside_int && inter_int && final_int
3898 && ((inside_prec < inter_prec && inter_prec < final_prec
3899 && inside_unsignedp && !inter_unsignedp)
3900 || final_prec == inter_prec))
3903 /* Two conversions in a row are not needed unless:
3904 - some conversion is floating-point (overstrict for now), or
3905 - some conversion is a vector (overstrict for now), or
3906 - the intermediate type is narrower than both initial and
3908 - the intermediate type and innermost type differ in signedness,
3909 and the outermost type is wider than the intermediate, or
3910 - the initial type is a pointer type and the precisions of the
3911 intermediate and final types differ, or
3912 - the final type is a pointer type and the precisions of the
3913 initial and intermediate types differ. */
3914 (if (! inside_float && ! inter_float && ! final_float
3915 && ! inside_vec && ! inter_vec && ! final_vec
3916 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3917 && ! (inside_int && inter_int
3918 && inter_unsignedp != inside_unsignedp
3919 && inter_prec < final_prec)
3920 && ((inter_unsignedp && inter_prec > inside_prec)
3921 == (final_unsignedp && final_prec > inter_prec))
3922 && ! (inside_ptr && inter_prec != final_prec)
3923 && ! (final_ptr && inside_prec != inter_prec))
3926 /* A truncation to an unsigned type (a zero-extension) should be
3927 canonicalized as bitwise and of a mask. */
3928 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3929 && final_int && inter_int && inside_int
3930 && final_prec == inside_prec
3931 && final_prec > inter_prec
3933 (convert (bit_and @0 { wide_int_to_tree
3935 wi::mask (inter_prec, false,
3936 TYPE_PRECISION (inside_type))); })))
3938 /* If we are converting an integer to a floating-point that can
3939 represent it exactly and back to an integer, we can skip the
3940 floating-point conversion. */
3941 (if (GIMPLE /* PR66211 */
3942 && inside_int && inter_float && final_int &&
3943 (unsigned) significand_size (TYPE_MODE (inter_type))
3944 >= inside_prec - !inside_unsignedp)
3947 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
3948 float_type. Only do the transformation if we do not need to preserve
3949 trapping behaviour, so require !flag_trapping_math. */
3952 (float (fix_trunc @0))
3953 (if (!flag_trapping_math
3954 && types_match (type, TREE_TYPE (@0))
3955 && direct_internal_fn_supported_p (IFN_TRUNC, type,
3960 /* If we have a narrowing conversion to an integral type that is fed by a
3961 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3962 masks off bits outside the final type (and nothing else). */
3964 (convert (bit_and @0 INTEGER_CST@1))
3965 (if (INTEGRAL_TYPE_P (type)
3966 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3967 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3968 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3969 TYPE_PRECISION (type)), 0))
3973 /* (X /[ex] A) * A -> X. */
3975 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3978 /* Simplify (A / B) * B + (A % B) -> A. */
3979 (for div (trunc_div ceil_div floor_div round_div)
3980 mod (trunc_mod ceil_mod floor_mod round_mod)
3982 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3985 /* x / y * y == x -> x % y == 0. */
3987 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
3988 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
3989 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
3991 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3992 (for op (plus minus)
3994 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3995 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3996 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3999 wi::overflow_type overflow;
4000 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4001 TYPE_SIGN (type), &overflow);
4003 (if (types_match (type, TREE_TYPE (@2))
4004 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4005 (op @0 { wide_int_to_tree (type, mul); })
4006 (with { tree utype = unsigned_type_for (type); }
4007 (convert (op (convert:utype @0)
4008 (mult (convert:utype @1) (convert:utype @2))))))))))
4010 /* Canonicalization of binary operations. */
4012 /* Convert X + -C into X - C. */
4014 (plus @0 REAL_CST@1)
4015 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4016 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4017 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4018 (minus @0 { tem; })))))
4020 /* Convert x+x into x*2. */
4023 (if (SCALAR_FLOAT_TYPE_P (type))
4024 (mult @0 { build_real (type, dconst2); })
4025 (if (INTEGRAL_TYPE_P (type))
4026 (mult @0 { build_int_cst (type, 2); }))))
4030 (minus integer_zerop @1)
4033 (pointer_diff integer_zerop @1)
4034 (negate (convert @1)))
4036 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4037 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4038 (-ARG1 + ARG0) reduces to -ARG1. */
4040 (minus real_zerop@0 @1)
4041 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4044 /* Transform x * -1 into -x. */
4046 (mult @0 integer_minus_onep)
4049 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4050 signed overflow for CST != 0 && CST != -1. */
4052 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4053 (if (TREE_CODE (@2) != INTEGER_CST
4055 && !integer_zerop (@1) && !integer_minus_onep (@1))
4056 (mult (mult @0 @2) @1)))
4058 /* True if we can easily extract the real and imaginary parts of a complex
4060 (match compositional_complex
4061 (convert? (complex @0 @1)))
4063 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4065 (complex (realpart @0) (imagpart @0))
4068 (realpart (complex @0 @1))
4071 (imagpart (complex @0 @1))
4074 /* Sometimes we only care about half of a complex expression. */
4076 (realpart (convert?:s (conj:s @0)))
4077 (convert (realpart @0)))
4079 (imagpart (convert?:s (conj:s @0)))
4080 (convert (negate (imagpart @0))))
4081 (for part (realpart imagpart)
4082 (for op (plus minus)
4084 (part (convert?:s@2 (op:s @0 @1)))
4085 (convert (op (part @0) (part @1))))))
4087 (realpart (convert?:s (CEXPI:s @0)))
4090 (imagpart (convert?:s (CEXPI:s @0)))
4093 /* conj(conj(x)) -> x */
4095 (conj (convert? (conj @0)))
4096 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4099 /* conj({x,y}) -> {x,-y} */
4101 (conj (convert?:s (complex:s @0 @1)))
4102 (with { tree itype = TREE_TYPE (type); }
4103 (complex (convert:itype @0) (negate (convert:itype @1)))))
4105 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4106 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
4107 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
4112 (bswap (bit_not (bswap @0)))
4114 (for bitop (bit_xor bit_ior bit_and)
4116 (bswap (bitop:c (bswap @0) @1))
4117 (bitop @0 (bswap @1))))
4120 (cmp (bswap@2 @0) (bswap @1))
4121 (with { tree ctype = TREE_TYPE (@2); }
4122 (cmp (convert:ctype @0) (convert:ctype @1))))
4124 (cmp (bswap @0) INTEGER_CST@1)
4125 (with { tree ctype = TREE_TYPE (@1); }
4126 (cmp (convert:ctype @0) (bswap! @1)))))
4127 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4129 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4131 (if (BITS_PER_UNIT == 8
4132 && tree_fits_uhwi_p (@2)
4133 && tree_fits_uhwi_p (@3))
4136 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4137 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4138 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4139 unsigned HOST_WIDE_INT lo = bits & 7;
4140 unsigned HOST_WIDE_INT hi = bits - lo;
4143 && mask < (256u>>lo)
4144 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4145 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4147 (bit_and (convert @1) @3)
4150 tree utype = unsigned_type_for (TREE_TYPE (@1));
4151 tree nst = build_int_cst (integer_type_node, ns);
4153 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4154 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4156 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4157 (if (BITS_PER_UNIT == 8
4158 && CHAR_TYPE_SIZE == 8
4159 && tree_fits_uhwi_p (@1))
4162 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4163 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4164 /* If the bswap was extended before the original shift, this
4165 byte (shift) has the sign of the extension, not the sign of
4166 the original shift. */
4167 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4169 /* Special case: logical right shift of sign-extended bswap.
4170 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4171 (if (TYPE_PRECISION (type) > prec
4172 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4173 && TYPE_UNSIGNED (type)
4174 && bits < prec && bits + 8 >= prec)
4175 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4176 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4177 (if (bits + 8 == prec)
4178 (if (TYPE_UNSIGNED (st))
4179 (convert (convert:unsigned_char_type_node @0))
4180 (convert (convert:signed_char_type_node @0)))
4181 (if (bits < prec && bits + 8 > prec)
4184 tree nst = build_int_cst (integer_type_node, bits & 7);
4185 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4186 : signed_char_type_node;
4188 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4189 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4191 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4192 (if (BITS_PER_UNIT == 8
4193 && tree_fits_uhwi_p (@1)
4194 && tree_to_uhwi (@1) < 256)
4197 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4198 tree utype = unsigned_type_for (TREE_TYPE (@0));
4199 tree nst = build_int_cst (integer_type_node, prec - 8);
4201 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4204 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
4206 /* Simplify constant conditions.
4207 Only optimize constant conditions when the selected branch
4208 has the same type as the COND_EXPR. This avoids optimizing
4209 away "c ? x : throw", where the throw has a void type.
4210 Note that we cannot throw away the fold-const.cc variant nor
4211 this one as we depend on doing this transform before possibly
4212 A ? B : B -> B triggers and the fold-const.cc one can optimize
4213 0 ? A : B to B even if A has side-effects. Something
4214 genmatch cannot handle. */
4216 (cond INTEGER_CST@0 @1 @2)
4217 (if (integer_zerop (@0))
4218 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4220 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4223 (vec_cond VECTOR_CST@0 @1 @2)
4224 (if (integer_all_onesp (@0))
4226 (if (integer_zerop (@0))
4229 /* Sink unary operations to branches, but only if we do fold both. */
4230 (for op (negate bit_not abs absu)
4232 (op (vec_cond:s @0 @1 @2))
4233 (vec_cond @0 (op! @1) (op! @2))))
4235 /* Sink binary operation to branches, but only if we can fold it. */
4236 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4237 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4238 trunc_mod ceil_mod floor_mod round_mod min max)
4239 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4241 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4242 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4244 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4246 (op (vec_cond:s @0 @1 @2) @3)
4247 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4249 (op @3 (vec_cond:s @0 @1 @2))
4250 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4253 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4254 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4257 int ibit = tree_log2 (@0);
4258 int ibit2 = tree_log2 (@1);
4262 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4264 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4265 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4268 int ibit = tree_log2 (@0);
4269 int ibit2 = tree_log2 (@1);
4273 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4275 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4278 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4280 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4282 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4285 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4287 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4289 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4290 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4293 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4294 TYPE_PRECISION(type)));
4295 int ibit2 = tree_log2 (@1);
4299 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4301 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4303 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4306 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4307 TYPE_PRECISION(type)));
4308 int ibit2 = tree_log2 (@1);
4312 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4314 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4317 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4319 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4321 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4324 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4326 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4330 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4331 Currently disabled after pass lvec because ARM understands
4332 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
4334 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4335 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4336 (vec_cond (bit_and @0 @3) @1 @2)))
4338 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4339 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4340 (vec_cond (bit_ior @0 @3) @1 @2)))
4342 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4343 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4344 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4346 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4347 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4348 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4350 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
4352 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4353 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4354 (vec_cond (bit_and @0 @1) @2 @3)))
4356 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4357 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4358 (vec_cond (bit_ior @0 @1) @2 @3)))
4360 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4361 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4362 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4364 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4365 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4366 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4368 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4369 types are compatible. */
4371 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4372 (if (VECTOR_BOOLEAN_TYPE_P (type)
4373 && types_match (type, TREE_TYPE (@0)))
4374 (if (integer_zerop (@1) && integer_all_onesp (@2))
4376 (if (integer_all_onesp (@1) && integer_zerop (@2))
4379 /* A few simplifications of "a ? CST1 : CST2". */
4380 /* NOTE: Only do this on gimple as the if-chain-to-switch
4381 optimization depends on the gimple to have if statements in it. */
4384 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4386 (if (integer_zerop (@2))
4388 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4389 (if (integer_onep (@1))
4390 (convert (convert:boolean_type_node @0)))
4391 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4392 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4394 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4396 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4397 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
4398 here as the powerof2cst case above will handle that case correctly. */
4399 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4400 (negate (convert (convert:boolean_type_node @0))))))
4401 (if (integer_zerop (@1))
4403 tree booltrue = constant_boolean_node (true, boolean_type_node);
4406 /* a ? 0 : 1 -> !a. */
4407 (if (integer_onep (@2))
4408 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4409 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4410 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
4412 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4414 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4416 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
4417 here as the powerof2cst case above will handle that case correctly. */
4418 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4419 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4428 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
4429 (if (INTEGRAL_TYPE_P (type)
4430 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4431 (cond @1 (convert @2) (convert @3))))
4433 /* Simplification moved from fold_cond_expr_with_comparison. It may also
4435 /* This pattern implements two kinds simplification:
4438 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4439 1) Conversions are type widening from smaller type.
4440 2) Const c1 equals to c2 after canonicalizing comparison.
4441 3) Comparison has tree code LT, LE, GT or GE.
4442 This specific pattern is needed when (cmp (convert x) c) may not
4443 be simplified by comparison patterns because of multiple uses of
4444 x. It also makes sense here because simplifying across multiple
4445 referred var is always benefitial for complicated cases.
4448 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
4449 (for cmp (lt le gt ge eq)
4451 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4454 tree from_type = TREE_TYPE (@1);
4455 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4456 enum tree_code code = ERROR_MARK;
4458 if (INTEGRAL_TYPE_P (from_type)
4459 && int_fits_type_p (@2, from_type)
4460 && (types_match (c1_type, from_type)
4461 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4462 && (TYPE_UNSIGNED (from_type)
4463 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4464 && (types_match (c2_type, from_type)
4465 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4466 && (TYPE_UNSIGNED (from_type)
4467 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4471 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4473 /* X <= Y - 1 equals to X < Y. */
4476 /* X > Y - 1 equals to X >= Y. */
4480 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4482 /* X < Y + 1 equals to X <= Y. */
4485 /* X >= Y + 1 equals to X > Y. */
4489 if (code != ERROR_MARK
4490 || wi::to_widest (@2) == wi::to_widest (@3))
4492 if (cmp == LT_EXPR || cmp == LE_EXPR)
4494 if (cmp == GT_EXPR || cmp == GE_EXPR)
4498 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4499 else if (int_fits_type_p (@3, from_type))
4503 (if (code == MAX_EXPR)
4504 (convert (max @1 (convert @2)))
4505 (if (code == MIN_EXPR)
4506 (convert (min @1 (convert @2)))
4507 (if (code == EQ_EXPR)
4508 (convert (cond (eq @1 (convert @3))
4509 (convert:from_type @3) (convert:from_type @2)))))))))
4511 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4513 1) OP is PLUS or MINUS.
4514 2) CMP is LT, LE, GT or GE.
4515 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4517 This pattern also handles special cases like:
4519 A) Operand x is a unsigned to signed type conversion and c1 is
4520 integer zero. In this case,
4521 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4522 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4523 B) Const c1 may not equal to (C3 op' C2). In this case we also
4524 check equality for (c1+1) and (c1-1) by adjusting comparison
4527 TODO: Though signed type is handled by this pattern, it cannot be
4528 simplified at the moment because C standard requires additional
4529 type promotion. In order to match&simplify it here, the IR needs
4530 to be cleaned up by other optimizers, i.e, VRP. */
4531 (for op (plus minus)
4532 (for cmp (lt le gt ge)
4534 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4535 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4536 (if (types_match (from_type, to_type)
4537 /* Check if it is special case A). */
4538 || (TYPE_UNSIGNED (from_type)
4539 && !TYPE_UNSIGNED (to_type)
4540 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4541 && integer_zerop (@1)
4542 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4545 wi::overflow_type overflow = wi::OVF_NONE;
4546 enum tree_code code, cmp_code = cmp;
4548 wide_int c1 = wi::to_wide (@1);
4549 wide_int c2 = wi::to_wide (@2);
4550 wide_int c3 = wi::to_wide (@3);
4551 signop sgn = TYPE_SIGN (from_type);
4553 /* Handle special case A), given x of unsigned type:
4554 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4555 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4556 if (!types_match (from_type, to_type))
4558 if (cmp_code == LT_EXPR)
4560 if (cmp_code == GE_EXPR)
4562 c1 = wi::max_value (to_type);
4564 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4565 compute (c3 op' c2) and check if it equals to c1 with op' being
4566 the inverted operator of op. Make sure overflow doesn't happen
4567 if it is undefined. */
4568 if (op == PLUS_EXPR)
4569 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4571 real_c1 = wi::add (c3, c2, sgn, &overflow);
4574 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4576 /* Check if c1 equals to real_c1. Boundary condition is handled
4577 by adjusting comparison operation if necessary. */
4578 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4581 /* X <= Y - 1 equals to X < Y. */
4582 if (cmp_code == LE_EXPR)
4584 /* X > Y - 1 equals to X >= Y. */
4585 if (cmp_code == GT_EXPR)
4588 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4591 /* X < Y + 1 equals to X <= Y. */
4592 if (cmp_code == LT_EXPR)
4594 /* X >= Y + 1 equals to X > Y. */
4595 if (cmp_code == GE_EXPR)
4598 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4600 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4602 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4607 (if (code == MAX_EXPR)
4608 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4609 { wide_int_to_tree (from_type, c2); })
4610 (if (code == MIN_EXPR)
4611 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4612 { wide_int_to_tree (from_type, c2); })))))))))
4615 /* A >= B ? A : B -> max (A, B) and friends. The code is still
4616 in fold_cond_expr_with_comparison for GENERIC folding with
4617 some extra constraints. */
4618 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
4620 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
4621 (convert3? @0) (convert4? @1))
4622 (if (!HONOR_SIGNED_ZEROS (type)
4623 && (/* Allow widening conversions of the compare operands as data. */
4624 (INTEGRAL_TYPE_P (type)
4625 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
4626 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
4627 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
4628 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
4629 /* Or sign conversions for the comparison. */
4630 || (types_match (type, TREE_TYPE (@0))
4631 && types_match (type, TREE_TYPE (@1)))))
4633 (if (cmp == EQ_EXPR)
4634 (if (VECTOR_TYPE_P (type))
4637 (if (cmp == NE_EXPR)
4638 (if (VECTOR_TYPE_P (type))
4641 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
4642 (if (!HONOR_NANS (type))
4643 (if (VECTOR_TYPE_P (type))
4644 (view_convert (min @c0 @c1))
4645 (convert (min @c0 @c1)))))
4646 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
4647 (if (!HONOR_NANS (type))
4648 (if (VECTOR_TYPE_P (type))
4649 (view_convert (max @c0 @c1))
4650 (convert (max @c0 @c1)))))
4651 (if (cmp == UNEQ_EXPR)
4652 (if (!HONOR_NANS (type))
4653 (if (VECTOR_TYPE_P (type))
4656 (if (cmp == LTGT_EXPR)
4657 (if (!HONOR_NANS (type))
4658 (if (VECTOR_TYPE_P (type))
4660 (convert @c0))))))))
4663 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
4665 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
4666 (if (!TYPE_SATURATING (type)
4667 && (TYPE_OVERFLOW_WRAPS (type)
4668 || !wi::only_sign_bit_p (wi::to_wide (@1)))
4669 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
4672 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
4674 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
4675 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
4678 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
4679 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
4681 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
4682 (if (TYPE_UNSIGNED (type))
4683 (cond (ge @0 @1) (negate @0) @2)))
4685 (for cnd (cond vec_cond)
4686 /* A ? B : (A ? X : C) -> A ? B : C. */
4688 (cnd @0 (cnd @0 @1 @2) @3)
4691 (cnd @0 @1 (cnd @0 @2 @3))
4693 /* A ? B : (!A ? C : X) -> A ? B : C. */
4694 /* ??? This matches embedded conditions open-coded because genmatch
4695 would generate matching code for conditions in separate stmts only.
4696 The following is still important to merge then and else arm cases
4697 from if-conversion. */
4699 (cnd @0 @1 (cnd @2 @3 @4))
4700 (if (inverse_conditions_p (@0, @2))
4703 (cnd @0 (cnd @1 @2 @3) @4)
4704 (if (inverse_conditions_p (@0, @1))
4707 /* A ? B : B -> B. */
4712 /* !A ? B : C -> A ? C : B. */
4714 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4717 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4718 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4719 Need to handle UN* comparisons.
4721 None of these transformations work for modes with signed
4722 zeros. If A is +/-0, the first two transformations will
4723 change the sign of the result (from +0 to -0, or vice
4724 versa). The last four will fix the sign of the result,
4725 even though the original expressions could be positive or
4726 negative, depending on the sign of A.
4728 Note that all these transformations are correct if A is
4729 NaN, since the two alternatives (A and -A) are also NaNs. */
4731 (for cnd (cond vec_cond)
4732 /* A == 0 ? A : -A same as -A */
4735 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4736 (if (!HONOR_SIGNED_ZEROS (type))
4739 (cnd (cmp @0 zerop) integer_zerop (negate@1 @0))
4740 (if (!HONOR_SIGNED_ZEROS (type))
4743 /* A != 0 ? A : -A same as A */
4746 (cnd (cmp @0 zerop) @0 (negate @0))
4747 (if (!HONOR_SIGNED_ZEROS (type))
4750 (cnd (cmp @0 zerop) @0 integer_zerop)
4751 (if (!HONOR_SIGNED_ZEROS (type))
4754 /* A >=/> 0 ? A : -A same as abs (A) */
4757 (cnd (cmp @0 zerop) @0 (negate @0))
4758 (if (!HONOR_SIGNED_ZEROS (type)
4759 && !TYPE_UNSIGNED (type))
4761 /* A <=/< 0 ? A : -A same as -abs (A) */
4764 (cnd (cmp @0 zerop) @0 (negate @0))
4765 (if (!HONOR_SIGNED_ZEROS (type)
4766 && !TYPE_UNSIGNED (type))
4767 (if (ANY_INTEGRAL_TYPE_P (type)
4768 && !TYPE_OVERFLOW_WRAPS (type))
4770 tree utype = unsigned_type_for (type);
4772 (convert (negate (absu:utype @0))))
4773 (negate (abs @0)))))
4777 /* -(type)!A -> (type)A - 1. */
4779 (negate (convert?:s (logical_inverted_value:s @0)))
4780 (if (INTEGRAL_TYPE_P (type)
4781 && TREE_CODE (type) != BOOLEAN_TYPE
4782 && TYPE_PRECISION (type) > 1
4783 && TREE_CODE (@0) == SSA_NAME
4784 && ssa_name_has_boolean_range (@0))
4785 (plus (convert:type @0) { build_all_ones_cst (type); })))
4787 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4788 return all -1 or all 0 results. */
4789 /* ??? We could instead convert all instances of the vec_cond to negate,
4790 but that isn't necessarily a win on its own. */
4792 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4793 (if (VECTOR_TYPE_P (type)
4794 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4795 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4796 && (TYPE_MODE (TREE_TYPE (type))
4797 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4798 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4800 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4802 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4803 (if (VECTOR_TYPE_P (type)
4804 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4805 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4806 && (TYPE_MODE (TREE_TYPE (type))
4807 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4808 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4811 /* Simplifications of comparisons. */
4813 /* See if we can reduce the magnitude of a constant involved in a
4814 comparison by changing the comparison code. This is a canonicalization
4815 formerly done by maybe_canonicalize_comparison_1. */
4819 (cmp @0 uniform_integer_cst_p@1)
4820 (with { tree cst = uniform_integer_cst_p (@1); }
4821 (if (tree_int_cst_sgn (cst) == -1)
4822 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4823 wide_int_to_tree (TREE_TYPE (cst),
4829 (cmp @0 uniform_integer_cst_p@1)
4830 (with { tree cst = uniform_integer_cst_p (@1); }
4831 (if (tree_int_cst_sgn (cst) == 1)
4832 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4833 wide_int_to_tree (TREE_TYPE (cst),
4834 wi::to_wide (cst) - 1)); })))))
4836 /* We can simplify a logical negation of a comparison to the
4837 inverted comparison. As we cannot compute an expression
4838 operator using invert_tree_comparison we have to simulate
4839 that with expression code iteration. */
4840 (for cmp (tcc_comparison)
4841 icmp (inverted_tcc_comparison)
4842 ncmp (inverted_tcc_comparison_with_nans)
4843 /* Ideally we'd like to combine the following two patterns
4844 and handle some more cases by using
4845 (logical_inverted_value (cmp @0 @1))
4846 here but for that genmatch would need to "inline" that.
4847 For now implement what forward_propagate_comparison did. */
4849 (bit_not (cmp @0 @1))
4850 (if (VECTOR_TYPE_P (type)
4851 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4852 /* Comparison inversion may be impossible for trapping math,
4853 invert_tree_comparison will tell us. But we can't use
4854 a computed operator in the replacement tree thus we have
4855 to play the trick below. */
4856 (with { enum tree_code ic = invert_tree_comparison
4857 (cmp, HONOR_NANS (@0)); }
4863 (bit_xor (cmp @0 @1) integer_truep)
4864 (with { enum tree_code ic = invert_tree_comparison
4865 (cmp, HONOR_NANS (@0)); }
4870 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
4872 (ne (cmp@2 @0 @1) integer_zerop)
4873 (if (types_match (type, TREE_TYPE (@2)))
4876 (eq (cmp@2 @0 @1) integer_truep)
4877 (if (types_match (type, TREE_TYPE (@2)))
4880 (ne (cmp@2 @0 @1) integer_truep)
4881 (if (types_match (type, TREE_TYPE (@2)))
4882 (with { enum tree_code ic = invert_tree_comparison
4883 (cmp, HONOR_NANS (@0)); }
4889 (eq (cmp@2 @0 @1) integer_zerop)
4890 (if (types_match (type, TREE_TYPE (@2)))
4891 (with { enum tree_code ic = invert_tree_comparison
4892 (cmp, HONOR_NANS (@0)); }
4898 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4899 ??? The transformation is valid for the other operators if overflow
4900 is undefined for the type, but performing it here badly interacts
4901 with the transformation in fold_cond_expr_with_comparison which
4902 attempts to synthetize ABS_EXPR. */
4904 (for sub (minus pointer_diff)
4906 (cmp (sub@2 @0 @1) integer_zerop)
4907 (if (single_use (@2))
4910 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4911 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4914 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4915 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4916 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4917 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4918 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4919 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4920 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4922 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4923 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4924 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4925 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4926 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4928 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4929 signed arithmetic case. That form is created by the compiler
4930 often enough for folding it to be of value. One example is in
4931 computing loop trip counts after Operator Strength Reduction. */
4932 (for cmp (simple_comparison)
4933 scmp (swapped_simple_comparison)
4935 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4936 /* Handle unfolded multiplication by zero. */
4937 (if (integer_zerop (@1))
4939 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4940 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4942 /* If @1 is negative we swap the sense of the comparison. */
4943 (if (tree_int_cst_sgn (@1) < 0)
4947 /* For integral types with undefined overflow fold
4948 x * C1 == C2 into x == C2 / C1 or false.
4949 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4953 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4954 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4955 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4956 && wi::to_wide (@1) != 0)
4957 (with { widest_int quot; }
4958 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4959 TYPE_SIGN (TREE_TYPE (@0)), "))
4960 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4961 { constant_boolean_node (cmp == NE_EXPR, type); }))
4962 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4963 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4964 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4967 tree itype = TREE_TYPE (@0);
4968 int p = TYPE_PRECISION (itype);
4969 wide_int m = wi::one (p + 1) << p;
4970 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4971 wide_int i = wide_int::from (wi::mod_inv (a, m),
4972 p, TYPE_SIGN (itype));
4973 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4976 /* Simplify comparison of something with itself. For IEEE
4977 floating-point, we can only do some of these simplifications. */
4981 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4982 || ! tree_expr_maybe_nan_p (@0))
4983 { constant_boolean_node (true, type); }
4985 /* With -ftrapping-math conversion to EQ loses an exception. */
4986 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
4987 || ! flag_trapping_math))
4993 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4994 || ! tree_expr_maybe_nan_p (@0))
4995 { constant_boolean_node (false, type); })))
4996 (for cmp (unle unge uneq)
4999 { constant_boolean_node (true, type); }))
5000 (for cmp (unlt ungt)
5006 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5007 { constant_boolean_node (false, type); }))
5009 /* x == ~x -> false */
5010 /* x != ~x -> true */
5013 (cmp:c @0 (bit_not @0))
5014 { constant_boolean_node (cmp == NE_EXPR, type); }))
5016 /* Fold ~X op ~Y as Y op X. */
5017 (for cmp (simple_comparison)
5019 (cmp (bit_not@2 @0) (bit_not@3 @1))
5020 (if (single_use (@2) && single_use (@3))
5023 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
5024 (for cmp (simple_comparison)
5025 scmp (swapped_simple_comparison)
5027 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5028 (if (single_use (@2)
5029 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5030 (scmp @0 (bit_not @1)))))
5032 (for cmp (simple_comparison)
5033 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
5035 (cmp (convert@2 @0) (convert? @1))
5036 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5037 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
5038 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5039 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
5040 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
5043 tree type1 = TREE_TYPE (@1);
5044 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
5046 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
5047 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
5048 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
5049 type1 = float_type_node;
5050 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
5051 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
5052 type1 = double_type_node;
5055 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
5056 ? TREE_TYPE (@0) : type1);
5058 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
5059 (cmp (convert:newtype @0) (convert:newtype @1))))))
5063 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
5065 /* a CMP (-0) -> a CMP 0 */
5066 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5067 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5068 /* (-0) CMP b -> 0 CMP b. */
5069 (if (TREE_CODE (@0) == REAL_CST
5070 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5071 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5072 /* x != NaN is always true, other ops are always false. */
5073 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5074 && !tree_expr_signaling_nan_p (@1)
5075 && !tree_expr_maybe_signaling_nan_p (@0))
5076 { constant_boolean_node (cmp == NE_EXPR, type); })
5077 /* NaN != y is always true, other ops are always false. */
5078 (if (TREE_CODE (@0) == REAL_CST
5079 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5080 && !tree_expr_signaling_nan_p (@0)
5081 && !tree_expr_signaling_nan_p (@1))
5082 { constant_boolean_node (cmp == NE_EXPR, type); })
5083 /* Fold comparisons against infinity. */
5084 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5085 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5088 REAL_VALUE_TYPE max;
5089 enum tree_code code = cmp;
5090 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5092 code = swap_tree_comparison (code);
5095 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
5096 (if (code == GT_EXPR
5097 && !(HONOR_NANS (@0) && flag_trapping_math))
5098 { constant_boolean_node (false, type); })
5099 (if (code == LE_EXPR)
5100 /* x <= +Inf is always true, if we don't care about NaNs. */
5101 (if (! HONOR_NANS (@0))
5102 { constant_boolean_node (true, type); }
5103 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5104 an "invalid" exception. */
5105 (if (!flag_trapping_math)
5107 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5108 for == this introduces an exception for x a NaN. */
5109 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5111 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5113 (lt @0 { build_real (TREE_TYPE (@0), max); })
5114 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5115 /* x < +Inf is always equal to x <= DBL_MAX. */
5116 (if (code == LT_EXPR)
5117 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5119 (ge @0 { build_real (TREE_TYPE (@0), max); })
5120 (le @0 { build_real (TREE_TYPE (@0), max); }))))
5121 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5122 an exception for x a NaN so use an unordered comparison. */
5123 (if (code == NE_EXPR)
5124 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5125 (if (! HONOR_NANS (@0))
5127 (ge @0 { build_real (TREE_TYPE (@0), max); })
5128 (le @0 { build_real (TREE_TYPE (@0), max); }))
5130 (unge @0 { build_real (TREE_TYPE (@0), max); })
5131 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5133 /* If this is a comparison of a real constant with a PLUS_EXPR
5134 or a MINUS_EXPR of a real constant, we can convert it into a
5135 comparison with a revised real constant as long as no overflow
5136 occurs when unsafe_math_optimizations are enabled. */
5137 (if (flag_unsafe_math_optimizations)
5138 (for op (plus minus)
5140 (cmp (op @0 REAL_CST@1) REAL_CST@2)
5143 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5144 TREE_TYPE (@1), @2, @1);
5146 (if (tem && !TREE_OVERFLOW (tem))
5147 (cmp @0 { tem; }))))))
5149 /* Likewise, we can simplify a comparison of a real constant with
5150 a MINUS_EXPR whose first operand is also a real constant, i.e.
5151 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
5152 floating-point types only if -fassociative-math is set. */
5153 (if (flag_associative_math)
5155 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5156 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5157 (if (tem && !TREE_OVERFLOW (tem))
5158 (cmp { tem; } @1)))))
5160 /* Fold comparisons against built-in math functions. */
5161 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5164 (cmp (sq @0) REAL_CST@1)
5166 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5168 /* sqrt(x) < y is always false, if y is negative. */
5169 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5170 { constant_boolean_node (false, type); })
5171 /* sqrt(x) > y is always true, if y is negative and we
5172 don't care about NaNs, i.e. negative values of x. */
5173 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5174 { constant_boolean_node (true, type); })
5175 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5176 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5177 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5179 /* sqrt(x) < 0 is always false. */
5180 (if (cmp == LT_EXPR)
5181 { constant_boolean_node (false, type); })
5182 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
5183 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5184 { constant_boolean_node (true, type); })
5185 /* sqrt(x) <= 0 -> x == 0. */
5186 (if (cmp == LE_EXPR)
5188 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
5189 == or !=. In the last case:
5191 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5193 if x is negative or NaN. Due to -funsafe-math-optimizations,
5194 the results for other x follow from natural arithmetic. */
5196 (if ((cmp == LT_EXPR
5200 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5201 /* Give up for -frounding-math. */
5202 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5206 enum tree_code ncmp = cmp;
5207 const real_format *fmt
5208 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5209 real_arithmetic (&c2, MULT_EXPR,
5210 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5211 real_convert (&c2, fmt, &c2);
5212 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5213 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
5214 if (!REAL_VALUE_ISINF (c2))
5216 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5217 build_real (TREE_TYPE (@0), c2));
5218 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5220 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5221 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5222 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5223 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5224 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5225 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5228 /* With rounding to even, sqrt of up to 3 different values
5229 gives the same normal result, so in some cases c2 needs
5231 REAL_VALUE_TYPE c2alt, tow;
5232 if (cmp == LT_EXPR || cmp == GE_EXPR)
5236 real_nextafter (&c2alt, fmt, &c2, &tow);
5237 real_convert (&c2alt, fmt, &c2alt);
5238 if (REAL_VALUE_ISINF (c2alt))
5242 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5243 build_real (TREE_TYPE (@0), c2alt));
5244 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5246 else if (real_equal (&TREE_REAL_CST (c3),
5247 &TREE_REAL_CST (@1)))
5253 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5254 (if (REAL_VALUE_ISINF (c2))
5255 /* sqrt(x) > y is x == +Inf, when y is very large. */
5256 (if (HONOR_INFINITIES (@0))
5257 (eq @0 { build_real (TREE_TYPE (@0), c2); })
5258 { constant_boolean_node (false, type); })
5259 /* sqrt(x) > c is the same as x > c*c. */
5260 (if (ncmp != ERROR_MARK)
5261 (if (ncmp == GE_EXPR)
5262 (ge @0 { build_real (TREE_TYPE (@0), c2); })
5263 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5264 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
5265 (if (REAL_VALUE_ISINF (c2))
5267 /* sqrt(x) < y is always true, when y is a very large
5268 value and we don't care about NaNs or Infinities. */
5269 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5270 { constant_boolean_node (true, type); })
5271 /* sqrt(x) < y is x != +Inf when y is very large and we
5272 don't care about NaNs. */
5273 (if (! HONOR_NANS (@0))
5274 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5275 /* sqrt(x) < y is x >= 0 when y is very large and we
5276 don't care about Infinities. */
5277 (if (! HONOR_INFINITIES (@0))
5278 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5279 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5282 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5283 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5284 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5285 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5286 (if (ncmp == LT_EXPR)
5287 (lt @0 { build_real (TREE_TYPE (@0), c2); })
5288 (le @0 { build_real (TREE_TYPE (@0), c2); }))
5289 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5290 (if (ncmp != ERROR_MARK && GENERIC)
5291 (if (ncmp == LT_EXPR)
5293 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5294 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5296 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5297 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5298 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
5300 (cmp (sq @0) (sq @1))
5301 (if (! HONOR_NANS (@0))
5304 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
5305 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5306 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
5308 (cmp (float@0 @1) (float @2))
5309 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5310 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5313 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5314 tree type1 = TREE_TYPE (@1);
5315 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5316 tree type2 = TREE_TYPE (@2);
5317 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5319 (if (fmt.can_represent_integral_type_p (type1)
5320 && fmt.can_represent_integral_type_p (type2))
5321 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5322 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5323 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5324 && type1_signed_p >= type2_signed_p)
5325 (icmp @1 (convert @2))
5326 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5327 && type1_signed_p <= type2_signed_p)
5328 (icmp (convert:type2 @1) @2)
5329 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5330 && type1_signed_p == type2_signed_p)
5331 (icmp @1 @2))))))))))
5333 /* Optimize various special cases of (FTYPE) N CMP CST. */
5334 (for cmp (lt le eq ne ge gt)
5335 icmp (le le eq ne ge ge)
5337 (cmp (float @0) REAL_CST@1)
5338 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5339 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5342 tree itype = TREE_TYPE (@0);
5343 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5344 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5345 /* Be careful to preserve any potential exceptions due to
5346 NaNs. qNaNs are ok in == or != context.
5347 TODO: relax under -fno-trapping-math or
5348 -fno-signaling-nans. */
5350 = real_isnan (cst) && (cst->signalling
5351 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5353 /* TODO: allow non-fitting itype and SNaNs when
5354 -fno-trapping-math. */
5355 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5358 signop isign = TYPE_SIGN (itype);
5359 REAL_VALUE_TYPE imin, imax;
5360 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5361 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5363 REAL_VALUE_TYPE icst;
5364 if (cmp == GT_EXPR || cmp == GE_EXPR)
5365 real_ceil (&icst, fmt, cst);
5366 else if (cmp == LT_EXPR || cmp == LE_EXPR)
5367 real_floor (&icst, fmt, cst);
5369 real_trunc (&icst, fmt, cst);
5371 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5373 bool overflow_p = false;
5375 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5378 /* Optimize cases when CST is outside of ITYPE's range. */
5379 (if (real_compare (LT_EXPR, cst, &imin))
5380 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5382 (if (real_compare (GT_EXPR, cst, &imax))
5383 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5385 /* Remove cast if CST is an integer representable by ITYPE. */
5387 (cmp @0 { gcc_assert (!overflow_p);
5388 wide_int_to_tree (itype, icst_val); })
5390 /* When CST is fractional, optimize
5391 (FTYPE) N == CST -> 0
5392 (FTYPE) N != CST -> 1. */
5393 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5394 { constant_boolean_node (cmp == NE_EXPR, type); })
5395 /* Otherwise replace with sensible integer constant. */
5398 gcc_checking_assert (!overflow_p);
5400 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5402 /* Fold A /[ex] B CMP C to A CMP B * C. */
5405 (cmp (exact_div @0 @1) INTEGER_CST@2)
5406 (if (!integer_zerop (@1))
5407 (if (wi::to_wide (@2) == 0)
5409 (if (TREE_CODE (@1) == INTEGER_CST)
5412 wi::overflow_type ovf;
5413 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5414 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5417 { constant_boolean_node (cmp == NE_EXPR, type); }
5418 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5419 (for cmp (lt le gt ge)
5421 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5422 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5425 wi::overflow_type ovf;
5426 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5427 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5430 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5431 TYPE_SIGN (TREE_TYPE (@2)))
5432 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5433 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5435 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5437 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5438 For large C (more than min/B+2^size), this is also true, with the
5439 multiplication computed modulo 2^size.
5440 For intermediate C, this just tests the sign of A. */
5441 (for cmp (lt le gt ge)
5444 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5445 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5446 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5447 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5450 tree utype = TREE_TYPE (@2);
5451 wide_int denom = wi::to_wide (@1);
5452 wide_int right = wi::to_wide (@2);
5453 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5454 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5455 bool small = wi::leu_p (right, smax);
5456 bool large = wi::geu_p (right, smin);
5458 (if (small || large)
5459 (cmp (convert:utype @0) (mult @2 (convert @1)))
5460 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5462 /* Unordered tests if either argument is a NaN. */
5464 (bit_ior (unordered @0 @0) (unordered @1 @1))
5465 (if (types_match (@0, @1))
5468 (bit_and (ordered @0 @0) (ordered @1 @1))
5469 (if (types_match (@0, @1))
5472 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5475 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5478 /* Simple range test simplifications. */
5479 /* A < B || A >= B -> true. */
5480 (for test1 (lt le le le ne ge)
5481 test2 (ge gt ge ne eq ne)
5483 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5484 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5485 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5486 { constant_boolean_node (true, type); })))
5487 /* A < B && A >= B -> false. */
5488 (for test1 (lt lt lt le ne eq)
5489 test2 (ge gt eq gt eq gt)
5491 (bit_and:c (test1 @0 @1) (test2 @0 @1))
5492 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5493 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5494 { constant_boolean_node (false, type); })))
5496 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5497 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
5499 Note that comparisons
5500 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
5501 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
5502 will be canonicalized to above so there's no need to
5509 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5510 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5513 tree ty = TREE_TYPE (@0);
5514 unsigned prec = TYPE_PRECISION (ty);
5515 wide_int mask = wi::to_wide (@2, prec);
5516 wide_int rhs = wi::to_wide (@3, prec);
5517 signop sgn = TYPE_SIGN (ty);
5519 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5520 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5521 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5522 { build_zero_cst (ty); }))))))
5524 /* -A CMP -B -> B CMP A. */
5525 (for cmp (tcc_comparison)
5526 scmp (swapped_tcc_comparison)
5528 (cmp (negate @0) (negate @1))
5529 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5530 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5531 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5534 (cmp (negate @0) CONSTANT_CLASS_P@1)
5535 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5536 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5537 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5538 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5539 (if (tem && !TREE_OVERFLOW (tem))
5540 (scmp @0 { tem; }))))))
5542 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
5545 (op (abs @0) zerop@1)
5548 /* From fold_sign_changed_comparison and fold_widened_comparison.
5549 FIXME: the lack of symmetry is disturbing. */
5550 (for cmp (simple_comparison)
5552 (cmp (convert@0 @00) (convert?@1 @10))
5553 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5554 /* Disable this optimization if we're casting a function pointer
5555 type on targets that require function pointer canonicalization. */
5556 && !(targetm.have_canonicalize_funcptr_for_compare ()
5557 && ((POINTER_TYPE_P (TREE_TYPE (@00))
5558 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5559 || (POINTER_TYPE_P (TREE_TYPE (@10))
5560 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5562 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5563 && (TREE_CODE (@10) == INTEGER_CST
5565 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5568 && !POINTER_TYPE_P (TREE_TYPE (@00))
5569 /* (int)bool:32 != (int)uint is not the same as
5570 bool:32 != (bool:32)uint since boolean types only have two valid
5571 values independent of their precision. */
5572 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
5573 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
5574 /* ??? The special-casing of INTEGER_CST conversion was in the original
5575 code and here to avoid a spurious overflow flag on the resulting
5576 constant which fold_convert produces. */
5577 (if (TREE_CODE (@1) == INTEGER_CST)
5578 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5579 TREE_OVERFLOW (@1)); })
5580 (cmp @00 (convert @1)))
5582 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5583 /* If possible, express the comparison in the shorter mode. */
5584 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5585 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5586 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5587 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5588 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5589 || ((TYPE_PRECISION (TREE_TYPE (@00))
5590 >= TYPE_PRECISION (TREE_TYPE (@10)))
5591 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5592 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5593 || (TREE_CODE (@10) == INTEGER_CST
5594 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5595 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5596 (cmp @00 (convert @10))
5597 (if (TREE_CODE (@10) == INTEGER_CST
5598 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5599 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5602 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5603 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5604 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5605 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5607 (if (above || below)
5608 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5609 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5610 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5611 { constant_boolean_node (above ? true : false, type); }
5612 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5613 { constant_boolean_node (above ? false : true, type); }))))))))))))
5617 /* SSA names are canonicalized to 2nd place. */
5618 (cmp addr@0 SSA_NAME@1)
5620 { poly_int64 off; tree base; }
5621 /* A local variable can never be pointed to by
5622 the default SSA name of an incoming parameter. */
5623 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5624 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5625 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5626 && TREE_CODE (base) == VAR_DECL
5627 && auto_var_in_fn_p (base, current_function_decl))
5628 (if (cmp == NE_EXPR)
5629 { constant_boolean_node (true, type); }
5630 { constant_boolean_node (false, type); })
5631 /* If the address is based on @1 decide using the offset. */
5632 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5633 && TREE_CODE (base) == MEM_REF
5634 && TREE_OPERAND (base, 0) == @1)
5635 (with { off += mem_ref_offset (base).force_shwi (); }
5636 (if (known_ne (off, 0))
5637 { constant_boolean_node (cmp == NE_EXPR, type); }
5638 (if (known_eq (off, 0))
5639 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5641 /* Equality compare simplifications from fold_binary */
5644 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5645 Similarly for NE_EXPR. */
5647 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5648 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5649 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5650 { constant_boolean_node (cmp == NE_EXPR, type); }))
5652 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5654 (cmp (bit_xor @0 @1) integer_zerop)
5657 /* (X ^ Y) == Y becomes X == 0.
5658 Likewise (X ^ Y) == X becomes Y == 0. */
5660 (cmp:c (bit_xor:c @0 @1) @0)
5661 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5663 /* (X & Y) == X becomes (X & ~Y) == 0. */
5665 (cmp:c (bit_and:c @0 @1) @0)
5666 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5668 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5669 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5670 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5671 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5672 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5673 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5674 && !wi::neg_p (wi::to_wide (@1)))
5675 (cmp (bit_and @0 (convert (bit_not @1)))
5676 { build_zero_cst (TREE_TYPE (@0)); })))
5678 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5680 (cmp:c (bit_ior:c @0 @1) @1)
5681 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5683 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5685 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5686 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5687 (cmp @0 (bit_xor @1 (convert @2)))))
5690 (cmp (convert? addr@0) integer_zerop)
5691 (if (tree_single_nonzero_warnv_p (@0, NULL))
5692 { constant_boolean_node (cmp == NE_EXPR, type); }))
5694 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5696 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5697 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5699 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5700 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5701 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5702 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5707 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5708 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5709 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5710 && types_match (@0, @1))
5711 (ncmp (bit_xor @0 @1) @2)))))
5712 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5713 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5717 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5718 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5719 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5720 && types_match (@0, @1))
5721 (ncmp (bit_xor @0 @1) @2))))
5723 /* If we have (A & C) == C where C is a power of 2, convert this into
5724 (A & C) != 0. Similarly for NE_EXPR. */
5728 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5729 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5731 /* From fold_binary_op_with_conditional_arg handle the case of
5732 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
5733 compares simplify. */
5734 (for cmp (simple_comparison)
5736 (cmp:c (cond @0 @1 @2) @3)
5737 /* Do not move possibly trapping operations into the conditional as this
5738 pessimizes code and causes gimplification issues when applied late. */
5739 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
5740 || operation_could_trap_p (cmp, true, false, @3))
5741 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
5744 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5745 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5747 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5748 (if (INTEGRAL_TYPE_P (type)
5749 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5750 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5751 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5754 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5756 (if (cmp == LT_EXPR)
5757 (bit_xor (convert (rshift @0 {shifter;})) @1)
5758 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5759 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5760 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5762 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5763 (if (INTEGRAL_TYPE_P (type)
5764 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5765 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5766 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5769 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5771 (if (cmp == GE_EXPR)
5772 (bit_xor (convert (rshift @0 {shifter;})) @1)
5773 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5775 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5776 convert this into a shift followed by ANDing with D. */
5779 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5780 INTEGER_CST@2 integer_zerop)
5781 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5783 int shift = (wi::exact_log2 (wi::to_wide (@2))
5784 - wi::exact_log2 (wi::to_wide (@1)));
5788 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5790 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5793 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5794 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5798 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5799 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5800 && type_has_mode_precision_p (TREE_TYPE (@0))
5801 && element_precision (@2) >= element_precision (@0)
5802 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5803 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5804 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5806 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5807 this into a right shift or sign extension followed by ANDing with C. */
5810 (lt @0 integer_zerop)
5811 INTEGER_CST@1 integer_zerop)
5812 (if (integer_pow2p (@1)
5813 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5815 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5819 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5821 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5822 sign extension followed by AND with C will achieve the effect. */
5823 (bit_and (convert @0) @1)))))
5825 /* When the addresses are not directly of decls compare base and offset.
5826 This implements some remaining parts of fold_comparison address
5827 comparisons but still no complete part of it. Still it is good
5828 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5829 (for cmp (simple_comparison)
5831 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5834 poly_int64 off0, off1;
5836 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
5837 off0, off1, GENERIC);
5841 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5842 { constant_boolean_node (known_eq (off0, off1), type); })
5843 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5844 { constant_boolean_node (known_ne (off0, off1), type); })
5845 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5846 { constant_boolean_node (known_lt (off0, off1), type); })
5847 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5848 { constant_boolean_node (known_le (off0, off1), type); })
5849 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5850 { constant_boolean_node (known_ge (off0, off1), type); })
5851 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5852 { constant_boolean_node (known_gt (off0, off1), type); }))
5855 (if (cmp == EQ_EXPR)
5856 { constant_boolean_node (false, type); })
5857 (if (cmp == NE_EXPR)
5858 { constant_boolean_node (true, type); })))))))
5860 /* Simplify pointer equality compares using PTA. */
5864 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5865 && ptrs_compare_unequal (@0, @1))
5866 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5868 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5869 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5870 Disable the transform if either operand is pointer to function.
5871 This broke pr22051-2.c for arm where function pointer
5872 canonicalizaion is not wanted. */
5876 (cmp (convert @0) INTEGER_CST@1)
5877 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5878 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5879 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5880 /* Don't perform this optimization in GENERIC if @0 has reference
5881 type when sanitizing. See PR101210. */
5883 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5884 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5885 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5886 && POINTER_TYPE_P (TREE_TYPE (@1))
5887 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5888 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5889 (cmp @0 (convert @1)))))
5891 /* Non-equality compare simplifications from fold_binary */
5892 (for cmp (lt gt le ge)
5893 /* Comparisons with the highest or lowest possible integer of
5894 the specified precision will have known values. */
5896 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5897 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5898 || POINTER_TYPE_P (TREE_TYPE (@1))
5899 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5900 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5903 tree cst = uniform_integer_cst_p (@1);
5904 tree arg1_type = TREE_TYPE (cst);
5905 unsigned int prec = TYPE_PRECISION (arg1_type);
5906 wide_int max = wi::max_value (arg1_type);
5907 wide_int signed_max = wi::max_value (prec, SIGNED);
5908 wide_int min = wi::min_value (arg1_type);
5911 (if (wi::to_wide (cst) == max)
5913 (if (cmp == GT_EXPR)
5914 { constant_boolean_node (false, type); })
5915 (if (cmp == GE_EXPR)
5917 (if (cmp == LE_EXPR)
5918 { constant_boolean_node (true, type); })
5919 (if (cmp == LT_EXPR)
5921 (if (wi::to_wide (cst) == min)
5923 (if (cmp == LT_EXPR)
5924 { constant_boolean_node (false, type); })
5925 (if (cmp == LE_EXPR)
5927 (if (cmp == GE_EXPR)
5928 { constant_boolean_node (true, type); })
5929 (if (cmp == GT_EXPR)
5931 (if (wi::to_wide (cst) == max - 1)
5933 (if (cmp == GT_EXPR)
5934 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5935 wide_int_to_tree (TREE_TYPE (cst),
5938 (if (cmp == LE_EXPR)
5939 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5940 wide_int_to_tree (TREE_TYPE (cst),
5943 (if (wi::to_wide (cst) == min + 1)
5945 (if (cmp == GE_EXPR)
5946 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5947 wide_int_to_tree (TREE_TYPE (cst),
5950 (if (cmp == LT_EXPR)
5951 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5952 wide_int_to_tree (TREE_TYPE (cst),
5955 (if (wi::to_wide (cst) == signed_max
5956 && TYPE_UNSIGNED (arg1_type)
5957 /* We will flip the signedness of the comparison operator
5958 associated with the mode of @1, so the sign bit is
5959 specified by this mode. Check that @1 is the signed
5960 max associated with this sign bit. */
5961 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5962 /* signed_type does not work on pointer types. */
5963 && INTEGRAL_TYPE_P (arg1_type))
5964 /* The following case also applies to X < signed_max+1
5965 and X >= signed_max+1 because previous transformations. */
5966 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5967 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5969 (if (cst == @1 && cmp == LE_EXPR)
5970 (ge (convert:st @0) { build_zero_cst (st); }))
5971 (if (cst == @1 && cmp == GT_EXPR)
5972 (lt (convert:st @0) { build_zero_cst (st); }))
5973 (if (cmp == LE_EXPR)
5974 (ge (view_convert:st @0) { build_zero_cst (st); }))
5975 (if (cmp == GT_EXPR)
5976 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5978 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5979 /* If the second operand is NaN, the result is constant. */
5982 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5983 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5984 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5985 ? false : true, type); })))
5987 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5991 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5992 { constant_boolean_node (true, type); })
5993 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5994 { constant_boolean_node (false, type); })))
5996 /* Fold ORDERED if either operand must be NaN, or neither can be. */
6000 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6001 { constant_boolean_node (false, type); })
6002 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6003 { constant_boolean_node (true, type); })))
6005 /* bool_var != 0 becomes bool_var. */
6007 (ne @0 integer_zerop)
6008 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6009 && types_match (type, TREE_TYPE (@0)))
6011 /* bool_var == 1 becomes bool_var. */
6013 (eq @0 integer_onep)
6014 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6015 && types_match (type, TREE_TYPE (@0)))
6018 bool_var == 0 becomes !bool_var or
6019 bool_var != 1 becomes !bool_var
6020 here because that only is good in assignment context as long
6021 as we require a tcc_comparison in GIMPLE_CONDs where we'd
6022 replace if (x == 0) with tem = ~x; if (tem != 0) which is
6023 clearly less optimal and which we'll transform again in forwprop. */
6025 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6026 where ~Y + 1 == pow2 and Z = ~Y. */
6027 (for cst (VECTOR_CST INTEGER_CST)
6031 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6032 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6033 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6034 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6035 ? optab_vector : optab_default;
6036 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6037 (if (target_supports_op_p (utype, icmp, optab)
6038 || (optimize_vectors_before_lowering_p ()
6039 && (!target_supports_op_p (type, cmp, optab)
6040 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6041 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6043 (icmp (view_convert:utype @0) { csts; })))))))))
6045 /* When one argument is a constant, overflow detection can be simplified.
6046 Currently restricted to single use so as not to interfere too much with
6047 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6048 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
6049 (for cmp (lt le ge gt)
6052 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6053 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6054 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6055 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6056 && wi::to_wide (@1) != 0
6059 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6060 signop sign = TYPE_SIGN (TREE_TYPE (@0));
6062 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6063 wi::max_value (prec, sign)
6064 - wi::to_wide (@1)); })))))
6066 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6067 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6068 expects the long form, so we restrict the transformation for now. */
6071 (cmp:c (minus@2 @0 @1) @0)
6072 (if (single_use (@2)
6073 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6074 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6077 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
6080 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6081 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6082 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6085 /* Testing for overflow is unnecessary if we already know the result. */
6090 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6091 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6092 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6093 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6098 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6099 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6100 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6101 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6103 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6104 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
6108 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6109 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6110 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6111 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6113 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6114 is at least twice as wide as type of A and B, simplify to
6115 __builtin_mul_overflow (A, B, <unused>). */
6118 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6120 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6121 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6122 && TYPE_UNSIGNED (TREE_TYPE (@0))
6123 && (TYPE_PRECISION (TREE_TYPE (@3))
6124 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6125 && tree_fits_uhwi_p (@2)
6126 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6127 && types_match (@0, @1)
6128 && type_has_mode_precision_p (TREE_TYPE (@0))
6129 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6130 != CODE_FOR_nothing))
6131 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6132 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6134 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
6135 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6137 (ovf (convert@2 @0) @1)
6138 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6139 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6140 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6141 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6144 (ovf @1 (convert@2 @0))
6145 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6146 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6147 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6148 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6151 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6152 are unsigned to x > (umax / cst). Similarly for signed type, but
6153 in that case it needs to be outside of a range. */
6155 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6156 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6157 && TYPE_MAX_VALUE (TREE_TYPE (@0))
6158 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6159 && int_fits_type_p (@1, TREE_TYPE (@0)))
6160 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6161 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6162 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6163 (if (integer_minus_onep (@1))
6164 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6167 tree div = fold_convert (TREE_TYPE (@0), @1);
6168 tree lo = int_const_binop (TRUNC_DIV_EXPR,
6169 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6170 tree hi = int_const_binop (TRUNC_DIV_EXPR,
6171 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6172 tree etype = range_check_type (TREE_TYPE (@0));
6175 if (wi::neg_p (wi::to_wide (div)))
6177 lo = fold_convert (etype, lo);
6178 hi = fold_convert (etype, hi);
6179 hi = int_const_binop (MINUS_EXPR, hi, lo);
6183 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
6185 /* Simplification of math builtins. These rules must all be optimizations
6186 as well as IL simplifications. If there is a possibility that the new
6187 form could be a pessimization, the rule should go in the canonicalization
6188 section that follows this one.
6190 Rules can generally go in this section if they satisfy one of
6193 - the rule describes an identity
6195 - the rule replaces calls with something as simple as addition or
6198 - the rule contains unary calls only and simplifies the surrounding
6199 arithmetic. (The idea here is to exclude non-unary calls in which
6200 one operand is constant and in which the call is known to be cheap
6201 when the operand has that value.) */
6203 (if (flag_unsafe_math_optimizations)
6204 /* Simplify sqrt(x) * sqrt(x) -> x. */
6206 (mult (SQRT_ALL@1 @0) @1)
6207 (if (!tree_expr_maybe_signaling_nan_p (@0))
6210 (for op (plus minus)
6211 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
6215 (rdiv (op @0 @2) @1)))
6217 (for cmp (lt le gt ge)
6218 neg_cmp (gt ge lt le)
6219 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
6221 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6223 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6225 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6226 || (real_zerop (tem) && !real_zerop (@1))))
6228 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6230 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6231 (neg_cmp @0 { tem; })))))))
6233 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
6234 (for root (SQRT CBRT)
6236 (mult (root:s @0) (root:s @1))
6237 (root (mult @0 @1))))
6239 /* Simplify expN(x) * expN(y) -> expN(x+y). */
6240 (for exps (EXP EXP2 EXP10 POW10)
6242 (mult (exps:s @0) (exps:s @1))
6243 (exps (plus @0 @1))))
6245 /* Simplify a/root(b/c) into a*root(c/b). */
6246 (for root (SQRT CBRT)
6248 (rdiv @0 (root:s (rdiv:s @1 @2)))
6249 (mult @0 (root (rdiv @2 @1)))))
6251 /* Simplify x/expN(y) into x*expN(-y). */
6252 (for exps (EXP EXP2 EXP10 POW10)
6254 (rdiv @0 (exps:s @1))
6255 (mult @0 (exps (negate @1)))))
6257 (for logs (LOG LOG2 LOG10 LOG10)
6258 exps (EXP EXP2 EXP10 POW10)
6259 /* logN(expN(x)) -> x. */
6263 /* expN(logN(x)) -> x. */
6268 /* Optimize logN(func()) for various exponential functions. We
6269 want to determine the value "x" and the power "exponent" in
6270 order to transform logN(x**exponent) into exponent*logN(x). */
6271 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
6272 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
6275 (if (SCALAR_FLOAT_TYPE_P (type))
6281 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
6282 x = build_real_truncate (type, dconst_e ());
6285 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
6286 x = build_real (type, dconst2);
6290 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
6292 REAL_VALUE_TYPE dconst10;
6293 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6294 x = build_real (type, dconst10);
6301 (mult (logs { x; }) @0)))))
6309 (if (SCALAR_FLOAT_TYPE_P (type))
6315 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
6316 x = build_real (type, dconsthalf);
6319 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
6320 x = build_real_truncate (type, dconst_third ());
6326 (mult { x; } (logs @0))))))
6328 /* logN(pow(x,exponent)) -> exponent*logN(x). */
6329 (for logs (LOG LOG2 LOG10)
6333 (mult @1 (logs @0))))
6335 /* pow(C,x) -> exp(log(C)*x) if C > 0,
6336 or if C is a positive power of 2,
6337 pow(C,x) -> exp2(log2(C)*x). */
6345 (pows REAL_CST@0 @1)
6346 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6347 && real_isfinite (TREE_REAL_CST_PTR (@0))
6348 /* As libmvec doesn't have a vectorized exp2, defer optimizing
6349 the use_exp2 case until after vectorization. It seems actually
6350 beneficial for all constants to postpone this until later,
6351 because exp(log(C)*x), while faster, will have worse precision
6352 and if x folds into a constant too, that is unnecessary
6354 && canonicalize_math_after_vectorization_p ())
6356 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
6357 bool use_exp2 = false;
6358 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
6359 && value->cl == rvc_normal)
6361 REAL_VALUE_TYPE frac_rvt = *value;
6362 SET_REAL_EXP (&frac_rvt, 1);
6363 if (real_equal (&frac_rvt, &dconst1))
6368 (if (optimize_pow_to_exp (@0, @1))
6369 (exps (mult (logs @0) @1)))
6370 (exp2s (mult (log2s @0) @1)))))))
6373 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
6375 exps (EXP EXP2 EXP10 POW10)
6376 logs (LOG LOG2 LOG10 LOG10)
6378 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6379 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6380 && real_isfinite (TREE_REAL_CST_PTR (@0)))
6381 (exps (plus (mult (logs @0) @1) @2)))))
6386 exps (EXP EXP2 EXP10 POW10)
6387 /* sqrt(expN(x)) -> expN(x*0.5). */
6390 (exps (mult @0 { build_real (type, dconsthalf); })))
6391 /* cbrt(expN(x)) -> expN(x/3). */
6394 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6395 /* pow(expN(x), y) -> expN(x*y). */
6398 (exps (mult @0 @1))))
6400 /* tan(atan(x)) -> x. */
6407 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6411 copysigns (COPYSIGN)
6416 REAL_VALUE_TYPE r_cst;
6417 build_sinatan_real (&r_cst, type);
6418 tree t_cst = build_real (type, r_cst);
6419 tree t_one = build_one_cst (type);
6421 (if (SCALAR_FLOAT_TYPE_P (type))
6422 (cond (lt (abs @0) { t_cst; })
6423 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6424 (copysigns { t_one; } @0))))))
6426 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6430 copysigns (COPYSIGN)
6435 REAL_VALUE_TYPE r_cst;
6436 build_sinatan_real (&r_cst, type);
6437 tree t_cst = build_real (type, r_cst);
6438 tree t_one = build_one_cst (type);
6439 tree t_zero = build_zero_cst (type);
6441 (if (SCALAR_FLOAT_TYPE_P (type))
6442 (cond (lt (abs @0) { t_cst; })
6443 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6444 (copysigns { t_zero; } @0))))))
6446 (if (!flag_errno_math)
6447 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6452 (sinhs (atanhs:s @0))
6453 (with { tree t_one = build_one_cst (type); }
6454 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6456 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6461 (coshs (atanhs:s @0))
6462 (with { tree t_one = build_one_cst (type); }
6463 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6465 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
6467 (CABS (complex:C @0 real_zerop@1))
6470 /* trunc(trunc(x)) -> trunc(x), etc. */
6471 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6475 /* f(x) -> x if x is integer valued and f does nothing for such values. */
6476 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6478 (fns integer_valued_real_p@0)
6481 /* hypot(x,0) and hypot(0,x) -> abs(x). */
6483 (HYPOT:c @0 real_zerop@1)
6486 /* pow(1,x) -> 1. */
6488 (POW real_onep@0 @1)
6492 /* copysign(x,x) -> x. */
6493 (COPYSIGN_ALL @0 @0)
6497 /* copysign(x,-x) -> -x. */
6498 (COPYSIGN_ALL @0 (negate@1 @0))
6502 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
6503 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6506 (for scale (LDEXP SCALBN SCALBLN)
6507 /* ldexp(0, x) -> 0. */
6509 (scale real_zerop@0 @1)
6511 /* ldexp(x, 0) -> x. */
6513 (scale @0 integer_zerop@1)
6515 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
6517 (scale REAL_CST@0 @1)
6518 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6521 /* Canonicalization of sequences of math builtins. These rules represent
6522 IL simplifications but are not necessarily optimizations.
6524 The sincos pass is responsible for picking "optimal" implementations
6525 of math builtins, which may be more complicated and can sometimes go
6526 the other way, e.g. converting pow into a sequence of sqrts.
6527 We only want to do these canonicalizations before the pass has run. */
6529 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6530 /* Simplify tan(x) * cos(x) -> sin(x). */
6532 (mult:c (TAN:s @0) (COS:s @0))
6535 /* Simplify x * pow(x,c) -> pow(x,c+1). */
6537 (mult:c @0 (POW:s @0 REAL_CST@1))
6538 (if (!TREE_OVERFLOW (@1))
6539 (POW @0 (plus @1 { build_one_cst (type); }))))
6541 /* Simplify sin(x) / cos(x) -> tan(x). */
6543 (rdiv (SIN:s @0) (COS:s @0))
6546 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6548 (rdiv (SINH:s @0) (COSH:s @0))
6551 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6553 (rdiv (TANH:s @0) (SINH:s @0))
6554 (rdiv {build_one_cst (type);} (COSH @0)))
6556 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6558 (rdiv (COS:s @0) (SIN:s @0))
6559 (rdiv { build_one_cst (type); } (TAN @0)))
6561 /* Simplify sin(x) / tan(x) -> cos(x). */
6563 (rdiv (SIN:s @0) (TAN:s @0))
6564 (if (! HONOR_NANS (@0)
6565 && ! HONOR_INFINITIES (@0))
6568 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6570 (rdiv (TAN:s @0) (SIN:s @0))
6571 (if (! HONOR_NANS (@0)
6572 && ! HONOR_INFINITIES (@0))
6573 (rdiv { build_one_cst (type); } (COS @0))))
6575 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6577 (mult (POW:s @0 @1) (POW:s @0 @2))
6578 (POW @0 (plus @1 @2)))
6580 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6582 (mult (POW:s @0 @1) (POW:s @2 @1))
6583 (POW (mult @0 @2) @1))
6585 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6587 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6588 (POWI (mult @0 @2) @1))
6590 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6592 (rdiv (POW:s @0 REAL_CST@1) @0)
6593 (if (!TREE_OVERFLOW (@1))
6594 (POW @0 (minus @1 { build_one_cst (type); }))))
6596 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6598 (rdiv @0 (POW:s @1 @2))
6599 (mult @0 (POW @1 (negate @2))))
6604 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6607 (pows @0 { build_real (type, dconst_quarter ()); }))
6608 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6611 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6612 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6615 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6616 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6618 (cbrts (cbrts tree_expr_nonnegative_p@0))
6619 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6620 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6622 (sqrts (pows @0 @1))
6623 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6624 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6626 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6627 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6628 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6630 (pows (sqrts @0) @1)
6631 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6632 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6634 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6635 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6636 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6638 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6639 (pows @0 (mult @1 @2))))
6641 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6643 (CABS (complex @0 @0))
6644 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6646 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6649 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6651 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6656 (cexps compositional_complex@0)
6657 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6659 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6660 (mult @1 (imagpart @2)))))))
6662 (if (canonicalize_math_p ())
6663 /* floor(x) -> trunc(x) if x is nonnegative. */
6664 (for floors (FLOOR_ALL)
6667 (floors tree_expr_nonnegative_p@0)
6670 (match double_value_p
6672 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6673 (for froms (BUILT_IN_TRUNCL
6685 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6686 (if (optimize && canonicalize_math_p ())
6688 (froms (convert double_value_p@0))
6689 (convert (tos @0)))))
6691 (match float_value_p
6693 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6694 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6695 BUILT_IN_FLOORL BUILT_IN_FLOOR
6696 BUILT_IN_CEILL BUILT_IN_CEIL
6697 BUILT_IN_ROUNDL BUILT_IN_ROUND
6698 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6699 BUILT_IN_RINTL BUILT_IN_RINT)
6700 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6701 BUILT_IN_FLOORF BUILT_IN_FLOORF
6702 BUILT_IN_CEILF BUILT_IN_CEILF
6703 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6704 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6705 BUILT_IN_RINTF BUILT_IN_RINTF)
6706 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6708 (if (optimize && canonicalize_math_p ()
6709 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6711 (froms (convert float_value_p@0))
6712 (convert (tos @0)))))
6715 (match float16_value_p
6717 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
6718 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
6719 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
6720 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
6721 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
6722 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
6723 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
6724 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
6725 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
6726 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
6727 IFN_FLOOR IFN_FLOOR IFN_FLOOR
6728 IFN_CEIL IFN_CEIL IFN_CEIL
6729 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
6730 IFN_ROUND IFN_ROUND IFN_ROUND
6731 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
6732 IFN_RINT IFN_RINT IFN_RINT
6733 IFN_SQRT IFN_SQRT IFN_SQRT)
6734 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
6735 if x is a _Float16. */
6737 (convert (froms (convert float16_value_p@0)))
6739 && types_match (type, TREE_TYPE (@0))
6740 && direct_internal_fn_supported_p (as_internal_fn (tos),
6741 type, OPTIMIZE_FOR_BOTH))
6744 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
6745 x,y is float value, similar for _Float16/double. */
6746 (for copysigns (COPYSIGN_ALL)
6748 (convert (copysigns (convert@2 @0) (convert @1)))
6750 && !HONOR_SNANS (@2)
6751 && types_match (type, TREE_TYPE (@0))
6752 && types_match (type, TREE_TYPE (@1))
6753 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
6754 && direct_internal_fn_supported_p (IFN_COPYSIGN,
6755 type, OPTIMIZE_FOR_BOTH))
6756 (IFN_COPYSIGN @0 @1))))
6758 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
6759 tos (IFN_FMA IFN_FMA IFN_FMA)
6761 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
6762 (if (flag_unsafe_math_optimizations
6764 && FLOAT_TYPE_P (type)
6765 && FLOAT_TYPE_P (TREE_TYPE (@3))
6766 && types_match (type, TREE_TYPE (@0))
6767 && types_match (type, TREE_TYPE (@1))
6768 && types_match (type, TREE_TYPE (@2))
6769 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
6770 && direct_internal_fn_supported_p (as_internal_fn (tos),
6771 type, OPTIMIZE_FOR_BOTH))
6774 (for maxmin (max min)
6776 (convert (maxmin (convert@2 @0) (convert @1)))
6778 && FLOAT_TYPE_P (type)
6779 && FLOAT_TYPE_P (TREE_TYPE (@2))
6780 && types_match (type, TREE_TYPE (@0))
6781 && types_match (type, TREE_TYPE (@1))
6782 && element_precision (type) < element_precision (TREE_TYPE (@2)))
6786 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6787 tos (XFLOOR XCEIL XROUND XRINT)
6788 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6789 (if (optimize && canonicalize_math_p ())
6791 (froms (convert double_value_p@0))
6794 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6795 XFLOOR XCEIL XROUND XRINT)
6796 tos (XFLOORF XCEILF XROUNDF XRINTF)
6797 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6799 (if (optimize && canonicalize_math_p ())
6801 (froms (convert float_value_p@0))
6804 (if (canonicalize_math_p ())
6805 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6806 (for floors (IFLOOR LFLOOR LLFLOOR)
6808 (floors tree_expr_nonnegative_p@0)
6811 (if (canonicalize_math_p ())
6812 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6813 (for fns (IFLOOR LFLOOR LLFLOOR
6815 IROUND LROUND LLROUND)
6817 (fns integer_valued_real_p@0)
6819 (if (!flag_errno_math)
6820 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6821 (for rints (IRINT LRINT LLRINT)
6823 (rints integer_valued_real_p@0)
6826 (if (canonicalize_math_p ())
6827 (for ifn (IFLOOR ICEIL IROUND IRINT)
6828 lfn (LFLOOR LCEIL LROUND LRINT)
6829 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6830 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6831 sizeof (int) == sizeof (long). */
6832 (if (TYPE_PRECISION (integer_type_node)
6833 == TYPE_PRECISION (long_integer_type_node))
6836 (lfn:long_integer_type_node @0)))
6837 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6838 sizeof (long long) == sizeof (long). */
6839 (if (TYPE_PRECISION (long_long_integer_type_node)
6840 == TYPE_PRECISION (long_integer_type_node))
6843 (lfn:long_integer_type_node @0)))))
6845 /* cproj(x) -> x if we're ignoring infinities. */
6848 (if (!HONOR_INFINITIES (type))
6851 /* If the real part is inf and the imag part is known to be
6852 nonnegative, return (inf + 0i). */
6854 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6855 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6856 { build_complex_inf (type, false); }))
6858 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6860 (CPROJ (complex @0 REAL_CST@1))
6861 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6862 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6868 (pows @0 REAL_CST@1)
6870 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6871 REAL_VALUE_TYPE tmp;
6874 /* pow(x,0) -> 1. */
6875 (if (real_equal (value, &dconst0))
6876 { build_real (type, dconst1); })
6877 /* pow(x,1) -> x. */
6878 (if (real_equal (value, &dconst1))
6880 /* pow(x,-1) -> 1/x. */
6881 (if (real_equal (value, &dconstm1))
6882 (rdiv { build_real (type, dconst1); } @0))
6883 /* pow(x,0.5) -> sqrt(x). */
6884 (if (flag_unsafe_math_optimizations
6885 && canonicalize_math_p ()
6886 && real_equal (value, &dconsthalf))
6888 /* pow(x,1/3) -> cbrt(x). */
6889 (if (flag_unsafe_math_optimizations
6890 && canonicalize_math_p ()
6891 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6892 real_equal (value, &tmp)))
6895 /* powi(1,x) -> 1. */
6897 (POWI real_onep@0 @1)
6901 (POWI @0 INTEGER_CST@1)
6903 /* powi(x,0) -> 1. */
6904 (if (wi::to_wide (@1) == 0)
6905 { build_real (type, dconst1); })
6906 /* powi(x,1) -> x. */
6907 (if (wi::to_wide (@1) == 1)
6909 /* powi(x,-1) -> 1/x. */
6910 (if (wi::to_wide (@1) == -1)
6911 (rdiv { build_real (type, dconst1); } @0))))
6913 /* Narrowing of arithmetic and logical operations.
6915 These are conceptually similar to the transformations performed for
6916 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6917 term we want to move all that code out of the front-ends into here. */
6919 /* Convert (outertype)((innertype0)a+(innertype1)b)
6920 into ((newtype)a+(newtype)b) where newtype
6921 is the widest mode from all of these. */
6922 (for op (plus minus mult rdiv)
6924 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6925 /* If we have a narrowing conversion of an arithmetic operation where
6926 both operands are widening conversions from the same type as the outer
6927 narrowing conversion. Then convert the innermost operands to a
6928 suitable unsigned type (to avoid introducing undefined behavior),
6929 perform the operation and convert the result to the desired type. */
6930 (if (INTEGRAL_TYPE_P (type)
6933 /* We check for type compatibility between @0 and @1 below,
6934 so there's no need to check that @2/@4 are integral types. */
6935 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6936 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6937 /* The precision of the type of each operand must match the
6938 precision of the mode of each operand, similarly for the
6940 && type_has_mode_precision_p (TREE_TYPE (@1))
6941 && type_has_mode_precision_p (TREE_TYPE (@2))
6942 && type_has_mode_precision_p (type)
6943 /* The inner conversion must be a widening conversion. */
6944 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
6945 && types_match (@1, type)
6946 && (types_match (@1, @2)
6947 /* Or the second operand is const integer or converted const
6948 integer from valueize. */
6949 || poly_int_tree_p (@4)))
6950 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
6951 (op @1 (convert @2))
6952 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6953 (convert (op (convert:utype @1)
6954 (convert:utype @2)))))
6955 (if (FLOAT_TYPE_P (type)
6956 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6957 == DECIMAL_FLOAT_TYPE_P (type))
6958 (with { tree arg0 = strip_float_extensions (@1);
6959 tree arg1 = strip_float_extensions (@2);
6960 tree itype = TREE_TYPE (@0);
6961 tree ty1 = TREE_TYPE (arg0);
6962 tree ty2 = TREE_TYPE (arg1);
6963 enum tree_code code = TREE_CODE (itype); }
6964 (if (FLOAT_TYPE_P (ty1)
6965 && FLOAT_TYPE_P (ty2))
6966 (with { tree newtype = type;
6967 if (TYPE_MODE (ty1) == SDmode
6968 || TYPE_MODE (ty2) == SDmode
6969 || TYPE_MODE (type) == SDmode)
6970 newtype = dfloat32_type_node;
6971 if (TYPE_MODE (ty1) == DDmode
6972 || TYPE_MODE (ty2) == DDmode
6973 || TYPE_MODE (type) == DDmode)
6974 newtype = dfloat64_type_node;
6975 if (TYPE_MODE (ty1) == TDmode
6976 || TYPE_MODE (ty2) == TDmode
6977 || TYPE_MODE (type) == TDmode)
6978 newtype = dfloat128_type_node; }
6979 (if ((newtype == dfloat32_type_node
6980 || newtype == dfloat64_type_node
6981 || newtype == dfloat128_type_node)
6983 && types_match (newtype, type))
6984 (op (convert:newtype @1) (convert:newtype @2))
6985 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
6987 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
6989 /* Sometimes this transformation is safe (cannot
6990 change results through affecting double rounding
6991 cases) and sometimes it is not. If NEWTYPE is
6992 wider than TYPE, e.g. (float)((long double)double
6993 + (long double)double) converted to
6994 (float)(double + double), the transformation is
6995 unsafe regardless of the details of the types
6996 involved; double rounding can arise if the result
6997 of NEWTYPE arithmetic is a NEWTYPE value half way
6998 between two representable TYPE values but the
6999 exact value is sufficiently different (in the
7000 right direction) for this difference to be
7001 visible in ITYPE arithmetic. If NEWTYPE is the
7002 same as TYPE, however, the transformation may be
7003 safe depending on the types involved: it is safe
7004 if the ITYPE has strictly more than twice as many
7005 mantissa bits as TYPE, can represent infinities
7006 and NaNs if the TYPE can, and has sufficient
7007 exponent range for the product or ratio of two
7008 values representable in the TYPE to be within the
7009 range of normal values of ITYPE. */
7010 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
7011 && (flag_unsafe_math_optimizations
7012 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
7013 && real_can_shorten_arithmetic (TYPE_MODE (itype),
7015 && !excess_precision_type (newtype)))
7016 && !types_match (itype, newtype))
7017 (convert:type (op (convert:newtype @1)
7018 (convert:newtype @2)))
7023 /* This is another case of narrowing, specifically when there's an outer
7024 BIT_AND_EXPR which masks off bits outside the type of the innermost
7025 operands. Like the previous case we have to convert the operands
7026 to unsigned types to avoid introducing undefined behavior for the
7027 arithmetic operation. */
7028 (for op (minus plus)
7030 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7031 (if (INTEGRAL_TYPE_P (type)
7032 /* We check for type compatibility between @0 and @1 below,
7033 so there's no need to check that @1/@3 are integral types. */
7034 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7035 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7036 /* The precision of the type of each operand must match the
7037 precision of the mode of each operand, similarly for the
7039 && type_has_mode_precision_p (TREE_TYPE (@0))
7040 && type_has_mode_precision_p (TREE_TYPE (@1))
7041 && type_has_mode_precision_p (type)
7042 /* The inner conversion must be a widening conversion. */
7043 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7044 && types_match (@0, @1)
7045 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7046 <= TYPE_PRECISION (TREE_TYPE (@0)))
7047 && (wi::to_wide (@4)
7048 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7049 true, TYPE_PRECISION (type))) == 0)
7050 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7051 (with { tree ntype = TREE_TYPE (@0); }
7052 (convert (bit_and (op @0 @1) (convert:ntype @4))))
7053 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7054 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7055 (convert:utype @4))))))))
7057 /* Transform (@0 < @1 and @0 < @2) to use min,
7058 (@0 > @1 and @0 > @2) to use max */
7059 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7060 op (lt le gt ge lt le gt ge )
7061 ext (min min max max max max min min )
7063 (logic (op:cs @0 @1) (op:cs @0 @2))
7064 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7065 && TREE_CODE (@0) != INTEGER_CST)
7066 (op @0 (ext @1 @2)))))
7069 /* signbit(x) -> 0 if x is nonnegative. */
7070 (SIGNBIT tree_expr_nonnegative_p@0)
7071 { integer_zero_node; })
7074 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
7076 (if (!HONOR_SIGNED_ZEROS (@0))
7077 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7079 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
7081 (for op (plus minus)
7084 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7085 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7086 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7087 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7088 && !TYPE_SATURATING (TREE_TYPE (@0)))
7089 (with { tree res = int_const_binop (rop, @2, @1); }
7090 (if (TREE_OVERFLOW (res)
7091 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7092 { constant_boolean_node (cmp == NE_EXPR, type); }
7093 (if (single_use (@3))
7094 (cmp @0 { TREE_OVERFLOW (res)
7095 ? drop_tree_overflow (res) : res; }))))))))
7096 (for cmp (lt le gt ge)
7097 (for op (plus minus)
7100 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7101 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7102 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7103 (with { tree res = int_const_binop (rop, @2, @1); }
7104 (if (TREE_OVERFLOW (res))
7106 fold_overflow_warning (("assuming signed overflow does not occur "
7107 "when simplifying conditional to constant"),
7108 WARN_STRICT_OVERFLOW_CONDITIONAL);
7109 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7110 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
7111 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7112 TYPE_SIGN (TREE_TYPE (@1)))
7113 != (op == MINUS_EXPR);
7114 constant_boolean_node (less == ovf_high, type);
7116 (if (single_use (@3))
7119 fold_overflow_warning (("assuming signed overflow does not occur "
7120 "when changing X +- C1 cmp C2 to "
7122 WARN_STRICT_OVERFLOW_COMPARISON);
7124 (cmp @0 { res; })))))))))
7126 /* Canonicalizations of BIT_FIELD_REFs. */
7129 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7130 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7133 (BIT_FIELD_REF (view_convert @0) @1 @2)
7134 (BIT_FIELD_REF @0 @1 @2))
7137 (BIT_FIELD_REF @0 @1 integer_zerop)
7138 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7142 (BIT_FIELD_REF @0 @1 @2)
7144 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7145 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7147 (if (integer_zerop (@2))
7148 (view_convert (realpart @0)))
7149 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7150 (view_convert (imagpart @0)))))
7151 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7152 && INTEGRAL_TYPE_P (type)
7153 /* On GIMPLE this should only apply to register arguments. */
7154 && (! GIMPLE || is_gimple_reg (@0))
7155 /* A bit-field-ref that referenced the full argument can be stripped. */
7156 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
7157 && integer_zerop (@2))
7158 /* Low-parts can be reduced to integral conversions.
7159 ??? The following doesn't work for PDP endian. */
7160 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7161 /* But only do this after vectorization. */
7162 && canonicalize_math_after_vectorization_p ()
7163 /* Don't even think about BITS_BIG_ENDIAN. */
7164 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
7165 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
7166 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
7167 ? (TYPE_PRECISION (TREE_TYPE (@0))
7168 - TYPE_PRECISION (type))
7172 /* Simplify vector extracts. */
7175 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
7176 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
7177 && tree_fits_uhwi_p (TYPE_SIZE (type))
7178 && ((tree_to_uhwi (TYPE_SIZE (type))
7179 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7180 || (VECTOR_TYPE_P (type)
7181 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
7182 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
7185 tree ctor = (TREE_CODE (@0) == SSA_NAME
7186 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7187 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
7188 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
7189 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
7190 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
7193 && (idx % width) == 0
7195 && known_le ((idx + n) / width,
7196 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
7201 /* Constructor elements can be subvectors. */
7203 if (CONSTRUCTOR_NELTS (ctor) != 0)
7205 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7206 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7207 k = TYPE_VECTOR_SUBPARTS (cons_elem);
7209 unsigned HOST_WIDE_INT elt, count, const_k;
7212 /* We keep an exact subset of the constructor elements. */
7213 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7214 (if (CONSTRUCTOR_NELTS (ctor) == 0)
7215 { build_zero_cst (type); }
7217 (if (elt < CONSTRUCTOR_NELTS (ctor))
7218 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7219 { build_zero_cst (type); })
7220 /* We don't want to emit new CTORs unless the old one goes away.
7221 ??? Eventually allow this if the CTOR ends up constant or
7223 (if (single_use (@0))
7226 vec<constructor_elt, va_gc> *vals;
7227 vec_alloc (vals, count);
7228 bool constant_p = true;
7230 for (unsigned i = 0;
7231 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7233 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7234 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7235 if (!CONSTANT_CLASS_P (e))
7238 tree evtype = (types_match (TREE_TYPE (type),
7239 TREE_TYPE (TREE_TYPE (ctor)))
7241 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7243 res = (constant_p ? build_vector_from_ctor (evtype, vals)
7244 : build_constructor (evtype, vals));
7246 (view_convert { res; }))))))
7247 /* The bitfield references a single constructor element. */
7248 (if (k.is_constant (&const_k)
7249 && idx + n <= (idx / const_k + 1) * const_k)
7251 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7252 { build_zero_cst (type); })
7254 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7255 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7256 @1 { bitsize_int ((idx % const_k) * width); })))))))))
7258 /* Simplify a bit extraction from a bit insertion for the cases with
7259 the inserted element fully covering the extraction or the insertion
7260 not touching the extraction. */
7262 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7265 unsigned HOST_WIDE_INT isize;
7266 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7267 isize = TYPE_PRECISION (TREE_TYPE (@1));
7269 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7272 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7273 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
7274 wi::to_wide (@ipos) + isize))
7275 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
7277 - wi::to_wide (@ipos)); }))
7278 (if (wi::geu_p (wi::to_wide (@ipos),
7279 wi::to_wide (@rpos) + wi::to_wide (@rsize))
7280 || wi::geu_p (wi::to_wide (@rpos),
7281 wi::to_wide (@ipos) + isize))
7282 (BIT_FIELD_REF @0 @rsize @rpos)))))
7284 (if (canonicalize_math_after_vectorization_p ())
7287 (fmas:c (negate @0) @1 @2)
7288 (IFN_FNMA @0 @1 @2))
7290 (fmas @0 @1 (negate @2))
7293 (fmas:c (negate @0) @1 (negate @2))
7294 (IFN_FNMS @0 @1 @2))
7296 (negate (fmas@3 @0 @1 @2))
7297 (if (single_use (@3))
7298 (IFN_FNMS @0 @1 @2))))
7301 (IFN_FMS:c (negate @0) @1 @2)
7302 (IFN_FNMS @0 @1 @2))
7304 (IFN_FMS @0 @1 (negate @2))
7307 (IFN_FMS:c (negate @0) @1 (negate @2))
7308 (IFN_FNMA @0 @1 @2))
7310 (negate (IFN_FMS@3 @0 @1 @2))
7311 (if (single_use (@3))
7312 (IFN_FNMA @0 @1 @2)))
7315 (IFN_FNMA:c (negate @0) @1 @2)
7318 (IFN_FNMA @0 @1 (negate @2))
7319 (IFN_FNMS @0 @1 @2))
7321 (IFN_FNMA:c (negate @0) @1 (negate @2))
7324 (negate (IFN_FNMA@3 @0 @1 @2))
7325 (if (single_use (@3))
7326 (IFN_FMS @0 @1 @2)))
7329 (IFN_FNMS:c (negate @0) @1 @2)
7332 (IFN_FNMS @0 @1 (negate @2))
7333 (IFN_FNMA @0 @1 @2))
7335 (IFN_FNMS:c (negate @0) @1 (negate @2))
7338 (negate (IFN_FNMS@3 @0 @1 @2))
7339 (if (single_use (@3))
7340 (IFN_FMA @0 @1 @2))))
7342 /* CLZ simplifications. */
7347 (op (clz:s@2 @0) INTEGER_CST@1)
7348 (if (integer_zerop (@1) && single_use (@2))
7349 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
7350 (with { tree type0 = TREE_TYPE (@0);
7351 tree stype = signed_type_for (type0);
7352 HOST_WIDE_INT val = 0;
7353 /* Punt on hypothetical weird targets. */
7355 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7361 (cmp (convert:stype @0) { build_zero_cst (stype); })))
7362 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
7363 (with { bool ok = true;
7364 HOST_WIDE_INT val = 0;
7365 tree type0 = TREE_TYPE (@0);
7366 /* Punt on hypothetical weird targets. */
7368 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7370 && val == TYPE_PRECISION (type0) - 1)
7373 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7374 (op @0 { build_one_cst (type0); })))))))
7376 /* CTZ simplifications. */
7378 (for op (ge gt le lt)
7381 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
7382 (op (ctz:s @0) INTEGER_CST@1)
7383 (with { bool ok = true;
7384 HOST_WIDE_INT val = 0;
7385 if (!tree_fits_shwi_p (@1))
7389 val = tree_to_shwi (@1);
7390 /* Canonicalize to >= or <. */
7391 if (op == GT_EXPR || op == LE_EXPR)
7393 if (val == HOST_WIDE_INT_MAX)
7399 bool zero_res = false;
7400 HOST_WIDE_INT zero_val = 0;
7401 tree type0 = TREE_TYPE (@0);
7402 int prec = TYPE_PRECISION (type0);
7404 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7409 (if (ok && (!zero_res || zero_val >= val))
7410 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7412 (if (ok && (!zero_res || zero_val < val))
7413 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7414 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7415 (cmp (bit_and @0 { wide_int_to_tree (type0,
7416 wi::mask (val, false, prec)); })
7417 { build_zero_cst (type0); })))))))
7420 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
7421 (op (ctz:s @0) INTEGER_CST@1)
7422 (with { bool zero_res = false;
7423 HOST_WIDE_INT zero_val = 0;
7424 tree type0 = TREE_TYPE (@0);
7425 int prec = TYPE_PRECISION (type0);
7427 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7431 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7432 (if (!zero_res || zero_val != wi::to_widest (@1))
7433 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7434 (if (!zero_res || zero_val < 0 || zero_val >= prec)
7435 (op (bit_and @0 { wide_int_to_tree (type0,
7436 wi::mask (tree_to_uhwi (@1) + 1,
7438 { wide_int_to_tree (type0,
7439 wi::shifted_mask (tree_to_uhwi (@1), 1,
7440 false, prec)); })))))))
7442 /* POPCOUNT simplifications. */
7443 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
7445 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7446 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7447 (POPCOUNT (bit_ior @0 @1))))
7449 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
7450 (for popcount (POPCOUNT)
7451 (for cmp (le eq ne gt)
7454 (cmp (popcount @0) integer_zerop)
7455 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7457 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
7459 (bit_and (POPCOUNT @0) integer_onep)
7462 /* PARITY simplifications. */
7463 /* parity(~X) is parity(X). */
7465 (PARITY (bit_not @0))
7468 /* parity(X)^parity(Y) is parity(X^Y). */
7470 (bit_xor (PARITY:s @0) (PARITY:s @1))
7471 (PARITY (bit_xor @0 @1)))
7473 /* Common POPCOUNT/PARITY simplifications. */
7474 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
7475 (for pfun (POPCOUNT PARITY)
7478 (with { wide_int nz = tree_nonzero_bits (@0); }
7482 (if (wi::popcount (nz) == 1)
7483 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7484 (convert (rshift:utype (convert:utype @0)
7485 { build_int_cst (integer_type_node,
7486 wi::ctz (nz)); }))))))))
7489 /* 64- and 32-bits branchless implementations of popcount are detected:
7491 int popcount64c (uint64_t x)
7493 x -= (x >> 1) & 0x5555555555555555ULL;
7494 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7495 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
7496 return (x * 0x0101010101010101ULL) >> 56;
7499 int popcount32c (uint32_t x)
7501 x -= (x >> 1) & 0x55555555;
7502 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
7503 x = (x + (x >> 4)) & 0x0f0f0f0f;
7504 return (x * 0x01010101) >> 24;
7511 (rshift @8 INTEGER_CST@5)
7513 (bit_and @6 INTEGER_CST@7)
7517 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
7523 /* Check constants and optab. */
7524 (with { unsigned prec = TYPE_PRECISION (type);
7525 int shift = (64 - prec) & 63;
7526 unsigned HOST_WIDE_INT c1
7527 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
7528 unsigned HOST_WIDE_INT c2
7529 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
7530 unsigned HOST_WIDE_INT c3
7531 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
7532 unsigned HOST_WIDE_INT c4
7533 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
7538 && TYPE_UNSIGNED (type)
7539 && integer_onep (@4)
7540 && wi::to_widest (@10) == 2
7541 && wi::to_widest (@5) == 4
7542 && wi::to_widest (@1) == prec - 8
7543 && tree_to_uhwi (@2) == c1
7544 && tree_to_uhwi (@3) == c2
7545 && tree_to_uhwi (@9) == c3
7546 && tree_to_uhwi (@7) == c3
7547 && tree_to_uhwi (@11) == c4)
7548 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
7550 (convert (IFN_POPCOUNT:type @0))
7551 /* Try to do popcount in two halves. PREC must be at least
7552 five bits for this to work without extension before adding. */
7554 tree half_type = NULL_TREE;
7555 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
7558 && m.require () != TYPE_MODE (type))
7560 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
7561 half_type = build_nonstandard_integer_type (half_prec, 1);
7563 gcc_assert (half_prec > 2);
7565 (if (half_type != NULL_TREE
7566 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
7569 (IFN_POPCOUNT:half_type (convert @0))
7570 (IFN_POPCOUNT:half_type (convert (rshift @0
7571 { build_int_cst (integer_type_node, half_prec); } )))))))))))
7573 /* __builtin_ffs needs to deal on many targets with the possible zero
7574 argument. If we know the argument is always non-zero, __builtin_ctz + 1
7575 should lead to better code. */
7577 (FFS tree_expr_nonzero_p@0)
7578 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7579 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
7580 OPTIMIZE_FOR_SPEED))
7581 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7582 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
7585 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
7587 /* __builtin_ffs (X) == 0 -> X == 0.
7588 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
7591 (cmp (ffs@2 @0) INTEGER_CST@1)
7592 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7594 (if (integer_zerop (@1))
7595 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7596 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7597 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7598 (if (single_use (@2))
7599 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7600 wi::mask (tree_to_uhwi (@1),
7602 { wide_int_to_tree (TREE_TYPE (@0),
7603 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7604 false, prec)); }))))))
7606 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
7610 bit_op (bit_and bit_ior)
7612 (cmp (ffs@2 @0) INTEGER_CST@1)
7613 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7615 (if (integer_zerop (@1))
7616 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7617 (if (tree_int_cst_sgn (@1) < 0)
7618 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7619 (if (wi::to_widest (@1) >= prec)
7620 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7621 (if (wi::to_widest (@1) == prec - 1)
7622 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7623 wi::shifted_mask (prec - 1, 1,
7625 (if (single_use (@2))
7626 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7628 { wide_int_to_tree (TREE_TYPE (@0),
7629 wi::mask (tree_to_uhwi (@1),
7631 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7638 --> r = .COND_FN (cond, a, b)
7642 --> r = .COND_FN (~cond, b, a). */
7644 (for uncond_op (UNCOND_UNARY)
7645 cond_op (COND_UNARY)
7647 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
7648 (with { tree op_type = TREE_TYPE (@3); }
7649 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7650 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7651 (cond_op @0 @1 @2))))
7653 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
7654 (with { tree op_type = TREE_TYPE (@3); }
7655 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7656 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7657 (cond_op (bit_not @0) @2 @1)))))
7666 r = c ? a1 op a2 : b;
7668 if the target can do it in one go. This makes the operation conditional
7669 on c, so could drop potentially-trapping arithmetic, but that's a valid
7670 simplification if the result of the operation isn't needed.
7672 Avoid speculatively generating a stand-alone vector comparison
7673 on targets that might not support them. Any target implementing
7674 conditional internal functions must support the same comparisons
7675 inside and outside a VEC_COND_EXPR. */
7677 (for uncond_op (UNCOND_BINARY)
7678 cond_op (COND_BINARY)
7680 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7681 (with { tree op_type = TREE_TYPE (@4); }
7682 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7683 && is_truth_type_for (op_type, TREE_TYPE (@0))
7685 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7687 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7688 (with { tree op_type = TREE_TYPE (@4); }
7689 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7690 && is_truth_type_for (op_type, TREE_TYPE (@0))
7692 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7694 /* Same for ternary operations. */
7695 (for uncond_op (UNCOND_TERNARY)
7696 cond_op (COND_TERNARY)
7698 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7699 (with { tree op_type = TREE_TYPE (@5); }
7700 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7701 && is_truth_type_for (op_type, TREE_TYPE (@0))
7703 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7705 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7706 (with { tree op_type = TREE_TYPE (@5); }
7707 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7708 && is_truth_type_for (op_type, TREE_TYPE (@0))
7710 (view_convert (cond_op (bit_not @0) @2 @3 @4
7711 (view_convert:op_type @1)))))))
7714 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7715 "else" value of an IFN_COND_*. */
7716 (for cond_op (COND_BINARY)
7718 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7719 (with { tree op_type = TREE_TYPE (@3); }
7720 (if (element_precision (type) == element_precision (op_type))
7721 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7723 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7724 (with { tree op_type = TREE_TYPE (@5); }
7725 (if (inverse_conditions_p (@0, @2)
7726 && element_precision (type) == element_precision (op_type))
7727 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7729 /* Same for ternary operations. */
7730 (for cond_op (COND_TERNARY)
7732 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7733 (with { tree op_type = TREE_TYPE (@4); }
7734 (if (element_precision (type) == element_precision (op_type))
7735 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7737 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7738 (with { tree op_type = TREE_TYPE (@6); }
7739 (if (inverse_conditions_p (@0, @2)
7740 && element_precision (type) == element_precision (op_type))
7741 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7743 /* Detect simplication for a conditional reduction where
7746 c = mask2 ? d + a : d
7750 c = mask1 && mask2 ? d + b : d. */
7752 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
7753 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
7755 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7758 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7759 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7761 If pointers are known not to wrap, B checks whether @1 bytes starting
7762 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7763 bytes. A is more efficiently tested as:
7765 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7767 The equivalent expression for B is given by replacing @1 with @1 - 1:
7769 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7771 @0 and @2 can be swapped in both expressions without changing the result.
7773 The folds rely on sizetype's being unsigned (which is always true)
7774 and on its being the same width as the pointer (which we have to check).
7776 The fold replaces two pointer_plus expressions, two comparisons and
7777 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7778 the best case it's a saving of two operations. The A fold retains one
7779 of the original pointer_pluses, so is a win even if both pointer_pluses
7780 are used elsewhere. The B fold is a wash if both pointer_pluses are
7781 used elsewhere, since all we end up doing is replacing a comparison with
7782 a pointer_plus. We do still apply the fold under those circumstances
7783 though, in case applying it to other conditions eventually makes one of the
7784 pointer_pluses dead. */
7785 (for ior (truth_orif truth_or bit_ior)
7788 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7789 (cmp:cs (pointer_plus@4 @2 @1) @0))
7790 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7791 && TYPE_OVERFLOW_WRAPS (sizetype)
7792 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7793 /* Calculate the rhs constant. */
7794 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7795 offset_int rhs = off * 2; }
7796 /* Always fails for negative values. */
7797 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7798 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7799 pick a canonical order. This increases the chances of using the
7800 same pointer_plus in multiple checks. */
7801 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7802 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7803 (if (cmp == LT_EXPR)
7804 (gt (convert:sizetype
7805 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7806 { swap_p ? @0 : @2; }))
7808 (gt (convert:sizetype
7809 (pointer_diff:ssizetype
7810 (pointer_plus { swap_p ? @2 : @0; }
7811 { wide_int_to_tree (sizetype, off); })
7812 { swap_p ? @0 : @2; }))
7813 { rhs_tree; })))))))))
7815 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7817 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7818 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7819 (with { int i = single_nonzero_element (@1); }
7821 (with { tree elt = vector_cst_elt (@1, i);
7822 tree elt_type = TREE_TYPE (elt);
7823 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7824 tree size = bitsize_int (elt_bits);
7825 tree pos = bitsize_int (elt_bits * i); }
7828 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7831 /* Fold reduction of a single nonzero element constructor. */
7832 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7833 (simplify (reduc (CONSTRUCTOR@0))
7834 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
7835 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7836 tree elt = ctor_single_nonzero_element (ctor); }
7838 && !HONOR_SNANS (type)
7839 && !HONOR_SIGNED_ZEROS (type))
7842 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
7843 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
7844 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
7845 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
7846 (simplify (reduc (op @0 VECTOR_CST@1))
7847 (op (reduc:type @0) (reduc:type @1))))
7850 (vec_perm @0 @1 VECTOR_CST@2)
7853 tree op0 = @0, op1 = @1, op2 = @2;
7854 machine_mode result_mode = TYPE_MODE (type);
7855 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
7857 /* Build a vector of integers from the tree mask. */
7858 vec_perm_builder builder;
7859 if (!tree_to_vec_perm_builder (&builder, op2))
7862 /* Create a vec_perm_indices for the integer vector. */
7863 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7864 bool single_arg = (op0 == op1);
7865 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7867 (if (sel.series_p (0, 1, 0, 1))
7869 (if (sel.series_p (0, 1, nelts, 1))
7875 if (sel.all_from_input_p (0))
7877 else if (sel.all_from_input_p (1))
7880 sel.rotate_inputs (1);
7882 else if (known_ge (poly_uint64 (sel[0]), nelts))
7884 std::swap (op0, op1);
7885 sel.rotate_inputs (1);
7889 tree cop0 = op0, cop1 = op1;
7890 if (TREE_CODE (op0) == SSA_NAME
7891 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7892 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7893 cop0 = gimple_assign_rhs1 (def);
7894 if (TREE_CODE (op1) == SSA_NAME
7895 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7896 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7897 cop1 = gimple_assign_rhs1 (def);
7901 (if ((TREE_CODE (cop0) == VECTOR_CST
7902 || TREE_CODE (cop0) == CONSTRUCTOR)
7903 && (TREE_CODE (cop1) == VECTOR_CST
7904 || TREE_CODE (cop1) == CONSTRUCTOR)
7905 && (t = fold_vec_perm (type, cop0, cop1, sel)))
7909 bool changed = (op0 == op1 && !single_arg);
7910 tree ins = NULL_TREE;
7913 /* See if the permutation is performing a single element
7914 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7915 in that case. But only if the vector mode is supported,
7916 otherwise this is invalid GIMPLE. */
7917 if (op_mode != BLKmode
7918 && (TREE_CODE (cop0) == VECTOR_CST
7919 || TREE_CODE (cop0) == CONSTRUCTOR
7920 || TREE_CODE (cop1) == VECTOR_CST
7921 || TREE_CODE (cop1) == CONSTRUCTOR))
7923 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7926 /* After canonicalizing the first elt to come from the
7927 first vector we only can insert the first elt from
7928 the first vector. */
7930 if ((ins = fold_read_from_vector (cop0, sel[0])))
7933 /* The above can fail for two-element vectors which always
7934 appear to insert the first element, so try inserting
7935 into the second lane as well. For more than two
7936 elements that's wasted time. */
7937 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
7939 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
7940 for (at = 0; at < encoded_nelts; ++at)
7941 if (maybe_ne (sel[at], at))
7943 if (at < encoded_nelts
7944 && (known_eq (at + 1, nelts)
7945 || sel.series_p (at + 1, 1, at + 1, 1)))
7947 if (known_lt (poly_uint64 (sel[at]), nelts))
7948 ins = fold_read_from_vector (cop0, sel[at]);
7950 ins = fold_read_from_vector (cop1, sel[at] - nelts);
7955 /* Generate a canonical form of the selector. */
7956 if (!ins && sel.encoding () != builder)
7958 /* Some targets are deficient and fail to expand a single
7959 argument permutation while still allowing an equivalent
7960 2-argument version. */
7962 if (sel.ninputs () == 2
7963 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
7964 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7967 vec_perm_indices sel2 (builder, 2, nelts);
7968 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
7969 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
7971 /* Not directly supported with either encoding,
7972 so use the preferred form. */
7973 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7975 if (!operand_equal_p (op2, oldop2, 0))
7980 (bit_insert { op0; } { ins; }
7981 { bitsize_int (at * vector_element_bits (type)); })
7983 (vec_perm { op0; } { op1; } { op2; }))))))))))
7985 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
7987 (match vec_same_elem_p
7990 (match vec_same_elem_p
7992 (if (TREE_CODE (@0) == SSA_NAME
7993 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
7995 (match vec_same_elem_p
7997 (if (uniform_vector_p (@0))))
8001 (vec_perm vec_same_elem_p@0 @0 @1)
8004 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
8006 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8007 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8008 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
8010 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8011 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8012 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
8015 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
8016 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
8017 constant which when multiplied by a power of 2 contains a unique value
8018 in the top 5 or 6 bits. This is then indexed into a table which maps it
8019 to the number of trailing zeroes. */
8020 (match (ctz_table_index @1 @2 @3)
8021 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
8023 (match (cond_expr_convert_p @0 @2 @3 @6)
8024 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
8025 (if (INTEGRAL_TYPE_P (type)
8026 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8027 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8028 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8029 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
8030 && TYPE_PRECISION (TREE_TYPE (@0))
8031 == TYPE_PRECISION (TREE_TYPE (@2))
8032 && TYPE_PRECISION (TREE_TYPE (@0))
8033 == TYPE_PRECISION (TREE_TYPE (@3))
8034 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
8035 signess when convert is truncation, but not ok for extension since
8036 it's sign_extend vs zero_extend. */
8037 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
8038 || (TYPE_UNSIGNED (TREE_TYPE (@2))
8039 == TYPE_UNSIGNED (TREE_TYPE (@3))))
8041 && single_use (@5))))
8043 (for bit_op (bit_and bit_ior bit_xor)
8044 (match (bitwise_induction_p @0 @2 @3)
8046 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
8049 (match (bitwise_induction_p @0 @2 @3)
8051 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
8053 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
8054 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
8056 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
8057 (with { auto i = wi::neg (wi::to_wide (@2)); }
8058 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
8059 (if (wi::popcount (i) == 1
8060 && (wi::to_wide (@1)) == (i - 1))
8061 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
8063 (cond (le @0 @1) @0 (bit_and @0 @1))))))
8065 /* -x & 1 -> x & 1. */
8067 (bit_and (negate @0) integer_onep@1)