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