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 /* Shifts by constants distribute over several binary operations,
986 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
989 (op (lshift:s @0 @1) (lshift:s @2 @1))
990 (if (INTEGRAL_TYPE_P (type)
991 && TYPE_OVERFLOW_WRAPS (type)
992 && !TYPE_SATURATING (type))
993 (lshift (op @0 @2) @1))))
995 (for op (bit_and bit_ior bit_xor)
997 (op (lshift:s @0 @1) (lshift:s @2 @1))
998 (if (INTEGRAL_TYPE_P (type))
999 (lshift (op @0 @2) @1)))
1001 (op (rshift:s @0 @1) (rshift:s @2 @1))
1002 (if (INTEGRAL_TYPE_P (type))
1003 (rshift (op @0 @2) @1))))
1005 /* Fold (1 << (C - x)) where C = precision(type) - 1
1006 into ((1 << C) >> x). */
1008 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1009 (if (INTEGRAL_TYPE_P (type)
1010 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1012 (if (TYPE_UNSIGNED (type))
1013 (rshift (lshift @0 @2) @3)
1015 { tree utype = unsigned_type_for (type); }
1016 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1018 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1020 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1021 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1022 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1023 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1024 (bit_and (convert @0)
1025 { wide_int_to_tree (type,
1026 wi::lshift (wone, wi::to_wide (@2))); }))))
1028 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1029 (for cst (INTEGER_CST VECTOR_CST)
1031 (rshift (negate:s @0) cst@1)
1032 (if (!TYPE_UNSIGNED (type)
1033 && TYPE_OVERFLOW_UNDEFINED (type))
1034 (with { tree stype = TREE_TYPE (@1);
1035 tree bt = truth_type_for (type);
1036 tree zeros = build_zero_cst (type);
1037 tree cst = NULL_TREE; }
1039 /* Handle scalar case. */
1040 (if (INTEGRAL_TYPE_P (type)
1041 /* If we apply the rule to the scalar type before vectorization
1042 we will enforce the result of the comparison being a bool
1043 which will require an extra AND on the result that will be
1044 indistinguishable from when the user did actually want 0
1045 or 1 as the result so it can't be removed. */
1046 && canonicalize_math_after_vectorization_p ()
1047 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1048 (negate (convert (gt @0 { zeros; }))))
1049 /* Handle vector case. */
1050 (if (VECTOR_INTEGER_TYPE_P (type)
1051 /* First check whether the target has the same mode for vector
1052 comparison results as it's operands do. */
1053 && TYPE_MODE (bt) == TYPE_MODE (type)
1054 /* Then check to see if the target is able to expand the comparison
1055 with the given type later on, otherwise we may ICE. */
1056 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1057 && (cst = uniform_integer_cst_p (@1)) != NULL
1058 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1059 (view_convert (gt:bt @0 { zeros; }))))))))
1061 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1063 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1064 (if (flag_associative_math
1067 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1069 (rdiv { tem; } @1)))))
1071 /* Simplify ~X & X as zero. */
1073 (bit_and:c (convert? @0) (convert? (bit_not @0)))
1074 { build_zero_cst (type); })
1076 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1078 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1079 (if (TYPE_UNSIGNED (type))
1080 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1082 (for bitop (bit_and bit_ior)
1084 /* PR35691: Transform
1085 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1086 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1088 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1089 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1090 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1091 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1092 (cmp (bit_ior @0 (convert @1)) @2)))
1094 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1095 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1097 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1098 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1099 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1100 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1101 (cmp (bit_and @0 (convert @1)) @2))))
1103 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1105 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1106 (minus (bit_xor @0 @1) @1))
1108 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1109 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1110 (minus (bit_xor @0 @1) @1)))
1112 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1114 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1115 (minus @1 (bit_xor @0 @1)))
1117 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1118 (for op (bit_ior bit_xor plus)
1120 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
1123 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
1124 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1127 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1129 (bit_ior:c (bit_xor:c @0 @1) @0)
1132 /* (a & ~b) | (a ^ b) --> a ^ b */
1134 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1137 /* (a & ~b) ^ ~a --> ~(a & b) */
1139 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1140 (bit_not (bit_and @0 @1)))
1142 /* (~a & b) ^ a --> (a | b) */
1144 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1147 /* (a | b) & ~(a ^ b) --> a & b */
1149 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1152 /* a | ~(a ^ b) --> a | ~b */
1154 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1155 (bit_ior @0 (bit_not @1)))
1157 /* (a | b) | (a &^ b) --> a | b */
1158 (for op (bit_and bit_xor)
1160 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1163 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1165 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1168 /* ~(~a & b) --> a | ~b */
1170 (bit_not (bit_and:cs (bit_not @0) @1))
1171 (bit_ior @0 (bit_not @1)))
1173 /* ~(~a | b) --> a & ~b */
1175 (bit_not (bit_ior:cs (bit_not @0) @1))
1176 (bit_and @0 (bit_not @1)))
1178 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1180 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1181 (bit_and @3 (bit_not @2)))
1183 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1185 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1188 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1190 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1191 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1193 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1195 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1196 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1198 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1200 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1201 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1202 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1205 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1206 ((A & N) + B) & M -> (A + B) & M
1207 Similarly if (N & M) == 0,
1208 ((A | N) + B) & M -> (A + B) & M
1209 and for - instead of + (or unary - instead of +)
1210 and/or ^ instead of |.
1211 If B is constant and (B & M) == 0, fold into A & M. */
1212 (for op (plus minus)
1213 (for bitop (bit_and bit_ior bit_xor)
1215 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1218 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1219 @3, @4, @1, ERROR_MARK, NULL_TREE,
1222 (convert (bit_and (op (convert:utype { pmop[0]; })
1223 (convert:utype { pmop[1]; }))
1224 (convert:utype @2))))))
1226 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1229 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1230 NULL_TREE, NULL_TREE, @1, bitop, @3,
1233 (convert (bit_and (op (convert:utype { pmop[0]; })
1234 (convert:utype { pmop[1]; }))
1235 (convert:utype @2)))))))
1237 (bit_and (op:s @0 @1) INTEGER_CST@2)
1240 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1241 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1242 NULL_TREE, NULL_TREE, pmop); }
1244 (convert (bit_and (op (convert:utype { pmop[0]; })
1245 (convert:utype { pmop[1]; }))
1246 (convert:utype @2)))))))
1247 (for bitop (bit_and bit_ior bit_xor)
1249 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1252 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1253 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1254 NULL_TREE, NULL_TREE, pmop); }
1256 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1257 (convert:utype @1)))))))
1259 /* X % Y is smaller than Y. */
1262 (cmp (trunc_mod @0 @1) @1)
1263 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1264 { constant_boolean_node (cmp == LT_EXPR, type); })))
1267 (cmp @1 (trunc_mod @0 @1))
1268 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1269 { constant_boolean_node (cmp == GT_EXPR, type); })))
1273 (bit_ior @0 integer_all_onesp@1)
1278 (bit_ior @0 integer_zerop)
1283 (bit_and @0 integer_zerop@1)
1289 (for op (bit_ior bit_xor plus)
1291 (op:c (convert? @0) (convert? (bit_not @0)))
1292 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1297 { build_zero_cst (type); })
1299 /* Canonicalize X ^ ~0 to ~X. */
1301 (bit_xor @0 integer_all_onesp@1)
1306 (bit_and @0 integer_all_onesp)
1309 /* x & x -> x, x | x -> x */
1310 (for bitop (bit_and bit_ior)
1315 /* x & C -> x if we know that x & ~C == 0. */
1318 (bit_and SSA_NAME@0 INTEGER_CST@1)
1319 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1320 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1324 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1326 (bit_not (minus (bit_not @0) @1))
1329 (bit_not (plus:c (bit_not @0) @1))
1331 /* (~X - ~Y) -> Y - X. */
1333 (minus (bit_not @0) (bit_not @1))
1334 (with { tree utype = unsigned_type_for (type); }
1335 (convert (minus (convert:utype @1) (convert:utype @0)))))
1337 /* ~(X - Y) -> ~X + Y. */
1339 (bit_not (minus:s @0 @1))
1340 (plus (bit_not @0) @1))
1342 (bit_not (plus:s @0 INTEGER_CST@1))
1343 (if ((INTEGRAL_TYPE_P (type)
1344 && TYPE_UNSIGNED (type))
1345 || (!TYPE_OVERFLOW_SANITIZED (type)
1346 && may_negate_without_overflow_p (@1)))
1347 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1350 /* ~X + Y -> (Y - X) - 1. */
1352 (plus:c (bit_not @0) @1)
1353 (if (ANY_INTEGRAL_TYPE_P (type)
1354 && TYPE_OVERFLOW_WRAPS (type)
1355 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1356 && !integer_all_onesp (@1))
1357 (plus (minus @1 @0) { build_minus_one_cst (type); })
1358 (if (INTEGRAL_TYPE_P (type)
1359 && TREE_CODE (@1) == INTEGER_CST
1360 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1362 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1365 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1367 (bit_not (rshift:s @0 @1))
1368 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1369 (rshift (bit_not! @0) @1)
1370 /* For logical right shifts, this is possible only if @0 doesn't
1371 have MSB set and the logical right shift is changed into
1372 arithmetic shift. */
1373 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1374 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1375 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1377 /* x + (x & 1) -> (x + 1) & ~1 */
1379 (plus:c @0 (bit_and:s @0 integer_onep@1))
1380 (bit_and (plus @0 @1) (bit_not @1)))
1382 /* x & ~(x & y) -> x & ~y */
1383 /* x | ~(x | y) -> x | ~y */
1384 (for bitop (bit_and bit_ior)
1386 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1387 (bitop @0 (bit_not @1))))
1389 /* (~x & y) | ~(x | y) -> ~x */
1391 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1394 /* (x | y) ^ (x | ~y) -> ~x */
1396 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1399 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1401 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1402 (bit_not (bit_xor @0 @1)))
1404 /* (~x | y) ^ (x ^ y) -> x | ~y */
1406 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1407 (bit_ior @0 (bit_not @1)))
1409 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1411 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1412 (bit_not (bit_and @0 @1)))
1414 /* (x | y) & ~x -> y & ~x */
1415 /* (x & y) | ~x -> y | ~x */
1416 (for bitop (bit_and bit_ior)
1417 rbitop (bit_ior bit_and)
1419 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1422 /* (x & y) ^ (x | y) -> x ^ y */
1424 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1427 /* (x ^ y) ^ (x | y) -> x & y */
1429 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1432 /* (x & y) + (x ^ y) -> x | y */
1433 /* (x & y) | (x ^ y) -> x | y */
1434 /* (x & y) ^ (x ^ y) -> x | y */
1435 (for op (plus bit_ior bit_xor)
1437 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1440 /* (x & y) + (x | y) -> x + y */
1442 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1445 /* (x + y) - (x | y) -> x & y */
1447 (minus (plus @0 @1) (bit_ior @0 @1))
1448 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1449 && !TYPE_SATURATING (type))
1452 /* (x + y) - (x & y) -> x | y */
1454 (minus (plus @0 @1) (bit_and @0 @1))
1455 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1456 && !TYPE_SATURATING (type))
1459 /* (x | y) - y -> (x & ~y) */
1461 (minus (bit_ior:cs @0 @1) @1)
1462 (bit_and @0 (bit_not @1)))
1464 /* (x | y) - (x ^ y) -> x & y */
1466 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1469 /* (x | y) - (x & y) -> x ^ y */
1471 (minus (bit_ior @0 @1) (bit_and @0 @1))
1474 /* (x | y) & ~(x & y) -> x ^ y */
1476 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1479 /* (x | y) & (~x ^ y) -> x & y */
1481 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1484 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1486 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1487 (bit_not (bit_xor @0 @1)))
1489 /* (~x | y) ^ (x | ~y) -> x ^ y */
1491 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1494 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1496 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1497 (nop_convert2? (bit_ior @0 @1))))
1499 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1500 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1501 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1502 && !TYPE_SATURATING (TREE_TYPE (@2)))
1503 (bit_not (convert (bit_xor @0 @1)))))
1505 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1507 (nop_convert3? (bit_ior @0 @1)))
1508 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1509 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1510 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1511 && !TYPE_SATURATING (TREE_TYPE (@2)))
1512 (bit_not (convert (bit_xor @0 @1)))))
1514 (minus (nop_convert1? (bit_and @0 @1))
1515 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1517 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1518 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1519 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1520 && !TYPE_SATURATING (TREE_TYPE (@2)))
1521 (bit_not (convert (bit_xor @0 @1)))))
1523 /* ~x & ~y -> ~(x | y)
1524 ~x | ~y -> ~(x & y) */
1525 (for op (bit_and bit_ior)
1526 rop (bit_ior bit_and)
1528 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1529 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1530 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1531 (bit_not (rop (convert @0) (convert @1))))))
1533 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1534 with a constant, and the two constants have no bits in common,
1535 we should treat this as a BIT_IOR_EXPR since this may produce more
1537 (for op (bit_xor plus)
1539 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1540 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1541 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1542 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1543 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1544 (bit_ior (convert @4) (convert @5)))))
1546 /* (X | Y) ^ X -> Y & ~ X*/
1548 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1549 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1550 (convert (bit_and @1 (bit_not @0)))))
1552 /* Convert ~X ^ ~Y to X ^ Y. */
1554 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1555 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1556 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1557 (bit_xor (convert @0) (convert @1))))
1559 /* Convert ~X ^ C to X ^ ~C. */
1561 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1562 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1563 (bit_xor (convert @0) (bit_not @1))))
1565 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1566 (for opo (bit_and bit_xor)
1567 opi (bit_xor bit_and)
1569 (opo:c (opi:cs @0 @1) @1)
1570 (bit_and (bit_not @0) @1)))
1572 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1573 operands are another bit-wise operation with a common input. If so,
1574 distribute the bit operations to save an operation and possibly two if
1575 constants are involved. For example, convert
1576 (A | B) & (A | C) into A | (B & C)
1577 Further simplification will occur if B and C are constants. */
1578 (for op (bit_and bit_ior bit_xor)
1579 rop (bit_ior bit_and bit_and)
1581 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1582 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1583 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1584 (rop (convert @0) (op (convert @1) (convert @2))))))
1586 /* Some simple reassociation for bit operations, also handled in reassoc. */
1587 /* (X & Y) & Y -> X & Y
1588 (X | Y) | Y -> X | Y */
1589 (for op (bit_and bit_ior)
1591 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1593 /* (X ^ Y) ^ Y -> X */
1595 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1597 /* (X & Y) & (X & Z) -> (X & Y) & Z
1598 (X | Y) | (X | Z) -> (X | Y) | Z */
1599 (for op (bit_and bit_ior)
1601 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1602 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1603 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1604 (if (single_use (@5) && single_use (@6))
1605 (op @3 (convert @2))
1606 (if (single_use (@3) && single_use (@4))
1607 (op (convert @1) @5))))))
1608 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1610 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1611 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1612 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1613 (bit_xor (convert @1) (convert @2))))
1615 /* Convert abs (abs (X)) into abs (X).
1616 also absu (absu (X)) into absu (X). */
1622 (absu (convert@2 (absu@1 @0)))
1623 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1626 /* Convert abs[u] (-X) -> abs[u] (X). */
1635 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1637 (abs tree_expr_nonnegative_p@0)
1641 (absu tree_expr_nonnegative_p@0)
1644 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1646 (mult:c (nop_convert1?
1647 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1650 (if (INTEGRAL_TYPE_P (type)
1651 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1652 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1653 (if (TYPE_UNSIGNED (type))
1660 /* A few cases of fold-const.cc negate_expr_p predicate. */
1661 (match negate_expr_p
1663 (if ((INTEGRAL_TYPE_P (type)
1664 && TYPE_UNSIGNED (type))
1665 || (!TYPE_OVERFLOW_SANITIZED (type)
1666 && may_negate_without_overflow_p (t)))))
1667 (match negate_expr_p
1669 (match negate_expr_p
1671 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1672 (match negate_expr_p
1674 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1675 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1677 (match negate_expr_p
1679 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1680 (match negate_expr_p
1682 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1683 || (FLOAT_TYPE_P (type)
1684 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1685 && !HONOR_SIGNED_ZEROS (type)))))
1687 /* (-A) * (-B) -> A * B */
1689 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1690 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1691 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1692 (mult (convert @0) (convert (negate @1)))))
1694 /* -(A + B) -> (-B) - A. */
1696 (negate (plus:c @0 negate_expr_p@1))
1697 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1698 && !HONOR_SIGNED_ZEROS (type))
1699 (minus (negate @1) @0)))
1701 /* -(A - B) -> B - A. */
1703 (negate (minus @0 @1))
1704 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1705 || (FLOAT_TYPE_P (type)
1706 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1707 && !HONOR_SIGNED_ZEROS (type)))
1710 (negate (pointer_diff @0 @1))
1711 (if (TYPE_OVERFLOW_UNDEFINED (type))
1712 (pointer_diff @1 @0)))
1714 /* A - B -> A + (-B) if B is easily negatable. */
1716 (minus @0 negate_expr_p@1)
1717 (if (!FIXED_POINT_TYPE_P (type))
1718 (plus @0 (negate @1))))
1720 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1722 (negate (mult:c@0 @1 negate_expr_p@2))
1723 (if (! TYPE_UNSIGNED (type)
1724 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1726 (mult @1 (negate @2))))
1729 (negate (rdiv@0 @1 negate_expr_p@2))
1730 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1732 (rdiv @1 (negate @2))))
1735 (negate (rdiv@0 negate_expr_p@1 @2))
1736 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1738 (rdiv (negate @1) @2)))
1740 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1742 (negate (convert? (rshift @0 INTEGER_CST@1)))
1743 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1744 && wi::to_wide (@1) == element_precision (type) - 1)
1745 (with { tree stype = TREE_TYPE (@0);
1746 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1747 : unsigned_type_for (stype); }
1748 (if (VECTOR_TYPE_P (type))
1749 (view_convert (rshift (view_convert:ntype @0) @1))
1750 (convert (rshift (convert:ntype @0) @1))))))
1752 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1754 For bitwise binary operations apply operand conversions to the
1755 binary operation result instead of to the operands. This allows
1756 to combine successive conversions and bitwise binary operations.
1757 We combine the above two cases by using a conditional convert. */
1758 (for bitop (bit_and bit_ior bit_xor)
1760 (bitop (convert@2 @0) (convert?@3 @1))
1761 (if (((TREE_CODE (@1) == INTEGER_CST
1762 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1763 && (int_fits_type_p (@1, TREE_TYPE (@0))
1764 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1765 || types_match (@0, @1))
1766 /* ??? This transform conflicts with fold-const.cc doing
1767 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1768 constants (if x has signed type, the sign bit cannot be set
1769 in c). This folds extension into the BIT_AND_EXPR.
1770 Restrict it to GIMPLE to avoid endless recursions. */
1771 && (bitop != BIT_AND_EXPR || GIMPLE)
1772 && (/* That's a good idea if the conversion widens the operand, thus
1773 after hoisting the conversion the operation will be narrower.
1774 It is also a good if the conversion is a nop as moves the
1775 conversion to one side; allowing for combining of the conversions. */
1776 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1777 /* The conversion check for being a nop can only be done at the gimple
1778 level as fold_binary has some re-association code which can conflict
1779 with this if there is a "constant" which is not a full INTEGER_CST. */
1780 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1781 /* It's also a good idea if the conversion is to a non-integer
1783 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1784 /* Or if the precision of TO is not the same as the precision
1786 || !type_has_mode_precision_p (type)
1787 /* In GIMPLE, getting rid of 2 conversions for one new results
1790 && TREE_CODE (@1) != INTEGER_CST
1791 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1793 && single_use (@3))))
1794 (convert (bitop @0 (convert @1)))))
1795 /* In GIMPLE, getting rid of 2 conversions for one new results
1798 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1800 && TREE_CODE (@1) != INTEGER_CST
1801 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1802 && types_match (type, @0))
1803 (bitop @0 (convert @1)))))
1805 (for bitop (bit_and bit_ior)
1806 rbitop (bit_ior bit_and)
1807 /* (x | y) & x -> x */
1808 /* (x & y) | x -> x */
1810 (bitop:c (rbitop:c @0 @1) @0)
1812 /* (~x | y) & x -> x & y */
1813 /* (~x & y) | x -> x | y */
1815 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1818 /* ((x | y) & z) | x -> (z & y) | x */
1820 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1821 (bit_ior (bit_and @2 @1) @0))
1823 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1825 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1826 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1828 /* Combine successive equal operations with constants. */
1829 (for bitop (bit_and bit_ior bit_xor)
1831 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1832 (if (!CONSTANT_CLASS_P (@0))
1833 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1834 folded to a constant. */
1835 (bitop @0 (bitop @1 @2))
1836 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1837 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1838 the values involved are such that the operation can't be decided at
1839 compile time. Try folding one of @0 or @1 with @2 to see whether
1840 that combination can be decided at compile time.
1842 Keep the existing form if both folds fail, to avoid endless
1844 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1846 (bitop @1 { cst1; })
1847 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1849 (bitop @0 { cst2; }))))))))
1851 /* Try simple folding for X op !X, and X op X with the help
1852 of the truth_valued_p and logical_inverted_value predicates. */
1853 (match truth_valued_p
1855 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1856 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1857 (match truth_valued_p
1859 (match truth_valued_p
1862 (match (logical_inverted_value @0)
1864 (match (logical_inverted_value @0)
1865 (bit_not truth_valued_p@0))
1866 (match (logical_inverted_value @0)
1867 (eq @0 integer_zerop))
1868 (match (logical_inverted_value @0)
1869 (ne truth_valued_p@0 integer_truep))
1870 (match (logical_inverted_value @0)
1871 (bit_xor truth_valued_p@0 integer_truep))
1875 (bit_and:c @0 (logical_inverted_value @0))
1876 { build_zero_cst (type); })
1877 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1878 (for op (bit_ior bit_xor)
1880 (op:c truth_valued_p@0 (logical_inverted_value @0))
1881 { constant_boolean_node (true, type); }))
1882 /* X ==/!= !X is false/true. */
1885 (op:c truth_valued_p@0 (logical_inverted_value @0))
1886 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1890 (bit_not (bit_not @0))
1893 (match zero_one_valued_p
1895 (if (INTEGRAL_TYPE_P (type) && tree_nonzero_bits (@0) == 1)))
1896 (match zero_one_valued_p
1899 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
1901 (mult zero_one_valued_p@0 zero_one_valued_p@1)
1902 (if (INTEGRAL_TYPE_P (type))
1905 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
1907 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
1908 (if (INTEGRAL_TYPE_P (type)
1909 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1910 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
1911 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1912 (mult (convert @0) @1)))
1914 /* Narrow integer multiplication by a zero_one_valued_p operand.
1915 Multiplication by [0,1] is guaranteed not to overflow. */
1917 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
1918 (if (INTEGRAL_TYPE_P (type)
1919 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1920 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
1921 (mult (convert @1) (convert @2))))
1923 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
1924 Check that the shift is well-defined (C is less than TYPE_PRECISION)
1925 as some targets (such as x86's SSE) may return zero for larger C. */
1927 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
1928 (if (tree_fits_shwi_p (@1)
1929 && tree_to_shwi (@1) > 0
1930 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
1933 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
1934 Check that the shift is well-defined (C is less than TYPE_PRECISION)
1935 as some targets (such as x86's SSE) may return zero for larger C. */
1937 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
1938 (if (tree_fits_shwi_p (@1)
1939 && tree_to_shwi (@1) > 0
1940 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
1943 /* Convert ~ (-A) to A - 1. */
1945 (bit_not (convert? (negate @0)))
1946 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1947 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1948 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1950 /* Convert - (~A) to A + 1. */
1952 (negate (nop_convert? (bit_not @0)))
1953 (plus (view_convert @0) { build_each_one_cst (type); }))
1955 /* (a & b) ^ (a == b) -> !(a | b) */
1956 /* (a & b) == (a ^ b) -> !(a | b) */
1957 (for first_op (bit_xor eq)
1958 second_op (eq bit_xor)
1960 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
1961 (bit_not (bit_ior @0 @1))))
1963 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1965 (bit_not (convert? (minus @0 integer_each_onep)))
1966 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1967 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1968 (convert (negate @0))))
1970 (bit_not (convert? (plus @0 integer_all_onesp)))
1971 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1972 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1973 (convert (negate @0))))
1975 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1977 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1978 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1979 (convert (bit_xor @0 (bit_not @1)))))
1981 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1982 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1983 (convert (bit_xor @0 @1))))
1985 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1987 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1988 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1989 (bit_not (bit_xor (view_convert @0) @1))))
1991 /* ~(a ^ b) is a == b for truth valued a and b. */
1993 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
1994 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1995 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1996 (convert (eq @0 @1))))
1998 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2000 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2001 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2003 /* Fold A - (A & B) into ~B & A. */
2005 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2006 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2007 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2008 (convert (bit_and (bit_not @1) @0))))
2010 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2011 (if (!canonicalize_math_p ())
2012 (for cmp (gt lt ge le)
2014 (mult (convert (cmp @0 @1)) @2)
2015 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
2017 /* For integral types with undefined overflow and C != 0 fold
2018 x * C EQ/NE y * C into x EQ/NE y. */
2021 (cmp (mult:c @0 @1) (mult:c @2 @1))
2022 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2023 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2024 && tree_expr_nonzero_p (@1))
2027 /* For integral types with wrapping overflow and C odd fold
2028 x * C EQ/NE y * C into x EQ/NE y. */
2031 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2032 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2033 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2034 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2037 /* For integral types with undefined overflow and C != 0 fold
2038 x * C RELOP y * C into:
2040 x RELOP y for nonnegative C
2041 y RELOP x for negative C */
2042 (for cmp (lt gt le ge)
2044 (cmp (mult:c @0 @1) (mult:c @2 @1))
2045 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2046 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2047 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2049 (if (TREE_CODE (@1) == INTEGER_CST
2050 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2053 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2057 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2058 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2059 && TYPE_UNSIGNED (TREE_TYPE (@0))
2060 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2061 && (wi::to_wide (@2)
2062 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2063 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2064 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2066 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2067 (for cmp (simple_comparison)
2069 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2070 (if (element_precision (@3) >= element_precision (@0)
2071 && types_match (@0, @1))
2072 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2073 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2075 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2078 tree utype = unsigned_type_for (TREE_TYPE (@0));
2080 (cmp (convert:utype @1) (convert:utype @0)))))
2081 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2082 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2086 tree utype = unsigned_type_for (TREE_TYPE (@0));
2088 (cmp (convert:utype @0) (convert:utype @1)))))))))
2090 /* X / C1 op C2 into a simple range test. */
2091 (for cmp (simple_comparison)
2093 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2094 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2095 && integer_nonzerop (@1)
2096 && !TREE_OVERFLOW (@1)
2097 && !TREE_OVERFLOW (@2))
2098 (with { tree lo, hi; bool neg_overflow;
2099 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2102 (if (code == LT_EXPR || code == GE_EXPR)
2103 (if (TREE_OVERFLOW (lo))
2104 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2105 (if (code == LT_EXPR)
2108 (if (code == LE_EXPR || code == GT_EXPR)
2109 (if (TREE_OVERFLOW (hi))
2110 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2111 (if (code == LE_EXPR)
2115 { build_int_cst (type, code == NE_EXPR); })
2116 (if (code == EQ_EXPR && !hi)
2118 (if (code == EQ_EXPR && !lo)
2120 (if (code == NE_EXPR && !hi)
2122 (if (code == NE_EXPR && !lo)
2125 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2129 tree etype = range_check_type (TREE_TYPE (@0));
2132 hi = fold_convert (etype, hi);
2133 lo = fold_convert (etype, lo);
2134 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2137 (if (etype && hi && !TREE_OVERFLOW (hi))
2138 (if (code == EQ_EXPR)
2139 (le (minus (convert:etype @0) { lo; }) { hi; })
2140 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2142 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2143 (for op (lt le ge gt)
2145 (op (plus:c @0 @2) (plus:c @1 @2))
2146 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2147 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2150 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2151 when C is an unsigned integer constant with only the MSB set, and X and
2152 Y have types of equal or lower integer conversion rank than C's. */
2153 (for op (lt le ge gt)
2155 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2156 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2157 && TYPE_UNSIGNED (TREE_TYPE (@0))
2158 && wi::only_sign_bit_p (wi::to_wide (@0)))
2159 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2160 (op (convert:stype @1) (convert:stype @2))))))
2162 /* For equality and subtraction, this is also true with wrapping overflow. */
2163 (for op (eq ne minus)
2165 (op (plus:c @0 @2) (plus:c @1 @2))
2166 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2167 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2168 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2171 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2172 (for op (lt le ge gt)
2174 (op (minus @0 @2) (minus @1 @2))
2175 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2176 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2178 /* For equality and subtraction, this is also true with wrapping overflow. */
2179 (for op (eq ne minus)
2181 (op (minus @0 @2) (minus @1 @2))
2182 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2183 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2184 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2186 /* And for pointers... */
2187 (for op (simple_comparison)
2189 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2190 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2193 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2194 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2195 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2196 (pointer_diff @0 @1)))
2198 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2199 (for op (lt le ge gt)
2201 (op (minus @2 @0) (minus @2 @1))
2202 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2203 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2205 /* For equality and subtraction, this is also true with wrapping overflow. */
2206 (for op (eq ne minus)
2208 (op (minus @2 @0) (minus @2 @1))
2209 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2210 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2211 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2213 /* And for pointers... */
2214 (for op (simple_comparison)
2216 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2217 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2220 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2221 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2222 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2223 (pointer_diff @1 @0)))
2225 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2226 (for op (lt le gt ge)
2228 (op:c (plus:c@2 @0 @1) @1)
2229 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2230 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2231 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2232 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2233 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2234 /* For equality, this is also true with wrapping overflow. */
2237 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2238 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2239 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2240 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2241 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2242 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2243 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2244 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2246 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2247 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2248 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2249 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2250 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2252 /* X - Y < X is the same as Y > 0 when there is no overflow.
2253 For equality, this is also true with wrapping overflow. */
2254 (for op (simple_comparison)
2256 (op:c @0 (minus@2 @0 @1))
2257 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2258 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2259 || ((op == EQ_EXPR || op == NE_EXPR)
2260 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2261 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2262 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2265 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2266 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2270 (cmp (trunc_div @0 @1) integer_zerop)
2271 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2272 /* Complex ==/!= is allowed, but not </>=. */
2273 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2274 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2277 /* X == C - X can never be true if C is odd. */
2280 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2281 (if (TREE_INT_CST_LOW (@1) & 1)
2282 { constant_boolean_node (cmp == NE_EXPR, type); })))
2284 /* Arguments on which one can call get_nonzero_bits to get the bits
2286 (match with_possible_nonzero_bits
2288 (match with_possible_nonzero_bits
2290 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2291 /* Slightly extended version, do not make it recursive to keep it cheap. */
2292 (match (with_possible_nonzero_bits2 @0)
2293 with_possible_nonzero_bits@0)
2294 (match (with_possible_nonzero_bits2 @0)
2295 (bit_and:c with_possible_nonzero_bits@0 @2))
2297 /* Same for bits that are known to be set, but we do not have
2298 an equivalent to get_nonzero_bits yet. */
2299 (match (with_certain_nonzero_bits2 @0)
2301 (match (with_certain_nonzero_bits2 @0)
2302 (bit_ior @1 INTEGER_CST@0))
2304 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2307 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2308 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2309 { constant_boolean_node (cmp == NE_EXPR, type); })))
2311 /* ((X inner_op C0) outer_op C1)
2312 With X being a tree where value_range has reasoned certain bits to always be
2313 zero throughout its computed value range,
2314 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2315 where zero_mask has 1's for all bits that are sure to be 0 in
2317 if (inner_op == '^') C0 &= ~C1;
2318 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2319 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2321 (for inner_op (bit_ior bit_xor)
2322 outer_op (bit_xor bit_ior)
2325 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2329 wide_int zero_mask_not;
2333 if (TREE_CODE (@2) == SSA_NAME)
2334 zero_mask_not = get_nonzero_bits (@2);
2338 if (inner_op == BIT_XOR_EXPR)
2340 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2341 cst_emit = C0 | wi::to_wide (@1);
2345 C0 = wi::to_wide (@0);
2346 cst_emit = C0 ^ wi::to_wide (@1);
2349 (if (!fail && (C0 & zero_mask_not) == 0)
2350 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2351 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2352 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2354 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2356 (pointer_plus (pointer_plus:s @0 @1) @3)
2357 (pointer_plus @0 (plus @1 @3)))
2360 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2361 (convert:type (pointer_plus @0 (plus @1 @3))))
2368 tem4 = (unsigned long) tem3;
2373 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2374 /* Conditionally look through a sign-changing conversion. */
2375 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2376 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2377 || (GENERIC && type == TREE_TYPE (@1))))
2380 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2381 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2385 tem = (sizetype) ptr;
2389 and produce the simpler and easier to analyze with respect to alignment
2390 ... = ptr & ~algn; */
2392 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2393 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2394 (bit_and @0 { algn; })))
2396 /* Try folding difference of addresses. */
2398 (minus (convert ADDR_EXPR@0) (convert @1))
2399 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2400 (with { poly_int64 diff; }
2401 (if (ptr_difference_const (@0, @1, &diff))
2402 { build_int_cst_type (type, diff); }))))
2404 (minus (convert @0) (convert ADDR_EXPR@1))
2405 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2406 (with { poly_int64 diff; }
2407 (if (ptr_difference_const (@0, @1, &diff))
2408 { build_int_cst_type (type, diff); }))))
2410 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2411 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2412 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2413 (with { poly_int64 diff; }
2414 (if (ptr_difference_const (@0, @1, &diff))
2415 { build_int_cst_type (type, diff); }))))
2417 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2418 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2419 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2420 (with { poly_int64 diff; }
2421 (if (ptr_difference_const (@0, @1, &diff))
2422 { build_int_cst_type (type, diff); }))))
2424 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2426 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2427 (with { poly_int64 diff; }
2428 (if (ptr_difference_const (@0, @2, &diff))
2429 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2431 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2434 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2435 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2436 (if (ptr_difference_const (@0, @2, &diff))
2437 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2439 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2441 (convert (pointer_diff @0 INTEGER_CST@1))
2442 (if (POINTER_TYPE_P (type))
2443 { build_fold_addr_expr_with_type
2444 (build2 (MEM_REF, char_type_node, @0,
2445 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2448 /* If arg0 is derived from the address of an object or function, we may
2449 be able to fold this expression using the object or function's
2452 (bit_and (convert? @0) INTEGER_CST@1)
2453 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2454 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2458 unsigned HOST_WIDE_INT bitpos;
2459 get_pointer_alignment_1 (@0, &align, &bitpos);
2461 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2462 { wide_int_to_tree (type, (wi::to_wide (@1)
2463 & (bitpos / BITS_PER_UNIT))); }))))
2467 (if (INTEGRAL_TYPE_P (type)
2468 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2472 (if (INTEGRAL_TYPE_P (type)
2473 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2475 /* x > y && x != XXX_MIN --> x > y
2476 x > y && x == XXX_MIN --> false . */
2479 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2481 (if (eqne == EQ_EXPR)
2482 { constant_boolean_node (false, type); })
2483 (if (eqne == NE_EXPR)
2487 /* x < y && x != XXX_MAX --> x < y
2488 x < y && x == XXX_MAX --> false. */
2491 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2493 (if (eqne == EQ_EXPR)
2494 { constant_boolean_node (false, type); })
2495 (if (eqne == NE_EXPR)
2499 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2501 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2504 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2506 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2509 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2511 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2514 /* x <= y || x != XXX_MIN --> true. */
2516 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2517 { constant_boolean_node (true, type); })
2519 /* x <= y || x == XXX_MIN --> x <= y. */
2521 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2524 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2526 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2529 /* x >= y || x != XXX_MAX --> true
2530 x >= y || x == XXX_MAX --> x >= y. */
2533 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2535 (if (eqne == EQ_EXPR)
2537 (if (eqne == NE_EXPR)
2538 { constant_boolean_node (true, type); }))))
2540 /* y == XXX_MIN || x < y --> x <= y - 1 */
2542 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2543 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2544 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2545 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2547 /* y != XXX_MIN && x >= y --> x > y - 1 */
2549 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2550 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2551 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2552 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2554 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2555 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2558 (for code2 (eq ne lt gt le ge)
2560 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2563 int cmp = tree_int_cst_compare (@1, @2);
2567 case EQ_EXPR: val = (cmp == 0); break;
2568 case NE_EXPR: val = (cmp != 0); break;
2569 case LT_EXPR: val = (cmp < 0); break;
2570 case GT_EXPR: val = (cmp > 0); break;
2571 case LE_EXPR: val = (cmp <= 0); break;
2572 case GE_EXPR: val = (cmp >= 0); break;
2573 default: gcc_unreachable ();
2577 (if (code1 == EQ_EXPR && val) @3)
2578 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2579 (if (code1 == NE_EXPR && !val) @4))))))
2581 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2583 (for code1 (lt le gt ge)
2584 (for code2 (lt le gt ge)
2586 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2589 int cmp = tree_int_cst_compare (@1, @2);
2592 /* Choose the more restrictive of two < or <= comparisons. */
2593 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2594 && (code2 == LT_EXPR || code2 == LE_EXPR))
2595 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2598 /* Likewise chose the more restrictive of two > or >= comparisons. */
2599 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2600 && (code2 == GT_EXPR || code2 == GE_EXPR))
2601 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2604 /* Check for singleton ranges. */
2606 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2607 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2609 /* Check for disjoint ranges. */
2611 && (code1 == LT_EXPR || code1 == LE_EXPR)
2612 && (code2 == GT_EXPR || code2 == GE_EXPR))
2613 { constant_boolean_node (false, type); })
2615 && (code1 == GT_EXPR || code1 == GE_EXPR)
2616 && (code2 == LT_EXPR || code2 == LE_EXPR))
2617 { constant_boolean_node (false, type); })
2620 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2621 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2624 (for code2 (eq ne lt gt le ge)
2626 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2629 int cmp = tree_int_cst_compare (@1, @2);
2633 case EQ_EXPR: val = (cmp == 0); break;
2634 case NE_EXPR: val = (cmp != 0); break;
2635 case LT_EXPR: val = (cmp < 0); break;
2636 case GT_EXPR: val = (cmp > 0); break;
2637 case LE_EXPR: val = (cmp <= 0); break;
2638 case GE_EXPR: val = (cmp >= 0); break;
2639 default: gcc_unreachable ();
2643 (if (code1 == EQ_EXPR && val) @4)
2644 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2645 (if (code1 == NE_EXPR && !val) @3))))))
2647 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2649 (for code1 (lt le gt ge)
2650 (for code2 (lt le gt ge)
2652 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2655 int cmp = tree_int_cst_compare (@1, @2);
2658 /* Choose the more restrictive of two < or <= comparisons. */
2659 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2660 && (code2 == LT_EXPR || code2 == LE_EXPR))
2661 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2664 /* Likewise chose the more restrictive of two > or >= comparisons. */
2665 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2666 && (code2 == GT_EXPR || code2 == GE_EXPR))
2667 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2670 /* Check for singleton ranges. */
2672 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2673 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2675 /* Check for disjoint ranges. */
2677 && (code1 == LT_EXPR || code1 == LE_EXPR)
2678 && (code2 == GT_EXPR || code2 == GE_EXPR))
2679 { constant_boolean_node (true, type); })
2681 && (code1 == GT_EXPR || code1 == GE_EXPR)
2682 && (code2 == LT_EXPR || code2 == LE_EXPR))
2683 { constant_boolean_node (true, type); })
2686 /* We can't reassociate at all for saturating types. */
2687 (if (!TYPE_SATURATING (type))
2689 /* Contract negates. */
2690 /* A + (-B) -> A - B */
2692 (plus:c @0 (convert? (negate @1)))
2693 /* Apply STRIP_NOPS on the negate. */
2694 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2695 && !TYPE_OVERFLOW_SANITIZED (type))
2699 if (INTEGRAL_TYPE_P (type)
2700 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2701 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2703 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2704 /* A - (-B) -> A + B */
2706 (minus @0 (convert? (negate @1)))
2707 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2708 && !TYPE_OVERFLOW_SANITIZED (type))
2712 if (INTEGRAL_TYPE_P (type)
2713 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2714 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2716 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2718 Sign-extension is ok except for INT_MIN, which thankfully cannot
2719 happen without overflow. */
2721 (negate (convert (negate @1)))
2722 (if (INTEGRAL_TYPE_P (type)
2723 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2724 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2725 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2726 && !TYPE_OVERFLOW_SANITIZED (type)
2727 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2730 (negate (convert negate_expr_p@1))
2731 (if (SCALAR_FLOAT_TYPE_P (type)
2732 && ((DECIMAL_FLOAT_TYPE_P (type)
2733 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2734 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2735 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2736 (convert (negate @1))))
2738 (negate (nop_convert? (negate @1)))
2739 (if (!TYPE_OVERFLOW_SANITIZED (type)
2740 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2743 /* We can't reassociate floating-point unless -fassociative-math
2744 or fixed-point plus or minus because of saturation to +-Inf. */
2745 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2746 && !FIXED_POINT_TYPE_P (type))
2748 /* Match patterns that allow contracting a plus-minus pair
2749 irrespective of overflow issues. */
2750 /* (A +- B) - A -> +- B */
2751 /* (A +- B) -+ B -> A */
2752 /* A - (A +- B) -> -+ B */
2753 /* A +- (B -+ A) -> +- B */
2755 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2758 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2759 (if (!ANY_INTEGRAL_TYPE_P (type)
2760 || TYPE_OVERFLOW_WRAPS (type))
2761 (negate (view_convert @1))
2762 (view_convert (negate @1))))
2764 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2767 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2768 (if (!ANY_INTEGRAL_TYPE_P (type)
2769 || TYPE_OVERFLOW_WRAPS (type))
2770 (negate (view_convert @1))
2771 (view_convert (negate @1))))
2773 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2775 /* (A +- B) + (C - A) -> C +- B */
2776 /* (A + B) - (A - C) -> B + C */
2777 /* More cases are handled with comparisons. */
2779 (plus:c (plus:c @0 @1) (minus @2 @0))
2782 (plus:c (minus @0 @1) (minus @2 @0))
2785 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2786 (if (TYPE_OVERFLOW_UNDEFINED (type)
2787 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2788 (pointer_diff @2 @1)))
2790 (minus (plus:c @0 @1) (minus @0 @2))
2793 /* (A +- CST1) +- CST2 -> A + CST3
2794 Use view_convert because it is safe for vectors and equivalent for
2796 (for outer_op (plus minus)
2797 (for inner_op (plus minus)
2798 neg_inner_op (minus plus)
2800 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2802 /* If one of the types wraps, use that one. */
2803 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2804 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2805 forever if something doesn't simplify into a constant. */
2806 (if (!CONSTANT_CLASS_P (@0))
2807 (if (outer_op == PLUS_EXPR)
2808 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2809 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2810 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2811 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2812 (if (outer_op == PLUS_EXPR)
2813 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2814 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2815 /* If the constant operation overflows we cannot do the transform
2816 directly as we would introduce undefined overflow, for example
2817 with (a - 1) + INT_MIN. */
2818 (if (types_match (type, @0))
2819 (with { tree cst = const_binop (outer_op == inner_op
2820 ? PLUS_EXPR : MINUS_EXPR,
2822 (if (cst && !TREE_OVERFLOW (cst))
2823 (inner_op @0 { cst; } )
2824 /* X+INT_MAX+1 is X-INT_MIN. */
2825 (if (INTEGRAL_TYPE_P (type) && cst
2826 && wi::to_wide (cst) == wi::min_value (type))
2827 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2828 /* Last resort, use some unsigned type. */
2829 (with { tree utype = unsigned_type_for (type); }
2831 (view_convert (inner_op
2832 (view_convert:utype @0)
2834 { drop_tree_overflow (cst); }))))))))))))))
2836 /* (CST1 - A) +- CST2 -> CST3 - A */
2837 (for outer_op (plus minus)
2839 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2840 /* If one of the types wraps, use that one. */
2841 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2842 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2843 forever if something doesn't simplify into a constant. */
2844 (if (!CONSTANT_CLASS_P (@0))
2845 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2846 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2847 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2848 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2849 (if (types_match (type, @0))
2850 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2851 (if (cst && !TREE_OVERFLOW (cst))
2852 (minus { cst; } @0))))))))
2854 /* CST1 - (CST2 - A) -> CST3 + A
2855 Use view_convert because it is safe for vectors and equivalent for
2858 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2859 /* If one of the types wraps, use that one. */
2860 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2861 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2862 forever if something doesn't simplify into a constant. */
2863 (if (!CONSTANT_CLASS_P (@0))
2864 (plus (view_convert @0) (minus @1 (view_convert @2))))
2865 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2866 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2867 (view_convert (plus @0 (minus (view_convert @1) @2)))
2868 (if (types_match (type, @0))
2869 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2870 (if (cst && !TREE_OVERFLOW (cst))
2871 (plus { cst; } @0)))))))
2873 /* ((T)(A)) + CST -> (T)(A + CST) */
2876 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2877 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2878 && TREE_CODE (type) == INTEGER_TYPE
2879 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2880 && int_fits_type_p (@1, TREE_TYPE (@0)))
2881 /* Perform binary operation inside the cast if the constant fits
2882 and (A + CST)'s range does not overflow. */
2885 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2886 max_ovf = wi::OVF_OVERFLOW;
2887 tree inner_type = TREE_TYPE (@0);
2890 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2891 TYPE_SIGN (inner_type));
2894 if (get_global_range_query ()->range_of_expr (vr, @0)
2895 && vr.kind () == VR_RANGE)
2897 wide_int wmin0 = vr.lower_bound ();
2898 wide_int wmax0 = vr.upper_bound ();
2899 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2900 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2903 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2904 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2908 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2910 (for op (plus minus)
2912 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2913 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2914 && TREE_CODE (type) == INTEGER_TYPE
2915 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2916 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2917 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2918 && TYPE_OVERFLOW_WRAPS (type))
2919 (plus (convert @0) (op @2 (convert @1))))))
2922 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2923 to a simple value. */
2924 (for op (plus minus)
2926 (op (convert @0) (convert @1))
2927 (if (INTEGRAL_TYPE_P (type)
2928 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2929 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2930 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2931 && !TYPE_OVERFLOW_TRAPS (type)
2932 && !TYPE_OVERFLOW_SANITIZED (type))
2933 (convert (op! @0 @1)))))
2937 (plus:c (bit_not @0) @0)
2938 (if (!TYPE_OVERFLOW_TRAPS (type))
2939 { build_all_ones_cst (type); }))
2943 (plus (convert? (bit_not @0)) integer_each_onep)
2944 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2945 (negate (convert @0))))
2949 (minus (convert? (negate @0)) integer_each_onep)
2950 (if (!TYPE_OVERFLOW_TRAPS (type)
2951 && TREE_CODE (type) != COMPLEX_TYPE
2952 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2953 (bit_not (convert @0))))
2957 (minus integer_all_onesp @0)
2958 (if (TREE_CODE (type) != COMPLEX_TYPE)
2961 /* (T)(P + A) - (T)P -> (T) A */
2963 (minus (convert (plus:c @@0 @1))
2965 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2966 /* For integer types, if A has a smaller type
2967 than T the result depends on the possible
2969 E.g. T=size_t, A=(unsigned)429497295, P>0.
2970 However, if an overflow in P + A would cause
2971 undefined behavior, we can assume that there
2973 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2974 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2977 (minus (convert (pointer_plus @@0 @1))
2979 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2980 /* For pointer types, if the conversion of A to the
2981 final type requires a sign- or zero-extension,
2982 then we have to punt - it is not defined which
2984 || (POINTER_TYPE_P (TREE_TYPE (@0))
2985 && TREE_CODE (@1) == INTEGER_CST
2986 && tree_int_cst_sign_bit (@1) == 0))
2989 (pointer_diff (pointer_plus @@0 @1) @0)
2990 /* The second argument of pointer_plus must be interpreted as signed, and
2991 thus sign-extended if necessary. */
2992 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2993 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2994 second arg is unsigned even when we need to consider it as signed,
2995 we don't want to diagnose overflow here. */
2996 (convert (view_convert:stype @1))))
2998 /* (T)P - (T)(P + A) -> -(T) A */
3000 (minus (convert? @0)
3001 (convert (plus:c @@0 @1)))
3002 (if (INTEGRAL_TYPE_P (type)
3003 && TYPE_OVERFLOW_UNDEFINED (type)
3004 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3005 (with { tree utype = unsigned_type_for (type); }
3006 (convert (negate (convert:utype @1))))
3007 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3008 /* For integer types, if A has a smaller type
3009 than T the result depends on the possible
3011 E.g. T=size_t, A=(unsigned)429497295, P>0.
3012 However, if an overflow in P + A would cause
3013 undefined behavior, we can assume that there
3015 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3016 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3017 (negate (convert @1)))))
3020 (convert (pointer_plus @@0 @1)))
3021 (if (INTEGRAL_TYPE_P (type)
3022 && TYPE_OVERFLOW_UNDEFINED (type)
3023 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3024 (with { tree utype = unsigned_type_for (type); }
3025 (convert (negate (convert:utype @1))))
3026 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3027 /* For pointer types, if the conversion of A to the
3028 final type requires a sign- or zero-extension,
3029 then we have to punt - it is not defined which
3031 || (POINTER_TYPE_P (TREE_TYPE (@0))
3032 && TREE_CODE (@1) == INTEGER_CST
3033 && tree_int_cst_sign_bit (@1) == 0))
3034 (negate (convert @1)))))
3036 (pointer_diff @0 (pointer_plus @@0 @1))
3037 /* The second argument of pointer_plus must be interpreted as signed, and
3038 thus sign-extended if necessary. */
3039 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3040 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3041 second arg is unsigned even when we need to consider it as signed,
3042 we don't want to diagnose overflow here. */
3043 (negate (convert (view_convert:stype @1)))))
3045 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3047 (minus (convert (plus:c @@0 @1))
3048 (convert (plus:c @0 @2)))
3049 (if (INTEGRAL_TYPE_P (type)
3050 && TYPE_OVERFLOW_UNDEFINED (type)
3051 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3052 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3053 (with { tree utype = unsigned_type_for (type); }
3054 (convert (minus (convert:utype @1) (convert:utype @2))))
3055 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3056 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3057 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3058 /* For integer types, if A has a smaller type
3059 than T the result depends on the possible
3061 E.g. T=size_t, A=(unsigned)429497295, P>0.
3062 However, if an overflow in P + A would cause
3063 undefined behavior, we can assume that there
3065 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3066 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3067 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3068 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3069 (minus (convert @1) (convert @2)))))
3071 (minus (convert (pointer_plus @@0 @1))
3072 (convert (pointer_plus @0 @2)))
3073 (if (INTEGRAL_TYPE_P (type)
3074 && TYPE_OVERFLOW_UNDEFINED (type)
3075 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3076 (with { tree utype = unsigned_type_for (type); }
3077 (convert (minus (convert:utype @1) (convert:utype @2))))
3078 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3079 /* For pointer types, if the conversion of A to the
3080 final type requires a sign- or zero-extension,
3081 then we have to punt - it is not defined which
3083 || (POINTER_TYPE_P (TREE_TYPE (@0))
3084 && TREE_CODE (@1) == INTEGER_CST
3085 && tree_int_cst_sign_bit (@1) == 0
3086 && TREE_CODE (@2) == INTEGER_CST
3087 && tree_int_cst_sign_bit (@2) == 0))
3088 (minus (convert @1) (convert @2)))))
3090 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3091 (pointer_diff @0 @1))
3093 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3094 /* The second argument of pointer_plus must be interpreted as signed, and
3095 thus sign-extended if necessary. */
3096 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3097 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3098 second arg is unsigned even when we need to consider it as signed,
3099 we don't want to diagnose overflow here. */
3100 (minus (convert (view_convert:stype @1))
3101 (convert (view_convert:stype @2)))))))
3103 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3104 Modeled after fold_plusminus_mult_expr. */
3105 (if (!TYPE_SATURATING (type)
3106 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3107 (for plusminus (plus minus)
3109 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3110 (if (!ANY_INTEGRAL_TYPE_P (type)
3111 || TYPE_OVERFLOW_WRAPS (type)
3112 || (INTEGRAL_TYPE_P (type)
3113 && tree_expr_nonzero_p (@0)
3114 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3115 (if (single_use (@3) || single_use (@4))
3116 /* If @1 +- @2 is constant require a hard single-use on either
3117 original operand (but not on both). */
3118 (mult (plusminus @1 @2) @0)
3119 (mult! (plusminus @1 @2) @0)
3121 /* We cannot generate constant 1 for fract. */
3122 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3124 (plusminus @0 (mult:c@3 @0 @2))
3125 (if ((!ANY_INTEGRAL_TYPE_P (type)
3126 || TYPE_OVERFLOW_WRAPS (type)
3127 /* For @0 + @0*@2 this transformation would introduce UB
3128 (where there was none before) for @0 in [-1,0] and @2 max.
3129 For @0 - @0*@2 this transformation would introduce UB
3130 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3131 || (INTEGRAL_TYPE_P (type)
3132 && ((tree_expr_nonzero_p (@0)
3133 && expr_not_equal_to (@0,
3134 wi::minus_one (TYPE_PRECISION (type))))
3135 || (plusminus == PLUS_EXPR
3136 ? expr_not_equal_to (@2,
3137 wi::max_value (TYPE_PRECISION (type), SIGNED))
3138 /* Let's ignore the @0 -1 and @2 min case. */
3139 : (expr_not_equal_to (@2,
3140 wi::min_value (TYPE_PRECISION (type), SIGNED))
3141 && expr_not_equal_to (@2,
3142 wi::min_value (TYPE_PRECISION (type), SIGNED)
3145 (mult (plusminus { build_one_cst (type); } @2) @0)))
3147 (plusminus (mult:c@3 @0 @2) @0)
3148 (if ((!ANY_INTEGRAL_TYPE_P (type)
3149 || TYPE_OVERFLOW_WRAPS (type)
3150 /* For @0*@2 + @0 this transformation would introduce UB
3151 (where there was none before) for @0 in [-1,0] and @2 max.
3152 For @0*@2 - @0 this transformation would introduce UB
3153 for @0 0 and @2 min. */
3154 || (INTEGRAL_TYPE_P (type)
3155 && ((tree_expr_nonzero_p (@0)
3156 && (plusminus == MINUS_EXPR
3157 || expr_not_equal_to (@0,
3158 wi::minus_one (TYPE_PRECISION (type)))))
3159 || expr_not_equal_to (@2,
3160 (plusminus == PLUS_EXPR
3161 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3162 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3164 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3167 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3168 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3170 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3171 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3172 && tree_fits_uhwi_p (@1)
3173 && tree_to_uhwi (@1) < element_precision (type)
3174 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3175 || optab_handler (smul_optab,
3176 TYPE_MODE (type)) != CODE_FOR_nothing))
3177 (with { tree t = type;
3178 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3179 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3180 element_precision (type));
3182 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3184 cst = build_uniform_cst (t, cst); }
3185 (convert (mult (convert:t @0) { cst; })))))
3187 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3188 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3189 && tree_fits_uhwi_p (@1)
3190 && tree_to_uhwi (@1) < element_precision (type)
3191 && tree_fits_uhwi_p (@2)
3192 && tree_to_uhwi (@2) < element_precision (type)
3193 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3194 || optab_handler (smul_optab,
3195 TYPE_MODE (type)) != CODE_FOR_nothing))
3196 (with { tree t = type;
3197 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3198 unsigned int prec = element_precision (type);
3199 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3200 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3201 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3203 cst = build_uniform_cst (t, cst); }
3204 (convert (mult (convert:t @0) { cst; })))))
3207 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3208 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3209 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3210 (for op (bit_ior bit_xor)
3212 (op (mult:s@0 @1 INTEGER_CST@2)
3213 (mult:s@3 @1 INTEGER_CST@4))
3214 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3215 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3217 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3219 (op:c (mult:s@0 @1 INTEGER_CST@2)
3220 (lshift:s@3 @1 INTEGER_CST@4))
3221 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3222 && tree_int_cst_sgn (@4) > 0
3223 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3224 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3225 wide_int c = wi::add (wi::to_wide (@2),
3226 wi::lshift (wone, wi::to_wide (@4))); }
3227 (mult @1 { wide_int_to_tree (type, c); }))))
3229 (op:c (mult:s@0 @1 INTEGER_CST@2)
3231 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3232 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3234 { wide_int_to_tree (type,
3235 wi::add (wi::to_wide (@2), 1)); })))
3237 (op (lshift:s@0 @1 INTEGER_CST@2)
3238 (lshift:s@3 @1 INTEGER_CST@4))
3239 (if (INTEGRAL_TYPE_P (type)
3240 && tree_int_cst_sgn (@2) > 0
3241 && tree_int_cst_sgn (@4) > 0
3242 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3243 (with { tree t = type;
3244 if (!TYPE_OVERFLOW_WRAPS (t))
3245 t = unsigned_type_for (t);
3246 wide_int wone = wi::one (TYPE_PRECISION (t));
3247 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3248 wi::lshift (wone, wi::to_wide (@4))); }
3249 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3251 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3253 (if (INTEGRAL_TYPE_P (type)
3254 && tree_int_cst_sgn (@2) > 0
3255 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3256 (with { tree t = type;
3257 if (!TYPE_OVERFLOW_WRAPS (t))
3258 t = unsigned_type_for (t);
3259 wide_int wone = wi::one (TYPE_PRECISION (t));
3260 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3261 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3263 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3265 (for minmax (min max)
3269 /* For fmin() and fmax(), skip folding when both are sNaN. */
3270 (for minmax (FMIN_ALL FMAX_ALL)
3273 (if (!tree_expr_maybe_signaling_nan_p (@0))
3275 /* min(max(x,y),y) -> y. */
3277 (min:c (max:c @0 @1) @1)
3279 /* max(min(x,y),y) -> y. */
3281 (max:c (min:c @0 @1) @1)
3283 /* max(a,-a) -> abs(a). */
3285 (max:c @0 (negate @0))
3286 (if (TREE_CODE (type) != COMPLEX_TYPE
3287 && (! ANY_INTEGRAL_TYPE_P (type)
3288 || TYPE_OVERFLOW_UNDEFINED (type)))
3290 /* min(a,-a) -> -abs(a). */
3292 (min:c @0 (negate @0))
3293 (if (TREE_CODE (type) != COMPLEX_TYPE
3294 && (! ANY_INTEGRAL_TYPE_P (type)
3295 || TYPE_OVERFLOW_UNDEFINED (type)))
3300 (if (INTEGRAL_TYPE_P (type)
3301 && TYPE_MIN_VALUE (type)
3302 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3304 (if (INTEGRAL_TYPE_P (type)
3305 && TYPE_MAX_VALUE (type)
3306 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3311 (if (INTEGRAL_TYPE_P (type)
3312 && TYPE_MAX_VALUE (type)
3313 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3315 (if (INTEGRAL_TYPE_P (type)
3316 && TYPE_MIN_VALUE (type)
3317 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3320 /* max (a, a + CST) -> a + CST where CST is positive. */
3321 /* max (a, a + CST) -> a where CST is negative. */
3323 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3324 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3325 (if (tree_int_cst_sgn (@1) > 0)
3329 /* min (a, a + CST) -> a where CST is positive. */
3330 /* min (a, a + CST) -> a + CST where CST is negative. */
3332 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3333 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3334 (if (tree_int_cst_sgn (@1) > 0)
3338 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3339 the addresses are known to be less, equal or greater. */
3340 (for minmax (min max)
3343 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3346 poly_int64 off0, off1;
3348 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3349 off0, off1, GENERIC);
3352 (if (minmax == MIN_EXPR)
3353 (if (known_le (off0, off1))
3355 (if (known_gt (off0, off1))
3357 (if (known_ge (off0, off1))
3359 (if (known_lt (off0, off1))
3362 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3363 and the outer convert demotes the expression back to x's type. */
3364 (for minmax (min max)
3366 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3367 (if (INTEGRAL_TYPE_P (type)
3368 && types_match (@1, type) && int_fits_type_p (@2, type)
3369 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3370 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3371 (minmax @1 (convert @2)))))
3373 (for minmax (FMIN_ALL FMAX_ALL)
3374 /* If either argument is NaN and other one is not sNaN, return the other
3375 one. Avoid the transformation if we get (and honor) a signalling NaN. */
3377 (minmax:c @0 REAL_CST@1)
3378 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3379 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3380 && !tree_expr_maybe_signaling_nan_p (@0))
3382 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3383 functions to return the numeric arg if the other one is NaN.
3384 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3385 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3386 worry about it either. */
3387 (if (flag_finite_math_only)
3394 /* min (-A, -B) -> -max (A, B) */
3395 (for minmax (min max FMIN_ALL FMAX_ALL)
3396 maxmin (max min FMAX_ALL FMIN_ALL)
3398 (minmax (negate:s@2 @0) (negate:s@3 @1))
3399 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3400 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3401 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3402 (negate (maxmin @0 @1)))))
3403 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3404 MAX (~X, ~Y) -> ~MIN (X, Y) */
3405 (for minmax (min max)
3408 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3409 (bit_not (maxmin @0 @1))))
3411 /* MIN (X, Y) == X -> X <= Y */
3412 (for minmax (min min max max)
3416 (cmp:c (minmax:c @0 @1) @0)
3417 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3419 /* MIN (X, 5) == 0 -> X == 0
3420 MIN (X, 5) == 7 -> false */
3423 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3424 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3425 TYPE_SIGN (TREE_TYPE (@0))))
3426 { constant_boolean_node (cmp == NE_EXPR, type); }
3427 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3428 TYPE_SIGN (TREE_TYPE (@0))))
3432 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3433 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3434 TYPE_SIGN (TREE_TYPE (@0))))
3435 { constant_boolean_node (cmp == NE_EXPR, type); }
3436 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3437 TYPE_SIGN (TREE_TYPE (@0))))
3439 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3440 (for minmax (min min max max min min max max )
3441 cmp (lt le gt ge gt ge lt le )
3442 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3444 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3445 (comb (cmp @0 @2) (cmp @1 @2))))
3447 /* X <= MAX(X, Y) -> true
3448 X > MAX(X, Y) -> false
3449 X >= MIN(X, Y) -> true
3450 X < MIN(X, Y) -> false */
3451 (for minmax (min min max max )
3454 (cmp @0 (minmax:c @0 @1))
3455 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3457 /* Undo fancy ways of writing max/min or other ?: expressions, like
3458 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
3459 People normally use ?: and that is what we actually try to optimize. */
3460 /* Transform A + (B-A)*cmp into cmp ? B : A. */
3462 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3463 (if (INTEGRAL_TYPE_P (type)
3464 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3465 (cond (convert:boolean_type_node @2) @1 @0)))
3466 /* Transform A - (A-B)*cmp into cmp ? B : A. */
3468 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3469 (if (INTEGRAL_TYPE_P (type)
3470 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3471 (cond (convert:boolean_type_node @2) @1 @0)))
3472 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
3474 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3475 (if (INTEGRAL_TYPE_P (type)
3476 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3477 (cond (convert:boolean_type_node @2) @1 @0)))
3479 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
3481 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3484 /* Simplifications of shift and rotates. */
3486 (for rotate (lrotate rrotate)
3488 (rotate integer_all_onesp@0 @1)
3491 /* Optimize -1 >> x for arithmetic right shifts. */
3493 (rshift integer_all_onesp@0 @1)
3494 (if (!TYPE_UNSIGNED (type))
3497 /* Optimize (x >> c) << c into x & (-1<<c). */
3499 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3500 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3501 /* It doesn't matter if the right shift is arithmetic or logical. */
3502 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3505 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3506 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3507 /* Allow intermediate conversion to integral type with whatever sign, as
3508 long as the low TYPE_PRECISION (type)
3509 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3510 && INTEGRAL_TYPE_P (type)
3511 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3512 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3513 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3514 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3515 || wi::geu_p (wi::to_wide (@1),
3516 TYPE_PRECISION (type)
3517 - TYPE_PRECISION (TREE_TYPE (@2)))))
3518 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3520 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3523 (rshift (lshift @0 INTEGER_CST@1) @1)
3524 (if (TYPE_UNSIGNED (type)
3525 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3526 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3528 /* Optimize x >> x into 0 */
3531 { build_zero_cst (type); })
3533 (for shiftrotate (lrotate rrotate lshift rshift)
3535 (shiftrotate @0 integer_zerop)
3538 (shiftrotate integer_zerop@0 @1)
3540 /* Prefer vector1 << scalar to vector1 << vector2
3541 if vector2 is uniform. */
3542 (for vec (VECTOR_CST CONSTRUCTOR)
3544 (shiftrotate @0 vec@1)
3545 (with { tree tem = uniform_vector_p (@1); }
3547 (shiftrotate @0 { tem; }))))))
3549 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3550 Y is 0. Similarly for X >> Y. */
3552 (for shift (lshift rshift)
3554 (shift @0 SSA_NAME@1)
3555 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3557 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3558 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3560 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3564 /* Rewrite an LROTATE_EXPR by a constant into an
3565 RROTATE_EXPR by a new constant. */
3567 (lrotate @0 INTEGER_CST@1)
3568 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3569 build_int_cst (TREE_TYPE (@1),
3570 element_precision (type)), @1); }))
3572 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3573 (for op (lrotate rrotate rshift lshift)
3575 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3576 (with { unsigned int prec = element_precision (type); }
3577 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3578 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3579 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3580 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3581 (with { unsigned int low = (tree_to_uhwi (@1)
3582 + tree_to_uhwi (@2)); }
3583 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3584 being well defined. */
3586 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3587 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3588 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3589 { build_zero_cst (type); }
3590 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3591 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3594 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3596 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3597 (if ((wi::to_wide (@1) & 1) != 0)
3598 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3599 { build_zero_cst (type); }))
3601 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3602 either to false if D is smaller (unsigned comparison) than C, or to
3603 x == log2 (D) - log2 (C). Similarly for right shifts. */
3607 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3608 (with { int c1 = wi::clz (wi::to_wide (@1));
3609 int c2 = wi::clz (wi::to_wide (@2)); }
3611 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3612 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3614 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3615 (if (tree_int_cst_sgn (@1) > 0)
3616 (with { int c1 = wi::clz (wi::to_wide (@1));
3617 int c2 = wi::clz (wi::to_wide (@2)); }
3619 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3620 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3622 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3623 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3627 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3628 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3630 || (!integer_zerop (@2)
3631 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3632 { constant_boolean_node (cmp == NE_EXPR, type); }
3633 (if (!integer_zerop (@2)
3634 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3635 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3637 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
3638 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
3641 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3642 (if (tree_fits_shwi_p (@1)
3643 && tree_to_shwi (@1) > 0
3644 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3645 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
3646 { constant_boolean_node (cmp == NE_EXPR, type); }
3647 (with { wide_int c1 = wi::to_wide (@1);
3648 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
3649 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
3650 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
3651 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
3653 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3654 (if (tree_fits_shwi_p (@1)
3655 && tree_to_shwi (@1) > 0
3656 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3657 (with { tree t0 = TREE_TYPE (@0);
3658 unsigned int prec = TYPE_PRECISION (t0);
3659 wide_int c1 = wi::to_wide (@1);
3660 wide_int c2 = wi::to_wide (@2);
3661 wide_int c3 = wi::to_wide (@3);
3662 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
3663 (if ((c2 & c3) != c3)
3664 { constant_boolean_node (cmp == NE_EXPR, type); }
3665 (if (TYPE_UNSIGNED (t0))
3666 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
3667 { constant_boolean_node (cmp == NE_EXPR, type); }
3668 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3669 { wide_int_to_tree (t0, c3 << c1); }))
3670 (with { wide_int smask = wi::arshift (sb, c1); }
3672 (if ((c2 & smask) == 0)
3673 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3674 { wide_int_to_tree (t0, c3 << c1); }))
3675 (if ((c3 & smask) == 0)
3676 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3677 { wide_int_to_tree (t0, c3 << c1); }))
3678 (if ((c2 & smask) != (c3 & smask))
3679 { constant_boolean_node (cmp == NE_EXPR, type); })
3680 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3681 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
3683 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3684 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3685 if the new mask might be further optimized. */
3686 (for shift (lshift rshift)
3688 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3690 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3691 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3692 && tree_fits_uhwi_p (@1)
3693 && tree_to_uhwi (@1) > 0
3694 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3697 unsigned int shiftc = tree_to_uhwi (@1);
3698 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3699 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3700 tree shift_type = TREE_TYPE (@3);
3703 if (shift == LSHIFT_EXPR)
3704 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3705 else if (shift == RSHIFT_EXPR
3706 && type_has_mode_precision_p (shift_type))
3708 prec = TYPE_PRECISION (TREE_TYPE (@3));
3710 /* See if more bits can be proven as zero because of
3713 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3715 tree inner_type = TREE_TYPE (@0);
3716 if (type_has_mode_precision_p (inner_type)
3717 && TYPE_PRECISION (inner_type) < prec)
3719 prec = TYPE_PRECISION (inner_type);
3720 /* See if we can shorten the right shift. */
3722 shift_type = inner_type;
3723 /* Otherwise X >> C1 is all zeros, so we'll optimize
3724 it into (X, 0) later on by making sure zerobits
3728 zerobits = HOST_WIDE_INT_M1U;
3731 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3732 zerobits <<= prec - shiftc;
3734 /* For arithmetic shift if sign bit could be set, zerobits
3735 can contain actually sign bits, so no transformation is
3736 possible, unless MASK masks them all away. In that
3737 case the shift needs to be converted into logical shift. */
3738 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3739 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3741 if ((mask & zerobits) == 0)
3742 shift_type = unsigned_type_for (TREE_TYPE (@3));
3748 /* ((X << 16) & 0xff00) is (X, 0). */
3749 (if ((mask & zerobits) == mask)
3750 { build_int_cst (type, 0); }
3751 (with { newmask = mask | zerobits; }
3752 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3755 /* Only do the transformation if NEWMASK is some integer
3757 for (prec = BITS_PER_UNIT;
3758 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3759 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3762 (if (prec < HOST_BITS_PER_WIDE_INT
3763 || newmask == HOST_WIDE_INT_M1U)
3765 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3766 (if (!tree_int_cst_equal (newmaskt, @2))
3767 (if (shift_type != TREE_TYPE (@3))
3768 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3769 (bit_and @4 { newmaskt; })))))))))))))
3771 /* ((1 << n) & M) != 0 -> n == log2 (M) */
3777 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3778 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3779 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3780 wi::exact_log2 (wi::to_wide (@1))); }))))
3782 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3783 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3784 (for shift (lshift rshift)
3785 (for bit_op (bit_and bit_xor bit_ior)
3787 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3788 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3789 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3791 (bit_op (shift (convert @0) @1) { mask; })))))))
3793 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3795 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3796 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3797 && (element_precision (TREE_TYPE (@0))
3798 <= element_precision (TREE_TYPE (@1))
3799 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3801 { tree shift_type = TREE_TYPE (@0); }
3802 (convert (rshift (convert:shift_type @1) @2)))))
3804 /* ~(~X >>r Y) -> X >>r Y
3805 ~(~X <<r Y) -> X <<r Y */
3806 (for rotate (lrotate rrotate)
3808 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3809 (if ((element_precision (TREE_TYPE (@0))
3810 <= element_precision (TREE_TYPE (@1))
3811 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3812 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3813 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3815 { tree rotate_type = TREE_TYPE (@0); }
3816 (convert (rotate (convert:rotate_type @1) @2))))))
3819 (for rotate (lrotate rrotate)
3820 invrot (rrotate lrotate)
3821 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3823 (cmp (rotate @1 @0) (rotate @2 @0))
3825 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3827 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3828 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3829 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3831 (cmp (rotate @0 @1) INTEGER_CST@2)
3832 (if (integer_zerop (@2) || integer_all_onesp (@2))
3835 /* Narrow a lshift by constant. */
3837 (convert (lshift:s@0 @1 INTEGER_CST@2))
3838 (if (INTEGRAL_TYPE_P (type)
3839 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3840 && !integer_zerop (@2)
3841 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
3842 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3843 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
3844 (lshift (convert @1) @2)
3845 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
3846 { build_zero_cst (type); }))))
3848 /* Simplifications of conversions. */
3850 /* Basic strip-useless-type-conversions / strip_nops. */
3851 (for cvt (convert view_convert float fix_trunc)
3854 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3855 || (GENERIC && type == TREE_TYPE (@0)))
3858 /* Contract view-conversions. */
3860 (view_convert (view_convert @0))
3863 /* For integral conversions with the same precision or pointer
3864 conversions use a NOP_EXPR instead. */
3867 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3868 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3869 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3872 /* Strip inner integral conversions that do not change precision or size, or
3873 zero-extend while keeping the same size (for bool-to-char). */
3875 (view_convert (convert@0 @1))
3876 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3877 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3878 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3879 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3880 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3881 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3884 /* Simplify a view-converted empty or single-element constructor. */
3886 (view_convert CONSTRUCTOR@0)
3888 { tree ctor = (TREE_CODE (@0) == SSA_NAME
3889 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
3891 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3892 { build_zero_cst (type); })
3893 (if (CONSTRUCTOR_NELTS (ctor) == 1
3894 && VECTOR_TYPE_P (TREE_TYPE (ctor))
3895 && operand_equal_p (TYPE_SIZE (type),
3896 TYPE_SIZE (TREE_TYPE
3897 (CONSTRUCTOR_ELT (ctor, 0)->value))))
3898 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
3900 /* Re-association barriers around constants and other re-association
3901 barriers can be removed. */
3903 (paren CONSTANT_CLASS_P@0)
3906 (paren (paren@1 @0))
3909 /* Handle cases of two conversions in a row. */
3910 (for ocvt (convert float fix_trunc)
3911 (for icvt (convert float)
3916 tree inside_type = TREE_TYPE (@0);
3917 tree inter_type = TREE_TYPE (@1);
3918 int inside_int = INTEGRAL_TYPE_P (inside_type);
3919 int inside_ptr = POINTER_TYPE_P (inside_type);
3920 int inside_float = FLOAT_TYPE_P (inside_type);
3921 int inside_vec = VECTOR_TYPE_P (inside_type);
3922 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3923 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3924 int inter_int = INTEGRAL_TYPE_P (inter_type);
3925 int inter_ptr = POINTER_TYPE_P (inter_type);
3926 int inter_float = FLOAT_TYPE_P (inter_type);
3927 int inter_vec = VECTOR_TYPE_P (inter_type);
3928 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3929 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3930 int final_int = INTEGRAL_TYPE_P (type);
3931 int final_ptr = POINTER_TYPE_P (type);
3932 int final_float = FLOAT_TYPE_P (type);
3933 int final_vec = VECTOR_TYPE_P (type);
3934 unsigned int final_prec = TYPE_PRECISION (type);
3935 int final_unsignedp = TYPE_UNSIGNED (type);
3938 /* In addition to the cases of two conversions in a row
3939 handled below, if we are converting something to its own
3940 type via an object of identical or wider precision, neither
3941 conversion is needed. */
3942 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3944 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3945 && (((inter_int || inter_ptr) && final_int)
3946 || (inter_float && final_float))
3947 && inter_prec >= final_prec)
3950 /* Likewise, if the intermediate and initial types are either both
3951 float or both integer, we don't need the middle conversion if the
3952 former is wider than the latter and doesn't change the signedness
3953 (for integers). Avoid this if the final type is a pointer since
3954 then we sometimes need the middle conversion. */
3955 (if (((inter_int && inside_int) || (inter_float && inside_float))
3956 && (final_int || final_float)
3957 && inter_prec >= inside_prec
3958 && (inter_float || inter_unsignedp == inside_unsignedp))
3961 /* If we have a sign-extension of a zero-extended value, we can
3962 replace that by a single zero-extension. Likewise if the
3963 final conversion does not change precision we can drop the
3964 intermediate conversion. */
3965 (if (inside_int && inter_int && final_int
3966 && ((inside_prec < inter_prec && inter_prec < final_prec
3967 && inside_unsignedp && !inter_unsignedp)
3968 || final_prec == inter_prec))
3971 /* Two conversions in a row are not needed unless:
3972 - some conversion is floating-point (overstrict for now), or
3973 - some conversion is a vector (overstrict for now), or
3974 - the intermediate type is narrower than both initial and
3976 - the intermediate type and innermost type differ in signedness,
3977 and the outermost type is wider than the intermediate, or
3978 - the initial type is a pointer type and the precisions of the
3979 intermediate and final types differ, or
3980 - the final type is a pointer type and the precisions of the
3981 initial and intermediate types differ. */
3982 (if (! inside_float && ! inter_float && ! final_float
3983 && ! inside_vec && ! inter_vec && ! final_vec
3984 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3985 && ! (inside_int && inter_int
3986 && inter_unsignedp != inside_unsignedp
3987 && inter_prec < final_prec)
3988 && ((inter_unsignedp && inter_prec > inside_prec)
3989 == (final_unsignedp && final_prec > inter_prec))
3990 && ! (inside_ptr && inter_prec != final_prec)
3991 && ! (final_ptr && inside_prec != inter_prec))
3994 /* A truncation to an unsigned type (a zero-extension) should be
3995 canonicalized as bitwise and of a mask. */
3996 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3997 && final_int && inter_int && inside_int
3998 && final_prec == inside_prec
3999 && final_prec > inter_prec
4001 (convert (bit_and @0 { wide_int_to_tree
4003 wi::mask (inter_prec, false,
4004 TYPE_PRECISION (inside_type))); })))
4006 /* If we are converting an integer to a floating-point that can
4007 represent it exactly and back to an integer, we can skip the
4008 floating-point conversion. */
4009 (if (GIMPLE /* PR66211 */
4010 && inside_int && inter_float && final_int &&
4011 (unsigned) significand_size (TYPE_MODE (inter_type))
4012 >= inside_prec - !inside_unsignedp)
4015 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4016 float_type. Only do the transformation if we do not need to preserve
4017 trapping behaviour, so require !flag_trapping_math. */
4020 (float (fix_trunc @0))
4021 (if (!flag_trapping_math
4022 && types_match (type, TREE_TYPE (@0))
4023 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4028 /* If we have a narrowing conversion to an integral type that is fed by a
4029 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4030 masks off bits outside the final type (and nothing else). */
4032 (convert (bit_and @0 INTEGER_CST@1))
4033 (if (INTEGRAL_TYPE_P (type)
4034 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4035 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4036 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4037 TYPE_PRECISION (type)), 0))
4041 /* (X /[ex] A) * A -> X. */
4043 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4046 /* Simplify (A / B) * B + (A % B) -> A. */
4047 (for div (trunc_div ceil_div floor_div round_div)
4048 mod (trunc_mod ceil_mod floor_mod round_mod)
4050 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4053 /* x / y * y == x -> x % y == 0. */
4055 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4056 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4057 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4059 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4060 (for op (plus minus)
4062 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4063 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4064 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4067 wi::overflow_type overflow;
4068 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4069 TYPE_SIGN (type), &overflow);
4071 (if (types_match (type, TREE_TYPE (@2))
4072 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4073 (op @0 { wide_int_to_tree (type, mul); })
4074 (with { tree utype = unsigned_type_for (type); }
4075 (convert (op (convert:utype @0)
4076 (mult (convert:utype @1) (convert:utype @2))))))))))
4078 /* Canonicalization of binary operations. */
4080 /* Convert X + -C into X - C. */
4082 (plus @0 REAL_CST@1)
4083 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4084 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4085 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4086 (minus @0 { tem; })))))
4088 /* Convert x+x into x*2. */
4091 (if (SCALAR_FLOAT_TYPE_P (type))
4092 (mult @0 { build_real (type, dconst2); })
4093 (if (INTEGRAL_TYPE_P (type))
4094 (mult @0 { build_int_cst (type, 2); }))))
4098 (minus integer_zerop @1)
4101 (pointer_diff integer_zerop @1)
4102 (negate (convert @1)))
4104 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4105 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4106 (-ARG1 + ARG0) reduces to -ARG1. */
4108 (minus real_zerop@0 @1)
4109 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4112 /* Transform x * -1 into -x. */
4114 (mult @0 integer_minus_onep)
4117 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4118 signed overflow for CST != 0 && CST != -1. */
4120 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4121 (if (TREE_CODE (@2) != INTEGER_CST
4123 && !integer_zerop (@1) && !integer_minus_onep (@1))
4124 (mult (mult @0 @2) @1)))
4126 /* True if we can easily extract the real and imaginary parts of a complex
4128 (match compositional_complex
4129 (convert? (complex @0 @1)))
4131 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4133 (complex (realpart @0) (imagpart @0))
4136 (realpart (complex @0 @1))
4139 (imagpart (complex @0 @1))
4142 /* Sometimes we only care about half of a complex expression. */
4144 (realpart (convert?:s (conj:s @0)))
4145 (convert (realpart @0)))
4147 (imagpart (convert?:s (conj:s @0)))
4148 (convert (negate (imagpart @0))))
4149 (for part (realpart imagpart)
4150 (for op (plus minus)
4152 (part (convert?:s@2 (op:s @0 @1)))
4153 (convert (op (part @0) (part @1))))))
4155 (realpart (convert?:s (CEXPI:s @0)))
4158 (imagpart (convert?:s (CEXPI:s @0)))
4161 /* conj(conj(x)) -> x */
4163 (conj (convert? (conj @0)))
4164 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4167 /* conj({x,y}) -> {x,-y} */
4169 (conj (convert?:s (complex:s @0 @1)))
4170 (with { tree itype = TREE_TYPE (type); }
4171 (complex (convert:itype @0) (negate (convert:itype @1)))))
4173 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4174 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
4175 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
4180 (bswap (bit_not (bswap @0)))
4182 (for bitop (bit_xor bit_ior bit_and)
4184 (bswap (bitop:c (bswap @0) @1))
4185 (bitop @0 (bswap @1))))
4188 (cmp (bswap@2 @0) (bswap @1))
4189 (with { tree ctype = TREE_TYPE (@2); }
4190 (cmp (convert:ctype @0) (convert:ctype @1))))
4192 (cmp (bswap @0) INTEGER_CST@1)
4193 (with { tree ctype = TREE_TYPE (@1); }
4194 (cmp (convert:ctype @0) (bswap! @1)))))
4195 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4197 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4199 (if (BITS_PER_UNIT == 8
4200 && tree_fits_uhwi_p (@2)
4201 && tree_fits_uhwi_p (@3))
4204 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4205 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4206 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4207 unsigned HOST_WIDE_INT lo = bits & 7;
4208 unsigned HOST_WIDE_INT hi = bits - lo;
4211 && mask < (256u>>lo)
4212 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4213 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4215 (bit_and (convert @1) @3)
4218 tree utype = unsigned_type_for (TREE_TYPE (@1));
4219 tree nst = build_int_cst (integer_type_node, ns);
4221 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4222 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4224 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4225 (if (BITS_PER_UNIT == 8
4226 && CHAR_TYPE_SIZE == 8
4227 && tree_fits_uhwi_p (@1))
4230 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4231 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4232 /* If the bswap was extended before the original shift, this
4233 byte (shift) has the sign of the extension, not the sign of
4234 the original shift. */
4235 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4237 /* Special case: logical right shift of sign-extended bswap.
4238 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4239 (if (TYPE_PRECISION (type) > prec
4240 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4241 && TYPE_UNSIGNED (type)
4242 && bits < prec && bits + 8 >= prec)
4243 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4244 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4245 (if (bits + 8 == prec)
4246 (if (TYPE_UNSIGNED (st))
4247 (convert (convert:unsigned_char_type_node @0))
4248 (convert (convert:signed_char_type_node @0)))
4249 (if (bits < prec && bits + 8 > prec)
4252 tree nst = build_int_cst (integer_type_node, bits & 7);
4253 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4254 : signed_char_type_node;
4256 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4257 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4259 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4260 (if (BITS_PER_UNIT == 8
4261 && tree_fits_uhwi_p (@1)
4262 && tree_to_uhwi (@1) < 256)
4265 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4266 tree utype = unsigned_type_for (TREE_TYPE (@0));
4267 tree nst = build_int_cst (integer_type_node, prec - 8);
4269 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4272 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
4274 /* Simplify constant conditions.
4275 Only optimize constant conditions when the selected branch
4276 has the same type as the COND_EXPR. This avoids optimizing
4277 away "c ? x : throw", where the throw has a void type.
4278 Note that we cannot throw away the fold-const.cc variant nor
4279 this one as we depend on doing this transform before possibly
4280 A ? B : B -> B triggers and the fold-const.cc one can optimize
4281 0 ? A : B to B even if A has side-effects. Something
4282 genmatch cannot handle. */
4284 (cond INTEGER_CST@0 @1 @2)
4285 (if (integer_zerop (@0))
4286 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4288 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4291 (vec_cond VECTOR_CST@0 @1 @2)
4292 (if (integer_all_onesp (@0))
4294 (if (integer_zerop (@0))
4297 /* Sink unary operations to branches, but only if we do fold both. */
4298 (for op (negate bit_not abs absu)
4300 (op (vec_cond:s @0 @1 @2))
4301 (vec_cond @0 (op! @1) (op! @2))))
4303 /* Sink binary operation to branches, but only if we can fold it. */
4304 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4305 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4306 trunc_mod ceil_mod floor_mod round_mod min max)
4307 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4309 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4310 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4312 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4314 (op (vec_cond:s @0 @1 @2) @3)
4315 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4317 (op @3 (vec_cond:s @0 @1 @2))
4318 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4321 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4322 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4325 int ibit = tree_log2 (@0);
4326 int ibit2 = tree_log2 (@1);
4330 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4332 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4333 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4336 int ibit = tree_log2 (@0);
4337 int ibit2 = tree_log2 (@1);
4341 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4343 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4346 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4348 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4350 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4353 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4355 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4357 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4358 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4361 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4362 TYPE_PRECISION(type)));
4363 int ibit2 = tree_log2 (@1);
4367 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4369 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4371 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4374 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4375 TYPE_PRECISION(type)));
4376 int ibit2 = tree_log2 (@1);
4380 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4382 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4385 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4387 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4389 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4392 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4394 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4398 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4399 Currently disabled after pass lvec because ARM understands
4400 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
4402 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4403 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4404 (vec_cond (bit_and @0 @3) @1 @2)))
4406 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4407 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4408 (vec_cond (bit_ior @0 @3) @1 @2)))
4410 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4411 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4412 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4414 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4415 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4416 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4418 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
4420 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4421 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4422 (vec_cond (bit_and @0 @1) @2 @3)))
4424 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4425 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4426 (vec_cond (bit_ior @0 @1) @2 @3)))
4428 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4429 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4430 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4432 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4433 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4434 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4436 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4437 types are compatible. */
4439 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4440 (if (VECTOR_BOOLEAN_TYPE_P (type)
4441 && types_match (type, TREE_TYPE (@0)))
4442 (if (integer_zerop (@1) && integer_all_onesp (@2))
4444 (if (integer_all_onesp (@1) && integer_zerop (@2))
4447 /* A few simplifications of "a ? CST1 : CST2". */
4448 /* NOTE: Only do this on gimple as the if-chain-to-switch
4449 optimization depends on the gimple to have if statements in it. */
4452 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4454 (if (integer_zerop (@2))
4456 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4457 (if (integer_onep (@1))
4458 (convert (convert:boolean_type_node @0)))
4459 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4460 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4462 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4464 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4465 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
4466 here as the powerof2cst case above will handle that case correctly. */
4467 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4468 (negate (convert (convert:boolean_type_node @0))))))
4469 (if (integer_zerop (@1))
4471 tree booltrue = constant_boolean_node (true, boolean_type_node);
4474 /* a ? 0 : 1 -> !a. */
4475 (if (integer_onep (@2))
4476 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4477 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4478 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
4480 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4482 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4484 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
4485 here as the powerof2cst case above will handle that case correctly. */
4486 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4487 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4496 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
4497 (if (INTEGRAL_TYPE_P (type)
4498 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4499 (cond @1 (convert @2) (convert @3))))
4501 /* Simplification moved from fold_cond_expr_with_comparison. It may also
4503 /* This pattern implements two kinds simplification:
4506 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4507 1) Conversions are type widening from smaller type.
4508 2) Const c1 equals to c2 after canonicalizing comparison.
4509 3) Comparison has tree code LT, LE, GT or GE.
4510 This specific pattern is needed when (cmp (convert x) c) may not
4511 be simplified by comparison patterns because of multiple uses of
4512 x. It also makes sense here because simplifying across multiple
4513 referred var is always benefitial for complicated cases.
4516 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
4517 (for cmp (lt le gt ge eq)
4519 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4522 tree from_type = TREE_TYPE (@1);
4523 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4524 enum tree_code code = ERROR_MARK;
4526 if (INTEGRAL_TYPE_P (from_type)
4527 && int_fits_type_p (@2, from_type)
4528 && (types_match (c1_type, from_type)
4529 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4530 && (TYPE_UNSIGNED (from_type)
4531 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4532 && (types_match (c2_type, from_type)
4533 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4534 && (TYPE_UNSIGNED (from_type)
4535 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4539 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4541 /* X <= Y - 1 equals to X < Y. */
4544 /* X > Y - 1 equals to X >= Y. */
4548 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4550 /* X < Y + 1 equals to X <= Y. */
4553 /* X >= Y + 1 equals to X > Y. */
4557 if (code != ERROR_MARK
4558 || wi::to_widest (@2) == wi::to_widest (@3))
4560 if (cmp == LT_EXPR || cmp == LE_EXPR)
4562 if (cmp == GT_EXPR || cmp == GE_EXPR)
4566 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4567 else if (int_fits_type_p (@3, from_type))
4571 (if (code == MAX_EXPR)
4572 (convert (max @1 (convert @2)))
4573 (if (code == MIN_EXPR)
4574 (convert (min @1 (convert @2)))
4575 (if (code == EQ_EXPR)
4576 (convert (cond (eq @1 (convert @3))
4577 (convert:from_type @3) (convert:from_type @2)))))))))
4579 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4581 1) OP is PLUS or MINUS.
4582 2) CMP is LT, LE, GT or GE.
4583 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4585 This pattern also handles special cases like:
4587 A) Operand x is a unsigned to signed type conversion and c1 is
4588 integer zero. In this case,
4589 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4590 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4591 B) Const c1 may not equal to (C3 op' C2). In this case we also
4592 check equality for (c1+1) and (c1-1) by adjusting comparison
4595 TODO: Though signed type is handled by this pattern, it cannot be
4596 simplified at the moment because C standard requires additional
4597 type promotion. In order to match&simplify it here, the IR needs
4598 to be cleaned up by other optimizers, i.e, VRP. */
4599 (for op (plus minus)
4600 (for cmp (lt le gt ge)
4602 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4603 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4604 (if (types_match (from_type, to_type)
4605 /* Check if it is special case A). */
4606 || (TYPE_UNSIGNED (from_type)
4607 && !TYPE_UNSIGNED (to_type)
4608 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4609 && integer_zerop (@1)
4610 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4613 wi::overflow_type overflow = wi::OVF_NONE;
4614 enum tree_code code, cmp_code = cmp;
4616 wide_int c1 = wi::to_wide (@1);
4617 wide_int c2 = wi::to_wide (@2);
4618 wide_int c3 = wi::to_wide (@3);
4619 signop sgn = TYPE_SIGN (from_type);
4621 /* Handle special case A), given x of unsigned type:
4622 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4623 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4624 if (!types_match (from_type, to_type))
4626 if (cmp_code == LT_EXPR)
4628 if (cmp_code == GE_EXPR)
4630 c1 = wi::max_value (to_type);
4632 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4633 compute (c3 op' c2) and check if it equals to c1 with op' being
4634 the inverted operator of op. Make sure overflow doesn't happen
4635 if it is undefined. */
4636 if (op == PLUS_EXPR)
4637 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4639 real_c1 = wi::add (c3, c2, sgn, &overflow);
4642 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4644 /* Check if c1 equals to real_c1. Boundary condition is handled
4645 by adjusting comparison operation if necessary. */
4646 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4649 /* X <= Y - 1 equals to X < Y. */
4650 if (cmp_code == LE_EXPR)
4652 /* X > Y - 1 equals to X >= Y. */
4653 if (cmp_code == GT_EXPR)
4656 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4659 /* X < Y + 1 equals to X <= Y. */
4660 if (cmp_code == LT_EXPR)
4662 /* X >= Y + 1 equals to X > Y. */
4663 if (cmp_code == GE_EXPR)
4666 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4668 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4670 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4675 (if (code == MAX_EXPR)
4676 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4677 { wide_int_to_tree (from_type, c2); })
4678 (if (code == MIN_EXPR)
4679 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4680 { wide_int_to_tree (from_type, c2); })))))))))
4683 /* A >= B ? A : B -> max (A, B) and friends. The code is still
4684 in fold_cond_expr_with_comparison for GENERIC folding with
4685 some extra constraints. */
4686 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
4688 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
4689 (convert3? @0) (convert4? @1))
4690 (if (!HONOR_SIGNED_ZEROS (type)
4691 && (/* Allow widening conversions of the compare operands as data. */
4692 (INTEGRAL_TYPE_P (type)
4693 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
4694 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
4695 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
4696 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
4697 /* Or sign conversions for the comparison. */
4698 || (types_match (type, TREE_TYPE (@0))
4699 && types_match (type, TREE_TYPE (@1)))))
4701 (if (cmp == EQ_EXPR)
4702 (if (VECTOR_TYPE_P (type))
4705 (if (cmp == NE_EXPR)
4706 (if (VECTOR_TYPE_P (type))
4709 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
4710 (if (!HONOR_NANS (type))
4711 (if (VECTOR_TYPE_P (type))
4712 (view_convert (min @c0 @c1))
4713 (convert (min @c0 @c1)))))
4714 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
4715 (if (!HONOR_NANS (type))
4716 (if (VECTOR_TYPE_P (type))
4717 (view_convert (max @c0 @c1))
4718 (convert (max @c0 @c1)))))
4719 (if (cmp == UNEQ_EXPR)
4720 (if (!HONOR_NANS (type))
4721 (if (VECTOR_TYPE_P (type))
4724 (if (cmp == LTGT_EXPR)
4725 (if (!HONOR_NANS (type))
4726 (if (VECTOR_TYPE_P (type))
4728 (convert @c0))))))))
4731 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
4733 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
4734 (if (!TYPE_SATURATING (type)
4735 && (TYPE_OVERFLOW_WRAPS (type)
4736 || !wi::only_sign_bit_p (wi::to_wide (@1)))
4737 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
4740 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
4742 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
4743 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
4746 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
4747 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
4749 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
4750 (if (TYPE_UNSIGNED (type))
4751 (cond (ge @0 @1) (negate @0) @2)))
4753 (for cnd (cond vec_cond)
4754 /* A ? B : (A ? X : C) -> A ? B : C. */
4756 (cnd @0 (cnd @0 @1 @2) @3)
4759 (cnd @0 @1 (cnd @0 @2 @3))
4761 /* A ? B : (!A ? C : X) -> A ? B : C. */
4762 /* ??? This matches embedded conditions open-coded because genmatch
4763 would generate matching code for conditions in separate stmts only.
4764 The following is still important to merge then and else arm cases
4765 from if-conversion. */
4767 (cnd @0 @1 (cnd @2 @3 @4))
4768 (if (inverse_conditions_p (@0, @2))
4771 (cnd @0 (cnd @1 @2 @3) @4)
4772 (if (inverse_conditions_p (@0, @1))
4775 /* A ? B : B -> B. */
4780 /* !A ? B : C -> A ? C : B. */
4782 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4785 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4786 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4787 Need to handle UN* comparisons.
4789 None of these transformations work for modes with signed
4790 zeros. If A is +/-0, the first two transformations will
4791 change the sign of the result (from +0 to -0, or vice
4792 versa). The last four will fix the sign of the result,
4793 even though the original expressions could be positive or
4794 negative, depending on the sign of A.
4796 Note that all these transformations are correct if A is
4797 NaN, since the two alternatives (A and -A) are also NaNs. */
4799 (for cnd (cond vec_cond)
4800 /* A == 0 ? A : -A same as -A */
4803 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4804 (if (!HONOR_SIGNED_ZEROS (type))
4807 (cnd (cmp @0 zerop) zerop (negate@1 @0))
4808 (if (!HONOR_SIGNED_ZEROS (type))
4811 /* A != 0 ? A : -A same as A */
4814 (cnd (cmp @0 zerop) @0 (negate @0))
4815 (if (!HONOR_SIGNED_ZEROS (type))
4818 (cnd (cmp @0 zerop) @0 integer_zerop)
4819 (if (!HONOR_SIGNED_ZEROS (type))
4822 /* A >=/> 0 ? A : -A same as abs (A) */
4825 (cnd (cmp @0 zerop) @0 (negate @0))
4826 (if (!HONOR_SIGNED_ZEROS (type)
4827 && !TYPE_UNSIGNED (type))
4829 /* A <=/< 0 ? A : -A same as -abs (A) */
4832 (cnd (cmp @0 zerop) @0 (negate @0))
4833 (if (!HONOR_SIGNED_ZEROS (type)
4834 && !TYPE_UNSIGNED (type))
4835 (if (ANY_INTEGRAL_TYPE_P (type)
4836 && !TYPE_OVERFLOW_WRAPS (type))
4838 tree utype = unsigned_type_for (type);
4840 (convert (negate (absu:utype @0))))
4841 (negate (abs @0)))))
4845 /* -(type)!A -> (type)A - 1. */
4847 (negate (convert?:s (logical_inverted_value:s @0)))
4848 (if (INTEGRAL_TYPE_P (type)
4849 && TREE_CODE (type) != BOOLEAN_TYPE
4850 && TYPE_PRECISION (type) > 1
4851 && TREE_CODE (@0) == SSA_NAME
4852 && ssa_name_has_boolean_range (@0))
4853 (plus (convert:type @0) { build_all_ones_cst (type); })))
4855 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4856 return all -1 or all 0 results. */
4857 /* ??? We could instead convert all instances of the vec_cond to negate,
4858 but that isn't necessarily a win on its own. */
4860 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4861 (if (VECTOR_TYPE_P (type)
4862 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4863 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4864 && (TYPE_MODE (TREE_TYPE (type))
4865 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4866 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4868 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4870 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4871 (if (VECTOR_TYPE_P (type)
4872 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4873 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4874 && (TYPE_MODE (TREE_TYPE (type))
4875 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4876 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4879 /* Simplifications of comparisons. */
4881 /* See if we can reduce the magnitude of a constant involved in a
4882 comparison by changing the comparison code. This is a canonicalization
4883 formerly done by maybe_canonicalize_comparison_1. */
4887 (cmp @0 uniform_integer_cst_p@1)
4888 (with { tree cst = uniform_integer_cst_p (@1); }
4889 (if (tree_int_cst_sgn (cst) == -1)
4890 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4891 wide_int_to_tree (TREE_TYPE (cst),
4897 (cmp @0 uniform_integer_cst_p@1)
4898 (with { tree cst = uniform_integer_cst_p (@1); }
4899 (if (tree_int_cst_sgn (cst) == 1)
4900 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4901 wide_int_to_tree (TREE_TYPE (cst),
4902 wi::to_wide (cst) - 1)); })))))
4904 /* We can simplify a logical negation of a comparison to the
4905 inverted comparison. As we cannot compute an expression
4906 operator using invert_tree_comparison we have to simulate
4907 that with expression code iteration. */
4908 (for cmp (tcc_comparison)
4909 icmp (inverted_tcc_comparison)
4910 ncmp (inverted_tcc_comparison_with_nans)
4911 /* Ideally we'd like to combine the following two patterns
4912 and handle some more cases by using
4913 (logical_inverted_value (cmp @0 @1))
4914 here but for that genmatch would need to "inline" that.
4915 For now implement what forward_propagate_comparison did. */
4917 (bit_not (cmp @0 @1))
4918 (if (VECTOR_TYPE_P (type)
4919 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4920 /* Comparison inversion may be impossible for trapping math,
4921 invert_tree_comparison will tell us. But we can't use
4922 a computed operator in the replacement tree thus we have
4923 to play the trick below. */
4924 (with { enum tree_code ic = invert_tree_comparison
4925 (cmp, HONOR_NANS (@0)); }
4931 (bit_xor (cmp @0 @1) integer_truep)
4932 (with { enum tree_code ic = invert_tree_comparison
4933 (cmp, HONOR_NANS (@0)); }
4938 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
4940 (ne (cmp@2 @0 @1) integer_zerop)
4941 (if (types_match (type, TREE_TYPE (@2)))
4944 (eq (cmp@2 @0 @1) integer_truep)
4945 (if (types_match (type, TREE_TYPE (@2)))
4948 (ne (cmp@2 @0 @1) integer_truep)
4949 (if (types_match (type, TREE_TYPE (@2)))
4950 (with { enum tree_code ic = invert_tree_comparison
4951 (cmp, HONOR_NANS (@0)); }
4957 (eq (cmp@2 @0 @1) integer_zerop)
4958 (if (types_match (type, TREE_TYPE (@2)))
4959 (with { enum tree_code ic = invert_tree_comparison
4960 (cmp, HONOR_NANS (@0)); }
4966 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4967 ??? The transformation is valid for the other operators if overflow
4968 is undefined for the type, but performing it here badly interacts
4969 with the transformation in fold_cond_expr_with_comparison which
4970 attempts to synthetize ABS_EXPR. */
4972 (for sub (minus pointer_diff)
4974 (cmp (sub@2 @0 @1) integer_zerop)
4975 (if (single_use (@2))
4978 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4979 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4982 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4983 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4984 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4985 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4986 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4987 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4988 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4990 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4991 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4992 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4993 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4994 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4996 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4997 signed arithmetic case. That form is created by the compiler
4998 often enough for folding it to be of value. One example is in
4999 computing loop trip counts after Operator Strength Reduction. */
5000 (for cmp (simple_comparison)
5001 scmp (swapped_simple_comparison)
5003 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5004 /* Handle unfolded multiplication by zero. */
5005 (if (integer_zerop (@1))
5007 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5008 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5010 /* If @1 is negative we swap the sense of the comparison. */
5011 (if (tree_int_cst_sgn (@1) < 0)
5015 /* For integral types with undefined overflow fold
5016 x * C1 == C2 into x == C2 / C1 or false.
5017 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5021 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5022 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5023 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5024 && wi::to_wide (@1) != 0)
5025 (with { widest_int quot; }
5026 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5027 TYPE_SIGN (TREE_TYPE (@0)), "))
5028 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5029 { constant_boolean_node (cmp == NE_EXPR, type); }))
5030 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5031 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5032 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5035 tree itype = TREE_TYPE (@0);
5036 int p = TYPE_PRECISION (itype);
5037 wide_int m = wi::one (p + 1) << p;
5038 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5039 wide_int i = wide_int::from (wi::mod_inv (a, m),
5040 p, TYPE_SIGN (itype));
5041 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5044 /* Simplify comparison of something with itself. For IEEE
5045 floating-point, we can only do some of these simplifications. */
5049 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5050 || ! tree_expr_maybe_nan_p (@0))
5051 { constant_boolean_node (true, type); }
5053 /* With -ftrapping-math conversion to EQ loses an exception. */
5054 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5055 || ! flag_trapping_math))
5061 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5062 || ! tree_expr_maybe_nan_p (@0))
5063 { constant_boolean_node (false, type); })))
5064 (for cmp (unle unge uneq)
5067 { constant_boolean_node (true, type); }))
5068 (for cmp (unlt ungt)
5074 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5075 { constant_boolean_node (false, type); }))
5077 /* x == ~x -> false */
5078 /* x != ~x -> true */
5081 (cmp:c @0 (bit_not @0))
5082 { constant_boolean_node (cmp == NE_EXPR, type); }))
5084 /* Fold ~X op ~Y as Y op X. */
5085 (for cmp (simple_comparison)
5087 (cmp (bit_not@2 @0) (bit_not@3 @1))
5088 (if (single_use (@2) && single_use (@3))
5091 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
5092 (for cmp (simple_comparison)
5093 scmp (swapped_simple_comparison)
5095 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5096 (if (single_use (@2)
5097 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5098 (scmp @0 (bit_not @1)))))
5100 (for cmp (simple_comparison)
5101 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
5103 (cmp (convert@2 @0) (convert? @1))
5104 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5105 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
5106 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5107 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
5108 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
5111 tree type1 = TREE_TYPE (@1);
5112 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
5114 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
5115 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
5116 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
5117 type1 = float_type_node;
5118 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
5119 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
5120 type1 = double_type_node;
5123 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
5124 ? TREE_TYPE (@0) : type1);
5126 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
5127 (cmp (convert:newtype @0) (convert:newtype @1))))))
5131 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
5133 /* a CMP (-0) -> a CMP 0 */
5134 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5135 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5136 /* (-0) CMP b -> 0 CMP b. */
5137 (if (TREE_CODE (@0) == REAL_CST
5138 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5139 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5140 /* x != NaN is always true, other ops are always false. */
5141 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5142 && !tree_expr_signaling_nan_p (@1)
5143 && !tree_expr_maybe_signaling_nan_p (@0))
5144 { constant_boolean_node (cmp == NE_EXPR, type); })
5145 /* NaN != y is always true, other ops are always false. */
5146 (if (TREE_CODE (@0) == REAL_CST
5147 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5148 && !tree_expr_signaling_nan_p (@0)
5149 && !tree_expr_signaling_nan_p (@1))
5150 { constant_boolean_node (cmp == NE_EXPR, type); })
5151 /* Fold comparisons against infinity. */
5152 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5153 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5156 REAL_VALUE_TYPE max;
5157 enum tree_code code = cmp;
5158 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5160 code = swap_tree_comparison (code);
5163 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
5164 (if (code == GT_EXPR
5165 && !(HONOR_NANS (@0) && flag_trapping_math))
5166 { constant_boolean_node (false, type); })
5167 (if (code == LE_EXPR)
5168 /* x <= +Inf is always true, if we don't care about NaNs. */
5169 (if (! HONOR_NANS (@0))
5170 { constant_boolean_node (true, type); }
5171 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5172 an "invalid" exception. */
5173 (if (!flag_trapping_math)
5175 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5176 for == this introduces an exception for x a NaN. */
5177 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5179 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5181 (lt @0 { build_real (TREE_TYPE (@0), max); })
5182 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5183 /* x < +Inf is always equal to x <= DBL_MAX. */
5184 (if (code == LT_EXPR)
5185 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5187 (ge @0 { build_real (TREE_TYPE (@0), max); })
5188 (le @0 { build_real (TREE_TYPE (@0), max); }))))
5189 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5190 an exception for x a NaN so use an unordered comparison. */
5191 (if (code == NE_EXPR)
5192 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5193 (if (! HONOR_NANS (@0))
5195 (ge @0 { build_real (TREE_TYPE (@0), max); })
5196 (le @0 { build_real (TREE_TYPE (@0), max); }))
5198 (unge @0 { build_real (TREE_TYPE (@0), max); })
5199 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5201 /* If this is a comparison of a real constant with a PLUS_EXPR
5202 or a MINUS_EXPR of a real constant, we can convert it into a
5203 comparison with a revised real constant as long as no overflow
5204 occurs when unsafe_math_optimizations are enabled. */
5205 (if (flag_unsafe_math_optimizations)
5206 (for op (plus minus)
5208 (cmp (op @0 REAL_CST@1) REAL_CST@2)
5211 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5212 TREE_TYPE (@1), @2, @1);
5214 (if (tem && !TREE_OVERFLOW (tem))
5215 (cmp @0 { tem; }))))))
5217 /* Likewise, we can simplify a comparison of a real constant with
5218 a MINUS_EXPR whose first operand is also a real constant, i.e.
5219 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
5220 floating-point types only if -fassociative-math is set. */
5221 (if (flag_associative_math)
5223 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5224 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5225 (if (tem && !TREE_OVERFLOW (tem))
5226 (cmp { tem; } @1)))))
5228 /* Fold comparisons against built-in math functions. */
5229 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5232 (cmp (sq @0) REAL_CST@1)
5234 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5236 /* sqrt(x) < y is always false, if y is negative. */
5237 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5238 { constant_boolean_node (false, type); })
5239 /* sqrt(x) > y is always true, if y is negative and we
5240 don't care about NaNs, i.e. negative values of x. */
5241 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5242 { constant_boolean_node (true, type); })
5243 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5244 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5245 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5247 /* sqrt(x) < 0 is always false. */
5248 (if (cmp == LT_EXPR)
5249 { constant_boolean_node (false, type); })
5250 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
5251 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5252 { constant_boolean_node (true, type); })
5253 /* sqrt(x) <= 0 -> x == 0. */
5254 (if (cmp == LE_EXPR)
5256 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
5257 == or !=. In the last case:
5259 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5261 if x is negative or NaN. Due to -funsafe-math-optimizations,
5262 the results for other x follow from natural arithmetic. */
5264 (if ((cmp == LT_EXPR
5268 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5269 /* Give up for -frounding-math. */
5270 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5274 enum tree_code ncmp = cmp;
5275 const real_format *fmt
5276 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5277 real_arithmetic (&c2, MULT_EXPR,
5278 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5279 real_convert (&c2, fmt, &c2);
5280 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5281 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
5282 if (!REAL_VALUE_ISINF (c2))
5284 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5285 build_real (TREE_TYPE (@0), c2));
5286 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5288 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5289 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5290 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5291 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5292 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5293 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5296 /* With rounding to even, sqrt of up to 3 different values
5297 gives the same normal result, so in some cases c2 needs
5299 REAL_VALUE_TYPE c2alt, tow;
5300 if (cmp == LT_EXPR || cmp == GE_EXPR)
5304 real_nextafter (&c2alt, fmt, &c2, &tow);
5305 real_convert (&c2alt, fmt, &c2alt);
5306 if (REAL_VALUE_ISINF (c2alt))
5310 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5311 build_real (TREE_TYPE (@0), c2alt));
5312 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5314 else if (real_equal (&TREE_REAL_CST (c3),
5315 &TREE_REAL_CST (@1)))
5321 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5322 (if (REAL_VALUE_ISINF (c2))
5323 /* sqrt(x) > y is x == +Inf, when y is very large. */
5324 (if (HONOR_INFINITIES (@0))
5325 (eq @0 { build_real (TREE_TYPE (@0), c2); })
5326 { constant_boolean_node (false, type); })
5327 /* sqrt(x) > c is the same as x > c*c. */
5328 (if (ncmp != ERROR_MARK)
5329 (if (ncmp == GE_EXPR)
5330 (ge @0 { build_real (TREE_TYPE (@0), c2); })
5331 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5332 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
5333 (if (REAL_VALUE_ISINF (c2))
5335 /* sqrt(x) < y is always true, when y is a very large
5336 value and we don't care about NaNs or Infinities. */
5337 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5338 { constant_boolean_node (true, type); })
5339 /* sqrt(x) < y is x != +Inf when y is very large and we
5340 don't care about NaNs. */
5341 (if (! HONOR_NANS (@0))
5342 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5343 /* sqrt(x) < y is x >= 0 when y is very large and we
5344 don't care about Infinities. */
5345 (if (! HONOR_INFINITIES (@0))
5346 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5347 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5350 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5351 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5352 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5353 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5354 (if (ncmp == LT_EXPR)
5355 (lt @0 { build_real (TREE_TYPE (@0), c2); })
5356 (le @0 { build_real (TREE_TYPE (@0), c2); }))
5357 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5358 (if (ncmp != ERROR_MARK && GENERIC)
5359 (if (ncmp == LT_EXPR)
5361 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5362 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5364 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5365 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5366 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
5368 (cmp (sq @0) (sq @1))
5369 (if (! HONOR_NANS (@0))
5372 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
5373 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5374 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
5376 (cmp (float@0 @1) (float @2))
5377 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5378 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5381 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5382 tree type1 = TREE_TYPE (@1);
5383 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5384 tree type2 = TREE_TYPE (@2);
5385 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5387 (if (fmt.can_represent_integral_type_p (type1)
5388 && fmt.can_represent_integral_type_p (type2))
5389 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5390 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5391 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5392 && type1_signed_p >= type2_signed_p)
5393 (icmp @1 (convert @2))
5394 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5395 && type1_signed_p <= type2_signed_p)
5396 (icmp (convert:type2 @1) @2)
5397 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5398 && type1_signed_p == type2_signed_p)
5399 (icmp @1 @2))))))))))
5401 /* Optimize various special cases of (FTYPE) N CMP CST. */
5402 (for cmp (lt le eq ne ge gt)
5403 icmp (le le eq ne ge ge)
5405 (cmp (float @0) REAL_CST@1)
5406 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5407 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5410 tree itype = TREE_TYPE (@0);
5411 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5412 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5413 /* Be careful to preserve any potential exceptions due to
5414 NaNs. qNaNs are ok in == or != context.
5415 TODO: relax under -fno-trapping-math or
5416 -fno-signaling-nans. */
5418 = real_isnan (cst) && (cst->signalling
5419 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5421 /* TODO: allow non-fitting itype and SNaNs when
5422 -fno-trapping-math. */
5423 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5426 signop isign = TYPE_SIGN (itype);
5427 REAL_VALUE_TYPE imin, imax;
5428 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5429 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5431 REAL_VALUE_TYPE icst;
5432 if (cmp == GT_EXPR || cmp == GE_EXPR)
5433 real_ceil (&icst, fmt, cst);
5434 else if (cmp == LT_EXPR || cmp == LE_EXPR)
5435 real_floor (&icst, fmt, cst);
5437 real_trunc (&icst, fmt, cst);
5439 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5441 bool overflow_p = false;
5443 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5446 /* Optimize cases when CST is outside of ITYPE's range. */
5447 (if (real_compare (LT_EXPR, cst, &imin))
5448 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5450 (if (real_compare (GT_EXPR, cst, &imax))
5451 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5453 /* Remove cast if CST is an integer representable by ITYPE. */
5455 (cmp @0 { gcc_assert (!overflow_p);
5456 wide_int_to_tree (itype, icst_val); })
5458 /* When CST is fractional, optimize
5459 (FTYPE) N == CST -> 0
5460 (FTYPE) N != CST -> 1. */
5461 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5462 { constant_boolean_node (cmp == NE_EXPR, type); })
5463 /* Otherwise replace with sensible integer constant. */
5466 gcc_checking_assert (!overflow_p);
5468 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5470 /* Fold A /[ex] B CMP C to A CMP B * C. */
5473 (cmp (exact_div @0 @1) INTEGER_CST@2)
5474 (if (!integer_zerop (@1))
5475 (if (wi::to_wide (@2) == 0)
5477 (if (TREE_CODE (@1) == INTEGER_CST)
5480 wi::overflow_type ovf;
5481 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5482 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5485 { constant_boolean_node (cmp == NE_EXPR, type); }
5486 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5487 (for cmp (lt le gt ge)
5489 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5490 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5493 wi::overflow_type ovf;
5494 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5495 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5498 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5499 TYPE_SIGN (TREE_TYPE (@2)))
5500 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5501 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5503 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5505 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5506 For large C (more than min/B+2^size), this is also true, with the
5507 multiplication computed modulo 2^size.
5508 For intermediate C, this just tests the sign of A. */
5509 (for cmp (lt le gt ge)
5512 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5513 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5514 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5515 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5518 tree utype = TREE_TYPE (@2);
5519 wide_int denom = wi::to_wide (@1);
5520 wide_int right = wi::to_wide (@2);
5521 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5522 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5523 bool small = wi::leu_p (right, smax);
5524 bool large = wi::geu_p (right, smin);
5526 (if (small || large)
5527 (cmp (convert:utype @0) (mult @2 (convert @1)))
5528 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5530 /* Unordered tests if either argument is a NaN. */
5532 (bit_ior (unordered @0 @0) (unordered @1 @1))
5533 (if (types_match (@0, @1))
5536 (bit_and (ordered @0 @0) (ordered @1 @1))
5537 (if (types_match (@0, @1))
5540 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5543 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5546 /* Simple range test simplifications. */
5547 /* A < B || A >= B -> true. */
5548 (for test1 (lt le le le ne ge)
5549 test2 (ge gt ge ne eq ne)
5551 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5552 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5553 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5554 { constant_boolean_node (true, type); })))
5555 /* A < B && A >= B -> false. */
5556 (for test1 (lt lt lt le ne eq)
5557 test2 (ge gt eq gt eq gt)
5559 (bit_and:c (test1 @0 @1) (test2 @0 @1))
5560 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5561 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5562 { constant_boolean_node (false, type); })))
5564 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5565 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
5567 Note that comparisons
5568 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
5569 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
5570 will be canonicalized to above so there's no need to
5577 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5578 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5581 tree ty = TREE_TYPE (@0);
5582 unsigned prec = TYPE_PRECISION (ty);
5583 wide_int mask = wi::to_wide (@2, prec);
5584 wide_int rhs = wi::to_wide (@3, prec);
5585 signop sgn = TYPE_SIGN (ty);
5587 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5588 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5589 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5590 { build_zero_cst (ty); }))))))
5592 /* -A CMP -B -> B CMP A. */
5593 (for cmp (tcc_comparison)
5594 scmp (swapped_tcc_comparison)
5596 (cmp (negate @0) (negate @1))
5597 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5598 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5599 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5602 (cmp (negate @0) CONSTANT_CLASS_P@1)
5603 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5604 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5605 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5606 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5607 (if (tem && !TREE_OVERFLOW (tem))
5608 (scmp @0 { tem; }))))))
5610 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
5613 (op (abs @0) zerop@1)
5616 /* From fold_sign_changed_comparison and fold_widened_comparison.
5617 FIXME: the lack of symmetry is disturbing. */
5618 (for cmp (simple_comparison)
5620 (cmp (convert@0 @00) (convert?@1 @10))
5621 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5622 /* Disable this optimization if we're casting a function pointer
5623 type on targets that require function pointer canonicalization. */
5624 && !(targetm.have_canonicalize_funcptr_for_compare ()
5625 && ((POINTER_TYPE_P (TREE_TYPE (@00))
5626 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5627 || (POINTER_TYPE_P (TREE_TYPE (@10))
5628 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5630 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5631 && (TREE_CODE (@10) == INTEGER_CST
5633 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5636 && !POINTER_TYPE_P (TREE_TYPE (@00))
5637 /* (int)bool:32 != (int)uint is not the same as
5638 bool:32 != (bool:32)uint since boolean types only have two valid
5639 values independent of their precision. */
5640 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
5641 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
5642 /* ??? The special-casing of INTEGER_CST conversion was in the original
5643 code and here to avoid a spurious overflow flag on the resulting
5644 constant which fold_convert produces. */
5645 (if (TREE_CODE (@1) == INTEGER_CST)
5646 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5647 TREE_OVERFLOW (@1)); })
5648 (cmp @00 (convert @1)))
5650 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5651 /* If possible, express the comparison in the shorter mode. */
5652 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5653 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5654 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5655 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5656 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5657 || ((TYPE_PRECISION (TREE_TYPE (@00))
5658 >= TYPE_PRECISION (TREE_TYPE (@10)))
5659 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5660 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5661 || (TREE_CODE (@10) == INTEGER_CST
5662 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5663 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5664 (cmp @00 (convert @10))
5665 (if (TREE_CODE (@10) == INTEGER_CST
5666 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5667 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5670 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5671 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5672 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5673 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5675 (if (above || below)
5676 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5677 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5678 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5679 { constant_boolean_node (above ? true : false, type); }
5680 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5681 { constant_boolean_node (above ? false : true, type); }))))))))))))
5685 /* SSA names are canonicalized to 2nd place. */
5686 (cmp addr@0 SSA_NAME@1)
5688 { poly_int64 off; tree base; }
5689 /* A local variable can never be pointed to by
5690 the default SSA name of an incoming parameter. */
5691 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5692 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5693 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5694 && TREE_CODE (base) == VAR_DECL
5695 && auto_var_in_fn_p (base, current_function_decl))
5696 (if (cmp == NE_EXPR)
5697 { constant_boolean_node (true, type); }
5698 { constant_boolean_node (false, type); })
5699 /* If the address is based on @1 decide using the offset. */
5700 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5701 && TREE_CODE (base) == MEM_REF
5702 && TREE_OPERAND (base, 0) == @1)
5703 (with { off += mem_ref_offset (base).force_shwi (); }
5704 (if (known_ne (off, 0))
5705 { constant_boolean_node (cmp == NE_EXPR, type); }
5706 (if (known_eq (off, 0))
5707 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5709 /* Equality compare simplifications from fold_binary */
5712 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5713 Similarly for NE_EXPR. */
5715 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5716 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5717 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5718 { constant_boolean_node (cmp == NE_EXPR, type); }))
5720 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5722 (cmp (bit_xor @0 @1) integer_zerop)
5725 /* (X ^ Y) == Y becomes X == 0.
5726 Likewise (X ^ Y) == X becomes Y == 0. */
5728 (cmp:c (bit_xor:c @0 @1) @0)
5729 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5731 /* (X & Y) == X becomes (X & ~Y) == 0. */
5733 (cmp:c (bit_and:c @0 @1) @0)
5734 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5736 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5737 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5738 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5739 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5740 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5741 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5742 && !wi::neg_p (wi::to_wide (@1)))
5743 (cmp (bit_and @0 (convert (bit_not @1)))
5744 { build_zero_cst (TREE_TYPE (@0)); })))
5746 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5748 (cmp:c (bit_ior:c @0 @1) @1)
5749 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5751 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5753 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5754 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5755 (cmp @0 (bit_xor @1 (convert @2)))))
5758 (cmp (convert? addr@0) integer_zerop)
5759 (if (tree_single_nonzero_warnv_p (@0, NULL))
5760 { constant_boolean_node (cmp == NE_EXPR, type); }))
5762 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5764 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5765 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5767 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5768 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5769 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5770 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5775 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5776 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5777 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5778 && types_match (@0, @1))
5779 (ncmp (bit_xor @0 @1) @2)))))
5780 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5781 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5785 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5786 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5787 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5788 && types_match (@0, @1))
5789 (ncmp (bit_xor @0 @1) @2))))
5791 /* If we have (A & C) == C where C is a power of 2, convert this into
5792 (A & C) != 0. Similarly for NE_EXPR. */
5796 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5797 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5800 /* From fold_binary_op_with_conditional_arg handle the case of
5801 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
5802 compares simplify. */
5803 (for cmp (simple_comparison)
5805 (cmp:c (cond @0 @1 @2) @3)
5806 /* Do not move possibly trapping operations into the conditional as this
5807 pessimizes code and causes gimplification issues when applied late. */
5808 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
5809 || !operation_could_trap_p (cmp, true, false, @3))
5810 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
5814 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5815 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5817 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5818 (if (INTEGRAL_TYPE_P (type)
5819 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5820 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5821 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5824 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5826 (if (cmp == LT_EXPR)
5827 (bit_xor (convert (rshift @0 {shifter;})) @1)
5828 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5829 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5830 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5832 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5833 (if (INTEGRAL_TYPE_P (type)
5834 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5835 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5836 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5839 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5841 (if (cmp == GE_EXPR)
5842 (bit_xor (convert (rshift @0 {shifter;})) @1)
5843 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5845 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5846 convert this into a shift followed by ANDing with D. */
5849 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5850 INTEGER_CST@2 integer_zerop)
5851 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5853 int shift = (wi::exact_log2 (wi::to_wide (@2))
5854 - wi::exact_log2 (wi::to_wide (@1)));
5858 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5860 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5863 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5864 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5868 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5869 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5870 && type_has_mode_precision_p (TREE_TYPE (@0))
5871 && element_precision (@2) >= element_precision (@0)
5872 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5873 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5874 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5876 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5877 this into a right shift or sign extension followed by ANDing with C. */
5880 (lt @0 integer_zerop)
5881 INTEGER_CST@1 integer_zerop)
5882 (if (integer_pow2p (@1)
5883 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5885 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5889 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5891 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5892 sign extension followed by AND with C will achieve the effect. */
5893 (bit_and (convert @0) @1)))))
5895 /* When the addresses are not directly of decls compare base and offset.
5896 This implements some remaining parts of fold_comparison address
5897 comparisons but still no complete part of it. Still it is good
5898 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5899 (for cmp (simple_comparison)
5901 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5904 poly_int64 off0, off1;
5906 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
5907 off0, off1, GENERIC);
5911 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5912 { constant_boolean_node (known_eq (off0, off1), type); })
5913 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5914 { constant_boolean_node (known_ne (off0, off1), type); })
5915 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5916 { constant_boolean_node (known_lt (off0, off1), type); })
5917 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5918 { constant_boolean_node (known_le (off0, off1), type); })
5919 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5920 { constant_boolean_node (known_ge (off0, off1), type); })
5921 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5922 { constant_boolean_node (known_gt (off0, off1), type); }))
5925 (if (cmp == EQ_EXPR)
5926 { constant_boolean_node (false, type); })
5927 (if (cmp == NE_EXPR)
5928 { constant_boolean_node (true, type); })))))))
5930 /* Simplify pointer equality compares using PTA. */
5934 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5935 && ptrs_compare_unequal (@0, @1))
5936 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5938 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5939 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5940 Disable the transform if either operand is pointer to function.
5941 This broke pr22051-2.c for arm where function pointer
5942 canonicalizaion is not wanted. */
5946 (cmp (convert @0) INTEGER_CST@1)
5947 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5948 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5949 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5950 /* Don't perform this optimization in GENERIC if @0 has reference
5951 type when sanitizing. See PR101210. */
5953 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5954 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5955 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5956 && POINTER_TYPE_P (TREE_TYPE (@1))
5957 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5958 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5959 (cmp @0 (convert @1)))))
5961 /* Non-equality compare simplifications from fold_binary */
5962 (for cmp (lt gt le ge)
5963 /* Comparisons with the highest or lowest possible integer of
5964 the specified precision will have known values. */
5966 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5967 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5968 || POINTER_TYPE_P (TREE_TYPE (@1))
5969 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5970 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5973 tree cst = uniform_integer_cst_p (@1);
5974 tree arg1_type = TREE_TYPE (cst);
5975 unsigned int prec = TYPE_PRECISION (arg1_type);
5976 wide_int max = wi::max_value (arg1_type);
5977 wide_int signed_max = wi::max_value (prec, SIGNED);
5978 wide_int min = wi::min_value (arg1_type);
5981 (if (wi::to_wide (cst) == max)
5983 (if (cmp == GT_EXPR)
5984 { constant_boolean_node (false, type); })
5985 (if (cmp == GE_EXPR)
5987 (if (cmp == LE_EXPR)
5988 { constant_boolean_node (true, type); })
5989 (if (cmp == LT_EXPR)
5991 (if (wi::to_wide (cst) == min)
5993 (if (cmp == LT_EXPR)
5994 { constant_boolean_node (false, type); })
5995 (if (cmp == LE_EXPR)
5997 (if (cmp == GE_EXPR)
5998 { constant_boolean_node (true, type); })
5999 (if (cmp == GT_EXPR)
6001 (if (wi::to_wide (cst) == max - 1)
6003 (if (cmp == GT_EXPR)
6004 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6005 wide_int_to_tree (TREE_TYPE (cst),
6008 (if (cmp == LE_EXPR)
6009 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6010 wide_int_to_tree (TREE_TYPE (cst),
6013 (if (wi::to_wide (cst) == min + 1)
6015 (if (cmp == GE_EXPR)
6016 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6017 wide_int_to_tree (TREE_TYPE (cst),
6020 (if (cmp == LT_EXPR)
6021 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6022 wide_int_to_tree (TREE_TYPE (cst),
6025 (if (wi::to_wide (cst) == signed_max
6026 && TYPE_UNSIGNED (arg1_type)
6027 /* We will flip the signedness of the comparison operator
6028 associated with the mode of @1, so the sign bit is
6029 specified by this mode. Check that @1 is the signed
6030 max associated with this sign bit. */
6031 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6032 /* signed_type does not work on pointer types. */
6033 && INTEGRAL_TYPE_P (arg1_type))
6034 /* The following case also applies to X < signed_max+1
6035 and X >= signed_max+1 because previous transformations. */
6036 (if (cmp == LE_EXPR || cmp == GT_EXPR)
6037 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6039 (if (cst == @1 && cmp == LE_EXPR)
6040 (ge (convert:st @0) { build_zero_cst (st); }))
6041 (if (cst == @1 && cmp == GT_EXPR)
6042 (lt (convert:st @0) { build_zero_cst (st); }))
6043 (if (cmp == LE_EXPR)
6044 (ge (view_convert:st @0) { build_zero_cst (st); }))
6045 (if (cmp == GT_EXPR)
6046 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6048 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6049 /* If the second operand is NaN, the result is constant. */
6052 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6053 && (cmp != LTGT_EXPR || ! flag_trapping_math))
6054 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6055 ? false : true, type); })))
6057 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
6061 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6062 { constant_boolean_node (true, type); })
6063 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6064 { constant_boolean_node (false, type); })))
6066 /* Fold ORDERED if either operand must be NaN, or neither can be. */
6070 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6071 { constant_boolean_node (false, type); })
6072 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6073 { constant_boolean_node (true, type); })))
6075 /* bool_var != 0 becomes bool_var. */
6077 (ne @0 integer_zerop)
6078 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6079 && types_match (type, TREE_TYPE (@0)))
6081 /* bool_var == 1 becomes bool_var. */
6083 (eq @0 integer_onep)
6084 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6085 && types_match (type, TREE_TYPE (@0)))
6088 bool_var == 0 becomes !bool_var or
6089 bool_var != 1 becomes !bool_var
6090 here because that only is good in assignment context as long
6091 as we require a tcc_comparison in GIMPLE_CONDs where we'd
6092 replace if (x == 0) with tem = ~x; if (tem != 0) which is
6093 clearly less optimal and which we'll transform again in forwprop. */
6095 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6096 where ~Y + 1 == pow2 and Z = ~Y. */
6097 (for cst (VECTOR_CST INTEGER_CST)
6101 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6102 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6103 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6104 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6105 ? optab_vector : optab_default;
6106 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6107 (if (target_supports_op_p (utype, icmp, optab)
6108 || (optimize_vectors_before_lowering_p ()
6109 && (!target_supports_op_p (type, cmp, optab)
6110 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6111 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6113 (icmp (view_convert:utype @0) { csts; })))))))))
6115 /* When one argument is a constant, overflow detection can be simplified.
6116 Currently restricted to single use so as not to interfere too much with
6117 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6118 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
6119 (for cmp (lt le ge gt)
6122 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6123 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6124 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6125 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6126 && wi::to_wide (@1) != 0
6129 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6130 signop sign = TYPE_SIGN (TREE_TYPE (@0));
6132 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6133 wi::max_value (prec, sign)
6134 - wi::to_wide (@1)); })))))
6136 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6137 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6138 expects the long form, so we restrict the transformation for now. */
6141 (cmp:c (minus@2 @0 @1) @0)
6142 (if (single_use (@2)
6143 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6144 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6147 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
6150 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6151 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6152 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6155 /* Testing for overflow is unnecessary if we already know the result. */
6160 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6161 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6162 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6163 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6168 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6169 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6170 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6171 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6173 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6174 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
6178 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6179 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6180 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6181 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6183 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6184 is at least twice as wide as type of A and B, simplify to
6185 __builtin_mul_overflow (A, B, <unused>). */
6188 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6190 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6191 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6192 && TYPE_UNSIGNED (TREE_TYPE (@0))
6193 && (TYPE_PRECISION (TREE_TYPE (@3))
6194 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6195 && tree_fits_uhwi_p (@2)
6196 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6197 && types_match (@0, @1)
6198 && type_has_mode_precision_p (TREE_TYPE (@0))
6199 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6200 != CODE_FOR_nothing))
6201 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6202 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6204 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
6205 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6207 (ovf (convert@2 @0) @1)
6208 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6209 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6210 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6211 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6214 (ovf @1 (convert@2 @0))
6215 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6216 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6217 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6218 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6221 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6222 are unsigned to x > (umax / cst). Similarly for signed type, but
6223 in that case it needs to be outside of a range. */
6225 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6226 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6227 && TYPE_MAX_VALUE (TREE_TYPE (@0))
6228 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6229 && int_fits_type_p (@1, TREE_TYPE (@0)))
6230 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6231 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6232 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6233 (if (integer_minus_onep (@1))
6234 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6237 tree div = fold_convert (TREE_TYPE (@0), @1);
6238 tree lo = int_const_binop (TRUNC_DIV_EXPR,
6239 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6240 tree hi = int_const_binop (TRUNC_DIV_EXPR,
6241 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6242 tree etype = range_check_type (TREE_TYPE (@0));
6245 if (wi::neg_p (wi::to_wide (div)))
6247 lo = fold_convert (etype, lo);
6248 hi = fold_convert (etype, hi);
6249 hi = int_const_binop (MINUS_EXPR, hi, lo);
6253 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
6255 /* Simplification of math builtins. These rules must all be optimizations
6256 as well as IL simplifications. If there is a possibility that the new
6257 form could be a pessimization, the rule should go in the canonicalization
6258 section that follows this one.
6260 Rules can generally go in this section if they satisfy one of
6263 - the rule describes an identity
6265 - the rule replaces calls with something as simple as addition or
6268 - the rule contains unary calls only and simplifies the surrounding
6269 arithmetic. (The idea here is to exclude non-unary calls in which
6270 one operand is constant and in which the call is known to be cheap
6271 when the operand has that value.) */
6273 (if (flag_unsafe_math_optimizations)
6274 /* Simplify sqrt(x) * sqrt(x) -> x. */
6276 (mult (SQRT_ALL@1 @0) @1)
6277 (if (!tree_expr_maybe_signaling_nan_p (@0))
6280 (for op (plus minus)
6281 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
6285 (rdiv (op @0 @2) @1)))
6287 (for cmp (lt le gt ge)
6288 neg_cmp (gt ge lt le)
6289 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
6291 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6293 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6295 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6296 || (real_zerop (tem) && !real_zerop (@1))))
6298 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6300 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6301 (neg_cmp @0 { tem; })))))))
6303 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
6304 (for root (SQRT CBRT)
6306 (mult (root:s @0) (root:s @1))
6307 (root (mult @0 @1))))
6309 /* Simplify expN(x) * expN(y) -> expN(x+y). */
6310 (for exps (EXP EXP2 EXP10 POW10)
6312 (mult (exps:s @0) (exps:s @1))
6313 (exps (plus @0 @1))))
6315 /* Simplify a/root(b/c) into a*root(c/b). */
6316 (for root (SQRT CBRT)
6318 (rdiv @0 (root:s (rdiv:s @1 @2)))
6319 (mult @0 (root (rdiv @2 @1)))))
6321 /* Simplify x/expN(y) into x*expN(-y). */
6322 (for exps (EXP EXP2 EXP10 POW10)
6324 (rdiv @0 (exps:s @1))
6325 (mult @0 (exps (negate @1)))))
6327 (for logs (LOG LOG2 LOG10 LOG10)
6328 exps (EXP EXP2 EXP10 POW10)
6329 /* logN(expN(x)) -> x. */
6333 /* expN(logN(x)) -> x. */
6338 /* Optimize logN(func()) for various exponential functions. We
6339 want to determine the value "x" and the power "exponent" in
6340 order to transform logN(x**exponent) into exponent*logN(x). */
6341 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
6342 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
6345 (if (SCALAR_FLOAT_TYPE_P (type))
6351 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
6352 x = build_real_truncate (type, dconst_e ());
6355 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
6356 x = build_real (type, dconst2);
6360 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
6362 REAL_VALUE_TYPE dconst10;
6363 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6364 x = build_real (type, dconst10);
6371 (mult (logs { x; }) @0)))))
6379 (if (SCALAR_FLOAT_TYPE_P (type))
6385 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
6386 x = build_real (type, dconsthalf);
6389 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
6390 x = build_real_truncate (type, dconst_third ());
6396 (mult { x; } (logs @0))))))
6398 /* logN(pow(x,exponent)) -> exponent*logN(x). */
6399 (for logs (LOG LOG2 LOG10)
6403 (mult @1 (logs @0))))
6405 /* pow(C,x) -> exp(log(C)*x) if C > 0,
6406 or if C is a positive power of 2,
6407 pow(C,x) -> exp2(log2(C)*x). */
6415 (pows REAL_CST@0 @1)
6416 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6417 && real_isfinite (TREE_REAL_CST_PTR (@0))
6418 /* As libmvec doesn't have a vectorized exp2, defer optimizing
6419 the use_exp2 case until after vectorization. It seems actually
6420 beneficial for all constants to postpone this until later,
6421 because exp(log(C)*x), while faster, will have worse precision
6422 and if x folds into a constant too, that is unnecessary
6424 && canonicalize_math_after_vectorization_p ())
6426 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
6427 bool use_exp2 = false;
6428 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
6429 && value->cl == rvc_normal)
6431 REAL_VALUE_TYPE frac_rvt = *value;
6432 SET_REAL_EXP (&frac_rvt, 1);
6433 if (real_equal (&frac_rvt, &dconst1))
6438 (if (optimize_pow_to_exp (@0, @1))
6439 (exps (mult (logs @0) @1)))
6440 (exp2s (mult (log2s @0) @1)))))))
6443 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
6445 exps (EXP EXP2 EXP10 POW10)
6446 logs (LOG LOG2 LOG10 LOG10)
6448 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6449 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6450 && real_isfinite (TREE_REAL_CST_PTR (@0)))
6451 (exps (plus (mult (logs @0) @1) @2)))))
6456 exps (EXP EXP2 EXP10 POW10)
6457 /* sqrt(expN(x)) -> expN(x*0.5). */
6460 (exps (mult @0 { build_real (type, dconsthalf); })))
6461 /* cbrt(expN(x)) -> expN(x/3). */
6464 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6465 /* pow(expN(x), y) -> expN(x*y). */
6468 (exps (mult @0 @1))))
6470 /* tan(atan(x)) -> x. */
6477 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6481 copysigns (COPYSIGN)
6486 REAL_VALUE_TYPE r_cst;
6487 build_sinatan_real (&r_cst, type);
6488 tree t_cst = build_real (type, r_cst);
6489 tree t_one = build_one_cst (type);
6491 (if (SCALAR_FLOAT_TYPE_P (type))
6492 (cond (lt (abs @0) { t_cst; })
6493 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6494 (copysigns { t_one; } @0))))))
6496 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6500 copysigns (COPYSIGN)
6505 REAL_VALUE_TYPE r_cst;
6506 build_sinatan_real (&r_cst, type);
6507 tree t_cst = build_real (type, r_cst);
6508 tree t_one = build_one_cst (type);
6509 tree t_zero = build_zero_cst (type);
6511 (if (SCALAR_FLOAT_TYPE_P (type))
6512 (cond (lt (abs @0) { t_cst; })
6513 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6514 (copysigns { t_zero; } @0))))))
6516 (if (!flag_errno_math)
6517 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6522 (sinhs (atanhs:s @0))
6523 (with { tree t_one = build_one_cst (type); }
6524 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6526 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6531 (coshs (atanhs:s @0))
6532 (with { tree t_one = build_one_cst (type); }
6533 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6535 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
6537 (CABS (complex:C @0 real_zerop@1))
6540 /* trunc(trunc(x)) -> trunc(x), etc. */
6541 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6545 /* f(x) -> x if x is integer valued and f does nothing for such values. */
6546 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6548 (fns integer_valued_real_p@0)
6551 /* hypot(x,0) and hypot(0,x) -> abs(x). */
6553 (HYPOT:c @0 real_zerop@1)
6556 /* pow(1,x) -> 1. */
6558 (POW real_onep@0 @1)
6562 /* copysign(x,x) -> x. */
6563 (COPYSIGN_ALL @0 @0)
6567 /* copysign(x,-x) -> -x. */
6568 (COPYSIGN_ALL @0 (negate@1 @0))
6572 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
6573 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6576 (for scale (LDEXP SCALBN SCALBLN)
6577 /* ldexp(0, x) -> 0. */
6579 (scale real_zerop@0 @1)
6581 /* ldexp(x, 0) -> x. */
6583 (scale @0 integer_zerop@1)
6585 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
6587 (scale REAL_CST@0 @1)
6588 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6591 /* Canonicalization of sequences of math builtins. These rules represent
6592 IL simplifications but are not necessarily optimizations.
6594 The sincos pass is responsible for picking "optimal" implementations
6595 of math builtins, which may be more complicated and can sometimes go
6596 the other way, e.g. converting pow into a sequence of sqrts.
6597 We only want to do these canonicalizations before the pass has run. */
6599 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6600 /* Simplify tan(x) * cos(x) -> sin(x). */
6602 (mult:c (TAN:s @0) (COS:s @0))
6605 /* Simplify x * pow(x,c) -> pow(x,c+1). */
6607 (mult:c @0 (POW:s @0 REAL_CST@1))
6608 (if (!TREE_OVERFLOW (@1))
6609 (POW @0 (plus @1 { build_one_cst (type); }))))
6611 /* Simplify sin(x) / cos(x) -> tan(x). */
6613 (rdiv (SIN:s @0) (COS:s @0))
6616 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6618 (rdiv (SINH:s @0) (COSH:s @0))
6621 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6623 (rdiv (TANH:s @0) (SINH:s @0))
6624 (rdiv {build_one_cst (type);} (COSH @0)))
6626 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6628 (rdiv (COS:s @0) (SIN:s @0))
6629 (rdiv { build_one_cst (type); } (TAN @0)))
6631 /* Simplify sin(x) / tan(x) -> cos(x). */
6633 (rdiv (SIN:s @0) (TAN:s @0))
6634 (if (! HONOR_NANS (@0)
6635 && ! HONOR_INFINITIES (@0))
6638 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6640 (rdiv (TAN:s @0) (SIN:s @0))
6641 (if (! HONOR_NANS (@0)
6642 && ! HONOR_INFINITIES (@0))
6643 (rdiv { build_one_cst (type); } (COS @0))))
6645 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6647 (mult (POW:s @0 @1) (POW:s @0 @2))
6648 (POW @0 (plus @1 @2)))
6650 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6652 (mult (POW:s @0 @1) (POW:s @2 @1))
6653 (POW (mult @0 @2) @1))
6655 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6657 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6658 (POWI (mult @0 @2) @1))
6660 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6662 (rdiv (POW:s @0 REAL_CST@1) @0)
6663 (if (!TREE_OVERFLOW (@1))
6664 (POW @0 (minus @1 { build_one_cst (type); }))))
6666 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6668 (rdiv @0 (POW:s @1 @2))
6669 (mult @0 (POW @1 (negate @2))))
6674 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6677 (pows @0 { build_real (type, dconst_quarter ()); }))
6678 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6681 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6682 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6685 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6686 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6688 (cbrts (cbrts tree_expr_nonnegative_p@0))
6689 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6690 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6692 (sqrts (pows @0 @1))
6693 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6694 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6696 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6697 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6698 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6700 (pows (sqrts @0) @1)
6701 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6702 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6704 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6705 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6706 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6708 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6709 (pows @0 (mult @1 @2))))
6711 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6713 (CABS (complex @0 @0))
6714 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6716 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6719 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6721 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6726 (cexps compositional_complex@0)
6727 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6729 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6730 (mult @1 (imagpart @2)))))))
6732 (if (canonicalize_math_p ())
6733 /* floor(x) -> trunc(x) if x is nonnegative. */
6734 (for floors (FLOOR_ALL)
6737 (floors tree_expr_nonnegative_p@0)
6740 (match double_value_p
6742 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6743 (for froms (BUILT_IN_TRUNCL
6755 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6756 (if (optimize && canonicalize_math_p ())
6758 (froms (convert double_value_p@0))
6759 (convert (tos @0)))))
6761 (match float_value_p
6763 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6764 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6765 BUILT_IN_FLOORL BUILT_IN_FLOOR
6766 BUILT_IN_CEILL BUILT_IN_CEIL
6767 BUILT_IN_ROUNDL BUILT_IN_ROUND
6768 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6769 BUILT_IN_RINTL BUILT_IN_RINT)
6770 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6771 BUILT_IN_FLOORF BUILT_IN_FLOORF
6772 BUILT_IN_CEILF BUILT_IN_CEILF
6773 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6774 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6775 BUILT_IN_RINTF BUILT_IN_RINTF)
6776 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6778 (if (optimize && canonicalize_math_p ()
6779 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6781 (froms (convert float_value_p@0))
6782 (convert (tos @0)))))
6785 (match float16_value_p
6787 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
6788 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
6789 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
6790 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
6791 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
6792 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
6793 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
6794 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
6795 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
6796 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
6797 IFN_FLOOR IFN_FLOOR IFN_FLOOR
6798 IFN_CEIL IFN_CEIL IFN_CEIL
6799 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
6800 IFN_ROUND IFN_ROUND IFN_ROUND
6801 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
6802 IFN_RINT IFN_RINT IFN_RINT
6803 IFN_SQRT IFN_SQRT IFN_SQRT)
6804 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
6805 if x is a _Float16. */
6807 (convert (froms (convert float16_value_p@0)))
6809 && types_match (type, TREE_TYPE (@0))
6810 && direct_internal_fn_supported_p (as_internal_fn (tos),
6811 type, OPTIMIZE_FOR_BOTH))
6814 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
6815 x,y is float value, similar for _Float16/double. */
6816 (for copysigns (COPYSIGN_ALL)
6818 (convert (copysigns (convert@2 @0) (convert @1)))
6820 && !HONOR_SNANS (@2)
6821 && types_match (type, TREE_TYPE (@0))
6822 && types_match (type, TREE_TYPE (@1))
6823 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
6824 && direct_internal_fn_supported_p (IFN_COPYSIGN,
6825 type, OPTIMIZE_FOR_BOTH))
6826 (IFN_COPYSIGN @0 @1))))
6828 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
6829 tos (IFN_FMA IFN_FMA IFN_FMA)
6831 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
6832 (if (flag_unsafe_math_optimizations
6834 && FLOAT_TYPE_P (type)
6835 && FLOAT_TYPE_P (TREE_TYPE (@3))
6836 && types_match (type, TREE_TYPE (@0))
6837 && types_match (type, TREE_TYPE (@1))
6838 && types_match (type, TREE_TYPE (@2))
6839 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
6840 && direct_internal_fn_supported_p (as_internal_fn (tos),
6841 type, OPTIMIZE_FOR_BOTH))
6844 (for maxmin (max min)
6846 (convert (maxmin (convert@2 @0) (convert @1)))
6848 && FLOAT_TYPE_P (type)
6849 && FLOAT_TYPE_P (TREE_TYPE (@2))
6850 && types_match (type, TREE_TYPE (@0))
6851 && types_match (type, TREE_TYPE (@1))
6852 && element_precision (type) < element_precision (TREE_TYPE (@2)))
6856 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6857 tos (XFLOOR XCEIL XROUND XRINT)
6858 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6859 (if (optimize && canonicalize_math_p ())
6861 (froms (convert double_value_p@0))
6864 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6865 XFLOOR XCEIL XROUND XRINT)
6866 tos (XFLOORF XCEILF XROUNDF XRINTF)
6867 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6869 (if (optimize && canonicalize_math_p ())
6871 (froms (convert float_value_p@0))
6874 (if (canonicalize_math_p ())
6875 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6876 (for floors (IFLOOR LFLOOR LLFLOOR)
6878 (floors tree_expr_nonnegative_p@0)
6881 (if (canonicalize_math_p ())
6882 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6883 (for fns (IFLOOR LFLOOR LLFLOOR
6885 IROUND LROUND LLROUND)
6887 (fns integer_valued_real_p@0)
6889 (if (!flag_errno_math)
6890 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6891 (for rints (IRINT LRINT LLRINT)
6893 (rints integer_valued_real_p@0)
6896 (if (canonicalize_math_p ())
6897 (for ifn (IFLOOR ICEIL IROUND IRINT)
6898 lfn (LFLOOR LCEIL LROUND LRINT)
6899 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6900 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6901 sizeof (int) == sizeof (long). */
6902 (if (TYPE_PRECISION (integer_type_node)
6903 == TYPE_PRECISION (long_integer_type_node))
6906 (lfn:long_integer_type_node @0)))
6907 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6908 sizeof (long long) == sizeof (long). */
6909 (if (TYPE_PRECISION (long_long_integer_type_node)
6910 == TYPE_PRECISION (long_integer_type_node))
6913 (lfn:long_integer_type_node @0)))))
6915 /* cproj(x) -> x if we're ignoring infinities. */
6918 (if (!HONOR_INFINITIES (type))
6921 /* If the real part is inf and the imag part is known to be
6922 nonnegative, return (inf + 0i). */
6924 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6925 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6926 { build_complex_inf (type, false); }))
6928 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6930 (CPROJ (complex @0 REAL_CST@1))
6931 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6932 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6938 (pows @0 REAL_CST@1)
6940 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6941 REAL_VALUE_TYPE tmp;
6944 /* pow(x,0) -> 1. */
6945 (if (real_equal (value, &dconst0))
6946 { build_real (type, dconst1); })
6947 /* pow(x,1) -> x. */
6948 (if (real_equal (value, &dconst1))
6950 /* pow(x,-1) -> 1/x. */
6951 (if (real_equal (value, &dconstm1))
6952 (rdiv { build_real (type, dconst1); } @0))
6953 /* pow(x,0.5) -> sqrt(x). */
6954 (if (flag_unsafe_math_optimizations
6955 && canonicalize_math_p ()
6956 && real_equal (value, &dconsthalf))
6958 /* pow(x,1/3) -> cbrt(x). */
6959 (if (flag_unsafe_math_optimizations
6960 && canonicalize_math_p ()
6961 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6962 real_equal (value, &tmp)))
6965 /* powi(1,x) -> 1. */
6967 (POWI real_onep@0 @1)
6971 (POWI @0 INTEGER_CST@1)
6973 /* powi(x,0) -> 1. */
6974 (if (wi::to_wide (@1) == 0)
6975 { build_real (type, dconst1); })
6976 /* powi(x,1) -> x. */
6977 (if (wi::to_wide (@1) == 1)
6979 /* powi(x,-1) -> 1/x. */
6980 (if (wi::to_wide (@1) == -1)
6981 (rdiv { build_real (type, dconst1); } @0))))
6983 /* Narrowing of arithmetic and logical operations.
6985 These are conceptually similar to the transformations performed for
6986 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6987 term we want to move all that code out of the front-ends into here. */
6989 /* Convert (outertype)((innertype0)a+(innertype1)b)
6990 into ((newtype)a+(newtype)b) where newtype
6991 is the widest mode from all of these. */
6992 (for op (plus minus mult rdiv)
6994 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6995 /* If we have a narrowing conversion of an arithmetic operation where
6996 both operands are widening conversions from the same type as the outer
6997 narrowing conversion. Then convert the innermost operands to a
6998 suitable unsigned type (to avoid introducing undefined behavior),
6999 perform the operation and convert the result to the desired type. */
7000 (if (INTEGRAL_TYPE_P (type)
7003 /* We check for type compatibility between @0 and @1 below,
7004 so there's no need to check that @2/@4 are integral types. */
7005 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7006 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7007 /* The precision of the type of each operand must match the
7008 precision of the mode of each operand, similarly for the
7010 && type_has_mode_precision_p (TREE_TYPE (@1))
7011 && type_has_mode_precision_p (TREE_TYPE (@2))
7012 && type_has_mode_precision_p (type)
7013 /* The inner conversion must be a widening conversion. */
7014 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7015 && types_match (@1, type)
7016 && (types_match (@1, @2)
7017 /* Or the second operand is const integer or converted const
7018 integer from valueize. */
7019 || poly_int_tree_p (@4)))
7020 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7021 (op @1 (convert @2))
7022 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7023 (convert (op (convert:utype @1)
7024 (convert:utype @2)))))
7025 (if (FLOAT_TYPE_P (type)
7026 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7027 == DECIMAL_FLOAT_TYPE_P (type))
7028 (with { tree arg0 = strip_float_extensions (@1);
7029 tree arg1 = strip_float_extensions (@2);
7030 tree itype = TREE_TYPE (@0);
7031 tree ty1 = TREE_TYPE (arg0);
7032 tree ty2 = TREE_TYPE (arg1);
7033 enum tree_code code = TREE_CODE (itype); }
7034 (if (FLOAT_TYPE_P (ty1)
7035 && FLOAT_TYPE_P (ty2))
7036 (with { tree newtype = type;
7037 if (TYPE_MODE (ty1) == SDmode
7038 || TYPE_MODE (ty2) == SDmode
7039 || TYPE_MODE (type) == SDmode)
7040 newtype = dfloat32_type_node;
7041 if (TYPE_MODE (ty1) == DDmode
7042 || TYPE_MODE (ty2) == DDmode
7043 || TYPE_MODE (type) == DDmode)
7044 newtype = dfloat64_type_node;
7045 if (TYPE_MODE (ty1) == TDmode
7046 || TYPE_MODE (ty2) == TDmode
7047 || TYPE_MODE (type) == TDmode)
7048 newtype = dfloat128_type_node; }
7049 (if ((newtype == dfloat32_type_node
7050 || newtype == dfloat64_type_node
7051 || newtype == dfloat128_type_node)
7053 && types_match (newtype, type))
7054 (op (convert:newtype @1) (convert:newtype @2))
7055 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
7057 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
7059 /* Sometimes this transformation is safe (cannot
7060 change results through affecting double rounding
7061 cases) and sometimes it is not. If NEWTYPE is
7062 wider than TYPE, e.g. (float)((long double)double
7063 + (long double)double) converted to
7064 (float)(double + double), the transformation is
7065 unsafe regardless of the details of the types
7066 involved; double rounding can arise if the result
7067 of NEWTYPE arithmetic is a NEWTYPE value half way
7068 between two representable TYPE values but the
7069 exact value is sufficiently different (in the
7070 right direction) for this difference to be
7071 visible in ITYPE arithmetic. If NEWTYPE is the
7072 same as TYPE, however, the transformation may be
7073 safe depending on the types involved: it is safe
7074 if the ITYPE has strictly more than twice as many
7075 mantissa bits as TYPE, can represent infinities
7076 and NaNs if the TYPE can, and has sufficient
7077 exponent range for the product or ratio of two
7078 values representable in the TYPE to be within the
7079 range of normal values of ITYPE. */
7080 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
7081 && (flag_unsafe_math_optimizations
7082 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
7083 && real_can_shorten_arithmetic (TYPE_MODE (itype),
7085 && !excess_precision_type (newtype)))
7086 && !types_match (itype, newtype))
7087 (convert:type (op (convert:newtype @1)
7088 (convert:newtype @2)))
7093 /* This is another case of narrowing, specifically when there's an outer
7094 BIT_AND_EXPR which masks off bits outside the type of the innermost
7095 operands. Like the previous case we have to convert the operands
7096 to unsigned types to avoid introducing undefined behavior for the
7097 arithmetic operation. */
7098 (for op (minus plus)
7100 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7101 (if (INTEGRAL_TYPE_P (type)
7102 /* We check for type compatibility between @0 and @1 below,
7103 so there's no need to check that @1/@3 are integral types. */
7104 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7105 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7106 /* The precision of the type of each operand must match the
7107 precision of the mode of each operand, similarly for the
7109 && type_has_mode_precision_p (TREE_TYPE (@0))
7110 && type_has_mode_precision_p (TREE_TYPE (@1))
7111 && type_has_mode_precision_p (type)
7112 /* The inner conversion must be a widening conversion. */
7113 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7114 && types_match (@0, @1)
7115 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7116 <= TYPE_PRECISION (TREE_TYPE (@0)))
7117 && (wi::to_wide (@4)
7118 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7119 true, TYPE_PRECISION (type))) == 0)
7120 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7121 (with { tree ntype = TREE_TYPE (@0); }
7122 (convert (bit_and (op @0 @1) (convert:ntype @4))))
7123 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7124 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7125 (convert:utype @4))))))))
7127 /* Transform (@0 < @1 and @0 < @2) to use min,
7128 (@0 > @1 and @0 > @2) to use max */
7129 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7130 op (lt le gt ge lt le gt ge )
7131 ext (min min max max max max min min )
7133 (logic (op:cs @0 @1) (op:cs @0 @2))
7134 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7135 && TREE_CODE (@0) != INTEGER_CST)
7136 (op @0 (ext @1 @2)))))
7139 /* signbit(x) -> 0 if x is nonnegative. */
7140 (SIGNBIT tree_expr_nonnegative_p@0)
7141 { integer_zero_node; })
7144 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
7146 (if (!HONOR_SIGNED_ZEROS (@0))
7147 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7149 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
7151 (for op (plus minus)
7154 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7155 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7156 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7157 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7158 && !TYPE_SATURATING (TREE_TYPE (@0)))
7159 (with { tree res = int_const_binop (rop, @2, @1); }
7160 (if (TREE_OVERFLOW (res)
7161 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7162 { constant_boolean_node (cmp == NE_EXPR, type); }
7163 (if (single_use (@3))
7164 (cmp @0 { TREE_OVERFLOW (res)
7165 ? drop_tree_overflow (res) : res; }))))))))
7166 (for cmp (lt le gt ge)
7167 (for op (plus minus)
7170 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7171 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7172 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7173 (with { tree res = int_const_binop (rop, @2, @1); }
7174 (if (TREE_OVERFLOW (res))
7176 fold_overflow_warning (("assuming signed overflow does not occur "
7177 "when simplifying conditional to constant"),
7178 WARN_STRICT_OVERFLOW_CONDITIONAL);
7179 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7180 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
7181 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7182 TYPE_SIGN (TREE_TYPE (@1)))
7183 != (op == MINUS_EXPR);
7184 constant_boolean_node (less == ovf_high, type);
7186 (if (single_use (@3))
7189 fold_overflow_warning (("assuming signed overflow does not occur "
7190 "when changing X +- C1 cmp C2 to "
7192 WARN_STRICT_OVERFLOW_COMPARISON);
7194 (cmp @0 { res; })))))))))
7196 /* Canonicalizations of BIT_FIELD_REFs. */
7199 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7200 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7203 (BIT_FIELD_REF (view_convert @0) @1 @2)
7204 (BIT_FIELD_REF @0 @1 @2))
7207 (BIT_FIELD_REF @0 @1 integer_zerop)
7208 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7212 (BIT_FIELD_REF @0 @1 @2)
7214 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7215 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7217 (if (integer_zerop (@2))
7218 (view_convert (realpart @0)))
7219 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7220 (view_convert (imagpart @0)))))
7221 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7222 && INTEGRAL_TYPE_P (type)
7223 /* On GIMPLE this should only apply to register arguments. */
7224 && (! GIMPLE || is_gimple_reg (@0))
7225 /* A bit-field-ref that referenced the full argument can be stripped. */
7226 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
7227 && integer_zerop (@2))
7228 /* Low-parts can be reduced to integral conversions.
7229 ??? The following doesn't work for PDP endian. */
7230 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7231 /* But only do this after vectorization. */
7232 && canonicalize_math_after_vectorization_p ()
7233 /* Don't even think about BITS_BIG_ENDIAN. */
7234 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
7235 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
7236 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
7237 ? (TYPE_PRECISION (TREE_TYPE (@0))
7238 - TYPE_PRECISION (type))
7242 /* Simplify vector extracts. */
7245 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
7246 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
7247 && tree_fits_uhwi_p (TYPE_SIZE (type))
7248 && ((tree_to_uhwi (TYPE_SIZE (type))
7249 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7250 || (VECTOR_TYPE_P (type)
7251 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
7252 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
7255 tree ctor = (TREE_CODE (@0) == SSA_NAME
7256 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7257 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
7258 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
7259 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
7260 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
7263 && (idx % width) == 0
7265 && known_le ((idx + n) / width,
7266 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
7271 /* Constructor elements can be subvectors. */
7273 if (CONSTRUCTOR_NELTS (ctor) != 0)
7275 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7276 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7277 k = TYPE_VECTOR_SUBPARTS (cons_elem);
7279 unsigned HOST_WIDE_INT elt, count, const_k;
7282 /* We keep an exact subset of the constructor elements. */
7283 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7284 (if (CONSTRUCTOR_NELTS (ctor) == 0)
7285 { build_zero_cst (type); }
7287 (if (elt < CONSTRUCTOR_NELTS (ctor))
7288 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7289 { build_zero_cst (type); })
7290 /* We don't want to emit new CTORs unless the old one goes away.
7291 ??? Eventually allow this if the CTOR ends up constant or
7293 (if (single_use (@0))
7296 vec<constructor_elt, va_gc> *vals;
7297 vec_alloc (vals, count);
7298 bool constant_p = true;
7300 for (unsigned i = 0;
7301 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7303 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7304 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7305 if (!CONSTANT_CLASS_P (e))
7308 tree evtype = (types_match (TREE_TYPE (type),
7309 TREE_TYPE (TREE_TYPE (ctor)))
7311 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7313 res = (constant_p ? build_vector_from_ctor (evtype, vals)
7314 : build_constructor (evtype, vals));
7316 (view_convert { res; }))))))
7317 /* The bitfield references a single constructor element. */
7318 (if (k.is_constant (&const_k)
7319 && idx + n <= (idx / const_k + 1) * const_k)
7321 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7322 { build_zero_cst (type); })
7324 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7325 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7326 @1 { bitsize_int ((idx % const_k) * width); })))))))))
7328 /* Simplify a bit extraction from a bit insertion for the cases with
7329 the inserted element fully covering the extraction or the insertion
7330 not touching the extraction. */
7332 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7335 unsigned HOST_WIDE_INT isize;
7336 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7337 isize = TYPE_PRECISION (TREE_TYPE (@1));
7339 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7342 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7343 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
7344 wi::to_wide (@ipos) + isize))
7345 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
7347 - wi::to_wide (@ipos)); }))
7348 (if (wi::geu_p (wi::to_wide (@ipos),
7349 wi::to_wide (@rpos) + wi::to_wide (@rsize))
7350 || wi::geu_p (wi::to_wide (@rpos),
7351 wi::to_wide (@ipos) + isize))
7352 (BIT_FIELD_REF @0 @rsize @rpos)))))
7354 (if (canonicalize_math_after_vectorization_p ())
7357 (fmas:c (negate @0) @1 @2)
7358 (IFN_FNMA @0 @1 @2))
7360 (fmas @0 @1 (negate @2))
7363 (fmas:c (negate @0) @1 (negate @2))
7364 (IFN_FNMS @0 @1 @2))
7366 (negate (fmas@3 @0 @1 @2))
7367 (if (single_use (@3))
7368 (IFN_FNMS @0 @1 @2))))
7371 (IFN_FMS:c (negate @0) @1 @2)
7372 (IFN_FNMS @0 @1 @2))
7374 (IFN_FMS @0 @1 (negate @2))
7377 (IFN_FMS:c (negate @0) @1 (negate @2))
7378 (IFN_FNMA @0 @1 @2))
7380 (negate (IFN_FMS@3 @0 @1 @2))
7381 (if (single_use (@3))
7382 (IFN_FNMA @0 @1 @2)))
7385 (IFN_FNMA:c (negate @0) @1 @2)
7388 (IFN_FNMA @0 @1 (negate @2))
7389 (IFN_FNMS @0 @1 @2))
7391 (IFN_FNMA:c (negate @0) @1 (negate @2))
7394 (negate (IFN_FNMA@3 @0 @1 @2))
7395 (if (single_use (@3))
7396 (IFN_FMS @0 @1 @2)))
7399 (IFN_FNMS:c (negate @0) @1 @2)
7402 (IFN_FNMS @0 @1 (negate @2))
7403 (IFN_FNMA @0 @1 @2))
7405 (IFN_FNMS:c (negate @0) @1 (negate @2))
7408 (negate (IFN_FNMS@3 @0 @1 @2))
7409 (if (single_use (@3))
7410 (IFN_FMA @0 @1 @2))))
7412 /* CLZ simplifications. */
7417 (op (clz:s@2 @0) INTEGER_CST@1)
7418 (if (integer_zerop (@1) && single_use (@2))
7419 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
7420 (with { tree type0 = TREE_TYPE (@0);
7421 tree stype = signed_type_for (type0);
7422 HOST_WIDE_INT val = 0;
7423 /* Punt on hypothetical weird targets. */
7425 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7431 (cmp (convert:stype @0) { build_zero_cst (stype); })))
7432 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
7433 (with { bool ok = true;
7434 HOST_WIDE_INT val = 0;
7435 tree type0 = TREE_TYPE (@0);
7436 /* Punt on hypothetical weird targets. */
7438 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7440 && val == TYPE_PRECISION (type0) - 1)
7443 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7444 (op @0 { build_one_cst (type0); })))))))
7446 /* CTZ simplifications. */
7448 (for op (ge gt le lt)
7451 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
7452 (op (ctz:s @0) INTEGER_CST@1)
7453 (with { bool ok = true;
7454 HOST_WIDE_INT val = 0;
7455 if (!tree_fits_shwi_p (@1))
7459 val = tree_to_shwi (@1);
7460 /* Canonicalize to >= or <. */
7461 if (op == GT_EXPR || op == LE_EXPR)
7463 if (val == HOST_WIDE_INT_MAX)
7469 bool zero_res = false;
7470 HOST_WIDE_INT zero_val = 0;
7471 tree type0 = TREE_TYPE (@0);
7472 int prec = TYPE_PRECISION (type0);
7474 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7479 (if (ok && (!zero_res || zero_val >= val))
7480 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7482 (if (ok && (!zero_res || zero_val < val))
7483 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7484 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7485 (cmp (bit_and @0 { wide_int_to_tree (type0,
7486 wi::mask (val, false, prec)); })
7487 { build_zero_cst (type0); })))))))
7490 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
7491 (op (ctz:s @0) INTEGER_CST@1)
7492 (with { bool zero_res = false;
7493 HOST_WIDE_INT zero_val = 0;
7494 tree type0 = TREE_TYPE (@0);
7495 int prec = TYPE_PRECISION (type0);
7497 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7501 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7502 (if (!zero_res || zero_val != wi::to_widest (@1))
7503 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7504 (if (!zero_res || zero_val < 0 || zero_val >= prec)
7505 (op (bit_and @0 { wide_int_to_tree (type0,
7506 wi::mask (tree_to_uhwi (@1) + 1,
7508 { wide_int_to_tree (type0,
7509 wi::shifted_mask (tree_to_uhwi (@1), 1,
7510 false, prec)); })))))))
7512 /* POPCOUNT simplifications. */
7513 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
7515 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7516 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7517 (POPCOUNT (bit_ior @0 @1))))
7519 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
7520 (for popcount (POPCOUNT)
7521 (for cmp (le eq ne gt)
7524 (cmp (popcount @0) integer_zerop)
7525 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7527 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
7529 (bit_and (POPCOUNT @0) integer_onep)
7532 /* PARITY simplifications. */
7533 /* parity(~X) is parity(X). */
7535 (PARITY (bit_not @0))
7538 /* parity(X)^parity(Y) is parity(X^Y). */
7540 (bit_xor (PARITY:s @0) (PARITY:s @1))
7541 (PARITY (bit_xor @0 @1)))
7543 /* Common POPCOUNT/PARITY simplifications. */
7544 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
7545 (for pfun (POPCOUNT PARITY)
7548 (with { wide_int nz = tree_nonzero_bits (@0); }
7552 (if (wi::popcount (nz) == 1)
7553 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7554 (convert (rshift:utype (convert:utype @0)
7555 { build_int_cst (integer_type_node,
7556 wi::ctz (nz)); }))))))))
7559 /* 64- and 32-bits branchless implementations of popcount are detected:
7561 int popcount64c (uint64_t x)
7563 x -= (x >> 1) & 0x5555555555555555ULL;
7564 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7565 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
7566 return (x * 0x0101010101010101ULL) >> 56;
7569 int popcount32c (uint32_t x)
7571 x -= (x >> 1) & 0x55555555;
7572 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
7573 x = (x + (x >> 4)) & 0x0f0f0f0f;
7574 return (x * 0x01010101) >> 24;
7581 (rshift @8 INTEGER_CST@5)
7583 (bit_and @6 INTEGER_CST@7)
7587 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
7593 /* Check constants and optab. */
7594 (with { unsigned prec = TYPE_PRECISION (type);
7595 int shift = (64 - prec) & 63;
7596 unsigned HOST_WIDE_INT c1
7597 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
7598 unsigned HOST_WIDE_INT c2
7599 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
7600 unsigned HOST_WIDE_INT c3
7601 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
7602 unsigned HOST_WIDE_INT c4
7603 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
7608 && TYPE_UNSIGNED (type)
7609 && integer_onep (@4)
7610 && wi::to_widest (@10) == 2
7611 && wi::to_widest (@5) == 4
7612 && wi::to_widest (@1) == prec - 8
7613 && tree_to_uhwi (@2) == c1
7614 && tree_to_uhwi (@3) == c2
7615 && tree_to_uhwi (@9) == c3
7616 && tree_to_uhwi (@7) == c3
7617 && tree_to_uhwi (@11) == c4)
7618 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
7620 (convert (IFN_POPCOUNT:type @0))
7621 /* Try to do popcount in two halves. PREC must be at least
7622 five bits for this to work without extension before adding. */
7624 tree half_type = NULL_TREE;
7625 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
7628 && m.require () != TYPE_MODE (type))
7630 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
7631 half_type = build_nonstandard_integer_type (half_prec, 1);
7633 gcc_assert (half_prec > 2);
7635 (if (half_type != NULL_TREE
7636 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
7639 (IFN_POPCOUNT:half_type (convert @0))
7640 (IFN_POPCOUNT:half_type (convert (rshift @0
7641 { build_int_cst (integer_type_node, half_prec); } )))))))))))
7643 /* __builtin_ffs needs to deal on many targets with the possible zero
7644 argument. If we know the argument is always non-zero, __builtin_ctz + 1
7645 should lead to better code. */
7647 (FFS tree_expr_nonzero_p@0)
7648 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7649 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
7650 OPTIMIZE_FOR_SPEED))
7651 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7652 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
7655 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
7657 /* __builtin_ffs (X) == 0 -> X == 0.
7658 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
7661 (cmp (ffs@2 @0) INTEGER_CST@1)
7662 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7664 (if (integer_zerop (@1))
7665 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7666 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7667 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7668 (if (single_use (@2))
7669 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7670 wi::mask (tree_to_uhwi (@1),
7672 { wide_int_to_tree (TREE_TYPE (@0),
7673 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7674 false, prec)); }))))))
7676 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
7680 bit_op (bit_and bit_ior)
7682 (cmp (ffs@2 @0) INTEGER_CST@1)
7683 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7685 (if (integer_zerop (@1))
7686 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7687 (if (tree_int_cst_sgn (@1) < 0)
7688 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7689 (if (wi::to_widest (@1) >= prec)
7690 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7691 (if (wi::to_widest (@1) == prec - 1)
7692 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7693 wi::shifted_mask (prec - 1, 1,
7695 (if (single_use (@2))
7696 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7698 { wide_int_to_tree (TREE_TYPE (@0),
7699 wi::mask (tree_to_uhwi (@1),
7701 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7708 --> r = .COND_FN (cond, a, b)
7712 --> r = .COND_FN (~cond, b, a). */
7714 (for uncond_op (UNCOND_UNARY)
7715 cond_op (COND_UNARY)
7717 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
7718 (with { tree op_type = TREE_TYPE (@3); }
7719 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7720 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7721 (cond_op @0 @1 @2))))
7723 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
7724 (with { tree op_type = TREE_TYPE (@3); }
7725 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7726 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7727 (cond_op (bit_not @0) @2 @1)))))
7736 r = c ? a1 op a2 : b;
7738 if the target can do it in one go. This makes the operation conditional
7739 on c, so could drop potentially-trapping arithmetic, but that's a valid
7740 simplification if the result of the operation isn't needed.
7742 Avoid speculatively generating a stand-alone vector comparison
7743 on targets that might not support them. Any target implementing
7744 conditional internal functions must support the same comparisons
7745 inside and outside a VEC_COND_EXPR. */
7747 (for uncond_op (UNCOND_BINARY)
7748 cond_op (COND_BINARY)
7750 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7751 (with { tree op_type = TREE_TYPE (@4); }
7752 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7753 && is_truth_type_for (op_type, TREE_TYPE (@0))
7755 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7757 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7758 (with { tree op_type = TREE_TYPE (@4); }
7759 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7760 && is_truth_type_for (op_type, TREE_TYPE (@0))
7762 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7764 /* Same for ternary operations. */
7765 (for uncond_op (UNCOND_TERNARY)
7766 cond_op (COND_TERNARY)
7768 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7769 (with { tree op_type = TREE_TYPE (@5); }
7770 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7771 && is_truth_type_for (op_type, TREE_TYPE (@0))
7773 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7775 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7776 (with { tree op_type = TREE_TYPE (@5); }
7777 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7778 && is_truth_type_for (op_type, TREE_TYPE (@0))
7780 (view_convert (cond_op (bit_not @0) @2 @3 @4
7781 (view_convert:op_type @1)))))))
7784 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7785 "else" value of an IFN_COND_*. */
7786 (for cond_op (COND_BINARY)
7788 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7789 (with { tree op_type = TREE_TYPE (@3); }
7790 (if (element_precision (type) == element_precision (op_type))
7791 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7793 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7794 (with { tree op_type = TREE_TYPE (@5); }
7795 (if (inverse_conditions_p (@0, @2)
7796 && element_precision (type) == element_precision (op_type))
7797 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7799 /* Same for ternary operations. */
7800 (for cond_op (COND_TERNARY)
7802 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7803 (with { tree op_type = TREE_TYPE (@4); }
7804 (if (element_precision (type) == element_precision (op_type))
7805 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7807 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7808 (with { tree op_type = TREE_TYPE (@6); }
7809 (if (inverse_conditions_p (@0, @2)
7810 && element_precision (type) == element_precision (op_type))
7811 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7813 /* Detect simplication for a conditional reduction where
7816 c = mask2 ? d + a : d
7820 c = mask1 && mask2 ? d + b : d. */
7822 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
7823 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
7825 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7828 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7829 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7831 If pointers are known not to wrap, B checks whether @1 bytes starting
7832 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7833 bytes. A is more efficiently tested as:
7835 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7837 The equivalent expression for B is given by replacing @1 with @1 - 1:
7839 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7841 @0 and @2 can be swapped in both expressions without changing the result.
7843 The folds rely on sizetype's being unsigned (which is always true)
7844 and on its being the same width as the pointer (which we have to check).
7846 The fold replaces two pointer_plus expressions, two comparisons and
7847 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7848 the best case it's a saving of two operations. The A fold retains one
7849 of the original pointer_pluses, so is a win even if both pointer_pluses
7850 are used elsewhere. The B fold is a wash if both pointer_pluses are
7851 used elsewhere, since all we end up doing is replacing a comparison with
7852 a pointer_plus. We do still apply the fold under those circumstances
7853 though, in case applying it to other conditions eventually makes one of the
7854 pointer_pluses dead. */
7855 (for ior (truth_orif truth_or bit_ior)
7858 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7859 (cmp:cs (pointer_plus@4 @2 @1) @0))
7860 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7861 && TYPE_OVERFLOW_WRAPS (sizetype)
7862 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7863 /* Calculate the rhs constant. */
7864 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7865 offset_int rhs = off * 2; }
7866 /* Always fails for negative values. */
7867 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7868 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7869 pick a canonical order. This increases the chances of using the
7870 same pointer_plus in multiple checks. */
7871 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7872 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7873 (if (cmp == LT_EXPR)
7874 (gt (convert:sizetype
7875 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7876 { swap_p ? @0 : @2; }))
7878 (gt (convert:sizetype
7879 (pointer_diff:ssizetype
7880 (pointer_plus { swap_p ? @2 : @0; }
7881 { wide_int_to_tree (sizetype, off); })
7882 { swap_p ? @0 : @2; }))
7883 { rhs_tree; })))))))))
7885 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7887 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7888 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7889 (with { int i = single_nonzero_element (@1); }
7891 (with { tree elt = vector_cst_elt (@1, i);
7892 tree elt_type = TREE_TYPE (elt);
7893 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7894 tree size = bitsize_int (elt_bits);
7895 tree pos = bitsize_int (elt_bits * i); }
7898 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7901 /* Fold reduction of a single nonzero element constructor. */
7902 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7903 (simplify (reduc (CONSTRUCTOR@0))
7904 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
7905 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7906 tree elt = ctor_single_nonzero_element (ctor); }
7908 && !HONOR_SNANS (type)
7909 && !HONOR_SIGNED_ZEROS (type))
7912 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
7913 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
7914 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
7915 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
7916 (simplify (reduc (op @0 VECTOR_CST@1))
7917 (op (reduc:type @0) (reduc:type @1))))
7920 (vec_perm @0 @1 VECTOR_CST@2)
7923 tree op0 = @0, op1 = @1, op2 = @2;
7924 machine_mode result_mode = TYPE_MODE (type);
7925 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
7927 /* Build a vector of integers from the tree mask. */
7928 vec_perm_builder builder;
7929 if (!tree_to_vec_perm_builder (&builder, op2))
7932 /* Create a vec_perm_indices for the integer vector. */
7933 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7934 bool single_arg = (op0 == op1);
7935 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7937 (if (sel.series_p (0, 1, 0, 1))
7939 (if (sel.series_p (0, 1, nelts, 1))
7945 if (sel.all_from_input_p (0))
7947 else if (sel.all_from_input_p (1))
7950 sel.rotate_inputs (1);
7952 else if (known_ge (poly_uint64 (sel[0]), nelts))
7954 std::swap (op0, op1);
7955 sel.rotate_inputs (1);
7959 tree cop0 = op0, cop1 = op1;
7960 if (TREE_CODE (op0) == SSA_NAME
7961 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7962 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7963 cop0 = gimple_assign_rhs1 (def);
7964 if (TREE_CODE (op1) == SSA_NAME
7965 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7966 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7967 cop1 = gimple_assign_rhs1 (def);
7971 (if ((TREE_CODE (cop0) == VECTOR_CST
7972 || TREE_CODE (cop0) == CONSTRUCTOR)
7973 && (TREE_CODE (cop1) == VECTOR_CST
7974 || TREE_CODE (cop1) == CONSTRUCTOR)
7975 && (t = fold_vec_perm (type, cop0, cop1, sel)))
7979 bool changed = (op0 == op1 && !single_arg);
7980 tree ins = NULL_TREE;
7983 /* See if the permutation is performing a single element
7984 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7985 in that case. But only if the vector mode is supported,
7986 otherwise this is invalid GIMPLE. */
7987 if (op_mode != BLKmode
7988 && (TREE_CODE (cop0) == VECTOR_CST
7989 || TREE_CODE (cop0) == CONSTRUCTOR
7990 || TREE_CODE (cop1) == VECTOR_CST
7991 || TREE_CODE (cop1) == CONSTRUCTOR))
7993 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7996 /* After canonicalizing the first elt to come from the
7997 first vector we only can insert the first elt from
7998 the first vector. */
8000 if ((ins = fold_read_from_vector (cop0, sel[0])))
8003 /* The above can fail for two-element vectors which always
8004 appear to insert the first element, so try inserting
8005 into the second lane as well. For more than two
8006 elements that's wasted time. */
8007 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
8009 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
8010 for (at = 0; at < encoded_nelts; ++at)
8011 if (maybe_ne (sel[at], at))
8013 if (at < encoded_nelts
8014 && (known_eq (at + 1, nelts)
8015 || sel.series_p (at + 1, 1, at + 1, 1)))
8017 if (known_lt (poly_uint64 (sel[at]), nelts))
8018 ins = fold_read_from_vector (cop0, sel[at]);
8020 ins = fold_read_from_vector (cop1, sel[at] - nelts);
8025 /* Generate a canonical form of the selector. */
8026 if (!ins && sel.encoding () != builder)
8028 /* Some targets are deficient and fail to expand a single
8029 argument permutation while still allowing an equivalent
8030 2-argument version. */
8032 if (sel.ninputs () == 2
8033 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
8034 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8037 vec_perm_indices sel2 (builder, 2, nelts);
8038 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
8039 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
8041 /* Not directly supported with either encoding,
8042 so use the preferred form. */
8043 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8045 if (!operand_equal_p (op2, oldop2, 0))
8050 (bit_insert { op0; } { ins; }
8051 { bitsize_int (at * vector_element_bits (type)); })
8053 (vec_perm { op0; } { op1; } { op2; }))))))))))
8055 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
8057 (match vec_same_elem_p
8060 (match vec_same_elem_p
8062 (if (TREE_CODE (@0) == SSA_NAME
8063 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
8065 (match vec_same_elem_p
8067 (if (uniform_vector_p (@0))))
8071 (vec_perm vec_same_elem_p@0 @0 @1)
8074 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
8076 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8077 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8078 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
8080 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8081 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8082 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
8085 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
8086 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
8087 constant which when multiplied by a power of 2 contains a unique value
8088 in the top 5 or 6 bits. This is then indexed into a table which maps it
8089 to the number of trailing zeroes. */
8090 (match (ctz_table_index @1 @2 @3)
8091 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
8093 (match (cond_expr_convert_p @0 @2 @3 @6)
8094 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
8095 (if (INTEGRAL_TYPE_P (type)
8096 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8097 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8098 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8099 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
8100 && TYPE_PRECISION (TREE_TYPE (@0))
8101 == TYPE_PRECISION (TREE_TYPE (@2))
8102 && TYPE_PRECISION (TREE_TYPE (@0))
8103 == TYPE_PRECISION (TREE_TYPE (@3))
8104 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
8105 signess when convert is truncation, but not ok for extension since
8106 it's sign_extend vs zero_extend. */
8107 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
8108 || (TYPE_UNSIGNED (TREE_TYPE (@2))
8109 == TYPE_UNSIGNED (TREE_TYPE (@3))))
8111 && single_use (@5))))
8113 (for bit_op (bit_and bit_ior bit_xor)
8114 (match (bitwise_induction_p @0 @2 @3)
8116 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
8119 (match (bitwise_induction_p @0 @2 @3)
8121 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
8123 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
8124 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
8126 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
8127 (with { auto i = wi::neg (wi::to_wide (@2)); }
8128 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
8129 (if (wi::popcount (i) == 1
8130 && (wi::to_wide (@1)) == (i - 1))
8131 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
8133 (cond (le @0 @1) @0 (bit_and @0 @1))))))
8135 /* -x & 1 -> x & 1. */
8137 (bit_and (negate @0) integer_onep@1)