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 * 1, X / 1 -> X. */
343 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
348 /* (A / (1 << B)) -> (A >> B).
349 Only for unsigned A. For signed A, this would not preserve rounding
351 For example: (-1 / ( 1 << B)) != -1 >> B.
352 Also also widening conversions, like:
353 (A / (unsigned long long) (1U << B)) -> (A >> B)
355 (A / (unsigned long long) (1 << B)) -> (A >> B).
356 If the left shift is signed, it can be done only if the upper bits
357 of A starting from shift's type sign bit are zero, as
358 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
359 so it is valid only if A >> 31 is zero. */
361 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
362 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
363 && (!VECTOR_TYPE_P (type)
364 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
365 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
366 && (useless_type_conversion_p (type, TREE_TYPE (@1))
367 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
368 && (TYPE_UNSIGNED (TREE_TYPE (@1))
369 || (element_precision (type)
370 == element_precision (TREE_TYPE (@1)))
371 || (INTEGRAL_TYPE_P (type)
372 && (tree_nonzero_bits (@0)
373 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
375 element_precision (type))) == 0)))))
376 (if (!VECTOR_TYPE_P (type)
377 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
378 && element_precision (TREE_TYPE (@3)) < element_precision (type))
379 (convert (rshift @3 @2))
382 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
383 undefined behavior in constexpr evaluation, and assuming that the division
384 traps enables better optimizations than these anyway. */
385 (for div (trunc_div ceil_div floor_div round_div exact_div)
386 /* 0 / X is always zero. */
388 (div integer_zerop@0 @1)
389 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
390 (if (!integer_zerop (@1))
394 (div @0 integer_minus_onep@1)
395 (if (!TYPE_UNSIGNED (type))
397 /* X / bool_range_Y is X. */
400 (if (INTEGRAL_TYPE_P (type)
401 && ssa_name_has_boolean_range (@1)
402 && !flag_non_call_exceptions)
407 /* But not for 0 / 0 so that we can get the proper warnings and errors.
408 And not for _Fract types where we can't build 1. */
409 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
410 && !integer_zerop (@0)
411 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
412 { build_one_cst (type); }))
413 /* X / abs (X) is X < 0 ? -1 : 1. */
416 (if (INTEGRAL_TYPE_P (type)
417 && TYPE_OVERFLOW_UNDEFINED (type)
418 && !integer_zerop (@0)
419 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
420 (cond (lt @0 { build_zero_cst (type); })
421 { build_minus_one_cst (type); } { build_one_cst (type); })))
424 (div:C @0 (negate @0))
425 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
426 && TYPE_OVERFLOW_UNDEFINED (type)
427 && !integer_zerop (@0)
428 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
429 { build_minus_one_cst (type); })))
431 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
432 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
433 for MOD instead of DIV. */
434 (for floor_divmod (floor_div floor_mod)
435 trunc_divmod (trunc_div trunc_mod)
438 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
439 && TYPE_UNSIGNED (type))
440 (trunc_divmod @0 @1))))
442 /* 1 / X -> X == 1 for unsigned integer X.
443 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
444 But not for 1 / 0 so that we can get proper warnings and errors,
445 and not for 1-bit integers as they are edge cases better handled
448 (trunc_div integer_onep@0 @1)
449 (if (INTEGRAL_TYPE_P (type)
450 && TYPE_PRECISION (type) > 1
451 && !integer_zerop (@1)
452 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
453 (if (TYPE_UNSIGNED (type))
454 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
455 (with { tree utype = unsigned_type_for (type); }
456 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
457 { build_int_cst (utype, 2); })
458 @1 { build_zero_cst (type); })))))
460 /* Combine two successive divisions. Note that combining ceil_div
461 and floor_div is trickier and combining round_div even more so. */
462 (for div (trunc_div exact_div)
464 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
466 wi::overflow_type overflow;
467 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
468 TYPE_SIGN (type), &overflow);
470 (if (div == EXACT_DIV_EXPR
471 || optimize_successive_divisions_p (@2, @3))
473 (div @0 { wide_int_to_tree (type, mul); })
474 (if (TYPE_UNSIGNED (type)
475 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
476 { build_zero_cst (type); }))))))
478 /* Combine successive multiplications. Similar to above, but handling
479 overflow is different. */
481 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
483 wi::overflow_type overflow;
484 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
485 TYPE_SIGN (type), &overflow);
487 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
488 otherwise undefined overflow implies that @0 must be zero. */
489 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
490 (mult @0 { wide_int_to_tree (type, mul); }))))
492 /* Similar to above, but there could be an extra add/sub between
493 successive multuiplications. */
495 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
497 bool overflowed = true;
498 wi::overflow_type ovf1, ovf2;
499 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
500 TYPE_SIGN (type), &ovf1);
501 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
502 TYPE_SIGN (type), &ovf2);
503 if (TYPE_OVERFLOW_UNDEFINED (type))
507 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
508 && get_global_range_query ()->range_of_expr (vr0, @4)
509 && vr0.kind () == VR_RANGE)
511 wide_int wmin0 = vr0.lower_bound ();
512 wide_int wmax0 = vr0.upper_bound ();
513 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
514 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
515 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
517 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
518 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
519 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
528 /* Skip folding on overflow. */
530 (plus (mult @0 { wide_int_to_tree (type, mul); })
531 { wide_int_to_tree (type, add); }))))
533 /* Similar to above, but a multiplication between successive additions. */
535 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
537 bool overflowed = true;
538 wi::overflow_type ovf1;
539 wi::overflow_type ovf2;
540 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
541 TYPE_SIGN (type), &ovf1);
542 wide_int add = wi::add (mul, wi::to_wide (@3),
543 TYPE_SIGN (type), &ovf2);
544 if (TYPE_OVERFLOW_UNDEFINED (type))
548 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
549 && get_global_range_query ()->range_of_expr (vr0, @0)
550 && vr0.kind () == VR_RANGE)
552 wide_int wmin0 = vr0.lower_bound ();
553 wide_int wmax0 = vr0.upper_bound ();
554 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
555 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
556 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
558 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
559 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
560 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
569 /* Skip folding on overflow. */
571 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
573 /* Optimize A / A to 1.0 if we don't care about
574 NaNs or Infinities. */
577 (if (FLOAT_TYPE_P (type)
578 && ! HONOR_NANS (type)
579 && ! HONOR_INFINITIES (type))
580 { build_one_cst (type); }))
582 /* Optimize -A / A to -1.0 if we don't care about
583 NaNs or Infinities. */
585 (rdiv:C @0 (negate @0))
586 (if (FLOAT_TYPE_P (type)
587 && ! HONOR_NANS (type)
588 && ! HONOR_INFINITIES (type))
589 { build_minus_one_cst (type); }))
591 /* PR71078: x / abs(x) -> copysign (1.0, x) */
593 (rdiv:C (convert? @0) (convert? (abs @0)))
594 (if (SCALAR_FLOAT_TYPE_P (type)
595 && ! HONOR_NANS (type)
596 && ! HONOR_INFINITIES (type))
598 (if (types_match (type, float_type_node))
599 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
600 (if (types_match (type, double_type_node))
601 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
602 (if (types_match (type, long_double_type_node))
603 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
605 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
608 (if (!tree_expr_maybe_signaling_nan_p (@0))
611 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
613 (rdiv @0 real_minus_onep)
614 (if (!tree_expr_maybe_signaling_nan_p (@0))
617 (if (flag_reciprocal_math)
618 /* Convert (A/B)/C to A/(B*C). */
620 (rdiv (rdiv:s @0 @1) @2)
621 (rdiv @0 (mult @1 @2)))
623 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
625 (rdiv @0 (mult:s @1 REAL_CST@2))
627 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
629 (rdiv (mult @0 { tem; } ) @1))))
631 /* Convert A/(B/C) to (A/B)*C */
633 (rdiv @0 (rdiv:s @1 @2))
634 (mult (rdiv @0 @1) @2)))
636 /* Simplify x / (- y) to -x / y. */
638 (rdiv @0 (negate @1))
639 (rdiv (negate @0) @1))
641 (if (flag_unsafe_math_optimizations)
642 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
643 Since C / x may underflow to zero, do this only for unsafe math. */
644 (for op (lt le gt ge)
647 (op (rdiv REAL_CST@0 @1) real_zerop@2)
648 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
650 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
652 /* For C < 0, use the inverted operator. */
653 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
656 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
657 (for div (trunc_div ceil_div floor_div round_div exact_div)
659 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
660 (if (integer_pow2p (@2)
661 && tree_int_cst_sgn (@2) > 0
662 && tree_nop_conversion_p (type, TREE_TYPE (@0))
663 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
665 { build_int_cst (integer_type_node,
666 wi::exact_log2 (wi::to_wide (@2))); }))))
668 /* If ARG1 is a constant, we can convert this to a multiply by the
669 reciprocal. This does not have the same rounding properties,
670 so only do this if -freciprocal-math. We can actually
671 always safely do it if ARG1 is a power of two, but it's hard to
672 tell if it is or not in a portable manner. */
673 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
677 (if (flag_reciprocal_math
680 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
682 (mult @0 { tem; } )))
683 (if (cst != COMPLEX_CST)
684 (with { tree inverse = exact_inverse (type, @1); }
686 (mult @0 { inverse; } ))))))))
688 (for mod (ceil_mod floor_mod round_mod trunc_mod)
689 /* 0 % X is always zero. */
691 (mod integer_zerop@0 @1)
692 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
693 (if (!integer_zerop (@1))
695 /* X % 1 is always zero. */
697 (mod @0 integer_onep)
698 { build_zero_cst (type); })
699 /* X % -1 is zero. */
701 (mod @0 integer_minus_onep@1)
702 (if (!TYPE_UNSIGNED (type))
703 { build_zero_cst (type); }))
707 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
708 (if (!integer_zerop (@0))
709 { build_zero_cst (type); }))
710 /* (X % Y) % Y is just X % Y. */
712 (mod (mod@2 @0 @1) @1)
714 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
716 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
717 (if (ANY_INTEGRAL_TYPE_P (type)
718 && TYPE_OVERFLOW_UNDEFINED (type)
719 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
721 { build_zero_cst (type); }))
722 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
723 modulo and comparison, since it is simpler and equivalent. */
726 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
727 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
728 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
729 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
731 /* X % -C is the same as X % C. */
733 (trunc_mod @0 INTEGER_CST@1)
734 (if (TYPE_SIGN (type) == SIGNED
735 && !TREE_OVERFLOW (@1)
736 && wi::neg_p (wi::to_wide (@1))
737 && !TYPE_OVERFLOW_TRAPS (type)
738 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
739 && !sign_bit_p (@1, @1))
740 (trunc_mod @0 (negate @1))))
742 /* X % -Y is the same as X % Y. */
744 (trunc_mod @0 (convert? (negate @1)))
745 (if (INTEGRAL_TYPE_P (type)
746 && !TYPE_UNSIGNED (type)
747 && !TYPE_OVERFLOW_TRAPS (type)
748 && tree_nop_conversion_p (type, TREE_TYPE (@1))
749 /* Avoid this transformation if X might be INT_MIN or
750 Y might be -1, because we would then change valid
751 INT_MIN % -(-1) into invalid INT_MIN % -1. */
752 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
753 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
755 (trunc_mod @0 (convert @1))))
757 /* X - (X / Y) * Y is the same as X % Y. */
759 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
760 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
761 (convert (trunc_mod @0 @1))))
763 /* x * (1 + y / x) - y -> x - y % x */
765 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
766 (if (INTEGRAL_TYPE_P (type))
767 (minus @0 (trunc_mod @1 @0))))
769 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
770 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
771 Also optimize A % (C << N) where C is a power of 2,
772 to A & ((C << N) - 1).
773 Also optimize "A shift (B % C)", if C is a power of 2, to
774 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
775 and assume (B % C) is nonnegative as shifts negative values would
777 (match (power_of_two_cand @1)
779 (match (power_of_two_cand @1)
780 (lshift INTEGER_CST@1 @2))
781 (for mod (trunc_mod floor_mod)
782 (for shift (lshift rshift)
784 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
785 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
786 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
789 (mod @0 (convert? (power_of_two_cand@1 @2)))
790 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
791 /* Allow any integral conversions of the divisor, except
792 conversion from narrower signed to wider unsigned type
793 where if @1 would be negative power of two, the divisor
794 would not be a power of two. */
795 && INTEGRAL_TYPE_P (type)
796 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
797 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
798 || TYPE_UNSIGNED (TREE_TYPE (@1))
799 || !TYPE_UNSIGNED (type))
800 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
801 (with { tree utype = TREE_TYPE (@1);
802 if (!TYPE_OVERFLOW_WRAPS (utype))
803 utype = unsigned_type_for (utype); }
804 (bit_and @0 (convert (minus (convert:utype @1)
805 { build_one_cst (utype); })))))))
807 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
809 (trunc_div (mult @0 integer_pow2p@1) @1)
810 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
811 (bit_and @0 { wide_int_to_tree
812 (type, wi::mask (TYPE_PRECISION (type)
813 - wi::exact_log2 (wi::to_wide (@1)),
814 false, TYPE_PRECISION (type))); })))
816 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
818 (mult (trunc_div @0 integer_pow2p@1) @1)
819 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
820 (bit_and @0 (negate @1))))
822 /* Simplify (t * 2) / 2) -> t. */
823 (for div (trunc_div ceil_div floor_div round_div exact_div)
825 (div (mult:c @0 @1) @1)
826 (if (ANY_INTEGRAL_TYPE_P (type))
827 (if (TYPE_OVERFLOW_UNDEFINED (type))
832 bool overflowed = true;
833 value_range vr0, vr1;
834 if (INTEGRAL_TYPE_P (type)
835 && get_global_range_query ()->range_of_expr (vr0, @0)
836 && get_global_range_query ()->range_of_expr (vr1, @1)
837 && vr0.kind () == VR_RANGE
838 && vr1.kind () == VR_RANGE)
840 wide_int wmin0 = vr0.lower_bound ();
841 wide_int wmax0 = vr0.upper_bound ();
842 wide_int wmin1 = vr1.lower_bound ();
843 wide_int wmax1 = vr1.upper_bound ();
844 /* If the multiplication can't overflow/wrap around, then
845 it can be optimized too. */
846 wi::overflow_type min_ovf, max_ovf;
847 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
848 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
849 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
851 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
852 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
853 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
864 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
869 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
872 (pows (op @0) REAL_CST@1)
873 (with { HOST_WIDE_INT n; }
874 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
876 /* Likewise for powi. */
879 (pows (op @0) INTEGER_CST@1)
880 (if ((wi::to_wide (@1) & 1) == 0)
882 /* Strip negate and abs from both operands of hypot. */
890 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
891 (for copysigns (COPYSIGN_ALL)
893 (copysigns (op @0) @1)
896 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
901 /* Convert absu(x)*absu(x) -> x*x. */
903 (mult (absu@1 @0) @1)
904 (mult (convert@2 @0) @2))
906 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
910 (coss (copysigns @0 @1))
913 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
917 (pows (copysigns @0 @2) REAL_CST@1)
918 (with { HOST_WIDE_INT n; }
919 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
921 /* Likewise for powi. */
925 (pows (copysigns @0 @2) INTEGER_CST@1)
926 (if ((wi::to_wide (@1) & 1) == 0)
931 /* hypot(copysign(x, y), z) -> hypot(x, z). */
933 (hypots (copysigns @0 @1) @2)
935 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
937 (hypots @0 (copysigns @1 @2))
940 /* copysign(x, CST) -> [-]abs (x). */
941 (for copysigns (COPYSIGN_ALL)
943 (copysigns @0 REAL_CST@1)
944 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
948 /* copysign(copysign(x, y), z) -> copysign(x, z). */
949 (for copysigns (COPYSIGN_ALL)
951 (copysigns (copysigns @0 @1) @2)
954 /* copysign(x,y)*copysign(x,y) -> x*x. */
955 (for copysigns (COPYSIGN_ALL)
957 (mult (copysigns@2 @0 @1) @2)
960 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
961 (for ccoss (CCOS CCOSH)
966 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
967 (for ops (conj negate)
973 /* Fold (a * (1 << b)) into (a << b) */
975 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
976 (if (! FLOAT_TYPE_P (type)
977 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
980 /* Fold (1 << (C - x)) where C = precision(type) - 1
981 into ((1 << C) >> x). */
983 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
984 (if (INTEGRAL_TYPE_P (type)
985 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
987 (if (TYPE_UNSIGNED (type))
988 (rshift (lshift @0 @2) @3)
990 { tree utype = unsigned_type_for (type); }
991 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
993 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
995 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
996 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
997 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
998 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
999 (bit_and (convert @0)
1000 { wide_int_to_tree (type,
1001 wi::lshift (wone, wi::to_wide (@2))); }))))
1003 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1004 (for cst (INTEGER_CST VECTOR_CST)
1006 (rshift (negate:s @0) cst@1)
1007 (if (!TYPE_UNSIGNED (type)
1008 && TYPE_OVERFLOW_UNDEFINED (type))
1009 (with { tree stype = TREE_TYPE (@1);
1010 tree bt = truth_type_for (type);
1011 tree zeros = build_zero_cst (type);
1012 tree cst = NULL_TREE; }
1014 /* Handle scalar case. */
1015 (if (INTEGRAL_TYPE_P (type)
1016 /* If we apply the rule to the scalar type before vectorization
1017 we will enforce the result of the comparison being a bool
1018 which will require an extra AND on the result that will be
1019 indistinguishable from when the user did actually want 0
1020 or 1 as the result so it can't be removed. */
1021 && canonicalize_math_after_vectorization_p ()
1022 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1023 (negate (convert (gt @0 { zeros; }))))
1024 /* Handle vector case. */
1025 (if (VECTOR_INTEGER_TYPE_P (type)
1026 /* First check whether the target has the same mode for vector
1027 comparison results as it's operands do. */
1028 && TYPE_MODE (bt) == TYPE_MODE (type)
1029 /* Then check to see if the target is able to expand the comparison
1030 with the given type later on, otherwise we may ICE. */
1031 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1032 && (cst = uniform_integer_cst_p (@1)) != NULL
1033 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1034 (view_convert (gt:bt @0 { zeros; }))))))))
1036 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1038 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1039 (if (flag_associative_math
1042 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1044 (rdiv { tem; } @1)))))
1046 /* Simplify ~X & X as zero. */
1048 (bit_and:c (convert? @0) (convert? (bit_not @0)))
1049 { build_zero_cst (type); })
1051 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1053 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1054 (if (TYPE_UNSIGNED (type))
1055 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1057 (for bitop (bit_and bit_ior)
1059 /* PR35691: Transform
1060 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1061 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1063 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1064 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1065 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1066 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1067 (cmp (bit_ior @0 (convert @1)) @2)))
1069 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1070 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1072 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1073 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1074 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1075 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1076 (cmp (bit_and @0 (convert @1)) @2))))
1078 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1080 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1081 (minus (bit_xor @0 @1) @1))
1083 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1084 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1085 (minus (bit_xor @0 @1) @1)))
1087 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1089 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1090 (minus @1 (bit_xor @0 @1)))
1092 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1093 (for op (bit_ior bit_xor plus)
1095 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
1098 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
1099 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1102 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1104 (bit_ior:c (bit_xor:c @0 @1) @0)
1107 /* (a & ~b) | (a ^ b) --> a ^ b */
1109 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1112 /* (a & ~b) ^ ~a --> ~(a & b) */
1114 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1115 (bit_not (bit_and @0 @1)))
1117 /* (~a & b) ^ a --> (a | b) */
1119 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1122 /* (a | b) & ~(a ^ b) --> a & b */
1124 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1127 /* a | ~(a ^ b) --> a | ~b */
1129 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1130 (bit_ior @0 (bit_not @1)))
1132 /* (a | b) | (a &^ b) --> a | b */
1133 (for op (bit_and bit_xor)
1135 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1138 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1140 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1143 /* ~(~a & b) --> a | ~b */
1145 (bit_not (bit_and:cs (bit_not @0) @1))
1146 (bit_ior @0 (bit_not @1)))
1148 /* ~(~a | b) --> a & ~b */
1150 (bit_not (bit_ior:cs (bit_not @0) @1))
1151 (bit_and @0 (bit_not @1)))
1153 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1155 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1156 (bit_and @3 (bit_not @2)))
1158 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1160 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1163 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1165 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1166 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1168 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1170 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1171 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1173 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1175 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1176 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1177 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1180 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1181 ((A & N) + B) & M -> (A + B) & M
1182 Similarly if (N & M) == 0,
1183 ((A | N) + B) & M -> (A + B) & M
1184 and for - instead of + (or unary - instead of +)
1185 and/or ^ instead of |.
1186 If B is constant and (B & M) == 0, fold into A & M. */
1187 (for op (plus minus)
1188 (for bitop (bit_and bit_ior bit_xor)
1190 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1193 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1194 @3, @4, @1, ERROR_MARK, NULL_TREE,
1197 (convert (bit_and (op (convert:utype { pmop[0]; })
1198 (convert:utype { pmop[1]; }))
1199 (convert:utype @2))))))
1201 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1204 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1205 NULL_TREE, NULL_TREE, @1, bitop, @3,
1208 (convert (bit_and (op (convert:utype { pmop[0]; })
1209 (convert:utype { pmop[1]; }))
1210 (convert:utype @2)))))))
1212 (bit_and (op:s @0 @1) INTEGER_CST@2)
1215 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1216 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1217 NULL_TREE, NULL_TREE, pmop); }
1219 (convert (bit_and (op (convert:utype { pmop[0]; })
1220 (convert:utype { pmop[1]; }))
1221 (convert:utype @2)))))))
1222 (for bitop (bit_and bit_ior bit_xor)
1224 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1227 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1228 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1229 NULL_TREE, NULL_TREE, pmop); }
1231 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1232 (convert:utype @1)))))))
1234 /* X % Y is smaller than Y. */
1237 (cmp (trunc_mod @0 @1) @1)
1238 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1239 { constant_boolean_node (cmp == LT_EXPR, type); })))
1242 (cmp @1 (trunc_mod @0 @1))
1243 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1244 { constant_boolean_node (cmp == GT_EXPR, type); })))
1248 (bit_ior @0 integer_all_onesp@1)
1253 (bit_ior @0 integer_zerop)
1258 (bit_and @0 integer_zerop@1)
1264 (for op (bit_ior bit_xor plus)
1266 (op:c (convert? @0) (convert? (bit_not @0)))
1267 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1272 { build_zero_cst (type); })
1274 /* Canonicalize X ^ ~0 to ~X. */
1276 (bit_xor @0 integer_all_onesp@1)
1281 (bit_and @0 integer_all_onesp)
1284 /* x & x -> x, x | x -> x */
1285 (for bitop (bit_and bit_ior)
1290 /* x & C -> x if we know that x & ~C == 0. */
1293 (bit_and SSA_NAME@0 INTEGER_CST@1)
1294 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1295 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1299 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1301 (bit_not (minus (bit_not @0) @1))
1304 (bit_not (plus:c (bit_not @0) @1))
1307 /* ~(X - Y) -> ~X + Y. */
1309 (bit_not (minus:s @0 @1))
1310 (plus (bit_not @0) @1))
1312 (bit_not (plus:s @0 INTEGER_CST@1))
1313 (if ((INTEGRAL_TYPE_P (type)
1314 && TYPE_UNSIGNED (type))
1315 || (!TYPE_OVERFLOW_SANITIZED (type)
1316 && may_negate_without_overflow_p (@1)))
1317 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1320 /* ~X + Y -> (Y - X) - 1. */
1322 (plus:c (bit_not @0) @1)
1323 (if (ANY_INTEGRAL_TYPE_P (type)
1324 && TYPE_OVERFLOW_WRAPS (type)
1325 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1326 && !integer_all_onesp (@1))
1327 (plus (minus @1 @0) { build_minus_one_cst (type); })
1328 (if (INTEGRAL_TYPE_P (type)
1329 && TREE_CODE (@1) == INTEGER_CST
1330 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1332 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1335 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1337 (bit_not (rshift:s @0 @1))
1338 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1339 (rshift (bit_not! @0) @1)
1340 /* For logical right shifts, this is possible only if @0 doesn't
1341 have MSB set and the logical right shift is changed into
1342 arithmetic shift. */
1343 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1344 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1345 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1347 /* x + (x & 1) -> (x + 1) & ~1 */
1349 (plus:c @0 (bit_and:s @0 integer_onep@1))
1350 (bit_and (plus @0 @1) (bit_not @1)))
1352 /* x & ~(x & y) -> x & ~y */
1353 /* x | ~(x | y) -> x | ~y */
1354 (for bitop (bit_and bit_ior)
1356 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1357 (bitop @0 (bit_not @1))))
1359 /* (~x & y) | ~(x | y) -> ~x */
1361 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1364 /* (x | y) ^ (x | ~y) -> ~x */
1366 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1369 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1371 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1372 (bit_not (bit_xor @0 @1)))
1374 /* (~x | y) ^ (x ^ y) -> x | ~y */
1376 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1377 (bit_ior @0 (bit_not @1)))
1379 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1381 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1382 (bit_not (bit_and @0 @1)))
1384 /* (x | y) & ~x -> y & ~x */
1385 /* (x & y) | ~x -> y | ~x */
1386 (for bitop (bit_and bit_ior)
1387 rbitop (bit_ior bit_and)
1389 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1392 /* (x & y) ^ (x | y) -> x ^ y */
1394 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1397 /* (x ^ y) ^ (x | y) -> x & y */
1399 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1402 /* (x & y) + (x ^ y) -> x | y */
1403 /* (x & y) | (x ^ y) -> x | y */
1404 /* (x & y) ^ (x ^ y) -> x | y */
1405 (for op (plus bit_ior bit_xor)
1407 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1410 /* (x & y) + (x | y) -> x + y */
1412 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1415 /* (x + y) - (x | y) -> x & y */
1417 (minus (plus @0 @1) (bit_ior @0 @1))
1418 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1419 && !TYPE_SATURATING (type))
1422 /* (x + y) - (x & y) -> x | y */
1424 (minus (plus @0 @1) (bit_and @0 @1))
1425 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1426 && !TYPE_SATURATING (type))
1429 /* (x | y) - y -> (x & ~y) */
1431 (minus (bit_ior:cs @0 @1) @1)
1432 (bit_and @0 (bit_not @1)))
1434 /* (x | y) - (x ^ y) -> x & y */
1436 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1439 /* (x | y) - (x & y) -> x ^ y */
1441 (minus (bit_ior @0 @1) (bit_and @0 @1))
1444 /* (x | y) & ~(x & y) -> x ^ y */
1446 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1449 /* (x | y) & (~x ^ y) -> x & y */
1451 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1454 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1456 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1457 (bit_not (bit_xor @0 @1)))
1459 /* (~x | y) ^ (x | ~y) -> x ^ y */
1461 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1464 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1466 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1467 (nop_convert2? (bit_ior @0 @1))))
1469 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1470 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1471 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1472 && !TYPE_SATURATING (TREE_TYPE (@2)))
1473 (bit_not (convert (bit_xor @0 @1)))))
1475 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1477 (nop_convert3? (bit_ior @0 @1)))
1478 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1479 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1480 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1481 && !TYPE_SATURATING (TREE_TYPE (@2)))
1482 (bit_not (convert (bit_xor @0 @1)))))
1484 (minus (nop_convert1? (bit_and @0 @1))
1485 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1487 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1488 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1489 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1490 && !TYPE_SATURATING (TREE_TYPE (@2)))
1491 (bit_not (convert (bit_xor @0 @1)))))
1493 /* ~x & ~y -> ~(x | y)
1494 ~x | ~y -> ~(x & y) */
1495 (for op (bit_and bit_ior)
1496 rop (bit_ior bit_and)
1498 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1499 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1500 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1501 (bit_not (rop (convert @0) (convert @1))))))
1503 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1504 with a constant, and the two constants have no bits in common,
1505 we should treat this as a BIT_IOR_EXPR since this may produce more
1507 (for op (bit_xor plus)
1509 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1510 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1511 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1512 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1513 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1514 (bit_ior (convert @4) (convert @5)))))
1516 /* (X | Y) ^ X -> Y & ~ X*/
1518 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1519 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1520 (convert (bit_and @1 (bit_not @0)))))
1522 /* Convert ~X ^ ~Y to X ^ Y. */
1524 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1525 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1526 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1527 (bit_xor (convert @0) (convert @1))))
1529 /* Convert ~X ^ C to X ^ ~C. */
1531 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1532 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1533 (bit_xor (convert @0) (bit_not @1))))
1535 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1536 (for opo (bit_and bit_xor)
1537 opi (bit_xor bit_and)
1539 (opo:c (opi:cs @0 @1) @1)
1540 (bit_and (bit_not @0) @1)))
1542 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1543 operands are another bit-wise operation with a common input. If so,
1544 distribute the bit operations to save an operation and possibly two if
1545 constants are involved. For example, convert
1546 (A | B) & (A | C) into A | (B & C)
1547 Further simplification will occur if B and C are constants. */
1548 (for op (bit_and bit_ior bit_xor)
1549 rop (bit_ior bit_and bit_and)
1551 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1552 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1553 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1554 (rop (convert @0) (op (convert @1) (convert @2))))))
1556 /* Some simple reassociation for bit operations, also handled in reassoc. */
1557 /* (X & Y) & Y -> X & Y
1558 (X | Y) | Y -> X | Y */
1559 (for op (bit_and bit_ior)
1561 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1563 /* (X ^ Y) ^ Y -> X */
1565 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1567 /* (X & Y) & (X & Z) -> (X & Y) & Z
1568 (X | Y) | (X | Z) -> (X | Y) | Z */
1569 (for op (bit_and bit_ior)
1571 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1572 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1573 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1574 (if (single_use (@5) && single_use (@6))
1575 (op @3 (convert @2))
1576 (if (single_use (@3) && single_use (@4))
1577 (op (convert @1) @5))))))
1578 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1580 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1581 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1582 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1583 (bit_xor (convert @1) (convert @2))))
1585 /* Convert abs (abs (X)) into abs (X).
1586 also absu (absu (X)) into absu (X). */
1592 (absu (convert@2 (absu@1 @0)))
1593 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1596 /* Convert abs[u] (-X) -> abs[u] (X). */
1605 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1607 (abs tree_expr_nonnegative_p@0)
1611 (absu tree_expr_nonnegative_p@0)
1614 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1616 (mult:c (nop_convert1?
1617 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1620 (if (INTEGRAL_TYPE_P (type)
1621 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1622 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1623 (if (TYPE_UNSIGNED (type))
1630 /* A few cases of fold-const.cc negate_expr_p predicate. */
1631 (match negate_expr_p
1633 (if ((INTEGRAL_TYPE_P (type)
1634 && TYPE_UNSIGNED (type))
1635 || (!TYPE_OVERFLOW_SANITIZED (type)
1636 && may_negate_without_overflow_p (t)))))
1637 (match negate_expr_p
1639 (match negate_expr_p
1641 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1642 (match negate_expr_p
1644 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1645 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1647 (match negate_expr_p
1649 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1650 (match negate_expr_p
1652 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1653 || (FLOAT_TYPE_P (type)
1654 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1655 && !HONOR_SIGNED_ZEROS (type)))))
1657 /* (-A) * (-B) -> A * B */
1659 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1660 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1661 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1662 (mult (convert @0) (convert (negate @1)))))
1664 /* -(A + B) -> (-B) - A. */
1666 (negate (plus:c @0 negate_expr_p@1))
1667 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1668 && !HONOR_SIGNED_ZEROS (type))
1669 (minus (negate @1) @0)))
1671 /* -(A - B) -> B - A. */
1673 (negate (minus @0 @1))
1674 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1675 || (FLOAT_TYPE_P (type)
1676 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1677 && !HONOR_SIGNED_ZEROS (type)))
1680 (negate (pointer_diff @0 @1))
1681 (if (TYPE_OVERFLOW_UNDEFINED (type))
1682 (pointer_diff @1 @0)))
1684 /* A - B -> A + (-B) if B is easily negatable. */
1686 (minus @0 negate_expr_p@1)
1687 (if (!FIXED_POINT_TYPE_P (type))
1688 (plus @0 (negate @1))))
1690 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1692 (negate (mult:c@0 @1 negate_expr_p@2))
1693 (if (! TYPE_UNSIGNED (type)
1694 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1696 (mult @1 (negate @2))))
1699 (negate (rdiv@0 @1 negate_expr_p@2))
1700 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1702 (rdiv @1 (negate @2))))
1705 (negate (rdiv@0 negate_expr_p@1 @2))
1706 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1708 (rdiv (negate @1) @2)))
1710 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1712 (negate (convert? (rshift @0 INTEGER_CST@1)))
1713 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1714 && wi::to_wide (@1) == element_precision (type) - 1)
1715 (with { tree stype = TREE_TYPE (@0);
1716 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1717 : unsigned_type_for (stype); }
1718 (if (VECTOR_TYPE_P (type))
1719 (view_convert (rshift (view_convert:ntype @0) @1))
1720 (convert (rshift (convert:ntype @0) @1))))))
1722 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1724 For bitwise binary operations apply operand conversions to the
1725 binary operation result instead of to the operands. This allows
1726 to combine successive conversions and bitwise binary operations.
1727 We combine the above two cases by using a conditional convert. */
1728 (for bitop (bit_and bit_ior bit_xor)
1730 (bitop (convert@2 @0) (convert?@3 @1))
1731 (if (((TREE_CODE (@1) == INTEGER_CST
1732 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1733 && (int_fits_type_p (@1, TREE_TYPE (@0))
1734 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1735 || types_match (@0, @1))
1736 /* ??? This transform conflicts with fold-const.cc doing
1737 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1738 constants (if x has signed type, the sign bit cannot be set
1739 in c). This folds extension into the BIT_AND_EXPR.
1740 Restrict it to GIMPLE to avoid endless recursions. */
1741 && (bitop != BIT_AND_EXPR || GIMPLE)
1742 && (/* That's a good idea if the conversion widens the operand, thus
1743 after hoisting the conversion the operation will be narrower.
1744 It is also a good if the conversion is a nop as moves the
1745 conversion to one side; allowing for combining of the conversions. */
1746 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1747 /* The conversion check for being a nop can only be done at the gimple
1748 level as fold_binary has some re-association code which can conflict
1749 with this if there is a "constant" which is not a full INTEGER_CST. */
1750 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1751 /* It's also a good idea if the conversion is to a non-integer
1753 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1754 /* Or if the precision of TO is not the same as the precision
1756 || !type_has_mode_precision_p (type)
1757 /* In GIMPLE, getting rid of 2 conversions for one new results
1760 && TREE_CODE (@1) != INTEGER_CST
1761 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1763 && single_use (@3))))
1764 (convert (bitop @0 (convert @1)))))
1765 /* In GIMPLE, getting rid of 2 conversions for one new results
1768 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1770 && TREE_CODE (@1) != INTEGER_CST
1771 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1772 && types_match (type, @0))
1773 (bitop @0 (convert @1)))))
1775 (for bitop (bit_and bit_ior)
1776 rbitop (bit_ior bit_and)
1777 /* (x | y) & x -> x */
1778 /* (x & y) | x -> x */
1780 (bitop:c (rbitop:c @0 @1) @0)
1782 /* (~x | y) & x -> x & y */
1783 /* (~x & y) | x -> x | y */
1785 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1788 /* ((x | y) & z) | x -> (z & y) | x */
1790 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1791 (bit_ior (bit_and @2 @1) @0))
1793 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1795 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1796 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1798 /* Combine successive equal operations with constants. */
1799 (for bitop (bit_and bit_ior bit_xor)
1801 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1802 (if (!CONSTANT_CLASS_P (@0))
1803 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1804 folded to a constant. */
1805 (bitop @0 (bitop @1 @2))
1806 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1807 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1808 the values involved are such that the operation can't be decided at
1809 compile time. Try folding one of @0 or @1 with @2 to see whether
1810 that combination can be decided at compile time.
1812 Keep the existing form if both folds fail, to avoid endless
1814 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1816 (bitop @1 { cst1; })
1817 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1819 (bitop @0 { cst2; }))))))))
1821 /* Try simple folding for X op !X, and X op X with the help
1822 of the truth_valued_p and logical_inverted_value predicates. */
1823 (match truth_valued_p
1825 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1826 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1827 (match truth_valued_p
1829 (match truth_valued_p
1832 (match (logical_inverted_value @0)
1834 (match (logical_inverted_value @0)
1835 (bit_not truth_valued_p@0))
1836 (match (logical_inverted_value @0)
1837 (eq @0 integer_zerop))
1838 (match (logical_inverted_value @0)
1839 (ne truth_valued_p@0 integer_truep))
1840 (match (logical_inverted_value @0)
1841 (bit_xor truth_valued_p@0 integer_truep))
1845 (bit_and:c @0 (logical_inverted_value @0))
1846 { build_zero_cst (type); })
1847 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1848 (for op (bit_ior bit_xor)
1850 (op:c truth_valued_p@0 (logical_inverted_value @0))
1851 { constant_boolean_node (true, type); }))
1852 /* X ==/!= !X is false/true. */
1855 (op:c truth_valued_p@0 (logical_inverted_value @0))
1856 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1860 (bit_not (bit_not @0))
1863 (match zero_one_valued_p
1865 (if (INTEGRAL_TYPE_P (type) && tree_nonzero_bits (@0) == 1)))
1866 (match zero_one_valued_p
1869 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
1871 (mult zero_one_valued_p@0 zero_one_valued_p@1)
1872 (if (INTEGRAL_TYPE_P (type))
1875 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
1877 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
1878 (if (INTEGRAL_TYPE_P (type)
1879 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1880 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
1881 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1882 (mult (convert @0) @1)))
1884 /* Narrow integer multiplication by a zero_one_valued_p operand.
1885 Multiplication by [0,1] is guaranteed not to overflow. */
1887 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
1888 (if (INTEGRAL_TYPE_P (type)
1889 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1890 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
1891 (mult (convert @1) (convert @2))))
1893 /* Convert ~ (-A) to A - 1. */
1895 (bit_not (convert? (negate @0)))
1896 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1897 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1898 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1900 /* Convert - (~A) to A + 1. */
1902 (negate (nop_convert? (bit_not @0)))
1903 (plus (view_convert @0) { build_each_one_cst (type); }))
1905 /* (a & b) ^ (a == b) -> !(a | b) */
1906 /* (a & b) == (a ^ b) -> !(a | b) */
1907 (for first_op (bit_xor eq)
1908 second_op (eq bit_xor)
1910 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
1911 (bit_not (bit_ior @0 @1))))
1913 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1915 (bit_not (convert? (minus @0 integer_each_onep)))
1916 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1917 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1918 (convert (negate @0))))
1920 (bit_not (convert? (plus @0 integer_all_onesp)))
1921 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1922 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1923 (convert (negate @0))))
1925 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1927 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1928 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1929 (convert (bit_xor @0 (bit_not @1)))))
1931 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1932 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1933 (convert (bit_xor @0 @1))))
1935 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1937 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1938 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1939 (bit_not (bit_xor (view_convert @0) @1))))
1941 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1943 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1944 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1946 /* Fold A - (A & B) into ~B & A. */
1948 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1949 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1950 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1951 (convert (bit_and (bit_not @1) @0))))
1953 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1954 (if (!canonicalize_math_p ())
1955 (for cmp (gt lt ge le)
1957 (mult (convert (cmp @0 @1)) @2)
1958 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1960 /* For integral types with undefined overflow and C != 0 fold
1961 x * C EQ/NE y * C into x EQ/NE y. */
1964 (cmp (mult:c @0 @1) (mult:c @2 @1))
1965 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1966 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1967 && tree_expr_nonzero_p (@1))
1970 /* For integral types with wrapping overflow and C odd fold
1971 x * C EQ/NE y * C into x EQ/NE y. */
1974 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1975 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1976 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1977 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1980 /* For integral types with undefined overflow and C != 0 fold
1981 x * C RELOP y * C into:
1983 x RELOP y for nonnegative C
1984 y RELOP x for negative C */
1985 (for cmp (lt gt le ge)
1987 (cmp (mult:c @0 @1) (mult:c @2 @1))
1988 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1989 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1990 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1992 (if (TREE_CODE (@1) == INTEGER_CST
1993 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1996 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2000 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2001 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2002 && TYPE_UNSIGNED (TREE_TYPE (@0))
2003 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2004 && (wi::to_wide (@2)
2005 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2006 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2007 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2009 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2010 (for cmp (simple_comparison)
2012 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2013 (if (element_precision (@3) >= element_precision (@0)
2014 && types_match (@0, @1))
2015 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2016 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2018 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2021 tree utype = unsigned_type_for (TREE_TYPE (@0));
2023 (cmp (convert:utype @1) (convert:utype @0)))))
2024 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2025 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2029 tree utype = unsigned_type_for (TREE_TYPE (@0));
2031 (cmp (convert:utype @0) (convert:utype @1)))))))))
2033 /* X / C1 op C2 into a simple range test. */
2034 (for cmp (simple_comparison)
2036 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2037 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2038 && integer_nonzerop (@1)
2039 && !TREE_OVERFLOW (@1)
2040 && !TREE_OVERFLOW (@2))
2041 (with { tree lo, hi; bool neg_overflow;
2042 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2045 (if (code == LT_EXPR || code == GE_EXPR)
2046 (if (TREE_OVERFLOW (lo))
2047 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2048 (if (code == LT_EXPR)
2051 (if (code == LE_EXPR || code == GT_EXPR)
2052 (if (TREE_OVERFLOW (hi))
2053 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2054 (if (code == LE_EXPR)
2058 { build_int_cst (type, code == NE_EXPR); })
2059 (if (code == EQ_EXPR && !hi)
2061 (if (code == EQ_EXPR && !lo)
2063 (if (code == NE_EXPR && !hi)
2065 (if (code == NE_EXPR && !lo)
2068 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2072 tree etype = range_check_type (TREE_TYPE (@0));
2075 hi = fold_convert (etype, hi);
2076 lo = fold_convert (etype, lo);
2077 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2080 (if (etype && hi && !TREE_OVERFLOW (hi))
2081 (if (code == EQ_EXPR)
2082 (le (minus (convert:etype @0) { lo; }) { hi; })
2083 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2085 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2086 (for op (lt le ge gt)
2088 (op (plus:c @0 @2) (plus:c @1 @2))
2089 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2090 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2093 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2094 when C is an unsigned integer constant with only the MSB set, and X and
2095 Y have types of equal or lower integer conversion rank than C's. */
2096 (for op (lt le ge gt)
2098 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2099 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2100 && TYPE_UNSIGNED (TREE_TYPE (@0))
2101 && wi::only_sign_bit_p (wi::to_wide (@0)))
2102 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2103 (op (convert:stype @1) (convert:stype @2))))))
2105 /* For equality and subtraction, this is also true with wrapping overflow. */
2106 (for op (eq ne minus)
2108 (op (plus:c @0 @2) (plus:c @1 @2))
2109 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2110 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2111 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2114 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2115 (for op (lt le ge gt)
2117 (op (minus @0 @2) (minus @1 @2))
2118 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2119 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2121 /* For equality and subtraction, this is also true with wrapping overflow. */
2122 (for op (eq ne minus)
2124 (op (minus @0 @2) (minus @1 @2))
2125 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2126 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2127 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2129 /* And for pointers... */
2130 (for op (simple_comparison)
2132 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2133 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2136 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2137 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2138 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2139 (pointer_diff @0 @1)))
2141 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2142 (for op (lt le ge gt)
2144 (op (minus @2 @0) (minus @2 @1))
2145 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2146 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2148 /* For equality and subtraction, this is also true with wrapping overflow. */
2149 (for op (eq ne minus)
2151 (op (minus @2 @0) (minus @2 @1))
2152 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2153 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2154 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2156 /* And for pointers... */
2157 (for op (simple_comparison)
2159 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2160 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2163 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2164 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2165 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2166 (pointer_diff @1 @0)))
2168 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2169 (for op (lt le gt ge)
2171 (op:c (plus:c@2 @0 @1) @1)
2172 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2173 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2174 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2175 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2176 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2177 /* For equality, this is also true with wrapping overflow. */
2180 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2181 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2182 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2183 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2184 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2185 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2186 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2187 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2189 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2190 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2191 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2192 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2193 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2195 /* X - Y < X is the same as Y > 0 when there is no overflow.
2196 For equality, this is also true with wrapping overflow. */
2197 (for op (simple_comparison)
2199 (op:c @0 (minus@2 @0 @1))
2200 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2201 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2202 || ((op == EQ_EXPR || op == NE_EXPR)
2203 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2204 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2205 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2208 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2209 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2213 (cmp (trunc_div @0 @1) integer_zerop)
2214 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2215 /* Complex ==/!= is allowed, but not </>=. */
2216 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2217 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2220 /* X == C - X can never be true if C is odd. */
2223 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2224 (if (TREE_INT_CST_LOW (@1) & 1)
2225 { constant_boolean_node (cmp == NE_EXPR, type); })))
2227 /* Arguments on which one can call get_nonzero_bits to get the bits
2229 (match with_possible_nonzero_bits
2231 (match with_possible_nonzero_bits
2233 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2234 /* Slightly extended version, do not make it recursive to keep it cheap. */
2235 (match (with_possible_nonzero_bits2 @0)
2236 with_possible_nonzero_bits@0)
2237 (match (with_possible_nonzero_bits2 @0)
2238 (bit_and:c with_possible_nonzero_bits@0 @2))
2240 /* Same for bits that are known to be set, but we do not have
2241 an equivalent to get_nonzero_bits yet. */
2242 (match (with_certain_nonzero_bits2 @0)
2244 (match (with_certain_nonzero_bits2 @0)
2245 (bit_ior @1 INTEGER_CST@0))
2247 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2250 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2251 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2252 { constant_boolean_node (cmp == NE_EXPR, type); })))
2254 /* ((X inner_op C0) outer_op C1)
2255 With X being a tree where value_range has reasoned certain bits to always be
2256 zero throughout its computed value range,
2257 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2258 where zero_mask has 1's for all bits that are sure to be 0 in
2260 if (inner_op == '^') C0 &= ~C1;
2261 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2262 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2264 (for inner_op (bit_ior bit_xor)
2265 outer_op (bit_xor bit_ior)
2268 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2272 wide_int zero_mask_not;
2276 if (TREE_CODE (@2) == SSA_NAME)
2277 zero_mask_not = get_nonzero_bits (@2);
2281 if (inner_op == BIT_XOR_EXPR)
2283 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2284 cst_emit = C0 | wi::to_wide (@1);
2288 C0 = wi::to_wide (@0);
2289 cst_emit = C0 ^ wi::to_wide (@1);
2292 (if (!fail && (C0 & zero_mask_not) == 0)
2293 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2294 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2295 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2297 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2299 (pointer_plus (pointer_plus:s @0 @1) @3)
2300 (pointer_plus @0 (plus @1 @3)))
2303 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2304 (convert:type (pointer_plus @0 (plus @1 @3))))
2311 tem4 = (unsigned long) tem3;
2316 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2317 /* Conditionally look through a sign-changing conversion. */
2318 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2319 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2320 || (GENERIC && type == TREE_TYPE (@1))))
2323 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2324 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2328 tem = (sizetype) ptr;
2332 and produce the simpler and easier to analyze with respect to alignment
2333 ... = ptr & ~algn; */
2335 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2336 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2337 (bit_and @0 { algn; })))
2339 /* Try folding difference of addresses. */
2341 (minus (convert ADDR_EXPR@0) (convert @1))
2342 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2343 (with { poly_int64 diff; }
2344 (if (ptr_difference_const (@0, @1, &diff))
2345 { build_int_cst_type (type, diff); }))))
2347 (minus (convert @0) (convert ADDR_EXPR@1))
2348 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2349 (with { poly_int64 diff; }
2350 (if (ptr_difference_const (@0, @1, &diff))
2351 { build_int_cst_type (type, diff); }))))
2353 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2354 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2355 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2356 (with { poly_int64 diff; }
2357 (if (ptr_difference_const (@0, @1, &diff))
2358 { build_int_cst_type (type, diff); }))))
2360 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2361 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2362 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2363 (with { poly_int64 diff; }
2364 (if (ptr_difference_const (@0, @1, &diff))
2365 { build_int_cst_type (type, diff); }))))
2367 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2369 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2370 (with { poly_int64 diff; }
2371 (if (ptr_difference_const (@0, @2, &diff))
2372 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2374 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2377 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2378 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2379 (if (ptr_difference_const (@0, @2, &diff))
2380 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2382 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2384 (convert (pointer_diff @0 INTEGER_CST@1))
2385 (if (POINTER_TYPE_P (type))
2386 { build_fold_addr_expr_with_type
2387 (build2 (MEM_REF, char_type_node, @0,
2388 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2391 /* If arg0 is derived from the address of an object or function, we may
2392 be able to fold this expression using the object or function's
2395 (bit_and (convert? @0) INTEGER_CST@1)
2396 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2397 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2401 unsigned HOST_WIDE_INT bitpos;
2402 get_pointer_alignment_1 (@0, &align, &bitpos);
2404 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2405 { wide_int_to_tree (type, (wi::to_wide (@1)
2406 & (bitpos / BITS_PER_UNIT))); }))))
2410 (if (INTEGRAL_TYPE_P (type)
2411 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2415 (if (INTEGRAL_TYPE_P (type)
2416 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2418 /* x > y && x != XXX_MIN --> x > y
2419 x > y && x == XXX_MIN --> false . */
2422 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2424 (if (eqne == EQ_EXPR)
2425 { constant_boolean_node (false, type); })
2426 (if (eqne == NE_EXPR)
2430 /* x < y && x != XXX_MAX --> x < y
2431 x < y && x == XXX_MAX --> false. */
2434 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2436 (if (eqne == EQ_EXPR)
2437 { constant_boolean_node (false, type); })
2438 (if (eqne == NE_EXPR)
2442 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2444 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2447 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2449 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2452 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2454 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2457 /* x <= y || x != XXX_MIN --> true. */
2459 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2460 { constant_boolean_node (true, type); })
2462 /* x <= y || x == XXX_MIN --> x <= y. */
2464 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2467 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2469 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2472 /* x >= y || x != XXX_MAX --> true
2473 x >= y || x == XXX_MAX --> x >= y. */
2476 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2478 (if (eqne == EQ_EXPR)
2480 (if (eqne == NE_EXPR)
2481 { constant_boolean_node (true, type); }))))
2483 /* y == XXX_MIN || x < y --> x <= y - 1 */
2485 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2486 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2487 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2488 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2490 /* y != XXX_MIN && x >= y --> x > y - 1 */
2492 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2493 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2494 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2495 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2497 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2498 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2501 (for code2 (eq ne lt gt le ge)
2503 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2506 int cmp = tree_int_cst_compare (@1, @2);
2510 case EQ_EXPR: val = (cmp == 0); break;
2511 case NE_EXPR: val = (cmp != 0); break;
2512 case LT_EXPR: val = (cmp < 0); break;
2513 case GT_EXPR: val = (cmp > 0); break;
2514 case LE_EXPR: val = (cmp <= 0); break;
2515 case GE_EXPR: val = (cmp >= 0); break;
2516 default: gcc_unreachable ();
2520 (if (code1 == EQ_EXPR && val) @3)
2521 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2522 (if (code1 == NE_EXPR && !val) @4))))))
2524 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2526 (for code1 (lt le gt ge)
2527 (for code2 (lt le gt ge)
2529 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2532 int cmp = tree_int_cst_compare (@1, @2);
2535 /* Choose the more restrictive of two < or <= comparisons. */
2536 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2537 && (code2 == LT_EXPR || code2 == LE_EXPR))
2538 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2541 /* Likewise chose the more restrictive of two > or >= comparisons. */
2542 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2543 && (code2 == GT_EXPR || code2 == GE_EXPR))
2544 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2547 /* Check for singleton ranges. */
2549 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2550 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2552 /* Check for disjoint ranges. */
2554 && (code1 == LT_EXPR || code1 == LE_EXPR)
2555 && (code2 == GT_EXPR || code2 == GE_EXPR))
2556 { constant_boolean_node (false, type); })
2558 && (code1 == GT_EXPR || code1 == GE_EXPR)
2559 && (code2 == LT_EXPR || code2 == LE_EXPR))
2560 { constant_boolean_node (false, type); })
2563 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2564 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2567 (for code2 (eq ne lt gt le ge)
2569 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2572 int cmp = tree_int_cst_compare (@1, @2);
2576 case EQ_EXPR: val = (cmp == 0); break;
2577 case NE_EXPR: val = (cmp != 0); break;
2578 case LT_EXPR: val = (cmp < 0); break;
2579 case GT_EXPR: val = (cmp > 0); break;
2580 case LE_EXPR: val = (cmp <= 0); break;
2581 case GE_EXPR: val = (cmp >= 0); break;
2582 default: gcc_unreachable ();
2586 (if (code1 == EQ_EXPR && val) @4)
2587 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2588 (if (code1 == NE_EXPR && !val) @3))))))
2590 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2592 (for code1 (lt le gt ge)
2593 (for code2 (lt le gt ge)
2595 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2598 int cmp = tree_int_cst_compare (@1, @2);
2601 /* Choose the more restrictive of two < or <= comparisons. */
2602 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2603 && (code2 == LT_EXPR || code2 == LE_EXPR))
2604 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2607 /* Likewise chose the more restrictive of two > or >= comparisons. */
2608 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2609 && (code2 == GT_EXPR || code2 == GE_EXPR))
2610 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2613 /* Check for singleton ranges. */
2615 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2616 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2618 /* Check for disjoint ranges. */
2620 && (code1 == LT_EXPR || code1 == LE_EXPR)
2621 && (code2 == GT_EXPR || code2 == GE_EXPR))
2622 { constant_boolean_node (true, type); })
2624 && (code1 == GT_EXPR || code1 == GE_EXPR)
2625 && (code2 == LT_EXPR || code2 == LE_EXPR))
2626 { constant_boolean_node (true, type); })
2629 /* We can't reassociate at all for saturating types. */
2630 (if (!TYPE_SATURATING (type))
2632 /* Contract negates. */
2633 /* A + (-B) -> A - B */
2635 (plus:c @0 (convert? (negate @1)))
2636 /* Apply STRIP_NOPS on the negate. */
2637 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2638 && !TYPE_OVERFLOW_SANITIZED (type))
2642 if (INTEGRAL_TYPE_P (type)
2643 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2644 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2646 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2647 /* A - (-B) -> A + B */
2649 (minus @0 (convert? (negate @1)))
2650 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2651 && !TYPE_OVERFLOW_SANITIZED (type))
2655 if (INTEGRAL_TYPE_P (type)
2656 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2657 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2659 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2661 Sign-extension is ok except for INT_MIN, which thankfully cannot
2662 happen without overflow. */
2664 (negate (convert (negate @1)))
2665 (if (INTEGRAL_TYPE_P (type)
2666 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2667 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2668 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2669 && !TYPE_OVERFLOW_SANITIZED (type)
2670 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2673 (negate (convert negate_expr_p@1))
2674 (if (SCALAR_FLOAT_TYPE_P (type)
2675 && ((DECIMAL_FLOAT_TYPE_P (type)
2676 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2677 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2678 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2679 (convert (negate @1))))
2681 (negate (nop_convert? (negate @1)))
2682 (if (!TYPE_OVERFLOW_SANITIZED (type)
2683 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2686 /* We can't reassociate floating-point unless -fassociative-math
2687 or fixed-point plus or minus because of saturation to +-Inf. */
2688 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2689 && !FIXED_POINT_TYPE_P (type))
2691 /* Match patterns that allow contracting a plus-minus pair
2692 irrespective of overflow issues. */
2693 /* (A +- B) - A -> +- B */
2694 /* (A +- B) -+ B -> A */
2695 /* A - (A +- B) -> -+ B */
2696 /* A +- (B -+ A) -> +- B */
2698 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2701 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2702 (if (!ANY_INTEGRAL_TYPE_P (type)
2703 || TYPE_OVERFLOW_WRAPS (type))
2704 (negate (view_convert @1))
2705 (view_convert (negate @1))))
2707 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2710 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2711 (if (!ANY_INTEGRAL_TYPE_P (type)
2712 || TYPE_OVERFLOW_WRAPS (type))
2713 (negate (view_convert @1))
2714 (view_convert (negate @1))))
2716 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2718 /* (A +- B) + (C - A) -> C +- B */
2719 /* (A + B) - (A - C) -> B + C */
2720 /* More cases are handled with comparisons. */
2722 (plus:c (plus:c @0 @1) (minus @2 @0))
2725 (plus:c (minus @0 @1) (minus @2 @0))
2728 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2729 (if (TYPE_OVERFLOW_UNDEFINED (type)
2730 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2731 (pointer_diff @2 @1)))
2733 (minus (plus:c @0 @1) (minus @0 @2))
2736 /* (A +- CST1) +- CST2 -> A + CST3
2737 Use view_convert because it is safe for vectors and equivalent for
2739 (for outer_op (plus minus)
2740 (for inner_op (plus minus)
2741 neg_inner_op (minus plus)
2743 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2745 /* If one of the types wraps, use that one. */
2746 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2747 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2748 forever if something doesn't simplify into a constant. */
2749 (if (!CONSTANT_CLASS_P (@0))
2750 (if (outer_op == PLUS_EXPR)
2751 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2752 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2753 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2754 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2755 (if (outer_op == PLUS_EXPR)
2756 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2757 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2758 /* If the constant operation overflows we cannot do the transform
2759 directly as we would introduce undefined overflow, for example
2760 with (a - 1) + INT_MIN. */
2761 (if (types_match (type, @0))
2762 (with { tree cst = const_binop (outer_op == inner_op
2763 ? PLUS_EXPR : MINUS_EXPR,
2765 (if (cst && !TREE_OVERFLOW (cst))
2766 (inner_op @0 { cst; } )
2767 /* X+INT_MAX+1 is X-INT_MIN. */
2768 (if (INTEGRAL_TYPE_P (type) && cst
2769 && wi::to_wide (cst) == wi::min_value (type))
2770 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2771 /* Last resort, use some unsigned type. */
2772 (with { tree utype = unsigned_type_for (type); }
2774 (view_convert (inner_op
2775 (view_convert:utype @0)
2777 { drop_tree_overflow (cst); }))))))))))))))
2779 /* (CST1 - A) +- CST2 -> CST3 - A */
2780 (for outer_op (plus minus)
2782 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2783 /* If one of the types wraps, use that one. */
2784 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2785 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2786 forever if something doesn't simplify into a constant. */
2787 (if (!CONSTANT_CLASS_P (@0))
2788 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2789 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2790 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2791 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2792 (if (types_match (type, @0))
2793 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2794 (if (cst && !TREE_OVERFLOW (cst))
2795 (minus { cst; } @0))))))))
2797 /* CST1 - (CST2 - A) -> CST3 + A
2798 Use view_convert because it is safe for vectors and equivalent for
2801 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
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 (plus (view_convert @0) (minus @1 (view_convert @2))))
2808 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2809 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2810 (view_convert (plus @0 (minus (view_convert @1) @2)))
2811 (if (types_match (type, @0))
2812 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2813 (if (cst && !TREE_OVERFLOW (cst))
2814 (plus { cst; } @0)))))))
2816 /* ((T)(A)) + CST -> (T)(A + CST) */
2819 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2820 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2821 && TREE_CODE (type) == INTEGER_TYPE
2822 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2823 && int_fits_type_p (@1, TREE_TYPE (@0)))
2824 /* Perform binary operation inside the cast if the constant fits
2825 and (A + CST)'s range does not overflow. */
2828 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2829 max_ovf = wi::OVF_OVERFLOW;
2830 tree inner_type = TREE_TYPE (@0);
2833 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2834 TYPE_SIGN (inner_type));
2837 if (get_global_range_query ()->range_of_expr (vr, @0)
2838 && vr.kind () == VR_RANGE)
2840 wide_int wmin0 = vr.lower_bound ();
2841 wide_int wmax0 = vr.upper_bound ();
2842 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2843 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2846 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2847 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2851 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2853 (for op (plus minus)
2855 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2856 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2857 && TREE_CODE (type) == INTEGER_TYPE
2858 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2859 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2860 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2861 && TYPE_OVERFLOW_WRAPS (type))
2862 (plus (convert @0) (op @2 (convert @1))))))
2865 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2866 to a simple value. */
2867 (for op (plus minus)
2869 (op (convert @0) (convert @1))
2870 (if (INTEGRAL_TYPE_P (type)
2871 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2872 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2873 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2874 && !TYPE_OVERFLOW_TRAPS (type)
2875 && !TYPE_OVERFLOW_SANITIZED (type))
2876 (convert (op! @0 @1)))))
2880 (plus:c (bit_not @0) @0)
2881 (if (!TYPE_OVERFLOW_TRAPS (type))
2882 { build_all_ones_cst (type); }))
2886 (plus (convert? (bit_not @0)) integer_each_onep)
2887 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2888 (negate (convert @0))))
2892 (minus (convert? (negate @0)) integer_each_onep)
2893 (if (!TYPE_OVERFLOW_TRAPS (type)
2894 && TREE_CODE (type) != COMPLEX_TYPE
2895 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2896 (bit_not (convert @0))))
2900 (minus integer_all_onesp @0)
2901 (if (TREE_CODE (type) != COMPLEX_TYPE)
2904 /* (T)(P + A) - (T)P -> (T) A */
2906 (minus (convert (plus:c @@0 @1))
2908 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2909 /* For integer types, if A has a smaller type
2910 than T the result depends on the possible
2912 E.g. T=size_t, A=(unsigned)429497295, P>0.
2913 However, if an overflow in P + A would cause
2914 undefined behavior, we can assume that there
2916 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2917 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2920 (minus (convert (pointer_plus @@0 @1))
2922 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2923 /* For pointer types, if the conversion of A to the
2924 final type requires a sign- or zero-extension,
2925 then we have to punt - it is not defined which
2927 || (POINTER_TYPE_P (TREE_TYPE (@0))
2928 && TREE_CODE (@1) == INTEGER_CST
2929 && tree_int_cst_sign_bit (@1) == 0))
2932 (pointer_diff (pointer_plus @@0 @1) @0)
2933 /* The second argument of pointer_plus must be interpreted as signed, and
2934 thus sign-extended if necessary. */
2935 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2936 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2937 second arg is unsigned even when we need to consider it as signed,
2938 we don't want to diagnose overflow here. */
2939 (convert (view_convert:stype @1))))
2941 /* (T)P - (T)(P + A) -> -(T) A */
2943 (minus (convert? @0)
2944 (convert (plus:c @@0 @1)))
2945 (if (INTEGRAL_TYPE_P (type)
2946 && TYPE_OVERFLOW_UNDEFINED (type)
2947 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2948 (with { tree utype = unsigned_type_for (type); }
2949 (convert (negate (convert:utype @1))))
2950 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2951 /* For integer types, if A has a smaller type
2952 than T the result depends on the possible
2954 E.g. T=size_t, A=(unsigned)429497295, P>0.
2955 However, if an overflow in P + A would cause
2956 undefined behavior, we can assume that there
2958 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2959 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2960 (negate (convert @1)))))
2963 (convert (pointer_plus @@0 @1)))
2964 (if (INTEGRAL_TYPE_P (type)
2965 && TYPE_OVERFLOW_UNDEFINED (type)
2966 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2967 (with { tree utype = unsigned_type_for (type); }
2968 (convert (negate (convert:utype @1))))
2969 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2970 /* For pointer types, if the conversion of A to the
2971 final type requires a sign- or zero-extension,
2972 then we have to punt - it is not defined which
2974 || (POINTER_TYPE_P (TREE_TYPE (@0))
2975 && TREE_CODE (@1) == INTEGER_CST
2976 && tree_int_cst_sign_bit (@1) == 0))
2977 (negate (convert @1)))))
2979 (pointer_diff @0 (pointer_plus @@0 @1))
2980 /* The second argument of pointer_plus must be interpreted as signed, and
2981 thus sign-extended if necessary. */
2982 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2983 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2984 second arg is unsigned even when we need to consider it as signed,
2985 we don't want to diagnose overflow here. */
2986 (negate (convert (view_convert:stype @1)))))
2988 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2990 (minus (convert (plus:c @@0 @1))
2991 (convert (plus:c @0 @2)))
2992 (if (INTEGRAL_TYPE_P (type)
2993 && TYPE_OVERFLOW_UNDEFINED (type)
2994 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2995 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2996 (with { tree utype = unsigned_type_for (type); }
2997 (convert (minus (convert:utype @1) (convert:utype @2))))
2998 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2999 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3000 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3001 /* For integer types, if A has a smaller type
3002 than T the result depends on the possible
3004 E.g. T=size_t, A=(unsigned)429497295, P>0.
3005 However, if an overflow in P + A would cause
3006 undefined behavior, we can assume that there
3008 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3009 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3010 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3011 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3012 (minus (convert @1) (convert @2)))))
3014 (minus (convert (pointer_plus @@0 @1))
3015 (convert (pointer_plus @0 @2)))
3016 (if (INTEGRAL_TYPE_P (type)
3017 && TYPE_OVERFLOW_UNDEFINED (type)
3018 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3019 (with { tree utype = unsigned_type_for (type); }
3020 (convert (minus (convert:utype @1) (convert:utype @2))))
3021 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3022 /* For pointer types, if the conversion of A to the
3023 final type requires a sign- or zero-extension,
3024 then we have to punt - it is not defined which
3026 || (POINTER_TYPE_P (TREE_TYPE (@0))
3027 && TREE_CODE (@1) == INTEGER_CST
3028 && tree_int_cst_sign_bit (@1) == 0
3029 && TREE_CODE (@2) == INTEGER_CST
3030 && tree_int_cst_sign_bit (@2) == 0))
3031 (minus (convert @1) (convert @2)))))
3033 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3034 (pointer_diff @0 @1))
3036 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
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 (minus (convert (view_convert:stype @1))
3044 (convert (view_convert:stype @2)))))))
3046 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3047 Modeled after fold_plusminus_mult_expr. */
3048 (if (!TYPE_SATURATING (type)
3049 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3050 (for plusminus (plus minus)
3052 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3053 (if (!ANY_INTEGRAL_TYPE_P (type)
3054 || TYPE_OVERFLOW_WRAPS (type)
3055 || (INTEGRAL_TYPE_P (type)
3056 && tree_expr_nonzero_p (@0)
3057 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3058 (if (single_use (@3) || single_use (@4))
3059 /* If @1 +- @2 is constant require a hard single-use on either
3060 original operand (but not on both). */
3061 (mult (plusminus @1 @2) @0)
3062 (mult! (plusminus @1 @2) @0)
3064 /* We cannot generate constant 1 for fract. */
3065 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3067 (plusminus @0 (mult:c@3 @0 @2))
3068 (if ((!ANY_INTEGRAL_TYPE_P (type)
3069 || TYPE_OVERFLOW_WRAPS (type)
3070 /* For @0 + @0*@2 this transformation would introduce UB
3071 (where there was none before) for @0 in [-1,0] and @2 max.
3072 For @0 - @0*@2 this transformation would introduce UB
3073 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3074 || (INTEGRAL_TYPE_P (type)
3075 && ((tree_expr_nonzero_p (@0)
3076 && expr_not_equal_to (@0,
3077 wi::minus_one (TYPE_PRECISION (type))))
3078 || (plusminus == PLUS_EXPR
3079 ? expr_not_equal_to (@2,
3080 wi::max_value (TYPE_PRECISION (type), SIGNED))
3081 /* Let's ignore the @0 -1 and @2 min case. */
3082 : (expr_not_equal_to (@2,
3083 wi::min_value (TYPE_PRECISION (type), SIGNED))
3084 && expr_not_equal_to (@2,
3085 wi::min_value (TYPE_PRECISION (type), SIGNED)
3088 (mult (plusminus { build_one_cst (type); } @2) @0)))
3090 (plusminus (mult:c@3 @0 @2) @0)
3091 (if ((!ANY_INTEGRAL_TYPE_P (type)
3092 || TYPE_OVERFLOW_WRAPS (type)
3093 /* For @0*@2 + @0 this transformation would introduce UB
3094 (where there was none before) for @0 in [-1,0] and @2 max.
3095 For @0*@2 - @0 this transformation would introduce UB
3096 for @0 0 and @2 min. */
3097 || (INTEGRAL_TYPE_P (type)
3098 && ((tree_expr_nonzero_p (@0)
3099 && (plusminus == MINUS_EXPR
3100 || expr_not_equal_to (@0,
3101 wi::minus_one (TYPE_PRECISION (type)))))
3102 || expr_not_equal_to (@2,
3103 (plusminus == PLUS_EXPR
3104 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3105 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3107 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3110 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3111 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3113 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3114 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3115 && tree_fits_uhwi_p (@1)
3116 && tree_to_uhwi (@1) < element_precision (type)
3117 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3118 || optab_handler (smul_optab,
3119 TYPE_MODE (type)) != CODE_FOR_nothing))
3120 (with { tree t = type;
3121 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3122 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3123 element_precision (type));
3125 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3127 cst = build_uniform_cst (t, cst); }
3128 (convert (mult (convert:t @0) { cst; })))))
3130 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3131 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3132 && tree_fits_uhwi_p (@1)
3133 && tree_to_uhwi (@1) < element_precision (type)
3134 && tree_fits_uhwi_p (@2)
3135 && tree_to_uhwi (@2) < element_precision (type)
3136 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3137 || optab_handler (smul_optab,
3138 TYPE_MODE (type)) != CODE_FOR_nothing))
3139 (with { tree t = type;
3140 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3141 unsigned int prec = element_precision (type);
3142 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3143 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3144 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3146 cst = build_uniform_cst (t, cst); }
3147 (convert (mult (convert:t @0) { cst; })))))
3150 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3151 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3152 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3153 (for op (bit_ior bit_xor)
3155 (op (mult:s@0 @1 INTEGER_CST@2)
3156 (mult:s@3 @1 INTEGER_CST@4))
3157 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3158 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3160 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3162 (op:c (mult:s@0 @1 INTEGER_CST@2)
3163 (lshift:s@3 @1 INTEGER_CST@4))
3164 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3165 && tree_int_cst_sgn (@4) > 0
3166 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3167 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3168 wide_int c = wi::add (wi::to_wide (@2),
3169 wi::lshift (wone, wi::to_wide (@4))); }
3170 (mult @1 { wide_int_to_tree (type, c); }))))
3172 (op:c (mult:s@0 @1 INTEGER_CST@2)
3174 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3175 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3177 { wide_int_to_tree (type,
3178 wi::add (wi::to_wide (@2), 1)); })))
3180 (op (lshift:s@0 @1 INTEGER_CST@2)
3181 (lshift:s@3 @1 INTEGER_CST@4))
3182 (if (INTEGRAL_TYPE_P (type)
3183 && tree_int_cst_sgn (@2) > 0
3184 && tree_int_cst_sgn (@4) > 0
3185 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3186 (with { tree t = type;
3187 if (!TYPE_OVERFLOW_WRAPS (t))
3188 t = unsigned_type_for (t);
3189 wide_int wone = wi::one (TYPE_PRECISION (t));
3190 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3191 wi::lshift (wone, wi::to_wide (@4))); }
3192 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3194 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3196 (if (INTEGRAL_TYPE_P (type)
3197 && tree_int_cst_sgn (@2) > 0
3198 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3199 (with { tree t = type;
3200 if (!TYPE_OVERFLOW_WRAPS (t))
3201 t = unsigned_type_for (t);
3202 wide_int wone = wi::one (TYPE_PRECISION (t));
3203 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3204 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3206 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3208 (for minmax (min max)
3212 /* For fmin() and fmax(), skip folding when both are sNaN. */
3213 (for minmax (FMIN_ALL FMAX_ALL)
3216 (if (!tree_expr_maybe_signaling_nan_p (@0))
3218 /* min(max(x,y),y) -> y. */
3220 (min:c (max:c @0 @1) @1)
3222 /* max(min(x,y),y) -> y. */
3224 (max:c (min:c @0 @1) @1)
3226 /* max(a,-a) -> abs(a). */
3228 (max:c @0 (negate @0))
3229 (if (TREE_CODE (type) != COMPLEX_TYPE
3230 && (! ANY_INTEGRAL_TYPE_P (type)
3231 || TYPE_OVERFLOW_UNDEFINED (type)))
3233 /* min(a,-a) -> -abs(a). */
3235 (min:c @0 (negate @0))
3236 (if (TREE_CODE (type) != COMPLEX_TYPE
3237 && (! ANY_INTEGRAL_TYPE_P (type)
3238 || TYPE_OVERFLOW_UNDEFINED (type)))
3243 (if (INTEGRAL_TYPE_P (type)
3244 && TYPE_MIN_VALUE (type)
3245 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3247 (if (INTEGRAL_TYPE_P (type)
3248 && TYPE_MAX_VALUE (type)
3249 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3254 (if (INTEGRAL_TYPE_P (type)
3255 && TYPE_MAX_VALUE (type)
3256 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3258 (if (INTEGRAL_TYPE_P (type)
3259 && TYPE_MIN_VALUE (type)
3260 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3263 /* max (a, a + CST) -> a + CST where CST is positive. */
3264 /* max (a, a + CST) -> a where CST is negative. */
3266 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3267 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3268 (if (tree_int_cst_sgn (@1) > 0)
3272 /* min (a, a + CST) -> a where CST is positive. */
3273 /* min (a, a + CST) -> a + CST where CST is negative. */
3275 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3276 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3277 (if (tree_int_cst_sgn (@1) > 0)
3281 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3282 the addresses are known to be less, equal or greater. */
3283 (for minmax (min max)
3286 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3289 poly_int64 off0, off1;
3291 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3292 off0, off1, GENERIC);
3295 (if (minmax == MIN_EXPR)
3296 (if (known_le (off0, off1))
3298 (if (known_gt (off0, off1))
3300 (if (known_ge (off0, off1))
3302 (if (known_lt (off0, off1))
3305 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3306 and the outer convert demotes the expression back to x's type. */
3307 (for minmax (min max)
3309 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3310 (if (INTEGRAL_TYPE_P (type)
3311 && types_match (@1, type) && int_fits_type_p (@2, type)
3312 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3313 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3314 (minmax @1 (convert @2)))))
3316 (for minmax (FMIN_ALL FMAX_ALL)
3317 /* If either argument is NaN and other one is not sNaN, return the other
3318 one. Avoid the transformation if we get (and honor) a signalling NaN. */
3320 (minmax:c @0 REAL_CST@1)
3321 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3322 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3323 && !tree_expr_maybe_signaling_nan_p (@0))
3325 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3326 functions to return the numeric arg if the other one is NaN.
3327 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3328 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3329 worry about it either. */
3330 (if (flag_finite_math_only)
3337 /* min (-A, -B) -> -max (A, B) */
3338 (for minmax (min max FMIN_ALL FMAX_ALL)
3339 maxmin (max min FMAX_ALL FMIN_ALL)
3341 (minmax (negate:s@2 @0) (negate:s@3 @1))
3342 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3343 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3344 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3345 (negate (maxmin @0 @1)))))
3346 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3347 MAX (~X, ~Y) -> ~MIN (X, Y) */
3348 (for minmax (min max)
3351 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3352 (bit_not (maxmin @0 @1))))
3354 /* MIN (X, Y) == X -> X <= Y */
3355 (for minmax (min min max max)
3359 (cmp:c (minmax:c @0 @1) @0)
3360 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3362 /* MIN (X, 5) == 0 -> X == 0
3363 MIN (X, 5) == 7 -> false */
3366 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3367 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3368 TYPE_SIGN (TREE_TYPE (@0))))
3369 { constant_boolean_node (cmp == NE_EXPR, type); }
3370 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3371 TYPE_SIGN (TREE_TYPE (@0))))
3375 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3376 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3377 TYPE_SIGN (TREE_TYPE (@0))))
3378 { constant_boolean_node (cmp == NE_EXPR, type); }
3379 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3380 TYPE_SIGN (TREE_TYPE (@0))))
3382 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3383 (for minmax (min min max max min min max max )
3384 cmp (lt le gt ge gt ge lt le )
3385 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3387 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3388 (comb (cmp @0 @2) (cmp @1 @2))))
3390 /* X <= MAX(X, Y) -> true
3391 X > MAX(X, Y) -> false
3392 X >= MIN(X, Y) -> true
3393 X < MIN(X, Y) -> false */
3394 (for minmax (min min max max )
3397 (cmp @0 (minmax:c @0 @1))
3398 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3400 /* Undo fancy ways of writing max/min or other ?: expressions, like
3401 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
3402 People normally use ?: and that is what we actually try to optimize. */
3403 /* Transform A + (B-A)*cmp into cmp ? B : A. */
3405 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3406 (if (INTEGRAL_TYPE_P (type)
3407 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3408 (cond (convert:boolean_type_node @2) @1 @0)))
3409 /* Transform A - (A-B)*cmp into cmp ? B : A. */
3411 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3412 (if (INTEGRAL_TYPE_P (type)
3413 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3414 (cond (convert:boolean_type_node @2) @1 @0)))
3415 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
3417 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3418 (if (INTEGRAL_TYPE_P (type)
3419 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3420 (cond (convert:boolean_type_node @2) @1 @0)))
3422 /* Simplifications of shift and rotates. */
3424 (for rotate (lrotate rrotate)
3426 (rotate integer_all_onesp@0 @1)
3429 /* Optimize -1 >> x for arithmetic right shifts. */
3431 (rshift integer_all_onesp@0 @1)
3432 (if (!TYPE_UNSIGNED (type))
3435 /* Optimize (x >> c) << c into x & (-1<<c). */
3437 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3438 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3439 /* It doesn't matter if the right shift is arithmetic or logical. */
3440 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3443 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3444 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3445 /* Allow intermediate conversion to integral type with whatever sign, as
3446 long as the low TYPE_PRECISION (type)
3447 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3448 && INTEGRAL_TYPE_P (type)
3449 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3450 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3451 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3452 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3453 || wi::geu_p (wi::to_wide (@1),
3454 TYPE_PRECISION (type)
3455 - TYPE_PRECISION (TREE_TYPE (@2)))))
3456 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3458 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3461 (rshift (lshift @0 INTEGER_CST@1) @1)
3462 (if (TYPE_UNSIGNED (type)
3463 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3464 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3466 /* Optimize x >> x into 0 */
3469 { build_zero_cst (type); })
3471 (for shiftrotate (lrotate rrotate lshift rshift)
3473 (shiftrotate @0 integer_zerop)
3476 (shiftrotate integer_zerop@0 @1)
3478 /* Prefer vector1 << scalar to vector1 << vector2
3479 if vector2 is uniform. */
3480 (for vec (VECTOR_CST CONSTRUCTOR)
3482 (shiftrotate @0 vec@1)
3483 (with { tree tem = uniform_vector_p (@1); }
3485 (shiftrotate @0 { tem; }))))))
3487 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3488 Y is 0. Similarly for X >> Y. */
3490 (for shift (lshift rshift)
3492 (shift @0 SSA_NAME@1)
3493 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3495 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3496 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3498 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3502 /* Rewrite an LROTATE_EXPR by a constant into an
3503 RROTATE_EXPR by a new constant. */
3505 (lrotate @0 INTEGER_CST@1)
3506 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3507 build_int_cst (TREE_TYPE (@1),
3508 element_precision (type)), @1); }))
3510 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3511 (for op (lrotate rrotate rshift lshift)
3513 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3514 (with { unsigned int prec = element_precision (type); }
3515 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3516 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3517 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3518 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3519 (with { unsigned int low = (tree_to_uhwi (@1)
3520 + tree_to_uhwi (@2)); }
3521 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3522 being well defined. */
3524 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3525 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3526 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3527 { build_zero_cst (type); }
3528 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3529 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3532 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3534 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3535 (if ((wi::to_wide (@1) & 1) != 0)
3536 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3537 { build_zero_cst (type); }))
3539 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3540 either to false if D is smaller (unsigned comparison) than C, or to
3541 x == log2 (D) - log2 (C). Similarly for right shifts. */
3545 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3546 (with { int c1 = wi::clz (wi::to_wide (@1));
3547 int c2 = wi::clz (wi::to_wide (@2)); }
3549 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3550 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3552 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3553 (if (tree_int_cst_sgn (@1) > 0)
3554 (with { int c1 = wi::clz (wi::to_wide (@1));
3555 int c2 = wi::clz (wi::to_wide (@2)); }
3557 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3558 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3560 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3561 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3565 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3566 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3568 || (!integer_zerop (@2)
3569 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3570 { constant_boolean_node (cmp == NE_EXPR, type); }
3571 (if (!integer_zerop (@2)
3572 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3573 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3575 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
3576 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
3579 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3580 (if (tree_fits_shwi_p (@1)
3581 && tree_to_shwi (@1) > 0
3582 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))
3583 && tree_to_shwi (@1) <= wi::ctz (wi::to_wide (@3)))
3584 (with { wide_int c1 = wi::to_wide (@1);
3585 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
3586 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
3587 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
3588 { wide_int_to_tree (TREE_TYPE (@0), c3); }))))
3590 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3591 (if (tree_fits_shwi_p (@1)
3592 && tree_to_shwi (@1) > 0
3593 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))
3594 && tree_to_shwi (@1) <= wi::clz (wi::to_wide (@2))
3595 && tree_to_shwi (@1) <= wi::clz (wi::to_wide (@3)))
3596 (cmp (bit_and @0 (lshift @2 @1)) (lshift @3 @1)))))
3598 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3599 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3600 if the new mask might be further optimized. */
3601 (for shift (lshift rshift)
3603 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3605 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3606 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3607 && tree_fits_uhwi_p (@1)
3608 && tree_to_uhwi (@1) > 0
3609 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3612 unsigned int shiftc = tree_to_uhwi (@1);
3613 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3614 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3615 tree shift_type = TREE_TYPE (@3);
3618 if (shift == LSHIFT_EXPR)
3619 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3620 else if (shift == RSHIFT_EXPR
3621 && type_has_mode_precision_p (shift_type))
3623 prec = TYPE_PRECISION (TREE_TYPE (@3));
3625 /* See if more bits can be proven as zero because of
3628 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3630 tree inner_type = TREE_TYPE (@0);
3631 if (type_has_mode_precision_p (inner_type)
3632 && TYPE_PRECISION (inner_type) < prec)
3634 prec = TYPE_PRECISION (inner_type);
3635 /* See if we can shorten the right shift. */
3637 shift_type = inner_type;
3638 /* Otherwise X >> C1 is all zeros, so we'll optimize
3639 it into (X, 0) later on by making sure zerobits
3643 zerobits = HOST_WIDE_INT_M1U;
3646 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3647 zerobits <<= prec - shiftc;
3649 /* For arithmetic shift if sign bit could be set, zerobits
3650 can contain actually sign bits, so no transformation is
3651 possible, unless MASK masks them all away. In that
3652 case the shift needs to be converted into logical shift. */
3653 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3654 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3656 if ((mask & zerobits) == 0)
3657 shift_type = unsigned_type_for (TREE_TYPE (@3));
3663 /* ((X << 16) & 0xff00) is (X, 0). */
3664 (if ((mask & zerobits) == mask)
3665 { build_int_cst (type, 0); }
3666 (with { newmask = mask | zerobits; }
3667 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3670 /* Only do the transformation if NEWMASK is some integer
3672 for (prec = BITS_PER_UNIT;
3673 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3674 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3677 (if (prec < HOST_BITS_PER_WIDE_INT
3678 || newmask == HOST_WIDE_INT_M1U)
3680 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3681 (if (!tree_int_cst_equal (newmaskt, @2))
3682 (if (shift_type != TREE_TYPE (@3))
3683 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3684 (bit_and @4 { newmaskt; })))))))))))))
3686 /* ((1 << n) & M) != 0 -> n == log2 (M) */
3692 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3693 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3694 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3695 wi::exact_log2 (wi::to_wide (@1))); }))))
3697 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3698 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3699 (for shift (lshift rshift)
3700 (for bit_op (bit_and bit_xor bit_ior)
3702 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3703 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3704 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3706 (bit_op (shift (convert @0) @1) { mask; })))))))
3708 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3710 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3711 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3712 && (element_precision (TREE_TYPE (@0))
3713 <= element_precision (TREE_TYPE (@1))
3714 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3716 { tree shift_type = TREE_TYPE (@0); }
3717 (convert (rshift (convert:shift_type @1) @2)))))
3719 /* ~(~X >>r Y) -> X >>r Y
3720 ~(~X <<r Y) -> X <<r Y */
3721 (for rotate (lrotate rrotate)
3723 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3724 (if ((element_precision (TREE_TYPE (@0))
3725 <= element_precision (TREE_TYPE (@1))
3726 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3727 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3728 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3730 { tree rotate_type = TREE_TYPE (@0); }
3731 (convert (rotate (convert:rotate_type @1) @2))))))
3734 (for rotate (lrotate rrotate)
3735 invrot (rrotate lrotate)
3736 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3738 (cmp (rotate @1 @0) (rotate @2 @0))
3740 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3742 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3743 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3744 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3746 (cmp (rotate @0 @1) INTEGER_CST@2)
3747 (if (integer_zerop (@2) || integer_all_onesp (@2))
3750 /* Narrow a lshift by constant. */
3752 (convert (lshift:s@0 @1 INTEGER_CST@2))
3753 (if (INTEGRAL_TYPE_P (type)
3754 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3755 && !integer_zerop (@2)
3756 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
3757 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3758 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
3759 (lshift (convert @1) @2)
3760 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
3761 { build_zero_cst (type); }))))
3763 /* Simplifications of conversions. */
3765 /* Basic strip-useless-type-conversions / strip_nops. */
3766 (for cvt (convert view_convert float fix_trunc)
3769 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3770 || (GENERIC && type == TREE_TYPE (@0)))
3773 /* Contract view-conversions. */
3775 (view_convert (view_convert @0))
3778 /* For integral conversions with the same precision or pointer
3779 conversions use a NOP_EXPR instead. */
3782 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3783 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3784 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3787 /* Strip inner integral conversions that do not change precision or size, or
3788 zero-extend while keeping the same size (for bool-to-char). */
3790 (view_convert (convert@0 @1))
3791 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3792 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3793 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3794 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3795 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3796 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3799 /* Simplify a view-converted empty or single-element constructor. */
3801 (view_convert CONSTRUCTOR@0)
3803 { tree ctor = (TREE_CODE (@0) == SSA_NAME
3804 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
3806 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3807 { build_zero_cst (type); })
3808 (if (CONSTRUCTOR_NELTS (ctor) == 1
3809 && VECTOR_TYPE_P (TREE_TYPE (ctor))
3810 && operand_equal_p (TYPE_SIZE (type),
3811 TYPE_SIZE (TREE_TYPE
3812 (CONSTRUCTOR_ELT (ctor, 0)->value))))
3813 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
3815 /* Re-association barriers around constants and other re-association
3816 barriers can be removed. */
3818 (paren CONSTANT_CLASS_P@0)
3821 (paren (paren@1 @0))
3824 /* Handle cases of two conversions in a row. */
3825 (for ocvt (convert float fix_trunc)
3826 (for icvt (convert float)
3831 tree inside_type = TREE_TYPE (@0);
3832 tree inter_type = TREE_TYPE (@1);
3833 int inside_int = INTEGRAL_TYPE_P (inside_type);
3834 int inside_ptr = POINTER_TYPE_P (inside_type);
3835 int inside_float = FLOAT_TYPE_P (inside_type);
3836 int inside_vec = VECTOR_TYPE_P (inside_type);
3837 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3838 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3839 int inter_int = INTEGRAL_TYPE_P (inter_type);
3840 int inter_ptr = POINTER_TYPE_P (inter_type);
3841 int inter_float = FLOAT_TYPE_P (inter_type);
3842 int inter_vec = VECTOR_TYPE_P (inter_type);
3843 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3844 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3845 int final_int = INTEGRAL_TYPE_P (type);
3846 int final_ptr = POINTER_TYPE_P (type);
3847 int final_float = FLOAT_TYPE_P (type);
3848 int final_vec = VECTOR_TYPE_P (type);
3849 unsigned int final_prec = TYPE_PRECISION (type);
3850 int final_unsignedp = TYPE_UNSIGNED (type);
3853 /* In addition to the cases of two conversions in a row
3854 handled below, if we are converting something to its own
3855 type via an object of identical or wider precision, neither
3856 conversion is needed. */
3857 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3859 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3860 && (((inter_int || inter_ptr) && final_int)
3861 || (inter_float && final_float))
3862 && inter_prec >= final_prec)
3865 /* Likewise, if the intermediate and initial types are either both
3866 float or both integer, we don't need the middle conversion if the
3867 former is wider than the latter and doesn't change the signedness
3868 (for integers). Avoid this if the final type is a pointer since
3869 then we sometimes need the middle conversion. */
3870 (if (((inter_int && inside_int) || (inter_float && inside_float))
3871 && (final_int || final_float)
3872 && inter_prec >= inside_prec
3873 && (inter_float || inter_unsignedp == inside_unsignedp))
3876 /* If we have a sign-extension of a zero-extended value, we can
3877 replace that by a single zero-extension. Likewise if the
3878 final conversion does not change precision we can drop the
3879 intermediate conversion. */
3880 (if (inside_int && inter_int && final_int
3881 && ((inside_prec < inter_prec && inter_prec < final_prec
3882 && inside_unsignedp && !inter_unsignedp)
3883 || final_prec == inter_prec))
3886 /* Two conversions in a row are not needed unless:
3887 - some conversion is floating-point (overstrict for now), or
3888 - some conversion is a vector (overstrict for now), or
3889 - the intermediate type is narrower than both initial and
3891 - the intermediate type and innermost type differ in signedness,
3892 and the outermost type is wider than the intermediate, or
3893 - the initial type is a pointer type and the precisions of the
3894 intermediate and final types differ, or
3895 - the final type is a pointer type and the precisions of the
3896 initial and intermediate types differ. */
3897 (if (! inside_float && ! inter_float && ! final_float
3898 && ! inside_vec && ! inter_vec && ! final_vec
3899 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3900 && ! (inside_int && inter_int
3901 && inter_unsignedp != inside_unsignedp
3902 && inter_prec < final_prec)
3903 && ((inter_unsignedp && inter_prec > inside_prec)
3904 == (final_unsignedp && final_prec > inter_prec))
3905 && ! (inside_ptr && inter_prec != final_prec)
3906 && ! (final_ptr && inside_prec != inter_prec))
3909 /* A truncation to an unsigned type (a zero-extension) should be
3910 canonicalized as bitwise and of a mask. */
3911 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3912 && final_int && inter_int && inside_int
3913 && final_prec == inside_prec
3914 && final_prec > inter_prec
3916 (convert (bit_and @0 { wide_int_to_tree
3918 wi::mask (inter_prec, false,
3919 TYPE_PRECISION (inside_type))); })))
3921 /* If we are converting an integer to a floating-point that can
3922 represent it exactly and back to an integer, we can skip the
3923 floating-point conversion. */
3924 (if (GIMPLE /* PR66211 */
3925 && inside_int && inter_float && final_int &&
3926 (unsigned) significand_size (TYPE_MODE (inter_type))
3927 >= inside_prec - !inside_unsignedp)
3930 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
3931 float_type. Only do the transformation if we do not need to preserve
3932 trapping behaviour, so require !flag_trapping_math. */
3935 (float (fix_trunc @0))
3936 (if (!flag_trapping_math
3937 && types_match (type, TREE_TYPE (@0))
3938 && direct_internal_fn_supported_p (IFN_TRUNC, type,
3943 /* If we have a narrowing conversion to an integral type that is fed by a
3944 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3945 masks off bits outside the final type (and nothing else). */
3947 (convert (bit_and @0 INTEGER_CST@1))
3948 (if (INTEGRAL_TYPE_P (type)
3949 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3950 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3951 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3952 TYPE_PRECISION (type)), 0))
3956 /* (X /[ex] A) * A -> X. */
3958 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3961 /* Simplify (A / B) * B + (A % B) -> A. */
3962 (for div (trunc_div ceil_div floor_div round_div)
3963 mod (trunc_mod ceil_mod floor_mod round_mod)
3965 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3968 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3969 (for op (plus minus)
3971 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3972 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3973 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3976 wi::overflow_type overflow;
3977 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3978 TYPE_SIGN (type), &overflow);
3980 (if (types_match (type, TREE_TYPE (@2))
3981 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3982 (op @0 { wide_int_to_tree (type, mul); })
3983 (with { tree utype = unsigned_type_for (type); }
3984 (convert (op (convert:utype @0)
3985 (mult (convert:utype @1) (convert:utype @2))))))))))
3987 /* Canonicalization of binary operations. */
3989 /* Convert X + -C into X - C. */
3991 (plus @0 REAL_CST@1)
3992 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3993 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3994 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3995 (minus @0 { tem; })))))
3997 /* Convert x+x into x*2. */
4000 (if (SCALAR_FLOAT_TYPE_P (type))
4001 (mult @0 { build_real (type, dconst2); })
4002 (if (INTEGRAL_TYPE_P (type))
4003 (mult @0 { build_int_cst (type, 2); }))))
4007 (minus integer_zerop @1)
4010 (pointer_diff integer_zerop @1)
4011 (negate (convert @1)))
4013 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4014 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4015 (-ARG1 + ARG0) reduces to -ARG1. */
4017 (minus real_zerop@0 @1)
4018 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4021 /* Transform x * -1 into -x. */
4023 (mult @0 integer_minus_onep)
4026 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4027 signed overflow for CST != 0 && CST != -1. */
4029 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4030 (if (TREE_CODE (@2) != INTEGER_CST
4032 && !integer_zerop (@1) && !integer_minus_onep (@1))
4033 (mult (mult @0 @2) @1)))
4035 /* True if we can easily extract the real and imaginary parts of a complex
4037 (match compositional_complex
4038 (convert? (complex @0 @1)))
4040 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4042 (complex (realpart @0) (imagpart @0))
4045 (realpart (complex @0 @1))
4048 (imagpart (complex @0 @1))
4051 /* Sometimes we only care about half of a complex expression. */
4053 (realpart (convert?:s (conj:s @0)))
4054 (convert (realpart @0)))
4056 (imagpart (convert?:s (conj:s @0)))
4057 (convert (negate (imagpart @0))))
4058 (for part (realpart imagpart)
4059 (for op (plus minus)
4061 (part (convert?:s@2 (op:s @0 @1)))
4062 (convert (op (part @0) (part @1))))))
4064 (realpart (convert?:s (CEXPI:s @0)))
4067 (imagpart (convert?:s (CEXPI:s @0)))
4070 /* conj(conj(x)) -> x */
4072 (conj (convert? (conj @0)))
4073 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4076 /* conj({x,y}) -> {x,-y} */
4078 (conj (convert?:s (complex:s @0 @1)))
4079 (with { tree itype = TREE_TYPE (type); }
4080 (complex (convert:itype @0) (negate (convert:itype @1)))))
4082 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4083 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
4084 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
4089 (bswap (bit_not (bswap @0)))
4091 (for bitop (bit_xor bit_ior bit_and)
4093 (bswap (bitop:c (bswap @0) @1))
4094 (bitop @0 (bswap @1))))
4097 (cmp (bswap@2 @0) (bswap @1))
4098 (with { tree ctype = TREE_TYPE (@2); }
4099 (cmp (convert:ctype @0) (convert:ctype @1))))
4101 (cmp (bswap @0) INTEGER_CST@1)
4102 (with { tree ctype = TREE_TYPE (@1); }
4103 (cmp (convert:ctype @0) (bswap! @1)))))
4104 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4106 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4108 (if (BITS_PER_UNIT == 8
4109 && tree_fits_uhwi_p (@2)
4110 && tree_fits_uhwi_p (@3))
4113 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4114 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4115 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4116 unsigned HOST_WIDE_INT lo = bits & 7;
4117 unsigned HOST_WIDE_INT hi = bits - lo;
4120 && mask < (256u>>lo)
4121 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4122 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4124 (bit_and (convert @1) @3)
4127 tree utype = unsigned_type_for (TREE_TYPE (@1));
4128 tree nst = build_int_cst (integer_type_node, ns);
4130 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4131 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4133 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4134 (if (BITS_PER_UNIT == 8
4135 && CHAR_TYPE_SIZE == 8
4136 && tree_fits_uhwi_p (@1))
4139 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4140 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4141 /* If the bswap was extended before the original shift, this
4142 byte (shift) has the sign of the extension, not the sign of
4143 the original shift. */
4144 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4146 /* Special case: logical right shift of sign-extended bswap.
4147 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4148 (if (TYPE_PRECISION (type) > prec
4149 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4150 && TYPE_UNSIGNED (type)
4151 && bits < prec && bits + 8 >= prec)
4152 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4153 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4154 (if (bits + 8 == prec)
4155 (if (TYPE_UNSIGNED (st))
4156 (convert (convert:unsigned_char_type_node @0))
4157 (convert (convert:signed_char_type_node @0)))
4158 (if (bits < prec && bits + 8 > prec)
4161 tree nst = build_int_cst (integer_type_node, bits & 7);
4162 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4163 : signed_char_type_node;
4165 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4166 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4168 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4169 (if (BITS_PER_UNIT == 8
4170 && tree_fits_uhwi_p (@1)
4171 && tree_to_uhwi (@1) < 256)
4174 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4175 tree utype = unsigned_type_for (TREE_TYPE (@0));
4176 tree nst = build_int_cst (integer_type_node, prec - 8);
4178 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4181 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
4183 /* Simplify constant conditions.
4184 Only optimize constant conditions when the selected branch
4185 has the same type as the COND_EXPR. This avoids optimizing
4186 away "c ? x : throw", where the throw has a void type.
4187 Note that we cannot throw away the fold-const.cc variant nor
4188 this one as we depend on doing this transform before possibly
4189 A ? B : B -> B triggers and the fold-const.cc one can optimize
4190 0 ? A : B to B even if A has side-effects. Something
4191 genmatch cannot handle. */
4193 (cond INTEGER_CST@0 @1 @2)
4194 (if (integer_zerop (@0))
4195 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4197 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4200 (vec_cond VECTOR_CST@0 @1 @2)
4201 (if (integer_all_onesp (@0))
4203 (if (integer_zerop (@0))
4206 /* Sink unary operations to branches, but only if we do fold both. */
4207 (for op (negate bit_not abs absu)
4209 (op (vec_cond:s @0 @1 @2))
4210 (vec_cond @0 (op! @1) (op! @2))))
4212 /* Sink binary operation to branches, but only if we can fold it. */
4213 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4214 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4215 trunc_mod ceil_mod floor_mod round_mod min max)
4216 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4218 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4219 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4221 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4223 (op (vec_cond:s @0 @1 @2) @3)
4224 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4226 (op @3 (vec_cond:s @0 @1 @2))
4227 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4230 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4231 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4234 int ibit = tree_log2 (@0);
4235 int ibit2 = tree_log2 (@1);
4239 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4241 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4242 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4245 int ibit = tree_log2 (@0);
4246 int ibit2 = tree_log2 (@1);
4250 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4252 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4255 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4257 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4259 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4262 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4264 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4266 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4267 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4270 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4271 TYPE_PRECISION(type)));
4272 int ibit2 = tree_log2 (@1);
4276 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4278 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4280 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4283 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4284 TYPE_PRECISION(type)));
4285 int ibit2 = tree_log2 (@1);
4289 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4291 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4294 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4296 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4298 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4301 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4303 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4307 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4308 Currently disabled after pass lvec because ARM understands
4309 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
4311 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4312 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4313 (vec_cond (bit_and @0 @3) @1 @2)))
4315 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4316 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4317 (vec_cond (bit_ior @0 @3) @1 @2)))
4319 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4320 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4321 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4323 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4324 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4325 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4327 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
4329 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4330 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4331 (vec_cond (bit_and @0 @1) @2 @3)))
4333 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4334 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4335 (vec_cond (bit_ior @0 @1) @2 @3)))
4337 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4338 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4339 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4341 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4342 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4343 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4345 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4346 types are compatible. */
4348 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4349 (if (VECTOR_BOOLEAN_TYPE_P (type)
4350 && types_match (type, TREE_TYPE (@0)))
4351 (if (integer_zerop (@1) && integer_all_onesp (@2))
4353 (if (integer_all_onesp (@1) && integer_zerop (@2))
4356 /* A few simplifications of "a ? CST1 : CST2". */
4357 /* NOTE: Only do this on gimple as the if-chain-to-switch
4358 optimization depends on the gimple to have if statements in it. */
4361 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4363 (if (integer_zerop (@2))
4365 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4366 (if (integer_onep (@1))
4367 (convert (convert:boolean_type_node @0)))
4368 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4369 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4371 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4373 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4374 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
4375 here as the powerof2cst case above will handle that case correctly. */
4376 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4377 (negate (convert (convert:boolean_type_node @0))))))
4378 (if (integer_zerop (@1))
4380 tree booltrue = constant_boolean_node (true, boolean_type_node);
4383 /* a ? 0 : 1 -> !a. */
4384 (if (integer_onep (@2))
4385 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4386 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4387 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
4389 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4391 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4393 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
4394 here as the powerof2cst case above will handle that case correctly. */
4395 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4396 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4405 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
4406 (if (INTEGRAL_TYPE_P (type)
4407 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4408 (cond @1 (convert @2) (convert @3))))
4410 /* Simplification moved from fold_cond_expr_with_comparison. It may also
4412 /* This pattern implements two kinds simplification:
4415 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4416 1) Conversions are type widening from smaller type.
4417 2) Const c1 equals to c2 after canonicalizing comparison.
4418 3) Comparison has tree code LT, LE, GT or GE.
4419 This specific pattern is needed when (cmp (convert x) c) may not
4420 be simplified by comparison patterns because of multiple uses of
4421 x. It also makes sense here because simplifying across multiple
4422 referred var is always benefitial for complicated cases.
4425 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
4426 (for cmp (lt le gt ge eq)
4428 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4431 tree from_type = TREE_TYPE (@1);
4432 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4433 enum tree_code code = ERROR_MARK;
4435 if (INTEGRAL_TYPE_P (from_type)
4436 && int_fits_type_p (@2, from_type)
4437 && (types_match (c1_type, from_type)
4438 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4439 && (TYPE_UNSIGNED (from_type)
4440 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4441 && (types_match (c2_type, from_type)
4442 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4443 && (TYPE_UNSIGNED (from_type)
4444 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4448 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4450 /* X <= Y - 1 equals to X < Y. */
4453 /* X > Y - 1 equals to X >= Y. */
4457 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4459 /* X < Y + 1 equals to X <= Y. */
4462 /* X >= Y + 1 equals to X > Y. */
4466 if (code != ERROR_MARK
4467 || wi::to_widest (@2) == wi::to_widest (@3))
4469 if (cmp == LT_EXPR || cmp == LE_EXPR)
4471 if (cmp == GT_EXPR || cmp == GE_EXPR)
4475 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4476 else if (int_fits_type_p (@3, from_type))
4480 (if (code == MAX_EXPR)
4481 (convert (max @1 (convert @2)))
4482 (if (code == MIN_EXPR)
4483 (convert (min @1 (convert @2)))
4484 (if (code == EQ_EXPR)
4485 (convert (cond (eq @1 (convert @3))
4486 (convert:from_type @3) (convert:from_type @2)))))))))
4488 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4490 1) OP is PLUS or MINUS.
4491 2) CMP is LT, LE, GT or GE.
4492 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4494 This pattern also handles special cases like:
4496 A) Operand x is a unsigned to signed type conversion and c1 is
4497 integer zero. In this case,
4498 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4499 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4500 B) Const c1 may not equal to (C3 op' C2). In this case we also
4501 check equality for (c1+1) and (c1-1) by adjusting comparison
4504 TODO: Though signed type is handled by this pattern, it cannot be
4505 simplified at the moment because C standard requires additional
4506 type promotion. In order to match&simplify it here, the IR needs
4507 to be cleaned up by other optimizers, i.e, VRP. */
4508 (for op (plus minus)
4509 (for cmp (lt le gt ge)
4511 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4512 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4513 (if (types_match (from_type, to_type)
4514 /* Check if it is special case A). */
4515 || (TYPE_UNSIGNED (from_type)
4516 && !TYPE_UNSIGNED (to_type)
4517 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4518 && integer_zerop (@1)
4519 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4522 wi::overflow_type overflow = wi::OVF_NONE;
4523 enum tree_code code, cmp_code = cmp;
4525 wide_int c1 = wi::to_wide (@1);
4526 wide_int c2 = wi::to_wide (@2);
4527 wide_int c3 = wi::to_wide (@3);
4528 signop sgn = TYPE_SIGN (from_type);
4530 /* Handle special case A), given x of unsigned type:
4531 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4532 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4533 if (!types_match (from_type, to_type))
4535 if (cmp_code == LT_EXPR)
4537 if (cmp_code == GE_EXPR)
4539 c1 = wi::max_value (to_type);
4541 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4542 compute (c3 op' c2) and check if it equals to c1 with op' being
4543 the inverted operator of op. Make sure overflow doesn't happen
4544 if it is undefined. */
4545 if (op == PLUS_EXPR)
4546 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4548 real_c1 = wi::add (c3, c2, sgn, &overflow);
4551 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4553 /* Check if c1 equals to real_c1. Boundary condition is handled
4554 by adjusting comparison operation if necessary. */
4555 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4558 /* X <= Y - 1 equals to X < Y. */
4559 if (cmp_code == LE_EXPR)
4561 /* X > Y - 1 equals to X >= Y. */
4562 if (cmp_code == GT_EXPR)
4565 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4568 /* X < Y + 1 equals to X <= Y. */
4569 if (cmp_code == LT_EXPR)
4571 /* X >= Y + 1 equals to X > Y. */
4572 if (cmp_code == GE_EXPR)
4575 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4577 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4579 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4584 (if (code == MAX_EXPR)
4585 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4586 { wide_int_to_tree (from_type, c2); })
4587 (if (code == MIN_EXPR)
4588 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4589 { wide_int_to_tree (from_type, c2); })))))))))
4592 /* A >= B ? A : B -> max (A, B) and friends. The code is still
4593 in fold_cond_expr_with_comparison for GENERIC folding with
4594 some extra constraints. */
4595 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
4597 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
4598 (convert3? @0) (convert4? @1))
4599 (if (!HONOR_SIGNED_ZEROS (type)
4600 && (/* Allow widening conversions of the compare operands as data. */
4601 (INTEGRAL_TYPE_P (type)
4602 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
4603 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
4604 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
4605 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
4606 /* Or sign conversions for the comparison. */
4607 || (types_match (type, TREE_TYPE (@0))
4608 && types_match (type, TREE_TYPE (@1)))))
4610 (if (cmp == EQ_EXPR)
4611 (if (VECTOR_TYPE_P (type))
4614 (if (cmp == NE_EXPR)
4615 (if (VECTOR_TYPE_P (type))
4618 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
4619 (if (!HONOR_NANS (type))
4620 (if (VECTOR_TYPE_P (type))
4621 (view_convert (min @c0 @c1))
4622 (convert (min @c0 @c1)))))
4623 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
4624 (if (!HONOR_NANS (type))
4625 (if (VECTOR_TYPE_P (type))
4626 (view_convert (max @c0 @c1))
4627 (convert (max @c0 @c1)))))
4628 (if (cmp == UNEQ_EXPR)
4629 (if (!HONOR_NANS (type))
4630 (if (VECTOR_TYPE_P (type))
4633 (if (cmp == LTGT_EXPR)
4634 (if (!HONOR_NANS (type))
4635 (if (VECTOR_TYPE_P (type))
4637 (convert @c0))))))))
4640 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
4642 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
4643 (if (!TYPE_SATURATING (type)
4644 && (TYPE_OVERFLOW_WRAPS (type)
4645 || !wi::only_sign_bit_p (wi::to_wide (@1)))
4646 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
4649 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
4651 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
4652 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
4655 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
4656 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
4658 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
4659 (if (TYPE_UNSIGNED (type))
4660 (cond (ge @0 @1) (negate @0) @2)))
4662 (for cnd (cond vec_cond)
4663 /* A ? B : (A ? X : C) -> A ? B : C. */
4665 (cnd @0 (cnd @0 @1 @2) @3)
4668 (cnd @0 @1 (cnd @0 @2 @3))
4670 /* A ? B : (!A ? C : X) -> A ? B : C. */
4671 /* ??? This matches embedded conditions open-coded because genmatch
4672 would generate matching code for conditions in separate stmts only.
4673 The following is still important to merge then and else arm cases
4674 from if-conversion. */
4676 (cnd @0 @1 (cnd @2 @3 @4))
4677 (if (inverse_conditions_p (@0, @2))
4680 (cnd @0 (cnd @1 @2 @3) @4)
4681 (if (inverse_conditions_p (@0, @1))
4684 /* A ? B : B -> B. */
4689 /* !A ? B : C -> A ? C : B. */
4691 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4694 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4695 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4696 Need to handle UN* comparisons.
4698 None of these transformations work for modes with signed
4699 zeros. If A is +/-0, the first two transformations will
4700 change the sign of the result (from +0 to -0, or vice
4701 versa). The last four will fix the sign of the result,
4702 even though the original expressions could be positive or
4703 negative, depending on the sign of A.
4705 Note that all these transformations are correct if A is
4706 NaN, since the two alternatives (A and -A) are also NaNs. */
4708 (for cnd (cond vec_cond)
4709 /* A == 0 ? A : -A same as -A */
4712 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4713 (if (!HONOR_SIGNED_ZEROS (type))
4716 (cnd (cmp @0 zerop) integer_zerop (negate@1 @0))
4717 (if (!HONOR_SIGNED_ZEROS (type))
4720 /* A != 0 ? A : -A same as A */
4723 (cnd (cmp @0 zerop) @0 (negate @0))
4724 (if (!HONOR_SIGNED_ZEROS (type))
4727 (cnd (cmp @0 zerop) @0 integer_zerop)
4728 (if (!HONOR_SIGNED_ZEROS (type))
4731 /* A >=/> 0 ? A : -A same as abs (A) */
4734 (cnd (cmp @0 zerop) @0 (negate @0))
4735 (if (!HONOR_SIGNED_ZEROS (type)
4736 && !TYPE_UNSIGNED (type))
4738 /* A <=/< 0 ? A : -A same as -abs (A) */
4741 (cnd (cmp @0 zerop) @0 (negate @0))
4742 (if (!HONOR_SIGNED_ZEROS (type)
4743 && !TYPE_UNSIGNED (type))
4744 (if (ANY_INTEGRAL_TYPE_P (type)
4745 && !TYPE_OVERFLOW_WRAPS (type))
4747 tree utype = unsigned_type_for (type);
4749 (convert (negate (absu:utype @0))))
4750 (negate (abs @0)))))
4754 /* -(type)!A -> (type)A - 1. */
4756 (negate (convert?:s (logical_inverted_value:s @0)))
4757 (if (INTEGRAL_TYPE_P (type)
4758 && TREE_CODE (type) != BOOLEAN_TYPE
4759 && TYPE_PRECISION (type) > 1
4760 && TREE_CODE (@0) == SSA_NAME
4761 && ssa_name_has_boolean_range (@0))
4762 (plus (convert:type @0) { build_all_ones_cst (type); })))
4764 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4765 return all -1 or all 0 results. */
4766 /* ??? We could instead convert all instances of the vec_cond to negate,
4767 but that isn't necessarily a win on its own. */
4769 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4770 (if (VECTOR_TYPE_P (type)
4771 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4772 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4773 && (TYPE_MODE (TREE_TYPE (type))
4774 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4775 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4777 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4779 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4780 (if (VECTOR_TYPE_P (type)
4781 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4782 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4783 && (TYPE_MODE (TREE_TYPE (type))
4784 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4785 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4788 /* Simplifications of comparisons. */
4790 /* See if we can reduce the magnitude of a constant involved in a
4791 comparison by changing the comparison code. This is a canonicalization
4792 formerly done by maybe_canonicalize_comparison_1. */
4796 (cmp @0 uniform_integer_cst_p@1)
4797 (with { tree cst = uniform_integer_cst_p (@1); }
4798 (if (tree_int_cst_sgn (cst) == -1)
4799 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4800 wide_int_to_tree (TREE_TYPE (cst),
4806 (cmp @0 uniform_integer_cst_p@1)
4807 (with { tree cst = uniform_integer_cst_p (@1); }
4808 (if (tree_int_cst_sgn (cst) == 1)
4809 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4810 wide_int_to_tree (TREE_TYPE (cst),
4811 wi::to_wide (cst) - 1)); })))))
4813 /* We can simplify a logical negation of a comparison to the
4814 inverted comparison. As we cannot compute an expression
4815 operator using invert_tree_comparison we have to simulate
4816 that with expression code iteration. */
4817 (for cmp (tcc_comparison)
4818 icmp (inverted_tcc_comparison)
4819 ncmp (inverted_tcc_comparison_with_nans)
4820 /* Ideally we'd like to combine the following two patterns
4821 and handle some more cases by using
4822 (logical_inverted_value (cmp @0 @1))
4823 here but for that genmatch would need to "inline" that.
4824 For now implement what forward_propagate_comparison did. */
4826 (bit_not (cmp @0 @1))
4827 (if (VECTOR_TYPE_P (type)
4828 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4829 /* Comparison inversion may be impossible for trapping math,
4830 invert_tree_comparison will tell us. But we can't use
4831 a computed operator in the replacement tree thus we have
4832 to play the trick below. */
4833 (with { enum tree_code ic = invert_tree_comparison
4834 (cmp, HONOR_NANS (@0)); }
4840 (bit_xor (cmp @0 @1) integer_truep)
4841 (with { enum tree_code ic = invert_tree_comparison
4842 (cmp, HONOR_NANS (@0)); }
4847 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
4849 (ne (cmp@2 @0 @1) integer_zerop)
4850 (if (types_match (type, TREE_TYPE (@2)))
4853 (eq (cmp@2 @0 @1) integer_truep)
4854 (if (types_match (type, TREE_TYPE (@2)))
4857 (ne (cmp@2 @0 @1) integer_truep)
4858 (if (types_match (type, TREE_TYPE (@2)))
4859 (with { enum tree_code ic = invert_tree_comparison
4860 (cmp, HONOR_NANS (@0)); }
4866 (eq (cmp@2 @0 @1) integer_zerop)
4867 (if (types_match (type, TREE_TYPE (@2)))
4868 (with { enum tree_code ic = invert_tree_comparison
4869 (cmp, HONOR_NANS (@0)); }
4875 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4876 ??? The transformation is valid for the other operators if overflow
4877 is undefined for the type, but performing it here badly interacts
4878 with the transformation in fold_cond_expr_with_comparison which
4879 attempts to synthetize ABS_EXPR. */
4881 (for sub (minus pointer_diff)
4883 (cmp (sub@2 @0 @1) integer_zerop)
4884 (if (single_use (@2))
4887 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4888 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4891 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4892 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4893 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4894 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4895 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4896 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4897 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4899 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4900 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4901 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4902 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4903 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4905 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4906 signed arithmetic case. That form is created by the compiler
4907 often enough for folding it to be of value. One example is in
4908 computing loop trip counts after Operator Strength Reduction. */
4909 (for cmp (simple_comparison)
4910 scmp (swapped_simple_comparison)
4912 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4913 /* Handle unfolded multiplication by zero. */
4914 (if (integer_zerop (@1))
4916 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4917 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4919 /* If @1 is negative we swap the sense of the comparison. */
4920 (if (tree_int_cst_sgn (@1) < 0)
4924 /* For integral types with undefined overflow fold
4925 x * C1 == C2 into x == C2 / C1 or false.
4926 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4930 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4931 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4932 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4933 && wi::to_wide (@1) != 0)
4934 (with { widest_int quot; }
4935 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4936 TYPE_SIGN (TREE_TYPE (@0)), "))
4937 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4938 { constant_boolean_node (cmp == NE_EXPR, type); }))
4939 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4940 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4941 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4944 tree itype = TREE_TYPE (@0);
4945 int p = TYPE_PRECISION (itype);
4946 wide_int m = wi::one (p + 1) << p;
4947 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4948 wide_int i = wide_int::from (wi::mod_inv (a, m),
4949 p, TYPE_SIGN (itype));
4950 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4953 /* Simplify comparison of something with itself. For IEEE
4954 floating-point, we can only do some of these simplifications. */
4958 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4959 || ! tree_expr_maybe_nan_p (@0))
4960 { constant_boolean_node (true, type); }
4962 /* With -ftrapping-math conversion to EQ loses an exception. */
4963 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
4964 || ! flag_trapping_math))
4970 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4971 || ! tree_expr_maybe_nan_p (@0))
4972 { constant_boolean_node (false, type); })))
4973 (for cmp (unle unge uneq)
4976 { constant_boolean_node (true, type); }))
4977 (for cmp (unlt ungt)
4983 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
4984 { constant_boolean_node (false, type); }))
4986 /* x == ~x -> false */
4987 /* x != ~x -> true */
4990 (cmp:c @0 (bit_not @0))
4991 { constant_boolean_node (cmp == NE_EXPR, type); }))
4993 /* Fold ~X op ~Y as Y op X. */
4994 (for cmp (simple_comparison)
4996 (cmp (bit_not@2 @0) (bit_not@3 @1))
4997 (if (single_use (@2) && single_use (@3))
5000 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
5001 (for cmp (simple_comparison)
5002 scmp (swapped_simple_comparison)
5004 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5005 (if (single_use (@2)
5006 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5007 (scmp @0 (bit_not @1)))))
5009 (for cmp (simple_comparison)
5010 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
5012 (cmp (convert@2 @0) (convert? @1))
5013 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5014 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
5015 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5016 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
5017 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
5020 tree type1 = TREE_TYPE (@1);
5021 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
5023 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
5024 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
5025 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
5026 type1 = float_type_node;
5027 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
5028 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
5029 type1 = double_type_node;
5032 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
5033 ? TREE_TYPE (@0) : type1);
5035 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
5036 (cmp (convert:newtype @0) (convert:newtype @1))))))
5040 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
5042 /* a CMP (-0) -> a CMP 0 */
5043 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5044 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5045 /* (-0) CMP b -> 0 CMP b. */
5046 (if (TREE_CODE (@0) == REAL_CST
5047 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5048 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5049 /* x != NaN is always true, other ops are always false. */
5050 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5051 && !tree_expr_signaling_nan_p (@1)
5052 && !tree_expr_maybe_signaling_nan_p (@0))
5053 { constant_boolean_node (cmp == NE_EXPR, type); })
5054 /* NaN != y is always true, other ops are always false. */
5055 (if (TREE_CODE (@0) == REAL_CST
5056 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5057 && !tree_expr_signaling_nan_p (@0)
5058 && !tree_expr_signaling_nan_p (@1))
5059 { constant_boolean_node (cmp == NE_EXPR, type); })
5060 /* Fold comparisons against infinity. */
5061 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5062 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5065 REAL_VALUE_TYPE max;
5066 enum tree_code code = cmp;
5067 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5069 code = swap_tree_comparison (code);
5072 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
5073 (if (code == GT_EXPR
5074 && !(HONOR_NANS (@0) && flag_trapping_math))
5075 { constant_boolean_node (false, type); })
5076 (if (code == LE_EXPR)
5077 /* x <= +Inf is always true, if we don't care about NaNs. */
5078 (if (! HONOR_NANS (@0))
5079 { constant_boolean_node (true, type); }
5080 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5081 an "invalid" exception. */
5082 (if (!flag_trapping_math)
5084 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5085 for == this introduces an exception for x a NaN. */
5086 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5088 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5090 (lt @0 { build_real (TREE_TYPE (@0), max); })
5091 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5092 /* x < +Inf is always equal to x <= DBL_MAX. */
5093 (if (code == LT_EXPR)
5094 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5096 (ge @0 { build_real (TREE_TYPE (@0), max); })
5097 (le @0 { build_real (TREE_TYPE (@0), max); }))))
5098 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5099 an exception for x a NaN so use an unordered comparison. */
5100 (if (code == NE_EXPR)
5101 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5102 (if (! HONOR_NANS (@0))
5104 (ge @0 { build_real (TREE_TYPE (@0), max); })
5105 (le @0 { build_real (TREE_TYPE (@0), max); }))
5107 (unge @0 { build_real (TREE_TYPE (@0), max); })
5108 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5110 /* If this is a comparison of a real constant with a PLUS_EXPR
5111 or a MINUS_EXPR of a real constant, we can convert it into a
5112 comparison with a revised real constant as long as no overflow
5113 occurs when unsafe_math_optimizations are enabled. */
5114 (if (flag_unsafe_math_optimizations)
5115 (for op (plus minus)
5117 (cmp (op @0 REAL_CST@1) REAL_CST@2)
5120 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5121 TREE_TYPE (@1), @2, @1);
5123 (if (tem && !TREE_OVERFLOW (tem))
5124 (cmp @0 { tem; }))))))
5126 /* Likewise, we can simplify a comparison of a real constant with
5127 a MINUS_EXPR whose first operand is also a real constant, i.e.
5128 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
5129 floating-point types only if -fassociative-math is set. */
5130 (if (flag_associative_math)
5132 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5133 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5134 (if (tem && !TREE_OVERFLOW (tem))
5135 (cmp { tem; } @1)))))
5137 /* Fold comparisons against built-in math functions. */
5138 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5141 (cmp (sq @0) REAL_CST@1)
5143 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5145 /* sqrt(x) < y is always false, if y is negative. */
5146 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5147 { constant_boolean_node (false, type); })
5148 /* sqrt(x) > y is always true, if y is negative and we
5149 don't care about NaNs, i.e. negative values of x. */
5150 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5151 { constant_boolean_node (true, type); })
5152 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5153 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5154 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5156 /* sqrt(x) < 0 is always false. */
5157 (if (cmp == LT_EXPR)
5158 { constant_boolean_node (false, type); })
5159 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
5160 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5161 { constant_boolean_node (true, type); })
5162 /* sqrt(x) <= 0 -> x == 0. */
5163 (if (cmp == LE_EXPR)
5165 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
5166 == or !=. In the last case:
5168 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5170 if x is negative or NaN. Due to -funsafe-math-optimizations,
5171 the results for other x follow from natural arithmetic. */
5173 (if ((cmp == LT_EXPR
5177 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5178 /* Give up for -frounding-math. */
5179 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5183 enum tree_code ncmp = cmp;
5184 const real_format *fmt
5185 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5186 real_arithmetic (&c2, MULT_EXPR,
5187 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5188 real_convert (&c2, fmt, &c2);
5189 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5190 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
5191 if (!REAL_VALUE_ISINF (c2))
5193 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5194 build_real (TREE_TYPE (@0), c2));
5195 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5197 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5198 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5199 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5200 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5201 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5202 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5205 /* With rounding to even, sqrt of up to 3 different values
5206 gives the same normal result, so in some cases c2 needs
5208 REAL_VALUE_TYPE c2alt, tow;
5209 if (cmp == LT_EXPR || cmp == GE_EXPR)
5213 real_nextafter (&c2alt, fmt, &c2, &tow);
5214 real_convert (&c2alt, fmt, &c2alt);
5215 if (REAL_VALUE_ISINF (c2alt))
5219 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5220 build_real (TREE_TYPE (@0), c2alt));
5221 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5223 else if (real_equal (&TREE_REAL_CST (c3),
5224 &TREE_REAL_CST (@1)))
5230 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5231 (if (REAL_VALUE_ISINF (c2))
5232 /* sqrt(x) > y is x == +Inf, when y is very large. */
5233 (if (HONOR_INFINITIES (@0))
5234 (eq @0 { build_real (TREE_TYPE (@0), c2); })
5235 { constant_boolean_node (false, type); })
5236 /* sqrt(x) > c is the same as x > c*c. */
5237 (if (ncmp != ERROR_MARK)
5238 (if (ncmp == GE_EXPR)
5239 (ge @0 { build_real (TREE_TYPE (@0), c2); })
5240 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5241 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
5242 (if (REAL_VALUE_ISINF (c2))
5244 /* sqrt(x) < y is always true, when y is a very large
5245 value and we don't care about NaNs or Infinities. */
5246 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5247 { constant_boolean_node (true, type); })
5248 /* sqrt(x) < y is x != +Inf when y is very large and we
5249 don't care about NaNs. */
5250 (if (! HONOR_NANS (@0))
5251 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5252 /* sqrt(x) < y is x >= 0 when y is very large and we
5253 don't care about Infinities. */
5254 (if (! HONOR_INFINITIES (@0))
5255 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5256 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5259 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5260 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5261 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5262 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5263 (if (ncmp == LT_EXPR)
5264 (lt @0 { build_real (TREE_TYPE (@0), c2); })
5265 (le @0 { build_real (TREE_TYPE (@0), c2); }))
5266 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5267 (if (ncmp != ERROR_MARK && GENERIC)
5268 (if (ncmp == LT_EXPR)
5270 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5271 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5273 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5274 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5275 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
5277 (cmp (sq @0) (sq @1))
5278 (if (! HONOR_NANS (@0))
5281 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
5282 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5283 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
5285 (cmp (float@0 @1) (float @2))
5286 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5287 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5290 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5291 tree type1 = TREE_TYPE (@1);
5292 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5293 tree type2 = TREE_TYPE (@2);
5294 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5296 (if (fmt.can_represent_integral_type_p (type1)
5297 && fmt.can_represent_integral_type_p (type2))
5298 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5299 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5300 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5301 && type1_signed_p >= type2_signed_p)
5302 (icmp @1 (convert @2))
5303 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5304 && type1_signed_p <= type2_signed_p)
5305 (icmp (convert:type2 @1) @2)
5306 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5307 && type1_signed_p == type2_signed_p)
5308 (icmp @1 @2))))))))))
5310 /* Optimize various special cases of (FTYPE) N CMP CST. */
5311 (for cmp (lt le eq ne ge gt)
5312 icmp (le le eq ne ge ge)
5314 (cmp (float @0) REAL_CST@1)
5315 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5316 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5319 tree itype = TREE_TYPE (@0);
5320 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5321 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5322 /* Be careful to preserve any potential exceptions due to
5323 NaNs. qNaNs are ok in == or != context.
5324 TODO: relax under -fno-trapping-math or
5325 -fno-signaling-nans. */
5327 = real_isnan (cst) && (cst->signalling
5328 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5330 /* TODO: allow non-fitting itype and SNaNs when
5331 -fno-trapping-math. */
5332 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5335 signop isign = TYPE_SIGN (itype);
5336 REAL_VALUE_TYPE imin, imax;
5337 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5338 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5340 REAL_VALUE_TYPE icst;
5341 if (cmp == GT_EXPR || cmp == GE_EXPR)
5342 real_ceil (&icst, fmt, cst);
5343 else if (cmp == LT_EXPR || cmp == LE_EXPR)
5344 real_floor (&icst, fmt, cst);
5346 real_trunc (&icst, fmt, cst);
5348 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5350 bool overflow_p = false;
5352 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5355 /* Optimize cases when CST is outside of ITYPE's range. */
5356 (if (real_compare (LT_EXPR, cst, &imin))
5357 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5359 (if (real_compare (GT_EXPR, cst, &imax))
5360 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5362 /* Remove cast if CST is an integer representable by ITYPE. */
5364 (cmp @0 { gcc_assert (!overflow_p);
5365 wide_int_to_tree (itype, icst_val); })
5367 /* When CST is fractional, optimize
5368 (FTYPE) N == CST -> 0
5369 (FTYPE) N != CST -> 1. */
5370 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5371 { constant_boolean_node (cmp == NE_EXPR, type); })
5372 /* Otherwise replace with sensible integer constant. */
5375 gcc_checking_assert (!overflow_p);
5377 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5379 /* Fold A /[ex] B CMP C to A CMP B * C. */
5382 (cmp (exact_div @0 @1) INTEGER_CST@2)
5383 (if (!integer_zerop (@1))
5384 (if (wi::to_wide (@2) == 0)
5386 (if (TREE_CODE (@1) == INTEGER_CST)
5389 wi::overflow_type ovf;
5390 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5391 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5394 { constant_boolean_node (cmp == NE_EXPR, type); }
5395 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5396 (for cmp (lt le gt ge)
5398 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5399 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5402 wi::overflow_type ovf;
5403 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5404 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5407 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5408 TYPE_SIGN (TREE_TYPE (@2)))
5409 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5410 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5412 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5414 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5415 For large C (more than min/B+2^size), this is also true, with the
5416 multiplication computed modulo 2^size.
5417 For intermediate C, this just tests the sign of A. */
5418 (for cmp (lt le gt ge)
5421 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5422 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5423 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5424 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5427 tree utype = TREE_TYPE (@2);
5428 wide_int denom = wi::to_wide (@1);
5429 wide_int right = wi::to_wide (@2);
5430 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5431 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5432 bool small = wi::leu_p (right, smax);
5433 bool large = wi::geu_p (right, smin);
5435 (if (small || large)
5436 (cmp (convert:utype @0) (mult @2 (convert @1)))
5437 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5439 /* Unordered tests if either argument is a NaN. */
5441 (bit_ior (unordered @0 @0) (unordered @1 @1))
5442 (if (types_match (@0, @1))
5445 (bit_and (ordered @0 @0) (ordered @1 @1))
5446 (if (types_match (@0, @1))
5449 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5452 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5455 /* Simple range test simplifications. */
5456 /* A < B || A >= B -> true. */
5457 (for test1 (lt le le le ne ge)
5458 test2 (ge gt ge ne eq ne)
5460 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5461 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5462 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5463 { constant_boolean_node (true, type); })))
5464 /* A < B && A >= B -> false. */
5465 (for test1 (lt lt lt le ne eq)
5466 test2 (ge gt eq gt eq gt)
5468 (bit_and:c (test1 @0 @1) (test2 @0 @1))
5469 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5470 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5471 { constant_boolean_node (false, type); })))
5473 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5474 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
5476 Note that comparisons
5477 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
5478 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
5479 will be canonicalized to above so there's no need to
5486 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5487 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5490 tree ty = TREE_TYPE (@0);
5491 unsigned prec = TYPE_PRECISION (ty);
5492 wide_int mask = wi::to_wide (@2, prec);
5493 wide_int rhs = wi::to_wide (@3, prec);
5494 signop sgn = TYPE_SIGN (ty);
5496 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5497 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5498 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5499 { build_zero_cst (ty); }))))))
5501 /* -A CMP -B -> B CMP A. */
5502 (for cmp (tcc_comparison)
5503 scmp (swapped_tcc_comparison)
5505 (cmp (negate @0) (negate @1))
5506 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5507 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5508 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5511 (cmp (negate @0) CONSTANT_CLASS_P@1)
5512 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5513 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5514 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5515 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5516 (if (tem && !TREE_OVERFLOW (tem))
5517 (scmp @0 { tem; }))))))
5519 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
5522 (op (abs @0) zerop@1)
5525 /* From fold_sign_changed_comparison and fold_widened_comparison.
5526 FIXME: the lack of symmetry is disturbing. */
5527 (for cmp (simple_comparison)
5529 (cmp (convert@0 @00) (convert?@1 @10))
5530 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5531 /* Disable this optimization if we're casting a function pointer
5532 type on targets that require function pointer canonicalization. */
5533 && !(targetm.have_canonicalize_funcptr_for_compare ()
5534 && ((POINTER_TYPE_P (TREE_TYPE (@00))
5535 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5536 || (POINTER_TYPE_P (TREE_TYPE (@10))
5537 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5539 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5540 && (TREE_CODE (@10) == INTEGER_CST
5542 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5545 && !POINTER_TYPE_P (TREE_TYPE (@00))
5546 /* (int)bool:32 != (int)uint is not the same as
5547 bool:32 != (bool:32)uint since boolean types only have two valid
5548 values independent of their precision. */
5549 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
5550 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
5551 /* ??? The special-casing of INTEGER_CST conversion was in the original
5552 code and here to avoid a spurious overflow flag on the resulting
5553 constant which fold_convert produces. */
5554 (if (TREE_CODE (@1) == INTEGER_CST)
5555 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5556 TREE_OVERFLOW (@1)); })
5557 (cmp @00 (convert @1)))
5559 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5560 /* If possible, express the comparison in the shorter mode. */
5561 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5562 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5563 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5564 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5565 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5566 || ((TYPE_PRECISION (TREE_TYPE (@00))
5567 >= TYPE_PRECISION (TREE_TYPE (@10)))
5568 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5569 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5570 || (TREE_CODE (@10) == INTEGER_CST
5571 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5572 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5573 (cmp @00 (convert @10))
5574 (if (TREE_CODE (@10) == INTEGER_CST
5575 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5576 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5579 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5580 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5581 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5582 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5584 (if (above || below)
5585 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5586 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5587 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5588 { constant_boolean_node (above ? true : false, type); }
5589 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5590 { constant_boolean_node (above ? false : true, type); }))))))))))))
5594 /* SSA names are canonicalized to 2nd place. */
5595 (cmp addr@0 SSA_NAME@1)
5597 { poly_int64 off; tree base; }
5598 /* A local variable can never be pointed to by
5599 the default SSA name of an incoming parameter. */
5600 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5601 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5602 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5603 && TREE_CODE (base) == VAR_DECL
5604 && auto_var_in_fn_p (base, current_function_decl))
5605 (if (cmp == NE_EXPR)
5606 { constant_boolean_node (true, type); }
5607 { constant_boolean_node (false, type); })
5608 /* If the address is based on @1 decide using the offset. */
5609 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5610 && TREE_CODE (base) == MEM_REF
5611 && TREE_OPERAND (base, 0) == @1)
5612 (with { off += mem_ref_offset (base).force_shwi (); }
5613 (if (known_ne (off, 0))
5614 { constant_boolean_node (cmp == NE_EXPR, type); }
5615 (if (known_eq (off, 0))
5616 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5618 /* Equality compare simplifications from fold_binary */
5621 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5622 Similarly for NE_EXPR. */
5624 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5625 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5626 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5627 { constant_boolean_node (cmp == NE_EXPR, type); }))
5629 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5631 (cmp (bit_xor @0 @1) integer_zerop)
5634 /* (X ^ Y) == Y becomes X == 0.
5635 Likewise (X ^ Y) == X becomes Y == 0. */
5637 (cmp:c (bit_xor:c @0 @1) @0)
5638 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5640 /* (X & Y) == X becomes (X & ~Y) == 0. */
5642 (cmp:c (bit_and:c @0 @1) @0)
5643 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5645 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5646 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5647 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5648 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5649 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5650 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5651 && !wi::neg_p (wi::to_wide (@1)))
5652 (cmp (bit_and @0 (convert (bit_not @1)))
5653 { build_zero_cst (TREE_TYPE (@0)); })))
5655 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5657 (cmp:c (bit_ior:c @0 @1) @1)
5658 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5660 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5662 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5663 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5664 (cmp @0 (bit_xor @1 (convert @2)))))
5667 (cmp (convert? addr@0) integer_zerop)
5668 (if (tree_single_nonzero_warnv_p (@0, NULL))
5669 { constant_boolean_node (cmp == NE_EXPR, type); }))
5671 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5673 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5674 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5676 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5677 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5678 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5679 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5684 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5685 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5686 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5687 && types_match (@0, @1))
5688 (ncmp (bit_xor @0 @1) @2)))))
5689 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5690 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5694 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5695 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5696 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5697 && types_match (@0, @1))
5698 (ncmp (bit_xor @0 @1) @2))))
5700 /* If we have (A & C) == C where C is a power of 2, convert this into
5701 (A & C) != 0. Similarly for NE_EXPR. */
5705 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5706 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5708 /* From fold_binary_op_with_conditional_arg handle the case of
5709 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
5710 compares simplify. */
5711 (for cmp (simple_comparison)
5713 (cmp:c (cond @0 @1 @2) @3)
5714 /* Do not move possibly trapping operations into the conditional as this
5715 pessimizes code and causes gimplification issues when applied late. */
5716 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
5717 || operation_could_trap_p (cmp, true, false, @3))
5718 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
5721 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5722 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5724 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5725 (if (INTEGRAL_TYPE_P (type)
5726 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5727 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5728 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5731 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5733 (if (cmp == LT_EXPR)
5734 (bit_xor (convert (rshift @0 {shifter;})) @1)
5735 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5736 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5737 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5739 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5740 (if (INTEGRAL_TYPE_P (type)
5741 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5742 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5743 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5746 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5748 (if (cmp == GE_EXPR)
5749 (bit_xor (convert (rshift @0 {shifter;})) @1)
5750 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5752 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5753 convert this into a shift followed by ANDing with D. */
5756 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5757 INTEGER_CST@2 integer_zerop)
5758 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5760 int shift = (wi::exact_log2 (wi::to_wide (@2))
5761 - wi::exact_log2 (wi::to_wide (@1)));
5765 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5767 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5770 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5771 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5775 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5776 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5777 && type_has_mode_precision_p (TREE_TYPE (@0))
5778 && element_precision (@2) >= element_precision (@0)
5779 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5780 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5781 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5783 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5784 this into a right shift or sign extension followed by ANDing with C. */
5787 (lt @0 integer_zerop)
5788 INTEGER_CST@1 integer_zerop)
5789 (if (integer_pow2p (@1)
5790 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5792 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5796 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5798 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5799 sign extension followed by AND with C will achieve the effect. */
5800 (bit_and (convert @0) @1)))))
5802 /* When the addresses are not directly of decls compare base and offset.
5803 This implements some remaining parts of fold_comparison address
5804 comparisons but still no complete part of it. Still it is good
5805 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5806 (for cmp (simple_comparison)
5808 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5811 poly_int64 off0, off1;
5813 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
5814 off0, off1, GENERIC);
5818 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5819 { constant_boolean_node (known_eq (off0, off1), type); })
5820 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5821 { constant_boolean_node (known_ne (off0, off1), type); })
5822 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5823 { constant_boolean_node (known_lt (off0, off1), type); })
5824 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5825 { constant_boolean_node (known_le (off0, off1), type); })
5826 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5827 { constant_boolean_node (known_ge (off0, off1), type); })
5828 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5829 { constant_boolean_node (known_gt (off0, off1), type); }))
5832 (if (cmp == EQ_EXPR)
5833 { constant_boolean_node (false, type); })
5834 (if (cmp == NE_EXPR)
5835 { constant_boolean_node (true, type); })))))))
5837 /* Simplify pointer equality compares using PTA. */
5841 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5842 && ptrs_compare_unequal (@0, @1))
5843 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5845 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5846 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5847 Disable the transform if either operand is pointer to function.
5848 This broke pr22051-2.c for arm where function pointer
5849 canonicalizaion is not wanted. */
5853 (cmp (convert @0) INTEGER_CST@1)
5854 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5855 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5856 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5857 /* Don't perform this optimization in GENERIC if @0 has reference
5858 type when sanitizing. See PR101210. */
5860 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5861 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5862 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5863 && POINTER_TYPE_P (TREE_TYPE (@1))
5864 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5865 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5866 (cmp @0 (convert @1)))))
5868 /* Non-equality compare simplifications from fold_binary */
5869 (for cmp (lt gt le ge)
5870 /* Comparisons with the highest or lowest possible integer of
5871 the specified precision will have known values. */
5873 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5874 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5875 || POINTER_TYPE_P (TREE_TYPE (@1))
5876 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5877 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5880 tree cst = uniform_integer_cst_p (@1);
5881 tree arg1_type = TREE_TYPE (cst);
5882 unsigned int prec = TYPE_PRECISION (arg1_type);
5883 wide_int max = wi::max_value (arg1_type);
5884 wide_int signed_max = wi::max_value (prec, SIGNED);
5885 wide_int min = wi::min_value (arg1_type);
5888 (if (wi::to_wide (cst) == max)
5890 (if (cmp == GT_EXPR)
5891 { constant_boolean_node (false, type); })
5892 (if (cmp == GE_EXPR)
5894 (if (cmp == LE_EXPR)
5895 { constant_boolean_node (true, type); })
5896 (if (cmp == LT_EXPR)
5898 (if (wi::to_wide (cst) == min)
5900 (if (cmp == LT_EXPR)
5901 { constant_boolean_node (false, type); })
5902 (if (cmp == LE_EXPR)
5904 (if (cmp == GE_EXPR)
5905 { constant_boolean_node (true, type); })
5906 (if (cmp == GT_EXPR)
5908 (if (wi::to_wide (cst) == max - 1)
5910 (if (cmp == GT_EXPR)
5911 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5912 wide_int_to_tree (TREE_TYPE (cst),
5915 (if (cmp == LE_EXPR)
5916 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5917 wide_int_to_tree (TREE_TYPE (cst),
5920 (if (wi::to_wide (cst) == min + 1)
5922 (if (cmp == GE_EXPR)
5923 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5924 wide_int_to_tree (TREE_TYPE (cst),
5927 (if (cmp == LT_EXPR)
5928 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5929 wide_int_to_tree (TREE_TYPE (cst),
5932 (if (wi::to_wide (cst) == signed_max
5933 && TYPE_UNSIGNED (arg1_type)
5934 /* We will flip the signedness of the comparison operator
5935 associated with the mode of @1, so the sign bit is
5936 specified by this mode. Check that @1 is the signed
5937 max associated with this sign bit. */
5938 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5939 /* signed_type does not work on pointer types. */
5940 && INTEGRAL_TYPE_P (arg1_type))
5941 /* The following case also applies to X < signed_max+1
5942 and X >= signed_max+1 because previous transformations. */
5943 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5944 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5946 (if (cst == @1 && cmp == LE_EXPR)
5947 (ge (convert:st @0) { build_zero_cst (st); }))
5948 (if (cst == @1 && cmp == GT_EXPR)
5949 (lt (convert:st @0) { build_zero_cst (st); }))
5950 (if (cmp == LE_EXPR)
5951 (ge (view_convert:st @0) { build_zero_cst (st); }))
5952 (if (cmp == GT_EXPR)
5953 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5955 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5956 /* If the second operand is NaN, the result is constant. */
5959 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5960 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5961 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5962 ? false : true, type); })))
5964 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5968 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5969 { constant_boolean_node (true, type); })
5970 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5971 { constant_boolean_node (false, type); })))
5973 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5977 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5978 { constant_boolean_node (false, type); })
5979 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5980 { constant_boolean_node (true, type); })))
5982 /* bool_var != 0 becomes bool_var. */
5984 (ne @0 integer_zerop)
5985 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5986 && types_match (type, TREE_TYPE (@0)))
5988 /* bool_var == 1 becomes bool_var. */
5990 (eq @0 integer_onep)
5991 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5992 && types_match (type, TREE_TYPE (@0)))
5995 bool_var == 0 becomes !bool_var or
5996 bool_var != 1 becomes !bool_var
5997 here because that only is good in assignment context as long
5998 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5999 replace if (x == 0) with tem = ~x; if (tem != 0) which is
6000 clearly less optimal and which we'll transform again in forwprop. */
6002 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6003 where ~Y + 1 == pow2 and Z = ~Y. */
6004 (for cst (VECTOR_CST INTEGER_CST)
6008 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6009 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6010 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6011 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6013 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6014 (icmp (view_convert:utype @0) { csts; }))))))))
6016 /* When one argument is a constant, overflow detection can be simplified.
6017 Currently restricted to single use so as not to interfere too much with
6018 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6019 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
6020 (for cmp (lt le ge gt)
6023 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6024 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6025 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6026 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6027 && wi::to_wide (@1) != 0
6030 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6031 signop sign = TYPE_SIGN (TREE_TYPE (@0));
6033 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6034 wi::max_value (prec, sign)
6035 - wi::to_wide (@1)); })))))
6037 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6038 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6039 expects the long form, so we restrict the transformation for now. */
6042 (cmp:c (minus@2 @0 @1) @0)
6043 (if (single_use (@2)
6044 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6045 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6048 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
6051 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6052 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6053 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6056 /* Testing for overflow is unnecessary if we already know the result. */
6061 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6062 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6063 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6064 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6069 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6070 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6071 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6072 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6074 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6075 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
6079 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6080 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6081 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6082 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6084 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6085 is at least twice as wide as type of A and B, simplify to
6086 __builtin_mul_overflow (A, B, <unused>). */
6089 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6091 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6092 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6093 && TYPE_UNSIGNED (TREE_TYPE (@0))
6094 && (TYPE_PRECISION (TREE_TYPE (@3))
6095 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6096 && tree_fits_uhwi_p (@2)
6097 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6098 && types_match (@0, @1)
6099 && type_has_mode_precision_p (TREE_TYPE (@0))
6100 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6101 != CODE_FOR_nothing))
6102 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6103 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6105 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
6106 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6108 (ovf (convert@2 @0) @1)
6109 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6110 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6111 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6112 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6115 (ovf @1 (convert@2 @0))
6116 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6117 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6118 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6119 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6122 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6123 are unsigned to x > (umax / cst). Similarly for signed type, but
6124 in that case it needs to be outside of a range. */
6126 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6127 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6128 && TYPE_MAX_VALUE (TREE_TYPE (@0))
6129 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6130 && int_fits_type_p (@1, TREE_TYPE (@0)))
6131 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6132 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6133 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6134 (if (integer_minus_onep (@1))
6135 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6138 tree div = fold_convert (TREE_TYPE (@0), @1);
6139 tree lo = int_const_binop (TRUNC_DIV_EXPR,
6140 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6141 tree hi = int_const_binop (TRUNC_DIV_EXPR,
6142 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6143 tree etype = range_check_type (TREE_TYPE (@0));
6146 if (wi::neg_p (wi::to_wide (div)))
6148 lo = fold_convert (etype, lo);
6149 hi = fold_convert (etype, hi);
6150 hi = int_const_binop (MINUS_EXPR, hi, lo);
6154 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
6156 /* Simplification of math builtins. These rules must all be optimizations
6157 as well as IL simplifications. If there is a possibility that the new
6158 form could be a pessimization, the rule should go in the canonicalization
6159 section that follows this one.
6161 Rules can generally go in this section if they satisfy one of
6164 - the rule describes an identity
6166 - the rule replaces calls with something as simple as addition or
6169 - the rule contains unary calls only and simplifies the surrounding
6170 arithmetic. (The idea here is to exclude non-unary calls in which
6171 one operand is constant and in which the call is known to be cheap
6172 when the operand has that value.) */
6174 (if (flag_unsafe_math_optimizations)
6175 /* Simplify sqrt(x) * sqrt(x) -> x. */
6177 (mult (SQRT_ALL@1 @0) @1)
6178 (if (!tree_expr_maybe_signaling_nan_p (@0))
6181 (for op (plus minus)
6182 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
6186 (rdiv (op @0 @2) @1)))
6188 (for cmp (lt le gt ge)
6189 neg_cmp (gt ge lt le)
6190 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
6192 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6194 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6196 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6197 || (real_zerop (tem) && !real_zerop (@1))))
6199 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6201 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6202 (neg_cmp @0 { tem; })))))))
6204 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
6205 (for root (SQRT CBRT)
6207 (mult (root:s @0) (root:s @1))
6208 (root (mult @0 @1))))
6210 /* Simplify expN(x) * expN(y) -> expN(x+y). */
6211 (for exps (EXP EXP2 EXP10 POW10)
6213 (mult (exps:s @0) (exps:s @1))
6214 (exps (plus @0 @1))))
6216 /* Simplify a/root(b/c) into a*root(c/b). */
6217 (for root (SQRT CBRT)
6219 (rdiv @0 (root:s (rdiv:s @1 @2)))
6220 (mult @0 (root (rdiv @2 @1)))))
6222 /* Simplify x/expN(y) into x*expN(-y). */
6223 (for exps (EXP EXP2 EXP10 POW10)
6225 (rdiv @0 (exps:s @1))
6226 (mult @0 (exps (negate @1)))))
6228 (for logs (LOG LOG2 LOG10 LOG10)
6229 exps (EXP EXP2 EXP10 POW10)
6230 /* logN(expN(x)) -> x. */
6234 /* expN(logN(x)) -> x. */
6239 /* Optimize logN(func()) for various exponential functions. We
6240 want to determine the value "x" and the power "exponent" in
6241 order to transform logN(x**exponent) into exponent*logN(x). */
6242 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
6243 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
6246 (if (SCALAR_FLOAT_TYPE_P (type))
6252 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
6253 x = build_real_truncate (type, dconst_e ());
6256 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
6257 x = build_real (type, dconst2);
6261 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
6263 REAL_VALUE_TYPE dconst10;
6264 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6265 x = build_real (type, dconst10);
6272 (mult (logs { x; }) @0)))))
6280 (if (SCALAR_FLOAT_TYPE_P (type))
6286 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
6287 x = build_real (type, dconsthalf);
6290 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
6291 x = build_real_truncate (type, dconst_third ());
6297 (mult { x; } (logs @0))))))
6299 /* logN(pow(x,exponent)) -> exponent*logN(x). */
6300 (for logs (LOG LOG2 LOG10)
6304 (mult @1 (logs @0))))
6306 /* pow(C,x) -> exp(log(C)*x) if C > 0,
6307 or if C is a positive power of 2,
6308 pow(C,x) -> exp2(log2(C)*x). */
6316 (pows REAL_CST@0 @1)
6317 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6318 && real_isfinite (TREE_REAL_CST_PTR (@0))
6319 /* As libmvec doesn't have a vectorized exp2, defer optimizing
6320 the use_exp2 case until after vectorization. It seems actually
6321 beneficial for all constants to postpone this until later,
6322 because exp(log(C)*x), while faster, will have worse precision
6323 and if x folds into a constant too, that is unnecessary
6325 && canonicalize_math_after_vectorization_p ())
6327 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
6328 bool use_exp2 = false;
6329 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
6330 && value->cl == rvc_normal)
6332 REAL_VALUE_TYPE frac_rvt = *value;
6333 SET_REAL_EXP (&frac_rvt, 1);
6334 if (real_equal (&frac_rvt, &dconst1))
6339 (if (optimize_pow_to_exp (@0, @1))
6340 (exps (mult (logs @0) @1)))
6341 (exp2s (mult (log2s @0) @1)))))))
6344 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
6346 exps (EXP EXP2 EXP10 POW10)
6347 logs (LOG LOG2 LOG10 LOG10)
6349 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6350 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6351 && real_isfinite (TREE_REAL_CST_PTR (@0)))
6352 (exps (plus (mult (logs @0) @1) @2)))))
6357 exps (EXP EXP2 EXP10 POW10)
6358 /* sqrt(expN(x)) -> expN(x*0.5). */
6361 (exps (mult @0 { build_real (type, dconsthalf); })))
6362 /* cbrt(expN(x)) -> expN(x/3). */
6365 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6366 /* pow(expN(x), y) -> expN(x*y). */
6369 (exps (mult @0 @1))))
6371 /* tan(atan(x)) -> x. */
6378 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6382 copysigns (COPYSIGN)
6387 REAL_VALUE_TYPE r_cst;
6388 build_sinatan_real (&r_cst, type);
6389 tree t_cst = build_real (type, r_cst);
6390 tree t_one = build_one_cst (type);
6392 (if (SCALAR_FLOAT_TYPE_P (type))
6393 (cond (lt (abs @0) { t_cst; })
6394 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6395 (copysigns { t_one; } @0))))))
6397 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6401 copysigns (COPYSIGN)
6406 REAL_VALUE_TYPE r_cst;
6407 build_sinatan_real (&r_cst, type);
6408 tree t_cst = build_real (type, r_cst);
6409 tree t_one = build_one_cst (type);
6410 tree t_zero = build_zero_cst (type);
6412 (if (SCALAR_FLOAT_TYPE_P (type))
6413 (cond (lt (abs @0) { t_cst; })
6414 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6415 (copysigns { t_zero; } @0))))))
6417 (if (!flag_errno_math)
6418 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6423 (sinhs (atanhs:s @0))
6424 (with { tree t_one = build_one_cst (type); }
6425 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6427 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6432 (coshs (atanhs:s @0))
6433 (with { tree t_one = build_one_cst (type); }
6434 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6436 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
6438 (CABS (complex:C @0 real_zerop@1))
6441 /* trunc(trunc(x)) -> trunc(x), etc. */
6442 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6446 /* f(x) -> x if x is integer valued and f does nothing for such values. */
6447 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6449 (fns integer_valued_real_p@0)
6452 /* hypot(x,0) and hypot(0,x) -> abs(x). */
6454 (HYPOT:c @0 real_zerop@1)
6457 /* pow(1,x) -> 1. */
6459 (POW real_onep@0 @1)
6463 /* copysign(x,x) -> x. */
6464 (COPYSIGN_ALL @0 @0)
6468 /* copysign(x,-x) -> -x. */
6469 (COPYSIGN_ALL @0 (negate@1 @0))
6473 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
6474 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6477 (for scale (LDEXP SCALBN SCALBLN)
6478 /* ldexp(0, x) -> 0. */
6480 (scale real_zerop@0 @1)
6482 /* ldexp(x, 0) -> x. */
6484 (scale @0 integer_zerop@1)
6486 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
6488 (scale REAL_CST@0 @1)
6489 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6492 /* Canonicalization of sequences of math builtins. These rules represent
6493 IL simplifications but are not necessarily optimizations.
6495 The sincos pass is responsible for picking "optimal" implementations
6496 of math builtins, which may be more complicated and can sometimes go
6497 the other way, e.g. converting pow into a sequence of sqrts.
6498 We only want to do these canonicalizations before the pass has run. */
6500 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6501 /* Simplify tan(x) * cos(x) -> sin(x). */
6503 (mult:c (TAN:s @0) (COS:s @0))
6506 /* Simplify x * pow(x,c) -> pow(x,c+1). */
6508 (mult:c @0 (POW:s @0 REAL_CST@1))
6509 (if (!TREE_OVERFLOW (@1))
6510 (POW @0 (plus @1 { build_one_cst (type); }))))
6512 /* Simplify sin(x) / cos(x) -> tan(x). */
6514 (rdiv (SIN:s @0) (COS:s @0))
6517 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6519 (rdiv (SINH:s @0) (COSH:s @0))
6522 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6524 (rdiv (TANH:s @0) (SINH:s @0))
6525 (rdiv {build_one_cst (type);} (COSH @0)))
6527 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6529 (rdiv (COS:s @0) (SIN:s @0))
6530 (rdiv { build_one_cst (type); } (TAN @0)))
6532 /* Simplify sin(x) / tan(x) -> cos(x). */
6534 (rdiv (SIN:s @0) (TAN:s @0))
6535 (if (! HONOR_NANS (@0)
6536 && ! HONOR_INFINITIES (@0))
6539 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6541 (rdiv (TAN:s @0) (SIN:s @0))
6542 (if (! HONOR_NANS (@0)
6543 && ! HONOR_INFINITIES (@0))
6544 (rdiv { build_one_cst (type); } (COS @0))))
6546 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6548 (mult (POW:s @0 @1) (POW:s @0 @2))
6549 (POW @0 (plus @1 @2)))
6551 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6553 (mult (POW:s @0 @1) (POW:s @2 @1))
6554 (POW (mult @0 @2) @1))
6556 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6558 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6559 (POWI (mult @0 @2) @1))
6561 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6563 (rdiv (POW:s @0 REAL_CST@1) @0)
6564 (if (!TREE_OVERFLOW (@1))
6565 (POW @0 (minus @1 { build_one_cst (type); }))))
6567 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6569 (rdiv @0 (POW:s @1 @2))
6570 (mult @0 (POW @1 (negate @2))))
6575 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6578 (pows @0 { build_real (type, dconst_quarter ()); }))
6579 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6582 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6583 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6586 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6587 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6589 (cbrts (cbrts tree_expr_nonnegative_p@0))
6590 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6591 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6593 (sqrts (pows @0 @1))
6594 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6595 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6597 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6598 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6599 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6601 (pows (sqrts @0) @1)
6602 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6603 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6605 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6606 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6607 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6609 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6610 (pows @0 (mult @1 @2))))
6612 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6614 (CABS (complex @0 @0))
6615 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6617 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6620 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6622 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6627 (cexps compositional_complex@0)
6628 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6630 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6631 (mult @1 (imagpart @2)))))))
6633 (if (canonicalize_math_p ())
6634 /* floor(x) -> trunc(x) if x is nonnegative. */
6635 (for floors (FLOOR_ALL)
6638 (floors tree_expr_nonnegative_p@0)
6641 (match double_value_p
6643 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6644 (for froms (BUILT_IN_TRUNCL
6656 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6657 (if (optimize && canonicalize_math_p ())
6659 (froms (convert double_value_p@0))
6660 (convert (tos @0)))))
6662 (match float_value_p
6664 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6665 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6666 BUILT_IN_FLOORL BUILT_IN_FLOOR
6667 BUILT_IN_CEILL BUILT_IN_CEIL
6668 BUILT_IN_ROUNDL BUILT_IN_ROUND
6669 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6670 BUILT_IN_RINTL BUILT_IN_RINT)
6671 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6672 BUILT_IN_FLOORF BUILT_IN_FLOORF
6673 BUILT_IN_CEILF BUILT_IN_CEILF
6674 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6675 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6676 BUILT_IN_RINTF BUILT_IN_RINTF)
6677 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6679 (if (optimize && canonicalize_math_p ()
6680 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6682 (froms (convert float_value_p@0))
6683 (convert (tos @0)))))
6686 (match float16_value_p
6688 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
6689 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
6690 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
6691 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
6692 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
6693 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
6694 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
6695 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
6696 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
6697 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
6698 IFN_FLOOR IFN_FLOOR IFN_FLOOR
6699 IFN_CEIL IFN_CEIL IFN_CEIL
6700 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
6701 IFN_ROUND IFN_ROUND IFN_ROUND
6702 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
6703 IFN_RINT IFN_RINT IFN_RINT
6704 IFN_SQRT IFN_SQRT IFN_SQRT)
6705 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
6706 if x is a _Float16. */
6708 (convert (froms (convert float16_value_p@0)))
6710 && types_match (type, TREE_TYPE (@0))
6711 && direct_internal_fn_supported_p (as_internal_fn (tos),
6712 type, OPTIMIZE_FOR_BOTH))
6715 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
6716 x,y is float value, similar for _Float16/double. */
6717 (for copysigns (COPYSIGN_ALL)
6719 (convert (copysigns (convert@2 @0) (convert @1)))
6721 && !HONOR_SNANS (@2)
6722 && types_match (type, TREE_TYPE (@0))
6723 && types_match (type, TREE_TYPE (@1))
6724 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
6725 && direct_internal_fn_supported_p (IFN_COPYSIGN,
6726 type, OPTIMIZE_FOR_BOTH))
6727 (IFN_COPYSIGN @0 @1))))
6729 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
6730 tos (IFN_FMA IFN_FMA IFN_FMA)
6732 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
6733 (if (flag_unsafe_math_optimizations
6735 && FLOAT_TYPE_P (type)
6736 && FLOAT_TYPE_P (TREE_TYPE (@3))
6737 && types_match (type, TREE_TYPE (@0))
6738 && types_match (type, TREE_TYPE (@1))
6739 && types_match (type, TREE_TYPE (@2))
6740 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
6741 && direct_internal_fn_supported_p (as_internal_fn (tos),
6742 type, OPTIMIZE_FOR_BOTH))
6745 (for maxmin (max min)
6747 (convert (maxmin (convert@2 @0) (convert @1)))
6749 && FLOAT_TYPE_P (type)
6750 && FLOAT_TYPE_P (TREE_TYPE (@2))
6751 && types_match (type, TREE_TYPE (@0))
6752 && types_match (type, TREE_TYPE (@1))
6753 && element_precision (type) < element_precision (TREE_TYPE (@2)))
6757 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6758 tos (XFLOOR XCEIL XROUND XRINT)
6759 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6760 (if (optimize && canonicalize_math_p ())
6762 (froms (convert double_value_p@0))
6765 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6766 XFLOOR XCEIL XROUND XRINT)
6767 tos (XFLOORF XCEILF XROUNDF XRINTF)
6768 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6770 (if (optimize && canonicalize_math_p ())
6772 (froms (convert float_value_p@0))
6775 (if (canonicalize_math_p ())
6776 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6777 (for floors (IFLOOR LFLOOR LLFLOOR)
6779 (floors tree_expr_nonnegative_p@0)
6782 (if (canonicalize_math_p ())
6783 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6784 (for fns (IFLOOR LFLOOR LLFLOOR
6786 IROUND LROUND LLROUND)
6788 (fns integer_valued_real_p@0)
6790 (if (!flag_errno_math)
6791 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6792 (for rints (IRINT LRINT LLRINT)
6794 (rints integer_valued_real_p@0)
6797 (if (canonicalize_math_p ())
6798 (for ifn (IFLOOR ICEIL IROUND IRINT)
6799 lfn (LFLOOR LCEIL LROUND LRINT)
6800 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6801 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6802 sizeof (int) == sizeof (long). */
6803 (if (TYPE_PRECISION (integer_type_node)
6804 == TYPE_PRECISION (long_integer_type_node))
6807 (lfn:long_integer_type_node @0)))
6808 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6809 sizeof (long long) == sizeof (long). */
6810 (if (TYPE_PRECISION (long_long_integer_type_node)
6811 == TYPE_PRECISION (long_integer_type_node))
6814 (lfn:long_integer_type_node @0)))))
6816 /* cproj(x) -> x if we're ignoring infinities. */
6819 (if (!HONOR_INFINITIES (type))
6822 /* If the real part is inf and the imag part is known to be
6823 nonnegative, return (inf + 0i). */
6825 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6826 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6827 { build_complex_inf (type, false); }))
6829 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6831 (CPROJ (complex @0 REAL_CST@1))
6832 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6833 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6839 (pows @0 REAL_CST@1)
6841 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6842 REAL_VALUE_TYPE tmp;
6845 /* pow(x,0) -> 1. */
6846 (if (real_equal (value, &dconst0))
6847 { build_real (type, dconst1); })
6848 /* pow(x,1) -> x. */
6849 (if (real_equal (value, &dconst1))
6851 /* pow(x,-1) -> 1/x. */
6852 (if (real_equal (value, &dconstm1))
6853 (rdiv { build_real (type, dconst1); } @0))
6854 /* pow(x,0.5) -> sqrt(x). */
6855 (if (flag_unsafe_math_optimizations
6856 && canonicalize_math_p ()
6857 && real_equal (value, &dconsthalf))
6859 /* pow(x,1/3) -> cbrt(x). */
6860 (if (flag_unsafe_math_optimizations
6861 && canonicalize_math_p ()
6862 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6863 real_equal (value, &tmp)))
6866 /* powi(1,x) -> 1. */
6868 (POWI real_onep@0 @1)
6872 (POWI @0 INTEGER_CST@1)
6874 /* powi(x,0) -> 1. */
6875 (if (wi::to_wide (@1) == 0)
6876 { build_real (type, dconst1); })
6877 /* powi(x,1) -> x. */
6878 (if (wi::to_wide (@1) == 1)
6880 /* powi(x,-1) -> 1/x. */
6881 (if (wi::to_wide (@1) == -1)
6882 (rdiv { build_real (type, dconst1); } @0))))
6884 /* Narrowing of arithmetic and logical operations.
6886 These are conceptually similar to the transformations performed for
6887 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6888 term we want to move all that code out of the front-ends into here. */
6890 /* Convert (outertype)((innertype0)a+(innertype1)b)
6891 into ((newtype)a+(newtype)b) where newtype
6892 is the widest mode from all of these. */
6893 (for op (plus minus mult rdiv)
6895 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6896 /* If we have a narrowing conversion of an arithmetic operation where
6897 both operands are widening conversions from the same type as the outer
6898 narrowing conversion. Then convert the innermost operands to a
6899 suitable unsigned type (to avoid introducing undefined behavior),
6900 perform the operation and convert the result to the desired type. */
6901 (if (INTEGRAL_TYPE_P (type)
6904 /* We check for type compatibility between @0 and @1 below,
6905 so there's no need to check that @2/@4 are integral types. */
6906 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6907 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6908 /* The precision of the type of each operand must match the
6909 precision of the mode of each operand, similarly for the
6911 && type_has_mode_precision_p (TREE_TYPE (@1))
6912 && type_has_mode_precision_p (TREE_TYPE (@2))
6913 && type_has_mode_precision_p (type)
6914 /* The inner conversion must be a widening conversion. */
6915 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
6916 && types_match (@1, type)
6917 && (types_match (@1, @2)
6918 /* Or the second operand is const integer or converted const
6919 integer from valueize. */
6920 || poly_int_tree_p (@4)))
6921 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
6922 (op @1 (convert @2))
6923 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6924 (convert (op (convert:utype @1)
6925 (convert:utype @2)))))
6926 (if (FLOAT_TYPE_P (type)
6927 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6928 == DECIMAL_FLOAT_TYPE_P (type))
6929 (with { tree arg0 = strip_float_extensions (@1);
6930 tree arg1 = strip_float_extensions (@2);
6931 tree itype = TREE_TYPE (@0);
6932 tree ty1 = TREE_TYPE (arg0);
6933 tree ty2 = TREE_TYPE (arg1);
6934 enum tree_code code = TREE_CODE (itype); }
6935 (if (FLOAT_TYPE_P (ty1)
6936 && FLOAT_TYPE_P (ty2))
6937 (with { tree newtype = type;
6938 if (TYPE_MODE (ty1) == SDmode
6939 || TYPE_MODE (ty2) == SDmode
6940 || TYPE_MODE (type) == SDmode)
6941 newtype = dfloat32_type_node;
6942 if (TYPE_MODE (ty1) == DDmode
6943 || TYPE_MODE (ty2) == DDmode
6944 || TYPE_MODE (type) == DDmode)
6945 newtype = dfloat64_type_node;
6946 if (TYPE_MODE (ty1) == TDmode
6947 || TYPE_MODE (ty2) == TDmode
6948 || TYPE_MODE (type) == TDmode)
6949 newtype = dfloat128_type_node; }
6950 (if ((newtype == dfloat32_type_node
6951 || newtype == dfloat64_type_node
6952 || newtype == dfloat128_type_node)
6954 && types_match (newtype, type))
6955 (op (convert:newtype @1) (convert:newtype @2))
6956 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
6958 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
6960 /* Sometimes this transformation is safe (cannot
6961 change results through affecting double rounding
6962 cases) and sometimes it is not. If NEWTYPE is
6963 wider than TYPE, e.g. (float)((long double)double
6964 + (long double)double) converted to
6965 (float)(double + double), the transformation is
6966 unsafe regardless of the details of the types
6967 involved; double rounding can arise if the result
6968 of NEWTYPE arithmetic is a NEWTYPE value half way
6969 between two representable TYPE values but the
6970 exact value is sufficiently different (in the
6971 right direction) for this difference to be
6972 visible in ITYPE arithmetic. If NEWTYPE is the
6973 same as TYPE, however, the transformation may be
6974 safe depending on the types involved: it is safe
6975 if the ITYPE has strictly more than twice as many
6976 mantissa bits as TYPE, can represent infinities
6977 and NaNs if the TYPE can, and has sufficient
6978 exponent range for the product or ratio of two
6979 values representable in the TYPE to be within the
6980 range of normal values of ITYPE. */
6981 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6982 && (flag_unsafe_math_optimizations
6983 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6984 && real_can_shorten_arithmetic (TYPE_MODE (itype),
6986 && !excess_precision_type (newtype)))
6987 && !types_match (itype, newtype))
6988 (convert:type (op (convert:newtype @1)
6989 (convert:newtype @2)))
6994 /* This is another case of narrowing, specifically when there's an outer
6995 BIT_AND_EXPR which masks off bits outside the type of the innermost
6996 operands. Like the previous case we have to convert the operands
6997 to unsigned types to avoid introducing undefined behavior for the
6998 arithmetic operation. */
6999 (for op (minus plus)
7001 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7002 (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 @1/@3 are integral types. */
7005 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7006 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
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 (@0))
7011 && type_has_mode_precision_p (TREE_TYPE (@1))
7012 && type_has_mode_precision_p (type)
7013 /* The inner conversion must be a widening conversion. */
7014 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7015 && types_match (@0, @1)
7016 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7017 <= TYPE_PRECISION (TREE_TYPE (@0)))
7018 && (wi::to_wide (@4)
7019 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7020 true, TYPE_PRECISION (type))) == 0)
7021 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7022 (with { tree ntype = TREE_TYPE (@0); }
7023 (convert (bit_and (op @0 @1) (convert:ntype @4))))
7024 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7025 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7026 (convert:utype @4))))))))
7028 /* Transform (@0 < @1 and @0 < @2) to use min,
7029 (@0 > @1 and @0 > @2) to use max */
7030 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7031 op (lt le gt ge lt le gt ge )
7032 ext (min min max max max max min min )
7034 (logic (op:cs @0 @1) (op:cs @0 @2))
7035 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7036 && TREE_CODE (@0) != INTEGER_CST)
7037 (op @0 (ext @1 @2)))))
7040 /* signbit(x) -> 0 if x is nonnegative. */
7041 (SIGNBIT tree_expr_nonnegative_p@0)
7042 { integer_zero_node; })
7045 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
7047 (if (!HONOR_SIGNED_ZEROS (@0))
7048 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7050 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
7052 (for op (plus minus)
7055 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7056 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7057 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7058 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7059 && !TYPE_SATURATING (TREE_TYPE (@0)))
7060 (with { tree res = int_const_binop (rop, @2, @1); }
7061 (if (TREE_OVERFLOW (res)
7062 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7063 { constant_boolean_node (cmp == NE_EXPR, type); }
7064 (if (single_use (@3))
7065 (cmp @0 { TREE_OVERFLOW (res)
7066 ? drop_tree_overflow (res) : res; }))))))))
7067 (for cmp (lt le gt ge)
7068 (for op (plus minus)
7071 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7072 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7073 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7074 (with { tree res = int_const_binop (rop, @2, @1); }
7075 (if (TREE_OVERFLOW (res))
7077 fold_overflow_warning (("assuming signed overflow does not occur "
7078 "when simplifying conditional to constant"),
7079 WARN_STRICT_OVERFLOW_CONDITIONAL);
7080 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7081 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
7082 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7083 TYPE_SIGN (TREE_TYPE (@1)))
7084 != (op == MINUS_EXPR);
7085 constant_boolean_node (less == ovf_high, type);
7087 (if (single_use (@3))
7090 fold_overflow_warning (("assuming signed overflow does not occur "
7091 "when changing X +- C1 cmp C2 to "
7093 WARN_STRICT_OVERFLOW_COMPARISON);
7095 (cmp @0 { res; })))))))))
7097 /* Canonicalizations of BIT_FIELD_REFs. */
7100 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7101 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7104 (BIT_FIELD_REF (view_convert @0) @1 @2)
7105 (BIT_FIELD_REF @0 @1 @2))
7108 (BIT_FIELD_REF @0 @1 integer_zerop)
7109 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7113 (BIT_FIELD_REF @0 @1 @2)
7115 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7116 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7118 (if (integer_zerop (@2))
7119 (view_convert (realpart @0)))
7120 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7121 (view_convert (imagpart @0)))))
7122 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7123 && INTEGRAL_TYPE_P (type)
7124 /* On GIMPLE this should only apply to register arguments. */
7125 && (! GIMPLE || is_gimple_reg (@0))
7126 /* A bit-field-ref that referenced the full argument can be stripped. */
7127 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
7128 && integer_zerop (@2))
7129 /* Low-parts can be reduced to integral conversions.
7130 ??? The following doesn't work for PDP endian. */
7131 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7132 /* But only do this after vectorization. */
7133 && canonicalize_math_after_vectorization_p ()
7134 /* Don't even think about BITS_BIG_ENDIAN. */
7135 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
7136 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
7137 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
7138 ? (TYPE_PRECISION (TREE_TYPE (@0))
7139 - TYPE_PRECISION (type))
7143 /* Simplify vector extracts. */
7146 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
7147 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
7148 && tree_fits_uhwi_p (TYPE_SIZE (type))
7149 && ((tree_to_uhwi (TYPE_SIZE (type))
7150 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7151 || (VECTOR_TYPE_P (type)
7152 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
7153 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
7156 tree ctor = (TREE_CODE (@0) == SSA_NAME
7157 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7158 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
7159 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
7160 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
7161 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
7164 && (idx % width) == 0
7166 && known_le ((idx + n) / width,
7167 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
7172 /* Constructor elements can be subvectors. */
7174 if (CONSTRUCTOR_NELTS (ctor) != 0)
7176 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7177 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7178 k = TYPE_VECTOR_SUBPARTS (cons_elem);
7180 unsigned HOST_WIDE_INT elt, count, const_k;
7183 /* We keep an exact subset of the constructor elements. */
7184 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7185 (if (CONSTRUCTOR_NELTS (ctor) == 0)
7186 { build_zero_cst (type); }
7188 (if (elt < CONSTRUCTOR_NELTS (ctor))
7189 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7190 { build_zero_cst (type); })
7191 /* We don't want to emit new CTORs unless the old one goes away.
7192 ??? Eventually allow this if the CTOR ends up constant or
7194 (if (single_use (@0))
7197 vec<constructor_elt, va_gc> *vals;
7198 vec_alloc (vals, count);
7199 bool constant_p = true;
7201 for (unsigned i = 0;
7202 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7204 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7205 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7206 if (!CONSTANT_CLASS_P (e))
7209 tree evtype = (types_match (TREE_TYPE (type),
7210 TREE_TYPE (TREE_TYPE (ctor)))
7212 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7214 res = (constant_p ? build_vector_from_ctor (evtype, vals)
7215 : build_constructor (evtype, vals));
7217 (view_convert { res; }))))))
7218 /* The bitfield references a single constructor element. */
7219 (if (k.is_constant (&const_k)
7220 && idx + n <= (idx / const_k + 1) * const_k)
7222 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7223 { build_zero_cst (type); })
7225 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7226 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7227 @1 { bitsize_int ((idx % const_k) * width); })))))))))
7229 /* Simplify a bit extraction from a bit insertion for the cases with
7230 the inserted element fully covering the extraction or the insertion
7231 not touching the extraction. */
7233 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7236 unsigned HOST_WIDE_INT isize;
7237 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7238 isize = TYPE_PRECISION (TREE_TYPE (@1));
7240 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7243 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7244 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
7245 wi::to_wide (@ipos) + isize))
7246 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
7248 - wi::to_wide (@ipos)); }))
7249 (if (wi::geu_p (wi::to_wide (@ipos),
7250 wi::to_wide (@rpos) + wi::to_wide (@rsize))
7251 || wi::geu_p (wi::to_wide (@rpos),
7252 wi::to_wide (@ipos) + isize))
7253 (BIT_FIELD_REF @0 @rsize @rpos)))))
7255 (if (canonicalize_math_after_vectorization_p ())
7258 (fmas:c (negate @0) @1 @2)
7259 (IFN_FNMA @0 @1 @2))
7261 (fmas @0 @1 (negate @2))
7264 (fmas:c (negate @0) @1 (negate @2))
7265 (IFN_FNMS @0 @1 @2))
7267 (negate (fmas@3 @0 @1 @2))
7268 (if (single_use (@3))
7269 (IFN_FNMS @0 @1 @2))))
7272 (IFN_FMS:c (negate @0) @1 @2)
7273 (IFN_FNMS @0 @1 @2))
7275 (IFN_FMS @0 @1 (negate @2))
7278 (IFN_FMS:c (negate @0) @1 (negate @2))
7279 (IFN_FNMA @0 @1 @2))
7281 (negate (IFN_FMS@3 @0 @1 @2))
7282 (if (single_use (@3))
7283 (IFN_FNMA @0 @1 @2)))
7286 (IFN_FNMA:c (negate @0) @1 @2)
7289 (IFN_FNMA @0 @1 (negate @2))
7290 (IFN_FNMS @0 @1 @2))
7292 (IFN_FNMA:c (negate @0) @1 (negate @2))
7295 (negate (IFN_FNMA@3 @0 @1 @2))
7296 (if (single_use (@3))
7297 (IFN_FMS @0 @1 @2)))
7300 (IFN_FNMS:c (negate @0) @1 @2)
7303 (IFN_FNMS @0 @1 (negate @2))
7304 (IFN_FNMA @0 @1 @2))
7306 (IFN_FNMS:c (negate @0) @1 (negate @2))
7309 (negate (IFN_FNMS@3 @0 @1 @2))
7310 (if (single_use (@3))
7311 (IFN_FMA @0 @1 @2))))
7313 /* CLZ simplifications. */
7318 (op (clz:s@2 @0) INTEGER_CST@1)
7319 (if (integer_zerop (@1) && single_use (@2))
7320 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
7321 (with { tree type0 = TREE_TYPE (@0);
7322 tree stype = signed_type_for (type0);
7323 HOST_WIDE_INT val = 0;
7324 /* Punt on hypothetical weird targets. */
7326 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7332 (cmp (convert:stype @0) { build_zero_cst (stype); })))
7333 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
7334 (with { bool ok = true;
7335 HOST_WIDE_INT val = 0;
7336 tree type0 = TREE_TYPE (@0);
7337 /* Punt on hypothetical weird targets. */
7339 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7341 && val == TYPE_PRECISION (type0) - 1)
7344 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7345 (op @0 { build_one_cst (type0); })))))))
7347 /* CTZ simplifications. */
7349 (for op (ge gt le lt)
7352 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
7353 (op (ctz:s @0) INTEGER_CST@1)
7354 (with { bool ok = true;
7355 HOST_WIDE_INT val = 0;
7356 if (!tree_fits_shwi_p (@1))
7360 val = tree_to_shwi (@1);
7361 /* Canonicalize to >= or <. */
7362 if (op == GT_EXPR || op == LE_EXPR)
7364 if (val == HOST_WIDE_INT_MAX)
7370 bool zero_res = false;
7371 HOST_WIDE_INT zero_val = 0;
7372 tree type0 = TREE_TYPE (@0);
7373 int prec = TYPE_PRECISION (type0);
7375 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7380 (if (ok && (!zero_res || zero_val >= val))
7381 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7383 (if (ok && (!zero_res || zero_val < val))
7384 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7385 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7386 (cmp (bit_and @0 { wide_int_to_tree (type0,
7387 wi::mask (val, false, prec)); })
7388 { build_zero_cst (type0); })))))))
7391 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
7392 (op (ctz:s @0) INTEGER_CST@1)
7393 (with { bool zero_res = false;
7394 HOST_WIDE_INT zero_val = 0;
7395 tree type0 = TREE_TYPE (@0);
7396 int prec = TYPE_PRECISION (type0);
7398 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7402 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7403 (if (!zero_res || zero_val != wi::to_widest (@1))
7404 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7405 (if (!zero_res || zero_val < 0 || zero_val >= prec)
7406 (op (bit_and @0 { wide_int_to_tree (type0,
7407 wi::mask (tree_to_uhwi (@1) + 1,
7409 { wide_int_to_tree (type0,
7410 wi::shifted_mask (tree_to_uhwi (@1), 1,
7411 false, prec)); })))))))
7413 /* POPCOUNT simplifications. */
7414 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
7416 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7417 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7418 (POPCOUNT (bit_ior @0 @1))))
7420 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
7421 (for popcount (POPCOUNT)
7422 (for cmp (le eq ne gt)
7425 (cmp (popcount @0) integer_zerop)
7426 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7428 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
7430 (bit_and (POPCOUNT @0) integer_onep)
7433 /* PARITY simplifications. */
7434 /* parity(~X) is parity(X). */
7436 (PARITY (bit_not @0))
7439 /* parity(X)^parity(Y) is parity(X^Y). */
7441 (bit_xor (PARITY:s @0) (PARITY:s @1))
7442 (PARITY (bit_xor @0 @1)))
7444 /* Common POPCOUNT/PARITY simplifications. */
7445 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
7446 (for pfun (POPCOUNT PARITY)
7449 (with { wide_int nz = tree_nonzero_bits (@0); }
7453 (if (wi::popcount (nz) == 1)
7454 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7455 (convert (rshift:utype (convert:utype @0)
7456 { build_int_cst (integer_type_node,
7457 wi::ctz (nz)); }))))))))
7460 /* 64- and 32-bits branchless implementations of popcount are detected:
7462 int popcount64c (uint64_t x)
7464 x -= (x >> 1) & 0x5555555555555555ULL;
7465 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7466 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
7467 return (x * 0x0101010101010101ULL) >> 56;
7470 int popcount32c (uint32_t x)
7472 x -= (x >> 1) & 0x55555555;
7473 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
7474 x = (x + (x >> 4)) & 0x0f0f0f0f;
7475 return (x * 0x01010101) >> 24;
7482 (rshift @8 INTEGER_CST@5)
7484 (bit_and @6 INTEGER_CST@7)
7488 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
7494 /* Check constants and optab. */
7495 (with { unsigned prec = TYPE_PRECISION (type);
7496 int shift = (64 - prec) & 63;
7497 unsigned HOST_WIDE_INT c1
7498 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
7499 unsigned HOST_WIDE_INT c2
7500 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
7501 unsigned HOST_WIDE_INT c3
7502 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
7503 unsigned HOST_WIDE_INT c4
7504 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
7509 && TYPE_UNSIGNED (type)
7510 && integer_onep (@4)
7511 && wi::to_widest (@10) == 2
7512 && wi::to_widest (@5) == 4
7513 && wi::to_widest (@1) == prec - 8
7514 && tree_to_uhwi (@2) == c1
7515 && tree_to_uhwi (@3) == c2
7516 && tree_to_uhwi (@9) == c3
7517 && tree_to_uhwi (@7) == c3
7518 && tree_to_uhwi (@11) == c4)
7519 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
7521 (convert (IFN_POPCOUNT:type @0))
7522 /* Try to do popcount in two halves. PREC must be at least
7523 five bits for this to work without extension before adding. */
7525 tree half_type = NULL_TREE;
7526 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
7529 && m.require () != TYPE_MODE (type))
7531 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
7532 half_type = build_nonstandard_integer_type (half_prec, 1);
7534 gcc_assert (half_prec > 2);
7536 (if (half_type != NULL_TREE
7537 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
7540 (IFN_POPCOUNT:half_type (convert @0))
7541 (IFN_POPCOUNT:half_type (convert (rshift @0
7542 { build_int_cst (integer_type_node, half_prec); } )))))))))))
7544 /* __builtin_ffs needs to deal on many targets with the possible zero
7545 argument. If we know the argument is always non-zero, __builtin_ctz + 1
7546 should lead to better code. */
7548 (FFS tree_expr_nonzero_p@0)
7549 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7550 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
7551 OPTIMIZE_FOR_SPEED))
7552 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7553 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
7556 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
7558 /* __builtin_ffs (X) == 0 -> X == 0.
7559 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
7562 (cmp (ffs@2 @0) INTEGER_CST@1)
7563 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7565 (if (integer_zerop (@1))
7566 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7567 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7568 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7569 (if (single_use (@2))
7570 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7571 wi::mask (tree_to_uhwi (@1),
7573 { wide_int_to_tree (TREE_TYPE (@0),
7574 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7575 false, prec)); }))))))
7577 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
7581 bit_op (bit_and bit_ior)
7583 (cmp (ffs@2 @0) INTEGER_CST@1)
7584 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7586 (if (integer_zerop (@1))
7587 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7588 (if (tree_int_cst_sgn (@1) < 0)
7589 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7590 (if (wi::to_widest (@1) >= prec)
7591 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7592 (if (wi::to_widest (@1) == prec - 1)
7593 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7594 wi::shifted_mask (prec - 1, 1,
7596 (if (single_use (@2))
7597 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7599 { wide_int_to_tree (TREE_TYPE (@0),
7600 wi::mask (tree_to_uhwi (@1),
7602 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7609 --> r = .COND_FN (cond, a, b)
7613 --> r = .COND_FN (~cond, b, a). */
7615 (for uncond_op (UNCOND_UNARY)
7616 cond_op (COND_UNARY)
7618 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
7619 (with { tree op_type = TREE_TYPE (@3); }
7620 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7621 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7622 (cond_op @0 @1 @2))))
7624 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
7625 (with { tree op_type = TREE_TYPE (@3); }
7626 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7627 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7628 (cond_op (bit_not @0) @2 @1)))))
7637 r = c ? a1 op a2 : b;
7639 if the target can do it in one go. This makes the operation conditional
7640 on c, so could drop potentially-trapping arithmetic, but that's a valid
7641 simplification if the result of the operation isn't needed.
7643 Avoid speculatively generating a stand-alone vector comparison
7644 on targets that might not support them. Any target implementing
7645 conditional internal functions must support the same comparisons
7646 inside and outside a VEC_COND_EXPR. */
7648 (for uncond_op (UNCOND_BINARY)
7649 cond_op (COND_BINARY)
7651 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7652 (with { tree op_type = TREE_TYPE (@4); }
7653 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7654 && is_truth_type_for (op_type, TREE_TYPE (@0))
7656 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7658 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7659 (with { tree op_type = TREE_TYPE (@4); }
7660 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7661 && is_truth_type_for (op_type, TREE_TYPE (@0))
7663 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7665 /* Same for ternary operations. */
7666 (for uncond_op (UNCOND_TERNARY)
7667 cond_op (COND_TERNARY)
7669 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7670 (with { tree op_type = TREE_TYPE (@5); }
7671 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7672 && is_truth_type_for (op_type, TREE_TYPE (@0))
7674 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7676 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7677 (with { tree op_type = TREE_TYPE (@5); }
7678 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7679 && is_truth_type_for (op_type, TREE_TYPE (@0))
7681 (view_convert (cond_op (bit_not @0) @2 @3 @4
7682 (view_convert:op_type @1)))))))
7685 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7686 "else" value of an IFN_COND_*. */
7687 (for cond_op (COND_BINARY)
7689 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7690 (with { tree op_type = TREE_TYPE (@3); }
7691 (if (element_precision (type) == element_precision (op_type))
7692 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7694 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7695 (with { tree op_type = TREE_TYPE (@5); }
7696 (if (inverse_conditions_p (@0, @2)
7697 && element_precision (type) == element_precision (op_type))
7698 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7700 /* Same for ternary operations. */
7701 (for cond_op (COND_TERNARY)
7703 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7704 (with { tree op_type = TREE_TYPE (@4); }
7705 (if (element_precision (type) == element_precision (op_type))
7706 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7708 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7709 (with { tree op_type = TREE_TYPE (@6); }
7710 (if (inverse_conditions_p (@0, @2)
7711 && element_precision (type) == element_precision (op_type))
7712 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7714 /* Detect simplication for a conditional reduction where
7717 c = mask2 ? d + a : d
7721 c = mask1 && mask2 ? d + b : d. */
7723 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
7724 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
7726 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7729 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7730 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7732 If pointers are known not to wrap, B checks whether @1 bytes starting
7733 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7734 bytes. A is more efficiently tested as:
7736 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7738 The equivalent expression for B is given by replacing @1 with @1 - 1:
7740 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7742 @0 and @2 can be swapped in both expressions without changing the result.
7744 The folds rely on sizetype's being unsigned (which is always true)
7745 and on its being the same width as the pointer (which we have to check).
7747 The fold replaces two pointer_plus expressions, two comparisons and
7748 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7749 the best case it's a saving of two operations. The A fold retains one
7750 of the original pointer_pluses, so is a win even if both pointer_pluses
7751 are used elsewhere. The B fold is a wash if both pointer_pluses are
7752 used elsewhere, since all we end up doing is replacing a comparison with
7753 a pointer_plus. We do still apply the fold under those circumstances
7754 though, in case applying it to other conditions eventually makes one of the
7755 pointer_pluses dead. */
7756 (for ior (truth_orif truth_or bit_ior)
7759 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7760 (cmp:cs (pointer_plus@4 @2 @1) @0))
7761 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7762 && TYPE_OVERFLOW_WRAPS (sizetype)
7763 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7764 /* Calculate the rhs constant. */
7765 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7766 offset_int rhs = off * 2; }
7767 /* Always fails for negative values. */
7768 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7769 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7770 pick a canonical order. This increases the chances of using the
7771 same pointer_plus in multiple checks. */
7772 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7773 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7774 (if (cmp == LT_EXPR)
7775 (gt (convert:sizetype
7776 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7777 { swap_p ? @0 : @2; }))
7779 (gt (convert:sizetype
7780 (pointer_diff:ssizetype
7781 (pointer_plus { swap_p ? @2 : @0; }
7782 { wide_int_to_tree (sizetype, off); })
7783 { swap_p ? @0 : @2; }))
7784 { rhs_tree; })))))))))
7786 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7788 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7789 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7790 (with { int i = single_nonzero_element (@1); }
7792 (with { tree elt = vector_cst_elt (@1, i);
7793 tree elt_type = TREE_TYPE (elt);
7794 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7795 tree size = bitsize_int (elt_bits);
7796 tree pos = bitsize_int (elt_bits * i); }
7799 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7802 /* Fold reduction of a single nonzero element constructor. */
7803 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7804 (simplify (reduc (CONSTRUCTOR@0))
7805 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
7806 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7807 tree elt = ctor_single_nonzero_element (ctor); }
7809 && !HONOR_SNANS (type)
7810 && !HONOR_SIGNED_ZEROS (type))
7813 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
7814 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
7815 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
7816 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
7817 (simplify (reduc (op @0 VECTOR_CST@1))
7818 (op (reduc:type @0) (reduc:type @1))))
7821 (vec_perm @0 @1 VECTOR_CST@2)
7824 tree op0 = @0, op1 = @1, op2 = @2;
7825 machine_mode result_mode = TYPE_MODE (type);
7826 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
7828 /* Build a vector of integers from the tree mask. */
7829 vec_perm_builder builder;
7830 if (!tree_to_vec_perm_builder (&builder, op2))
7833 /* Create a vec_perm_indices for the integer vector. */
7834 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7835 bool single_arg = (op0 == op1);
7836 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7838 (if (sel.series_p (0, 1, 0, 1))
7840 (if (sel.series_p (0, 1, nelts, 1))
7846 if (sel.all_from_input_p (0))
7848 else if (sel.all_from_input_p (1))
7851 sel.rotate_inputs (1);
7853 else if (known_ge (poly_uint64 (sel[0]), nelts))
7855 std::swap (op0, op1);
7856 sel.rotate_inputs (1);
7860 tree cop0 = op0, cop1 = op1;
7861 if (TREE_CODE (op0) == SSA_NAME
7862 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7863 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7864 cop0 = gimple_assign_rhs1 (def);
7865 if (TREE_CODE (op1) == SSA_NAME
7866 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7867 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7868 cop1 = gimple_assign_rhs1 (def);
7872 (if ((TREE_CODE (cop0) == VECTOR_CST
7873 || TREE_CODE (cop0) == CONSTRUCTOR)
7874 && (TREE_CODE (cop1) == VECTOR_CST
7875 || TREE_CODE (cop1) == CONSTRUCTOR)
7876 && (t = fold_vec_perm (type, cop0, cop1, sel)))
7880 bool changed = (op0 == op1 && !single_arg);
7881 tree ins = NULL_TREE;
7884 /* See if the permutation is performing a single element
7885 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7886 in that case. But only if the vector mode is supported,
7887 otherwise this is invalid GIMPLE. */
7888 if (op_mode != BLKmode
7889 && (TREE_CODE (cop0) == VECTOR_CST
7890 || TREE_CODE (cop0) == CONSTRUCTOR
7891 || TREE_CODE (cop1) == VECTOR_CST
7892 || TREE_CODE (cop1) == CONSTRUCTOR))
7894 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7897 /* After canonicalizing the first elt to come from the
7898 first vector we only can insert the first elt from
7899 the first vector. */
7901 if ((ins = fold_read_from_vector (cop0, sel[0])))
7904 /* The above can fail for two-element vectors which always
7905 appear to insert the first element, so try inserting
7906 into the second lane as well. For more than two
7907 elements that's wasted time. */
7908 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
7910 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
7911 for (at = 0; at < encoded_nelts; ++at)
7912 if (maybe_ne (sel[at], at))
7914 if (at < encoded_nelts
7915 && (known_eq (at + 1, nelts)
7916 || sel.series_p (at + 1, 1, at + 1, 1)))
7918 if (known_lt (poly_uint64 (sel[at]), nelts))
7919 ins = fold_read_from_vector (cop0, sel[at]);
7921 ins = fold_read_from_vector (cop1, sel[at] - nelts);
7926 /* Generate a canonical form of the selector. */
7927 if (!ins && sel.encoding () != builder)
7929 /* Some targets are deficient and fail to expand a single
7930 argument permutation while still allowing an equivalent
7931 2-argument version. */
7933 if (sel.ninputs () == 2
7934 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
7935 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7938 vec_perm_indices sel2 (builder, 2, nelts);
7939 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
7940 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
7942 /* Not directly supported with either encoding,
7943 so use the preferred form. */
7944 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7946 if (!operand_equal_p (op2, oldop2, 0))
7951 (bit_insert { op0; } { ins; }
7952 { bitsize_int (at * vector_element_bits (type)); })
7954 (vec_perm { op0; } { op1; } { op2; }))))))))))
7956 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
7958 (match vec_same_elem_p
7961 (match vec_same_elem_p
7963 (if (TREE_CODE (@0) == SSA_NAME
7964 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
7966 (match vec_same_elem_p
7968 (if (uniform_vector_p (@0))))
7972 (vec_perm vec_same_elem_p@0 @0 @1)
7975 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
7977 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
7978 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
7979 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
7981 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
7982 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
7983 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
7986 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
7987 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
7988 constant which when multiplied by a power of 2 contains a unique value
7989 in the top 5 or 6 bits. This is then indexed into a table which maps it
7990 to the number of trailing zeroes. */
7991 (match (ctz_table_index @1 @2 @3)
7992 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
7994 (match (cond_expr_convert_p @0 @2 @3 @6)
7995 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
7996 (if (INTEGRAL_TYPE_P (type)
7997 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7998 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7999 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8000 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
8001 && TYPE_PRECISION (TREE_TYPE (@0))
8002 == TYPE_PRECISION (TREE_TYPE (@2))
8003 && TYPE_PRECISION (TREE_TYPE (@0))
8004 == TYPE_PRECISION (TREE_TYPE (@3))
8005 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
8006 signess when convert is truncation, but not ok for extension since
8007 it's sign_extend vs zero_extend. */
8008 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
8009 || (TYPE_UNSIGNED (TREE_TYPE (@2))
8010 == TYPE_UNSIGNED (TREE_TYPE (@3))))
8012 && single_use (@5))))
8014 (for bit_op (bit_and bit_ior bit_xor)
8015 (match (bitwise_induction_p @0 @2 @3)
8017 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
8020 (match (bitwise_induction_p @0 @2 @3)
8022 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))