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