MATCH: Add ABSU<a> == 0 to a == 0 simplification
[official-gcc.git] / gcc / match.pd
blobceae1c34abc121a43dbad560e1a8b47c8139fbd0
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-2023 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
14 version.
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
19 for more details.
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.  */
27 (define_predicates
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
31    zerop
32    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
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)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG)
91 /* Binary operations and their associated IFN_COND_* function.  */
92 (define_operator_list UNCOND_BINARY
93   plus minus
94   mult trunc_div trunc_mod rdiv
95   min max
96   IFN_FMIN IFN_FMAX
97   bit_and bit_ior bit_xor
98   lshift rshift)
99 (define_operator_list COND_BINARY
100   IFN_COND_ADD IFN_COND_SUB
101   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
102   IFN_COND_MIN IFN_COND_MAX
103   IFN_COND_FMIN IFN_COND_FMAX
104   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
105   IFN_COND_SHL IFN_COND_SHR)
107 /* Same for ternary operations.  */
108 (define_operator_list UNCOND_TERNARY
109   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
110 (define_operator_list COND_TERNARY
111   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
113 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
114 (define_operator_list ATOMIC_FETCH_OR_XOR_N
115   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
116   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
117   BUILT_IN_ATOMIC_FETCH_OR_16
118   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
119   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
120   BUILT_IN_ATOMIC_FETCH_XOR_16
121   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
122   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
123   BUILT_IN_ATOMIC_XOR_FETCH_16)
124 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
125 (define_operator_list SYNC_FETCH_OR_XOR_N
126   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
127   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
128   BUILT_IN_SYNC_FETCH_AND_OR_16
129   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
130   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
131   BUILT_IN_SYNC_FETCH_AND_XOR_16
132   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
133   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
134   BUILT_IN_SYNC_XOR_AND_FETCH_16)
135 /* __atomic_fetch_and_*.  */
136 (define_operator_list ATOMIC_FETCH_AND_N
137   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
138   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
139   BUILT_IN_ATOMIC_FETCH_AND_16)
140 /* __sync_fetch_and_and_*.  */
141 (define_operator_list SYNC_FETCH_AND_AND_N
142   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
143   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
144   BUILT_IN_SYNC_FETCH_AND_AND_16)
146 /* With nop_convert? combine convert? and view_convert? in one pattern
147    plus conditionalize on tree_nop_conversion_p conversions.  */
148 (match (nop_convert @0)
149  (convert @0)
150  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_convert @0)
152  (view_convert @0)
153  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
154       && known_eq (TYPE_VECTOR_SUBPARTS (type),
155                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
156       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
158 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
159    ABSU_EXPR returns unsigned absolute value of the operand and the operand
160    of the ABSU_EXPR will have the corresponding signed type.  */
161 (simplify (abs (convert @0))
162  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
163       && !TYPE_UNSIGNED (TREE_TYPE (@0))
164       && element_precision (type) > element_precision (TREE_TYPE (@0)))
165   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
166    (convert (absu:utype @0)))))
168 #if GIMPLE
169 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
170 (simplify
171  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
172  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
173       && !TYPE_UNSIGNED (TREE_TYPE (@0))
174       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
175   (abs @0)))
176 #endif
178 /* Simplifications of operations with one constant operand and
179    simplifications to constants or single values.  */
181 (for op (plus pointer_plus minus bit_ior bit_xor)
182   (simplify
183     (op @0 integer_zerop)
184     (non_lvalue @0)))
186 /* 0 +p index -> (type)index */
187 (simplify
188  (pointer_plus integer_zerop @1)
189  (non_lvalue (convert @1)))
191 /* ptr - 0 -> (type)ptr */
192 (simplify
193  (pointer_diff @0 integer_zerop)
194  (convert @0))
196 /* See if ARG1 is zero and X + ARG1 reduces to X.
197    Likewise if the operands are reversed.  */
198 (simplify
199  (plus:c @0 real_zerop@1)
200  (if (fold_real_zero_addition_p (type, @0, @1, 0))
201   (non_lvalue @0)))
203 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
204 (simplify
205  (minus @0 real_zerop@1)
206  (if (fold_real_zero_addition_p (type, @0, @1, 1))
207   (non_lvalue @0)))
209 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
210    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
211    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
212    if not -frounding-math.  For sNaNs the first operation would raise
213    exceptions but turn the result into qNan, so the second operation
214    would not raise it.   */
215 (for inner_op (plus minus)
216  (for outer_op (plus minus)
217   (simplify
218    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
219     (if (real_zerop (@1)
220          && real_zerop (@2)
221          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
222      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
223                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
224              bool outer_plus
225                = ((outer_op == PLUS_EXPR)
226                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
227       (if (outer_plus && !inner_plus)
228        (outer_op @0 @2)
229        @3))))))
231 /* Simplify x - x.
232    This is unsafe for certain floats even in non-IEEE formats.
233    In IEEE, it is unsafe because it does wrong for NaNs.
234    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
235    Also note that operand_equal_p is always false if an operand
236    is volatile.  */
237 (simplify
238  (minus @0 @0)
239  (if (!FLOAT_TYPE_P (type)
240       || (!tree_expr_maybe_nan_p (@0)
241           && !tree_expr_maybe_infinite_p (@0)
242           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
243               || !HONOR_SIGNED_ZEROS (type))))
244   { build_zero_cst (type); }))
245 (simplify
246  (pointer_diff @@0 @0)
247  { build_zero_cst (type); })
249 (simplify
250  (mult @0 integer_zerop@1)
251  @1)
253 /* -x == x -> x == 0 */
254 (for cmp (eq ne)
255  (simplify
256   (cmp:c @0 (negate @0))
257    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
258         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
259     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
261 /* Maybe fold x * 0 to 0.  The expressions aren't the same
262    when x is NaN, since x * 0 is also NaN.  Nor are they the
263    same in modes with signed zeros, since multiplying a
264    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
265    since x * 0 is NaN.  */
266 (simplify
267  (mult @0 real_zerop@1)
268  (if (!tree_expr_maybe_nan_p (@0)
269       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
270       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
271   @1))
273 /* In IEEE floating point, x*1 is not equivalent to x for snans.
274    Likewise for complex arithmetic with signed zeros.  */
275 (simplify
276  (mult @0 real_onep)
277  (if (!tree_expr_maybe_signaling_nan_p (@0)
278       && (!HONOR_SIGNED_ZEROS (type)
279           || !COMPLEX_FLOAT_TYPE_P (type)))
280   (non_lvalue @0)))
282 /* Transform x * -1.0 into -x.  */
283 (simplify
284  (mult @0 real_minus_onep)
285   (if (!tree_expr_maybe_signaling_nan_p (@0)
286        && (!HONOR_SIGNED_ZEROS (type)
287            || !COMPLEX_FLOAT_TYPE_P (type)))
288    (negate @0)))
290 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
291    unless the target has native support for the former but not the latter.  */
292 (simplify
293  (mult @0 VECTOR_CST@1)
294  (if (initializer_each_zero_or_onep (@1)
295       && !HONOR_SNANS (type)
296       && !HONOR_SIGNED_ZEROS (type))
297   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
298    (if (itype
299         && (!VECTOR_MODE_P (TYPE_MODE (type))
300             || (VECTOR_MODE_P (TYPE_MODE (itype))
301                 && optab_handler (and_optab,
302                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
303     (view_convert (bit_and:itype (view_convert @0)
304                                  (ne @1 { build_zero_cst (type); })))))))
306 (for cmp (gt ge lt le)
307      outp (convert convert negate negate)
308      outn (negate negate convert convert)
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). */
311  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
312  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
313  (simplify
314   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
315   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
316    (outp (abs @0))))
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). */
319  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
320  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
321  (simplify
322   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
323   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
324    (outn (abs @0)))))
326 /* Transform X * copysign (1.0, X) into abs(X). */
327 (simplify
328  (mult:c @0 (COPYSIGN_ALL real_onep @0))
329  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
330   (abs @0)))
332 /* Transform X * copysign (1.0, -X) into -abs(X). */
333 (simplify
334  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
335  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
336   (negate (abs @0))))
338 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
339 (simplify
340  (COPYSIGN_ALL REAL_CST@0 @1)
341  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
342   (COPYSIGN_ALL (negate @0) @1)))
344 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
345    tree-ssa-math-opts.cc does the corresponding optimization for
346    unconditional multiplications (via xorsign).  */
347 (simplify
348  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
349  (with { tree signs = sign_mask_for (type); }
350   (if (signs)
351    (with { tree inttype = TREE_TYPE (signs); }
352     (view_convert:type
353      (IFN_COND_XOR:inttype @0
354       (view_convert:inttype @1)
355       (bit_and (view_convert:inttype @2) { signs; })
356       (view_convert:inttype @3)))))))
358 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
359 (simplify
360   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
361   (abs @0))
363 /* X * 1, X / 1 -> X.  */
364 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
365   (simplify
366     (op @0 integer_onep)
367     (non_lvalue @0)))
369 /* (A / (1 << B)) -> (A >> B).
370    Only for unsigned A.  For signed A, this would not preserve rounding
371    toward zero.
372    For example: (-1 / ( 1 << B)) !=  -1 >> B.
373    Also handle widening conversions, like:
374    (A / (unsigned long long) (1U << B)) -> (A >> B)
375    or
376    (A / (unsigned long long) (1 << B)) -> (A >> B).
377    If the left shift is signed, it can be done only if the upper bits
378    of A starting from shift's type sign bit are zero, as
379    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
380    so it is valid only if A >> 31 is zero.  */
381 (simplify
382  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
383  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
384       && (!VECTOR_TYPE_P (type)
385           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
386           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
387       && (useless_type_conversion_p (type, TREE_TYPE (@1))
388           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
389               && (TYPE_UNSIGNED (TREE_TYPE (@1))
390                   || (element_precision (type)
391                       == element_precision (TREE_TYPE (@1)))
392                   || (INTEGRAL_TYPE_P (type)
393                       && (tree_nonzero_bits (@0)
394                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
395                                       true,
396                                       element_precision (type))) == 0)))))
397    (if (!VECTOR_TYPE_P (type)
398         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
399         && element_precision (TREE_TYPE (@3)) < element_precision (type))
400     (convert (rshift @3 @2))
401     (rshift @0 @2))))
403 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
404    undefined behavior in constexpr evaluation, and assuming that the division
405    traps enables better optimizations than these anyway.  */
406 (for div (trunc_div ceil_div floor_div round_div exact_div)
407  /* 0 / X is always zero.  */
408  (simplify
409   (div integer_zerop@0 @1)
410   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
411   (if (!integer_zerop (@1))
412    @0))
413  /* X / -1 is -X.  */
414  (simplify
415   (div @0 integer_minus_onep@1)
416   (if (!TYPE_UNSIGNED (type))
417    (negate @0)))
418  /* X / bool_range_Y is X.  */ 
419  (simplify
420   (div @0 SSA_NAME@1)
421   (if (INTEGRAL_TYPE_P (type)
422        && ssa_name_has_boolean_range (@1)
423        && !flag_non_call_exceptions)
424    @0))
425  /* X / X is one.  */
426  (simplify
427   (div @0 @0)
428   /* But not for 0 / 0 so that we can get the proper warnings and errors.
429      And not for _Fract types where we can't build 1.  */
430   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
431        && !integer_zerop (@0)
432        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
433    { build_one_cst (type); }))
434  /* X / abs (X) is X < 0 ? -1 : 1.  */
435  (simplify
436    (div:C @0 (abs @0))
437    (if (INTEGRAL_TYPE_P (type)
438         && TYPE_OVERFLOW_UNDEFINED (type)
439         && !integer_zerop (@0)
440         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
441     (cond (lt @0 { build_zero_cst (type); })
442           { build_minus_one_cst (type); } { build_one_cst (type); })))
443  /* X / -X is -1.  */
444  (simplify
445    (div:C @0 (negate @0))
446    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
447         && TYPE_OVERFLOW_UNDEFINED (type)
448         && !integer_zerop (@0)
449         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
450     { build_minus_one_cst (type); })))
452 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
453    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
454    for MOD instead of DIV.  */
455 (for floor_divmod (floor_div floor_mod)
456      trunc_divmod (trunc_div trunc_mod)
457  (simplify
458   (floor_divmod @0 @1)
459   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
460        && TYPE_UNSIGNED (type))
461    (trunc_divmod @0 @1))))
463 /* 1 / X -> X == 1 for unsigned integer X.
464    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
465    But not for 1 / 0 so that we can get proper warnings and errors,
466    and not for 1-bit integers as they are edge cases better handled
467    elsewhere.  */
468 (simplify
469  (trunc_div integer_onep@0 @1)
470  (if (INTEGRAL_TYPE_P (type)
471       && TYPE_PRECISION (type) > 1
472       && !integer_zerop (@1)
473       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
474   (if (TYPE_UNSIGNED (type))
475    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
476    (with { tree utype = unsigned_type_for (type); }
477     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
478               { build_int_cst (utype, 2); })
479      @1 { build_zero_cst (type); })))))
481 /* Combine two successive divisions.  Note that combining ceil_div
482    and floor_div is trickier and combining round_div even more so.  */
483 (for div (trunc_div exact_div)
484  (simplify
485   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
486   (with {
487     wi::overflow_type overflow;
488     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
489                             TYPE_SIGN (type), &overflow);
490    }
491    (if (div == EXACT_DIV_EXPR
492         || optimize_successive_divisions_p (@2, @3))
493     (if (!overflow)
494      (div @0 { wide_int_to_tree (type, mul); })
495      (if (TYPE_UNSIGNED (type)
496           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
497       { build_zero_cst (type); }))))))
499 /* Combine successive multiplications.  Similar to above, but handling
500    overflow is different.  */
501 (simplify
502  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
503  (with {
504    wi::overflow_type overflow;
505    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
506                            TYPE_SIGN (type), &overflow);
507   }
508   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
509      otherwise undefined overflow implies that @0 must be zero.  */
510   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
511    (mult @0 { wide_int_to_tree (type, mul); }))))
513 /* Similar to above, but there could be an extra add/sub between
514    successive multuiplications.  */
515 (simplify
516  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
517  (with {
518    bool overflowed = true;
519    wi::overflow_type ovf1, ovf2;
520    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
521                            TYPE_SIGN (type), &ovf1);
522    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
523                            TYPE_SIGN (type), &ovf2);
524   if (TYPE_OVERFLOW_UNDEFINED (type))
525     {
526 #if GIMPLE
527       value_range vr0;
528       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
529           && get_global_range_query ()->range_of_expr (vr0, @4)
530           && !vr0.varying_p () && !vr0.undefined_p ())
531         {
532           wide_int wmin0 = vr0.lower_bound ();
533           wide_int wmax0 = vr0.upper_bound ();
534           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
535           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
536           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
537             {
538               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
539               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
540               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
541                 overflowed = false;
542             }
543         }
544 #endif
545     }
546   else
547    overflowed = false;
549   /* Skip folding on overflow.  */
550   (if (!overflowed)
551    (plus (mult @0 { wide_int_to_tree (type, mul); })
552          { wide_int_to_tree (type, add); }))))
554 /* Similar to above, but a multiplication between successive additions.  */
555 (simplify
556  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
557  (with {
558    bool overflowed = true;
559    wi::overflow_type ovf1;
560    wi::overflow_type ovf2;
561    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
562                            TYPE_SIGN (type), &ovf1);
563    wide_int add = wi::add (mul, wi::to_wide (@3),
564                            TYPE_SIGN (type), &ovf2);
565   if (TYPE_OVERFLOW_UNDEFINED (type))
566     {
567 #if GIMPLE
568       value_range vr0;
569       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
570           && get_global_range_query ()->range_of_expr (vr0, @0)
571           && !vr0.varying_p () && !vr0.undefined_p ())
572         {
573           wide_int wmin0 = vr0.lower_bound ();
574           wide_int wmax0 = vr0.upper_bound ();
575           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
576           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
577           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
578             {
579               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
580               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
581               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
582                 overflowed = false;
583             }
584         }
585 #endif
586     }
587   else
588    overflowed = false;
590   /* Skip folding on overflow.  */
591   (if (!overflowed)
592    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
594 /* Optimize A / A to 1.0 if we don't care about
595    NaNs or Infinities.  */
596 (simplify
597  (rdiv @0 @0)
598  (if (FLOAT_TYPE_P (type)
599       && ! HONOR_NANS (type)
600       && ! HONOR_INFINITIES (type))
601   { build_one_cst (type); }))
603 /* Optimize -A / A to -1.0 if we don't care about
604    NaNs or Infinities.  */
605 (simplify
606  (rdiv:C @0 (negate @0))
607  (if (FLOAT_TYPE_P (type)
608       && ! HONOR_NANS (type)
609       && ! HONOR_INFINITIES (type))
610   { build_minus_one_cst (type); }))
612 /* PR71078: x / abs(x) -> copysign (1.0, x) */
613 (simplify
614  (rdiv:C (convert? @0) (convert? (abs @0)))
615   (if (SCALAR_FLOAT_TYPE_P (type)
616        && ! HONOR_NANS (type)
617        && ! HONOR_INFINITIES (type))
618    (switch
619     (if (types_match (type, float_type_node))
620      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
621     (if (types_match (type, double_type_node))
622      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
623     (if (types_match (type, long_double_type_node))
624      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
626 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
627 (simplify
628  (rdiv @0 real_onep)
629  (if (!tree_expr_maybe_signaling_nan_p (@0))
630   (non_lvalue @0)))
632 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
633 (simplify
634  (rdiv @0 real_minus_onep)
635  (if (!tree_expr_maybe_signaling_nan_p (@0))
636   (negate @0)))
638 (if (flag_reciprocal_math)
639  /* Convert (A/B)/C to A/(B*C). */
640  (simplify
641   (rdiv (rdiv:s @0 @1) @2)
642   (rdiv @0 (mult @1 @2)))
644  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
645  (simplify
646   (rdiv @0 (mult:s @1 REAL_CST@2))
647   (with
648    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
649    (if (tem)
650     (rdiv (mult @0 { tem; } ) @1))))
652  /* Convert A/(B/C) to (A/B)*C  */
653  (simplify
654   (rdiv @0 (rdiv:s @1 @2))
655    (mult (rdiv @0 @1) @2)))
657 /* Simplify x / (- y) to -x / y.  */
658 (simplify
659  (rdiv @0 (negate @1))
660  (rdiv (negate @0) @1))
662 (if (flag_unsafe_math_optimizations)
663  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
664     Since C / x may underflow to zero, do this only for unsafe math.  */
665  (for op (lt le gt ge)
666       neg_op (gt ge lt le)
667   (simplify
668    (op (rdiv REAL_CST@0 @1) real_zerop@2)
669    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
670     (switch
671      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
672       (op @1 @2))
673      /* For C < 0, use the inverted operator.  */
674      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
675       (neg_op @1 @2)))))))
677 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
678 (for div (trunc_div ceil_div floor_div round_div exact_div)
679  (simplify
680   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
681   (if (integer_pow2p (@2)
682        && tree_int_cst_sgn (@2) > 0
683        && tree_nop_conversion_p (type, TREE_TYPE (@0))
684        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
685    (rshift (convert @0)
686            { build_int_cst (integer_type_node,
687                             wi::exact_log2 (wi::to_wide (@2))); }))))
689 /* If ARG1 is a constant, we can convert this to a multiply by the
690    reciprocal.  This does not have the same rounding properties,
691    so only do this if -freciprocal-math.  We can actually
692    always safely do it if ARG1 is a power of two, but it's hard to
693    tell if it is or not in a portable manner.  */
694 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
695  (simplify
696   (rdiv @0 cst@1)
697   (if (optimize)
698    (if (flag_reciprocal_math
699         && !real_zerop (@1))
700     (with
701      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
702      (if (tem)
703       (mult @0 { tem; } )))
704     (if (cst != COMPLEX_CST)
705      (with { tree inverse = exact_inverse (type, @1); }
706       (if (inverse)
707        (mult @0 { inverse; } ))))))))
709 (for mod (ceil_mod floor_mod round_mod trunc_mod)
710  /* 0 % X is always zero.  */
711  (simplify
712   (mod integer_zerop@0 @1)
713   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
714   (if (!integer_zerop (@1))
715    @0))
716  /* X % 1 is always zero.  */
717  (simplify
718   (mod @0 integer_onep)
719   { build_zero_cst (type); })
720  /* X % -1 is zero.  */
721  (simplify
722   (mod @0 integer_minus_onep@1)
723   (if (!TYPE_UNSIGNED (type))
724    { build_zero_cst (type); }))
725  /* X % X is zero.  */
726  (simplify
727   (mod @0 @0)
728   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
729   (if (!integer_zerop (@0))
730    { build_zero_cst (type); }))
731  /* (X % Y) % Y is just X % Y.  */
732  (simplify
733   (mod (mod@2 @0 @1) @1)
734   @2)
735  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
736  (simplify
737   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
738   (if (ANY_INTEGRAL_TYPE_P (type)
739        && TYPE_OVERFLOW_UNDEFINED (type)
740        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
741                              TYPE_SIGN (type)))
742    { build_zero_cst (type); }))
743  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
744     modulo and comparison, since it is simpler and equivalent.  */
745  (for cmp (eq ne)
746   (simplify
747    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
748    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
749     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
750      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
752 /* X % -C is the same as X % C.  */
753 (simplify
754  (trunc_mod @0 INTEGER_CST@1)
755   (if (TYPE_SIGN (type) == SIGNED
756        && !TREE_OVERFLOW (@1)
757        && wi::neg_p (wi::to_wide (@1))
758        && !TYPE_OVERFLOW_TRAPS (type)
759        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
760        && !sign_bit_p (@1, @1))
761    (trunc_mod @0 (negate @1))))
763 /* X % -Y is the same as X % Y.  */
764 (simplify
765  (trunc_mod @0 (convert? (negate @1)))
766  (if (INTEGRAL_TYPE_P (type)
767       && !TYPE_UNSIGNED (type)
768       && !TYPE_OVERFLOW_TRAPS (type)
769       && tree_nop_conversion_p (type, TREE_TYPE (@1))
770       /* Avoid this transformation if X might be INT_MIN or
771          Y might be -1, because we would then change valid
772          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
773       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
774           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
775                                                         (TREE_TYPE (@1))))))
776   (trunc_mod @0 (convert @1))))
778 /* X - (X / Y) * Y is the same as X % Y.  */
779 (simplify
780  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
781  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
782   (convert (trunc_mod @0 @1))))
784 /* x * (1 + y / x) - y -> x - y % x */
785 (simplify
786  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
787  (if (INTEGRAL_TYPE_P (type))
788   (minus @0 (trunc_mod @1 @0))))
790 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
791    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
792    Also optimize A % (C << N)  where C is a power of 2,
793    to A & ((C << N) - 1).
794    Also optimize "A shift (B % C)", if C is a power of 2, to
795    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
796    and assume (B % C) is nonnegative as shifts negative values would
797    be UB.  */
798 (match (power_of_two_cand @1)
799  INTEGER_CST@1)
800 (match (power_of_two_cand @1)
801  (lshift INTEGER_CST@1 @2))
802 (for mod (trunc_mod floor_mod)
803  (for shift (lshift rshift)
804   (simplify
805    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
806    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
807     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
808                                                       1); }))))))
809  (simplify
810   (mod @0 (convert? (power_of_two_cand@1 @2)))
811   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
812        /* Allow any integral conversions of the divisor, except
813           conversion from narrower signed to wider unsigned type
814           where if @1 would be negative power of two, the divisor
815           would not be a power of two.  */
816        && INTEGRAL_TYPE_P (type)
817        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
818        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
819            || TYPE_UNSIGNED (TREE_TYPE (@1))
820            || !TYPE_UNSIGNED (type))
821        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
822    (with { tree utype = TREE_TYPE (@1);
823            if (!TYPE_OVERFLOW_WRAPS (utype))
824              utype = unsigned_type_for (utype); }
825     (bit_and @0 (convert (minus (convert:utype @1)
826                                 { build_one_cst (utype); })))))))
828 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
829 (simplify
830  (trunc_div (mult @0 integer_pow2p@1) @1)
831  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
832   (bit_and @0 { wide_int_to_tree
833                 (type, wi::mask (TYPE_PRECISION (type)
834                                  - wi::exact_log2 (wi::to_wide (@1)),
835                                  false, TYPE_PRECISION (type))); })))
837 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
838 (simplify
839  (mult (trunc_div @0 integer_pow2p@1) @1)
840  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
841   (bit_and @0 (negate @1))))
843 /* Simplify (t * 2) / 2) -> t.  */
844 (for div (trunc_div ceil_div floor_div round_div exact_div)
845  (simplify
846   (div (mult:c @0 @1) @1)
847   (if (ANY_INTEGRAL_TYPE_P (type))
848    (if (TYPE_OVERFLOW_UNDEFINED (type))
849     @0
850 #if GIMPLE
851     (with
852      {
853        bool overflowed = true;
854        value_range vr0, vr1;
855        if (INTEGRAL_TYPE_P (type)
856            && get_global_range_query ()->range_of_expr (vr0, @0)
857            && get_global_range_query ()->range_of_expr (vr1, @1)
858            && !vr0.varying_p () && !vr0.undefined_p ()
859            && !vr1.varying_p () && !vr1.undefined_p ())
860          {
861            wide_int wmin0 = vr0.lower_bound ();
862            wide_int wmax0 = vr0.upper_bound ();
863            wide_int wmin1 = vr1.lower_bound ();
864            wide_int wmax1 = vr1.upper_bound ();
865            /* If the multiplication can't overflow/wrap around, then
866               it can be optimized too.  */
867            wi::overflow_type min_ovf, max_ovf;
868            wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
869            wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
870            if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
871              {
872                wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
873                wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
874                if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
875                  overflowed = false;
876              }
877          }
878      }
879     (if (!overflowed)
880      @0))
881 #endif
882    ))))
884 (for op (negate abs)
885  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
886  (for coss (COS COSH)
887   (simplify
888    (coss (op @0))
889     (coss @0)))
890  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
891  (for pows (POW)
892   (simplify
893    (pows (op @0) REAL_CST@1)
894    (with { HOST_WIDE_INT n; }
895     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
896      (pows @0 @1)))))
897  /* Likewise for powi.  */
898  (for pows (POWI)
899   (simplify
900    (pows (op @0) INTEGER_CST@1)
901    (if ((wi::to_wide (@1) & 1) == 0)
902     (pows @0 @1))))
903  /* Strip negate and abs from both operands of hypot.  */
904  (for hypots (HYPOT)
905   (simplify
906    (hypots (op @0) @1)
907    (hypots @0 @1))
908   (simplify
909    (hypots @0 (op @1))
910    (hypots @0 @1)))
911  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
912  (for copysigns (COPYSIGN_ALL)
913   (simplify
914    (copysigns (op @0) @1)
915    (copysigns @0 @1))))
917 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
918 (simplify
919  (mult (abs@1 @0) @1)
920  (mult @0 @0))
922 /* Convert absu(x)*absu(x) -> x*x.  */
923 (simplify
924  (mult (absu@1 @0) @1)
925  (mult (convert@2 @0) @2))
927 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
928 (for coss (COS COSH)
929      copysigns (COPYSIGN)
930  (simplify
931   (coss (copysigns @0 @1))
932    (coss @0)))
934 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
935 (for pows (POW)
936      copysigns (COPYSIGN)
937  (simplify
938   (pows (copysigns @0 @2) REAL_CST@1)
939   (with { HOST_WIDE_INT n; }
940    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
941     (pows @0 @1)))))
942 /* Likewise for powi.  */
943 (for pows (POWI)
944      copysigns (COPYSIGN)
945  (simplify
946   (pows (copysigns @0 @2) INTEGER_CST@1)
947   (if ((wi::to_wide (@1) & 1) == 0)
948    (pows @0 @1))))
950 (for hypots (HYPOT)
951      copysigns (COPYSIGN)
952  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
953  (simplify
954   (hypots (copysigns @0 @1) @2)
955   (hypots @0 @2))
956  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
957  (simplify
958   (hypots @0 (copysigns @1 @2))
959   (hypots @0 @1)))
961 /* copysign(x, CST) -> [-]abs (x).  */
962 (for copysigns (COPYSIGN_ALL)
963  (simplify
964   (copysigns @0 REAL_CST@1)
965   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
966    (negate (abs @0))
967    (abs @0))))
969 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
970 (for copysigns (COPYSIGN_ALL)
971  (simplify
972   (copysigns (copysigns @0 @1) @2)
973   (copysigns @0 @2)))
975 /* copysign(x,y)*copysign(x,y) -> x*x.  */
976 (for copysigns (COPYSIGN_ALL)
977  (simplify
978   (mult (copysigns@2 @0 @1) @2)
979   (mult @0 @0)))
981 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
982 (for ccoss (CCOS CCOSH)
983  (simplify
984   (ccoss (negate @0))
985    (ccoss @0)))
987 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
988 (for ops (conj negate)
989  (for cabss (CABS)
990   (simplify
991    (cabss (ops @0))
992    (cabss @0))))
994 /* Fold (a * (1 << b)) into (a << b)  */
995 (simplify
996  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
997   (if (! FLOAT_TYPE_P (type)
998        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
999    (lshift @0 @2)))
1001 /* Shifts by constants distribute over several binary operations,
1002    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1003 (for op (plus minus)
1004   (simplify
1005     (op (lshift:s @0 @1) (lshift:s @2 @1))
1006     (if (INTEGRAL_TYPE_P (type)
1007          && TYPE_OVERFLOW_WRAPS (type)
1008          && !TYPE_SATURATING (type))
1009       (lshift (op @0 @2) @1))))
1011 (for op (bit_and bit_ior bit_xor)
1012   (simplify
1013     (op (lshift:s @0 @1) (lshift:s @2 @1))
1014     (if (INTEGRAL_TYPE_P (type))
1015       (lshift (op @0 @2) @1)))
1016   (simplify
1017     (op (rshift:s @0 @1) (rshift:s @2 @1))
1018     (if (INTEGRAL_TYPE_P (type))
1019       (rshift (op @0 @2) @1))))
1021 /* Fold (1 << (C - x)) where C = precision(type) - 1
1022    into ((1 << C) >> x). */
1023 (simplify
1024  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1025   (if (INTEGRAL_TYPE_P (type)
1026        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1027        && single_use (@1))
1028    (if (TYPE_UNSIGNED (type))
1029      (rshift (lshift @0 @2) @3)
1030    (with
1031     { tree utype = unsigned_type_for (type); }
1032     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1034 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1035 (simplify
1036  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1037  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1038       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1039   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1040    (bit_and (convert @0)
1041             { wide_int_to_tree (type,
1042                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1044 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1045 (for cst (INTEGER_CST VECTOR_CST)
1046  (simplify
1047   (rshift (negate:s @0) cst@1)
1048    (if (!TYPE_UNSIGNED (type)
1049         && TYPE_OVERFLOW_UNDEFINED (type))
1050     (with { tree stype = TREE_TYPE (@1);
1051             tree bt = truth_type_for (type);
1052             tree zeros = build_zero_cst (type);
1053             tree cst = NULL_TREE; }
1054      (switch
1055       /* Handle scalar case.  */
1056       (if (INTEGRAL_TYPE_P (type)
1057            /* If we apply the rule to the scalar type before vectorization
1058               we will enforce the result of the comparison being a bool
1059               which will require an extra AND on the result that will be
1060               indistinguishable from when the user did actually want 0
1061               or 1 as the result so it can't be removed.  */
1062            && canonicalize_math_after_vectorization_p ()
1063            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1064        (negate (convert (gt @0 { zeros; }))))
1065       /* Handle vector case.  */
1066       (if (VECTOR_INTEGER_TYPE_P (type)
1067            /* First check whether the target has the same mode for vector
1068               comparison results as it's operands do.  */
1069            && TYPE_MODE (bt) == TYPE_MODE (type)
1070            /* Then check to see if the target is able to expand the comparison
1071               with the given type later on, otherwise we may ICE.  */
1072            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1073            && (cst = uniform_integer_cst_p (@1)) != NULL
1074            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1075        (view_convert (gt:bt @0 { zeros; }))))))))
1077 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1078 (simplify
1079  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1080   (if (flag_associative_math
1081        && single_use (@3))
1082    (with
1083     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1084     (if (tem)
1085      (rdiv { tem; } @1)))))
1087 /* Simplify ~X & X as zero.  */
1088 (simplify
1089  (bit_and:c (convert? @0) (convert? (bit_not @0)))
1090   { build_zero_cst (type); })
1092 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1093 (simplify
1094   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1095   (if (TYPE_UNSIGNED (type))
1096     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1098 (for bitop (bit_and bit_ior)
1099      cmp (eq ne)
1100  /* PR35691: Transform
1101     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1102     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1103  (simplify
1104   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1105    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1106         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1107         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1108     (cmp (bit_ior @0 (convert @1)) @2)))
1109  /* Transform:
1110     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1111     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1112  (simplify
1113   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1114    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1115         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1116         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1117     (cmp (bit_and @0 (convert @1)) @2))))
1119 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1120 (simplify
1121  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1122   (minus (bit_xor @0 @1) @1))
1123 (simplify
1124  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1125  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1126   (minus (bit_xor @0 @1) @1)))
1128 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1129 (simplify
1130  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1131   (minus @1 (bit_xor @0 @1)))
1133 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1134 (for op (bit_ior bit_xor plus)
1135  (simplify
1136   (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
1137    (bit_xor @0 @1))
1138  (simplify
1139   (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
1140   (if (~wi::to_wide (@2) == wi::to_wide (@1))
1141    (bit_xor @0 @1))))
1143 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1144 (simplify
1145   (bit_ior:c (bit_xor:c @0 @1) @0)
1146   (bit_ior @0 @1))
1148 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1149 (simplify
1150  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1151  @2)
1153 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1154 (simplify
1155  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1156  (bit_not (bit_and @0 @1)))
1158 /* (~a & b) ^ a  -->   (a | b)   */
1159 (simplify
1160  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1161  (bit_ior @0 @1))
1163 /* (a | b) & ~(a ^ b)  -->  a & b  */
1164 (simplify
1165  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1166  (bit_and @0 @1))
1168 /* a | ~(a ^ b)  -->  a | ~b  */
1169 (simplify
1170  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1171  (bit_ior @0 (bit_not @1)))
1173 /* (a | b) | (a &^ b)  -->  a | b  */
1174 (for op (bit_and bit_xor)
1175  (simplify
1176   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1177   @2))
1179 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1180 (simplify
1181  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1182  @2)
1184 /* ~(~a & b)  -->  a | ~b  */
1185 (simplify
1186  (bit_not (bit_and:cs (bit_not @0) @1))
1187  (bit_ior @0 (bit_not @1)))
1189 /* ~(~a | b) --> a & ~b */
1190 (simplify
1191  (bit_not (bit_ior:cs (bit_not @0) @1))
1192  (bit_and @0 (bit_not @1)))
1194 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1195 (simplify
1196  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1197  (bit_and @3 (bit_not @2)))
1199 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1200 (simplify
1201  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1202  (bit_ior @3 @2))
1204 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1205 (simplify
1206  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1207   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1209 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1210 (simplify
1211  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1212   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1214 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1215 (simplify
1216  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1217  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1218       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1219   (bit_xor @0 @1)))
1221 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1222    ((A & N) + B) & M -> (A + B) & M
1223    Similarly if (N & M) == 0,
1224    ((A | N) + B) & M -> (A + B) & M
1225    and for - instead of + (or unary - instead of +)
1226    and/or ^ instead of |.
1227    If B is constant and (B & M) == 0, fold into A & M.  */
1228 (for op (plus minus)
1229  (for bitop (bit_and bit_ior bit_xor)
1230   (simplify
1231    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1232     (with
1233      { tree pmop[2];
1234        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1235                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1236                                        NULL_TREE, pmop); }
1237      (if (utype)
1238       (convert (bit_and (op (convert:utype { pmop[0]; })
1239                             (convert:utype { pmop[1]; }))
1240                         (convert:utype @2))))))
1241   (simplify
1242    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1243     (with
1244      { tree pmop[2];
1245        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1246                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1247                                        @4, pmop); }
1248      (if (utype)
1249       (convert (bit_and (op (convert:utype { pmop[0]; })
1250                             (convert:utype { pmop[1]; }))
1251                         (convert:utype @2)))))))
1252  (simplify
1253   (bit_and (op:s @0 @1) INTEGER_CST@2)
1254    (with
1255     { tree pmop[2];
1256       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1257                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1258                                       NULL_TREE, NULL_TREE, pmop); }
1259     (if (utype)
1260      (convert (bit_and (op (convert:utype { pmop[0]; })
1261                            (convert:utype { pmop[1]; }))
1262                        (convert:utype @2)))))))
1263 (for bitop (bit_and bit_ior bit_xor)
1264  (simplify
1265   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1266    (with
1267     { tree pmop[2];
1268       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1269                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1270                                       NULL_TREE, NULL_TREE, pmop); }
1271     (if (utype)
1272      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1273                        (convert:utype @1)))))))
1275 /* X % Y is smaller than Y.  */
1276 (for cmp (lt ge)
1277  (simplify
1278   (cmp (trunc_mod @0 @1) @1)
1279   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1280    { constant_boolean_node (cmp == LT_EXPR, type); })))
1281 (for cmp (gt le)
1282  (simplify
1283   (cmp @1 (trunc_mod @0 @1))
1284   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1285    { constant_boolean_node (cmp == GT_EXPR, type); })))
1287 /* x | ~0 -> ~0  */
1288 (simplify
1289  (bit_ior @0 integer_all_onesp@1)
1290  @1)
1292 /* x | 0 -> x  */
1293 (simplify
1294  (bit_ior @0 integer_zerop)
1295  @0)
1297 /* x & 0 -> 0  */
1298 (simplify
1299  (bit_and @0 integer_zerop@1)
1300  @1)
1302 /* ~x | x -> -1 */
1303 /* ~x ^ x -> -1 */
1304 (for op (bit_ior bit_xor)
1305  (simplify
1306   (op:c (convert? @0) (convert? (bit_not @0)))
1307   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1309 /* x ^ x -> 0 */
1310 (simplify
1311   (bit_xor @0 @0)
1312   { build_zero_cst (type); })
1314 /* Canonicalize X ^ ~0 to ~X.  */
1315 (simplify
1316   (bit_xor @0 integer_all_onesp@1)
1317   (bit_not @0))
1319 /* x & ~0 -> x  */
1320 (simplify
1321  (bit_and @0 integer_all_onesp)
1322   (non_lvalue @0))
1324 /* x & x -> x,  x | x -> x  */
1325 (for bitop (bit_and bit_ior)
1326  (simplify
1327   (bitop @0 @0)
1328   (non_lvalue @0)))
1330 /* x & C -> x if we know that x & ~C == 0.  */
1331 #if GIMPLE
1332 (simplify
1333  (bit_and SSA_NAME@0 INTEGER_CST@1)
1334  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1335       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1336   @0))
1337 #endif
1339 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1340 (simplify
1341  (bit_not (minus (bit_not @0) @1))
1342  (plus @0 @1))
1343 (simplify
1344  (bit_not (plus:c (bit_not @0) @1))
1345  (minus @0 @1))
1346 /* (~X - ~Y) -> Y - X.  */
1347 (simplify
1348  (minus (bit_not @0) (bit_not @1))
1349   (if (!TYPE_OVERFLOW_SANITIZED (type))
1350    (with { tree utype = unsigned_type_for (type); }
1351     (convert (minus (convert:utype @1) (convert:utype @0))))))
1353 /* ~(X - Y) -> ~X + Y.  */
1354 (simplify
1355  (bit_not (minus:s @0 @1))
1356  (plus (bit_not @0) @1))
1357 (simplify
1358  (bit_not (plus:s @0 INTEGER_CST@1))
1359  (if ((INTEGRAL_TYPE_P (type)
1360        && TYPE_UNSIGNED (type))
1361       || (!TYPE_OVERFLOW_SANITIZED (type)
1362           && may_negate_without_overflow_p (@1)))
1363   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1365 #if GIMPLE
1366 /* ~X + Y -> (Y - X) - 1.  */
1367 (simplify
1368  (plus:c (bit_not @0) @1)
1369   (if (ANY_INTEGRAL_TYPE_P (type)
1370        && TYPE_OVERFLOW_WRAPS (type)
1371        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1372        && !integer_all_onesp (@1))
1373    (plus (minus @1 @0) { build_minus_one_cst (type); })
1374    (if (INTEGRAL_TYPE_P (type)
1375         && TREE_CODE (@1) == INTEGER_CST
1376         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1377                                               SIGNED))
1378     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1379 #endif
1381 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1382 (simplify
1383  (bit_not (rshift:s @0 @1))
1384   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1385    (rshift (bit_not! @0) @1)
1386    /* For logical right shifts, this is possible only if @0 doesn't
1387       have MSB set and the logical right shift is changed into
1388       arithmetic shift.  */
1389    (if (INTEGRAL_TYPE_P (type)
1390         && !wi::neg_p (tree_nonzero_bits (@0)))
1391     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1392      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1394 /* x + (x & 1) -> (x + 1) & ~1 */
1395 (simplify
1396  (plus:c @0 (bit_and:s @0 integer_onep@1))
1397  (bit_and (plus @0 @1) (bit_not @1)))
1399 /* x & ~(x & y) -> x & ~y */
1400 /* x | ~(x | y) -> x | ~y  */
1401 (for bitop (bit_and bit_ior)
1402  (simplify
1403   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1404   (bitop @0 (bit_not @1))))
1406 /* (~x & y) | ~(x | y) -> ~x */
1407 (simplify
1408  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1409  @2)
1411 /* (x | y) ^ (x | ~y) -> ~x */
1412 (simplify
1413  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1414  (bit_not @0))
1416 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1417 (simplify
1418  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1419  (bit_not (bit_xor @0 @1)))
1421 /* (~x | y) ^ (x ^ y) -> x | ~y */
1422 (simplify
1423  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1424  (bit_ior @0 (bit_not @1)))
1426 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1427 (simplify
1428  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1429  (bit_not (bit_and @0 @1)))
1431 /* (x | y) & ~x -> y & ~x */
1432 /* (x & y) | ~x -> y | ~x */
1433 (for bitop (bit_and bit_ior)
1434      rbitop (bit_ior bit_and)
1435  (simplify
1436   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1437   (bitop @1 @2)))
1439 /* (x & y) ^ (x | y) -> x ^ y */
1440 (simplify
1441  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1442  (bit_xor @0 @1))
1444 /* (x ^ y) ^ (x | y) -> x & y */
1445 (simplify
1446  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1447  (bit_and @0 @1))
1449 /* (x & y) + (x ^ y) -> x | y */
1450 /* (x & y) | (x ^ y) -> x | y */
1451 /* (x & y) ^ (x ^ y) -> x | y */
1452 (for op (plus bit_ior bit_xor)
1453  (simplify
1454   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1455   (bit_ior @0 @1)))
1457 /* (x & y) + (x | y) -> x + y */
1458 (simplify
1459  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1460  (plus @0 @1))
1462 /* (x + y) - (x | y) -> x & y */
1463 (simplify
1464  (minus (plus @0 @1) (bit_ior @0 @1))
1465  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1466       && !TYPE_SATURATING (type))
1467   (bit_and @0 @1)))
1469 /* (x + y) - (x & y) -> x | y */
1470 (simplify
1471  (minus (plus @0 @1) (bit_and @0 @1))
1472  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1473       && !TYPE_SATURATING (type))
1474   (bit_ior @0 @1)))
1476 /* (x | y) - y -> (x & ~y) */
1477 (simplify
1478  (minus (bit_ior:cs @0 @1) @1)
1479  (bit_and @0 (bit_not @1)))
1481 /* (x | y) - (x ^ y) -> x & y */
1482 (simplify
1483  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1484  (bit_and @0 @1))
1486 /* (x | y) - (x & y) -> x ^ y */
1487 (simplify
1488  (minus (bit_ior @0 @1) (bit_and @0 @1))
1489  (bit_xor @0 @1))
1491 /* (x | y) & ~(x & y) -> x ^ y */
1492 (simplify
1493  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1494  (bit_xor @0 @1))
1496 /* (x | y) & (~x ^ y) -> x & y */
1497 (simplify
1498  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1499  (bit_and @0 @1))
1501 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1502 (simplify
1503  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1504  (bit_not (bit_xor @0 @1)))
1506 /* (~x | y) ^ (x | ~y) -> x ^ y */
1507 (simplify
1508  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1509  (bit_xor @0 @1))
1511 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1512 (simplify
1513  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1514                               (nop_convert2? (bit_ior @0 @1))))
1515        integer_all_onesp)
1516  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1517       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1518       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1519       && !TYPE_SATURATING (TREE_TYPE (@2)))
1520  (bit_not (convert (bit_xor @0 @1)))))
1521 (simplify
1522  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1523                                integer_all_onesp))
1524        (nop_convert3? (bit_ior @0 @1)))
1525  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1526       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1527       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1528       && !TYPE_SATURATING (TREE_TYPE (@2)))
1529  (bit_not (convert (bit_xor @0 @1)))))
1530 (simplify
1531  (minus (nop_convert1? (bit_and @0 @1))
1532        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1533                                integer_onep)))
1534  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1535       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1536       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1537       && !TYPE_SATURATING (TREE_TYPE (@2)))
1538  (bit_not (convert (bit_xor @0 @1)))))
1540 /* ~x & ~y -> ~(x | y)
1541    ~x | ~y -> ~(x & y) */
1542 (for op (bit_and bit_ior)
1543      rop (bit_ior bit_and)
1544  (simplify
1545   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1546   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1547        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1548    (bit_not (rop (convert @0) (convert @1))))))
1550 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1551    with a constant, and the two constants have no bits in common,
1552    we should treat this as a BIT_IOR_EXPR since this may produce more
1553    simplifications.  */
1554 (for op (bit_xor plus)
1555  (simplify
1556   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1557       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1558   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1559        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1560        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1561    (bit_ior (convert @4) (convert @5)))))
1563 /* (X | Y) ^ X -> Y & ~ X*/
1564 (simplify
1565  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1566  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1567   (convert (bit_and @1 (bit_not @0)))))
1569 /* Convert ~X ^ ~Y to X ^ Y.  */
1570 (simplify
1571  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1572  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1573       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1574   (bit_xor (convert @0) (convert @1))))
1576 /* Convert ~X ^ C to X ^ ~C.  */
1577 (simplify
1578  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1579  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1580   (bit_xor (convert @0) (bit_not @1))))
1582 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1583 (for opo (bit_and bit_xor)
1584      opi (bit_xor bit_and)
1585  (simplify
1586   (opo:c (opi:cs @0 @1) @1)
1587   (bit_and (bit_not @0) @1)))
1589 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1590    operands are another bit-wise operation with a common input.  If so,
1591    distribute the bit operations to save an operation and possibly two if
1592    constants are involved.  For example, convert
1593      (A | B) & (A | C) into A | (B & C)
1594    Further simplification will occur if B and C are constants.  */
1595 (for op (bit_and bit_ior bit_xor)
1596      rop (bit_ior bit_and bit_and)
1597  (simplify
1598   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1599   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1600        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1601    (rop (convert @0) (op (convert @1) (convert @2))))))
1603 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1604 /* (X & Y) & Y -> X & Y
1605    (X | Y) | Y -> X | Y  */
1606 (for op (bit_and bit_ior)
1607  (simplify
1608   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1609   @2))
1610 /* (X ^ Y) ^ Y -> X  */
1611 (simplify
1612  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1613  (convert @0))
1614 /* (X & Y) & (X & Z) -> (X & Y) & Z
1615    (X | Y) | (X | Z) -> (X | Y) | Z  */
1616 (for op (bit_and bit_ior)
1617  (simplify
1618   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1619   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1620        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1621    (if (single_use (@5) && single_use (@6))
1622     (op @3 (convert @2))
1623     (if (single_use (@3) && single_use (@4))
1624      (op (convert @1) @5))))))
1625 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1626 (simplify
1627  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1628  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1629       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1630   (bit_xor (convert @1) (convert @2))))
1632 /* Convert abs (abs (X)) into abs (X).
1633    also absu (absu (X)) into absu (X).  */
1634 (simplify
1635  (abs (abs@1 @0))
1636  @1)
1638 (simplify
1639  (absu (convert@2 (absu@1 @0)))
1640  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1641   @1))
1643 /* Convert abs[u] (-X) -> abs[u] (X).  */
1644 (simplify
1645  (abs (negate @0))
1646  (abs @0))
1648 (simplify
1649  (absu (negate @0))
1650  (absu @0))
1652 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1653 (simplify
1654  (abs tree_expr_nonnegative_p@0)
1655  @0)
1657 (simplify
1658  (absu tree_expr_nonnegative_p@0)
1659  (convert @0))
1661 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1662 (simplify
1663  (mult:c (nop_convert1?
1664           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1665                     integer_onep))
1666          (nop_convert3? @0))
1667  (if (INTEGRAL_TYPE_P (type)
1668       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1669       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1670   (if (TYPE_UNSIGNED (type))
1671    (absu @0)
1672    (abs @0)
1673   )
1677 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1678 (match negate_expr_p
1679  INTEGER_CST
1680  (if ((INTEGRAL_TYPE_P (type)
1681        && TYPE_UNSIGNED (type))
1682       || (!TYPE_OVERFLOW_SANITIZED (type)
1683           && may_negate_without_overflow_p (t)))))
1684 (match negate_expr_p
1685  FIXED_CST)
1686 (match negate_expr_p
1687  (negate @0)
1688  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1689 (match negate_expr_p
1690  REAL_CST
1691  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1692 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1693    ways.  */
1694 (match negate_expr_p
1695  VECTOR_CST
1696  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1697 (match negate_expr_p
1698  (minus @0 @1)
1699  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1700       || (FLOAT_TYPE_P (type)
1701           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1702           && !HONOR_SIGNED_ZEROS (type)))))
1704 /* (-A) * (-B) -> A * B  */
1705 (simplify
1706  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1707   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1708        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1709    (mult (convert @0) (convert (negate @1)))))
1711 /* -(A + B) -> (-B) - A.  */
1712 (simplify
1713  (negate (plus:c @0 negate_expr_p@1))
1714  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1715       && !HONOR_SIGNED_ZEROS (type))
1716   (minus (negate @1) @0)))
1718 /* -(A - B) -> B - A.  */
1719 (simplify
1720  (negate (minus @0 @1))
1721  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1722       || (FLOAT_TYPE_P (type)
1723           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1724           && !HONOR_SIGNED_ZEROS (type)))
1725   (minus @1 @0)))
1726 (simplify
1727  (negate (pointer_diff @0 @1))
1728  (if (TYPE_OVERFLOW_UNDEFINED (type))
1729   (pointer_diff @1 @0)))
1731 /* A - B -> A + (-B) if B is easily negatable.  */
1732 (simplify
1733  (minus @0 negate_expr_p@1)
1734  (if (!FIXED_POINT_TYPE_P (type))
1735  (plus @0 (negate @1))))
1737 /* 1 - a is a ^ 1 if a had a bool range. */
1738 /* This is only enabled for gimple as sometimes
1739    cfun is not set for the function which contains
1740    the SSA_NAME (e.g. while IPA passes are happening,
1741    fold might be called).  */
1742 (simplify
1743  (minus integer_onep@0 SSA_NAME@1)
1744   (if (INTEGRAL_TYPE_P (type)
1745        && ssa_name_has_boolean_range (@1))
1746    (bit_xor @1 @0)))
1748 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
1749 (simplify
1750  (negate (mult:c@0 @1 negate_expr_p@2))
1751  (if (! TYPE_UNSIGNED (type)
1752       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1753       && single_use (@0))
1754   (mult @1 (negate @2))))
1756 (simplify
1757  (negate (rdiv@0 @1 negate_expr_p@2))
1758  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1759       && single_use (@0))
1760   (rdiv @1 (negate @2))))
1762 (simplify
1763  (negate (rdiv@0 negate_expr_p@1 @2))
1764  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1765       && single_use (@0))
1766   (rdiv (negate @1) @2)))
1768 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
1769 (simplify
1770  (negate (convert? (rshift @0 INTEGER_CST@1)))
1771  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1772       && wi::to_wide (@1) == element_precision (type) - 1)
1773   (with { tree stype = TREE_TYPE (@0);
1774           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1775                                              : unsigned_type_for (stype); }
1776    (if (VECTOR_TYPE_P (type))
1777     (view_convert (rshift (view_convert:ntype @0) @1))
1778     (convert (rshift (convert:ntype @0) @1))))))
1780 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1781    when profitable.
1782    For bitwise binary operations apply operand conversions to the
1783    binary operation result instead of to the operands.  This allows
1784    to combine successive conversions and bitwise binary operations.
1785    We combine the above two cases by using a conditional convert.  */
1786 (for bitop (bit_and bit_ior bit_xor)
1787  (simplify
1788   (bitop (convert@2 @0) (convert?@3 @1))
1789   (if (((TREE_CODE (@1) == INTEGER_CST
1790          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1791          && (int_fits_type_p (@1, TREE_TYPE (@0))
1792              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1793         || types_match (@0, @1))
1794        && !POINTER_TYPE_P (TREE_TYPE (@0))
1795        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
1796        /* ???  This transform conflicts with fold-const.cc doing
1797           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1798           constants (if x has signed type, the sign bit cannot be set
1799           in c).  This folds extension into the BIT_AND_EXPR.
1800           Restrict it to GIMPLE to avoid endless recursions.  */
1801        && (bitop != BIT_AND_EXPR || GIMPLE)
1802        && (/* That's a good idea if the conversion widens the operand, thus
1803               after hoisting the conversion the operation will be narrower.
1804               It is also a good if the conversion is a nop as moves the
1805               conversion to one side; allowing for combining of the conversions.  */
1806            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1807            /* The conversion check for being a nop can only be done at the gimple
1808               level as fold_binary has some re-association code which can conflict
1809               with this if there is a "constant" which is not a full INTEGER_CST.  */
1810            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1811            /* It's also a good idea if the conversion is to a non-integer
1812               mode.  */
1813            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1814            /* Or if the precision of TO is not the same as the precision
1815               of its mode.  */
1816            || !type_has_mode_precision_p (type)
1817            /* In GIMPLE, getting rid of 2 conversions for one new results
1818               in smaller IL.  */
1819            || (GIMPLE
1820                && TREE_CODE (@1) != INTEGER_CST
1821                && tree_nop_conversion_p (type, TREE_TYPE (@0))
1822                && single_use (@2)
1823                && single_use (@3))))
1824    (convert (bitop @0 (convert @1)))))
1825  /* In GIMPLE, getting rid of 2 conversions for one new results
1826     in smaller IL.  */
1827  (simplify
1828   (convert (bitop:cs@2 (nop_convert:s @0) @1))
1829   (if (GIMPLE
1830        && TREE_CODE (@1) != INTEGER_CST
1831        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1832        && types_match (type, @0)
1833        && !POINTER_TYPE_P (TREE_TYPE (@0))
1834        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
1835    (bitop @0 (convert @1)))))
1837 (for bitop (bit_and bit_ior)
1838      rbitop (bit_ior bit_and)
1839   /* (x | y) & x -> x */
1840   /* (x & y) | x -> x */
1841  (simplify
1842   (bitop:c (rbitop:c @0 @1) @0)
1843   @0)
1844  /* (~x | y) & x -> x & y */
1845  /* (~x & y) | x -> x | y */
1846  (simplify
1847   (bitop:c (rbitop:c (bit_not @0) @1) @0)
1848   (bitop @0 @1)))
1850 /* ((x | y) & z) | x -> (z & y) | x */
1851 (simplify
1852   (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1853   (bit_ior (bit_and @2 @1) @0))
1855 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1856 (simplify
1857   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1858   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1860 /* Combine successive equal operations with constants.  */
1861 (for bitop (bit_and bit_ior bit_xor)
1862  (simplify
1863   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1864   (if (!CONSTANT_CLASS_P (@0))
1865    /* This is the canonical form regardless of whether (bitop @1 @2) can be
1866       folded to a constant.  */
1867    (bitop @0 (bitop @1 @2))
1868    /* In this case we have three constants and (bitop @0 @1) doesn't fold
1869       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
1870       the values involved are such that the operation can't be decided at
1871       compile time.  Try folding one of @0 or @1 with @2 to see whether
1872       that combination can be decided at compile time.
1874       Keep the existing form if both folds fail, to avoid endless
1875       oscillation.  */
1876    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1877     (if (cst1)
1878      (bitop @1 { cst1; })
1879      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1880       (if (cst2)
1881        (bitop @0 { cst2; }))))))))
1883 /* Try simple folding for X op !X, and X op X with the help
1884    of the truth_valued_p and logical_inverted_value predicates.  */
1885 (match truth_valued_p
1886  @0
1887  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1888 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1889  (match truth_valued_p
1890   (op @0 @1)))
1891 (match truth_valued_p
1892   (truth_not @0))
1894 (match (logical_inverted_value @0)
1895  (truth_not @0))
1896 (match (logical_inverted_value @0)
1897  (bit_not truth_valued_p@0))
1898 (match (logical_inverted_value @0)
1899  (eq @0 integer_zerop))
1900 (match (logical_inverted_value @0)
1901  (ne truth_valued_p@0 integer_truep))
1902 (match (logical_inverted_value @0)
1903  (bit_xor truth_valued_p@0 integer_truep))
1905 /* X & !X -> 0.  */
1906 (simplify
1907  (bit_and:c @0 (logical_inverted_value @0))
1908  { build_zero_cst (type); })
1909 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
1910 (for op (bit_ior bit_xor)
1911  (simplify
1912   (op:c truth_valued_p@0 (logical_inverted_value @0))
1913   { constant_boolean_node (true, type); }))
1914 /* X ==/!= !X is false/true.  */
1915 (for op (eq ne)
1916  (simplify
1917   (op:c truth_valued_p@0 (logical_inverted_value @0))
1918   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1920 /* ~~x -> x */
1921 (simplify
1922   (bit_not (bit_not @0))
1923   @0)
1925 (match zero_one_valued_p
1926  @0
1927  (if (INTEGRAL_TYPE_P (type) && tree_nonzero_bits (@0) == 1)))
1928 (match zero_one_valued_p
1929  truth_valued_p@0)
1931 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
1932 (simplify
1933  (mult zero_one_valued_p@0 zero_one_valued_p@1)
1934  (if (INTEGRAL_TYPE_P (type))
1935   (bit_and @0 @1)))
1937 (for cmp (tcc_comparison)
1938      icmp (inverted_tcc_comparison)
1939  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
1940  (simplify
1941   (bit_ior
1942    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
1943    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
1944     (if (INTEGRAL_TYPE_P (type)
1945          /* The scalar version has to be canonicalized after vectorization
1946             because it makes unconditional loads conditional ones, which
1947             means we lose vectorization because the loads may trap.  */
1948          && canonicalize_math_after_vectorization_p ())
1949      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
1951  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
1952     canonicalized further and we recognize the conditional form:
1953     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
1954  (simplify
1955   (bit_ior
1956    (cond (cmp@0  @01 @02) @3 zerop)
1957    (cond (icmp@4 @01 @02) @5 zerop))
1958     (if (INTEGRAL_TYPE_P (type)
1959          /* The scalar version has to be canonicalized after vectorization
1960             because it makes unconditional loads conditional ones, which
1961             means we lose vectorization because the loads may trap.  */
1962          && canonicalize_math_after_vectorization_p ())
1963     (cond @0 @3 @5)))
1965  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
1966     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
1967  (simplify
1968   (bit_ior
1969    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
1970    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
1971     (if (integer_zerop (@5))
1972      (switch
1973       (if (integer_onep (@4))
1974        (bit_and (vec_cond @0 @2 @3) @4))
1975         (if (integer_minus_onep (@4))
1976          (vec_cond @0 @2 @3)))
1977     (if (integer_zerop (@4))
1978      (switch
1979       (if (integer_onep (@5))
1980        (bit_and (vec_cond @0 @3 @2) @5))
1981       (if (integer_minus_onep (@5))
1982        (vec_cond @0 @3 @2))))))
1984  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
1985     into a < b ? d : c.  */
1986  (simplify
1987   (bit_ior
1988    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
1989    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
1990     (vec_cond @0 @2 @3)))
1992 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
1993 (simplify
1994  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
1995  (if (INTEGRAL_TYPE_P (type)
1996       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1997       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
1998       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1999   (mult (convert @0) @1)))
2001 /* Narrow integer multiplication by a zero_one_valued_p operand.
2002    Multiplication by [0,1] is guaranteed not to overflow.  */
2003 (simplify
2004  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2005  (if (INTEGRAL_TYPE_P (type)
2006       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2007       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2008   (mult (convert @1) (convert @2))))
2010 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2011    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2012    as some targets (such as x86's SSE) may return zero for larger C.  */
2013 (simplify
2014   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2015   (if (tree_fits_shwi_p (@1)
2016        && tree_to_shwi (@1) > 0
2017        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2018     (convert @0)))
2020 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2021    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2022    as some targets (such as x86's SSE) may return zero for larger C.  */
2023 (simplify
2024   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2025   (if (tree_fits_shwi_p (@1)
2026        && tree_to_shwi (@1) > 0
2027        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2028     (eq @0 @2)))
2030 /* Convert ~ (-A) to A - 1.  */
2031 (simplify
2032  (bit_not (convert? (negate @0)))
2033  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2034       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2035   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2037 /* Convert - (~A) to A + 1.  */
2038 (simplify
2039  (negate (nop_convert? (bit_not @0)))
2040  (plus (view_convert @0) { build_each_one_cst (type); }))
2042 /* (a & b) ^ (a == b) -> !(a | b) */
2043 /* (a & b) == (a ^ b) -> !(a | b) */
2044 (for first_op (bit_xor eq)
2045      second_op (eq bit_xor)
2046  (simplify
2047   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2048     (bit_not (bit_ior @0 @1))))
2050 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2051 (simplify
2052  (bit_not (convert? (minus @0 integer_each_onep)))
2053  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2054       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2055   (convert (negate @0))))
2056 (simplify
2057  (bit_not (convert? (plus @0 integer_all_onesp)))
2058  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2059       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2060   (convert (negate @0))))
2062 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2063 (simplify
2064  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2065  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2066   (convert (bit_xor @0 (bit_not @1)))))
2067 (simplify
2068  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2069  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2070   (convert (bit_xor @0 @1))))
2072 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2073 (simplify
2074  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2075  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2076   (bit_not (bit_xor (view_convert @0) @1))))
2078 /* ~(a ^ b) is a == b for truth valued a and b.  */
2079 (simplify
2080  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2081  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2082       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2083   (convert (eq @0 @1))))
2085 /* (~a) == b is a ^ b for truth valued a and b.  */
2086 (simplify
2087  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2088  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2089       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2090   (convert (bit_xor @0 @1))))
2092 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2093 (simplify
2094  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2095  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2097 /* Fold A - (A & B) into ~B & A.  */
2098 (simplify
2099  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2100  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2101       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2102   (convert (bit_and (bit_not @1) @0))))
2104 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2105 (if (!canonicalize_math_p ())
2106  (for cmp (tcc_comparison)
2107   (simplify
2108    (mult:c (convert (cmp@0 @1 @2)) @3)
2109    (if (INTEGRAL_TYPE_P (type)
2110         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2111      (cond @0 @3 { build_zero_cst (type); })))
2112 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2113   (simplify
2114    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2115    (if (INTEGRAL_TYPE_P (type)
2116         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2117      (cond @0 @3 { build_zero_cst (type); })))
2121 /* For integral types with undefined overflow and C != 0 fold
2122    x * C EQ/NE y * C into x EQ/NE y.  */
2123 (for cmp (eq ne)
2124  (simplify
2125   (cmp (mult:c @0 @1) (mult:c @2 @1))
2126   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2127        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2128        && tree_expr_nonzero_p (@1))
2129    (cmp @0 @2))))
2131 /* For integral types with wrapping overflow and C odd fold
2132    x * C EQ/NE y * C into x EQ/NE y.  */
2133 (for cmp (eq ne)
2134  (simplify
2135   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2136   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2137        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2138        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2139    (cmp @0 @2))))
2141 /* For integral types with undefined overflow and C != 0 fold
2142    x * C RELOP y * C into:
2144    x RELOP y for nonnegative C
2145    y RELOP x for negative C  */
2146 (for cmp (lt gt le ge)
2147  (simplify
2148   (cmp (mult:c @0 @1) (mult:c @2 @1))
2149   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2150        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2151    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2152     (cmp @0 @2)
2153    (if (TREE_CODE (@1) == INTEGER_CST
2154         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2155     (cmp @2 @0))))))
2157 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2158 (for cmp (le gt)
2159      icmp (gt le)
2160  (simplify
2161   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2162    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2163         && TYPE_UNSIGNED (TREE_TYPE (@0))
2164         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2165         && (wi::to_wide (@2)
2166             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2167     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2168      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2170 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2171 (for cmp (simple_comparison)
2172  (simplify
2173   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2174   (if (element_precision (@3) >= element_precision (@0)
2175        && types_match (@0, @1))
2176    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2177     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2178      (cmp @1 @0)
2179      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2180       (with
2181        {
2182         tree utype = unsigned_type_for (TREE_TYPE (@0));
2183        }
2184        (cmp (convert:utype @1) (convert:utype @0)))))
2185     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2186      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2187       (cmp @0 @1)
2188       (with
2189        {
2190         tree utype = unsigned_type_for (TREE_TYPE (@0));
2191        }
2192        (cmp (convert:utype @0) (convert:utype @1)))))))))
2194 /* X / C1 op C2 into a simple range test.  */
2195 (for cmp (simple_comparison)
2196  (simplify
2197   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2198   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2199        && integer_nonzerop (@1)
2200        && !TREE_OVERFLOW (@1)
2201        && !TREE_OVERFLOW (@2))
2202    (with { tree lo, hi; bool neg_overflow;
2203            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2204                                                    &neg_overflow); }
2205     (switch
2206      (if (code == LT_EXPR || code == GE_EXPR)
2207        (if (TREE_OVERFLOW (lo))
2208         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2209         (if (code == LT_EXPR)
2210          (lt @0 { lo; })
2211          (ge @0 { lo; }))))
2212      (if (code == LE_EXPR || code == GT_EXPR)
2213        (if (TREE_OVERFLOW (hi))
2214         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2215         (if (code == LE_EXPR)
2216          (le @0 { hi; })
2217          (gt @0 { hi; }))))
2218      (if (!lo && !hi)
2219       { build_int_cst (type, code == NE_EXPR); })
2220      (if (code == EQ_EXPR && !hi)
2221       (ge @0 { lo; }))
2222      (if (code == EQ_EXPR && !lo)
2223       (le @0 { hi; }))
2224      (if (code == NE_EXPR && !hi)
2225       (lt @0 { lo; }))
2226      (if (code == NE_EXPR && !lo)
2227       (gt @0 { hi; }))
2228      (if (GENERIC)
2229       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2230                            lo, hi); })
2231      (with
2232       {
2233         tree etype = range_check_type (TREE_TYPE (@0));
2234         if (etype)
2235           {
2236             hi = fold_convert (etype, hi);
2237             lo = fold_convert (etype, lo);
2238             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2239           }
2240       }
2241       (if (etype && hi && !TREE_OVERFLOW (hi))
2242        (if (code == EQ_EXPR)
2243         (le (minus (convert:etype @0) { lo; }) { hi; })
2244         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2246 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2247 (for op (lt le ge gt)
2248  (simplify
2249   (op (plus:c @0 @2) (plus:c @1 @2))
2250   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2251        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2252    (op @0 @1))))
2254 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2255    when C is an unsigned integer constant with only the MSB set, and X and
2256    Y have types of equal or lower integer conversion rank than C's.  */
2257 (for op (lt le ge gt)
2258  (simplify
2259   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2260   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2261        && TYPE_UNSIGNED (TREE_TYPE (@0))
2262        && wi::only_sign_bit_p (wi::to_wide (@0)))
2263    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2264     (op (convert:stype @1) (convert:stype @2))))))
2266 /* For equality and subtraction, this is also true with wrapping overflow.  */
2267 (for op (eq ne minus)
2268  (simplify
2269   (op (plus:c @0 @2) (plus:c @1 @2))
2270   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2271        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2272            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2273    (op @0 @1))))
2275 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2276 (for op (lt le ge gt)
2277  (simplify
2278   (op (minus @0 @2) (minus @1 @2))
2279   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2280        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2281    (op @0 @1))))
2282 /* For equality and subtraction, this is also true with wrapping overflow.  */
2283 (for op (eq ne minus)
2284  (simplify
2285   (op (minus @0 @2) (minus @1 @2))
2286   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2287        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2288            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2289    (op @0 @1))))
2290 /* And for pointers...  */
2291 (for op (simple_comparison)
2292  (simplify
2293   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2294   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2295    (op @0 @1))))
2296 (simplify
2297  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2298  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2299       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2300   (pointer_diff @0 @1)))
2302 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2303 (for op (lt le ge gt)
2304  (simplify
2305   (op (minus @2 @0) (minus @2 @1))
2306   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2307        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2308    (op @1 @0))))
2309 /* For equality and subtraction, this is also true with wrapping overflow.  */
2310 (for op (eq ne minus)
2311  (simplify
2312   (op (minus @2 @0) (minus @2 @1))
2313   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2314        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2315            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2316    (op @1 @0))))
2317 /* And for pointers...  */
2318 (for op (simple_comparison)
2319  (simplify
2320   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2321   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2322    (op @1 @0))))
2323 (simplify
2324  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2325  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2326       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2327   (pointer_diff @1 @0)))
2329 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2330 (for op (lt le gt ge)
2331  (simplify
2332   (op:c (plus:c@2 @0 @1) @1)
2333   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2334        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2335        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2336        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2337    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2338 /* For equality, this is also true with wrapping overflow.  */
2339 (for op (eq ne)
2340  (simplify
2341   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2342   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2343        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2344            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2345        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2346        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2347        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2348    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2349  (simplify
2350   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2351   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2352        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2353        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2354    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2356 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2357 (for neeq (ne eq)
2358  (simplify
2359   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2360    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2361     (if (ptr_difference_const (@0, @2, &diff))
2362      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2363  (simplify
2364   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2365    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2366     (if (ptr_difference_const (@0, @2, &diff))
2367      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2369 /* X - Y < X is the same as Y > 0 when there is no overflow.
2370    For equality, this is also true with wrapping overflow.  */
2371 (for op (simple_comparison)
2372  (simplify
2373   (op:c @0 (minus@2 @0 @1))
2374   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2375        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2376            || ((op == EQ_EXPR || op == NE_EXPR)
2377                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2378        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2379    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2381 /* Transform:
2382    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2383    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2384 (for cmp (eq ne)
2385      ocmp (lt ge)
2386  (simplify
2387   (cmp (trunc_div @0 @1) integer_zerop)
2388   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2389        /* Complex ==/!= is allowed, but not </>=.  */
2390        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2391        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2392    (ocmp @0 @1))))
2394 /* X == C - X can never be true if C is odd.  */
2395 (for cmp (eq ne)
2396  (simplify
2397   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2398   (if (TREE_INT_CST_LOW (@1) & 1)
2399    { constant_boolean_node (cmp == NE_EXPR, type); })))
2401 /* Arguments on which one can call get_nonzero_bits to get the bits
2402    possibly set.  */
2403 (match with_possible_nonzero_bits
2404  INTEGER_CST@0)
2405 (match with_possible_nonzero_bits
2406  SSA_NAME@0
2407  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2408 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2409 (match (with_possible_nonzero_bits2 @0)
2410  with_possible_nonzero_bits@0)
2411 (match (with_possible_nonzero_bits2 @0)
2412  (bit_and:c with_possible_nonzero_bits@0 @2))
2414 /* Same for bits that are known to be set, but we do not have
2415    an equivalent to get_nonzero_bits yet.  */
2416 (match (with_certain_nonzero_bits2 @0)
2417  INTEGER_CST@0)
2418 (match (with_certain_nonzero_bits2 @0)
2419  (bit_ior @1 INTEGER_CST@0))
2421 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2422 (for cmp (eq ne)
2423  (simplify
2424   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2425   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2426    { constant_boolean_node (cmp == NE_EXPR, type); })))
2428 /* ((X inner_op C0) outer_op C1)
2429    With X being a tree where value_range has reasoned certain bits to always be
2430    zero throughout its computed value range,
2431    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2432    where zero_mask has 1's for all bits that are sure to be 0 in
2433    and 0's otherwise.
2434    if (inner_op == '^') C0 &= ~C1;
2435    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2436    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2438 (for inner_op (bit_ior bit_xor)
2439      outer_op (bit_xor bit_ior)
2440 (simplify
2441  (outer_op
2442   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2443  (with
2444   {
2445     bool fail = false;
2446     wide_int zero_mask_not;
2447     wide_int C0;
2448     wide_int cst_emit;
2450     if (TREE_CODE (@2) == SSA_NAME)
2451       zero_mask_not = get_nonzero_bits (@2);
2452     else
2453       fail = true;
2455     if (inner_op == BIT_XOR_EXPR)
2456       {
2457         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2458         cst_emit = C0 | wi::to_wide (@1);
2459       }
2460     else
2461       {
2462         C0 = wi::to_wide (@0);
2463         cst_emit = C0 ^ wi::to_wide (@1);
2464       }
2465   }
2466   (if (!fail && (C0 & zero_mask_not) == 0)
2467    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2468    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2469     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2471 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2472 (simplify
2473   (pointer_plus (pointer_plus:s @0 @1) @3)
2474   (pointer_plus @0 (plus @1 @3)))
2475 #if GENERIC
2476 (simplify
2477   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2478   (convert:type (pointer_plus @0 (plus @1 @3))))
2479 #endif
2481 /* Pattern match
2482      tem1 = (long) ptr1;
2483      tem2 = (long) ptr2;
2484      tem3 = tem2 - tem1;
2485      tem4 = (unsigned long) tem3;
2486      tem5 = ptr1 + tem4;
2487    and produce
2488      tem5 = ptr2;  */
2489 (simplify
2490   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2491   /* Conditionally look through a sign-changing conversion.  */
2492   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2493        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2494             || (GENERIC && type == TREE_TYPE (@1))))
2495    @1))
2496 (simplify
2497   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2498   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2499    (convert @1)))
2501 /* Pattern match
2502      tem = (sizetype) ptr;
2503      tem = tem & algn;
2504      tem = -tem;
2505      ... = ptr p+ tem;
2506    and produce the simpler and easier to analyze with respect to alignment
2507      ... = ptr & ~algn;  */
2508 (simplify
2509   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2510   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2511    (bit_and @0 { algn; })))
2513 /* Try folding difference of addresses.  */
2514 (simplify
2515  (minus (convert ADDR_EXPR@0) (convert @1))
2516  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2517   (with { poly_int64 diff; }
2518    (if (ptr_difference_const (@0, @1, &diff))
2519     { build_int_cst_type (type, diff); }))))
2520 (simplify
2521  (minus (convert @0) (convert ADDR_EXPR@1))
2522  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2523   (with { poly_int64 diff; }
2524    (if (ptr_difference_const (@0, @1, &diff))
2525     { build_int_cst_type (type, diff); }))))
2526 (simplify
2527  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2528  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2529       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2530   (with { poly_int64 diff; }
2531    (if (ptr_difference_const (@0, @1, &diff))
2532     { build_int_cst_type (type, diff); }))))
2533 (simplify
2534  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2535  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2536       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2537   (with { poly_int64 diff; }
2538    (if (ptr_difference_const (@0, @1, &diff))
2539     { build_int_cst_type (type, diff); }))))
2541 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2542 (simplify
2543  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2544  (with { poly_int64 diff; }
2545    (if (ptr_difference_const (@0, @2, &diff))
2546     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2547 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2548 (simplify
2549  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2550  (with { poly_int64 diff; }
2551    (if (ptr_difference_const (@0, @2, &diff))
2552     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2553 (simplify
2554  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2555  (with { poly_int64 diff; }
2556    (if (ptr_difference_const (@0, @1, &diff))
2557     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2559 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2560 (simplify
2561  (convert (pointer_diff @0 INTEGER_CST@1))
2562  (if (POINTER_TYPE_P (type))
2563   { build_fold_addr_expr_with_type
2564       (build2 (MEM_REF, char_type_node, @0,
2565                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2566                type); }))
2568 /* If arg0 is derived from the address of an object or function, we may
2569    be able to fold this expression using the object or function's
2570    alignment.  */
2571 (simplify
2572  (bit_and (convert? @0) INTEGER_CST@1)
2573  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2574       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2575   (with
2576    {
2577      unsigned int align;
2578      unsigned HOST_WIDE_INT bitpos;
2579      get_pointer_alignment_1 (@0, &align, &bitpos);
2580    }
2581    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2582     { wide_int_to_tree (type, (wi::to_wide (@1)
2583                                & (bitpos / BITS_PER_UNIT))); }))))
2585 (match min_value
2586  INTEGER_CST
2587  (if (INTEGRAL_TYPE_P (type)
2588       && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2590 (match max_value
2591  INTEGER_CST
2592  (if (INTEGRAL_TYPE_P (type)
2593       && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2595 /* x >  y  &&  x != XXX_MIN  -->  x > y
2596    x >  y  &&  x == XXX_MIN  -->  false . */
2597 (for eqne (eq ne)
2598  (simplify
2599   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2600    (switch
2601     (if (eqne == EQ_EXPR)
2602      { constant_boolean_node (false, type); })
2603     (if (eqne == NE_EXPR)
2604      @2)
2605     )))
2607 /* x <  y  &&  x != XXX_MAX  -->  x < y
2608    x <  y  &&  x == XXX_MAX  -->  false.  */
2609 (for eqne (eq ne)
2610  (simplify
2611   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2612    (switch
2613     (if (eqne == EQ_EXPR)
2614      { constant_boolean_node (false, type); })
2615     (if (eqne == NE_EXPR)
2616      @2)
2617     )))
2619 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2620 (simplify
2621  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2622   @2)
2624 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2625 (simplify
2626  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2627   @2)
2629 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2630 (simplify
2631  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2632   @2)
2634 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2635 (simplify
2636  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2637   { constant_boolean_node (true, type); })
2639 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2640 (simplify
2641  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2642   @2)
2644 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2645 (simplify
2646  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2647   @2)
2649 /* x >=  y  ||  x != XXX_MAX   -->  true
2650    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2651 (for eqne (eq ne)
2652  (simplify
2653   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2654    (switch
2655     (if (eqne == EQ_EXPR)
2656      @2)
2657     (if (eqne == NE_EXPR)
2658      { constant_boolean_node (true, type); }))))
2660 /* y == XXX_MIN || x < y --> x <= y - 1 */
2661 (simplify
2662  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2663   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2664        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2665   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2667 /* y != XXX_MIN && x >= y --> x > y - 1 */
2668 (simplify
2669  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2670   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2671        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2672   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2674 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2675    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2677 (for code1 (eq ne)
2678  (for code2 (eq ne lt gt le ge)
2679   (simplify
2680    (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2681     (with
2682      {
2683       int cmp = tree_int_cst_compare (@1, @2);
2684       bool val;
2685       switch (code2)
2686          {
2687         case EQ_EXPR: val = (cmp == 0); break;
2688         case NE_EXPR: val = (cmp != 0); break;
2689         case LT_EXPR: val = (cmp < 0); break;
2690         case GT_EXPR: val = (cmp > 0); break;
2691         case LE_EXPR: val = (cmp <= 0); break;
2692         case GE_EXPR: val = (cmp >= 0); break;
2693         default: gcc_unreachable ();
2694         }
2695      }
2696      (switch
2697       (if (code1 == EQ_EXPR && val) @3)
2698       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2699       (if (code1 == NE_EXPR && !val) @4))))))
2701 /* Convert (X OP1 CST1) && (X OP2 CST2).  */
2703 (for code1 (lt le gt ge)
2704  (for code2 (lt le gt ge)
2705   (simplify
2706   (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2707    (with
2708     {
2709      int cmp = tree_int_cst_compare (@1, @2);
2710     }
2711     (switch
2712      /* Choose the more restrictive of two < or <= comparisons.  */
2713      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2714           && (code2 == LT_EXPR || code2 == LE_EXPR))
2715       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2716        @3
2717        @4))
2718      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2719      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2720           && (code2 == GT_EXPR || code2 == GE_EXPR))
2721       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2722        @3
2723        @4))
2724      /* Check for singleton ranges.  */
2725      (if (cmp == 0
2726           && ((code1 == LE_EXPR && code2 == GE_EXPR)
2727             || (code1 == GE_EXPR && code2 == LE_EXPR)))
2728       (eq @0 @1))
2729      /* Check for disjoint ranges.  */
2730      (if (cmp <= 0
2731           && (code1 == LT_EXPR || code1 == LE_EXPR)
2732           && (code2 == GT_EXPR || code2 == GE_EXPR))
2733       { constant_boolean_node (false, type); })
2734      (if (cmp >= 0
2735           && (code1 == GT_EXPR || code1 == GE_EXPR)
2736           && (code2 == LT_EXPR || code2 == LE_EXPR))
2737       { constant_boolean_node (false, type); })
2738      )))))
2740 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2741    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2743 (for code1 (eq ne)
2744  (for code2 (eq ne lt gt le ge)
2745   (simplify
2746    (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2747     (with
2748      {
2749       int cmp = tree_int_cst_compare (@1, @2);
2750       bool val;
2751       switch (code2)
2752         {
2753         case EQ_EXPR: val = (cmp == 0); break;
2754         case NE_EXPR: val = (cmp != 0); break;
2755         case LT_EXPR: val = (cmp < 0); break;
2756         case GT_EXPR: val = (cmp > 0); break;
2757         case LE_EXPR: val = (cmp <= 0); break;
2758         case GE_EXPR: val = (cmp >= 0); break;
2759         default: gcc_unreachable ();
2760         }
2761      }
2762      (switch
2763       (if (code1 == EQ_EXPR && val) @4)
2764       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2765       (if (code1 == NE_EXPR && !val) @3))))))
2767 /* Convert (X OP1 CST1) || (X OP2 CST2).  */
2769 (for code1 (lt le gt ge)
2770  (for code2 (lt le gt ge)
2771   (simplify
2772   (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2773    (with
2774     {
2775      int cmp = tree_int_cst_compare (@1, @2);
2776     }
2777     (switch
2778      /* Choose the more restrictive of two < or <= comparisons.  */
2779      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2780           && (code2 == LT_EXPR || code2 == LE_EXPR))
2781       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2782        @4
2783        @3))
2784      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2785      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2786           && (code2 == GT_EXPR || code2 == GE_EXPR))
2787       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2788        @4
2789        @3))
2790      /* Check for singleton ranges.  */
2791      (if (cmp == 0
2792           && ((code1 == LT_EXPR && code2 == GT_EXPR)
2793               || (code1 == GT_EXPR && code2 == LT_EXPR)))
2794       (ne @0 @2))
2795      /* Check for disjoint ranges.  */
2796      (if (cmp >= 0
2797           && (code1 == LT_EXPR || code1 == LE_EXPR)
2798           && (code2 == GT_EXPR || code2 == GE_EXPR))
2799       { constant_boolean_node (true, type); })
2800      (if (cmp <= 0
2801           && (code1 == GT_EXPR || code1 == GE_EXPR)
2802           && (code2 == LT_EXPR || code2 == LE_EXPR))
2803       { constant_boolean_node (true, type); })
2804      )))))
2806 /* We can't reassociate at all for saturating types.  */
2807 (if (!TYPE_SATURATING (type))
2809  /* Contract negates.  */
2810  /* A + (-B) -> A - B */
2811  (simplify
2812   (plus:c @0 (convert? (negate @1)))
2813   /* Apply STRIP_NOPS on the negate.  */
2814   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2815        && !TYPE_OVERFLOW_SANITIZED (type))
2816    (with
2817     {
2818      tree t1 = type;
2819      if (INTEGRAL_TYPE_P (type)
2820          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2821        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2822     }
2823     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2824  /* A - (-B) -> A + B */
2825  (simplify
2826   (minus @0 (convert? (negate @1)))
2827   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2828        && !TYPE_OVERFLOW_SANITIZED (type))
2829    (with
2830     {
2831      tree t1 = type;
2832      if (INTEGRAL_TYPE_P (type)
2833          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2834        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2835     }
2836     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2837  /* -(T)(-A) -> (T)A
2838     Sign-extension is ok except for INT_MIN, which thankfully cannot
2839     happen without overflow.  */
2840  (simplify
2841   (negate (convert (negate @1)))
2842   (if (INTEGRAL_TYPE_P (type)
2843        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2844            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2845                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2846        && !TYPE_OVERFLOW_SANITIZED (type)
2847        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2848    (convert @1)))
2849  (simplify
2850   (negate (convert negate_expr_p@1))
2851   (if (SCALAR_FLOAT_TYPE_P (type)
2852        && ((DECIMAL_FLOAT_TYPE_P (type)
2853             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2854             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2855            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2856    (convert (negate @1))))
2857  (simplify
2858   (negate (nop_convert? (negate @1)))
2859   (if (!TYPE_OVERFLOW_SANITIZED (type)
2860        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2861    (view_convert @1)))
2863  /* We can't reassociate floating-point unless -fassociative-math
2864     or fixed-point plus or minus because of saturation to +-Inf.  */
2865  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2866       && !FIXED_POINT_TYPE_P (type))
2868   /* Match patterns that allow contracting a plus-minus pair
2869      irrespective of overflow issues.  */
2870   /* (A +- B) - A       ->  +- B */
2871   /* (A +- B) -+ B      ->  A */
2872   /* A - (A +- B)       -> -+ B */
2873   /* A +- (B -+ A)      ->  +- B */
2874   (simplify
2875    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2876    (view_convert @1))
2877   (simplify
2878    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2879    (if (!ANY_INTEGRAL_TYPE_P (type)
2880         || TYPE_OVERFLOW_WRAPS (type))
2881    (negate (view_convert @1))
2882    (view_convert (negate @1))))
2883   (simplify
2884    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2885    (view_convert @0))
2886   (simplify
2887    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2888     (if (!ANY_INTEGRAL_TYPE_P (type)
2889          || TYPE_OVERFLOW_WRAPS (type))
2890      (negate (view_convert @1))
2891      (view_convert (negate @1))))
2892   (simplify
2893    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2894    (view_convert @1))
2895   /* (A +- B) + (C - A)   -> C +- B */
2896   /* (A +  B) - (A - C)   -> B + C */
2897   /* More cases are handled with comparisons.  */
2898   (simplify
2899    (plus:c (plus:c @0 @1) (minus @2 @0))
2900    (plus @2 @1))
2901   (simplify
2902    (plus:c (minus @0 @1) (minus @2 @0))
2903    (minus @2 @1))
2904   (simplify
2905    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2906    (if (TYPE_OVERFLOW_UNDEFINED (type)
2907         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2908     (pointer_diff @2 @1)))
2909   (simplify
2910    (minus (plus:c @0 @1) (minus @0 @2))
2911    (plus @1 @2))
2913   /* (A +- CST1) +- CST2 -> A + CST3
2914      Use view_convert because it is safe for vectors and equivalent for
2915      scalars.  */
2916   (for outer_op (plus minus)
2917    (for inner_op (plus minus)
2918         neg_inner_op (minus plus)
2919     (simplify
2920      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2921                CONSTANT_CLASS_P@2)
2922      /* If one of the types wraps, use that one.  */
2923      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2924       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2925          forever if something doesn't simplify into a constant.  */
2926       (if (!CONSTANT_CLASS_P (@0))
2927        (if (outer_op == PLUS_EXPR)
2928         (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2929         (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2930       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2931            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2932        (if (outer_op == PLUS_EXPR)
2933         (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2934         (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2935        /* If the constant operation overflows we cannot do the transform
2936           directly as we would introduce undefined overflow, for example
2937           with (a - 1) + INT_MIN.  */
2938        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
2939         (with { tree cst = const_binop (outer_op == inner_op
2940                                         ? PLUS_EXPR : MINUS_EXPR,
2941                                         type, @1, @2); }
2942          (if (cst && !TREE_OVERFLOW (cst))
2943           (inner_op @0 { cst; } )
2944           /* X+INT_MAX+1 is X-INT_MIN.  */
2945           (if (INTEGRAL_TYPE_P (type) && cst
2946                && wi::to_wide (cst) == wi::min_value (type))
2947            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2948            /* Last resort, use some unsigned type.  */
2949            (with { tree utype = unsigned_type_for (type); }
2950             (if (utype)
2951              (view_convert (inner_op
2952                             (view_convert:utype @0)
2953                             (view_convert:utype
2954                              { drop_tree_overflow (cst); }))))))))))))))
2956   /* (CST1 - A) +- CST2 -> CST3 - A  */
2957   (for outer_op (plus minus)
2958    (simplify
2959     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2960     /* If one of the types wraps, use that one.  */
2961     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2962      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2963         forever if something doesn't simplify into a constant.  */
2964      (if (!CONSTANT_CLASS_P (@0))
2965       (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2966      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2967           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2968       (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2969       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
2970        (with { tree cst = const_binop (outer_op, type, @1, @2); }
2971         (if (cst && !TREE_OVERFLOW (cst))
2972          (minus { cst; } @0))))))))
2974   /* CST1 - (CST2 - A) -> CST3 + A
2975      Use view_convert because it is safe for vectors and equivalent for
2976      scalars.  */
2977   (simplify
2978    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2979    /* If one of the types wraps, use that one.  */
2980    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2981     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2982       forever if something doesn't simplify into a constant.  */
2983     (if (!CONSTANT_CLASS_P (@0))
2984      (plus (view_convert @0) (minus @1 (view_convert @2))))
2985     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2986          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2987      (view_convert (plus @0 (minus (view_convert @1) @2)))
2988      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
2989       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2990        (if (cst && !TREE_OVERFLOW (cst))
2991         (plus { cst; } @0)))))))
2993 /* ((T)(A)) + CST -> (T)(A + CST)  */
2994 #if GIMPLE
2995   (simplify
2996    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2997     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2998          && TREE_CODE (type) == INTEGER_TYPE
2999          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3000          && int_fits_type_p (@1, TREE_TYPE (@0)))
3001      /* Perform binary operation inside the cast if the constant fits
3002         and (A + CST)'s range does not overflow.  */
3003      (with
3004       {
3005         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3006                           max_ovf = wi::OVF_OVERFLOW;
3007         tree inner_type = TREE_TYPE (@0);
3009         wide_int w1
3010           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3011                             TYPE_SIGN (inner_type));
3013         value_range vr;
3014         if (get_global_range_query ()->range_of_expr (vr, @0)
3015             && !vr.varying_p () && !vr.undefined_p ())
3016           {
3017             wide_int wmin0 = vr.lower_bound ();
3018             wide_int wmax0 = vr.upper_bound ();
3019             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3020             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3021           }
3022       }
3023      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3024       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3025      )))
3026 #endif
3028 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3029 #if GIMPLE
3030   (for op (plus minus)
3031    (simplify
3032     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3033      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3034           && TREE_CODE (type) == INTEGER_TYPE
3035           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3036           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3037           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3038           && TYPE_OVERFLOW_WRAPS (type))
3039        (plus (convert @0) (op @2 (convert @1))))))
3040 #endif
3042 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3043    to a simple value.  */
3044   (for op (plus minus)
3045    (simplify
3046     (op (convert @0) (convert @1))
3047      (if (INTEGRAL_TYPE_P (type)
3048           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3049           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3050           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3051           && !TYPE_OVERFLOW_TRAPS (type)
3052           && !TYPE_OVERFLOW_SANITIZED (type))
3053       (convert (op! @0 @1)))))
3055   /* ~A + A -> -1 */
3056   (simplify
3057    (plus:c (convert? (bit_not @0)) (convert? @0))
3058    (if (!TYPE_OVERFLOW_TRAPS (type))
3059     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3061   /* ~A + 1 -> -A */
3062   (simplify
3063    (plus (convert? (bit_not @0)) integer_each_onep)
3064    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3065     (negate (convert @0))))
3067   /* -A - 1 -> ~A */
3068   (simplify
3069    (minus (convert? (negate @0)) integer_each_onep)
3070    (if (!TYPE_OVERFLOW_TRAPS (type)
3071         && TREE_CODE (type) != COMPLEX_TYPE
3072         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3073     (bit_not (convert @0))))
3075   /* -1 - A -> ~A */
3076   (simplify
3077    (minus integer_all_onesp @0)
3078    (if (TREE_CODE (type) != COMPLEX_TYPE)
3079     (bit_not @0)))
3081   /* (T)(P + A) - (T)P -> (T) A */
3082   (simplify
3083    (minus (convert (plus:c @@0 @1))
3084     (convert? @0))
3085    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3086         /* For integer types, if A has a smaller type
3087            than T the result depends on the possible
3088            overflow in P + A.
3089            E.g. T=size_t, A=(unsigned)429497295, P>0.
3090            However, if an overflow in P + A would cause
3091            undefined behavior, we can assume that there
3092            is no overflow.  */
3093         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3094             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3095     (convert @1)))
3096   (simplify
3097    (minus (convert (pointer_plus @@0 @1))
3098     (convert @0))
3099    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3100         /* For pointer types, if the conversion of A to the
3101            final type requires a sign- or zero-extension,
3102            then we have to punt - it is not defined which
3103            one is correct.  */
3104         || (POINTER_TYPE_P (TREE_TYPE (@0))
3105             && TREE_CODE (@1) == INTEGER_CST
3106             && tree_int_cst_sign_bit (@1) == 0))
3107     (convert @1)))
3108    (simplify
3109     (pointer_diff (pointer_plus @@0 @1) @0)
3110     /* The second argument of pointer_plus must be interpreted as signed, and
3111        thus sign-extended if necessary.  */
3112     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3113      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3114         second arg is unsigned even when we need to consider it as signed,
3115         we don't want to diagnose overflow here.  */
3116      (convert (view_convert:stype @1))))
3118   /* (T)P - (T)(P + A) -> -(T) A */
3119   (simplify
3120    (minus (convert? @0)
3121     (convert (plus:c @@0 @1)))
3122    (if (INTEGRAL_TYPE_P (type)
3123         && TYPE_OVERFLOW_UNDEFINED (type)
3124         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3125     (with { tree utype = unsigned_type_for (type); }
3126      (convert (negate (convert:utype @1))))
3127     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3128          /* For integer types, if A has a smaller type
3129             than T the result depends on the possible
3130             overflow in P + A.
3131             E.g. T=size_t, A=(unsigned)429497295, P>0.
3132             However, if an overflow in P + A would cause
3133             undefined behavior, we can assume that there
3134             is no overflow.  */
3135          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3136              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3137      (negate (convert @1)))))
3138   (simplify
3139    (minus (convert @0)
3140     (convert (pointer_plus @@0 @1)))
3141    (if (INTEGRAL_TYPE_P (type)
3142         && TYPE_OVERFLOW_UNDEFINED (type)
3143         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3144     (with { tree utype = unsigned_type_for (type); }
3145      (convert (negate (convert:utype @1))))
3146     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3147          /* For pointer types, if the conversion of A to the
3148             final type requires a sign- or zero-extension,
3149             then we have to punt - it is not defined which
3150             one is correct.  */
3151          || (POINTER_TYPE_P (TREE_TYPE (@0))
3152              && TREE_CODE (@1) == INTEGER_CST
3153              && tree_int_cst_sign_bit (@1) == 0))
3154      (negate (convert @1)))))
3155    (simplify
3156     (pointer_diff @0 (pointer_plus @@0 @1))
3157     /* The second argument of pointer_plus must be interpreted as signed, and
3158        thus sign-extended if necessary.  */
3159     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3160      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3161         second arg is unsigned even when we need to consider it as signed,
3162         we don't want to diagnose overflow here.  */
3163      (negate (convert (view_convert:stype @1)))))
3165   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3166   (simplify
3167    (minus (convert (plus:c @@0 @1))
3168     (convert (plus:c @0 @2)))
3169    (if (INTEGRAL_TYPE_P (type)
3170         && TYPE_OVERFLOW_UNDEFINED (type)
3171         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3172         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3173     (with { tree utype = unsigned_type_for (type); }
3174      (convert (minus (convert:utype @1) (convert:utype @2))))
3175     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3176           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3177          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3178              /* For integer types, if A has a smaller type
3179                 than T the result depends on the possible
3180                 overflow in P + A.
3181                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3182                 However, if an overflow in P + A would cause
3183                 undefined behavior, we can assume that there
3184                 is no overflow.  */
3185              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3186                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3187                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3188                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3189      (minus (convert @1) (convert @2)))))
3190   (simplify
3191    (minus (convert (pointer_plus @@0 @1))
3192     (convert (pointer_plus @0 @2)))
3193    (if (INTEGRAL_TYPE_P (type)
3194         && TYPE_OVERFLOW_UNDEFINED (type)
3195         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3196     (with { tree utype = unsigned_type_for (type); }
3197      (convert (minus (convert:utype @1) (convert:utype @2))))
3198     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3199          /* For pointer types, if the conversion of A to the
3200             final type requires a sign- or zero-extension,
3201             then we have to punt - it is not defined which
3202             one is correct.  */
3203          || (POINTER_TYPE_P (TREE_TYPE (@0))
3204              && TREE_CODE (@1) == INTEGER_CST
3205              && tree_int_cst_sign_bit (@1) == 0
3206              && TREE_CODE (@2) == INTEGER_CST
3207              && tree_int_cst_sign_bit (@2) == 0))
3208      (minus (convert @1) (convert @2)))))
3209    (simplify
3210     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3211      (pointer_diff @0 @1))
3212    (simplify
3213     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3214     /* The second argument of pointer_plus must be interpreted as signed, and
3215        thus sign-extended if necessary.  */
3216     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3217      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3218         second arg is unsigned even when we need to consider it as signed,
3219         we don't want to diagnose overflow here.  */
3220      (minus (convert (view_convert:stype @1))
3221             (convert (view_convert:stype @2)))))))
3223 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3224     Modeled after fold_plusminus_mult_expr.  */
3225 (if (!TYPE_SATURATING (type)
3226      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3227  (for plusminus (plus minus)
3228   (simplify
3229    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3230    (if (!ANY_INTEGRAL_TYPE_P (type)
3231         || TYPE_OVERFLOW_WRAPS (type)
3232         || (INTEGRAL_TYPE_P (type)
3233             && tree_expr_nonzero_p (@0)
3234             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3235     (if (single_use (@3) || single_use (@4))
3236      /* If @1 +- @2 is constant require a hard single-use on either
3237         original operand (but not on both).  */
3238      (mult (plusminus @1 @2) @0)
3239      (mult! (plusminus @1 @2) @0)
3240   )))
3241   /* We cannot generate constant 1 for fract.  */
3242   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3243    (simplify
3244     (plusminus @0 (mult:c@3 @0 @2))
3245     (if ((!ANY_INTEGRAL_TYPE_P (type)
3246           || TYPE_OVERFLOW_WRAPS (type)
3247           /* For @0 + @0*@2 this transformation would introduce UB
3248              (where there was none before) for @0 in [-1,0] and @2 max.
3249              For @0 - @0*@2 this transformation would introduce UB
3250              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3251           || (INTEGRAL_TYPE_P (type)
3252               && ((tree_expr_nonzero_p (@0)
3253                    && expr_not_equal_to (@0,
3254                                 wi::minus_one (TYPE_PRECISION (type))))
3255                   || (plusminus == PLUS_EXPR
3256                       ? expr_not_equal_to (@2,
3257                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3258                       /* Let's ignore the @0 -1 and @2 min case.  */
3259                       : (expr_not_equal_to (@2,
3260                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3261                          && expr_not_equal_to (@2,
3262                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3263                                 + 1))))))
3264          && single_use (@3))
3265      (mult (plusminus { build_one_cst (type); } @2) @0)))
3266    (simplify
3267     (plusminus (mult:c@3 @0 @2) @0)
3268     (if ((!ANY_INTEGRAL_TYPE_P (type)
3269           || TYPE_OVERFLOW_WRAPS (type)
3270           /* For @0*@2 + @0 this transformation would introduce UB
3271              (where there was none before) for @0 in [-1,0] and @2 max.
3272              For @0*@2 - @0 this transformation would introduce UB
3273              for @0 0 and @2 min.  */
3274           || (INTEGRAL_TYPE_P (type)
3275               && ((tree_expr_nonzero_p (@0)
3276                    && (plusminus == MINUS_EXPR
3277                        || expr_not_equal_to (@0,
3278                                 wi::minus_one (TYPE_PRECISION (type)))))
3279                   || expr_not_equal_to (@2,
3280                         (plusminus == PLUS_EXPR
3281                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3282                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3283          && single_use (@3))
3284      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3286 #if GIMPLE
3287 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3288    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3289 (simplify
3290  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3291   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3292        && tree_fits_uhwi_p (@1)
3293        && tree_to_uhwi (@1) < element_precision (type)
3294        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3295            || optab_handler (smul_optab,
3296                              TYPE_MODE (type)) != CODE_FOR_nothing))
3297    (with { tree t = type;
3298            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3299            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3300                                              element_precision (type));
3301            w += 1;
3302            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3303                                         : t, w);
3304            cst = build_uniform_cst (t, cst); }
3305     (convert (mult (convert:t @0) { cst; })))))
3306 (simplify
3307  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3308   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3309        && tree_fits_uhwi_p (@1)
3310        && tree_to_uhwi (@1) < element_precision (type)
3311        && tree_fits_uhwi_p (@2)
3312        && tree_to_uhwi (@2) < element_precision (type)
3313        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3314            || optab_handler (smul_optab,
3315                              TYPE_MODE (type)) != CODE_FOR_nothing))
3316    (with { tree t = type;
3317            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3318            unsigned int prec = element_precision (type);
3319            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3320            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3321            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3322                                         : t, w);
3323            cst = build_uniform_cst (t, cst); }
3324     (convert (mult (convert:t @0) { cst; })))))
3325 #endif
3327 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3328    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3329    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3330 (for op (bit_ior bit_xor)
3331  (simplify
3332   (op (mult:s@0 @1 INTEGER_CST@2)
3333       (mult:s@3 @1 INTEGER_CST@4))
3334   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3335        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3336    (mult @1
3337          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3338  (simplify
3339   (op:c (mult:s@0 @1 INTEGER_CST@2)
3340         (lshift:s@3 @1 INTEGER_CST@4))
3341   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3342        && tree_int_cst_sgn (@4) > 0
3343        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3344    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3345            wide_int c = wi::add (wi::to_wide (@2),
3346                                  wi::lshift (wone, wi::to_wide (@4))); }
3347     (mult @1 { wide_int_to_tree (type, c); }))))
3348  (simplify
3349   (op:c (mult:s@0 @1 INTEGER_CST@2)
3350         @1)
3351   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3352        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3353    (mult @1
3354          { wide_int_to_tree (type,
3355                              wi::add (wi::to_wide (@2), 1)); })))
3356  (simplify
3357   (op (lshift:s@0 @1 INTEGER_CST@2)
3358       (lshift:s@3 @1 INTEGER_CST@4))
3359   (if (INTEGRAL_TYPE_P (type)
3360        && tree_int_cst_sgn (@2) > 0
3361        && tree_int_cst_sgn (@4) > 0
3362        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3363    (with { tree t = type;
3364            if (!TYPE_OVERFLOW_WRAPS (t))
3365              t = unsigned_type_for (t);
3366            wide_int wone = wi::one (TYPE_PRECISION (t));
3367            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3368                                  wi::lshift (wone, wi::to_wide (@4))); }
3369     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3370  (simplify
3371   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3372         @1)
3373   (if (INTEGRAL_TYPE_P (type)
3374        && tree_int_cst_sgn (@2) > 0
3375        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3376    (with { tree t = type;
3377            if (!TYPE_OVERFLOW_WRAPS (t))
3378              t = unsigned_type_for (t);
3379            wide_int wone = wi::one (TYPE_PRECISION (t));
3380            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3381     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3383 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3385 (for minmax (min max)
3386  (simplify
3387   (minmax @0 @0)
3388   @0))
3389 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3390 (for minmax (FMIN_ALL FMAX_ALL)
3391  (simplify
3392   (minmax @0 @0)
3393   (if (!tree_expr_maybe_signaling_nan_p (@0))
3394     @0)))
3395 /* min(max(x,y),y) -> y.  */
3396 (simplify
3397  (min:c (max:c @0 @1) @1)
3398  @1)
3399 /* max(min(x,y),y) -> y.  */
3400 (simplify
3401  (max:c (min:c @0 @1) @1)
3402  @1)
3403 /* max(a,-a) -> abs(a).  */
3404 (simplify
3405  (max:c @0 (negate @0))
3406  (if (TREE_CODE (type) != COMPLEX_TYPE
3407       && (! ANY_INTEGRAL_TYPE_P (type)
3408           || TYPE_OVERFLOW_UNDEFINED (type)))
3409   (abs @0)))
3410 /* min(a,-a) -> -abs(a).  */
3411 (simplify
3412  (min:c @0 (negate @0))
3413  (if (TREE_CODE (type) != COMPLEX_TYPE
3414       && (! ANY_INTEGRAL_TYPE_P (type)
3415           || TYPE_OVERFLOW_UNDEFINED (type)))
3416   (negate (abs @0))))
3417 (simplify
3418  (min @0 @1)
3419  (switch
3420   (if (INTEGRAL_TYPE_P (type)
3421        && TYPE_MIN_VALUE (type)
3422        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3423    @1)
3424   (if (INTEGRAL_TYPE_P (type)
3425        && TYPE_MAX_VALUE (type)
3426        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3427    @0)))
3428 (simplify
3429  (max @0 @1)
3430  (switch
3431   (if (INTEGRAL_TYPE_P (type)
3432        && TYPE_MAX_VALUE (type)
3433        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3434    @1)
3435   (if (INTEGRAL_TYPE_P (type)
3436        && TYPE_MIN_VALUE (type)
3437        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3438    @0)))
3440 /* max (a, a + CST) -> a + CST where CST is positive.  */
3441 /* max (a, a + CST) -> a where CST is negative.  */
3442 (simplify
3443  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3444   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3445    (if (tree_int_cst_sgn (@1) > 0)
3446     @2
3447     @0)))
3449 /* min (a, a + CST) -> a where CST is positive.  */
3450 /* min (a, a + CST) -> a + CST where CST is negative. */
3451 (simplify
3452  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3453   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3454    (if (tree_int_cst_sgn (@1) > 0)
3455     @0
3456     @2)))
3458 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3459    the addresses are known to be less, equal or greater.  */
3460 (for minmax (min max)
3461      cmp (lt gt)
3462  (simplify
3463   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3464   (with
3465    {
3466      poly_int64 off0, off1;
3467      tree base0, base1;
3468      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3469                                   off0, off1, GENERIC);
3470    }
3471    (if (equal == 1)
3472     (if (minmax == MIN_EXPR)
3473      (if (known_le (off0, off1))
3474       @2
3475       (if (known_gt (off0, off1))
3476        @3))
3477      (if (known_ge (off0, off1))
3478       @2
3479       (if (known_lt (off0, off1))
3480        @3)))))))
3482 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3483    and the outer convert demotes the expression back to x's type.  */
3484 (for minmax (min max)
3485  (simplify
3486   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3487   (if (INTEGRAL_TYPE_P (type)
3488        && types_match (@1, type) && int_fits_type_p (@2, type)
3489        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3490        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3491    (minmax @1 (convert @2)))))
3493 (for minmax (FMIN_ALL FMAX_ALL)
3494  /* If either argument is NaN and other one is not sNaN, return the other
3495     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3496  (simplify
3497   (minmax:c @0 REAL_CST@1)
3498    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3499        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3500        && !tree_expr_maybe_signaling_nan_p (@0))
3501    @0)))
3502 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3503    functions to return the numeric arg if the other one is NaN.
3504    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3505    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3506    worry about it either.  */
3507 (if (flag_finite_math_only)
3508  (simplify
3509   (FMIN_ALL @0 @1)
3510   (min @0 @1))
3511  (simplify
3512   (FMAX_ALL @0 @1)
3513   (max @0 @1)))
3514 /* min (-A, -B) -> -max (A, B)  */
3515 (for minmax (min max FMIN_ALL FMAX_ALL)
3516      maxmin (max min FMAX_ALL FMIN_ALL)
3517  (simplify
3518   (minmax (negate:s@2 @0) (negate:s@3 @1))
3519   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3520        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3521            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3522    (negate (maxmin @0 @1)))))
3523 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3524    MAX (~X, ~Y) -> ~MIN (X, Y)  */
3525 (for minmax (min max)
3526  maxmin (max min)
3527  (simplify
3528   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3529   (bit_not (maxmin @0 @1))))
3531 /* MIN (X, Y) == X -> X <= Y  */
3532 (for minmax (min min max max)
3533      cmp    (eq  ne  eq  ne )
3534      out    (le  gt  ge  lt )
3535  (simplify
3536   (cmp:c (minmax:c @0 @1) @0)
3537   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3538    (out @0 @1))))
3539 /* MIN (X, 5) == 0 -> X == 0
3540    MIN (X, 5) == 7 -> false  */
3541 (for cmp (eq ne)
3542  (simplify
3543   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3544   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3545                  TYPE_SIGN (TREE_TYPE (@0))))
3546    { constant_boolean_node (cmp == NE_EXPR, type); }
3547    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3548                   TYPE_SIGN (TREE_TYPE (@0))))
3549     (cmp @0 @2)))))
3550 (for cmp (eq ne)
3551  (simplify
3552   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3553   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3554                  TYPE_SIGN (TREE_TYPE (@0))))
3555    { constant_boolean_node (cmp == NE_EXPR, type); }
3556    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3557                   TYPE_SIGN (TREE_TYPE (@0))))
3558     (cmp @0 @2)))))
3559 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
3560 (for minmax (min     min     max     max     min     min     max     max    )
3561      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
3562      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3563  (simplify
3564   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3565   (comb (cmp @0 @2) (cmp @1 @2))))
3567 /* X <= MAX(X, Y) -> true
3568    X > MAX(X, Y) -> false 
3569    X >= MIN(X, Y) -> true
3570    X < MIN(X, Y) -> false */
3571 (for minmax (min     min     max     max     )
3572      cmp    (ge      lt      le      gt      )
3573  (simplify
3574   (cmp @0 (minmax:c @0 @1))
3575   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3577 /* Undo fancy ways of writing max/min or other ?: expressions, like
3578    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
3579    People normally use ?: and that is what we actually try to optimize.  */
3580 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
3581 (simplify
3582  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3583  (if (INTEGRAL_TYPE_P (type)
3584       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3585   (cond (convert:boolean_type_node @2) @1 @0)))
3586 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
3587 (simplify
3588  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3589  (if (INTEGRAL_TYPE_P (type)
3590       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3591   (cond (convert:boolean_type_node @2) @1 @0)))
3592 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
3593 (simplify
3594  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3595  (if (INTEGRAL_TYPE_P (type)
3596       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3597   (cond (convert:boolean_type_node @2) @1 @0)))
3599 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
3600 (simplify
3601   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3602   (max @2 @1))
3604 /* ((x & 0x1) == 0) ? y : z <op> y -> (-(typeof(y))(x & 0x1) & z) <op> y */
3605 (for op (bit_xor bit_ior)
3606  (simplify
3607   (cond (eq zero_one_valued_p@0
3608             integer_zerop)
3609         @1
3610         (op:c @2 @1))
3611   (if (INTEGRAL_TYPE_P (type)
3612        && TYPE_PRECISION (type) > 1
3613        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3614        (op (bit_and (negate (convert:type @0)) @2) @1))))
3616 /* ((x & 0x1) == 0) ? z <op> y : y -> (-(typeof(y))(x & 0x1) & z) <op> y */
3617 (for op (bit_xor bit_ior)
3618  (simplify
3619   (cond (ne zero_one_valued_p@0
3620             integer_zerop)
3621        (op:c @2 @1)
3622         @1)
3623   (if (INTEGRAL_TYPE_P (type)
3624        && TYPE_PRECISION (type) > 1
3625        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3626        (op (bit_and (negate (convert:type @0)) @2) @1))))
3628 /* Simplifications of shift and rotates.  */
3630 (for rotate (lrotate rrotate)
3631  (simplify
3632   (rotate integer_all_onesp@0 @1)
3633   @0))
3635 /* Optimize -1 >> x for arithmetic right shifts.  */
3636 (simplify
3637  (rshift integer_all_onesp@0 @1)
3638  (if (!TYPE_UNSIGNED (type))
3639   @0))
3641 /* Optimize (x >> c) << c into x & (-1<<c).  */
3642 (simplify
3643  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3644  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3645   /* It doesn't matter if the right shift is arithmetic or logical.  */
3646   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3648 (simplify
3649  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3650  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3651       /* Allow intermediate conversion to integral type with whatever sign, as
3652          long as the low TYPE_PRECISION (type)
3653          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
3654       && INTEGRAL_TYPE_P (type)
3655       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3656       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3657       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3658       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3659           || wi::geu_p (wi::to_wide (@1),
3660                         TYPE_PRECISION (type)
3661                         - TYPE_PRECISION (TREE_TYPE (@2)))))
3662   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3664 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3665    types.  */
3666 (simplify
3667  (rshift (lshift @0 INTEGER_CST@1) @1)
3668  (if (TYPE_UNSIGNED (type)
3669       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3670   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3672 /* Optimize x >> x into 0 */
3673 (simplify
3674  (rshift @0 @0)
3675   { build_zero_cst (type); })
3677 (for shiftrotate (lrotate rrotate lshift rshift)
3678  (simplify
3679   (shiftrotate @0 integer_zerop)
3680   (non_lvalue @0))
3681  (simplify
3682   (shiftrotate integer_zerop@0 @1)
3683   @0)
3684  /* Prefer vector1 << scalar to vector1 << vector2
3685     if vector2 is uniform.  */
3686  (for vec (VECTOR_CST CONSTRUCTOR)
3687   (simplify
3688    (shiftrotate @0 vec@1)
3689    (with { tree tem = uniform_vector_p (@1); }
3690     (if (tem)
3691      (shiftrotate @0 { tem; }))))))
3693 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3694    Y is 0.  Similarly for X >> Y.  */
3695 #if GIMPLE
3696 (for shift (lshift rshift)
3697  (simplify
3698   (shift @0 SSA_NAME@1)
3699    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3700     (with {
3701       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3702       int prec = TYPE_PRECISION (TREE_TYPE (@1));
3703      }
3704      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3705       @0)))))
3706 #endif
3708 /* Rewrite an LROTATE_EXPR by a constant into an
3709    RROTATE_EXPR by a new constant.  */
3710 (simplify
3711  (lrotate @0 INTEGER_CST@1)
3712  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3713                             build_int_cst (TREE_TYPE (@1),
3714                                            element_precision (type)), @1); }))
3716 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
3717 (for op (lrotate rrotate rshift lshift)
3718  (simplify
3719   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3720   (with { unsigned int prec = element_precision (type); }
3721    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3722         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3723         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3724         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3725     (with { unsigned int low = (tree_to_uhwi (@1)
3726                                 + tree_to_uhwi (@2)); }
3727      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3728         being well defined.  */
3729      (if (low >= prec)
3730       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3731        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3732        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3733         { build_zero_cst (type); }
3734         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3735       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3738 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
3739 (simplify
3740  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3741   (if ((wi::to_wide (@1) & 1) != 0)
3742    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3743    { build_zero_cst (type); }))
3745 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3746    either to false if D is smaller (unsigned comparison) than C, or to
3747    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
3748 (for cmp (ne eq)
3749      icmp (eq ne)
3750  (simplify
3751   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3752    (with { int c1 = wi::clz (wi::to_wide (@1));
3753            int c2 = wi::clz (wi::to_wide (@2)); }
3754     (if (c1 < c2)
3755      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3756      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3757  (simplify
3758   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3759    (if (tree_int_cst_sgn (@1) > 0)
3760     (with { int c1 = wi::clz (wi::to_wide (@1));
3761             int c2 = wi::clz (wi::to_wide (@2)); }
3762      (if (c1 > c2)
3763       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3764       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3766 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3767    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3768    if CST2 != 0.  */
3769 (for cmp (ne eq)
3770  (simplify
3771   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3772   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3773    (if (cand < 0
3774         || (!integer_zerop (@2)
3775             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3776     { constant_boolean_node (cmp == NE_EXPR, type); }
3777     (if (!integer_zerop (@2)
3778          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3779      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3781 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
3782         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
3783 (for cmp (ne eq)
3784  (simplify
3785   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3786   (if (tree_fits_shwi_p (@1)
3787        && tree_to_shwi (@1) > 0
3788        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3789     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
3790       { constant_boolean_node (cmp == NE_EXPR, type); }
3791       (with { wide_int c1 = wi::to_wide (@1);
3792               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
3793               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
3794         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
3795              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
3796  (simplify
3797   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
3798   (if (tree_fits_shwi_p (@1)
3799        && tree_to_shwi (@1) > 0
3800        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
3801     (with { tree t0 = TREE_TYPE (@0);
3802             unsigned int prec = TYPE_PRECISION (t0);
3803             wide_int c1 = wi::to_wide (@1);
3804             wide_int c2 = wi::to_wide (@2);
3805             wide_int c3 = wi::to_wide (@3);
3806             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
3807       (if ((c2 & c3) != c3)
3808         { constant_boolean_node (cmp == NE_EXPR, type); }
3809         (if (TYPE_UNSIGNED (t0))
3810           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
3811             { constant_boolean_node (cmp == NE_EXPR, type); }
3812             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3813                  { wide_int_to_tree (t0, c3 << c1); }))
3814           (with { wide_int smask = wi::arshift (sb, c1); }
3815             (switch
3816               (if ((c2 & smask) == 0)
3817                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
3818                      { wide_int_to_tree (t0, c3 << c1); }))
3819               (if ((c3 & smask) == 0)
3820                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3821                      { wide_int_to_tree (t0, c3 << c1); }))
3822               (if ((c2 & smask) != (c3 & smask))
3823                 { constant_boolean_node (cmp == NE_EXPR, type); })
3824               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
3825                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
3827 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3828         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3829    if the new mask might be further optimized.  */
3830 (for shift (lshift rshift)
3831  (simplify
3832   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3833            INTEGER_CST@2)
3834    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3835         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3836         && tree_fits_uhwi_p (@1)
3837         && tree_to_uhwi (@1) > 0
3838         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3839     (with
3840      {
3841        unsigned int shiftc = tree_to_uhwi (@1);
3842        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3843        unsigned HOST_WIDE_INT newmask, zerobits = 0;
3844        tree shift_type = TREE_TYPE (@3);
3845        unsigned int prec;
3847        if (shift == LSHIFT_EXPR)
3848          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3849        else if (shift == RSHIFT_EXPR
3850                 && type_has_mode_precision_p (shift_type))
3851          {
3852            prec = TYPE_PRECISION (TREE_TYPE (@3));
3853            tree arg00 = @0;
3854            /* See if more bits can be proven as zero because of
3855               zero extension.  */
3856            if (@3 != @0
3857                && TYPE_UNSIGNED (TREE_TYPE (@0)))
3858              {
3859                tree inner_type = TREE_TYPE (@0);
3860                if (type_has_mode_precision_p (inner_type)
3861                    && TYPE_PRECISION (inner_type) < prec)
3862                  {
3863                    prec = TYPE_PRECISION (inner_type);
3864                    /* See if we can shorten the right shift.  */
3865                    if (shiftc < prec)
3866                      shift_type = inner_type;
3867                    /* Otherwise X >> C1 is all zeros, so we'll optimize
3868                       it into (X, 0) later on by making sure zerobits
3869                       is all ones.  */
3870                  }
3871              }
3872            zerobits = HOST_WIDE_INT_M1U;
3873            if (shiftc < prec)
3874              {
3875                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3876                zerobits <<= prec - shiftc;
3877              }
3878            /* For arithmetic shift if sign bit could be set, zerobits
3879               can contain actually sign bits, so no transformation is
3880               possible, unless MASK masks them all away.  In that
3881               case the shift needs to be converted into logical shift.  */
3882            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3883                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3884              {
3885                if ((mask & zerobits) == 0)
3886                  shift_type = unsigned_type_for (TREE_TYPE (@3));
3887                else
3888                  zerobits = 0;
3889              }
3890          }
3891      }
3892      /* ((X << 16) & 0xff00) is (X, 0).  */
3893      (if ((mask & zerobits) == mask)
3894       { build_int_cst (type, 0); }
3895       (with { newmask = mask | zerobits; }
3896        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3897         (with
3898          {
3899            /* Only do the transformation if NEWMASK is some integer
3900               mode's mask.  */
3901            for (prec = BITS_PER_UNIT;
3902                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3903              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3904                break;
3905          }
3906          (if (prec < HOST_BITS_PER_WIDE_INT
3907               || newmask == HOST_WIDE_INT_M1U)
3908           (with
3909            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3910            (if (!tree_int_cst_equal (newmaskt, @2))
3911             (if (shift_type != TREE_TYPE (@3))
3912              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3913              (bit_and @4 { newmaskt; })))))))))))))
3915 /* ((1 << n) & M) != 0  -> n == log2 (M) */
3916 (for cmp (ne eq)
3917        icmp (eq ne)
3918  (simplify
3919   (cmp
3920    (bit_and
3921     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3922   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3923    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3924                                 wi::exact_log2 (wi::to_wide (@1))); }))))
3926 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3927    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
3928 (for shift (lshift rshift)
3929  (for bit_op (bit_and bit_xor bit_ior)
3930   (simplify
3931    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3932    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3933     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3934      (if (mask)
3935       (bit_op (shift (convert @0) @1) { mask; })))))))
3937 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
3938 (simplify
3939  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3940   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3941        && (element_precision (TREE_TYPE (@0))
3942            <= element_precision (TREE_TYPE (@1))
3943            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3944    (with
3945     { tree shift_type = TREE_TYPE (@0); }
3946      (convert (rshift (convert:shift_type @1) @2)))))
3948 /* ~(~X >>r Y) -> X >>r Y
3949    ~(~X <<r Y) -> X <<r Y */
3950 (for rotate (lrotate rrotate)
3951  (simplify
3952   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3953    (if ((element_precision (TREE_TYPE (@0))
3954          <= element_precision (TREE_TYPE (@1))
3955          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3956         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3957             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3958     (with
3959      { tree rotate_type = TREE_TYPE (@0); }
3960       (convert (rotate (convert:rotate_type @1) @2))))))
3962 (for cmp (eq ne)
3963  (for rotate (lrotate rrotate)
3964       invrot (rrotate lrotate)
3965   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3966   (simplify
3967    (cmp (rotate @1 @0) (rotate @2 @0))
3968    (cmp @1 @2))
3969   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3970   (simplify
3971    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3972    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3973   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
3974   (simplify
3975    (cmp (rotate @0 @1) INTEGER_CST@2)
3976     (if (integer_zerop (@2) || integer_all_onesp (@2))
3977      (cmp @0 @2)))))
3979 /* Narrow a lshift by constant.  */
3980 (simplify
3981  (convert (lshift:s@0 @1 INTEGER_CST@2))
3982  (if (INTEGRAL_TYPE_P (type)
3983       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3984       && !integer_zerop (@2)
3985       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
3986   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3987        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
3988    (lshift (convert @1) @2)
3989    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
3990     { build_zero_cst (type); }))))
3992 /* Simplifications of conversions.  */
3994 /* Basic strip-useless-type-conversions / strip_nops.  */
3995 (for cvt (convert view_convert float fix_trunc)
3996  (simplify
3997   (cvt @0)
3998   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3999        || (GENERIC && type == TREE_TYPE (@0)))
4000    @0)))
4002 /* Contract view-conversions.  */
4003 (simplify
4004   (view_convert (view_convert @0))
4005   (view_convert @0))
4007 /* For integral conversions with the same precision or pointer
4008    conversions use a NOP_EXPR instead.  */
4009 (simplify
4010   (view_convert @0)
4011   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4012        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4013        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4014    (convert @0)))
4016 /* Strip inner integral conversions that do not change precision or size, or
4017    zero-extend while keeping the same size (for bool-to-char).  */
4018 (simplify
4019   (view_convert (convert@0 @1))
4020   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4021        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4022        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4023        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4024            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4025                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4026    (view_convert @1)))
4028 /* Simplify a view-converted empty or single-element constructor.  */
4029 (simplify
4030   (view_convert CONSTRUCTOR@0)
4031   (with
4032    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4033                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4034    (switch
4035     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4036      { build_zero_cst (type); })
4037     (if (CONSTRUCTOR_NELTS (ctor) == 1
4038          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4039          && operand_equal_p (TYPE_SIZE (type),
4040                              TYPE_SIZE (TREE_TYPE
4041                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4042      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4044 /* Re-association barriers around constants and other re-association
4045    barriers can be removed.  */
4046 (simplify
4047  (paren CONSTANT_CLASS_P@0)
4048  @0)
4049 (simplify
4050  (paren (paren@1 @0))
4051  @1)
4053 /* Handle cases of two conversions in a row.  */
4054 (for ocvt (convert float fix_trunc)
4055  (for icvt (convert float)
4056   (simplify
4057    (ocvt (icvt@1 @0))
4058    (with
4059     {
4060       tree inside_type = TREE_TYPE (@0);
4061       tree inter_type = TREE_TYPE (@1);
4062       int inside_int = INTEGRAL_TYPE_P (inside_type);
4063       int inside_ptr = POINTER_TYPE_P (inside_type);
4064       int inside_float = FLOAT_TYPE_P (inside_type);
4065       int inside_vec = VECTOR_TYPE_P (inside_type);
4066       unsigned int inside_prec = TYPE_PRECISION (inside_type);
4067       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4068       int inter_int = INTEGRAL_TYPE_P (inter_type);
4069       int inter_ptr = POINTER_TYPE_P (inter_type);
4070       int inter_float = FLOAT_TYPE_P (inter_type);
4071       int inter_vec = VECTOR_TYPE_P (inter_type);
4072       unsigned int inter_prec = TYPE_PRECISION (inter_type);
4073       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4074       int final_int = INTEGRAL_TYPE_P (type);
4075       int final_ptr = POINTER_TYPE_P (type);
4076       int final_float = FLOAT_TYPE_P (type);
4077       int final_vec = VECTOR_TYPE_P (type);
4078       unsigned int final_prec = TYPE_PRECISION (type);
4079       int final_unsignedp = TYPE_UNSIGNED (type);
4080     }
4081    (switch
4082     /* In addition to the cases of two conversions in a row
4083        handled below, if we are converting something to its own
4084        type via an object of identical or wider precision, neither
4085        conversion is needed.  */
4086     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4087           || (GENERIC
4088               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4089          && (((inter_int || inter_ptr) && final_int)
4090              || (inter_float && final_float))
4091          && inter_prec >= final_prec)
4092      (ocvt @0))
4094     /* Likewise, if the intermediate and initial types are either both
4095        float or both integer, we don't need the middle conversion if the
4096        former is wider than the latter and doesn't change the signedness
4097        (for integers).  Avoid this if the final type is a pointer since
4098        then we sometimes need the middle conversion.  */
4099     (if (((inter_int && inside_int) || (inter_float && inside_float))
4100          && (final_int || final_float)
4101          && inter_prec >= inside_prec
4102          && (inter_float || inter_unsignedp == inside_unsignedp))
4103      (ocvt @0))
4105     /* If we have a sign-extension of a zero-extended value, we can
4106        replace that by a single zero-extension.  Likewise if the
4107        final conversion does not change precision we can drop the
4108        intermediate conversion.  */
4109     (if (inside_int && inter_int && final_int
4110          && ((inside_prec < inter_prec && inter_prec < final_prec
4111               && inside_unsignedp && !inter_unsignedp)
4112              || final_prec == inter_prec))
4113      (ocvt @0))
4115     /* Two conversions in a row are not needed unless:
4116         - some conversion is floating-point (overstrict for now), or
4117         - some conversion is a vector (overstrict for now), or
4118         - the intermediate type is narrower than both initial and
4119           final, or
4120         - the intermediate type and innermost type differ in signedness,
4121           and the outermost type is wider than the intermediate, or
4122         - the initial type is a pointer type and the precisions of the
4123           intermediate and final types differ, or
4124         - the final type is a pointer type and the precisions of the
4125           initial and intermediate types differ.  */
4126     (if (! inside_float && ! inter_float && ! final_float
4127          && ! inside_vec && ! inter_vec && ! final_vec
4128          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4129          && ! (inside_int && inter_int
4130                && inter_unsignedp != inside_unsignedp
4131                && inter_prec < final_prec)
4132          && ((inter_unsignedp && inter_prec > inside_prec)
4133              == (final_unsignedp && final_prec > inter_prec))
4134          && ! (inside_ptr && inter_prec != final_prec)
4135          && ! (final_ptr && inside_prec != inter_prec))
4136      (ocvt @0))
4138     /* A truncation to an unsigned type (a zero-extension) should be
4139        canonicalized as bitwise and of a mask.  */
4140     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4141          && final_int && inter_int && inside_int
4142          && final_prec == inside_prec
4143          && final_prec > inter_prec
4144          && inter_unsignedp)
4145      (convert (bit_and @0 { wide_int_to_tree
4146                               (inside_type,
4147                                wi::mask (inter_prec, false,
4148                                          TYPE_PRECISION (inside_type))); })))
4150     /* If we are converting an integer to a floating-point that can
4151        represent it exactly and back to an integer, we can skip the
4152        floating-point conversion.  */
4153     (if (GIMPLE /* PR66211 */
4154          && inside_int && inter_float && final_int &&
4155          (unsigned) significand_size (TYPE_MODE (inter_type))
4156          >= inside_prec - !inside_unsignedp)
4157      (convert @0)))))))
4159 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4160    float_type.  Only do the transformation if we do not need to preserve
4161    trapping behaviour, so require !flag_trapping_math. */
4162 #if GIMPLE
4163 (simplify
4164    (float (fix_trunc @0))
4165    (if (!flag_trapping_math
4166         && types_match (type, TREE_TYPE (@0))
4167         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4168                                           OPTIMIZE_FOR_BOTH))
4169       (IFN_TRUNC @0)))
4170 #endif
4172 /* If we have a narrowing conversion to an integral type that is fed by a
4173    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4174    masks off bits outside the final type (and nothing else).  */
4175 (simplify
4176   (convert (bit_and @0 INTEGER_CST@1))
4177   (if (INTEGRAL_TYPE_P (type)
4178        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4179        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4180        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4181                                                     TYPE_PRECISION (type)), 0))
4182    (convert @0)))
4185 /* (X /[ex] A) * A -> X.  */
4186 (simplify
4187   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4188   (convert @0))
4190 /* Simplify (A / B) * B + (A % B) -> A.  */
4191 (for div (trunc_div ceil_div floor_div round_div)
4192      mod (trunc_mod ceil_mod floor_mod round_mod)
4193   (simplify
4194    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4195    @0))
4197 /* x / y * y == x -> x % y == 0.  */
4198 (simplify
4199   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4200   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4201     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4203 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4204 (for op (plus minus)
4205  (simplify
4206   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4207   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4208        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4209    (with
4210      {
4211        wi::overflow_type overflow;
4212        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4213                                TYPE_SIGN (type), &overflow);
4214      }
4215      (if (types_match (type, TREE_TYPE (@2))
4216          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4217       (op @0 { wide_int_to_tree (type, mul); })
4218       (with { tree utype = unsigned_type_for (type); }
4219        (convert (op (convert:utype @0)
4220                     (mult (convert:utype @1) (convert:utype @2))))))))))
4222 /* Canonicalization of binary operations.  */
4224 /* Convert X + -C into X - C.  */
4225 (simplify
4226  (plus @0 REAL_CST@1)
4227  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4228   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4229    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4230     (minus @0 { tem; })))))
4232 /* Convert x+x into x*2.  */
4233 (simplify
4234  (plus @0 @0)
4235  (if (SCALAR_FLOAT_TYPE_P (type))
4236   (mult @0 { build_real (type, dconst2); })
4237   (if (INTEGRAL_TYPE_P (type))
4238    (mult @0 { build_int_cst (type, 2); }))))
4240 /* 0 - X  ->  -X.  */
4241 (simplify
4242  (minus integer_zerop @1)
4243  (negate @1))
4244 (simplify
4245  (pointer_diff integer_zerop @1)
4246  (negate (convert @1)))
4248 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4249    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4250    (-ARG1 + ARG0) reduces to -ARG1.  */
4251 (simplify
4252  (minus real_zerop@0 @1)
4253  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4254   (negate @1)))
4256 /* Transform x * -1 into -x.  */
4257 (simplify
4258  (mult @0 integer_minus_onep)
4259  (negate @0))
4261 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4262    signed overflow for CST != 0 && CST != -1.  */
4263 (simplify
4264  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4265  (if (TREE_CODE (@2) != INTEGER_CST
4266       && single_use (@3)
4267       && !integer_zerop (@1) && !integer_minus_onep (@1))
4268   (mult (mult @0 @2) @1)))
4270 /* True if we can easily extract the real and imaginary parts of a complex
4271    number.  */
4272 (match compositional_complex
4273  (convert? (complex @0 @1)))
4275 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4276 (simplify
4277  (complex (realpart @0) (imagpart @0))
4278  @0)
4279 (simplify
4280  (realpart (complex @0 @1))
4281  @0)
4282 (simplify
4283  (imagpart (complex @0 @1))
4284  @1)
4286 /* Sometimes we only care about half of a complex expression.  */
4287 (simplify
4288  (realpart (convert?:s (conj:s @0)))
4289  (convert (realpart @0)))
4290 (simplify
4291  (imagpart (convert?:s (conj:s @0)))
4292  (convert (negate (imagpart @0))))
4293 (for part (realpart imagpart)
4294  (for op (plus minus)
4295   (simplify
4296    (part (convert?:s@2 (op:s @0 @1)))
4297    (convert (op (part @0) (part @1))))))
4298 (simplify
4299  (realpart (convert?:s (CEXPI:s @0)))
4300  (convert (COS @0)))
4301 (simplify
4302  (imagpart (convert?:s (CEXPI:s @0)))
4303  (convert (SIN @0)))
4305 /* conj(conj(x)) -> x  */
4306 (simplify
4307  (conj (convert? (conj @0)))
4308  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4309   (convert @0)))
4311 /* conj({x,y}) -> {x,-y}  */
4312 (simplify
4313  (conj (convert?:s (complex:s @0 @1)))
4314  (with { tree itype = TREE_TYPE (type); }
4315   (complex (convert:itype @0) (negate (convert:itype @1)))))
4317 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4318 (for bswap (BSWAP)
4319  (simplify
4320   (bswap (bswap @0))
4321   @0)
4322  (simplify
4323   (bswap (bit_not (bswap @0)))
4324   (bit_not @0))
4325  (for bitop (bit_xor bit_ior bit_and)
4326   (simplify
4327    (bswap (bitop:c (bswap @0) @1))
4328    (bitop @0 (bswap @1))))
4329  (for cmp (eq ne)
4330   (simplify
4331    (cmp (bswap@2 @0) (bswap @1))
4332    (with { tree ctype = TREE_TYPE (@2); }
4333     (cmp (convert:ctype @0) (convert:ctype @1))))
4334   (simplify
4335    (cmp (bswap @0) INTEGER_CST@1)
4336    (with { tree ctype = TREE_TYPE (@1); }
4337     (cmp (convert:ctype @0) (bswap! @1)))))
4338  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4339  (simplify
4340   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4341            INTEGER_CST@3)
4342    (if (BITS_PER_UNIT == 8
4343         && tree_fits_uhwi_p (@2)
4344         && tree_fits_uhwi_p (@3))
4345     (with
4346      {
4347       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4348       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4349       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4350       unsigned HOST_WIDE_INT lo = bits & 7;
4351       unsigned HOST_WIDE_INT hi = bits - lo;
4352      }
4353      (if (bits < prec
4354           && mask < (256u>>lo)
4355           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4356       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4357        (if (ns == 0)
4358         (bit_and (convert @1) @3)
4359         (with
4360          {
4361           tree utype = unsigned_type_for (TREE_TYPE (@1));
4362           tree nst = build_int_cst (integer_type_node, ns);
4363          }
4364          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4365  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4366  (simplify
4367   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4368    (if (BITS_PER_UNIT == 8
4369         && CHAR_TYPE_SIZE == 8
4370         && tree_fits_uhwi_p (@1))
4371     (with
4372      {
4373       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4374       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4375       /* If the bswap was extended before the original shift, this
4376          byte (shift) has the sign of the extension, not the sign of
4377          the original shift.  */
4378       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4379      }
4380      /* Special case: logical right shift of sign-extended bswap.
4381         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4382      (if (TYPE_PRECISION (type) > prec
4383           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4384           && TYPE_UNSIGNED (type)
4385           && bits < prec && bits + 8 >= prec)
4386       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4387        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4388       (if (bits + 8 == prec)
4389        (if (TYPE_UNSIGNED (st))
4390         (convert (convert:unsigned_char_type_node @0))
4391         (convert (convert:signed_char_type_node @0)))
4392        (if (bits < prec && bits + 8 > prec)
4393         (with 
4394          {
4395           tree nst = build_int_cst (integer_type_node, bits & 7);
4396           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4397                                        : signed_char_type_node;
4398          }
4399          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4400  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4401  (simplify
4402   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4403    (if (BITS_PER_UNIT == 8
4404         && tree_fits_uhwi_p (@1)
4405         && tree_to_uhwi (@1) < 256)
4406     (with
4407      {
4408       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4409       tree utype = unsigned_type_for (TREE_TYPE (@0));
4410       tree nst = build_int_cst (integer_type_node, prec - 8);
4411      }
4412      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4415 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4417 /* Simplify constant conditions.
4418    Only optimize constant conditions when the selected branch
4419    has the same type as the COND_EXPR.  This avoids optimizing
4420    away "c ? x : throw", where the throw has a void type.
4421    Note that we cannot throw away the fold-const.cc variant nor
4422    this one as we depend on doing this transform before possibly
4423    A ? B : B -> B triggers and the fold-const.cc one can optimize
4424    0 ? A : B to B even if A has side-effects.  Something
4425    genmatch cannot handle.  */
4426 (simplify
4427  (cond INTEGER_CST@0 @1 @2)
4428  (if (integer_zerop (@0))
4429   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4430    @2)
4431   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4432    @1)))
4433 (simplify
4434  (vec_cond VECTOR_CST@0 @1 @2)
4435  (if (integer_all_onesp (@0))
4436   @1
4437   (if (integer_zerop (@0))
4438    @2)))
4440 /* Sink unary operations to branches, but only if we do fold both.  */
4441 (for op (negate bit_not abs absu)
4442  (simplify
4443   (op (vec_cond:s @0 @1 @2))
4444   (vec_cond @0 (op! @1) (op! @2))))
4446 /* Sink binary operation to branches, but only if we can fold it.  */
4447 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4448          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4449          trunc_mod ceil_mod floor_mod round_mod min max)
4450 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4451  (simplify
4452   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4453   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4455 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4456  (simplify
4457   (op (vec_cond:s @0 @1 @2) @3)
4458   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4459  (simplify
4460   (op @3 (vec_cond:s @0 @1 @2))
4461   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4463 #if GIMPLE
4464 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4465  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4466            INTEGER_CST@1)
4467  (with {
4468          int ibit = tree_log2 (@0);
4469          int ibit2 = tree_log2 (@1);
4470        }
4471   (if (ibit == ibit2
4472       && ibit >= 0
4473       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4475 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4476  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4477           INTEGER_CST@1)
4478  (with {
4479          int ibit = tree_log2 (@0);
4480          int ibit2 = tree_log2 (@1);
4481        }
4482   (if (ibit == ibit2
4483       && ibit >= 0
4484       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4486 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4487  (bit_and:c
4488   (convert1?@4
4489    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4490   (convert2? @0))
4491  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4493 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4494  (bit_and:c
4495   (convert1?@4
4496    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4497   (convert2? @0))
4498  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4500 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4501  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4502             INTEGER_CST@1)
4503  (with {
4504          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4505                                               TYPE_PRECISION(type)));
4506          int ibit2 = tree_log2 (@1);
4507        }
4508   (if (ibit == ibit2
4509       && ibit >= 0
4510       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4512 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4513  (bit_and@4
4514   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4515   INTEGER_CST@1)
4516  (with {
4517          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4518                                               TYPE_PRECISION(type)));
4519          int ibit2 = tree_log2 (@1);
4520        }
4521   (if (ibit == ibit2
4522       && ibit >= 0
4523       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4525 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4526  (bit_and:c
4527   (convert1?@3
4528    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4529   (convert2? @0))
4530  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4532 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4533  (bit_and:c
4534   (convert1?@3
4535    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4536   (convert2? @0))
4537   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4539 #endif
4541 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4542    Currently disabled after pass lvec because ARM understands
4543    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
4544 (simplify
4545  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4546  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4547   (vec_cond (bit_and @0 @3) @1 @2)))
4548 (simplify
4549  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4550  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4551   (vec_cond (bit_ior @0 @3) @1 @2)))
4552 (simplify
4553  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4554  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4555   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4556 (simplify
4557  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4558  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4559   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4561 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
4562 (simplify
4563  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4564  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4565   (vec_cond (bit_and @0 @1) @2 @3)))
4566 (simplify
4567  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4568  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4569   (vec_cond (bit_ior @0 @1) @2 @3)))
4570 (simplify
4571  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4572  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4573   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4574 (simplify
4575  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4576  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4577   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4579 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4580    types are compatible.  */
4581 (simplify
4582  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4583  (if (VECTOR_BOOLEAN_TYPE_P (type)
4584       && types_match (type, TREE_TYPE (@0)))
4585   (if (integer_zerop (@1) && integer_all_onesp (@2))
4586    (bit_not @0)
4587    (if (integer_all_onesp (@1) && integer_zerop (@2))
4588     @0))))
4590 /* A few simplifications of "a ? CST1 : CST2". */
4591 /* NOTE: Only do this on gimple as the if-chain-to-switch
4592    optimization depends on the gimple to have if statements in it. */
4593 #if GIMPLE
4594 (simplify
4595  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4596  (switch
4597   (if (integer_zerop (@2))
4598    (switch
4599     /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4600     (if (integer_onep (@1))
4601      (convert (convert:boolean_type_node @0)))
4602     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4603     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4604      (with {
4605        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4606       }
4607       (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4608     /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
4609        here as the powerof2cst case above will handle that case correctly.  */
4610     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4611      (negate (convert (convert:boolean_type_node @0))))))
4612   (if (integer_zerop (@1))
4613    (with {
4614       tree booltrue = constant_boolean_node (true, boolean_type_node);
4615     }
4616     (switch
4617      /* a ? 0 : 1 -> !a. */
4618      (if (integer_onep (@2))
4619       (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4620      /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4621      (if (INTEGRAL_TYPE_P (type) &&  integer_pow2p (@2))
4622       (with {
4623         tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4624        }
4625        (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4626         { shift; })))
4627      /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
4628        here as the powerof2cst case above will handle that case correctly.  */
4629      (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4630       (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4631     )
4632    )
4633   )
4637 /* Optimize
4638    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
4639    x_5 ? cstN ? cst4 : cst3
4640    # op is == or != and N is 1 or 2
4641    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
4642    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
4643    of cst3 and cst4 is smaller.
4644    This was originally done by two_value_replacement in phiopt (PR 88676).  */
4645 (for eqne (ne eq)
4646  (simplify
4647   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
4648   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4649        && INTEGRAL_TYPE_P (type)
4650        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
4651            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
4652    (with {
4653      value_range r;
4654      get_range_query (cfun)->range_of_expr (r, @0);
4655      if (r.undefined_p ())
4656        r.set_varying (TREE_TYPE (@0));
4658      wide_int min = r.lower_bound ();
4659      wide_int max = r.upper_bound ();
4660     }
4661     (if (min + 1 == max
4662          && (wi::to_wide (@1) == min
4663              || wi::to_wide (@1) == max))
4664      (with {
4665        tree arg0 = @2, arg1 = @3;
4666        tree type1;
4667        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
4668          std::swap (arg0, arg1);
4669        if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
4670          {
4671            /* Avoid performing the arithmetics in bool type which has different
4672               semantics, otherwise prefer unsigned types from the two with
4673              the same precision.  */
4674            if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE
4675                || !TYPE_UNSIGNED (type))
4676              type1 = TREE_TYPE (@0);
4677            else
4678              type1 = TREE_TYPE (arg0);
4679          }
4680        else if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4681          type1 = TREE_TYPE (@0);
4682        else
4683          type1 = type;
4684        min = wide_int::from (min, TYPE_PRECISION (type1),
4685                              TYPE_SIGN (TREE_TYPE (@0)));
4686        wide_int a = wide_int::from (wi::to_wide (arg0), TYPE_PRECISION (type1),
4687                                     TYPE_SIGN (type));
4688        enum tree_code code;
4689        wi::overflow_type ovf;
4690        if (tree_int_cst_lt (arg0, arg1))
4691          {
4692            code = PLUS_EXPR;
4693            a -= min;
4694            if (!TYPE_UNSIGNED (type1))
4695              {
4696                /* lhs is known to be in range [min, min+1] and we want to add a
4697                   to it.  Check if that operation can overflow for those 2 values
4698                   and if yes, force unsigned type.  */
4699                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
4700                if (ovf)
4701                  type1 = unsigned_type_for (type1);
4702              }
4703          }
4704        else
4705          {
4706            code = MINUS_EXPR;
4707            a += min;
4708            if (!TYPE_UNSIGNED (type1))
4709              {
4710                /* lhs is known to be in range [min, min+1] and we want to subtract
4711                   it from a.  Check if that operation can overflow for those 2
4712                   values and if yes, force unsigned type.  */
4713                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
4714                if (ovf)
4715                 type1 = unsigned_type_for (type1);
4716              }
4717          }
4718        tree arg = wide_int_to_tree (type1, a);
4719       }
4720       (if (code == PLUS_EXPR)
4721        (convert (plus (convert:type1 @0) { arg; }))
4722        (convert (minus { arg; } (convert:type1 @0)))
4723       )
4724      )
4725     )
4726    )
4727   )
4730 #endif
4732 (simplify
4733  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
4734  (if (INTEGRAL_TYPE_P (type)
4735       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4736   (cond @1 (convert @2) (convert @3))))
4738 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
4739    be extended.  */
4740 /* This pattern implements two kinds simplification:
4742    Case 1)
4743    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4744      1) Conversions are type widening from smaller type.
4745      2) Const c1 equals to c2 after canonicalizing comparison.
4746      3) Comparison has tree code LT, LE, GT or GE.
4747    This specific pattern is needed when (cmp (convert x) c) may not
4748    be simplified by comparison patterns because of multiple uses of
4749    x.  It also makes sense here because simplifying across multiple
4750    referred var is always benefitial for complicated cases.
4752    Case 2)
4753    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
4754 (for cmp (lt le gt ge eq)
4755  (simplify
4756   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4757   (with
4758    {
4759      tree from_type = TREE_TYPE (@1);
4760      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4761      enum tree_code code = ERROR_MARK;
4763      if (INTEGRAL_TYPE_P (from_type)
4764          && int_fits_type_p (@2, from_type)
4765          && (types_match (c1_type, from_type)
4766              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4767                  && (TYPE_UNSIGNED (from_type)
4768                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4769          && (types_match (c2_type, from_type)
4770              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4771                  && (TYPE_UNSIGNED (from_type)
4772                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4773        {
4774          if (cmp != EQ_EXPR)
4775            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
4776          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
4777          else if (int_fits_type_p (@3, from_type))
4778            code = EQ_EXPR;
4779        }
4780    }
4781    (if (code == MAX_EXPR)
4782     (convert (max @1 (convert @2)))
4783     (if (code == MIN_EXPR)
4784      (convert (min @1 (convert @2)))
4785      (if (code == EQ_EXPR)
4786       (convert (cond (eq @1 (convert @3))
4787                      (convert:from_type @3) (convert:from_type @2)))))))))
4789 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4791      1) OP is PLUS or MINUS.
4792      2) CMP is LT, LE, GT or GE.
4793      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4795    This pattern also handles special cases like:
4797      A) Operand x is a unsigned to signed type conversion and c1 is
4798         integer zero.  In this case,
4799           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
4800           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
4801      B) Const c1 may not equal to (C3 op' C2).  In this case we also
4802         check equality for (c1+1) and (c1-1) by adjusting comparison
4803         code.
4805    TODO: Though signed type is handled by this pattern, it cannot be
4806    simplified at the moment because C standard requires additional
4807    type promotion.  In order to match&simplify it here, the IR needs
4808    to be cleaned up by other optimizers, i.e, VRP.  */
4809 (for op (plus minus)
4810  (for cmp (lt le gt ge)
4811   (simplify
4812    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4813    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4814     (if (types_match (from_type, to_type)
4815          /* Check if it is special case A).  */
4816          || (TYPE_UNSIGNED (from_type)
4817              && !TYPE_UNSIGNED (to_type)
4818              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4819              && integer_zerop (@1)
4820              && (cmp == LT_EXPR || cmp == GE_EXPR)))
4821      (with
4822       {
4823         wi::overflow_type overflow = wi::OVF_NONE;
4824         enum tree_code code, cmp_code = cmp;
4825         wide_int real_c1;
4826         wide_int c1 = wi::to_wide (@1);
4827         wide_int c2 = wi::to_wide (@2);
4828         wide_int c3 = wi::to_wide (@3);
4829         signop sgn = TYPE_SIGN (from_type);
4831         /* Handle special case A), given x of unsigned type:
4832             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
4833             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
4834         if (!types_match (from_type, to_type))
4835           {
4836             if (cmp_code == LT_EXPR)
4837               cmp_code = GT_EXPR;
4838             if (cmp_code == GE_EXPR)
4839               cmp_code = LE_EXPR;
4840             c1 = wi::max_value (to_type);
4841           }
4842         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
4843            compute (c3 op' c2) and check if it equals to c1 with op' being
4844            the inverted operator of op.  Make sure overflow doesn't happen
4845            if it is undefined.  */
4846         if (op == PLUS_EXPR)
4847           real_c1 = wi::sub (c3, c2, sgn, &overflow);
4848         else
4849           real_c1 = wi::add (c3, c2, sgn, &overflow);
4851         code = cmp_code;
4852         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4853           {
4854             /* Check if c1 equals to real_c1.  Boundary condition is handled
4855                by adjusting comparison operation if necessary.  */
4856             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4857                 && !overflow)
4858               {
4859                 /* X <= Y - 1 equals to X < Y.  */
4860                 if (cmp_code == LE_EXPR)
4861                   code = LT_EXPR;
4862                 /* X > Y - 1 equals to X >= Y.  */
4863                 if (cmp_code == GT_EXPR)
4864                   code = GE_EXPR;
4865               }
4866             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4867                 && !overflow)
4868               {
4869                 /* X < Y + 1 equals to X <= Y.  */
4870                 if (cmp_code == LT_EXPR)
4871                   code = LE_EXPR;
4872                 /* X >= Y + 1 equals to X > Y.  */
4873                 if (cmp_code == GE_EXPR)
4874                   code = GT_EXPR;
4875               }
4876             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4877               {
4878                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4879                   code = MIN_EXPR;
4880                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4881                   code = MAX_EXPR;
4882               }
4883           }
4884       }
4885       (if (code == MAX_EXPR)
4886        (op (max @X { wide_int_to_tree (from_type, real_c1); })
4887            { wide_int_to_tree (from_type, c2); })
4888        (if (code == MIN_EXPR)
4889         (op (min @X { wide_int_to_tree (from_type, real_c1); })
4890             { wide_int_to_tree (from_type, c2); })))))))))
4892 #if GIMPLE
4893 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
4894    in fold_cond_expr_with_comparison for GENERIC folding with
4895    some extra constraints.  */
4896 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
4897  (simplify
4898   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
4899         (convert3? @0) (convert4? @1))
4900   (if (!HONOR_SIGNED_ZEROS (type)
4901        && (/* Allow widening conversions of the compare operands as data.  */
4902            (INTEGRAL_TYPE_P (type)
4903             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
4904             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
4905             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
4906             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
4907            /* Or sign conversions for the comparison.  */
4908            || (types_match (type, TREE_TYPE (@0))
4909                && types_match (type, TREE_TYPE (@1)))))
4910    (switch
4911     (if (cmp == EQ_EXPR)
4912      (if (VECTOR_TYPE_P (type))
4913       (view_convert @c1)
4914       (convert @c1)))
4915     (if (cmp == NE_EXPR)
4916      (if (VECTOR_TYPE_P (type))
4917       (view_convert @c0)
4918       (convert @c0)))
4919     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
4920      (if (!HONOR_NANS (type))
4921       (if (VECTOR_TYPE_P (type))
4922        (view_convert (min @c0 @c1))
4923        (convert (min @c0 @c1)))))
4924     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
4925      (if (!HONOR_NANS (type))
4926       (if (VECTOR_TYPE_P (type))
4927        (view_convert (max @c0 @c1))
4928        (convert (max @c0 @c1)))))
4929     (if (cmp == UNEQ_EXPR)
4930      (if (!HONOR_NANS (type))
4931       (if (VECTOR_TYPE_P (type))
4932        (view_convert @c1)
4933        (convert @c1))))
4934     (if (cmp == LTGT_EXPR)
4935      (if (!HONOR_NANS (type))
4936       (if (VECTOR_TYPE_P (type))
4937        (view_convert @c0)
4938        (convert @c0))))))))
4939 #endif
4941 /* These was part of minmax phiopt.  */
4942 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
4943    to minmax<min/max<a, b>, c> */
4944 (for minmax (min max)
4945  (for cmp (lt le gt ge)
4946   (simplify
4947    (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
4948    (with
4949     {
4950       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
4951     }
4952     (if (code == MIN_EXPR)
4953      (minmax (min @1 @2) @4)
4954      (if (code == MAX_EXPR)
4955       (minmax (max @1 @2) @4)))))))
4957 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
4958 (simplify
4959  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
4960  (if (!TYPE_SATURATING (type)
4961       && (TYPE_OVERFLOW_WRAPS (type)
4962           || !wi::only_sign_bit_p (wi::to_wide (@1)))
4963       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
4964   @3))
4966 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
4967 (simplify
4968  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
4969  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
4970   @3))
4972 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
4973    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
4974 (simplify
4975  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
4976  (if (TYPE_UNSIGNED (type))
4977   (cond (ge @0 @1) (negate @0) @2)))
4979 (for cnd (cond vec_cond)
4980  /* A ? B : (A ? X : C) -> A ? B : C.  */
4981  (simplify
4982   (cnd @0 (cnd @0 @1 @2) @3)
4983   (cnd @0 @1 @3))
4984  (simplify
4985   (cnd @0 @1 (cnd @0 @2 @3))
4986   (cnd @0 @1 @3))
4987  /* A ? B : (!A ? C : X) -> A ? B : C.  */
4988  /* ???  This matches embedded conditions open-coded because genmatch
4989     would generate matching code for conditions in separate stmts only.
4990     The following is still important to merge then and else arm cases
4991     from if-conversion.  */
4992  (simplify
4993   (cnd @0 @1 (cnd @2 @3 @4))
4994   (if (inverse_conditions_p (@0, @2))
4995    (cnd @0 @1 @3)))
4996  (simplify
4997   (cnd @0 (cnd @1 @2 @3) @4)
4998   (if (inverse_conditions_p (@0, @1))
4999    (cnd @0 @3 @4)))
5001  /* A ? B : B -> B.  */
5002  (simplify
5003   (cnd @0 @1 @1)
5004   @1)
5006  /* !A ? B : C -> A ? C : B.  */
5007  (simplify
5008   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5009   (cnd @0 @2 @1)))
5011 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5012    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5013    Need to handle UN* comparisons.
5015    None of these transformations work for modes with signed
5016    zeros.  If A is +/-0, the first two transformations will
5017    change the sign of the result (from +0 to -0, or vice
5018    versa).  The last four will fix the sign of the result,
5019    even though the original expressions could be positive or
5020    negative, depending on the sign of A.
5022    Note that all these transformations are correct if A is
5023    NaN, since the two alternatives (A and -A) are also NaNs.  */
5025 (for cnd (cond vec_cond)
5026  /* A == 0 ? A : -A    same as -A */
5027  (for cmp (eq uneq)
5028   (simplify
5029    (cnd (cmp @0 zerop) @0 (negate@1 @0))
5030     (if (!HONOR_SIGNED_ZEROS (type))
5031      @1))
5032   (simplify
5033    (cnd (cmp @0 zerop) zerop (negate@1 @0))
5034     (if (!HONOR_SIGNED_ZEROS (type))
5035      @1))
5037  /* A != 0 ? A : -A    same as A */
5038  (for cmp (ne ltgt)
5039   (simplify
5040    (cnd (cmp @0 zerop) @0 (negate @0))
5041     (if (!HONOR_SIGNED_ZEROS (type))
5042      @0))
5043   (simplify
5044    (cnd (cmp @0 zerop) @0 integer_zerop)
5045     (if (!HONOR_SIGNED_ZEROS (type))
5046      @0))
5048  /* A >=/> 0 ? A : -A    same as abs (A) */
5049  (for cmp (ge gt)
5050   (simplify
5051    (cnd (cmp @0 zerop) @0 (negate @0))
5052     (if (!HONOR_SIGNED_ZEROS (type)
5053          && !TYPE_UNSIGNED (type))
5054      (abs @0))))
5055  /* A <=/< 0 ? A : -A    same as -abs (A) */
5056  (for cmp (le lt)
5057   (simplify
5058    (cnd (cmp @0 zerop) @0 (negate @0))
5059     (if (!HONOR_SIGNED_ZEROS (type)
5060          && !TYPE_UNSIGNED (type))
5061      (if (ANY_INTEGRAL_TYPE_P (type)
5062           && !TYPE_OVERFLOW_WRAPS (type))
5063       (with {
5064         tree utype = unsigned_type_for (type);
5065        }
5066        (convert (negate (absu:utype @0))))
5067        (negate (abs @0)))))
5071 /* -(type)!A -> (type)A - 1.  */
5072 (simplify
5073  (negate (convert?:s (logical_inverted_value:s @0)))
5074  (if (INTEGRAL_TYPE_P (type)
5075       && TREE_CODE (type) != BOOLEAN_TYPE
5076       && TYPE_PRECISION (type) > 1
5077       && TREE_CODE (@0) == SSA_NAME
5078       && ssa_name_has_boolean_range (@0))
5079   (plus (convert:type @0) { build_all_ones_cst (type); })))
5081 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5082    return all -1 or all 0 results.  */
5083 /* ??? We could instead convert all instances of the vec_cond to negate,
5084    but that isn't necessarily a win on its own.  */
5085 (simplify
5086  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5087  (if (VECTOR_TYPE_P (type)
5088       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5089                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5090       && (TYPE_MODE (TREE_TYPE (type))
5091           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5092   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5094 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5095 (simplify
5096  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5097  (if (VECTOR_TYPE_P (type)
5098       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5099                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5100       && (TYPE_MODE (TREE_TYPE (type))
5101           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5102   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5105 /* Simplifications of comparisons.  */
5107 /* See if we can reduce the magnitude of a constant involved in a
5108    comparison by changing the comparison code.  This is a canonicalization
5109    formerly done by maybe_canonicalize_comparison_1.  */
5110 (for cmp  (le gt)
5111      acmp (lt ge)
5112  (simplify
5113   (cmp @0 uniform_integer_cst_p@1)
5114   (with { tree cst = uniform_integer_cst_p (@1); }
5115    (if (tree_int_cst_sgn (cst) == -1)
5116      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5117                                    wide_int_to_tree (TREE_TYPE (cst),
5118                                                      wi::to_wide (cst)
5119                                                      + 1)); })))))
5120 (for cmp  (ge lt)
5121      acmp (gt le)
5122  (simplify
5123   (cmp @0 uniform_integer_cst_p@1)
5124   (with { tree cst = uniform_integer_cst_p (@1); }
5125    (if (tree_int_cst_sgn (cst) == 1)
5126     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5127                                   wide_int_to_tree (TREE_TYPE (cst),
5128                                   wi::to_wide (cst) - 1)); })))))
5130 /* We can simplify a logical negation of a comparison to the
5131    inverted comparison.  As we cannot compute an expression
5132    operator using invert_tree_comparison we have to simulate
5133    that with expression code iteration.  */
5134 (for cmp (tcc_comparison)
5135      icmp (inverted_tcc_comparison)
5136      ncmp (inverted_tcc_comparison_with_nans)
5137  /* Ideally we'd like to combine the following two patterns
5138     and handle some more cases by using
5139       (logical_inverted_value (cmp @0 @1))
5140     here but for that genmatch would need to "inline" that.
5141     For now implement what forward_propagate_comparison did.  */
5142  (simplify
5143   (bit_not (cmp @0 @1))
5144   (if (VECTOR_TYPE_P (type)
5145        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5146    /* Comparison inversion may be impossible for trapping math,
5147       invert_tree_comparison will tell us.  But we can't use
5148       a computed operator in the replacement tree thus we have
5149       to play the trick below.  */
5150    (with { enum tree_code ic = invert_tree_comparison
5151              (cmp, HONOR_NANS (@0)); }
5152     (if (ic == icmp)
5153      (icmp @0 @1)
5154      (if (ic == ncmp)
5155       (ncmp @0 @1))))))
5156  (simplify
5157   (bit_xor (cmp @0 @1) integer_truep)
5158   (with { enum tree_code ic = invert_tree_comparison
5159             (cmp, HONOR_NANS (@0)); }
5160    (if (ic == icmp)
5161     (icmp @0 @1)
5162     (if (ic == ncmp)
5163      (ncmp @0 @1)))))
5164  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5165  (simplify
5166   (ne (cmp@2 @0 @1) integer_zerop)
5167   (if (types_match (type, TREE_TYPE (@2)))
5168    (cmp @0 @1)))
5169  (simplify
5170   (eq (cmp@2 @0 @1) integer_truep)
5171   (if (types_match (type, TREE_TYPE (@2)))
5172    (cmp @0 @1)))
5173  (simplify
5174   (ne (cmp@2 @0 @1) integer_truep)
5175   (if (types_match (type, TREE_TYPE (@2)))
5176    (with { enum tree_code ic = invert_tree_comparison
5177              (cmp, HONOR_NANS (@0)); }
5178     (if (ic == icmp)
5179      (icmp @0 @1)
5180      (if (ic == ncmp)
5181       (ncmp @0 @1))))))
5182  (simplify
5183   (eq (cmp@2 @0 @1) integer_zerop)
5184   (if (types_match (type, TREE_TYPE (@2)))
5185    (with { enum tree_code ic = invert_tree_comparison
5186              (cmp, HONOR_NANS (@0)); }
5187     (if (ic == icmp)
5188      (icmp @0 @1)
5189      (if (ic == ncmp)
5190       (ncmp @0 @1)))))))
5192 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5193    ??? The transformation is valid for the other operators if overflow
5194    is undefined for the type, but performing it here badly interacts
5195    with the transformation in fold_cond_expr_with_comparison which
5196    attempts to synthetize ABS_EXPR.  */
5197 (for cmp (eq ne)
5198  (for sub (minus pointer_diff)
5199   (simplify
5200    (cmp (sub@2 @0 @1) integer_zerop)
5201    (if (single_use (@2))
5202     (cmp @0 @1)))))
5204 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5205    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5206 (for cmp (lt ge)
5207  (simplify
5208   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5209    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5210         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5211         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5212     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5213 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5214    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5215 (simplify
5216  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5217   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5218        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5219        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5220    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5222 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5223    signed arithmetic case.  That form is created by the compiler
5224    often enough for folding it to be of value.  One example is in
5225    computing loop trip counts after Operator Strength Reduction.  */
5226 (for cmp (simple_comparison)
5227      scmp (swapped_simple_comparison)
5228  (simplify
5229   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5230   /* Handle unfolded multiplication by zero.  */
5231   (if (integer_zerop (@1))
5232    (cmp @1 @2)
5233    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5234         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5235         && single_use (@3))
5236     /* If @1 is negative we swap the sense of the comparison.  */
5237     (if (tree_int_cst_sgn (@1) < 0)
5238      (scmp @0 @2)
5239      (cmp @0 @2))))))
5241 /* For integral types with undefined overflow fold
5242    x * C1 == C2 into x == C2 / C1 or false.
5243    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5244    Z / 2^n Z.  */
5245 (for cmp (eq ne)
5246  (simplify
5247   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5248   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5249        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5250        && wi::to_wide (@1) != 0)
5251    (with { widest_int quot; }
5252     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5253                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
5254      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5255      { constant_boolean_node (cmp == NE_EXPR, type); }))
5256    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5257         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5258         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5259     (cmp @0
5260      {
5261        tree itype = TREE_TYPE (@0);
5262        int p = TYPE_PRECISION (itype);
5263        wide_int m = wi::one (p + 1) << p;
5264        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5265        wide_int i = wide_int::from (wi::mod_inv (a, m),
5266                                     p, TYPE_SIGN (itype));
5267        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5268      })))))
5270 /* Simplify comparison of something with itself.  For IEEE
5271    floating-point, we can only do some of these simplifications.  */
5272 (for cmp (eq ge le)
5273  (simplify
5274   (cmp @0 @0)
5275   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5276        || ! tree_expr_maybe_nan_p (@0))
5277    { constant_boolean_node (true, type); }
5278    (if (cmp != EQ_EXPR
5279         /* With -ftrapping-math conversion to EQ loses an exception.  */
5280         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5281             || ! flag_trapping_math))
5282     (eq @0 @0)))))
5283 (for cmp (ne gt lt)
5284  (simplify
5285   (cmp @0 @0)
5286   (if (cmp != NE_EXPR
5287        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5288        || ! tree_expr_maybe_nan_p (@0))
5289    { constant_boolean_node (false, type); })))
5290 (for cmp (unle unge uneq)
5291  (simplify
5292   (cmp @0 @0)
5293   { constant_boolean_node (true, type); }))
5294 (for cmp (unlt ungt)
5295  (simplify
5296   (cmp @0 @0)
5297   (unordered @0 @0)))
5298 (simplify
5299  (ltgt @0 @0)
5300  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5301   { constant_boolean_node (false, type); }))
5303 /* x == ~x -> false */
5304 /* x != ~x -> true */
5305 (for cmp (eq ne)
5306  (simplify
5307   (cmp:c @0 (bit_not @0))
5308   { constant_boolean_node (cmp == NE_EXPR, type); }))
5310 /* Fold ~X op ~Y as Y op X.  */
5311 (for cmp (simple_comparison)
5312  (simplify
5313   (cmp (bit_not@2 @0) (bit_not@3 @1))
5314   (if (single_use (@2) && single_use (@3))
5315    (cmp @1 @0))))
5317 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
5318 (for cmp (simple_comparison)
5319      scmp (swapped_simple_comparison)
5320  (simplify
5321   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5322   (if (single_use (@2)
5323        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5324    (scmp @0 (bit_not @1)))))
5326 (for cmp (simple_comparison)
5327  (simplify
5328   (cmp @0 REAL_CST@1)
5329   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
5330   (switch
5331    /* a CMP (-0) -> a CMP 0  */
5332    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5333     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5334    /* (-0) CMP b -> 0 CMP b.  */
5335    (if (TREE_CODE (@0) == REAL_CST
5336         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5337     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5338    /* x != NaN is always true, other ops are always false.  */
5339    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5340         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5341         && !tree_expr_signaling_nan_p (@1)
5342         && !tree_expr_maybe_signaling_nan_p (@0))
5343     { constant_boolean_node (cmp == NE_EXPR, type); })
5344    /* NaN != y is always true, other ops are always false.  */
5345    (if (TREE_CODE (@0) == REAL_CST
5346         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5347         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5348         && !tree_expr_signaling_nan_p (@0)
5349         && !tree_expr_signaling_nan_p (@1))
5350     { constant_boolean_node (cmp == NE_EXPR, type); })
5351    /* Fold comparisons against infinity.  */
5352    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5353         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5354     (with
5355      {
5356        REAL_VALUE_TYPE max;
5357        enum tree_code code = cmp;
5358        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5359        if (neg)
5360          code = swap_tree_comparison (code);
5361      }
5362      (switch
5363       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
5364       (if (code == GT_EXPR
5365            && !(HONOR_NANS (@0) && flag_trapping_math))
5366        { constant_boolean_node (false, type); })
5367       (if (code == LE_EXPR)
5368        /* x <= +Inf is always true, if we don't care about NaNs.  */
5369        (if (! HONOR_NANS (@0))
5370         { constant_boolean_node (true, type); }
5371         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5372            an "invalid" exception.  */
5373         (if (!flag_trapping_math)
5374          (eq @0 @0))))
5375       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5376          for == this introduces an exception for x a NaN.  */
5377       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5378            || code == GE_EXPR)
5379        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5380         (if (neg)
5381          (lt @0 { build_real (TREE_TYPE (@0), max); })
5382          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5383       /* x < +Inf is always equal to x <= DBL_MAX.  */
5384       (if (code == LT_EXPR)
5385        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5386         (if (neg)
5387          (ge @0 { build_real (TREE_TYPE (@0), max); })
5388          (le @0 { build_real (TREE_TYPE (@0), max); }))))
5389       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5390          an exception for x a NaN so use an unordered comparison.  */
5391       (if (code == NE_EXPR)
5392        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5393         (if (! HONOR_NANS (@0))
5394          (if (neg)
5395           (ge @0 { build_real (TREE_TYPE (@0), max); })
5396           (le @0 { build_real (TREE_TYPE (@0), max); }))
5397          (if (neg)
5398           (unge @0 { build_real (TREE_TYPE (@0), max); })
5399           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5401  /* If this is a comparison of a real constant with a PLUS_EXPR
5402     or a MINUS_EXPR of a real constant, we can convert it into a
5403     comparison with a revised real constant as long as no overflow
5404     occurs when unsafe_math_optimizations are enabled.  */
5405  (if (flag_unsafe_math_optimizations)
5406   (for op (plus minus)
5407    (simplify
5408     (cmp (op @0 REAL_CST@1) REAL_CST@2)
5409     (with
5410      {
5411        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5412                                TREE_TYPE (@1), @2, @1);
5413      }
5414      (if (tem && !TREE_OVERFLOW (tem))
5415       (cmp @0 { tem; }))))))
5417  /* Likewise, we can simplify a comparison of a real constant with
5418     a MINUS_EXPR whose first operand is also a real constant, i.e.
5419     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
5420     floating-point types only if -fassociative-math is set.  */
5421  (if (flag_associative_math)
5422   (simplify
5423    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5424    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5425     (if (tem && !TREE_OVERFLOW (tem))
5426      (cmp { tem; } @1)))))
5428  /* Fold comparisons against built-in math functions.  */
5429  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5430   (for sq (SQRT)
5431    (simplify
5432     (cmp (sq @0) REAL_CST@1)
5433     (switch
5434      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5435       (switch
5436        /* sqrt(x) < y is always false, if y is negative.  */
5437        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5438         { constant_boolean_node (false, type); })
5439        /* sqrt(x) > y is always true, if y is negative and we
5440           don't care about NaNs, i.e. negative values of x.  */
5441        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5442         { constant_boolean_node (true, type); })
5443        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5444        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5445      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5446       (switch
5447        /* sqrt(x) < 0 is always false.  */
5448        (if (cmp == LT_EXPR)
5449         { constant_boolean_node (false, type); })
5450        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
5451        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5452         { constant_boolean_node (true, type); })
5453        /* sqrt(x) <= 0 -> x == 0.  */
5454        (if (cmp == LE_EXPR)
5455         (eq @0 @1))
5456        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
5457           == or !=.  In the last case:
5459             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5461           if x is negative or NaN.  Due to -funsafe-math-optimizations,
5462           the results for other x follow from natural arithmetic.  */
5463        (cmp @0 @1)))
5464      (if ((cmp == LT_EXPR
5465            || cmp == LE_EXPR
5466            || cmp == GT_EXPR
5467            || cmp == GE_EXPR)
5468           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5469           /* Give up for -frounding-math.  */
5470           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5471       (with
5472        {
5473          REAL_VALUE_TYPE c2;
5474          enum tree_code ncmp = cmp;
5475          const real_format *fmt
5476            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5477          real_arithmetic (&c2, MULT_EXPR,
5478                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5479          real_convert (&c2, fmt, &c2);
5480          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5481             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
5482          if (!REAL_VALUE_ISINF (c2))
5483            {
5484              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5485                                         build_real (TREE_TYPE (@0), c2));
5486              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5487                ncmp = ERROR_MARK;
5488              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
5489                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
5490                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
5491              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
5492                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
5493                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
5494              else
5495                {
5496                  /* With rounding to even, sqrt of up to 3 different values
5497                     gives the same normal result, so in some cases c2 needs
5498                     to be adjusted.  */
5499                  REAL_VALUE_TYPE c2alt, tow;
5500                  if (cmp == LT_EXPR || cmp == GE_EXPR)
5501                    tow = dconst0;
5502                  else
5503                    tow = dconstinf;
5504                  real_nextafter (&c2alt, fmt, &c2, &tow);
5505                  real_convert (&c2alt, fmt, &c2alt);
5506                  if (REAL_VALUE_ISINF (c2alt))
5507                    ncmp = ERROR_MARK;
5508                  else
5509                    {
5510                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
5511                                            build_real (TREE_TYPE (@0), c2alt));
5512                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
5513                        ncmp = ERROR_MARK;
5514                      else if (real_equal (&TREE_REAL_CST (c3),
5515                                           &TREE_REAL_CST (@1)))
5516                        c2 = c2alt;
5517                    }
5518                }
5519            }
5520        }
5521        (if (cmp == GT_EXPR || cmp == GE_EXPR)
5522         (if (REAL_VALUE_ISINF (c2))
5523          /* sqrt(x) > y is x == +Inf, when y is very large.  */
5524          (if (HONOR_INFINITIES (@0))
5525           (eq @0 { build_real (TREE_TYPE (@0), c2); })
5526           { constant_boolean_node (false, type); })
5527          /* sqrt(x) > c is the same as x > c*c.  */
5528          (if (ncmp != ERROR_MARK)
5529           (if (ncmp == GE_EXPR)
5530            (ge @0 { build_real (TREE_TYPE (@0), c2); })
5531            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
5532         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
5533         (if (REAL_VALUE_ISINF (c2))
5534          (switch
5535           /* sqrt(x) < y is always true, when y is a very large
5536              value and we don't care about NaNs or Infinities.  */
5537           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
5538            { constant_boolean_node (true, type); })
5539           /* sqrt(x) < y is x != +Inf when y is very large and we
5540              don't care about NaNs.  */
5541           (if (! HONOR_NANS (@0))
5542            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
5543           /* sqrt(x) < y is x >= 0 when y is very large and we
5544              don't care about Infinities.  */
5545           (if (! HONOR_INFINITIES (@0))
5546            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
5547           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
5548           (if (GENERIC)
5549            (truth_andif
5550             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5551             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
5552          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
5553          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
5554           (if (ncmp == LT_EXPR)
5555            (lt @0 { build_real (TREE_TYPE (@0), c2); })
5556            (le @0 { build_real (TREE_TYPE (@0), c2); }))
5557           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
5558           (if (ncmp != ERROR_MARK && GENERIC)
5559            (if (ncmp == LT_EXPR)
5560             (truth_andif
5561              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5562              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
5563             (truth_andif
5564              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
5565              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
5566    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
5567    (simplify
5568     (cmp (sq @0) (sq @1))
5569       (if (! HONOR_NANS (@0))
5570         (cmp @0 @1))))))
5572 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
5573 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
5574      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
5575  (simplify
5576   (cmp (float@0 @1) (float @2))
5577    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5578         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5579     (with
5580      {
5581        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5582        tree type1 = TREE_TYPE (@1);
5583        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5584        tree type2 = TREE_TYPE (@2);
5585        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5586      }
5587      (if (fmt.can_represent_integral_type_p (type1)
5588           && fmt.can_represent_integral_type_p (type2))
5589       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5590        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5591        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5592             && type1_signed_p >= type2_signed_p)
5593         (icmp @1 (convert @2))
5594         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5595              && type1_signed_p <= type2_signed_p)
5596          (icmp (convert:type2 @1) @2)
5597          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5598               && type1_signed_p == type2_signed_p)
5599           (icmp @1 @2))))))))))
5601 /* Optimize various special cases of (FTYPE) N CMP CST.  */
5602 (for cmp  (lt le eq ne ge gt)
5603      icmp (le le eq ne ge ge)
5604  (simplify
5605   (cmp (float @0) REAL_CST@1)
5606    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5607         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5608     (with
5609      {
5610        tree itype = TREE_TYPE (@0);
5611        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5612        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5613        /* Be careful to preserve any potential exceptions due to
5614           NaNs.  qNaNs are ok in == or != context.
5615           TODO: relax under -fno-trapping-math or
5616           -fno-signaling-nans.  */
5617        bool exception_p
5618          = real_isnan (cst) && (cst->signalling
5619                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5620      }
5621      /* TODO: allow non-fitting itype and SNaNs when
5622         -fno-trapping-math.  */
5623      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5624       (with
5625        {
5626          signop isign = TYPE_SIGN (itype);
5627          REAL_VALUE_TYPE imin, imax;
5628          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5629          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5631          REAL_VALUE_TYPE icst;
5632          if (cmp == GT_EXPR || cmp == GE_EXPR)
5633            real_ceil (&icst, fmt, cst);
5634          else if (cmp == LT_EXPR || cmp == LE_EXPR)
5635            real_floor (&icst, fmt, cst);
5636          else
5637            real_trunc (&icst, fmt, cst);
5639          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5641          bool overflow_p = false;
5642          wide_int icst_val
5643            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5644        }
5645        (switch
5646         /* Optimize cases when CST is outside of ITYPE's range.  */
5647         (if (real_compare (LT_EXPR, cst, &imin))
5648          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5649                                   type); })
5650         (if (real_compare (GT_EXPR, cst, &imax))
5651          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5652                                   type); })
5653         /* Remove cast if CST is an integer representable by ITYPE.  */
5654         (if (cst_int_p)
5655          (cmp @0 { gcc_assert (!overflow_p);
5656                    wide_int_to_tree (itype, icst_val); })
5657         )
5658         /* When CST is fractional, optimize
5659             (FTYPE) N == CST -> 0
5660             (FTYPE) N != CST -> 1.  */
5661         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5662          { constant_boolean_node (cmp == NE_EXPR, type); })
5663         /* Otherwise replace with sensible integer constant.  */
5664         (with
5665          {
5666            gcc_checking_assert (!overflow_p);
5667          }
5668          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5670 /* Fold A /[ex] B CMP C to A CMP B * C.  */
5671 (for cmp (eq ne)
5672  (simplify
5673   (cmp (exact_div @0 @1) INTEGER_CST@2)
5674   (if (!integer_zerop (@1))
5675    (if (wi::to_wide (@2) == 0)
5676     (cmp @0 @2)
5677     (if (TREE_CODE (@1) == INTEGER_CST)
5678      (with
5679       {
5680         wi::overflow_type ovf;
5681         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5682                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5683       }
5684       (if (ovf)
5685        { constant_boolean_node (cmp == NE_EXPR, type); }
5686        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5687 (for cmp (lt le gt ge)
5688  (simplify
5689   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5690   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5691    (with
5692     {
5693       wi::overflow_type ovf;
5694       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5695                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5696     }
5697     (if (ovf)
5698      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5699                                         TYPE_SIGN (TREE_TYPE (@2)))
5700                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5701      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5703 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5705    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5706    For large C (more than min/B+2^size), this is also true, with the
5707    multiplication computed modulo 2^size.
5708    For intermediate C, this just tests the sign of A.  */
5709 (for cmp  (lt le gt ge)
5710      cmp2 (ge ge lt lt)
5711  (simplify
5712   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5713   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5714        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5715        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5716    (with
5717     {
5718       tree utype = TREE_TYPE (@2);
5719       wide_int denom = wi::to_wide (@1);
5720       wide_int right = wi::to_wide (@2);
5721       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5722       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5723       bool small = wi::leu_p (right, smax);
5724       bool large = wi::geu_p (right, smin);
5725     }
5726     (if (small || large)
5727      (cmp (convert:utype @0) (mult @2 (convert @1)))
5728      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5730 /* Unordered tests if either argument is a NaN.  */
5731 (simplify
5732  (bit_ior (unordered @0 @0) (unordered @1 @1))
5733  (if (types_match (@0, @1))
5734   (unordered @0 @1)))
5735 (simplify
5736  (bit_and (ordered @0 @0) (ordered @1 @1))
5737  (if (types_match (@0, @1))
5738   (ordered @0 @1)))
5739 (simplify
5740  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5741  @2)
5742 (simplify
5743  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5744  @2)
5746 /* Simple range test simplifications.  */
5747 /* A < B || A >= B -> true.  */
5748 (for test1 (lt le le le ne ge)
5749      test2 (ge gt ge ne eq ne)
5750  (simplify
5751   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5752   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5753        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5754    { constant_boolean_node (true, type); })))
5755 /* A < B && A >= B -> false.  */
5756 (for test1 (lt lt lt le ne eq)
5757      test2 (ge gt eq gt eq gt)
5758  (simplify
5759   (bit_and:c (test1 @0 @1) (test2 @0 @1))
5760   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5761        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5762    { constant_boolean_node (false, type); })))
5764 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5765    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
5767    Note that comparisons
5768      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
5769      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
5770    will be canonicalized to above so there's no need to
5771    consider them here.
5772  */
5774 (for cmp (le gt)
5775      eqcmp (eq ne)
5776  (simplify
5777   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5778   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5779    (with
5780     {
5781      tree ty = TREE_TYPE (@0);
5782      unsigned prec = TYPE_PRECISION (ty);
5783      wide_int mask = wi::to_wide (@2, prec);
5784      wide_int rhs = wi::to_wide (@3, prec);
5785      signop sgn = TYPE_SIGN (ty);
5786     }
5787     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5788          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5789       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5790              { build_zero_cst (ty); }))))))
5792 /* -A CMP -B -> B CMP A.  */
5793 (for cmp (tcc_comparison)
5794      scmp (swapped_tcc_comparison)
5795  (simplify
5796   (cmp (negate @0) (negate @1))
5797   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5798        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5799            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5800    (scmp @0 @1)))
5801  (simplify
5802   (cmp (negate @0) CONSTANT_CLASS_P@1)
5803   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5804        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5805            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5806    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5807     (if (tem && !TREE_OVERFLOW (tem))
5808      (scmp @0 { tem; }))))))
5810 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
5811 (for op (abs absu)
5812  (for eqne (eq ne)
5813   (simplify
5814    (eqne (op @0) zerop@1)
5815    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5817 /* From fold_sign_changed_comparison and fold_widened_comparison.
5818    FIXME: the lack of symmetry is disturbing.  */
5819 (for cmp (simple_comparison)
5820  (simplify
5821   (cmp (convert@0 @00) (convert?@1 @10))
5822   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5823        /* Disable this optimization if we're casting a function pointer
5824           type on targets that require function pointer canonicalization.  */
5825        && !(targetm.have_canonicalize_funcptr_for_compare ()
5826             && ((POINTER_TYPE_P (TREE_TYPE (@00))
5827                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5828                 || (POINTER_TYPE_P (TREE_TYPE (@10))
5829                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5830        && single_use (@0))
5831    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5832         && (TREE_CODE (@10) == INTEGER_CST
5833             || @1 != @10)
5834         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5835             || cmp == NE_EXPR
5836             || cmp == EQ_EXPR)
5837         && !POINTER_TYPE_P (TREE_TYPE (@00))
5838         /* (int)bool:32 != (int)uint is not the same as
5839            bool:32 != (bool:32)uint since boolean types only have two valid
5840            values independent of their precision.  */
5841         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
5842             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
5843     /* ???  The special-casing of INTEGER_CST conversion was in the original
5844        code and here to avoid a spurious overflow flag on the resulting
5845        constant which fold_convert produces.  */
5846     (if (TREE_CODE (@1) == INTEGER_CST)
5847      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5848                                 TREE_OVERFLOW (@1)); })
5849      (cmp @00 (convert @1)))
5851     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5852      /* If possible, express the comparison in the shorter mode.  */
5853      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5854            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5855            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5856                && TYPE_UNSIGNED (TREE_TYPE (@00))))
5857           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5858               || ((TYPE_PRECISION (TREE_TYPE (@00))
5859                    >= TYPE_PRECISION (TREE_TYPE (@10)))
5860                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
5861                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
5862               || (TREE_CODE (@10) == INTEGER_CST
5863                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5864                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
5865       (cmp @00 (convert @10))
5866       (if (TREE_CODE (@10) == INTEGER_CST
5867            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5868            && !int_fits_type_p (@10, TREE_TYPE (@00)))
5869        (with
5870         {
5871           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5872           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5873           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5874           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5875         }
5876         (if (above || below)
5877          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5878           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5879           (if (cmp == LT_EXPR || cmp == LE_EXPR)
5880            { constant_boolean_node (above ? true : false, type); }
5881            (if (cmp == GT_EXPR || cmp == GE_EXPR)
5882             { constant_boolean_node (above ? false : true, type); })))))))))
5883    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
5884    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
5885         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5886             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
5887         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5888             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
5889     (with
5890      {
5891        tree type1 = TREE_TYPE (@10);
5892        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
5893          {
5894            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
5895            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
5896                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
5897              type1 = float_type_node;
5898            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
5899                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
5900              type1 = double_type_node;
5901          }
5902       tree newtype
5903         = (TYPE_PRECISION (TREE_TYPE (@00)) > TYPE_PRECISION (type1)
5904            ? TREE_TYPE (@00) : type1);
5905      }
5906      (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (newtype))
5907       (cmp (convert:newtype @00) (convert:newtype @10))))))))
5910 (for cmp (eq ne)
5911  (simplify
5912   /* SSA names are canonicalized to 2nd place.  */
5913   (cmp addr@0 SSA_NAME@1)
5914   (with
5915    {
5916      poly_int64 off; tree base;
5917      tree addr = (TREE_CODE (@0) == SSA_NAME
5918                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5919    }
5920    /* A local variable can never be pointed to by
5921       the default SSA name of an incoming parameter.  */
5922    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5923         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5924         && (base = get_base_address (TREE_OPERAND (addr, 0)))
5925         && TREE_CODE (base) == VAR_DECL
5926         && auto_var_in_fn_p (base, current_function_decl))
5927     (if (cmp == NE_EXPR)
5928      { constant_boolean_node (true, type); }
5929      { constant_boolean_node (false, type); })
5930     /* If the address is based on @1 decide using the offset.  */
5931     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
5932          && TREE_CODE (base) == MEM_REF
5933          && TREE_OPERAND (base, 0) == @1)
5934      (with { off += mem_ref_offset (base).force_shwi (); }
5935       (if (known_ne (off, 0))
5936        { constant_boolean_node (cmp == NE_EXPR, type); }
5937        (if (known_eq (off, 0))
5938         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5940 /* Equality compare simplifications from fold_binary  */
5941 (for cmp (eq ne)
5943  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5944     Similarly for NE_EXPR.  */
5945  (simplify
5946   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5947   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5948        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5949    { constant_boolean_node (cmp == NE_EXPR, type); }))
5951  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
5952  (simplify
5953   (cmp (bit_xor @0 @1) integer_zerop)
5954   (cmp @0 @1))
5956  /* (X ^ Y) == Y becomes X == 0.
5957     Likewise (X ^ Y) == X becomes Y == 0.  */
5958  (simplify
5959   (cmp:c (bit_xor:c @0 @1) @0)
5960   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5962  /* (X & Y) == X becomes (X & ~Y) == 0.  */
5963  (simplify
5964   (cmp:c (bit_and:c @0 @1) @0)
5965   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5966  (simplify
5967   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5968   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5969        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5970        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5971        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5972        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5973        && !wi::neg_p (wi::to_wide (@1)))
5974    (cmp (bit_and @0 (convert (bit_not @1)))
5975         { build_zero_cst (TREE_TYPE (@0)); })))
5977  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
5978  (simplify
5979   (cmp:c (bit_ior:c @0 @1) @1)
5980   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5982  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
5983  (simplify
5984   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5985   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5986    (cmp @0 (bit_xor @1 (convert @2)))))
5988  (simplify
5989   (cmp (convert? addr@0) integer_zerop)
5990   (if (tree_single_nonzero_warnv_p (@0, NULL))
5991    { constant_boolean_node (cmp == NE_EXPR, type); }))
5993  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
5994  (simplify
5995   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5996   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5998 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5999    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6000    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6001    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6002 (for cmp (eq ne)
6003      ncmp (ge lt)
6004  (for sgncmp (ge lt)
6005   (simplify
6006    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6007    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6008         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6009         && types_match (@0, @1))
6010     (ncmp (bit_xor @0 @1) @2)))))
6011 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6012    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6013 (for cmp (eq ne)
6014      ncmp (lt ge)
6015  (simplify
6016   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6017    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6018         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6019         && types_match (@0, @1))
6020     (ncmp (bit_xor @0 @1) @2))))
6022 /* If we have (A & C) == C where C is a power of 2, convert this into
6023    (A & C) != 0.  Similarly for NE_EXPR.  */
6024 (for cmp (eq ne)
6025      icmp (ne eq)
6026  (simplify
6027   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6028   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6030 #if GIMPLE
6031 /* From fold_binary_op_with_conditional_arg handle the case of
6032    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6033    compares simplify.  */
6034 (for cmp (simple_comparison)
6035  (simplify
6036   (cmp:c (cond @0 @1 @2) @3)
6037   /* Do not move possibly trapping operations into the conditional as this
6038      pessimizes code and causes gimplification issues when applied late.  */
6039   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6040        || !operation_could_trap_p (cmp, true, false, @3))
6041    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6042 #endif
6044 (for cmp (ge lt)
6045 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6046 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6047  (simplify
6048   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6049    (if (INTEGRAL_TYPE_P (type)
6050         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6051         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6052         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6053     (with
6054      {
6055        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6056      }
6057     (if (cmp == LT_EXPR)
6058      (bit_xor (convert (rshift @0 {shifter;})) @1)
6059      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6060 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6061 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6062  (simplify
6063   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6064    (if (INTEGRAL_TYPE_P (type)
6065         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6066         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6067         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6068     (with
6069      {
6070        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6071      }
6072     (if (cmp == GE_EXPR)
6073      (bit_xor (convert (rshift @0 {shifter;})) @1)
6074      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6076 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6077    convert this into a shift followed by ANDing with D.  */
6078 (simplify
6079  (cond
6080   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6081   INTEGER_CST@2 integer_zerop)
6082  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6083   (with {
6084      int shift = (wi::exact_log2 (wi::to_wide (@2))
6085                   - wi::exact_log2 (wi::to_wide (@1)));
6086    }
6087    (if (shift > 0)
6088     (bit_and
6089      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6090     (bit_and
6091      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6092      @2)))))
6094 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6095    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6096 (for cmp (eq ne)
6097      ncmp (ge lt)
6098  (simplify
6099   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6100   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6101        && type_has_mode_precision_p (TREE_TYPE (@0))
6102        && element_precision (@2) >= element_precision (@0)
6103        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6104    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6105     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6107 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6108    this into a right shift or sign extension followed by ANDing with C.  */
6109 (simplify
6110  (cond
6111   (lt @0 integer_zerop)
6112   INTEGER_CST@1 integer_zerop)
6113  (if (integer_pow2p (@1)
6114       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6115   (with {
6116     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6117    }
6118    (if (shift >= 0)
6119     (bit_and
6120      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6121      @1)
6122     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6123        sign extension followed by AND with C will achieve the effect.  */
6124     (bit_and (convert @0) @1)))))
6126 /* When the addresses are not directly of decls compare base and offset.
6127    This implements some remaining parts of fold_comparison address
6128    comparisons but still no complete part of it.  Still it is good
6129    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6130 (for cmp (simple_comparison)
6131  (simplify
6132   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6133   (with
6134    {
6135      poly_int64 off0, off1;
6136      tree base0, base1;
6137      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6138                                   off0, off1, GENERIC);
6139    }
6140    (if (equal == 1)
6141     (switch
6142      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6143       { constant_boolean_node (known_eq (off0, off1), type); })
6144      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6145       { constant_boolean_node (known_ne (off0, off1), type); })
6146      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6147       { constant_boolean_node (known_lt (off0, off1), type); })
6148      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6149       { constant_boolean_node (known_le (off0, off1), type); })
6150      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6151       { constant_boolean_node (known_ge (off0, off1), type); })
6152      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6153       { constant_boolean_node (known_gt (off0, off1), type); }))
6154     (if (equal == 0)
6155      (switch
6156       (if (cmp == EQ_EXPR)
6157        { constant_boolean_node (false, type); })
6158       (if (cmp == NE_EXPR)
6159        { constant_boolean_node (true, type); })))))))
6161 /* Simplify pointer equality compares using PTA.  */
6162 (for neeq (ne eq)
6163  (simplify
6164   (neeq @0 @1)
6165   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6166        && ptrs_compare_unequal (@0, @1))
6167    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6169 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6170    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6171    Disable the transform if either operand is pointer to function.
6172    This broke pr22051-2.c for arm where function pointer
6173    canonicalizaion is not wanted.  */
6175 (for cmp (ne eq)
6176  (simplify
6177   (cmp (convert @0) INTEGER_CST@1)
6178   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6179          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6180          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6181          /* Don't perform this optimization in GENERIC if @0 has reference
6182             type when sanitizing.  See PR101210.  */
6183          && !(GENERIC
6184               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6185               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6186         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6187             && POINTER_TYPE_P (TREE_TYPE (@1))
6188             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6189        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6190    (cmp @0 (convert @1)))))
6192 /* Non-equality compare simplifications from fold_binary  */
6193 (for cmp (lt gt le ge)
6194  /* Comparisons with the highest or lowest possible integer of
6195     the specified precision will have known values.  */
6196  (simplify
6197   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6198   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6199         || POINTER_TYPE_P (TREE_TYPE (@1))
6200         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6201        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6202    (with
6203     {
6204       tree cst = uniform_integer_cst_p (@1);
6205       tree arg1_type = TREE_TYPE (cst);
6206       unsigned int prec = TYPE_PRECISION (arg1_type);
6207       wide_int max = wi::max_value (arg1_type);
6208       wide_int signed_max = wi::max_value (prec, SIGNED);
6209       wide_int min = wi::min_value (arg1_type);
6210     }
6211     (switch
6212      (if (wi::to_wide (cst) == max)
6213       (switch
6214        (if (cmp == GT_EXPR)
6215         { constant_boolean_node (false, type); })
6216        (if (cmp == GE_EXPR)
6217         (eq @2 @1))
6218        (if (cmp == LE_EXPR)
6219         { constant_boolean_node (true, type); })
6220        (if (cmp == LT_EXPR)
6221         (ne @2 @1))))
6222      (if (wi::to_wide (cst) == min)
6223       (switch
6224        (if (cmp == LT_EXPR)
6225         { constant_boolean_node (false, type); })
6226        (if (cmp == LE_EXPR)
6227         (eq @2 @1))
6228        (if (cmp == GE_EXPR)
6229         { constant_boolean_node (true, type); })
6230        (if (cmp == GT_EXPR)
6231         (ne @2 @1))))
6232      (if (wi::to_wide (cst) == max - 1)
6233       (switch
6234        (if (cmp == GT_EXPR)
6235         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6236                                     wide_int_to_tree (TREE_TYPE (cst),
6237                                                       wi::to_wide (cst)
6238                                                       + 1)); }))
6239        (if (cmp == LE_EXPR)
6240         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6241                                     wide_int_to_tree (TREE_TYPE (cst),
6242                                                       wi::to_wide (cst)
6243                                                       + 1)); }))))
6244      (if (wi::to_wide (cst) == min + 1)
6245       (switch
6246        (if (cmp == GE_EXPR)
6247         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6248                                     wide_int_to_tree (TREE_TYPE (cst),
6249                                                       wi::to_wide (cst)
6250                                                       - 1)); }))
6251        (if (cmp == LT_EXPR)
6252         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6253                                     wide_int_to_tree (TREE_TYPE (cst),
6254                                                       wi::to_wide (cst)
6255                                                       - 1)); }))))
6256      (if (wi::to_wide (cst) == signed_max
6257           && TYPE_UNSIGNED (arg1_type)
6258           /* We will flip the signedness of the comparison operator
6259              associated with the mode of @1, so the sign bit is
6260              specified by this mode.  Check that @1 is the signed
6261              max associated with this sign bit.  */
6262           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6263           /* signed_type does not work on pointer types.  */
6264           && INTEGRAL_TYPE_P (arg1_type))
6265       /* The following case also applies to X < signed_max+1
6266          and X >= signed_max+1 because previous transformations.  */
6267       (if (cmp == LE_EXPR || cmp == GT_EXPR)
6268        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6269         (switch
6270          (if (cst == @1 && cmp == LE_EXPR)
6271           (ge (convert:st @0) { build_zero_cst (st); }))
6272          (if (cst == @1 && cmp == GT_EXPR)
6273           (lt (convert:st @0) { build_zero_cst (st); }))
6274          (if (cmp == LE_EXPR)
6275           (ge (view_convert:st @0) { build_zero_cst (st); }))
6276          (if (cmp == GT_EXPR)
6277           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6279 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6280  /* If the second operand is NaN, the result is constant.  */
6281  (simplify
6282   (cmp @0 REAL_CST@1)
6283   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6284        && (cmp != LTGT_EXPR || ! flag_trapping_math))
6285    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6286                             ? false : true, type); })))
6288 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
6289 (simplify
6290   (unordered @0 @1)
6291   (switch
6292     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6293         { constant_boolean_node (true, type); })
6294     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6295         { constant_boolean_node (false, type); })))
6297 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
6298 (simplify
6299   (ordered @0 @1)
6300   (switch
6301     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6302         { constant_boolean_node (false, type); })
6303     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6304         { constant_boolean_node (true, type); })))
6306 /* bool_var != 0 becomes bool_var.  */
6307 (simplify
6308  (ne @0 integer_zerop)
6309  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6310       && types_match (type, TREE_TYPE (@0)))
6311   (non_lvalue @0)))
6312 /* bool_var == 1 becomes bool_var.  */
6313 (simplify
6314  (eq @0 integer_onep)
6315  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6316       && types_match (type, TREE_TYPE (@0)))
6317   (non_lvalue @0)))
6318 /* Do not handle
6319    bool_var == 0 becomes !bool_var or
6320    bool_var != 1 becomes !bool_var
6321    here because that only is good in assignment context as long
6322    as we require a tcc_comparison in GIMPLE_CONDs where we'd
6323    replace if (x == 0) with tem = ~x; if (tem != 0) which is
6324    clearly less optimal and which we'll transform again in forwprop.  */
6326 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6327    where ~Y + 1 == pow2 and Z = ~Y.  */
6328 (for cst (VECTOR_CST INTEGER_CST)
6329  (for cmp (eq ne)
6330       icmp (le gt)
6331   (simplify
6332    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6333     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6334      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6335       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6336                          ? optab_vector : optab_default;
6337               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6338        (if (target_supports_op_p (utype, icmp, optab)
6339             || (optimize_vectors_before_lowering_p ()
6340                 && (!target_supports_op_p (type, cmp, optab)
6341                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6342         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6343          (icmp @0 { csts; })
6344          (icmp (view_convert:utype @0) { csts; })))))))))
6346 /* When one argument is a constant, overflow detection can be simplified.
6347    Currently restricted to single use so as not to interfere too much with
6348    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6349    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
6350 (for cmp (lt le ge gt)
6351      out (gt gt le le)
6352  (simplify
6353   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6354   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6355        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6356        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6357        && wi::to_wide (@1) != 0
6358        && single_use (@2))
6359    (with {
6360      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6361      signop sign = TYPE_SIGN (TREE_TYPE (@0));
6362     }
6363     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6364                                 wi::max_value (prec, sign)
6365                                 - wi::to_wide (@1)); })))))
6367 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6368    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6369    expects the long form, so we restrict the transformation for now.  */
6370 (for cmp (gt le)
6371  (simplify
6372   (cmp:c (minus@2 @0 @1) @0)
6373   (if (single_use (@2)
6374        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6375        && TYPE_UNSIGNED (TREE_TYPE (@0)))
6376    (cmp @1 @0))))
6378 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
6379 (for cmp (ge lt)
6380  (simplify
6381   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6382    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6383         && TYPE_UNSIGNED (TREE_TYPE (@0)))
6384     (cmp @1 @0))))
6386 /* Testing for overflow is unnecessary if we already know the result.  */
6387 /* A - B > A  */
6388 (for cmp (gt le)
6389      out (ne eq)
6390  (simplify
6391   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6392   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6393        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6394    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6395 /* A + B < A  */
6396 (for cmp (lt ge)
6397      out (ne eq)
6398  (simplify
6399   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6400   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6401        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6402    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6404 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6405    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
6406 (for cmp (lt ge)
6407      out (ne eq)
6408  (simplify
6409   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6410   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6411    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6412     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6414 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6415    is at least twice as wide as type of A and B, simplify to
6416    __builtin_mul_overflow (A, B, <unused>).  */
6417 (for cmp (eq ne)
6418  (simplify
6419   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6420        integer_zerop)
6421   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6422        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6423        && TYPE_UNSIGNED (TREE_TYPE (@0))
6424        && (TYPE_PRECISION (TREE_TYPE (@3))
6425            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6426        && tree_fits_uhwi_p (@2)
6427        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6428        && types_match (@0, @1)
6429        && type_has_mode_precision_p (TREE_TYPE (@0))
6430        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6431            != CODE_FOR_nothing))
6432    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6433     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6435 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
6436 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
6437  (simplify
6438   (ovf (convert@2 @0) @1)
6439   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6440        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6441        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6442        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6443    (ovf @0 @1)))
6444  (simplify
6445   (ovf @1 (convert@2 @0))
6446   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6447        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6448        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6449        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
6450    (ovf @1 @0))))
6452 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
6453    are unsigned to x > (umax / cst).  Similarly for signed type, but
6454    in that case it needs to be outside of a range.  */
6455 (simplify
6456  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
6457   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6458        && TYPE_MAX_VALUE (TREE_TYPE (@0))
6459        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
6460        && int_fits_type_p (@1, TREE_TYPE (@0)))
6461    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6462     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
6463     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
6464      (if (integer_minus_onep (@1))
6465       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
6466       (with
6467        {
6468          tree div = fold_convert (TREE_TYPE (@0), @1);
6469          tree lo = int_const_binop (TRUNC_DIV_EXPR,
6470                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
6471          tree hi = int_const_binop (TRUNC_DIV_EXPR,
6472                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
6473          tree etype = range_check_type (TREE_TYPE (@0));
6474          if (etype)
6475            {
6476              if (wi::neg_p (wi::to_wide (div)))
6477                std::swap (lo, hi);
6478              lo = fold_convert (etype, lo);
6479              hi = fold_convert (etype, hi);
6480              hi = int_const_binop (MINUS_EXPR, hi, lo);
6481            }
6482        }
6483        (if (etype)
6484         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
6486 /* Simplification of math builtins.  These rules must all be optimizations
6487    as well as IL simplifications.  If there is a possibility that the new
6488    form could be a pessimization, the rule should go in the canonicalization
6489    section that follows this one.
6491    Rules can generally go in this section if they satisfy one of
6492    the following:
6494    - the rule describes an identity
6496    - the rule replaces calls with something as simple as addition or
6497      multiplication
6499    - the rule contains unary calls only and simplifies the surrounding
6500      arithmetic.  (The idea here is to exclude non-unary calls in which
6501      one operand is constant and in which the call is known to be cheap
6502      when the operand has that value.)  */
6504 (if (flag_unsafe_math_optimizations)
6505  /* Simplify sqrt(x) * sqrt(x) -> x.  */
6506  (simplify
6507   (mult (SQRT_ALL@1 @0) @1)
6508   (if (!tree_expr_maybe_signaling_nan_p (@0))
6509    @0))
6511  (for op (plus minus)
6512   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
6513   (simplify
6514    (op (rdiv @0 @1)
6515        (rdiv @2 @1))
6516    (rdiv (op @0 @2) @1)))
6518  (for cmp (lt le gt ge)
6519       neg_cmp (gt ge lt le)
6520   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
6521   (simplify
6522    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
6523    (with
6524     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
6525     (if (tem
6526          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
6527               || (real_zerop (tem) && !real_zerop (@1))))
6528      (switch
6529       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
6530        (cmp @0 { tem; }))
6531       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
6532        (neg_cmp @0 { tem; })))))))
6534  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
6535  (for root (SQRT CBRT)
6536   (simplify
6537    (mult (root:s @0) (root:s @1))
6538     (root (mult @0 @1))))
6540  /* Simplify expN(x) * expN(y) -> expN(x+y). */
6541  (for exps (EXP EXP2 EXP10 POW10)
6542   (simplify
6543    (mult (exps:s @0) (exps:s @1))
6544     (exps (plus @0 @1))))
6546  /* Simplify a/root(b/c) into a*root(c/b).  */
6547  (for root (SQRT CBRT)
6548   (simplify
6549    (rdiv @0 (root:s (rdiv:s @1 @2)))
6550     (mult @0 (root (rdiv @2 @1)))))
6552  /* Simplify x/expN(y) into x*expN(-y).  */
6553  (for exps (EXP EXP2 EXP10 POW10)
6554   (simplify
6555    (rdiv @0 (exps:s @1))
6556     (mult @0 (exps (negate @1)))))
6558  (for logs (LOG LOG2 LOG10 LOG10)
6559       exps (EXP EXP2 EXP10 POW10)
6560   /* logN(expN(x)) -> x.  */
6561   (simplify
6562    (logs (exps @0))
6563    @0)
6564   /* expN(logN(x)) -> x.  */
6565   (simplify
6566    (exps (logs @0))
6567    @0))
6569  /* Optimize logN(func()) for various exponential functions.  We
6570     want to determine the value "x" and the power "exponent" in
6571     order to transform logN(x**exponent) into exponent*logN(x).  */
6572  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
6573       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
6574   (simplify
6575    (logs (exps @0))
6576    (if (SCALAR_FLOAT_TYPE_P (type))
6577     (with {
6578       tree x;
6579       switch (exps)
6580         {
6581         CASE_CFN_EXP:
6582           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
6583           x = build_real_truncate (type, dconst_e ());
6584           break;
6585         CASE_CFN_EXP2:
6586           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
6587           x = build_real (type, dconst2);
6588           break;
6589         CASE_CFN_EXP10:
6590         CASE_CFN_POW10:
6591           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
6592           {
6593             REAL_VALUE_TYPE dconst10;
6594             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
6595             x = build_real (type, dconst10);
6596           }
6597           break;
6598         default:
6599           gcc_unreachable ();
6600         }
6601       }
6602      (mult (logs { x; }) @0)))))
6604  (for logs (LOG LOG
6605             LOG2 LOG2
6606             LOG10 LOG10)
6607       exps (SQRT CBRT)
6608   (simplify
6609    (logs (exps @0))
6610    (if (SCALAR_FLOAT_TYPE_P (type))
6611     (with {
6612       tree x;
6613       switch (exps)
6614         {
6615         CASE_CFN_SQRT:
6616           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
6617           x = build_real (type, dconsthalf);
6618           break;
6619         CASE_CFN_CBRT:
6620           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
6621           x = build_real_truncate (type, dconst_third ());
6622           break;
6623         default:
6624           gcc_unreachable ();
6625         }
6626       }
6627      (mult { x; } (logs @0))))))
6629  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
6630  (for logs (LOG LOG2 LOG10)
6631       pows (POW)
6632   (simplify
6633    (logs (pows @0 @1))
6634    (mult @1 (logs @0))))
6636  /* pow(C,x) -> exp(log(C)*x) if C > 0,
6637     or if C is a positive power of 2,
6638     pow(C,x) -> exp2(log2(C)*x).  */
6639 #if GIMPLE
6640  (for pows (POW)
6641       exps (EXP)
6642       logs (LOG)
6643       exp2s (EXP2)
6644       log2s (LOG2)
6645   (simplify
6646    (pows REAL_CST@0 @1)
6647    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6648         && real_isfinite (TREE_REAL_CST_PTR (@0))
6649         /* As libmvec doesn't have a vectorized exp2, defer optimizing
6650            the use_exp2 case until after vectorization.  It seems actually
6651            beneficial for all constants to postpone this until later,
6652            because exp(log(C)*x), while faster, will have worse precision
6653            and if x folds into a constant too, that is unnecessary
6654            pessimization.  */
6655         && canonicalize_math_after_vectorization_p ())
6656     (with {
6657        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
6658        bool use_exp2 = false;
6659        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
6660            && value->cl == rvc_normal)
6661          {
6662            REAL_VALUE_TYPE frac_rvt = *value;
6663            SET_REAL_EXP (&frac_rvt, 1);
6664            if (real_equal (&frac_rvt, &dconst1))
6665              use_exp2 = true;
6666          }
6667      }
6668      (if (!use_exp2)
6669       (if (optimize_pow_to_exp (@0, @1))
6670        (exps (mult (logs @0) @1)))
6671       (exp2s (mult (log2s @0) @1)))))))
6672 #endif
6674  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
6675  (for pows (POW)
6676       exps (EXP EXP2 EXP10 POW10)
6677       logs (LOG LOG2 LOG10 LOG10)
6678   (simplify
6679    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6680    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6681         && real_isfinite (TREE_REAL_CST_PTR (@0)))
6682     (exps (plus (mult (logs @0) @1) @2)))))
6684  (for sqrts (SQRT)
6685       cbrts (CBRT)
6686       pows (POW)
6687       exps (EXP EXP2 EXP10 POW10)
6688   /* sqrt(expN(x)) -> expN(x*0.5).  */
6689   (simplify
6690    (sqrts (exps @0))
6691    (exps (mult @0 { build_real (type, dconsthalf); })))
6692   /* cbrt(expN(x)) -> expN(x/3).  */
6693   (simplify
6694    (cbrts (exps @0))
6695    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6696   /* pow(expN(x), y) -> expN(x*y).  */
6697   (simplify
6698    (pows (exps @0) @1)
6699    (exps (mult @0 @1))))
6701  /* tan(atan(x)) -> x.  */
6702  (for tans (TAN)
6703       atans (ATAN)
6704   (simplify
6705    (tans (atans @0))
6706    @0)))
6708  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6709  (for sins (SIN)
6710       atans (ATAN)
6711       sqrts (SQRT)
6712       copysigns (COPYSIGN)
6713   (simplify
6714    (sins (atans:s @0))
6715    (with
6716      {
6717       REAL_VALUE_TYPE r_cst;
6718       build_sinatan_real (&r_cst, type);
6719       tree t_cst = build_real (type, r_cst);
6720       tree t_one = build_one_cst (type);
6721      }
6722     (if (SCALAR_FLOAT_TYPE_P (type))
6723      (cond (lt (abs @0) { t_cst; })
6724       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6725       (copysigns { t_one; } @0))))))
6727 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6728  (for coss (COS)
6729       atans (ATAN)
6730       sqrts (SQRT)
6731       copysigns (COPYSIGN)
6732   (simplify
6733    (coss (atans:s @0))
6734    (with
6735      {
6736       REAL_VALUE_TYPE r_cst;
6737       build_sinatan_real (&r_cst, type);
6738       tree t_cst = build_real (type, r_cst);
6739       tree t_one = build_one_cst (type);
6740       tree t_zero = build_zero_cst (type);
6741      }
6742     (if (SCALAR_FLOAT_TYPE_P (type))
6743      (cond (lt (abs @0) { t_cst; })
6744       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6745       (copysigns { t_zero; } @0))))))
6747  (if (!flag_errno_math)
6748   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6749   (for sinhs (SINH)
6750        atanhs (ATANH)
6751        sqrts (SQRT)
6752    (simplify
6753     (sinhs (atanhs:s @0))
6754     (with { tree t_one = build_one_cst (type); }
6755     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6757   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6758   (for coshs (COSH)
6759        atanhs (ATANH)
6760        sqrts (SQRT)
6761    (simplify
6762     (coshs (atanhs:s @0))
6763     (with { tree t_one = build_one_cst (type); }
6764     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6766 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
6767 (simplify
6768  (CABS (complex:C @0 real_zerop@1))
6769  (abs @0))
6771 /* trunc(trunc(x)) -> trunc(x), etc.  */
6772 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6773  (simplify
6774   (fns (fns @0))
6775   (fns @0)))
6776 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
6777 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6778  (simplify
6779   (fns integer_valued_real_p@0)
6780   @0))
6782 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
6783 (simplify
6784  (HYPOT:c @0 real_zerop@1)
6785  (abs @0))
6787 /* pow(1,x) -> 1.  */
6788 (simplify
6789  (POW real_onep@0 @1)
6790  @0)
6792 (simplify
6793  /* copysign(x,x) -> x.  */
6794  (COPYSIGN_ALL @0 @0)
6795  @0)
6797 (simplify
6798  /* copysign(x,-x) -> -x.  */
6799  (COPYSIGN_ALL @0 (negate@1 @0))
6800  @1)
6802 (simplify
6803  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
6804  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6805  (abs @0))
6807 (for scale (LDEXP SCALBN SCALBLN)
6808  /* ldexp(0, x) -> 0.  */
6809  (simplify
6810   (scale real_zerop@0 @1)
6811   @0)
6812  /* ldexp(x, 0) -> x.  */
6813  (simplify
6814   (scale @0 integer_zerop@1)
6815   @0)
6816  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
6817  (simplify
6818   (scale REAL_CST@0 @1)
6819   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6820    @0)))
6822 /* Canonicalization of sequences of math builtins.  These rules represent
6823    IL simplifications but are not necessarily optimizations.
6825    The sincos pass is responsible for picking "optimal" implementations
6826    of math builtins, which may be more complicated and can sometimes go
6827    the other way, e.g. converting pow into a sequence of sqrts.
6828    We only want to do these canonicalizations before the pass has run.  */
6830 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6831  /* Simplify tan(x) * cos(x) -> sin(x). */
6832  (simplify
6833   (mult:c (TAN:s @0) (COS:s @0))
6834    (SIN @0))
6836  /* Simplify x * pow(x,c) -> pow(x,c+1). */
6837  (simplify
6838   (mult:c @0 (POW:s @0 REAL_CST@1))
6839   (if (!TREE_OVERFLOW (@1))
6840    (POW @0 (plus @1 { build_one_cst (type); }))))
6842  /* Simplify sin(x) / cos(x) -> tan(x). */
6843  (simplify
6844   (rdiv (SIN:s @0) (COS:s @0))
6845    (TAN @0))
6847  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6848  (simplify
6849   (rdiv (SINH:s @0) (COSH:s @0))
6850    (TANH @0))
6852  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6853  (simplify
6854    (rdiv (TANH:s @0) (SINH:s @0))
6855    (rdiv {build_one_cst (type);} (COSH @0)))
6857  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6858  (simplify
6859   (rdiv (COS:s @0) (SIN:s @0))
6860    (rdiv { build_one_cst (type); } (TAN @0)))
6862  /* Simplify sin(x) / tan(x) -> cos(x). */
6863  (simplify
6864   (rdiv (SIN:s @0) (TAN:s @0))
6865   (if (! HONOR_NANS (@0)
6866        && ! HONOR_INFINITIES (@0))
6867    (COS @0)))
6869  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6870  (simplify
6871   (rdiv (TAN:s @0) (SIN:s @0))
6872   (if (! HONOR_NANS (@0)
6873        && ! HONOR_INFINITIES (@0))
6874    (rdiv { build_one_cst (type); } (COS @0))))
6876  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6877  (simplify
6878   (mult (POW:s @0 @1) (POW:s @0 @2))
6879    (POW @0 (plus @1 @2)))
6881  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6882  (simplify
6883   (mult (POW:s @0 @1) (POW:s @2 @1))
6884    (POW (mult @0 @2) @1))
6886  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6887  (simplify
6888   (mult (POWI:s @0 @1) (POWI:s @2 @1))
6889    (POWI (mult @0 @2) @1))
6891  /* Simplify pow(x,c) / x -> pow(x,c-1). */
6892  (simplify
6893   (rdiv (POW:s @0 REAL_CST@1) @0)
6894   (if (!TREE_OVERFLOW (@1))
6895    (POW @0 (minus @1 { build_one_cst (type); }))))
6897  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6898  (simplify
6899   (rdiv @0 (POW:s @1 @2))
6900    (mult @0 (POW @1 (negate @2))))
6902  (for sqrts (SQRT)
6903       cbrts (CBRT)
6904       pows (POW)
6905   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
6906   (simplify
6907    (sqrts (sqrts @0))
6908    (pows @0 { build_real (type, dconst_quarter ()); }))
6909   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
6910   (simplify
6911    (sqrts (cbrts @0))
6912    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6913   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
6914   (simplify
6915    (cbrts (sqrts @0))
6916    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6917   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
6918   (simplify
6919    (cbrts (cbrts tree_expr_nonnegative_p@0))
6920    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6921   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
6922   (simplify
6923    (sqrts (pows @0 @1))
6924    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6925   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
6926   (simplify
6927    (cbrts (pows tree_expr_nonnegative_p@0 @1))
6928    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6929   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
6930   (simplify
6931    (pows (sqrts @0) @1)
6932    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6933   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
6934   (simplify
6935    (pows (cbrts tree_expr_nonnegative_p@0) @1)
6936    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6937   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
6938   (simplify
6939    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6940    (pows @0 (mult @1 @2))))
6942  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
6943  (simplify
6944   (CABS (complex @0 @0))
6945   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6947  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
6948  (simplify
6949   (HYPOT @0 @0)
6950   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6952  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
6953  (for cexps (CEXP)
6954       exps (EXP)
6955       cexpis (CEXPI)
6956   (simplify
6957    (cexps compositional_complex@0)
6958    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6959     (complex
6960      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6961      (mult @1 (imagpart @2)))))))
6963 (if (canonicalize_math_p ())
6964  /* floor(x) -> trunc(x) if x is nonnegative.  */
6965  (for floors (FLOOR_ALL)
6966       truncs (TRUNC_ALL)
6967   (simplify
6968    (floors tree_expr_nonnegative_p@0)
6969    (truncs @0))))
6971 (match double_value_p
6972  @0
6973  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6974 (for froms (BUILT_IN_TRUNCL
6975             BUILT_IN_FLOORL
6976             BUILT_IN_CEILL
6977             BUILT_IN_ROUNDL
6978             BUILT_IN_NEARBYINTL
6979             BUILT_IN_RINTL)
6980      tos (BUILT_IN_TRUNC
6981           BUILT_IN_FLOOR
6982           BUILT_IN_CEIL
6983           BUILT_IN_ROUND
6984           BUILT_IN_NEARBYINT
6985           BUILT_IN_RINT)
6986  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
6987  (if (optimize && canonicalize_math_p ())
6988   (simplify
6989    (froms (convert double_value_p@0))
6990    (convert (tos @0)))))
6992 (match float_value_p
6993  @0
6994  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6995 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6996             BUILT_IN_FLOORL BUILT_IN_FLOOR
6997             BUILT_IN_CEILL BUILT_IN_CEIL
6998             BUILT_IN_ROUNDL BUILT_IN_ROUND
6999             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7000             BUILT_IN_RINTL BUILT_IN_RINT)
7001      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7002           BUILT_IN_FLOORF BUILT_IN_FLOORF
7003           BUILT_IN_CEILF BUILT_IN_CEILF
7004           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7005           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7006           BUILT_IN_RINTF BUILT_IN_RINTF)
7007  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7008     if x is a float.  */
7009  (if (optimize && canonicalize_math_p ()
7010       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7011   (simplify
7012    (froms (convert float_value_p@0))
7013    (convert (tos @0)))))
7015 #if GIMPLE
7016 (match float16_value_p
7017  @0
7018  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7019 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7020             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7021             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7022             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7023             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7024             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7025             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7026             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7027      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7028           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7029           IFN_CEIL IFN_CEIL IFN_CEIL
7030           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7031           IFN_ROUND IFN_ROUND IFN_ROUND
7032           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7033           IFN_RINT IFN_RINT IFN_RINT
7034           IFN_SQRT IFN_SQRT IFN_SQRT)
7035  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7036     if x is a _Float16.  */
7037  (simplify
7038    (convert (froms (convert float16_value_p@0)))
7039      (if (optimize
7040           && types_match (type, TREE_TYPE (@0))
7041           && direct_internal_fn_supported_p (as_internal_fn (tos),
7042                                              type, OPTIMIZE_FOR_BOTH))
7043        (tos @0))))
7045 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7046    x,y is float value, similar for _Float16/double.  */
7047 (for copysigns (COPYSIGN_ALL)
7048  (simplify
7049   (convert (copysigns (convert@2 @0) (convert @1)))
7050    (if (optimize
7051        && !HONOR_SNANS (@2)
7052        && types_match (type, TREE_TYPE (@0))
7053        && types_match (type, TREE_TYPE (@1))
7054        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7055        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7056                                           type, OPTIMIZE_FOR_BOTH))
7057     (IFN_COPYSIGN @0 @1))))
7059 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7060      tos (IFN_FMA IFN_FMA IFN_FMA)
7061  (simplify
7062   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7063    (if (flag_unsafe_math_optimizations
7064        && optimize
7065        && FLOAT_TYPE_P (type)
7066        && FLOAT_TYPE_P (TREE_TYPE (@3))
7067        && types_match (type, TREE_TYPE (@0))
7068        && types_match (type, TREE_TYPE (@1))
7069        && types_match (type, TREE_TYPE (@2))
7070        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7071        && direct_internal_fn_supported_p (as_internal_fn (tos),
7072                                           type, OPTIMIZE_FOR_BOTH))
7073     (tos @0 @1 @2))))
7075 (for maxmin (max min)
7076  (simplify
7077   (convert (maxmin (convert@2 @0) (convert @1)))
7078    (if (optimize
7079        && FLOAT_TYPE_P (type)
7080        && FLOAT_TYPE_P (TREE_TYPE (@2))
7081        && types_match (type, TREE_TYPE (@0))
7082        && types_match (type, TREE_TYPE (@1))
7083        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7084     (maxmin @0 @1))))
7085 #endif
7087 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7088      tos (XFLOOR XCEIL XROUND XRINT)
7089  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7090  (if (optimize && canonicalize_math_p ())
7091   (simplify
7092    (froms (convert double_value_p@0))
7093    (tos @0))))
7095 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7096             XFLOOR XCEIL XROUND XRINT)
7097      tos (XFLOORF XCEILF XROUNDF XRINTF)
7098  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7099     if x is a float.  */
7100  (if (optimize && canonicalize_math_p ())
7101   (simplify
7102    (froms (convert float_value_p@0))
7103    (tos @0))))
7105 (if (canonicalize_math_p ())
7106  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7107  (for floors (IFLOOR LFLOOR LLFLOOR)
7108   (simplify
7109    (floors tree_expr_nonnegative_p@0)
7110    (fix_trunc @0))))
7112 (if (canonicalize_math_p ())
7113  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7114  (for fns (IFLOOR LFLOOR LLFLOOR
7115            ICEIL LCEIL LLCEIL
7116            IROUND LROUND LLROUND)
7117   (simplify
7118    (fns integer_valued_real_p@0)
7119    (fix_trunc @0)))
7120  (if (!flag_errno_math)
7121   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7122   (for rints (IRINT LRINT LLRINT)
7123    (simplify
7124     (rints integer_valued_real_p@0)
7125     (fix_trunc @0)))))
7127 (if (canonicalize_math_p ())
7128  (for ifn (IFLOOR ICEIL IROUND IRINT)
7129       lfn (LFLOOR LCEIL LROUND LRINT)
7130       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7131   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7132      sizeof (int) == sizeof (long).  */
7133   (if (TYPE_PRECISION (integer_type_node)
7134        == TYPE_PRECISION (long_integer_type_node))
7135    (simplify
7136     (ifn @0)
7137     (lfn:long_integer_type_node @0)))
7138   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7139      sizeof (long long) == sizeof (long).  */
7140   (if (TYPE_PRECISION (long_long_integer_type_node)
7141        == TYPE_PRECISION (long_integer_type_node))
7142    (simplify
7143     (llfn @0)
7144     (lfn:long_integer_type_node @0)))))
7146 /* cproj(x) -> x if we're ignoring infinities.  */
7147 (simplify
7148  (CPROJ @0)
7149  (if (!HONOR_INFINITIES (type))
7150    @0))
7152 /* If the real part is inf and the imag part is known to be
7153    nonnegative, return (inf + 0i).  */
7154 (simplify
7155  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7156  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7157   { build_complex_inf (type, false); }))
7159 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7160 (simplify
7161  (CPROJ (complex @0 REAL_CST@1))
7162  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7163   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7165 (for pows (POW)
7166      sqrts (SQRT)
7167      cbrts (CBRT)
7168  (simplify
7169   (pows @0 REAL_CST@1)
7170   (with {
7171     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7172     REAL_VALUE_TYPE tmp;
7173    }
7174    (switch
7175     /* pow(x,0) -> 1.  */
7176     (if (real_equal (value, &dconst0))
7177      { build_real (type, dconst1); })
7178     /* pow(x,1) -> x.  */
7179     (if (real_equal (value, &dconst1))
7180      @0)
7181     /* pow(x,-1) -> 1/x.  */
7182     (if (real_equal (value, &dconstm1))
7183      (rdiv { build_real (type, dconst1); } @0))
7184     /* pow(x,0.5) -> sqrt(x).  */
7185     (if (flag_unsafe_math_optimizations
7186          && canonicalize_math_p ()
7187          && real_equal (value, &dconsthalf))
7188      (sqrts @0))
7189     /* pow(x,1/3) -> cbrt(x).  */
7190     (if (flag_unsafe_math_optimizations
7191          && canonicalize_math_p ()
7192          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7193              real_equal (value, &tmp)))
7194      (cbrts @0))))))
7196 /* powi(1,x) -> 1.  */
7197 (simplify
7198  (POWI real_onep@0 @1)
7199  @0)
7201 (simplify
7202  (POWI @0 INTEGER_CST@1)
7203  (switch
7204   /* powi(x,0) -> 1.  */
7205   (if (wi::to_wide (@1) == 0)
7206    { build_real (type, dconst1); })
7207   /* powi(x,1) -> x.  */
7208   (if (wi::to_wide (@1) == 1)
7209    @0)
7210   /* powi(x,-1) -> 1/x.  */
7211   (if (wi::to_wide (@1) == -1)
7212    (rdiv { build_real (type, dconst1); } @0))))
7214 /* Narrowing of arithmetic and logical operations.
7216    These are conceptually similar to the transformations performed for
7217    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
7218    term we want to move all that code out of the front-ends into here.  */
7220 /* Convert (outertype)((innertype0)a+(innertype1)b)
7221    into ((newtype)a+(newtype)b) where newtype
7222    is the widest mode from all of these.  */
7223 (for op (plus minus mult rdiv)
7224  (simplify
7225    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7226    /* If we have a narrowing conversion of an arithmetic operation where
7227       both operands are widening conversions from the same type as the outer
7228       narrowing conversion.  Then convert the innermost operands to a
7229       suitable unsigned type (to avoid introducing undefined behavior),
7230       perform the operation and convert the result to the desired type.  */
7231    (if (INTEGRAL_TYPE_P (type)
7232         && op != MULT_EXPR
7233         && op != RDIV_EXPR
7234         /* We check for type compatibility between @0 and @1 below,
7235            so there's no need to check that @2/@4 are integral types.  */
7236         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7237         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7238         /* The precision of the type of each operand must match the
7239            precision of the mode of each operand, similarly for the
7240            result.  */
7241         && type_has_mode_precision_p (TREE_TYPE (@1))
7242         && type_has_mode_precision_p (TREE_TYPE (@2))
7243         && type_has_mode_precision_p (type)
7244         /* The inner conversion must be a widening conversion.  */
7245         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7246         && types_match (@1, type)
7247         && (types_match (@1, @2)
7248             /* Or the second operand is const integer or converted const
7249                integer from valueize.  */
7250             || poly_int_tree_p (@4)))
7251      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7252        (op @1 (convert @2))
7253        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7254         (convert (op (convert:utype @1)
7255                      (convert:utype @2)))))
7256      (if (FLOAT_TYPE_P (type)
7257           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7258                == DECIMAL_FLOAT_TYPE_P (type))
7259       (with { tree arg0 = strip_float_extensions (@1);
7260               tree arg1 = strip_float_extensions (@2);
7261               tree itype = TREE_TYPE (@0);
7262               tree ty1 = TREE_TYPE (arg0);
7263               tree ty2 = TREE_TYPE (arg1);
7264               enum tree_code code = TREE_CODE (itype); }
7265         (if (FLOAT_TYPE_P (ty1)
7266              && FLOAT_TYPE_P (ty2))
7267          (with { tree newtype = type;
7268                  if (TYPE_MODE (ty1) == SDmode
7269                      || TYPE_MODE (ty2) == SDmode
7270                      || TYPE_MODE (type) == SDmode)
7271                    newtype = dfloat32_type_node;
7272                  if (TYPE_MODE (ty1) == DDmode
7273                      || TYPE_MODE (ty2) == DDmode
7274                      || TYPE_MODE (type) == DDmode)
7275                    newtype = dfloat64_type_node;
7276                  if (TYPE_MODE (ty1) == TDmode
7277                      || TYPE_MODE (ty2) == TDmode
7278                      || TYPE_MODE (type) == TDmode)
7279                    newtype = dfloat128_type_node; }
7280           (if ((newtype == dfloat32_type_node
7281                 || newtype == dfloat64_type_node
7282                 || newtype == dfloat128_type_node)
7283               && newtype == type
7284               && types_match (newtype, type))
7285             (op (convert:newtype @1) (convert:newtype @2))
7286             (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
7287                       newtype = ty1;
7288                     if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
7289                       newtype = ty2; }
7290                /* Sometimes this transformation is safe (cannot
7291                   change results through affecting double rounding
7292                   cases) and sometimes it is not.  If NEWTYPE is
7293                   wider than TYPE, e.g. (float)((long double)double
7294                   + (long double)double) converted to
7295                   (float)(double + double), the transformation is
7296                   unsafe regardless of the details of the types
7297                   involved; double rounding can arise if the result
7298                   of NEWTYPE arithmetic is a NEWTYPE value half way
7299                   between two representable TYPE values but the
7300                   exact value is sufficiently different (in the
7301                   right direction) for this difference to be
7302                   visible in ITYPE arithmetic.  If NEWTYPE is the
7303                   same as TYPE, however, the transformation may be
7304                   safe depending on the types involved: it is safe
7305                   if the ITYPE has strictly more than twice as many
7306                   mantissa bits as TYPE, can represent infinities
7307                   and NaNs if the TYPE can, and has sufficient
7308                   exponent range for the product or ratio of two
7309                   values representable in the TYPE to be within the
7310                   range of normal values of ITYPE.  */
7311               (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
7312                    && (flag_unsafe_math_optimizations
7313                        || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
7314                            && real_can_shorten_arithmetic (TYPE_MODE (itype),
7315                                                            TYPE_MODE (type))
7316                            && !excess_precision_type (newtype)))
7317                    && !types_match (itype, newtype))
7318                  (convert:type (op (convert:newtype @1)
7319                                    (convert:newtype @2)))
7320          )))) )
7321    ))
7324 /* This is another case of narrowing, specifically when there's an outer
7325    BIT_AND_EXPR which masks off bits outside the type of the innermost
7326    operands.   Like the previous case we have to convert the operands
7327    to unsigned types to avoid introducing undefined behavior for the
7328    arithmetic operation.  */
7329 (for op (minus plus)
7330  (simplify
7331   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7332   (if (INTEGRAL_TYPE_P (type)
7333        /* We check for type compatibility between @0 and @1 below,
7334           so there's no need to check that @1/@3 are integral types.  */
7335        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7336        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7337        /* The precision of the type of each operand must match the
7338           precision of the mode of each operand, similarly for the
7339           result.  */
7340        && type_has_mode_precision_p (TREE_TYPE (@0))
7341        && type_has_mode_precision_p (TREE_TYPE (@1))
7342        && type_has_mode_precision_p (type)
7343        /* The inner conversion must be a widening conversion.  */
7344        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7345        && types_match (@0, @1)
7346        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7347            <= TYPE_PRECISION (TREE_TYPE (@0)))
7348        && (wi::to_wide (@4)
7349            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7350                        true, TYPE_PRECISION (type))) == 0)
7351    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7352     (with { tree ntype = TREE_TYPE (@0); }
7353      (convert (bit_and (op @0 @1) (convert:ntype @4))))
7354     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7355      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7356                (convert:utype @4))))))))
7358 /* Transform (@0 < @1 and @0 < @2) to use min,
7359    (@0 > @1 and @0 > @2) to use max */
7360 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7361      op    (lt      le      gt      ge      lt      le      gt      ge     )
7362      ext   (min     min     max     max     max     max     min     min    )
7363  (simplify
7364   (logic (op:cs @0 @1) (op:cs @0 @2))
7365   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7366        && TREE_CODE (@0) != INTEGER_CST)
7367    (op @0 (ext @1 @2)))))
7369 (simplify
7370  /* signbit(x) -> 0 if x is nonnegative.  */
7371  (SIGNBIT tree_expr_nonnegative_p@0)
7372  { integer_zero_node; })
7374 (simplify
7375  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
7376  (SIGNBIT @0)
7377  (if (!HONOR_SIGNED_ZEROS (@0))
7378   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7380 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
7381 (for cmp (eq ne)
7382  (for op (plus minus)
7383       rop (minus plus)
7384   (simplify
7385    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7386    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7387         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7388         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7389         && !TYPE_SATURATING (TREE_TYPE (@0)))
7390     (with { tree res = int_const_binop (rop, @2, @1); }
7391      (if (TREE_OVERFLOW (res)
7392           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7393       { constant_boolean_node (cmp == NE_EXPR, type); }
7394       (if (single_use (@3))
7395        (cmp @0 { TREE_OVERFLOW (res)
7396                  ? drop_tree_overflow (res) : res; }))))))))
7397 (for cmp (lt le gt ge)
7398  (for op (plus minus)
7399       rop (minus plus)
7400   (simplify
7401    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7402    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7403         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7404     (with { tree res = int_const_binop (rop, @2, @1); }
7405      (if (TREE_OVERFLOW (res))
7406       {
7407         fold_overflow_warning (("assuming signed overflow does not occur "
7408                                 "when simplifying conditional to constant"),
7409                                WARN_STRICT_OVERFLOW_CONDITIONAL);
7410         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7411         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
7412         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7413                                   TYPE_SIGN (TREE_TYPE (@1)))
7414                         != (op == MINUS_EXPR);
7415         constant_boolean_node (less == ovf_high, type);
7416       }
7417       (if (single_use (@3))
7418        (with
7419         {
7420           fold_overflow_warning (("assuming signed overflow does not occur "
7421                                   "when changing X +- C1 cmp C2 to "
7422                                   "X cmp C2 -+ C1"),
7423                                  WARN_STRICT_OVERFLOW_COMPARISON);
7424         }
7425         (cmp @0 { res; })))))))))
7427 /* Canonicalizations of BIT_FIELD_REFs.  */
7429 (simplify
7430  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
7431  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
7433 (simplify
7434  (BIT_FIELD_REF (view_convert @0) @1 @2)
7435  (BIT_FIELD_REF @0 @1 @2))
7437 (simplify
7438  (BIT_FIELD_REF @0 @1 integer_zerop)
7439  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
7440   (view_convert @0)))
7442 (simplify
7443  (BIT_FIELD_REF @0 @1 @2)
7444  (switch
7445   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
7446        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7447    (switch
7448     (if (integer_zerop (@2))
7449      (view_convert (realpart @0)))
7450     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7451      (view_convert (imagpart @0)))))
7452   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7453        && INTEGRAL_TYPE_P (type)
7454        /* On GIMPLE this should only apply to register arguments.  */
7455        && (! GIMPLE || is_gimple_reg (@0))
7456        /* A bit-field-ref that referenced the full argument can be stripped.  */
7457        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
7458             && integer_zerop (@2))
7459            /* Low-parts can be reduced to integral conversions.
7460               ???  The following doesn't work for PDP endian.  */
7461            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7462                /* But only do this after vectorization.  */
7463                && canonicalize_math_after_vectorization_p ()
7464                /* Don't even think about BITS_BIG_ENDIAN.  */
7465                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
7466                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
7467                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
7468                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
7469                                             - TYPE_PRECISION (type))
7470                                          : 0)) == 0)))
7471    (convert @0))))
7473 /* Simplify vector extracts.  */
7475 (simplify
7476  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
7477  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
7478       && tree_fits_uhwi_p (TYPE_SIZE (type))
7479       && ((tree_to_uhwi (TYPE_SIZE (type))
7480            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
7481           || (VECTOR_TYPE_P (type)
7482               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
7483                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
7484   (with
7485    {
7486      tree ctor = (TREE_CODE (@0) == SSA_NAME
7487                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7488      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
7489      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
7490      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
7491      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
7492    }
7493    (if (n != 0
7494         && (idx % width) == 0
7495         && (n % width) == 0
7496         && known_le ((idx + n) / width,
7497                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
7498     (with
7499      {
7500        idx = idx / width;
7501        n = n / width;
7502        /* Constructor elements can be subvectors.  */
7503        poly_uint64 k = 1;
7504        if (CONSTRUCTOR_NELTS (ctor) != 0)
7505          {
7506            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
7507            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
7508              k = TYPE_VECTOR_SUBPARTS (cons_elem);
7509          }
7510        unsigned HOST_WIDE_INT elt, count, const_k;
7511      }
7512      (switch
7513       /* We keep an exact subset of the constructor elements.  */
7514       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
7515        (if (CONSTRUCTOR_NELTS (ctor) == 0)
7516         { build_zero_cst (type); }
7517         (if (count == 1)
7518          (if (elt < CONSTRUCTOR_NELTS (ctor))
7519           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
7520           { build_zero_cst (type); })
7521          /* We don't want to emit new CTORs unless the old one goes away.
7522             ???  Eventually allow this if the CTOR ends up constant or
7523             uniform.  */
7524          (if (single_use (@0))
7525           (with
7526             {
7527               vec<constructor_elt, va_gc> *vals;
7528               vec_alloc (vals, count);
7529               bool constant_p = true;
7530               tree res;
7531               for (unsigned i = 0;
7532                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
7533                 {
7534                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
7535                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
7536                   if (!CONSTANT_CLASS_P (e))
7537                     constant_p = false;
7538                 }
7539               tree evtype = (types_match (TREE_TYPE (type),
7540                                           TREE_TYPE (TREE_TYPE (ctor)))
7541                              ? type
7542                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
7543                                                   count * k));
7544               /* We used to build a CTOR in the non-constant case here
7545                  but that's not a GIMPLE value.  We'd have to expose this
7546                  operation somehow so the code generation can properly
7547                  split it out to a separate stmt.  */
7548               res = (constant_p ? build_vector_from_ctor (evtype, vals)
7549                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
7550             }
7551             (if (res)
7552              (view_convert { res; })))))))
7553       /* The bitfield references a single constructor element.  */
7554       (if (k.is_constant (&const_k)
7555            && idx + n <= (idx / const_k + 1) * const_k)
7556        (switch
7557         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
7558          { build_zero_cst (type); })
7559         (if (n == const_k)
7560          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
7561         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
7562                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
7564 /* Simplify a bit extraction from a bit insertion for the cases with
7565    the inserted element fully covering the extraction or the insertion
7566    not touching the extraction.  */
7567 (simplify
7568  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
7569  (with
7570   {
7571     unsigned HOST_WIDE_INT isize;
7572     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
7573       isize = TYPE_PRECISION (TREE_TYPE (@1));
7574     else
7575       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
7576   }
7577   (switch
7578    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
7579          || type_has_mode_precision_p (TREE_TYPE (@1)))
7580         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7581         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
7582                       wi::to_wide (@ipos) + isize))
7583     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
7584                                                  wi::to_wide (@rpos)
7585                                                  - wi::to_wide (@ipos)); }))
7586    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
7587         && compare_tree_int (@rsize, isize) == 0)
7588     (convert @1))
7589    (if (wi::geu_p (wi::to_wide (@ipos),
7590                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
7591         || wi::geu_p (wi::to_wide (@rpos),
7592                       wi::to_wide (@ipos) + isize))
7593     (BIT_FIELD_REF @0 @rsize @rpos)))))
7595 (if (canonicalize_math_after_vectorization_p ())
7596  (for fmas (FMA)
7597   (simplify
7598    (fmas:c (negate @0) @1 @2)
7599    (IFN_FNMA @0 @1 @2))
7600   (simplify
7601    (fmas @0 @1 (negate @2))
7602    (IFN_FMS @0 @1 @2))
7603   (simplify
7604    (fmas:c (negate @0) @1 (negate @2))
7605    (IFN_FNMS @0 @1 @2))
7606   (simplify
7607    (negate (fmas@3 @0 @1 @2))
7608    (if (single_use (@3))
7609     (IFN_FNMS @0 @1 @2))))
7611  (simplify
7612   (IFN_FMS:c (negate @0) @1 @2)
7613   (IFN_FNMS @0 @1 @2))
7614  (simplify
7615   (IFN_FMS @0 @1 (negate @2))
7616   (IFN_FMA @0 @1 @2))
7617  (simplify
7618   (IFN_FMS:c (negate @0) @1 (negate @2))
7619   (IFN_FNMA @0 @1 @2))
7620  (simplify
7621   (negate (IFN_FMS@3 @0 @1 @2))
7622    (if (single_use (@3))
7623     (IFN_FNMA @0 @1 @2)))
7625  (simplify
7626   (IFN_FNMA:c (negate @0) @1 @2)
7627   (IFN_FMA @0 @1 @2))
7628  (simplify
7629   (IFN_FNMA @0 @1 (negate @2))
7630   (IFN_FNMS @0 @1 @2))
7631  (simplify
7632   (IFN_FNMA:c (negate @0) @1 (negate @2))
7633   (IFN_FMS @0 @1 @2))
7634  (simplify
7635   (negate (IFN_FNMA@3 @0 @1 @2))
7636   (if (single_use (@3))
7637    (IFN_FMS @0 @1 @2)))
7639  (simplify
7640   (IFN_FNMS:c (negate @0) @1 @2)
7641   (IFN_FMS @0 @1 @2))
7642  (simplify
7643   (IFN_FNMS @0 @1 (negate @2))
7644   (IFN_FNMA @0 @1 @2))
7645  (simplify
7646   (IFN_FNMS:c (negate @0) @1 (negate @2))
7647   (IFN_FMA @0 @1 @2))
7648  (simplify
7649   (negate (IFN_FNMS@3 @0 @1 @2))
7650   (if (single_use (@3))
7651    (IFN_FMA @0 @1 @2))))
7653 /* CLZ simplifications.  */
7654 (for clz (CLZ)
7655  (for op (eq ne)
7656       cmp (lt ge)
7657   (simplify
7658    (op (clz:s@2 @0) INTEGER_CST@1)
7659    (if (integer_zerop (@1) && single_use (@2))
7660     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
7661     (with { tree type0 = TREE_TYPE (@0);
7662             tree stype = signed_type_for (type0);
7663             HOST_WIDE_INT val = 0;
7664             /* Punt on hypothetical weird targets.  */
7665             if (clz == CFN_CLZ
7666                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7667                                               val) == 2
7668                 && val == 0)
7669               stype = NULL_TREE;
7670           }
7671      (if (stype)
7672       (cmp (convert:stype @0) { build_zero_cst (stype); })))
7673     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
7674     (with { bool ok = true;
7675             HOST_WIDE_INT val = 0;
7676             tree type0 = TREE_TYPE (@0);
7677             /* Punt on hypothetical weird targets.  */
7678             if (clz == CFN_CLZ
7679                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7680                                               val) == 2
7681                 && val == TYPE_PRECISION (type0) - 1)
7682               ok = false;
7683           }
7684      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7685       (op @0 { build_one_cst (type0); })))))))
7687 /* CTZ simplifications.  */
7688 (for ctz (CTZ)
7689  (for op (ge gt le lt)
7690       cmp (eq eq ne ne)
7691   (simplify
7692    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
7693    (op (ctz:s @0) INTEGER_CST@1)
7694     (with { bool ok = true;
7695             HOST_WIDE_INT val = 0;
7696             if (!tree_fits_shwi_p (@1))
7697               ok = false;
7698             else
7699               {
7700                 val = tree_to_shwi (@1);
7701                 /* Canonicalize to >= or <.  */
7702                 if (op == GT_EXPR || op == LE_EXPR)
7703                   {
7704                     if (val == HOST_WIDE_INT_MAX)
7705                       ok = false;
7706                     else
7707                       val++;
7708                   }
7709               }
7710             bool zero_res = false;
7711             HOST_WIDE_INT zero_val = 0;
7712             tree type0 = TREE_TYPE (@0);
7713             int prec = TYPE_PRECISION (type0);
7714             if (ctz == CFN_CTZ
7715                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7716                                               zero_val) == 2)
7717               zero_res = true;
7718           }
7719      (if (val <= 0)
7720       (if (ok && (!zero_res || zero_val >= val))
7721        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7722       (if (val >= prec)
7723        (if (ok && (!zero_res || zero_val < val))
7724         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7725        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7726         (cmp (bit_and @0 { wide_int_to_tree (type0,
7727                                              wi::mask (val, false, prec)); })
7728              { build_zero_cst (type0); })))))))
7729  (for op (eq ne)
7730   (simplify
7731    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
7732    (op (ctz:s @0) INTEGER_CST@1)
7733     (with { bool zero_res = false;
7734             HOST_WIDE_INT zero_val = 0;
7735             tree type0 = TREE_TYPE (@0);
7736             int prec = TYPE_PRECISION (type0);
7737             if (ctz == CFN_CTZ
7738                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7739                                               zero_val) == 2)
7740               zero_res = true;
7741           }
7742      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7743       (if (!zero_res || zero_val != wi::to_widest (@1))
7744        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7745       (if (!zero_res || zero_val < 0 || zero_val >= prec)
7746        (op (bit_and @0 { wide_int_to_tree (type0,
7747                                            wi::mask (tree_to_uhwi (@1) + 1,
7748                                                      false, prec)); })
7749            { wide_int_to_tree (type0,
7750                                wi::shifted_mask (tree_to_uhwi (@1), 1,
7751                                                  false, prec)); })))))))
7753 /* POPCOUNT simplifications.  */
7754 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
7755 (simplify
7756   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7757   (if (INTEGRAL_TYPE_P (type)
7758        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7759     (POPCOUNT (bit_ior @0 @1))))
7761 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
7762 (for popcount (POPCOUNT)
7763   (for cmp (le eq ne gt)
7764        rep (eq eq ne ne)
7765     (simplify
7766       (cmp (popcount @0) integer_zerop)
7767       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7769 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
7770 (simplify
7771   (bit_and (POPCOUNT @0) integer_onep)
7772   (PARITY @0))
7774 /* PARITY simplifications.  */
7775 /* parity(~X) is parity(X).  */
7776 (simplify
7777   (PARITY (bit_not @0))
7778   (PARITY @0))
7780 /* parity(X)^parity(Y) is parity(X^Y).  */
7781 (simplify
7782   (bit_xor (PARITY:s @0) (PARITY:s @1))
7783   (PARITY (bit_xor @0 @1)))
7785 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
7786 (for func (POPCOUNT BSWAP FFS PARITY)
7787  (simplify
7788   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
7789   @3))
7791 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
7792    where CST is precision-1. */
7793 (for func (CLRSB)
7794  (simplify
7795   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
7796   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
7797    @4)))
7799 #if GIMPLE
7800 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
7801 (for func (CLZ)
7802  (simplify
7803   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
7804   (with { int val;
7805           internal_fn ifn = IFN_LAST;
7806           if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
7807               && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
7808                                             val) == 2)
7809             ifn = IFN_CLZ;
7810         }
7811    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
7812     (IFN_CLZ @3)))))
7814 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
7815 (for func (CTZ)
7816  (simplify
7817   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
7818   (with { int val;
7819           internal_fn ifn = IFN_LAST;
7820           if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
7821               && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
7822                                             val) == 2)
7823             ifn = IFN_CTZ;
7824         }
7825    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
7826     (IFN_CTZ @3)))))
7827 #endif
7829 /* Common POPCOUNT/PARITY simplifications.  */
7830 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
7831 (for pfun (POPCOUNT PARITY)
7832   (simplify
7833     (pfun @0)
7834     (if (INTEGRAL_TYPE_P (type))
7835      (with { wide_int nz = tree_nonzero_bits (@0); }
7836        (switch
7837          (if (nz == 1)
7838            (convert @0))
7839          (if (wi::popcount (nz) == 1)
7840            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7841              (convert (rshift:utype (convert:utype @0)
7842                                     { build_int_cst (integer_type_node,
7843                                                      wi::ctz (nz)); })))))))))
7845 #if GIMPLE
7846 /* 64- and 32-bits branchless implementations of popcount are detected:
7848    int popcount64c (uint64_t x)
7849    {
7850      x -= (x >> 1) & 0x5555555555555555ULL;
7851      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7852      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
7853      return (x * 0x0101010101010101ULL) >> 56;
7854    }
7856    int popcount32c (uint32_t x)
7857    {
7858      x -= (x >> 1) & 0x55555555;
7859      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
7860      x = (x + (x >> 4)) & 0x0f0f0f0f;
7861      return (x * 0x01010101) >> 24;
7862    }  */
7863 (simplify
7864  (rshift
7865   (mult
7866    (bit_and
7867     (plus:c
7868      (rshift @8 INTEGER_CST@5)
7869       (plus:c@8
7870        (bit_and @6 INTEGER_CST@7)
7871         (bit_and
7872          (rshift
7873           (minus@6 @0
7874            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
7875           INTEGER_CST@10)
7876          INTEGER_CST@9)))
7877     INTEGER_CST@3)
7878    INTEGER_CST@2)
7879   INTEGER_CST@1)
7880   /* Check constants and optab.  */
7881   (with { unsigned prec = TYPE_PRECISION (type);
7882           int shift = (64 - prec) & 63;
7883           unsigned HOST_WIDE_INT c1
7884             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
7885           unsigned HOST_WIDE_INT c2
7886             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
7887           unsigned HOST_WIDE_INT c3
7888             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
7889           unsigned HOST_WIDE_INT c4
7890             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
7891    }
7892    (if (prec >= 16
7893         && prec <= 64
7894         && pow2p_hwi (prec)
7895         && TYPE_UNSIGNED (type)
7896         && integer_onep (@4)
7897         && wi::to_widest (@10) == 2
7898         && wi::to_widest (@5) == 4
7899         && wi::to_widest (@1) == prec - 8
7900         && tree_to_uhwi (@2) == c1
7901         && tree_to_uhwi (@3) == c2
7902         && tree_to_uhwi (@9) == c3
7903         && tree_to_uhwi (@7) == c3
7904         && tree_to_uhwi (@11) == c4)
7905     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
7906                                          OPTIMIZE_FOR_BOTH))
7907      (convert (IFN_POPCOUNT:type @0))
7908      /* Try to do popcount in two halves.  PREC must be at least
7909         five bits for this to work without extension before adding.  */
7910      (with {
7911        tree half_type = NULL_TREE;
7912        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
7913        int half_prec = 8;
7914        if (m.exists ()
7915            && m.require () != TYPE_MODE (type))
7916          {
7917            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
7918            half_type = build_nonstandard_integer_type (half_prec, 1);
7919          }
7920        gcc_assert (half_prec > 2);
7921       }
7922       (if (half_type != NULL_TREE
7923            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
7924                                               OPTIMIZE_FOR_BOTH))
7925        (convert (plus
7926          (IFN_POPCOUNT:half_type (convert @0))
7927          (IFN_POPCOUNT:half_type (convert (rshift @0
7928             { build_int_cst (integer_type_node, half_prec); } )))))))))))
7930 /* __builtin_ffs needs to deal on many targets with the possible zero
7931    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
7932    should lead to better code.  */
7933 (simplify
7934  (FFS tree_expr_nonzero_p@0)
7935  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7936       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
7937                                          OPTIMIZE_FOR_SPEED))
7938   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7939    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
7940 #endif
7942 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
7943           BUILT_IN_FFSIMAX)
7944  /* __builtin_ffs (X) == 0 -> X == 0.
7945     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
7946  (for cmp (eq ne)
7947   (simplify
7948    (cmp (ffs@2 @0) INTEGER_CST@1)
7949     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7950      (switch
7951       (if (integer_zerop (@1))
7952        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7953       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7954        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7955       (if (single_use (@2))
7956        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7957                                             wi::mask (tree_to_uhwi (@1),
7958                                                       false, prec)); })
7959             { wide_int_to_tree (TREE_TYPE (@0),
7960                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7961                                                   false, prec)); }))))))
7963  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
7964  (for cmp (gt le)
7965       cmp2 (ne eq)
7966       cmp3 (eq ne)
7967       bit_op (bit_and bit_ior)
7968   (simplify
7969    (cmp (ffs@2 @0) INTEGER_CST@1)
7970     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7971      (switch
7972       (if (integer_zerop (@1))
7973        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7974       (if (tree_int_cst_sgn (@1) < 0)
7975        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7976       (if (wi::to_widest (@1) >= prec)
7977        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7978       (if (wi::to_widest (@1) == prec - 1)
7979        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7980                                     wi::shifted_mask (prec - 1, 1,
7981                                                       false, prec)); }))
7982       (if (single_use (@2))
7983        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7984                (cmp3 (bit_and @0
7985                               { wide_int_to_tree (TREE_TYPE (@0),
7986                                                   wi::mask (tree_to_uhwi (@1),
7987                                                   false, prec)); })
7988                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
7990 #if GIMPLE
7992 /* Simplify:
7993      a = op a1
7994      r = cond ? a : b
7995      --> r = .COND_FN (cond, a, b)
7996 and,
7997     a = op a1
7998     r = cond ? b : a
7999     --> r = .COND_FN (~cond, b, a).  */
8001 (for uncond_op (UNCOND_UNARY)
8002      cond_op (COND_UNARY)
8003  (simplify
8004   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8005    (with { tree op_type = TREE_TYPE (@3); }
8006     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8007         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8008      (cond_op @0 @1 @2))))
8009  (simplify
8010   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8011    (with { tree op_type = TREE_TYPE (@3); }
8012     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8013         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8014      (cond_op (bit_not @0) @2 @1)))))
8016 /* Simplify:
8018      a = a1 op a2
8019      r = c ? a : b;
8021    to:
8023      r = c ? a1 op a2 : b;
8025    if the target can do it in one go.  This makes the operation conditional
8026    on c, so could drop potentially-trapping arithmetic, but that's a valid
8027    simplification if the result of the operation isn't needed.
8029    Avoid speculatively generating a stand-alone vector comparison
8030    on targets that might not support them.  Any target implementing
8031    conditional internal functions must support the same comparisons
8032    inside and outside a VEC_COND_EXPR.  */
8034 (for uncond_op (UNCOND_BINARY)
8035      cond_op (COND_BINARY)
8036  (simplify
8037   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8038   (with { tree op_type = TREE_TYPE (@4); }
8039    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8040         && is_truth_type_for (op_type, TREE_TYPE (@0))
8041         && single_use (@4))
8042     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8043  (simplify
8044   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8045   (with { tree op_type = TREE_TYPE (@4); }
8046    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8047         && is_truth_type_for (op_type, TREE_TYPE (@0))
8048         && single_use (@4))
8049     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8051 /* Same for ternary operations.  */
8052 (for uncond_op (UNCOND_TERNARY)
8053      cond_op (COND_TERNARY)
8054  (simplify
8055   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8056   (with { tree op_type = TREE_TYPE (@5); }
8057    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8058         && is_truth_type_for (op_type, TREE_TYPE (@0))
8059         && single_use (@5))
8060     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8061  (simplify
8062   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8063   (with { tree op_type = TREE_TYPE (@5); }
8064    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8065         && is_truth_type_for (op_type, TREE_TYPE (@0))
8066         && single_use (@5))
8067     (view_convert (cond_op (bit_not @0) @2 @3 @4
8068                   (view_convert:op_type @1)))))))
8069 #endif
8071 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8072    "else" value of an IFN_COND_*.  */
8073 (for cond_op (COND_BINARY)
8074  (simplify
8075   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8076   (with { tree op_type = TREE_TYPE (@3); }
8077    (if (element_precision (type) == element_precision (op_type))
8078     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8079  (simplify
8080   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8081   (with { tree op_type = TREE_TYPE (@5); }
8082    (if (inverse_conditions_p (@0, @2)
8083         && element_precision (type) == element_precision (op_type))
8084     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8086 /* Same for ternary operations.  */
8087 (for cond_op (COND_TERNARY)
8088  (simplify
8089   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8090   (with { tree op_type = TREE_TYPE (@4); }
8091    (if (element_precision (type) == element_precision (op_type))
8092     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8093  (simplify
8094   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8095   (with { tree op_type = TREE_TYPE (@6); }
8096    (if (inverse_conditions_p (@0, @2)
8097         && element_precision (type) == element_precision (op_type))
8098     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8100 /* Detect simplication for a conditional reduction where
8102    a = mask1 ? b : 0
8103    c = mask2 ? d + a : d
8105    is turned into
8107    c = mask1 && mask2 ? d + b : d.  */
8108 (simplify
8109   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8110    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8112 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8113    expressions like:
8115    A: (@0 + @1 < @2) | (@2 + @1 < @0)
8116    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8118    If pointers are known not to wrap, B checks whether @1 bytes starting
8119    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8120    bytes.  A is more efficiently tested as:
8122    A: (sizetype) (@0 + @1 - @2) > @1 * 2
8124    The equivalent expression for B is given by replacing @1 with @1 - 1:
8126    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8128    @0 and @2 can be swapped in both expressions without changing the result.
8130    The folds rely on sizetype's being unsigned (which is always true)
8131    and on its being the same width as the pointer (which we have to check).
8133    The fold replaces two pointer_plus expressions, two comparisons and
8134    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8135    the best case it's a saving of two operations.  The A fold retains one
8136    of the original pointer_pluses, so is a win even if both pointer_pluses
8137    are used elsewhere.  The B fold is a wash if both pointer_pluses are
8138    used elsewhere, since all we end up doing is replacing a comparison with
8139    a pointer_plus.  We do still apply the fold under those circumstances
8140    though, in case applying it to other conditions eventually makes one of the
8141    pointer_pluses dead.  */
8142 (for ior (truth_orif truth_or bit_ior)
8143  (for cmp (le lt)
8144   (simplify
8145    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8146         (cmp:cs (pointer_plus@4 @2 @1) @0))
8147    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8148         && TYPE_OVERFLOW_WRAPS (sizetype)
8149         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8150     /* Calculate the rhs constant.  */
8151     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8152             offset_int rhs = off * 2; }
8153      /* Always fails for negative values.  */
8154      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8155       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8156          pick a canonical order.  This increases the chances of using the
8157          same pointer_plus in multiple checks.  */
8158       (with { bool swap_p = tree_swap_operands_p (@0, @2);
8159               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8160        (if (cmp == LT_EXPR)
8161         (gt (convert:sizetype
8162              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8163                                      { swap_p ? @0 : @2; }))
8164             { rhs_tree; })
8165         (gt (convert:sizetype
8166              (pointer_diff:ssizetype
8167               (pointer_plus { swap_p ? @2 : @0; }
8168                             { wide_int_to_tree (sizetype, off); })
8169               { swap_p ? @0 : @2; }))
8170             { rhs_tree; })))))))))
8172 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8173    element of @1.  */
8174 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8175  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8176   (with { int i = single_nonzero_element (@1); }
8177    (if (i >= 0)
8178     (with { tree elt = vector_cst_elt (@1, i);
8179             tree elt_type = TREE_TYPE (elt);
8180             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8181             tree size = bitsize_int (elt_bits);
8182             tree pos = bitsize_int (elt_bits * i); }
8183      (view_convert
8184       (bit_and:elt_type
8185        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8186        { elt; })))))))
8188 /* Fold reduction of a single nonzero element constructor.  */
8189 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8190   (simplify (reduc (CONSTRUCTOR@0))
8191     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8192                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8193             tree elt = ctor_single_nonzero_element (ctor); }
8194       (if (elt
8195            && !HONOR_SNANS (type)
8196            && !HONOR_SIGNED_ZEROS (type))
8197         { elt; }))))
8199 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
8200 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8201             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8202      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8203   (simplify (reduc (op @0 VECTOR_CST@1))
8204     (op (reduc:type @0) (reduc:type @1))))
8206 /* Simplify vector floating point operations of alternating sub/add pairs
8207    into using an fneg of a wider element type followed by a normal add.
8208    under IEEE 754 the fneg of the wider type will negate every even entry
8209    and when doing an add we get a sub of the even and add of every odd
8210    elements.  */
8211 (for plusminus (plus minus)
8212      minusplus (minus plus)
8213  (simplify
8214   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8215    (if (!VECTOR_INTEGER_TYPE_P (type)
8216         && !FLOAT_WORDS_BIG_ENDIAN
8217         /* plus is commutative, while minus is not, so :c can't be used.
8218            Do equality comparisons by hand and at the end pick the operands
8219            from the minus.  */
8220         && (operand_equal_p (@0, @2, 0)
8221             ? operand_equal_p (@1, @3, 0)
8222             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8223    (with
8224     {
8225       /* Build a vector of integers from the tree mask.  */
8226       vec_perm_builder builder;
8227     }
8228     (if (tree_to_vec_perm_builder (&builder, @4))
8229      (with
8230       {
8231         /* Create a vec_perm_indices for the integer vector.  */
8232         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8233         vec_perm_indices sel (builder, 2, nelts);
8234         machine_mode vec_mode = TYPE_MODE (type);
8235         machine_mode wide_mode;
8236         scalar_mode wide_elt_mode;
8237         poly_uint64 wide_nunits;
8238         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
8239       }
8240       (if (VECTOR_MODE_P (vec_mode)
8241            && sel.series_p (0, 2, 0, 2)
8242            && sel.series_p (1, 2, nelts + 1, 2)
8243            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
8244            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
8245            && related_vector_mode (vec_mode, wide_elt_mode,
8246                                    wide_nunits).exists (&wide_mode))
8247        (with
8248         {
8249           tree stype
8250             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
8251                                               TYPE_UNSIGNED (type));
8252           tree ntype = build_vector_type_for_mode (stype, wide_mode);
8254           /* The format has to be a non-extended ieee format.  */
8255           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
8256           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
8257         }
8258         (if (TYPE_MODE (stype) != BLKmode
8259              && VECTOR_TYPE_P (ntype)
8260              && fmt_old != NULL
8261              && fmt_new != NULL)
8262          (with
8263           {
8264             /* If the target doesn't support v1xx vectors, try using
8265                scalar mode xx instead.  */
8266             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
8267                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
8268               ntype = stype;
8269           }
8270           (if (fmt_new->signbit_rw
8271                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
8272                && fmt_new->signbit_rw == fmt_new->signbit_ro
8273                && targetm.can_change_mode_class (TYPE_MODE (ntype),
8274                                                  TYPE_MODE (type), ALL_REGS)
8275                && ((optimize_vectors_before_lowering_p ()
8276                     && VECTOR_TYPE_P (ntype))
8277                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
8278            (if (plusminus == PLUS_EXPR)
8279             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
8280             (minus @0 (view_convert:type
8281                         (negate (view_convert:ntype @1))))))))))))))))
8283 (simplify
8284  (vec_perm @0 @1 VECTOR_CST@2)
8285  (with
8286   {
8287     tree op0 = @0, op1 = @1, op2 = @2;
8288     machine_mode result_mode = TYPE_MODE (type);
8289     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
8291     /* Build a vector of integers from the tree mask.  */
8292     vec_perm_builder builder;
8293   }
8294   (if (tree_to_vec_perm_builder (&builder, op2))
8295    (with
8296     {
8297       /* Create a vec_perm_indices for the integer vector.  */
8298       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8299       bool single_arg = (op0 == op1);
8300       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
8301     }
8302     (if (sel.series_p (0, 1, 0, 1))
8303      { op0; }
8304      (if (sel.series_p (0, 1, nelts, 1))
8305       { op1; }
8306       (with
8307        {
8308          if (!single_arg)
8309            {
8310              if (sel.all_from_input_p (0))
8311                op1 = op0;
8312              else if (sel.all_from_input_p (1))
8313                {
8314                  op0 = op1;
8315                  sel.rotate_inputs (1);
8316                }
8317              else if (known_ge (poly_uint64 (sel[0]), nelts))
8318                {
8319                  std::swap (op0, op1);
8320                  sel.rotate_inputs (1);
8321                }
8322            }
8323          gassign *def;
8324          tree cop0 = op0, cop1 = op1;
8325          if (TREE_CODE (op0) == SSA_NAME
8326              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
8327              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8328            cop0 = gimple_assign_rhs1 (def);
8329          if (TREE_CODE (op1) == SSA_NAME
8330              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
8331              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
8332            cop1 = gimple_assign_rhs1 (def);
8333          tree t;
8334        }
8335        (if ((TREE_CODE (cop0) == VECTOR_CST
8336              || TREE_CODE (cop0) == CONSTRUCTOR)
8337             && (TREE_CODE (cop1) == VECTOR_CST
8338                 || TREE_CODE (cop1) == CONSTRUCTOR)
8339             && (t = fold_vec_perm (type, cop0, cop1, sel)))
8340         { t; }
8341         (with
8342          {
8343            bool changed = (op0 == op1 && !single_arg);
8344            tree ins = NULL_TREE;
8345            unsigned at = 0;
8347            /* See if the permutation is performing a single element
8348               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
8349               in that case.  But only if the vector mode is supported,
8350               otherwise this is invalid GIMPLE.  */
8351            if (op_mode != BLKmode
8352                && (TREE_CODE (cop0) == VECTOR_CST
8353                    || TREE_CODE (cop0) == CONSTRUCTOR
8354                    || TREE_CODE (cop1) == VECTOR_CST
8355                    || TREE_CODE (cop1) == CONSTRUCTOR))
8356              {
8357                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
8358                if (insert_first_p)
8359                  {
8360                    /* After canonicalizing the first elt to come from the
8361                       first vector we only can insert the first elt from
8362                       the first vector.  */
8363                    at = 0;
8364                    if ((ins = fold_read_from_vector (cop0, sel[0])))
8365                      op0 = op1;
8366                  }
8367                /* The above can fail for two-element vectors which always
8368                   appear to insert the first element, so try inserting
8369                   into the second lane as well.  For more than two
8370                   elements that's wasted time.  */
8371                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
8372                  {
8373                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
8374                    for (at = 0; at < encoded_nelts; ++at)
8375                      if (maybe_ne (sel[at], at))
8376                        break;
8377                    if (at < encoded_nelts
8378                        && (known_eq (at + 1, nelts)
8379                            || sel.series_p (at + 1, 1, at + 1, 1)))
8380                      {
8381                        if (known_lt (poly_uint64 (sel[at]), nelts))
8382                          ins = fold_read_from_vector (cop0, sel[at]);
8383                        else
8384                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
8385                      }
8386                  }
8387              }
8389            /* Generate a canonical form of the selector.  */
8390            if (!ins && sel.encoding () != builder)
8391              {
8392                /* Some targets are deficient and fail to expand a single
8393                   argument permutation while still allowing an equivalent
8394                   2-argument version.  */
8395                tree oldop2 = op2;
8396                if (sel.ninputs () == 2
8397                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
8398                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8399                else
8400                  {
8401                    vec_perm_indices sel2 (builder, 2, nelts);
8402                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
8403                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
8404                    else
8405                      /* Not directly supported with either encoding,
8406                         so use the preferred form.  */
8407                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
8408                  }
8409                if (!operand_equal_p (op2, oldop2, 0))
8410                  changed = true;
8411              }
8412          }
8413          (if (ins)
8414           (bit_insert { op0; } { ins; }
8415            { bitsize_int (at * vector_element_bits (type)); })
8416           (if (changed)
8417            (vec_perm { op0; } { op1; } { op2; }))))))))))))
8419 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
8421 (match vec_same_elem_p
8422  (vec_duplicate @0))
8424 (match vec_same_elem_p
8425  CONSTRUCTOR@0
8426  (if (TREE_CODE (@0) == SSA_NAME
8427       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
8429 (match vec_same_elem_p
8430  @0
8431  (if (uniform_vector_p (@0))))
8434 (simplify
8435  (vec_perm vec_same_elem_p@0 @0 @1)
8436  @0)
8438 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
8439 (simplify
8440  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8441  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8442   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
8443 (simplify
8444  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
8445  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
8446   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
8449 /* Merge
8450    c = VEC_PERM_EXPR <a, b, VCST0>;
8451    d = VEC_PERM_EXPR <c, c, VCST1>;
8452    to
8453    d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
8455 (simplify
8456  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
8457  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
8458   (with
8459    {
8460      machine_mode result_mode = TYPE_MODE (type);
8461      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
8462      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8463      vec_perm_builder builder0;
8464      vec_perm_builder builder1;
8465      vec_perm_builder builder2 (nelts, nelts, 1);
8466    }
8467    (if (tree_to_vec_perm_builder (&builder0, @3)
8468         && tree_to_vec_perm_builder (&builder1, @4))
8469     (with
8470      {
8471        vec_perm_indices sel0 (builder0, 2, nelts);
8472        vec_perm_indices sel1 (builder1, 1, nelts);
8474        for (int i = 0; i < nelts; i++)
8475          builder2.quick_push (sel0[sel1[i].to_constant ()]);
8477        vec_perm_indices sel2 (builder2, 2, nelts);
8479        tree op0 = NULL_TREE;
8480        /* If the new VEC_PERM_EXPR can't be handled but both
8481           original VEC_PERM_EXPRs can, punt.
8482           If one or both of the original VEC_PERM_EXPRs can't be
8483           handled and the new one can't be either, don't increase
8484           number of VEC_PERM_EXPRs that can't be handled.  */
8485        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
8486            || (single_use (@0)
8487                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
8488                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
8489                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
8490          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
8491      }
8492      (if (op0)
8493       (vec_perm @1 @2 { op0; })))))))
8496 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
8497    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
8498    constant which when multiplied by a power of 2 contains a unique value
8499    in the top 5 or 6 bits.  This is then indexed into a table which maps it
8500    to the number of trailing zeroes.  */
8501 (match (ctz_table_index @1 @2 @3)
8502   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
8504 (match (cond_expr_convert_p @0 @2 @3 @6)
8505  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
8506   (if (INTEGRAL_TYPE_P (type)
8507        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8508        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8509        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8510        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
8511        && TYPE_PRECISION (TREE_TYPE (@0))
8512           == TYPE_PRECISION (TREE_TYPE (@2))
8513        && TYPE_PRECISION (TREE_TYPE (@0))
8514           == TYPE_PRECISION (TREE_TYPE (@3))
8515        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
8516           signess when convert is truncation, but not ok for extension since
8517           it's sign_extend vs zero_extend.  */
8518        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
8519            || (TYPE_UNSIGNED (TREE_TYPE (@2))
8520                == TYPE_UNSIGNED (TREE_TYPE (@3))))
8521        && single_use (@4)
8522        && single_use (@5))))
8524 (for bit_op (bit_and bit_ior bit_xor)
8525  (match (bitwise_induction_p @0 @2 @3)
8526   (bit_op:c
8527    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
8528    @3)))
8530 (match (bitwise_induction_p @0 @2 @3)
8531  (bit_not
8532   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
8534 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
8535    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
8536 (simplify
8537   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
8538   (with { auto i = wi::neg (wi::to_wide (@2)); }
8539   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
8540     (if (wi::popcount (i) == 1
8541          && (wi::to_wide (@1)) == (i - 1))
8542       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
8543         (bit_and @0 @1)
8544       (cond (le @0 @1) @0 (bit_and @0 @1))))))
8546 /* -x & 1 -> x & 1.  */
8547 (simplify 
8548  (bit_and (negate @0) integer_onep@1)
8549  (if (!TYPE_OVERFLOW_SANITIZED (type))
8550   (bit_and @0 @1)))
8552 /* Optimize
8553    c1 = VEC_PERM_EXPR (a, a, mask)
8554    c2 = VEC_PERM_EXPR (b, b, mask)
8555    c3 = c1 op c2
8556    -->
8557    c = a op b
8558    c3 = VEC_PERM_EXPR (c, c, mask)
8559    For all integer non-div operations.  */
8560 (for op (plus minus mult bit_and bit_ior bit_xor
8561          lshift rshift)
8562  (simplify
8563   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
8564    (if (VECTOR_INTEGER_TYPE_P (type))
8565     (vec_perm (op@3 @0 @1) @3 @2))))
8567 /* Similar for float arithmetic when permutation constant covers
8568    all vector elements.  */
8569 (for op (plus minus mult)
8570  (simplify
8571   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
8572    (if (VECTOR_FLOAT_TYPE_P (type)
8573         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8574     (with
8575      {
8576        tree perm_cst = @2;
8577        vec_perm_builder builder;
8578        bool full_perm_p = false;
8579        if (tree_to_vec_perm_builder (&builder, perm_cst))
8580          {
8581            unsigned HOST_WIDE_INT nelts;
8583            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8584            /* Create a vec_perm_indices for the VECTOR_CST.  */
8585            vec_perm_indices sel (builder, 1, nelts);
8587            /* Check if perm indices covers all vector elements.  */
8588            if (sel.encoding ().encoded_full_vector_p ())
8589              {
8590                auto_sbitmap seen (nelts);
8591                bitmap_clear (seen);
8593                unsigned HOST_WIDE_INT count = 0, i;
8595                for (i = 0; i < nelts; i++)
8596                  {
8597                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
8598                     break;
8599                    count++;
8600                  }
8601                full_perm_p = count == nelts;
8602              }
8603          }
8604       }
8605       (if (full_perm_p)
8606         (vec_perm (op@3 @0 @1) @3 @2))))))