Daily bump.
[official-gcc.git] / gcc / match.pd
blob7b4b15acc412935ea86756720f6f6605a32b171f
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2    This file is consumed by genmatch which produces gimple-match.cc
3    and generic-match.cc from it.
5    Copyright (C) 2014-2024 Free Software Foundation, Inc.
6    Contributed by Richard Biener <rguenther@suse.de>
7    and Prathamesh Kulkarni  <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
26 /* Generic tree predicates we inherit.  */
27 (define_predicates
28    integer_onep integer_zerop integer_all_onesp integer_minus_onep
29    integer_each_onep integer_truep integer_nonzerop
30    real_zerop real_onep real_minus_onep
31    zerop
32    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p
41    expand_vec_cmp_expr_p
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate bit_not)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG IFN_COND_NOT)
90 (define_operator_list COND_LEN_UNARY
91   IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function.  */
94 (define_operator_list UNCOND_BINARY
95   plus minus
96   mult trunc_div trunc_mod rdiv
97   min max
98   IFN_FMIN IFN_FMAX IFN_COPYSIGN
99   bit_and bit_ior bit_xor
100   lshift rshift)
101 (define_operator_list COND_BINARY
102   IFN_COND_ADD IFN_COND_SUB
103   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
104   IFN_COND_MIN IFN_COND_MAX
105   IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
106   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
107   IFN_COND_SHL IFN_COND_SHR)
108 (define_operator_list COND_LEN_BINARY
109   IFN_COND_LEN_ADD IFN_COND_LEN_SUB
110   IFN_COND_LEN_MUL IFN_COND_LEN_DIV
111   IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
112   IFN_COND_LEN_MIN IFN_COND_LEN_MAX
113   IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
114   IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115   IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations.  */
118 (define_operator_list UNCOND_TERNARY
119   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123   IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129   BUILT_IN_ATOMIC_FETCH_OR_16
130   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132   BUILT_IN_ATOMIC_FETCH_XOR_16
133   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135   BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140   BUILT_IN_SYNC_FETCH_AND_OR_16
141   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143   BUILT_IN_SYNC_FETCH_AND_XOR_16
144   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146   BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*.  */
148 (define_operator_list ATOMIC_FETCH_AND_N
149   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151   BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*.  */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156   BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159    plus conditionalize on tree_nop_conversion_p conversions.  */
160 (match (nop_convert @0)
161  (convert @0)
162  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
164  (view_convert @0)
165  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166       && known_eq (TYPE_VECTOR_SUBPARTS (type),
167                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
170 #if GIMPLE
171 /* These are used by gimple_bitwise_inverted_equal_p to simplify
172    detection of BIT_NOT and comparisons. */
173 (match (bit_not_with_nop @0)
174  (bit_not @0))
175 (match (bit_not_with_nop @0)
176  (convert (bit_not @0))
177  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
178 (for cmp (tcc_comparison)
179  (match (maybe_cmp @0)
180   (cmp@0 @1 @2))
181  (match (maybe_cmp @0)
182   (convert (cmp@0 @1 @2))
183    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
185 /* `a ^ b` is another form of `a != b` when the type
186     is a 1bit precission integer.  */
187 (match (maybe_cmp @0)
188  (bit_xor@0 @1 @2)
189  (if (INTEGRAL_TYPE_P (type)
190       && TYPE_PRECISION (type) == 1)))
191 #endif
193 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
194    ABSU_EXPR returns unsigned absolute value of the operand and the operand
195    of the ABSU_EXPR will have the corresponding signed type.  */
196 (simplify (abs (convert @0))
197  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
198       && !TYPE_UNSIGNED (TREE_TYPE (@0))
199       && element_precision (type) > element_precision (TREE_TYPE (@0)))
200   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
201    (convert (absu:utype @0)))))
203 #if GIMPLE
204 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
205 (simplify
206  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
207  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
208       && !TYPE_UNSIGNED (TREE_TYPE (@0))
209       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
210   (abs @0)))
211 #endif
213 /* Simplifications of operations with one constant operand and
214    simplifications to constants or single values.  */
216 (for op (plus pointer_plus minus bit_ior bit_xor)
217   (simplify
218     (op @0 integer_zerop)
219     (non_lvalue @0)))
221 /* 0 +p index -> (type)index */
222 (simplify
223  (pointer_plus integer_zerop @1)
224  (non_lvalue (convert @1)))
226 /* ptr - 0 -> (type)ptr */
227 (simplify
228  (pointer_diff @0 integer_zerop)
229  (convert @0))
231 /* See if ARG1 is zero and X + ARG1 reduces to X.
232    Likewise if the operands are reversed.  */
233 (simplify
234  (plus:c @0 real_zerop@1)
235  (if (fold_real_zero_addition_p (type, @0, @1, 0))
236   (non_lvalue @0)))
238 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
239 (simplify
240  (minus @0 real_zerop@1)
241  (if (fold_real_zero_addition_p (type, @0, @1, 1))
242   (non_lvalue @0)))
244 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
245    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
246    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
247    if not -frounding-math.  For sNaNs the first operation would raise
248    exceptions but turn the result into qNan, so the second operation
249    would not raise it.   */
250 (for inner_op (plus minus)
251  (for outer_op (plus minus)
252   (simplify
253    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
254     (if (real_zerop (@1)
255          && real_zerop (@2)
256          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
257      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
258                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
259              bool outer_plus
260                = ((outer_op == PLUS_EXPR)
261                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
262       (if (outer_plus && !inner_plus)
263        (outer_op @0 @2)
264        @3))))))
266 /* Simplify x - x.
267    This is unsafe for certain floats even in non-IEEE formats.
268    In IEEE, it is unsafe because it does wrong for NaNs.
269    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
270    Also note that operand_equal_p is always false if an operand
271    is volatile.  */
272 (simplify
273  (minus @0 @0)
274  (if (!FLOAT_TYPE_P (type)
275       || (!tree_expr_maybe_nan_p (@0)
276           && !tree_expr_maybe_infinite_p (@0)
277           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
278               || !HONOR_SIGNED_ZEROS (type))))
279   { build_zero_cst (type); }))
280 (simplify
281  (pointer_diff @@0 @0)
282  { build_zero_cst (type); })
284 (simplify
285  (mult @0 integer_zerop@1)
286  @1)
288 /* -x == x -> x == 0 */
289 (for cmp (eq ne)
290  (simplify
291   (cmp:c @0 (negate @0))
292    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
293         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
294     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
296 /* Maybe fold x * 0 to 0.  The expressions aren't the same
297    when x is NaN, since x * 0 is also NaN.  Nor are they the
298    same in modes with signed zeros, since multiplying a
299    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
300    since x * 0 is NaN.  */
301 (simplify
302  (mult @0 real_zerop@1)
303  (if (!tree_expr_maybe_nan_p (@0)
304       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
305       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
306   @1))
308 /* In IEEE floating point, x*1 is not equivalent to x for snans.
309    Likewise for complex arithmetic with signed zeros.  */
310 (simplify
311  (mult @0 real_onep)
312  (if (!tree_expr_maybe_signaling_nan_p (@0)
313       && (!HONOR_SIGNED_ZEROS (type)
314           || !COMPLEX_FLOAT_TYPE_P (type)))
315   (non_lvalue @0)))
317 /* Transform x * -1.0 into -x.  */
318 (simplify
319  (mult @0 real_minus_onep)
320   (if (!tree_expr_maybe_signaling_nan_p (@0)
321        && (!HONOR_SIGNED_ZEROS (type)
322            || !COMPLEX_FLOAT_TYPE_P (type)))
323    (negate @0)))
325 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
326    unless the target has native support for the former but not the latter.  */
327 (simplify
328  (mult @0 VECTOR_CST@1)
329  (if (initializer_each_zero_or_onep (@1)
330       && !HONOR_SNANS (type)
331       && !HONOR_SIGNED_ZEROS (type))
332   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
333    (if (itype
334         && (!VECTOR_MODE_P (TYPE_MODE (type))
335             || (VECTOR_MODE_P (TYPE_MODE (itype))
336                 && optab_handler (and_optab,
337                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
338     (view_convert (bit_and:itype (view_convert @0)
339                                  (ne @1 { build_zero_cst (type); })))))))
341 /* In SWAR (SIMD within a register) code a signed comparison of packed data
342    can be constructed with a particular combination of shift, bitwise and,
343    and multiplication by constants.  If that code is vectorized we can
344    convert this pattern into a more efficient vector comparison.  */
345 (simplify
346  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
347             uniform_integer_cst_p@2)
348     uniform_integer_cst_p@3)
349  (with {
350    tree rshift_cst = uniform_integer_cst_p (@1);
351    tree bit_and_cst = uniform_integer_cst_p (@2);
352    tree mult_cst = uniform_integer_cst_p (@3);
353   }
354   /* Make sure we're working with vectors and uniform vector constants.  */
355   (if (VECTOR_TYPE_P (type)
356        && tree_fits_uhwi_p (rshift_cst)
357        && tree_fits_uhwi_p (mult_cst)
358        && tree_fits_uhwi_p (bit_and_cst))
359    /* Compute what constants would be needed for this to represent a packed
360       comparison based on the shift amount denoted by RSHIFT_CST.  */
361    (with {
362      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
363      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
364      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
365      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
366      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
367      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
368      mult_i = tree_to_uhwi (mult_cst);
369      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
370      bit_and_i = tree_to_uhwi (bit_and_cst);
371      target_bit_and_i = 0;
373      /* The bit pattern in BIT_AND_I should be a mask for the least
374         significant bit of each packed element that is CMP_BITS wide.  */
375      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
376        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
377     }
378     (if ((exact_log2 (cmp_bits_i)) >= 0
379          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
380          && multiple_p (vec_bits, cmp_bits_i)
381          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
382          && target_mult_i == mult_i
383          && target_bit_and_i == bit_and_i)
384      /* Compute the vector shape for the comparison and check if the target is
385         able to expand the comparison with that type.  */
386      (with {
387        /* We're doing a signed comparison.  */
388        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
389        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
390        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
391        tree vec_truth_type = truth_type_for (vec_cmp_type);
392        tree zeros = build_zero_cst (vec_cmp_type);
393        tree ones = build_all_ones_cst (vec_cmp_type);
394       }
395       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
396            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
397        (view_convert:type (vec_cond (lt:vec_truth_type
398                                      (view_convert:vec_cmp_type @0)
399                                      { zeros; })
400                            { ones; } { zeros; })))))))))
402 (for cmp (gt ge lt le)
403      outp (convert convert negate negate)
404      outn (negate negate convert convert)
405  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
406  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
407  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
408  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
409  (simplify
410   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
411   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
412    (outp (abs @0))))
413  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
414  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
415  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
416  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
417  (simplify
418   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
419   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
420    (outn (abs @0)))))
422 /* Transform X * copysign (1.0, X) into abs(X). */
423 (simplify
424  (mult:c @0 (COPYSIGN_ALL real_onep @0))
425  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
426   (abs @0)))
428 /* Transform X * copysign (1.0, -X) into -abs(X). */
429 (simplify
430  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
431  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
432   (negate (abs @0))))
434 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
435 (simplify
436  (COPYSIGN_ALL REAL_CST@0 @1)
437  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
438   (COPYSIGN_ALL (negate @0) @1)))
440 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
441    tree-ssa-math-opts.cc does the corresponding optimization for
442    unconditional multiplications (via xorsign).  */
443 (simplify
444  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
445  (with { tree signs = sign_mask_for (type); }
446   (if (signs)
447    (with { tree inttype = TREE_TYPE (signs); }
448     (view_convert:type
449      (IFN_COND_XOR:inttype @0
450       (view_convert:inttype @1)
451       (bit_and (view_convert:inttype @2) { signs; })
452       (view_convert:inttype @3)))))))
454 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
455 (simplify
456   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
457   (abs @0))
459 /* X * 1, X / 1 -> X.  */
460 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
461   (simplify
462     (op @0 integer_onep)
463     (non_lvalue @0)))
465 /* (A / (1 << B)) -> (A >> B).
466    Only for unsigned A.  For signed A, this would not preserve rounding
467    toward zero.
468    For example: (-1 / ( 1 << B)) !=  -1 >> B.
469    Also handle widening conversions, like:
470    (A / (unsigned long long) (1U << B)) -> (A >> B)
471    or
472    (A / (unsigned long long) (1 << B)) -> (A >> B).
473    If the left shift is signed, it can be done only if the upper bits
474    of A starting from shift's type sign bit are zero, as
475    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
476    so it is valid only if A >> 31 is zero.  */
477 (simplify
478  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
479  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
480       && (!VECTOR_TYPE_P (type)
481           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
482           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
483       && (useless_type_conversion_p (type, TREE_TYPE (@1))
484           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
485               && (TYPE_UNSIGNED (TREE_TYPE (@1))
486                   || (element_precision (type)
487                       == element_precision (TREE_TYPE (@1)))
488                   || (INTEGRAL_TYPE_P (type)
489                       && (tree_nonzero_bits (@0)
490                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
491                                       true,
492                                       element_precision (type))) == 0)))))
493    (if (!VECTOR_TYPE_P (type)
494         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
495         && element_precision (TREE_TYPE (@3)) < element_precision (type))
496     (convert (rshift @3 @2))
497     (rshift @0 @2))))
499 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
500    undefined behavior in constexpr evaluation, and assuming that the division
501    traps enables better optimizations than these anyway.  */
502 (for div (trunc_div ceil_div floor_div round_div exact_div)
503  /* 0 / X is always zero.  */
504  (simplify
505   (div integer_zerop@0 @1)
506   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
507   (if (!integer_zerop (@1))
508    @0))
509  /* X / -1 is -X.  */
510  (simplify
511   (div @0 integer_minus_onep@1)
512   (if (!TYPE_UNSIGNED (type))
513    (negate @0)))
514  /* X / bool_range_Y is X.  */ 
515  (simplify
516   (div @0 SSA_NAME@1)
517   (if (INTEGRAL_TYPE_P (type)
518        && ssa_name_has_boolean_range (@1)
519        && !flag_non_call_exceptions)
520    @0))
521  /* X / X is one.  */
522  (simplify
523   (div @0 @0)
524   /* But not for 0 / 0 so that we can get the proper warnings and errors.
525      And not for _Fract types where we can't build 1.  */
526   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
527        && !integer_zerop (@0)
528        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
529    { build_one_cst (type); }))
530  /* X / abs (X) is X < 0 ? -1 : 1.  */
531  (simplify
532    (div:C @0 (abs @0))
533    (if (INTEGRAL_TYPE_P (type)
534         && TYPE_OVERFLOW_UNDEFINED (type)
535         && !integer_zerop (@0)
536         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
537     (cond (lt @0 { build_zero_cst (type); })
538           { build_minus_one_cst (type); } { build_one_cst (type); })))
539  /* X / -X is -1.  */
540  (simplify
541    (div:C @0 (negate @0))
542    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
543         && TYPE_OVERFLOW_UNDEFINED (type)
544         && !integer_zerop (@0)
545         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
546     { build_minus_one_cst (type); })))
548 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
549    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
550    for MOD instead of DIV.  */
551 (for floor_divmod (floor_div floor_mod)
552      trunc_divmod (trunc_div trunc_mod)
553  (simplify
554   (floor_divmod @0 @1)
555   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
556        && TYPE_UNSIGNED (type))
557    (trunc_divmod @0 @1))))
559 /* 1 / X -> X == 1 for unsigned integer X.
560    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
561    But not for 1 / 0 so that we can get proper warnings and errors,
562    and not for 1-bit integers as they are edge cases better handled
563    elsewhere.  */
564 (simplify
565  (trunc_div integer_onep@0 @1)
566  (if (INTEGRAL_TYPE_P (type)
567       && TYPE_PRECISION (type) > 1
568       && !integer_zerop (@1)
569       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
570   (if (TYPE_UNSIGNED (type))
571    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
572    (with { tree utype = unsigned_type_for (type); }
573     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
574               { build_int_cst (utype, 2); })
575      @1 { build_zero_cst (type); })))))
577 /* Combine two successive divisions.  Note that combining ceil_div
578    and floor_div is trickier and combining round_div even more so.  */
579 (for div (trunc_div exact_div)
580  (simplify
581   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
582   (with {
583     wi::overflow_type overflow;
584     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
585                             TYPE_SIGN (type), &overflow);
586    }
587    (if (div == EXACT_DIV_EXPR
588         || optimize_successive_divisions_p (@2, @3))
589     (if (!overflow)
590      (div @0 { wide_int_to_tree (type, mul); })
591      (if (TYPE_UNSIGNED (type)
592           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
593       { build_zero_cst (type); }))))))
595 /* Combine successive multiplications.  Similar to above, but handling
596    overflow is different.  */
597 (simplify
598  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
599  (with {
600    wi::overflow_type overflow;
601    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
602                            TYPE_SIGN (type), &overflow);
603   }
604   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
605      otherwise undefined overflow implies that @0 must be zero.  */
606   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
607    (mult @0 { wide_int_to_tree (type, mul); }))))
609 /* Similar to above, but there could be an extra add/sub between
610    successive multuiplications.  */
611 (simplify
612  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
613  (with {
614    bool overflowed = true;
615    wi::overflow_type ovf1, ovf2;
616    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
617                            TYPE_SIGN (type), &ovf1);
618    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
619                            TYPE_SIGN (type), &ovf2);
620   if (TYPE_OVERFLOW_UNDEFINED (type))
621     {
622 #if GIMPLE
623       value_range vr0;
624       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
625           && get_global_range_query ()->range_of_expr (vr0, @4)
626           && !vr0.varying_p () && !vr0.undefined_p ())
627         {
628           wide_int wmin0 = vr0.lower_bound ();
629           wide_int wmax0 = vr0.upper_bound ();
630           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
631           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
632           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
633             {
634               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
635               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
636               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
637                 overflowed = false;
638             }
639         }
640 #endif
641     }
642   else
643    overflowed = false;
645   /* Skip folding on overflow.  */
646   (if (!overflowed)
647    (plus (mult @0 { wide_int_to_tree (type, mul); })
648          { wide_int_to_tree (type, add); }))))
650 /* Similar to above, but a multiplication between successive additions.  */
651 (simplify
652  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
653  (with {
654    bool overflowed = true;
655    wi::overflow_type ovf1;
656    wi::overflow_type ovf2;
657    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
658                            TYPE_SIGN (type), &ovf1);
659    wide_int add = wi::add (mul, wi::to_wide (@3),
660                            TYPE_SIGN (type), &ovf2);
661   if (TYPE_OVERFLOW_UNDEFINED (type))
662     {
663 #if GIMPLE
664       value_range vr0;
665       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
666           && get_global_range_query ()->range_of_expr (vr0, @0)
667           && !vr0.varying_p () && !vr0.undefined_p ())
668         {
669           wide_int wmin0 = vr0.lower_bound ();
670           wide_int wmax0 = vr0.upper_bound ();
671           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
672           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
673           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
674             {
675               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
676               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
677               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
678                 overflowed = false;
679             }
680         }
681 #endif
682     }
683   else
684    overflowed = false;
686   /* Skip folding on overflow.  */
687   (if (!overflowed)
688    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
690 /* Optimize A / A to 1.0 if we don't care about
691    NaNs or Infinities.  */
692 (simplify
693  (rdiv @0 @0)
694  (if (FLOAT_TYPE_P (type)
695       && ! HONOR_NANS (type)
696       && ! HONOR_INFINITIES (type))
697   { build_one_cst (type); }))
699 /* Optimize -A / A to -1.0 if we don't care about
700    NaNs or Infinities.  */
701 (simplify
702  (rdiv:C @0 (negate @0))
703  (if (FLOAT_TYPE_P (type)
704       && ! HONOR_NANS (type)
705       && ! HONOR_INFINITIES (type))
706   { build_minus_one_cst (type); }))
708 /* PR71078: x / abs(x) -> copysign (1.0, x) */
709 (simplify
710  (rdiv:C (convert? @0) (convert? (abs @0)))
711   (if (SCALAR_FLOAT_TYPE_P (type)
712        && ! HONOR_NANS (type)
713        && ! HONOR_INFINITIES (type))
714    (switch
715     (if (types_match (type, float_type_node))
716      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
717     (if (types_match (type, double_type_node))
718      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
719     (if (types_match (type, long_double_type_node))
720      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
722 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
723 (simplify
724  (rdiv @0 real_onep)
725  (if (!tree_expr_maybe_signaling_nan_p (@0))
726   (non_lvalue @0)))
728 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
729 (simplify
730  (rdiv @0 real_minus_onep)
731  (if (!tree_expr_maybe_signaling_nan_p (@0))
732   (negate @0)))
734 (if (flag_reciprocal_math)
735  /* Convert (A/B)/C to A/(B*C). */
736  (simplify
737   (rdiv (rdiv:s @0 @1) @2)
738   (rdiv @0 (mult @1 @2)))
740  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
741  (simplify
742   (rdiv @0 (mult:s @1 REAL_CST@2))
743   (with
744    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
745    (if (tem)
746     (rdiv (mult @0 { tem; } ) @1))))
748  /* Convert A/(B/C) to (A/B)*C  */
749  (simplify
750   (rdiv @0 (rdiv:s @1 @2))
751    (mult (rdiv @0 @1) @2)))
753 /* Simplify x / (- y) to -x / y.  */
754 (simplify
755  (rdiv @0 (negate @1))
756  (rdiv (negate @0) @1))
758 (if (flag_unsafe_math_optimizations)
759  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
760     Since C / x may underflow to zero, do this only for unsafe math.  */
761  (for op (lt le gt ge)
762       neg_op (gt ge lt le)
763   (simplify
764    (op (rdiv REAL_CST@0 @1) real_zerop@2)
765    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
766     (switch
767      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
768       (op @1 @2))
769      /* For C < 0, use the inverted operator.  */
770      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
771       (neg_op @1 @2)))))))
773 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
774 (for div (trunc_div ceil_div floor_div round_div exact_div)
775  (simplify
776   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
777   (if (integer_pow2p (@2)
778        && tree_int_cst_sgn (@2) > 0
779        && tree_nop_conversion_p (type, TREE_TYPE (@0))
780        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
781    (rshift (convert @0)
782            { build_int_cst (integer_type_node,
783                             wi::exact_log2 (wi::to_wide (@2))); }))))
785 /* If ARG1 is a constant, we can convert this to a multiply by the
786    reciprocal.  This does not have the same rounding properties,
787    so only do this if -freciprocal-math.  We can actually
788    always safely do it if ARG1 is a power of two, but it's hard to
789    tell if it is or not in a portable manner.  */
790 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
791  (simplify
792   (rdiv @0 cst@1)
793   (if (optimize)
794    (if (flag_reciprocal_math
795         && !real_zerop (@1))
796     (with
797      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
798      (if (tem)
799       (mult @0 { tem; } )))
800     (if (cst != COMPLEX_CST)
801      (with { tree inverse = exact_inverse (type, @1); }
802       (if (inverse)
803        (mult @0 { inverse; } ))))))))
805 (for mod (ceil_mod floor_mod round_mod trunc_mod)
806  /* 0 % X is always zero.  */
807  (simplify
808   (mod integer_zerop@0 @1)
809   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
810   (if (!integer_zerop (@1))
811    @0))
812  /* X % 1 is always zero.  */
813  (simplify
814   (mod @0 integer_onep)
815   { build_zero_cst (type); })
816  /* X % -1 is zero.  */
817  (simplify
818   (mod @0 integer_minus_onep@1)
819   (if (!TYPE_UNSIGNED (type))
820    { build_zero_cst (type); }))
821  /* X % X is zero.  */
822  (simplify
823   (mod @0 @0)
824   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
825   (if (!integer_zerop (@0))
826    { build_zero_cst (type); }))
827  /* (X % Y) % Y is just X % Y.  */
828  (simplify
829   (mod (mod@2 @0 @1) @1)
830   @2)
831  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
832  (simplify
833   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
834   (if (ANY_INTEGRAL_TYPE_P (type)
835        && TYPE_OVERFLOW_UNDEFINED (type)
836        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
837                              TYPE_SIGN (type)))
838    { build_zero_cst (type); }))
839  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
840     modulo and comparison, since it is simpler and equivalent.  */
841  (for cmp (eq ne)
842   (simplify
843    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
844    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
845     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
846      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
848 /* X % -C is the same as X % C.  */
849 (simplify
850  (trunc_mod @0 INTEGER_CST@1)
851   (if (TYPE_SIGN (type) == SIGNED
852        && !TREE_OVERFLOW (@1)
853        && wi::neg_p (wi::to_wide (@1))
854        && !TYPE_OVERFLOW_TRAPS (type)
855        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
856        && !sign_bit_p (@1, @1))
857    (trunc_mod @0 (negate @1))))
859 /* X % -Y is the same as X % Y.  */
860 (simplify
861  (trunc_mod @0 (convert? (negate @1)))
862  (if (INTEGRAL_TYPE_P (type)
863       && !TYPE_UNSIGNED (type)
864       && !TYPE_OVERFLOW_TRAPS (type)
865       && tree_nop_conversion_p (type, TREE_TYPE (@1))
866       /* Avoid this transformation if X might be INT_MIN or
867          Y might be -1, because we would then change valid
868          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
869       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
870           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
871                                                         (TREE_TYPE (@1))))))
872   (trunc_mod @0 (convert @1))))
874 /* X - (X / Y) * Y is the same as X % Y.  */
875 (simplify
876  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
877  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
878   (convert (trunc_mod @0 @1))))
880 /* x * (1 + y / x) - y -> x - y % x */
881 (simplify
882  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
883  (if (INTEGRAL_TYPE_P (type))
884   (minus @0 (trunc_mod @1 @0))))
886 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
887    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
888    Also optimize A % (C << N)  where C is a power of 2,
889    to A & ((C << N) - 1).
890    Also optimize "A shift (B % C)", if C is a power of 2, to
891    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
892    and assume (B % C) is nonnegative as shifts negative values would
893    be UB.  */
894 (match (power_of_two_cand @1)
895  INTEGER_CST@1)
896 (match (power_of_two_cand @1)
897  (lshift INTEGER_CST@1 @2))
898 (for mod (trunc_mod floor_mod)
899  (for shift (lshift rshift)
900   (simplify
901    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
902    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
903     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
904                                                       1); }))))))
905  (simplify
906   (mod @0 (convert? (power_of_two_cand@1 @2)))
907   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
908        /* Allow any integral conversions of the divisor, except
909           conversion from narrower signed to wider unsigned type
910           where if @1 would be negative power of two, the divisor
911           would not be a power of two.  */
912        && INTEGRAL_TYPE_P (type)
913        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
914        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
915            || TYPE_UNSIGNED (TREE_TYPE (@1))
916            || !TYPE_UNSIGNED (type))
917        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
918    (with { tree utype = TREE_TYPE (@1);
919            if (!TYPE_OVERFLOW_WRAPS (utype))
920              utype = unsigned_type_for (utype); }
921     (bit_and @0 (convert (minus (convert:utype @1)
922                                 { build_one_cst (utype); })))))))
924 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
925 (simplify
926  (trunc_div (mult @0 integer_pow2p@1) @1)
927  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
928   (bit_and @0 { wide_int_to_tree
929                 (type, wi::mask (TYPE_PRECISION (type)
930                                  - wi::exact_log2 (wi::to_wide (@1)),
931                                  false, TYPE_PRECISION (type))); })))
933 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
934 (simplify
935  (mult (trunc_div @0 integer_pow2p@1) @1)
936  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
937   (bit_and @0 (negate @1))))
939 (for div (trunc_div ceil_div floor_div round_div exact_div)
940  /* Simplify (t * u) / u -> t.  */
941  (simplify
942   (div (mult:c @0 @1) @1)
943   (if (ANY_INTEGRAL_TYPE_P (type))
944    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
945     @0
946 #if GIMPLE
947     (with {value_range vr0, vr1;}
948      (if (INTEGRAL_TYPE_P (type)
949           && get_range_query (cfun)->range_of_expr (vr0, @0)
950           && get_range_query (cfun)->range_of_expr (vr1, @1)
951           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
952       @0))
953 #endif
954    )))
955 #if GIMPLE
956  /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v.  */
957  (simplify
958   (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
959   (if (INTEGRAL_TYPE_P (type)
960        && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
961    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
962     (mult @0 (div! @1 @2))
963     (with {value_range vr0, vr1;}
964      (if (get_range_query (cfun)->range_of_expr (vr0, @0)
965           && get_range_query (cfun)->range_of_expr (vr1, @1)
966           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
967       (mult @0 (div! @1 @2))))
968    )))
969 #endif
970  /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v.  */
971  (simplify
972   (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
973   (if (INTEGRAL_TYPE_P (type)
974        && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
975    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
976     (div @1 @2)
977 #if GIMPLE
978     (with {value_range vr0, vr1, vr2;}
979      (if (get_range_query (cfun)->range_of_expr (vr0, @0)
980           && get_range_query (cfun)->range_of_expr (vr1, @1)
981           && get_range_query (cfun)->range_of_expr (vr2, @2)
982           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
983           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
984       (div @1 @2)))
985 #endif
986    ))))
988 #if GIMPLE
989 (for div (trunc_div exact_div)
990  /* Simplify (X + M*N) / N -> X / N + M.  */
991  (simplify
992   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
993   (with {value_range vr0, vr1, vr2, vr3, vr4;}
994   (if (INTEGRAL_TYPE_P (type)
995        && get_range_query (cfun)->range_of_expr (vr1, @1)
996        && get_range_query (cfun)->range_of_expr (vr2, @2)
997        /* "N*M" doesn't overflow.  */
998        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
999        && get_range_query (cfun)->range_of_expr (vr0, @0)
1000        && get_range_query (cfun)->range_of_expr (vr3, @3)
1001        /* "X+(N*M)" doesn't overflow.  */
1002        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
1003        && get_range_query (cfun)->range_of_expr (vr4, @4)
1004        && !vr4.undefined_p ()
1005        /* "X+N*M" is not with opposite sign as "X".  */
1006        && (TYPE_UNSIGNED (type)
1007            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1008            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1009   (plus (div @0 @2) @1))))
1011  /* Simplify (X - M*N) / N -> X / N - M.  */
1012  (simplify
1013   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1014   (with {value_range vr0, vr1, vr2, vr3, vr4;}
1015   (if (INTEGRAL_TYPE_P (type)
1016        && get_range_query (cfun)->range_of_expr (vr1, @1)
1017        && get_range_query (cfun)->range_of_expr (vr2, @2)
1018        /* "N * M" doesn't overflow.  */
1019        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1020        && get_range_query (cfun)->range_of_expr (vr0, @0)
1021        && get_range_query (cfun)->range_of_expr (vr3, @3)
1022        /* "X - (N*M)" doesn't overflow.  */
1023        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1024        && get_range_query (cfun)->range_of_expr (vr4, @4)
1025        && !vr4.undefined_p ()
1026        /* "X-N*M" is not with opposite sign as "X".  */
1027        && (TYPE_UNSIGNED (type)
1028            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1029            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1030   (minus (div @0 @2) @1)))))
1032 /* Simplify
1033    (X + C) / N -> X / N + C / N where C is multiple of N.
1034    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
1035 (for op (trunc_div exact_div rshift)
1036  (simplify
1037   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1038    (with
1039     {
1040       wide_int c = wi::to_wide (@1);
1041       wide_int n = wi::to_wide (@2);
1042       bool shift = op == RSHIFT_EXPR;
1043 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1044                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
1045 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1046                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1047       value_range vr0, vr1, vr3;
1048     }
1049     (if (INTEGRAL_TYPE_P (type)
1050          && get_range_query (cfun)->range_of_expr (vr0, @0))
1051      (if (exact_mod (c)
1052           && get_range_query (cfun)->range_of_expr (vr1, @1)
1053           /* "X+C" doesn't overflow.  */
1054           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1055           && get_range_query (cfun)->range_of_expr (vr3, @3)
1056           && !vr3.undefined_p ()
1057           /* "X+C" and "X" are not of opposite sign.  */
1058           && (TYPE_UNSIGNED (type)
1059               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1060               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1061        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1062        (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1063             && exact_mod (-c)
1064             /* unsigned "X-(-C)" doesn't underflow.  */
1065             && wi::geu_p (vr0.lower_bound (), -c))
1066          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1067 #undef plus_op1
1068 #undef exact_mod
1069 #endif
1071 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1072    if var is smaller in precision.
1073    This is always safe for both doing the negative in signed or unsigned
1074    as the value for undefined will not show up.
1075    Note the outer cast cannot be a boolean type as the only valid values
1076    are 0,-1/1 (depending on the signedness of the boolean) and the negative
1077    is there to get the correct value.  */
1078 (simplify
1079  (convert (negate:s@1 (convert:s @0)))
1080  (if (INTEGRAL_TYPE_P (type)
1081       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1082       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1083       && TREE_CODE (type) != BOOLEAN_TYPE)
1084     (negate (convert @0))))
1086 (for op (negate abs)
1087  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1088  (for coss (COS COSH)
1089   (simplify
1090    (coss (op @0))
1091     (coss @0)))
1092  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1093  (for pows (POW)
1094   (simplify
1095    (pows (op @0) REAL_CST@1)
1096    (with { HOST_WIDE_INT n; }
1097     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1098      (pows @0 @1)))))
1099  /* Likewise for powi.  */
1100  (for pows (POWI)
1101   (simplify
1102    (pows (op @0) INTEGER_CST@1)
1103    (if ((wi::to_wide (@1) & 1) == 0)
1104     (pows @0 @1))))
1105  /* Strip negate and abs from both operands of hypot.  */
1106  (for hypots (HYPOT)
1107   (simplify
1108    (hypots (op @0) @1)
1109    (hypots @0 @1))
1110   (simplify
1111    (hypots @0 (op @1))
1112    (hypots @0 @1)))
1113  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1114  (for copysigns (COPYSIGN_ALL)
1115   (simplify
1116    (copysigns (op @0) @1)
1117    (copysigns @0 @1))))
1119 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1120 (simplify
1121  (mult (abs@1 @0) @1)
1122  (mult @0 @0))
1124 /* Convert absu(x)*absu(x) -> x*x.  */
1125 (simplify
1126  (mult (absu@1 @0) @1)
1127  (mult (convert@2 @0) @2))
1129 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1130 (for coss (COS COSH)
1131  (for copysigns (COPYSIGN)
1132   (simplify
1133    (coss (copysigns @0 @1))
1134     (coss @0))))
1136 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1137 (for pows (POW)
1138  (for copysigns (COPYSIGN)
1139   (simplify
1140    (pows (copysigns @0 @2) REAL_CST@1)
1141    (with { HOST_WIDE_INT n; }
1142     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1143      (pows @0 @1))))))
1144 /* Likewise for powi.  */
1145 (for pows (POWI)
1146  (for copysigns (COPYSIGN)
1147   (simplify
1148    (pows (copysigns @0 @2) INTEGER_CST@1)
1149    (if ((wi::to_wide (@1) & 1) == 0)
1150     (pows @0 @1)))))
1152 (for hypots (HYPOT)
1153  (for copysigns (COPYSIGN)
1154   /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1155   (simplify
1156    (hypots (copysigns @0 @1) @2)
1157    (hypots @0 @2))
1158   /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1159   (simplify
1160    (hypots @0 (copysigns @1 @2))
1161    (hypots @0 @1))))
1163 /* copysign(x, CST) -> abs (x).  */
1164 (for copysigns (COPYSIGN_ALL)
1165  (simplify
1166   (copysigns @0 REAL_CST@1)
1167   (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1168    (abs @0))))
1170 /* Transform fneg (fabs (X)) -> copysign (X, -1).  */
1171 (simplify
1172  (negate (abs @0))
1173  (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1175 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1176 (for copysigns (COPYSIGN_ALL)
1177  (simplify
1178   (copysigns (copysigns @0 @1) @2)
1179   (copysigns @0 @2)))
1181 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1182 (for copysigns (COPYSIGN_ALL)
1183  (simplify
1184   (mult (copysigns@2 @0 @1) @2)
1185   (mult @0 @0)))
1187 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1188 (for ccoss (CCOS CCOSH)
1189  (simplify
1190   (ccoss (negate @0))
1191    (ccoss @0)))
1193 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1194 (for ops (conj negate)
1195  (for cabss (CABS)
1196   (simplify
1197    (cabss (ops @0))
1198    (cabss @0))))
1200 /* Fold (a * (1 << b)) into (a << b)  */
1201 (simplify
1202  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1203   (if (! FLOAT_TYPE_P (type)
1204        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1205    (lshift @0 @2)))
1207 /* Shifts by precision or greater result in zero.  */
1208 (for shift (lshift rshift)
1209  (simplify
1210   (shift @0 uniform_integer_cst_p@1)
1211   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1212        /* Leave arithmetic right shifts of possibly negative values alone.  */
1213        && (TYPE_UNSIGNED (type)
1214            || shift == LSHIFT_EXPR
1215            || tree_expr_nonnegative_p (@0))
1216        /* Use a signed compare to leave negative shift counts alone.  */
1217        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1218                      element_precision (type)))
1219    { build_zero_cst (type); })))
1221 /* Shifts by constants distribute over several binary operations,
1222    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1223 (for op (plus minus)
1224   (simplify
1225     (op (lshift:s @0 @1) (lshift:s @2 @1))
1226     (if (INTEGRAL_TYPE_P (type)
1227          && TYPE_OVERFLOW_WRAPS (type)
1228          && !TYPE_SATURATING (type))
1229       (lshift (op @0 @2) @1))))
1231 (for op (bit_and bit_ior bit_xor)
1232   (simplify
1233     (op (lshift:s @0 @1) (lshift:s @2 @1))
1234     (if (INTEGRAL_TYPE_P (type))
1235       (lshift (op @0 @2) @1)))
1236   (simplify
1237     (op (rshift:s @0 @1) (rshift:s @2 @1))
1238     (if (INTEGRAL_TYPE_P (type))
1239       (rshift (op @0 @2) @1))))
1241 /* Fold (1 << (C - x)) where C = precision(type) - 1
1242    into ((1 << C) >> x). */
1243 (simplify
1244  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1245   (if (INTEGRAL_TYPE_P (type)
1246        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1247        && single_use (@1))
1248    (if (TYPE_UNSIGNED (type))
1249      (rshift (lshift @0 @2) @3)
1250    (with
1251     { tree utype = unsigned_type_for (type); }
1252     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1254 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1255 (simplify
1256  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1257  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1258       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1259   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1260    (bit_and (convert @0)
1261             { wide_int_to_tree (type,
1262                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1264 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1265 (for cst (INTEGER_CST VECTOR_CST)
1266  (simplify
1267   (rshift (negate:s @0) cst@1)
1268    (if (!TYPE_UNSIGNED (type)
1269         && TYPE_OVERFLOW_UNDEFINED (type))
1270     (with { tree stype = TREE_TYPE (@1);
1271             tree bt = truth_type_for (type);
1272             tree zeros = build_zero_cst (type);
1273             tree cst = NULL_TREE; }
1274      (switch
1275       /* Handle scalar case.  */
1276       (if (INTEGRAL_TYPE_P (type)
1277            /* If we apply the rule to the scalar type before vectorization
1278               we will enforce the result of the comparison being a bool
1279               which will require an extra AND on the result that will be
1280               indistinguishable from when the user did actually want 0
1281               or 1 as the result so it can't be removed.  */
1282            && canonicalize_math_after_vectorization_p ()
1283            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1284        (negate (convert (gt @0 { zeros; }))))
1285       /* Handle vector case.  */
1286       (if (VECTOR_INTEGER_TYPE_P (type)
1287            /* First check whether the target has the same mode for vector
1288               comparison results as it's operands do.  */
1289            && TYPE_MODE (bt) == TYPE_MODE (type)
1290            /* Then check to see if the target is able to expand the comparison
1291               with the given type later on, otherwise we may ICE.  */
1292            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1293            && (cst = uniform_integer_cst_p (@1)) != NULL
1294            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1295        (view_convert (gt:bt @0 { zeros; }))))))))
1297 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1298 (simplify
1299  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1300   (if (flag_associative_math
1301        && single_use (@3))
1302    (with
1303     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1304     (if (tem)
1305      (rdiv { tem; } @1)))))
1307 /* Simplify ~X & X as zero.  */
1308 (simplify
1309  (bit_and (convert? @0) (convert? @1))
1310  (with { bool wascmp; }
1311   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1312        && bitwise_inverted_equal_p (@0, @1, wascmp))
1313    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1315 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1316 (simplify
1317   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1318   (if (TYPE_UNSIGNED (type))
1319     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1321 (for bitop (bit_and bit_ior)
1322      cmp (eq ne)
1323  /* PR35691: Transform
1324     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1325     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1326  (simplify
1327   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1328    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1329         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1330         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1331     (cmp (bit_ior @0 (convert @1)) @2)))
1332  /* Transform:
1333     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1334     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1335  (simplify
1336   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1337    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1338         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1339         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1340     (cmp (bit_and @0 (convert @1)) @2))))
1342 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1343 (simplify
1344  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1345   (minus (bit_xor @0 @1) @1))
1346 (simplify
1347  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1348  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1349   (minus (bit_xor @0 @1) @1)))
1351 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1352 (simplify
1353  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1354   (minus @1 (bit_xor @0 @1)))
1356 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1357 (for op (bit_ior bit_xor plus)
1358  (simplify
1359   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1360   (with { bool wascmp0, wascmp1; }
1361    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1362         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1363         && ((!wascmp0 && !wascmp1)
1364             || element_precision (type) == 1))
1365    (bit_xor @0 @1)))))
1367 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1368 (simplify
1369   (bit_ior:c (bit_xor:c @0 @1) @0)
1370   (bit_ior @0 @1))
1372 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1373 (simplify
1374  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1375  @2)
1377 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1378 (simplify
1379  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1380  (bit_not (bit_and @0 @1)))
1382 /* (~a & b) ^ a  -->   (a | b)   */
1383 (simplify
1384  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1385  (bit_ior @0 @1))
1387 /* (a | b) & ~(a ^ b)  -->  a & b  */
1388 (simplify
1389  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1390  (bit_and @0 @1))
1392 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1393 (simplify
1394  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1395  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1396       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1397   (bit_and @0 @1)))
1399 /* a | ~(a ^ b)  -->  a | ~b  */
1400 (simplify
1401  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1402  (bit_ior @0 (bit_not @1)))
1404 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1405 (simplify
1406  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1407  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1408       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1409   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1411 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1412 (simplify
1413  (bit_ior:c @0 (bit_xor:cs @1 @2))
1414  (with { bool wascmp; }
1415  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1416       && (!wascmp || element_precision (type) == 1))
1417   (bit_ior @0 (bit_not @2)))))
1419 /* a & ~(a ^ b)  -->  a & b  */
1420 (simplify
1421  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1422  (bit_and @0 @1))
1424 /* a & (a == b)  -->  a & b (boolean version of the above). */
1425 (simplify
1426  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1427  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1428       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1429   (bit_and @0 @1)))
1431 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1432 (simplify
1433  (bit_and:c @0 (bit_xor:c @1 @2))
1434  (with { bool wascmp; }
1435  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1436       && (!wascmp || element_precision (type) == 1))
1437   (bit_and @0 @2))))
1439 /* (a | b) | (a &^ b)  -->  a | b  */
1440 (for op (bit_and bit_xor)
1441  (simplify
1442   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1443   @2))
1445 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1446 (simplify
1447  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1448  @2)
1450 /* (a & b) | (a == b)  -->  a == b  */
1451 (simplify
1452  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1453  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1454       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1455   @2))
1457 /* ~(~a & b)  -->  a | ~b  */
1458 (simplify
1459  (bit_not (bit_and:cs (bit_not @0) @1))
1460  (bit_ior @0 (bit_not @1)))
1462 /* ~(~a | b) --> a & ~b */
1463 (simplify
1464  (bit_not (bit_ior:cs (bit_not @0) @1))
1465  (bit_and @0 (bit_not @1)))
1467 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1468 (simplify
1469  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1470  (bit_and @3 (bit_not @2)))
1472 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1473 (simplify
1474  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1475  (bit_ior @3 @2))
1477 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1478 (simplify
1479  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1480   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1482 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1483 (simplify
1484  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1485   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1487 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1488 (simplify
1489  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1490  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1491       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1492   (bit_xor @0 @1)))
1494 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1495    ((A & N) + B) & M -> (A + B) & M
1496    Similarly if (N & M) == 0,
1497    ((A | N) + B) & M -> (A + B) & M
1498    and for - instead of + (or unary - instead of +)
1499    and/or ^ instead of |.
1500    If B is constant and (B & M) == 0, fold into A & M.  */
1501 (for op (plus minus)
1502  (for bitop (bit_and bit_ior bit_xor)
1503   (simplify
1504    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1505     (with
1506      { tree pmop[2];
1507        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1508                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1509                                        NULL_TREE, pmop); }
1510      (if (utype)
1511       (convert (bit_and (op (convert:utype { pmop[0]; })
1512                             (convert:utype { pmop[1]; }))
1513                         (convert:utype @2))))))
1514   (simplify
1515    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1516     (with
1517      { tree pmop[2];
1518        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1519                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1520                                        @4, pmop); }
1521      (if (utype)
1522       (convert (bit_and (op (convert:utype { pmop[0]; })
1523                             (convert:utype { pmop[1]; }))
1524                         (convert:utype @2)))))))
1525  (simplify
1526   (bit_and (op:s @0 @1) INTEGER_CST@2)
1527    (with
1528     { tree pmop[2];
1529       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1530                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1531                                       NULL_TREE, NULL_TREE, pmop); }
1532     (if (utype)
1533      (convert (bit_and (op (convert:utype { pmop[0]; })
1534                            (convert:utype { pmop[1]; }))
1535                        (convert:utype @2)))))))
1536 (for bitop (bit_and bit_ior bit_xor)
1537  (simplify
1538   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1539    (with
1540     { tree pmop[2];
1541       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1542                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1543                                       NULL_TREE, NULL_TREE, pmop); }
1544     (if (utype)
1545      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1546                        (convert:utype @1)))))))
1548 /* X % Y is smaller than Y.  */
1549 (for cmp (lt ge)
1550  (simplify
1551   (cmp:c (trunc_mod @0 @1) @1)
1552   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1553    { constant_boolean_node (cmp == LT_EXPR, type); })))
1555 /* x | ~0 -> ~0  */
1556 (simplify
1557  (bit_ior @0 integer_all_onesp@1)
1558  @1)
1560 /* x | 0 -> x  */
1561 (simplify
1562  (bit_ior @0 integer_zerop)
1563  @0)
1565 /* x & 0 -> 0  */
1566 (simplify
1567  (bit_and @0 integer_zerop@1)
1568  @1)
1570 /* ~x | x -> -1 */
1571 /* ~x ^ x -> -1 */
1572 (for op (bit_ior bit_xor)
1573  (simplify
1574   (op (convert? @0) (convert? @1))
1575   (with { bool wascmp; }
1576    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1577         && bitwise_inverted_equal_p (@0, @1, wascmp))
1578     (convert
1579      { wascmp
1580         ? constant_boolean_node (true, type)
1581         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1583 /* x ^ x -> 0 */
1584 (simplify
1585   (bit_xor @0 @0)
1586   { build_zero_cst (type); })
1588 /* Canonicalize X ^ ~0 to ~X.  */
1589 (simplify
1590   (bit_xor @0 integer_all_onesp@1)
1591   (bit_not @0))
1593 /* x & ~0 -> x  */
1594 (simplify
1595  (bit_and @0 integer_all_onesp)
1596   (non_lvalue @0))
1598 /* x & x -> x,  x | x -> x  */
1599 (for bitop (bit_and bit_ior)
1600  (simplify
1601   (bitop @0 @0)
1602   (non_lvalue @0)))
1604 /* x & C -> x if we know that x & ~C == 0.  */
1605 #if GIMPLE
1606 (simplify
1607  (bit_and SSA_NAME@0 INTEGER_CST@1)
1608  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1609       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1610   @0))
1612 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1613 (simplify
1614  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1615  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1616       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1617   @0))
1619 /* x | C -> C if we know that x & ~C == 0.  */
1620 (simplify
1621  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1622  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1623       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1624   @1))
1625 #endif
1627 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1628 (simplify
1629  (bit_not (minus (bit_not @0) @1))
1630  (plus @0 @1))
1631 (simplify
1632  (bit_not (plus:c (bit_not @0) @1))
1633  (minus @0 @1))
1634 /* (~X - ~Y) -> Y - X.  */
1635 (simplify
1636  (minus (bit_not @0) (bit_not @1))
1637   (if (!TYPE_OVERFLOW_SANITIZED (type))
1638    (with { tree utype = unsigned_type_for (type); }
1639     (convert (minus (convert:utype @1) (convert:utype @0))))))
1641 /* ~(X - Y) -> ~X + Y.  */
1642 (simplify
1643  (bit_not (minus:s @0 @1))
1644  (plus (bit_not @0) @1))
1645 (simplify
1646  (bit_not (plus:s @0 INTEGER_CST@1))
1647  (if ((INTEGRAL_TYPE_P (type)
1648        && TYPE_UNSIGNED (type))
1649       || (!TYPE_OVERFLOW_SANITIZED (type)
1650           && may_negate_without_overflow_p (@1)))
1651   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1653 #if GIMPLE
1654 /* ~X + Y -> (Y - X) - 1.  */
1655 (simplify
1656  (plus:c (bit_not @0) @1)
1657   (if (ANY_INTEGRAL_TYPE_P (type)
1658        && TYPE_OVERFLOW_WRAPS (type)
1659        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1660        && !integer_all_onesp (@1))
1661    (plus (minus @1 @0) { build_minus_one_cst (type); })
1662    (if (INTEGRAL_TYPE_P (type)
1663         && TREE_CODE (@1) == INTEGER_CST
1664         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1665                                               SIGNED))
1666     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1667 #endif
1669 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1670 (simplify
1671  (bit_not (rshift:s @0 @1))
1672   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1673    (rshift (bit_not! @0) @1)
1674    /* For logical right shifts, this is possible only if @0 doesn't
1675       have MSB set and the logical right shift is changed into
1676       arithmetic shift.  */
1677    (if (INTEGRAL_TYPE_P (type)
1678         && !wi::neg_p (tree_nonzero_bits (@0)))
1679     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1680      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1682 /* x + (x & 1) -> (x + 1) & ~1 */
1683 (simplify
1684  (plus:c @0 (bit_and:s @0 integer_onep@1))
1685  (bit_and (plus @0 @1) (bit_not @1)))
1687 /* x & ~(x & y) -> x & ~y */
1688 /* x | ~(x | y) -> x | ~y  */
1689 (for bitop (bit_and bit_ior)
1690  (simplify
1691   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1692   (bitop @0 (bit_not @1))))
1694 /* (~x & y) | ~(x | y) -> ~x */
1695 (simplify
1696  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1697  @2)
1699 /* (x | y) ^ (x | ~y) -> ~x */
1700 (simplify
1701  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1702  (bit_not @0))
1704 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1705 (simplify
1706  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1707  (bit_not (bit_xor @0 @1)))
1709 /* (~x | y) ^ (x ^ y) -> x | ~y */
1710 (simplify
1711  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1712  (bit_ior @0 (bit_not @1)))
1714 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1715 (simplify
1716  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1717  (bit_not (bit_and @0 @1)))
1719 /* (x & y) ^ (x | y) -> x ^ y */
1720 (simplify
1721  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1722  (bit_xor @0 @1))
1724 /* (x ^ y) ^ (x | y) -> x & y */
1725 (simplify
1726  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1727  (bit_and @0 @1))
1729 /* (x & y) + (x ^ y) -> x | y */
1730 /* (x & y) | (x ^ y) -> x | y */
1731 /* (x & y) ^ (x ^ y) -> x | y */
1732 (for op (plus bit_ior bit_xor)
1733  (simplify
1734   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1735   (bit_ior @0 @1)))
1737 /* (x & y) + (x | y) -> x + y */
1738 (simplify
1739  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1740  (plus @0 @1))
1742 /* (x + y) - (x | y) -> x & y */
1743 (simplify
1744  (minus (plus @0 @1) (bit_ior @0 @1))
1745  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1746       && !TYPE_SATURATING (type))
1747   (bit_and @0 @1)))
1749 /* (x + y) - (x & y) -> x | y */
1750 (simplify
1751  (minus (plus @0 @1) (bit_and @0 @1))
1752  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1753       && !TYPE_SATURATING (type))
1754   (bit_ior @0 @1)))
1756 /* (x | y) - y -> (x & ~y) */
1757 (simplify
1758  (minus (bit_ior:cs @0 @1) @1)
1759  (bit_and @0 (bit_not @1)))
1761 /* (x | y) - (x ^ y) -> x & y */
1762 (simplify
1763  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1764  (bit_and @0 @1))
1766 /* (x | y) - (x & y) -> x ^ y */
1767 (simplify
1768  (minus (bit_ior @0 @1) (bit_and @0 @1))
1769  (bit_xor @0 @1))
1771 /* (x | y) & ~(x & y) -> x ^ y */
1772 (simplify
1773  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1774  (bit_xor @0 @1))
1776 /* (x | y) & (~x ^ y) -> x & y */
1777 (simplify
1778  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1779  (with { bool wascmp; }
1780   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1781        && (!wascmp || element_precision (type) == 1))
1782    (bit_and @0 @1))))
1784 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1785 (simplify
1786  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1787  (bit_not (bit_xor @0 @1)))
1789 /* (~x | y) ^ (x | ~y) -> x ^ y */
1790 (simplify
1791  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1792  (bit_xor @0 @1))
1794 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1795 (simplify
1796  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1797                               (nop_convert2? (bit_ior @0 @1))))
1798        integer_all_onesp)
1799  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1800       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1801       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1802       && !TYPE_SATURATING (TREE_TYPE (@2)))
1803  (bit_not (convert (bit_xor @0 @1)))))
1804 (simplify
1805  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1806                                integer_all_onesp))
1807        (nop_convert3? (bit_ior @0 @1)))
1808  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1809       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1810       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1811       && !TYPE_SATURATING (TREE_TYPE (@2)))
1812  (bit_not (convert (bit_xor @0 @1)))))
1813 (simplify
1814  (minus (nop_convert1? (bit_and @0 @1))
1815        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1816                                integer_onep)))
1817  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1818       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1819       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1820       && !TYPE_SATURATING (TREE_TYPE (@2)))
1821  (bit_not (convert (bit_xor @0 @1)))))
1823 /* ~x & ~y -> ~(x | y)
1824    ~x | ~y -> ~(x & y) */
1825 (for op (bit_and bit_ior)
1826      rop (bit_ior bit_and)
1827  (simplify
1828   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1829   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1830        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1831    (bit_not (rop (convert @0) (convert @1))))))
1833 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1834    with a constant, and the two constants have no bits in common,
1835    we should treat this as a BIT_IOR_EXPR since this may produce more
1836    simplifications.  */
1837 (for op (bit_xor plus)
1838  (simplify
1839   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1840       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1841   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1842        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1843        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1844    (bit_ior (convert @4) (convert @5)))))
1846 /* (X | Y) ^ X -> Y & ~ X*/
1847 (simplify
1848  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1849  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1850   (convert (bit_and @1 (bit_not @0)))))
1852 /* (~X | Y) ^ X -> ~(X & Y).  */
1853 (simplify
1854  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1855  (if (bitwise_equal_p (@0, @2))
1856   (convert (bit_not (bit_and @0 (convert @1))))))
1858 /* Convert ~X ^ ~Y to X ^ Y.  */
1859 (simplify
1860  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1861  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1862       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1863   (bit_xor (convert @0) (convert @1))))
1865 /* Convert ~X ^ C to X ^ ~C.  */
1866 (simplify
1867  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1868  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1869   (bit_xor (convert @0) (bit_not @1))))
1871 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1872 (for opo (bit_and bit_xor)
1873      opi (bit_xor bit_and)
1874  (simplify
1875   (opo:c (opi:cs @0 @1) @1)
1876   (bit_and (bit_not @0) @1)))
1878 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1879    operands are another bit-wise operation with a common input.  If so,
1880    distribute the bit operations to save an operation and possibly two if
1881    constants are involved.  For example, convert
1882      (A | B) & (A | C) into A | (B & C)
1883    Further simplification will occur if B and C are constants.  */
1884 (for op (bit_and bit_ior bit_xor)
1885      rop (bit_ior bit_and bit_and)
1886  (simplify
1887   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1888   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1889        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1890    (rop (convert @0) (op (convert @1) (convert @2))))))
1892 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1893 /* (X & Y) & Y -> X & Y
1894    (X | Y) | Y -> X | Y  */
1895 (for op (bit_and bit_ior)
1896  (simplify
1897   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1898   @2))
1899 /* (X ^ Y) ^ Y -> X  */
1900 (simplify
1901  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1902  (convert @0))
1904 /* (X & ~Y) & Y -> 0 */
1905 (simplify
1906  (bit_and:c (bit_and @0 @1) @2)
1907  (with { bool wascmp; }
1908   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1909        || bitwise_inverted_equal_p (@1, @2, wascmp))
1910    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1911 /* (X | ~Y) | Y -> -1 */
1912 (simplify
1913  (bit_ior:c (bit_ior @0 @1) @2)
1914  (with { bool wascmp; }
1915   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1916         || bitwise_inverted_equal_p (@1, @2, wascmp))
1917        && (!wascmp || element_precision (type) == 1))
1918    { build_all_ones_cst (TREE_TYPE (@0)); })))
1920 /* (X & Y) & (X & Z) -> (X & Y) & Z
1921    (X | Y) | (X | Z) -> (X | Y) | Z  */
1922 (for op (bit_and bit_ior)
1923  (simplify
1924   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1925   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1926        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1927    (if (single_use (@5) && single_use (@6))
1928     (op @3 (convert @2))
1929     (if (single_use (@3) && single_use (@4))
1930      (op (convert @1) @5))))))
1931 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1932 (simplify
1933  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1934  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1935       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1936   (bit_xor (convert @1) (convert @2))))
1938 /* Convert abs (abs (X)) into abs (X).
1939    also absu (absu (X)) into absu (X).  */
1940 (simplify
1941  (abs (abs@1 @0))
1942  @1)
1944 (simplify
1945  (absu (convert@2 (absu@1 @0)))
1946  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1947   @1))
1949 /* Convert abs[u] (-X) -> abs[u] (X).  */
1950 (simplify
1951  (abs (negate @0))
1952  (abs @0))
1954 (simplify
1955  (absu (negate @0))
1956  (absu @0))
1958 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1959 (simplify
1960  (abs tree_expr_nonnegative_p@0)
1961  @0)
1963 (simplify
1964  (absu tree_expr_nonnegative_p@0)
1965  (convert @0))
1967 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1968 (simplify
1969  (mult:c (nop_convert1?
1970           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1971                     integer_onep))
1972          (nop_convert3? @0))
1973  (if (INTEGRAL_TYPE_P (type)
1974       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1975       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1976   (if (TYPE_UNSIGNED (type))
1977    (absu @0)
1978    (abs @0)
1979   )
1983 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1984 (match negate_expr_p
1985  INTEGER_CST
1986  (if ((INTEGRAL_TYPE_P (type)
1987        && TYPE_UNSIGNED (type))
1988       || (!TYPE_OVERFLOW_SANITIZED (type)
1989           && may_negate_without_overflow_p (t)))))
1990 (match negate_expr_p
1991  FIXED_CST)
1992 (match negate_expr_p
1993  (negate @0)
1994  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1995 (match negate_expr_p
1996  REAL_CST
1997  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1998 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1999    ways.  */
2000 (match negate_expr_p
2001  VECTOR_CST
2002  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2003 (match negate_expr_p
2004  (minus @0 @1)
2005  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2006       || (FLOAT_TYPE_P (type)
2007           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2008           && !HONOR_SIGNED_ZEROS (type)))))
2010 /* (-A) * (-B) -> A * B  */
2011 (simplify
2012  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2013   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2014        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2015    (mult (convert @0) (convert (negate @1)))))
2017 /* -(A + B) -> (-B) - A.  */
2018 (simplify
2019  (negate (plus:c @0 negate_expr_p@1))
2020  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2021       && !HONOR_SIGNED_ZEROS (type))
2022   (minus (negate @1) @0)))
2024 /* -(A - B) -> B - A.  */
2025 (simplify
2026  (negate (minus @0 @1))
2027  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2028       || (FLOAT_TYPE_P (type)
2029           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2030           && !HONOR_SIGNED_ZEROS (type)))
2031   (minus @1 @0)))
2032 (simplify
2033  (negate (pointer_diff @0 @1))
2034  (if (TYPE_OVERFLOW_UNDEFINED (type))
2035   (pointer_diff @1 @0)))
2037 /* A - B -> A + (-B) if B is easily negatable.  */
2038 (simplify
2039  (minus @0 negate_expr_p@1)
2040  (if (!FIXED_POINT_TYPE_P (type))
2041  (plus @0 (negate @1))))
2043 /* 1 - a is a ^ 1 if a had a bool range. */
2044 /* This is only enabled for gimple as sometimes
2045    cfun is not set for the function which contains
2046    the SSA_NAME (e.g. while IPA passes are happening,
2047    fold might be called).  */
2048 (simplify
2049  (minus integer_onep@0 SSA_NAME@1)
2050   (if (INTEGRAL_TYPE_P (type)
2051        && ssa_name_has_boolean_range (@1))
2052    (bit_xor @1 @0)))
2054 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
2055 (simplify
2056  (negate (mult:c@0 @1 negate_expr_p@2))
2057  (if (! TYPE_UNSIGNED (type)
2058       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2059       && single_use (@0))
2060   (mult @1 (negate @2))))
2062 (simplify
2063  (negate (rdiv@0 @1 negate_expr_p@2))
2064  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2065       && single_use (@0))
2066   (rdiv @1 (negate @2))))
2068 (simplify
2069  (negate (rdiv@0 negate_expr_p@1 @2))
2070  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2071       && single_use (@0))
2072   (rdiv (negate @1) @2)))
2074 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2075 (simplify
2076  (negate (convert? (rshift @0 INTEGER_CST@1)))
2077  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2078       && wi::to_wide (@1) == element_precision (type) - 1)
2079   (with { tree stype = TREE_TYPE (@0);
2080           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2081                                              : unsigned_type_for (stype); }
2082    (if (VECTOR_TYPE_P (type))
2083     (view_convert (rshift (view_convert:ntype @0) @1))
2084     (convert (rshift (convert:ntype @0) @1))))))
2086 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2087    when profitable.
2088    For bitwise binary operations apply operand conversions to the
2089    binary operation result instead of to the operands.  This allows
2090    to combine successive conversions and bitwise binary operations.
2091    We combine the above two cases by using a conditional convert.  */
2092 (for bitop (bit_and bit_ior bit_xor)
2093  (simplify
2094   (bitop (convert@2 @0) (convert?@3 @1))
2095   (if (((TREE_CODE (@1) == INTEGER_CST
2096          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2097          && (int_fits_type_p (@1, TREE_TYPE (@0))
2098              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2099         || types_match (@0, @1))
2100        && !POINTER_TYPE_P (TREE_TYPE (@0))
2101        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2102        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2103        /* ???  This transform conflicts with fold-const.cc doing
2104           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2105           constants (if x has signed type, the sign bit cannot be set
2106           in c).  This folds extension into the BIT_AND_EXPR.
2107           Restrict it to GIMPLE to avoid endless recursions.  */
2108        && (bitop != BIT_AND_EXPR || GIMPLE)
2109        && (/* That's a good idea if the conversion widens the operand, thus
2110               after hoisting the conversion the operation will be narrower.
2111               It is also a good if the conversion is a nop as moves the
2112               conversion to one side; allowing for combining of the conversions.  */
2113            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2114            /* The conversion check for being a nop can only be done at the gimple
2115               level as fold_binary has some re-association code which can conflict
2116               with this if there is a "constant" which is not a full INTEGER_CST.  */
2117            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2118            /* It's also a good idea if the conversion is to a non-integer
2119               mode.  */
2120            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2121            /* Or if the precision of TO is not the same as the precision
2122               of its mode.  */
2123            || !type_has_mode_precision_p (type)
2124            /* In GIMPLE, getting rid of 2 conversions for one new results
2125               in smaller IL.  */
2126            || (GIMPLE
2127                && TREE_CODE (@1) != INTEGER_CST
2128                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2129                && single_use (@2)
2130                && single_use (@3))))
2131    (convert (bitop @0 (convert @1)))))
2132  /* In GIMPLE, getting rid of 2 conversions for one new results
2133     in smaller IL.  */
2134  (simplify
2135   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2136   (if (GIMPLE
2137        && TREE_CODE (@1) != INTEGER_CST
2138        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2139        && types_match (type, @0)
2140        && !POINTER_TYPE_P (TREE_TYPE (@0))
2141        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2142    (bitop @0 (convert @1)))))
2144 (for bitop (bit_and bit_ior)
2145      rbitop (bit_ior bit_and)
2146   /* (x | y) & x -> x */
2147   /* (x & y) | x -> x */
2148  (simplify
2149   (bitop:c (rbitop:c @0 @1) @0)
2150   @0)
2151  /* (~x | y) & x -> x & y */
2152  /* (~x & y) | x -> x | y */
2153  (simplify
2154   (bitop:c (rbitop:c @2 @1) @0)
2155   (with { bool wascmp; }
2156    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2157         && (!wascmp || element_precision (type) == 1))
2158     (bitop @0 @1))))
2159   /* (x | y) & (x & z) -> (x & z) */
2160   /* (x & y) | (x | z) -> (x | z) */
2161  (simplify
2162   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2163   @3)
2164  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2165  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2166  (simplify
2167   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2168   (bitop @0 @3))
2169  /* x & ~(y | x) -> 0 */
2170  /* x | ~(y & x) -> -1 */
2171  (simplify
2172   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2173   (if (bitop == BIT_AND_EXPR)
2174    { build_zero_cst (type); }
2175    { build_minus_one_cst (type); })))
2177 /* ((x | y) & z) | x -> (z & y) | x
2178    ((x ^ y) & z) | x -> (z & y) | x  */
2179 (for op (bit_ior bit_xor)
2180  (simplify
2181   (bit_ior:c (nop_convert1?:s
2182                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2183   (if (bitwise_equal_p (@0, @3))
2184    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2186 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2187 (simplify
2188   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2189   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2191 /* Combine successive equal operations with constants.  */
2192 (for bitop (bit_and bit_ior bit_xor)
2193  (simplify
2194   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2195   (if (!CONSTANT_CLASS_P (@0))
2196    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2197       folded to a constant.  */
2198    (bitop @0 (bitop! @1 @2))
2199    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2200       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2201       the values involved are such that the operation can't be decided at
2202       compile time.  Try folding one of @0 or @1 with @2 to see whether
2203       that combination can be decided at compile time.
2205       Keep the existing form if both folds fail, to avoid endless
2206       oscillation.  */
2207    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2208     (if (cst1)
2209      (bitop @1 { cst1; })
2210      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2211       (if (cst2)
2212        (bitop @0 { cst2; }))))))))
2214 /* Try simple folding for X op !X, and X op X with the help
2215    of the truth_valued_p and logical_inverted_value predicates.  */
2216 (match truth_valued_p
2217  @0
2218  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2219 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2220  (match truth_valued_p
2221   (op @0 @1)))
2222 (match truth_valued_p
2223   (truth_not @0))
2225 (match (logical_inverted_value @0)
2226  (truth_not @0))
2227 (match (logical_inverted_value @0)
2228  (bit_not truth_valued_p@0))
2229 (match (logical_inverted_value @0)
2230  (eq @0 integer_zerop))
2231 (match (logical_inverted_value @0)
2232  (ne truth_valued_p@0 integer_truep))
2233 (match (logical_inverted_value @0)
2234  (bit_xor truth_valued_p@0 integer_truep))
2236 /* X & !X -> 0.  */
2237 (simplify
2238  (bit_and:c @0 (logical_inverted_value @0))
2239  { build_zero_cst (type); })
2240 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2241 (for op (bit_ior bit_xor)
2242  (simplify
2243   (op:c truth_valued_p@0 (logical_inverted_value @0))
2244   { constant_boolean_node (true, type); }))
2245 /* X ==/!= !X is false/true.  */
2246 (for op (eq ne)
2247  (simplify
2248   (op:c truth_valued_p@0 (logical_inverted_value @0))
2249   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2251 /* ~~x -> x */
2252 (simplify
2253   (bit_not (bit_not @0))
2254   @0)
2256 /* zero_one_valued_p will match when a value is known to be either
2257    0 or 1 including constants 0 or 1.
2258    Signed 1-bits includes -1 so they cannot match here. */
2259 (match zero_one_valued_p
2260  @0
2261  (if (INTEGRAL_TYPE_P (type)
2262       && (TYPE_UNSIGNED (type)
2263           || TYPE_PRECISION (type) > 1)
2264       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2265 (match zero_one_valued_p
2266  truth_valued_p@0
2267  (if (INTEGRAL_TYPE_P (type)
2268       && (TYPE_UNSIGNED (type)
2269           || TYPE_PRECISION (type) > 1))))
2271 /* (a&1) is always [0,1] too. This is useful again when
2272    the range is not known. */
2273 /* Note this can't be recursive due to VN handling of equivalents,
2274    VN and would cause an infinite recursion. */
2275 (match zero_one_valued_p
2276  (bit_and:c@0 @1 integer_onep)
2277  (if (INTEGRAL_TYPE_P (type))))
2279 /* A conversion from an zero_one_valued_p is still a [0,1].
2280    This is useful when the range of a variable is not known */
2281 /* Note this matches can't be recursive because of the way VN handles
2282    nop conversions being equivalent and then recursive between them. */
2283 (match zero_one_valued_p
2284  (convert@0 @1)
2285  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2286       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2287           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2288       && INTEGRAL_TYPE_P (type)
2289       && (TYPE_UNSIGNED (type)
2290           || TYPE_PRECISION (type) > 1)
2291       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2293 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2294 (simplify
2295  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2296  (if (INTEGRAL_TYPE_P (type))
2297   (bit_and @0 @1)))
2299 (for cmp (tcc_comparison)
2300      icmp (inverted_tcc_comparison)
2301  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2302  (simplify
2303   (bit_ior
2304    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2305    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2306     (if (INTEGRAL_TYPE_P (type)
2307          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2308          /* The scalar version has to be canonicalized after vectorization
2309             because it makes unconditional loads conditional ones, which
2310             means we lose vectorization because the loads may trap.  */
2311          && canonicalize_math_after_vectorization_p ())
2312      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2314  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2315     canonicalized further and we recognize the conditional form:
2316     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2317  (simplify
2318   (bit_ior
2319    (cond (cmp@0  @01 @02) @3 zerop)
2320    (cond (icmp@4 @01 @02) @5 zerop))
2321     (if (INTEGRAL_TYPE_P (type)
2322          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2323          /* The scalar version has to be canonicalized after vectorization
2324             because it makes unconditional loads conditional ones, which
2325             means we lose vectorization because the loads may trap.  */
2326          && canonicalize_math_after_vectorization_p ())
2327     (cond @0 @3 @5)))
2329  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2330     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2331  (simplify
2332   (bit_ior
2333    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2334    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2335     (if (integer_zerop (@5)
2336          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2337      (switch
2338       (if (integer_onep (@4))
2339        (bit_and (vec_cond @0 @2 @3) @4))
2340         (if (integer_minus_onep (@4))
2341          (vec_cond @0 @2 @3)))
2342     (if (integer_zerop (@4)
2343          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2344      (switch
2345       (if (integer_onep (@5))
2346        (bit_and (vec_cond @0 @3 @2) @5))
2347       (if (integer_minus_onep (@5))
2348        (vec_cond @0 @3 @2))))))
2350  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2351     into a < b ? d : c.  */
2352  (simplify
2353   (bit_ior
2354    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2355    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2356   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2357    (vec_cond @0 @2 @3))))
2359 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2360 (simplify
2361  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2362  (if (INTEGRAL_TYPE_P (type)
2363       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2364       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2365       /* Sign extending of the neg or a truncation of the neg
2366          is needed. */
2367       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2368           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2369   (mult (convert @0) @1)))
2371 /* Narrow integer multiplication by a zero_one_valued_p operand.
2372    Multiplication by [0,1] is guaranteed not to overflow.  */
2373 (simplify
2374  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2375  (if (INTEGRAL_TYPE_P (type)
2376       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2377       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2378   (mult (convert @1) (convert @2))))
2380 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2381    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2382    as some targets (such as x86's SSE) may return zero for larger C.  */
2383 (simplify
2384   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2385   (if (tree_fits_shwi_p (@1)
2386        && tree_to_shwi (@1) > 0
2387        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2388     (convert @0)))
2390 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2391    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2392    as some targets (such as x86's SSE) may return zero for larger C.  */
2393 (simplify
2394   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2395   (if (tree_fits_shwi_p (@1)
2396        && tree_to_shwi (@1) > 0
2397        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2398     (eq @0 @2)))
2400 /* Convert ~ (-A) to A - 1.  */
2401 (simplify
2402  (bit_not (convert? (negate @0)))
2403  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2404       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2405   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2407 /* Convert - (~A) to A + 1.  */
2408 (simplify
2409  (negate (nop_convert? (bit_not @0)))
2410  (plus (view_convert @0) { build_each_one_cst (type); }))
2412 /* (a & b) ^ (a == b) -> !(a | b) */
2413 /* (a & b) == (a ^ b) -> !(a | b) */
2414 (for first_op (bit_xor eq)
2415      second_op (eq bit_xor)
2416  (simplify
2417   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2418     (bit_not (bit_ior @0 @1))))
2420 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2421 (simplify
2422  (bit_not (convert? (minus @0 integer_each_onep)))
2423  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2424       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2425   (convert (negate @0))))
2426 (simplify
2427  (bit_not (convert? (plus @0 integer_all_onesp)))
2428  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2429       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2430   (convert (negate @0))))
2432 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2433 (simplify
2434  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2435  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2436   (convert (bit_xor @0 (bit_not @1)))))
2437 (simplify
2438  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2439  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2440   (convert (bit_xor @0 @1))))
2442 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2443 (simplify
2444  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2445  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2446   (bit_not (bit_xor (view_convert @0) @1))))
2448 /* ~(a ^ b) is a == b for truth valued a and b.  */
2449 (simplify
2450  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2451  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2452       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2453   (convert (eq @0 @1))))
2455 /* (~a) == b is a ^ b for truth valued a and b.  */
2456 (simplify
2457  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2458  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2459       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2460   (convert (bit_xor @0 @1))))
2462 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2463 (simplify
2464  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2465  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2467 /* Fold A - (A & B) into ~B & A.  */
2468 (simplify
2469  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2470  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2471       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2472   (convert (bit_and (bit_not @1) @0))))
2474 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2475 (if (!canonicalize_math_p ())
2476  (for cmp (tcc_comparison)
2477   (simplify
2478    (mult:c (convert (cmp@0 @1 @2)) @3)
2479    (if (INTEGRAL_TYPE_P (type)
2480         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2481      (cond @0 @3 { build_zero_cst (type); })))
2482 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2483   (simplify
2484    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2485    (if (INTEGRAL_TYPE_P (type)
2486         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2487      (cond @0 @3 { build_zero_cst (type); })))
2491 /* For integral types with undefined overflow and C != 0 fold
2492    x * C EQ/NE y * C into x EQ/NE y.  */
2493 (for cmp (eq ne)
2494  (simplify
2495   (cmp (mult:c @0 @1) (mult:c @2 @1))
2496   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2497        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2498        && tree_expr_nonzero_p (@1))
2499    (cmp @0 @2))))
2501 /* For integral types with wrapping overflow and C odd fold
2502    x * C EQ/NE y * C into x EQ/NE y.  */
2503 (for cmp (eq ne)
2504  (simplify
2505   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2506   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2507        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2508        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2509    (cmp @0 @2))))
2511 /* For integral types with undefined overflow and C != 0 fold
2512    x * C RELOP y * C into:
2514    x RELOP y for nonnegative C
2515    y RELOP x for negative C  */
2516 (for cmp (lt gt le ge)
2517  (simplify
2518   (cmp (mult:c @0 @1) (mult:c @2 @1))
2519   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2520        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2521    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2522     (cmp @0 @2)
2523    (if (TREE_CODE (@1) == INTEGER_CST
2524         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2525     (cmp @2 @0))))))
2527 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2528 (for cmp (le gt)
2529      icmp (gt le)
2530  (simplify
2531   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2532    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2533         && TYPE_UNSIGNED (TREE_TYPE (@0))
2534         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2535         && (wi::to_wide (@2)
2536             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2537     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2538      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2540 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2541 (for cmp (simple_comparison)
2542  (simplify
2543   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2544   (if (element_precision (@3) >= element_precision (@0)
2545        && types_match (@0, @1))
2546    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2547     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2548      (cmp @1 @0)
2549      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2550       (with
2551        {
2552         tree utype = unsigned_type_for (TREE_TYPE (@0));
2553        }
2554        (cmp (convert:utype @1) (convert:utype @0)))))
2555     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2556      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2557       (cmp @0 @1)
2558       (with
2559        {
2560         tree utype = unsigned_type_for (TREE_TYPE (@0));
2561        }
2562        (cmp (convert:utype @0) (convert:utype @1)))))))))
2564 /* X / C1 op C2 into a simple range test.  */
2565 (for cmp (simple_comparison)
2566  (simplify
2567   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2568   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2569        && integer_nonzerop (@1)
2570        && !TREE_OVERFLOW (@1)
2571        && !TREE_OVERFLOW (@2))
2572    (with { tree lo, hi; bool neg_overflow;
2573            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2574                                                    &neg_overflow); }
2575     (switch
2576      (if (code == LT_EXPR || code == GE_EXPR)
2577        (if (TREE_OVERFLOW (lo))
2578         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2579         (if (code == LT_EXPR)
2580          (lt @0 { lo; })
2581          (ge @0 { lo; }))))
2582      (if (code == LE_EXPR || code == GT_EXPR)
2583        (if (TREE_OVERFLOW (hi))
2584         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2585         (if (code == LE_EXPR)
2586          (le @0 { hi; })
2587          (gt @0 { hi; }))))
2588      (if (!lo && !hi)
2589       { build_int_cst (type, code == NE_EXPR); })
2590      (if (code == EQ_EXPR && !hi)
2591       (ge @0 { lo; }))
2592      (if (code == EQ_EXPR && !lo)
2593       (le @0 { hi; }))
2594      (if (code == NE_EXPR && !hi)
2595       (lt @0 { lo; }))
2596      (if (code == NE_EXPR && !lo)
2597       (gt @0 { hi; }))
2598      (if (GENERIC)
2599       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2600                            lo, hi); })
2601      (with
2602       {
2603         tree etype = range_check_type (TREE_TYPE (@0));
2604         if (etype)
2605           {
2606             hi = fold_convert (etype, hi);
2607             lo = fold_convert (etype, lo);
2608             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2609           }
2610       }
2611       (if (etype && hi && !TREE_OVERFLOW (hi))
2612        (if (code == EQ_EXPR)
2613         (le (minus (convert:etype @0) { lo; }) { hi; })
2614         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2616 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2617 (for op (lt le ge gt)
2618  (simplify
2619   (op (plus:c @0 @2) (plus:c @1 @2))
2620   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2621        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2622    (op @0 @1))))
2624 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2625    when C is an unsigned integer constant with only the MSB set, and X and
2626    Y have types of equal or lower integer conversion rank than C's.  */
2627 (for op (lt le ge gt)
2628  (simplify
2629   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2630   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2631        && TYPE_UNSIGNED (TREE_TYPE (@0))
2632        && wi::only_sign_bit_p (wi::to_wide (@0)))
2633    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2634     (op (convert:stype @1) (convert:stype @2))))))
2636 /* For equality and subtraction, this is also true with wrapping overflow.  */
2637 (for op (eq ne minus)
2638  (simplify
2639   (op (plus:c @0 @2) (plus:c @1 @2))
2640   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2641        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2642            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2643    (op @0 @1))))
2644 /* And similar for pointers.  */
2645 (for op (eq ne)
2646  (simplify
2647   (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2648   (op @1 @2)))
2649 (simplify
2650  (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2651  (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2652   (convert (minus @1 @2))))
2654 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2655 (for op (lt le ge gt)
2656  (simplify
2657   (op (minus @0 @2) (minus @1 @2))
2658   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2659        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2660    (op @0 @1))))
2661 /* For equality and subtraction, this is also true with wrapping overflow.  */
2662 (for op (eq ne minus)
2663  (simplify
2664   (op (minus @0 @2) (minus @1 @2))
2665   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2666        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2667            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2668    (op @0 @1))))
2669 /* And for pointers...  */
2670 (for op (simple_comparison)
2671  (simplify
2672   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2673   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2674    (op @0 @1))))
2675 (simplify
2676  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2677  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2678       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2679   (pointer_diff @0 @1)))
2681 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2682 (for op (lt le ge gt)
2683  (simplify
2684   (op (minus @2 @0) (minus @2 @1))
2685   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2686        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2687    (op @1 @0))))
2688 /* For equality and subtraction, this is also true with wrapping overflow.  */
2689 (for op (eq ne minus)
2690  (simplify
2691   (op (minus @2 @0) (minus @2 @1))
2692   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2693        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2694            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2695    (op @1 @0))))
2696 /* And for pointers...  */
2697 (for op (simple_comparison)
2698  (simplify
2699   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2700   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2701    (op @1 @0))))
2702 (simplify
2703  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2704  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2705       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2706   (pointer_diff @1 @0)))
2708 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2709 (for op (lt le gt ge)
2710  (simplify
2711   (op:c (plus:c@2 @0 @1) @1)
2712   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2713        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2714        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2715        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2716    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2717 /* For equality, this is also true with wrapping overflow.  */
2718 (for op (eq ne)
2719  (simplify
2720   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2721   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2722        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2723            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2724        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2725        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2726        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2727    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2728  (simplify
2729   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2730   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2731        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2732        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2733    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2735 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2736 (for neeq (ne eq)
2737  (simplify
2738   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2739    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2740     (if (ptr_difference_const (@0, @2, &diff))
2741      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2742  (simplify
2743   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2744    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2745     (if (ptr_difference_const (@0, @2, &diff))
2746      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2748 /* X - Y < X is the same as Y > 0 when there is no overflow.
2749    For equality, this is also true with wrapping overflow.  */
2750 (for op (simple_comparison)
2751  (simplify
2752   (op:c @0 (minus@2 @0 @1))
2753   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2754        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2755            || ((op == EQ_EXPR || op == NE_EXPR)
2756                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2757        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2758    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2760 /* Transform:
2761    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2762    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2763 (for cmp (eq ne)
2764      ocmp (lt ge)
2765  (simplify
2766   (cmp (trunc_div @0 @1) integer_zerop)
2767   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2768        /* Complex ==/!= is allowed, but not </>=.  */
2769        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2770        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2771    (ocmp @0 @1))))
2773 /* X == C - X can never be true if C is odd.  */
2774 (for cmp (eq ne)
2775  (simplify
2776   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2777   (if (TREE_INT_CST_LOW (@1) & 1)
2778    { constant_boolean_node (cmp == NE_EXPR, type); })))
2781    U & N <= U  -> true
2782    U & N >  U  -> false
2783    U needs to be non-negative.
2785    U | N <  U  -> false
2786    U | N >= U  -> true
2787    U and N needs to be non-negative
2789    U | N <  U  -> true
2790    U | N >= U  -> false
2791    U needs to be non-negative and N needs to be a negative constant.
2792    */
2793 (for cmp   (lt      ge      le      gt     )
2794      bitop (bit_ior bit_ior bit_and bit_and)
2795  (simplify
2796   (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2797   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2798    (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2799     { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2800     /* The sign is opposite now so the comparison is swapped around. */
2801     (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2802      { constant_boolean_node (cmp == LT_EXPR, type); })))))
2804 /* Arguments on which one can call get_nonzero_bits to get the bits
2805    possibly set.  */
2806 (match with_possible_nonzero_bits
2807  INTEGER_CST@0)
2808 (match with_possible_nonzero_bits
2809  SSA_NAME@0
2810  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2811 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2812 (match (with_possible_nonzero_bits2 @0)
2813  with_possible_nonzero_bits@0)
2814 (match (with_possible_nonzero_bits2 @0)
2815  (bit_and:c with_possible_nonzero_bits@0 @2))
2817 /* Same for bits that are known to be set, but we do not have
2818    an equivalent to get_nonzero_bits yet.  */
2819 (match (with_certain_nonzero_bits2 @0)
2820  INTEGER_CST@0)
2821 (match (with_certain_nonzero_bits2 @0)
2822  (bit_ior @1 INTEGER_CST@0))
2824 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2825 (for cmp (eq ne)
2826  (simplify
2827   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2828   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2829    { constant_boolean_node (cmp == NE_EXPR, type); })))
2831 /* ((X inner_op C0) outer_op C1)
2832    With X being a tree where value_range has reasoned certain bits to always be
2833    zero throughout its computed value range,
2834    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2835    where zero_mask has 1's for all bits that are sure to be 0 in
2836    and 0's otherwise.
2837    if (inner_op == '^') C0 &= ~C1;
2838    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2839    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2841 (for inner_op (bit_ior bit_xor)
2842      outer_op (bit_xor bit_ior)
2843 (simplify
2844  (outer_op
2845   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2846  (with
2847   {
2848     bool fail = false;
2849     wide_int zero_mask_not;
2850     wide_int C0;
2851     wide_int cst_emit;
2853     if (TREE_CODE (@2) == SSA_NAME)
2854       zero_mask_not = get_nonzero_bits (@2);
2855     else
2856       fail = true;
2858     if (inner_op == BIT_XOR_EXPR)
2859       {
2860         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2861         cst_emit = C0 | wi::to_wide (@1);
2862       }
2863     else
2864       {
2865         C0 = wi::to_wide (@0);
2866         cst_emit = C0 ^ wi::to_wide (@1);
2867       }
2868   }
2869   (if (!fail && (C0 & zero_mask_not) == 0)
2870    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2871    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2872     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2874 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2875 (simplify
2876   (pointer_plus (pointer_plus:s @0 @1) @3)
2877   (pointer_plus @0 (plus @1 @3)))
2878 #if GENERIC
2879 (simplify
2880   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2881   (convert:type (pointer_plus @0 (plus @1 @3))))
2882 #endif
2884 /* Pattern match
2885      tem1 = (long) ptr1;
2886      tem2 = (long) ptr2;
2887      tem3 = tem2 - tem1;
2888      tem4 = (unsigned long) tem3;
2889      tem5 = ptr1 + tem4;
2890    and produce
2891      tem5 = ptr2;  */
2892 (simplify
2893   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2894   /* Conditionally look through a sign-changing conversion.  */
2895   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2896        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2897             || (GENERIC && type == TREE_TYPE (@1))))
2898    @1))
2899 (simplify
2900   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2901   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2902    (convert @1)))
2904 /* Pattern match
2905      tem = (sizetype) ptr;
2906      tem = tem & algn;
2907      tem = -tem;
2908      ... = ptr p+ tem;
2909    and produce the simpler and easier to analyze with respect to alignment
2910      ... = ptr & ~algn;  */
2911 (simplify
2912   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2913   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2914    (bit_and @0 { algn; })))
2916 /* Try folding difference of addresses.  */
2917 (simplify
2918  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2919  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2920   (with { poly_int64 diff; }
2921    (if (ptr_difference_const (@0, @1, &diff))
2922     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2923 (simplify
2924  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2925  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2926   (with { poly_int64 diff; }
2927    (if (ptr_difference_const (@0, @1, &diff))
2928     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2929 (simplify
2930  (minus (convert ADDR_EXPR@0) (convert @1))
2931  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2932   (with { poly_int64 diff; }
2933    (if (ptr_difference_const (@0, @1, &diff))
2934     { build_int_cst_type (type, diff); }))))
2935 (simplify
2936  (minus (convert @0) (convert ADDR_EXPR@1))
2937  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2938   (with { poly_int64 diff; }
2939    (if (ptr_difference_const (@0, @1, &diff))
2940     { build_int_cst_type (type, diff); }))))
2941 (simplify
2942  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2943  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2944       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2945   (with { poly_int64 diff; }
2946    (if (ptr_difference_const (@0, @1, &diff))
2947     { build_int_cst_type (type, diff); }))))
2948 (simplify
2949  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2950  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2951       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2952   (with { poly_int64 diff; }
2953    (if (ptr_difference_const (@0, @1, &diff))
2954     { build_int_cst_type (type, diff); }))))
2956 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2957 (simplify
2958  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2959  (with { poly_int64 diff; }
2960    (if (ptr_difference_const (@0, @2, &diff))
2961     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2962 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2963 (simplify
2964  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2965  (with { poly_int64 diff; }
2966    (if (ptr_difference_const (@0, @2, &diff))
2967     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2968 (simplify
2969  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2970  (with { poly_int64 diff; }
2971    (if (ptr_difference_const (@0, @1, &diff))
2972     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2974 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2975 (simplify
2976  (convert (pointer_diff @0 INTEGER_CST@1))
2977  (if (POINTER_TYPE_P (type))
2978   { build_fold_addr_expr_with_type
2979       (build2 (MEM_REF, char_type_node, @0,
2980                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2981                type); }))
2983 /* If arg0 is derived from the address of an object or function, we may
2984    be able to fold this expression using the object or function's
2985    alignment.  */
2986 (simplify
2987  (bit_and (convert? @0) INTEGER_CST@1)
2988  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2989       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2990   (with
2991    {
2992      unsigned int align;
2993      unsigned HOST_WIDE_INT bitpos;
2994      get_pointer_alignment_1 (@0, &align, &bitpos);
2995    }
2996    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2997     { wide_int_to_tree (type, (wi::to_wide (@1)
2998                                & (bitpos / BITS_PER_UNIT))); }))))
3000 (match min_value
3001  uniform_integer_cst_p
3002  (with {
3003    tree int_cst = uniform_integer_cst_p (t);
3004    tree inner_type = TREE_TYPE (int_cst);
3005   }
3006   (if ((INTEGRAL_TYPE_P (inner_type)
3007         || POINTER_TYPE_P (inner_type))
3008        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3010 (match max_value
3011  uniform_integer_cst_p
3012  (with {
3013    tree int_cst = uniform_integer_cst_p (t);
3014    tree itype = TREE_TYPE (int_cst);
3015   }
3016  (if ((INTEGRAL_TYPE_P (itype)
3017        || POINTER_TYPE_P (itype))
3018       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3020 /* x >  y  &&  x != XXX_MIN  -->  x > y
3021    x >  y  &&  x == XXX_MIN  -->  false . */
3022 (for eqne (eq ne)
3023  (simplify
3024   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3025    (switch
3026     (if (eqne == EQ_EXPR)
3027      { constant_boolean_node (false, type); })
3028     (if (eqne == NE_EXPR)
3029      @2)
3030     )))
3032 /* x <  y  &&  x != XXX_MAX  -->  x < y
3033    x <  y  &&  x == XXX_MAX  -->  false.  */
3034 (for eqne (eq ne)
3035  (simplify
3036   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3037    (switch
3038     (if (eqne == EQ_EXPR)
3039      { constant_boolean_node (false, type); })
3040     (if (eqne == NE_EXPR)
3041      @2)
3042     )))
3044 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
3045 (simplify
3046  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3047   @2)
3049 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
3050 (simplify
3051  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3052   @2)
3054 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
3055 (simplify
3056  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3057   @2)
3059 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
3060 (simplify
3061  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3062   { constant_boolean_node (true, type); })
3064 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
3065 (simplify
3066  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3067   @2)
3069 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
3070 (simplify
3071  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3072   @2)
3074 /* x >=  y  ||  x != XXX_MAX   -->  true
3075    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
3076 (for eqne (eq ne)
3077  (simplify
3078   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3079    (switch
3080     (if (eqne == EQ_EXPR)
3081      @2)
3082     (if (eqne == NE_EXPR)
3083      { constant_boolean_node (true, type); }))))
3085 /* y == XXX_MIN || x < y --> x <= y - 1 */
3086 (simplify
3087  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3088   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3089        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3090   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3092 /* y != XXX_MIN && x >= y --> x > y - 1 */
3093 (simplify
3094  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3095   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3096        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3097   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3099 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3100    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3101 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3102    Similarly for (X != Y).  */
3104 (for code1 (eq ne)
3105  (for code2 (eq ne lt gt le ge)
3106   (simplify
3107    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3108    (if ((TREE_CODE (@1) == INTEGER_CST
3109          && TREE_CODE (@2) == INTEGER_CST)
3110         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3111              || POINTER_TYPE_P (TREE_TYPE (@1)))
3112             && bitwise_equal_p (@1, @2)))
3113     (with
3114      {
3115       bool one_before = false;
3116       bool one_after = false;
3117       int cmp = 0;
3118       bool allbits = true;
3119       if (TREE_CODE (@1) == INTEGER_CST
3120           && TREE_CODE (@2) == INTEGER_CST)
3121         {
3122           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3123           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3124           auto t2 = wi::to_wide (@2);
3125           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3126           if (cmp < 0
3127               && t1 == t2 - 1)
3128             one_before = true;
3129           if (cmp > 0
3130               && t1 == t2 + 1)
3131             one_after = true;
3132         }
3133       bool val;
3134       switch (code2)
3135          {
3136         case EQ_EXPR: val = (cmp == 0); break;
3137         case NE_EXPR: val = (cmp != 0); break;
3138         case LT_EXPR: val = (cmp < 0); break;
3139         case GT_EXPR: val = (cmp > 0); break;
3140         case LE_EXPR: val = (cmp <= 0); break;
3141         case GE_EXPR: val = (cmp >= 0); break;
3142         default: gcc_unreachable ();
3143         }
3144      }
3145      (switch
3146       (if (code1 == EQ_EXPR && val) @3)
3147       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3148       (if (code1 == NE_EXPR && !val && allbits) @4)
3149       (if (code1 == NE_EXPR
3150            && code2 == GE_EXPR
3151            && cmp == 0
3152            && allbits)
3153        (gt @c0 (convert @1)))
3154       (if (code1 == NE_EXPR
3155            && code2 == LE_EXPR
3156            && cmp == 0
3157            && allbits)
3158        (lt @c0 (convert @1)))
3159       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3160       (if (code1 == NE_EXPR
3161            && code2 == GT_EXPR
3162            && one_after
3163            && allbits)
3164        (gt @c0 (convert @1)))
3165       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3166       (if (code1 == NE_EXPR
3167            && code2 == LT_EXPR
3168            && one_before
3169            && allbits)
3170        (lt @c0 (convert @1)))
3171      )
3172     )
3173    )
3174   )
3178 /* Convert (X OP1 CST1) && (X OP2 CST2).
3179    Convert (X OP1 Y) && (X OP2 Y).  */
3181 (for code1 (lt le gt ge)
3182  (for code2 (lt le gt ge)
3183   (simplify
3184   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3185   (if ((TREE_CODE (@1) == INTEGER_CST
3186         && TREE_CODE (@2) == INTEGER_CST)
3187        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3188             || POINTER_TYPE_P (TREE_TYPE (@1)))
3189            && operand_equal_p (@1, @2)))
3190    (with
3191     {
3192      int cmp = 0;
3193      if (TREE_CODE (@1) == INTEGER_CST
3194          && TREE_CODE (@2) == INTEGER_CST)
3195        cmp = tree_int_cst_compare (@1, @2);
3196     }
3197     (switch
3198      /* Choose the more restrictive of two < or <= comparisons.  */
3199      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3200           && (code2 == LT_EXPR || code2 == LE_EXPR))
3201       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3202        @3
3203        @4))
3204      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3205      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3206           && (code2 == GT_EXPR || code2 == GE_EXPR))
3207       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3208        @3
3209        @4))
3210      /* Check for singleton ranges.  */
3211      (if (cmp == 0
3212           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3213             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3214       (eq @0 @1))
3215      /* Check for disjoint ranges.  */
3216      (if (cmp <= 0
3217           && (code1 == LT_EXPR || code1 == LE_EXPR)
3218           && (code2 == GT_EXPR || code2 == GE_EXPR))
3219       { constant_boolean_node (false, type); })
3220      (if (cmp >= 0
3221           && (code1 == GT_EXPR || code1 == GE_EXPR)
3222           && (code2 == LT_EXPR || code2 == LE_EXPR))
3223       { constant_boolean_node (false, type); })
3224      ))))))
3226 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3227    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3228 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3229    Similarly for (X != Y).  */
3231 (for code1 (eq ne)
3232  (for code2 (eq ne lt gt le ge)
3233   (simplify
3234    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3235    (if ((TREE_CODE (@1) == INTEGER_CST
3236          && TREE_CODE (@2) == INTEGER_CST)
3237         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3238             || POINTER_TYPE_P (TREE_TYPE (@1)))
3239             && bitwise_equal_p (@1, @2)))
3240     (with
3241      {
3242       bool one_before = false;
3243       bool one_after = false;
3244       int cmp = 0;
3245       bool allbits = true;
3246       if (TREE_CODE (@1) == INTEGER_CST
3247           && TREE_CODE (@2) == INTEGER_CST)
3248         {
3249           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3250           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3251           auto t2 = wi::to_wide (@2);
3252           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3253           if (cmp < 0
3254               && t1 == t2 - 1)
3255             one_before = true;
3256           if (cmp > 0
3257               && t1 == t2 + 1)
3258             one_after = true;
3259         }
3260       bool val;
3261       switch (code2)
3262         {
3263         case EQ_EXPR: val = (cmp == 0); break;
3264         case NE_EXPR: val = (cmp != 0); break;
3265         case LT_EXPR: val = (cmp < 0); break;
3266         case GT_EXPR: val = (cmp > 0); break;
3267         case LE_EXPR: val = (cmp <= 0); break;
3268         case GE_EXPR: val = (cmp >= 0); break;
3269         default: gcc_unreachable ();
3270         }
3271      }
3272      (switch
3273       (if (code1 == EQ_EXPR && val) @4)
3274       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3275       (if (code1 == NE_EXPR && !val && allbits) @3)
3276       (if (code1 == EQ_EXPR
3277            && code2 == GT_EXPR
3278            && cmp == 0
3279            && allbits)
3280        (ge @c0 @2))
3281       (if (code1 == EQ_EXPR
3282            && code2 == LT_EXPR
3283            && cmp == 0
3284            && allbits)
3285        (le @c0 @2))
3286       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3287       (if (code1 == EQ_EXPR
3288            && code2 == GE_EXPR
3289            && one_before
3290            && allbits)
3291        (ge @c0 (convert @1)))
3292       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3293       (if (code1 == EQ_EXPR
3294            && code2 == LE_EXPR
3295            && one_after
3296            && allbits)
3297        (le @c0 (convert @1)))
3298      )
3299     )
3300    )
3301   )
3305 /* Convert (X OP1 CST1) || (X OP2 CST2).
3306    Convert (X OP1 Y)    || (X OP2 Y).  */
3308 (for code1 (lt le gt ge)
3309  (for code2 (lt le gt ge)
3310   (simplify
3311   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3312   (if ((TREE_CODE (@1) == INTEGER_CST
3313         && TREE_CODE (@2) == INTEGER_CST)
3314        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3315             || POINTER_TYPE_P (TREE_TYPE (@1)))
3316            && operand_equal_p (@1, @2)))
3317    (with
3318     {
3319      int cmp = 0;
3320      if (TREE_CODE (@1) == INTEGER_CST
3321          && TREE_CODE (@2) == INTEGER_CST)
3322        cmp = tree_int_cst_compare (@1, @2);
3323     }
3324     (switch
3325      /* Choose the more restrictive of two < or <= comparisons.  */
3326      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3327           && (code2 == LT_EXPR || code2 == LE_EXPR))
3328       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3329        @4
3330        @3))
3331      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3332      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3333           && (code2 == GT_EXPR || code2 == GE_EXPR))
3334       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3335        @4
3336        @3))
3337      /* Check for singleton ranges.  */
3338      (if (cmp == 0
3339           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3340               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3341       (ne @0 @2))
3342      /* Check for disjoint ranges.  */
3343      (if (cmp >= 0
3344           && (code1 == LT_EXPR || code1 == LE_EXPR)
3345           && (code2 == GT_EXPR || code2 == GE_EXPR))
3346       { constant_boolean_node (true, type); })
3347      (if (cmp <= 0
3348           && (code1 == GT_EXPR || code1 == GE_EXPR)
3349           && (code2 == LT_EXPR || code2 == LE_EXPR))
3350       { constant_boolean_node (true, type); })
3351      ))))))
3353 /* Optimize (a CMP b) ^ (a CMP b)  */
3354 /* Optimize (a CMP b) != (a CMP b)  */
3355 (for op (bit_xor ne)
3356  (for cmp1 (lt lt lt le le le)
3357       cmp2 (gt eq ne ge eq ne)
3358       rcmp (ne le gt ne lt ge)
3359   (simplify
3360    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3361    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3362     (rcmp @0 @1)))))
3364 /* Optimize (a CMP b) == (a CMP b)  */
3365 (for cmp1 (lt lt lt le le le)
3366      cmp2 (gt eq ne ge eq ne)
3367      rcmp (eq gt le eq ge lt)
3368  (simplify
3369   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3370   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3371     (rcmp @0 @1))))
3373 /* (type)([0,1]@a != 0) -> (type)a
3374    (type)([0,1]@a == 1) -> (type)a
3375    (type)([0,1]@a == 0) -> a ^ 1
3376    (type)([0,1]@a != 1) -> a ^ 1.  */
3377 (for eqne (eq ne)
3378  (simplify
3379   (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3380   (if ((integer_zerop (@1) || integer_onep (@1)))
3381    (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3382     (convert @0)
3383     /* Only do this if the types match as (type)(a == 0) is
3384        canonical form normally, while `a ^ 1` is canonical when
3385        there is no type change. */
3386     (if (types_match (type, TREE_TYPE (@0)))
3387      (bit_xor @0 { build_one_cst (type); } ))))))
3389 /* We can't reassociate at all for saturating types.  */
3390 (if (!TYPE_SATURATING (type))
3392  /* Contract negates.  */
3393  /* A + (-B) -> A - B */
3394  (simplify
3395   (plus:c @0 (convert? (negate @1)))
3396   /* Apply STRIP_NOPS on the negate.  */
3397   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3398        && !TYPE_OVERFLOW_SANITIZED (type))
3399    (with
3400     {
3401      tree t1 = type;
3402      if (INTEGRAL_TYPE_P (type)
3403          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3404        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3405     }
3406     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3407  /* A - (-B) -> A + B */
3408  (simplify
3409   (minus @0 (convert? (negate @1)))
3410   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3411        && !TYPE_OVERFLOW_SANITIZED (type))
3412    (with
3413     {
3414      tree t1 = type;
3415      if (INTEGRAL_TYPE_P (type)
3416          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3417        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3418     }
3419     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3420  /* -(T)(-A) -> (T)A
3421     Sign-extension is ok except for INT_MIN, which thankfully cannot
3422     happen without overflow.  */
3423  (simplify
3424   (negate (convert (negate @1)))
3425   (if (INTEGRAL_TYPE_P (type)
3426        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3427            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3428                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3429        && !TYPE_OVERFLOW_SANITIZED (type)
3430        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3431    (convert @1)))
3432  (simplify
3433   (negate (convert negate_expr_p@1))
3434   (if (SCALAR_FLOAT_TYPE_P (type)
3435        && ((DECIMAL_FLOAT_TYPE_P (type)
3436             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3437             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3438            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3439    (convert (negate @1))))
3440  (simplify
3441   (negate (nop_convert? (negate @1)))
3442   (if (!TYPE_OVERFLOW_SANITIZED (type)
3443        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3444    (view_convert @1)))
3446  /* We can't reassociate floating-point unless -fassociative-math
3447     or fixed-point plus or minus because of saturation to +-Inf.  */
3448  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3449       && !FIXED_POINT_TYPE_P (type))
3451   /* Match patterns that allow contracting a plus-minus pair
3452      irrespective of overflow issues.  */
3453   /* (A +- B) - A       ->  +- B */
3454   /* (A +- B) -+ B      ->  A */
3455   /* A - (A +- B)       -> -+ B */
3456   /* A +- (B -+ A)      ->  +- B */
3457   (simplify
3458    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3459    (view_convert @1))
3460   (simplify
3461    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3462    (if (!ANY_INTEGRAL_TYPE_P (type)
3463         || TYPE_OVERFLOW_WRAPS (type))
3464    (negate (view_convert @1))
3465    (view_convert (negate @1))))
3466   (simplify
3467    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3468    (view_convert @0))
3469   (simplify
3470    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3471     (if (!ANY_INTEGRAL_TYPE_P (type)
3472          || TYPE_OVERFLOW_WRAPS (type))
3473      (negate (view_convert @1))
3474      (view_convert (negate @1))))
3475   (simplify
3476    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3477    (view_convert @1))
3478   /* (A +- B) + (C - A)   -> C +- B */
3479   /* (A +  B) - (A - C)   -> B + C */
3480   /* More cases are handled with comparisons.  */
3481   (simplify
3482    (plus:c (plus:c @0 @1) (minus @2 @0))
3483    (plus @2 @1))
3484   (simplify
3485    (plus:c (minus @0 @1) (minus @2 @0))
3486    (minus @2 @1))
3487   (simplify
3488    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3489    (if (TYPE_OVERFLOW_UNDEFINED (type)
3490         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3491     (pointer_diff @2 @1)))
3492   (simplify
3493    (minus (plus:c @0 @1) (minus @0 @2))
3494    (plus @1 @2))
3496   /* (A +- CST1) +- CST2 -> A + CST3
3497      Use view_convert because it is safe for vectors and equivalent for
3498      scalars.  */
3499   (for outer_op (plus minus)
3500    (for inner_op (plus minus)
3501         neg_inner_op (minus plus)
3502     (simplify
3503      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3504                CONSTANT_CLASS_P@2)
3505      /* If one of the types wraps, use that one.  */
3506      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3507       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3508          forever if something doesn't simplify into a constant.  */
3509       (if (!CONSTANT_CLASS_P (@0))
3510        (if (outer_op == PLUS_EXPR)
3511         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3512         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3513       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3514            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3515        (if (outer_op == PLUS_EXPR)
3516         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3517         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3518        /* If the constant operation overflows we cannot do the transform
3519           directly as we would introduce undefined overflow, for example
3520           with (a - 1) + INT_MIN.  */
3521        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3522         (with { tree cst = const_binop (outer_op == inner_op
3523                                         ? PLUS_EXPR : MINUS_EXPR,
3524                                         type, @1, @2); }
3525          (if (cst)
3526           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3527            (inner_op @0 { cst; } )
3528            /* X+INT_MAX+1 is X-INT_MIN.  */
3529            (if (INTEGRAL_TYPE_P (type)
3530                 && wi::to_wide (cst) == wi::min_value (type))
3531             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3532             /* Last resort, use some unsigned type.  */
3533             (with { tree utype = unsigned_type_for (type); }
3534              (if (utype)
3535               (view_convert (inner_op
3536                              (view_convert:utype @0)
3537                              (view_convert:utype
3538                               { TREE_OVERFLOW (cst)
3539                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3541   /* (CST1 - A) +- CST2 -> CST3 - A  */
3542   (for outer_op (plus minus)
3543    (simplify
3544     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3545     /* If one of the types wraps, use that one.  */
3546     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3547      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3548         forever if something doesn't simplify into a constant.  */
3549      (if (!CONSTANT_CLASS_P (@0))
3550       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3551      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3552           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3553       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3554       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3555        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3556         (if (cst && !TREE_OVERFLOW (cst))
3557          (minus { cst; } @0))))))))
3559   /* CST1 - (CST2 - A) -> CST3 + A
3560      Use view_convert because it is safe for vectors and equivalent for
3561      scalars.  */
3562   (simplify
3563    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3564    /* If one of the types wraps, use that one.  */
3565    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3566     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3567       forever if something doesn't simplify into a constant.  */
3568     (if (!CONSTANT_CLASS_P (@0))
3569      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3570     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3571          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3572      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3573      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3574       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3575        (if (cst && !TREE_OVERFLOW (cst))
3576         (plus { cst; } @0)))))))
3578 /* ((T)(A)) + CST -> (T)(A + CST)  */
3579 #if GIMPLE
3580   (simplify
3581    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3582     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3583          && TREE_CODE (type) == INTEGER_TYPE
3584          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3585          && int_fits_type_p (@1, TREE_TYPE (@0)))
3586      /* Perform binary operation inside the cast if the constant fits
3587         and (A + CST)'s range does not overflow.  */
3588      (with
3589       {
3590         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3591                           max_ovf = wi::OVF_OVERFLOW;
3592         tree inner_type = TREE_TYPE (@0);
3594         wide_int w1
3595           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3596                             TYPE_SIGN (inner_type));
3598         value_range vr;
3599         if (get_global_range_query ()->range_of_expr (vr, @0)
3600             && !vr.varying_p () && !vr.undefined_p ())
3601           {
3602             wide_int wmin0 = vr.lower_bound ();
3603             wide_int wmax0 = vr.upper_bound ();
3604             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3605             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3606           }
3607       }
3608      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3609       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3610      )))
3611 #endif
3613 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3614 #if GIMPLE
3615   (for op (plus minus)
3616    (simplify
3617     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3618      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3619           && TREE_CODE (type) == INTEGER_TYPE
3620           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3621           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3622           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3623           && TYPE_OVERFLOW_WRAPS (type))
3624        (plus (convert @0) (op @2 (convert @1))))))
3625 #endif
3627 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3628    to a simple value.  */
3629   (for op (plus minus)
3630    (simplify
3631     (op (convert @0) (convert @1))
3632      (if (INTEGRAL_TYPE_P (type)
3633           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3634           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3635           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3636           && !TYPE_OVERFLOW_TRAPS (type)
3637           && !TYPE_OVERFLOW_SANITIZED (type))
3638       (convert (op! @0 @1)))))
3640   /* ~A + A -> -1 */
3641   (simplify
3642    (plus:c (convert? (bit_not @0)) (convert? @0))
3643    (if (!TYPE_OVERFLOW_TRAPS (type))
3644     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3646   /* ~A + 1 -> -A */
3647   (simplify
3648    (plus (convert? (bit_not @0)) integer_each_onep)
3649    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3650     (negate (convert @0))))
3652   /* -A - 1 -> ~A */
3653   (simplify
3654    (minus (convert? (negate @0)) integer_each_onep)
3655    (if (!TYPE_OVERFLOW_TRAPS (type)
3656         && TREE_CODE (type) != COMPLEX_TYPE
3657         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3658     (bit_not (convert @0))))
3660   /* -1 - A -> ~A */
3661   (simplify
3662    (minus integer_all_onesp @0)
3663    (if (TREE_CODE (type) != COMPLEX_TYPE)
3664     (bit_not @0)))
3666   /* (T)(P + A) - (T)P -> (T) A */
3667   (simplify
3668    (minus (convert (plus:c @@0 @1))
3669     (convert? @0))
3670    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3671         /* For integer types, if A has a smaller type
3672            than T the result depends on the possible
3673            overflow in P + A.
3674            E.g. T=size_t, A=(unsigned)429497295, P>0.
3675            However, if an overflow in P + A would cause
3676            undefined behavior, we can assume that there
3677            is no overflow.  */
3678         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3679             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3680     (convert @1)))
3681   (simplify
3682    (minus (convert (pointer_plus @@0 @1))
3683     (convert @0))
3684    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3685         /* For pointer types, if the conversion of A to the
3686            final type requires a sign- or zero-extension,
3687            then we have to punt - it is not defined which
3688            one is correct.  */
3689         || (POINTER_TYPE_P (TREE_TYPE (@0))
3690             && TREE_CODE (@1) == INTEGER_CST
3691             && tree_int_cst_sign_bit (@1) == 0))
3692     (convert @1)))
3693    (simplify
3694     (pointer_diff (pointer_plus @@0 @1) @0)
3695     /* The second argument of pointer_plus must be interpreted as signed, and
3696        thus sign-extended if necessary.  */
3697     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3698      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3699         second arg is unsigned even when we need to consider it as signed,
3700         we don't want to diagnose overflow here.  */
3701      (convert (view_convert:stype @1))))
3703   /* (T)P - (T)(P + A) -> -(T) A */
3704   (simplify
3705    (minus (convert? @0)
3706     (convert (plus:c @@0 @1)))
3707    (if (INTEGRAL_TYPE_P (type)
3708         && TYPE_OVERFLOW_UNDEFINED (type)
3709         /* For integer literals, using an intermediate unsigned type to avoid
3710            an overflow at run time is counter-productive because it introduces
3711            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3712            the result, which may be problematic in GENERIC for some front-ends:
3713              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3714            so we use the direct path for them.  */
3715         && TREE_CODE (@1) != INTEGER_CST
3716         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3717     (with { tree utype = unsigned_type_for (type); }
3718      (convert (negate (convert:utype @1))))
3719     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3720          /* For integer types, if A has a smaller type
3721             than T the result depends on the possible
3722             overflow in P + A.
3723             E.g. T=size_t, A=(unsigned)429497295, P>0.
3724             However, if an overflow in P + A would cause
3725             undefined behavior, we can assume that there
3726             is no overflow.  */
3727          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3728              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3729      (negate (convert @1)))))
3730   (simplify
3731    (minus (convert @0)
3732     (convert (pointer_plus @@0 @1)))
3733    (if (INTEGRAL_TYPE_P (type)
3734         && TYPE_OVERFLOW_UNDEFINED (type)
3735         /* See above the rationale for this condition.  */
3736         && TREE_CODE (@1) != INTEGER_CST
3737         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3738     (with { tree utype = unsigned_type_for (type); }
3739      (convert (negate (convert:utype @1))))
3740     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3741          /* For pointer types, if the conversion of A to the
3742             final type requires a sign- or zero-extension,
3743             then we have to punt - it is not defined which
3744             one is correct.  */
3745          || (POINTER_TYPE_P (TREE_TYPE (@0))
3746              && TREE_CODE (@1) == INTEGER_CST
3747              && tree_int_cst_sign_bit (@1) == 0))
3748      (negate (convert @1)))))
3749    (simplify
3750     (pointer_diff @0 (pointer_plus @@0 @1))
3751     /* The second argument of pointer_plus must be interpreted as signed, and
3752        thus sign-extended if necessary.  */
3753     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3754      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3755         second arg is unsigned even when we need to consider it as signed,
3756         we don't want to diagnose overflow here.  */
3757      (negate (convert (view_convert:stype @1)))))
3759   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3760   (simplify
3761    (minus (convert (plus:c @@0 @1))
3762     (convert (plus:c @0 @2)))
3763    (if (INTEGRAL_TYPE_P (type)
3764         && TYPE_OVERFLOW_UNDEFINED (type)
3765         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3766         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3767     (with { tree utype = unsigned_type_for (type); }
3768      (convert (minus (convert:utype @1) (convert:utype @2))))
3769     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3770           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3771          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3772              /* For integer types, if A has a smaller type
3773                 than T the result depends on the possible
3774                 overflow in P + A.
3775                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3776                 However, if an overflow in P + A would cause
3777                 undefined behavior, we can assume that there
3778                 is no overflow.  */
3779              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3780                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3781                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3782                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3783      (minus (convert @1) (convert @2)))))
3784   (simplify
3785    (minus (convert (pointer_plus @@0 @1))
3786     (convert (pointer_plus @0 @2)))
3787    (if (INTEGRAL_TYPE_P (type)
3788         && TYPE_OVERFLOW_UNDEFINED (type)
3789         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3790     (with { tree utype = unsigned_type_for (type); }
3791      (convert (minus (convert:utype @1) (convert:utype @2))))
3792     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3793          /* For pointer types, if the conversion of A to the
3794             final type requires a sign- or zero-extension,
3795             then we have to punt - it is not defined which
3796             one is correct.  */
3797          || (POINTER_TYPE_P (TREE_TYPE (@0))
3798              && TREE_CODE (@1) == INTEGER_CST
3799              && tree_int_cst_sign_bit (@1) == 0
3800              && TREE_CODE (@2) == INTEGER_CST
3801              && tree_int_cst_sign_bit (@2) == 0))
3802      (minus (convert @1) (convert @2)))))
3803    (simplify
3804     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3805      (pointer_diff @0 @1))
3806    (simplify
3807     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3808     /* The second argument of pointer_plus must be interpreted as signed, and
3809        thus sign-extended if necessary.  */
3810     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3811      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3812         second arg is unsigned even when we need to consider it as signed,
3813         we don't want to diagnose overflow here.  */
3814      (minus (convert (view_convert:stype @1))
3815             (convert (view_convert:stype @2)))))))
3817 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3818     Modeled after fold_plusminus_mult_expr.  */
3819 (if (!TYPE_SATURATING (type)
3820      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3821  (for plusminus (plus minus)
3822   (simplify
3823    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3824    (if (!ANY_INTEGRAL_TYPE_P (type)
3825         || TYPE_OVERFLOW_WRAPS (type)
3826         || (INTEGRAL_TYPE_P (type)
3827             && tree_expr_nonzero_p (@0)
3828             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3829     (if (single_use (@3) || single_use (@4))
3830      /* If @1 +- @2 is constant require a hard single-use on either
3831         original operand (but not on both).  */
3832      (mult (plusminus @1 @2) @0)
3833      (mult! (plusminus @1 @2) @0)
3834   )))
3835   /* We cannot generate constant 1 for fract.  */
3836   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3837    (simplify
3838     (plusminus @0 (mult:c@3 @0 @2))
3839     (if ((!ANY_INTEGRAL_TYPE_P (type)
3840           || TYPE_OVERFLOW_WRAPS (type)
3841           /* For @0 + @0*@2 this transformation would introduce UB
3842              (where there was none before) for @0 in [-1,0] and @2 max.
3843              For @0 - @0*@2 this transformation would introduce UB
3844              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3845           || (INTEGRAL_TYPE_P (type)
3846               && ((tree_expr_nonzero_p (@0)
3847                    && expr_not_equal_to (@0,
3848                                 wi::minus_one (TYPE_PRECISION (type))))
3849                   || (plusminus == PLUS_EXPR
3850                       ? expr_not_equal_to (@2,
3851                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3852                       /* Let's ignore the @0 -1 and @2 min case.  */
3853                       : (expr_not_equal_to (@2,
3854                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3855                          && expr_not_equal_to (@2,
3856                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3857                                 + 1))))))
3858          && single_use (@3))
3859      (mult (plusminus { build_one_cst (type); } @2) @0)))
3860    (simplify
3861     (plusminus (mult:c@3 @0 @2) @0)
3862     (if ((!ANY_INTEGRAL_TYPE_P (type)
3863           || TYPE_OVERFLOW_WRAPS (type)
3864           /* For @0*@2 + @0 this transformation would introduce UB
3865              (where there was none before) for @0 in [-1,0] and @2 max.
3866              For @0*@2 - @0 this transformation would introduce UB
3867              for @0 0 and @2 min.  */
3868           || (INTEGRAL_TYPE_P (type)
3869               && ((tree_expr_nonzero_p (@0)
3870                    && (plusminus == MINUS_EXPR
3871                        || expr_not_equal_to (@0,
3872                                 wi::minus_one (TYPE_PRECISION (type)))))
3873                   || expr_not_equal_to (@2,
3874                         (plusminus == PLUS_EXPR
3875                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3876                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3877          && single_use (@3))
3878      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3880 #if GIMPLE
3881 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3882    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3883 (simplify
3884  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3885   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3886        && tree_fits_uhwi_p (@1)
3887        && tree_to_uhwi (@1) < element_precision (type)
3888        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3889            || optab_handler (smul_optab,
3890                              TYPE_MODE (type)) != CODE_FOR_nothing))
3891    (with { tree t = type;
3892            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3893            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3894                                              element_precision (type));
3895            w += 1;
3896            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3897                                         : t, w);
3898            cst = build_uniform_cst (t, cst); }
3899     (convert (mult (convert:t @0) { cst; })))))
3900 (simplify
3901  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3902   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3903        && tree_fits_uhwi_p (@1)
3904        && tree_to_uhwi (@1) < element_precision (type)
3905        && tree_fits_uhwi_p (@2)
3906        && tree_to_uhwi (@2) < element_precision (type)
3907        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3908            || optab_handler (smul_optab,
3909                              TYPE_MODE (type)) != CODE_FOR_nothing))
3910    (with { tree t = type;
3911            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3912            unsigned int prec = element_precision (type);
3913            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3914            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3915            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3916                                         : t, w);
3917            cst = build_uniform_cst (t, cst); }
3918     (convert (mult (convert:t @0) { cst; })))))
3919 #endif
3921 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3922    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3923    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3924 (for op (bit_ior bit_xor)
3925  (simplify
3926   (op (mult:s@0 @1 INTEGER_CST@2)
3927       (mult:s@3 @1 INTEGER_CST@4))
3928   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3929        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3930    (mult @1
3931          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3932  (simplify
3933   (op:c (mult:s@0 @1 INTEGER_CST@2)
3934         (lshift:s@3 @1 INTEGER_CST@4))
3935   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3936        && tree_int_cst_sgn (@4) > 0
3937        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3938    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3939            wide_int c = wi::add (wi::to_wide (@2),
3940                                  wi::lshift (wone, wi::to_wide (@4))); }
3941     (mult @1 { wide_int_to_tree (type, c); }))))
3942  (simplify
3943   (op:c (mult:s@0 @1 INTEGER_CST@2)
3944         @1)
3945   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3946        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3947    (mult @1
3948          { wide_int_to_tree (type,
3949                              wi::add (wi::to_wide (@2), 1)); })))
3950  (simplify
3951   (op (lshift:s@0 @1 INTEGER_CST@2)
3952       (lshift:s@3 @1 INTEGER_CST@4))
3953   (if (INTEGRAL_TYPE_P (type)
3954        && tree_int_cst_sgn (@2) > 0
3955        && tree_int_cst_sgn (@4) > 0
3956        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3957    (with { tree t = type;
3958            if (!TYPE_OVERFLOW_WRAPS (t))
3959              t = unsigned_type_for (t);
3960            wide_int wone = wi::one (TYPE_PRECISION (t));
3961            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3962                                  wi::lshift (wone, wi::to_wide (@4))); }
3963     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3964  (simplify
3965   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3966         @1)
3967   (if (INTEGRAL_TYPE_P (type)
3968        && tree_int_cst_sgn (@2) > 0
3969        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3970    (with { tree t = type;
3971            if (!TYPE_OVERFLOW_WRAPS (t))
3972              t = unsigned_type_for (t);
3973            wide_int wone = wi::one (TYPE_PRECISION (t));
3974            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3975     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3977 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3979 (for minmax (min max)
3980  (simplify
3981   (minmax @0 @0)
3982   @0)
3983 /* max(max(x,y),x) -> max(x,y)  */
3984  (simplify
3985   (minmax:c (minmax:c@2 @0 @1) @0)
3986   @2))
3987 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3988 (for minmax (FMIN_ALL FMAX_ALL)
3989  (simplify
3990   (minmax @0 @0)
3991   (if (!tree_expr_maybe_signaling_nan_p (@0))
3992     @0)))
3993 /* min(max(x,y),y) -> y.  */
3994 (simplify
3995  (min:c (max:c @0 @1) @1)
3996  @1)
3997 /* max(min(x,y),y) -> y.  */
3998 (simplify
3999  (max:c (min:c @0 @1) @1)
4000  @1)
4001 /* max(a,-a) -> abs(a).  */
4002 (simplify
4003  (max:c @0 (negate @0))
4004  (if (TREE_CODE (type) != COMPLEX_TYPE
4005       && (! ANY_INTEGRAL_TYPE_P (type)
4006           || TYPE_OVERFLOW_UNDEFINED (type)))
4007   (abs @0)))
4008 /* min(a,-a) -> -abs(a).  */
4009 (simplify
4010  (min:c @0 (negate @0))
4011  (if (TREE_CODE (type) != COMPLEX_TYPE
4012       && (! ANY_INTEGRAL_TYPE_P (type)
4013           || TYPE_OVERFLOW_UNDEFINED (type)))
4014   (negate (abs @0))))
4015 (simplify
4016  (min @0 @1)
4017  (switch
4018   (if (INTEGRAL_TYPE_P (type)
4019        && TYPE_MIN_VALUE (type)
4020        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4021    @1)
4022   (if (INTEGRAL_TYPE_P (type)
4023        && TYPE_MAX_VALUE (type)
4024        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4025    @0)))
4026 (simplify
4027  (max @0 @1)
4028  (switch
4029   (if (INTEGRAL_TYPE_P (type)
4030        && TYPE_MAX_VALUE (type)
4031        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4032    @1)
4033   (if (INTEGRAL_TYPE_P (type)
4034        && TYPE_MIN_VALUE (type)
4035        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4036    @0)))
4038 /* max (a, a + CST) -> a + CST where CST is positive.  */
4039 /* max (a, a + CST) -> a where CST is negative.  */
4040 (simplify
4041  (max:c @0 (plus@2 @0 INTEGER_CST@1))
4042   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4043    (if (tree_int_cst_sgn (@1) > 0)
4044     @2
4045     @0)))
4047 /* min (a, a + CST) -> a where CST is positive.  */
4048 /* min (a, a + CST) -> a + CST where CST is negative. */
4049 (simplify
4050  (min:c @0 (plus@2 @0 INTEGER_CST@1))
4051   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4052    (if (tree_int_cst_sgn (@1) > 0)
4053     @0
4054     @2)))
4056 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4057    the addresses are known to be less, equal or greater.  */
4058 (for minmax (min max)
4059      cmp (lt gt)
4060  (simplify
4061   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4062   (with
4063    {
4064      poly_int64 off0, off1;
4065      tree base0, base1;
4066      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4067                                   off0, off1, GENERIC);
4068    }
4069    (if (equal == 1)
4070     (if (minmax == MIN_EXPR)
4071      (if (known_le (off0, off1))
4072       @2
4073       (if (known_gt (off0, off1))
4074        @3))
4075      (if (known_ge (off0, off1))
4076       @2
4077       (if (known_lt (off0, off1))
4078        @3)))))))
4080 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4081    and the outer convert demotes the expression back to x's type.  */
4082 (for minmax (min max)
4083  (simplify
4084   (convert (minmax@0 (convert @1) INTEGER_CST@2))
4085   (if (INTEGRAL_TYPE_P (type)
4086        && types_match (@1, type) && int_fits_type_p (@2, type)
4087        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4088        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4089    (minmax @1 (convert @2)))))
4091 (for minmax (FMIN_ALL FMAX_ALL)
4092  /* If either argument is NaN and other one is not sNaN, return the other
4093     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
4094  (simplify
4095   (minmax:c @0 REAL_CST@1)
4096    (if (real_isnan (TREE_REAL_CST_PTR (@1))
4097        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4098        && !tree_expr_maybe_signaling_nan_p (@0))
4099    @0)))
4100 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
4101    functions to return the numeric arg if the other one is NaN.
4102    MIN and MAX don't honor that, so only transform if -ffinite-math-only
4103    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
4104    worry about it either.  */
4105 (if (flag_finite_math_only)
4106  (simplify
4107   (FMIN_ALL @0 @1)
4108   (min @0 @1))
4109  (simplify
4110   (FMAX_ALL @0 @1)
4111   (max @0 @1)))
4112 /* min (-A, -B) -> -max (A, B)  */
4113 (for minmax (min max FMIN_ALL FMAX_ALL)
4114      maxmin (max min FMAX_ALL FMIN_ALL)
4115  (simplify
4116   (minmax (negate:s@2 @0) (negate:s@3 @1))
4117   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4118        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4119            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4120    (negate (maxmin @0 @1)))))
4121 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4122    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4123 (for minmax (min max)
4124  maxmin (max min)
4125  (simplify
4126   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4127   (bit_not (maxmin @0 @1)))
4128 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4129 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4130  (simplify
4131   (bit_not (minmax:cs (bit_not @0) @1))
4132   (maxmin @0 (bit_not @1))))
4134 /* MIN (X, Y) == X -> X <= Y  */
4135 /* MIN (X, Y) < X -> X > Y  */
4136 /* MIN (X, Y) >= X -> X <= Y  */
4137 (for minmax (min min min min max max max max)
4138      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4139      out    (le  gt  gt  le  ge  lt  lt  ge )
4140  (simplify
4141   (cmp:c (minmax:c @0 @1) @0)
4142   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4143    (out @0 @1))))
4144 /* MIN (X, 5) == 0 -> X == 0
4145    MIN (X, 5) == 7 -> false  */
4146 (for cmp (eq ne)
4147  (simplify
4148   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4149   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4150                  TYPE_SIGN (TREE_TYPE (@0))))
4151    { constant_boolean_node (cmp == NE_EXPR, type); }
4152    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4153                   TYPE_SIGN (TREE_TYPE (@0))))
4154     (cmp @0 @2)))))
4155 (for cmp (eq ne)
4156  (simplify
4157   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4158   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4159                  TYPE_SIGN (TREE_TYPE (@0))))
4160    { constant_boolean_node (cmp == NE_EXPR, type); }
4161    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4162                   TYPE_SIGN (TREE_TYPE (@0))))
4163     (cmp @0 @2)))))
4165 /* X <= MAX(X, Y) -> true
4166    X > MAX(X, Y) -> false 
4167    X >= MIN(X, Y) -> true
4168    X < MIN(X, Y) -> false */
4169 (for minmax (min     min     max     max     )
4170      cmp    (ge      lt      le      gt      )
4171  (simplify
4172   (cmp:c @0 (minmax:c @0 @1))
4173   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4175 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4176 (for minmax (min     min     max     max     min     min     max     max    )
4177      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4178      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4179  (simplify
4180   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4181   (comb (cmp @0 @2) (cmp @1 @2))))
4183 /* Undo fancy ways of writing max/min or other ?: expressions, like
4184    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4185    People normally use ?: and that is what we actually try to optimize.  */
4186 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4187 (simplify
4188  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4189  (if (INTEGRAL_TYPE_P (type)
4190       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4191   (cond (convert:boolean_type_node @2) @1 @0)))
4192 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4193 (simplify
4194  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4195  (if (INTEGRAL_TYPE_P (type)
4196       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4197   (cond (convert:boolean_type_node @2) @1 @0)))
4198 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4199 (simplify
4200  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4201  (if (INTEGRAL_TYPE_P (type)
4202       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4203   (cond (convert:boolean_type_node @2) @1 @0)))
4205 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4206 (simplify
4207   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4208   (max @2 @1))
4210 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4211 (for op (bit_xor bit_ior plus)
4212  (simplify
4213   (cond (eq zero_one_valued_p@0
4214             integer_zerop)
4215         @1
4216         (op:c @2 @1))
4217   (if (INTEGRAL_TYPE_P (type)
4218        && TYPE_PRECISION (type) > 1
4219        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4220        (op (mult (convert:type @0) @2) @1))))
4222 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4223 (for op (bit_xor bit_ior plus)
4224  (simplify
4225   (cond (ne zero_one_valued_p@0
4226             integer_zerop)
4227        (op:c @2 @1)
4228         @1)
4229   (if (INTEGRAL_TYPE_P (type)
4230        && TYPE_PRECISION (type) > 1
4231        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4232        (op (mult (convert:type @0) @2) @1))))
4234 /* ?: Value replacement. */
4235 /* a == 0 ? b : b + a  -> b + a */
4236 (for op (plus bit_ior bit_xor)
4237  (simplify
4238   (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4239    @2))
4240 /* a == 0 ? b : b - a  -> b - a */
4241 /* a == 0 ? b : b ptr+ a  -> b ptr+ a */
4242 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4243 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4244  (simplify
4245   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4246    @2))
4248 /* a == 1 ? b : b / a  -> b / a */
4249 (for op (trunc_div ceil_div floor_div round_div exact_div)
4250  (simplify
4251   (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4252    @2))
4254 /* a == 1 ? b : a * b -> a * b */
4255 (for op (mult)
4256  (simplify
4257   (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4258    @2))
4260 /* a == -1 ? b : a & b -> a & b */
4261 (for op (bit_and)
4262  (simplify
4263   (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4264    @2))
4266 /* Simplifications of shift and rotates.  */
4268 (for rotate (lrotate rrotate)
4269  (simplify
4270   (rotate integer_all_onesp@0 @1)
4271   @0))
4273 /* Optimize -1 >> x for arithmetic right shifts.  */
4274 (simplify
4275  (rshift integer_all_onesp@0 @1)
4276  (if (!TYPE_UNSIGNED (type))
4277   @0))
4279 /* Optimize (x >> c) << c into x & (-1<<c).  */
4280 (simplify
4281  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4282  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4283   /* It doesn't matter if the right shift is arithmetic or logical.  */
4284   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4286 (simplify
4287  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4288  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4289       /* Allow intermediate conversion to integral type with whatever sign, as
4290          long as the low TYPE_PRECISION (type)
4291          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4292       && INTEGRAL_TYPE_P (type)
4293       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4294       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4295       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4296       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4297           || wi::geu_p (wi::to_wide (@1),
4298                         TYPE_PRECISION (type)
4299                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4300   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4302 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4303    unsigned x OR truncate into the precision(type) - c lowest bits
4304    of signed x (if they have mode precision or a precision of 1).  */
4305 (simplify
4306  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4307  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4308   (if (TYPE_UNSIGNED (type))
4309    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4310    (if (INTEGRAL_TYPE_P (type))
4311     (with {
4312       int width = element_precision (type) - tree_to_uhwi (@1);
4313       tree stype = NULL_TREE;
4314       if (width <= MAX_FIXED_MODE_SIZE)
4315         stype = build_nonstandard_integer_type (width, 0);
4316      }
4317      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4318       (convert (convert:stype @0))))))))
4320 /* Optimize x >> x into 0 */
4321 (simplify
4322  (rshift @0 @0)
4323   { build_zero_cst (type); })
4325 (for shiftrotate (lrotate rrotate lshift rshift)
4326  (simplify
4327   (shiftrotate @0 integer_zerop)
4328   (non_lvalue @0))
4329  (simplify
4330   (shiftrotate integer_zerop@0 @1)
4331   @0)
4332  /* Prefer vector1 << scalar to vector1 << vector2
4333     if vector2 is uniform.  */
4334  (for vec (VECTOR_CST CONSTRUCTOR)
4335   (simplify
4336    (shiftrotate @0 vec@1)
4337    (with { tree tem = uniform_vector_p (@1); }
4338     (if (tem)
4339      (shiftrotate @0 { tem; }))))))
4341 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4342    Y is 0.  Similarly for X >> Y.  */
4343 #if GIMPLE
4344 (for shift (lshift rshift)
4345  (simplify
4346   (shift @0 SSA_NAME@1)
4347    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4348     (with {
4349       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4350       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4351      }
4352      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4353       @0)))))
4354 #endif
4356 /* Rewrite an LROTATE_EXPR by a constant into an
4357    RROTATE_EXPR by a new constant.  */
4358 (simplify
4359  (lrotate @0 INTEGER_CST@1)
4360  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4361                             build_int_cst (TREE_TYPE (@1),
4362                                            element_precision (type)), @1); }))
4364 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4365 (for op (lrotate rrotate rshift lshift)
4366  (simplify
4367   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4368   (with { unsigned int prec = element_precision (type); }
4369    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4370         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4371         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4372         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4373     (with { unsigned int low = (tree_to_uhwi (@1)
4374                                 + tree_to_uhwi (@2)); }
4375      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4376         being well defined.  */
4377      (if (low >= prec)
4378       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4379        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4380        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4381         { build_zero_cst (type); }
4382         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4383       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4386 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4387 (simplify
4388  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4389   (if ((wi::to_wide (@1) & 1) != 0)
4390    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4391    { build_zero_cst (type); }))
4393 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4394    either to false if D is smaller (unsigned comparison) than C, or to
4395    x == log2 (D) - log2 (C).  Similarly for right shifts.
4396    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4397 (for cmp (ne eq)
4398      icmp (eq ne)
4399  (simplify
4400   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4401    (with { int c1 = wi::clz (wi::to_wide (@1));
4402            int c2 = wi::clz (wi::to_wide (@2)); }
4403     (if (c1 < c2)
4404      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4405      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4406  (simplify
4407   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4408    (if (tree_int_cst_sgn (@1) > 0)
4409     (with { int c1 = wi::clz (wi::to_wide (@1));
4410             int c2 = wi::clz (wi::to_wide (@2)); }
4411      (if (c1 > c2)
4412       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4413       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4414  /* `(1 >> X) != 0` -> `X == 0` */
4415  /* `(1 >> X) == 0` -> `X != 0` */
4416  (simplify
4417   (cmp (rshift integer_onep@1 @0) integer_zerop)
4418    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4419     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4421 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4422    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4423    if CST2 != 0.  */
4424 (for cmp (ne eq)
4425  (simplify
4426   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4427   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4428    (if (cand < 0
4429         || (!integer_zerop (@2)
4430             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4431     { constant_boolean_node (cmp == NE_EXPR, type); }
4432     (if (!integer_zerop (@2)
4433          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4434      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4436 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4437         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4438 (for cmp (ne eq)
4439  (simplify
4440   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4441   (if (tree_fits_shwi_p (@1)
4442        && tree_to_shwi (@1) > 0
4443        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4444     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4445       { constant_boolean_node (cmp == NE_EXPR, type); }
4446       (with { wide_int c1 = wi::to_wide (@1);
4447               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4448               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4449         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4450              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4451  (simplify
4452   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4453   (if (tree_fits_shwi_p (@1)
4454        && tree_to_shwi (@1) > 0
4455        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4456     (with { tree t0 = TREE_TYPE (@0);
4457             unsigned int prec = TYPE_PRECISION (t0);
4458             wide_int c1 = wi::to_wide (@1);
4459             wide_int c2 = wi::to_wide (@2);
4460             wide_int c3 = wi::to_wide (@3);
4461             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4462       (if ((c2 & c3) != c3)
4463         { constant_boolean_node (cmp == NE_EXPR, type); }
4464         (if (TYPE_UNSIGNED (t0))
4465           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4466             { constant_boolean_node (cmp == NE_EXPR, type); }
4467             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4468                  { wide_int_to_tree (t0, c3 << c1); }))
4469           (with { wide_int smask = wi::arshift (sb, c1); }
4470             (switch
4471               (if ((c2 & smask) == 0)
4472                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4473                      { wide_int_to_tree (t0, c3 << c1); }))
4474               (if ((c3 & smask) == 0)
4475                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4476                      { wide_int_to_tree (t0, c3 << c1); }))
4477               (if ((c2 & smask) != (c3 & smask))
4478                 { constant_boolean_node (cmp == NE_EXPR, type); })
4479               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4480                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4482 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4483         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4484    if the new mask might be further optimized.  */
4485 (for shift (lshift rshift)
4486  (simplify
4487   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4488            INTEGER_CST@2)
4489    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4490         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4491         && tree_fits_uhwi_p (@1)
4492         && tree_to_uhwi (@1) > 0
4493         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4494     (with
4495      {
4496        unsigned int shiftc = tree_to_uhwi (@1);
4497        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4498        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4499        tree shift_type = TREE_TYPE (@3);
4500        unsigned int prec;
4502        if (shift == LSHIFT_EXPR)
4503          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4504        else if (shift == RSHIFT_EXPR
4505                 && type_has_mode_precision_p (shift_type))
4506          {
4507            prec = TYPE_PRECISION (TREE_TYPE (@3));
4508            tree arg00 = @0;
4509            /* See if more bits can be proven as zero because of
4510               zero extension.  */
4511            if (@3 != @0
4512                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4513              {
4514                tree inner_type = TREE_TYPE (@0);
4515                if (type_has_mode_precision_p (inner_type)
4516                    && TYPE_PRECISION (inner_type) < prec)
4517                  {
4518                    prec = TYPE_PRECISION (inner_type);
4519                    /* See if we can shorten the right shift.  */
4520                    if (shiftc < prec)
4521                      shift_type = inner_type;
4522                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4523                       it into (X, 0) later on by making sure zerobits
4524                       is all ones.  */
4525                  }
4526              }
4527            zerobits = HOST_WIDE_INT_M1U;
4528            if (shiftc < prec)
4529              {
4530                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4531                zerobits <<= prec - shiftc;
4532              }
4533            /* For arithmetic shift if sign bit could be set, zerobits
4534               can contain actually sign bits, so no transformation is
4535               possible, unless MASK masks them all away.  In that
4536               case the shift needs to be converted into logical shift.  */
4537            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4538                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4539              {
4540                if ((mask & zerobits) == 0)
4541                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4542                else
4543                  zerobits = 0;
4544              }
4545          }
4546      }
4547      /* ((X << 16) & 0xff00) is (X, 0).  */
4548      (if ((mask & zerobits) == mask)
4549       { build_int_cst (type, 0); }
4550       (with { newmask = mask | zerobits; }
4551        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4552         (with
4553          {
4554            /* Only do the transformation if NEWMASK is some integer
4555               mode's mask.  */
4556            for (prec = BITS_PER_UNIT;
4557                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4558              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4559                break;
4560          }
4561          (if (prec < HOST_BITS_PER_WIDE_INT
4562               || newmask == HOST_WIDE_INT_M1U)
4563           (with
4564            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4565            (if (!tree_int_cst_equal (newmaskt, @2))
4566             (if (shift_type != TREE_TYPE (@3))
4567              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4568              (bit_and @4 { newmaskt; })))))))))))))
4570 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4571 (for cmp (ne eq)
4572        icmp (eq ne)
4573  (simplify
4574   (cmp
4575    (bit_and
4576     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4577   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4578    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4579                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4581 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4582    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4583 (for shift (lshift rshift)
4584  (for bit_op (bit_and bit_xor bit_ior)
4585   (simplify
4586    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4587    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4588     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4589      (if (mask)
4590       (bit_op (shift (convert @0) @1) { mask; })))))))
4592 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4593 (simplify
4594  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4595   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4596        && (element_precision (TREE_TYPE (@0))
4597            <= element_precision (TREE_TYPE (@1))
4598            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4599    (with
4600     { tree shift_type = TREE_TYPE (@0); }
4601      (convert (rshift (convert:shift_type @1) @2)))))
4603 /* ~(~X >>r Y) -> X >>r Y
4604    ~(~X <<r Y) -> X <<r Y */
4605 (for rotate (lrotate rrotate)
4606  (simplify
4607   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4608    (if ((element_precision (TREE_TYPE (@0))
4609          <= element_precision (TREE_TYPE (@1))
4610          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4611         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4612             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4613     (with
4614      { tree rotate_type = TREE_TYPE (@0); }
4615       (convert (rotate (convert:rotate_type @1) @2))))))
4617 (for cmp (eq ne)
4618  (for rotate (lrotate rrotate)
4619       invrot (rrotate lrotate)
4620   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4621   (simplify
4622    (cmp (rotate @1 @0) (rotate @2 @0))
4623    (cmp @1 @2))
4624   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4625   (simplify
4626    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4627    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4628   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4629   (simplify
4630    (cmp (rotate @0 @1) INTEGER_CST@2)
4631     (if (integer_zerop (@2) || integer_all_onesp (@2))
4632      (cmp @0 @2)))))
4634 /* Narrow a lshift by constant.  */
4635 (simplify
4636  (convert (lshift:s@0 @1 INTEGER_CST@2))
4637  (if (INTEGRAL_TYPE_P (type)
4638       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4639       && !integer_zerop (@2)
4640       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4641   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4642        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4643    (lshift (convert @1) @2)
4644    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4645     { build_zero_cst (type); }))))
4647 /* Simplifications of conversions.  */
4649 /* Basic strip-useless-type-conversions / strip_nops.  */
4650 (for cvt (convert view_convert float fix_trunc)
4651  (simplify
4652   (cvt @0)
4653   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4654        || (GENERIC && type == TREE_TYPE (@0)))
4655    @0)))
4657 /* Contract view-conversions.  */
4658 (simplify
4659   (view_convert (view_convert @0))
4660   (view_convert @0))
4662 /* For integral conversions with the same precision or pointer
4663    conversions use a NOP_EXPR instead.  */
4664 (simplify
4665   (view_convert @0)
4666   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4667        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4668        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4669    (convert @0)))
4671 /* Strip inner integral conversions that do not change precision or size, or
4672    zero-extend while keeping the same size (for bool-to-char).  */
4673 (simplify
4674   (view_convert (convert@0 @1))
4675   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4676        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4677        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4678        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4679            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4680                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4681    (view_convert @1)))
4683 /* Simplify a view-converted empty or single-element constructor.  */
4684 (simplify
4685   (view_convert CONSTRUCTOR@0)
4686   (with
4687    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4688                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4689    (switch
4690     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4691      { build_zero_cst (type); })
4692     (if (CONSTRUCTOR_NELTS (ctor) == 1
4693          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4694          && operand_equal_p (TYPE_SIZE (type),
4695                              TYPE_SIZE (TREE_TYPE
4696                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4697      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4699 /* Re-association barriers around constants and other re-association
4700    barriers can be removed.  */
4701 (simplify
4702  (paren CONSTANT_CLASS_P@0)
4703  @0)
4704 (simplify
4705  (paren (paren@1 @0))
4706  @1)
4708 /* Handle cases of two conversions in a row.  */
4709 (for ocvt (convert float fix_trunc)
4710  (for icvt (convert float)
4711   (simplify
4712    (ocvt (icvt@1 @0))
4713    (with
4714     {
4715       tree inside_type = TREE_TYPE (@0);
4716       tree inter_type = TREE_TYPE (@1);
4717       int inside_int = INTEGRAL_TYPE_P (inside_type);
4718       int inside_ptr = POINTER_TYPE_P (inside_type);
4719       int inside_float = FLOAT_TYPE_P (inside_type);
4720       int inside_vec = VECTOR_TYPE_P (inside_type);
4721       unsigned int inside_prec = element_precision (inside_type);
4722       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4723       int inter_int = INTEGRAL_TYPE_P (inter_type);
4724       int inter_ptr = POINTER_TYPE_P (inter_type);
4725       int inter_float = FLOAT_TYPE_P (inter_type);
4726       int inter_vec = VECTOR_TYPE_P (inter_type);
4727       unsigned int inter_prec = element_precision (inter_type);
4728       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4729       int final_int = INTEGRAL_TYPE_P (type);
4730       int final_ptr = POINTER_TYPE_P (type);
4731       int final_float = FLOAT_TYPE_P (type);
4732       int final_vec = VECTOR_TYPE_P (type);
4733       unsigned int final_prec = element_precision (type);
4734       int final_unsignedp = TYPE_UNSIGNED (type);
4735     }
4736    (switch
4737     /* In addition to the cases of two conversions in a row
4738        handled below, if we are converting something to its own
4739        type via an object of identical or wider precision, neither
4740        conversion is needed.  */
4741     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4742           || (GENERIC
4743               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4744          && (((inter_int || inter_ptr) && final_int)
4745              || (inter_float && final_float))
4746          && inter_prec >= final_prec)
4747      (ocvt @0))
4749     /* Likewise, if the intermediate and initial types are either both
4750        float or both integer, we don't need the middle conversion if the
4751        former is wider than the latter and doesn't change the signedness
4752        (for integers).  Avoid this if the final type is a pointer since
4753        then we sometimes need the middle conversion.  */
4754     (if (((inter_int && inside_int) || (inter_float && inside_float))
4755          && (final_int || final_float)
4756          && inter_prec >= inside_prec
4757          && (inter_float || inter_unsignedp == inside_unsignedp))
4758      (ocvt @0))
4760     /* If we have a sign-extension of a zero-extended value, we can
4761        replace that by a single zero-extension.  Likewise if the
4762        final conversion does not change precision we can drop the
4763        intermediate conversion.  Similarly truncation of a sign-extension
4764        can be replaced by a single sign-extension.  */
4765     (if (inside_int && inter_int && final_int
4766          && ((inside_prec < inter_prec && inter_prec < final_prec
4767               && inside_unsignedp && !inter_unsignedp)
4768              || final_prec == inter_prec
4769              || (inside_prec < inter_prec && inter_prec > final_prec
4770                  && !inside_unsignedp && inter_unsignedp)))
4771      (ocvt @0))
4773     /* Two conversions in a row are not needed unless:
4774         - some conversion is floating-point (overstrict for now), or
4775         - some conversion is a vector (overstrict for now), or
4776         - the intermediate type is narrower than both initial and
4777           final, or
4778         - the intermediate type and innermost type differ in signedness,
4779           and the outermost type is wider than the intermediate, or
4780         - the initial type is a pointer type and the precisions of the
4781           intermediate and final types differ, or
4782         - the final type is a pointer type and the precisions of the
4783           initial and intermediate types differ.  */
4784     (if (! inside_float && ! inter_float && ! final_float
4785          && ! inside_vec && ! inter_vec && ! final_vec
4786          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4787          && ! (inside_int && inter_int
4788                && inter_unsignedp != inside_unsignedp
4789                && inter_prec < final_prec)
4790          && ((inter_unsignedp && inter_prec > inside_prec)
4791              == (final_unsignedp && final_prec > inter_prec))
4792          && ! (inside_ptr && inter_prec != final_prec)
4793          && ! (final_ptr && inside_prec != inter_prec))
4794      (ocvt @0))
4796    /* `(outer:M)(inter:N) a:O`
4797       can be converted to `(outer:M) a`
4798       if M <= O && N >= O. No matter what signedness of the casts,
4799       as the final is either a truncation from the original or just
4800       a sign change of the type. */
4801    (if (inside_int && inter_int && final_int
4802         && final_prec <= inside_prec
4803         && inter_prec >= inside_prec)
4804     (convert @0))
4806     /* A truncation to an unsigned type (a zero-extension) should be
4807        canonicalized as bitwise and of a mask.  */
4808     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4809          && final_int && inter_int && inside_int
4810          && final_prec == inside_prec
4811          && final_prec > inter_prec
4812          && inter_unsignedp)
4813      (convert (bit_and @0 { wide_int_to_tree
4814                               (inside_type,
4815                                wi::mask (inter_prec, false,
4816                                          TYPE_PRECISION (inside_type))); })))
4818     /* If we are converting an integer to a floating-point that can
4819        represent it exactly and back to an integer, we can skip the
4820        floating-point conversion.  */
4821     (if (GIMPLE /* PR66211 */
4822          && inside_int && inter_float && final_int &&
4823          (unsigned) significand_size (TYPE_MODE (inter_type))
4824          >= inside_prec - !inside_unsignedp)
4825      (convert @0)))))))
4827 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4828    float_type.  Only do the transformation if we do not need to preserve
4829    trapping behaviour, so require !flag_trapping_math. */
4830 #if GIMPLE
4831 (simplify
4832    (float (fix_trunc @0))
4833    (if (!flag_trapping_math
4834         && types_match (type, TREE_TYPE (@0))
4835         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4836                                           OPTIMIZE_FOR_BOTH))
4837       (IFN_TRUNC @0)))
4838 #endif
4840 /* If we have a narrowing conversion to an integral type that is fed by a
4841    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4842    masks off bits outside the final type (and nothing else).  */
4843 (simplify
4844   (convert (bit_and @0 INTEGER_CST@1))
4845   (if (INTEGRAL_TYPE_P (type)
4846        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4847        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4848        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4849                                                     TYPE_PRECISION (type)), 0))
4850    (convert @0)))
4853 /* (X /[ex] A) * A -> X.  */
4854 (simplify
4855   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4856   (convert @0))
4858 /* Simplify (A / B) * B + (A % B) -> A.  */
4859 (for div (trunc_div ceil_div floor_div round_div)
4860      mod (trunc_mod ceil_mod floor_mod round_mod)
4861   (simplify
4862    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4863    @0))
4865 /* x / y * y == x -> x % y == 0.  */
4866 (simplify
4867   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4868   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4869     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4871 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4872 (for op (plus minus)
4873  (simplify
4874   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4875   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4876        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4877    (with
4878      {
4879        wi::overflow_type overflow;
4880        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4881                                TYPE_SIGN (type), &overflow);
4882      }
4883      (if (types_match (type, TREE_TYPE (@2))
4884          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4885       (op @0 { wide_int_to_tree (type, mul); })
4886       (with { tree utype = unsigned_type_for (type); }
4887        (convert (op (convert:utype @0)
4888                     (mult (convert:utype @1) (convert:utype @2))))))))))
4890 /* Canonicalization of binary operations.  */
4892 /* Convert X + -C into X - C.  */
4893 (simplify
4894  (plus @0 REAL_CST@1)
4895  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4896   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4897    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4898     (minus @0 { tem; })))))
4900 /* Convert x+x into x*2.  */
4901 (simplify
4902  (plus @0 @0)
4903  (if (SCALAR_FLOAT_TYPE_P (type))
4904   (mult @0 { build_real (type, dconst2); })
4905   (if (INTEGRAL_TYPE_P (type))
4906    (mult @0 { build_int_cst (type, 2); }))))
4908 /* 0 - X  ->  -X.  */
4909 (simplify
4910  (minus integer_zerop @1)
4911  (negate @1))
4912 (simplify
4913  (pointer_diff integer_zerop @1)
4914  (negate (convert @1)))
4916 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4917    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4918    (-ARG1 + ARG0) reduces to -ARG1.  */
4919 (simplify
4920  (minus real_zerop@0 @1)
4921  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4922   (negate @1)))
4924 /* Transform x * -1 into -x.  */
4925 (simplify
4926  (mult @0 integer_minus_onep)
4927  (negate @0))
4929 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4930    signed overflow for CST != 0 && CST != -1.  */
4931 (simplify
4932  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4933  (if (TREE_CODE (@2) != INTEGER_CST
4934       && single_use (@3)
4935       && !integer_zerop (@1) && !integer_minus_onep (@1))
4936   (mult (mult @0 @2) @1)))
4938 /* True if we can easily extract the real and imaginary parts of a complex
4939    number.  */
4940 (match compositional_complex
4941  (convert? (complex @0 @1)))
4943 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4944 (simplify
4945  (complex (realpart @0) (imagpart @0))
4946  @0)
4947 (simplify
4948  (realpart (complex @0 @1))
4949  @0)
4950 (simplify
4951  (imagpart (complex @0 @1))
4952  @1)
4954 /* Sometimes we only care about half of a complex expression.  */
4955 (simplify
4956  (realpart (convert?:s (conj:s @0)))
4957  (convert (realpart @0)))
4958 (simplify
4959  (imagpart (convert?:s (conj:s @0)))
4960  (convert (negate (imagpart @0))))
4961 (for part (realpart imagpart)
4962  (for op (plus minus)
4963   (simplify
4964    (part (convert?:s@2 (op:s @0 @1)))
4965    (convert (op (part @0) (part @1))))))
4966 (simplify
4967  (realpart (convert?:s (CEXPI:s @0)))
4968  (convert (COS @0)))
4969 (simplify
4970  (imagpart (convert?:s (CEXPI:s @0)))
4971  (convert (SIN @0)))
4973 /* conj(conj(x)) -> x  */
4974 (simplify
4975  (conj (convert? (conj @0)))
4976  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4977   (convert @0)))
4979 /* conj({x,y}) -> {x,-y}  */
4980 (simplify
4981  (conj (convert?:s (complex:s @0 @1)))
4982  (with { tree itype = TREE_TYPE (type); }
4983   (complex (convert:itype @0) (negate (convert:itype @1)))))
4985 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4986 (for bswap (BSWAP)
4987  (simplify
4988   (bswap (bswap @0))
4989   @0)
4990  (simplify
4991   (bswap (bit_not (bswap @0)))
4992   (bit_not @0))
4993  (for bitop (bit_xor bit_ior bit_and)
4994   (simplify
4995    (bswap (bitop:c (bswap @0) @1))
4996    (bitop @0 (bswap @1))))
4997  (for cmp (eq ne)
4998   (simplify
4999    (cmp (bswap@2 @0) (bswap @1))
5000    (with { tree ctype = TREE_TYPE (@2); }
5001     (cmp (convert:ctype @0) (convert:ctype @1))))
5002   (simplify
5003    (cmp (bswap @0) INTEGER_CST@1)
5004    (with { tree ctype = TREE_TYPE (@1); }
5005     (cmp (convert:ctype @0) (bswap! @1)))))
5006  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
5007  (simplify
5008   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5009            INTEGER_CST@3)
5010    (if (BITS_PER_UNIT == 8
5011         && tree_fits_uhwi_p (@2)
5012         && tree_fits_uhwi_p (@3))
5013     (with
5014      {
5015       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5016       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5017       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5018       unsigned HOST_WIDE_INT lo = bits & 7;
5019       unsigned HOST_WIDE_INT hi = bits - lo;
5020      }
5021      (if (bits < prec
5022           && mask < (256u>>lo)
5023           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5024       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5025        (if (ns == 0)
5026         (bit_and (convert @1) @3)
5027         (with
5028          {
5029           tree utype = unsigned_type_for (TREE_TYPE (@1));
5030           tree nst = build_int_cst (integer_type_node, ns);
5031          }
5032          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5033  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
5034  (simplify
5035   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5036    (if (BITS_PER_UNIT == 8
5037         && CHAR_TYPE_SIZE == 8
5038         && tree_fits_uhwi_p (@1))
5039     (with
5040      {
5041       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5042       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5043       /* If the bswap was extended before the original shift, this
5044          byte (shift) has the sign of the extension, not the sign of
5045          the original shift.  */
5046       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5047      }
5048      /* Special case: logical right shift of sign-extended bswap.
5049         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5050      (if (TYPE_PRECISION (type) > prec
5051           && !TYPE_UNSIGNED (TREE_TYPE (@2))
5052           && TYPE_UNSIGNED (type)
5053           && bits < prec && bits + 8 >= prec)
5054       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5055        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5056       (if (bits + 8 == prec)
5057        (if (TYPE_UNSIGNED (st))
5058         (convert (convert:unsigned_char_type_node @0))
5059         (convert (convert:signed_char_type_node @0)))
5060        (if (bits < prec && bits + 8 > prec)
5061         (with 
5062          {
5063           tree nst = build_int_cst (integer_type_node, bits & 7);
5064           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5065                                        : signed_char_type_node;
5066          }
5067          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5068  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
5069  (simplify
5070   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5071    (if (BITS_PER_UNIT == 8
5072         && tree_fits_uhwi_p (@1)
5073         && tree_to_uhwi (@1) < 256)
5074     (with
5075      {
5076       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5077       tree utype = unsigned_type_for (TREE_TYPE (@0));
5078       tree nst = build_int_cst (integer_type_node, prec - 8);
5079      }
5080      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5083 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
5085 /* Simplify constant conditions.
5086    Only optimize constant conditions when the selected branch
5087    has the same type as the COND_EXPR.  This avoids optimizing
5088    away "c ? x : throw", where the throw has a void type.
5089    Note that we cannot throw away the fold-const.cc variant nor
5090    this one as we depend on doing this transform before possibly
5091    A ? B : B -> B triggers and the fold-const.cc one can optimize
5092    0 ? A : B to B even if A has side-effects.  Something
5093    genmatch cannot handle.  */
5094 (simplify
5095  (cond INTEGER_CST@0 @1 @2)
5096  (if (integer_zerop (@0))
5097   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5098    @2)
5099   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5100    @1)))
5101 (simplify
5102  (vec_cond VECTOR_CST@0 @1 @2)
5103  (if (integer_all_onesp (@0))
5104   @1
5105   (if (integer_zerop (@0))
5106    @2)))
5108 /* Sink unary operations to branches, but only if we do fold both.  */
5109 (for op (negate bit_not abs absu)
5110  (simplify
5111   (op (vec_cond:s @0 @1 @2))
5112   (vec_cond @0 (op! @1) (op! @2))))
5114 /* Sink unary conversions to branches, but only if we do fold both
5115    and the target's truth type is the same as we already have.  */
5116 (simplify
5117  (convert (vec_cond:s @0 @1 @2))
5118  (if (VECTOR_TYPE_P (type)
5119       && types_match (TREE_TYPE (@0), truth_type_for (type)))
5120   (vec_cond @0 (convert! @1) (convert! @2))))
5122 /* Likewise for view_convert of nop_conversions. */
5123 (simplify
5124  (view_convert (vec_cond:s @0 @1 @2))
5125  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5126       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5127                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5128       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5129   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5131 /* Sink binary operation to branches, but only if we can fold it.  */
5132 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5133          lshift rshift rdiv trunc_div ceil_div floor_div round_div
5134          trunc_mod ceil_mod floor_mod round_mod min max)
5135 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
5136  (simplify
5137   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5138   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5140 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
5141  (simplify
5142   (op (vec_cond:s @0 @1 @2) @3)
5143   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5144  (simplify
5145   (op @3 (vec_cond:s @0 @1 @2))
5146   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5148 #if GIMPLE
5149 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5150  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5151            INTEGER_CST@1)
5152  (with {
5153          int ibit = tree_log2 (@0);
5154          int ibit2 = tree_log2 (@1);
5155        }
5156   (if (ibit == ibit2
5157       && ibit >= 0
5158       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5160 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5161  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5162           INTEGER_CST@1)
5163  (with {
5164          int ibit = tree_log2 (@0);
5165          int ibit2 = tree_log2 (@1);
5166        }
5167   (if (ibit == ibit2
5168       && ibit >= 0
5169       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5171 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5172  (bit_and:c
5173   (convert1?@4
5174    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5175   (convert2? @0))
5176  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5178 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5179  (bit_and:c
5180   (convert1?@4
5181    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5182   (convert2? @0))
5183  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5185 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5186  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5187             INTEGER_CST@1)
5188  (with {
5189          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5190                                               TYPE_PRECISION(type)));
5191          int ibit2 = tree_log2 (@1);
5192        }
5193   (if (ibit == ibit2
5194       && ibit >= 0
5195       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5197 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5198  (bit_and@4
5199   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5200   INTEGER_CST@1)
5201  (with {
5202          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5203                                               TYPE_PRECISION(type)));
5204          int ibit2 = tree_log2 (@1);
5205        }
5206   (if (ibit == ibit2
5207       && ibit >= 0
5208       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5210 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5211  (bit_and:c
5212   (convert1?@3
5213    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5214   (convert2? @0))
5215  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5217 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5218  (bit_and:c
5219   (convert1?@3
5220    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5221   (convert2? @0))
5222   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5224 #endif
5226 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5227    Currently disabled after pass lvec because ARM understands
5228    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5229 #if GIMPLE
5230 /* These can only be done in gimple as fold likes to convert:
5231    (CMP) & N into (CMP) ? N : 0
5232    and we try to match the same pattern again and again. */
5233 (simplify
5234  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5235  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5236   (vec_cond (bit_and @0 @3) @1 @2)))
5237 (simplify
5238  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5239  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5240   (vec_cond (bit_ior @0 @3) @1 @2)))
5241 (simplify
5242  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5243  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5244   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5245 (simplify
5246  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5247  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5248   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5250 /*  ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5251     (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5252     component type of the outer vec_cond is greater equal the inner one.  */
5253 (for cmp (simple_comparison)
5254  (simplify
5255   (vec_cond
5256     (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5257                                     integer_all_onesp
5258                                     integer_zerop))
5259           integer_zerop) @2 @3)
5260   (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5261        && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5262        && !TYPE_UNSIGNED (TREE_TYPE (@5))
5263        && VECTOR_TYPE_P (TREE_TYPE (@6))
5264        && VECTOR_TYPE_P (type)
5265        && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5266                            TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5267        && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5268    (with { tree vtype = TREE_TYPE (@6);}
5269      (view_convert:type
5270        (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5272 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5273 (simplify
5274  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5275  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5276   (vec_cond (bit_and @0 @1) @2 @3)))
5277 (simplify
5278  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5279  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5280   (vec_cond (bit_ior @0 @1) @2 @3)))
5281 (simplify
5282  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5283  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5284   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5285 (simplify
5286  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5287  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5288   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5289 #endif
5291 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5292    types are compatible.  */
5293 (simplify
5294  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5295  (if (VECTOR_BOOLEAN_TYPE_P (type)
5296       && types_match (type, TREE_TYPE (@0)))
5297   (if (integer_zerop (@1) && integer_all_onesp (@2))
5298    (bit_not @0)
5299    (if (integer_all_onesp (@1) && integer_zerop (@2))
5300     @0))))
5302 /* A few simplifications of "a ? CST1 : CST2". */
5303 /* NOTE: Only do this on gimple as the if-chain-to-switch
5304    optimization depends on the gimple to have if statements in it. */
5305 #if GIMPLE
5306 (simplify
5307  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5308  (switch
5309   (if (integer_zerop (@2))
5310    (switch
5311     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5312     (if (integer_onep (@1))
5313      (convert (convert:boolean_type_node @0)))
5314     /* a ? -1 : 0 -> -a.  */
5315     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5316      (if (TYPE_PRECISION (type) == 1)
5317       /* For signed 1-bit precision just cast bool to the type.  */
5318       (convert (convert:boolean_type_node @0))
5319       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5320        (with {
5321           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5322                                                       TYPE_UNSIGNED (type));
5323         }
5324         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5325        (negate (convert:type (convert:boolean_type_node @0))))))
5326     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5327     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5328      (with {
5329        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5330       }
5331       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5332   (if (integer_zerop (@1))
5333    (switch
5334     /* a ? 0 : 1 -> !a.  */
5335     (if (integer_onep (@2))
5336      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5337     /* a ? 0 : -1 -> -(!a).  */
5338     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5339      (if (TYPE_PRECISION (type) == 1)
5340       /* For signed 1-bit precision just cast bool to the type.  */
5341       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5342       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5343        (with {
5344           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5345                                                       TYPE_UNSIGNED (type));
5346         }
5347         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5348                                                 { boolean_true_node; })))))
5349        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5350                                       { boolean_true_node; }))))))
5351     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5352     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5353      (with {
5354        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5355       }
5356       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5357                                 { boolean_true_node; })) { shift; })))))))
5359 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5360    for unsigned types. */
5361 (simplify
5362  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5363  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5364       && bitwise_equal_p (@0, @2))
5365   (convert (eq @0 @1))
5369 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5370    for unsigned types. */
5371 (simplify
5372  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5373  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5374       && bitwise_equal_p (@0, @2))
5375   (convert (eq @0 @1))
5379 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5380    on the first bit of the CST.  */
5381 (simplify
5382  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5383  (if ((wi::to_wide (@1) & 1) != 0)
5384   @2
5385   { build_zero_cst (type); }))
5387 /* Optimize
5388    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5389    x_5 == cstN ? cst4 : cst3
5390    # op is == or != and N is 1 or 2
5391    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5392    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5393    of cst3 and cst4 is smaller.
5394    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5395 (for eqne (ne eq)
5396  (simplify
5397   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5398   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5399        && INTEGRAL_TYPE_P (type)
5400        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5401            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5402    (with {
5403      value_range r;
5404      get_range_query (cfun)->range_of_expr (r, @0);
5405      if (r.undefined_p ())
5406        r.set_varying (TREE_TYPE (@0));
5408      wide_int min = r.lower_bound ();
5409      wide_int max = r.upper_bound ();
5410     }
5411     (if (min + 1 == max
5412          && (wi::to_wide (@1) == min
5413              || wi::to_wide (@1) == max))
5414      (with {
5415        tree arg0 = @2, arg1 = @3;
5416        tree type1;
5417        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5418          std::swap (arg0, arg1);
5419        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5420          type1 = TREE_TYPE (@0);
5421        else
5422          type1 = type;
5423        auto prec = TYPE_PRECISION (type1);
5424        auto unsign = TYPE_UNSIGNED (type1);
5425        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5426         type1 = build_nonstandard_integer_type (prec, unsign);
5427        min = wide_int::from (min, prec,
5428                              TYPE_SIGN (TREE_TYPE (@0)));
5429        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5430                                     TYPE_SIGN (type));
5431        enum tree_code code;
5432        wi::overflow_type ovf;
5433        if (tree_int_cst_lt (arg0, arg1))
5434          {
5435            code = PLUS_EXPR;
5436            a -= min;
5437            if (!unsign)
5438              {
5439                /* lhs is known to be in range [min, min+1] and we want to add a
5440                   to it.  Check if that operation can overflow for those 2 values
5441                   and if yes, force unsigned type.  */
5442                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5443                if (ovf)
5444                  type1 = unsigned_type_for (type1);
5445              }
5446          }
5447        else
5448          {
5449            code = MINUS_EXPR;
5450            a += min;
5451            if (!unsign)
5452              {
5453                /* lhs is known to be in range [min, min+1] and we want to subtract
5454                   it from a.  Check if that operation can overflow for those 2
5455                   values and if yes, force unsigned type.  */
5456                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5457                if (ovf)
5458                 type1 = unsigned_type_for (type1);
5459              }
5460          }
5461        tree arg = wide_int_to_tree (type1, a);
5462       }
5463       (if (code == PLUS_EXPR)
5464        (convert (plus (convert:type1 @0) { arg; }))
5465        (convert (minus { arg; } (convert:type1 @0))))))))))
5466 #endif
5468 (simplify
5469  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5470  (if (INTEGRAL_TYPE_P (type)
5471       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5472   (cond @1 (convert @2) (convert @3))))
5474 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5475    be extended.  */
5476 /* This pattern implements two kinds simplification:
5478    Case 1)
5479    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5480      1) Conversions are type widening from smaller type.
5481      2) Const c1 equals to c2 after canonicalizing comparison.
5482      3) Comparison has tree code LT, LE, GT or GE.
5483    This specific pattern is needed when (cmp (convert x) c) may not
5484    be simplified by comparison patterns because of multiple uses of
5485    x.  It also makes sense here because simplifying across multiple
5486    referred var is always benefitial for complicated cases.
5488    Case 2)
5489    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5490 (for cmp (lt le gt ge eq ne)
5491  (simplify
5492   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5493   (with
5494    {
5495      tree from_type = TREE_TYPE (@1);
5496      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5497      enum tree_code code = ERROR_MARK;
5499      if (INTEGRAL_TYPE_P (from_type)
5500          && int_fits_type_p (@2, from_type)
5501          && (types_match (c1_type, from_type)
5502              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5503                  && (TYPE_UNSIGNED (from_type)
5504                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5505          && (types_match (c2_type, from_type)
5506              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5507                  && (TYPE_UNSIGNED (from_type)
5508                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5509        {
5510          if (cmp != EQ_EXPR)
5511            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5512          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5513          else if (int_fits_type_p (@3, from_type))
5514            code = EQ_EXPR;
5515        }
5516    }
5517    (if (code == MAX_EXPR)
5518     (convert (max @1 (convert @2)))
5519     (if (code == MIN_EXPR)
5520      (convert (min @1 (convert @2)))
5521      (if (code == EQ_EXPR)
5522       (convert (cond (eq @1 (convert @3))
5523                      (convert:from_type @3) (convert:from_type @2)))))))))
5525 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5527      1) OP is PLUS or MINUS.
5528      2) CMP is LT, LE, GT or GE.
5529      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5531    This pattern also handles special cases like:
5533      A) Operand x is a unsigned to signed type conversion and c1 is
5534         integer zero.  In this case,
5535           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5536           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5537      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5538         check equality for (c1+1) and (c1-1) by adjusting comparison
5539         code.
5541    TODO: Though signed type is handled by this pattern, it cannot be
5542    simplified at the moment because C standard requires additional
5543    type promotion.  In order to match&simplify it here, the IR needs
5544    to be cleaned up by other optimizers, i.e, VRP.  */
5545 (for op (plus minus)
5546  (for cmp (lt le gt ge)
5547   (simplify
5548    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5549    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5550     (if (types_match (from_type, to_type)
5551          /* Check if it is special case A).  */
5552          || (TYPE_UNSIGNED (from_type)
5553              && !TYPE_UNSIGNED (to_type)
5554              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5555              && integer_zerop (@1)
5556              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5557      (with
5558       {
5559         wi::overflow_type overflow = wi::OVF_NONE;
5560         enum tree_code code, cmp_code = cmp;
5561         wide_int real_c1;
5562         wide_int c1 = wi::to_wide (@1);
5563         wide_int c2 = wi::to_wide (@2);
5564         wide_int c3 = wi::to_wide (@3);
5565         signop sgn = TYPE_SIGN (from_type);
5567         /* Handle special case A), given x of unsigned type:
5568             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5569             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5570         if (!types_match (from_type, to_type))
5571           {
5572             if (cmp_code == LT_EXPR)
5573               cmp_code = GT_EXPR;
5574             if (cmp_code == GE_EXPR)
5575               cmp_code = LE_EXPR;
5576             c1 = wi::max_value (to_type);
5577           }
5578         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5579            compute (c3 op' c2) and check if it equals to c1 with op' being
5580            the inverted operator of op.  Make sure overflow doesn't happen
5581            if it is undefined.  */
5582         if (op == PLUS_EXPR)
5583           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5584         else
5585           real_c1 = wi::add (c3, c2, sgn, &overflow);
5587         code = cmp_code;
5588         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5589           {
5590             /* Check if c1 equals to real_c1.  Boundary condition is handled
5591                by adjusting comparison operation if necessary.  */
5592             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5593                 && !overflow)
5594               {
5595                 /* X <= Y - 1 equals to X < Y.  */
5596                 if (cmp_code == LE_EXPR)
5597                   code = LT_EXPR;
5598                 /* X > Y - 1 equals to X >= Y.  */
5599                 if (cmp_code == GT_EXPR)
5600                   code = GE_EXPR;
5601               }
5602             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5603                 && !overflow)
5604               {
5605                 /* X < Y + 1 equals to X <= Y.  */
5606                 if (cmp_code == LT_EXPR)
5607                   code = LE_EXPR;
5608                 /* X >= Y + 1 equals to X > Y.  */
5609                 if (cmp_code == GE_EXPR)
5610                   code = GT_EXPR;
5611               }
5612             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5613               {
5614                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5615                   code = MIN_EXPR;
5616                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5617                   code = MAX_EXPR;
5618               }
5619           }
5620       }
5621       (if (code == MAX_EXPR)
5622        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5623            { wide_int_to_tree (from_type, c2); })
5624        (if (code == MIN_EXPR)
5625         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5626             { wide_int_to_tree (from_type, c2); })))))))))
5628 #if GIMPLE
5629 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5630    in fold_cond_expr_with_comparison for GENERIC folding with
5631    some extra constraints.  */
5632 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5633  (simplify
5634   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5635         (convert3? @0) (convert4? @1))
5636   (if (!HONOR_SIGNED_ZEROS (type)
5637        && (/* Allow widening conversions of the compare operands as data.  */
5638            (INTEGRAL_TYPE_P (type)
5639             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5640             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5641             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5642             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5643            /* Or sign conversions for the comparison.  */
5644            || (types_match (type, TREE_TYPE (@0))
5645                && types_match (type, TREE_TYPE (@1)))))
5646    (switch
5647     (if (cmp == EQ_EXPR)
5648      (if (VECTOR_TYPE_P (type))
5649       (view_convert @c1)
5650       (convert @c1)))
5651     (if (cmp == NE_EXPR)
5652      (if (VECTOR_TYPE_P (type))
5653       (view_convert @c0)
5654       (convert @c0)))
5655     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5656      (if (!HONOR_NANS (type))
5657       (if (VECTOR_TYPE_P (type))
5658        (view_convert (min @c0 @c1))
5659        (convert (min @c0 @c1)))))
5660     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5661      (if (!HONOR_NANS (type))
5662       (if (VECTOR_TYPE_P (type))
5663        (view_convert (max @c0 @c1))
5664        (convert (max @c0 @c1)))))
5665     (if (cmp == UNEQ_EXPR)
5666      (if (!HONOR_NANS (type))
5667       (if (VECTOR_TYPE_P (type))
5668        (view_convert @c1)
5669        (convert @c1))))
5670     (if (cmp == LTGT_EXPR)
5671      (if (!HONOR_NANS (type))
5672       (if (VECTOR_TYPE_P (type))
5673        (view_convert @c0)
5674        (convert @c0))))))))
5675 #endif
5677 (for cnd (cond vec_cond)
5678  /* (a != b) ? (a - b) : 0 -> (a - b) */
5679  (simplify
5680   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5681   @2)
5682  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5683  (simplify
5684   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5685   @2)
5686  /* (a != b) ? (a & b) : a -> (a & b) */
5687  /* (a != b) ? (a | b) : a -> (a | b) */
5688  /* (a != b) ? min(a,b) : a -> min(a,b) */
5689  /* (a != b) ? max(a,b) : a -> max(a,b) */
5690  (for op (bit_and bit_ior min max)
5691   (simplify
5692    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5693    @2))
5694  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5695  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5696  (for op (mult plus)
5697   (simplify
5698    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5699    (if (ANY_INTEGRAL_TYPE_P (type))
5700     @2)))
5701  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5702  (simplify
5703   (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5704   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5705    @2))
5708 /* These was part of minmax phiopt.  */
5709 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5710    to minmax<min/max<a, b>, c> */
5711 (for minmax (min max)
5712  (for cmp (lt le gt ge ne)
5713   (simplify
5714    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5715    (with
5716     {
5717       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5718     }
5719     (if (code == MIN_EXPR)
5720      (minmax (min @1 @2) @4)
5721      (if (code == MAX_EXPR)
5722       (minmax (max @1 @2) @4)))))))
5724 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5725 (for cmp    (gt  ge  lt  le)
5726      minmax (min min max max)
5727  (simplify
5728   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5729    (with
5730     {
5731       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5732     }
5733     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5734          && code == MIN_EXPR
5735          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5736      (min @2 @4)
5737      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5738           && code == MAX_EXPR
5739           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5740       (max @2 @4))))))
5742 #if GIMPLE
5743 /* These patterns should be after min/max detection as simplifications
5744    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5745    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5746    Even without those, reaching min/max/and/ior faster is better.  */
5747 (simplify
5748  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5749  (switch
5750   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5751   (if (integer_zerop (@2))
5752    (bit_and (convert @0) @1))
5753   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5754   (if (integer_zerop (@1))
5755    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5756   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5757   (if (integer_onep (@1))
5758    (bit_ior (convert @0) @2))
5759   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5760   (if (integer_onep (@2))
5761    (bit_ior (bit_xor (convert @0) @2) @1))
5764 #endif
5766 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5767 (simplify
5768  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5769  (if (!TYPE_SATURATING (type)
5770       && (TYPE_OVERFLOW_WRAPS (type)
5771           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5772       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5773   @3))
5775 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5776 (simplify
5777  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5778  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5779   @3))
5781 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5782 (for op (abs absu)
5783  (simplify
5784   (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5785   (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5786    (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5787     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5788      (convert (absu:utype @0)))
5789     @3))))
5791 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5792    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5793 (simplify
5794  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5795  (if (TYPE_UNSIGNED (type))
5796   (cond (ge @0 @1) (negate @0) @2)))
5798 (for cnd (cond vec_cond)
5799  /* A ? B : (A ? X : C) -> A ? B : C.  */
5800  (simplify
5801   (cnd @0 (cnd @0 @1 @2) @3)
5802   (cnd @0 @1 @3))
5803  (simplify
5804   (cnd @0 @1 (cnd @0 @2 @3))
5805   (cnd @0 @1 @3))
5806  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5807  /* ???  This matches embedded conditions open-coded because genmatch
5808     would generate matching code for conditions in separate stmts only.
5809     The following is still important to merge then and else arm cases
5810     from if-conversion.  */
5811  (simplify
5812   (cnd @0 @1 (cnd @2 @3 @4))
5813   (if (inverse_conditions_p (@0, @2))
5814    (cnd @0 @1 @3)))
5815  (simplify
5816   (cnd @0 (cnd @1 @2 @3) @4)
5817   (if (inverse_conditions_p (@0, @1))
5818    (cnd @0 @3 @4)))
5820  /* A ? B : B -> B.  */
5821  (simplify
5822   (cnd @0 @1 @1)
5823   @1)
5825  /* !A ? B : C -> A ? C : B.  */
5826  (simplify
5827   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5828   (cnd @0 @2 @1)))
5830 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5832    None of these transformations work for modes with signed
5833    zeros.  If A is +/-0, the first two transformations will
5834    change the sign of the result (from +0 to -0, or vice
5835    versa).  The last four will fix the sign of the result,
5836    even though the original expressions could be positive or
5837    negative, depending on the sign of A.
5839    Note that all these transformations are correct if A is
5840    NaN, since the two alternatives (A and -A) are also NaNs.  */
5842 (for cnd (cond vec_cond)
5843  /* A == 0 ? A : -A    same as -A */
5844  (for cmp (eq uneq)
5845   (simplify
5846    (cnd (cmp @0 zerop) @2 (negate@1 @2))
5847     (if (!HONOR_SIGNED_ZEROS (type)
5848          && bitwise_equal_p (@0, @2))
5849      @1))
5850   (simplify
5851    (cnd (cmp @0 zerop) zerop (negate@1 @2))
5852     (if (!HONOR_SIGNED_ZEROS (type)
5853          && bitwise_equal_p (@0, @2))
5854      @1))
5856  /* A != 0 ? A : -A    same as A */
5857  (for cmp (ne ltgt)
5858   (simplify
5859    (cnd (cmp @0 zerop) @1 (negate @1))
5860     (if (!HONOR_SIGNED_ZEROS (type)
5861          && bitwise_equal_p (@0, @1))
5862      @1))
5863   (simplify
5864    (cnd (cmp @0 zerop) @1 integer_zerop)
5865     (if (!HONOR_SIGNED_ZEROS (type)
5866          && bitwise_equal_p (@0, @1))
5867      @1))
5869  /* A >=/> 0 ? A : -A    same as abs (A) */
5870  (for cmp (ge gt)
5871   (simplify
5872    (cnd (cmp @0 zerop) @1 (negate @1))
5873     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5874          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5875          && bitwise_equal_p (@0, @1))
5876      (if (TYPE_UNSIGNED (type))
5877       (absu:type @0)
5878       (abs @0)))))
5879  /* A <=/< 0 ? A : -A    same as -abs (A) */
5880  (for cmp (le lt)
5881   (simplify
5882    (cnd (cmp @0 zerop) @1 (negate @1))
5883     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5884          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5885          && bitwise_equal_p (@0, @1))
5886      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5887            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5888           || TYPE_UNSIGNED (type))
5889       (with {
5890         tree utype = unsigned_type_for (TREE_TYPE(@0));
5891        }
5892        (convert (negate (absu:utype @0))))
5893        (negate (abs @0)))))
5896  /* (A - B) == 0 ? (A - B) : (B - A)    same as (B - A) */
5897  (for cmp (eq uneq)
5898   (simplify
5899    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5900    (if (!HONOR_SIGNED_ZEROS (type))
5901     @3))
5902   (simplify
5903    (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5904    @3)
5906  /* (A - B) != 0 ? (A - B) : (B - A)    same as (A - B) */
5907  (for cmp (ne ltgt)
5908   (simplify
5909    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5910    (if (!HONOR_SIGNED_ZEROS (type))
5911     @0))
5912   (simplify
5913    (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5914    @0)
5916  /* (A - B) >=/> 0 ? (A - B) : (B - A)    same as abs (A - B) */
5917  (for cmp (ge gt)
5918   (simplify
5919    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5920    (if (!HONOR_SIGNED_ZEROS (type)
5921         && !TYPE_UNSIGNED (type))
5922     (abs @0))))
5923  /* (A - B) <=/< 0 ? (A - B) : (B - A)    same as -abs (A - B) */
5924  (for cmp (le lt)
5925   (simplify
5926    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5927    (if (!HONOR_SIGNED_ZEROS (type)
5928         && !TYPE_UNSIGNED (type))
5929     (if (ANY_INTEGRAL_TYPE_P (type)
5930          && !TYPE_OVERFLOW_WRAPS (type))
5931      (with {
5932         tree utype = unsigned_type_for (type);
5933       }
5934       (convert (negate (absu:utype @0))))
5935       (negate (abs @0)))))
5939 /* -(type)!A -> (type)A - 1.  */
5940 (simplify
5941  (negate (convert?:s (logical_inverted_value:s @0)))
5942  (if (INTEGRAL_TYPE_P (type)
5943       && TREE_CODE (type) != BOOLEAN_TYPE
5944       && TYPE_PRECISION (type) > 1
5945       && TREE_CODE (@0) == SSA_NAME
5946       && ssa_name_has_boolean_range (@0))
5947   (plus (convert:type @0) { build_all_ones_cst (type); })))
5949 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5950    return all -1 or all 0 results.  */
5951 /* ??? We could instead convert all instances of the vec_cond to negate,
5952    but that isn't necessarily a win on its own.  */
5953 (simplify
5954  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5955  (if (VECTOR_TYPE_P (type)
5956       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5957                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5958       && (TYPE_MODE (TREE_TYPE (type))
5959           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5960   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5962 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5963 (simplify
5964  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5965  (if (VECTOR_TYPE_P (type)
5966       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5967                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5968       && (TYPE_MODE (TREE_TYPE (type))
5969           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5970   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5973 /* Simplifications of comparisons.  */
5975 /* See if we can reduce the magnitude of a constant involved in a
5976    comparison by changing the comparison code.  This is a canonicalization
5977    formerly done by maybe_canonicalize_comparison_1.  */
5978 (for cmp  (le gt)
5979      acmp (lt ge)
5980  (simplify
5981   (cmp @0 uniform_integer_cst_p@1)
5982   (with { tree cst = uniform_integer_cst_p (@1); }
5983    (if (tree_int_cst_sgn (cst) == -1)
5984      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5985                                    wide_int_to_tree (TREE_TYPE (cst),
5986                                                      wi::to_wide (cst)
5987                                                      + 1)); })))))
5988 (for cmp  (ge lt)
5989      acmp (gt le)
5990  (simplify
5991   (cmp @0 uniform_integer_cst_p@1)
5992   (with { tree cst = uniform_integer_cst_p (@1); }
5993    (if (tree_int_cst_sgn (cst) == 1)
5994     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5995                                   wide_int_to_tree (TREE_TYPE (cst),
5996                                   wi::to_wide (cst) - 1)); })))))
5998 /* We can simplify a logical negation of a comparison to the
5999    inverted comparison.  As we cannot compute an expression
6000    operator using invert_tree_comparison we have to simulate
6001    that with expression code iteration.  */
6002 (for cmp (tcc_comparison)
6003      icmp (inverted_tcc_comparison)
6004      ncmp (inverted_tcc_comparison_with_nans)
6005  /* Ideally we'd like to combine the following two patterns
6006     and handle some more cases by using
6007       (logical_inverted_value (cmp @0 @1))
6008     here but for that genmatch would need to "inline" that.
6009     For now implement what forward_propagate_comparison did.  */
6010  (simplify
6011   (bit_not (cmp @0 @1))
6012   (if (VECTOR_TYPE_P (type)
6013        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6014    /* Comparison inversion may be impossible for trapping math,
6015       invert_tree_comparison will tell us.  But we can't use
6016       a computed operator in the replacement tree thus we have
6017       to play the trick below.  */
6018    (with { enum tree_code ic = invert_tree_comparison
6019              (cmp, HONOR_NANS (@0)); }
6020     (if (ic == icmp)
6021      (icmp @0 @1)
6022      (if (ic == ncmp)
6023       (ncmp @0 @1))))))
6024  (simplify
6025   (bit_xor (cmp @0 @1) integer_truep)
6026   (with { enum tree_code ic = invert_tree_comparison
6027             (cmp, HONOR_NANS (@0)); }
6028    (if (ic == icmp)
6029     (icmp @0 @1)
6030     (if (ic == ncmp)
6031      (ncmp @0 @1)))))
6032  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
6033  (simplify
6034   (ne (cmp@2 @0 @1) integer_zerop)
6035   (if (types_match (type, TREE_TYPE (@2)))
6036    (cmp @0 @1)))
6037  (simplify
6038   (eq (cmp@2 @0 @1) integer_truep)
6039   (if (types_match (type, TREE_TYPE (@2)))
6040    (cmp @0 @1)))
6041  (simplify
6042   (ne (cmp@2 @0 @1) integer_truep)
6043   (if (types_match (type, TREE_TYPE (@2)))
6044    (with { enum tree_code ic = invert_tree_comparison
6045              (cmp, HONOR_NANS (@0)); }
6046     (if (ic == icmp)
6047      (icmp @0 @1)
6048      (if (ic == ncmp)
6049       (ncmp @0 @1))))))
6050  (simplify
6051   (eq (cmp@2 @0 @1) integer_zerop)
6052   (if (types_match (type, TREE_TYPE (@2)))
6053    (with { enum tree_code ic = invert_tree_comparison
6054              (cmp, HONOR_NANS (@0)); }
6055     (if (ic == icmp)
6056      (icmp @0 @1)
6057      (if (ic == ncmp)
6058       (ncmp @0 @1)))))))
6060 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6061    ??? The transformation is valid for the other operators if overflow
6062    is undefined for the type, but performing it here badly interacts
6063    with the transformation in fold_cond_expr_with_comparison which
6064    attempts to synthetize ABS_EXPR.  */
6065 (for cmp (eq ne)
6066  (for sub (minus pointer_diff)
6067   (simplify
6068    (cmp (sub@2 @0 @1) integer_zerop)
6069    (if (single_use (@2))
6070     (cmp @0 @1)))))
6072 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6073    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
6074 (for cmp (lt ge)
6075  (simplify
6076   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6077    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6078         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6079         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6080     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6081 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6082    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
6083 (simplify
6084  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6085   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6086        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6087        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6088    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6090 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6091    signed arithmetic case.  That form is created by the compiler
6092    often enough for folding it to be of value.  One example is in
6093    computing loop trip counts after Operator Strength Reduction.  */
6094 (for cmp (simple_comparison)
6095      scmp (swapped_simple_comparison)
6096  (simplify
6097   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6098   /* Handle unfolded multiplication by zero.  */
6099   (if (integer_zerop (@1))
6100    (cmp @1 @2)
6101    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6102         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6103         && single_use (@3))
6104     /* If @1 is negative we swap the sense of the comparison.  */
6105     (if (tree_int_cst_sgn (@1) < 0)
6106      (scmp @0 @2)
6107      (cmp @0 @2))))))
6109 /* For integral types with undefined overflow fold
6110    x * C1 == C2 into x == C2 / C1 or false.
6111    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6112    Z / 2^n Z.  */
6113 (for cmp (eq ne)
6114  (simplify
6115   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6116   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6117        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6118        && wi::to_wide (@1) != 0)
6119    (with { widest_int quot; }
6120     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6121                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
6122      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6123      { constant_boolean_node (cmp == NE_EXPR, type); }))
6124    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6125         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6126         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6127     (cmp @0
6128      {
6129        tree itype = TREE_TYPE (@0);
6130        int p = TYPE_PRECISION (itype);
6131        wide_int m = wi::one (p + 1) << p;
6132        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6133        wide_int i = wide_int::from (wi::mod_inv (a, m),
6134                                     p, TYPE_SIGN (itype));
6135        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6136      })))))
6138 /* Simplify comparison of something with itself.  For IEEE
6139    floating-point, we can only do some of these simplifications.  */
6140 (for cmp (eq ge le)
6141  (simplify
6142   (cmp @0 @0)
6143   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6144        || ! tree_expr_maybe_nan_p (@0))
6145    { constant_boolean_node (true, type); }
6146    (if (cmp != EQ_EXPR
6147         /* With -ftrapping-math conversion to EQ loses an exception.  */
6148         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6149             || ! flag_trapping_math))
6150     (eq @0 @0)))))
6151 (for cmp (ne gt lt)
6152  (simplify
6153   (cmp @0 @0)
6154   (if (cmp != NE_EXPR
6155        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6156        || ! tree_expr_maybe_nan_p (@0))
6157    { constant_boolean_node (false, type); })))
6158 (for cmp (unle unge uneq)
6159  (simplify
6160   (cmp @0 @0)
6161   { constant_boolean_node (true, type); }))
6162 (for cmp (unlt ungt)
6163  (simplify
6164   (cmp @0 @0)
6165   (unordered @0 @0)))
6166 (simplify
6167  (ltgt @0 @0)
6168  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6169   { constant_boolean_node (false, type); }))
6171 /* x == ~x -> false */
6172 /* x != ~x -> true */
6173 (for cmp (eq ne)
6174  (simplify
6175   (cmp:c @0 (bit_not @0))
6176   { constant_boolean_node (cmp == NE_EXPR, type); }))
6178 /* Fold ~X op ~Y as Y op X.  */
6179 (for cmp (simple_comparison)
6180  (simplify
6181   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6182   (if (single_use (@2) && single_use (@3))
6183    (with { tree otype = TREE_TYPE (@4); }
6184     (cmp (convert:otype @1) (convert:otype @0))))))
6186 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
6187 (for cmp (simple_comparison)
6188      scmp (swapped_simple_comparison)
6189  (simplify
6190   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6191   (if (single_use (@2)
6192        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6193    (with { tree otype = TREE_TYPE (@1); }
6194     (scmp (convert:otype @0) (bit_not @1))))))
6196 (for cmp (simple_comparison)
6197  (simplify
6198   (cmp @0 REAL_CST@1)
6199   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
6200   (switch
6201    /* a CMP (-0) -> a CMP 0  */
6202    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6203     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6204    /* (-0) CMP b -> 0 CMP b.  */
6205    (if (TREE_CODE (@0) == REAL_CST
6206         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6207     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6208    /* x != NaN is always true, other ops are always false.  */
6209    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6210         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6211         && !tree_expr_signaling_nan_p (@1)
6212         && !tree_expr_maybe_signaling_nan_p (@0))
6213     { constant_boolean_node (cmp == NE_EXPR, type); })
6214    /* NaN != y is always true, other ops are always false.  */
6215    (if (TREE_CODE (@0) == REAL_CST
6216         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6217         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6218         && !tree_expr_signaling_nan_p (@0)
6219         && !tree_expr_signaling_nan_p (@1))
6220     { constant_boolean_node (cmp == NE_EXPR, type); })
6221    /* Fold comparisons against infinity.  */
6222    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6223         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6224     (with
6225      {
6226        REAL_VALUE_TYPE max;
6227        enum tree_code code = cmp;
6228        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6229        if (neg)
6230          code = swap_tree_comparison (code);
6231      }
6232      (switch
6233       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6234       (if (code == GT_EXPR
6235            && !(HONOR_NANS (@0) && flag_trapping_math))
6236        { constant_boolean_node (false, type); })
6237       (if (code == LE_EXPR)
6238        /* x <= +Inf is always true, if we don't care about NaNs.  */
6239        (if (! HONOR_NANS (@0))
6240         { constant_boolean_node (true, type); }
6241         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6242            an "invalid" exception.  */
6243         (if (!flag_trapping_math)
6244          (eq @0 @0))))
6245       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6246          for == this introduces an exception for x a NaN.  */
6247       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6248            || code == GE_EXPR)
6249        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6250         (if (neg)
6251          (lt @0 { build_real (TREE_TYPE (@0), max); })
6252          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6253       /* x < +Inf is always equal to x <= DBL_MAX.  */
6254       (if (code == LT_EXPR)
6255        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6256         (if (neg)
6257          (ge @0 { build_real (TREE_TYPE (@0), max); })
6258          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6259       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6260          an exception for x a NaN so use an unordered comparison.  */
6261       (if (code == NE_EXPR)
6262        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6263         (if (! HONOR_NANS (@0))
6264          (if (neg)
6265           (ge @0 { build_real (TREE_TYPE (@0), max); })
6266           (le @0 { build_real (TREE_TYPE (@0), max); }))
6267          (if (neg)
6268           (unge @0 { build_real (TREE_TYPE (@0), max); })
6269           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6271  /* If this is a comparison of a real constant with a PLUS_EXPR
6272     or a MINUS_EXPR of a real constant, we can convert it into a
6273     comparison with a revised real constant as long as no overflow
6274     occurs when unsafe_math_optimizations are enabled.  */
6275  (if (flag_unsafe_math_optimizations)
6276   (for op (plus minus)
6277    (simplify
6278     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6279     (with
6280      {
6281        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6282                                TREE_TYPE (@1), @2, @1);
6283      }
6284      (if (tem && !TREE_OVERFLOW (tem))
6285       (cmp @0 { tem; }))))))
6287  /* Likewise, we can simplify a comparison of a real constant with
6288     a MINUS_EXPR whose first operand is also a real constant, i.e.
6289     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6290     floating-point types only if -fassociative-math is set.  */
6291  (if (flag_associative_math)
6292   (simplify
6293    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6294    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6295     (if (tem && !TREE_OVERFLOW (tem))
6296      (cmp { tem; } @1)))))
6298  /* Fold comparisons against built-in math functions.  */
6299  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6300   (for sq (SQRT)
6301    (simplify
6302     (cmp (sq @0) REAL_CST@1)
6303     (switch
6304      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6305       (switch
6306        /* sqrt(x) < y is always false, if y is negative.  */
6307        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6308         { constant_boolean_node (false, type); })
6309        /* sqrt(x) > y is always true, if y is negative and we
6310           don't care about NaNs, i.e. negative values of x.  */
6311        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6312         { constant_boolean_node (true, type); })
6313        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6314        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6315      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6316       (switch
6317        /* sqrt(x) < 0 is always false.  */
6318        (if (cmp == LT_EXPR)
6319         { constant_boolean_node (false, type); })
6320        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6321        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6322         { constant_boolean_node (true, type); })
6323        /* sqrt(x) <= 0 -> x == 0.  */
6324        (if (cmp == LE_EXPR)
6325         (eq @0 @1))
6326        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6327           == or !=.  In the last case:
6329             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6331           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6332           the results for other x follow from natural arithmetic.  */
6333        (cmp @0 @1)))
6334      (if ((cmp == LT_EXPR
6335            || cmp == LE_EXPR
6336            || cmp == GT_EXPR
6337            || cmp == GE_EXPR)
6338           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6339           /* Give up for -frounding-math.  */
6340           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6341       (with
6342        {
6343          REAL_VALUE_TYPE c2;
6344          enum tree_code ncmp = cmp;
6345          const real_format *fmt
6346            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6347          real_arithmetic (&c2, MULT_EXPR,
6348                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6349          real_convert (&c2, fmt, &c2);
6350          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6351             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6352          if (!REAL_VALUE_ISINF (c2))
6353            {
6354              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6355                                         build_real (TREE_TYPE (@0), c2));
6356              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6357                ncmp = ERROR_MARK;
6358              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6359                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6360                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6361              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6362                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6363                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6364              else
6365                {
6366                  /* With rounding to even, sqrt of up to 3 different values
6367                     gives the same normal result, so in some cases c2 needs
6368                     to be adjusted.  */
6369                  REAL_VALUE_TYPE c2alt, tow;
6370                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6371                    tow = dconst0;
6372                  else
6373                    tow = dconstinf;
6374                  real_nextafter (&c2alt, fmt, &c2, &tow);
6375                  real_convert (&c2alt, fmt, &c2alt);
6376                  if (REAL_VALUE_ISINF (c2alt))
6377                    ncmp = ERROR_MARK;
6378                  else
6379                    {
6380                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6381                                            build_real (TREE_TYPE (@0), c2alt));
6382                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6383                        ncmp = ERROR_MARK;
6384                      else if (real_equal (&TREE_REAL_CST (c3),
6385                                           &TREE_REAL_CST (@1)))
6386                        c2 = c2alt;
6387                    }
6388                }
6389            }
6390        }
6391        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6392         (if (REAL_VALUE_ISINF (c2))
6393          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6394          (if (HONOR_INFINITIES (@0))
6395           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6396           { constant_boolean_node (false, type); })
6397          /* sqrt(x) > c is the same as x > c*c.  */
6398          (if (ncmp != ERROR_MARK)
6399           (if (ncmp == GE_EXPR)
6400            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6401            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6402         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6403         (if (REAL_VALUE_ISINF (c2))
6404          (switch
6405           /* sqrt(x) < y is always true, when y is a very large
6406              value and we don't care about NaNs or Infinities.  */
6407           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6408            { constant_boolean_node (true, type); })
6409           /* sqrt(x) < y is x != +Inf when y is very large and we
6410              don't care about NaNs.  */
6411           (if (! HONOR_NANS (@0))
6412            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6413           /* sqrt(x) < y is x >= 0 when y is very large and we
6414              don't care about Infinities.  */
6415           (if (! HONOR_INFINITIES (@0))
6416            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6417           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6418           (if (GENERIC)
6419            (truth_andif
6420             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6421             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6422          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6423          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6424           (if (ncmp == LT_EXPR)
6425            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6426            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6427           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6428           (if (ncmp != ERROR_MARK && GENERIC)
6429            (if (ncmp == LT_EXPR)
6430             (truth_andif
6431              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6432              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6433             (truth_andif
6434              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6435              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6436    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6437    (simplify
6438     (cmp (sq @0) (sq @1))
6439       (if (! HONOR_NANS (@0))
6440         (cmp @0 @1))))))
6442 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6443 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6444      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6445  (simplify
6446   (cmp (float@0 @1) (float @2))
6447    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6448         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6449     (with
6450      {
6451        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6452        tree type1 = TREE_TYPE (@1);
6453        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6454        tree type2 = TREE_TYPE (@2);
6455        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6456      }
6457      (if (fmt.can_represent_integral_type_p (type1)
6458           && fmt.can_represent_integral_type_p (type2))
6459       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6460        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6461        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6462             && type1_signed_p >= type2_signed_p)
6463         (icmp @1 (convert @2))
6464         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6465              && type1_signed_p <= type2_signed_p)
6466          (icmp (convert:type2 @1) @2)
6467          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6468               && type1_signed_p == type2_signed_p)
6469           (icmp @1 @2))))))))))
6471 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6472 (for cmp  (lt le eq ne ge gt)
6473      icmp (le le eq ne ge ge)
6474  (simplify
6475   (cmp (float @0) REAL_CST@1)
6476    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6477         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6478     (with
6479      {
6480        tree itype = TREE_TYPE (@0);
6481        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6482        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6483        /* Be careful to preserve any potential exceptions due to
6484           NaNs.  qNaNs are ok in == or != context.
6485           TODO: relax under -fno-trapping-math or
6486           -fno-signaling-nans.  */
6487        bool exception_p
6488          = real_isnan (cst) && (cst->signalling
6489                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6490      }
6491      /* TODO: allow non-fitting itype and SNaNs when
6492         -fno-trapping-math.  */
6493      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6494       (with
6495        {
6496          signop isign = TYPE_SIGN (itype);
6497          REAL_VALUE_TYPE imin, imax;
6498          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6499          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6501          REAL_VALUE_TYPE icst;
6502          if (cmp == GT_EXPR || cmp == GE_EXPR)
6503            real_ceil (&icst, fmt, cst);
6504          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6505            real_floor (&icst, fmt, cst);
6506          else
6507            real_trunc (&icst, fmt, cst);
6509          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6511          bool overflow_p = false;
6512          wide_int icst_val
6513            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6514        }
6515        (switch
6516         /* Optimize cases when CST is outside of ITYPE's range.  */
6517         (if (real_compare (LT_EXPR, cst, &imin))
6518          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6519                                   type); })
6520         (if (real_compare (GT_EXPR, cst, &imax))
6521          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6522                                   type); })
6523         /* Remove cast if CST is an integer representable by ITYPE.  */
6524         (if (cst_int_p)
6525          (cmp @0 { gcc_assert (!overflow_p);
6526                    wide_int_to_tree (itype, icst_val); })
6527         )
6528         /* When CST is fractional, optimize
6529             (FTYPE) N == CST -> 0
6530             (FTYPE) N != CST -> 1.  */
6531         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6532          { constant_boolean_node (cmp == NE_EXPR, type); })
6533         /* Otherwise replace with sensible integer constant.  */
6534         (with
6535          {
6536            gcc_checking_assert (!overflow_p);
6537          }
6538          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6540 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6541 (for cmp (eq ne)
6542  (simplify
6543   (cmp (exact_div @0 @1) INTEGER_CST@2)
6544   (if (!integer_zerop (@1))
6545    (if (wi::to_wide (@2) == 0)
6546     (cmp @0 @2)
6547     (if (TREE_CODE (@1) == INTEGER_CST)
6548      (with
6549       {
6550         wi::overflow_type ovf;
6551         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6552                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6553       }
6554       (if (ovf)
6555        { constant_boolean_node (cmp == NE_EXPR, type); }
6556        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6557 (for cmp (lt le gt ge)
6558  (simplify
6559   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6560   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6561    (with
6562     {
6563       wi::overflow_type ovf;
6564       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6565                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6566     }
6567     (if (ovf)
6568      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6569                                         TYPE_SIGN (TREE_TYPE (@2)))
6570                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6571      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6573 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6575    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6576    For large C (more than min/B+2^size), this is also true, with the
6577    multiplication computed modulo 2^size.
6578    For intermediate C, this just tests the sign of A.  */
6579 (for cmp  (lt le gt ge)
6580      cmp2 (ge ge lt lt)
6581  (simplify
6582   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6583   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6584        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6585        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6586    (with
6587     {
6588       tree utype = TREE_TYPE (@2);
6589       wide_int denom = wi::to_wide (@1);
6590       wide_int right = wi::to_wide (@2);
6591       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6592       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6593       bool small = wi::leu_p (right, smax);
6594       bool large = wi::geu_p (right, smin);
6595     }
6596     (if (small || large)
6597      (cmp (convert:utype @0) (mult @2 (convert @1)))
6598      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6600 /* Unordered tests if either argument is a NaN.  */
6601 (simplify
6602  (bit_ior (unordered @0 @0) (unordered @1 @1))
6603  (if (types_match (@0, @1))
6604   (unordered @0 @1)))
6605 (simplify
6606  (bit_and (ordered @0 @0) (ordered @1 @1))
6607  (if (types_match (@0, @1))
6608   (ordered @0 @1)))
6609 (simplify
6610  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6611  @2)
6612 (simplify
6613  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6614  @2)
6616 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6617    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6619    Note that comparisons
6620      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6621      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6622    will be canonicalized to above so there's no need to
6623    consider them here.
6624  */
6626 (for cmp (le gt)
6627      eqcmp (eq ne)
6628  (simplify
6629   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6630   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6631    (with
6632     {
6633      tree ty = TREE_TYPE (@0);
6634      unsigned prec = TYPE_PRECISION (ty);
6635      wide_int mask = wi::to_wide (@2, prec);
6636      wide_int rhs = wi::to_wide (@3, prec);
6637      signop sgn = TYPE_SIGN (ty);
6638     }
6639     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6640          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6641       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6642              { build_zero_cst (ty); }))))))
6644 /* -A CMP -B -> B CMP A.  */
6645 (for cmp (tcc_comparison)
6646      scmp (swapped_tcc_comparison)
6647  (simplify
6648   (cmp (negate @0) (negate @1))
6649   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6650        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6651            && (cmp == EQ_EXPR
6652                || cmp == NE_EXPR
6653                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6654    (scmp @0 @1)))
6655  (simplify
6656   (cmp (negate @0) CONSTANT_CLASS_P@1)
6657   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6658        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6659            && (cmp == EQ_EXPR
6660                || cmp == NE_EXPR
6661                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6662    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6663     (if (tem && !TREE_OVERFLOW (tem))
6664      (scmp @0 { tem; }))))))
6666 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6667 (for op (abs absu)
6668  (for eqne (eq ne)
6669   (simplify
6670    (eqne (op @0) zerop@1)
6671    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6673 /* From fold_sign_changed_comparison and fold_widened_comparison.
6674    FIXME: the lack of symmetry is disturbing.  */
6675 (for cmp (simple_comparison)
6676  (simplify
6677   (cmp (convert@0 @00) (convert?@1 @10))
6678   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6679        /* Disable this optimization if we're casting a function pointer
6680           type on targets that require function pointer canonicalization.  */
6681        && !(targetm.have_canonicalize_funcptr_for_compare ()
6682             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6683                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6684                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6685                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6686        && single_use (@0))
6687    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6688         && (TREE_CODE (@10) == INTEGER_CST
6689             || @1 != @10)
6690         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6691             || cmp == NE_EXPR
6692             || cmp == EQ_EXPR)
6693         && !POINTER_TYPE_P (TREE_TYPE (@00))
6694         /* (int)bool:32 != (int)uint is not the same as
6695            bool:32 != (bool:32)uint since boolean types only have two valid
6696            values independent of their precision.  */
6697         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6698             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6699     /* ???  The special-casing of INTEGER_CST conversion was in the original
6700        code and here to avoid a spurious overflow flag on the resulting
6701        constant which fold_convert produces.  */
6702     (if (TREE_CODE (@1) == INTEGER_CST)
6703      (cmp @00 { force_fit_type (TREE_TYPE (@00),
6704                                 wide_int::from (wi::to_wide (@1),
6705                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6706                                                      TYPE_PRECISION (TREE_TYPE (@00))),
6707                                                 TYPE_SIGN (TREE_TYPE (@1))),
6708                                 0, TREE_OVERFLOW (@1)); })
6709      (cmp @00 (convert @1)))
6711     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6712      /* If possible, express the comparison in the shorter mode.  */
6713      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6714            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6715            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6716                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6717           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6718               || ((TYPE_PRECISION (TREE_TYPE (@00))
6719                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6720                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6721                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6722               || (TREE_CODE (@10) == INTEGER_CST
6723                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6724                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6725       (cmp @00 (convert @10))
6726       (if (TREE_CODE (@10) == INTEGER_CST
6727            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6728            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6729        (with
6730         {
6731           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6732           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6733           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6734           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6735         }
6736         (if (above || below)
6737          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6738           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6739           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6740            { constant_boolean_node (above ? true : false, type); }
6741            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6742             { constant_boolean_node (above ? false : true, type); })))))))))
6743    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6744    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6745         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6746             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6747         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6748             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6749     (with
6750      {
6751        tree type1 = TREE_TYPE (@10);
6752        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6753          {
6754            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6755            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6756                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6757              type1 = float_type_node;
6758            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6759                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6760              type1 = double_type_node;
6761          }
6762       tree newtype
6763         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6764            ? TREE_TYPE (@00) : type1);
6765      }
6766      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6767       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6770 (for cmp (eq ne)
6771  (simplify
6772   /* SSA names are canonicalized to 2nd place.  */
6773   (cmp addr@0 SSA_NAME@1)
6774   (with
6775    {
6776      poly_int64 off; tree base;
6777      tree addr = (TREE_CODE (@0) == SSA_NAME
6778                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6779    }
6780    /* A local variable can never be pointed to by
6781       the default SSA name of an incoming parameter.  */
6782    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6783         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6784         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6785         && TREE_CODE (base) == VAR_DECL
6786         && auto_var_in_fn_p (base, current_function_decl))
6787     (if (cmp == NE_EXPR)
6788      { constant_boolean_node (true, type); }
6789      { constant_boolean_node (false, type); })
6790     /* If the address is based on @1 decide using the offset.  */
6791     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6792          && TREE_CODE (base) == MEM_REF
6793          && TREE_OPERAND (base, 0) == @1)
6794      (with { off += mem_ref_offset (base).force_shwi (); }
6795       (if (known_ne (off, 0))
6796        { constant_boolean_node (cmp == NE_EXPR, type); }
6797        (if (known_eq (off, 0))
6798         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6800 /* Equality compare simplifications from fold_binary  */
6801 (for cmp (eq ne)
6803  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6804     Similarly for NE_EXPR.  */
6805  (simplify
6806   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6807   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6808        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6809    { constant_boolean_node (cmp == NE_EXPR, type); }))
6811  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6812  (simplify
6813   (cmp (bit_xor @0 @1) integer_zerop)
6814   (cmp @0 @1))
6816  /* (X ^ Y) == Y becomes X == 0.
6817     Likewise (X ^ Y) == X becomes Y == 0.  */
6818  (simplify
6819   (cmp:c (bit_xor:c @0 @1) @0)
6820   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6822  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6823  (simplify
6824   (cmp:c (bit_and:c @0 @1) @0)
6825   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6826  (simplify
6827   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6828   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6829        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6830        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6831        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6832        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6833        && !wi::neg_p (wi::to_wide (@1)))
6834    (cmp (bit_and @0 (convert (bit_not @1)))
6835         { build_zero_cst (TREE_TYPE (@0)); })))
6837  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6838  (simplify
6839   (cmp:c (bit_ior:c @0 @1) @1)
6840   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6842  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6843  (simplify
6844   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6845   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6846    (cmp @0 (bit_xor @1 (convert @2)))))
6848  (simplify
6849   (cmp (nop_convert? @0) integer_zerop)
6850   (if (tree_expr_nonzero_p (@0))
6851    { constant_boolean_node (cmp == NE_EXPR, type); }))
6853  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6854  (simplify
6855   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6856   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6858 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6859    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6860    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6861    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6862 (for cmp (eq ne)
6863      ncmp (ge lt)
6864  (for sgncmp (ge lt)
6865   (simplify
6866    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6867    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6868         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6869         && types_match (@0, @1))
6870     (ncmp (bit_xor @0 @1) @2)))))
6871 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6872    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6873 (for cmp (eq ne)
6874      ncmp (lt ge)
6875  (simplify
6876   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6877    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6878         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6879         && types_match (@0, @1))
6880     (ncmp (bit_xor @0 @1) @2))))
6882 /* If we have (A & C) == C where C is a power of 2, convert this into
6883    (A & C) != 0.  Similarly for NE_EXPR.  */
6884 (for cmp (eq ne)
6885      icmp (ne eq)
6886  (simplify
6887   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6888   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6890 #if GIMPLE
6891 /* From fold_binary_op_with_conditional_arg handle the case of
6892    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6893    compares simplify.  */
6894 (for cmp (simple_comparison)
6895  (simplify
6896   (cmp:c (cond @0 @1 @2) @3)
6897   /* Do not move possibly trapping operations into the conditional as this
6898      pessimizes code and causes gimplification issues when applied late.  */
6899   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6900        || !operation_could_trap_p (cmp, true, false, @3))
6901    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6902 #endif
6904 (for cmp (ge lt)
6905 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6906 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6907  (simplify
6908   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6909    (if (INTEGRAL_TYPE_P (type)
6910         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6911         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6912         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6913     (with
6914      {
6915        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6916      }
6917     (if (cmp == LT_EXPR)
6918      (bit_xor (convert (rshift @0 {shifter;})) @1)
6919      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6920 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6921 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6922  (simplify
6923   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6924    (if (INTEGRAL_TYPE_P (type)
6925         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6926         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6927         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6928     (with
6929      {
6930        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6931      }
6932     (if (cmp == GE_EXPR)
6933      (bit_xor (convert (rshift @0 {shifter;})) @1)
6934      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6936 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6937    convert this into a shift followed by ANDing with D.  */
6938 (simplify
6939  (cond
6940   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6941   INTEGER_CST@2 integer_zerop)
6942  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6943   (with {
6944      int shift = (wi::exact_log2 (wi::to_wide (@2))
6945                   - wi::exact_log2 (wi::to_wide (@1)));
6946    }
6947    (if (shift > 0)
6948     (bit_and
6949      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6950     (bit_and
6951      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6952      @2)))))
6954 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6955    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6956 (for cmp (eq ne)
6957      ncmp (ge lt)
6958  (simplify
6959   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6960   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6961        && type_has_mode_precision_p (TREE_TYPE (@0))
6962        && element_precision (@2) >= element_precision (@0)
6963        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6964    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6965     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6967 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6968    this into a right shift or sign extension followed by ANDing with C.  */
6969 (simplify
6970  (cond
6971   (lt @0 integer_zerop)
6972   INTEGER_CST@1 integer_zerop)
6973  (if (integer_pow2p (@1)
6974       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6975   (with {
6976     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6977    }
6978    (if (shift >= 0)
6979     (bit_and
6980      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6981      @1)
6982     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6983        sign extension followed by AND with C will achieve the effect.  */
6984     (bit_and (convert @0) @1)))))
6986 /* When the addresses are not directly of decls compare base and offset.
6987    This implements some remaining parts of fold_comparison address
6988    comparisons but still no complete part of it.  Still it is good
6989    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6990 (for cmp (simple_comparison)
6991  (simplify
6992   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6993   (with
6994    {
6995      poly_int64 off0, off1;
6996      tree base0, base1;
6997      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6998                                   off0, off1, GENERIC);
6999    }
7000    (if (equal == 1)
7001     (switch
7002      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7003       { constant_boolean_node (known_eq (off0, off1), type); })
7004      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7005       { constant_boolean_node (known_ne (off0, off1), type); })
7006      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7007       { constant_boolean_node (known_lt (off0, off1), type); })
7008      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7009       { constant_boolean_node (known_le (off0, off1), type); })
7010      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7011       { constant_boolean_node (known_ge (off0, off1), type); })
7012      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7013       { constant_boolean_node (known_gt (off0, off1), type); }))
7014     (if (equal == 0)
7015      (switch
7016       (if (cmp == EQ_EXPR)
7017        { constant_boolean_node (false, type); })
7018       (if (cmp == NE_EXPR)
7019        { constant_boolean_node (true, type); })))))))
7021 #if GIMPLE
7022 /* a?~t:t -> (-(a))^t */
7023 (simplify
7024  (cond @0 @1 @2)
7025  (with { bool wascmp; }
7026   (if (INTEGRAL_TYPE_P (type)
7027        && bitwise_inverted_equal_p (@1, @2, wascmp)
7028        && (!wascmp || TYPE_PRECISION (type) == 1))
7029    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7030         || TYPE_PRECISION (type) == 1)
7031     (bit_xor (convert:type @0) @2)
7032     (bit_xor (negate (convert:type @0)) @2)))))
7033 #endif
7035 /* Simplify pointer equality compares using PTA.  */
7036 (for neeq (ne eq)
7037  (simplify
7038   (neeq @0 @1)
7039   (if (POINTER_TYPE_P (TREE_TYPE (@0))
7040        && ptrs_compare_unequal (@0, @1))
7041    { constant_boolean_node (neeq != EQ_EXPR, type); })))
7043 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7044    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7045    Disable the transform if either operand is pointer to function.
7046    This broke pr22051-2.c for arm where function pointer
7047    canonicalizaion is not wanted.  */
7049 (for cmp (ne eq)
7050  (simplify
7051   (cmp (convert @0) INTEGER_CST@1)
7052   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7053          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7054          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7055          /* Don't perform this optimization in GENERIC if @0 has reference
7056             type when sanitizing.  See PR101210.  */
7057          && !(GENERIC
7058               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7059               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7060         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7061             && POINTER_TYPE_P (TREE_TYPE (@1))
7062             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7063        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7064    (cmp @0 (convert @1)))))
7066 /* Non-equality compare simplifications from fold_binary  */
7067 (for cmp (lt gt le ge)
7068  /* Comparisons with the highest or lowest possible integer of
7069     the specified precision will have known values.  */
7070  (simplify
7071   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7072   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7073         || POINTER_TYPE_P (TREE_TYPE (@1))
7074         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7075        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7076    (with
7077     {
7078       tree cst = uniform_integer_cst_p (@1);
7079       tree arg1_type = TREE_TYPE (cst);
7080       unsigned int prec = TYPE_PRECISION (arg1_type);
7081       wide_int max = wi::max_value (arg1_type);
7082       wide_int signed_max = wi::max_value (prec, SIGNED);
7083       wide_int min = wi::min_value (arg1_type);
7084     }
7085     (switch
7086      (if (wi::to_wide (cst) == max)
7087       (switch
7088        (if (cmp == GT_EXPR)
7089         { constant_boolean_node (false, type); })
7090        (if (cmp == GE_EXPR)
7091         (eq @2 @1))
7092        (if (cmp == LE_EXPR)
7093         { constant_boolean_node (true, type); })
7094        (if (cmp == LT_EXPR)
7095         (ne @2 @1))))
7096      (if (wi::to_wide (cst) == min)
7097       (switch
7098        (if (cmp == LT_EXPR)
7099         { constant_boolean_node (false, type); })
7100        (if (cmp == LE_EXPR)
7101         (eq @2 @1))
7102        (if (cmp == GE_EXPR)
7103         { constant_boolean_node (true, type); })
7104        (if (cmp == GT_EXPR)
7105         (ne @2 @1))))
7106      (if (wi::to_wide (cst) == max - 1)
7107       (switch
7108        (if (cmp == GT_EXPR)
7109         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7110                                     wide_int_to_tree (TREE_TYPE (cst),
7111                                                       wi::to_wide (cst)
7112                                                       + 1)); }))
7113        (if (cmp == LE_EXPR)
7114         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7115                                     wide_int_to_tree (TREE_TYPE (cst),
7116                                                       wi::to_wide (cst)
7117                                                       + 1)); }))))
7118      (if (wi::to_wide (cst) == min + 1)
7119       (switch
7120        (if (cmp == GE_EXPR)
7121         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7122                                     wide_int_to_tree (TREE_TYPE (cst),
7123                                                       wi::to_wide (cst)
7124                                                       - 1)); }))
7125        (if (cmp == LT_EXPR)
7126         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7127                                     wide_int_to_tree (TREE_TYPE (cst),
7128                                                       wi::to_wide (cst)
7129                                                       - 1)); }))))
7130      (if (wi::to_wide (cst) == signed_max
7131           && TYPE_UNSIGNED (arg1_type)
7132           && TYPE_MODE (arg1_type) != BLKmode
7133           /* We will flip the signedness of the comparison operator
7134              associated with the mode of @1, so the sign bit is
7135              specified by this mode.  Check that @1 is the signed
7136              max associated with this sign bit.  */
7137           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7138           /* signed_type does not work on pointer types.  */
7139           && INTEGRAL_TYPE_P (arg1_type))
7140       /* The following case also applies to X < signed_max+1
7141          and X >= signed_max+1 because previous transformations.  */
7142       (if (cmp == LE_EXPR || cmp == GT_EXPR)
7143        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7144         (switch
7145          (if (cst == @1 && cmp == LE_EXPR)
7146           (ge (convert:st @0) { build_zero_cst (st); }))
7147          (if (cst == @1 && cmp == GT_EXPR)
7148           (lt (convert:st @0) { build_zero_cst (st); }))
7149          (if (cmp == LE_EXPR)
7150           (ge (view_convert:st @0) { build_zero_cst (st); }))
7151          (if (cmp == GT_EXPR)
7152           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7154 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
7155 (simplify
7156  (lt:c @0 (convert (ne @0 integer_zerop)))
7157  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7158   { constant_boolean_node (false, type); }))
7160 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7161 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7162 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7163 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7164 (for outer (ne eq)
7165  (for inner (ne eq)
7166   (simplify
7167    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7168    (with {
7169      bool cst1 = integer_onep (@1);
7170      bool cst0 = integer_zerop (@1);
7171      bool innereq = inner == EQ_EXPR;
7172      bool outereq = outer == EQ_EXPR;
7173     }
7174     (switch
7175      (if (innereq ? cst0 : cst1)
7176       { constant_boolean_node (!outereq, type); })
7177      (if (innereq ? cst1 : cst0)
7178       (with {
7179         tree utype = unsigned_type_for (TREE_TYPE (@0));
7180         tree ucst1 = build_one_cst (utype);
7181        }
7182        (if (!outereq)
7183         (gt (convert:utype @0) { ucst1; })
7184         (le (convert:utype @0) { ucst1; })
7185        )
7186       )
7187      )
7188      (with {
7189        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7190       }
7191       (if (outereq)
7192        (eq @0 { value; })
7193        (ne @0 { value; })
7194       )
7195      )
7196     )
7197    )
7198   )
7202 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7203  /* If the second operand is NaN, the result is constant.  */
7204  (simplify
7205   (cmp @0 REAL_CST@1)
7206   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7207        && (cmp != LTGT_EXPR || ! flag_trapping_math))
7208    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7209                             ? false : true, type); })))
7211 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
7212 (simplify
7213   (unordered @0 @1)
7214   (switch
7215     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7216         { constant_boolean_node (true, type); })
7217     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7218         { constant_boolean_node (false, type); })))
7220 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7221 (simplify
7222   (ordered @0 @1)
7223   (switch
7224     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7225         { constant_boolean_node (false, type); })
7226     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7227         { constant_boolean_node (true, type); })))
7229 /* bool_var != 0 becomes bool_var.  */
7230 (simplify
7231  (ne @0 integer_zerop)
7232  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7233       && types_match (type, TREE_TYPE (@0)))
7234   (non_lvalue @0)))
7235 /* bool_var == 1 becomes bool_var.  */
7236 (simplify
7237  (eq @0 integer_onep)
7238  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7239       && types_match (type, TREE_TYPE (@0)))
7240   (non_lvalue @0)))
7241 /* Do not handle
7242    bool_var == 0 becomes !bool_var or
7243    bool_var != 1 becomes !bool_var
7244    here because that only is good in assignment context as long
7245    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7246    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7247    clearly less optimal and which we'll transform again in forwprop.  */
7249 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7250    where ~Y + 1 == pow2 and Z = ~Y.  */
7251 (for cst (VECTOR_CST INTEGER_CST)
7252  (for cmp (eq ne)
7253       icmp (le gt)
7254   (simplify
7255    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7256     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7257      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7258       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7259                          ? optab_vector : optab_default;
7260               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7261        (if (target_supports_op_p (utype, icmp, optab)
7262             || (optimize_vectors_before_lowering_p ()
7263                 && (!target_supports_op_p (type, cmp, optab)
7264                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7265         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7266          (icmp @0 { csts; })
7267          (icmp (view_convert:utype @0) { csts; })))))))))
7269 /* When one argument is a constant, overflow detection can be simplified.
7270    Currently restricted to single use so as not to interfere too much with
7271    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7272    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7273 (for cmp (lt le ge gt)
7274      out (gt gt le le)
7275  (simplify
7276   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7277   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7278        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7279        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7280        && wi::to_wide (@1) != 0
7281        && single_use (@2))
7282    (with {
7283      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7284      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7285     }
7286     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7287                                 wi::max_value (prec, sign)
7288                                 - wi::to_wide (@1)); })))))
7290 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7291    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7292    expects the long form, so we restrict the transformation for now.  */
7293 (for cmp (gt le)
7294  (simplify
7295   (cmp:c (minus@2 @0 @1) @0)
7296   (if (single_use (@2)
7297        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7298        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7299    (cmp @1 @0))))
7301 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7302 (for cmp (ge lt)
7303  (simplify
7304   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7305    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7306         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7307     (cmp @1 @0))))
7309 /* Testing for overflow is unnecessary if we already know the result.  */
7310 /* A - B > A  */
7311 (for cmp (gt le)
7312      out (ne eq)
7313  (simplify
7314   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7315   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7316        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7317    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7318 /* A + B < A  */
7319 (for cmp (lt ge)
7320      out (ne eq)
7321  (simplify
7322   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7323   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7324        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7325    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7327 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7328    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7329 (for cmp (lt ge)
7330      out (ne eq)
7331  (simplify
7332   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7333   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7334    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7335     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7337 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7338    is at least twice as wide as type of A and B, simplify to
7339    __builtin_mul_overflow (A, B, <unused>).  */
7340 (for cmp (eq ne)
7341  (simplify
7342   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7343        integer_zerop)
7344   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7345        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7346        && TYPE_UNSIGNED (TREE_TYPE (@0))
7347        && (TYPE_PRECISION (TREE_TYPE (@3))
7348            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7349        && tree_fits_uhwi_p (@2)
7350        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7351        && types_match (@0, @1)
7352        && type_has_mode_precision_p (TREE_TYPE (@0))
7353        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7354            != CODE_FOR_nothing))
7355    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7356     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7358 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7359 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7360  (simplify
7361   (ovf (convert@2 @0) @1)
7362   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7363        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7364        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7365        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7366    (ovf @0 @1)))
7367  (simplify
7368   (ovf @1 (convert@2 @0))
7369   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7370        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7371        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7372        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7373    (ovf @1 @0))))
7375 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7376    are unsigned to x > (umax / cst).  Similarly for signed type, but
7377    in that case it needs to be outside of a range.  */
7378 (simplify
7379  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7380   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7381        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7382        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7383        && int_fits_type_p (@1, TREE_TYPE (@0)))
7384    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7385     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7386     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7387      (if (integer_minus_onep (@1))
7388       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7389       (with
7390        {
7391          tree div = fold_convert (TREE_TYPE (@0), @1);
7392          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7393                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7394          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7395                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7396          tree etype = range_check_type (TREE_TYPE (@0));
7397          if (etype)
7398            {
7399              if (wi::neg_p (wi::to_wide (div)))
7400                std::swap (lo, hi);
7401              lo = fold_convert (etype, lo);
7402              hi = fold_convert (etype, hi);
7403              hi = int_const_binop (MINUS_EXPR, hi, lo);
7404            }
7405        }
7406        (if (etype)
7407         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7409 /* Simplification of math builtins.  These rules must all be optimizations
7410    as well as IL simplifications.  If there is a possibility that the new
7411    form could be a pessimization, the rule should go in the canonicalization
7412    section that follows this one.
7414    Rules can generally go in this section if they satisfy one of
7415    the following:
7417    - the rule describes an identity
7419    - the rule replaces calls with something as simple as addition or
7420      multiplication
7422    - the rule contains unary calls only and simplifies the surrounding
7423      arithmetic.  (The idea here is to exclude non-unary calls in which
7424      one operand is constant and in which the call is known to be cheap
7425      when the operand has that value.)  */
7427 (if (flag_unsafe_math_optimizations)
7428  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7429  (simplify
7430   (mult (SQRT_ALL@1 @0) @1)
7431   (if (!tree_expr_maybe_signaling_nan_p (@0))
7432    @0))
7434  (for op (plus minus)
7435   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7436   (simplify
7437    (op (rdiv @0 @1)
7438        (rdiv @2 @1))
7439    (rdiv (op @0 @2) @1)))
7441  (for cmp (lt le gt ge)
7442       neg_cmp (gt ge lt le)
7443   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7444   (simplify
7445    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7446    (with
7447     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7448     (if (tem
7449          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7450               || (real_zerop (tem) && !real_zerop (@1))))
7451      (switch
7452       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7453        (cmp @0 { tem; }))
7454       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7455        (neg_cmp @0 { tem; })))))))
7457  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7458  (for root (SQRT CBRT)
7459   (simplify
7460    (mult (root:s @0) (root:s @1))
7461     (root (mult @0 @1))))
7463  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7464  (for exps (EXP EXP2 EXP10 POW10)
7465   (simplify
7466    (mult (exps:s @0) (exps:s @1))
7467     (exps (plus @0 @1))))
7469  /* Simplify a/root(b/c) into a*root(c/b).  */
7470  (for root (SQRT CBRT)
7471   (simplify
7472    (rdiv @0 (root:s (rdiv:s @1 @2)))
7473     (mult @0 (root (rdiv @2 @1)))))
7475  /* Simplify x/expN(y) into x*expN(-y).  */
7476  (for exps (EXP EXP2 EXP10 POW10)
7477   (simplify
7478    (rdiv @0 (exps:s @1))
7479     (mult @0 (exps (negate @1)))))
7481  (for logs (LOG LOG2 LOG10 LOG10)
7482       exps (EXP EXP2 EXP10 POW10)
7483   /* logN(expN(x)) -> x.  */
7484   (simplify
7485    (logs (exps @0))
7486    @0)
7487   /* expN(logN(x)) -> x.  */
7488   (simplify
7489    (exps (logs @0))
7490    @0))
7492  /* Optimize logN(func()) for various exponential functions.  We
7493     want to determine the value "x" and the power "exponent" in
7494     order to transform logN(x**exponent) into exponent*logN(x).  */
7495  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7496       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7497   (simplify
7498    (logs (exps @0))
7499    (if (SCALAR_FLOAT_TYPE_P (type))
7500     (with {
7501       tree x;
7502       switch (exps)
7503         {
7504         CASE_CFN_EXP:
7505           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7506           x = build_real_truncate (type, dconst_e ());
7507           break;
7508         CASE_CFN_EXP2:
7509           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7510           x = build_real (type, dconst2);
7511           break;
7512         CASE_CFN_EXP10:
7513         CASE_CFN_POW10:
7514           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7515           {
7516             REAL_VALUE_TYPE dconst10;
7517             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7518             x = build_real (type, dconst10);
7519           }
7520           break;
7521         default:
7522           gcc_unreachable ();
7523         }
7524       }
7525      (mult (logs { x; }) @0)))))
7527  (for logs (LOG LOG
7528             LOG2 LOG2
7529             LOG10 LOG10)
7530       exps (SQRT CBRT)
7531   (simplify
7532    (logs (exps @0))
7533    (if (SCALAR_FLOAT_TYPE_P (type))
7534     (with {
7535       tree x;
7536       switch (exps)
7537         {
7538         CASE_CFN_SQRT:
7539           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7540           x = build_real (type, dconsthalf);
7541           break;
7542         CASE_CFN_CBRT:
7543           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7544           x = build_real_truncate (type, dconst_third ());
7545           break;
7546         default:
7547           gcc_unreachable ();
7548         }
7549       }
7550      (mult { x; } (logs @0))))))
7552  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7553  (for logs (LOG LOG2 LOG10)
7554       pows (POW)
7555   (simplify
7556    (logs (pows @0 @1))
7557    (mult @1 (logs @0))))
7559  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7560     or if C is a positive power of 2,
7561     pow(C,x) -> exp2(log2(C)*x).  */
7562 #if GIMPLE
7563  (for pows (POW)
7564       exps (EXP)
7565       logs (LOG)
7566       exp2s (EXP2)
7567       log2s (LOG2)
7568   (simplify
7569    (pows REAL_CST@0 @1)
7570    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7571         && real_isfinite (TREE_REAL_CST_PTR (@0))
7572         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7573            the use_exp2 case until after vectorization.  It seems actually
7574            beneficial for all constants to postpone this until later,
7575            because exp(log(C)*x), while faster, will have worse precision
7576            and if x folds into a constant too, that is unnecessary
7577            pessimization.  */
7578         && canonicalize_math_after_vectorization_p ())
7579     (with {
7580        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7581        bool use_exp2 = false;
7582        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7583            && value->cl == rvc_normal)
7584          {
7585            REAL_VALUE_TYPE frac_rvt = *value;
7586            SET_REAL_EXP (&frac_rvt, 1);
7587            if (real_equal (&frac_rvt, &dconst1))
7588              use_exp2 = true;
7589          }
7590      }
7591      (if (!use_exp2)
7592       (if (optimize_pow_to_exp (@0, @1))
7593        (exps (mult (logs @0) @1)))
7594       (exp2s (mult (log2s @0) @1)))))))
7595 #endif
7597  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7598  (for pows (POW)
7599       exps (EXP EXP2 EXP10 POW10)
7600       logs (LOG LOG2 LOG10 LOG10)
7601   (simplify
7602    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7603    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7604         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7605     (exps (plus (mult (logs @0) @1) @2)))))
7607  (for sqrts (SQRT)
7608       cbrts (CBRT)
7609       pows (POW)
7610       exps (EXP EXP2 EXP10 POW10)
7611   /* sqrt(expN(x)) -> expN(x*0.5).  */
7612   (simplify
7613    (sqrts (exps @0))
7614    (exps (mult @0 { build_real (type, dconsthalf); })))
7615   /* cbrt(expN(x)) -> expN(x/3).  */
7616   (simplify
7617    (cbrts (exps @0))
7618    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7619   /* pow(expN(x), y) -> expN(x*y).  */
7620   (simplify
7621    (pows (exps @0) @1)
7622    (exps (mult @0 @1))))
7624  /* tan(atan(x)) -> x.  */
7625  (for tans (TAN)
7626       atans (ATAN)
7627   (simplify
7628    (tans (atans @0))
7629    @0)))
7631  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7632  (for sins (SIN)
7633       atans (ATAN)
7634       sqrts (SQRT)
7635       copysigns (COPYSIGN)
7636   (simplify
7637    (sins (atans:s @0))
7638    (with
7639      {
7640       REAL_VALUE_TYPE r_cst;
7641       build_sinatan_real (&r_cst, type);
7642       tree t_cst = build_real (type, r_cst);
7643       tree t_one = build_one_cst (type);
7644      }
7645     (if (SCALAR_FLOAT_TYPE_P (type))
7646      (cond (lt (abs @0) { t_cst; })
7647       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7648       (copysigns { t_one; } @0))))))
7650 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7651  (for coss (COS)
7652       atans (ATAN)
7653       sqrts (SQRT)
7654       copysigns (COPYSIGN)
7655   (simplify
7656    (coss (atans:s @0))
7657    (with
7658      {
7659       REAL_VALUE_TYPE r_cst;
7660       build_sinatan_real (&r_cst, type);
7661       tree t_cst = build_real (type, r_cst);
7662       tree t_one = build_one_cst (type);
7663       tree t_zero = build_zero_cst (type);
7664      }
7665     (if (SCALAR_FLOAT_TYPE_P (type))
7666      (cond (lt (abs @0) { t_cst; })
7667       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7668       (copysigns { t_zero; } @0))))))
7670  (if (!flag_errno_math)
7671   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7672   (for sinhs (SINH)
7673        atanhs (ATANH)
7674        sqrts (SQRT)
7675    (simplify
7676     (sinhs (atanhs:s @0))
7677     (with { tree t_one = build_one_cst (type); }
7678     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7680   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7681   (for coshs (COSH)
7682        atanhs (ATANH)
7683        sqrts (SQRT)
7684    (simplify
7685     (coshs (atanhs:s @0))
7686     (with { tree t_one = build_one_cst (type); }
7687     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7689 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7690 (simplify
7691  (CABS (complex:C @0 real_zerop@1))
7692  (abs @0))
7694 /* trunc(trunc(x)) -> trunc(x), etc.  */
7695 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7696  (simplify
7697   (fns (fns @0))
7698   (fns @0)))
7699 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7700 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7701  (simplify
7702   (fns integer_valued_real_p@0)
7703   @0))
7705 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7706 (simplify
7707  (HYPOT:c @0 real_zerop@1)
7708  (abs @0))
7710 /* pow(1,x) -> 1.  */
7711 (simplify
7712  (POW real_onep@0 @1)
7713  @0)
7715 (simplify
7716  /* copysign(x,x) -> x.  */
7717  (COPYSIGN_ALL @0 @0)
7718  @0)
7720 (simplify
7721  /* copysign(x,-x) -> -x.  */
7722  (COPYSIGN_ALL @0 (negate@1 @0))
7723  @1)
7725 (simplify
7726  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7727  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7728  (abs @0))
7730 (simplify
7731  /* fabs (copysign(x, y)) -> fabs (x).  */
7732  (abs (COPYSIGN_ALL @0 @1))
7733  (abs @0))
7735 (for scale (LDEXP SCALBN SCALBLN)
7736  /* ldexp(0, x) -> 0.  */
7737  (simplify
7738   (scale real_zerop@0 @1)
7739   @0)
7740  /* ldexp(x, 0) -> x.  */
7741  (simplify
7742   (scale @0 integer_zerop@1)
7743   @0)
7744  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7745  (simplify
7746   (scale REAL_CST@0 @1)
7747   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7748    @0)))
7750 /* Canonicalization of sequences of math builtins.  These rules represent
7751    IL simplifications but are not necessarily optimizations.
7753    The sincos pass is responsible for picking "optimal" implementations
7754    of math builtins, which may be more complicated and can sometimes go
7755    the other way, e.g. converting pow into a sequence of sqrts.
7756    We only want to do these canonicalizations before the pass has run.  */
7758 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7759  /* Simplify tan(x) * cos(x) -> sin(x). */
7760  (simplify
7761   (mult:c (TAN:s @0) (COS:s @0))
7762    (SIN @0))
7764  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7765  (simplify
7766   (mult:c @0 (POW:s @0 REAL_CST@1))
7767   (if (!TREE_OVERFLOW (@1))
7768    (POW @0 (plus @1 { build_one_cst (type); }))))
7770  /* Simplify sin(x) / cos(x) -> tan(x). */
7771  (simplify
7772   (rdiv (SIN:s @0) (COS:s @0))
7773    (TAN @0))
7775  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7776  (simplify
7777   (rdiv (SINH:s @0) (COSH:s @0))
7778    (TANH @0))
7780  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7781  (simplify
7782    (rdiv (TANH:s @0) (SINH:s @0))
7783    (rdiv {build_one_cst (type);} (COSH @0)))
7785  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7786  (simplify
7787   (rdiv (COS:s @0) (SIN:s @0))
7788    (rdiv { build_one_cst (type); } (TAN @0)))
7790  /* Simplify sin(x) / tan(x) -> cos(x). */
7791  (simplify
7792   (rdiv (SIN:s @0) (TAN:s @0))
7793   (if (! HONOR_NANS (@0)
7794        && ! HONOR_INFINITIES (@0))
7795    (COS @0)))
7797  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7798  (simplify
7799   (rdiv (TAN:s @0) (SIN:s @0))
7800   (if (! HONOR_NANS (@0)
7801        && ! HONOR_INFINITIES (@0))
7802    (rdiv { build_one_cst (type); } (COS @0))))
7804  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7805  (simplify
7806   (mult (POW:s @0 @1) (POW:s @0 @2))
7807    (POW @0 (plus @1 @2)))
7809  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7810  (simplify
7811   (mult (POW:s @0 @1) (POW:s @2 @1))
7812    (POW (mult @0 @2) @1))
7814  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7815  (simplify
7816   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7817    (POWI (mult @0 @2) @1))
7819  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7820  (simplify
7821   (rdiv (POW:s @0 REAL_CST@1) @0)
7822   (if (!TREE_OVERFLOW (@1))
7823    (POW @0 (minus @1 { build_one_cst (type); }))))
7825  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7826  (simplify
7827   (rdiv @0 (POW:s @1 @2))
7828    (mult @0 (POW @1 (negate @2))))
7830  (for sqrts (SQRT)
7831       cbrts (CBRT)
7832       pows (POW)
7833   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7834   (simplify
7835    (sqrts (sqrts @0))
7836    (pows @0 { build_real (type, dconst_quarter ()); }))
7837   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7838   (simplify
7839    (sqrts (cbrts @0))
7840    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7841   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7842   (simplify
7843    (cbrts (sqrts @0))
7844    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7845   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7846   (simplify
7847    (cbrts (cbrts tree_expr_nonnegative_p@0))
7848    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7849   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7850   (simplify
7851    (sqrts (pows @0 @1))
7852    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7853   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7854   (simplify
7855    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7856    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7857   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7858   (simplify
7859    (pows (sqrts @0) @1)
7860    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7861   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7862   (simplify
7863    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7864    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7865   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7866   (simplify
7867    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7868    (pows @0 (mult @1 @2))))
7870  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7871  (simplify
7872   (CABS (complex @0 @0))
7873   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7875  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7876  (simplify
7877   (HYPOT @0 @0)
7878   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7880  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7881  (for cexps (CEXP)
7882       exps (EXP)
7883       cexpis (CEXPI)
7884   (simplify
7885    (cexps compositional_complex@0)
7886    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7887     (complex
7888      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7889      (mult @1 (imagpart @2)))))))
7891 (if (canonicalize_math_p ())
7892  /* floor(x) -> trunc(x) if x is nonnegative.  */
7893  (for floors (FLOOR_ALL)
7894       truncs (TRUNC_ALL)
7895   (simplify
7896    (floors tree_expr_nonnegative_p@0)
7897    (truncs @0))))
7899 (match double_value_p
7900  @0
7901  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7902 (for froms (BUILT_IN_TRUNCL
7903             BUILT_IN_FLOORL
7904             BUILT_IN_CEILL
7905             BUILT_IN_ROUNDL
7906             BUILT_IN_NEARBYINTL
7907             BUILT_IN_RINTL)
7908      tos (BUILT_IN_TRUNC
7909           BUILT_IN_FLOOR
7910           BUILT_IN_CEIL
7911           BUILT_IN_ROUND
7912           BUILT_IN_NEARBYINT
7913           BUILT_IN_RINT)
7914  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7915  (if (optimize && canonicalize_math_p ())
7916   (simplify
7917    (froms (convert double_value_p@0))
7918    (convert (tos @0)))))
7920 (match float_value_p
7921  @0
7922  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7923 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7924             BUILT_IN_FLOORL BUILT_IN_FLOOR
7925             BUILT_IN_CEILL BUILT_IN_CEIL
7926             BUILT_IN_ROUNDL BUILT_IN_ROUND
7927             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7928             BUILT_IN_RINTL BUILT_IN_RINT)
7929      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7930           BUILT_IN_FLOORF BUILT_IN_FLOORF
7931           BUILT_IN_CEILF BUILT_IN_CEILF
7932           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7933           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7934           BUILT_IN_RINTF BUILT_IN_RINTF)
7935  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7936     if x is a float.  */
7937  (if (optimize && canonicalize_math_p ()
7938       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7939   (simplify
7940    (froms (convert float_value_p@0))
7941    (convert (tos @0)))))
7943 #if GIMPLE
7944 (match float16_value_p
7945  @0
7946  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7947 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7948             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7949             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7950             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7951             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7952             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7953             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7954             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7955      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7956           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7957           IFN_CEIL IFN_CEIL IFN_CEIL
7958           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7959           IFN_ROUND IFN_ROUND IFN_ROUND
7960           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7961           IFN_RINT IFN_RINT IFN_RINT
7962           IFN_SQRT IFN_SQRT IFN_SQRT)
7963  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7964     if x is a _Float16.  */
7965  (simplify
7966    (convert (froms (convert float16_value_p@0)))
7967      (if (optimize
7968           && types_match (type, TREE_TYPE (@0))
7969           && direct_internal_fn_supported_p (as_internal_fn (tos),
7970                                              type, OPTIMIZE_FOR_BOTH))
7971        (tos @0))))
7973 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7974    x,y is float value, similar for _Float16/double.  */
7975 (for copysigns (COPYSIGN_ALL)
7976  (simplify
7977   (convert (copysigns (convert@2 @0) (convert @1)))
7978    (if (optimize
7979        && !HONOR_SNANS (@2)
7980        && types_match (type, TREE_TYPE (@0))
7981        && types_match (type, TREE_TYPE (@1))
7982        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7983        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7984                                           type, OPTIMIZE_FOR_BOTH))
7985     (IFN_COPYSIGN @0 @1))))
7987 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7988      tos (IFN_FMA IFN_FMA IFN_FMA)
7989  (simplify
7990   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7991    (if (flag_unsafe_math_optimizations
7992        && optimize
7993        && FLOAT_TYPE_P (type)
7994        && FLOAT_TYPE_P (TREE_TYPE (@3))
7995        && types_match (type, TREE_TYPE (@0))
7996        && types_match (type, TREE_TYPE (@1))
7997        && types_match (type, TREE_TYPE (@2))
7998        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7999        && direct_internal_fn_supported_p (as_internal_fn (tos),
8000                                           type, OPTIMIZE_FOR_BOTH))
8001     (tos @0 @1 @2))))
8003 (for maxmin (max min)
8004  (simplify
8005   (convert (maxmin (convert@2 @0) (convert @1)))
8006    (if (optimize
8007        && FLOAT_TYPE_P (type)
8008        && FLOAT_TYPE_P (TREE_TYPE (@2))
8009        && types_match (type, TREE_TYPE (@0))
8010        && types_match (type, TREE_TYPE (@1))
8011        && element_precision (type) < element_precision (TREE_TYPE (@2)))
8012     (maxmin @0 @1))))
8013 #endif
8015 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8016      tos (XFLOOR XCEIL XROUND XRINT)
8017  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
8018  (if (optimize && canonicalize_math_p ())
8019   (simplify
8020    (froms (convert double_value_p@0))
8021    (tos @0))))
8023 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8024             XFLOOR XCEIL XROUND XRINT)
8025      tos (XFLOORF XCEILF XROUNDF XRINTF)
8026  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8027     if x is a float.  */
8028  (if (optimize && canonicalize_math_p ())
8029   (simplify
8030    (froms (convert float_value_p@0))
8031    (tos @0))))
8033 (if (canonicalize_math_p ())
8034  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
8035  (for floors (IFLOOR LFLOOR LLFLOOR)
8036   (simplify
8037    (floors tree_expr_nonnegative_p@0)
8038    (fix_trunc @0))))
8040 (if (canonicalize_math_p ())
8041  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
8042  (for fns (IFLOOR LFLOOR LLFLOOR
8043            ICEIL LCEIL LLCEIL
8044            IROUND LROUND LLROUND)
8045   (simplify
8046    (fns integer_valued_real_p@0)
8047    (fix_trunc @0)))
8048  (if (!flag_errno_math)
8049   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
8050   (for rints (IRINT LRINT LLRINT)
8051    (simplify
8052     (rints integer_valued_real_p@0)
8053     (fix_trunc @0)))))
8055 (if (canonicalize_math_p ())
8056  (for ifn (IFLOOR ICEIL IROUND IRINT)
8057       lfn (LFLOOR LCEIL LROUND LRINT)
8058       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8059   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8060      sizeof (int) == sizeof (long).  */
8061   (if (TYPE_PRECISION (integer_type_node)
8062        == TYPE_PRECISION (long_integer_type_node))
8063    (simplify
8064     (ifn @0)
8065     (lfn:long_integer_type_node @0)))
8066   /* Canonicalize llround (x) to lround (x) on LP64 targets where
8067      sizeof (long long) == sizeof (long).  */
8068   (if (TYPE_PRECISION (long_long_integer_type_node)
8069        == TYPE_PRECISION (long_integer_type_node))
8070    (simplify
8071     (llfn @0)
8072     (lfn:long_integer_type_node @0)))))
8074 /* cproj(x) -> x if we're ignoring infinities.  */
8075 (simplify
8076  (CPROJ @0)
8077  (if (!HONOR_INFINITIES (type))
8078    @0))
8080 /* If the real part is inf and the imag part is known to be
8081    nonnegative, return (inf + 0i).  */
8082 (simplify
8083  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8084  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8085   { build_complex_inf (type, false); }))
8087 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
8088 (simplify
8089  (CPROJ (complex @0 REAL_CST@1))
8090  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8091   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8093 (for pows (POW)
8094      sqrts (SQRT)
8095      cbrts (CBRT)
8096  (simplify
8097   (pows @0 REAL_CST@1)
8098   (with {
8099     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8100     REAL_VALUE_TYPE tmp;
8101    }
8102    (switch
8103     /* pow(x,0) -> 1.  */
8104     (if (real_equal (value, &dconst0))
8105      { build_real (type, dconst1); })
8106     /* pow(x,1) -> x.  */
8107     (if (real_equal (value, &dconst1))
8108      @0)
8109     /* pow(x,-1) -> 1/x.  */
8110     (if (real_equal (value, &dconstm1))
8111      (rdiv { build_real (type, dconst1); } @0))
8112     /* pow(x,0.5) -> sqrt(x).  */
8113     (if (flag_unsafe_math_optimizations
8114          && canonicalize_math_p ()
8115          && real_equal (value, &dconsthalf))
8116      (sqrts @0))
8117     /* pow(x,1/3) -> cbrt(x).  */
8118     (if (flag_unsafe_math_optimizations
8119          && canonicalize_math_p ()
8120          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8121              real_equal (value, &tmp)))
8122      (cbrts @0))))))
8124 /* powi(1,x) -> 1.  */
8125 (simplify
8126  (POWI real_onep@0 @1)
8127  @0)
8129 (simplify
8130  (POWI @0 INTEGER_CST@1)
8131  (switch
8132   /* powi(x,0) -> 1.  */
8133   (if (wi::to_wide (@1) == 0)
8134    { build_real (type, dconst1); })
8135   /* powi(x,1) -> x.  */
8136   (if (wi::to_wide (@1) == 1)
8137    @0)
8138   /* powi(x,-1) -> 1/x.  */
8139   (if (wi::to_wide (@1) == -1)
8140    (rdiv { build_real (type, dconst1); } @0))))
8142 /* Narrowing of arithmetic and logical operations.
8144    These are conceptually similar to the transformations performed for
8145    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
8146    term we want to move all that code out of the front-ends into here.  */
8148 /* Convert (outertype)((innertype0)a+(innertype1)b)
8149    into ((newtype)a+(newtype)b) where newtype
8150    is the widest mode from all of these.  */
8151 (for op (plus minus mult rdiv)
8152  (simplify
8153    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8154    /* If we have a narrowing conversion of an arithmetic operation where
8155       both operands are widening conversions from the same type as the outer
8156       narrowing conversion.  Then convert the innermost operands to a
8157       suitable unsigned type (to avoid introducing undefined behavior),
8158       perform the operation and convert the result to the desired type.  */
8159    (if (INTEGRAL_TYPE_P (type)
8160         && op != MULT_EXPR
8161         && op != RDIV_EXPR
8162         /* We check for type compatibility between @0 and @1 below,
8163            so there's no need to check that @2/@4 are integral types.  */
8164         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8165         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8166         /* The precision of the type of each operand must match the
8167            precision of the mode of each operand, similarly for the
8168            result.  */
8169         && type_has_mode_precision_p (TREE_TYPE (@1))
8170         && type_has_mode_precision_p (TREE_TYPE (@2))
8171         && type_has_mode_precision_p (type)
8172         /* The inner conversion must be a widening conversion.  */
8173         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8174         && types_match (@1, type)
8175         && (types_match (@1, @2)
8176             /* Or the second operand is const integer or converted const
8177                integer from valueize.  */
8178             || poly_int_tree_p (@4)))
8179      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8180        (op @1 (convert @2))
8181        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8182         (convert (op (convert:utype @1)
8183                      (convert:utype @2)))))
8184      (if (FLOAT_TYPE_P (type)
8185           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8186                == DECIMAL_FLOAT_TYPE_P (type))
8187       (with { tree arg0 = strip_float_extensions (@1);
8188               tree arg1 = strip_float_extensions (@2);
8189               tree itype = TREE_TYPE (@0);
8190               tree ty1 = TREE_TYPE (arg0);
8191               tree ty2 = TREE_TYPE (arg1);
8192               enum tree_code code = TREE_CODE (itype); }
8193         (if (FLOAT_TYPE_P (ty1)
8194              && FLOAT_TYPE_P (ty2))
8195          (with { tree newtype = type;
8196                  if (TYPE_MODE (ty1) == SDmode
8197                      || TYPE_MODE (ty2) == SDmode
8198                      || TYPE_MODE (type) == SDmode)
8199                    newtype = dfloat32_type_node;
8200                  if (TYPE_MODE (ty1) == DDmode
8201                      || TYPE_MODE (ty2) == DDmode
8202                      || TYPE_MODE (type) == DDmode)
8203                    newtype = dfloat64_type_node;
8204                  if (TYPE_MODE (ty1) == TDmode
8205                      || TYPE_MODE (ty2) == TDmode
8206                      || TYPE_MODE (type) == TDmode)
8207                    newtype = dfloat128_type_node; }
8208           (if ((newtype == dfloat32_type_node
8209                 || newtype == dfloat64_type_node
8210                 || newtype == dfloat128_type_node)
8211               && newtype == type
8212               && types_match (newtype, type))
8213             (op (convert:newtype @1) (convert:newtype @2))
8214             (with { if (element_precision (ty1) > element_precision (newtype))
8215                       newtype = ty1;
8216                     if (element_precision (ty2) > element_precision (newtype))
8217                       newtype = ty2; }
8218                /* Sometimes this transformation is safe (cannot
8219                   change results through affecting double rounding
8220                   cases) and sometimes it is not.  If NEWTYPE is
8221                   wider than TYPE, e.g. (float)((long double)double
8222                   + (long double)double) converted to
8223                   (float)(double + double), the transformation is
8224                   unsafe regardless of the details of the types
8225                   involved; double rounding can arise if the result
8226                   of NEWTYPE arithmetic is a NEWTYPE value half way
8227                   between two representable TYPE values but the
8228                   exact value is sufficiently different (in the
8229                   right direction) for this difference to be
8230                   visible in ITYPE arithmetic.  If NEWTYPE is the
8231                   same as TYPE, however, the transformation may be
8232                   safe depending on the types involved: it is safe
8233                   if the ITYPE has strictly more than twice as many
8234                   mantissa bits as TYPE, can represent infinities
8235                   and NaNs if the TYPE can, and has sufficient
8236                   exponent range for the product or ratio of two
8237                   values representable in the TYPE to be within the
8238                   range of normal values of ITYPE.  */
8239               (if (element_precision (newtype) < element_precision (itype)
8240                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8241                        || target_supports_op_p (newtype, op, optab_default))
8242                    && (flag_unsafe_math_optimizations
8243                        || (element_precision (newtype) == element_precision (type)
8244                            && real_can_shorten_arithmetic (element_mode (itype),
8245                                                            element_mode (type))
8246                            && !excess_precision_type (newtype)))
8247                    && !types_match (itype, newtype))
8248                  (convert:type (op (convert:newtype @1)
8249                                    (convert:newtype @2)))
8250          )))) )
8251    ))
8254 /* This is another case of narrowing, specifically when there's an outer
8255    BIT_AND_EXPR which masks off bits outside the type of the innermost
8256    operands.   Like the previous case we have to convert the operands
8257    to unsigned types to avoid introducing undefined behavior for the
8258    arithmetic operation.  */
8259 (for op (minus plus)
8260  (simplify
8261   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8262   (if (INTEGRAL_TYPE_P (type)
8263        /* We check for type compatibility between @0 and @1 below,
8264           so there's no need to check that @1/@3 are integral types.  */
8265        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8266        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8267        /* The precision of the type of each operand must match the
8268           precision of the mode of each operand, similarly for the
8269           result.  */
8270        && type_has_mode_precision_p (TREE_TYPE (@0))
8271        && type_has_mode_precision_p (TREE_TYPE (@1))
8272        && type_has_mode_precision_p (type)
8273        /* The inner conversion must be a widening conversion.  */
8274        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8275        && types_match (@0, @1)
8276        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8277            <= TYPE_PRECISION (TREE_TYPE (@0)))
8278        && (wi::to_wide (@4)
8279            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8280                        true, TYPE_PRECISION (type))) == 0)
8281    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8282     (with { tree ntype = TREE_TYPE (@0); }
8283      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8284     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8285      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8286                (convert:utype @4))))))))
8288 /* Transform (@0 < @1 and @0 < @2) to use min,
8289    (@0 > @1 and @0 > @2) to use max */
8290 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8291      op    (lt      le      gt      ge      lt      le      gt      ge     )
8292      ext   (min     min     max     max     max     max     min     min    )
8293  (simplify
8294   (logic (op:cs @0 @1) (op:cs @0 @2))
8295   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8296        && TREE_CODE (@0) != INTEGER_CST)
8297    (op @0 (ext @1 @2)))))
8299 /* Max<bool0, bool1> -> bool0 | bool1
8300    Min<bool0, bool1> -> bool0 & bool1 */
8301 (for op    (max     min)
8302      logic (bit_ior bit_and)
8303  (simplify
8304   (op zero_one_valued_p@0 zero_one_valued_p@1)
8305   (logic @0 @1)))
8307 /* signbit(x) != 0 ? -x : x -> abs(x)
8308    signbit(x) == 0 ? -x : x -> -abs(x) */
8309 (for sign (SIGNBIT)
8310  (for neeq (ne eq)
8311   (simplify
8312    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8313     (if (neeq == NE_EXPR)
8314      (abs @0)
8315      (negate (abs @0))))))
8317 (simplify
8318  /* signbit(x) -> 0 if x is nonnegative.  */
8319  (SIGNBIT tree_expr_nonnegative_p@0)
8320  { integer_zero_node; })
8322 (simplify
8323  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8324  (SIGNBIT @0)
8325  (if (!HONOR_SIGNED_ZEROS (@0))
8326   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8328 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8329 (for cmp (eq ne)
8330  (for op (plus minus)
8331       rop (minus plus)
8332   (simplify
8333    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8334    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8335         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8336         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8337         && !TYPE_SATURATING (TREE_TYPE (@0)))
8338     (with { tree res = int_const_binop (rop, @2, @1); }
8339      (if (TREE_OVERFLOW (res)
8340           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8341       { constant_boolean_node (cmp == NE_EXPR, type); }
8342       (if (single_use (@3))
8343        (cmp @0 { TREE_OVERFLOW (res)
8344                  ? drop_tree_overflow (res) : res; }))))))))
8345 (for cmp (lt le gt ge)
8346  (for op (plus minus)
8347       rop (minus plus)
8348   (simplify
8349    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8350    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8351         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8352     (with { tree res = int_const_binop (rop, @2, @1); }
8353      (if (TREE_OVERFLOW (res))
8354       {
8355         fold_overflow_warning (("assuming signed overflow does not occur "
8356                                 "when simplifying conditional to constant"),
8357                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8358         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8359         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8360         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8361                                   TYPE_SIGN (TREE_TYPE (@1)))
8362                         != (op == MINUS_EXPR);
8363         constant_boolean_node (less == ovf_high, type);
8364       }
8365       (if (single_use (@3))
8366        (with
8367         {
8368           fold_overflow_warning (("assuming signed overflow does not occur "
8369                                   "when changing X +- C1 cmp C2 to "
8370                                   "X cmp C2 -+ C1"),
8371                                  WARN_STRICT_OVERFLOW_COMPARISON);
8372         }
8373         (cmp @0 { res; })))))))))
8375 /* Canonicalizations of BIT_FIELD_REFs.  */
8377 (simplify
8378  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8379  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8381 (simplify
8382  (BIT_FIELD_REF (view_convert @0) @1 @2)
8383  (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8384       || type_has_mode_precision_p (TREE_TYPE (@0)))
8385   (BIT_FIELD_REF @0 @1 @2)))
8387 (simplify
8388  (BIT_FIELD_REF @0 @1 integer_zerop)
8389  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8390   (view_convert @0)))
8392 (simplify
8393  (BIT_FIELD_REF @0 @1 @2)
8394  (switch
8395   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8396        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8397    (switch
8398     (if (integer_zerop (@2))
8399      (view_convert (realpart @0)))
8400     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8401      (view_convert (imagpart @0)))))
8402   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8403        && INTEGRAL_TYPE_P (type)
8404        /* On GIMPLE this should only apply to register arguments.  */
8405        && (! GIMPLE || is_gimple_reg (@0))
8406        /* A bit-field-ref that referenced the full argument can be stripped.  */
8407        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8408             && integer_zerop (@2))
8409            /* Low-parts can be reduced to integral conversions.
8410               ???  The following doesn't work for PDP endian.  */
8411            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8412                /* But only do this after vectorization.  */
8413                && canonicalize_math_after_vectorization_p ()
8414                /* Don't even think about BITS_BIG_ENDIAN.  */
8415                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8416                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8417                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8418                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8419                                             - TYPE_PRECISION (type))
8420                                          : 0)) == 0)))
8421    (convert @0))))
8423 /* Simplify vector extracts.  */
8425 (simplify
8426  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8427  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8428       && tree_fits_uhwi_p (TYPE_SIZE (type))
8429       && ((tree_to_uhwi (TYPE_SIZE (type))
8430            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8431           || (VECTOR_TYPE_P (type)
8432               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8433                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8434   (with
8435    {
8436      tree ctor = (TREE_CODE (@0) == SSA_NAME
8437                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8438      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8439      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8440      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8441      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8442    }
8443    (if (n != 0
8444         && (idx % width) == 0
8445         && (n % width) == 0
8446         && known_le ((idx + n) / width,
8447                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8448     (with
8449      {
8450        idx = idx / width;
8451        n = n / width;
8452        /* Constructor elements can be subvectors.  */
8453        poly_uint64 k = 1;
8454        if (CONSTRUCTOR_NELTS (ctor) != 0)
8455          {
8456            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8457            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8458              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8459          }
8460        unsigned HOST_WIDE_INT elt, count, const_k;
8461      }
8462      (switch
8463       /* We keep an exact subset of the constructor elements.  */
8464       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8465        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8466         { build_zero_cst (type); }
8467         (if (count == 1)
8468          (if (elt < CONSTRUCTOR_NELTS (ctor))
8469           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8470           { build_zero_cst (type); })
8471          /* We don't want to emit new CTORs unless the old one goes away.
8472             ???  Eventually allow this if the CTOR ends up constant or
8473             uniform.  */
8474          (if (single_use (@0))
8475           (with
8476             {
8477               vec<constructor_elt, va_gc> *vals;
8478               vec_alloc (vals, count);
8479               bool constant_p = true;
8480               tree res;
8481               for (unsigned i = 0;
8482                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8483                 {
8484                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8485                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8486                   if (!CONSTANT_CLASS_P (e))
8487                     constant_p = false;
8488                 }
8489               tree evtype = (types_match (TREE_TYPE (type),
8490                                           TREE_TYPE (TREE_TYPE (ctor)))
8491                              ? type
8492                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8493                                                   count * k));
8494               /* We used to build a CTOR in the non-constant case here
8495                  but that's not a GIMPLE value.  We'd have to expose this
8496                  operation somehow so the code generation can properly
8497                  split it out to a separate stmt.  */
8498               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8499                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8500             }
8501             (if (res)
8502              (view_convert { res; })))))))
8503       /* The bitfield references a single constructor element.  */
8504       (if (k.is_constant (&const_k)
8505            && idx + n <= (idx / const_k + 1) * const_k)
8506        (switch
8507         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8508          { build_zero_cst (type); })
8509         (if (n == const_k)
8510          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8511         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8512                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8514 /* Simplify a bit extraction from a bit insertion for the cases with
8515    the inserted element fully covering the extraction or the insertion
8516    not touching the extraction.  */
8517 (simplify
8518  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8519  (with
8520   {
8521     unsigned HOST_WIDE_INT isize;
8522     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8523       isize = TYPE_PRECISION (TREE_TYPE (@1));
8524     else
8525       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8526   }
8527   (switch
8528    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8529          || type_has_mode_precision_p (TREE_TYPE (@1)))
8530         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8531         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8532                       wi::to_wide (@ipos) + isize))
8533     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8534                                                  wi::to_wide (@rpos)
8535                                                  - wi::to_wide (@ipos)); }))
8536    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8537         && compare_tree_int (@rsize, isize) == 0)
8538     (convert @1))
8539    (if (wi::geu_p (wi::to_wide (@ipos),
8540                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8541         || wi::geu_p (wi::to_wide (@rpos),
8542                       wi::to_wide (@ipos) + isize))
8543     (BIT_FIELD_REF @0 @rsize @rpos)))))
8545 /* Simplify vector inserts of other vector extracts to a permute.  */
8546 (simplify
8547  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8548  (if (VECTOR_TYPE_P (type)
8549       && (VECTOR_MODE_P (TYPE_MODE (type))
8550           || optimize_vectors_before_lowering_p ())
8551       && types_match (@0, @1)
8552       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8553       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8554   (with
8555    {
8556      unsigned HOST_WIDE_INT elsz
8557        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8558      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8559      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8560      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8561      vec_perm_builder builder;
8562      builder.new_vector (nunits, nunits, 1);
8563      for (unsigned i = 0; i < nunits; ++i)
8564        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8565      vec_perm_indices sel (builder, 2, nunits);
8566    }
8567    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8568         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8569     (vec_perm @0 @1 { vec_perm_indices_to_tree
8570                         (build_vector_type (ssizetype, nunits), sel); })))))
8572 (if (canonicalize_math_after_vectorization_p ())
8573  (for fmas (FMA)
8574   (simplify
8575    (fmas:c (negate @0) @1 @2)
8576    (IFN_FNMA @0 @1 @2))
8577   (simplify
8578    (fmas @0 @1 (negate @2))
8579    (IFN_FMS @0 @1 @2))
8580   (simplify
8581    (fmas:c (negate @0) @1 (negate @2))
8582    (IFN_FNMS @0 @1 @2))
8583   (simplify
8584    (negate (fmas@3 @0 @1 @2))
8585    (if (single_use (@3))
8586     (IFN_FNMS @0 @1 @2))))
8588  (simplify
8589   (IFN_FMS:c (negate @0) @1 @2)
8590   (IFN_FNMS @0 @1 @2))
8591  (simplify
8592   (IFN_FMS @0 @1 (negate @2))
8593   (IFN_FMA @0 @1 @2))
8594  (simplify
8595   (IFN_FMS:c (negate @0) @1 (negate @2))
8596   (IFN_FNMA @0 @1 @2))
8597  (simplify
8598   (negate (IFN_FMS@3 @0 @1 @2))
8599    (if (single_use (@3))
8600     (IFN_FNMA @0 @1 @2)))
8602  (simplify
8603   (IFN_FNMA:c (negate @0) @1 @2)
8604   (IFN_FMA @0 @1 @2))
8605  (simplify
8606   (IFN_FNMA @0 @1 (negate @2))
8607   (IFN_FNMS @0 @1 @2))
8608  (simplify
8609   (IFN_FNMA:c (negate @0) @1 (negate @2))
8610   (IFN_FMS @0 @1 @2))
8611  (simplify
8612   (negate (IFN_FNMA@3 @0 @1 @2))
8613   (if (single_use (@3))
8614    (IFN_FMS @0 @1 @2)))
8616  (simplify
8617   (IFN_FNMS:c (negate @0) @1 @2)
8618   (IFN_FMS @0 @1 @2))
8619  (simplify
8620   (IFN_FNMS @0 @1 (negate @2))
8621   (IFN_FNMA @0 @1 @2))
8622  (simplify
8623   (IFN_FNMS:c (negate @0) @1 (negate @2))
8624   (IFN_FMA @0 @1 @2))
8625  (simplify
8626   (negate (IFN_FNMS@3 @0 @1 @2))
8627   (if (single_use (@3))
8628    (IFN_FMA @0 @1 @2))))
8630 /* CLZ simplifications.  */
8631 (for clz (CLZ)
8632  (for op (eq ne)
8633       cmp (lt ge)
8634   (simplify
8635    (op (clz:s@2 @0) INTEGER_CST@1)
8636    (if (integer_zerop (@1) && single_use (@2))
8637     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8638     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8639      (cmp (convert:stype @0) { build_zero_cst (stype); }))
8640     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8641     (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8642      (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8643 (for op (eq ne)
8644      cmp (lt ge)
8645  (simplify
8646   (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8647   (if (integer_zerop (@1) && single_use (@2))
8648    /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8649    (with { tree type0 = TREE_TYPE (@0);
8650            tree stype = signed_type_for (TREE_TYPE (@0));
8651            /* Punt if clz(0) == 0.  */
8652            if (integer_zerop (@3))
8653              stype = NULL_TREE;
8654          }
8655     (if (stype)
8656      (cmp (convert:stype @0) { build_zero_cst (stype); })))
8657    /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8658    (with { bool ok = true;
8659            tree type0 = TREE_TYPE (@0);
8660            /* Punt if clz(0) == prec - 1.  */
8661            if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8662              ok = false;
8663          }
8664     (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8665      (op @0 { build_one_cst (type0); }))))))
8667 /* CTZ simplifications.  */
8668 (for ctz (CTZ)
8669  (for op (ge gt le lt)
8670       cmp (eq eq ne ne)
8671   (simplify
8672    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8673    (op (ctz:s @0) INTEGER_CST@1)
8674     (with { bool ok = true;
8675             HOST_WIDE_INT val = 0;
8676             if (!tree_fits_shwi_p (@1))
8677               ok = false;
8678             else
8679               {
8680                 val = tree_to_shwi (@1);
8681                 /* Canonicalize to >= or <.  */
8682                 if (op == GT_EXPR || op == LE_EXPR)
8683                   {
8684                     if (val == HOST_WIDE_INT_MAX)
8685                       ok = false;
8686                     else
8687                       val++;
8688                   }
8689               }
8690             tree type0 = TREE_TYPE (@0);
8691             int prec = TYPE_PRECISION (type0);
8692           }
8693      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8694       (if (val <= 0)
8695        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8696        (if (val >= prec)
8697         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8698         (cmp (bit_and @0 { wide_int_to_tree (type0,
8699                                              wi::mask (val, false, prec)); })
8700              { build_zero_cst (type0); })))))))
8701  (for op (eq ne)
8702   (simplify
8703    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8704    (op (ctz:s @0) INTEGER_CST@1)
8705     (with { tree type0 = TREE_TYPE (@0);
8706             int prec = TYPE_PRECISION (type0);
8707           }
8708      (if (prec <= MAX_FIXED_MODE_SIZE)
8709       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8710        { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8711        (op (bit_and @0 { wide_int_to_tree (type0,
8712                                            wi::mask (tree_to_uhwi (@1) + 1,
8713                                                      false, prec)); })
8714            { wide_int_to_tree (type0,
8715                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8716                                                  false, prec)); })))))))
8717 (for op (ge gt le lt)
8718      cmp (eq eq ne ne)
8719  (simplify
8720   /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8721   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8722    (with { bool ok = true;
8723            HOST_WIDE_INT val = 0;
8724            if (!tree_fits_shwi_p (@1))
8725              ok = false;
8726            else
8727              {
8728                val = tree_to_shwi (@1);
8729                /* Canonicalize to >= or <.  */
8730                if (op == GT_EXPR || op == LE_EXPR)
8731                  {
8732                    if (val == HOST_WIDE_INT_MAX)
8733                      ok = false;
8734                    else
8735                      val++;
8736                  }
8737              }
8738            HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8739            tree type0 = TREE_TYPE (@0);
8740            int prec = TYPE_PRECISION (type0);
8741            if (prec > MAX_FIXED_MODE_SIZE)
8742              ok = false;
8743           }
8744      (if (val <= 0)
8745       (if (ok && zero_val >= val)
8746        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8747       (if (val >= prec)
8748        (if (ok && zero_val < val)
8749         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8750        (if (ok && (zero_val < 0 || zero_val >= prec))
8751         (cmp (bit_and @0 { wide_int_to_tree (type0,
8752                                              wi::mask (val, false, prec)); })
8753              { build_zero_cst (type0); })))))))
8754 (for op (eq ne)
8755  (simplify
8756   /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8757   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8758    (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8759            tree type0 = TREE_TYPE (@0);
8760            int prec = TYPE_PRECISION (type0);
8761          }
8762     (if (prec <= MAX_FIXED_MODE_SIZE)
8763      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8764       (if (zero_val != wi::to_widest (@1))
8765        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8766       (if (zero_val < 0 || zero_val >= prec)
8767        (op (bit_and @0 { wide_int_to_tree (type0,
8768                                            wi::mask (tree_to_uhwi (@1) + 1,
8769                                                      false, prec)); })
8770            { wide_int_to_tree (type0,
8771                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8772                                                  false, prec)); })))))))
8774 #if GIMPLE
8775 /* ctz(ext(X)) == ctz(X).  Valid just for the UB at zero cases though.  */
8776 (simplify
8777   (CTZ (convert@1 @0))
8778   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8779        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8780        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8781    (with { combined_fn cfn = CFN_LAST;
8782            tree type0 = TREE_TYPE (@0);
8783            if (TREE_CODE (type0) == BITINT_TYPE)
8784              {
8785                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8786                  cfn = CFN_CTZ;
8787                else
8788                  type0
8789                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8790                                                      1);
8791              }
8792            type0 = unsigned_type_for (type0);
8793            if (cfn == CFN_LAST
8794                && direct_internal_fn_supported_p (IFN_CTZ, type0,
8795                                                   OPTIMIZE_FOR_BOTH))
8796              cfn = CFN_CTZ;
8797            if (cfn == CFN_LAST
8798                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8799                && !direct_internal_fn_supported_p (IFN_CTZ,
8800                                                    TREE_TYPE (@1),
8801                                                    OPTIMIZE_FOR_BOTH))
8802              {
8803                if (TYPE_PRECISION (type0)
8804                    == TYPE_PRECISION (unsigned_type_node))
8805                  cfn = CFN_BUILT_IN_CTZ;
8806                else if (TYPE_PRECISION (type0)
8807                         == TYPE_PRECISION (long_long_unsigned_type_node))
8808                  cfn = CFN_BUILT_IN_CTZLL;
8809              } }
8810     (if (cfn == CFN_CTZ)
8811      (IFN_CTZ (convert:type0 @0))
8812      (if (cfn == CFN_BUILT_IN_CTZ)
8813       (BUILT_IN_CTZ (convert:type0 @0))
8814       (if (cfn == CFN_BUILT_IN_CTZLL)
8815        (BUILT_IN_CTZLL (convert:type0 @0))))))))
8816 #endif
8818 /* POPCOUNT simplifications.  */
8819 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8820 (simplify
8821   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8822   (if (INTEGRAL_TYPE_P (type)
8823        && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8824                         widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8825            == 0))
8826    (with { tree utype = TREE_TYPE (@0);
8827            if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8828              utype = TREE_TYPE (@1); }
8829     (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8831 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8832 (for popcount (POPCOUNT)
8833   (for cmp (le eq ne gt)
8834        rep (eq eq ne ne)
8835     (simplify
8836       (cmp (popcount @0) integer_zerop)
8837       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8839 /* popcount(bswap(x)) is popcount(x).  */
8840 (for popcount (POPCOUNT)
8841   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8842               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8843     (simplify
8844       (popcount (convert?@0 (bswap:s@1 @2)))
8845       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8846            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8847         (with { tree type0 = TREE_TYPE (@0);
8848                 tree type1 = TREE_TYPE (@1);
8849                 unsigned int prec0 = TYPE_PRECISION (type0);
8850                 unsigned int prec1 = TYPE_PRECISION (type1); }
8851           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8852             (popcount (convert:type0 (convert:type1 @2)))))))))
8854 /* popcount(rotate(X Y)) is popcount(X).  */
8855 (for popcount (POPCOUNT)
8856   (for rot (lrotate rrotate)
8857     (simplify
8858       (popcount (convert?@0 (rot:s@1 @2 @3)))
8859       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8860            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8861            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8862         (with { tree type0 = TREE_TYPE (@0);
8863                 tree type1 = TREE_TYPE (@1);
8864                 unsigned int prec0 = TYPE_PRECISION (type0);
8865                 unsigned int prec1 = TYPE_PRECISION (type1); }
8866           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8867             (popcount (convert:type0 @2))))))))
8869 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8870 (simplify
8871   (bit_and (POPCOUNT @0) integer_onep)
8872   (PARITY @0))
8874 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8875 (simplify
8876   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8877   (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8879 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8880 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8881 (for popcount (POPCOUNT)
8882   (for log1 (bit_and bit_ior)
8883        log2 (bit_ior bit_and)
8884     (simplify
8885       (minus (plus:s (popcount:s @0) (popcount:s @1))
8886              (popcount:s (log1:cs @0 @1)))
8887       (popcount (log2 @0 @1)))
8888     (simplify
8889       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8890               (popcount:s @1))
8891       (popcount (log2 @0 @1)))))
8893 #if GIMPLE
8894 /* popcount(zext(X)) == popcount(X).  */
8895 (simplify
8896   (POPCOUNT (convert@1 @0))
8897   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8898        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8899        && TYPE_UNSIGNED (TREE_TYPE (@0))
8900        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8901    (with { combined_fn cfn = CFN_LAST;
8902            tree type0 = TREE_TYPE (@0);
8903            if (TREE_CODE (type0) == BITINT_TYPE)
8904              {
8905                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8906                  cfn = CFN_POPCOUNT;
8907                else
8908                  type0
8909                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8910                                                      1);
8911              }
8912            if (cfn == CFN_LAST
8913                && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8914                                                   OPTIMIZE_FOR_BOTH))
8915              cfn = CFN_POPCOUNT;
8916            if (cfn == CFN_LAST
8917                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8918                && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8919                                                    TREE_TYPE (@1),
8920                                                    OPTIMIZE_FOR_BOTH))
8921              {
8922                if (TYPE_PRECISION (type0)
8923                    == TYPE_PRECISION (unsigned_type_node))
8924                  cfn = CFN_BUILT_IN_POPCOUNT;
8925                else if (TYPE_PRECISION (type0)
8926                         == TYPE_PRECISION (long_long_unsigned_type_node))
8927                  cfn = CFN_BUILT_IN_POPCOUNTLL;
8928              } }
8929     (if (cfn == CFN_POPCOUNT)
8930      (IFN_POPCOUNT (convert:type0 @0))
8931      (if (cfn == CFN_BUILT_IN_POPCOUNT)
8932       (BUILT_IN_POPCOUNT (convert:type0 @0))
8933       (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8934        (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8935 #endif
8937 /* PARITY simplifications.  */
8938 /* parity(~X) is parity(X).  */
8939 (simplify
8940   (PARITY (bit_not @0))
8941   (PARITY @0))
8943 /* parity(bswap(x)) is parity(x).  */
8944 (for parity (PARITY)
8945   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8946               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8947     (simplify
8948       (parity (convert?@0 (bswap:s@1 @2)))
8949       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8950            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8951            && TYPE_PRECISION (TREE_TYPE (@0))
8952               >= TYPE_PRECISION (TREE_TYPE (@1)))
8953         (with { tree type0 = TREE_TYPE (@0);
8954                 tree type1 = TREE_TYPE (@1); }
8955           (parity (convert:type0 (convert:type1 @2))))))))
8957 /* parity(rotate(X Y)) is parity(X).  */
8958 (for parity (PARITY)
8959   (for rot (lrotate rrotate)
8960     (simplify
8961       (parity (convert?@0 (rot:s@1 @2 @3)))
8962       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8963            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8964            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8965            && TYPE_PRECISION (TREE_TYPE (@0))
8966               >= TYPE_PRECISION (TREE_TYPE (@1)))
8967         (with { tree type0 = TREE_TYPE (@0); }
8968           (parity (convert:type0 @2)))))))
8970 /* parity(X)^parity(Y) is parity(X^Y).  */
8971 (simplify
8972   (bit_xor (PARITY:s @0) (PARITY:s @1))
8973   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
8974    (PARITY (bit_xor @0 @1))
8975    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8976         && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8977     (with { tree utype = TREE_TYPE (@0);
8978             if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8979               utype = TREE_TYPE (@1); }
8980      (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
8982 #if GIMPLE
8983 /* parity(zext(X)) == parity(X).  */
8984 /* parity(sext(X)) == parity(X) if the difference in precision is even.  */
8985 (simplify
8986   (PARITY (convert@1 @0))
8987   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8988        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8989        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
8990        && (TYPE_UNSIGNED (TREE_TYPE (@0))
8991            || ((TYPE_PRECISION (TREE_TYPE (@1))
8992                 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
8993    (with { combined_fn cfn = CFN_LAST;
8994            tree type0 = TREE_TYPE (@0);
8995            if (TREE_CODE (type0) == BITINT_TYPE)
8996              {
8997                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8998                  cfn = CFN_PARITY;
8999                else
9000                  type0
9001                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9002                                                      1);
9003              }
9004            type0 = unsigned_type_for (type0);
9005            if (cfn == CFN_LAST
9006                && direct_internal_fn_supported_p (IFN_PARITY, type0,
9007                                                   OPTIMIZE_FOR_BOTH))
9008              cfn = CFN_PARITY;
9009            if (cfn == CFN_LAST
9010                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9011                && !direct_internal_fn_supported_p (IFN_PARITY,
9012                                                    TREE_TYPE (@1),
9013                                                    OPTIMIZE_FOR_BOTH))
9014              {
9015                if (TYPE_PRECISION (type0)
9016                    == TYPE_PRECISION (unsigned_type_node))
9017                  cfn = CFN_BUILT_IN_PARITY;
9018                else if (TYPE_PRECISION (type0)
9019                         == TYPE_PRECISION (long_long_unsigned_type_node))
9020                  cfn = CFN_BUILT_IN_PARITYLL;
9021              } }
9022     (if (cfn == CFN_PARITY)
9023      (IFN_PARITY (convert:type0 @0))
9024      (if (cfn == CFN_BUILT_IN_PARITY)
9025       (BUILT_IN_PARITY (convert:type0 @0))
9026       (if (cfn == CFN_BUILT_IN_PARITYLL)
9027        (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9028 #endif
9030 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9031 (for func (POPCOUNT BSWAP FFS PARITY)
9032  (simplify
9033   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9034   @3))
9036 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9037    where CST is precision-1. */
9038 (for func (CLRSB)
9039  (simplify
9040   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9041   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9042    @4)))
9044 #if GIMPLE
9045 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9046 (for func (CLZ)
9047  (simplify
9048   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9049   (with { int val;
9050           internal_fn ifn = IFN_LAST;
9051           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9052             {
9053               if (tree_fits_shwi_p (@2))
9054                 {
9055                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9056                   if ((int) valw == valw)
9057                     {
9058                       val = valw;
9059                       ifn = IFN_CLZ;
9060                     }
9061                 }
9062             }
9063           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9064                                                    OPTIMIZE_FOR_BOTH)
9065                    && CLZ_DEFINED_VALUE_AT_ZERO
9066                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9067             ifn = IFN_CLZ;
9068         }
9069    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9070     (IFN_CLZ @3 @2)))))
9071 (simplify
9072  (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9073   (with { int val;
9074           internal_fn ifn = IFN_LAST;
9075           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9076             ifn = IFN_CLZ;
9077           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9078                                                    OPTIMIZE_FOR_BOTH))
9079             ifn = IFN_CLZ;
9080         }
9081    (if (ifn == IFN_CLZ)
9082     (IFN_CLZ @3 @2))))
9084 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9085 (for func (CTZ)
9086  (simplify
9087   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9088   (with { int val;
9089           internal_fn ifn = IFN_LAST;
9090           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9091             {
9092               if (tree_fits_shwi_p (@2))
9093                 {
9094                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9095                   if ((int) valw == valw)
9096                     {
9097                       val = valw;
9098                       ifn = IFN_CTZ;
9099                     }
9100                 }
9101             }
9102           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9103                                                    OPTIMIZE_FOR_BOTH)
9104                    && CTZ_DEFINED_VALUE_AT_ZERO
9105                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9106             ifn = IFN_CTZ;
9107         }
9108    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9109     (IFN_CTZ @3 @2)))))
9110 (simplify
9111  (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9112   (with { int val;
9113           internal_fn ifn = IFN_LAST;
9114           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9115             ifn = IFN_CTZ;
9116           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9117                                                    OPTIMIZE_FOR_BOTH))
9118             ifn = IFN_CTZ;
9119         }
9120    (if (ifn == IFN_CTZ)
9121     (IFN_CTZ @3 @2))))
9122 #endif
9124 /* Common POPCOUNT/PARITY simplifications.  */
9125 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
9126 (for pfun (POPCOUNT PARITY)
9127   (simplify
9128     (pfun @0)
9129     (if (INTEGRAL_TYPE_P (type))
9130      (with { wide_int nz = tree_nonzero_bits (@0); }
9131        (switch
9132          (if (nz == 1)
9133            (convert @0))
9134          (if (wi::popcount (nz) == 1)
9135            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9136              (convert (rshift:utype (convert:utype @0)
9137                                     { build_int_cst (integer_type_node,
9138                                                      wi::ctz (nz)); })))))))))
9140 #if GIMPLE
9141 /* 64- and 32-bits branchless implementations of popcount are detected:
9143    int popcount64c (uint64_t x)
9144    {
9145      x -= (x >> 1) & 0x5555555555555555ULL;
9146      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9147      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9148      return (x * 0x0101010101010101ULL) >> 56;
9149    }
9151    int popcount32c (uint32_t x)
9152    {
9153      x -= (x >> 1) & 0x55555555;
9154      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9155      x = (x + (x >> 4)) & 0x0f0f0f0f;
9156      return (x * 0x01010101) >> 24;
9157    }  */
9158 (simplify
9159  (rshift
9160   (mult
9161    (bit_and
9162     (plus:c
9163      (rshift @8 INTEGER_CST@5)
9164       (plus:c@8
9165        (bit_and @6 INTEGER_CST@7)
9166         (bit_and
9167          (rshift
9168           (minus@6 @0
9169            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9170           INTEGER_CST@10)
9171          INTEGER_CST@9)))
9172     INTEGER_CST@3)
9173    INTEGER_CST@2)
9174   INTEGER_CST@1)
9175   /* Check constants and optab.  */
9176   (with { unsigned prec = TYPE_PRECISION (type);
9177           int shift = (64 - prec) & 63;
9178           unsigned HOST_WIDE_INT c1
9179             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9180           unsigned HOST_WIDE_INT c2
9181             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9182           unsigned HOST_WIDE_INT c3
9183             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9184           unsigned HOST_WIDE_INT c4
9185             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9186    }
9187    (if (prec >= 16
9188         && prec <= 64
9189         && pow2p_hwi (prec)
9190         && TYPE_UNSIGNED (type)
9191         && integer_onep (@4)
9192         && wi::to_widest (@10) == 2
9193         && wi::to_widest (@5) == 4
9194         && wi::to_widest (@1) == prec - 8
9195         && tree_to_uhwi (@2) == c1
9196         && tree_to_uhwi (@3) == c2
9197         && tree_to_uhwi (@9) == c3
9198         && tree_to_uhwi (@7) == c3
9199         && tree_to_uhwi (@11) == c4)
9200     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9201                                          OPTIMIZE_FOR_BOTH))
9202      (convert (IFN_POPCOUNT:type @0))
9203      /* Try to do popcount in two halves.  PREC must be at least
9204         five bits for this to work without extension before adding.  */
9205      (with {
9206        tree half_type = NULL_TREE;
9207        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9208        int half_prec = 8;
9209        if (m.exists ()
9210            && m.require () != TYPE_MODE (type))
9211          {
9212            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9213            half_type = build_nonstandard_integer_type (half_prec, 1);
9214          }
9215        gcc_assert (half_prec > 2);
9216       }
9217       (if (half_type != NULL_TREE
9218            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9219                                               OPTIMIZE_FOR_BOTH))
9220        (convert (plus
9221          (IFN_POPCOUNT:half_type (convert @0))
9222          (IFN_POPCOUNT:half_type (convert (rshift @0
9223             { build_int_cst (integer_type_node, half_prec); } )))))))))))
9225 /* __builtin_ffs needs to deal on many targets with the possible zero
9226    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
9227    should lead to better code.  */
9228 (simplify
9229  (FFS tree_expr_nonzero_p@0)
9230  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9231       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9232                                          OPTIMIZE_FOR_SPEED))
9233   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9234    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9235 #endif
9237 (for ffs (FFS)
9238  /* __builtin_ffs (X) == 0 -> X == 0.
9239     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
9240  (for cmp (eq ne)
9241   (simplify
9242    (cmp (ffs@2 @0) INTEGER_CST@1)
9243     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9244      (switch
9245       (if (integer_zerop (@1))
9246        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9247       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9248        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9249       (if (single_use (@2))
9250        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9251                                             wi::mask (tree_to_uhwi (@1),
9252                                                       false, prec)); })
9253             { wide_int_to_tree (TREE_TYPE (@0),
9254                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9255                                                   false, prec)); }))))))
9257  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
9258  (for cmp (gt le)
9259       cmp2 (ne eq)
9260       cmp3 (eq ne)
9261       bit_op (bit_and bit_ior)
9262   (simplify
9263    (cmp (ffs@2 @0) INTEGER_CST@1)
9264     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9265      (switch
9266       (if (integer_zerop (@1))
9267        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9268       (if (tree_int_cst_sgn (@1) < 0)
9269        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9270       (if (wi::to_widest (@1) >= prec)
9271        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9272       (if (wi::to_widest (@1) == prec - 1)
9273        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9274                                     wi::shifted_mask (prec - 1, 1,
9275                                                       false, prec)); }))
9276       (if (single_use (@2))
9277        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9278                (cmp3 (bit_and @0
9279                               { wide_int_to_tree (TREE_TYPE (@0),
9280                                                   wi::mask (tree_to_uhwi (@1),
9281                                                   false, prec)); })
9282                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
9284 #if GIMPLE
9285 /* ffs(ext(X)) == ffs(X).  */
9286 (simplify
9287   (FFS (convert@1 @0))
9288   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9289        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9290        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9291    (with { combined_fn cfn = CFN_LAST;
9292            tree type0 = TREE_TYPE (@0);
9293            if (TREE_CODE (type0) == BITINT_TYPE)
9294              {
9295                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9296                  cfn = CFN_FFS;
9297                else
9298                  type0
9299                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9300                                                      0);
9301              }
9302            type0 = signed_type_for (type0);
9303            if (cfn == CFN_LAST
9304                && direct_internal_fn_supported_p (IFN_FFS, type0,
9305                                                   OPTIMIZE_FOR_BOTH))
9306              cfn = CFN_FFS;
9307            if (cfn == CFN_LAST
9308                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9309                && !direct_internal_fn_supported_p (IFN_FFS,
9310                                                    TREE_TYPE (@1),
9311                                                    OPTIMIZE_FOR_BOTH))
9312              {
9313                if (TYPE_PRECISION (type0)
9314                    == TYPE_PRECISION (integer_type_node))
9315                  cfn = CFN_BUILT_IN_FFS;
9316                else if (TYPE_PRECISION (type0)
9317                         == TYPE_PRECISION (long_long_integer_type_node))
9318                  cfn = CFN_BUILT_IN_FFSLL;
9319              } }
9320     (if (cfn == CFN_FFS)
9321      (IFN_FFS (convert:type0 @0))
9322      (if (cfn == CFN_BUILT_IN_FFS)
9323       (BUILT_IN_FFS (convert:type0 @0))
9324       (if (cfn == CFN_BUILT_IN_FFSLL)
9325        (BUILT_IN_FFSLL (convert:type0 @0))))))))
9326 #endif
9328 #if GIMPLE
9330 /* Simplify:
9331      a = op a1
9332      r = cond ? a : b
9333      --> r = .COND_FN (cond, a, b)
9334 and,
9335     a = op a1
9336     r = cond ? b : a
9337     --> r = .COND_FN (~cond, b, a).  */
9339 (for uncond_op (UNCOND_UNARY)
9340      cond_op (COND_UNARY)
9341  (simplify
9342   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9343    (with { tree op_type = TREE_TYPE (@3); }
9344     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9345         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9346      (cond_op @0 (view_convert @1) @2))))
9347  (simplify
9348   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9349    (with { tree op_type = TREE_TYPE (@3); }
9350     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9351         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9352      (cond_op (bit_not @0) (view_convert @2) @1)))))
9354 (for uncond_op (UNCOND_UNARY)
9355      cond_op (COND_LEN_UNARY)
9356  (simplify
9357   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9358    (with { tree op_type = TREE_TYPE (@3); }
9359     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9360         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9361      (cond_op @0 (view_convert @1) @2 @4 @5))))
9362  (simplify
9363   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9364    (with { tree op_type = TREE_TYPE (@3); }
9365     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9366         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9367      (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9369 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
9370 (simplify
9371  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9372  (if (canonicalize_math_after_vectorization_p ()
9373       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9374       && is_truth_type_for (type, TREE_TYPE (@0)))
9375  (if (integer_all_onesp (@1) && integer_zerop (@2))
9376   (IFN_COND_NOT @0 @3 @3))
9377   (if (integer_all_onesp (@2) && integer_zerop (@1))
9378    (IFN_COND_NOT (bit_not @0) @3 @3))))
9380 /* Simplify:
9382      a = a1 op a2
9383      r = c ? a : b;
9385    to:
9387      r = c ? a1 op a2 : b;
9389    if the target can do it in one go.  This makes the operation conditional
9390    on c, so could drop potentially-trapping arithmetic, but that's a valid
9391    simplification if the result of the operation isn't needed.
9393    Avoid speculatively generating a stand-alone vector comparison
9394    on targets that might not support them.  Any target implementing
9395    conditional internal functions must support the same comparisons
9396    inside and outside a VEC_COND_EXPR.  */
9398 (for uncond_op (UNCOND_BINARY)
9399      cond_op (COND_BINARY)
9400  (simplify
9401   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9402   (with { tree op_type = TREE_TYPE (@4); }
9403    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9404         && is_truth_type_for (op_type, TREE_TYPE (@0))
9405         && single_use (@4))
9406     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9407  (simplify
9408   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9409   (with { tree op_type = TREE_TYPE (@4); }
9410    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9411         && is_truth_type_for (op_type, TREE_TYPE (@0))
9412         && single_use (@4))
9413     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9415 (for uncond_op (UNCOND_BINARY)
9416      cond_op (COND_LEN_BINARY)
9417  (simplify
9418   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9419   (with { tree op_type = TREE_TYPE (@4); }
9420    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9421         && is_truth_type_for (op_type, TREE_TYPE (@0))
9422         && single_use (@4))
9423     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9424  (simplify
9425   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9426   (with { tree op_type = TREE_TYPE (@4); }
9427    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9428         && is_truth_type_for (op_type, TREE_TYPE (@0))
9429         && single_use (@4))
9430     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9432 /* Same for ternary operations.  */
9433 (for uncond_op (UNCOND_TERNARY)
9434      cond_op (COND_TERNARY)
9435  (simplify
9436   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9437   (with { tree op_type = TREE_TYPE (@5); }
9438    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9439         && is_truth_type_for (op_type, TREE_TYPE (@0))
9440         && single_use (@5))
9441     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9442  (simplify
9443   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9444   (with { tree op_type = TREE_TYPE (@5); }
9445    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9446         && is_truth_type_for (op_type, TREE_TYPE (@0))
9447         && single_use (@5))
9448     (view_convert (cond_op (bit_not @0) @2 @3 @4
9449                   (view_convert:op_type @1)))))))
9451 (for uncond_op (UNCOND_TERNARY)
9452      cond_op (COND_LEN_TERNARY)
9453  (simplify
9454   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9455   (with { tree op_type = TREE_TYPE (@5); }
9456    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9457         && is_truth_type_for (op_type, TREE_TYPE (@0))
9458         && single_use (@5))
9459     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9460  (simplify
9461   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9462   (with { tree op_type = TREE_TYPE (@5); }
9463    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9464         && is_truth_type_for (op_type, TREE_TYPE (@0))
9465         && single_use (@5))
9466     (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9467 #endif
9469 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9470    "else" value of an IFN_COND_*.  */
9471 (for cond_op (COND_BINARY)
9472  (simplify
9473   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9474   (with { tree op_type = TREE_TYPE (@3); }
9475    (if (element_precision (type) == element_precision (op_type))
9476     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9477  (simplify
9478   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9479   (with { tree op_type = TREE_TYPE (@5); }
9480    (if (inverse_conditions_p (@0, @2)
9481         && element_precision (type) == element_precision (op_type))
9482     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9484 /* Same for ternary operations.  */
9485 (for cond_op (COND_TERNARY)
9486  (simplify
9487   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9488   (with { tree op_type = TREE_TYPE (@4); }
9489    (if (element_precision (type) == element_precision (op_type))
9490     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9491  (simplify
9492   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9493   (with { tree op_type = TREE_TYPE (@6); }
9494    (if (inverse_conditions_p (@0, @2)
9495         && element_precision (type) == element_precision (op_type))
9496     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9498 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9499    "else" value of an IFN_COND_LEN_*.  */
9500 (for cond_len_op (COND_LEN_BINARY)
9501  (simplify
9502   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9503   (with { tree op_type = TREE_TYPE (@3); }
9504    (if (element_precision (type) == element_precision (op_type))
9505     (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9506  (simplify
9507   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9508   (with { tree op_type = TREE_TYPE (@5); }
9509    (if (inverse_conditions_p (@0, @2)
9510         && element_precision (type) == element_precision (op_type))
9511     (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9513 /* Same for ternary operations.  */
9514 (for cond_len_op (COND_LEN_TERNARY)
9515  (simplify
9516   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9517   (with { tree op_type = TREE_TYPE (@4); }
9518    (if (element_precision (type) == element_precision (op_type))
9519     (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9520  (simplify
9521   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9522   (with { tree op_type = TREE_TYPE (@6); }
9523    (if (inverse_conditions_p (@0, @2)
9524         && element_precision (type) == element_precision (op_type))
9525     (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9527 /* Detect simplication for a conditional reduction where
9529    a = mask1 ? b : 0
9530    c = mask2 ? d + a : d
9532    is turned into
9534    c = mask1 && mask2 ? d + b : d.  */
9535 (simplify
9536   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9537    (if (ANY_INTEGRAL_TYPE_P (type)
9538         || (FLOAT_TYPE_P (type)
9539             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9540    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9542 /* Detect simplication for a conditional length reduction where
9544    a = mask ? b : 0
9545    c = i < len + bias ? d + a : d
9547    is turned into
9549    c = mask && i < len + bias ? d + b : d.  */
9550 (simplify
9551   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9552    (if (ANY_INTEGRAL_TYPE_P (type)
9553         || (FLOAT_TYPE_P (type)
9554             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9555     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9557 /* Detect simplification for vector condition folding where
9559   c = mask1 ? (masked_op mask2 a b) : b
9561   into
9563   c = masked_op (mask1 & mask2) a b
9565   where the operation can be partially applied to one operand. */
9567 (for cond_op (COND_BINARY)
9568  (simplify
9569   (vec_cond @0
9570    (cond_op:s @1 @2 @3 @4) @3)
9571   (cond_op (bit_and @1 @0) @2 @3 @4)))
9573 /* And same for ternary expressions.  */
9575 (for cond_op (COND_TERNARY)
9576  (simplify
9577   (vec_cond @0
9578    (cond_op:s @1 @2 @3 @4 @5) @4)
9579   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9581 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9582    expressions like:
9584    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9585    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9587    If pointers are known not to wrap, B checks whether @1 bytes starting
9588    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9589    bytes.  A is more efficiently tested as:
9591    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9593    The equivalent expression for B is given by replacing @1 with @1 - 1:
9595    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9597    @0 and @2 can be swapped in both expressions without changing the result.
9599    The folds rely on sizetype's being unsigned (which is always true)
9600    and on its being the same width as the pointer (which we have to check).
9602    The fold replaces two pointer_plus expressions, two comparisons and
9603    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9604    the best case it's a saving of two operations.  The A fold retains one
9605    of the original pointer_pluses, so is a win even if both pointer_pluses
9606    are used elsewhere.  The B fold is a wash if both pointer_pluses are
9607    used elsewhere, since all we end up doing is replacing a comparison with
9608    a pointer_plus.  We do still apply the fold under those circumstances
9609    though, in case applying it to other conditions eventually makes one of the
9610    pointer_pluses dead.  */
9611 (for ior (truth_orif truth_or bit_ior)
9612  (for cmp (le lt)
9613   (simplify
9614    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9615         (cmp:cs (pointer_plus@4 @2 @1) @0))
9616    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9617         && TYPE_OVERFLOW_WRAPS (sizetype)
9618         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9619     /* Calculate the rhs constant.  */
9620     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9621             offset_int rhs = off * 2; }
9622      /* Always fails for negative values.  */
9623      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9624       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9625          pick a canonical order.  This increases the chances of using the
9626          same pointer_plus in multiple checks.  */
9627       (with { bool swap_p = tree_swap_operands_p (@0, @2);
9628               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9629        (if (cmp == LT_EXPR)
9630         (gt (convert:sizetype
9631              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9632                                      { swap_p ? @0 : @2; }))
9633             { rhs_tree; })
9634         (gt (convert:sizetype
9635              (pointer_diff:ssizetype
9636               (pointer_plus { swap_p ? @2 : @0; }
9637                             { wide_int_to_tree (sizetype, off); })
9638               { swap_p ? @0 : @2; }))
9639             { rhs_tree; })))))))))
9641 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9642    element of @1.  */
9643 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9644  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9645   (with { int i = single_nonzero_element (@1); }
9646    (if (i >= 0)
9647     (with { tree elt = vector_cst_elt (@1, i);
9648             tree elt_type = TREE_TYPE (elt);
9649             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9650             tree size = bitsize_int (elt_bits);
9651             tree pos = bitsize_int (elt_bits * i); }
9652      (view_convert
9653       (bit_and:elt_type
9654        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9655        { elt; })))))))
9657 /* Fold reduction of a single nonzero element constructor.  */
9658 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9659   (simplify (reduc (CONSTRUCTOR@0))
9660     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9661                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9662             tree elt = ctor_single_nonzero_element (ctor); }
9663       (if (elt
9664            && !HONOR_SNANS (type)
9665            && !HONOR_SIGNED_ZEROS (type))
9666         { elt; }))))
9668 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
9669 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9670             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9671      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9672   (simplify (reduc (op @0 VECTOR_CST@1))
9673     (op (reduc:type @0) (reduc:type @1))))
9675 /* Simplify vector floating point operations of alternating sub/add pairs
9676    into using an fneg of a wider element type followed by a normal add.
9677    under IEEE 754 the fneg of the wider type will negate every even entry
9678    and when doing an add we get a sub of the even and add of every odd
9679    elements.  */
9680 (for plusminus (plus minus)
9681      minusplus (minus plus)
9682  (simplify
9683   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9684    (if (!VECTOR_INTEGER_TYPE_P (type)
9685         && !FLOAT_WORDS_BIG_ENDIAN
9686         /* plus is commutative, while minus is not, so :c can't be used.
9687            Do equality comparisons by hand and at the end pick the operands
9688            from the minus.  */
9689         && (operand_equal_p (@0, @2, 0)
9690             ? operand_equal_p (@1, @3, 0)
9691             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9692    (with
9693     {
9694       /* Build a vector of integers from the tree mask.  */
9695       vec_perm_builder builder;
9696     }
9697     (if (tree_to_vec_perm_builder (&builder, @4))
9698      (with
9699       {
9700         /* Create a vec_perm_indices for the integer vector.  */
9701         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9702         vec_perm_indices sel (builder, 2, nelts);
9703         machine_mode vec_mode = TYPE_MODE (type);
9704         machine_mode wide_mode;
9705         scalar_mode wide_elt_mode;
9706         poly_uint64 wide_nunits;
9707         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9708       }
9709       (if (VECTOR_MODE_P (vec_mode)
9710            && sel.series_p (0, 2, 0, 2)
9711            && sel.series_p (1, 2, nelts + 1, 2)
9712            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9713            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9714            && related_vector_mode (vec_mode, wide_elt_mode,
9715                                    wide_nunits).exists (&wide_mode))
9716        (with
9717         {
9718           tree stype
9719             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9720                                               TYPE_UNSIGNED (type));
9721           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9723           /* The format has to be a non-extended ieee format.  */
9724           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9725           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9726         }
9727         (if (TYPE_MODE (stype) != BLKmode
9728              && VECTOR_TYPE_P (ntype)
9729              && fmt_old != NULL
9730              && fmt_new != NULL)
9731          (with
9732           {
9733             /* If the target doesn't support v1xx vectors, try using
9734                scalar mode xx instead.  */
9735             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9736                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9737               ntype = stype;
9738           }
9739           (if (fmt_new->signbit_rw
9740                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9741                && fmt_new->signbit_rw == fmt_new->signbit_ro
9742                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9743                                                  TYPE_MODE (type), ALL_REGS)
9744                && ((optimize_vectors_before_lowering_p ()
9745                     && VECTOR_TYPE_P (ntype))
9746                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9747            (if (plusminus == PLUS_EXPR)
9748             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9749             (minus @0 (view_convert:type
9750                         (negate (view_convert:ntype @1))))))))))))))))
9752 (simplify
9753  (vec_perm @0 @1 VECTOR_CST@2)
9754  (with
9755   {
9756     tree op0 = @0, op1 = @1, op2 = @2;
9757     machine_mode result_mode = TYPE_MODE (type);
9758     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9760     /* Build a vector of integers from the tree mask.  */
9761     vec_perm_builder builder;
9762   }
9763   (if (tree_to_vec_perm_builder (&builder, op2))
9764    (with
9765     {
9766       /* Create a vec_perm_indices for the integer vector.  */
9767       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9768       bool single_arg = (op0 == op1);
9769       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9770     }
9771     (if (sel.series_p (0, 1, 0, 1))
9772      { op0; }
9773      (if (sel.series_p (0, 1, nelts, 1))
9774       { op1; }
9775       (with
9776        {
9777          if (!single_arg)
9778            {
9779              if (sel.all_from_input_p (0))
9780                op1 = op0;
9781              else if (sel.all_from_input_p (1))
9782                {
9783                  op0 = op1;
9784                  sel.rotate_inputs (1);
9785                }
9786              else if (known_ge (poly_uint64 (sel[0]), nelts))
9787                {
9788                  std::swap (op0, op1);
9789                  sel.rotate_inputs (1);
9790                }
9791            }
9792          gassign *def;
9793          tree cop0 = op0, cop1 = op1;
9794          if (TREE_CODE (op0) == SSA_NAME
9795              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9796              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9797            cop0 = gimple_assign_rhs1 (def);
9798          if (TREE_CODE (op1) == SSA_NAME
9799              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9800              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9801            cop1 = gimple_assign_rhs1 (def);
9802          tree t;
9803        }
9804        (if ((TREE_CODE (cop0) == VECTOR_CST
9805              || TREE_CODE (cop0) == CONSTRUCTOR)
9806             && (TREE_CODE (cop1) == VECTOR_CST
9807                 || TREE_CODE (cop1) == CONSTRUCTOR)
9808             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9809         { t; }
9810         (with
9811          {
9812            bool changed = (op0 == op1 && !single_arg);
9813            tree ins = NULL_TREE;
9814            unsigned at = 0;
9816            /* See if the permutation is performing a single element
9817               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9818               in that case.  But only if the vector mode is supported,
9819               otherwise this is invalid GIMPLE.  */
9820            if (op_mode != BLKmode
9821                && (TREE_CODE (cop0) == VECTOR_CST
9822                    || TREE_CODE (cop0) == CONSTRUCTOR
9823                    || TREE_CODE (cop1) == VECTOR_CST
9824                    || TREE_CODE (cop1) == CONSTRUCTOR))
9825              {
9826                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9827                if (insert_first_p)
9828                  {
9829                    /* After canonicalizing the first elt to come from the
9830                       first vector we only can insert the first elt from
9831                       the first vector.  */
9832                    at = 0;
9833                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9834                      op0 = op1;
9835                  }
9836                /* The above can fail for two-element vectors which always
9837                   appear to insert the first element, so try inserting
9838                   into the second lane as well.  For more than two
9839                   elements that's wasted time.  */
9840                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9841                  {
9842                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9843                    for (at = 0; at < encoded_nelts; ++at)
9844                      if (maybe_ne (sel[at], at))
9845                        break;
9846                    if (at < encoded_nelts
9847                        && (known_eq (at + 1, nelts)
9848                            || sel.series_p (at + 1, 1, at + 1, 1)))
9849                      {
9850                        if (known_lt (poly_uint64 (sel[at]), nelts))
9851                          ins = fold_read_from_vector (cop0, sel[at]);
9852                        else
9853                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9854                      }
9855                  }
9856              }
9858            /* Generate a canonical form of the selector.  */
9859            if (!ins && sel.encoding () != builder)
9860              {
9861                /* Some targets are deficient and fail to expand a single
9862                   argument permutation while still allowing an equivalent
9863                   2-argument version.  */
9864                tree oldop2 = op2;
9865                if (sel.ninputs () == 2
9866                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9867                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9868                else
9869                  {
9870                    vec_perm_indices sel2 (builder, 2, nelts);
9871                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9872                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9873                    else
9874                      /* Not directly supported with either encoding,
9875                         so use the preferred form.  */
9876                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9877                  }
9878                if (!operand_equal_p (op2, oldop2, 0))
9879                  changed = true;
9880              }
9881          }
9882          (if (ins)
9883           (bit_insert { op0; } { ins; }
9884            { bitsize_int (at * vector_element_bits (type)); })
9885           (if (changed)
9886            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9888 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9890 (match vec_same_elem_p
9891  (vec_duplicate @0))
9893 (match vec_same_elem_p
9894  CONSTRUCTOR@0
9895  (if (TREE_CODE (@0) == SSA_NAME
9896       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9898 (match vec_same_elem_p
9899  @0
9900  (if (uniform_vector_p (@0))))
9903 (simplify
9904  (vec_perm vec_same_elem_p@0 @0 @1)
9905  (if (types_match (type, TREE_TYPE (@0)))
9906   @0
9907   (with
9908    {
9909      tree elem = uniform_vector_p (@0);
9910    }
9911    (if (elem)
9912     { build_vector_from_val (type, elem); }))))
9914 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9915 (simplify
9916  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9917  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9918   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9919 (simplify
9920  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9921  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9922   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9925 /* Merge
9926      c = VEC_PERM_EXPR <a, b, VCST0>;
9927      d = VEC_PERM_EXPR <c, c, VCST1>;
9928    to
9929      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9931 (simplify
9932  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9933  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9934   (with
9935    {
9936      machine_mode result_mode = TYPE_MODE (type);
9937      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9938      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9939      vec_perm_builder builder0;
9940      vec_perm_builder builder1;
9941      vec_perm_builder builder2 (nelts, nelts, 1);
9942    }
9943    (if (tree_to_vec_perm_builder (&builder0, @3)
9944         && tree_to_vec_perm_builder (&builder1, @4))
9945     (with
9946      {
9947        vec_perm_indices sel0 (builder0, 2, nelts);
9948        vec_perm_indices sel1 (builder1, 1, nelts);
9950        for (int i = 0; i < nelts; i++)
9951          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9953        vec_perm_indices sel2 (builder2, 2, nelts);
9955        tree op0 = NULL_TREE;
9956        /* If the new VEC_PERM_EXPR can't be handled but both
9957           original VEC_PERM_EXPRs can, punt.
9958           If one or both of the original VEC_PERM_EXPRs can't be
9959           handled and the new one can't be either, don't increase
9960           number of VEC_PERM_EXPRs that can't be handled.  */
9961        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9962            || (single_use (@0)
9963                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9964                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9965                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9966          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9967      }
9968      (if (op0)
9969       (vec_perm @1 @2 { op0; })))))))
9971 /* Merge
9972      c = VEC_PERM_EXPR <a, b, VCST0>;
9973      d = VEC_PERM_EXPR <x, c, VCST1>;
9974    to
9975      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9976    when all elements from a or b are replaced by the later
9977    permutation.  */
9979 (simplify
9980  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9981  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9982   (with
9983    {
9984      machine_mode result_mode = TYPE_MODE (type);
9985      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9986      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9987      vec_perm_builder builder0;
9988      vec_perm_builder builder1;
9989      vec_perm_builder builder2 (nelts, nelts, 2);
9990    }
9991    (if (tree_to_vec_perm_builder (&builder0, @3)
9992         && tree_to_vec_perm_builder (&builder1, @4))
9993     (with
9994      {
9995        vec_perm_indices sel0 (builder0, 2, nelts);
9996        vec_perm_indices sel1 (builder1, 2, nelts);
9997        bool use_1 = false, use_2 = false;
9999        for (int i = 0; i < nelts; i++)
10000          {
10001            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10002              builder2.quick_push (sel1[i]);
10003            else
10004              {
10005                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10006                                     .to_constant ()];
10007                if (known_lt (j, sel0.nelts_per_input ()))
10008                  use_1 = true;
10009                else
10010                  {
10011                    use_2 = true;
10012                    j -= sel0.nelts_per_input ();
10013                  }
10014                builder2.quick_push (j + sel1.nelts_per_input ());
10015              }
10016          }
10017      }
10018      (if (use_1 ^ use_2)
10019       (with
10020        {
10021          vec_perm_indices sel2 (builder2, 2, nelts);
10022          tree op0 = NULL_TREE;
10023          /* If the new VEC_PERM_EXPR can't be handled but both
10024             original VEC_PERM_EXPRs can, punt.
10025             If one or both of the original VEC_PERM_EXPRs can't be
10026             handled and the new one can't be either, don't increase
10027             number of VEC_PERM_EXPRs that can't be handled.  */
10028          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10029              || (single_use (@0)
10030                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10031                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10032                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10033            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10034        }
10035        (if (op0)
10036         (switch
10037          (if (use_1)
10038           (vec_perm @5 @1 { op0; }))
10039          (if (use_2)
10040           (vec_perm @5 @2 { op0; })))))))))))
10042 /* And the case with swapped outer permute sources.  */
10044 (simplify
10045  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10046  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10047   (with
10048    {
10049      machine_mode result_mode = TYPE_MODE (type);
10050      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10051      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10052      vec_perm_builder builder0;
10053      vec_perm_builder builder1;
10054      vec_perm_builder builder2 (nelts, nelts, 2);
10055    }
10056    (if (tree_to_vec_perm_builder (&builder0, @3)
10057         && tree_to_vec_perm_builder (&builder1, @4))
10058     (with
10059      {
10060        vec_perm_indices sel0 (builder0, 2, nelts);
10061        vec_perm_indices sel1 (builder1, 2, nelts);
10062        bool use_1 = false, use_2 = false;
10064        for (int i = 0; i < nelts; i++)
10065          {
10066            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10067              builder2.quick_push (sel1[i]);
10068            else
10069              {
10070                poly_uint64 j = sel0[sel1[i].to_constant ()];
10071                if (known_lt (j, sel0.nelts_per_input ()))
10072                  use_1 = true;
10073                else
10074                  {
10075                    use_2 = true;
10076                    j -= sel0.nelts_per_input ();
10077                  }
10078                builder2.quick_push (j);
10079              }
10080          }
10081      }
10082      (if (use_1 ^ use_2)
10083       (with
10084        {
10085          vec_perm_indices sel2 (builder2, 2, nelts);
10086          tree op0 = NULL_TREE;
10087          /* If the new VEC_PERM_EXPR can't be handled but both
10088             original VEC_PERM_EXPRs can, punt.
10089             If one or both of the original VEC_PERM_EXPRs can't be
10090             handled and the new one can't be either, don't increase
10091             number of VEC_PERM_EXPRs that can't be handled.  */
10092          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10093              || (single_use (@0)
10094                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10095                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10096                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10097            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10098        }
10099        (if (op0)
10100         (switch
10101          (if (use_1)
10102           (vec_perm @1 @5 { op0; }))
10103          (if (use_2)
10104           (vec_perm @2 @5 { op0; })))))))))))
10107 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10108    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10109    constant which when multiplied by a power of 2 contains a unique value
10110    in the top 5 or 6 bits.  This is then indexed into a table which maps it
10111    to the number of trailing zeroes.  */
10112 (match (ctz_table_index @1 @2 @3)
10113   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10115 (match (cond_expr_convert_p @0 @2 @3 @6)
10116  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10117   (if (INTEGRAL_TYPE_P (type)
10118        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10119        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10120        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10121        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10122        && TYPE_PRECISION (TREE_TYPE (@0))
10123           == TYPE_PRECISION (TREE_TYPE (@2))
10124        && TYPE_PRECISION (TREE_TYPE (@0))
10125           == TYPE_PRECISION (TREE_TYPE (@3))
10126        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10127           signess when convert is truncation, but not ok for extension since
10128           it's sign_extend vs zero_extend.  */
10129        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10130            || (TYPE_UNSIGNED (TREE_TYPE (@2))
10131                == TYPE_UNSIGNED (TREE_TYPE (@3))))
10132        && single_use (@4)
10133        && single_use (@5))))
10135 (for bit_op (bit_and bit_ior bit_xor)
10136  (match (bitwise_induction_p @0 @2 @3)
10137   (bit_op:c
10138    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10139    @3)))
10141 (match (bitwise_induction_p @0 @2 @3)
10142  (bit_not
10143   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10145 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
10146    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
10147 (simplify
10148   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10149   (with { auto i = wi::neg (wi::to_wide (@2)); }
10150   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
10151     (if (wi::popcount (i) == 1
10152          && (wi::to_wide (@1)) == (i - 1))
10153       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10154         (bit_and @0 @1)
10155       (cond (le @0 @1) @0 (bit_and @0 @1))))))
10157 /* -x & 1 -> x & 1.  */
10158 (simplify 
10159  (bit_and (negate @0) integer_onep@1)
10160  (if (!TYPE_OVERFLOW_SANITIZED (type))
10161   (bit_and @0 @1)))
10163 /* `-a` is just `a` if the type is 1bit wide or when converting
10164    to a 1bit type; similar to the above transformation of `(-x)&1`.
10165    This is used mostly with the transformation of
10166    `a ? ~b : b` into `(-a)^b`.
10167    It also can show up with bitfields.  */
10168 (simplify
10169  (convert? (negate @0))
10170  (if (INTEGRAL_TYPE_P (type)
10171       && TYPE_PRECISION (type) == 1
10172       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10173   (convert @0)))
10175 /* Optimize
10176    c1 = VEC_PERM_EXPR (a, a, mask)
10177    c2 = VEC_PERM_EXPR (b, b, mask)
10178    c3 = c1 op c2
10179    -->
10180    c = a op b
10181    c3 = VEC_PERM_EXPR (c, c, mask)
10182    For all integer non-div operations.  */
10183 (for op (plus minus mult bit_and bit_ior bit_xor
10184          lshift rshift)
10185  (simplify
10186   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10187    (if (VECTOR_INTEGER_TYPE_P (type))
10188     (vec_perm (op@3 @0 @1) @3 @2))))
10190 /* Similar for float arithmetic when permutation constant covers
10191    all vector elements.  */
10192 (for op (plus minus mult)
10193  (simplify
10194   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10195    (if (VECTOR_FLOAT_TYPE_P (type)
10196         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10197     (with
10198      {
10199        tree perm_cst = @2;
10200        vec_perm_builder builder;
10201        bool full_perm_p = false;
10202        if (tree_to_vec_perm_builder (&builder, perm_cst))
10203          {
10204            unsigned HOST_WIDE_INT nelts;
10206            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10207            /* Create a vec_perm_indices for the VECTOR_CST.  */
10208            vec_perm_indices sel (builder, 1, nelts);
10210            /* Check if perm indices covers all vector elements.  */
10211            if (sel.encoding ().encoded_full_vector_p ())
10212              {
10213                auto_sbitmap seen (nelts);
10214                bitmap_clear (seen);
10216                unsigned HOST_WIDE_INT count = 0, i;
10218                for (i = 0; i < nelts; i++)
10219                  {
10220                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10221                     break;
10222                    count++;
10223                  }
10224                full_perm_p = count == nelts;
10225              }
10226          }
10227       }
10228       (if (full_perm_p)
10229         (vec_perm (op@3 @0 @1) @3 @2))))))