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