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