Fix PR 93568 (thinko)
[official-gcc.git] / gcc / match.pd
blob73834c255935930e2c93d7ba21a126f17d222882
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-2020 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 /* Binary operations and their associated IFN_COND_* function.  */
82 (define_operator_list UNCOND_BINARY
83   plus minus
84   mult trunc_div trunc_mod rdiv
85   min max
86   bit_and bit_ior bit_xor
87   lshift rshift)
88 (define_operator_list COND_BINARY
89   IFN_COND_ADD IFN_COND_SUB
90   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
91   IFN_COND_MIN IFN_COND_MAX
92   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
93   IFN_COND_SHL IFN_COND_SHR)
95 /* Same for ternary operations.  */
96 (define_operator_list UNCOND_TERNARY
97   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
98 (define_operator_list COND_TERNARY
99   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
101 /* With nop_convert? combine convert? and view_convert? in one pattern
102    plus conditionalize on tree_nop_conversion_p conversions.  */
103 (match (nop_convert @0)
104  (convert @0)
105  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
106 (match (nop_convert @0)
107  (view_convert @0)
108  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
109       && known_eq (TYPE_VECTOR_SUBPARTS (type),
110                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
111       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114    ABSU_EXPR returns unsigned absolute value of the operand and the operand
115    of the ABSU_EXPR will have the corresponding signed type.  */
116 (simplify (abs (convert @0))
117  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118       && !TYPE_UNSIGNED (TREE_TYPE (@0))
119       && element_precision (type) > element_precision (TREE_TYPE (@0)))
120   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121    (convert (absu:utype @0)))))
124 /* Simplifications of operations with one constant operand and
125    simplifications to constants or single values.  */
127 (for op (plus pointer_plus minus bit_ior bit_xor)
128   (simplify
129     (op @0 integer_zerop)
130     (non_lvalue @0)))
132 /* 0 +p index -> (type)index */
133 (simplify
134  (pointer_plus integer_zerop @1)
135  (non_lvalue (convert @1)))
137 /* ptr - 0 -> (type)ptr */
138 (simplify
139  (pointer_diff @0 integer_zerop)
140  (convert @0))
142 /* See if ARG1 is zero and X + ARG1 reduces to X.
143    Likewise if the operands are reversed.  */
144 (simplify
145  (plus:c @0 real_zerop@1)
146  (if (fold_real_zero_addition_p (type, @1, 0))
147   (non_lvalue @0)))
149 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
150 (simplify
151  (minus @0 real_zerop@1)
152  (if (fold_real_zero_addition_p (type, @1, 1))
153   (non_lvalue @0)))
155 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
156    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
157    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
158    if not -frounding-math.  For sNaNs the first operation would raise
159    exceptions but turn the result into qNan, so the second operation
160    would not raise it.   */
161 (for inner_op (plus minus)
162  (for outer_op (plus minus)
163   (simplify
164    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
165     (if (real_zerop (@1)
166          && real_zerop (@2)
167          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
168      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
169                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
170              bool outer_plus
171                = ((outer_op == PLUS_EXPR)
172                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
173       (if (outer_plus && !inner_plus)
174        (outer_op @0 @2)
175        @3))))))
177 /* Simplify x - x.
178    This is unsafe for certain floats even in non-IEEE formats.
179    In IEEE, it is unsafe because it does wrong for NaNs.
180    Also note that operand_equal_p is always false if an operand
181    is volatile.  */
182 (simplify
183  (minus @0 @0)
184  (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
185   { build_zero_cst (type); }))
186 (simplify
187  (pointer_diff @@0 @0)
188  { build_zero_cst (type); })
190 (simplify
191  (mult @0 integer_zerop@1)
192  @1)
194 /* Maybe fold x * 0 to 0.  The expressions aren't the same
195    when x is NaN, since x * 0 is also NaN.  Nor are they the
196    same in modes with signed zeros, since multiplying a
197    negative value by 0 gives -0, not +0.  */
198 (simplify
199  (mult @0 real_zerop@1)
200  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
201   @1))
203 /* In IEEE floating point, x*1 is not equivalent to x for snans.
204    Likewise for complex arithmetic with signed zeros.  */
205 (simplify
206  (mult @0 real_onep)
207  (if (!HONOR_SNANS (type)
208       && (!HONOR_SIGNED_ZEROS (type)
209           || !COMPLEX_FLOAT_TYPE_P (type)))
210   (non_lvalue @0)))
212 /* Transform x * -1.0 into -x.  */
213 (simplify
214  (mult @0 real_minus_onep)
215   (if (!HONOR_SNANS (type)
216        && (!HONOR_SIGNED_ZEROS (type)
217            || !COMPLEX_FLOAT_TYPE_P (type)))
218    (negate @0)))
220 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
221 (simplify
222  (mult SSA_NAME@1 SSA_NAME@2)
223   (if (INTEGRAL_TYPE_P (type)
224        && get_nonzero_bits (@1) == 1
225        && get_nonzero_bits (@2) == 1)
226    (bit_and @1 @2)))
228 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
229    unless the target has native support for the former but not the latter.  */
230 (simplify
231  (mult @0 VECTOR_CST@1)
232  (if (initializer_each_zero_or_onep (@1)
233       && !HONOR_SNANS (type)
234       && !HONOR_SIGNED_ZEROS (type))
235   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
236    (if (itype
237         && (!VECTOR_MODE_P (TYPE_MODE (type))
238             || (VECTOR_MODE_P (TYPE_MODE (itype))
239                 && optab_handler (and_optab,
240                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
241     (view_convert (bit_and:itype (view_convert @0)
242                                  (ne @1 { build_zero_cst (type); })))))))
244 (for cmp (gt ge lt le)
245      outp (convert convert negate negate)
246      outn (negate negate convert convert)
247  /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
248  /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
249  /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
250  /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
251  (simplify
252   (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
253   (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
254        && types_match (type, TREE_TYPE (@0)))
255    (switch
256     (if (types_match (type, float_type_node))
257      (BUILT_IN_COPYSIGNF @1 (outp @0)))
258     (if (types_match (type, double_type_node))
259      (BUILT_IN_COPYSIGN @1 (outp @0)))
260     (if (types_match (type, long_double_type_node))
261      (BUILT_IN_COPYSIGNL @1 (outp @0))))))
262  /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
263  /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
264  /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
265  /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
266  (simplify
267   (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
268   (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
269        && types_match (type, TREE_TYPE (@0)))
270    (switch
271     (if (types_match (type, float_type_node))
272      (BUILT_IN_COPYSIGNF @1 (outn @0)))
273     (if (types_match (type, double_type_node))
274      (BUILT_IN_COPYSIGN @1 (outn @0)))
275     (if (types_match (type, long_double_type_node))
276      (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
278 /* Transform X * copysign (1.0, X) into abs(X). */
279 (simplify
280  (mult:c @0 (COPYSIGN_ALL real_onep @0))
281  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
282   (abs @0)))
284 /* Transform X * copysign (1.0, -X) into -abs(X). */
285 (simplify
286  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
287  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
288   (negate (abs @0))))
290 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
291 (simplify
292  (COPYSIGN_ALL REAL_CST@0 @1)
293  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
294   (COPYSIGN_ALL (negate @0) @1)))
296 /* X * 1, X / 1 -> X.  */
297 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
298   (simplify
299     (op @0 integer_onep)
300     (non_lvalue @0)))
302 /* (A / (1 << B)) -> (A >> B).
303    Only for unsigned A.  For signed A, this would not preserve rounding
304    toward zero.
305    For example: (-1 / ( 1 << B)) !=  -1 >> B.
306    Also also widening conversions, like:
307    (A / (unsigned long long) (1U << B)) -> (A >> B)
308    or
309    (A / (unsigned long long) (1 << B)) -> (A >> B).
310    If the left shift is signed, it can be done only if the upper bits
311    of A starting from shift's type sign bit are zero, as
312    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
313    so it is valid only if A >> 31 is zero.  */
314 (simplify
315  (trunc_div @0 (convert? (lshift integer_onep@1 @2)))
316  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
317       && (!VECTOR_TYPE_P (type)
318           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
319           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
320       && (useless_type_conversion_p (type, TREE_TYPE (@1))
321           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
322               && (TYPE_UNSIGNED (TREE_TYPE (@1))
323                   || (element_precision (type)
324                       == element_precision (TREE_TYPE (@1)))
325                   || (INTEGRAL_TYPE_P (type)
326                       && (tree_nonzero_bits (@0)
327                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
328                                       true,
329                                       element_precision (type))) == 0)))))
330   (rshift @0 @2)))
332 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
333    undefined behavior in constexpr evaluation, and assuming that the division
334    traps enables better optimizations than these anyway.  */
335 (for div (trunc_div ceil_div floor_div round_div exact_div)
336  /* 0 / X is always zero.  */
337  (simplify
338   (div integer_zerop@0 @1)
339   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
340   (if (!integer_zerop (@1))
341    @0))
342   /* X / -1 is -X.  */
343  (simplify
344    (div @0 integer_minus_onep@1)
345    (if (!TYPE_UNSIGNED (type))
346     (negate @0)))
347  /* X / X is one.  */
348  (simplify
349   (div @0 @0)
350   /* But not for 0 / 0 so that we can get the proper warnings and errors.
351      And not for _Fract types where we can't build 1.  */
352   (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
353    { build_one_cst (type); }))
354  /* X / abs (X) is X < 0 ? -1 : 1.  */
355  (simplify
356    (div:C @0 (abs @0))
357    (if (INTEGRAL_TYPE_P (type)
358         && TYPE_OVERFLOW_UNDEFINED (type))
359     (cond (lt @0 { build_zero_cst (type); })
360           { build_minus_one_cst (type); } { build_one_cst (type); })))
361  /* X / -X is -1.  */
362  (simplify
363    (div:C @0 (negate @0))
364    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
365         && TYPE_OVERFLOW_UNDEFINED (type))
366     { build_minus_one_cst (type); })))
368 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
369    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
370 (simplify
371  (floor_div @0 @1)
372  (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
373       && TYPE_UNSIGNED (type))
374   (trunc_div @0 @1)))
376 /* Combine two successive divisions.  Note that combining ceil_div
377    and floor_div is trickier and combining round_div even more so.  */
378 (for div (trunc_div exact_div)
379  (simplify
380   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
381   (with {
382     wi::overflow_type overflow;
383     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
384                             TYPE_SIGN (type), &overflow);
385    }
386    (if (div == EXACT_DIV_EXPR
387         || optimize_successive_divisions_p (@2, @3))
388     (if (!overflow)
389      (div @0 { wide_int_to_tree (type, mul); })
390      (if (TYPE_UNSIGNED (type)
391           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
392       { build_zero_cst (type); }))))))
394 /* Combine successive multiplications.  Similar to above, but handling
395    overflow is different.  */
396 (simplify
397  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
398  (with {
399    wi::overflow_type overflow;
400    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
401                            TYPE_SIGN (type), &overflow);
402   }
403   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
404      otherwise undefined overflow implies that @0 must be zero.  */
405   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
406    (mult @0 { wide_int_to_tree (type, mul); }))))
408 /* Optimize A / A to 1.0 if we don't care about
409    NaNs or Infinities.  */
410 (simplify
411  (rdiv @0 @0)
412  (if (FLOAT_TYPE_P (type)
413       && ! HONOR_NANS (type)
414       && ! HONOR_INFINITIES (type))
415   { build_one_cst (type); }))
417 /* Optimize -A / A to -1.0 if we don't care about
418    NaNs or Infinities.  */
419 (simplify
420  (rdiv:C @0 (negate @0))
421  (if (FLOAT_TYPE_P (type)
422       && ! HONOR_NANS (type)
423       && ! HONOR_INFINITIES (type))
424   { build_minus_one_cst (type); }))
426 /* PR71078: x / abs(x) -> copysign (1.0, x) */
427 (simplify
428  (rdiv:C (convert? @0) (convert? (abs @0)))
429   (if (SCALAR_FLOAT_TYPE_P (type)
430        && ! HONOR_NANS (type)
431        && ! HONOR_INFINITIES (type))
432    (switch
433     (if (types_match (type, float_type_node))
434      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
435     (if (types_match (type, double_type_node))
436      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
437     (if (types_match (type, long_double_type_node))
438      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
440 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
441 (simplify
442  (rdiv @0 real_onep)
443  (if (!HONOR_SNANS (type))
444   (non_lvalue @0)))
446 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
447 (simplify
448  (rdiv @0 real_minus_onep)
449  (if (!HONOR_SNANS (type))
450   (negate @0)))
452 (if (flag_reciprocal_math)
453  /* Convert (A/B)/C to A/(B*C). */
454  (simplify
455   (rdiv (rdiv:s @0 @1) @2)
456   (rdiv @0 (mult @1 @2)))
458  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
459  (simplify
460   (rdiv @0 (mult:s @1 REAL_CST@2))
461   (with
462    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
463    (if (tem)
464     (rdiv (mult @0 { tem; } ) @1))))
466  /* Convert A/(B/C) to (A/B)*C  */
467  (simplify
468   (rdiv @0 (rdiv:s @1 @2))
469    (mult (rdiv @0 @1) @2)))
471 /* Simplify x / (- y) to -x / y.  */
472 (simplify
473  (rdiv @0 (negate @1))
474  (rdiv (negate @0) @1))
476 (if (flag_unsafe_math_optimizations)
477  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
478     Since C / x may underflow to zero, do this only for unsafe math.  */
479  (for op (lt le gt ge)
480       neg_op (gt ge lt le)
481   (simplify
482    (op (rdiv REAL_CST@0 @1) real_zerop@2)
483    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
484     (switch
485      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
486       (op @1 @2))
487      /* For C < 0, use the inverted operator.  */
488      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
489       (neg_op @1 @2)))))))
491 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
492 (for div (trunc_div ceil_div floor_div round_div exact_div)
493  (simplify
494   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
495   (if (integer_pow2p (@2)
496        && tree_int_cst_sgn (@2) > 0
497        && tree_nop_conversion_p (type, TREE_TYPE (@0))
498        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
499    (rshift (convert @0)
500            { build_int_cst (integer_type_node,
501                             wi::exact_log2 (wi::to_wide (@2))); }))))
503 /* If ARG1 is a constant, we can convert this to a multiply by the
504    reciprocal.  This does not have the same rounding properties,
505    so only do this if -freciprocal-math.  We can actually
506    always safely do it if ARG1 is a power of two, but it's hard to
507    tell if it is or not in a portable manner.  */
508 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
509  (simplify
510   (rdiv @0 cst@1)
511   (if (optimize)
512    (if (flag_reciprocal_math
513         && !real_zerop (@1))
514     (with
515      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
516      (if (tem)
517       (mult @0 { tem; } )))
518     (if (cst != COMPLEX_CST)
519      (with { tree inverse = exact_inverse (type, @1); }
520       (if (inverse)
521        (mult @0 { inverse; } ))))))))
523 (for mod (ceil_mod floor_mod round_mod trunc_mod)
524  /* 0 % X is always zero.  */
525  (simplify
526   (mod integer_zerop@0 @1)
527   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
528   (if (!integer_zerop (@1))
529    @0))
530  /* X % 1 is always zero.  */
531  (simplify
532   (mod @0 integer_onep)
533   { build_zero_cst (type); })
534  /* X % -1 is zero.  */
535  (simplify
536   (mod @0 integer_minus_onep@1)
537   (if (!TYPE_UNSIGNED (type))
538    { build_zero_cst (type); }))
539  /* X % X is zero.  */
540  (simplify
541   (mod @0 @0)
542   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
543   (if (!integer_zerop (@0))
544    { build_zero_cst (type); }))
545  /* (X % Y) % Y is just X % Y.  */
546  (simplify
547   (mod (mod@2 @0 @1) @1)
548   @2)
549  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
550  (simplify
551   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
552   (if (ANY_INTEGRAL_TYPE_P (type)
553        && TYPE_OVERFLOW_UNDEFINED (type)
554        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
555                              TYPE_SIGN (type)))
556    { build_zero_cst (type); }))
557  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
558     modulo and comparison, since it is simpler and equivalent.  */
559  (for cmp (eq ne)
560   (simplify
561    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
562    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
563     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
564      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
566 /* X % -C is the same as X % C.  */
567 (simplify
568  (trunc_mod @0 INTEGER_CST@1)
569   (if (TYPE_SIGN (type) == SIGNED
570        && !TREE_OVERFLOW (@1)
571        && wi::neg_p (wi::to_wide (@1))
572        && !TYPE_OVERFLOW_TRAPS (type)
573        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
574        && !sign_bit_p (@1, @1))
575    (trunc_mod @0 (negate @1))))
577 /* X % -Y is the same as X % Y.  */
578 (simplify
579  (trunc_mod @0 (convert? (negate @1)))
580  (if (INTEGRAL_TYPE_P (type)
581       && !TYPE_UNSIGNED (type)
582       && !TYPE_OVERFLOW_TRAPS (type)
583       && tree_nop_conversion_p (type, TREE_TYPE (@1))
584       /* Avoid this transformation if X might be INT_MIN or
585          Y might be -1, because we would then change valid
586          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
587       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
588           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
589                                                         (TREE_TYPE (@1))))))
590   (trunc_mod @0 (convert @1))))
592 /* X - (X / Y) * Y is the same as X % Y.  */
593 (simplify
594  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
595  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
596   (convert (trunc_mod @0 @1))))
598 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
599    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
600    Also optimize A % (C << N)  where C is a power of 2,
601    to A & ((C << N) - 1).  */
602 (match (power_of_two_cand @1)
603  INTEGER_CST@1)
604 (match (power_of_two_cand @1)
605  (lshift INTEGER_CST@1 @2))
606 (for mod (trunc_mod floor_mod)
607  (simplify
608   (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
609   (if ((TYPE_UNSIGNED (type)
610         || tree_expr_nonnegative_p (@0))
611         && tree_nop_conversion_p (type, TREE_TYPE (@3))
612         && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
613    (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
615 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
616 (simplify
617  (trunc_div (mult @0 integer_pow2p@1) @1)
618  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
619   (bit_and @0 { wide_int_to_tree
620                 (type, wi::mask (TYPE_PRECISION (type)
621                                  - wi::exact_log2 (wi::to_wide (@1)),
622                                  false, TYPE_PRECISION (type))); })))
624 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
625 (simplify
626  (mult (trunc_div @0 integer_pow2p@1) @1)
627  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
628   (bit_and @0 (negate @1))))
630 /* Simplify (t * 2) / 2) -> t.  */
631 (for div (trunc_div ceil_div floor_div round_div exact_div)
632  (simplify
633   (div (mult:c @0 @1) @1)
634   (if (ANY_INTEGRAL_TYPE_P (type)
635        && TYPE_OVERFLOW_UNDEFINED (type))
636    @0)))
638 (for op (negate abs)
639  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
640  (for coss (COS COSH)
641   (simplify
642    (coss (op @0))
643     (coss @0)))
644  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
645  (for pows (POW)
646   (simplify
647    (pows (op @0) REAL_CST@1)
648    (with { HOST_WIDE_INT n; }
649     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
650      (pows @0 @1)))))
651  /* Likewise for powi.  */
652  (for pows (POWI)
653   (simplify
654    (pows (op @0) INTEGER_CST@1)
655    (if ((wi::to_wide (@1) & 1) == 0)
656     (pows @0 @1))))
657  /* Strip negate and abs from both operands of hypot.  */
658  (for hypots (HYPOT)
659   (simplify
660    (hypots (op @0) @1)
661    (hypots @0 @1))
662   (simplify
663    (hypots @0 (op @1))
664    (hypots @0 @1)))
665  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
666  (for copysigns (COPYSIGN_ALL)
667   (simplify
668    (copysigns (op @0) @1)
669    (copysigns @0 @1))))
671 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
672 (simplify
673  (mult (abs@1 @0) @1)
674  (mult @0 @0))
676 /* Convert absu(x)*absu(x) -> x*x.  */
677 (simplify
678  (mult (absu@1 @0) @1)
679  (mult (convert@2 @0) @2))
681 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
682 (for coss (COS COSH)
683      copysigns (COPYSIGN)
684  (simplify
685   (coss (copysigns @0 @1))
686    (coss @0)))
688 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
689 (for pows (POW)
690      copysigns (COPYSIGN)
691  (simplify
692   (pows (copysigns @0 @2) REAL_CST@1)
693   (with { HOST_WIDE_INT n; }
694    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
695     (pows @0 @1)))))
696 /* Likewise for powi.  */
697 (for pows (POWI)
698      copysigns (COPYSIGN)
699  (simplify
700   (pows (copysigns @0 @2) INTEGER_CST@1)
701   (if ((wi::to_wide (@1) & 1) == 0)
702    (pows @0 @1))))
704 (for hypots (HYPOT)
705      copysigns (COPYSIGN)
706  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
707  (simplify
708   (hypots (copysigns @0 @1) @2)
709   (hypots @0 @2))
710  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
711  (simplify
712   (hypots @0 (copysigns @1 @2))
713   (hypots @0 @1)))
715 /* copysign(x, CST) -> [-]abs (x).  */
716 (for copysigns (COPYSIGN_ALL)
717  (simplify
718   (copysigns @0 REAL_CST@1)
719   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
720    (negate (abs @0))
721    (abs @0))))
723 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
724 (for copysigns (COPYSIGN_ALL)
725  (simplify
726   (copysigns (copysigns @0 @1) @2)
727   (copysigns @0 @2)))
729 /* copysign(x,y)*copysign(x,y) -> x*x.  */
730 (for copysigns (COPYSIGN_ALL)
731  (simplify
732   (mult (copysigns@2 @0 @1) @2)
733   (mult @0 @0)))
735 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
736 (for ccoss (CCOS CCOSH)
737  (simplify
738   (ccoss (negate @0))
739    (ccoss @0)))
741 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
742 (for ops (conj negate)
743  (for cabss (CABS)
744   (simplify
745    (cabss (ops @0))
746    (cabss @0))))
748 /* Fold (a * (1 << b)) into (a << b)  */
749 (simplify
750  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
751   (if (! FLOAT_TYPE_P (type)
752        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
753    (lshift @0 @2)))
755 /* Fold (1 << (C - x)) where C = precision(type) - 1
756    into ((1 << C) >> x). */
757 (simplify
758  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
759   (if (INTEGRAL_TYPE_P (type)
760        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
761        && single_use (@1))
762    (if (TYPE_UNSIGNED (type))
763      (rshift (lshift @0 @2) @3)
764    (with
765     { tree utype = unsigned_type_for (type); }
766     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
768 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
769 (simplify
770  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
771   (if (flag_associative_math
772        && single_use (@3))
773    (with
774     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
775     (if (tem)
776      (rdiv { tem; } @1)))))
778 /* Simplify ~X & X as zero.  */
779 (simplify
780  (bit_and:c (convert? @0) (convert? (bit_not @0)))
781   { build_zero_cst (type); })
783 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
784 (simplify
785   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
786   (if (TYPE_UNSIGNED (type))
787     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
789 (for bitop (bit_and bit_ior)
790      cmp (eq ne)
791  /* PR35691: Transform
792     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
793     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
794  (simplify
795   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
796    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
797         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
798         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
799     (cmp (bit_ior @0 (convert @1)) @2)))
800  /* Transform:
801     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
802     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
803  (simplify
804   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
805    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
806         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
807         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
808     (cmp (bit_and @0 (convert @1)) @2))))
810 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
811 (simplify
812  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
813   (minus (bit_xor @0 @1) @1))
814 (simplify
815  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
816  (if (~wi::to_wide (@2) == wi::to_wide (@1))
817   (minus (bit_xor @0 @1) @1)))
819 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
820 (simplify
821  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
822   (minus @1 (bit_xor @0 @1)))
824 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
825 (for op (bit_ior bit_xor plus)
826  (simplify
827   (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
828    (bit_xor @0 @1))
829  (simplify
830   (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
831   (if (~wi::to_wide (@2) == wi::to_wide (@1))
832    (bit_xor @0 @1))))
834 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
835 (simplify
836   (bit_ior:c (bit_xor:c @0 @1) @0)
837   (bit_ior @0 @1))
839 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
840 (simplify
841  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
842  @2)
844 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
845 (simplify
846  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
847  (bit_not (bit_and @0 @1)))
849 /* (~a & b) ^ a  -->   (a | b)   */
850 (simplify
851  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
852  (bit_ior @0 @1))
854 /* (a | b) & ~(a ^ b)  -->  a & b  */
855 (simplify
856  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
857  (bit_and @0 @1))
859 /* a | ~(a ^ b)  -->  a | ~b  */
860 (simplify
861  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
862  (bit_ior @0 (bit_not @1)))
864 /* (a | b) | (a &^ b)  -->  a | b  */
865 (for op (bit_and bit_xor)
866  (simplify
867   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
868   @2))
870 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
871 (simplify
872  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
873  @2)
875 /* ~(~a & b)  -->  a | ~b  */
876 (simplify
877  (bit_not (bit_and:cs (bit_not @0) @1))
878  (bit_ior @0 (bit_not @1)))
880 /* ~(~a | b) --> a & ~b */
881 (simplify
882  (bit_not (bit_ior:cs (bit_not @0) @1))
883  (bit_and @0 (bit_not @1)))
885 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
886 #if GIMPLE
887 (simplify
888  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
889  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
890       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
891   (bit_xor @0 @1)))
892 #endif
894 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
895    ((A & N) + B) & M -> (A + B) & M
896    Similarly if (N & M) == 0,
897    ((A | N) + B) & M -> (A + B) & M
898    and for - instead of + (or unary - instead of +)
899    and/or ^ instead of |.
900    If B is constant and (B & M) == 0, fold into A & M.  */
901 (for op (plus minus)
902  (for bitop (bit_and bit_ior bit_xor)
903   (simplify
904    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
905     (with
906      { tree pmop[2];
907        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
908                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
909                                        NULL_TREE, pmop); }
910      (if (utype)
911       (convert (bit_and (op (convert:utype { pmop[0]; })
912                             (convert:utype { pmop[1]; }))
913                         (convert:utype @2))))))
914   (simplify
915    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
916     (with
917      { tree pmop[2];
918        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
919                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
920                                        @4, pmop); }
921      (if (utype)
922       (convert (bit_and (op (convert:utype { pmop[0]; })
923                             (convert:utype { pmop[1]; }))
924                         (convert:utype @2)))))))
925  (simplify
926   (bit_and (op:s @0 @1) INTEGER_CST@2)
927    (with
928     { tree pmop[2];
929       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
930                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
931                                       NULL_TREE, NULL_TREE, pmop); }
932     (if (utype)
933      (convert (bit_and (op (convert:utype { pmop[0]; })
934                            (convert:utype { pmop[1]; }))
935                        (convert:utype @2)))))))
936 (for bitop (bit_and bit_ior bit_xor)
937  (simplify
938   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
939    (with
940     { tree pmop[2];
941       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
942                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
943                                       NULL_TREE, NULL_TREE, pmop); }
944     (if (utype)
945      (convert (bit_and (negate (convert:utype { pmop[0]; }))
946                        (convert:utype @1)))))))
948 /* X % Y is smaller than Y.  */
949 (for cmp (lt ge)
950  (simplify
951   (cmp (trunc_mod @0 @1) @1)
952   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
953    { constant_boolean_node (cmp == LT_EXPR, type); })))
954 (for cmp (gt le)
955  (simplify
956   (cmp @1 (trunc_mod @0 @1))
957   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
958    { constant_boolean_node (cmp == GT_EXPR, type); })))
960 /* x | ~0 -> ~0  */
961 (simplify
962  (bit_ior @0 integer_all_onesp@1)
963  @1)
965 /* x | 0 -> x  */
966 (simplify
967  (bit_ior @0 integer_zerop)
968  @0)
970 /* x & 0 -> 0  */
971 (simplify
972  (bit_and @0 integer_zerop@1)
973  @1)
975 /* ~x | x -> -1 */
976 /* ~x ^ x -> -1 */
977 /* ~x + x -> -1 */
978 (for op (bit_ior bit_xor plus)
979  (simplify
980   (op:c (convert? @0) (convert? (bit_not @0)))
981   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
983 /* x ^ x -> 0 */
984 (simplify
985   (bit_xor @0 @0)
986   { build_zero_cst (type); })
988 /* Canonicalize X ^ ~0 to ~X.  */
989 (simplify
990   (bit_xor @0 integer_all_onesp@1)
991   (bit_not @0))
993 /* x & ~0 -> x  */
994 (simplify
995  (bit_and @0 integer_all_onesp)
996   (non_lvalue @0))
998 /* x & x -> x,  x | x -> x  */
999 (for bitop (bit_and bit_ior)
1000  (simplify
1001   (bitop @0 @0)
1002   (non_lvalue @0)))
1004 /* x & C -> x if we know that x & ~C == 0.  */
1005 #if GIMPLE
1006 (simplify
1007  (bit_and SSA_NAME@0 INTEGER_CST@1)
1008  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1009       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1010   @0))
1011 #endif
1013 /* x + (x & 1) -> (x + 1) & ~1 */
1014 (simplify
1015  (plus:c @0 (bit_and:s @0 integer_onep@1))
1016  (bit_and (plus @0 @1) (bit_not @1)))
1018 /* x & ~(x & y) -> x & ~y */
1019 /* x | ~(x | y) -> x | ~y  */
1020 (for bitop (bit_and bit_ior)
1021  (simplify
1022   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1023   (bitop @0 (bit_not @1))))
1025 /* (~x & y) | ~(x | y) -> ~x */
1026 (simplify
1027  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1028  @2)
1030 /* (x | y) ^ (x | ~y) -> ~x */
1031 (simplify
1032  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1033  (bit_not @0))
1035 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1036 (simplify
1037  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1038  (bit_not (bit_xor @0 @1)))
1040 /* (~x | y) ^ (x ^ y) -> x | ~y */
1041 (simplify
1042  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1043  (bit_ior @0 (bit_not @1)))
1045 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1046 (simplify
1047  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1048  (bit_not (bit_and @0 @1)))
1050 /* (x | y) & ~x -> y & ~x */
1051 /* (x & y) | ~x -> y | ~x */
1052 (for bitop (bit_and bit_ior)
1053      rbitop (bit_ior bit_and)
1054  (simplify
1055   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1056   (bitop @1 @2)))
1058 /* (x & y) ^ (x | y) -> x ^ y */
1059 (simplify
1060  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1061  (bit_xor @0 @1))
1063 /* (x ^ y) ^ (x | y) -> x & y */
1064 (simplify
1065  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1066  (bit_and @0 @1))
1068 /* (x & y) + (x ^ y) -> x | y */
1069 /* (x & y) | (x ^ y) -> x | y */
1070 /* (x & y) ^ (x ^ y) -> x | y */
1071 (for op (plus bit_ior bit_xor)
1072  (simplify
1073   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1074   (bit_ior @0 @1)))
1076 /* (x & y) + (x | y) -> x + y */
1077 (simplify
1078  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1079  (plus @0 @1))
1081 /* (x + y) - (x | y) -> x & y */
1082 (simplify
1083  (minus (plus @0 @1) (bit_ior @0 @1))
1084  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1085       && !TYPE_SATURATING (type))
1086   (bit_and @0 @1)))
1088 /* (x + y) - (x & y) -> x | y */
1089 (simplify
1090  (minus (plus @0 @1) (bit_and @0 @1))
1091  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1092       && !TYPE_SATURATING (type))
1093   (bit_ior @0 @1)))
1095 /* (x | y) - (x ^ y) -> x & y */
1096 (simplify
1097  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1098  (bit_and @0 @1))
1100 /* (x | y) - (x & y) -> x ^ y */
1101 (simplify
1102  (minus (bit_ior @0 @1) (bit_and @0 @1))
1103  (bit_xor @0 @1))
1105 /* (x | y) & ~(x & y) -> x ^ y */
1106 (simplify
1107  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1108  (bit_xor @0 @1))
1110 /* (x | y) & (~x ^ y) -> x & y */
1111 (simplify
1112  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1113  (bit_and @0 @1))
1115 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1116 (simplify
1117  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1118  (bit_not (bit_xor @0 @1)))
1120 /* (~x | y) ^ (x | ~y) -> x ^ y */
1121 (simplify
1122  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1123  (bit_xor @0 @1))
1125 /* ~x & ~y -> ~(x | y)
1126    ~x | ~y -> ~(x & y) */
1127 (for op (bit_and bit_ior)
1128      rop (bit_ior bit_and)
1129  (simplify
1130   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1131   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1132        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1133    (bit_not (rop (convert @0) (convert @1))))))
1135 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1136    with a constant, and the two constants have no bits in common,
1137    we should treat this as a BIT_IOR_EXPR since this may produce more
1138    simplifications.  */
1139 (for op (bit_xor plus)
1140  (simplify
1141   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1142       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1143   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1144        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1145        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1146    (bit_ior (convert @4) (convert @5)))))
1148 /* (X | Y) ^ X -> Y & ~ X*/
1149 (simplify
1150  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1151  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1152   (convert (bit_and @1 (bit_not @0)))))
1154 /* Convert ~X ^ ~Y to X ^ Y.  */
1155 (simplify
1156  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1157  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1158       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1159   (bit_xor (convert @0) (convert @1))))
1161 /* Convert ~X ^ C to X ^ ~C.  */
1162 (simplify
1163  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1164  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1165   (bit_xor (convert @0) (bit_not @1))))
1167 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1168 (for opo (bit_and bit_xor)
1169      opi (bit_xor bit_and)
1170  (simplify
1171   (opo:c (opi:cs @0 @1) @1)
1172   (bit_and (bit_not @0) @1)))
1174 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1175    operands are another bit-wise operation with a common input.  If so,
1176    distribute the bit operations to save an operation and possibly two if
1177    constants are involved.  For example, convert
1178      (A | B) & (A | C) into A | (B & C)
1179    Further simplification will occur if B and C are constants.  */
1180 (for op (bit_and bit_ior bit_xor)
1181      rop (bit_ior bit_and bit_and)
1182  (simplify
1183   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1184   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1185        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1186    (rop (convert @0) (op (convert @1) (convert @2))))))
1188 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1189 /* (X & Y) & Y -> X & Y
1190    (X | Y) | Y -> X | Y  */
1191 (for op (bit_and bit_ior)
1192  (simplify
1193   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1194   @2))
1195 /* (X ^ Y) ^ Y -> X  */
1196 (simplify
1197  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1198  (convert @0))
1199 /* (X & Y) & (X & Z) -> (X & Y) & Z
1200    (X | Y) | (X | Z) -> (X | Y) | Z  */
1201 (for op (bit_and bit_ior)
1202  (simplify
1203   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1204   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1205        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1206    (if (single_use (@5) && single_use (@6))
1207     (op @3 (convert @2))
1208     (if (single_use (@3) && single_use (@4))
1209      (op (convert @1) @5))))))
1210 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1211 (simplify
1212  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1213  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1214       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1215   (bit_xor (convert @1) (convert @2))))
1217 /* Convert abs (abs (X)) into abs (X).
1218    also absu (absu (X)) into absu (X).  */
1219 (simplify
1220  (abs (abs@1 @0))
1221  @1)
1223 (simplify
1224  (absu (convert@2 (absu@1 @0)))
1225  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1226   @1))
1228 /* Convert abs[u] (-X) -> abs[u] (X).  */
1229 (simplify
1230  (abs (negate @0))
1231  (abs @0))
1233 (simplify
1234  (absu (negate @0))
1235  (absu @0))
1237 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1238 (simplify
1239  (abs tree_expr_nonnegative_p@0)
1240  @0)
1242 (simplify
1243  (absu tree_expr_nonnegative_p@0)
1244  (convert @0))
1246 /* A few cases of fold-const.c negate_expr_p predicate.  */
1247 (match negate_expr_p
1248  INTEGER_CST
1249  (if ((INTEGRAL_TYPE_P (type)
1250        && TYPE_UNSIGNED (type))
1251       || (!TYPE_OVERFLOW_SANITIZED (type)
1252           && may_negate_without_overflow_p (t)))))
1253 (match negate_expr_p
1254  FIXED_CST)
1255 (match negate_expr_p
1256  (negate @0)
1257  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1258 (match negate_expr_p
1259  REAL_CST
1260  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1261 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1262    ways.  */
1263 (match negate_expr_p
1264  VECTOR_CST
1265  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1266 (match negate_expr_p
1267  (minus @0 @1)
1268  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1269       || (FLOAT_TYPE_P (type)
1270           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1271           && !HONOR_SIGNED_ZEROS (type)))))
1273 /* (-A) * (-B) -> A * B  */
1274 (simplify
1275  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1276   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1277        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1278    (mult (convert @0) (convert (negate @1)))))
1280 /* -(A + B) -> (-B) - A.  */
1281 (simplify
1282  (negate (plus:c @0 negate_expr_p@1))
1283  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1284       && !HONOR_SIGNED_ZEROS (element_mode (type)))
1285   (minus (negate @1) @0)))
1287 /* -(A - B) -> B - A.  */
1288 (simplify
1289  (negate (minus @0 @1))
1290  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1291       || (FLOAT_TYPE_P (type)
1292           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1293           && !HONOR_SIGNED_ZEROS (type)))
1294   (minus @1 @0)))
1295 (simplify
1296  (negate (pointer_diff @0 @1))
1297  (if (TYPE_OVERFLOW_UNDEFINED (type))
1298   (pointer_diff @1 @0)))
1300 /* A - B -> A + (-B) if B is easily negatable.  */
1301 (simplify
1302  (minus @0 negate_expr_p@1)
1303  (if (!FIXED_POINT_TYPE_P (type))
1304  (plus @0 (negate @1))))
1306 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1307    when profitable.
1308    For bitwise binary operations apply operand conversions to the
1309    binary operation result instead of to the operands.  This allows
1310    to combine successive conversions and bitwise binary operations.
1311    We combine the above two cases by using a conditional convert.  */
1312 (for bitop (bit_and bit_ior bit_xor)
1313  (simplify
1314   (bitop (convert @0) (convert? @1))
1315   (if (((TREE_CODE (@1) == INTEGER_CST
1316          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1317          && int_fits_type_p (@1, TREE_TYPE (@0)))
1318         || types_match (@0, @1))
1319        /* ???  This transform conflicts with fold-const.c doing
1320           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1321           constants (if x has signed type, the sign bit cannot be set
1322           in c).  This folds extension into the BIT_AND_EXPR.
1323           Restrict it to GIMPLE to avoid endless recursions.  */
1324        && (bitop != BIT_AND_EXPR || GIMPLE)
1325        && (/* That's a good idea if the conversion widens the operand, thus
1326               after hoisting the conversion the operation will be narrower.  */
1327            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1328            /* It's also a good idea if the conversion is to a non-integer
1329               mode.  */
1330            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1331            /* Or if the precision of TO is not the same as the precision
1332               of its mode.  */
1333            || !type_has_mode_precision_p (type)))
1334    (convert (bitop @0 (convert @1))))))
1336 (for bitop (bit_and bit_ior)
1337      rbitop (bit_ior bit_and)
1338   /* (x | y) & x -> x */
1339   /* (x & y) | x -> x */
1340  (simplify
1341   (bitop:c (rbitop:c @0 @1) @0)
1342   @0)
1343  /* (~x | y) & x -> x & y */
1344  /* (~x & y) | x -> x | y */
1345  (simplify
1346   (bitop:c (rbitop:c (bit_not @0) @1) @0)
1347   (bitop @0 @1)))
1349 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1350 (simplify
1351   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1352   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1354 /* Combine successive equal operations with constants.  */
1355 (for bitop (bit_and bit_ior bit_xor)
1356  (simplify
1357   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1358   (if (!CONSTANT_CLASS_P (@0))
1359    /* This is the canonical form regardless of whether (bitop @1 @2) can be
1360       folded to a constant.  */
1361    (bitop @0 (bitop @1 @2))
1362    /* In this case we have three constants and (bitop @0 @1) doesn't fold
1363       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
1364       the values involved are such that the operation can't be decided at
1365       compile time.  Try folding one of @0 or @1 with @2 to see whether
1366       that combination can be decided at compile time.
1368       Keep the existing form if both folds fail, to avoid endless
1369       oscillation.  */
1370    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1371     (if (cst1)
1372      (bitop @1 { cst1; })
1373      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1374       (if (cst2)
1375        (bitop @0 { cst2; }))))))))
1377 /* Try simple folding for X op !X, and X op X with the help
1378    of the truth_valued_p and logical_inverted_value predicates.  */
1379 (match truth_valued_p
1380  @0
1381  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1382 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1383  (match truth_valued_p
1384   (op @0 @1)))
1385 (match truth_valued_p
1386   (truth_not @0))
1388 (match (logical_inverted_value @0)
1389  (truth_not @0))
1390 (match (logical_inverted_value @0)
1391  (bit_not truth_valued_p@0))
1392 (match (logical_inverted_value @0)
1393  (eq @0 integer_zerop))
1394 (match (logical_inverted_value @0)
1395  (ne truth_valued_p@0 integer_truep))
1396 (match (logical_inverted_value @0)
1397  (bit_xor truth_valued_p@0 integer_truep))
1399 /* X & !X -> 0.  */
1400 (simplify
1401  (bit_and:c @0 (logical_inverted_value @0))
1402  { build_zero_cst (type); })
1403 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
1404 (for op (bit_ior bit_xor)
1405  (simplify
1406   (op:c truth_valued_p@0 (logical_inverted_value @0))
1407   { constant_boolean_node (true, type); }))
1408 /* X ==/!= !X is false/true.  */
1409 (for op (eq ne)
1410  (simplify
1411   (op:c truth_valued_p@0 (logical_inverted_value @0))
1412   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1414 /* ~~x -> x */
1415 (simplify
1416   (bit_not (bit_not @0))
1417   @0)
1419 /* Convert ~ (-A) to A - 1.  */
1420 (simplify
1421  (bit_not (convert? (negate @0)))
1422  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1423       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1424   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1426 /* Convert - (~A) to A + 1.  */
1427 (simplify
1428  (negate (nop_convert? (bit_not @0)))
1429  (plus (view_convert @0) { build_each_one_cst (type); }))
1431 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
1432 (simplify
1433  (bit_not (convert? (minus @0 integer_each_onep)))
1434  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1435       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1436   (convert (negate @0))))
1437 (simplify
1438  (bit_not (convert? (plus @0 integer_all_onesp)))
1439  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1440       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1441   (convert (negate @0))))
1443 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
1444 (simplify
1445  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1446  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1447   (convert (bit_xor @0 (bit_not @1)))))
1448 (simplify
1449  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1450  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1451   (convert (bit_xor @0 @1))))
1453 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
1454 (simplify
1455  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1456  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1457   (bit_not (bit_xor (view_convert @0) @1))))
1459 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1460 (simplify
1461  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1462  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1464 /* Fold A - (A & B) into ~B & A.  */
1465 (simplify
1466  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1467  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1468       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1469   (convert (bit_and (bit_not @1) @0))))
1471 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
1472 (for cmp (gt lt ge le)
1473 (simplify
1474  (mult (convert (cmp @0 @1)) @2)
1475   (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1477 /* For integral types with undefined overflow and C != 0 fold
1478    x * C EQ/NE y * C into x EQ/NE y.  */
1479 (for cmp (eq ne)
1480  (simplify
1481   (cmp (mult:c @0 @1) (mult:c @2 @1))
1482   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1483        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1484        && tree_expr_nonzero_p (@1))
1485    (cmp @0 @2))))
1487 /* For integral types with wrapping overflow and C odd fold
1488    x * C EQ/NE y * C into x EQ/NE y.  */
1489 (for cmp (eq ne)
1490  (simplify
1491   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1492   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1493        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1494        && (TREE_INT_CST_LOW (@1) & 1) != 0)
1495    (cmp @0 @2))))
1497 /* For integral types with undefined overflow and C != 0 fold
1498    x * C RELOP y * C into:
1500    x RELOP y for nonnegative C
1501    y RELOP x for negative C  */
1502 (for cmp (lt gt le ge)
1503  (simplify
1504   (cmp (mult:c @0 @1) (mult:c @2 @1))
1505   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1506        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1507    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1508     (cmp @0 @2)
1509    (if (TREE_CODE (@1) == INTEGER_CST
1510         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1511     (cmp @2 @0))))))
1513 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
1514 (for cmp (le gt)
1515      icmp (gt le)
1516  (simplify
1517   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1518    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1519         && TYPE_UNSIGNED (TREE_TYPE (@0))
1520         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1521         && (wi::to_wide (@2)
1522             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1523     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1524      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1526 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
1527 (for cmp (simple_comparison)
1528  (simplify
1529   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1530   (if (element_precision (@3) >= element_precision (@0)
1531        && types_match (@0, @1))
1532    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1533     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1534      (cmp @1 @0)
1535      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1536       (with
1537        {
1538         tree utype = unsigned_type_for (TREE_TYPE (@0));
1539        }
1540        (cmp (convert:utype @1) (convert:utype @0)))))
1541     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1542      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1543       (cmp @0 @1)
1544       (with
1545        {
1546         tree utype = unsigned_type_for (TREE_TYPE (@0));
1547        }
1548        (cmp (convert:utype @0) (convert:utype @1)))))))))
1550 /* X / C1 op C2 into a simple range test.  */
1551 (for cmp (simple_comparison)
1552  (simplify
1553   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1554   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1555        && integer_nonzerop (@1)
1556        && !TREE_OVERFLOW (@1)
1557        && !TREE_OVERFLOW (@2))
1558    (with { tree lo, hi; bool neg_overflow;
1559            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1560                                                    &neg_overflow); }
1561     (switch
1562      (if (code == LT_EXPR || code == GE_EXPR)
1563        (if (TREE_OVERFLOW (lo))
1564         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1565         (if (code == LT_EXPR)
1566          (lt @0 { lo; })
1567          (ge @0 { lo; }))))
1568      (if (code == LE_EXPR || code == GT_EXPR)
1569        (if (TREE_OVERFLOW (hi))
1570         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1571         (if (code == LE_EXPR)
1572          (le @0 { hi; })
1573          (gt @0 { hi; }))))
1574      (if (!lo && !hi)
1575       { build_int_cst (type, code == NE_EXPR); })
1576      (if (code == EQ_EXPR && !hi)
1577       (ge @0 { lo; }))
1578      (if (code == EQ_EXPR && !lo)
1579       (le @0 { hi; }))
1580      (if (code == NE_EXPR && !hi)
1581       (lt @0 { lo; }))
1582      (if (code == NE_EXPR && !lo)
1583       (gt @0 { hi; }))
1584      (if (GENERIC)
1585       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1586                            lo, hi); })
1587      (with
1588       {
1589         tree etype = range_check_type (TREE_TYPE (@0));
1590         if (etype)
1591           {
1592             hi = fold_convert (etype, hi);
1593             lo = fold_convert (etype, lo);
1594             hi = const_binop (MINUS_EXPR, etype, hi, lo);
1595           }
1596       }
1597       (if (etype && hi && !TREE_OVERFLOW (hi))
1598        (if (code == EQ_EXPR)
1599         (le (minus (convert:etype @0) { lo; }) { hi; })
1600         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1602 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
1603 (for op (lt le ge gt)
1604  (simplify
1605   (op (plus:c @0 @2) (plus:c @1 @2))
1606   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1607        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1608    (op @0 @1))))
1609 /* For equality and subtraction, this is also true with wrapping overflow.  */
1610 (for op (eq ne minus)
1611  (simplify
1612   (op (plus:c @0 @2) (plus:c @1 @2))
1613   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1614        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1615            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1616    (op @0 @1))))
1618 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
1619 (for op (lt le ge gt)
1620  (simplify
1621   (op (minus @0 @2) (minus @1 @2))
1622   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1623        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1624    (op @0 @1))))
1625 /* For equality and subtraction, this is also true with wrapping overflow.  */
1626 (for op (eq ne minus)
1627  (simplify
1628   (op (minus @0 @2) (minus @1 @2))
1629   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1630        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1631            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1632    (op @0 @1))))
1633 /* And for pointers...  */
1634 (for op (simple_comparison)
1635  (simplify
1636   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1637   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1638    (op @0 @1))))
1639 (simplify
1640  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1641  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1642       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1643   (pointer_diff @0 @1)))
1645 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
1646 (for op (lt le ge gt)
1647  (simplify
1648   (op (minus @2 @0) (minus @2 @1))
1649   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1650        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1651    (op @1 @0))))
1652 /* For equality and subtraction, this is also true with wrapping overflow.  */
1653 (for op (eq ne minus)
1654  (simplify
1655   (op (minus @2 @0) (minus @2 @1))
1656   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1657        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1658            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1659    (op @1 @0))))
1660 /* And for pointers...  */
1661 (for op (simple_comparison)
1662  (simplify
1663   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1664   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1665    (op @1 @0))))
1666 (simplify
1667  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1668  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1669       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1670   (pointer_diff @1 @0)))
1672 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
1673 (for op (lt le gt ge)
1674  (simplify
1675   (op:c (plus:c@2 @0 @1) @1)
1676   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1677        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1678        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1679        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1680    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1681 /* For equality, this is also true with wrapping overflow.  */
1682 (for op (eq ne)
1683  (simplify
1684   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1685   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1686        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1687            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1688        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1689        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1690        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1691    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1692  (simplify
1693   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1694   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1695        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1696        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1697    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1699 /* X - Y < X is the same as Y > 0 when there is no overflow.
1700    For equality, this is also true with wrapping overflow.  */
1701 (for op (simple_comparison)
1702  (simplify
1703   (op:c @0 (minus@2 @0 @1))
1704   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1705        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1706            || ((op == EQ_EXPR || op == NE_EXPR)
1707                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1708        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1709    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1711 /* Transform:
1712    (X / Y) == 0 -> X < Y if X, Y are unsigned.
1713    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
1714 (for cmp (eq ne)
1715      ocmp (lt ge)
1716  (simplify
1717   (cmp (trunc_div @0 @1) integer_zerop)
1718   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1719        /* Complex ==/!= is allowed, but not </>=.  */
1720        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1721        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1722    (ocmp @0 @1))))
1724 /* X == C - X can never be true if C is odd.  */
1725 (for cmp (eq ne)
1726  (simplify
1727   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1728   (if (TREE_INT_CST_LOW (@1) & 1)
1729    { constant_boolean_node (cmp == NE_EXPR, type); })))
1731 /* Arguments on which one can call get_nonzero_bits to get the bits
1732    possibly set.  */
1733 (match with_possible_nonzero_bits
1734  INTEGER_CST@0)
1735 (match with_possible_nonzero_bits
1736  SSA_NAME@0
1737  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1738 /* Slightly extended version, do not make it recursive to keep it cheap.  */
1739 (match (with_possible_nonzero_bits2 @0)
1740  with_possible_nonzero_bits@0)
1741 (match (with_possible_nonzero_bits2 @0)
1742  (bit_and:c with_possible_nonzero_bits@0 @2))
1744 /* Same for bits that are known to be set, but we do not have
1745    an equivalent to get_nonzero_bits yet.  */
1746 (match (with_certain_nonzero_bits2 @0)
1747  INTEGER_CST@0)
1748 (match (with_certain_nonzero_bits2 @0)
1749  (bit_ior @1 INTEGER_CST@0))
1751 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
1752 (for cmp (eq ne)
1753  (simplify
1754   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1755   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1756    { constant_boolean_node (cmp == NE_EXPR, type); })))
1758 /* ((X inner_op C0) outer_op C1)
1759    With X being a tree where value_range has reasoned certain bits to always be
1760    zero throughout its computed value range,
1761    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1762    where zero_mask has 1's for all bits that are sure to be 0 in
1763    and 0's otherwise.
1764    if (inner_op == '^') C0 &= ~C1;
1765    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1766    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1768 (for inner_op (bit_ior bit_xor)
1769      outer_op (bit_xor bit_ior)
1770 (simplify
1771  (outer_op
1772   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1773  (with
1774   {
1775     bool fail = false;
1776     wide_int zero_mask_not;
1777     wide_int C0;
1778     wide_int cst_emit;
1780     if (TREE_CODE (@2) == SSA_NAME)
1781       zero_mask_not = get_nonzero_bits (@2);
1782     else
1783       fail = true;
1785     if (inner_op == BIT_XOR_EXPR)
1786       {
1787         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1788         cst_emit = C0 | wi::to_wide (@1);
1789       }
1790     else
1791       {
1792         C0 = wi::to_wide (@0);
1793         cst_emit = C0 ^ wi::to_wide (@1);
1794       }
1795   }
1796   (if (!fail && (C0 & zero_mask_not) == 0)
1797    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1798    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1799     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1801 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
1802 (simplify
1803   (pointer_plus (pointer_plus:s @0 @1) @3)
1804   (pointer_plus @0 (plus @1 @3)))
1806 /* Pattern match
1807      tem1 = (long) ptr1;
1808      tem2 = (long) ptr2;
1809      tem3 = tem2 - tem1;
1810      tem4 = (unsigned long) tem3;
1811      tem5 = ptr1 + tem4;
1812    and produce
1813      tem5 = ptr2;  */
1814 (simplify
1815   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1816   /* Conditionally look through a sign-changing conversion.  */
1817   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1818        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1819             || (GENERIC && type == TREE_TYPE (@1))))
1820    @1))
1821 (simplify
1822   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1823   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1824    (convert @1)))
1826 /* Pattern match
1827      tem = (sizetype) ptr;
1828      tem = tem & algn;
1829      tem = -tem;
1830      ... = ptr p+ tem;
1831    and produce the simpler and easier to analyze with respect to alignment
1832      ... = ptr & ~algn;  */
1833 (simplify
1834   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1835   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1836    (bit_and @0 { algn; })))
1838 /* Try folding difference of addresses.  */
1839 (simplify
1840  (minus (convert ADDR_EXPR@0) (convert @1))
1841  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1842   (with { poly_int64 diff; }
1843    (if (ptr_difference_const (@0, @1, &diff))
1844     { build_int_cst_type (type, diff); }))))
1845 (simplify
1846  (minus (convert @0) (convert ADDR_EXPR@1))
1847  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1848   (with { poly_int64 diff; }
1849    (if (ptr_difference_const (@0, @1, &diff))
1850     { build_int_cst_type (type, diff); }))))
1851 (simplify
1852  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1853  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1854       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1855   (with { poly_int64 diff; }
1856    (if (ptr_difference_const (@0, @1, &diff))
1857     { build_int_cst_type (type, diff); }))))
1858 (simplify
1859  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1860  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1861       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1862   (with { poly_int64 diff; }
1863    (if (ptr_difference_const (@0, @1, &diff))
1864     { build_int_cst_type (type, diff); }))))
1866 /* If arg0 is derived from the address of an object or function, we may
1867    be able to fold this expression using the object or function's
1868    alignment.  */
1869 (simplify
1870  (bit_and (convert? @0) INTEGER_CST@1)
1871  (if (POINTER_TYPE_P (TREE_TYPE (@0))
1872       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1873   (with
1874    {
1875      unsigned int align;
1876      unsigned HOST_WIDE_INT bitpos;
1877      get_pointer_alignment_1 (@0, &align, &bitpos);
1878    }
1879    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1880     { wide_int_to_tree (type, (wi::to_wide (@1)
1881                                & (bitpos / BITS_PER_UNIT))); }))))
1883 (match min_value
1884  INTEGER_CST
1885  (if (INTEGRAL_TYPE_P (type)
1886       && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
1888 (match max_value
1889  INTEGER_CST
1890  (if (INTEGRAL_TYPE_P (type)
1891       && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
1893 /* x >  y  &&  x != XXX_MIN  -->  x > y
1894    x >  y  &&  x == XXX_MIN  -->  false . */
1895 (for eqne (eq ne)
1896  (simplify
1897   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
1898    (switch
1899     (if (eqne == EQ_EXPR)
1900      { constant_boolean_node (false, type); })
1901     (if (eqne == NE_EXPR)
1902      @2)
1903     )))
1905 /* x <  y  &&  x != XXX_MAX  -->  x < y
1906    x <  y  &&  x == XXX_MAX  -->  false.  */
1907 (for eqne (eq ne)
1908  (simplify
1909   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
1910    (switch
1911     (if (eqne == EQ_EXPR)
1912      { constant_boolean_node (false, type); })
1913     (if (eqne == NE_EXPR)
1914      @2)
1915     )))
1917 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
1918 (simplify
1919  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
1920   @2)
1922 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
1923 (simplify
1924  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
1925   @2)
1927 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
1928 (simplify
1929  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
1930   @2)
1932 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
1933 (simplify
1934  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
1935   { constant_boolean_node (true, type); })
1937 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
1938 (simplify
1939  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
1940   @2)
1942 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
1943 (simplify
1944  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
1945   @2)
1947 /* x >=  y  ||  x != XXX_MAX   -->  true
1948    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
1949 (for eqne (eq ne)
1950  (simplify
1951   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
1952    (switch
1953     (if (eqne == EQ_EXPR)
1954      @2)
1955     (if (eqne == NE_EXPR)
1956      { constant_boolean_node (true, type); }))))
1958 /* Convert (X == CST1) && (X OP2 CST2) to a known value
1959    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
1961 (for code1 (eq ne)
1962  (for code2 (eq ne lt gt le ge)
1963   (simplify
1964    (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
1965     (with
1966      {
1967       int cmp = tree_int_cst_compare (@1, @2);
1968       bool val;
1969       switch (code2)
1970          {
1971         case EQ_EXPR: val = (cmp == 0); break;
1972         case NE_EXPR: val = (cmp != 0); break;
1973         case LT_EXPR: val = (cmp < 0); break;
1974         case GT_EXPR: val = (cmp > 0); break;
1975         case LE_EXPR: val = (cmp <= 0); break;
1976         case GE_EXPR: val = (cmp >= 0); break;
1977         default: gcc_unreachable ();
1978         }
1979      }
1980      (switch
1981       (if (code1 == EQ_EXPR && val) @3)
1982       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
1983       (if (code1 == NE_EXPR && !val) @4))))))
1985 /* Convert (X OP1 CST1) && (X OP2 CST2).  */
1987 (for code1 (lt le gt ge)
1988  (for code2 (lt le gt ge)
1989   (simplify
1990   (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
1991    (with
1992     {
1993      int cmp = tree_int_cst_compare (@1, @2);
1994     }
1995     (switch
1996      /* Choose the more restrictive of two < or <= comparisons.  */
1997      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
1998           && (code2 == LT_EXPR || code2 == LE_EXPR))
1999       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2000        @3
2001        @4))
2002      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2003      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2004           && (code2 == GT_EXPR || code2 == GE_EXPR))
2005       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2006        @3
2007        @4))
2008      /* Check for singleton ranges.  */
2009      (if (cmp == 0
2010           && ((code1 == LE_EXPR && code2 == GE_EXPR)
2011             || (code1 == GE_EXPR && code2 == LE_EXPR)))
2012       (eq @0 @1))
2013      /* Check for disjoint ranges.  */
2014      (if (cmp <= 0
2015           && (code1 == LT_EXPR || code1 == LE_EXPR)
2016           && (code2 == GT_EXPR || code2 == GE_EXPR))
2017       { constant_boolean_node (false, type); })
2018      (if (cmp >= 0
2019           && (code1 == GT_EXPR || code1 == GE_EXPR)
2020           && (code2 == LT_EXPR || code2 == LE_EXPR))
2021       { constant_boolean_node (false, type); })
2022      )))))
2024 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2025    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2027 (for code1 (eq ne)
2028  (for code2 (eq ne lt gt le ge)
2029   (simplify
2030    (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2031     (with
2032      {
2033       int cmp = tree_int_cst_compare (@1, @2);
2034       bool val;
2035       switch (code2)
2036         {
2037         case EQ_EXPR: val = (cmp == 0); break;
2038         case NE_EXPR: val = (cmp != 0); break;
2039         case LT_EXPR: val = (cmp < 0); break;
2040         case GT_EXPR: val = (cmp > 0); break;
2041         case LE_EXPR: val = (cmp <= 0); break;
2042         case GE_EXPR: val = (cmp >= 0); break;
2043         default: gcc_unreachable ();
2044         }
2045      }
2046      (switch
2047       (if (code1 == EQ_EXPR && val) @4)
2048       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2049       (if (code1 == NE_EXPR && !val) @3))))))
2051 /* Convert (X OP1 CST1) || (X OP2 CST2).  */
2053 (for code1 (lt le gt ge)
2054  (for code2 (lt le gt ge)
2055   (simplify
2056   (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2057    (with
2058     {
2059      int cmp = tree_int_cst_compare (@1, @2);
2060     }
2061     (switch
2062      /* Choose the more restrictive of two < or <= comparisons.  */
2063      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2064           && (code2 == LT_EXPR || code2 == LE_EXPR))
2065       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2066        @4
2067        @3))
2068      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2069      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2070           && (code2 == GT_EXPR || code2 == GE_EXPR))
2071       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2072        @4
2073        @3))
2074      /* Check for singleton ranges.  */
2075      (if (cmp == 0
2076           && ((code1 == LT_EXPR && code2 == GT_EXPR)
2077               || (code1 == GT_EXPR && code2 == LT_EXPR)))
2078       (ne @0 @2))
2079      /* Check for disjoint ranges.  */
2080      (if (cmp >= 0
2081           && (code1 == LT_EXPR || code1 == LE_EXPR)
2082           && (code2 == GT_EXPR || code2 == GE_EXPR))
2083       { constant_boolean_node (true, type); })
2084      (if (cmp <= 0
2085           && (code1 == GT_EXPR || code1 == GE_EXPR)
2086           && (code2 == LT_EXPR || code2 == LE_EXPR))
2087       { constant_boolean_node (true, type); })
2088      )))))
2090 /* We can't reassociate at all for saturating types.  */
2091 (if (!TYPE_SATURATING (type))
2093  /* Contract negates.  */
2094  /* A + (-B) -> A - B */
2095  (simplify
2096   (plus:c @0 (convert? (negate @1)))
2097   /* Apply STRIP_NOPS on the negate.  */
2098   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2099        && !TYPE_OVERFLOW_SANITIZED (type))
2100    (with
2101     {
2102      tree t1 = type;
2103      if (INTEGRAL_TYPE_P (type)
2104          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2105        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2106     }
2107     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2108  /* A - (-B) -> A + B */
2109  (simplify
2110   (minus @0 (convert? (negate @1)))
2111   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2112        && !TYPE_OVERFLOW_SANITIZED (type))
2113    (with
2114     {
2115      tree t1 = type;
2116      if (INTEGRAL_TYPE_P (type)
2117          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2118        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2119     }
2120     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2121  /* -(T)(-A) -> (T)A
2122     Sign-extension is ok except for INT_MIN, which thankfully cannot
2123     happen without overflow.  */
2124  (simplify
2125   (negate (convert (negate @1)))
2126   (if (INTEGRAL_TYPE_P (type)
2127        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2128            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2129                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2130        && !TYPE_OVERFLOW_SANITIZED (type)
2131        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2132    (convert @1)))
2133  (simplify
2134   (negate (convert negate_expr_p@1))
2135   (if (SCALAR_FLOAT_TYPE_P (type)
2136        && ((DECIMAL_FLOAT_TYPE_P (type)
2137             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2138             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2139            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2140    (convert (negate @1))))
2141  (simplify
2142   (negate (nop_convert? (negate @1)))
2143   (if (!TYPE_OVERFLOW_SANITIZED (type)
2144        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2145    (view_convert @1)))
2147  /* We can't reassociate floating-point unless -fassociative-math
2148     or fixed-point plus or minus because of saturation to +-Inf.  */
2149  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2150       && !FIXED_POINT_TYPE_P (type))
2152   /* Match patterns that allow contracting a plus-minus pair
2153      irrespective of overflow issues.  */
2154   /* (A +- B) - A       ->  +- B */
2155   /* (A +- B) -+ B      ->  A */
2156   /* A - (A +- B)       -> -+ B */
2157   /* A +- (B -+ A)      ->  +- B */
2158   (simplify
2159    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2160    (view_convert @1))
2161   (simplify
2162    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2163    (if (!ANY_INTEGRAL_TYPE_P (type)
2164         || TYPE_OVERFLOW_WRAPS (type))
2165    (negate (view_convert @1))
2166    (view_convert (negate @1))))
2167   (simplify
2168    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2169    (view_convert @0))
2170   (simplify
2171    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2172     (if (!ANY_INTEGRAL_TYPE_P (type)
2173          || TYPE_OVERFLOW_WRAPS (type))
2174      (negate (view_convert @1))
2175      (view_convert (negate @1))))
2176   (simplify
2177    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2178    (view_convert @1))
2179   /* (A +- B) + (C - A)   -> C +- B */
2180   /* (A +  B) - (A - C)   -> B + C */
2181   /* More cases are handled with comparisons.  */
2182   (simplify
2183    (plus:c (plus:c @0 @1) (minus @2 @0))
2184    (plus @2 @1))
2185   (simplify
2186    (plus:c (minus @0 @1) (minus @2 @0))
2187    (minus @2 @1))
2188   (simplify
2189    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2190    (if (TYPE_OVERFLOW_UNDEFINED (type)
2191         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2192     (pointer_diff @2 @1)))
2193   (simplify
2194    (minus (plus:c @0 @1) (minus @0 @2))
2195    (plus @1 @2))
2197   /* (A +- CST1) +- CST2 -> A + CST3
2198      Use view_convert because it is safe for vectors and equivalent for
2199      scalars.  */
2200   (for outer_op (plus minus)
2201    (for inner_op (plus minus)
2202         neg_inner_op (minus plus)
2203     (simplify
2204      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2205                CONSTANT_CLASS_P@2)
2206      /* If one of the types wraps, use that one.  */
2207      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2208       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2209          forever if something doesn't simplify into a constant.  */
2210       (if (!CONSTANT_CLASS_P (@0))
2211        (if (outer_op == PLUS_EXPR)
2212         (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2213         (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2214       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2215            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2216        (if (outer_op == PLUS_EXPR)
2217         (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2218         (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2219        /* If the constant operation overflows we cannot do the transform
2220           directly as we would introduce undefined overflow, for example
2221           with (a - 1) + INT_MIN.  */
2222        (if (types_match (type, @0))
2223         (with { tree cst = const_binop (outer_op == inner_op
2224                                         ? PLUS_EXPR : MINUS_EXPR,
2225                                         type, @1, @2); }
2226          (if (cst && !TREE_OVERFLOW (cst))
2227           (inner_op @0 { cst; } )
2228           /* X+INT_MAX+1 is X-INT_MIN.  */
2229           (if (INTEGRAL_TYPE_P (type) && cst
2230                && wi::to_wide (cst) == wi::min_value (type))
2231            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2232            /* Last resort, use some unsigned type.  */
2233            (with { tree utype = unsigned_type_for (type); }
2234             (if (utype)
2235              (view_convert (inner_op
2236                             (view_convert:utype @0)
2237                             (view_convert:utype
2238                              { drop_tree_overflow (cst); }))))))))))))))
2240   /* (CST1 - A) +- CST2 -> CST3 - A  */
2241   (for outer_op (plus minus)
2242    (simplify
2243     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2244     /* If one of the types wraps, use that one.  */
2245     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2246      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2247         forever if something doesn't simplify into a constant.  */
2248      (if (!CONSTANT_CLASS_P (@0))
2249       (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2250      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2251           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2252       (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2253       (if (types_match (type, @0))
2254        (with { tree cst = const_binop (outer_op, type, @1, @2); }
2255         (if (cst && !TREE_OVERFLOW (cst))
2256          (minus { cst; } @0))))))))
2258   /* CST1 - (CST2 - A) -> CST3 + A
2259      Use view_convert because it is safe for vectors and equivalent for
2260      scalars.  */
2261   (simplify
2262    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2263    /* If one of the types wraps, use that one.  */
2264    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2265     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2266       forever if something doesn't simplify into a constant.  */
2267     (if (!CONSTANT_CLASS_P (@0))
2268      (plus (view_convert @0) (minus @1 (view_convert @2))))
2269     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2270          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2271      (view_convert (plus @0 (minus (view_convert @1) @2)))
2272      (if (types_match (type, @0))
2273       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2274        (if (cst && !TREE_OVERFLOW (cst))
2275         (plus { cst; } @0)))))))
2277 /* ((T)(A)) + CST -> (T)(A + CST)  */
2278 #if GIMPLE
2279   (simplify
2280    (plus (convert SSA_NAME@0) INTEGER_CST@1)
2281     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2282          && TREE_CODE (type) == INTEGER_TYPE
2283          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2284          && int_fits_type_p (@1, TREE_TYPE (@0)))
2285      /* Perform binary operation inside the cast if the constant fits
2286         and (A + CST)'s range does not overflow.  */
2287      (with
2288       {
2289         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2290                           max_ovf = wi::OVF_OVERFLOW;
2291         tree inner_type = TREE_TYPE (@0);
2293         wide_int w1
2294           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2295                             TYPE_SIGN (inner_type));
2297         wide_int wmin0, wmax0;
2298         if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2299           {
2300             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2301             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2302           }
2303       }
2304      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2305       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2306      )))
2307 #endif
2309 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
2310 #if GIMPLE
2311   (for op (plus minus)
2312    (simplify
2313     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2314      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2315           && TREE_CODE (type) == INTEGER_TYPE
2316           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2317           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2318           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2319           && TYPE_OVERFLOW_WRAPS (type))
2320        (plus (convert @0) (op @2 (convert @1))))))
2321 #endif
2323   /* ~A + A -> -1 */
2324   (simplify
2325    (plus:c (bit_not @0) @0)
2326    (if (!TYPE_OVERFLOW_TRAPS (type))
2327     { build_all_ones_cst (type); }))
2329   /* ~A + 1 -> -A */
2330   (simplify
2331    (plus (convert? (bit_not @0)) integer_each_onep)
2332    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2333     (negate (convert @0))))
2335   /* -A - 1 -> ~A */
2336   (simplify
2337    (minus (convert? (negate @0)) integer_each_onep)
2338    (if (!TYPE_OVERFLOW_TRAPS (type)
2339         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2340     (bit_not (convert @0))))
2342   /* -1 - A -> ~A */
2343   (simplify
2344    (minus integer_all_onesp @0)
2345    (bit_not @0))
2347   /* (T)(P + A) - (T)P -> (T) A */
2348   (simplify
2349    (minus (convert (plus:c @@0 @1))
2350     (convert? @0))
2351    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2352         /* For integer types, if A has a smaller type
2353            than T the result depends on the possible
2354            overflow in P + A.
2355            E.g. T=size_t, A=(unsigned)429497295, P>0.
2356            However, if an overflow in P + A would cause
2357            undefined behavior, we can assume that there
2358            is no overflow.  */
2359         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2360             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2361     (convert @1)))
2362   (simplify
2363    (minus (convert (pointer_plus @@0 @1))
2364     (convert @0))
2365    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2366         /* For pointer types, if the conversion of A to the
2367            final type requires a sign- or zero-extension,
2368            then we have to punt - it is not defined which
2369            one is correct.  */
2370         || (POINTER_TYPE_P (TREE_TYPE (@0))
2371             && TREE_CODE (@1) == INTEGER_CST
2372             && tree_int_cst_sign_bit (@1) == 0))
2373     (convert @1)))
2374    (simplify
2375     (pointer_diff (pointer_plus @@0 @1) @0)
2376     /* The second argument of pointer_plus must be interpreted as signed, and
2377        thus sign-extended if necessary.  */
2378     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2379      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2380         second arg is unsigned even when we need to consider it as signed,
2381         we don't want to diagnose overflow here.  */
2382      (convert (view_convert:stype @1))))
2384   /* (T)P - (T)(P + A) -> -(T) A */
2385   (simplify
2386    (minus (convert? @0)
2387     (convert (plus:c @@0 @1)))
2388    (if (INTEGRAL_TYPE_P (type)
2389         && TYPE_OVERFLOW_UNDEFINED (type)
2390         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2391     (with { tree utype = unsigned_type_for (type); }
2392      (convert (negate (convert:utype @1))))
2393     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2394          /* For integer types, if A has a smaller type
2395             than T the result depends on the possible
2396             overflow in P + A.
2397             E.g. T=size_t, A=(unsigned)429497295, P>0.
2398             However, if an overflow in P + A would cause
2399             undefined behavior, we can assume that there
2400             is no overflow.  */
2401          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2402              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2403      (negate (convert @1)))))
2404   (simplify
2405    (minus (convert @0)
2406     (convert (pointer_plus @@0 @1)))
2407    (if (INTEGRAL_TYPE_P (type)
2408         && TYPE_OVERFLOW_UNDEFINED (type)
2409         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2410     (with { tree utype = unsigned_type_for (type); }
2411      (convert (negate (convert:utype @1))))
2412     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2413          /* For pointer types, if the conversion of A to the
2414             final type requires a sign- or zero-extension,
2415             then we have to punt - it is not defined which
2416             one is correct.  */
2417          || (POINTER_TYPE_P (TREE_TYPE (@0))
2418              && TREE_CODE (@1) == INTEGER_CST
2419              && tree_int_cst_sign_bit (@1) == 0))
2420      (negate (convert @1)))))
2421    (simplify
2422     (pointer_diff @0 (pointer_plus @@0 @1))
2423     /* The second argument of pointer_plus must be interpreted as signed, and
2424        thus sign-extended if necessary.  */
2425     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2426      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2427         second arg is unsigned even when we need to consider it as signed,
2428         we don't want to diagnose overflow here.  */
2429      (negate (convert (view_convert:stype @1)))))
2431   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2432   (simplify
2433    (minus (convert (plus:c @@0 @1))
2434     (convert (plus:c @0 @2)))
2435    (if (INTEGRAL_TYPE_P (type)
2436         && TYPE_OVERFLOW_UNDEFINED (type)
2437         && element_precision (type) <= element_precision (TREE_TYPE (@1))
2438         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2439     (with { tree utype = unsigned_type_for (type); }
2440      (convert (minus (convert:utype @1) (convert:utype @2))))
2441     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2442           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2443          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2444              /* For integer types, if A has a smaller type
2445                 than T the result depends on the possible
2446                 overflow in P + A.
2447                 E.g. T=size_t, A=(unsigned)429497295, P>0.
2448                 However, if an overflow in P + A would cause
2449                 undefined behavior, we can assume that there
2450                 is no overflow.  */
2451              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2452                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2453                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2454                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2455      (minus (convert @1) (convert @2)))))
2456   (simplify
2457    (minus (convert (pointer_plus @@0 @1))
2458     (convert (pointer_plus @0 @2)))
2459    (if (INTEGRAL_TYPE_P (type)
2460         && TYPE_OVERFLOW_UNDEFINED (type)
2461         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2462     (with { tree utype = unsigned_type_for (type); }
2463      (convert (minus (convert:utype @1) (convert:utype @2))))
2464     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2465          /* For pointer types, if the conversion of A to the
2466             final type requires a sign- or zero-extension,
2467             then we have to punt - it is not defined which
2468             one is correct.  */
2469          || (POINTER_TYPE_P (TREE_TYPE (@0))
2470              && TREE_CODE (@1) == INTEGER_CST
2471              && tree_int_cst_sign_bit (@1) == 0
2472              && TREE_CODE (@2) == INTEGER_CST
2473              && tree_int_cst_sign_bit (@2) == 0))
2474      (minus (convert @1) (convert @2)))))
2475    (simplify
2476     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2477     /* The second argument of pointer_plus must be interpreted as signed, and
2478        thus sign-extended if necessary.  */
2479     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2480      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2481         second arg is unsigned even when we need to consider it as signed,
2482         we don't want to diagnose overflow here.  */
2483      (minus (convert (view_convert:stype @1))
2484             (convert (view_convert:stype @2)))))))
2486 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2487     Modeled after fold_plusminus_mult_expr.  */
2488 (if (!TYPE_SATURATING (type)
2489      && (!FLOAT_TYPE_P (type) || flag_associative_math))
2490  (for plusminus (plus minus)
2491   (simplify
2492    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2493    (if ((!ANY_INTEGRAL_TYPE_P (type)
2494          || TYPE_OVERFLOW_WRAPS (type)
2495          || (INTEGRAL_TYPE_P (type)
2496              && tree_expr_nonzero_p (@0)
2497              && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2498         /* If @1 +- @2 is constant require a hard single-use on either
2499            original operand (but not on both).  */
2500         && (single_use (@3) || single_use (@4)))
2501     (mult (plusminus @1 @2) @0)))
2502   /* We cannot generate constant 1 for fract.  */
2503   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2504    (simplify
2505     (plusminus @0 (mult:c@3 @0 @2))
2506     (if ((!ANY_INTEGRAL_TYPE_P (type)
2507           || TYPE_OVERFLOW_WRAPS (type)
2508           /* For @0 + @0*@2 this transformation would introduce UB
2509              (where there was none before) for @0 in [-1,0] and @2 max.
2510              For @0 - @0*@2 this transformation would introduce UB
2511              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
2512           || (INTEGRAL_TYPE_P (type)
2513               && ((tree_expr_nonzero_p (@0)
2514                    && expr_not_equal_to (@0,
2515                                 wi::minus_one (TYPE_PRECISION (type))))
2516                   || (plusminus == PLUS_EXPR
2517                       ? expr_not_equal_to (@2,
2518                             wi::max_value (TYPE_PRECISION (type), SIGNED))
2519                       /* Let's ignore the @0 -1 and @2 min case.  */
2520                       : (expr_not_equal_to (@2,
2521                             wi::min_value (TYPE_PRECISION (type), SIGNED))
2522                          && expr_not_equal_to (@2,
2523                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
2524                                 + 1))))))
2525          && single_use (@3))
2526      (mult (plusminus { build_one_cst (type); } @2) @0)))
2527    (simplify
2528     (plusminus (mult:c@3 @0 @2) @0)
2529     (if ((!ANY_INTEGRAL_TYPE_P (type)
2530           || TYPE_OVERFLOW_WRAPS (type)
2531           /* For @0*@2 + @0 this transformation would introduce UB
2532              (where there was none before) for @0 in [-1,0] and @2 max.
2533              For @0*@2 - @0 this transformation would introduce UB
2534              for @0 0 and @2 min.  */
2535           || (INTEGRAL_TYPE_P (type)
2536               && ((tree_expr_nonzero_p (@0)
2537                    && (plusminus == MINUS_EXPR
2538                        || expr_not_equal_to (@0,
2539                                 wi::minus_one (TYPE_PRECISION (type)))))
2540                   || expr_not_equal_to (@2,
2541                         (plusminus == PLUS_EXPR
2542                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2543                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2544          && single_use (@3))
2545      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2547 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
2549 (for minmax (min max FMIN_ALL FMAX_ALL)
2550  (simplify
2551   (minmax @0 @0)
2552   @0))
2553 /* min(max(x,y),y) -> y.  */
2554 (simplify
2555  (min:c (max:c @0 @1) @1)
2556  @1)
2557 /* max(min(x,y),y) -> y.  */
2558 (simplify
2559  (max:c (min:c @0 @1) @1)
2560  @1)
2561 /* max(a,-a) -> abs(a).  */
2562 (simplify
2563  (max:c @0 (negate @0))
2564  (if (TREE_CODE (type) != COMPLEX_TYPE
2565       && (! ANY_INTEGRAL_TYPE_P (type)
2566           || TYPE_OVERFLOW_UNDEFINED (type)))
2567   (abs @0)))
2568 /* min(a,-a) -> -abs(a).  */
2569 (simplify
2570  (min:c @0 (negate @0))
2571  (if (TREE_CODE (type) != COMPLEX_TYPE
2572       && (! ANY_INTEGRAL_TYPE_P (type)
2573           || TYPE_OVERFLOW_UNDEFINED (type)))
2574   (negate (abs @0))))
2575 (simplify
2576  (min @0 @1)
2577  (switch
2578   (if (INTEGRAL_TYPE_P (type)
2579        && TYPE_MIN_VALUE (type)
2580        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2581    @1)
2582   (if (INTEGRAL_TYPE_P (type)
2583        && TYPE_MAX_VALUE (type)
2584        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2585    @0)))
2586 (simplify
2587  (max @0 @1)
2588  (switch
2589   (if (INTEGRAL_TYPE_P (type)
2590        && TYPE_MAX_VALUE (type)
2591        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2592    @1)
2593   (if (INTEGRAL_TYPE_P (type)
2594        && TYPE_MIN_VALUE (type)
2595        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2596    @0)))
2598 /* max (a, a + CST) -> a + CST where CST is positive.  */
2599 /* max (a, a + CST) -> a where CST is negative.  */
2600 (simplify
2601  (max:c @0 (plus@2 @0 INTEGER_CST@1))
2602   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2603    (if (tree_int_cst_sgn (@1) > 0)
2604     @2
2605     @0)))
2607 /* min (a, a + CST) -> a where CST is positive.  */
2608 /* min (a, a + CST) -> a + CST where CST is negative. */
2609 (simplify
2610  (min:c @0 (plus@2 @0 INTEGER_CST@1))
2611   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2612    (if (tree_int_cst_sgn (@1) > 0)
2613     @0
2614     @2)))
2616 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2617    and the outer convert demotes the expression back to x's type.  */
2618 (for minmax (min max)
2619  (simplify
2620   (convert (minmax@0 (convert @1) INTEGER_CST@2))
2621   (if (INTEGRAL_TYPE_P (type)
2622        && types_match (@1, type) && int_fits_type_p (@2, type)
2623        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2624        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2625    (minmax @1 (convert @2)))))
2627 (for minmax (FMIN_ALL FMAX_ALL)
2628  /* If either argument is NaN, return the other one.  Avoid the
2629     transformation if we get (and honor) a signalling NaN.  */
2630  (simplify
2631   (minmax:c @0 REAL_CST@1)
2632   (if (real_isnan (TREE_REAL_CST_PTR (@1))
2633        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2634    @0)))
2635 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
2636    functions to return the numeric arg if the other one is NaN.
2637    MIN and MAX don't honor that, so only transform if -ffinite-math-only
2638    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
2639    worry about it either.  */
2640 (if (flag_finite_math_only)
2641  (simplify
2642   (FMIN_ALL @0 @1)
2643   (min @0 @1))
2644  (simplify
2645   (FMAX_ALL @0 @1)
2646   (max @0 @1)))
2647 /* min (-A, -B) -> -max (A, B)  */
2648 (for minmax (min max FMIN_ALL FMAX_ALL)
2649      maxmin (max min FMAX_ALL FMIN_ALL)
2650  (simplify
2651   (minmax (negate:s@2 @0) (negate:s@3 @1))
2652   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2653        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2654            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2655    (negate (maxmin @0 @1)))))
2656 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2657    MAX (~X, ~Y) -> ~MIN (X, Y)  */
2658 (for minmax (min max)
2659  maxmin (max min)
2660  (simplify
2661   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2662   (bit_not (maxmin @0 @1))))
2664 /* MIN (X, Y) == X -> X <= Y  */
2665 (for minmax (min min max max)
2666      cmp    (eq  ne  eq  ne )
2667      out    (le  gt  ge  lt )
2668  (simplify
2669   (cmp:c (minmax:c @0 @1) @0)
2670   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2671    (out @0 @1))))
2672 /* MIN (X, 5) == 0 -> X == 0
2673    MIN (X, 5) == 7 -> false  */
2674 (for cmp (eq ne)
2675  (simplify
2676   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2677   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2678                  TYPE_SIGN (TREE_TYPE (@0))))
2679    { constant_boolean_node (cmp == NE_EXPR, type); }
2680    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2681                   TYPE_SIGN (TREE_TYPE (@0))))
2682     (cmp @0 @2)))))
2683 (for cmp (eq ne)
2684  (simplify
2685   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2686   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2687                  TYPE_SIGN (TREE_TYPE (@0))))
2688    { constant_boolean_node (cmp == NE_EXPR, type); }
2689    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2690                   TYPE_SIGN (TREE_TYPE (@0))))
2691     (cmp @0 @2)))))
2692 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
2693 (for minmax (min     min     max     max     min     min     max     max    )
2694      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
2695      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2696  (simplify
2697   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2698   (comb (cmp @0 @2) (cmp @1 @2))))
2700 /* Undo fancy way of writing max/min or other ?: expressions,
2701    like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2702    People normally use ?: and that is what we actually try to optimize.  */
2703 (for cmp (simple_comparison)
2704  (simplify
2705   (minus @0 (bit_and:c (minus @0 @1)
2706                        (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2707   (if (INTEGRAL_TYPE_P (type)
2708        && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2709        && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2710        && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2711        && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2712            || !TYPE_UNSIGNED (TREE_TYPE (@4))))
2713    (cond (cmp @2 @3) @1 @0)))
2714  (simplify
2715   (plus:c @0 (bit_and:c (minus @1 @0)
2716                         (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2717   (if (INTEGRAL_TYPE_P (type)
2718        && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2719        && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2720        && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2721        && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2722            || !TYPE_UNSIGNED (TREE_TYPE (@4))))
2723    (cond (cmp @2 @3) @1 @0))))
2725 /* Simplifications of shift and rotates.  */
2727 (for rotate (lrotate rrotate)
2728  (simplify
2729   (rotate integer_all_onesp@0 @1)
2730   @0))
2732 /* Optimize -1 >> x for arithmetic right shifts.  */
2733 (simplify
2734  (rshift integer_all_onesp@0 @1)
2735  (if (!TYPE_UNSIGNED (type)
2736       && tree_expr_nonnegative_p (@1))
2737   @0))
2739 /* Optimize (x >> c) << c into x & (-1<<c).  */
2740 (simplify
2741  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
2742  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2743   /* It doesn't matter if the right shift is arithmetic or logical.  */
2744   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
2746 (simplify
2747  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
2748  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
2749       /* Allow intermediate conversion to integral type with whatever sign, as
2750          long as the low TYPE_PRECISION (type)
2751          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
2752       && INTEGRAL_TYPE_P (type)
2753       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2754       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2755       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
2756       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
2757           || wi::geu_p (wi::to_wide (@1),
2758                         TYPE_PRECISION (type)
2759                         - TYPE_PRECISION (TREE_TYPE (@2)))))
2760   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
2762 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2763    types.  */
2764 (simplify
2765  (rshift (lshift @0 INTEGER_CST@1) @1)
2766  (if (TYPE_UNSIGNED (type)
2767       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2768   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2770 (for shiftrotate (lrotate rrotate lshift rshift)
2771  (simplify
2772   (shiftrotate @0 integer_zerop)
2773   (non_lvalue @0))
2774  (simplify
2775   (shiftrotate integer_zerop@0 @1)
2776   @0)
2777  /* Prefer vector1 << scalar to vector1 << vector2
2778     if vector2 is uniform.  */
2779  (for vec (VECTOR_CST CONSTRUCTOR)
2780   (simplify
2781    (shiftrotate @0 vec@1)
2782    (with { tree tem = uniform_vector_p (@1); }
2783     (if (tem)
2784      (shiftrotate @0 { tem; }))))))
2786 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2787    Y is 0.  Similarly for X >> Y.  */
2788 #if GIMPLE
2789 (for shift (lshift rshift)
2790  (simplify
2791   (shift @0 SSA_NAME@1)
2792    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2793     (with {
2794       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2795       int prec = TYPE_PRECISION (TREE_TYPE (@1));
2796      }
2797      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2798       @0)))))
2799 #endif
2801 /* Rewrite an LROTATE_EXPR by a constant into an
2802    RROTATE_EXPR by a new constant.  */
2803 (simplify
2804  (lrotate @0 INTEGER_CST@1)
2805  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2806                             build_int_cst (TREE_TYPE (@1),
2807                                            element_precision (type)), @1); }))
2809 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
2810 (for op (lrotate rrotate rshift lshift)
2811  (simplify
2812   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2813   (with { unsigned int prec = element_precision (type); }
2814    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2815         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2816         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2817         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2818     (with { unsigned int low = (tree_to_uhwi (@1)
2819                                 + tree_to_uhwi (@2)); }
2820      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2821         being well defined.  */
2822      (if (low >= prec)
2823       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2824        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2825        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2826         { build_zero_cst (type); }
2827         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2828       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2831 /* ((1 << A) & 1) != 0 -> A == 0
2832    ((1 << A) & 1) == 0 -> A != 0 */
2833 (for cmp (ne eq)
2834      icmp (eq ne)
2835  (simplify
2836   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2837   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2839 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2840    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2841    if CST2 != 0.  */
2842 (for cmp (ne eq)
2843  (simplify
2844   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2845   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2846    (if (cand < 0
2847         || (!integer_zerop (@2)
2848             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2849     { constant_boolean_node (cmp == NE_EXPR, type); }
2850     (if (!integer_zerop (@2)
2851          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2852      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2854 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2855         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2856    if the new mask might be further optimized.  */
2857 (for shift (lshift rshift)
2858  (simplify
2859   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2860            INTEGER_CST@2)
2861    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2862         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2863         && tree_fits_uhwi_p (@1)
2864         && tree_to_uhwi (@1) > 0
2865         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2866     (with
2867      {
2868        unsigned int shiftc = tree_to_uhwi (@1);
2869        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2870        unsigned HOST_WIDE_INT newmask, zerobits = 0;
2871        tree shift_type = TREE_TYPE (@3);
2872        unsigned int prec;
2874        if (shift == LSHIFT_EXPR)
2875          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2876        else if (shift == RSHIFT_EXPR
2877                 && type_has_mode_precision_p (shift_type))
2878          {
2879            prec = TYPE_PRECISION (TREE_TYPE (@3));
2880            tree arg00 = @0;
2881            /* See if more bits can be proven as zero because of
2882               zero extension.  */
2883            if (@3 != @0
2884                && TYPE_UNSIGNED (TREE_TYPE (@0)))
2885              {
2886                tree inner_type = TREE_TYPE (@0);
2887                if (type_has_mode_precision_p (inner_type)
2888                    && TYPE_PRECISION (inner_type) < prec)
2889                  {
2890                    prec = TYPE_PRECISION (inner_type);
2891                    /* See if we can shorten the right shift.  */
2892                    if (shiftc < prec)
2893                      shift_type = inner_type;
2894                    /* Otherwise X >> C1 is all zeros, so we'll optimize
2895                       it into (X, 0) later on by making sure zerobits
2896                       is all ones.  */
2897                  }
2898              }
2899            zerobits = HOST_WIDE_INT_M1U;
2900            if (shiftc < prec)
2901              {
2902                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2903                zerobits <<= prec - shiftc;
2904              }
2905            /* For arithmetic shift if sign bit could be set, zerobits
2906               can contain actually sign bits, so no transformation is
2907               possible, unless MASK masks them all away.  In that
2908               case the shift needs to be converted into logical shift.  */
2909            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2910                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2911              {
2912                if ((mask & zerobits) == 0)
2913                  shift_type = unsigned_type_for (TREE_TYPE (@3));
2914                else
2915                  zerobits = 0;
2916              }
2917          }
2918      }
2919      /* ((X << 16) & 0xff00) is (X, 0).  */
2920      (if ((mask & zerobits) == mask)
2921       { build_int_cst (type, 0); }
2922       (with { newmask = mask | zerobits; }
2923        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2924         (with
2925          {
2926            /* Only do the transformation if NEWMASK is some integer
2927               mode's mask.  */
2928            for (prec = BITS_PER_UNIT;
2929                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2930              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2931                break;
2932          }
2933          (if (prec < HOST_BITS_PER_WIDE_INT
2934               || newmask == HOST_WIDE_INT_M1U)
2935           (with
2936            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2937            (if (!tree_int_cst_equal (newmaskt, @2))
2938             (if (shift_type != TREE_TYPE (@3))
2939              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2940              (bit_and @4 { newmaskt; })))))))))))))
2942 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2943    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
2944 (for shift (lshift rshift)
2945  (for bit_op (bit_and bit_xor bit_ior)
2946   (simplify
2947    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2948    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2949     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2950      (bit_op (shift (convert @0) @1) { mask; }))))))
2952 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
2953 (simplify
2954  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2955   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2956        && (element_precision (TREE_TYPE (@0))
2957            <= element_precision (TREE_TYPE (@1))
2958            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2959    (with
2960     { tree shift_type = TREE_TYPE (@0); }
2961      (convert (rshift (convert:shift_type @1) @2)))))
2963 /* ~(~X >>r Y) -> X >>r Y
2964    ~(~X <<r Y) -> X <<r Y */
2965 (for rotate (lrotate rrotate)
2966  (simplify
2967   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2968    (if ((element_precision (TREE_TYPE (@0))
2969          <= element_precision (TREE_TYPE (@1))
2970          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2971         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2972             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2973     (with
2974      { tree rotate_type = TREE_TYPE (@0); }
2975       (convert (rotate (convert:rotate_type @1) @2))))))
2977 /* Simplifications of conversions.  */
2979 /* Basic strip-useless-type-conversions / strip_nops.  */
2980 (for cvt (convert view_convert float fix_trunc)
2981  (simplify
2982   (cvt @0)
2983   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2984        || (GENERIC && type == TREE_TYPE (@0)))
2985    @0)))
2987 /* Contract view-conversions.  */
2988 (simplify
2989   (view_convert (view_convert @0))
2990   (view_convert @0))
2992 /* For integral conversions with the same precision or pointer
2993    conversions use a NOP_EXPR instead.  */
2994 (simplify
2995   (view_convert @0)
2996   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2997        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2998        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2999    (convert @0)))
3001 /* Strip inner integral conversions that do not change precision or size, or
3002    zero-extend while keeping the same size (for bool-to-char).  */
3003 (simplify
3004   (view_convert (convert@0 @1))
3005   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3006        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3007        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3008        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3009            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3010                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3011    (view_convert @1)))
3013 /* Simplify a view-converted empty constructor.  */
3014 (simplify
3015   (view_convert CONSTRUCTOR@0)
3016   (if (TREE_CODE (@0) != SSA_NAME
3017        && CONSTRUCTOR_NELTS (@0) == 0)
3018    { build_zero_cst (type); }))
3020 /* Re-association barriers around constants and other re-association
3021    barriers can be removed.  */
3022 (simplify
3023  (paren CONSTANT_CLASS_P@0)
3024  @0)
3025 (simplify
3026  (paren (paren@1 @0))
3027  @1)
3029 /* Handle cases of two conversions in a row.  */
3030 (for ocvt (convert float fix_trunc)
3031  (for icvt (convert float)
3032   (simplify
3033    (ocvt (icvt@1 @0))
3034    (with
3035     {
3036       tree inside_type = TREE_TYPE (@0);
3037       tree inter_type = TREE_TYPE (@1);
3038       int inside_int = INTEGRAL_TYPE_P (inside_type);
3039       int inside_ptr = POINTER_TYPE_P (inside_type);
3040       int inside_float = FLOAT_TYPE_P (inside_type);
3041       int inside_vec = VECTOR_TYPE_P (inside_type);
3042       unsigned int inside_prec = TYPE_PRECISION (inside_type);
3043       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3044       int inter_int = INTEGRAL_TYPE_P (inter_type);
3045       int inter_ptr = POINTER_TYPE_P (inter_type);
3046       int inter_float = FLOAT_TYPE_P (inter_type);
3047       int inter_vec = VECTOR_TYPE_P (inter_type);
3048       unsigned int inter_prec = TYPE_PRECISION (inter_type);
3049       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3050       int final_int = INTEGRAL_TYPE_P (type);
3051       int final_ptr = POINTER_TYPE_P (type);
3052       int final_float = FLOAT_TYPE_P (type);
3053       int final_vec = VECTOR_TYPE_P (type);
3054       unsigned int final_prec = TYPE_PRECISION (type);
3055       int final_unsignedp = TYPE_UNSIGNED (type);
3056     }
3057    (switch
3058     /* In addition to the cases of two conversions in a row
3059        handled below, if we are converting something to its own
3060        type via an object of identical or wider precision, neither
3061        conversion is needed.  */
3062     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3063           || (GENERIC
3064               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3065          && (((inter_int || inter_ptr) && final_int)
3066              || (inter_float && final_float))
3067          && inter_prec >= final_prec)
3068      (ocvt @0))
3070     /* Likewise, if the intermediate and initial types are either both
3071        float or both integer, we don't need the middle conversion if the
3072        former is wider than the latter and doesn't change the signedness
3073        (for integers).  Avoid this if the final type is a pointer since
3074        then we sometimes need the middle conversion.  */
3075     (if (((inter_int && inside_int) || (inter_float && inside_float))
3076          && (final_int || final_float)
3077          && inter_prec >= inside_prec
3078          && (inter_float || inter_unsignedp == inside_unsignedp))
3079      (ocvt @0))
3081     /* If we have a sign-extension of a zero-extended value, we can
3082        replace that by a single zero-extension.  Likewise if the
3083        final conversion does not change precision we can drop the
3084        intermediate conversion.  */
3085     (if (inside_int && inter_int && final_int
3086          && ((inside_prec < inter_prec && inter_prec < final_prec
3087               && inside_unsignedp && !inter_unsignedp)
3088              || final_prec == inter_prec))
3089      (ocvt @0))
3091     /* Two conversions in a row are not needed unless:
3092         - some conversion is floating-point (overstrict for now), or
3093         - some conversion is a vector (overstrict for now), or
3094         - the intermediate type is narrower than both initial and
3095           final, or
3096         - the intermediate type and innermost type differ in signedness,
3097           and the outermost type is wider than the intermediate, or
3098         - the initial type is a pointer type and the precisions of the
3099           intermediate and final types differ, or
3100         - the final type is a pointer type and the precisions of the
3101           initial and intermediate types differ.  */
3102     (if (! inside_float && ! inter_float && ! final_float
3103          && ! inside_vec && ! inter_vec && ! final_vec
3104          && (inter_prec >= inside_prec || inter_prec >= final_prec)
3105          && ! (inside_int && inter_int
3106                && inter_unsignedp != inside_unsignedp
3107                && inter_prec < final_prec)
3108          && ((inter_unsignedp && inter_prec > inside_prec)
3109              == (final_unsignedp && final_prec > inter_prec))
3110          && ! (inside_ptr && inter_prec != final_prec)
3111          && ! (final_ptr && inside_prec != inter_prec))
3112      (ocvt @0))
3114     /* A truncation to an unsigned type (a zero-extension) should be
3115        canonicalized as bitwise and of a mask.  */
3116     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
3117          && final_int && inter_int && inside_int
3118          && final_prec == inside_prec
3119          && final_prec > inter_prec
3120          && inter_unsignedp)
3121      (convert (bit_and @0 { wide_int_to_tree
3122                               (inside_type,
3123                                wi::mask (inter_prec, false,
3124                                          TYPE_PRECISION (inside_type))); })))
3126     /* If we are converting an integer to a floating-point that can
3127        represent it exactly and back to an integer, we can skip the
3128        floating-point conversion.  */
3129     (if (GIMPLE /* PR66211 */
3130          && inside_int && inter_float && final_int &&
3131          (unsigned) significand_size (TYPE_MODE (inter_type))
3132          >= inside_prec - !inside_unsignedp)
3133      (convert @0)))))))
3135 /* If we have a narrowing conversion to an integral type that is fed by a
3136    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3137    masks off bits outside the final type (and nothing else).  */
3138 (simplify
3139   (convert (bit_and @0 INTEGER_CST@1))
3140   (if (INTEGRAL_TYPE_P (type)
3141        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3142        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3143        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3144                                                     TYPE_PRECISION (type)), 0))
3145    (convert @0)))
3148 /* (X /[ex] A) * A -> X.  */
3149 (simplify
3150   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3151   (convert @0))
3153 /* Simplify (A / B) * B + (A % B) -> A.  */
3154 (for div (trunc_div ceil_div floor_div round_div)
3155      mod (trunc_mod ceil_mod floor_mod round_mod)
3156   (simplify
3157    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3158    @0))
3160 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
3161 (for op (plus minus)
3162  (simplify
3163   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3164   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3165        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3166    (with
3167      {
3168        wi::overflow_type overflow;
3169        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3170                                TYPE_SIGN (type), &overflow);
3171      }
3172      (if (types_match (type, TREE_TYPE (@2))
3173          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3174       (op @0 { wide_int_to_tree (type, mul); })
3175       (with { tree utype = unsigned_type_for (type); }
3176        (convert (op (convert:utype @0)
3177                     (mult (convert:utype @1) (convert:utype @2))))))))))
3179 /* Canonicalization of binary operations.  */
3181 /* Convert X + -C into X - C.  */
3182 (simplify
3183  (plus @0 REAL_CST@1)
3184  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3185   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3186    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3187     (minus @0 { tem; })))))
3189 /* Convert x+x into x*2.  */
3190 (simplify
3191  (plus @0 @0)
3192  (if (SCALAR_FLOAT_TYPE_P (type))
3193   (mult @0 { build_real (type, dconst2); })
3194   (if (INTEGRAL_TYPE_P (type))
3195    (mult @0 { build_int_cst (type, 2); }))))
3197 /* 0 - X  ->  -X.  */
3198 (simplify
3199  (minus integer_zerop @1)
3200  (negate @1))
3201 (simplify
3202  (pointer_diff integer_zerop @1)
3203  (negate (convert @1)))
3205 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
3206    ARG0 is zero and X + ARG0 reduces to X, since that would mean
3207    (-ARG1 + ARG0) reduces to -ARG1.  */
3208 (simplify
3209  (minus real_zerop@0 @1)
3210  (if (fold_real_zero_addition_p (type, @0, 0))
3211   (negate @1)))
3213 /* Transform x * -1 into -x.  */
3214 (simplify
3215  (mult @0 integer_minus_onep)
3216  (negate @0))
3218 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
3219    signed overflow for CST != 0 && CST != -1.  */
3220 (simplify
3221  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3222  (if (TREE_CODE (@2) != INTEGER_CST
3223       && single_use (@3)
3224       && !integer_zerop (@1) && !integer_minus_onep (@1))
3225   (mult (mult @0 @2) @1)))
3227 /* True if we can easily extract the real and imaginary parts of a complex
3228    number.  */
3229 (match compositional_complex
3230  (convert? (complex @0 @1)))
3232 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
3233 (simplify
3234  (complex (realpart @0) (imagpart @0))
3235  @0)
3236 (simplify
3237  (realpart (complex @0 @1))
3238  @0)
3239 (simplify
3240  (imagpart (complex @0 @1))
3241  @1)
3243 /* Sometimes we only care about half of a complex expression.  */
3244 (simplify
3245  (realpart (convert?:s (conj:s @0)))
3246  (convert (realpart @0)))
3247 (simplify
3248  (imagpart (convert?:s (conj:s @0)))
3249  (convert (negate (imagpart @0))))
3250 (for part (realpart imagpart)
3251  (for op (plus minus)
3252   (simplify
3253    (part (convert?:s@2 (op:s @0 @1)))
3254    (convert (op (part @0) (part @1))))))
3255 (simplify
3256  (realpart (convert?:s (CEXPI:s @0)))
3257  (convert (COS @0)))
3258 (simplify
3259  (imagpart (convert?:s (CEXPI:s @0)))
3260  (convert (SIN @0)))
3262 /* conj(conj(x)) -> x  */
3263 (simplify
3264  (conj (convert? (conj @0)))
3265  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3266   (convert @0)))
3268 /* conj({x,y}) -> {x,-y}  */
3269 (simplify
3270  (conj (convert?:s (complex:s @0 @1)))
3271  (with { tree itype = TREE_TYPE (type); }
3272   (complex (convert:itype @0) (negate (convert:itype @1)))))
3274 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
3275 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3276  (simplify
3277   (bswap (bswap @0))
3278   @0)
3279  (simplify
3280   (bswap (bit_not (bswap @0)))
3281   (bit_not @0))
3282  (for bitop (bit_xor bit_ior bit_and)
3283   (simplify
3284    (bswap (bitop:c (bswap @0) @1))
3285    (bitop @0 (bswap @1)))))
3288 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
3290 /* Simplify constant conditions.
3291    Only optimize constant conditions when the selected branch
3292    has the same type as the COND_EXPR.  This avoids optimizing
3293    away "c ? x : throw", where the throw has a void type.
3294    Note that we cannot throw away the fold-const.c variant nor
3295    this one as we depend on doing this transform before possibly
3296    A ? B : B -> B triggers and the fold-const.c one can optimize
3297    0 ? A : B to B even if A has side-effects.  Something
3298    genmatch cannot handle.  */
3299 (simplify
3300  (cond INTEGER_CST@0 @1 @2)
3301  (if (integer_zerop (@0))
3302   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3303    @2)
3304   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3305    @1)))
3306 (simplify
3307  (vec_cond VECTOR_CST@0 @1 @2)
3308  (if (integer_all_onesp (@0))
3309   @1
3310   (if (integer_zerop (@0))
3311    @2)))
3313 /* Sink unary operations to constant branches, but only if we do fold it to
3314    constants.  */
3315 (for op (negate bit_not abs absu)
3316  (simplify
3317   (op (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2))
3318   (with
3319    {
3320      tree cst1, cst2;
3321      cst1 = const_unop (op, type, @1);
3322      if (cst1)
3323        cst2 = const_unop (op, type, @2);
3324    }
3325    (if (cst1 && cst2)
3326     (vec_cond @0 { cst1; } { cst2; })))))
3328 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
3329    be extended.  */
3330 /* This pattern implements two kinds simplification:
3332    Case 1)
3333    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3334      1) Conversions are type widening from smaller type.
3335      2) Const c1 equals to c2 after canonicalizing comparison.
3336      3) Comparison has tree code LT, LE, GT or GE.
3337    This specific pattern is needed when (cmp (convert x) c) may not
3338    be simplified by comparison patterns because of multiple uses of
3339    x.  It also makes sense here because simplifying across multiple
3340    referred var is always benefitial for complicated cases.
3342    Case 2)
3343    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
3344 (for cmp (lt le gt ge eq)
3345  (simplify
3346   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3347   (with
3348    {
3349      tree from_type = TREE_TYPE (@1);
3350      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3351      enum tree_code code = ERROR_MARK;
3353      if (INTEGRAL_TYPE_P (from_type)
3354          && int_fits_type_p (@2, from_type)
3355          && (types_match (c1_type, from_type)
3356              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3357                  && (TYPE_UNSIGNED (from_type)
3358                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3359          && (types_match (c2_type, from_type)
3360              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3361                  && (TYPE_UNSIGNED (from_type)
3362                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3363        {
3364          if (cmp != EQ_EXPR)
3365            {
3366              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3367                {
3368                  /* X <= Y - 1 equals to X < Y.  */
3369                  if (cmp == LE_EXPR)
3370                    code = LT_EXPR;
3371                  /* X > Y - 1 equals to X >= Y.  */
3372                  if (cmp == GT_EXPR)
3373                    code = GE_EXPR;
3374                }
3375              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3376                {
3377                  /* X < Y + 1 equals to X <= Y.  */
3378                  if (cmp == LT_EXPR)
3379                    code = LE_EXPR;
3380                  /* X >= Y + 1 equals to X > Y.  */
3381                  if (cmp == GE_EXPR)
3382                    code = GT_EXPR;
3383                }
3384              if (code != ERROR_MARK
3385                  || wi::to_widest (@2) == wi::to_widest (@3))
3386                {
3387                  if (cmp == LT_EXPR || cmp == LE_EXPR)
3388                    code = MIN_EXPR;
3389                  if (cmp == GT_EXPR || cmp == GE_EXPR)
3390                    code = MAX_EXPR;
3391                }
3392            }
3393          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
3394          else if (int_fits_type_p (@3, from_type))
3395            code = EQ_EXPR;
3396        }
3397    }
3398    (if (code == MAX_EXPR)
3399     (convert (max @1 (convert @2)))
3400     (if (code == MIN_EXPR)
3401      (convert (min @1 (convert @2)))
3402      (if (code == EQ_EXPR)
3403       (convert (cond (eq @1 (convert @3))
3404                      (convert:from_type @3) (convert:from_type @2)))))))))
3406 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3408      1) OP is PLUS or MINUS.
3409      2) CMP is LT, LE, GT or GE.
3410      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3412    This pattern also handles special cases like:
3414      A) Operand x is a unsigned to signed type conversion and c1 is
3415         integer zero.  In this case,
3416           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
3417           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
3418      B) Const c1 may not equal to (C3 op' C2).  In this case we also
3419         check equality for (c1+1) and (c1-1) by adjusting comparison
3420         code.
3422    TODO: Though signed type is handled by this pattern, it cannot be
3423    simplified at the moment because C standard requires additional
3424    type promotion.  In order to match&simplify it here, the IR needs
3425    to be cleaned up by other optimizers, i.e, VRP.  */
3426 (for op (plus minus)
3427  (for cmp (lt le gt ge)
3428   (simplify
3429    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3430    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3431     (if (types_match (from_type, to_type)
3432          /* Check if it is special case A).  */
3433          || (TYPE_UNSIGNED (from_type)
3434              && !TYPE_UNSIGNED (to_type)
3435              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3436              && integer_zerop (@1)
3437              && (cmp == LT_EXPR || cmp == GE_EXPR)))
3438      (with
3439       {
3440         wi::overflow_type overflow = wi::OVF_NONE;
3441         enum tree_code code, cmp_code = cmp;
3442         wide_int real_c1;
3443         wide_int c1 = wi::to_wide (@1);
3444         wide_int c2 = wi::to_wide (@2);
3445         wide_int c3 = wi::to_wide (@3);
3446         signop sgn = TYPE_SIGN (from_type);
3448         /* Handle special case A), given x of unsigned type:
3449             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
3450             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
3451         if (!types_match (from_type, to_type))
3452           {
3453             if (cmp_code == LT_EXPR)
3454               cmp_code = GT_EXPR;
3455             if (cmp_code == GE_EXPR)
3456               cmp_code = LE_EXPR;
3457             c1 = wi::max_value (to_type);
3458           }
3459         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
3460            compute (c3 op' c2) and check if it equals to c1 with op' being
3461            the inverted operator of op.  Make sure overflow doesn't happen
3462            if it is undefined.  */
3463         if (op == PLUS_EXPR)
3464           real_c1 = wi::sub (c3, c2, sgn, &overflow);
3465         else
3466           real_c1 = wi::add (c3, c2, sgn, &overflow);
3468         code = cmp_code;
3469         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3470           {
3471             /* Check if c1 equals to real_c1.  Boundary condition is handled
3472                by adjusting comparison operation if necessary.  */
3473             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3474                 && !overflow)
3475               {
3476                 /* X <= Y - 1 equals to X < Y.  */
3477                 if (cmp_code == LE_EXPR)
3478                   code = LT_EXPR;
3479                 /* X > Y - 1 equals to X >= Y.  */
3480                 if (cmp_code == GT_EXPR)
3481                   code = GE_EXPR;
3482               }
3483             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3484                 && !overflow)
3485               {
3486                 /* X < Y + 1 equals to X <= Y.  */
3487                 if (cmp_code == LT_EXPR)
3488                   code = LE_EXPR;
3489                 /* X >= Y + 1 equals to X > Y.  */
3490                 if (cmp_code == GE_EXPR)
3491                   code = GT_EXPR;
3492               }
3493             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3494               {
3495                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3496                   code = MIN_EXPR;
3497                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3498                   code = MAX_EXPR;
3499               }
3500           }
3501       }
3502       (if (code == MAX_EXPR)
3503        (op (max @X { wide_int_to_tree (from_type, real_c1); })
3504            { wide_int_to_tree (from_type, c2); })
3505        (if (code == MIN_EXPR)
3506         (op (min @X { wide_int_to_tree (from_type, real_c1); })
3507             { wide_int_to_tree (from_type, c2); })))))))))
3509 (for cnd (cond vec_cond)
3510  /* A ? B : (A ? X : C) -> A ? B : C.  */
3511  (simplify
3512   (cnd @0 (cnd @0 @1 @2) @3)
3513   (cnd @0 @1 @3))
3514  (simplify
3515   (cnd @0 @1 (cnd @0 @2 @3))
3516   (cnd @0 @1 @3))
3517  /* A ? B : (!A ? C : X) -> A ? B : C.  */
3518  /* ???  This matches embedded conditions open-coded because genmatch
3519     would generate matching code for conditions in separate stmts only.
3520     The following is still important to merge then and else arm cases
3521     from if-conversion.  */
3522  (simplify
3523   (cnd @0 @1 (cnd @2 @3 @4))
3524   (if (inverse_conditions_p (@0, @2))
3525    (cnd @0 @1 @3)))
3526  (simplify
3527   (cnd @0 (cnd @1 @2 @3) @4)
3528   (if (inverse_conditions_p (@0, @1))
3529    (cnd @0 @3 @4)))
3531  /* A ? B : B -> B.  */
3532  (simplify
3533   (cnd @0 @1 @1)
3534   @1)
3536  /* !A ? B : C -> A ? C : B.  */
3537  (simplify
3538   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3539   (cnd @0 @2 @1)))
3541 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3542    return all -1 or all 0 results.  */
3543 /* ??? We could instead convert all instances of the vec_cond to negate,
3544    but that isn't necessarily a win on its own.  */
3545 (simplify
3546  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3547  (if (VECTOR_TYPE_P (type)
3548       && known_eq (TYPE_VECTOR_SUBPARTS (type),
3549                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3550       && (TYPE_MODE (TREE_TYPE (type))
3551           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3552   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3554 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
3555 (simplify
3556  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3557  (if (VECTOR_TYPE_P (type)
3558       && known_eq (TYPE_VECTOR_SUBPARTS (type),
3559                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3560       && (TYPE_MODE (TREE_TYPE (type))
3561           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3562   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3565 /* Simplifications of comparisons.  */
3567 /* See if we can reduce the magnitude of a constant involved in a
3568    comparison by changing the comparison code.  This is a canonicalization
3569    formerly done by maybe_canonicalize_comparison_1.  */
3570 (for cmp  (le gt)
3571      acmp (lt ge)
3572  (simplify
3573   (cmp @0 uniform_integer_cst_p@1)
3574   (with { tree cst = uniform_integer_cst_p (@1); }
3575    (if (tree_int_cst_sgn (cst) == -1)
3576      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3577                                    wide_int_to_tree (TREE_TYPE (cst),
3578                                                      wi::to_wide (cst)
3579                                                      + 1)); })))))
3580 (for cmp  (ge lt)
3581      acmp (gt le)
3582  (simplify
3583   (cmp @0 uniform_integer_cst_p@1)
3584   (with { tree cst = uniform_integer_cst_p (@1); }
3585    (if (tree_int_cst_sgn (cst) == 1)
3586     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3587                                   wide_int_to_tree (TREE_TYPE (cst),
3588                                   wi::to_wide (cst) - 1)); })))))
3590 /* We can simplify a logical negation of a comparison to the
3591    inverted comparison.  As we cannot compute an expression
3592    operator using invert_tree_comparison we have to simulate
3593    that with expression code iteration.  */
3594 (for cmp (tcc_comparison)
3595      icmp (inverted_tcc_comparison)
3596      ncmp (inverted_tcc_comparison_with_nans)
3597  /* Ideally we'd like to combine the following two patterns
3598     and handle some more cases by using
3599       (logical_inverted_value (cmp @0 @1))
3600     here but for that genmatch would need to "inline" that.
3601     For now implement what forward_propagate_comparison did.  */
3602  (simplify
3603   (bit_not (cmp @0 @1))
3604   (if (VECTOR_TYPE_P (type)
3605        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3606    /* Comparison inversion may be impossible for trapping math,
3607       invert_tree_comparison will tell us.  But we can't use
3608       a computed operator in the replacement tree thus we have
3609       to play the trick below.  */
3610    (with { enum tree_code ic = invert_tree_comparison
3611              (cmp, HONOR_NANS (@0)); }
3612     (if (ic == icmp)
3613      (icmp @0 @1)
3614      (if (ic == ncmp)
3615       (ncmp @0 @1))))))
3616  (simplify
3617   (bit_xor (cmp @0 @1) integer_truep)
3618   (with { enum tree_code ic = invert_tree_comparison
3619             (cmp, HONOR_NANS (@0)); }
3620    (if (ic == icmp)
3621     (icmp @0 @1)
3622     (if (ic == ncmp)
3623      (ncmp @0 @1))))))
3625 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3626    ??? The transformation is valid for the other operators if overflow
3627    is undefined for the type, but performing it here badly interacts
3628    with the transformation in fold_cond_expr_with_comparison which
3629    attempts to synthetize ABS_EXPR.  */
3630 (for cmp (eq ne)
3631  (for sub (minus pointer_diff)
3632   (simplify
3633    (cmp (sub@2 @0 @1) integer_zerop)
3634    (if (single_use (@2))
3635     (cmp @0 @1)))))
3637 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3638    signed arithmetic case.  That form is created by the compiler
3639    often enough for folding it to be of value.  One example is in
3640    computing loop trip counts after Operator Strength Reduction.  */
3641 (for cmp (simple_comparison)
3642      scmp (swapped_simple_comparison)
3643  (simplify
3644   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3645   /* Handle unfolded multiplication by zero.  */
3646   (if (integer_zerop (@1))
3647    (cmp @1 @2)
3648    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3649         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3650         && single_use (@3))
3651     /* If @1 is negative we swap the sense of the comparison.  */
3652     (if (tree_int_cst_sgn (@1) < 0)
3653      (scmp @0 @2)
3654      (cmp @0 @2))))))
3656 /* Simplify comparison of something with itself.  For IEEE
3657    floating-point, we can only do some of these simplifications.  */
3658 (for cmp (eq ge le)
3659  (simplify
3660   (cmp @0 @0)
3661   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3662        || ! HONOR_NANS (@0))
3663    { constant_boolean_node (true, type); }
3664    (if (cmp != EQ_EXPR)
3665     (eq @0 @0)))))
3666 (for cmp (ne gt lt)
3667  (simplify
3668   (cmp @0 @0)
3669   (if (cmp != NE_EXPR
3670        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3671        || ! HONOR_NANS (@0))
3672    { constant_boolean_node (false, type); })))
3673 (for cmp (unle unge uneq)
3674  (simplify
3675   (cmp @0 @0)
3676   { constant_boolean_node (true, type); }))
3677 (for cmp (unlt ungt)
3678  (simplify
3679   (cmp @0 @0)
3680   (unordered @0 @0)))
3681 (simplify
3682  (ltgt @0 @0)
3683  (if (!flag_trapping_math)
3684   { constant_boolean_node (false, type); }))
3686 /* Fold ~X op ~Y as Y op X.  */
3687 (for cmp (simple_comparison)
3688  (simplify
3689   (cmp (bit_not@2 @0) (bit_not@3 @1))
3690   (if (single_use (@2) && single_use (@3))
3691    (cmp @1 @0))))
3693 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
3694 (for cmp (simple_comparison)
3695      scmp (swapped_simple_comparison)
3696  (simplify
3697   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3698   (if (single_use (@2)
3699        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3700    (scmp @0 (bit_not @1)))))
3702 (for cmp (simple_comparison)
3703  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
3704  (simplify
3705   (cmp (convert@2 @0) (convert? @1))
3706   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3707        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3708            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3709        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3710            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3711    (with
3712     {
3713       tree type1 = TREE_TYPE (@1);
3714       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3715         {
3716           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3717           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3718               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3719             type1 = float_type_node;
3720           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3721               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3722             type1 = double_type_node;
3723         }
3724       tree newtype
3725         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3726            ? TREE_TYPE (@0) : type1);
3727     }
3728     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3729      (cmp (convert:newtype @0) (convert:newtype @1))))))
3731  (simplify
3732   (cmp @0 REAL_CST@1)
3733   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
3734   (switch
3735    /* a CMP (-0) -> a CMP 0  */
3736    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3737     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3738    /* x != NaN is always true, other ops are always false.  */
3739    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3740         && ! HONOR_SNANS (@1))
3741     { constant_boolean_node (cmp == NE_EXPR, type); })
3742    /* Fold comparisons against infinity.  */
3743    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3744         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3745     (with
3746      {
3747        REAL_VALUE_TYPE max;
3748        enum tree_code code = cmp;
3749        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3750        if (neg)
3751          code = swap_tree_comparison (code);
3752      }
3753      (switch
3754       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
3755       (if (code == GT_EXPR
3756            && !(HONOR_NANS (@0) && flag_trapping_math))
3757        { constant_boolean_node (false, type); })
3758       (if (code == LE_EXPR)
3759        /* x <= +Inf is always true, if we don't care about NaNs.  */
3760        (if (! HONOR_NANS (@0))
3761         { constant_boolean_node (true, type); }
3762         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3763            an "invalid" exception.  */
3764         (if (!flag_trapping_math)
3765          (eq @0 @0))))
3766       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3767          for == this introduces an exception for x a NaN.  */
3768       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3769            || code == GE_EXPR)
3770        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3771         (if (neg)
3772          (lt @0 { build_real (TREE_TYPE (@0), max); })
3773          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3774       /* x < +Inf is always equal to x <= DBL_MAX.  */
3775       (if (code == LT_EXPR)
3776        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3777         (if (neg)
3778          (ge @0 { build_real (TREE_TYPE (@0), max); })
3779          (le @0 { build_real (TREE_TYPE (@0), max); }))))
3780       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3781          an exception for x a NaN so use an unordered comparison.  */
3782       (if (code == NE_EXPR)
3783        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3784         (if (! HONOR_NANS (@0))
3785          (if (neg)
3786           (ge @0 { build_real (TREE_TYPE (@0), max); })
3787           (le @0 { build_real (TREE_TYPE (@0), max); }))
3788          (if (neg)
3789           (unge @0 { build_real (TREE_TYPE (@0), max); })
3790           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3792  /* If this is a comparison of a real constant with a PLUS_EXPR
3793     or a MINUS_EXPR of a real constant, we can convert it into a
3794     comparison with a revised real constant as long as no overflow
3795     occurs when unsafe_math_optimizations are enabled.  */
3796  (if (flag_unsafe_math_optimizations)
3797   (for op (plus minus)
3798    (simplify
3799     (cmp (op @0 REAL_CST@1) REAL_CST@2)
3800     (with
3801      {
3802        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3803                                TREE_TYPE (@1), @2, @1);
3804      }
3805      (if (tem && !TREE_OVERFLOW (tem))
3806       (cmp @0 { tem; }))))))
3808  /* Likewise, we can simplify a comparison of a real constant with
3809     a MINUS_EXPR whose first operand is also a real constant, i.e.
3810     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
3811     floating-point types only if -fassociative-math is set.  */
3812  (if (flag_associative_math)
3813   (simplify
3814    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3815    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3816     (if (tem && !TREE_OVERFLOW (tem))
3817      (cmp { tem; } @1)))))
3819  /* Fold comparisons against built-in math functions.  */
3820  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
3821   (for sq (SQRT)
3822    (simplify
3823     (cmp (sq @0) REAL_CST@1)
3824     (switch
3825      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3826       (switch
3827        /* sqrt(x) < y is always false, if y is negative.  */
3828        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3829         { constant_boolean_node (false, type); })
3830        /* sqrt(x) > y is always true, if y is negative and we
3831           don't care about NaNs, i.e. negative values of x.  */
3832        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3833         { constant_boolean_node (true, type); })
3834        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
3835        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3836      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3837       (switch
3838        /* sqrt(x) < 0 is always false.  */
3839        (if (cmp == LT_EXPR)
3840         { constant_boolean_node (false, type); })
3841        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
3842        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3843         { constant_boolean_node (true, type); })
3844        /* sqrt(x) <= 0 -> x == 0.  */
3845        (if (cmp == LE_EXPR)
3846         (eq @0 @1))
3847        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
3848           == or !=.  In the last case:
3850             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3852           if x is negative or NaN.  Due to -funsafe-math-optimizations,
3853           the results for other x follow from natural arithmetic.  */
3854        (cmp @0 @1)))
3855      (if ((cmp == LT_EXPR
3856            || cmp == LE_EXPR
3857            || cmp == GT_EXPR
3858            || cmp == GE_EXPR)
3859           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3860           /* Give up for -frounding-math.  */
3861           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
3862       (with
3863        {
3864          REAL_VALUE_TYPE c2;
3865          enum tree_code ncmp = cmp;
3866          const real_format *fmt
3867            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
3868          real_arithmetic (&c2, MULT_EXPR,
3869                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3870          real_convert (&c2, fmt, &c2);
3871          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
3872             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
3873          if (!REAL_VALUE_ISINF (c2))
3874            {
3875              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
3876                                         build_real (TREE_TYPE (@0), c2));
3877              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
3878                ncmp = ERROR_MARK;
3879              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
3880                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
3881                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
3882              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
3883                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
3884                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
3885              else
3886                {
3887                  /* With rounding to even, sqrt of up to 3 different values
3888                     gives the same normal result, so in some cases c2 needs
3889                     to be adjusted.  */
3890                  REAL_VALUE_TYPE c2alt, tow;
3891                  if (cmp == LT_EXPR || cmp == GE_EXPR)
3892                    tow = dconst0;
3893                  else
3894                    real_inf (&tow);
3895                  real_nextafter (&c2alt, fmt, &c2, &tow);
3896                  real_convert (&c2alt, fmt, &c2alt);
3897                  if (REAL_VALUE_ISINF (c2alt))
3898                    ncmp = ERROR_MARK;
3899                  else
3900                    {
3901                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
3902                                            build_real (TREE_TYPE (@0), c2alt));
3903                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
3904                        ncmp = ERROR_MARK;
3905                      else if (real_equal (&TREE_REAL_CST (c3),
3906                                           &TREE_REAL_CST (@1)))
3907                        c2 = c2alt;
3908                    }
3909                }
3910            }
3911        }
3912        (if (cmp == GT_EXPR || cmp == GE_EXPR)
3913         (if (REAL_VALUE_ISINF (c2))
3914          /* sqrt(x) > y is x == +Inf, when y is very large.  */
3915          (if (HONOR_INFINITIES (@0))
3916           (eq @0 { build_real (TREE_TYPE (@0), c2); })
3917           { constant_boolean_node (false, type); })
3918          /* sqrt(x) > c is the same as x > c*c.  */
3919          (if (ncmp != ERROR_MARK)
3920           (if (ncmp == GE_EXPR)
3921            (ge @0 { build_real (TREE_TYPE (@0), c2); })
3922            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
3923         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
3924         (if (REAL_VALUE_ISINF (c2))
3925          (switch
3926           /* sqrt(x) < y is always true, when y is a very large
3927              value and we don't care about NaNs or Infinities.  */
3928           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3929            { constant_boolean_node (true, type); })
3930           /* sqrt(x) < y is x != +Inf when y is very large and we
3931              don't care about NaNs.  */
3932           (if (! HONOR_NANS (@0))
3933            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3934           /* sqrt(x) < y is x >= 0 when y is very large and we
3935              don't care about Infinities.  */
3936           (if (! HONOR_INFINITIES (@0))
3937            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3938           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
3939           (if (GENERIC)
3940            (truth_andif
3941             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3942             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3943          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
3944          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
3945           (if (ncmp == LT_EXPR)
3946            (lt @0 { build_real (TREE_TYPE (@0), c2); })
3947            (le @0 { build_real (TREE_TYPE (@0), c2); }))
3948           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
3949           (if (ncmp != ERROR_MARK && GENERIC)
3950            (if (ncmp == LT_EXPR)
3951             (truth_andif
3952              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3953              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
3954             (truth_andif
3955              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3956              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
3957    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
3958    (simplify
3959     (cmp (sq @0) (sq @1))
3960       (if (! HONOR_NANS (@0))
3961         (cmp @0 @1))))))
3963 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
3964 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3965      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
3966  (simplify
3967   (cmp (float@0 @1) (float @2))
3968    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3969         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3970     (with
3971      {
3972        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3973        tree type1 = TREE_TYPE (@1);
3974        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3975        tree type2 = TREE_TYPE (@2);
3976        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3977      }
3978      (if (fmt.can_represent_integral_type_p (type1)
3979           && fmt.can_represent_integral_type_p (type2))
3980       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3981        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3982        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3983             && type1_signed_p >= type2_signed_p)
3984         (icmp @1 (convert @2))
3985         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3986              && type1_signed_p <= type2_signed_p)
3987          (icmp (convert:type2 @1) @2)
3988          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3989               && type1_signed_p == type2_signed_p)
3990           (icmp @1 @2))))))))))
3992 /* Optimize various special cases of (FTYPE) N CMP CST.  */
3993 (for cmp  (lt le eq ne ge gt)
3994      icmp (le le eq ne ge ge)
3995  (simplify
3996   (cmp (float @0) REAL_CST@1)
3997    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3998         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3999     (with
4000      {
4001        tree itype = TREE_TYPE (@0);
4002        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4003        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4004        /* Be careful to preserve any potential exceptions due to
4005           NaNs.  qNaNs are ok in == or != context.
4006           TODO: relax under -fno-trapping-math or
4007           -fno-signaling-nans.  */
4008        bool exception_p
4009          = real_isnan (cst) && (cst->signalling
4010                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4011      }
4012      /* TODO: allow non-fitting itype and SNaNs when
4013         -fno-trapping-math.  */
4014      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4015       (with
4016        {
4017          signop isign = TYPE_SIGN (itype);
4018          REAL_VALUE_TYPE imin, imax;
4019          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4020          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4022          REAL_VALUE_TYPE icst;
4023          if (cmp == GT_EXPR || cmp == GE_EXPR)
4024            real_ceil (&icst, fmt, cst);
4025          else if (cmp == LT_EXPR || cmp == LE_EXPR)
4026            real_floor (&icst, fmt, cst);
4027          else
4028            real_trunc (&icst, fmt, cst);
4030          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4032          bool overflow_p = false;
4033          wide_int icst_val
4034            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4035        }
4036        (switch
4037         /* Optimize cases when CST is outside of ITYPE's range.  */
4038         (if (real_compare (LT_EXPR, cst, &imin))
4039          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4040                                   type); })
4041         (if (real_compare (GT_EXPR, cst, &imax))
4042          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4043                                   type); })
4044         /* Remove cast if CST is an integer representable by ITYPE.  */
4045         (if (cst_int_p)
4046          (cmp @0 { gcc_assert (!overflow_p);
4047                    wide_int_to_tree (itype, icst_val); })
4048         )
4049         /* When CST is fractional, optimize
4050             (FTYPE) N == CST -> 0
4051             (FTYPE) N != CST -> 1.  */
4052         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4053          { constant_boolean_node (cmp == NE_EXPR, type); })
4054         /* Otherwise replace with sensible integer constant.  */
4055         (with
4056          {
4057            gcc_checking_assert (!overflow_p);
4058          }
4059          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4061 /* Fold A /[ex] B CMP C to A CMP B * C.  */
4062 (for cmp (eq ne)
4063  (simplify
4064   (cmp (exact_div @0 @1) INTEGER_CST@2)
4065   (if (!integer_zerop (@1))
4066    (if (wi::to_wide (@2) == 0)
4067     (cmp @0 @2)
4068     (if (TREE_CODE (@1) == INTEGER_CST)
4069      (with
4070       {
4071         wi::overflow_type ovf;
4072         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4073                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4074       }
4075       (if (ovf)
4076        { constant_boolean_node (cmp == NE_EXPR, type); }
4077        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4078 (for cmp (lt le gt ge)
4079  (simplify
4080   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4081   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4082    (with
4083     {
4084       wi::overflow_type ovf;
4085       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4086                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4087     }
4088     (if (ovf)
4089      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4090                                         TYPE_SIGN (TREE_TYPE (@2)))
4091                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4092      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4094 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4096    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4097    For large C (more than min/B+2^size), this is also true, with the
4098    multiplication computed modulo 2^size.
4099    For intermediate C, this just tests the sign of A.  */
4100 (for cmp  (lt le gt ge)
4101      cmp2 (ge ge lt lt)
4102  (simplify
4103   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4104   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4105        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4106        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4107    (with
4108     {
4109       tree utype = TREE_TYPE (@2);
4110       wide_int denom = wi::to_wide (@1);
4111       wide_int right = wi::to_wide (@2);
4112       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4113       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4114       bool small = wi::leu_p (right, smax);
4115       bool large = wi::geu_p (right, smin);
4116     }
4117     (if (small || large)
4118      (cmp (convert:utype @0) (mult @2 (convert @1)))
4119      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4121 /* Unordered tests if either argument is a NaN.  */
4122 (simplify
4123  (bit_ior (unordered @0 @0) (unordered @1 @1))
4124  (if (types_match (@0, @1))
4125   (unordered @0 @1)))
4126 (simplify
4127  (bit_and (ordered @0 @0) (ordered @1 @1))
4128  (if (types_match (@0, @1))
4129   (ordered @0 @1)))
4130 (simplify
4131  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4132  @2)
4133 (simplify
4134  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4135  @2)
4137 /* Simple range test simplifications.  */
4138 /* A < B || A >= B -> true.  */
4139 (for test1 (lt le le le ne ge)
4140      test2 (ge gt ge ne eq ne)
4141  (simplify
4142   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4143   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4144        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4145    { constant_boolean_node (true, type); })))
4146 /* A < B && A >= B -> false.  */
4147 (for test1 (lt lt lt le ne eq)
4148      test2 (ge gt eq gt eq gt)
4149  (simplify
4150   (bit_and:c (test1 @0 @1) (test2 @0 @1))
4151   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4152        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4153    { constant_boolean_node (false, type); })))
4155 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4156    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
4158    Note that comparisons
4159      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
4160      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
4161    will be canonicalized to above so there's no need to
4162    consider them here.
4163  */
4165 (for cmp (le gt)
4166      eqcmp (eq ne)
4167  (simplify
4168   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4169   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4170    (with
4171     {
4172      tree ty = TREE_TYPE (@0);
4173      unsigned prec = TYPE_PRECISION (ty);
4174      wide_int mask = wi::to_wide (@2, prec);
4175      wide_int rhs = wi::to_wide (@3, prec);
4176      signop sgn = TYPE_SIGN (ty);
4177     }
4178     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4179          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4180       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4181              { build_zero_cst (ty); }))))))
4183 /* -A CMP -B -> B CMP A.  */
4184 (for cmp (tcc_comparison)
4185      scmp (swapped_tcc_comparison)
4186  (simplify
4187   (cmp (negate @0) (negate @1))
4188   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4189        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4190            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4191    (scmp @0 @1)))
4192  (simplify
4193   (cmp (negate @0) CONSTANT_CLASS_P@1)
4194   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4195        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4196            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4197    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4198     (if (tem && !TREE_OVERFLOW (tem))
4199      (scmp @0 { tem; }))))))
4201 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
4202 (for op (eq ne)
4203  (simplify
4204   (op (abs @0) zerop@1)
4205   (op @0 @1)))
4207 /* From fold_sign_changed_comparison and fold_widened_comparison.
4208    FIXME: the lack of symmetry is disturbing.  */
4209 (for cmp (simple_comparison)
4210  (simplify
4211   (cmp (convert@0 @00) (convert?@1 @10))
4212   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4213        /* Disable this optimization if we're casting a function pointer
4214           type on targets that require function pointer canonicalization.  */
4215        && !(targetm.have_canonicalize_funcptr_for_compare ()
4216             && ((POINTER_TYPE_P (TREE_TYPE (@00))
4217                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4218                 || (POINTER_TYPE_P (TREE_TYPE (@10))
4219                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4220        && single_use (@0))
4221    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4222         && (TREE_CODE (@10) == INTEGER_CST
4223             || @1 != @10)
4224         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4225             || cmp == NE_EXPR
4226             || cmp == EQ_EXPR)
4227         && !POINTER_TYPE_P (TREE_TYPE (@00)))
4228     /* ???  The special-casing of INTEGER_CST conversion was in the original
4229        code and here to avoid a spurious overflow flag on the resulting
4230        constant which fold_convert produces.  */
4231     (if (TREE_CODE (@1) == INTEGER_CST)
4232      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4233                                 TREE_OVERFLOW (@1)); })
4234      (cmp @00 (convert @1)))
4236     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4237      /* If possible, express the comparison in the shorter mode.  */
4238      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4239            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4240            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4241                && TYPE_UNSIGNED (TREE_TYPE (@00))))
4242           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4243               || ((TYPE_PRECISION (TREE_TYPE (@00))
4244                    >= TYPE_PRECISION (TREE_TYPE (@10)))
4245                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
4246                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
4247               || (TREE_CODE (@10) == INTEGER_CST
4248                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4249                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
4250       (cmp @00 (convert @10))
4251       (if (TREE_CODE (@10) == INTEGER_CST
4252            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4253            && !int_fits_type_p (@10, TREE_TYPE (@00)))
4254        (with
4255         {
4256           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4257           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4258           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4259           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4260         }
4261         (if (above || below)
4262          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4263           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4264           (if (cmp == LT_EXPR || cmp == LE_EXPR)
4265            { constant_boolean_node (above ? true : false, type); }
4266            (if (cmp == GT_EXPR || cmp == GE_EXPR)
4267             { constant_boolean_node (above ? false : true, type); }))))))))))))
4269 (for cmp (eq ne)
4270  (simplify
4271   /* SSA names are canonicalized to 2nd place.  */
4272   (cmp addr@0 SSA_NAME@1)
4273   (with
4274    { poly_int64 off; tree base; }
4275    /* A local variable can never be pointed to by
4276       the default SSA name of an incoming parameter.  */
4277    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4278         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4279         && (base = get_base_address (TREE_OPERAND (@0, 0)))
4280         && TREE_CODE (base) == VAR_DECL
4281         && auto_var_in_fn_p (base, current_function_decl))
4282     (if (cmp == NE_EXPR)
4283      { constant_boolean_node (true, type); }
4284      { constant_boolean_node (false, type); })
4285     /* If the address is based on @1 decide using the offset.  */
4286     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4287          && TREE_CODE (base) == MEM_REF
4288          && TREE_OPERAND (base, 0) == @1)
4289      (with { off += mem_ref_offset (base).force_shwi (); }
4290       (if (known_ne (off, 0))
4291        { constant_boolean_node (cmp == NE_EXPR, type); }
4292        (if (known_eq (off, 0))
4293         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4295 /* Equality compare simplifications from fold_binary  */
4296 (for cmp (eq ne)
4298  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4299     Similarly for NE_EXPR.  */
4300  (simplify
4301   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4302   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4303        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4304    { constant_boolean_node (cmp == NE_EXPR, type); }))
4306  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
4307  (simplify
4308   (cmp (bit_xor @0 @1) integer_zerop)
4309   (cmp @0 @1))
4311  /* (X ^ Y) == Y becomes X == 0.
4312     Likewise (X ^ Y) == X becomes Y == 0.  */
4313  (simplify
4314   (cmp:c (bit_xor:c @0 @1) @0)
4315   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4317  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
4318  (simplify
4319   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4320   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4321    (cmp @0 (bit_xor @1 (convert @2)))))
4323  (simplify
4324   (cmp (convert? addr@0) integer_zerop)
4325   (if (tree_single_nonzero_warnv_p (@0, NULL))
4326    { constant_boolean_node (cmp == NE_EXPR, type); })))
4328 /* If we have (A & C) == C where C is a power of 2, convert this into
4329    (A & C) != 0.  Similarly for NE_EXPR.  */
4330 (for cmp (eq ne)
4331      icmp (ne eq)
4332  (simplify
4333   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4334   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4336 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4337    convert this into a shift followed by ANDing with D.  */
4338 (simplify
4339  (cond
4340   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4341   INTEGER_CST@2 integer_zerop)
4342  (if (integer_pow2p (@2))
4343   (with {
4344      int shift = (wi::exact_log2 (wi::to_wide (@2))
4345                   - wi::exact_log2 (wi::to_wide (@1)));
4346    }
4347    (if (shift > 0)
4348     (bit_and
4349      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4350     (bit_and
4351      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4352      @2)))))
4354 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4355    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
4356 (for cmp (eq ne)
4357      ncmp (ge lt)
4358  (simplify
4359   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4360   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4361        && type_has_mode_precision_p (TREE_TYPE (@0))
4362        && element_precision (@2) >= element_precision (@0)
4363        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4364    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4365     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4367 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4368    this into a right shift or sign extension followed by ANDing with C.  */
4369 (simplify
4370  (cond
4371   (lt @0 integer_zerop)
4372   INTEGER_CST@1 integer_zerop)
4373  (if (integer_pow2p (@1)
4374       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4375   (with {
4376     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4377    }
4378    (if (shift >= 0)
4379     (bit_and
4380      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4381      @1)
4382     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4383        sign extension followed by AND with C will achieve the effect.  */
4384     (bit_and (convert @0) @1)))))
4386 /* When the addresses are not directly of decls compare base and offset.
4387    This implements some remaining parts of fold_comparison address
4388    comparisons but still no complete part of it.  Still it is good
4389    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
4390 (for cmp (simple_comparison)
4391  (simplify
4392   (cmp (convert1?@2 addr@0) (convert2? addr@1))
4393   (with
4394    {
4395      poly_int64 off0, off1;
4396      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4397      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4398      if (base0 && TREE_CODE (base0) == MEM_REF)
4399        {
4400          off0 += mem_ref_offset (base0).force_shwi ();
4401          base0 = TREE_OPERAND (base0, 0);
4402        }
4403      if (base1 && TREE_CODE (base1) == MEM_REF)
4404        {
4405          off1 += mem_ref_offset (base1).force_shwi ();
4406          base1 = TREE_OPERAND (base1, 0);
4407        }
4408    }
4409    (if (base0 && base1)
4410     (with
4411      {
4412        int equal = 2;
4413        /* Punt in GENERIC on variables with value expressions;
4414           the value expressions might point to fields/elements
4415           of other vars etc.  */
4416        if (GENERIC
4417            && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4418                || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4419          ;
4420        else if (decl_in_symtab_p (base0)
4421                 && decl_in_symtab_p (base1))
4422          equal = symtab_node::get_create (base0)
4423                    ->equal_address_to (symtab_node::get_create (base1));
4424        else if ((DECL_P (base0)
4425                  || TREE_CODE (base0) == SSA_NAME
4426                  || TREE_CODE (base0) == STRING_CST)
4427                 && (DECL_P (base1)
4428                     || TREE_CODE (base1) == SSA_NAME
4429                     || TREE_CODE (base1) == STRING_CST))
4430          equal = (base0 == base1);
4431        if (equal == 0)
4432          {
4433            HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4434            off0.is_constant (&ioff0);
4435            off1.is_constant (&ioff1);
4436            if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4437                || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4438                || (TREE_CODE (base0) == STRING_CST
4439                    && TREE_CODE (base1) == STRING_CST
4440                    && ioff0 >= 0 && ioff1 >= 0
4441                    && ioff0 < TREE_STRING_LENGTH (base0)
4442                    && ioff1 < TREE_STRING_LENGTH (base1)
4443                    /* This is a too conservative test that the STRING_CSTs
4444                       will not end up being string-merged.  */
4445                    && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4446                                TREE_STRING_POINTER (base1) + ioff1,
4447                                MIN (TREE_STRING_LENGTH (base0) - ioff0,
4448                                     TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4449              ;
4450            else if (!DECL_P (base0) || !DECL_P (base1))
4451              equal = 2;
4452            else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4453              equal = 2;
4454            /* If this is a pointer comparison, ignore for now even
4455               valid equalities where one pointer is the offset zero
4456               of one object and the other to one past end of another one.  */
4457            else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4458              ;
4459            /* Assume that automatic variables can't be adjacent to global
4460               variables.  */
4461            else if (is_global_var (base0) != is_global_var (base1))
4462              ;
4463            else
4464              {
4465                tree sz0 = DECL_SIZE_UNIT (base0);
4466                tree sz1 = DECL_SIZE_UNIT (base1);
4467                /* If sizes are unknown, e.g. VLA or not representable,
4468                   punt.  */
4469                if (!tree_fits_poly_int64_p (sz0)
4470                    || !tree_fits_poly_int64_p (sz1))
4471                  equal = 2;
4472                else
4473                  {
4474                    poly_int64 size0 = tree_to_poly_int64 (sz0);
4475                    poly_int64 size1 = tree_to_poly_int64 (sz1);
4476                    /* If one offset is pointing (or could be) to the beginning
4477                       of one object and the other is pointing to one past the
4478                       last byte of the other object, punt.  */
4479                    if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4480                      equal = 2;
4481                    else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4482                      equal = 2;
4483                    /* If both offsets are the same, there are some cases
4484                       we know that are ok.  Either if we know they aren't
4485                       zero, or if we know both sizes are no zero.  */
4486                    if (equal == 2
4487                        && known_eq (off0, off1)
4488                        && (known_ne (off0, 0)
4489                            || (known_ne (size0, 0) && known_ne (size1, 0))))
4490                      equal = 0;
4491                  }
4492              }
4493          }
4494      }
4495      (if (equal == 1
4496           && (cmp == EQ_EXPR || cmp == NE_EXPR
4497               /* If the offsets are equal we can ignore overflow.  */
4498               || known_eq (off0, off1)
4499               || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4500                  /* Or if we compare using pointers to decls or strings.  */
4501               || (POINTER_TYPE_P (TREE_TYPE (@2))
4502                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4503       (switch
4504        (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4505         { constant_boolean_node (known_eq (off0, off1), type); })
4506        (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4507         { constant_boolean_node (known_ne (off0, off1), type); })
4508        (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4509         { constant_boolean_node (known_lt (off0, off1), type); })
4510        (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4511         { constant_boolean_node (known_le (off0, off1), type); })
4512        (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4513         { constant_boolean_node (known_ge (off0, off1), type); })
4514        (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4515         { constant_boolean_node (known_gt (off0, off1), type); }))
4516       (if (equal == 0)
4517         (switch
4518          (if (cmp == EQ_EXPR)
4519           { constant_boolean_node (false, type); })
4520          (if (cmp == NE_EXPR)
4521           { constant_boolean_node (true, type); })))))))))
4523 /* Simplify pointer equality compares using PTA.  */
4524 (for neeq (ne eq)
4525  (simplify
4526   (neeq @0 @1)
4527   (if (POINTER_TYPE_P (TREE_TYPE (@0))
4528        && ptrs_compare_unequal (@0, @1))
4529    { constant_boolean_node (neeq != EQ_EXPR, type); })))
4531 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4532    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4533    Disable the transform if either operand is pointer to function.
4534    This broke pr22051-2.c for arm where function pointer
4535    canonicalizaion is not wanted.  */
4537 (for cmp (ne eq)
4538  (simplify
4539   (cmp (convert @0) INTEGER_CST@1)
4540   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4541          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4542          && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4543         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4544             && POINTER_TYPE_P (TREE_TYPE (@1))
4545             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4546        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4547    (cmp @0 (convert @1)))))
4549 /* Non-equality compare simplifications from fold_binary  */
4550 (for cmp (lt gt le ge)
4551  /* Comparisons with the highest or lowest possible integer of
4552     the specified precision will have known values.  */
4553  (simplify
4554   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4555   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4556         || POINTER_TYPE_P (TREE_TYPE (@1))
4557         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4558        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4559    (with
4560     {
4561       tree cst = uniform_integer_cst_p (@1);
4562       tree arg1_type = TREE_TYPE (cst);
4563       unsigned int prec = TYPE_PRECISION (arg1_type);
4564       wide_int max = wi::max_value (arg1_type);
4565       wide_int signed_max = wi::max_value (prec, SIGNED);
4566       wide_int min = wi::min_value (arg1_type);
4567     }
4568     (switch
4569      (if (wi::to_wide (cst) == max)
4570       (switch
4571        (if (cmp == GT_EXPR)
4572         { constant_boolean_node (false, type); })
4573        (if (cmp == GE_EXPR)
4574         (eq @2 @1))
4575        (if (cmp == LE_EXPR)
4576         { constant_boolean_node (true, type); })
4577        (if (cmp == LT_EXPR)
4578         (ne @2 @1))))
4579      (if (wi::to_wide (cst) == min)
4580       (switch
4581        (if (cmp == LT_EXPR)
4582         { constant_boolean_node (false, type); })
4583        (if (cmp == LE_EXPR)
4584         (eq @2 @1))
4585        (if (cmp == GE_EXPR)
4586         { constant_boolean_node (true, type); })
4587        (if (cmp == GT_EXPR)
4588         (ne @2 @1))))
4589      (if (wi::to_wide (cst) == max - 1)
4590       (switch
4591        (if (cmp == GT_EXPR)
4592         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4593                                     wide_int_to_tree (TREE_TYPE (cst),
4594                                                       wi::to_wide (cst)
4595                                                       + 1)); }))
4596        (if (cmp == LE_EXPR)
4597         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4598                                     wide_int_to_tree (TREE_TYPE (cst),
4599                                                       wi::to_wide (cst)
4600                                                       + 1)); }))))
4601      (if (wi::to_wide (cst) == min + 1)
4602       (switch
4603        (if (cmp == GE_EXPR)
4604         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4605                                     wide_int_to_tree (TREE_TYPE (cst),
4606                                                       wi::to_wide (cst)
4607                                                       - 1)); }))
4608        (if (cmp == LT_EXPR)
4609         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4610                                     wide_int_to_tree (TREE_TYPE (cst),
4611                                                       wi::to_wide (cst)
4612                                                       - 1)); }))))
4613      (if (wi::to_wide (cst) == signed_max
4614           && TYPE_UNSIGNED (arg1_type)
4615           /* We will flip the signedness of the comparison operator
4616              associated with the mode of @1, so the sign bit is
4617              specified by this mode.  Check that @1 is the signed
4618              max associated with this sign bit.  */
4619           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4620           /* signed_type does not work on pointer types.  */
4621           && INTEGRAL_TYPE_P (arg1_type))
4622       /* The following case also applies to X < signed_max+1
4623          and X >= signed_max+1 because previous transformations.  */
4624       (if (cmp == LE_EXPR || cmp == GT_EXPR)
4625        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4626         (switch
4627          (if (cst == @1 && cmp == LE_EXPR)
4628           (ge (convert:st @0) { build_zero_cst (st); }))
4629          (if (cst == @1 && cmp == GT_EXPR)
4630           (lt (convert:st @0) { build_zero_cst (st); }))
4631          (if (cmp == LE_EXPR)
4632           (ge (view_convert:st @0) { build_zero_cst (st); }))
4633          (if (cmp == GT_EXPR)
4634           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4636 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4637  /* If the second operand is NaN, the result is constant.  */
4638  (simplify
4639   (cmp @0 REAL_CST@1)
4640   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4641        && (cmp != LTGT_EXPR || ! flag_trapping_math))
4642    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4643                             ? false : true, type); })))
4645 /* bool_var != 0 becomes bool_var.  */
4646 (simplify
4647  (ne @0 integer_zerop)
4648  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4649       && types_match (type, TREE_TYPE (@0)))
4650   (non_lvalue @0)))
4651 /* bool_var == 1 becomes bool_var.  */
4652 (simplify
4653  (eq @0 integer_onep)
4654  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4655       && types_match (type, TREE_TYPE (@0)))
4656   (non_lvalue @0)))
4657 /* Do not handle
4658    bool_var == 0 becomes !bool_var or
4659    bool_var != 1 becomes !bool_var
4660    here because that only is good in assignment context as long
4661    as we require a tcc_comparison in GIMPLE_CONDs where we'd
4662    replace if (x == 0) with tem = ~x; if (tem != 0) which is
4663    clearly less optimal and which we'll transform again in forwprop.  */
4665 /* When one argument is a constant, overflow detection can be simplified.
4666    Currently restricted to single use so as not to interfere too much with
4667    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4668    A + CST CMP A  ->  A CMP' CST' */
4669 (for cmp (lt le ge gt)
4670      out (gt gt le le)
4671  (simplify
4672   (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4673   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4674        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4675        && wi::to_wide (@1) != 0
4676        && single_use (@2))
4677    (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4678     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4679                                 wi::max_value (prec, UNSIGNED)
4680                                 - wi::to_wide (@1)); })))))
4682 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4683    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4684    expects the long form, so we restrict the transformation for now.  */
4685 (for cmp (gt le)
4686  (simplify
4687   (cmp:c (minus@2 @0 @1) @0)
4688   (if (single_use (@2)
4689        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4690        && TYPE_UNSIGNED (TREE_TYPE (@0))
4691        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4692    (cmp @1 @0))))
4694 /* Testing for overflow is unnecessary if we already know the result.  */
4695 /* A - B > A  */
4696 (for cmp (gt le)
4697      out (ne eq)
4698  (simplify
4699   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4700   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4701        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4702    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4703 /* A + B < A  */
4704 (for cmp (lt ge)
4705      out (ne eq)
4706  (simplify
4707   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4708   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4709        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4710    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4712 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4713    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
4714 (for cmp (lt ge)
4715      out (ne eq)
4716  (simplify
4717   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4718   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4719    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4720     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4722 /* Simplification of math builtins.  These rules must all be optimizations
4723    as well as IL simplifications.  If there is a possibility that the new
4724    form could be a pessimization, the rule should go in the canonicalization
4725    section that follows this one.
4727    Rules can generally go in this section if they satisfy one of
4728    the following:
4730    - the rule describes an identity
4732    - the rule replaces calls with something as simple as addition or
4733      multiplication
4735    - the rule contains unary calls only and simplifies the surrounding
4736      arithmetic.  (The idea here is to exclude non-unary calls in which
4737      one operand is constant and in which the call is known to be cheap
4738      when the operand has that value.)  */
4740 (if (flag_unsafe_math_optimizations)
4741  /* Simplify sqrt(x) * sqrt(x) -> x.  */
4742  (simplify
4743   (mult (SQRT_ALL@1 @0) @1)
4744   (if (!HONOR_SNANS (type))
4745    @0))
4747  (for op (plus minus)
4748   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
4749   (simplify
4750    (op (rdiv @0 @1)
4751        (rdiv @2 @1))
4752    (rdiv (op @0 @2) @1)))
4754  (for cmp (lt le gt ge)
4755       neg_cmp (gt ge lt le)
4756   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
4757   (simplify
4758    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4759    (with
4760     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4761     (if (tem
4762          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4763               || (real_zerop (tem) && !real_zerop (@1))))
4764      (switch
4765       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4766        (cmp @0 { tem; }))
4767       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4768        (neg_cmp @0 { tem; })))))))
4770  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
4771  (for root (SQRT CBRT)
4772   (simplify
4773    (mult (root:s @0) (root:s @1))
4774     (root (mult @0 @1))))
4776  /* Simplify expN(x) * expN(y) -> expN(x+y). */
4777  (for exps (EXP EXP2 EXP10 POW10)
4778   (simplify
4779    (mult (exps:s @0) (exps:s @1))
4780     (exps (plus @0 @1))))
4782  /* Simplify a/root(b/c) into a*root(c/b).  */
4783  (for root (SQRT CBRT)
4784   (simplify
4785    (rdiv @0 (root:s (rdiv:s @1 @2)))
4786     (mult @0 (root (rdiv @2 @1)))))
4788  /* Simplify x/expN(y) into x*expN(-y).  */
4789  (for exps (EXP EXP2 EXP10 POW10)
4790   (simplify
4791    (rdiv @0 (exps:s @1))
4792     (mult @0 (exps (negate @1)))))
4794  (for logs (LOG LOG2 LOG10 LOG10)
4795       exps (EXP EXP2 EXP10 POW10)
4796   /* logN(expN(x)) -> x.  */
4797   (simplify
4798    (logs (exps @0))
4799    @0)
4800   /* expN(logN(x)) -> x.  */
4801   (simplify
4802    (exps (logs @0))
4803    @0))
4805  /* Optimize logN(func()) for various exponential functions.  We
4806     want to determine the value "x" and the power "exponent" in
4807     order to transform logN(x**exponent) into exponent*logN(x).  */
4808  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
4809       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
4810   (simplify
4811    (logs (exps @0))
4812    (if (SCALAR_FLOAT_TYPE_P (type))
4813     (with {
4814       tree x;
4815       switch (exps)
4816         {
4817         CASE_CFN_EXP:
4818           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
4819           x = build_real_truncate (type, dconst_e ());
4820           break;
4821         CASE_CFN_EXP2:
4822           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
4823           x = build_real (type, dconst2);
4824           break;
4825         CASE_CFN_EXP10:
4826         CASE_CFN_POW10:
4827           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
4828           {
4829             REAL_VALUE_TYPE dconst10;
4830             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4831             x = build_real (type, dconst10);
4832           }
4833           break;
4834         default:
4835           gcc_unreachable ();
4836         }
4837       }
4838      (mult (logs { x; }) @0)))))
4840  (for logs (LOG LOG
4841             LOG2 LOG2
4842             LOG10 LOG10)
4843       exps (SQRT CBRT)
4844   (simplify
4845    (logs (exps @0))
4846    (if (SCALAR_FLOAT_TYPE_P (type))
4847     (with {
4848       tree x;
4849       switch (exps)
4850         {
4851         CASE_CFN_SQRT:
4852           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
4853           x = build_real (type, dconsthalf);
4854           break;
4855         CASE_CFN_CBRT:
4856           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
4857           x = build_real_truncate (type, dconst_third ());
4858           break;
4859         default:
4860           gcc_unreachable ();
4861         }
4862       }
4863      (mult { x; } (logs @0))))))
4865  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
4866  (for logs (LOG LOG2 LOG10)
4867       pows (POW)
4868   (simplify
4869    (logs (pows @0 @1))
4870    (mult @1 (logs @0))))
4872  /* pow(C,x) -> exp(log(C)*x) if C > 0,
4873     or if C is a positive power of 2,
4874     pow(C,x) -> exp2(log2(C)*x).  */
4875 #if GIMPLE
4876  (for pows (POW)
4877       exps (EXP)
4878       logs (LOG)
4879       exp2s (EXP2)
4880       log2s (LOG2)
4881   (simplify
4882    (pows REAL_CST@0 @1)
4883    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4884         && real_isfinite (TREE_REAL_CST_PTR (@0))
4885         /* As libmvec doesn't have a vectorized exp2, defer optimizing
4886            the use_exp2 case until after vectorization.  It seems actually
4887            beneficial for all constants to postpone this until later,
4888            because exp(log(C)*x), while faster, will have worse precision
4889            and if x folds into a constant too, that is unnecessary
4890            pessimization.  */
4891         && canonicalize_math_after_vectorization_p ())
4892     (with {
4893        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4894        bool use_exp2 = false;
4895        if (targetm.libc_has_function (function_c99_misc)
4896            && value->cl == rvc_normal)
4897          {
4898            REAL_VALUE_TYPE frac_rvt = *value;
4899            SET_REAL_EXP (&frac_rvt, 1);
4900            if (real_equal (&frac_rvt, &dconst1))
4901              use_exp2 = true;
4902          }
4903      }
4904      (if (!use_exp2)
4905       (if (optimize_pow_to_exp (@0, @1))
4906        (exps (mult (logs @0) @1)))
4907       (exp2s (mult (log2s @0) @1)))))))
4908 #endif
4910  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
4911  (for pows (POW)
4912       exps (EXP EXP2 EXP10 POW10)
4913       logs (LOG LOG2 LOG10 LOG10)
4914   (simplify
4915    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4916    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4917         && real_isfinite (TREE_REAL_CST_PTR (@0)))
4918     (exps (plus (mult (logs @0) @1) @2)))))
4920  (for sqrts (SQRT)
4921       cbrts (CBRT)
4922       pows (POW)
4923       exps (EXP EXP2 EXP10 POW10)
4924   /* sqrt(expN(x)) -> expN(x*0.5).  */
4925   (simplify
4926    (sqrts (exps @0))
4927    (exps (mult @0 { build_real (type, dconsthalf); })))
4928   /* cbrt(expN(x)) -> expN(x/3).  */
4929   (simplify
4930    (cbrts (exps @0))
4931    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4932   /* pow(expN(x), y) -> expN(x*y).  */
4933   (simplify
4934    (pows (exps @0) @1)
4935    (exps (mult @0 @1))))
4937  /* tan(atan(x)) -> x.  */
4938  (for tans (TAN)
4939       atans (ATAN)
4940   (simplify
4941    (tans (atans @0))
4942    @0)))
4944  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4945  (for sins (SIN)
4946       atans (ATAN)
4947       sqrts (SQRT)
4948       copysigns (COPYSIGN)
4949   (simplify
4950    (sins (atans:s @0))
4951    (with
4952      {
4953       REAL_VALUE_TYPE r_cst;
4954       build_sinatan_real (&r_cst, type);
4955       tree t_cst = build_real (type, r_cst);
4956       tree t_one = build_one_cst (type);
4957      }
4958     (if (SCALAR_FLOAT_TYPE_P (type))
4959      (cond (lt (abs @0) { t_cst; })
4960       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4961       (copysigns { t_one; } @0))))))
4963 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4964  (for coss (COS)
4965       atans (ATAN)
4966       sqrts (SQRT)
4967       copysigns (COPYSIGN)
4968   (simplify
4969    (coss (atans:s @0))
4970    (with
4971      {
4972       REAL_VALUE_TYPE r_cst;
4973       build_sinatan_real (&r_cst, type);
4974       tree t_cst = build_real (type, r_cst);
4975       tree t_one = build_one_cst (type);
4976       tree t_zero = build_zero_cst (type);
4977      }
4978     (if (SCALAR_FLOAT_TYPE_P (type))
4979      (cond (lt (abs @0) { t_cst; })
4980       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4981       (copysigns { t_zero; } @0))))))
4983  (if (!flag_errno_math)
4984   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4985   (for sinhs (SINH)
4986        atanhs (ATANH)
4987        sqrts (SQRT)
4988    (simplify
4989     (sinhs (atanhs:s @0))
4990     (with { tree t_one = build_one_cst (type); }
4991     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4993   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4994   (for coshs (COSH)
4995        atanhs (ATANH)
4996        sqrts (SQRT)
4997    (simplify
4998     (coshs (atanhs:s @0))
4999     (with { tree t_one = build_one_cst (type); }
5000     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5002 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
5003 (simplify
5004  (CABS (complex:C @0 real_zerop@1))
5005  (abs @0))
5007 /* trunc(trunc(x)) -> trunc(x), etc.  */
5008 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5009  (simplify
5010   (fns (fns @0))
5011   (fns @0)))
5012 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
5013 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5014  (simplify
5015   (fns integer_valued_real_p@0)
5016   @0))
5018 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
5019 (simplify
5020  (HYPOT:c @0 real_zerop@1)
5021  (abs @0))
5023 /* pow(1,x) -> 1.  */
5024 (simplify
5025  (POW real_onep@0 @1)
5026  @0)
5028 (simplify
5029  /* copysign(x,x) -> x.  */
5030  (COPYSIGN_ALL @0 @0)
5031  @0)
5033 (simplify
5034  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
5035  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5036  (abs @0))
5038 (for scale (LDEXP SCALBN SCALBLN)
5039  /* ldexp(0, x) -> 0.  */
5040  (simplify
5041   (scale real_zerop@0 @1)
5042   @0)
5043  /* ldexp(x, 0) -> x.  */
5044  (simplify
5045   (scale @0 integer_zerop@1)
5046   @0)
5047  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
5048  (simplify
5049   (scale REAL_CST@0 @1)
5050   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5051    @0)))
5053 /* Canonicalization of sequences of math builtins.  These rules represent
5054    IL simplifications but are not necessarily optimizations.
5056    The sincos pass is responsible for picking "optimal" implementations
5057    of math builtins, which may be more complicated and can sometimes go
5058    the other way, e.g. converting pow into a sequence of sqrts.
5059    We only want to do these canonicalizations before the pass has run.  */
5061 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5062  /* Simplify tan(x) * cos(x) -> sin(x). */
5063  (simplify
5064   (mult:c (TAN:s @0) (COS:s @0))
5065    (SIN @0))
5067  /* Simplify x * pow(x,c) -> pow(x,c+1). */
5068  (simplify
5069   (mult:c @0 (POW:s @0 REAL_CST@1))
5070   (if (!TREE_OVERFLOW (@1))
5071    (POW @0 (plus @1 { build_one_cst (type); }))))
5073  /* Simplify sin(x) / cos(x) -> tan(x). */
5074  (simplify
5075   (rdiv (SIN:s @0) (COS:s @0))
5076    (TAN @0))
5078  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5079  (simplify
5080   (rdiv (SINH:s @0) (COSH:s @0))
5081    (TANH @0))
5083  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5084  (simplify
5085   (rdiv (COS:s @0) (SIN:s @0))
5086    (rdiv { build_one_cst (type); } (TAN @0)))
5088  /* Simplify sin(x) / tan(x) -> cos(x). */
5089  (simplify
5090   (rdiv (SIN:s @0) (TAN:s @0))
5091   (if (! HONOR_NANS (@0)
5092        && ! HONOR_INFINITIES (@0))
5093    (COS @0)))
5095  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5096  (simplify
5097   (rdiv (TAN:s @0) (SIN:s @0))
5098   (if (! HONOR_NANS (@0)
5099        && ! HONOR_INFINITIES (@0))
5100    (rdiv { build_one_cst (type); } (COS @0))))
5102  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5103  (simplify
5104   (mult (POW:s @0 @1) (POW:s @0 @2))
5105    (POW @0 (plus @1 @2)))
5107  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5108  (simplify
5109   (mult (POW:s @0 @1) (POW:s @2 @1))
5110    (POW (mult @0 @2) @1))
5112  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5113  (simplify
5114   (mult (POWI:s @0 @1) (POWI:s @2 @1))
5115    (POWI (mult @0 @2) @1))
5117  /* Simplify pow(x,c) / x -> pow(x,c-1). */
5118  (simplify
5119   (rdiv (POW:s @0 REAL_CST@1) @0)
5120   (if (!TREE_OVERFLOW (@1))
5121    (POW @0 (minus @1 { build_one_cst (type); }))))
5123  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5124  (simplify
5125   (rdiv @0 (POW:s @1 @2))
5126    (mult @0 (POW @1 (negate @2))))
5128  (for sqrts (SQRT)
5129       cbrts (CBRT)
5130       pows (POW)
5131   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
5132   (simplify
5133    (sqrts (sqrts @0))
5134    (pows @0 { build_real (type, dconst_quarter ()); }))
5135   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
5136   (simplify
5137    (sqrts (cbrts @0))
5138    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5139   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
5140   (simplify
5141    (cbrts (sqrts @0))
5142    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5143   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
5144   (simplify
5145    (cbrts (cbrts tree_expr_nonnegative_p@0))
5146    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5147   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
5148   (simplify
5149    (sqrts (pows @0 @1))
5150    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5151   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
5152   (simplify
5153    (cbrts (pows tree_expr_nonnegative_p@0 @1))
5154    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5155   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
5156   (simplify
5157    (pows (sqrts @0) @1)
5158    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5159   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
5160   (simplify
5161    (pows (cbrts tree_expr_nonnegative_p@0) @1)
5162    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5163   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
5164   (simplify
5165    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5166    (pows @0 (mult @1 @2))))
5168  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
5169  (simplify
5170   (CABS (complex @0 @0))
5171   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5173  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
5174  (simplify
5175   (HYPOT @0 @0)
5176   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5178  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
5179  (for cexps (CEXP)
5180       exps (EXP)
5181       cexpis (CEXPI)
5182   (simplify
5183    (cexps compositional_complex@0)
5184    (if (targetm.libc_has_function (function_c99_math_complex))
5185     (complex
5186      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5187      (mult @1 (imagpart @2)))))))
5189 (if (canonicalize_math_p ())
5190  /* floor(x) -> trunc(x) if x is nonnegative.  */
5191  (for floors (FLOOR_ALL)
5192       truncs (TRUNC_ALL)
5193   (simplify
5194    (floors tree_expr_nonnegative_p@0)
5195    (truncs @0))))
5197 (match double_value_p
5198  @0
5199  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5200 (for froms (BUILT_IN_TRUNCL
5201             BUILT_IN_FLOORL
5202             BUILT_IN_CEILL
5203             BUILT_IN_ROUNDL
5204             BUILT_IN_NEARBYINTL
5205             BUILT_IN_RINTL)
5206      tos (BUILT_IN_TRUNC
5207           BUILT_IN_FLOOR
5208           BUILT_IN_CEIL
5209           BUILT_IN_ROUND
5210           BUILT_IN_NEARBYINT
5211           BUILT_IN_RINT)
5212  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
5213  (if (optimize && canonicalize_math_p ())
5214   (simplify
5215    (froms (convert double_value_p@0))
5216    (convert (tos @0)))))
5218 (match float_value_p
5219  @0
5220  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5221 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5222             BUILT_IN_FLOORL BUILT_IN_FLOOR
5223             BUILT_IN_CEILL BUILT_IN_CEIL
5224             BUILT_IN_ROUNDL BUILT_IN_ROUND
5225             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5226             BUILT_IN_RINTL BUILT_IN_RINT)
5227      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5228           BUILT_IN_FLOORF BUILT_IN_FLOORF
5229           BUILT_IN_CEILF BUILT_IN_CEILF
5230           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5231           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5232           BUILT_IN_RINTF BUILT_IN_RINTF)
5233  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5234     if x is a float.  */
5235  (if (optimize && canonicalize_math_p ()
5236       && targetm.libc_has_function (function_c99_misc))
5237   (simplify
5238    (froms (convert float_value_p@0))
5239    (convert (tos @0)))))
5241 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5242      tos (XFLOOR XCEIL XROUND XRINT)
5243  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
5244  (if (optimize && canonicalize_math_p ())
5245   (simplify
5246    (froms (convert double_value_p@0))
5247    (tos @0))))
5249 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5250             XFLOOR XCEIL XROUND XRINT)
5251      tos (XFLOORF XCEILF XROUNDF XRINTF)
5252  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5253     if x is a float.  */
5254  (if (optimize && canonicalize_math_p ())
5255   (simplify
5256    (froms (convert float_value_p@0))
5257    (tos @0))))
5259 (if (canonicalize_math_p ())
5260  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
5261  (for floors (IFLOOR LFLOOR LLFLOOR)
5262   (simplify
5263    (floors tree_expr_nonnegative_p@0)
5264    (fix_trunc @0))))
5266 (if (canonicalize_math_p ())
5267  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
5268  (for fns (IFLOOR LFLOOR LLFLOOR
5269            ICEIL LCEIL LLCEIL
5270            IROUND LROUND LLROUND)
5271   (simplify
5272    (fns integer_valued_real_p@0)
5273    (fix_trunc @0)))
5274  (if (!flag_errno_math)
5275   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
5276   (for rints (IRINT LRINT LLRINT)
5277    (simplify
5278     (rints integer_valued_real_p@0)
5279     (fix_trunc @0)))))
5281 (if (canonicalize_math_p ())
5282  (for ifn (IFLOOR ICEIL IROUND IRINT)
5283       lfn (LFLOOR LCEIL LROUND LRINT)
5284       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5285   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5286      sizeof (int) == sizeof (long).  */
5287   (if (TYPE_PRECISION (integer_type_node)
5288        == TYPE_PRECISION (long_integer_type_node))
5289    (simplify
5290     (ifn @0)
5291     (lfn:long_integer_type_node @0)))
5292   /* Canonicalize llround (x) to lround (x) on LP64 targets where
5293      sizeof (long long) == sizeof (long).  */
5294   (if (TYPE_PRECISION (long_long_integer_type_node)
5295        == TYPE_PRECISION (long_integer_type_node))
5296    (simplify
5297     (llfn @0)
5298     (lfn:long_integer_type_node @0)))))
5300 /* cproj(x) -> x if we're ignoring infinities.  */
5301 (simplify
5302  (CPROJ @0)
5303  (if (!HONOR_INFINITIES (type))
5304    @0))
5306 /* If the real part is inf and the imag part is known to be
5307    nonnegative, return (inf + 0i).  */
5308 (simplify
5309  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5310  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5311   { build_complex_inf (type, false); }))
5313 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
5314 (simplify
5315  (CPROJ (complex @0 REAL_CST@1))
5316  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5317   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5319 (for pows (POW)
5320      sqrts (SQRT)
5321      cbrts (CBRT)
5322  (simplify
5323   (pows @0 REAL_CST@1)
5324   (with {
5325     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5326     REAL_VALUE_TYPE tmp;
5327    }
5328    (switch
5329     /* pow(x,0) -> 1.  */
5330     (if (real_equal (value, &dconst0))
5331      { build_real (type, dconst1); })
5332     /* pow(x,1) -> x.  */
5333     (if (real_equal (value, &dconst1))
5334      @0)
5335     /* pow(x,-1) -> 1/x.  */
5336     (if (real_equal (value, &dconstm1))
5337      (rdiv { build_real (type, dconst1); } @0))
5338     /* pow(x,0.5) -> sqrt(x).  */
5339     (if (flag_unsafe_math_optimizations
5340          && canonicalize_math_p ()
5341          && real_equal (value, &dconsthalf))
5342      (sqrts @0))
5343     /* pow(x,1/3) -> cbrt(x).  */
5344     (if (flag_unsafe_math_optimizations
5345          && canonicalize_math_p ()
5346          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5347              real_equal (value, &tmp)))
5348      (cbrts @0))))))
5350 /* powi(1,x) -> 1.  */
5351 (simplify
5352  (POWI real_onep@0 @1)
5353  @0)
5355 (simplify
5356  (POWI @0 INTEGER_CST@1)
5357  (switch
5358   /* powi(x,0) -> 1.  */
5359   (if (wi::to_wide (@1) == 0)
5360    { build_real (type, dconst1); })
5361   /* powi(x,1) -> x.  */
5362   (if (wi::to_wide (@1) == 1)
5363    @0)
5364   /* powi(x,-1) -> 1/x.  */
5365   (if (wi::to_wide (@1) == -1)
5366    (rdiv { build_real (type, dconst1); } @0))))
5368 /* Narrowing of arithmetic and logical operations.
5370    These are conceptually similar to the transformations performed for
5371    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
5372    term we want to move all that code out of the front-ends into here.  */
5374 /* Convert (outertype)((innertype0)a+(innertype1)b)
5375    into ((newtype)a+(newtype)b) where newtype
5376    is the widest mode from all of these.  */
5377 (for op (plus minus mult rdiv)
5378  (simplify
5379    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5380    /* If we have a narrowing conversion of an arithmetic operation where
5381       both operands are widening conversions from the same type as the outer
5382       narrowing conversion.  Then convert the innermost operands to a
5383       suitable unsigned type (to avoid introducing undefined behavior),
5384       perform the operation and convert the result to the desired type.  */
5385    (if (INTEGRAL_TYPE_P (type)
5386         && op != MULT_EXPR
5387         && op != RDIV_EXPR
5388         /* We check for type compatibility between @0 and @1 below,
5389            so there's no need to check that @2/@4 are integral types.  */
5390         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5391         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5392         /* The precision of the type of each operand must match the
5393            precision of the mode of each operand, similarly for the
5394            result.  */
5395         && type_has_mode_precision_p (TREE_TYPE (@1))
5396         && type_has_mode_precision_p (TREE_TYPE (@2))
5397         && type_has_mode_precision_p (type)
5398         /* The inner conversion must be a widening conversion.  */
5399         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5400         && types_match (@1, type)
5401         && (types_match (@1, @2)
5402             /* Or the second operand is const integer or converted const
5403                integer from valueize.  */
5404             || TREE_CODE (@2) == INTEGER_CST))
5405      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5406        (op @1 (convert @2))
5407        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5408         (convert (op (convert:utype @1)
5409                      (convert:utype @2)))))
5410      (if (FLOAT_TYPE_P (type)
5411           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5412                == DECIMAL_FLOAT_TYPE_P (type))
5413       (with { tree arg0 = strip_float_extensions (@1);
5414               tree arg1 = strip_float_extensions (@2);
5415               tree itype = TREE_TYPE (@0);
5416               tree ty1 = TREE_TYPE (arg0);
5417               tree ty2 = TREE_TYPE (arg1);
5418               enum tree_code code = TREE_CODE (itype); }
5419         (if (FLOAT_TYPE_P (ty1)
5420              && FLOAT_TYPE_P (ty2))
5421          (with { tree newtype = type;
5422                  if (TYPE_MODE (ty1) == SDmode
5423                      || TYPE_MODE (ty2) == SDmode
5424                      || TYPE_MODE (type) == SDmode)
5425                    newtype = dfloat32_type_node;
5426                  if (TYPE_MODE (ty1) == DDmode
5427                      || TYPE_MODE (ty2) == DDmode
5428                      || TYPE_MODE (type) == DDmode)
5429                    newtype = dfloat64_type_node;
5430                  if (TYPE_MODE (ty1) == TDmode
5431                      || TYPE_MODE (ty2) == TDmode
5432                      || TYPE_MODE (type) == TDmode)
5433                    newtype = dfloat128_type_node; }
5434           (if ((newtype == dfloat32_type_node
5435                 || newtype == dfloat64_type_node
5436                 || newtype == dfloat128_type_node)
5437               && newtype == type
5438               && types_match (newtype, type))
5439             (op (convert:newtype @1) (convert:newtype @2))
5440             (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5441                       newtype = ty1;
5442                     if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5443                       newtype = ty2; }
5444                /* Sometimes this transformation is safe (cannot
5445                   change results through affecting double rounding
5446                   cases) and sometimes it is not.  If NEWTYPE is
5447                   wider than TYPE, e.g. (float)((long double)double
5448                   + (long double)double) converted to
5449                   (float)(double + double), the transformation is
5450                   unsafe regardless of the details of the types
5451                   involved; double rounding can arise if the result
5452                   of NEWTYPE arithmetic is a NEWTYPE value half way
5453                   between two representable TYPE values but the
5454                   exact value is sufficiently different (in the
5455                   right direction) for this difference to be
5456                   visible in ITYPE arithmetic.  If NEWTYPE is the
5457                   same as TYPE, however, the transformation may be
5458                   safe depending on the types involved: it is safe
5459                   if the ITYPE has strictly more than twice as many
5460                   mantissa bits as TYPE, can represent infinities
5461                   and NaNs if the TYPE can, and has sufficient
5462                   exponent range for the product or ratio of two
5463                   values representable in the TYPE to be within the
5464                   range of normal values of ITYPE.  */
5465               (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5466                    && (flag_unsafe_math_optimizations
5467                        || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5468                            && real_can_shorten_arithmetic (TYPE_MODE (itype),
5469                                                            TYPE_MODE (type))
5470                            && !excess_precision_type (newtype)))
5471                    && !types_match (itype, newtype))
5472                  (convert:type (op (convert:newtype @1)
5473                                    (convert:newtype @2)))
5474          )))) )
5475    ))
5478 /* This is another case of narrowing, specifically when there's an outer
5479    BIT_AND_EXPR which masks off bits outside the type of the innermost
5480    operands.   Like the previous case we have to convert the operands
5481    to unsigned types to avoid introducing undefined behavior for the
5482    arithmetic operation.  */
5483 (for op (minus plus)
5484  (simplify
5485   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5486   (if (INTEGRAL_TYPE_P (type)
5487        /* We check for type compatibility between @0 and @1 below,
5488           so there's no need to check that @1/@3 are integral types.  */
5489        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5490        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5491        /* The precision of the type of each operand must match the
5492           precision of the mode of each operand, similarly for the
5493           result.  */
5494        && type_has_mode_precision_p (TREE_TYPE (@0))
5495        && type_has_mode_precision_p (TREE_TYPE (@1))
5496        && type_has_mode_precision_p (type)
5497        /* The inner conversion must be a widening conversion.  */
5498        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5499        && types_match (@0, @1)
5500        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5501            <= TYPE_PRECISION (TREE_TYPE (@0)))
5502        && (wi::to_wide (@4)
5503            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5504                        true, TYPE_PRECISION (type))) == 0)
5505    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5506     (with { tree ntype = TREE_TYPE (@0); }
5507      (convert (bit_and (op @0 @1) (convert:ntype @4))))
5508     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5509      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5510                (convert:utype @4))))))))
5512 /* Transform (@0 < @1 and @0 < @2) to use min,
5513    (@0 > @1 and @0 > @2) to use max */
5514 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5515      op    (lt      le      gt      ge      lt      le      gt      ge     )
5516      ext   (min     min     max     max     max     max     min     min    )
5517  (simplify
5518   (logic (op:cs @0 @1) (op:cs @0 @2))
5519   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5520        && TREE_CODE (@0) != INTEGER_CST)
5521    (op @0 (ext @1 @2)))))
5523 (simplify
5524  /* signbit(x) -> 0 if x is nonnegative.  */
5525  (SIGNBIT tree_expr_nonnegative_p@0)
5526  { integer_zero_node; })
5528 (simplify
5529  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
5530  (SIGNBIT @0)
5531  (if (!HONOR_SIGNED_ZEROS (@0))
5532   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
5534 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
5535 (for cmp (eq ne)
5536  (for op (plus minus)
5537       rop (minus plus)
5538   (simplify
5539    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5540    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5541         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
5542         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
5543         && !TYPE_SATURATING (TREE_TYPE (@0)))
5544     (with { tree res = int_const_binop (rop, @2, @1); }
5545      (if (TREE_OVERFLOW (res)
5546           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5547       { constant_boolean_node (cmp == NE_EXPR, type); }
5548       (if (single_use (@3))
5549        (cmp @0 { TREE_OVERFLOW (res)
5550                  ? drop_tree_overflow (res) : res; }))))))))
5551 (for cmp (lt le gt ge)
5552  (for op (plus minus)
5553       rop (minus plus)
5554   (simplify
5555    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5556    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5557         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5558     (with { tree res = int_const_binop (rop, @2, @1); }
5559      (if (TREE_OVERFLOW (res))
5560       {
5561         fold_overflow_warning (("assuming signed overflow does not occur "
5562                                 "when simplifying conditional to constant"),
5563                                WARN_STRICT_OVERFLOW_CONDITIONAL);
5564         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
5565         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
5566         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
5567                                   TYPE_SIGN (TREE_TYPE (@1)))
5568                         != (op == MINUS_EXPR);
5569         constant_boolean_node (less == ovf_high, type);
5570       }
5571       (if (single_use (@3))
5572        (with
5573         {
5574           fold_overflow_warning (("assuming signed overflow does not occur "
5575                                   "when changing X +- C1 cmp C2 to "
5576                                   "X cmp C2 -+ C1"),
5577                                  WARN_STRICT_OVERFLOW_COMPARISON);
5578         }
5579         (cmp @0 { res; })))))))))
5581 /* Canonicalizations of BIT_FIELD_REFs.  */
5583 (simplify
5584  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5585  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5587 (simplify
5588  (BIT_FIELD_REF (view_convert @0) @1 @2)
5589  (BIT_FIELD_REF @0 @1 @2))
5591 (simplify
5592  (BIT_FIELD_REF @0 @1 integer_zerop)
5593  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5594   (view_convert @0)))
5596 (simplify
5597  (BIT_FIELD_REF @0 @1 @2)
5598  (switch
5599   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5600        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5601    (switch
5602     (if (integer_zerop (@2))
5603      (view_convert (realpart @0)))
5604     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5605      (view_convert (imagpart @0)))))
5606   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5607        && INTEGRAL_TYPE_P (type)
5608        /* On GIMPLE this should only apply to register arguments.  */
5609        && (! GIMPLE || is_gimple_reg (@0))
5610        /* A bit-field-ref that referenced the full argument can be stripped.  */
5611        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5612             && integer_zerop (@2))
5613            /* Low-parts can be reduced to integral conversions.
5614               ???  The following doesn't work for PDP endian.  */
5615            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5616                /* Don't even think about BITS_BIG_ENDIAN.  */
5617                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5618                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5619                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5620                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
5621                                             - TYPE_PRECISION (type))
5622                                          : 0)) == 0)))
5623    (convert @0))))
5625 /* Simplify vector extracts.  */
5627 (simplify
5628  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5629  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5630       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5631           || (VECTOR_TYPE_P (type)
5632               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5633   (with
5634    {
5635      tree ctor = (TREE_CODE (@0) == SSA_NAME
5636                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5637      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5638      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5639      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5640      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5641    }
5642    (if (n != 0
5643         && (idx % width) == 0
5644         && (n % width) == 0
5645         && known_le ((idx + n) / width,
5646                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5647     (with
5648      {
5649        idx = idx / width;
5650        n = n / width;
5651        /* Constructor elements can be subvectors.  */
5652        poly_uint64 k = 1;
5653        if (CONSTRUCTOR_NELTS (ctor) != 0)
5654          {
5655            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5656            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5657              k = TYPE_VECTOR_SUBPARTS (cons_elem);
5658          }
5659        unsigned HOST_WIDE_INT elt, count, const_k;
5660      }
5661      (switch
5662       /* We keep an exact subset of the constructor elements.  */
5663       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5664        (if (CONSTRUCTOR_NELTS (ctor) == 0)
5665         { build_constructor (type, NULL); }
5666         (if (count == 1)
5667          (if (elt < CONSTRUCTOR_NELTS (ctor))
5668           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5669           { build_zero_cst (type); })
5670          /* We don't want to emit new CTORs unless the old one goes away.
5671             ???  Eventually allow this if the CTOR ends up constant or
5672             uniform.  */
5673          (if (single_use (@0))
5674           {
5675             vec<constructor_elt, va_gc> *vals;
5676             vec_alloc (vals, count);
5677             for (unsigned i = 0;
5678                  i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5679               CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5680                                       CONSTRUCTOR_ELT (ctor, elt + i)->value);
5681             build_constructor (type, vals);
5682           }))))
5683       /* The bitfield references a single constructor element.  */
5684       (if (k.is_constant (&const_k)
5685            && idx + n <= (idx / const_k + 1) * const_k)
5686        (switch
5687         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5688          { build_zero_cst (type); })
5689         (if (n == const_k)
5690          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5691         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5692                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
5694 /* Simplify a bit extraction from a bit insertion for the cases with
5695    the inserted element fully covering the extraction or the insertion
5696    not touching the extraction.  */
5697 (simplify
5698  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5699  (with
5700   {
5701     unsigned HOST_WIDE_INT isize;
5702     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5703       isize = TYPE_PRECISION (TREE_TYPE (@1));
5704     else
5705       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5706   }
5707   (switch
5708    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5709         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5710                       wi::to_wide (@ipos) + isize))
5711     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
5712                                                  wi::to_wide (@rpos)
5713                                                  - wi::to_wide (@ipos)); }))
5714    (if (wi::geu_p (wi::to_wide (@ipos),
5715                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
5716         || wi::geu_p (wi::to_wide (@rpos),
5717                       wi::to_wide (@ipos) + isize))
5718     (BIT_FIELD_REF @0 @rsize @rpos)))))
5720 (if (canonicalize_math_after_vectorization_p ())
5721  (for fmas (FMA)
5722   (simplify
5723    (fmas:c (negate @0) @1 @2)
5724    (IFN_FNMA @0 @1 @2))
5725   (simplify
5726    (fmas @0 @1 (negate @2))
5727    (IFN_FMS @0 @1 @2))
5728   (simplify
5729    (fmas:c (negate @0) @1 (negate @2))
5730    (IFN_FNMS @0 @1 @2))
5731   (simplify
5732    (negate (fmas@3 @0 @1 @2))
5733    (if (single_use (@3))
5734     (IFN_FNMS @0 @1 @2))))
5736  (simplify
5737   (IFN_FMS:c (negate @0) @1 @2)
5738   (IFN_FNMS @0 @1 @2))
5739  (simplify
5740   (IFN_FMS @0 @1 (negate @2))
5741   (IFN_FMA @0 @1 @2))
5742  (simplify
5743   (IFN_FMS:c (negate @0) @1 (negate @2))
5744   (IFN_FNMA @0 @1 @2))
5745  (simplify
5746   (negate (IFN_FMS@3 @0 @1 @2))
5747    (if (single_use (@3))
5748     (IFN_FNMA @0 @1 @2)))
5750  (simplify
5751   (IFN_FNMA:c (negate @0) @1 @2)
5752   (IFN_FMA @0 @1 @2))
5753  (simplify
5754   (IFN_FNMA @0 @1 (negate @2))
5755   (IFN_FNMS @0 @1 @2))
5756  (simplify
5757   (IFN_FNMA:c (negate @0) @1 (negate @2))
5758   (IFN_FMS @0 @1 @2))
5759  (simplify
5760   (negate (IFN_FNMA@3 @0 @1 @2))
5761   (if (single_use (@3))
5762    (IFN_FMS @0 @1 @2)))
5764  (simplify
5765   (IFN_FNMS:c (negate @0) @1 @2)
5766   (IFN_FMS @0 @1 @2))
5767  (simplify
5768   (IFN_FNMS @0 @1 (negate @2))
5769   (IFN_FNMA @0 @1 @2))
5770  (simplify
5771   (IFN_FNMS:c (negate @0) @1 (negate @2))
5772   (IFN_FMA @0 @1 @2))
5773  (simplify
5774   (negate (IFN_FNMS@3 @0 @1 @2))
5775   (if (single_use (@3))
5776    (IFN_FMA @0 @1 @2))))
5778 /* POPCOUNT simplifications.  */
5779 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5780                BUILT_IN_POPCOUNTIMAX)
5781   /* popcount(X&1) is nop_expr(X&1).  */
5782   (simplify
5783     (popcount @0)
5784     (if (tree_nonzero_bits (@0) == 1)
5785       (convert @0)))
5786   /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
5787   (simplify
5788     (plus (popcount:s @0) (popcount:s @1))
5789     (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5790       (popcount (bit_ior @0 @1))))
5791   /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
5792   (for cmp (le eq ne gt)
5793        rep (eq eq ne ne)
5794     (simplify
5795       (cmp (popcount @0) integer_zerop)
5796       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5798 #if GIMPLE
5799 /* 64- and 32-bits branchless implementations of popcount are detected:
5801    int popcount64c (uint64_t x)
5802    {
5803      x -= (x >> 1) & 0x5555555555555555ULL;
5804      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
5805      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
5806      return (x * 0x0101010101010101ULL) >> 56;
5807    }
5809    int popcount32c (uint32_t x)
5810    {
5811      x -= (x >> 1) & 0x55555555;
5812      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
5813      x = (x + (x >> 4)) & 0x0f0f0f0f;
5814      return (x * 0x01010101) >> 24;
5815    }  */
5816 (simplify
5817  (rshift
5818   (mult
5819    (bit_and
5820     (plus:c
5821      (rshift @8 INTEGER_CST@5)
5822       (plus:c@8
5823        (bit_and @6 INTEGER_CST@7)
5824         (bit_and
5825          (rshift
5826           (minus@6 @0
5827            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
5828           INTEGER_CST@10)
5829          INTEGER_CST@9)))
5830     INTEGER_CST@3)
5831    INTEGER_CST@2)
5832   INTEGER_CST@1)
5833   /* Check constants and optab.  */
5834   (with { unsigned prec = TYPE_PRECISION (type);
5835           int shift = (64 - prec) & 63;
5836           unsigned HOST_WIDE_INT c1
5837             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
5838           unsigned HOST_WIDE_INT c2
5839             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
5840           unsigned HOST_WIDE_INT c3
5841             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
5842           unsigned HOST_WIDE_INT c4
5843             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
5844    }
5845    (if (prec >= 16
5846         && prec <= 64
5847         && pow2p_hwi (prec)
5848         && TYPE_UNSIGNED (type)
5849         && integer_onep (@4)
5850         && wi::to_widest (@10) == 2
5851         && wi::to_widest (@5) == 4
5852         && wi::to_widest (@1) == prec - 8
5853         && tree_to_uhwi (@2) == c1
5854         && tree_to_uhwi (@3) == c2
5855         && tree_to_uhwi (@9) == c3
5856         && tree_to_uhwi (@7) == c3
5857         && tree_to_uhwi (@11) == c4
5858         && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
5859                                            OPTIMIZE_FOR_BOTH))
5860     (convert (IFN_POPCOUNT:type @0)))))
5861 #endif
5863 /* Simplify:
5865      a = a1 op a2
5866      r = c ? a : b;
5868    to:
5870      r = c ? a1 op a2 : b;
5872    if the target can do it in one go.  This makes the operation conditional
5873    on c, so could drop potentially-trapping arithmetic, but that's a valid
5874    simplification if the result of the operation isn't needed.
5876    Avoid speculatively generating a stand-alone vector comparison
5877    on targets that might not support them.  Any target implementing
5878    conditional internal functions must support the same comparisons
5879    inside and outside a VEC_COND_EXPR.  */
5881 #if GIMPLE
5882 (for uncond_op (UNCOND_BINARY)
5883      cond_op (COND_BINARY)
5884  (simplify
5885   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5886   (with { tree op_type = TREE_TYPE (@4); }
5887    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5888         && element_precision (type) == element_precision (op_type))
5889     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5890  (simplify
5891   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5892   (with { tree op_type = TREE_TYPE (@4); }
5893    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5894         && element_precision (type) == element_precision (op_type))
5895     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5897 /* Same for ternary operations.  */
5898 (for uncond_op (UNCOND_TERNARY)
5899      cond_op (COND_TERNARY)
5900  (simplify
5901   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5902   (with { tree op_type = TREE_TYPE (@5); }
5903    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5904         && element_precision (type) == element_precision (op_type))
5905     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5906  (simplify
5907   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5908   (with { tree op_type = TREE_TYPE (@5); }
5909    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5910         && element_precision (type) == element_precision (op_type))
5911     (view_convert (cond_op (bit_not @0) @2 @3 @4
5912                   (view_convert:op_type @1)))))))
5913 #endif
5915 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5916    "else" value of an IFN_COND_*.  */
5917 (for cond_op (COND_BINARY)
5918  (simplify
5919   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5920   (with { tree op_type = TREE_TYPE (@3); }
5921    (if (element_precision (type) == element_precision (op_type))
5922     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5923  (simplify
5924   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5925   (with { tree op_type = TREE_TYPE (@5); }
5926    (if (inverse_conditions_p (@0, @2)
5927         && element_precision (type) == element_precision (op_type))
5928     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
5930 /* Same for ternary operations.  */
5931 (for cond_op (COND_TERNARY)
5932  (simplify
5933   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5934   (with { tree op_type = TREE_TYPE (@4); }
5935    (if (element_precision (type) == element_precision (op_type))
5936     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5937  (simplify
5938   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5939   (with { tree op_type = TREE_TYPE (@6); }
5940    (if (inverse_conditions_p (@0, @2)
5941         && element_precision (type) == element_precision (op_type))
5942     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5944 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5945    expressions like:
5947    A: (@0 + @1 < @2) | (@2 + @1 < @0)
5948    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5950    If pointers are known not to wrap, B checks whether @1 bytes starting
5951    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5952    bytes.  A is more efficiently tested as:
5954    A: (sizetype) (@0 + @1 - @2) > @1 * 2
5956    The equivalent expression for B is given by replacing @1 with @1 - 1:
5958    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5960    @0 and @2 can be swapped in both expressions without changing the result.
5962    The folds rely on sizetype's being unsigned (which is always true)
5963    and on its being the same width as the pointer (which we have to check).
5965    The fold replaces two pointer_plus expressions, two comparisons and
5966    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5967    the best case it's a saving of two operations.  The A fold retains one
5968    of the original pointer_pluses, so is a win even if both pointer_pluses
5969    are used elsewhere.  The B fold is a wash if both pointer_pluses are
5970    used elsewhere, since all we end up doing is replacing a comparison with
5971    a pointer_plus.  We do still apply the fold under those circumstances
5972    though, in case applying it to other conditions eventually makes one of the
5973    pointer_pluses dead.  */
5974 (for ior (truth_orif truth_or bit_ior)
5975  (for cmp (le lt)
5976   (simplify
5977    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5978         (cmp:cs (pointer_plus@4 @2 @1) @0))
5979    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5980         && TYPE_OVERFLOW_WRAPS (sizetype)
5981         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5982     /* Calculate the rhs constant.  */
5983     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5984             offset_int rhs = off * 2; }
5985      /* Always fails for negative values.  */
5986      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5987       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5988          pick a canonical order.  This increases the chances of using the
5989          same pointer_plus in multiple checks.  */
5990       (with { bool swap_p = tree_swap_operands_p (@0, @2);
5991               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5992        (if (cmp == LT_EXPR)
5993         (gt (convert:sizetype
5994              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5995                                      { swap_p ? @0 : @2; }))
5996             { rhs_tree; })
5997         (gt (convert:sizetype
5998              (pointer_diff:ssizetype
5999               (pointer_plus { swap_p ? @2 : @0; }
6000                             { wide_int_to_tree (sizetype, off); })
6001               { swap_p ? @0 : @2; }))
6002             { rhs_tree; })))))))))
6004 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6005    element of @1.  */
6006 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6007  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6008   (with { int i = single_nonzero_element (@1); }
6009    (if (i >= 0)
6010     (with { tree elt = vector_cst_elt (@1, i);
6011             tree elt_type = TREE_TYPE (elt);
6012             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6013             tree size = bitsize_int (elt_bits);
6014             tree pos = bitsize_int (elt_bits * i); }
6015      (view_convert
6016       (bit_and:elt_type
6017        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6018        { elt; })))))))
6020 (simplify
6021  (vec_perm @0 @1 VECTOR_CST@2)
6022  (with
6023   {
6024     tree op0 = @0, op1 = @1, op2 = @2;
6026     /* Build a vector of integers from the tree mask.  */
6027     vec_perm_builder builder;
6028     if (!tree_to_vec_perm_builder (&builder, op2))
6029       return NULL_TREE;
6031     /* Create a vec_perm_indices for the integer vector.  */
6032     poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6033     bool single_arg = (op0 == op1);
6034     vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6035   }
6036   (if (sel.series_p (0, 1, 0, 1))
6037    { op0; }
6038    (if (sel.series_p (0, 1, nelts, 1))
6039     { op1; }
6040     (with
6041      {
6042        if (!single_arg)
6043          {
6044            if (sel.all_from_input_p (0))
6045              op1 = op0;
6046            else if (sel.all_from_input_p (1))
6047              {
6048                op0 = op1;
6049                sel.rotate_inputs (1);
6050              }
6051            else if (known_ge (poly_uint64 (sel[0]), nelts))
6052              {
6053                std::swap (op0, op1);
6054                sel.rotate_inputs (1);
6055              }
6056          }
6057        gassign *def;
6058        tree cop0 = op0, cop1 = op1;
6059        if (TREE_CODE (op0) == SSA_NAME
6060            && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6061            && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6062          cop0 = gimple_assign_rhs1 (def);
6063        if (TREE_CODE (op1) == SSA_NAME
6064            && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6065            && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6066          cop1 = gimple_assign_rhs1 (def);
6068        tree t;
6069     }
6070     (if ((TREE_CODE (cop0) == VECTOR_CST
6071           || TREE_CODE (cop0) == CONSTRUCTOR)
6072          && (TREE_CODE (cop1) == VECTOR_CST
6073              || TREE_CODE (cop1) == CONSTRUCTOR)
6074          && (t = fold_vec_perm (type, cop0, cop1, sel)))
6075      { t; }
6076      (with
6077       {
6078         bool changed = (op0 == op1 && !single_arg);
6079         tree ins = NULL_TREE;
6080         unsigned at = 0;
6082         /* See if the permutation is performing a single element
6083            insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6084            in that case.  But only if the vector mode is supported,
6085            otherwise this is invalid GIMPLE.  */
6086         if (TYPE_MODE (type) != BLKmode
6087             && (TREE_CODE (cop0) == VECTOR_CST
6088                 || TREE_CODE (cop0) == CONSTRUCTOR
6089                 || TREE_CODE (cop1) == VECTOR_CST
6090                 || TREE_CODE (cop1) == CONSTRUCTOR))
6091           {
6092             bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6093             if (insert_first_p)
6094               {
6095                 /* After canonicalizing the first elt to come from the
6096                    first vector we only can insert the first elt from
6097                    the first vector.  */
6098                 at = 0;
6099                 if ((ins = fold_read_from_vector (cop0, sel[0])))
6100                   op0 = op1;
6101               }
6102             /* The above can fail for two-element vectors which always
6103                appear to insert the first element, so try inserting
6104                into the second lane as well.  For more than two
6105                elements that's wasted time.  */
6106             if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6107               {
6108                 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6109                 for (at = 0; at < encoded_nelts; ++at)
6110                   if (maybe_ne (sel[at], at))
6111                     break;
6112                 if (at < encoded_nelts
6113                     && (known_eq (at + 1, nelts)
6114                         || sel.series_p (at + 1, 1, at + 1, 1)))
6115                   {
6116                     if (known_lt (poly_uint64 (sel[at]), nelts))
6117                       ins = fold_read_from_vector (cop0, sel[at]);
6118                     else
6119                       ins = fold_read_from_vector (cop1, sel[at] - nelts);
6120                   }
6121               }
6122           }
6124         /* Generate a canonical form of the selector.  */
6125         if (!ins && sel.encoding () != builder)
6126           {
6127             /* Some targets are deficient and fail to expand a single
6128                argument permutation while still allowing an equivalent
6129                2-argument version.  */
6130             tree oldop2 = op2;
6131             if (sel.ninputs () == 2
6132                || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6133               op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6134             else
6135               {
6136                 vec_perm_indices sel2 (builder, 2, nelts);
6137                 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6138                   op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6139                 else
6140                   /* Not directly supported with either encoding,
6141                      so use the preferred form.  */
6142                   op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6143               }
6144             if (!operand_equal_p (op2, oldop2, 0))
6145               changed = true;
6146           }
6147       }
6148       (if (ins)
6149        (bit_insert { op0; } { ins; }
6150          { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); })
6151        (if (changed)
6152         (vec_perm { op0; } { op1; } { op2; }))))))))))
6154 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
6156 (match vec_same_elem_p
6157  @0
6158  (if (uniform_vector_p (@0))))
6160 (match vec_same_elem_p
6161  (vec_duplicate @0))
6163 (simplify
6164  (vec_perm vec_same_elem_p@0 @0 @1)
6165  @0)
6167 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6168    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6169    constant which when multiplied by a power of 2 contains a unique value
6170    in the top 5 or 6 bits.  This is then indexed into a table which maps it
6171    to the number of trailing zeroes.  */
6172 (match (ctz_table_index @1 @2 @3)
6173   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))