tree-optimization/113126 - vector extension compare optimization
[official-gcc.git] / gcc / match.pd
blobabbd03742f9932c6ddb44a188a71006aba1f7561
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).  If the target does not
1164    support the copysign optab then canonicalize
1165    copysign(x, -CST) -> fneg (abs (x)).   */
1166 (for copysigns (COPYSIGN_ALL)
1167  (simplify
1168   (copysigns @0 REAL_CST@1)
1169   (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1170    (abs @0)
1171 #if GIMPLE
1172    (if (!direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1173                                          OPTIMIZE_FOR_BOTH))
1174     (negate (abs @0)))
1175 #endif
1176    )))
1178 #if GIMPLE
1179 /* Transform fneg (fabs (X)) -> copysign (X, -1) as the canonical
1180    representation if the target supports the copysign optab.  */
1181 (simplify
1182  (negate (abs @0))
1183  (if (direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1184                                       OPTIMIZE_FOR_BOTH))
1185    (IFN_COPYSIGN @0 { build_minus_one_cst (type); })))
1186 #endif
1187 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1188 (for copysigns (COPYSIGN_ALL)
1189  (simplify
1190   (copysigns (copysigns @0 @1) @2)
1191   (copysigns @0 @2)))
1193 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1194 (for copysigns (COPYSIGN_ALL)
1195  (simplify
1196   (mult (copysigns@2 @0 @1) @2)
1197   (mult @0 @0)))
1199 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1200 (for ccoss (CCOS CCOSH)
1201  (simplify
1202   (ccoss (negate @0))
1203    (ccoss @0)))
1205 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1206 (for ops (conj negate)
1207  (for cabss (CABS)
1208   (simplify
1209    (cabss (ops @0))
1210    (cabss @0))))
1212 /* Fold (a * (1 << b)) into (a << b)  */
1213 (simplify
1214  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1215   (if (! FLOAT_TYPE_P (type)
1216        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1217    (lshift @0 @2)))
1219 /* Shifts by precision or greater result in zero.  */
1220 (for shift (lshift rshift)
1221  (simplify
1222   (shift @0 uniform_integer_cst_p@1)
1223   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1224        /* Leave arithmetic right shifts of possibly negative values alone.  */
1225        && (TYPE_UNSIGNED (type)
1226            || shift == LSHIFT_EXPR
1227            || tree_expr_nonnegative_p (@0))
1228        /* Use a signed compare to leave negative shift counts alone.  */
1229        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1230                      element_precision (type)))
1231    { build_zero_cst (type); })))
1233 /* Shifts by constants distribute over several binary operations,
1234    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1235 (for op (plus minus)
1236   (simplify
1237     (op (lshift:s @0 @1) (lshift:s @2 @1))
1238     (if (INTEGRAL_TYPE_P (type)
1239          && TYPE_OVERFLOW_WRAPS (type)
1240          && !TYPE_SATURATING (type))
1241       (lshift (op @0 @2) @1))))
1243 (for op (bit_and bit_ior bit_xor)
1244   (simplify
1245     (op (lshift:s @0 @1) (lshift:s @2 @1))
1246     (if (INTEGRAL_TYPE_P (type))
1247       (lshift (op @0 @2) @1)))
1248   (simplify
1249     (op (rshift:s @0 @1) (rshift:s @2 @1))
1250     (if (INTEGRAL_TYPE_P (type))
1251       (rshift (op @0 @2) @1))))
1253 /* Fold (1 << (C - x)) where C = precision(type) - 1
1254    into ((1 << C) >> x). */
1255 (simplify
1256  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1257   (if (INTEGRAL_TYPE_P (type)
1258        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1259        && single_use (@1))
1260    (if (TYPE_UNSIGNED (type))
1261      (rshift (lshift @0 @2) @3)
1262    (with
1263     { tree utype = unsigned_type_for (type); }
1264     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1266 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1267 (simplify
1268  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1269  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1270       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1271   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1272    (bit_and (convert @0)
1273             { wide_int_to_tree (type,
1274                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1276 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1277 (for cst (INTEGER_CST VECTOR_CST)
1278  (simplify
1279   (rshift (negate:s @0) cst@1)
1280    (if (!TYPE_UNSIGNED (type)
1281         && TYPE_OVERFLOW_UNDEFINED (type))
1282     (with { tree stype = TREE_TYPE (@1);
1283             tree bt = truth_type_for (type);
1284             tree zeros = build_zero_cst (type);
1285             tree cst = NULL_TREE; }
1286      (switch
1287       /* Handle scalar case.  */
1288       (if (INTEGRAL_TYPE_P (type)
1289            /* If we apply the rule to the scalar type before vectorization
1290               we will enforce the result of the comparison being a bool
1291               which will require an extra AND on the result that will be
1292               indistinguishable from when the user did actually want 0
1293               or 1 as the result so it can't be removed.  */
1294            && canonicalize_math_after_vectorization_p ()
1295            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1296        (negate (convert (gt @0 { zeros; }))))
1297       /* Handle vector case.  */
1298       (if (VECTOR_INTEGER_TYPE_P (type)
1299            /* First check whether the target has the same mode for vector
1300               comparison results as it's operands do.  */
1301            && TYPE_MODE (bt) == TYPE_MODE (type)
1302            /* Then check to see if the target is able to expand the comparison
1303               with the given type later on, otherwise we may ICE.  */
1304            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1305            && (cst = uniform_integer_cst_p (@1)) != NULL
1306            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1307        (view_convert (gt:bt @0 { zeros; }))))))))
1309 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1310 (simplify
1311  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1312   (if (flag_associative_math
1313        && single_use (@3))
1314    (with
1315     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1316     (if (tem)
1317      (rdiv { tem; } @1)))))
1319 /* Simplify ~X & X as zero.  */
1320 (simplify
1321  (bit_and (convert? @0) (convert? @1))
1322  (with { bool wascmp; }
1323   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1324        && bitwise_inverted_equal_p (@0, @1, wascmp))
1325    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1327 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1328 (simplify
1329   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1330   (if (TYPE_UNSIGNED (type))
1331     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1333 (for bitop (bit_and bit_ior)
1334      cmp (eq ne)
1335  /* PR35691: Transform
1336     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1337     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1338  (simplify
1339   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1340    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1341         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1342         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1343     (cmp (bit_ior @0 (convert @1)) @2)))
1344  /* Transform:
1345     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1346     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1347  (simplify
1348   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1349    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1350         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1351         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1352     (cmp (bit_and @0 (convert @1)) @2))))
1354 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1355 (simplify
1356  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1357   (minus (bit_xor @0 @1) @1))
1358 (simplify
1359  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1360  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1361   (minus (bit_xor @0 @1) @1)))
1363 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1364 (simplify
1365  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1366   (minus @1 (bit_xor @0 @1)))
1368 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1369 (for op (bit_ior bit_xor plus)
1370  (simplify
1371   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1372   (with { bool wascmp0, wascmp1; }
1373    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1374         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1375         && ((!wascmp0 && !wascmp1)
1376             || element_precision (type) == 1))
1377    (bit_xor @0 @1)))))
1379 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1380 (simplify
1381   (bit_ior:c (bit_xor:c @0 @1) @0)
1382   (bit_ior @0 @1))
1384 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1385 (simplify
1386  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1387  @2)
1389 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1390 (simplify
1391  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1392  (bit_not (bit_and @0 @1)))
1394 /* (~a & b) ^ a  -->   (a | b)   */
1395 (simplify
1396  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1397  (bit_ior @0 @1))
1399 /* (a | b) & ~(a ^ b)  -->  a & b  */
1400 (simplify
1401  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1402  (bit_and @0 @1))
1404 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1405 (simplify
1406  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1407  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1408       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1409   (bit_and @0 @1)))
1411 /* a | ~(a ^ b)  -->  a | ~b  */
1412 (simplify
1413  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1414  (bit_ior @0 (bit_not @1)))
1416 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1417 (simplify
1418  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1419  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1420       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1421   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1423 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1424 (simplify
1425  (bit_ior:c @0 (bit_xor:cs @1 @2))
1426  (with { bool wascmp; }
1427  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1428       && (!wascmp || element_precision (type) == 1))
1429   (bit_ior @0 (bit_not @2)))))
1431 /* a & ~(a ^ b)  -->  a & b  */
1432 (simplify
1433  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1434  (bit_and @0 @1))
1436 /* a & (a == b)  -->  a & b (boolean version of the above). */
1437 (simplify
1438  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1439  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1440       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1441   (bit_and @0 @1)))
1443 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1444 (simplify
1445  (bit_and:c @0 (bit_xor:c @1 @2))
1446  (with { bool wascmp; }
1447  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1448       && (!wascmp || element_precision (type) == 1))
1449   (bit_and @0 @2))))
1451 /* (a | b) | (a &^ b)  -->  a | b  */
1452 (for op (bit_and bit_xor)
1453  (simplify
1454   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1455   @2))
1457 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1458 (simplify
1459  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1460  @2)
1462 /* (a & b) | (a == b)  -->  a == b  */
1463 (simplify
1464  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1465  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1466       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1467   @2))
1469 /* ~(~a & b)  -->  a | ~b  */
1470 (simplify
1471  (bit_not (bit_and:cs (bit_not @0) @1))
1472  (bit_ior @0 (bit_not @1)))
1474 /* ~(~a | b) --> a & ~b */
1475 (simplify
1476  (bit_not (bit_ior:cs (bit_not @0) @1))
1477  (bit_and @0 (bit_not @1)))
1479 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1480 (simplify
1481  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1482  (bit_and @3 (bit_not @2)))
1484 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1485 (simplify
1486  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1487  (bit_ior @3 @2))
1489 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1490 (simplify
1491  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1492   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1494 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1495 (simplify
1496  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1497   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1499 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1500 (simplify
1501  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1502  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1503       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1504   (bit_xor @0 @1)))
1506 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1507    ((A & N) + B) & M -> (A + B) & M
1508    Similarly if (N & M) == 0,
1509    ((A | N) + B) & M -> (A + B) & M
1510    and for - instead of + (or unary - instead of +)
1511    and/or ^ instead of |.
1512    If B is constant and (B & M) == 0, fold into A & M.  */
1513 (for op (plus minus)
1514  (for bitop (bit_and bit_ior bit_xor)
1515   (simplify
1516    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1517     (with
1518      { tree pmop[2];
1519        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1520                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1521                                        NULL_TREE, pmop); }
1522      (if (utype)
1523       (convert (bit_and (op (convert:utype { pmop[0]; })
1524                             (convert:utype { pmop[1]; }))
1525                         (convert:utype @2))))))
1526   (simplify
1527    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1528     (with
1529      { tree pmop[2];
1530        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1531                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1532                                        @4, pmop); }
1533      (if (utype)
1534       (convert (bit_and (op (convert:utype { pmop[0]; })
1535                             (convert:utype { pmop[1]; }))
1536                         (convert:utype @2)))))))
1537  (simplify
1538   (bit_and (op:s @0 @1) INTEGER_CST@2)
1539    (with
1540     { tree pmop[2];
1541       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1542                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1543                                       NULL_TREE, NULL_TREE, pmop); }
1544     (if (utype)
1545      (convert (bit_and (op (convert:utype { pmop[0]; })
1546                            (convert:utype { pmop[1]; }))
1547                        (convert:utype @2)))))))
1548 (for bitop (bit_and bit_ior bit_xor)
1549  (simplify
1550   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1551    (with
1552     { tree pmop[2];
1553       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1554                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1555                                       NULL_TREE, NULL_TREE, pmop); }
1556     (if (utype)
1557      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1558                        (convert:utype @1)))))))
1560 /* X % Y is smaller than Y.  */
1561 (for cmp (lt ge)
1562  (simplify
1563   (cmp:c (trunc_mod @0 @1) @1)
1564   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1565    { constant_boolean_node (cmp == LT_EXPR, type); })))
1567 /* x | ~0 -> ~0  */
1568 (simplify
1569  (bit_ior @0 integer_all_onesp@1)
1570  @1)
1572 /* x | 0 -> x  */
1573 (simplify
1574  (bit_ior @0 integer_zerop)
1575  @0)
1577 /* x & 0 -> 0  */
1578 (simplify
1579  (bit_and @0 integer_zerop@1)
1580  @1)
1582 /* ~x | x -> -1 */
1583 /* ~x ^ x -> -1 */
1584 (for op (bit_ior bit_xor)
1585  (simplify
1586   (op (convert? @0) (convert? @1))
1587   (with { bool wascmp; }
1588    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1589         && bitwise_inverted_equal_p (@0, @1, wascmp))
1590     (convert
1591      { wascmp
1592         ? constant_boolean_node (true, type)
1593         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1595 /* x ^ x -> 0 */
1596 (simplify
1597   (bit_xor @0 @0)
1598   { build_zero_cst (type); })
1600 /* Canonicalize X ^ ~0 to ~X.  */
1601 (simplify
1602   (bit_xor @0 integer_all_onesp@1)
1603   (bit_not @0))
1605 /* x & ~0 -> x  */
1606 (simplify
1607  (bit_and @0 integer_all_onesp)
1608   (non_lvalue @0))
1610 /* x & x -> x,  x | x -> x  */
1611 (for bitop (bit_and bit_ior)
1612  (simplify
1613   (bitop @0 @0)
1614   (non_lvalue @0)))
1616 /* x & C -> x if we know that x & ~C == 0.  */
1617 #if GIMPLE
1618 (simplify
1619  (bit_and SSA_NAME@0 INTEGER_CST@1)
1620  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1621       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1622   @0))
1624 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1625 (simplify
1626  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1627  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1628       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1629   @0))
1631 /* x | C -> C if we know that x & ~C == 0.  */
1632 (simplify
1633  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1634  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1635       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1636   @1))
1637 #endif
1639 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1640 (simplify
1641  (bit_not (minus (bit_not @0) @1))
1642  (plus @0 @1))
1643 (simplify
1644  (bit_not (plus:c (bit_not @0) @1))
1645  (minus @0 @1))
1646 /* (~X - ~Y) -> Y - X.  */
1647 (simplify
1648  (minus (bit_not @0) (bit_not @1))
1649   (if (!TYPE_OVERFLOW_SANITIZED (type))
1650    (with { tree utype = unsigned_type_for (type); }
1651     (convert (minus (convert:utype @1) (convert:utype @0))))))
1653 /* ~(X - Y) -> ~X + Y.  */
1654 (simplify
1655  (bit_not (minus:s @0 @1))
1656  (plus (bit_not @0) @1))
1657 (simplify
1658  (bit_not (plus:s @0 INTEGER_CST@1))
1659  (if ((INTEGRAL_TYPE_P (type)
1660        && TYPE_UNSIGNED (type))
1661       || (!TYPE_OVERFLOW_SANITIZED (type)
1662           && may_negate_without_overflow_p (@1)))
1663   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1665 #if GIMPLE
1666 /* ~X + Y -> (Y - X) - 1.  */
1667 (simplify
1668  (plus:c (bit_not @0) @1)
1669   (if (ANY_INTEGRAL_TYPE_P (type)
1670        && TYPE_OVERFLOW_WRAPS (type)
1671        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1672        && !integer_all_onesp (@1))
1673    (plus (minus @1 @0) { build_minus_one_cst (type); })
1674    (if (INTEGRAL_TYPE_P (type)
1675         && TREE_CODE (@1) == INTEGER_CST
1676         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1677                                               SIGNED))
1678     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1679 #endif
1681 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1682 (simplify
1683  (bit_not (rshift:s @0 @1))
1684   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1685    (rshift (bit_not! @0) @1)
1686    /* For logical right shifts, this is possible only if @0 doesn't
1687       have MSB set and the logical right shift is changed into
1688       arithmetic shift.  */
1689    (if (INTEGRAL_TYPE_P (type)
1690         && !wi::neg_p (tree_nonzero_bits (@0)))
1691     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1692      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1694 /* x + (x & 1) -> (x + 1) & ~1 */
1695 (simplify
1696  (plus:c @0 (bit_and:s @0 integer_onep@1))
1697  (bit_and (plus @0 @1) (bit_not @1)))
1699 /* x & ~(x & y) -> x & ~y */
1700 /* x | ~(x | y) -> x | ~y  */
1701 (for bitop (bit_and bit_ior)
1702  (simplify
1703   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1704   (bitop @0 (bit_not @1))))
1706 /* (~x & y) | ~(x | y) -> ~x */
1707 (simplify
1708  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1709  @2)
1711 /* (x | y) ^ (x | ~y) -> ~x */
1712 (simplify
1713  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1714  (bit_not @0))
1716 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1717 (simplify
1718  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1719  (bit_not (bit_xor @0 @1)))
1721 /* (~x | y) ^ (x ^ y) -> x | ~y */
1722 (simplify
1723  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1724  (bit_ior @0 (bit_not @1)))
1726 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1727 (simplify
1728  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1729  (bit_not (bit_and @0 @1)))
1731 /* (x & y) ^ (x | y) -> x ^ y */
1732 (simplify
1733  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1734  (bit_xor @0 @1))
1736 /* (x ^ y) ^ (x | y) -> x & y */
1737 (simplify
1738  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1739  (bit_and @0 @1))
1741 /* (x & y) + (x ^ y) -> x | y */
1742 /* (x & y) | (x ^ y) -> x | y */
1743 /* (x & y) ^ (x ^ y) -> x | y */
1744 (for op (plus bit_ior bit_xor)
1745  (simplify
1746   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1747   (bit_ior @0 @1)))
1749 /* (x & y) + (x | y) -> x + y */
1750 (simplify
1751  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1752  (plus @0 @1))
1754 /* (x + y) - (x | y) -> x & y */
1755 (simplify
1756  (minus (plus @0 @1) (bit_ior @0 @1))
1757  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1758       && !TYPE_SATURATING (type))
1759   (bit_and @0 @1)))
1761 /* (x + y) - (x & y) -> x | y */
1762 (simplify
1763  (minus (plus @0 @1) (bit_and @0 @1))
1764  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1765       && !TYPE_SATURATING (type))
1766   (bit_ior @0 @1)))
1768 /* (x | y) - y -> (x & ~y) */
1769 (simplify
1770  (minus (bit_ior:cs @0 @1) @1)
1771  (bit_and @0 (bit_not @1)))
1773 /* (x | y) - (x ^ y) -> x & y */
1774 (simplify
1775  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1776  (bit_and @0 @1))
1778 /* (x | y) - (x & y) -> x ^ y */
1779 (simplify
1780  (minus (bit_ior @0 @1) (bit_and @0 @1))
1781  (bit_xor @0 @1))
1783 /* (x | y) & ~(x & y) -> x ^ y */
1784 (simplify
1785  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1786  (bit_xor @0 @1))
1788 /* (x | y) & (~x ^ y) -> x & y */
1789 (simplify
1790  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1791  (with { bool wascmp; }
1792   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1793        && (!wascmp || element_precision (type) == 1))
1794    (bit_and @0 @1))))
1796 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1797 (simplify
1798  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1799  (bit_not (bit_xor @0 @1)))
1801 /* (~x | y) ^ (x | ~y) -> x ^ y */
1802 (simplify
1803  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1804  (bit_xor @0 @1))
1806 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1807 (simplify
1808  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1809                               (nop_convert2? (bit_ior @0 @1))))
1810        integer_all_onesp)
1811  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1812       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1813       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1814       && !TYPE_SATURATING (TREE_TYPE (@2)))
1815  (bit_not (convert (bit_xor @0 @1)))))
1816 (simplify
1817  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1818                                integer_all_onesp))
1819        (nop_convert3? (bit_ior @0 @1)))
1820  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1821       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1822       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1823       && !TYPE_SATURATING (TREE_TYPE (@2)))
1824  (bit_not (convert (bit_xor @0 @1)))))
1825 (simplify
1826  (minus (nop_convert1? (bit_and @0 @1))
1827        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1828                                integer_onep)))
1829  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1830       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1831       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1832       && !TYPE_SATURATING (TREE_TYPE (@2)))
1833  (bit_not (convert (bit_xor @0 @1)))))
1835 /* ~x & ~y -> ~(x | y)
1836    ~x | ~y -> ~(x & y) */
1837 (for op (bit_and bit_ior)
1838      rop (bit_ior bit_and)
1839  (simplify
1840   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1841   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1842        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1843    (bit_not (rop (convert @0) (convert @1))))))
1845 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1846    with a constant, and the two constants have no bits in common,
1847    we should treat this as a BIT_IOR_EXPR since this may produce more
1848    simplifications.  */
1849 (for op (bit_xor plus)
1850  (simplify
1851   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1852       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1853   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1854        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1855        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1856    (bit_ior (convert @4) (convert @5)))))
1858 /* (X | Y) ^ X -> Y & ~ X*/
1859 (simplify
1860  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1861  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1862   (convert (bit_and @1 (bit_not @0)))))
1864 /* (~X | Y) ^ X -> ~(X & Y).  */
1865 (simplify
1866  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1867  (if (bitwise_equal_p (@0, @2))
1868   (convert (bit_not (bit_and @0 (convert @1))))))
1870 /* Convert ~X ^ ~Y to X ^ Y.  */
1871 (simplify
1872  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1873  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1874       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1875   (bit_xor (convert @0) (convert @1))))
1877 /* Convert ~X ^ C to X ^ ~C.  */
1878 (simplify
1879  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1880  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1881   (bit_xor (convert @0) (bit_not @1))))
1883 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1884 (for opo (bit_and bit_xor)
1885      opi (bit_xor bit_and)
1886  (simplify
1887   (opo:c (opi:cs @0 @1) @1)
1888   (bit_and (bit_not @0) @1)))
1890 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1891    operands are another bit-wise operation with a common input.  If so,
1892    distribute the bit operations to save an operation and possibly two if
1893    constants are involved.  For example, convert
1894      (A | B) & (A | C) into A | (B & C)
1895    Further simplification will occur if B and C are constants.  */
1896 (for op (bit_and bit_ior bit_xor)
1897      rop (bit_ior bit_and bit_and)
1898  (simplify
1899   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1900   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1901        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1902    (rop (convert @0) (op (convert @1) (convert @2))))))
1904 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1905 /* (X & Y) & Y -> X & Y
1906    (X | Y) | Y -> X | Y  */
1907 (for op (bit_and bit_ior)
1908  (simplify
1909   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1910   @2))
1911 /* (X ^ Y) ^ Y -> X  */
1912 (simplify
1913  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1914  (convert @0))
1916 /* (X & ~Y) & Y -> 0 */
1917 (simplify
1918  (bit_and:c (bit_and @0 @1) @2)
1919  (with { bool wascmp; }
1920   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1921        || bitwise_inverted_equal_p (@1, @2, wascmp))
1922    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1923 /* (X | ~Y) | Y -> -1 */
1924 (simplify
1925  (bit_ior:c (bit_ior @0 @1) @2)
1926  (with { bool wascmp; }
1927   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1928         || bitwise_inverted_equal_p (@1, @2, wascmp))
1929        && (!wascmp || element_precision (type) == 1))
1930    { build_all_ones_cst (TREE_TYPE (@0)); })))
1932 /* (X & Y) & (X & Z) -> (X & Y) & Z
1933    (X | Y) | (X | Z) -> (X | Y) | Z  */
1934 (for op (bit_and bit_ior)
1935  (simplify
1936   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1937   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1938        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1939    (if (single_use (@5) && single_use (@6))
1940     (op @3 (convert @2))
1941     (if (single_use (@3) && single_use (@4))
1942      (op (convert @1) @5))))))
1943 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1944 (simplify
1945  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1946  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1947       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1948   (bit_xor (convert @1) (convert @2))))
1950 /* Convert abs (abs (X)) into abs (X).
1951    also absu (absu (X)) into absu (X).  */
1952 (simplify
1953  (abs (abs@1 @0))
1954  @1)
1956 (simplify
1957  (absu (convert@2 (absu@1 @0)))
1958  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1959   @1))
1961 /* Convert abs[u] (-X) -> abs[u] (X).  */
1962 (simplify
1963  (abs (negate @0))
1964  (abs @0))
1966 (simplify
1967  (absu (negate @0))
1968  (absu @0))
1970 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1971 (simplify
1972  (abs tree_expr_nonnegative_p@0)
1973  @0)
1975 (simplify
1976  (absu tree_expr_nonnegative_p@0)
1977  (convert @0))
1979 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1980 (simplify
1981  (mult:c (nop_convert1?
1982           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1983                     integer_onep))
1984          (nop_convert3? @0))
1985  (if (INTEGRAL_TYPE_P (type)
1986       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1987       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1988   (if (TYPE_UNSIGNED (type))
1989    (absu @0)
1990    (abs @0)
1991   )
1995 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1996 (match negate_expr_p
1997  INTEGER_CST
1998  (if ((INTEGRAL_TYPE_P (type)
1999        && TYPE_UNSIGNED (type))
2000       || (!TYPE_OVERFLOW_SANITIZED (type)
2001           && may_negate_without_overflow_p (t)))))
2002 (match negate_expr_p
2003  FIXED_CST)
2004 (match negate_expr_p
2005  (negate @0)
2006  (if (!TYPE_OVERFLOW_SANITIZED (type))))
2007 (match negate_expr_p
2008  REAL_CST
2009  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
2010 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
2011    ways.  */
2012 (match negate_expr_p
2013  VECTOR_CST
2014  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2015 (match negate_expr_p
2016  (minus @0 @1)
2017  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2018       || (FLOAT_TYPE_P (type)
2019           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2020           && !HONOR_SIGNED_ZEROS (type)))))
2022 /* (-A) * (-B) -> A * B  */
2023 (simplify
2024  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2025   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2026        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2027    (mult (convert @0) (convert (negate @1)))))
2029 /* -(A + B) -> (-B) - A.  */
2030 (simplify
2031  (negate (plus:c @0 negate_expr_p@1))
2032  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2033       && !HONOR_SIGNED_ZEROS (type))
2034   (minus (negate @1) @0)))
2036 /* -(A - B) -> B - A.  */
2037 (simplify
2038  (negate (minus @0 @1))
2039  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2040       || (FLOAT_TYPE_P (type)
2041           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2042           && !HONOR_SIGNED_ZEROS (type)))
2043   (minus @1 @0)))
2044 (simplify
2045  (negate (pointer_diff @0 @1))
2046  (if (TYPE_OVERFLOW_UNDEFINED (type))
2047   (pointer_diff @1 @0)))
2049 /* A - B -> A + (-B) if B is easily negatable.  */
2050 (simplify
2051  (minus @0 negate_expr_p@1)
2052  (if (!FIXED_POINT_TYPE_P (type))
2053  (plus @0 (negate @1))))
2055 /* 1 - a is a ^ 1 if a had a bool range. */
2056 /* This is only enabled for gimple as sometimes
2057    cfun is not set for the function which contains
2058    the SSA_NAME (e.g. while IPA passes are happening,
2059    fold might be called).  */
2060 (simplify
2061  (minus integer_onep@0 SSA_NAME@1)
2062   (if (INTEGRAL_TYPE_P (type)
2063        && ssa_name_has_boolean_range (@1))
2064    (bit_xor @1 @0)))
2066 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
2067 (simplify
2068  (negate (mult:c@0 @1 negate_expr_p@2))
2069  (if (! TYPE_UNSIGNED (type)
2070       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2071       && single_use (@0))
2072   (mult @1 (negate @2))))
2074 (simplify
2075  (negate (rdiv@0 @1 negate_expr_p@2))
2076  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2077       && single_use (@0))
2078   (rdiv @1 (negate @2))))
2080 (simplify
2081  (negate (rdiv@0 negate_expr_p@1 @2))
2082  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2083       && single_use (@0))
2084   (rdiv (negate @1) @2)))
2086 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2087 (simplify
2088  (negate (convert? (rshift @0 INTEGER_CST@1)))
2089  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2090       && wi::to_wide (@1) == element_precision (type) - 1)
2091   (with { tree stype = TREE_TYPE (@0);
2092           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2093                                              : unsigned_type_for (stype); }
2094    (if (VECTOR_TYPE_P (type))
2095     (view_convert (rshift (view_convert:ntype @0) @1))
2096     (convert (rshift (convert:ntype @0) @1))))))
2098 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2099    when profitable.
2100    For bitwise binary operations apply operand conversions to the
2101    binary operation result instead of to the operands.  This allows
2102    to combine successive conversions and bitwise binary operations.
2103    We combine the above two cases by using a conditional convert.  */
2104 (for bitop (bit_and bit_ior bit_xor)
2105  (simplify
2106   (bitop (convert@2 @0) (convert?@3 @1))
2107   (if (((TREE_CODE (@1) == INTEGER_CST
2108          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2109          && (int_fits_type_p (@1, TREE_TYPE (@0))
2110              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2111         || types_match (@0, @1))
2112        && !POINTER_TYPE_P (TREE_TYPE (@0))
2113        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2114        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2115        /* ???  This transform conflicts with fold-const.cc doing
2116           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2117           constants (if x has signed type, the sign bit cannot be set
2118           in c).  This folds extension into the BIT_AND_EXPR.
2119           Restrict it to GIMPLE to avoid endless recursions.  */
2120        && (bitop != BIT_AND_EXPR || GIMPLE)
2121        && (/* That's a good idea if the conversion widens the operand, thus
2122               after hoisting the conversion the operation will be narrower.
2123               It is also a good if the conversion is a nop as moves the
2124               conversion to one side; allowing for combining of the conversions.  */
2125            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2126            /* The conversion check for being a nop can only be done at the gimple
2127               level as fold_binary has some re-association code which can conflict
2128               with this if there is a "constant" which is not a full INTEGER_CST.  */
2129            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2130            /* It's also a good idea if the conversion is to a non-integer
2131               mode.  */
2132            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2133            /* Or if the precision of TO is not the same as the precision
2134               of its mode.  */
2135            || !type_has_mode_precision_p (type)
2136            /* In GIMPLE, getting rid of 2 conversions for one new results
2137               in smaller IL.  */
2138            || (GIMPLE
2139                && TREE_CODE (@1) != INTEGER_CST
2140                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2141                && single_use (@2)
2142                && single_use (@3))))
2143    (convert (bitop @0 (convert @1)))))
2144  /* In GIMPLE, getting rid of 2 conversions for one new results
2145     in smaller IL.  */
2146  (simplify
2147   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2148   (if (GIMPLE
2149        && TREE_CODE (@1) != INTEGER_CST
2150        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2151        && types_match (type, @0)
2152        && !POINTER_TYPE_P (TREE_TYPE (@0))
2153        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2154    (bitop @0 (convert @1)))))
2156 (for bitop (bit_and bit_ior)
2157      rbitop (bit_ior bit_and)
2158   /* (x | y) & x -> x */
2159   /* (x & y) | x -> x */
2160  (simplify
2161   (bitop:c (rbitop:c @0 @1) @0)
2162   @0)
2163  /* (~x | y) & x -> x & y */
2164  /* (~x & y) | x -> x | y */
2165  (simplify
2166   (bitop:c (rbitop:c @2 @1) @0)
2167   (with { bool wascmp; }
2168    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2169         && (!wascmp || element_precision (type) == 1))
2170     (bitop @0 @1))))
2171   /* (x | y) & (x & z) -> (x & z) */
2172   /* (x & y) | (x | z) -> (x | z) */
2173  (simplify
2174   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2175   @3)
2176  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2177  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2178  (simplify
2179   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2180   (bitop @0 @3))
2181  /* x & ~(y | x) -> 0 */
2182  /* x | ~(y & x) -> -1 */
2183  (simplify
2184   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2185   (if (bitop == BIT_AND_EXPR)
2186    { build_zero_cst (type); }
2187    { build_minus_one_cst (type); })))
2189 /* ((x | y) & z) | x -> (z & y) | x
2190    ((x ^ y) & z) | x -> (z & y) | x  */
2191 (for op (bit_ior bit_xor)
2192  (simplify
2193   (bit_ior:c (nop_convert1?:s
2194                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2195   (if (bitwise_equal_p (@0, @3))
2196    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2198 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2199 (simplify
2200   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2201   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2203 /* Combine successive equal operations with constants.  */
2204 (for bitop (bit_and bit_ior bit_xor)
2205  (simplify
2206   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2207   (if (!CONSTANT_CLASS_P (@0))
2208    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2209       folded to a constant.  */
2210    (bitop @0 (bitop! @1 @2))
2211    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2212       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2213       the values involved are such that the operation can't be decided at
2214       compile time.  Try folding one of @0 or @1 with @2 to see whether
2215       that combination can be decided at compile time.
2217       Keep the existing form if both folds fail, to avoid endless
2218       oscillation.  */
2219    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2220     (if (cst1)
2221      (bitop @1 { cst1; })
2222      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2223       (if (cst2)
2224        (bitop @0 { cst2; }))))))))
2226 /* Try simple folding for X op !X, and X op X with the help
2227    of the truth_valued_p and logical_inverted_value predicates.  */
2228 (match truth_valued_p
2229  @0
2230  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2231 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2232  (match truth_valued_p
2233   (op @0 @1)))
2234 (match truth_valued_p
2235   (truth_not @0))
2237 (match (logical_inverted_value @0)
2238  (truth_not @0))
2239 (match (logical_inverted_value @0)
2240  (bit_not truth_valued_p@0))
2241 (match (logical_inverted_value @0)
2242  (eq @0 integer_zerop))
2243 (match (logical_inverted_value @0)
2244  (ne truth_valued_p@0 integer_truep))
2245 (match (logical_inverted_value @0)
2246  (bit_xor truth_valued_p@0 integer_truep))
2248 /* X & !X -> 0.  */
2249 (simplify
2250  (bit_and:c @0 (logical_inverted_value @0))
2251  { build_zero_cst (type); })
2252 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2253 (for op (bit_ior bit_xor)
2254  (simplify
2255   (op:c truth_valued_p@0 (logical_inverted_value @0))
2256   { constant_boolean_node (true, type); }))
2257 /* X ==/!= !X is false/true.  */
2258 (for op (eq ne)
2259  (simplify
2260   (op:c truth_valued_p@0 (logical_inverted_value @0))
2261   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2263 /* ~~x -> x */
2264 (simplify
2265   (bit_not (bit_not @0))
2266   @0)
2268 /* zero_one_valued_p will match when a value is known to be either
2269    0 or 1 including constants 0 or 1.
2270    Signed 1-bits includes -1 so they cannot match here. */
2271 (match zero_one_valued_p
2272  @0
2273  (if (INTEGRAL_TYPE_P (type)
2274       && (TYPE_UNSIGNED (type)
2275           || TYPE_PRECISION (type) > 1)
2276       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2277 (match zero_one_valued_p
2278  truth_valued_p@0
2279  (if (INTEGRAL_TYPE_P (type)
2280       && (TYPE_UNSIGNED (type)
2281           || TYPE_PRECISION (type) > 1))))
2283 /* (a&1) is always [0,1] too. This is useful again when
2284    the range is not known. */
2285 /* Note this can't be recursive due to VN handling of equivalents,
2286    VN and would cause an infinite recursion. */
2287 (match zero_one_valued_p
2288  (bit_and:c@0 @1 integer_onep)
2289  (if (INTEGRAL_TYPE_P (type))))
2291 /* A conversion from an zero_one_valued_p is still a [0,1].
2292    This is useful when the range of a variable is not known */
2293 /* Note this matches can't be recursive because of the way VN handles
2294    nop conversions being equivalent and then recursive between them. */
2295 (match zero_one_valued_p
2296  (convert@0 @1)
2297  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2298       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2299           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2300       && INTEGRAL_TYPE_P (type)
2301       && (TYPE_UNSIGNED (type)
2302           || TYPE_PRECISION (type) > 1)
2303       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2305 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2306 (simplify
2307  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2308  (if (INTEGRAL_TYPE_P (type))
2309   (bit_and @0 @1)))
2311 (for cmp (tcc_comparison)
2312      icmp (inverted_tcc_comparison)
2313  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2314  (simplify
2315   (bit_ior
2316    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2317    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2318     (if (INTEGRAL_TYPE_P (type)
2319          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2320          /* The scalar version has to be canonicalized after vectorization
2321             because it makes unconditional loads conditional ones, which
2322             means we lose vectorization because the loads may trap.  */
2323          && canonicalize_math_after_vectorization_p ())
2324      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2326  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2327     canonicalized further and we recognize the conditional form:
2328     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2329  (simplify
2330   (bit_ior
2331    (cond (cmp@0  @01 @02) @3 zerop)
2332    (cond (icmp@4 @01 @02) @5 zerop))
2333     (if (INTEGRAL_TYPE_P (type)
2334          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2335          /* The scalar version has to be canonicalized after vectorization
2336             because it makes unconditional loads conditional ones, which
2337             means we lose vectorization because the loads may trap.  */
2338          && canonicalize_math_after_vectorization_p ())
2339     (cond @0 @3 @5)))
2341  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2342     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2343  (simplify
2344   (bit_ior
2345    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2346    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2347     (if (integer_zerop (@5)
2348          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2349      (switch
2350       (if (integer_onep (@4))
2351        (bit_and (vec_cond @0 @2 @3) @4))
2352         (if (integer_minus_onep (@4))
2353          (vec_cond @0 @2 @3)))
2354     (if (integer_zerop (@4)
2355          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2356      (switch
2357       (if (integer_onep (@5))
2358        (bit_and (vec_cond @0 @3 @2) @5))
2359       (if (integer_minus_onep (@5))
2360        (vec_cond @0 @3 @2))))))
2362  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2363     into a < b ? d : c.  */
2364  (simplify
2365   (bit_ior
2366    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2367    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2368   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2369    (vec_cond @0 @2 @3))))
2371 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2372 (simplify
2373  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2374  (if (INTEGRAL_TYPE_P (type)
2375       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2376       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2377       /* Sign extending of the neg or a truncation of the neg
2378          is needed. */
2379       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2380           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2381   (mult (convert @0) @1)))
2383 /* Narrow integer multiplication by a zero_one_valued_p operand.
2384    Multiplication by [0,1] is guaranteed not to overflow.  */
2385 (simplify
2386  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2387  (if (INTEGRAL_TYPE_P (type)
2388       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2389       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2390   (mult (convert @1) (convert @2))))
2392 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2393    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2394    as some targets (such as x86's SSE) may return zero for larger C.  */
2395 (simplify
2396   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2397   (if (tree_fits_shwi_p (@1)
2398        && tree_to_shwi (@1) > 0
2399        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2400     (convert @0)))
2402 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2403    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2404    as some targets (such as x86's SSE) may return zero for larger C.  */
2405 (simplify
2406   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2407   (if (tree_fits_shwi_p (@1)
2408        && tree_to_shwi (@1) > 0
2409        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2410     (eq @0 @2)))
2412 /* Convert ~ (-A) to A - 1.  */
2413 (simplify
2414  (bit_not (convert? (negate @0)))
2415  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2416       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2417   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2419 /* Convert - (~A) to A + 1.  */
2420 (simplify
2421  (negate (nop_convert? (bit_not @0)))
2422  (plus (view_convert @0) { build_each_one_cst (type); }))
2424 /* (a & b) ^ (a == b) -> !(a | b) */
2425 /* (a & b) == (a ^ b) -> !(a | b) */
2426 (for first_op (bit_xor eq)
2427      second_op (eq bit_xor)
2428  (simplify
2429   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2430     (bit_not (bit_ior @0 @1))))
2432 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2433 (simplify
2434  (bit_not (convert? (minus @0 integer_each_onep)))
2435  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2436       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2437   (convert (negate @0))))
2438 (simplify
2439  (bit_not (convert? (plus @0 integer_all_onesp)))
2440  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2441       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2442   (convert (negate @0))))
2444 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2445 (simplify
2446  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2447  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2448   (convert (bit_xor @0 (bit_not @1)))))
2449 (simplify
2450  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2451  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2452   (convert (bit_xor @0 @1))))
2454 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2455 (simplify
2456  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2457  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2458   (bit_not (bit_xor (view_convert @0) @1))))
2460 /* ~(a ^ b) is a == b for truth valued a and b.  */
2461 (simplify
2462  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2463  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2464       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2465   (convert (eq @0 @1))))
2467 /* (~a) == b is a ^ b for truth valued a and b.  */
2468 (simplify
2469  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2470  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2471       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2472   (convert (bit_xor @0 @1))))
2474 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2475 (simplify
2476  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2477  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2479 /* Fold A - (A & B) into ~B & A.  */
2480 (simplify
2481  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2482  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2483       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2484   (convert (bit_and (bit_not @1) @0))))
2486 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2487 (if (!canonicalize_math_p ())
2488  (for cmp (tcc_comparison)
2489   (simplify
2490    (mult:c (convert (cmp@0 @1 @2)) @3)
2491    (if (INTEGRAL_TYPE_P (type)
2492         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2493      (cond @0 @3 { build_zero_cst (type); })))
2494 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2495   (simplify
2496    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2497    (if (INTEGRAL_TYPE_P (type)
2498         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2499      (cond @0 @3 { build_zero_cst (type); })))
2503 /* For integral types with undefined overflow and C != 0 fold
2504    x * C EQ/NE y * C into x EQ/NE y.  */
2505 (for cmp (eq ne)
2506  (simplify
2507   (cmp (mult:c @0 @1) (mult:c @2 @1))
2508   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2509        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2510        && tree_expr_nonzero_p (@1))
2511    (cmp @0 @2))))
2513 /* For integral types with wrapping overflow and C odd fold
2514    x * C EQ/NE y * C into x EQ/NE y.  */
2515 (for cmp (eq ne)
2516  (simplify
2517   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2518   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2519        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2520        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2521    (cmp @0 @2))))
2523 /* For integral types with undefined overflow and C != 0 fold
2524    x * C RELOP y * C into:
2526    x RELOP y for nonnegative C
2527    y RELOP x for negative C  */
2528 (for cmp (lt gt le ge)
2529  (simplify
2530   (cmp (mult:c @0 @1) (mult:c @2 @1))
2531   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2532        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2533    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2534     (cmp @0 @2)
2535    (if (TREE_CODE (@1) == INTEGER_CST
2536         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2537     (cmp @2 @0))))))
2539 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2540 (for cmp (le gt)
2541      icmp (gt le)
2542  (simplify
2543   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2544    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2545         && TYPE_UNSIGNED (TREE_TYPE (@0))
2546         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2547         && (wi::to_wide (@2)
2548             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2549     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2550      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2552 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2553 (for cmp (simple_comparison)
2554  (simplify
2555   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2556   (if (element_precision (@3) >= element_precision (@0)
2557        && types_match (@0, @1))
2558    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2559     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2560      (cmp @1 @0)
2561      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2562       (with
2563        {
2564         tree utype = unsigned_type_for (TREE_TYPE (@0));
2565        }
2566        (cmp (convert:utype @1) (convert:utype @0)))))
2567     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2568      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2569       (cmp @0 @1)
2570       (with
2571        {
2572         tree utype = unsigned_type_for (TREE_TYPE (@0));
2573        }
2574        (cmp (convert:utype @0) (convert:utype @1)))))))))
2576 /* X / C1 op C2 into a simple range test.  */
2577 (for cmp (simple_comparison)
2578  (simplify
2579   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2580   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2581        && integer_nonzerop (@1)
2582        && !TREE_OVERFLOW (@1)
2583        && !TREE_OVERFLOW (@2))
2584    (with { tree lo, hi; bool neg_overflow;
2585            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2586                                                    &neg_overflow); }
2587     (switch
2588      (if (code == LT_EXPR || code == GE_EXPR)
2589        (if (TREE_OVERFLOW (lo))
2590         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2591         (if (code == LT_EXPR)
2592          (lt @0 { lo; })
2593          (ge @0 { lo; }))))
2594      (if (code == LE_EXPR || code == GT_EXPR)
2595        (if (TREE_OVERFLOW (hi))
2596         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2597         (if (code == LE_EXPR)
2598          (le @0 { hi; })
2599          (gt @0 { hi; }))))
2600      (if (!lo && !hi)
2601       { build_int_cst (type, code == NE_EXPR); })
2602      (if (code == EQ_EXPR && !hi)
2603       (ge @0 { lo; }))
2604      (if (code == EQ_EXPR && !lo)
2605       (le @0 { hi; }))
2606      (if (code == NE_EXPR && !hi)
2607       (lt @0 { lo; }))
2608      (if (code == NE_EXPR && !lo)
2609       (gt @0 { hi; }))
2610      (if (GENERIC)
2611       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2612                            lo, hi); })
2613      (with
2614       {
2615         tree etype = range_check_type (TREE_TYPE (@0));
2616         if (etype)
2617           {
2618             hi = fold_convert (etype, hi);
2619             lo = fold_convert (etype, lo);
2620             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2621           }
2622       }
2623       (if (etype && hi && !TREE_OVERFLOW (hi))
2624        (if (code == EQ_EXPR)
2625         (le (minus (convert:etype @0) { lo; }) { hi; })
2626         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2628 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2629 (for op (lt le ge gt)
2630  (simplify
2631   (op (plus:c @0 @2) (plus:c @1 @2))
2632   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2633        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2634    (op @0 @1))))
2636 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2637    when C is an unsigned integer constant with only the MSB set, and X and
2638    Y have types of equal or lower integer conversion rank than C's.  */
2639 (for op (lt le ge gt)
2640  (simplify
2641   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2642   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2643        && TYPE_UNSIGNED (TREE_TYPE (@0))
2644        && wi::only_sign_bit_p (wi::to_wide (@0)))
2645    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2646     (op (convert:stype @1) (convert:stype @2))))))
2648 /* For equality and subtraction, this is also true with wrapping overflow.  */
2649 (for op (eq ne minus)
2650  (simplify
2651   (op (plus:c @0 @2) (plus:c @1 @2))
2652   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2653        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2654            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2655    (op @0 @1))))
2656 /* And similar for pointers.  */
2657 (for op (eq ne)
2658  (simplify
2659   (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2660   (op @1 @2)))
2661 (simplify
2662  (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2663  (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2664   (convert (minus @1 @2))))
2666 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2667 (for op (lt le ge gt)
2668  (simplify
2669   (op (minus @0 @2) (minus @1 @2))
2670   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2671        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2672    (op @0 @1))))
2673 /* For equality and subtraction, this is also true with wrapping overflow.  */
2674 (for op (eq ne minus)
2675  (simplify
2676   (op (minus @0 @2) (minus @1 @2))
2677   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2678        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2679            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2680    (op @0 @1))))
2681 /* And for pointers...  */
2682 (for op (simple_comparison)
2683  (simplify
2684   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2685   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2686    (op @0 @1))))
2687 (simplify
2688  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2689  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2690       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2691   (pointer_diff @0 @1)))
2693 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2694 (for op (lt le ge gt)
2695  (simplify
2696   (op (minus @2 @0) (minus @2 @1))
2697   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2698        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2699    (op @1 @0))))
2700 /* For equality and subtraction, this is also true with wrapping overflow.  */
2701 (for op (eq ne minus)
2702  (simplify
2703   (op (minus @2 @0) (minus @2 @1))
2704   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2705        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2706            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2707    (op @1 @0))))
2708 /* And for pointers...  */
2709 (for op (simple_comparison)
2710  (simplify
2711   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2712   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2713    (op @1 @0))))
2714 (simplify
2715  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2716  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2717       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2718   (pointer_diff @1 @0)))
2720 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2721 (for op (lt le gt ge)
2722  (simplify
2723   (op:c (plus:c@2 @0 @1) @1)
2724   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2725        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2726        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2727        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2728    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2729 /* For equality, this is also true with wrapping overflow.  */
2730 (for op (eq ne)
2731  (simplify
2732   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2733   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2734        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2735            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2736        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2737        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2738        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2739    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2740  (simplify
2741   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2742   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2743        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2744        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2745    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2747 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2748 (for neeq (ne eq)
2749  (simplify
2750   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2751    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2752     (if (ptr_difference_const (@0, @2, &diff))
2753      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2754  (simplify
2755   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2756    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2757     (if (ptr_difference_const (@0, @2, &diff))
2758      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2760 /* X - Y < X is the same as Y > 0 when there is no overflow.
2761    For equality, this is also true with wrapping overflow.  */
2762 (for op (simple_comparison)
2763  (simplify
2764   (op:c @0 (minus@2 @0 @1))
2765   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2766        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2767            || ((op == EQ_EXPR || op == NE_EXPR)
2768                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2769        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2770    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2772 /* Transform:
2773    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2774    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2775 (for cmp (eq ne)
2776      ocmp (lt ge)
2777  (simplify
2778   (cmp (trunc_div @0 @1) integer_zerop)
2779   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2780        /* Complex ==/!= is allowed, but not </>=.  */
2781        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2782        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2783    (ocmp @0 @1))))
2785 /* X == C - X can never be true if C is odd.  */
2786 (for cmp (eq ne)
2787  (simplify
2788   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2789   (if (TREE_INT_CST_LOW (@1) & 1)
2790    { constant_boolean_node (cmp == NE_EXPR, type); })))
2793    U & N <= U  -> true
2794    U & N >  U  -> false
2795    U needs to be non-negative.
2797    U | N <  U  -> false
2798    U | N >= U  -> true
2799    U and N needs to be non-negative
2801    U | N <  U  -> true
2802    U | N >= U  -> false
2803    U needs to be non-negative and N needs to be a negative constant.
2804    */
2805 (for cmp   (lt      ge      le      gt     )
2806      bitop (bit_ior bit_ior bit_and bit_and)
2807  (simplify
2808   (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2809   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2810    (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2811     { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2812     /* The sign is opposite now so the comparison is swapped around. */
2813     (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2814      { constant_boolean_node (cmp == LT_EXPR, type); })))))
2816 /* Arguments on which one can call get_nonzero_bits to get the bits
2817    possibly set.  */
2818 (match with_possible_nonzero_bits
2819  INTEGER_CST@0)
2820 (match with_possible_nonzero_bits
2821  SSA_NAME@0
2822  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2823 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2824 (match (with_possible_nonzero_bits2 @0)
2825  with_possible_nonzero_bits@0)
2826 (match (with_possible_nonzero_bits2 @0)
2827  (bit_and:c with_possible_nonzero_bits@0 @2))
2829 /* Same for bits that are known to be set, but we do not have
2830    an equivalent to get_nonzero_bits yet.  */
2831 (match (with_certain_nonzero_bits2 @0)
2832  INTEGER_CST@0)
2833 (match (with_certain_nonzero_bits2 @0)
2834  (bit_ior @1 INTEGER_CST@0))
2836 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2837 (for cmp (eq ne)
2838  (simplify
2839   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2840   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2841    { constant_boolean_node (cmp == NE_EXPR, type); })))
2843 /* ((X inner_op C0) outer_op C1)
2844    With X being a tree where value_range has reasoned certain bits to always be
2845    zero throughout its computed value range,
2846    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2847    where zero_mask has 1's for all bits that are sure to be 0 in
2848    and 0's otherwise.
2849    if (inner_op == '^') C0 &= ~C1;
2850    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2851    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2853 (for inner_op (bit_ior bit_xor)
2854      outer_op (bit_xor bit_ior)
2855 (simplify
2856  (outer_op
2857   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2858  (with
2859   {
2860     bool fail = false;
2861     wide_int zero_mask_not;
2862     wide_int C0;
2863     wide_int cst_emit;
2865     if (TREE_CODE (@2) == SSA_NAME)
2866       zero_mask_not = get_nonzero_bits (@2);
2867     else
2868       fail = true;
2870     if (inner_op == BIT_XOR_EXPR)
2871       {
2872         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2873         cst_emit = C0 | wi::to_wide (@1);
2874       }
2875     else
2876       {
2877         C0 = wi::to_wide (@0);
2878         cst_emit = C0 ^ wi::to_wide (@1);
2879       }
2880   }
2881   (if (!fail && (C0 & zero_mask_not) == 0)
2882    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2883    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2884     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2886 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2887 (simplify
2888   (pointer_plus (pointer_plus:s @0 @1) @3)
2889   (pointer_plus @0 (plus @1 @3)))
2890 #if GENERIC
2891 (simplify
2892   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2893   (convert:type (pointer_plus @0 (plus @1 @3))))
2894 #endif
2896 /* Pattern match
2897      tem1 = (long) ptr1;
2898      tem2 = (long) ptr2;
2899      tem3 = tem2 - tem1;
2900      tem4 = (unsigned long) tem3;
2901      tem5 = ptr1 + tem4;
2902    and produce
2903      tem5 = ptr2;  */
2904 (simplify
2905   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2906   /* Conditionally look through a sign-changing conversion.  */
2907   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2908        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2909             || (GENERIC && type == TREE_TYPE (@1))))
2910    @1))
2911 (simplify
2912   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2913   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2914    (convert @1)))
2916 /* Pattern match
2917      tem = (sizetype) ptr;
2918      tem = tem & algn;
2919      tem = -tem;
2920      ... = ptr p+ tem;
2921    and produce the simpler and easier to analyze with respect to alignment
2922      ... = ptr & ~algn;  */
2923 (simplify
2924   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2925   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2926    (bit_and @0 { algn; })))
2928 /* Try folding difference of addresses.  */
2929 (simplify
2930  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2931  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2932   (with { poly_int64 diff; }
2933    (if (ptr_difference_const (@0, @1, &diff))
2934     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2935 (simplify
2936  (minus (convert (pointer_plus @0 @2)) (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     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2941 (simplify
2942  (minus (convert ADDR_EXPR@0) (convert @1))
2943  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2944   (with { poly_int64 diff; }
2945    (if (ptr_difference_const (@0, @1, &diff))
2946     { build_int_cst_type (type, diff); }))))
2947 (simplify
2948  (minus (convert @0) (convert ADDR_EXPR@1))
2949  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2950   (with { poly_int64 diff; }
2951    (if (ptr_difference_const (@0, @1, &diff))
2952     { build_int_cst_type (type, diff); }))))
2953 (simplify
2954  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2955  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2956       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2957   (with { poly_int64 diff; }
2958    (if (ptr_difference_const (@0, @1, &diff))
2959     { build_int_cst_type (type, diff); }))))
2960 (simplify
2961  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2962  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2963       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2964   (with { poly_int64 diff; }
2965    (if (ptr_difference_const (@0, @1, &diff))
2966     { build_int_cst_type (type, diff); }))))
2968 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2969 (simplify
2970  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2971  (with { poly_int64 diff; }
2972    (if (ptr_difference_const (@0, @2, &diff))
2973     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2974 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2975 (simplify
2976  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2977  (with { poly_int64 diff; }
2978    (if (ptr_difference_const (@0, @2, &diff))
2979     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2980 (simplify
2981  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2982  (with { poly_int64 diff; }
2983    (if (ptr_difference_const (@0, @1, &diff))
2984     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2986 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2987 (simplify
2988  (convert (pointer_diff @0 INTEGER_CST@1))
2989  (if (POINTER_TYPE_P (type))
2990   { build_fold_addr_expr_with_type
2991       (build2 (MEM_REF, char_type_node, @0,
2992                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2993                type); }))
2995 /* If arg0 is derived from the address of an object or function, we may
2996    be able to fold this expression using the object or function's
2997    alignment.  */
2998 (simplify
2999  (bit_and (convert? @0) INTEGER_CST@1)
3000  (if (POINTER_TYPE_P (TREE_TYPE (@0))
3001       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3002   (with
3003    {
3004      unsigned int align;
3005      unsigned HOST_WIDE_INT bitpos;
3006      get_pointer_alignment_1 (@0, &align, &bitpos);
3007    }
3008    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
3009     { wide_int_to_tree (type, (wi::to_wide (@1)
3010                                & (bitpos / BITS_PER_UNIT))); }))))
3012 (match min_value
3013  uniform_integer_cst_p
3014  (with {
3015    tree int_cst = uniform_integer_cst_p (t);
3016    tree inner_type = TREE_TYPE (int_cst);
3017   }
3018   (if ((INTEGRAL_TYPE_P (inner_type)
3019         || POINTER_TYPE_P (inner_type))
3020        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3022 (match max_value
3023  uniform_integer_cst_p
3024  (with {
3025    tree int_cst = uniform_integer_cst_p (t);
3026    tree itype = TREE_TYPE (int_cst);
3027   }
3028  (if ((INTEGRAL_TYPE_P (itype)
3029        || POINTER_TYPE_P (itype))
3030       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3032 /* x >  y  &&  x != XXX_MIN  -->  x > y
3033    x >  y  &&  x == XXX_MIN  -->  false . */
3034 (for eqne (eq ne)
3035  (simplify
3036   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_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_MAX  -->  x < y
3045    x <  y  &&  x == XXX_MAX  -->  false.  */
3046 (for eqne (eq ne)
3047  (simplify
3048   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3049    (switch
3050     (if (eqne == EQ_EXPR)
3051      { constant_boolean_node (false, type); })
3052     (if (eqne == NE_EXPR)
3053      @2)
3054     )))
3056 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
3057 (simplify
3058  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3059   @2)
3061 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
3062 (simplify
3063  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3064   @2)
3066 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
3067 (simplify
3068  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3069   @2)
3071 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
3072 (simplify
3073  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3074   { constant_boolean_node (true, type); })
3076 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
3077 (simplify
3078  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3079   @2)
3081 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
3082 (simplify
3083  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3084   @2)
3086 /* x >=  y  ||  x != XXX_MAX   -->  true
3087    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
3088 (for eqne (eq ne)
3089  (simplify
3090   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3091    (switch
3092     (if (eqne == EQ_EXPR)
3093      @2)
3094     (if (eqne == NE_EXPR)
3095      { constant_boolean_node (true, type); }))))
3097 /* y == XXX_MIN || x < y --> x <= y - 1 */
3098 (simplify
3099  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3100   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3101        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3102   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3104 /* y != XXX_MIN && x >= y --> x > y - 1 */
3105 (simplify
3106  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3107   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3108        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3109   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3111 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3112    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3113 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3114    Similarly for (X != Y).  */
3116 (for code1 (eq ne)
3117  (for code2 (eq ne lt gt le ge)
3118   (simplify
3119    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3120    (if ((TREE_CODE (@1) == INTEGER_CST
3121          && TREE_CODE (@2) == INTEGER_CST)
3122         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3123              || POINTER_TYPE_P (TREE_TYPE (@1)))
3124             && bitwise_equal_p (@1, @2)))
3125     (with
3126      {
3127       bool one_before = false;
3128       bool one_after = false;
3129       int cmp = 0;
3130       bool allbits = true;
3131       if (TREE_CODE (@1) == INTEGER_CST
3132           && TREE_CODE (@2) == INTEGER_CST)
3133         {
3134           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3135           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3136           auto t2 = wi::to_wide (@2);
3137           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3138           if (cmp < 0
3139               && t1 == t2 - 1)
3140             one_before = true;
3141           if (cmp > 0
3142               && t1 == t2 + 1)
3143             one_after = true;
3144         }
3145       bool val;
3146       switch (code2)
3147          {
3148         case EQ_EXPR: val = (cmp == 0); break;
3149         case NE_EXPR: val = (cmp != 0); break;
3150         case LT_EXPR: val = (cmp < 0); break;
3151         case GT_EXPR: val = (cmp > 0); break;
3152         case LE_EXPR: val = (cmp <= 0); break;
3153         case GE_EXPR: val = (cmp >= 0); break;
3154         default: gcc_unreachable ();
3155         }
3156      }
3157      (switch
3158       (if (code1 == EQ_EXPR && val) @3)
3159       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3160       (if (code1 == NE_EXPR && !val && allbits) @4)
3161       (if (code1 == NE_EXPR
3162            && code2 == GE_EXPR
3163            && cmp == 0
3164            && allbits)
3165        (gt @c0 (convert @1)))
3166       (if (code1 == NE_EXPR
3167            && code2 == LE_EXPR
3168            && cmp == 0
3169            && allbits)
3170        (lt @c0 (convert @1)))
3171       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3172       (if (code1 == NE_EXPR
3173            && code2 == GT_EXPR
3174            && one_after
3175            && allbits)
3176        (gt @c0 (convert @1)))
3177       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3178       (if (code1 == NE_EXPR
3179            && code2 == LT_EXPR
3180            && one_before
3181            && allbits)
3182        (lt @c0 (convert @1)))
3183      )
3184     )
3185    )
3186   )
3190 /* Convert (X OP1 CST1) && (X OP2 CST2).
3191    Convert (X OP1 Y) && (X OP2 Y).  */
3193 (for code1 (lt le gt ge)
3194  (for code2 (lt le gt ge)
3195   (simplify
3196   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3197   (if ((TREE_CODE (@1) == INTEGER_CST
3198         && TREE_CODE (@2) == INTEGER_CST)
3199        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3200             || POINTER_TYPE_P (TREE_TYPE (@1)))
3201            && operand_equal_p (@1, @2)))
3202    (with
3203     {
3204      int cmp = 0;
3205      if (TREE_CODE (@1) == INTEGER_CST
3206          && TREE_CODE (@2) == INTEGER_CST)
3207        cmp = tree_int_cst_compare (@1, @2);
3208     }
3209     (switch
3210      /* Choose the more restrictive of two < or <= comparisons.  */
3211      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3212           && (code2 == LT_EXPR || code2 == LE_EXPR))
3213       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3214        @3
3215        @4))
3216      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3217      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3218           && (code2 == GT_EXPR || code2 == GE_EXPR))
3219       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3220        @3
3221        @4))
3222      /* Check for singleton ranges.  */
3223      (if (cmp == 0
3224           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3225             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3226       (eq @0 @1))
3227      /* Check for disjoint ranges.  */
3228      (if (cmp <= 0
3229           && (code1 == LT_EXPR || code1 == LE_EXPR)
3230           && (code2 == GT_EXPR || code2 == GE_EXPR))
3231       { constant_boolean_node (false, type); })
3232      (if (cmp >= 0
3233           && (code1 == GT_EXPR || code1 == GE_EXPR)
3234           && (code2 == LT_EXPR || code2 == LE_EXPR))
3235       { constant_boolean_node (false, type); })
3236      ))))))
3238 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3239    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3240 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3241    Similarly for (X != Y).  */
3243 (for code1 (eq ne)
3244  (for code2 (eq ne lt gt le ge)
3245   (simplify
3246    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3247    (if ((TREE_CODE (@1) == INTEGER_CST
3248          && TREE_CODE (@2) == INTEGER_CST)
3249         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3250             || POINTER_TYPE_P (TREE_TYPE (@1)))
3251             && bitwise_equal_p (@1, @2)))
3252     (with
3253      {
3254       bool one_before = false;
3255       bool one_after = false;
3256       int cmp = 0;
3257       bool allbits = true;
3258       if (TREE_CODE (@1) == INTEGER_CST
3259           && TREE_CODE (@2) == INTEGER_CST)
3260         {
3261           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3262           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3263           auto t2 = wi::to_wide (@2);
3264           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3265           if (cmp < 0
3266               && t1 == t2 - 1)
3267             one_before = true;
3268           if (cmp > 0
3269               && t1 == t2 + 1)
3270             one_after = true;
3271         }
3272       bool val;
3273       switch (code2)
3274         {
3275         case EQ_EXPR: val = (cmp == 0); break;
3276         case NE_EXPR: val = (cmp != 0); break;
3277         case LT_EXPR: val = (cmp < 0); break;
3278         case GT_EXPR: val = (cmp > 0); break;
3279         case LE_EXPR: val = (cmp <= 0); break;
3280         case GE_EXPR: val = (cmp >= 0); break;
3281         default: gcc_unreachable ();
3282         }
3283      }
3284      (switch
3285       (if (code1 == EQ_EXPR && val) @4)
3286       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3287       (if (code1 == NE_EXPR && !val && allbits) @3)
3288       (if (code1 == EQ_EXPR
3289            && code2 == GT_EXPR
3290            && cmp == 0
3291            && allbits)
3292        (ge @c0 @2))
3293       (if (code1 == EQ_EXPR
3294            && code2 == LT_EXPR
3295            && cmp == 0
3296            && allbits)
3297        (le @c0 @2))
3298       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3299       (if (code1 == EQ_EXPR
3300            && code2 == GE_EXPR
3301            && one_before
3302            && allbits)
3303        (ge @c0 (convert @1)))
3304       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3305       (if (code1 == EQ_EXPR
3306            && code2 == LE_EXPR
3307            && one_after
3308            && allbits)
3309        (le @c0 (convert @1)))
3310      )
3311     )
3312    )
3313   )
3317 /* Convert (X OP1 CST1) || (X OP2 CST2).
3318    Convert (X OP1 Y)    || (X OP2 Y).  */
3320 (for code1 (lt le gt ge)
3321  (for code2 (lt le gt ge)
3322   (simplify
3323   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3324   (if ((TREE_CODE (@1) == INTEGER_CST
3325         && TREE_CODE (@2) == INTEGER_CST)
3326        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3327             || POINTER_TYPE_P (TREE_TYPE (@1)))
3328            && operand_equal_p (@1, @2)))
3329    (with
3330     {
3331      int cmp = 0;
3332      if (TREE_CODE (@1) == INTEGER_CST
3333          && TREE_CODE (@2) == INTEGER_CST)
3334        cmp = tree_int_cst_compare (@1, @2);
3335     }
3336     (switch
3337      /* Choose the more restrictive of two < or <= comparisons.  */
3338      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3339           && (code2 == LT_EXPR || code2 == LE_EXPR))
3340       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3341        @4
3342        @3))
3343      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3344      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3345           && (code2 == GT_EXPR || code2 == GE_EXPR))
3346       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3347        @4
3348        @3))
3349      /* Check for singleton ranges.  */
3350      (if (cmp == 0
3351           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3352               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3353       (ne @0 @2))
3354      /* Check for disjoint ranges.  */
3355      (if (cmp >= 0
3356           && (code1 == LT_EXPR || code1 == LE_EXPR)
3357           && (code2 == GT_EXPR || code2 == GE_EXPR))
3358       { constant_boolean_node (true, type); })
3359      (if (cmp <= 0
3360           && (code1 == GT_EXPR || code1 == GE_EXPR)
3361           && (code2 == LT_EXPR || code2 == LE_EXPR))
3362       { constant_boolean_node (true, type); })
3363      ))))))
3365 /* Optimize (a CMP b) ^ (a CMP b)  */
3366 /* Optimize (a CMP b) != (a CMP b)  */
3367 (for op (bit_xor ne)
3368  (for cmp1 (lt lt lt le le le)
3369       cmp2 (gt eq ne ge eq ne)
3370       rcmp (ne le gt ne lt ge)
3371   (simplify
3372    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3373    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3374     (rcmp @0 @1)))))
3376 /* Optimize (a CMP b) == (a CMP b)  */
3377 (for cmp1 (lt lt lt le le le)
3378      cmp2 (gt eq ne ge eq ne)
3379      rcmp (eq gt le eq ge lt)
3380  (simplify
3381   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3382   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3383     (rcmp @0 @1))))
3385 /* (type)([0,1]@a != 0) -> (type)a
3386    (type)([0,1]@a == 1) -> (type)a
3387    (type)([0,1]@a == 0) -> a ^ 1
3388    (type)([0,1]@a != 1) -> a ^ 1.  */
3389 (for eqne (eq ne)
3390  (simplify
3391   (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3392   (if ((integer_zerop (@1) || integer_onep (@1)))
3393    (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3394     (convert @0)
3395     /* Only do this if the types match as (type)(a == 0) is
3396        canonical form normally, while `a ^ 1` is canonical when
3397        there is no type change. */
3398     (if (types_match (type, TREE_TYPE (@0)))
3399      (bit_xor @0 { build_one_cst (type); } ))))))
3401 /* We can't reassociate at all for saturating types.  */
3402 (if (!TYPE_SATURATING (type))
3404  /* Contract negates.  */
3405  /* A + (-B) -> A - B */
3406  (simplify
3407   (plus:c @0 (convert? (negate @1)))
3408   /* Apply STRIP_NOPS on the negate.  */
3409   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3410        && !TYPE_OVERFLOW_SANITIZED (type))
3411    (with
3412     {
3413      tree t1 = type;
3414      if (INTEGRAL_TYPE_P (type)
3415          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3416        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3417     }
3418     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3419  /* A - (-B) -> A + B */
3420  (simplify
3421   (minus @0 (convert? (negate @1)))
3422   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3423        && !TYPE_OVERFLOW_SANITIZED (type))
3424    (with
3425     {
3426      tree t1 = type;
3427      if (INTEGRAL_TYPE_P (type)
3428          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3429        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3430     }
3431     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3432  /* -(T)(-A) -> (T)A
3433     Sign-extension is ok except for INT_MIN, which thankfully cannot
3434     happen without overflow.  */
3435  (simplify
3436   (negate (convert (negate @1)))
3437   (if (INTEGRAL_TYPE_P (type)
3438        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3439            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3440                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3441        && !TYPE_OVERFLOW_SANITIZED (type)
3442        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3443    (convert @1)))
3444  (simplify
3445   (negate (convert negate_expr_p@1))
3446   (if (SCALAR_FLOAT_TYPE_P (type)
3447        && ((DECIMAL_FLOAT_TYPE_P (type)
3448             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3449             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3450            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3451    (convert (negate @1))))
3452  (simplify
3453   (negate (nop_convert? (negate @1)))
3454   (if (!TYPE_OVERFLOW_SANITIZED (type)
3455        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3456    (view_convert @1)))
3458  /* We can't reassociate floating-point unless -fassociative-math
3459     or fixed-point plus or minus because of saturation to +-Inf.  */
3460  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3461       && !FIXED_POINT_TYPE_P (type))
3463   /* Match patterns that allow contracting a plus-minus pair
3464      irrespective of overflow issues.  */
3465   /* (A +- B) - A       ->  +- B */
3466   /* (A +- B) -+ B      ->  A */
3467   /* A - (A +- B)       -> -+ B */
3468   /* A +- (B -+ A)      ->  +- B */
3469   (simplify
3470    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3471    (view_convert @1))
3472   (simplify
3473    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3474    (if (!ANY_INTEGRAL_TYPE_P (type)
3475         || TYPE_OVERFLOW_WRAPS (type))
3476    (negate (view_convert @1))
3477    (view_convert (negate @1))))
3478   (simplify
3479    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3480    (view_convert @0))
3481   (simplify
3482    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3483     (if (!ANY_INTEGRAL_TYPE_P (type)
3484          || TYPE_OVERFLOW_WRAPS (type))
3485      (negate (view_convert @1))
3486      (view_convert (negate @1))))
3487   (simplify
3488    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3489    (view_convert @1))
3490   /* (A +- B) + (C - A)   -> C +- B */
3491   /* (A +  B) - (A - C)   -> B + C */
3492   /* More cases are handled with comparisons.  */
3493   (simplify
3494    (plus:c (plus:c @0 @1) (minus @2 @0))
3495    (plus @2 @1))
3496   (simplify
3497    (plus:c (minus @0 @1) (minus @2 @0))
3498    (minus @2 @1))
3499   (simplify
3500    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3501    (if (TYPE_OVERFLOW_UNDEFINED (type)
3502         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3503     (pointer_diff @2 @1)))
3504   (simplify
3505    (minus (plus:c @0 @1) (minus @0 @2))
3506    (plus @1 @2))
3508   /* (A +- CST1) +- CST2 -> A + CST3
3509      Use view_convert because it is safe for vectors and equivalent for
3510      scalars.  */
3511   (for outer_op (plus minus)
3512    (for inner_op (plus minus)
3513         neg_inner_op (minus plus)
3514     (simplify
3515      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3516                CONSTANT_CLASS_P@2)
3517      /* If one of the types wraps, use that one.  */
3518      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3519       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3520          forever if something doesn't simplify into a constant.  */
3521       (if (!CONSTANT_CLASS_P (@0))
3522        (if (outer_op == PLUS_EXPR)
3523         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3524         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3525       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3526            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3527        (if (outer_op == PLUS_EXPR)
3528         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3529         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3530        /* If the constant operation overflows we cannot do the transform
3531           directly as we would introduce undefined overflow, for example
3532           with (a - 1) + INT_MIN.  */
3533        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3534         (with { tree cst = const_binop (outer_op == inner_op
3535                                         ? PLUS_EXPR : MINUS_EXPR,
3536                                         type, @1, @2); }
3537          (if (cst)
3538           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3539            (inner_op @0 { cst; } )
3540            /* X+INT_MAX+1 is X-INT_MIN.  */
3541            (if (INTEGRAL_TYPE_P (type)
3542                 && wi::to_wide (cst) == wi::min_value (type))
3543             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3544             /* Last resort, use some unsigned type.  */
3545             (with { tree utype = unsigned_type_for (type); }
3546              (if (utype)
3547               (view_convert (inner_op
3548                              (view_convert:utype @0)
3549                              (view_convert:utype
3550                               { TREE_OVERFLOW (cst)
3551                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3553   /* (CST1 - A) +- CST2 -> CST3 - A  */
3554   (for outer_op (plus minus)
3555    (simplify
3556     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3557     /* If one of the types wraps, use that one.  */
3558     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3559      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3560         forever if something doesn't simplify into a constant.  */
3561      (if (!CONSTANT_CLASS_P (@0))
3562       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3563      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3564           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3565       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3566       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3567        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3568         (if (cst && !TREE_OVERFLOW (cst))
3569          (minus { cst; } @0))))))))
3571   /* CST1 - (CST2 - A) -> CST3 + A
3572      Use view_convert because it is safe for vectors and equivalent for
3573      scalars.  */
3574   (simplify
3575    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3576    /* If one of the types wraps, use that one.  */
3577    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3578     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3579       forever if something doesn't simplify into a constant.  */
3580     (if (!CONSTANT_CLASS_P (@0))
3581      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3582     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3583          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3584      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3585      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3586       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3587        (if (cst && !TREE_OVERFLOW (cst))
3588         (plus { cst; } @0)))))))
3590 /* ((T)(A)) + CST -> (T)(A + CST)  */
3591 #if GIMPLE
3592   (simplify
3593    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3594     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3595          && TREE_CODE (type) == INTEGER_TYPE
3596          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3597          && int_fits_type_p (@1, TREE_TYPE (@0)))
3598      /* Perform binary operation inside the cast if the constant fits
3599         and (A + CST)'s range does not overflow.  */
3600      (with
3601       {
3602         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3603                           max_ovf = wi::OVF_OVERFLOW;
3604         tree inner_type = TREE_TYPE (@0);
3606         wide_int w1
3607           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3608                             TYPE_SIGN (inner_type));
3610         value_range vr;
3611         if (get_global_range_query ()->range_of_expr (vr, @0)
3612             && !vr.varying_p () && !vr.undefined_p ())
3613           {
3614             wide_int wmin0 = vr.lower_bound ();
3615             wide_int wmax0 = vr.upper_bound ();
3616             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3617             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3618           }
3619       }
3620      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3621       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3622      )))
3623 #endif
3625 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3626 #if GIMPLE
3627   (for op (plus minus)
3628    (simplify
3629     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3630      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3631           && TREE_CODE (type) == INTEGER_TYPE
3632           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3633           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3634           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3635           && TYPE_OVERFLOW_WRAPS (type))
3636        (plus (convert @0) (op @2 (convert @1))))))
3637 #endif
3639 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3640    to a simple value.  */
3641   (for op (plus minus)
3642    (simplify
3643     (op (convert @0) (convert @1))
3644      (if (INTEGRAL_TYPE_P (type)
3645           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3646           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3647           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3648           && !TYPE_OVERFLOW_TRAPS (type)
3649           && !TYPE_OVERFLOW_SANITIZED (type))
3650       (convert (op! @0 @1)))))
3652   /* ~A + A -> -1 */
3653   (simplify
3654    (plus:c (convert? (bit_not @0)) (convert? @0))
3655    (if (!TYPE_OVERFLOW_TRAPS (type))
3656     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3658   /* ~A + 1 -> -A */
3659   (simplify
3660    (plus (convert? (bit_not @0)) integer_each_onep)
3661    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3662     (negate (convert @0))))
3664   /* -A - 1 -> ~A */
3665   (simplify
3666    (minus (convert? (negate @0)) integer_each_onep)
3667    (if (!TYPE_OVERFLOW_TRAPS (type)
3668         && TREE_CODE (type) != COMPLEX_TYPE
3669         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3670     (bit_not (convert @0))))
3672   /* -1 - A -> ~A */
3673   (simplify
3674    (minus integer_all_onesp @0)
3675    (if (TREE_CODE (type) != COMPLEX_TYPE)
3676     (bit_not @0)))
3678   /* (T)(P + A) - (T)P -> (T) A */
3679   (simplify
3680    (minus (convert (plus:c @@0 @1))
3681     (convert? @0))
3682    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3683         /* For integer types, if A has a smaller type
3684            than T the result depends on the possible
3685            overflow in P + A.
3686            E.g. T=size_t, A=(unsigned)429497295, P>0.
3687            However, if an overflow in P + A would cause
3688            undefined behavior, we can assume that there
3689            is no overflow.  */
3690         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3691             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3692     (convert @1)))
3693   (simplify
3694    (minus (convert (pointer_plus @@0 @1))
3695     (convert @0))
3696    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3697         /* For pointer types, if the conversion of A to the
3698            final type requires a sign- or zero-extension,
3699            then we have to punt - it is not defined which
3700            one is correct.  */
3701         || (POINTER_TYPE_P (TREE_TYPE (@0))
3702             && TREE_CODE (@1) == INTEGER_CST
3703             && tree_int_cst_sign_bit (@1) == 0))
3704     (convert @1)))
3705    (simplify
3706     (pointer_diff (pointer_plus @@0 @1) @0)
3707     /* The second argument of pointer_plus must be interpreted as signed, and
3708        thus sign-extended if necessary.  */
3709     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3710      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3711         second arg is unsigned even when we need to consider it as signed,
3712         we don't want to diagnose overflow here.  */
3713      (convert (view_convert:stype @1))))
3715   /* (T)P - (T)(P + A) -> -(T) A */
3716   (simplify
3717    (minus (convert? @0)
3718     (convert (plus:c @@0 @1)))
3719    (if (INTEGRAL_TYPE_P (type)
3720         && TYPE_OVERFLOW_UNDEFINED (type)
3721         /* For integer literals, using an intermediate unsigned type to avoid
3722            an overflow at run time is counter-productive because it introduces
3723            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3724            the result, which may be problematic in GENERIC for some front-ends:
3725              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3726            so we use the direct path for them.  */
3727         && TREE_CODE (@1) != INTEGER_CST
3728         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3729     (with { tree utype = unsigned_type_for (type); }
3730      (convert (negate (convert:utype @1))))
3731     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3732          /* For integer types, if A has a smaller type
3733             than T the result depends on the possible
3734             overflow in P + A.
3735             E.g. T=size_t, A=(unsigned)429497295, P>0.
3736             However, if an overflow in P + A would cause
3737             undefined behavior, we can assume that there
3738             is no overflow.  */
3739          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3740              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3741      (negate (convert @1)))))
3742   (simplify
3743    (minus (convert @0)
3744     (convert (pointer_plus @@0 @1)))
3745    (if (INTEGRAL_TYPE_P (type)
3746         && TYPE_OVERFLOW_UNDEFINED (type)
3747         /* See above the rationale for this condition.  */
3748         && TREE_CODE (@1) != INTEGER_CST
3749         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3750     (with { tree utype = unsigned_type_for (type); }
3751      (convert (negate (convert:utype @1))))
3752     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3753          /* For pointer types, if the conversion of A to the
3754             final type requires a sign- or zero-extension,
3755             then we have to punt - it is not defined which
3756             one is correct.  */
3757          || (POINTER_TYPE_P (TREE_TYPE (@0))
3758              && TREE_CODE (@1) == INTEGER_CST
3759              && tree_int_cst_sign_bit (@1) == 0))
3760      (negate (convert @1)))))
3761    (simplify
3762     (pointer_diff @0 (pointer_plus @@0 @1))
3763     /* The second argument of pointer_plus must be interpreted as signed, and
3764        thus sign-extended if necessary.  */
3765     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3766      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3767         second arg is unsigned even when we need to consider it as signed,
3768         we don't want to diagnose overflow here.  */
3769      (negate (convert (view_convert:stype @1)))))
3771   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3772   (simplify
3773    (minus (convert (plus:c @@0 @1))
3774     (convert (plus:c @0 @2)))
3775    (if (INTEGRAL_TYPE_P (type)
3776         && TYPE_OVERFLOW_UNDEFINED (type)
3777         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3778         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3779     (with { tree utype = unsigned_type_for (type); }
3780      (convert (minus (convert:utype @1) (convert:utype @2))))
3781     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3782           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3783          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3784              /* For integer types, if A has a smaller type
3785                 than T the result depends on the possible
3786                 overflow in P + A.
3787                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3788                 However, if an overflow in P + A would cause
3789                 undefined behavior, we can assume that there
3790                 is no overflow.  */
3791              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3792                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3793                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3794                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3795      (minus (convert @1) (convert @2)))))
3796   (simplify
3797    (minus (convert (pointer_plus @@0 @1))
3798     (convert (pointer_plus @0 @2)))
3799    (if (INTEGRAL_TYPE_P (type)
3800         && TYPE_OVERFLOW_UNDEFINED (type)
3801         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3802     (with { tree utype = unsigned_type_for (type); }
3803      (convert (minus (convert:utype @1) (convert:utype @2))))
3804     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3805          /* For pointer types, if the conversion of A to the
3806             final type requires a sign- or zero-extension,
3807             then we have to punt - it is not defined which
3808             one is correct.  */
3809          || (POINTER_TYPE_P (TREE_TYPE (@0))
3810              && TREE_CODE (@1) == INTEGER_CST
3811              && tree_int_cst_sign_bit (@1) == 0
3812              && TREE_CODE (@2) == INTEGER_CST
3813              && tree_int_cst_sign_bit (@2) == 0))
3814      (minus (convert @1) (convert @2)))))
3815    (simplify
3816     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3817      (pointer_diff @0 @1))
3818    (simplify
3819     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3820     /* The second argument of pointer_plus must be interpreted as signed, and
3821        thus sign-extended if necessary.  */
3822     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3823      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3824         second arg is unsigned even when we need to consider it as signed,
3825         we don't want to diagnose overflow here.  */
3826      (minus (convert (view_convert:stype @1))
3827             (convert (view_convert:stype @2)))))))
3829 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3830     Modeled after fold_plusminus_mult_expr.  */
3831 (if (!TYPE_SATURATING (type)
3832      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3833  (for plusminus (plus minus)
3834   (simplify
3835    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3836    (if (!ANY_INTEGRAL_TYPE_P (type)
3837         || TYPE_OVERFLOW_WRAPS (type)
3838         || (INTEGRAL_TYPE_P (type)
3839             && tree_expr_nonzero_p (@0)
3840             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3841     (if (single_use (@3) || single_use (@4))
3842      /* If @1 +- @2 is constant require a hard single-use on either
3843         original operand (but not on both).  */
3844      (mult (plusminus @1 @2) @0)
3845      (mult! (plusminus @1 @2) @0)
3846   )))
3847   /* We cannot generate constant 1 for fract.  */
3848   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3849    (simplify
3850     (plusminus @0 (mult:c@3 @0 @2))
3851     (if ((!ANY_INTEGRAL_TYPE_P (type)
3852           || TYPE_OVERFLOW_WRAPS (type)
3853           /* For @0 + @0*@2 this transformation would introduce UB
3854              (where there was none before) for @0 in [-1,0] and @2 max.
3855              For @0 - @0*@2 this transformation would introduce UB
3856              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3857           || (INTEGRAL_TYPE_P (type)
3858               && ((tree_expr_nonzero_p (@0)
3859                    && expr_not_equal_to (@0,
3860                                 wi::minus_one (TYPE_PRECISION (type))))
3861                   || (plusminus == PLUS_EXPR
3862                       ? expr_not_equal_to (@2,
3863                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3864                       /* Let's ignore the @0 -1 and @2 min case.  */
3865                       : (expr_not_equal_to (@2,
3866                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3867                          && expr_not_equal_to (@2,
3868                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3869                                 + 1))))))
3870          && single_use (@3))
3871      (mult (plusminus { build_one_cst (type); } @2) @0)))
3872    (simplify
3873     (plusminus (mult:c@3 @0 @2) @0)
3874     (if ((!ANY_INTEGRAL_TYPE_P (type)
3875           || TYPE_OVERFLOW_WRAPS (type)
3876           /* For @0*@2 + @0 this transformation would introduce UB
3877              (where there was none before) for @0 in [-1,0] and @2 max.
3878              For @0*@2 - @0 this transformation would introduce UB
3879              for @0 0 and @2 min.  */
3880           || (INTEGRAL_TYPE_P (type)
3881               && ((tree_expr_nonzero_p (@0)
3882                    && (plusminus == MINUS_EXPR
3883                        || expr_not_equal_to (@0,
3884                                 wi::minus_one (TYPE_PRECISION (type)))))
3885                   || expr_not_equal_to (@2,
3886                         (plusminus == PLUS_EXPR
3887                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3888                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3889          && single_use (@3))
3890      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3892 #if GIMPLE
3893 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3894    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3895 (simplify
3896  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3897   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3898        && tree_fits_uhwi_p (@1)
3899        && tree_to_uhwi (@1) < element_precision (type)
3900        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3901            || optab_handler (smul_optab,
3902                              TYPE_MODE (type)) != CODE_FOR_nothing))
3903    (with { tree t = type;
3904            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3905            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3906                                              element_precision (type));
3907            w += 1;
3908            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3909                                         : t, w);
3910            cst = build_uniform_cst (t, cst); }
3911     (convert (mult (convert:t @0) { cst; })))))
3912 (simplify
3913  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3914   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3915        && tree_fits_uhwi_p (@1)
3916        && tree_to_uhwi (@1) < element_precision (type)
3917        && tree_fits_uhwi_p (@2)
3918        && tree_to_uhwi (@2) < element_precision (type)
3919        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3920            || optab_handler (smul_optab,
3921                              TYPE_MODE (type)) != CODE_FOR_nothing))
3922    (with { tree t = type;
3923            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3924            unsigned int prec = element_precision (type);
3925            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3926            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3927            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3928                                         : t, w);
3929            cst = build_uniform_cst (t, cst); }
3930     (convert (mult (convert:t @0) { cst; })))))
3931 #endif
3933 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3934    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3935    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3936 (for op (bit_ior bit_xor)
3937  (simplify
3938   (op (mult:s@0 @1 INTEGER_CST@2)
3939       (mult:s@3 @1 INTEGER_CST@4))
3940   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3941        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3942    (mult @1
3943          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3944  (simplify
3945   (op:c (mult:s@0 @1 INTEGER_CST@2)
3946         (lshift:s@3 @1 INTEGER_CST@4))
3947   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3948        && tree_int_cst_sgn (@4) > 0
3949        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3950    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3951            wide_int c = wi::add (wi::to_wide (@2),
3952                                  wi::lshift (wone, wi::to_wide (@4))); }
3953     (mult @1 { wide_int_to_tree (type, c); }))))
3954  (simplify
3955   (op:c (mult:s@0 @1 INTEGER_CST@2)
3956         @1)
3957   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3958        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3959    (mult @1
3960          { wide_int_to_tree (type,
3961                              wi::add (wi::to_wide (@2), 1)); })))
3962  (simplify
3963   (op (lshift:s@0 @1 INTEGER_CST@2)
3964       (lshift:s@3 @1 INTEGER_CST@4))
3965   (if (INTEGRAL_TYPE_P (type)
3966        && tree_int_cst_sgn (@2) > 0
3967        && tree_int_cst_sgn (@4) > 0
3968        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3969    (with { tree t = type;
3970            if (!TYPE_OVERFLOW_WRAPS (t))
3971              t = unsigned_type_for (t);
3972            wide_int wone = wi::one (TYPE_PRECISION (t));
3973            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3974                                  wi::lshift (wone, wi::to_wide (@4))); }
3975     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3976  (simplify
3977   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3978         @1)
3979   (if (INTEGRAL_TYPE_P (type)
3980        && tree_int_cst_sgn (@2) > 0
3981        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3982    (with { tree t = type;
3983            if (!TYPE_OVERFLOW_WRAPS (t))
3984              t = unsigned_type_for (t);
3985            wide_int wone = wi::one (TYPE_PRECISION (t));
3986            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3987     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3989 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3991 (for minmax (min max)
3992  (simplify
3993   (minmax @0 @0)
3994   @0)
3995 /* max(max(x,y),x) -> max(x,y)  */
3996  (simplify
3997   (minmax:c (minmax:c@2 @0 @1) @0)
3998   @2))
3999 /* For fmin() and fmax(), skip folding when both are sNaN.  */
4000 (for minmax (FMIN_ALL FMAX_ALL)
4001  (simplify
4002   (minmax @0 @0)
4003   (if (!tree_expr_maybe_signaling_nan_p (@0))
4004     @0)))
4005 /* min(max(x,y),y) -> y.  */
4006 (simplify
4007  (min:c (max:c @0 @1) @1)
4008  @1)
4009 /* max(min(x,y),y) -> y.  */
4010 (simplify
4011  (max:c (min:c @0 @1) @1)
4012  @1)
4013 /* max(a,-a) -> abs(a).  */
4014 (simplify
4015  (max:c @0 (negate @0))
4016  (if (TREE_CODE (type) != COMPLEX_TYPE
4017       && (! ANY_INTEGRAL_TYPE_P (type)
4018           || TYPE_OVERFLOW_UNDEFINED (type)))
4019   (abs @0)))
4020 /* min(a,-a) -> -abs(a).  */
4021 (simplify
4022  (min:c @0 (negate @0))
4023  (if (TREE_CODE (type) != COMPLEX_TYPE
4024       && (! ANY_INTEGRAL_TYPE_P (type)
4025           || TYPE_OVERFLOW_UNDEFINED (type)))
4026   (negate (abs @0))))
4027 (simplify
4028  (min @0 @1)
4029  (switch
4030   (if (INTEGRAL_TYPE_P (type)
4031        && TYPE_MIN_VALUE (type)
4032        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4033    @1)
4034   (if (INTEGRAL_TYPE_P (type)
4035        && TYPE_MAX_VALUE (type)
4036        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4037    @0)))
4038 (simplify
4039  (max @0 @1)
4040  (switch
4041   (if (INTEGRAL_TYPE_P (type)
4042        && TYPE_MAX_VALUE (type)
4043        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4044    @1)
4045   (if (INTEGRAL_TYPE_P (type)
4046        && TYPE_MIN_VALUE (type)
4047        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4048    @0)))
4050 /* max (a, a + CST) -> a + CST where CST is positive.  */
4051 /* max (a, a + CST) -> a where CST is negative.  */
4052 (simplify
4053  (max:c @0 (plus@2 @0 INTEGER_CST@1))
4054   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4055    (if (tree_int_cst_sgn (@1) > 0)
4056     @2
4057     @0)))
4059 /* min (a, a + CST) -> a where CST is positive.  */
4060 /* min (a, a + CST) -> a + CST where CST is negative. */
4061 (simplify
4062  (min:c @0 (plus@2 @0 INTEGER_CST@1))
4063   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4064    (if (tree_int_cst_sgn (@1) > 0)
4065     @0
4066     @2)))
4068 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4069    the addresses are known to be less, equal or greater.  */
4070 (for minmax (min max)
4071      cmp (lt gt)
4072  (simplify
4073   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4074   (with
4075    {
4076      poly_int64 off0, off1;
4077      tree base0, base1;
4078      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4079                                   off0, off1, GENERIC);
4080    }
4081    (if (equal == 1)
4082     (if (minmax == MIN_EXPR)
4083      (if (known_le (off0, off1))
4084       @2
4085       (if (known_gt (off0, off1))
4086        @3))
4087      (if (known_ge (off0, off1))
4088       @2
4089       (if (known_lt (off0, off1))
4090        @3)))))))
4092 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4093    and the outer convert demotes the expression back to x's type.  */
4094 (for minmax (min max)
4095  (simplify
4096   (convert (minmax@0 (convert @1) INTEGER_CST@2))
4097   (if (INTEGRAL_TYPE_P (type)
4098        && types_match (@1, type) && int_fits_type_p (@2, type)
4099        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4100        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4101    (minmax @1 (convert @2)))))
4103 (for minmax (FMIN_ALL FMAX_ALL)
4104  /* If either argument is NaN and other one is not sNaN, return the other
4105     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
4106  (simplify
4107   (minmax:c @0 REAL_CST@1)
4108    (if (real_isnan (TREE_REAL_CST_PTR (@1))
4109        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4110        && !tree_expr_maybe_signaling_nan_p (@0))
4111    @0)))
4112 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
4113    functions to return the numeric arg if the other one is NaN.
4114    MIN and MAX don't honor that, so only transform if -ffinite-math-only
4115    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
4116    worry about it either.  */
4117 (if (flag_finite_math_only)
4118  (simplify
4119   (FMIN_ALL @0 @1)
4120   (min @0 @1))
4121  (simplify
4122   (FMAX_ALL @0 @1)
4123   (max @0 @1)))
4124 /* min (-A, -B) -> -max (A, B)  */
4125 (for minmax (min max FMIN_ALL FMAX_ALL)
4126      maxmin (max min FMAX_ALL FMIN_ALL)
4127  (simplify
4128   (minmax (negate:s@2 @0) (negate:s@3 @1))
4129   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4130        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4131            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4132    (negate (maxmin @0 @1)))))
4133 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4134    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4135 (for minmax (min max)
4136  maxmin (max min)
4137  (simplify
4138   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4139   (bit_not (maxmin @0 @1)))
4140 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4141 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4142  (simplify
4143   (bit_not (minmax:cs (bit_not @0) @1))
4144   (maxmin @0 (bit_not @1))))
4146 /* MIN (X, Y) == X -> X <= Y  */
4147 /* MIN (X, Y) < X -> X > Y  */
4148 /* MIN (X, Y) >= X -> X <= Y  */
4149 (for minmax (min min min min max max max max)
4150      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4151      out    (le  gt  gt  le  ge  lt  lt  ge )
4152  (simplify
4153   (cmp:c (minmax:c @0 @1) @0)
4154   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4155    (out @0 @1))))
4156 /* MIN (X, 5) == 0 -> X == 0
4157    MIN (X, 5) == 7 -> false  */
4158 (for cmp (eq ne)
4159  (simplify
4160   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4161   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4162                  TYPE_SIGN (TREE_TYPE (@0))))
4163    { constant_boolean_node (cmp == NE_EXPR, type); }
4164    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4165                   TYPE_SIGN (TREE_TYPE (@0))))
4166     (cmp @0 @2)))))
4167 (for cmp (eq ne)
4168  (simplify
4169   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4170   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4171                  TYPE_SIGN (TREE_TYPE (@0))))
4172    { constant_boolean_node (cmp == NE_EXPR, type); }
4173    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4174                   TYPE_SIGN (TREE_TYPE (@0))))
4175     (cmp @0 @2)))))
4177 /* X <= MAX(X, Y) -> true
4178    X > MAX(X, Y) -> false 
4179    X >= MIN(X, Y) -> true
4180    X < MIN(X, Y) -> false */
4181 (for minmax (min     min     max     max     )
4182      cmp    (ge      lt      le      gt      )
4183  (simplify
4184   (cmp:c @0 (minmax:c @0 @1))
4185   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4187 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4188 (for minmax (min     min     max     max     min     min     max     max    )
4189      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4190      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4191  (simplify
4192   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4193   (comb (cmp @0 @2) (cmp @1 @2))))
4195 /* Undo fancy ways of writing max/min or other ?: expressions, like
4196    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4197    People normally use ?: and that is what we actually try to optimize.  */
4198 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4199 (simplify
4200  (plus:c @0 (mult:c (minus @1 @0) 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)))
4204 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4205 (simplify
4206  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4207  (if (INTEGRAL_TYPE_P (type)
4208       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4209   (cond (convert:boolean_type_node @2) @1 @0)))
4210 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4211 (simplify
4212  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4213  (if (INTEGRAL_TYPE_P (type)
4214       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4215   (cond (convert:boolean_type_node @2) @1 @0)))
4217 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4218 (simplify
4219   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4220   (max @2 @1))
4222 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4223 (for op (bit_xor bit_ior plus)
4224  (simplify
4225   (cond (eq zero_one_valued_p@0
4226             integer_zerop)
4227         @1
4228         (op:c @2 @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 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4235 (for op (bit_xor bit_ior plus)
4236  (simplify
4237   (cond (ne zero_one_valued_p@0
4238             integer_zerop)
4239        (op:c @2 @1)
4240         @1)
4241   (if (INTEGRAL_TYPE_P (type)
4242        && TYPE_PRECISION (type) > 1
4243        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4244        (op (mult (convert:type @0) @2) @1))))
4246 /* ?: Value replacement. */
4247 /* a == 0 ? b : b + a  -> b + a */
4248 (for op (plus bit_ior bit_xor)
4249  (simplify
4250   (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4251    @2))
4252 /* a == 0 ? b : b - a  -> b - a */
4253 /* a == 0 ? b : b ptr+ a  -> b ptr+ a */
4254 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4255 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4256  (simplify
4257   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4258    @2))
4260 /* a == 1 ? b : b / a  -> b / a */
4261 (for op (trunc_div ceil_div floor_div round_div exact_div)
4262  (simplify
4263   (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4264    @2))
4266 /* a == 1 ? b : a * b -> a * b */
4267 (for op (mult)
4268  (simplify
4269   (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4270    @2))
4272 /* a == -1 ? b : a & b -> a & b */
4273 (for op (bit_and)
4274  (simplify
4275   (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4276    @2))
4278 /* Simplifications of shift and rotates.  */
4280 (for rotate (lrotate rrotate)
4281  (simplify
4282   (rotate integer_all_onesp@0 @1)
4283   @0))
4285 /* Optimize -1 >> x for arithmetic right shifts.  */
4286 (simplify
4287  (rshift integer_all_onesp@0 @1)
4288  (if (!TYPE_UNSIGNED (type))
4289   @0))
4291 /* Optimize (x >> c) << c into x & (-1<<c).  */
4292 (simplify
4293  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4294  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4295   /* It doesn't matter if the right shift is arithmetic or logical.  */
4296   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4298 (simplify
4299  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4300  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4301       /* Allow intermediate conversion to integral type with whatever sign, as
4302          long as the low TYPE_PRECISION (type)
4303          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4304       && INTEGRAL_TYPE_P (type)
4305       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4306       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4307       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4308       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4309           || wi::geu_p (wi::to_wide (@1),
4310                         TYPE_PRECISION (type)
4311                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4312   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4314 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4315    unsigned x OR truncate into the precision(type) - c lowest bits
4316    of signed x (if they have mode precision or a precision of 1).  */
4317 (simplify
4318  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4319  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4320   (if (TYPE_UNSIGNED (type))
4321    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4322    (if (INTEGRAL_TYPE_P (type))
4323     (with {
4324       int width = element_precision (type) - tree_to_uhwi (@1);
4325       tree stype = NULL_TREE;
4326       if (width <= MAX_FIXED_MODE_SIZE)
4327         stype = build_nonstandard_integer_type (width, 0);
4328      }
4329      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4330       (convert (convert:stype @0))))))))
4332 /* Optimize x >> x into 0 */
4333 (simplify
4334  (rshift @0 @0)
4335   { build_zero_cst (type); })
4337 (for shiftrotate (lrotate rrotate lshift rshift)
4338  (simplify
4339   (shiftrotate @0 integer_zerop)
4340   (non_lvalue @0))
4341  (simplify
4342   (shiftrotate integer_zerop@0 @1)
4343   @0)
4344  /* Prefer vector1 << scalar to vector1 << vector2
4345     if vector2 is uniform.  */
4346  (for vec (VECTOR_CST CONSTRUCTOR)
4347   (simplify
4348    (shiftrotate @0 vec@1)
4349    (with { tree tem = uniform_vector_p (@1); }
4350     (if (tem)
4351      (shiftrotate @0 { tem; }))))))
4353 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4354    Y is 0.  Similarly for X >> Y.  */
4355 #if GIMPLE
4356 (for shift (lshift rshift)
4357  (simplify
4358   (shift @0 SSA_NAME@1)
4359    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4360     (with {
4361       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4362       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4363      }
4364      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4365       @0)))))
4366 #endif
4368 /* Rewrite an LROTATE_EXPR by a constant into an
4369    RROTATE_EXPR by a new constant.  */
4370 (simplify
4371  (lrotate @0 INTEGER_CST@1)
4372  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4373                             build_int_cst (TREE_TYPE (@1),
4374                                            element_precision (type)), @1); }))
4376 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4377 (for op (lrotate rrotate rshift lshift)
4378  (simplify
4379   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4380   (with { unsigned int prec = element_precision (type); }
4381    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4382         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4383         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4384         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4385     (with { unsigned int low = (tree_to_uhwi (@1)
4386                                 + tree_to_uhwi (@2)); }
4387      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4388         being well defined.  */
4389      (if (low >= prec)
4390       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4391        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4392        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4393         { build_zero_cst (type); }
4394         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4395       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4398 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4399 (simplify
4400  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4401   (if ((wi::to_wide (@1) & 1) != 0)
4402    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4403    { build_zero_cst (type); }))
4405 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4406    either to false if D is smaller (unsigned comparison) than C, or to
4407    x == log2 (D) - log2 (C).  Similarly for right shifts.
4408    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4409 (for cmp (ne eq)
4410      icmp (eq ne)
4411  (simplify
4412   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4413    (with { int c1 = wi::clz (wi::to_wide (@1));
4414            int c2 = wi::clz (wi::to_wide (@2)); }
4415     (if (c1 < c2)
4416      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4417      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4418  (simplify
4419   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4420    (if (tree_int_cst_sgn (@1) > 0)
4421     (with { int c1 = wi::clz (wi::to_wide (@1));
4422             int c2 = wi::clz (wi::to_wide (@2)); }
4423      (if (c1 > c2)
4424       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4425       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4426  /* `(1 >> X) != 0` -> `X == 0` */
4427  /* `(1 >> X) == 0` -> `X != 0` */
4428  (simplify
4429   (cmp (rshift integer_onep@1 @0) integer_zerop)
4430    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4431     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4433 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4434    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4435    if CST2 != 0.  */
4436 (for cmp (ne eq)
4437  (simplify
4438   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4439   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4440    (if (cand < 0
4441         || (!integer_zerop (@2)
4442             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4443     { constant_boolean_node (cmp == NE_EXPR, type); }
4444     (if (!integer_zerop (@2)
4445          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4446      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4448 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4449         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4450 (for cmp (ne eq)
4451  (simplify
4452   (cmp (bit_and:s (lshift: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     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4457       { constant_boolean_node (cmp == NE_EXPR, type); }
4458       (with { wide_int c1 = wi::to_wide (@1);
4459               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4460               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4461         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4462              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4463  (simplify
4464   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4465   (if (tree_fits_shwi_p (@1)
4466        && tree_to_shwi (@1) > 0
4467        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4468     (with { tree t0 = TREE_TYPE (@0);
4469             unsigned int prec = TYPE_PRECISION (t0);
4470             wide_int c1 = wi::to_wide (@1);
4471             wide_int c2 = wi::to_wide (@2);
4472             wide_int c3 = wi::to_wide (@3);
4473             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4474       (if ((c2 & c3) != c3)
4475         { constant_boolean_node (cmp == NE_EXPR, type); }
4476         (if (TYPE_UNSIGNED (t0))
4477           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4478             { constant_boolean_node (cmp == NE_EXPR, type); }
4479             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4480                  { wide_int_to_tree (t0, c3 << c1); }))
4481           (with { wide_int smask = wi::arshift (sb, c1); }
4482             (switch
4483               (if ((c2 & smask) == 0)
4484                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4485                      { wide_int_to_tree (t0, c3 << c1); }))
4486               (if ((c3 & smask) == 0)
4487                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4488                      { wide_int_to_tree (t0, c3 << c1); }))
4489               (if ((c2 & smask) != (c3 & smask))
4490                 { constant_boolean_node (cmp == NE_EXPR, type); })
4491               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4492                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4494 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4495         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4496    if the new mask might be further optimized.  */
4497 (for shift (lshift rshift)
4498  (simplify
4499   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4500            INTEGER_CST@2)
4501    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4502         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4503         && tree_fits_uhwi_p (@1)
4504         && tree_to_uhwi (@1) > 0
4505         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4506     (with
4507      {
4508        unsigned int shiftc = tree_to_uhwi (@1);
4509        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4510        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4511        tree shift_type = TREE_TYPE (@3);
4512        unsigned int prec;
4514        if (shift == LSHIFT_EXPR)
4515          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4516        else if (shift == RSHIFT_EXPR
4517                 && type_has_mode_precision_p (shift_type))
4518          {
4519            prec = TYPE_PRECISION (TREE_TYPE (@3));
4520            tree arg00 = @0;
4521            /* See if more bits can be proven as zero because of
4522               zero extension.  */
4523            if (@3 != @0
4524                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4525              {
4526                tree inner_type = TREE_TYPE (@0);
4527                if (type_has_mode_precision_p (inner_type)
4528                    && TYPE_PRECISION (inner_type) < prec)
4529                  {
4530                    prec = TYPE_PRECISION (inner_type);
4531                    /* See if we can shorten the right shift.  */
4532                    if (shiftc < prec)
4533                      shift_type = inner_type;
4534                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4535                       it into (X, 0) later on by making sure zerobits
4536                       is all ones.  */
4537                  }
4538              }
4539            zerobits = HOST_WIDE_INT_M1U;
4540            if (shiftc < prec)
4541              {
4542                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4543                zerobits <<= prec - shiftc;
4544              }
4545            /* For arithmetic shift if sign bit could be set, zerobits
4546               can contain actually sign bits, so no transformation is
4547               possible, unless MASK masks them all away.  In that
4548               case the shift needs to be converted into logical shift.  */
4549            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4550                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4551              {
4552                if ((mask & zerobits) == 0)
4553                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4554                else
4555                  zerobits = 0;
4556              }
4557          }
4558      }
4559      /* ((X << 16) & 0xff00) is (X, 0).  */
4560      (if ((mask & zerobits) == mask)
4561       { build_int_cst (type, 0); }
4562       (with { newmask = mask | zerobits; }
4563        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4564         (with
4565          {
4566            /* Only do the transformation if NEWMASK is some integer
4567               mode's mask.  */
4568            for (prec = BITS_PER_UNIT;
4569                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4570              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4571                break;
4572          }
4573          (if (prec < HOST_BITS_PER_WIDE_INT
4574               || newmask == HOST_WIDE_INT_M1U)
4575           (with
4576            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4577            (if (!tree_int_cst_equal (newmaskt, @2))
4578             (if (shift_type != TREE_TYPE (@3))
4579              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4580              (bit_and @4 { newmaskt; })))))))))))))
4582 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4583 (for cmp (ne eq)
4584        icmp (eq ne)
4585  (simplify
4586   (cmp
4587    (bit_and
4588     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4589   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4590    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4591                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4593 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4594    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4595 (for shift (lshift rshift)
4596  (for bit_op (bit_and bit_xor bit_ior)
4597   (simplify
4598    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4599    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4600     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4601      (if (mask)
4602       (bit_op (shift (convert @0) @1) { mask; })))))))
4604 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4605 (simplify
4606  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4607   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4608        && (element_precision (TREE_TYPE (@0))
4609            <= element_precision (TREE_TYPE (@1))
4610            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4611    (with
4612     { tree shift_type = TREE_TYPE (@0); }
4613      (convert (rshift (convert:shift_type @1) @2)))))
4615 /* ~(~X >>r Y) -> X >>r Y
4616    ~(~X <<r Y) -> X <<r Y */
4617 (for rotate (lrotate rrotate)
4618  (simplify
4619   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4620    (if ((element_precision (TREE_TYPE (@0))
4621          <= element_precision (TREE_TYPE (@1))
4622          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4623         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4624             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4625     (with
4626      { tree rotate_type = TREE_TYPE (@0); }
4627       (convert (rotate (convert:rotate_type @1) @2))))))
4629 (for cmp (eq ne)
4630  (for rotate (lrotate rrotate)
4631       invrot (rrotate lrotate)
4632   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4633   (simplify
4634    (cmp (rotate @1 @0) (rotate @2 @0))
4635    (cmp @1 @2))
4636   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4637   (simplify
4638    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4639    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4640   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4641   (simplify
4642    (cmp (rotate @0 @1) INTEGER_CST@2)
4643     (if (integer_zerop (@2) || integer_all_onesp (@2))
4644      (cmp @0 @2)))))
4646 /* Narrow a lshift by constant.  */
4647 (simplify
4648  (convert (lshift:s@0 @1 INTEGER_CST@2))
4649  (if (INTEGRAL_TYPE_P (type)
4650       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4651       && !integer_zerop (@2)
4652       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4653   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4654        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4655    (lshift (convert @1) @2)
4656    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4657     { build_zero_cst (type); }))))
4659 /* Simplifications of conversions.  */
4661 /* Basic strip-useless-type-conversions / strip_nops.  */
4662 (for cvt (convert view_convert float fix_trunc)
4663  (simplify
4664   (cvt @0)
4665   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4666        || (GENERIC && type == TREE_TYPE (@0)))
4667    @0)))
4669 /* Contract view-conversions.  */
4670 (simplify
4671   (view_convert (view_convert @0))
4672   (view_convert @0))
4674 /* For integral conversions with the same precision or pointer
4675    conversions use a NOP_EXPR instead.  */
4676 (simplify
4677   (view_convert @0)
4678   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4679        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4680        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4681    (convert @0)))
4683 /* Strip inner integral conversions that do not change precision or size, or
4684    zero-extend while keeping the same size (for bool-to-char).  */
4685 (simplify
4686   (view_convert (convert@0 @1))
4687   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4688        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4689        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4690        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4691            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4692                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4693    (view_convert @1)))
4695 /* Simplify a view-converted empty or single-element constructor.  */
4696 (simplify
4697   (view_convert CONSTRUCTOR@0)
4698   (with
4699    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4700                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4701    (switch
4702     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4703      { build_zero_cst (type); })
4704     (if (CONSTRUCTOR_NELTS (ctor) == 1
4705          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4706          && operand_equal_p (TYPE_SIZE (type),
4707                              TYPE_SIZE (TREE_TYPE
4708                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4709      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4711 /* Re-association barriers around constants and other re-association
4712    barriers can be removed.  */
4713 (simplify
4714  (paren CONSTANT_CLASS_P@0)
4715  @0)
4716 (simplify
4717  (paren (paren@1 @0))
4718  @1)
4720 /* Handle cases of two conversions in a row.  */
4721 (for ocvt (convert float fix_trunc)
4722  (for icvt (convert float)
4723   (simplify
4724    (ocvt (icvt@1 @0))
4725    (with
4726     {
4727       tree inside_type = TREE_TYPE (@0);
4728       tree inter_type = TREE_TYPE (@1);
4729       int inside_int = INTEGRAL_TYPE_P (inside_type);
4730       int inside_ptr = POINTER_TYPE_P (inside_type);
4731       int inside_float = FLOAT_TYPE_P (inside_type);
4732       int inside_vec = VECTOR_TYPE_P (inside_type);
4733       unsigned int inside_prec = element_precision (inside_type);
4734       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4735       int inter_int = INTEGRAL_TYPE_P (inter_type);
4736       int inter_ptr = POINTER_TYPE_P (inter_type);
4737       int inter_float = FLOAT_TYPE_P (inter_type);
4738       int inter_vec = VECTOR_TYPE_P (inter_type);
4739       unsigned int inter_prec = element_precision (inter_type);
4740       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4741       int final_int = INTEGRAL_TYPE_P (type);
4742       int final_ptr = POINTER_TYPE_P (type);
4743       int final_float = FLOAT_TYPE_P (type);
4744       int final_vec = VECTOR_TYPE_P (type);
4745       unsigned int final_prec = element_precision (type);
4746       int final_unsignedp = TYPE_UNSIGNED (type);
4747     }
4748    (switch
4749     /* In addition to the cases of two conversions in a row
4750        handled below, if we are converting something to its own
4751        type via an object of identical or wider precision, neither
4752        conversion is needed.  */
4753     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4754           || (GENERIC
4755               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4756          && (((inter_int || inter_ptr) && final_int)
4757              || (inter_float && final_float))
4758          && inter_prec >= final_prec)
4759      (ocvt @0))
4761     /* Likewise, if the intermediate and initial types are either both
4762        float or both integer, we don't need the middle conversion if the
4763        former is wider than the latter and doesn't change the signedness
4764        (for integers).  Avoid this if the final type is a pointer since
4765        then we sometimes need the middle conversion.  */
4766     (if (((inter_int && inside_int) || (inter_float && inside_float))
4767          && (final_int || final_float)
4768          && inter_prec >= inside_prec
4769          && (inter_float || inter_unsignedp == inside_unsignedp))
4770      (ocvt @0))
4772     /* If we have a sign-extension of a zero-extended value, we can
4773        replace that by a single zero-extension.  Likewise if the
4774        final conversion does not change precision we can drop the
4775        intermediate conversion.  Similarly truncation of a sign-extension
4776        can be replaced by a single sign-extension.  */
4777     (if (inside_int && inter_int && final_int
4778          && ((inside_prec < inter_prec && inter_prec < final_prec
4779               && inside_unsignedp && !inter_unsignedp)
4780              || final_prec == inter_prec
4781              || (inside_prec < inter_prec && inter_prec > final_prec
4782                  && !inside_unsignedp && inter_unsignedp)))
4783      (ocvt @0))
4785     /* Two conversions in a row are not needed unless:
4786         - some conversion is floating-point (overstrict for now), or
4787         - some conversion is a vector (overstrict for now), or
4788         - the intermediate type is narrower than both initial and
4789           final, or
4790         - the intermediate type and innermost type differ in signedness,
4791           and the outermost type is wider than the intermediate, or
4792         - the initial type is a pointer type and the precisions of the
4793           intermediate and final types differ, or
4794         - the final type is a pointer type and the precisions of the
4795           initial and intermediate types differ.  */
4796     (if (! inside_float && ! inter_float && ! final_float
4797          && ! inside_vec && ! inter_vec && ! final_vec
4798          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4799          && ! (inside_int && inter_int
4800                && inter_unsignedp != inside_unsignedp
4801                && inter_prec < final_prec)
4802          && ((inter_unsignedp && inter_prec > inside_prec)
4803              == (final_unsignedp && final_prec > inter_prec))
4804          && ! (inside_ptr && inter_prec != final_prec)
4805          && ! (final_ptr && inside_prec != inter_prec))
4806      (ocvt @0))
4808    /* `(outer:M)(inter:N) a:O`
4809       can be converted to `(outer:M) a`
4810       if M <= O && N >= O. No matter what signedness of the casts,
4811       as the final is either a truncation from the original or just
4812       a sign change of the type. */
4813    (if (inside_int && inter_int && final_int
4814         && final_prec <= inside_prec
4815         && inter_prec >= inside_prec)
4816     (convert @0))
4818     /* A truncation to an unsigned type (a zero-extension) should be
4819        canonicalized as bitwise and of a mask.  */
4820     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4821          && final_int && inter_int && inside_int
4822          && final_prec == inside_prec
4823          && final_prec > inter_prec
4824          && inter_unsignedp)
4825      (convert (bit_and @0 { wide_int_to_tree
4826                               (inside_type,
4827                                wi::mask (inter_prec, false,
4828                                          TYPE_PRECISION (inside_type))); })))
4830     /* If we are converting an integer to a floating-point that can
4831        represent it exactly and back to an integer, we can skip the
4832        floating-point conversion.  */
4833     (if (GIMPLE /* PR66211 */
4834          && inside_int && inter_float && final_int &&
4835          (unsigned) significand_size (TYPE_MODE (inter_type))
4836          >= inside_prec - !inside_unsignedp)
4837      (convert @0)))))))
4839 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4840    float_type.  Only do the transformation if we do not need to preserve
4841    trapping behaviour, so require !flag_trapping_math. */
4842 #if GIMPLE
4843 (simplify
4844    (float (fix_trunc @0))
4845    (if (!flag_trapping_math
4846         && types_match (type, TREE_TYPE (@0))
4847         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4848                                           OPTIMIZE_FOR_BOTH))
4849       (IFN_TRUNC @0)))
4850 #endif
4852 /* If we have a narrowing conversion to an integral type that is fed by a
4853    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4854    masks off bits outside the final type (and nothing else).  */
4855 (simplify
4856   (convert (bit_and @0 INTEGER_CST@1))
4857   (if (INTEGRAL_TYPE_P (type)
4858        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4859        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4860        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4861                                                     TYPE_PRECISION (type)), 0))
4862    (convert @0)))
4865 /* (X /[ex] A) * A -> X.  */
4866 (simplify
4867   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4868   (convert @0))
4870 /* Simplify (A / B) * B + (A % B) -> A.  */
4871 (for div (trunc_div ceil_div floor_div round_div)
4872      mod (trunc_mod ceil_mod floor_mod round_mod)
4873   (simplify
4874    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4875    @0))
4877 /* x / y * y == x -> x % y == 0.  */
4878 (simplify
4879   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4880   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4881     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4883 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4884 (for op (plus minus)
4885  (simplify
4886   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4887   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4888        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4889    (with
4890      {
4891        wi::overflow_type overflow;
4892        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4893                                TYPE_SIGN (type), &overflow);
4894      }
4895      (if (types_match (type, TREE_TYPE (@2))
4896          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4897       (op @0 { wide_int_to_tree (type, mul); })
4898       (with { tree utype = unsigned_type_for (type); }
4899        (convert (op (convert:utype @0)
4900                     (mult (convert:utype @1) (convert:utype @2))))))))))
4902 /* Canonicalization of binary operations.  */
4904 /* Convert X + -C into X - C.  */
4905 (simplify
4906  (plus @0 REAL_CST@1)
4907  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4908   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4909    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4910     (minus @0 { tem; })))))
4912 /* Convert x+x into x*2.  */
4913 (simplify
4914  (plus @0 @0)
4915  (if (SCALAR_FLOAT_TYPE_P (type))
4916   (mult @0 { build_real (type, dconst2); })
4917   (if (INTEGRAL_TYPE_P (type))
4918    (mult @0 { build_int_cst (type, 2); }))))
4920 /* 0 - X  ->  -X.  */
4921 (simplify
4922  (minus integer_zerop @1)
4923  (negate @1))
4924 (simplify
4925  (pointer_diff integer_zerop @1)
4926  (negate (convert @1)))
4928 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4929    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4930    (-ARG1 + ARG0) reduces to -ARG1.  */
4931 (simplify
4932  (minus real_zerop@0 @1)
4933  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4934   (negate @1)))
4936 /* Transform x * -1 into -x.  */
4937 (simplify
4938  (mult @0 integer_minus_onep)
4939  (negate @0))
4941 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4942    signed overflow for CST != 0 && CST != -1.  */
4943 (simplify
4944  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4945  (if (TREE_CODE (@2) != INTEGER_CST
4946       && single_use (@3)
4947       && !integer_zerop (@1) && !integer_minus_onep (@1))
4948   (mult (mult @0 @2) @1)))
4950 /* True if we can easily extract the real and imaginary parts of a complex
4951    number.  */
4952 (match compositional_complex
4953  (convert? (complex @0 @1)))
4955 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4956 (simplify
4957  (complex (realpart @0) (imagpart @0))
4958  @0)
4959 (simplify
4960  (realpart (complex @0 @1))
4961  @0)
4962 (simplify
4963  (imagpart (complex @0 @1))
4964  @1)
4966 /* Sometimes we only care about half of a complex expression.  */
4967 (simplify
4968  (realpart (convert?:s (conj:s @0)))
4969  (convert (realpart @0)))
4970 (simplify
4971  (imagpart (convert?:s (conj:s @0)))
4972  (convert (negate (imagpart @0))))
4973 (for part (realpart imagpart)
4974  (for op (plus minus)
4975   (simplify
4976    (part (convert?:s@2 (op:s @0 @1)))
4977    (convert (op (part @0) (part @1))))))
4978 (simplify
4979  (realpart (convert?:s (CEXPI:s @0)))
4980  (convert (COS @0)))
4981 (simplify
4982  (imagpart (convert?:s (CEXPI:s @0)))
4983  (convert (SIN @0)))
4985 /* conj(conj(x)) -> x  */
4986 (simplify
4987  (conj (convert? (conj @0)))
4988  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4989   (convert @0)))
4991 /* conj({x,y}) -> {x,-y}  */
4992 (simplify
4993  (conj (convert?:s (complex:s @0 @1)))
4994  (with { tree itype = TREE_TYPE (type); }
4995   (complex (convert:itype @0) (negate (convert:itype @1)))))
4997 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4998 (for bswap (BSWAP)
4999  (simplify
5000   (bswap (bswap @0))
5001   @0)
5002  (simplify
5003   (bswap (bit_not (bswap @0)))
5004   (bit_not @0))
5005  (for bitop (bit_xor bit_ior bit_and)
5006   (simplify
5007    (bswap (bitop:c (bswap @0) @1))
5008    (bitop @0 (bswap @1))))
5009  (for cmp (eq ne)
5010   (simplify
5011    (cmp (bswap@2 @0) (bswap @1))
5012    (with { tree ctype = TREE_TYPE (@2); }
5013     (cmp (convert:ctype @0) (convert:ctype @1))))
5014   (simplify
5015    (cmp (bswap @0) INTEGER_CST@1)
5016    (with { tree ctype = TREE_TYPE (@1); }
5017     (cmp (convert:ctype @0) (bswap! @1)))))
5018  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
5019  (simplify
5020   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5021            INTEGER_CST@3)
5022    (if (BITS_PER_UNIT == 8
5023         && tree_fits_uhwi_p (@2)
5024         && tree_fits_uhwi_p (@3))
5025     (with
5026      {
5027       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5028       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5029       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5030       unsigned HOST_WIDE_INT lo = bits & 7;
5031       unsigned HOST_WIDE_INT hi = bits - lo;
5032      }
5033      (if (bits < prec
5034           && mask < (256u>>lo)
5035           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5036       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5037        (if (ns == 0)
5038         (bit_and (convert @1) @3)
5039         (with
5040          {
5041           tree utype = unsigned_type_for (TREE_TYPE (@1));
5042           tree nst = build_int_cst (integer_type_node, ns);
5043          }
5044          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5045  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
5046  (simplify
5047   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5048    (if (BITS_PER_UNIT == 8
5049         && CHAR_TYPE_SIZE == 8
5050         && tree_fits_uhwi_p (@1))
5051     (with
5052      {
5053       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5054       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5055       /* If the bswap was extended before the original shift, this
5056          byte (shift) has the sign of the extension, not the sign of
5057          the original shift.  */
5058       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5059      }
5060      /* Special case: logical right shift of sign-extended bswap.
5061         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5062      (if (TYPE_PRECISION (type) > prec
5063           && !TYPE_UNSIGNED (TREE_TYPE (@2))
5064           && TYPE_UNSIGNED (type)
5065           && bits < prec && bits + 8 >= prec)
5066       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5067        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5068       (if (bits + 8 == prec)
5069        (if (TYPE_UNSIGNED (st))
5070         (convert (convert:unsigned_char_type_node @0))
5071         (convert (convert:signed_char_type_node @0)))
5072        (if (bits < prec && bits + 8 > prec)
5073         (with 
5074          {
5075           tree nst = build_int_cst (integer_type_node, bits & 7);
5076           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5077                                        : signed_char_type_node;
5078          }
5079          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5080  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
5081  (simplify
5082   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5083    (if (BITS_PER_UNIT == 8
5084         && tree_fits_uhwi_p (@1)
5085         && tree_to_uhwi (@1) < 256)
5086     (with
5087      {
5088       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5089       tree utype = unsigned_type_for (TREE_TYPE (@0));
5090       tree nst = build_int_cst (integer_type_node, prec - 8);
5091      }
5092      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5095 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
5097 /* Simplify constant conditions.
5098    Only optimize constant conditions when the selected branch
5099    has the same type as the COND_EXPR.  This avoids optimizing
5100    away "c ? x : throw", where the throw has a void type.
5101    Note that we cannot throw away the fold-const.cc variant nor
5102    this one as we depend on doing this transform before possibly
5103    A ? B : B -> B triggers and the fold-const.cc one can optimize
5104    0 ? A : B to B even if A has side-effects.  Something
5105    genmatch cannot handle.  */
5106 (simplify
5107  (cond INTEGER_CST@0 @1 @2)
5108  (if (integer_zerop (@0))
5109   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5110    @2)
5111   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5112    @1)))
5113 (simplify
5114  (vec_cond VECTOR_CST@0 @1 @2)
5115  (if (integer_all_onesp (@0))
5116   @1
5117   (if (integer_zerop (@0))
5118    @2)))
5120 /* Sink unary operations to branches, but only if we do fold both.  */
5121 (for op (negate bit_not abs absu)
5122  (simplify
5123   (op (vec_cond:s @0 @1 @2))
5124   (vec_cond @0 (op! @1) (op! @2))))
5126 /* Sink unary conversions to branches, but only if we do fold both
5127    and the target's truth type is the same as we already have.  */
5128 (simplify
5129  (convert (vec_cond:s @0 @1 @2))
5130  (if (VECTOR_TYPE_P (type)
5131       && types_match (TREE_TYPE (@0), truth_type_for (type)))
5132   (vec_cond @0 (convert! @1) (convert! @2))))
5134 /* Likewise for view_convert of nop_conversions. */
5135 (simplify
5136  (view_convert (vec_cond:s @0 @1 @2))
5137  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5138       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5139                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5140       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5141   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5143 /* Sink binary operation to branches, but only if we can fold it.  */
5144 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5145          lshift rshift rdiv trunc_div ceil_div floor_div round_div
5146          trunc_mod ceil_mod floor_mod round_mod min max)
5147 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
5148  (simplify
5149   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5150   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5152 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
5153  (simplify
5154   (op (vec_cond:s @0 @1 @2) @3)
5155   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5156  (simplify
5157   (op @3 (vec_cond:s @0 @1 @2))
5158   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5160 #if GIMPLE
5161 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5162  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5163            INTEGER_CST@1)
5164  (with {
5165          int ibit = tree_log2 (@0);
5166          int ibit2 = tree_log2 (@1);
5167        }
5168   (if (ibit == ibit2
5169       && ibit >= 0
5170       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5172 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5173  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5174           INTEGER_CST@1)
5175  (with {
5176          int ibit = tree_log2 (@0);
5177          int ibit2 = tree_log2 (@1);
5178        }
5179   (if (ibit == ibit2
5180       && ibit >= 0
5181       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5183 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5184  (bit_and:c
5185   (convert1?@4
5186    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5187   (convert2? @0))
5188  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5190 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5191  (bit_and:c
5192   (convert1?@4
5193    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5194   (convert2? @0))
5195  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5197 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5198  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5199             INTEGER_CST@1)
5200  (with {
5201          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5202                                               TYPE_PRECISION(type)));
5203          int ibit2 = tree_log2 (@1);
5204        }
5205   (if (ibit == ibit2
5206       && ibit >= 0
5207       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5209 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5210  (bit_and@4
5211   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5212   INTEGER_CST@1)
5213  (with {
5214          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5215                                               TYPE_PRECISION(type)));
5216          int ibit2 = tree_log2 (@1);
5217        }
5218   (if (ibit == ibit2
5219       && ibit >= 0
5220       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5222 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5223  (bit_and:c
5224   (convert1?@3
5225    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5226   (convert2? @0))
5227  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5229 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5230  (bit_and:c
5231   (convert1?@3
5232    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5233   (convert2? @0))
5234   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5236 #endif
5238 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5239    Currently disabled after pass lvec because ARM understands
5240    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5241 #if GIMPLE
5242 /* These can only be done in gimple as fold likes to convert:
5243    (CMP) & N into (CMP) ? N : 0
5244    and we try to match the same pattern again and again. */
5245 (simplify
5246  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5247  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5248   (vec_cond (bit_and @0 @3) @1 @2)))
5249 (simplify
5250  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5251  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5252   (vec_cond (bit_ior @0 @3) @1 @2)))
5253 (simplify
5254  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5255  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5256   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5257 (simplify
5258  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5259  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5260   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5262 /*  ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5263     (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5264     component type of the outer vec_cond is greater equal the inner one.  */
5265 (for cmp (simple_comparison)
5266  (simplify
5267   (vec_cond
5268     (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5269                                     integer_all_onesp
5270                                     integer_zerop))
5271           integer_zerop) @2 @3)
5272   (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5273        && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5274        && !TYPE_UNSIGNED (TREE_TYPE (@5))
5275        && VECTOR_TYPE_P (TREE_TYPE (@6))
5276        && VECTOR_TYPE_P (type)
5277        && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5278                            TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5279        && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5280    (with { tree vtype = TREE_TYPE (@6);}
5281      (view_convert:type
5282        (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5284 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5285 (simplify
5286  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5287  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5288   (vec_cond (bit_and @0 @1) @2 @3)))
5289 (simplify
5290  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5291  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5292   (vec_cond (bit_ior @0 @1) @2 @3)))
5293 (simplify
5294  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5295  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5296   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5297 (simplify
5298  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5299  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5300   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5301 #endif
5303 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5304    types are compatible.  */
5305 (simplify
5306  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5307  (if (VECTOR_BOOLEAN_TYPE_P (type)
5308       && types_match (type, TREE_TYPE (@0)))
5309   (if (integer_zerop (@1) && integer_all_onesp (@2))
5310    (bit_not @0)
5311    (if (integer_all_onesp (@1) && integer_zerop (@2))
5312     @0))))
5314 /* A few simplifications of "a ? CST1 : CST2". */
5315 /* NOTE: Only do this on gimple as the if-chain-to-switch
5316    optimization depends on the gimple to have if statements in it. */
5317 #if GIMPLE
5318 (simplify
5319  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5320  (switch
5321   (if (integer_zerop (@2))
5322    (switch
5323     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5324     (if (integer_onep (@1))
5325      (convert (convert:boolean_type_node @0)))
5326     /* a ? -1 : 0 -> -a.  */
5327     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5328      (if (TYPE_PRECISION (type) == 1)
5329       /* For signed 1-bit precision just cast bool to the type.  */
5330       (convert (convert:boolean_type_node @0))
5331       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5332        (with {
5333           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5334                                                       TYPE_UNSIGNED (type));
5335         }
5336         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5337        (negate (convert:type (convert:boolean_type_node @0))))))
5338     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5339     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5340      (with {
5341        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5342       }
5343       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5344   (if (integer_zerop (@1))
5345    (switch
5346     /* a ? 0 : 1 -> !a.  */
5347     (if (integer_onep (@2))
5348      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5349     /* a ? 0 : -1 -> -(!a).  */
5350     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5351      (if (TYPE_PRECISION (type) == 1)
5352       /* For signed 1-bit precision just cast bool to the type.  */
5353       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5354       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5355        (with {
5356           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5357                                                       TYPE_UNSIGNED (type));
5358         }
5359         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5360                                                 { boolean_true_node; })))))
5361        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5362                                       { boolean_true_node; }))))))
5363     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5364     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5365      (with {
5366        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5367       }
5368       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5369                                 { boolean_true_node; })) { shift; })))))))
5371 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5372    for unsigned types. */
5373 (simplify
5374  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5375  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5376       && bitwise_equal_p (@0, @2))
5377   (convert (eq @0 @1))
5381 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5382    for unsigned types. */
5383 (simplify
5384  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5385  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5386       && bitwise_equal_p (@0, @2))
5387   (convert (eq @0 @1))
5391 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5392    on the first bit of the CST.  */
5393 (simplify
5394  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5395  (if ((wi::to_wide (@1) & 1) != 0)
5396   @2
5397   { build_zero_cst (type); }))
5399 /* Optimize
5400    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5401    x_5 == cstN ? cst4 : cst3
5402    # op is == or != and N is 1 or 2
5403    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5404    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5405    of cst3 and cst4 is smaller.
5406    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5407 (for eqne (ne eq)
5408  (simplify
5409   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5410   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5411        && INTEGRAL_TYPE_P (type)
5412        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5413            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5414    (with {
5415      value_range r;
5416      get_range_query (cfun)->range_of_expr (r, @0);
5417      if (r.undefined_p ())
5418        r.set_varying (TREE_TYPE (@0));
5420      wide_int min = r.lower_bound ();
5421      wide_int max = r.upper_bound ();
5422     }
5423     (if (min + 1 == max
5424          && (wi::to_wide (@1) == min
5425              || wi::to_wide (@1) == max))
5426      (with {
5427        tree arg0 = @2, arg1 = @3;
5428        tree type1;
5429        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5430          std::swap (arg0, arg1);
5431        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5432          type1 = TREE_TYPE (@0);
5433        else
5434          type1 = type;
5435        auto prec = TYPE_PRECISION (type1);
5436        auto unsign = TYPE_UNSIGNED (type1);
5437        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5438         type1 = build_nonstandard_integer_type (prec, unsign);
5439        min = wide_int::from (min, prec,
5440                              TYPE_SIGN (TREE_TYPE (@0)));
5441        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5442                                     TYPE_SIGN (type));
5443        enum tree_code code;
5444        wi::overflow_type ovf;
5445        if (tree_int_cst_lt (arg0, arg1))
5446          {
5447            code = PLUS_EXPR;
5448            a -= min;
5449            if (!unsign)
5450              {
5451                /* lhs is known to be in range [min, min+1] and we want to add a
5452                   to it.  Check if that operation can overflow for those 2 values
5453                   and if yes, force unsigned type.  */
5454                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5455                if (ovf)
5456                  type1 = unsigned_type_for (type1);
5457              }
5458          }
5459        else
5460          {
5461            code = MINUS_EXPR;
5462            a += min;
5463            if (!unsign)
5464              {
5465                /* lhs is known to be in range [min, min+1] and we want to subtract
5466                   it from a.  Check if that operation can overflow for those 2
5467                   values and if yes, force unsigned type.  */
5468                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5469                if (ovf)
5470                 type1 = unsigned_type_for (type1);
5471              }
5472          }
5473        tree arg = wide_int_to_tree (type1, a);
5474       }
5475       (if (code == PLUS_EXPR)
5476        (convert (plus (convert:type1 @0) { arg; }))
5477        (convert (minus { arg; } (convert:type1 @0))))))))))
5478 #endif
5480 (simplify
5481  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5482  (if (INTEGRAL_TYPE_P (type)
5483       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5484   (cond @1 (convert @2) (convert @3))))
5486 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5487    be extended.  */
5488 /* This pattern implements two kinds simplification:
5490    Case 1)
5491    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5492      1) Conversions are type widening from smaller type.
5493      2) Const c1 equals to c2 after canonicalizing comparison.
5494      3) Comparison has tree code LT, LE, GT or GE.
5495    This specific pattern is needed when (cmp (convert x) c) may not
5496    be simplified by comparison patterns because of multiple uses of
5497    x.  It also makes sense here because simplifying across multiple
5498    referred var is always benefitial for complicated cases.
5500    Case 2)
5501    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5502 (for cmp (lt le gt ge eq ne)
5503  (simplify
5504   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5505   (with
5506    {
5507      tree from_type = TREE_TYPE (@1);
5508      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5509      enum tree_code code = ERROR_MARK;
5511      if (INTEGRAL_TYPE_P (from_type)
5512          && int_fits_type_p (@2, from_type)
5513          && (types_match (c1_type, from_type)
5514              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5515                  && (TYPE_UNSIGNED (from_type)
5516                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5517          && (types_match (c2_type, from_type)
5518              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5519                  && (TYPE_UNSIGNED (from_type)
5520                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5521        {
5522          if (cmp != EQ_EXPR)
5523            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5524          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5525          else if (int_fits_type_p (@3, from_type))
5526            code = EQ_EXPR;
5527        }
5528    }
5529    (if (code == MAX_EXPR)
5530     (convert (max @1 (convert @2)))
5531     (if (code == MIN_EXPR)
5532      (convert (min @1 (convert @2)))
5533      (if (code == EQ_EXPR)
5534       (convert (cond (eq @1 (convert @3))
5535                      (convert:from_type @3) (convert:from_type @2)))))))))
5537 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5539      1) OP is PLUS or MINUS.
5540      2) CMP is LT, LE, GT or GE.
5541      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5543    This pattern also handles special cases like:
5545      A) Operand x is a unsigned to signed type conversion and c1 is
5546         integer zero.  In this case,
5547           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5548           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5549      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5550         check equality for (c1+1) and (c1-1) by adjusting comparison
5551         code.
5553    TODO: Though signed type is handled by this pattern, it cannot be
5554    simplified at the moment because C standard requires additional
5555    type promotion.  In order to match&simplify it here, the IR needs
5556    to be cleaned up by other optimizers, i.e, VRP.  */
5557 (for op (plus minus)
5558  (for cmp (lt le gt ge)
5559   (simplify
5560    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5561    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5562     (if (types_match (from_type, to_type)
5563          /* Check if it is special case A).  */
5564          || (TYPE_UNSIGNED (from_type)
5565              && !TYPE_UNSIGNED (to_type)
5566              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5567              && integer_zerop (@1)
5568              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5569      (with
5570       {
5571         wi::overflow_type overflow = wi::OVF_NONE;
5572         enum tree_code code, cmp_code = cmp;
5573         wide_int real_c1;
5574         wide_int c1 = wi::to_wide (@1);
5575         wide_int c2 = wi::to_wide (@2);
5576         wide_int c3 = wi::to_wide (@3);
5577         signop sgn = TYPE_SIGN (from_type);
5579         /* Handle special case A), given x of unsigned type:
5580             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5581             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5582         if (!types_match (from_type, to_type))
5583           {
5584             if (cmp_code == LT_EXPR)
5585               cmp_code = GT_EXPR;
5586             if (cmp_code == GE_EXPR)
5587               cmp_code = LE_EXPR;
5588             c1 = wi::max_value (to_type);
5589           }
5590         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5591            compute (c3 op' c2) and check if it equals to c1 with op' being
5592            the inverted operator of op.  Make sure overflow doesn't happen
5593            if it is undefined.  */
5594         if (op == PLUS_EXPR)
5595           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5596         else
5597           real_c1 = wi::add (c3, c2, sgn, &overflow);
5599         code = cmp_code;
5600         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5601           {
5602             /* Check if c1 equals to real_c1.  Boundary condition is handled
5603                by adjusting comparison operation if necessary.  */
5604             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5605                 && !overflow)
5606               {
5607                 /* X <= Y - 1 equals to X < Y.  */
5608                 if (cmp_code == LE_EXPR)
5609                   code = LT_EXPR;
5610                 /* X > Y - 1 equals to X >= Y.  */
5611                 if (cmp_code == GT_EXPR)
5612                   code = GE_EXPR;
5613               }
5614             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5615                 && !overflow)
5616               {
5617                 /* X < Y + 1 equals to X <= Y.  */
5618                 if (cmp_code == LT_EXPR)
5619                   code = LE_EXPR;
5620                 /* X >= Y + 1 equals to X > Y.  */
5621                 if (cmp_code == GE_EXPR)
5622                   code = GT_EXPR;
5623               }
5624             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5625               {
5626                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5627                   code = MIN_EXPR;
5628                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5629                   code = MAX_EXPR;
5630               }
5631           }
5632       }
5633       (if (code == MAX_EXPR)
5634        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5635            { wide_int_to_tree (from_type, c2); })
5636        (if (code == MIN_EXPR)
5637         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5638             { wide_int_to_tree (from_type, c2); })))))))))
5640 #if GIMPLE
5641 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5642    in fold_cond_expr_with_comparison for GENERIC folding with
5643    some extra constraints.  */
5644 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5645  (simplify
5646   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5647         (convert3? @0) (convert4? @1))
5648   (if (!HONOR_SIGNED_ZEROS (type)
5649        && (/* Allow widening conversions of the compare operands as data.  */
5650            (INTEGRAL_TYPE_P (type)
5651             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5652             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5653             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5654             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5655            /* Or sign conversions for the comparison.  */
5656            || (types_match (type, TREE_TYPE (@0))
5657                && types_match (type, TREE_TYPE (@1)))))
5658    (switch
5659     (if (cmp == EQ_EXPR)
5660      (if (VECTOR_TYPE_P (type))
5661       (view_convert @c1)
5662       (convert @c1)))
5663     (if (cmp == NE_EXPR)
5664      (if (VECTOR_TYPE_P (type))
5665       (view_convert @c0)
5666       (convert @c0)))
5667     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5668      (if (!HONOR_NANS (type))
5669       (if (VECTOR_TYPE_P (type))
5670        (view_convert (min @c0 @c1))
5671        (convert (min @c0 @c1)))))
5672     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5673      (if (!HONOR_NANS (type))
5674       (if (VECTOR_TYPE_P (type))
5675        (view_convert (max @c0 @c1))
5676        (convert (max @c0 @c1)))))
5677     (if (cmp == UNEQ_EXPR)
5678      (if (!HONOR_NANS (type))
5679       (if (VECTOR_TYPE_P (type))
5680        (view_convert @c1)
5681        (convert @c1))))
5682     (if (cmp == LTGT_EXPR)
5683      (if (!HONOR_NANS (type))
5684       (if (VECTOR_TYPE_P (type))
5685        (view_convert @c0)
5686        (convert @c0))))))))
5688 /* This is for VEC_COND_EXPR
5689    Optimize A < B ? A : B to MIN (A, B)
5690             A > B ? A : B to MAX (A, B).  */
5691 (for cmp (lt le ungt unge gt ge unlt unle)
5692      minmax (min min min min max max max max)
5693      MINMAX (MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR)
5694  (simplify
5695   (vec_cond (cmp @0 @1) @0 @1)
5696    (if (VECTOR_INTEGER_TYPE_P (type)
5697        && target_supports_op_p (type, MINMAX, optab_vector))
5698     (minmax @0 @1))))
5700 (for cmp (lt le ungt unge gt ge unlt unle)
5701      minmax (max max max max min min min min)
5702      MINMAX (MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR)
5703  (simplify
5704   (vec_cond (cmp @0 @1) @1 @0)
5705    (if (VECTOR_INTEGER_TYPE_P (type)
5706        && target_supports_op_p (type, MINMAX, optab_vector))
5707     (minmax @0 @1))))
5708 #endif
5710 (for cnd (cond vec_cond)
5711  /* (a != b) ? (a - b) : 0 -> (a - b) */
5712  (simplify
5713   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5714   @2)
5715  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5716  (simplify
5717   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5718   @2)
5719  /* (a != b) ? (a & b) : a -> (a & b) */
5720  /* (a != b) ? (a | b) : a -> (a | b) */
5721  /* (a != b) ? min(a,b) : a -> min(a,b) */
5722  /* (a != b) ? max(a,b) : a -> max(a,b) */
5723  (for op (bit_and bit_ior min max)
5724   (simplify
5725    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5726    @2))
5727  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5728  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5729  (for op (mult plus)
5730   (simplify
5731    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5732    (if (ANY_INTEGRAL_TYPE_P (type))
5733     @2)))
5734  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5735  (simplify
5736   (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5737   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5738    @2))
5741 /* These was part of minmax phiopt.  */
5742 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5743    to minmax<min/max<a, b>, c> */
5744 (for minmax (min max)
5745  (for cmp (lt le gt ge ne)
5746   (simplify
5747    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5748    (with
5749     {
5750       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5751     }
5752     (if (code == MIN_EXPR)
5753      (minmax (min @1 @2) @4)
5754      (if (code == MAX_EXPR)
5755       (minmax (max @1 @2) @4)))))))
5757 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5758 (for cmp    (gt  ge  lt  le)
5759      minmax (min min max max)
5760  (simplify
5761   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5762    (with
5763     {
5764       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5765     }
5766     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5767          && code == MIN_EXPR
5768          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5769      (min @2 @4)
5770      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5771           && code == MAX_EXPR
5772           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5773       (max @2 @4))))))
5775 #if GIMPLE
5776 /* These patterns should be after min/max detection as simplifications
5777    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5778    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5779    Even without those, reaching min/max/and/ior faster is better.  */
5780 (simplify
5781  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5782  (switch
5783   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5784   (if (integer_zerop (@2))
5785    (bit_and (convert @0) @1))
5786   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5787   (if (integer_zerop (@1))
5788    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5789   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5790   (if (integer_onep (@1))
5791    (bit_ior (convert @0) @2))
5792   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5793   (if (integer_onep (@2))
5794    (bit_ior (bit_xor (convert @0) @2) @1))
5797 #endif
5799 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5800 (simplify
5801  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5802  (if (!TYPE_SATURATING (type)
5803       && (TYPE_OVERFLOW_WRAPS (type)
5804           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5805       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5806   @3))
5808 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5809 (simplify
5810  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5811  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5812   @3))
5814 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5815 (for op (abs absu)
5816  (simplify
5817   (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5818   (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5819    (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5820     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5821      (convert (absu:utype @0)))
5822     @3))))
5824 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5825    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5826 (simplify
5827  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5828  (if (TYPE_UNSIGNED (type))
5829   (cond (ge @0 @1) (negate @0) @2)))
5831 (for cnd (cond vec_cond)
5832  /* A ? B : (A ? X : C) -> A ? B : C.  */
5833  (simplify
5834   (cnd @0 (cnd @0 @1 @2) @3)
5835   (cnd @0 @1 @3))
5836  (simplify
5837   (cnd @0 @1 (cnd @0 @2 @3))
5838   (cnd @0 @1 @3))
5839  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5840  /* ???  This matches embedded conditions open-coded because genmatch
5841     would generate matching code for conditions in separate stmts only.
5842     The following is still important to merge then and else arm cases
5843     from if-conversion.  */
5844  (simplify
5845   (cnd @0 @1 (cnd @2 @3 @4))
5846   (if (inverse_conditions_p (@0, @2))
5847    (cnd @0 @1 @3)))
5848  (simplify
5849   (cnd @0 (cnd @1 @2 @3) @4)
5850   (if (inverse_conditions_p (@0, @1))
5851    (cnd @0 @3 @4)))
5853  /* A ? B : B -> B.  */
5854  (simplify
5855   (cnd @0 @1 @1)
5856   @1)
5858  /* !A ? B : C -> A ? C : B.  */
5859  (simplify
5860   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5861   (cnd @0 @2 @1)))
5863 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5865    None of these transformations work for modes with signed
5866    zeros.  If A is +/-0, the first two transformations will
5867    change the sign of the result (from +0 to -0, or vice
5868    versa).  The last four will fix the sign of the result,
5869    even though the original expressions could be positive or
5870    negative, depending on the sign of A.
5872    Note that all these transformations are correct if A is
5873    NaN, since the two alternatives (A and -A) are also NaNs.  */
5875 (for cnd (cond vec_cond)
5876  /* A == 0 ? A : -A    same as -A */
5877  (for cmp (eq uneq)
5878   (simplify
5879    (cnd (cmp @0 zerop) @2 (negate@1 @2))
5880     (if (!HONOR_SIGNED_ZEROS (type)
5881          && bitwise_equal_p (@0, @2))
5882      @1))
5883   (simplify
5884    (cnd (cmp @0 zerop) zerop (negate@1 @2))
5885     (if (!HONOR_SIGNED_ZEROS (type)
5886          && bitwise_equal_p (@0, @2))
5887      @1))
5889  /* A != 0 ? A : -A    same as A */
5890  (for cmp (ne ltgt)
5891   (simplify
5892    (cnd (cmp @0 zerop) @1 (negate @1))
5893     (if (!HONOR_SIGNED_ZEROS (type)
5894          && bitwise_equal_p (@0, @1))
5895      @1))
5896   (simplify
5897    (cnd (cmp @0 zerop) @1 integer_zerop)
5898     (if (!HONOR_SIGNED_ZEROS (type)
5899          && bitwise_equal_p (@0, @1))
5900      @1))
5902  /* A >=/> 0 ? A : -A    same as abs (A) */
5903  (for cmp (ge gt)
5904   (simplify
5905    (cnd (cmp @0 zerop) @1 (negate @1))
5906     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5907          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5908          && bitwise_equal_p (@0, @1))
5909      (if (TYPE_UNSIGNED (type))
5910       (absu:type @0)
5911       (abs @0)))))
5912  /* A <=/< 0 ? A : -A    same as -abs (A) */
5913  (for cmp (le lt)
5914   (simplify
5915    (cnd (cmp @0 zerop) @1 (negate @1))
5916     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5917          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5918          && bitwise_equal_p (@0, @1))
5919      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5920            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5921           || TYPE_UNSIGNED (type))
5922       (with {
5923         tree utype = unsigned_type_for (TREE_TYPE(@0));
5924        }
5925        (convert (negate (absu:utype @0))))
5926        (negate (abs @0)))))
5929  /* (A - B) == 0 ? (A - B) : (B - A)    same as (B - A) */
5930  (for cmp (eq uneq)
5931   (simplify
5932    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5933    (if (!HONOR_SIGNED_ZEROS (type))
5934     @3))
5935   (simplify
5936    (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5937    @3)
5939  /* (A - B) != 0 ? (A - B) : (B - A)    same as (A - B) */
5940  (for cmp (ne ltgt)
5941   (simplify
5942    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5943    (if (!HONOR_SIGNED_ZEROS (type))
5944     @0))
5945   (simplify
5946    (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5947    @0)
5949  /* (A - B) >=/> 0 ? (A - B) : (B - A)    same as abs (A - B) */
5950  (for cmp (ge gt)
5951   (simplify
5952    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5953    (if (!HONOR_SIGNED_ZEROS (type)
5954         && !TYPE_UNSIGNED (type))
5955     (abs @0))))
5956  /* (A - B) <=/< 0 ? (A - B) : (B - A)    same as -abs (A - B) */
5957  (for cmp (le lt)
5958   (simplify
5959    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5960    (if (!HONOR_SIGNED_ZEROS (type)
5961         && !TYPE_UNSIGNED (type))
5962     (if (ANY_INTEGRAL_TYPE_P (type)
5963          && !TYPE_OVERFLOW_WRAPS (type))
5964      (with {
5965         tree utype = unsigned_type_for (type);
5966       }
5967       (convert (negate (absu:utype @0))))
5968       (negate (abs @0)))))
5972 /* -(type)!A -> (type)A - 1.  */
5973 (simplify
5974  (negate (convert?:s (logical_inverted_value:s @0)))
5975  (if (INTEGRAL_TYPE_P (type)
5976       && TREE_CODE (type) != BOOLEAN_TYPE
5977       && TYPE_PRECISION (type) > 1
5978       && TREE_CODE (@0) == SSA_NAME
5979       && ssa_name_has_boolean_range (@0))
5980   (plus (convert:type @0) { build_all_ones_cst (type); })))
5982 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5983    return all -1 or all 0 results.  */
5984 /* ??? We could instead convert all instances of the vec_cond to negate,
5985    but that isn't necessarily a win on its own.  */
5986 (simplify
5987  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5988  (if (VECTOR_TYPE_P (type)
5989       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5990                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5991       && (TYPE_MODE (TREE_TYPE (type))
5992           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5993   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5995 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5996 (simplify
5997  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5998  (if (VECTOR_TYPE_P (type)
5999       && known_eq (TYPE_VECTOR_SUBPARTS (type),
6000                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6001       && (TYPE_MODE (TREE_TYPE (type))
6002           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6003   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6006 /* Simplifications of comparisons.  */
6008 /* See if we can reduce the magnitude of a constant involved in a
6009    comparison by changing the comparison code.  This is a canonicalization
6010    formerly done by maybe_canonicalize_comparison_1.  */
6011 (for cmp  (le gt)
6012      acmp (lt ge)
6013  (simplify
6014   (cmp @0 uniform_integer_cst_p@1)
6015   (with { tree cst = uniform_integer_cst_p (@1); }
6016    (if (tree_int_cst_sgn (cst) == -1)
6017      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6018                                    wide_int_to_tree (TREE_TYPE (cst),
6019                                                      wi::to_wide (cst)
6020                                                      + 1)); })))))
6021 (for cmp  (ge lt)
6022      acmp (gt le)
6023  (simplify
6024   (cmp @0 uniform_integer_cst_p@1)
6025   (with { tree cst = uniform_integer_cst_p (@1); }
6026    (if (tree_int_cst_sgn (cst) == 1)
6027     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6028                                   wide_int_to_tree (TREE_TYPE (cst),
6029                                   wi::to_wide (cst) - 1)); })))))
6031 /* We can simplify a logical negation of a comparison to the
6032    inverted comparison.  As we cannot compute an expression
6033    operator using invert_tree_comparison we have to simulate
6034    that with expression code iteration.  */
6035 (for cmp (tcc_comparison)
6036      icmp (inverted_tcc_comparison)
6037      ncmp (inverted_tcc_comparison_with_nans)
6038  /* Ideally we'd like to combine the following two patterns
6039     and handle some more cases by using
6040       (logical_inverted_value (cmp @0 @1))
6041     here but for that genmatch would need to "inline" that.
6042     For now implement what forward_propagate_comparison did.  */
6043  (simplify
6044   (bit_not (cmp @0 @1))
6045   (if (VECTOR_TYPE_P (type)
6046        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6047    /* Comparison inversion may be impossible for trapping math,
6048       invert_tree_comparison will tell us.  But we can't use
6049       a computed operator in the replacement tree thus we have
6050       to play the trick below.  */
6051    (with { enum tree_code ic = invert_tree_comparison
6052              (cmp, HONOR_NANS (@0)); }
6053     (if (ic == icmp)
6054      (icmp @0 @1)
6055      (if (ic == ncmp)
6056       (ncmp @0 @1))))))
6057  (simplify
6058   (bit_xor (cmp @0 @1) integer_truep)
6059   (with { enum tree_code ic = invert_tree_comparison
6060             (cmp, HONOR_NANS (@0)); }
6061    (if (ic == icmp)
6062     (icmp @0 @1)
6063     (if (ic == ncmp)
6064      (ncmp @0 @1)))))
6065  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
6066  (simplify
6067   (ne (cmp@2 @0 @1) integer_zerop)
6068   (if (types_match (type, TREE_TYPE (@2)))
6069    (cmp @0 @1)))
6070  (simplify
6071   (eq (cmp@2 @0 @1) integer_truep)
6072   (if (types_match (type, TREE_TYPE (@2)))
6073    (cmp @0 @1)))
6074  (simplify
6075   (ne (cmp@2 @0 @1) integer_truep)
6076   (if (types_match (type, TREE_TYPE (@2)))
6077    (with { enum tree_code ic = invert_tree_comparison
6078              (cmp, HONOR_NANS (@0)); }
6079     (if (ic == icmp)
6080      (icmp @0 @1)
6081      (if (ic == ncmp)
6082       (ncmp @0 @1))))))
6083  (simplify
6084   (eq (cmp@2 @0 @1) integer_zerop)
6085   (if (types_match (type, TREE_TYPE (@2)))
6086    (with { enum tree_code ic = invert_tree_comparison
6087              (cmp, HONOR_NANS (@0)); }
6088     (if (ic == icmp)
6089      (icmp @0 @1)
6090      (if (ic == ncmp)
6091       (ncmp @0 @1)))))))
6093 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6094    ??? The transformation is valid for the other operators if overflow
6095    is undefined for the type, but performing it here badly interacts
6096    with the transformation in fold_cond_expr_with_comparison which
6097    attempts to synthetize ABS_EXPR.  */
6098 (for cmp (eq ne)
6099  (for sub (minus pointer_diff)
6100   (simplify
6101    (cmp (sub@2 @0 @1) integer_zerop)
6102    (if (single_use (@2))
6103     (cmp @0 @1)))))
6105 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6106    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
6107 (for cmp (lt ge)
6108  (simplify
6109   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6110    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6111         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6112         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6113     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6114 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6115    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
6116 (simplify
6117  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6118   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6119        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6120        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6121    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6123 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6124    signed arithmetic case.  That form is created by the compiler
6125    often enough for folding it to be of value.  One example is in
6126    computing loop trip counts after Operator Strength Reduction.  */
6127 (for cmp (simple_comparison)
6128      scmp (swapped_simple_comparison)
6129  (simplify
6130   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6131   /* Handle unfolded multiplication by zero.  */
6132   (if (integer_zerop (@1))
6133    (cmp @1 @2)
6134    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6135         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6136         && single_use (@3))
6137     /* If @1 is negative we swap the sense of the comparison.  */
6138     (if (tree_int_cst_sgn (@1) < 0)
6139      (scmp @0 @2)
6140      (cmp @0 @2))))))
6142 /* For integral types with undefined overflow fold
6143    x * C1 == C2 into x == C2 / C1 or false.
6144    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6145    Z / 2^n Z.  */
6146 (for cmp (eq ne)
6147  (simplify
6148   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6149   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6150        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6151        && wi::to_wide (@1) != 0)
6152    (with { widest_int quot; }
6153     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6154                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
6155      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6156      { constant_boolean_node (cmp == NE_EXPR, type); }))
6157    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6158         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6159         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6160     (cmp @0
6161      {
6162        tree itype = TREE_TYPE (@0);
6163        int p = TYPE_PRECISION (itype);
6164        wide_int m = wi::one (p + 1) << p;
6165        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6166        wide_int i = wide_int::from (wi::mod_inv (a, m),
6167                                     p, TYPE_SIGN (itype));
6168        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6169      })))))
6171 /* Simplify comparison of something with itself.  For IEEE
6172    floating-point, we can only do some of these simplifications.  */
6173 (for cmp (eq ge le)
6174  (simplify
6175   (cmp @0 @0)
6176   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6177        || ! tree_expr_maybe_nan_p (@0))
6178    { constant_boolean_node (true, type); }
6179    (if (cmp != EQ_EXPR
6180         /* With -ftrapping-math conversion to EQ loses an exception.  */
6181         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6182             || ! flag_trapping_math))
6183     (eq @0 @0)))))
6184 (for cmp (ne gt lt)
6185  (simplify
6186   (cmp @0 @0)
6187   (if (cmp != NE_EXPR
6188        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6189        || ! tree_expr_maybe_nan_p (@0))
6190    { constant_boolean_node (false, type); })))
6191 (for cmp (unle unge uneq)
6192  (simplify
6193   (cmp @0 @0)
6194   { constant_boolean_node (true, type); }))
6195 (for cmp (unlt ungt)
6196  (simplify
6197   (cmp @0 @0)
6198   (unordered @0 @0)))
6199 (simplify
6200  (ltgt @0 @0)
6201  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6202   { constant_boolean_node (false, type); }))
6204 /* x == ~x -> false */
6205 /* x != ~x -> true */
6206 (for cmp (eq ne)
6207  (simplify
6208   (cmp:c @0 (bit_not @0))
6209   { constant_boolean_node (cmp == NE_EXPR, type); }))
6211 /* Fold ~X op ~Y as Y op X.  */
6212 (for cmp (simple_comparison)
6213  (simplify
6214   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6215   (if (single_use (@2) && single_use (@3))
6216    (with { tree otype = TREE_TYPE (@4); }
6217     (cmp (convert:otype @1) (convert:otype @0))))))
6219 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
6220 (for cmp (simple_comparison)
6221      scmp (swapped_simple_comparison)
6222  (simplify
6223   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6224   (if (single_use (@2)
6225        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6226    (with { tree otype = TREE_TYPE (@1); }
6227     (scmp (convert:otype @0) (bit_not @1))))))
6229 (for cmp (simple_comparison)
6230  (simplify
6231   (cmp @0 REAL_CST@1)
6232   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
6233   (switch
6234    /* a CMP (-0) -> a CMP 0  */
6235    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6236     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6237    /* (-0) CMP b -> 0 CMP b.  */
6238    (if (TREE_CODE (@0) == REAL_CST
6239         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6240     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6241    /* x != NaN is always true, other ops are always false.  */
6242    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6243         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6244         && !tree_expr_signaling_nan_p (@1)
6245         && !tree_expr_maybe_signaling_nan_p (@0))
6246     { constant_boolean_node (cmp == NE_EXPR, type); })
6247    /* NaN != y is always true, other ops are always false.  */
6248    (if (TREE_CODE (@0) == REAL_CST
6249         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6250         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6251         && !tree_expr_signaling_nan_p (@0)
6252         && !tree_expr_signaling_nan_p (@1))
6253     { constant_boolean_node (cmp == NE_EXPR, type); })
6254    /* Fold comparisons against infinity.  */
6255    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6256         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6257     (with
6258      {
6259        REAL_VALUE_TYPE max;
6260        enum tree_code code = cmp;
6261        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6262        if (neg)
6263          code = swap_tree_comparison (code);
6264      }
6265      (switch
6266       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6267       (if (code == GT_EXPR
6268            && !(HONOR_NANS (@0) && flag_trapping_math))
6269        { constant_boolean_node (false, type); })
6270       (if (code == LE_EXPR)
6271        /* x <= +Inf is always true, if we don't care about NaNs.  */
6272        (if (! HONOR_NANS (@0))
6273         { constant_boolean_node (true, type); }
6274         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6275            an "invalid" exception.  */
6276         (if (!flag_trapping_math)
6277          (eq @0 @0))))
6278       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6279          for == this introduces an exception for x a NaN.  */
6280       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6281            || code == GE_EXPR)
6282        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6283         (if (neg)
6284          (lt @0 { build_real (TREE_TYPE (@0), max); })
6285          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6286       /* x < +Inf is always equal to x <= DBL_MAX.  */
6287       (if (code == LT_EXPR)
6288        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6289         (if (neg)
6290          (ge @0 { build_real (TREE_TYPE (@0), max); })
6291          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6292       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6293          an exception for x a NaN so use an unordered comparison.  */
6294       (if (code == NE_EXPR)
6295        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6296         (if (! HONOR_NANS (@0))
6297          (if (neg)
6298           (ge @0 { build_real (TREE_TYPE (@0), max); })
6299           (le @0 { build_real (TREE_TYPE (@0), max); }))
6300          (if (neg)
6301           (unge @0 { build_real (TREE_TYPE (@0), max); })
6302           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6304  /* If this is a comparison of a real constant with a PLUS_EXPR
6305     or a MINUS_EXPR of a real constant, we can convert it into a
6306     comparison with a revised real constant as long as no overflow
6307     occurs when unsafe_math_optimizations are enabled.  */
6308  (if (flag_unsafe_math_optimizations)
6309   (for op (plus minus)
6310    (simplify
6311     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6312     (with
6313      {
6314        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6315                                TREE_TYPE (@1), @2, @1);
6316      }
6317      (if (tem && !TREE_OVERFLOW (tem))
6318       (cmp @0 { tem; }))))))
6320  /* Likewise, we can simplify a comparison of a real constant with
6321     a MINUS_EXPR whose first operand is also a real constant, i.e.
6322     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6323     floating-point types only if -fassociative-math is set.  */
6324  (if (flag_associative_math)
6325   (simplify
6326    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6327    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6328     (if (tem && !TREE_OVERFLOW (tem))
6329      (cmp { tem; } @1)))))
6331  /* Fold comparisons against built-in math functions.  */
6332  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6333   (for sq (SQRT)
6334    (simplify
6335     (cmp (sq @0) REAL_CST@1)
6336     (switch
6337      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6338       (switch
6339        /* sqrt(x) < y is always false, if y is negative.  */
6340        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6341         { constant_boolean_node (false, type); })
6342        /* sqrt(x) > y is always true, if y is negative and we
6343           don't care about NaNs, i.e. negative values of x.  */
6344        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6345         { constant_boolean_node (true, type); })
6346        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6347        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6348      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6349       (switch
6350        /* sqrt(x) < 0 is always false.  */
6351        (if (cmp == LT_EXPR)
6352         { constant_boolean_node (false, type); })
6353        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6354        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6355         { constant_boolean_node (true, type); })
6356        /* sqrt(x) <= 0 -> x == 0.  */
6357        (if (cmp == LE_EXPR)
6358         (eq @0 @1))
6359        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6360           == or !=.  In the last case:
6362             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6364           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6365           the results for other x follow from natural arithmetic.  */
6366        (cmp @0 @1)))
6367      (if ((cmp == LT_EXPR
6368            || cmp == LE_EXPR
6369            || cmp == GT_EXPR
6370            || cmp == GE_EXPR)
6371           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6372           /* Give up for -frounding-math.  */
6373           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6374       (with
6375        {
6376          REAL_VALUE_TYPE c2;
6377          enum tree_code ncmp = cmp;
6378          const real_format *fmt
6379            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6380          real_arithmetic (&c2, MULT_EXPR,
6381                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6382          real_convert (&c2, fmt, &c2);
6383          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6384             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6385          if (!REAL_VALUE_ISINF (c2))
6386            {
6387              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6388                                         build_real (TREE_TYPE (@0), c2));
6389              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6390                ncmp = ERROR_MARK;
6391              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6392                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6393                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6394              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6395                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6396                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6397              else
6398                {
6399                  /* With rounding to even, sqrt of up to 3 different values
6400                     gives the same normal result, so in some cases c2 needs
6401                     to be adjusted.  */
6402                  REAL_VALUE_TYPE c2alt, tow;
6403                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6404                    tow = dconst0;
6405                  else
6406                    tow = dconstinf;
6407                  real_nextafter (&c2alt, fmt, &c2, &tow);
6408                  real_convert (&c2alt, fmt, &c2alt);
6409                  if (REAL_VALUE_ISINF (c2alt))
6410                    ncmp = ERROR_MARK;
6411                  else
6412                    {
6413                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6414                                            build_real (TREE_TYPE (@0), c2alt));
6415                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6416                        ncmp = ERROR_MARK;
6417                      else if (real_equal (&TREE_REAL_CST (c3),
6418                                           &TREE_REAL_CST (@1)))
6419                        c2 = c2alt;
6420                    }
6421                }
6422            }
6423        }
6424        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6425         (if (REAL_VALUE_ISINF (c2))
6426          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6427          (if (HONOR_INFINITIES (@0))
6428           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6429           { constant_boolean_node (false, type); })
6430          /* sqrt(x) > c is the same as x > c*c.  */
6431          (if (ncmp != ERROR_MARK)
6432           (if (ncmp == GE_EXPR)
6433            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6434            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6435         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6436         (if (REAL_VALUE_ISINF (c2))
6437          (switch
6438           /* sqrt(x) < y is always true, when y is a very large
6439              value and we don't care about NaNs or Infinities.  */
6440           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6441            { constant_boolean_node (true, type); })
6442           /* sqrt(x) < y is x != +Inf when y is very large and we
6443              don't care about NaNs.  */
6444           (if (! HONOR_NANS (@0))
6445            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6446           /* sqrt(x) < y is x >= 0 when y is very large and we
6447              don't care about Infinities.  */
6448           (if (! HONOR_INFINITIES (@0))
6449            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6450           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6451           (if (GENERIC)
6452            (truth_andif
6453             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6454             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6455          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6456          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6457           (if (ncmp == LT_EXPR)
6458            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6459            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6460           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6461           (if (ncmp != ERROR_MARK && GENERIC)
6462            (if (ncmp == LT_EXPR)
6463             (truth_andif
6464              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6465              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6466             (truth_andif
6467              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6468              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6469    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6470    (simplify
6471     (cmp (sq @0) (sq @1))
6472       (if (! HONOR_NANS (@0))
6473         (cmp @0 @1))))))
6475 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6476 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6477      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6478  (simplify
6479   (cmp (float@0 @1) (float @2))
6480    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6481         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6482     (with
6483      {
6484        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6485        tree type1 = TREE_TYPE (@1);
6486        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6487        tree type2 = TREE_TYPE (@2);
6488        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6489      }
6490      (if (fmt.can_represent_integral_type_p (type1)
6491           && fmt.can_represent_integral_type_p (type2))
6492       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6493        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6494        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6495             && type1_signed_p >= type2_signed_p)
6496         (icmp @1 (convert @2))
6497         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6498              && type1_signed_p <= type2_signed_p)
6499          (icmp (convert:type2 @1) @2)
6500          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6501               && type1_signed_p == type2_signed_p)
6502           (icmp @1 @2))))))))))
6504 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6505 (for cmp  (lt le eq ne ge gt)
6506      icmp (le le eq ne ge ge)
6507  (simplify
6508   (cmp (float @0) REAL_CST@1)
6509    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6510         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6511     (with
6512      {
6513        tree itype = TREE_TYPE (@0);
6514        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6515        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6516        /* Be careful to preserve any potential exceptions due to
6517           NaNs.  qNaNs are ok in == or != context.
6518           TODO: relax under -fno-trapping-math or
6519           -fno-signaling-nans.  */
6520        bool exception_p
6521          = real_isnan (cst) && (cst->signalling
6522                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6523      }
6524      /* TODO: allow non-fitting itype and SNaNs when
6525         -fno-trapping-math.  */
6526      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6527       (with
6528        {
6529          signop isign = TYPE_SIGN (itype);
6530          REAL_VALUE_TYPE imin, imax;
6531          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6532          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6534          REAL_VALUE_TYPE icst;
6535          if (cmp == GT_EXPR || cmp == GE_EXPR)
6536            real_ceil (&icst, fmt, cst);
6537          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6538            real_floor (&icst, fmt, cst);
6539          else
6540            real_trunc (&icst, fmt, cst);
6542          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6544          bool overflow_p = false;
6545          wide_int icst_val
6546            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6547        }
6548        (switch
6549         /* Optimize cases when CST is outside of ITYPE's range.  */
6550         (if (real_compare (LT_EXPR, cst, &imin))
6551          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6552                                   type); })
6553         (if (real_compare (GT_EXPR, cst, &imax))
6554          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6555                                   type); })
6556         /* Remove cast if CST is an integer representable by ITYPE.  */
6557         (if (cst_int_p)
6558          (cmp @0 { gcc_assert (!overflow_p);
6559                    wide_int_to_tree (itype, icst_val); })
6560         )
6561         /* When CST is fractional, optimize
6562             (FTYPE) N == CST -> 0
6563             (FTYPE) N != CST -> 1.  */
6564         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6565          { constant_boolean_node (cmp == NE_EXPR, type); })
6566         /* Otherwise replace with sensible integer constant.  */
6567         (with
6568          {
6569            gcc_checking_assert (!overflow_p);
6570          }
6571          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6573 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6574 (for cmp (eq ne)
6575  (simplify
6576   (cmp (exact_div @0 @1) INTEGER_CST@2)
6577   (if (!integer_zerop (@1))
6578    (if (wi::to_wide (@2) == 0)
6579     (cmp @0 @2)
6580     (if (TREE_CODE (@1) == INTEGER_CST)
6581      (with
6582       {
6583         wi::overflow_type ovf;
6584         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6585                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6586       }
6587       (if (ovf)
6588        { constant_boolean_node (cmp == NE_EXPR, type); }
6589        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6590 (for cmp (lt le gt ge)
6591  (simplify
6592   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6593   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6594    (with
6595     {
6596       wi::overflow_type ovf;
6597       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6598                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6599     }
6600     (if (ovf)
6601      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6602                                         TYPE_SIGN (TREE_TYPE (@2)))
6603                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6604      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6606 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6608    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6609    For large C (more than min/B+2^size), this is also true, with the
6610    multiplication computed modulo 2^size.
6611    For intermediate C, this just tests the sign of A.  */
6612 (for cmp  (lt le gt ge)
6613      cmp2 (ge ge lt lt)
6614  (simplify
6615   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6616   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6617        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6618        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6619    (with
6620     {
6621       tree utype = TREE_TYPE (@2);
6622       wide_int denom = wi::to_wide (@1);
6623       wide_int right = wi::to_wide (@2);
6624       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6625       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6626       bool small = wi::leu_p (right, smax);
6627       bool large = wi::geu_p (right, smin);
6628     }
6629     (if (small || large)
6630      (cmp (convert:utype @0) (mult @2 (convert @1)))
6631      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6633 /* Unordered tests if either argument is a NaN.  */
6634 (simplify
6635  (bit_ior (unordered @0 @0) (unordered @1 @1))
6636  (if (types_match (@0, @1))
6637   (unordered @0 @1)))
6638 (simplify
6639  (bit_and (ordered @0 @0) (ordered @1 @1))
6640  (if (types_match (@0, @1))
6641   (ordered @0 @1)))
6642 (simplify
6643  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6644  @2)
6645 (simplify
6646  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6647  @2)
6649 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6650    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6652    Note that comparisons
6653      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6654      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6655    will be canonicalized to above so there's no need to
6656    consider them here.
6657  */
6659 (for cmp (le gt)
6660      eqcmp (eq ne)
6661  (simplify
6662   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6663   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6664    (with
6665     {
6666      tree ty = TREE_TYPE (@0);
6667      unsigned prec = TYPE_PRECISION (ty);
6668      wide_int mask = wi::to_wide (@2, prec);
6669      wide_int rhs = wi::to_wide (@3, prec);
6670      signop sgn = TYPE_SIGN (ty);
6671     }
6672     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6673          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6674       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6675              { build_zero_cst (ty); }))))))
6677 /* -A CMP -B -> B CMP A.  */
6678 (for cmp (tcc_comparison)
6679      scmp (swapped_tcc_comparison)
6680  (simplify
6681   (cmp (negate @0) (negate @1))
6682   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6683        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6684            && (cmp == EQ_EXPR
6685                || cmp == NE_EXPR
6686                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6687    (scmp @0 @1)))
6688  (simplify
6689   (cmp (negate @0) CONSTANT_CLASS_P@1)
6690   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6691        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6692            && (cmp == EQ_EXPR
6693                || cmp == NE_EXPR
6694                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6695    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6696     (if (tem && !TREE_OVERFLOW (tem))
6697      (scmp @0 { tem; }))))))
6699 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6700 (for op (abs absu)
6701  (for eqne (eq ne)
6702   (simplify
6703    (eqne (op @0) zerop@1)
6704    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6706 /* From fold_sign_changed_comparison and fold_widened_comparison.
6707    FIXME: the lack of symmetry is disturbing.  */
6708 (for cmp (simple_comparison)
6709  (simplify
6710   (cmp (convert@0 @00) (convert?@1 @10))
6711   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6712        /* Disable this optimization if we're casting a function pointer
6713           type on targets that require function pointer canonicalization.  */
6714        && !(targetm.have_canonicalize_funcptr_for_compare ()
6715             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6716                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6717                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6718                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6719        && single_use (@0))
6720    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6721         && (TREE_CODE (@10) == INTEGER_CST
6722             || @1 != @10)
6723         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6724             || cmp == NE_EXPR
6725             || cmp == EQ_EXPR)
6726         && !POINTER_TYPE_P (TREE_TYPE (@00))
6727         /* (int)bool:32 != (int)uint is not the same as
6728            bool:32 != (bool:32)uint since boolean types only have two valid
6729            values independent of their precision.  */
6730         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6731             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6732     /* ???  The special-casing of INTEGER_CST conversion was in the original
6733        code and here to avoid a spurious overflow flag on the resulting
6734        constant which fold_convert produces.  */
6735     (if (TREE_CODE (@1) == INTEGER_CST)
6736      (cmp @00 { force_fit_type (TREE_TYPE (@00),
6737                                 wide_int::from (wi::to_wide (@1),
6738                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6739                                                      TYPE_PRECISION (TREE_TYPE (@00))),
6740                                                 TYPE_SIGN (TREE_TYPE (@1))),
6741                                 0, TREE_OVERFLOW (@1)); })
6742      (cmp @00 (convert @1)))
6744     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6745      /* If possible, express the comparison in the shorter mode.  */
6746      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6747            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6748            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6749                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6750           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6751               || ((TYPE_PRECISION (TREE_TYPE (@00))
6752                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6753                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6754                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6755               || (TREE_CODE (@10) == INTEGER_CST
6756                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6757                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6758       (cmp @00 (convert @10))
6759       (if (TREE_CODE (@10) == INTEGER_CST
6760            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6761            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6762        (with
6763         {
6764           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6765           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6766           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6767           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6768         }
6769         (if (above || below)
6770          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6771           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6772           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6773            { constant_boolean_node (above ? true : false, type); }
6774            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6775             { constant_boolean_node (above ? false : true, type); })))))))))
6776    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6777    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6778         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6779             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6780         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6781             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6782     (with
6783      {
6784        tree type1 = TREE_TYPE (@10);
6785        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6786          {
6787            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6788            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6789                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6790              type1 = float_type_node;
6791            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6792                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6793              type1 = double_type_node;
6794          }
6795        tree newtype
6796          = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6797             ? TREE_TYPE (@00) : type1);
6798      }
6799      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype)
6800           && is_truth_type_for (newtype, type))
6801       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6804 (for cmp (eq ne)
6805  (simplify
6806   /* SSA names are canonicalized to 2nd place.  */
6807   (cmp addr@0 SSA_NAME@1)
6808   (with
6809    {
6810      poly_int64 off; tree base;
6811      tree addr = (TREE_CODE (@0) == SSA_NAME
6812                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6813    }
6814    /* A local variable can never be pointed to by
6815       the default SSA name of an incoming parameter.  */
6816    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6817         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6818         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6819         && TREE_CODE (base) == VAR_DECL
6820         && auto_var_in_fn_p (base, current_function_decl))
6821     (if (cmp == NE_EXPR)
6822      { constant_boolean_node (true, type); }
6823      { constant_boolean_node (false, type); })
6824     /* If the address is based on @1 decide using the offset.  */
6825     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6826          && TREE_CODE (base) == MEM_REF
6827          && TREE_OPERAND (base, 0) == @1)
6828      (with { off += mem_ref_offset (base).force_shwi (); }
6829       (if (known_ne (off, 0))
6830        { constant_boolean_node (cmp == NE_EXPR, type); }
6831        (if (known_eq (off, 0))
6832         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6834 /* Equality compare simplifications from fold_binary  */
6835 (for cmp (eq ne)
6837  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6838     Similarly for NE_EXPR.  */
6839  (simplify
6840   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6841   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6842        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6843    { constant_boolean_node (cmp == NE_EXPR, type); }))
6845  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6846  (simplify
6847   (cmp (bit_xor @0 @1) integer_zerop)
6848   (cmp @0 @1))
6850  /* (X ^ Y) == Y becomes X == 0.
6851     Likewise (X ^ Y) == X becomes Y == 0.  */
6852  (simplify
6853   (cmp:c (bit_xor:c @0 @1) @0)
6854   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6856  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6857  (simplify
6858   (cmp:c (bit_and:c @0 @1) @0)
6859   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6860  (simplify
6861   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6862   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6863        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6864        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6865        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6866        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6867        && !wi::neg_p (wi::to_wide (@1)))
6868    (cmp (bit_and @0 (convert (bit_not @1)))
6869         { build_zero_cst (TREE_TYPE (@0)); })))
6871  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6872  (simplify
6873   (cmp:c (bit_ior:c @0 @1) @1)
6874   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6876  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6877  (simplify
6878   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6879   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6880    (cmp @0 (bit_xor @1 (convert @2)))))
6882  (simplify
6883   (cmp (nop_convert? @0) integer_zerop)
6884   (if (tree_expr_nonzero_p (@0))
6885    { constant_boolean_node (cmp == NE_EXPR, type); }))
6887  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6888  (simplify
6889   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6890   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6892 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6893    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6894    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6895    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6896 (for cmp (eq ne)
6897      ncmp (ge lt)
6898  (for sgncmp (ge lt)
6899   (simplify
6900    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6901    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6902         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6903         && types_match (@0, @1))
6904     (ncmp (bit_xor @0 @1) @2)))))
6905 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6906    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6907 (for cmp (eq ne)
6908      ncmp (lt ge)
6909  (simplify
6910   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6911    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6912         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6913         && types_match (@0, @1))
6914     (ncmp (bit_xor @0 @1) @2))))
6916 /* If we have (A & C) == C where C is a power of 2, convert this into
6917    (A & C) != 0.  Similarly for NE_EXPR.  */
6918 (for cmp (eq ne)
6919      icmp (ne eq)
6920  (simplify
6921   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6922   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6924 #if GIMPLE
6925 /* From fold_binary_op_with_conditional_arg handle the case of
6926    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6927    compares simplify.  */
6928 (for cmp (simple_comparison)
6929  (simplify
6930   (cmp:c (cond @0 @1 @2) @3)
6931   /* Do not move possibly trapping operations into the conditional as this
6932      pessimizes code and causes gimplification issues when applied late.  */
6933   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6934        || !operation_could_trap_p (cmp, true, false, @3))
6935    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6936 #endif
6938 (for cmp (ge lt)
6939 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6940 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6941  (simplify
6942   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6943    (if (INTEGRAL_TYPE_P (type)
6944         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6945         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6946         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6947     (with
6948      {
6949        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6950      }
6951     (if (cmp == LT_EXPR)
6952      (bit_xor (convert (rshift @0 {shifter;})) @1)
6953      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6954 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6955 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6956  (simplify
6957   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6958    (if (INTEGRAL_TYPE_P (type)
6959         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6960         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6961         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6962     (with
6963      {
6964        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6965      }
6966     (if (cmp == GE_EXPR)
6967      (bit_xor (convert (rshift @0 {shifter;})) @1)
6968      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6970 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6971    convert this into a shift followed by ANDing with D.  */
6972 (simplify
6973  (cond
6974   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6975   INTEGER_CST@2 integer_zerop)
6976  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6977   (with {
6978      int shift = (wi::exact_log2 (wi::to_wide (@2))
6979                   - wi::exact_log2 (wi::to_wide (@1)));
6980    }
6981    (if (shift > 0)
6982     (bit_and
6983      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6984     (bit_and
6985      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6986      @2)))))
6988 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6989    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6990 (for cmp (eq ne)
6991      ncmp (ge lt)
6992  (simplify
6993   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6994   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6995        && type_has_mode_precision_p (TREE_TYPE (@0))
6996        && element_precision (@2) >= element_precision (@0)
6997        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6998    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6999     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
7001 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
7002    this into a right shift or sign extension followed by ANDing with C.  */
7003 (simplify
7004  (cond
7005   (lt @0 integer_zerop)
7006   INTEGER_CST@1 integer_zerop)
7007  (if (integer_pow2p (@1)
7008       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
7009   (with {
7010     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
7011    }
7012    (if (shift >= 0)
7013     (bit_and
7014      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
7015      @1)
7016     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
7017        sign extension followed by AND with C will achieve the effect.  */
7018     (bit_and (convert @0) @1)))))
7020 /* When the addresses are not directly of decls compare base and offset.
7021    This implements some remaining parts of fold_comparison address
7022    comparisons but still no complete part of it.  Still it is good
7023    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
7024 (for cmp (simple_comparison)
7025  (simplify
7026   (cmp (convert1?@2 addr@0) (convert2? addr@1))
7027   (with
7028    {
7029      poly_int64 off0, off1;
7030      tree base0, base1;
7031      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
7032                                   off0, off1, GENERIC);
7033    }
7034    (if (equal == 1)
7035     (switch
7036      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7037       { constant_boolean_node (known_eq (off0, off1), type); })
7038      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7039       { constant_boolean_node (known_ne (off0, off1), type); })
7040      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7041       { constant_boolean_node (known_lt (off0, off1), type); })
7042      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7043       { constant_boolean_node (known_le (off0, off1), type); })
7044      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7045       { constant_boolean_node (known_ge (off0, off1), type); })
7046      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7047       { constant_boolean_node (known_gt (off0, off1), type); }))
7048     (if (equal == 0)
7049      (switch
7050       (if (cmp == EQ_EXPR)
7051        { constant_boolean_node (false, type); })
7052       (if (cmp == NE_EXPR)
7053        { constant_boolean_node (true, type); })))))))
7055 #if GIMPLE
7056 /* a?~t:t -> (-(a))^t */
7057 (simplify
7058  (cond @0 @1 @2)
7059  (with { bool wascmp; }
7060   (if (INTEGRAL_TYPE_P (type)
7061        && bitwise_inverted_equal_p (@1, @2, wascmp)
7062        && (!wascmp || TYPE_PRECISION (type) == 1))
7063    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7064         || TYPE_PRECISION (type) == 1)
7065     (bit_xor (convert:type @0) @2)
7066     (bit_xor (negate (convert:type @0)) @2)))))
7067 #endif
7069 /* Simplify pointer equality compares using PTA.  */
7070 (for neeq (ne eq)
7071  (simplify
7072   (neeq @0 @1)
7073   (if (POINTER_TYPE_P (TREE_TYPE (@0))
7074        && ptrs_compare_unequal (@0, @1))
7075    { constant_boolean_node (neeq != EQ_EXPR, type); })))
7077 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7078    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7079    Disable the transform if either operand is pointer to function.
7080    This broke pr22051-2.c for arm where function pointer
7081    canonicalizaion is not wanted.  */
7083 (for cmp (ne eq)
7084  (simplify
7085   (cmp (convert @0) INTEGER_CST@1)
7086   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7087          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7088          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7089          /* Don't perform this optimization in GENERIC if @0 has reference
7090             type when sanitizing.  See PR101210.  */
7091          && !(GENERIC
7092               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7093               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7094         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7095             && POINTER_TYPE_P (TREE_TYPE (@1))
7096             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7097        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7098    (cmp @0 (convert @1)))))
7100 /* Non-equality compare simplifications from fold_binary  */
7101 (for cmp (lt gt le ge)
7102  /* Comparisons with the highest or lowest possible integer of
7103     the specified precision will have known values.  */
7104  (simplify
7105   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7106   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7107         || POINTER_TYPE_P (TREE_TYPE (@1))
7108         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7109        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7110    (with
7111     {
7112       tree cst = uniform_integer_cst_p (@1);
7113       tree arg1_type = TREE_TYPE (cst);
7114       unsigned int prec = TYPE_PRECISION (arg1_type);
7115       wide_int max = wi::max_value (arg1_type);
7116       wide_int signed_max = wi::max_value (prec, SIGNED);
7117       wide_int min = wi::min_value (arg1_type);
7118     }
7119     (switch
7120      (if (wi::to_wide (cst) == max)
7121       (switch
7122        (if (cmp == GT_EXPR)
7123         { constant_boolean_node (false, type); })
7124        (if (cmp == GE_EXPR)
7125         (eq @2 @1))
7126        (if (cmp == LE_EXPR)
7127         { constant_boolean_node (true, type); })
7128        (if (cmp == LT_EXPR)
7129         (ne @2 @1))))
7130      (if (wi::to_wide (cst) == min)
7131       (switch
7132        (if (cmp == LT_EXPR)
7133         { constant_boolean_node (false, type); })
7134        (if (cmp == LE_EXPR)
7135         (eq @2 @1))
7136        (if (cmp == GE_EXPR)
7137         { constant_boolean_node (true, type); })
7138        (if (cmp == GT_EXPR)
7139         (ne @2 @1))))
7140      (if (wi::to_wide (cst) == max - 1)
7141       (switch
7142        (if (cmp == GT_EXPR)
7143         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7144                                     wide_int_to_tree (TREE_TYPE (cst),
7145                                                       wi::to_wide (cst)
7146                                                       + 1)); }))
7147        (if (cmp == LE_EXPR)
7148         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7149                                     wide_int_to_tree (TREE_TYPE (cst),
7150                                                       wi::to_wide (cst)
7151                                                       + 1)); }))))
7152      (if (wi::to_wide (cst) == min + 1)
7153       (switch
7154        (if (cmp == GE_EXPR)
7155         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7156                                     wide_int_to_tree (TREE_TYPE (cst),
7157                                                       wi::to_wide (cst)
7158                                                       - 1)); }))
7159        (if (cmp == LT_EXPR)
7160         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7161                                     wide_int_to_tree (TREE_TYPE (cst),
7162                                                       wi::to_wide (cst)
7163                                                       - 1)); }))))
7164      (if (wi::to_wide (cst) == signed_max
7165           && TYPE_UNSIGNED (arg1_type)
7166           && TYPE_MODE (arg1_type) != BLKmode
7167           /* We will flip the signedness of the comparison operator
7168              associated with the mode of @1, so the sign bit is
7169              specified by this mode.  Check that @1 is the signed
7170              max associated with this sign bit.  */
7171           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7172           /* signed_type does not work on pointer types.  */
7173           && INTEGRAL_TYPE_P (arg1_type))
7174       /* The following case also applies to X < signed_max+1
7175          and X >= signed_max+1 because previous transformations.  */
7176       (if (cmp == LE_EXPR || cmp == GT_EXPR)
7177        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7178         (switch
7179          (if (cst == @1 && cmp == LE_EXPR)
7180           (ge (convert:st @0) { build_zero_cst (st); }))
7181          (if (cst == @1 && cmp == GT_EXPR)
7182           (lt (convert:st @0) { build_zero_cst (st); }))
7183          (if (cmp == LE_EXPR)
7184           (ge (view_convert:st @0) { build_zero_cst (st); }))
7185          (if (cmp == GT_EXPR)
7186           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7188 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
7189 (simplify
7190  (lt:c @0 (convert (ne @0 integer_zerop)))
7191  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7192   { constant_boolean_node (false, type); }))
7194 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7195 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7196 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7197 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7198 (for outer (ne eq)
7199  (for inner (ne eq)
7200   (simplify
7201    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7202    (with {
7203      bool cst1 = integer_onep (@1);
7204      bool cst0 = integer_zerop (@1);
7205      bool innereq = inner == EQ_EXPR;
7206      bool outereq = outer == EQ_EXPR;
7207     }
7208     (switch
7209      (if (innereq ? cst0 : cst1)
7210       { constant_boolean_node (!outereq, type); })
7211      (if (innereq ? cst1 : cst0)
7212       (with {
7213         tree utype = unsigned_type_for (TREE_TYPE (@0));
7214         tree ucst1 = build_one_cst (utype);
7215        }
7216        (if (!outereq)
7217         (gt (convert:utype @0) { ucst1; })
7218         (le (convert:utype @0) { ucst1; })
7219        )
7220       )
7221      )
7222      (with {
7223        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7224       }
7225       (if (outereq)
7226        (eq @0 { value; })
7227        (ne @0 { value; })
7228       )
7229      )
7230     )
7231    )
7232   )
7236 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7237  /* If the second operand is NaN, the result is constant.  */
7238  (simplify
7239   (cmp @0 REAL_CST@1)
7240   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7241        && (cmp != LTGT_EXPR || ! flag_trapping_math))
7242    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7243                             ? false : true, type); })))
7245 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
7246 (simplify
7247   (unordered @0 @1)
7248   (switch
7249     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7250         { constant_boolean_node (true, type); })
7251     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7252         { constant_boolean_node (false, type); })))
7254 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7255 (simplify
7256   (ordered @0 @1)
7257   (switch
7258     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7259         { constant_boolean_node (false, type); })
7260     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7261         { constant_boolean_node (true, type); })))
7263 /* bool_var != 0 becomes bool_var.  */
7264 (simplify
7265  (ne @0 integer_zerop)
7266  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7267       && types_match (type, TREE_TYPE (@0)))
7268   (non_lvalue @0)))
7269 /* bool_var == 1 becomes bool_var.  */
7270 (simplify
7271  (eq @0 integer_onep)
7272  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7273       && types_match (type, TREE_TYPE (@0)))
7274   (non_lvalue @0)))
7275 /* Do not handle
7276    bool_var == 0 becomes !bool_var or
7277    bool_var != 1 becomes !bool_var
7278    here because that only is good in assignment context as long
7279    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7280    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7281    clearly less optimal and which we'll transform again in forwprop.  */
7283 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7284    where ~Y + 1 == pow2 and Z = ~Y.  */
7285 (for cst (VECTOR_CST INTEGER_CST)
7286  (for cmp (eq ne)
7287       icmp (le gt)
7288   (simplify
7289    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7290     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7291      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7292       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7293                          ? optab_vector : optab_default;
7294               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7295        (if (target_supports_op_p (utype, icmp, optab)
7296             || (optimize_vectors_before_lowering_p ()
7297                 && (!target_supports_op_p (type, cmp, optab)
7298                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7299         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7300          (icmp @0 { csts; })
7301          (icmp (view_convert:utype @0) { csts; })))))))))
7303 /* When one argument is a constant, overflow detection can be simplified.
7304    Currently restricted to single use so as not to interfere too much with
7305    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7306    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7307 (for cmp (lt le ge gt)
7308      out (gt gt le le)
7309  (simplify
7310   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7311   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7312        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7313        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7314        && wi::to_wide (@1) != 0
7315        && single_use (@2))
7316    (with {
7317      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7318      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7319     }
7320     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7321                                 wi::max_value (prec, sign)
7322                                 - wi::to_wide (@1)); })))))
7324 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7325    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7326    expects the long form, so we restrict the transformation for now.  */
7327 (for cmp (gt le)
7328  (simplify
7329   (cmp:c (minus@2 @0 @1) @0)
7330   (if (single_use (@2)
7331        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7332        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7333    (cmp @1 @0))))
7335 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7336 (for cmp (ge lt)
7337  (simplify
7338   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7339    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7340         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7341     (cmp @1 @0))))
7343 /* Testing for overflow is unnecessary if we already know the result.  */
7344 /* A - B > A  */
7345 (for cmp (gt le)
7346      out (ne eq)
7347  (simplify
7348   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7349   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7350        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7351    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7352 /* A + B < A  */
7353 (for cmp (lt ge)
7354      out (ne eq)
7355  (simplify
7356   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7357   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7358        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7359    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7361 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7362    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7363 (for cmp (lt ge)
7364      out (ne eq)
7365  (simplify
7366   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7367   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7368    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7369     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7371 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7372    is at least twice as wide as type of A and B, simplify to
7373    __builtin_mul_overflow (A, B, <unused>).  */
7374 (for cmp (eq ne)
7375  (simplify
7376   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7377        integer_zerop)
7378   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7379        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7380        && TYPE_UNSIGNED (TREE_TYPE (@0))
7381        && (TYPE_PRECISION (TREE_TYPE (@3))
7382            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7383        && tree_fits_uhwi_p (@2)
7384        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7385        && types_match (@0, @1)
7386        && type_has_mode_precision_p (TREE_TYPE (@0))
7387        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7388            != CODE_FOR_nothing))
7389    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7390     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7392 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7393 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7394  (simplify
7395   (ovf (convert@2 @0) @1)
7396   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7397        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7398        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7399        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7400    (ovf @0 @1)))
7401  (simplify
7402   (ovf @1 (convert@2 @0))
7403   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7404        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7405        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7406        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7407    (ovf @1 @0))))
7409 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7410    are unsigned to x > (umax / cst).  Similarly for signed type, but
7411    in that case it needs to be outside of a range.  */
7412 (simplify
7413  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7414   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7415        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7416        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7417        && int_fits_type_p (@1, TREE_TYPE (@0)))
7418    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7419     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7420     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7421      (if (integer_minus_onep (@1))
7422       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7423       (with
7424        {
7425          tree div = fold_convert (TREE_TYPE (@0), @1);
7426          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7427                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7428          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7429                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7430          tree etype = range_check_type (TREE_TYPE (@0));
7431          if (etype)
7432            {
7433              if (wi::neg_p (wi::to_wide (div)))
7434                std::swap (lo, hi);
7435              lo = fold_convert (etype, lo);
7436              hi = fold_convert (etype, hi);
7437              hi = int_const_binop (MINUS_EXPR, hi, lo);
7438            }
7439        }
7440        (if (etype)
7441         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7443 /* Simplification of math builtins.  These rules must all be optimizations
7444    as well as IL simplifications.  If there is a possibility that the new
7445    form could be a pessimization, the rule should go in the canonicalization
7446    section that follows this one.
7448    Rules can generally go in this section if they satisfy one of
7449    the following:
7451    - the rule describes an identity
7453    - the rule replaces calls with something as simple as addition or
7454      multiplication
7456    - the rule contains unary calls only and simplifies the surrounding
7457      arithmetic.  (The idea here is to exclude non-unary calls in which
7458      one operand is constant and in which the call is known to be cheap
7459      when the operand has that value.)  */
7461 (if (flag_unsafe_math_optimizations)
7462  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7463  (simplify
7464   (mult (SQRT_ALL@1 @0) @1)
7465   (if (!tree_expr_maybe_signaling_nan_p (@0))
7466    @0))
7468  (for op (plus minus)
7469   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7470   (simplify
7471    (op (rdiv @0 @1)
7472        (rdiv @2 @1))
7473    (rdiv (op @0 @2) @1)))
7475  (for cmp (lt le gt ge)
7476       neg_cmp (gt ge lt le)
7477   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7478   (simplify
7479    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7480    (with
7481     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7482     (if (tem
7483          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7484               || (real_zerop (tem) && !real_zerop (@1))))
7485      (switch
7486       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7487        (cmp @0 { tem; }))
7488       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7489        (neg_cmp @0 { tem; })))))))
7491  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7492  (for root (SQRT CBRT)
7493   (simplify
7494    (mult (root:s @0) (root:s @1))
7495     (root (mult @0 @1))))
7497  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7498  (for exps (EXP EXP2 EXP10 POW10)
7499   (simplify
7500    (mult (exps:s @0) (exps:s @1))
7501     (exps (plus @0 @1))))
7503  /* Simplify a/root(b/c) into a*root(c/b).  */
7504  (for root (SQRT CBRT)
7505   (simplify
7506    (rdiv @0 (root:s (rdiv:s @1 @2)))
7507     (mult @0 (root (rdiv @2 @1)))))
7509  /* Simplify x/expN(y) into x*expN(-y).  */
7510  (for exps (EXP EXP2 EXP10 POW10)
7511   (simplify
7512    (rdiv @0 (exps:s @1))
7513     (mult @0 (exps (negate @1)))))
7515  (for logs (LOG LOG2 LOG10 LOG10)
7516       exps (EXP EXP2 EXP10 POW10)
7517   /* logN(expN(x)) -> x.  */
7518   (simplify
7519    (logs (exps @0))
7520    @0)
7521   /* expN(logN(x)) -> x.  */
7522   (simplify
7523    (exps (logs @0))
7524    @0))
7526  /* Optimize logN(func()) for various exponential functions.  We
7527     want to determine the value "x" and the power "exponent" in
7528     order to transform logN(x**exponent) into exponent*logN(x).  */
7529  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7530       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
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_EXP:
7539           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7540           x = build_real_truncate (type, dconst_e ());
7541           break;
7542         CASE_CFN_EXP2:
7543           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7544           x = build_real (type, dconst2);
7545           break;
7546         CASE_CFN_EXP10:
7547         CASE_CFN_POW10:
7548           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7549           {
7550             REAL_VALUE_TYPE dconst10;
7551             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7552             x = build_real (type, dconst10);
7553           }
7554           break;
7555         default:
7556           gcc_unreachable ();
7557         }
7558       }
7559      (mult (logs { x; }) @0)))))
7561  (for logs (LOG LOG
7562             LOG2 LOG2
7563             LOG10 LOG10)
7564       exps (SQRT CBRT)
7565   (simplify
7566    (logs (exps @0))
7567    (if (SCALAR_FLOAT_TYPE_P (type))
7568     (with {
7569       tree x;
7570       switch (exps)
7571         {
7572         CASE_CFN_SQRT:
7573           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7574           x = build_real (type, dconsthalf);
7575           break;
7576         CASE_CFN_CBRT:
7577           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7578           x = build_real_truncate (type, dconst_third ());
7579           break;
7580         default:
7581           gcc_unreachable ();
7582         }
7583       }
7584      (mult { x; } (logs @0))))))
7586  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7587  (for logs (LOG LOG2 LOG10)
7588       pows (POW)
7589   (simplify
7590    (logs (pows @0 @1))
7591    (mult @1 (logs @0))))
7593  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7594     or if C is a positive power of 2,
7595     pow(C,x) -> exp2(log2(C)*x).  */
7596 #if GIMPLE
7597  (for pows (POW)
7598       exps (EXP)
7599       logs (LOG)
7600       exp2s (EXP2)
7601       log2s (LOG2)
7602   (simplify
7603    (pows REAL_CST@0 @1)
7604    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7605         && real_isfinite (TREE_REAL_CST_PTR (@0))
7606         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7607            the use_exp2 case until after vectorization.  It seems actually
7608            beneficial for all constants to postpone this until later,
7609            because exp(log(C)*x), while faster, will have worse precision
7610            and if x folds into a constant too, that is unnecessary
7611            pessimization.  */
7612         && canonicalize_math_after_vectorization_p ())
7613     (with {
7614        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7615        bool use_exp2 = false;
7616        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7617            && value->cl == rvc_normal)
7618          {
7619            REAL_VALUE_TYPE frac_rvt = *value;
7620            SET_REAL_EXP (&frac_rvt, 1);
7621            if (real_equal (&frac_rvt, &dconst1))
7622              use_exp2 = true;
7623          }
7624      }
7625      (if (!use_exp2)
7626       (if (optimize_pow_to_exp (@0, @1))
7627        (exps (mult (logs @0) @1)))
7628       (exp2s (mult (log2s @0) @1)))))))
7629 #endif
7631  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7632  (for pows (POW)
7633       exps (EXP EXP2 EXP10 POW10)
7634       logs (LOG LOG2 LOG10 LOG10)
7635   (simplify
7636    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7637    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7638         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7639     (exps (plus (mult (logs @0) @1) @2)))))
7641  (for sqrts (SQRT)
7642       cbrts (CBRT)
7643       pows (POW)
7644       exps (EXP EXP2 EXP10 POW10)
7645   /* sqrt(expN(x)) -> expN(x*0.5).  */
7646   (simplify
7647    (sqrts (exps @0))
7648    (exps (mult @0 { build_real (type, dconsthalf); })))
7649   /* cbrt(expN(x)) -> expN(x/3).  */
7650   (simplify
7651    (cbrts (exps @0))
7652    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7653   /* pow(expN(x), y) -> expN(x*y).  */
7654   (simplify
7655    (pows (exps @0) @1)
7656    (exps (mult @0 @1))))
7658  /* tan(atan(x)) -> x.  */
7659  (for tans (TAN)
7660       atans (ATAN)
7661   (simplify
7662    (tans (atans @0))
7663    @0)))
7665  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7666  (for sins (SIN)
7667       atans (ATAN)
7668       sqrts (SQRT)
7669       copysigns (COPYSIGN)
7670   (simplify
7671    (sins (atans:s @0))
7672    (with
7673      {
7674       REAL_VALUE_TYPE r_cst;
7675       build_sinatan_real (&r_cst, type);
7676       tree t_cst = build_real (type, r_cst);
7677       tree t_one = build_one_cst (type);
7678      }
7679     (if (SCALAR_FLOAT_TYPE_P (type))
7680      (cond (lt (abs @0) { t_cst; })
7681       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7682       (copysigns { t_one; } @0))))))
7684 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7685  (for coss (COS)
7686       atans (ATAN)
7687       sqrts (SQRT)
7688       copysigns (COPYSIGN)
7689   (simplify
7690    (coss (atans:s @0))
7691    (with
7692      {
7693       REAL_VALUE_TYPE r_cst;
7694       build_sinatan_real (&r_cst, type);
7695       tree t_cst = build_real (type, r_cst);
7696       tree t_one = build_one_cst (type);
7697       tree t_zero = build_zero_cst (type);
7698      }
7699     (if (SCALAR_FLOAT_TYPE_P (type))
7700      (cond (lt (abs @0) { t_cst; })
7701       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7702       (copysigns { t_zero; } @0))))))
7704  (if (!flag_errno_math)
7705   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7706   (for sinhs (SINH)
7707        atanhs (ATANH)
7708        sqrts (SQRT)
7709    (simplify
7710     (sinhs (atanhs:s @0))
7711     (with { tree t_one = build_one_cst (type); }
7712     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7714   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7715   (for coshs (COSH)
7716        atanhs (ATANH)
7717        sqrts (SQRT)
7718    (simplify
7719     (coshs (atanhs:s @0))
7720     (with { tree t_one = build_one_cst (type); }
7721     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7723 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7724 (simplify
7725  (CABS (complex:C @0 real_zerop@1))
7726  (abs @0))
7728 /* trunc(trunc(x)) -> trunc(x), etc.  */
7729 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7730  (simplify
7731   (fns (fns @0))
7732   (fns @0)))
7733 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7734 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7735  (simplify
7736   (fns integer_valued_real_p@0)
7737   @0))
7739 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7740 (simplify
7741  (HYPOT:c @0 real_zerop@1)
7742  (abs @0))
7744 /* pow(1,x) -> 1.  */
7745 (simplify
7746  (POW real_onep@0 @1)
7747  @0)
7749 (simplify
7750  /* copysign(x,x) -> x.  */
7751  (COPYSIGN_ALL @0 @0)
7752  @0)
7754 (simplify
7755  /* copysign(x,-x) -> -x.  */
7756  (COPYSIGN_ALL @0 (negate@1 @0))
7757  @1)
7759 (simplify
7760  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7761  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7762  (abs @0))
7764 (simplify
7765  /* fabs (copysign(x, y)) -> fabs (x).  */
7766  (abs (COPYSIGN_ALL @0 @1))
7767  (abs @0))
7769 (for scale (LDEXP SCALBN SCALBLN)
7770  /* ldexp(0, x) -> 0.  */
7771  (simplify
7772   (scale real_zerop@0 @1)
7773   @0)
7774  /* ldexp(x, 0) -> x.  */
7775  (simplify
7776   (scale @0 integer_zerop@1)
7777   @0)
7778  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7779  (simplify
7780   (scale REAL_CST@0 @1)
7781   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7782    @0)))
7784 /* Canonicalization of sequences of math builtins.  These rules represent
7785    IL simplifications but are not necessarily optimizations.
7787    The sincos pass is responsible for picking "optimal" implementations
7788    of math builtins, which may be more complicated and can sometimes go
7789    the other way, e.g. converting pow into a sequence of sqrts.
7790    We only want to do these canonicalizations before the pass has run.  */
7792 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7793  /* Simplify tan(x) * cos(x) -> sin(x). */
7794  (simplify
7795   (mult:c (TAN:s @0) (COS:s @0))
7796    (SIN @0))
7798  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7799  (simplify
7800   (mult:c @0 (POW:s @0 REAL_CST@1))
7801   (if (!TREE_OVERFLOW (@1))
7802    (POW @0 (plus @1 { build_one_cst (type); }))))
7804  /* Simplify sin(x) / cos(x) -> tan(x). */
7805  (simplify
7806   (rdiv (SIN:s @0) (COS:s @0))
7807    (TAN @0))
7809  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7810  (simplify
7811   (rdiv (SINH:s @0) (COSH:s @0))
7812    (TANH @0))
7814  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7815  (simplify
7816    (rdiv (TANH:s @0) (SINH:s @0))
7817    (rdiv {build_one_cst (type);} (COSH @0)))
7819  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7820  (simplify
7821   (rdiv (COS:s @0) (SIN:s @0))
7822    (rdiv { build_one_cst (type); } (TAN @0)))
7824  /* Simplify sin(x) / tan(x) -> cos(x). */
7825  (simplify
7826   (rdiv (SIN:s @0) (TAN:s @0))
7827   (if (! HONOR_NANS (@0)
7828        && ! HONOR_INFINITIES (@0))
7829    (COS @0)))
7831  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7832  (simplify
7833   (rdiv (TAN:s @0) (SIN:s @0))
7834   (if (! HONOR_NANS (@0)
7835        && ! HONOR_INFINITIES (@0))
7836    (rdiv { build_one_cst (type); } (COS @0))))
7838  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7839  (simplify
7840   (mult (POW:s @0 @1) (POW:s @0 @2))
7841    (POW @0 (plus @1 @2)))
7843  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7844  (simplify
7845   (mult (POW:s @0 @1) (POW:s @2 @1))
7846    (POW (mult @0 @2) @1))
7848  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7849  (simplify
7850   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7851    (POWI (mult @0 @2) @1))
7853  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7854  (simplify
7855   (rdiv (POW:s @0 REAL_CST@1) @0)
7856   (if (!TREE_OVERFLOW (@1))
7857    (POW @0 (minus @1 { build_one_cst (type); }))))
7859  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7860  (simplify
7861   (rdiv @0 (POW:s @1 @2))
7862    (mult @0 (POW @1 (negate @2))))
7864  (for sqrts (SQRT)
7865       cbrts (CBRT)
7866       pows (POW)
7867   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7868   (simplify
7869    (sqrts (sqrts @0))
7870    (pows @0 { build_real (type, dconst_quarter ()); }))
7871   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7872   (simplify
7873    (sqrts (cbrts @0))
7874    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7875   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7876   (simplify
7877    (cbrts (sqrts @0))
7878    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7879   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7880   (simplify
7881    (cbrts (cbrts tree_expr_nonnegative_p@0))
7882    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7883   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7884   (simplify
7885    (sqrts (pows @0 @1))
7886    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7887   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7888   (simplify
7889    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7890    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7891   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7892   (simplify
7893    (pows (sqrts @0) @1)
7894    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7895   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7896   (simplify
7897    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7898    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7899   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7900   (simplify
7901    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7902    (pows @0 (mult @1 @2))))
7904  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7905  (simplify
7906   (CABS (complex @0 @0))
7907   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7909  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7910  (simplify
7911   (HYPOT @0 @0)
7912   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7914  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7915  (for cexps (CEXP)
7916       exps (EXP)
7917       cexpis (CEXPI)
7918   (simplify
7919    (cexps compositional_complex@0)
7920    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7921     (complex
7922      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7923      (mult @1 (imagpart @2)))))))
7925 (if (canonicalize_math_p ())
7926  /* floor(x) -> trunc(x) if x is nonnegative.  */
7927  (for floors (FLOOR_ALL)
7928       truncs (TRUNC_ALL)
7929   (simplify
7930    (floors tree_expr_nonnegative_p@0)
7931    (truncs @0))))
7933 (match double_value_p
7934  @0
7935  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7936 (for froms (BUILT_IN_TRUNCL
7937             BUILT_IN_FLOORL
7938             BUILT_IN_CEILL
7939             BUILT_IN_ROUNDL
7940             BUILT_IN_NEARBYINTL
7941             BUILT_IN_RINTL)
7942      tos (BUILT_IN_TRUNC
7943           BUILT_IN_FLOOR
7944           BUILT_IN_CEIL
7945           BUILT_IN_ROUND
7946           BUILT_IN_NEARBYINT
7947           BUILT_IN_RINT)
7948  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7949  (if (optimize && canonicalize_math_p ())
7950   (simplify
7951    (froms (convert double_value_p@0))
7952    (convert (tos @0)))))
7954 (match float_value_p
7955  @0
7956  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7957 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7958             BUILT_IN_FLOORL BUILT_IN_FLOOR
7959             BUILT_IN_CEILL BUILT_IN_CEIL
7960             BUILT_IN_ROUNDL BUILT_IN_ROUND
7961             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7962             BUILT_IN_RINTL BUILT_IN_RINT)
7963      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7964           BUILT_IN_FLOORF BUILT_IN_FLOORF
7965           BUILT_IN_CEILF BUILT_IN_CEILF
7966           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7967           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7968           BUILT_IN_RINTF BUILT_IN_RINTF)
7969  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7970     if x is a float.  */
7971  (if (optimize && canonicalize_math_p ()
7972       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7973   (simplify
7974    (froms (convert float_value_p@0))
7975    (convert (tos @0)))))
7977 #if GIMPLE
7978 (match float16_value_p
7979  @0
7980  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7981 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7982             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7983             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7984             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7985             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7986             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7987             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7988             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7989      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7990           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7991           IFN_CEIL IFN_CEIL IFN_CEIL
7992           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7993           IFN_ROUND IFN_ROUND IFN_ROUND
7994           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7995           IFN_RINT IFN_RINT IFN_RINT
7996           IFN_SQRT IFN_SQRT IFN_SQRT)
7997  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7998     if x is a _Float16.  */
7999  (simplify
8000    (convert (froms (convert float16_value_p@0)))
8001      (if (optimize
8002           && types_match (type, TREE_TYPE (@0))
8003           && direct_internal_fn_supported_p (as_internal_fn (tos),
8004                                              type, OPTIMIZE_FOR_BOTH))
8005        (tos @0))))
8007 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
8008    x,y is float value, similar for _Float16/double.  */
8009 (for copysigns (COPYSIGN_ALL)
8010  (simplify
8011   (convert (copysigns (convert@2 @0) (convert @1)))
8012    (if (optimize
8013        && !HONOR_SNANS (@2)
8014        && types_match (type, TREE_TYPE (@0))
8015        && types_match (type, TREE_TYPE (@1))
8016        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
8017        && direct_internal_fn_supported_p (IFN_COPYSIGN,
8018                                           type, OPTIMIZE_FOR_BOTH))
8019     (IFN_COPYSIGN @0 @1))))
8021 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
8022      tos (IFN_FMA IFN_FMA IFN_FMA)
8023  (simplify
8024   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
8025    (if (flag_unsafe_math_optimizations
8026        && optimize
8027        && FLOAT_TYPE_P (type)
8028        && FLOAT_TYPE_P (TREE_TYPE (@3))
8029        && types_match (type, TREE_TYPE (@0))
8030        && types_match (type, TREE_TYPE (@1))
8031        && types_match (type, TREE_TYPE (@2))
8032        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
8033        && direct_internal_fn_supported_p (as_internal_fn (tos),
8034                                           type, OPTIMIZE_FOR_BOTH))
8035     (tos @0 @1 @2))))
8037 (for maxmin (max min)
8038  (simplify
8039   (convert (maxmin (convert@2 @0) (convert @1)))
8040    (if (optimize
8041        && FLOAT_TYPE_P (type)
8042        && FLOAT_TYPE_P (TREE_TYPE (@2))
8043        && types_match (type, TREE_TYPE (@0))
8044        && types_match (type, TREE_TYPE (@1))
8045        && element_precision (type) < element_precision (TREE_TYPE (@2)))
8046     (maxmin @0 @1))))
8047 #endif
8049 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8050      tos (XFLOOR XCEIL XROUND XRINT)
8051  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
8052  (if (optimize && canonicalize_math_p ())
8053   (simplify
8054    (froms (convert double_value_p@0))
8055    (tos @0))))
8057 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8058             XFLOOR XCEIL XROUND XRINT)
8059      tos (XFLOORF XCEILF XROUNDF XRINTF)
8060  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8061     if x is a float.  */
8062  (if (optimize && canonicalize_math_p ())
8063   (simplify
8064    (froms (convert float_value_p@0))
8065    (tos @0))))
8067 (if (canonicalize_math_p ())
8068  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
8069  (for floors (IFLOOR LFLOOR LLFLOOR)
8070   (simplify
8071    (floors tree_expr_nonnegative_p@0)
8072    (fix_trunc @0))))
8074 (if (canonicalize_math_p ())
8075  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
8076  (for fns (IFLOOR LFLOOR LLFLOOR
8077            ICEIL LCEIL LLCEIL
8078            IROUND LROUND LLROUND)
8079   (simplify
8080    (fns integer_valued_real_p@0)
8081    (fix_trunc @0)))
8082  (if (!flag_errno_math)
8083   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
8084   (for rints (IRINT LRINT LLRINT)
8085    (simplify
8086     (rints integer_valued_real_p@0)
8087     (fix_trunc @0)))))
8089 (if (canonicalize_math_p ())
8090  (for ifn (IFLOOR ICEIL IROUND IRINT)
8091       lfn (LFLOOR LCEIL LROUND LRINT)
8092       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8093   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8094      sizeof (int) == sizeof (long).  */
8095   (if (TYPE_PRECISION (integer_type_node)
8096        == TYPE_PRECISION (long_integer_type_node))
8097    (simplify
8098     (ifn @0)
8099     (lfn:long_integer_type_node @0)))
8100   /* Canonicalize llround (x) to lround (x) on LP64 targets where
8101      sizeof (long long) == sizeof (long).  */
8102   (if (TYPE_PRECISION (long_long_integer_type_node)
8103        == TYPE_PRECISION (long_integer_type_node))
8104    (simplify
8105     (llfn @0)
8106     (lfn:long_integer_type_node @0)))))
8108 /* cproj(x) -> x if we're ignoring infinities.  */
8109 (simplify
8110  (CPROJ @0)
8111  (if (!HONOR_INFINITIES (type))
8112    @0))
8114 /* If the real part is inf and the imag part is known to be
8115    nonnegative, return (inf + 0i).  */
8116 (simplify
8117  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8118  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8119   { build_complex_inf (type, false); }))
8121 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
8122 (simplify
8123  (CPROJ (complex @0 REAL_CST@1))
8124  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8125   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8127 (for pows (POW)
8128      sqrts (SQRT)
8129      cbrts (CBRT)
8130  (simplify
8131   (pows @0 REAL_CST@1)
8132   (with {
8133     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8134     REAL_VALUE_TYPE tmp;
8135    }
8136    (switch
8137     /* pow(x,0) -> 1.  */
8138     (if (real_equal (value, &dconst0))
8139      { build_real (type, dconst1); })
8140     /* pow(x,1) -> x.  */
8141     (if (real_equal (value, &dconst1))
8142      @0)
8143     /* pow(x,-1) -> 1/x.  */
8144     (if (real_equal (value, &dconstm1))
8145      (rdiv { build_real (type, dconst1); } @0))
8146     /* pow(x,0.5) -> sqrt(x).  */
8147     (if (flag_unsafe_math_optimizations
8148          && canonicalize_math_p ()
8149          && real_equal (value, &dconsthalf))
8150      (sqrts @0))
8151     /* pow(x,1/3) -> cbrt(x).  */
8152     (if (flag_unsafe_math_optimizations
8153          && canonicalize_math_p ()
8154          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8155              real_equal (value, &tmp)))
8156      (cbrts @0))))))
8158 /* powi(1,x) -> 1.  */
8159 (simplify
8160  (POWI real_onep@0 @1)
8161  @0)
8163 (simplify
8164  (POWI @0 INTEGER_CST@1)
8165  (switch
8166   /* powi(x,0) -> 1.  */
8167   (if (wi::to_wide (@1) == 0)
8168    { build_real (type, dconst1); })
8169   /* powi(x,1) -> x.  */
8170   (if (wi::to_wide (@1) == 1)
8171    @0)
8172   /* powi(x,-1) -> 1/x.  */
8173   (if (wi::to_wide (@1) == -1)
8174    (rdiv { build_real (type, dconst1); } @0))))
8176 /* Narrowing of arithmetic and logical operations.
8178    These are conceptually similar to the transformations performed for
8179    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
8180    term we want to move all that code out of the front-ends into here.  */
8182 /* Convert (outertype)((innertype0)a+(innertype1)b)
8183    into ((newtype)a+(newtype)b) where newtype
8184    is the widest mode from all of these.  */
8185 (for op (plus minus mult rdiv)
8186  (simplify
8187    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8188    /* If we have a narrowing conversion of an arithmetic operation where
8189       both operands are widening conversions from the same type as the outer
8190       narrowing conversion.  Then convert the innermost operands to a
8191       suitable unsigned type (to avoid introducing undefined behavior),
8192       perform the operation and convert the result to the desired type.  */
8193    (if (INTEGRAL_TYPE_P (type)
8194         && op != MULT_EXPR
8195         && op != RDIV_EXPR
8196         /* We check for type compatibility between @0 and @1 below,
8197            so there's no need to check that @2/@4 are integral types.  */
8198         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8199         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8200         /* The precision of the type of each operand must match the
8201            precision of the mode of each operand, similarly for the
8202            result.  */
8203         && type_has_mode_precision_p (TREE_TYPE (@1))
8204         && type_has_mode_precision_p (TREE_TYPE (@2))
8205         && type_has_mode_precision_p (type)
8206         /* The inner conversion must be a widening conversion.  */
8207         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8208         && types_match (@1, type)
8209         && (types_match (@1, @2)
8210             /* Or the second operand is const integer or converted const
8211                integer from valueize.  */
8212             || poly_int_tree_p (@4)))
8213      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8214        (op @1 (convert @2))
8215        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8216         (convert (op (convert:utype @1)
8217                      (convert:utype @2)))))
8218      (if (FLOAT_TYPE_P (type)
8219           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8220                == DECIMAL_FLOAT_TYPE_P (type))
8221       (with { tree arg0 = strip_float_extensions (@1);
8222               tree arg1 = strip_float_extensions (@2);
8223               tree itype = TREE_TYPE (@0);
8224               tree ty1 = TREE_TYPE (arg0);
8225               tree ty2 = TREE_TYPE (arg1);
8226               enum tree_code code = TREE_CODE (itype); }
8227         (if (FLOAT_TYPE_P (ty1)
8228              && FLOAT_TYPE_P (ty2))
8229          (with { tree newtype = type;
8230                  if (TYPE_MODE (ty1) == SDmode
8231                      || TYPE_MODE (ty2) == SDmode
8232                      || TYPE_MODE (type) == SDmode)
8233                    newtype = dfloat32_type_node;
8234                  if (TYPE_MODE (ty1) == DDmode
8235                      || TYPE_MODE (ty2) == DDmode
8236                      || TYPE_MODE (type) == DDmode)
8237                    newtype = dfloat64_type_node;
8238                  if (TYPE_MODE (ty1) == TDmode
8239                      || TYPE_MODE (ty2) == TDmode
8240                      || TYPE_MODE (type) == TDmode)
8241                    newtype = dfloat128_type_node; }
8242           (if ((newtype == dfloat32_type_node
8243                 || newtype == dfloat64_type_node
8244                 || newtype == dfloat128_type_node)
8245               && newtype == type
8246               && types_match (newtype, type))
8247             (op (convert:newtype @1) (convert:newtype @2))
8248             (with { if (element_precision (ty1) > element_precision (newtype))
8249                       newtype = ty1;
8250                     if (element_precision (ty2) > element_precision (newtype))
8251                       newtype = ty2; }
8252                /* Sometimes this transformation is safe (cannot
8253                   change results through affecting double rounding
8254                   cases) and sometimes it is not.  If NEWTYPE is
8255                   wider than TYPE, e.g. (float)((long double)double
8256                   + (long double)double) converted to
8257                   (float)(double + double), the transformation is
8258                   unsafe regardless of the details of the types
8259                   involved; double rounding can arise if the result
8260                   of NEWTYPE arithmetic is a NEWTYPE value half way
8261                   between two representable TYPE values but the
8262                   exact value is sufficiently different (in the
8263                   right direction) for this difference to be
8264                   visible in ITYPE arithmetic.  If NEWTYPE is the
8265                   same as TYPE, however, the transformation may be
8266                   safe depending on the types involved: it is safe
8267                   if the ITYPE has strictly more than twice as many
8268                   mantissa bits as TYPE, can represent infinities
8269                   and NaNs if the TYPE can, and has sufficient
8270                   exponent range for the product or ratio of two
8271                   values representable in the TYPE to be within the
8272                   range of normal values of ITYPE.  */
8273               (if (element_precision (newtype) < element_precision (itype)
8274                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8275                        || target_supports_op_p (newtype, op, optab_default))
8276                    && (flag_unsafe_math_optimizations
8277                        || (element_precision (newtype) == element_precision (type)
8278                            && real_can_shorten_arithmetic (element_mode (itype),
8279                                                            element_mode (type))
8280                            && !excess_precision_type (newtype)))
8281                    && !types_match (itype, newtype))
8282                  (convert:type (op (convert:newtype @1)
8283                                    (convert:newtype @2)))
8284          )))) )
8285    ))
8288 /* This is another case of narrowing, specifically when there's an outer
8289    BIT_AND_EXPR which masks off bits outside the type of the innermost
8290    operands.   Like the previous case we have to convert the operands
8291    to unsigned types to avoid introducing undefined behavior for the
8292    arithmetic operation.  */
8293 (for op (minus plus)
8294  (simplify
8295   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8296   (if (INTEGRAL_TYPE_P (type)
8297        /* We check for type compatibility between @0 and @1 below,
8298           so there's no need to check that @1/@3 are integral types.  */
8299        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8300        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8301        /* The precision of the type of each operand must match the
8302           precision of the mode of each operand, similarly for the
8303           result.  */
8304        && type_has_mode_precision_p (TREE_TYPE (@0))
8305        && type_has_mode_precision_p (TREE_TYPE (@1))
8306        && type_has_mode_precision_p (type)
8307        /* The inner conversion must be a widening conversion.  */
8308        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8309        && types_match (@0, @1)
8310        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8311            <= TYPE_PRECISION (TREE_TYPE (@0)))
8312        && (wi::to_wide (@4)
8313            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8314                        true, TYPE_PRECISION (type))) == 0)
8315    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8316     (with { tree ntype = TREE_TYPE (@0); }
8317      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8318     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8319      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8320                (convert:utype @4))))))))
8322 /* Transform (@0 < @1 and @0 < @2) to use min,
8323    (@0 > @1 and @0 > @2) to use max */
8324 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8325      op    (lt      le      gt      ge      lt      le      gt      ge     )
8326      ext   (min     min     max     max     max     max     min     min    )
8327  (simplify
8328   (logic (op:cs @0 @1) (op:cs @0 @2))
8329   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8330        && TREE_CODE (@0) != INTEGER_CST)
8331    (op @0 (ext @1 @2)))))
8333 /* Max<bool0, bool1> -> bool0 | bool1
8334    Min<bool0, bool1> -> bool0 & bool1 */
8335 (for op    (max     min)
8336      logic (bit_ior bit_and)
8337  (simplify
8338   (op zero_one_valued_p@0 zero_one_valued_p@1)
8339   (logic @0 @1)))
8341 /* signbit(x) != 0 ? -x : x -> abs(x)
8342    signbit(x) == 0 ? -x : x -> -abs(x) */
8343 (for sign (SIGNBIT)
8344  (for neeq (ne eq)
8345   (simplify
8346    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8347     (if (neeq == NE_EXPR)
8348      (abs @0)
8349      (negate (abs @0))))))
8351 (simplify
8352  /* signbit(x) -> 0 if x is nonnegative.  */
8353  (SIGNBIT tree_expr_nonnegative_p@0)
8354  { integer_zero_node; })
8356 (simplify
8357  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8358  (SIGNBIT @0)
8359  (if (!HONOR_SIGNED_ZEROS (@0))
8360   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8362 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8363 (for cmp (eq ne)
8364  (for op (plus minus)
8365       rop (minus plus)
8366   (simplify
8367    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8368    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8369         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8370         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8371         && !TYPE_SATURATING (TREE_TYPE (@0)))
8372     (with { tree res = int_const_binop (rop, @2, @1); }
8373      (if (TREE_OVERFLOW (res)
8374           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8375       { constant_boolean_node (cmp == NE_EXPR, type); }
8376       (if (single_use (@3))
8377        (cmp @0 { TREE_OVERFLOW (res)
8378                  ? drop_tree_overflow (res) : res; }))))))))
8379 (for cmp (lt le gt ge)
8380  (for op (plus minus)
8381       rop (minus plus)
8382   (simplify
8383    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8384    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8385         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8386     (with { tree res = int_const_binop (rop, @2, @1); }
8387      (if (TREE_OVERFLOW (res))
8388       {
8389         fold_overflow_warning (("assuming signed overflow does not occur "
8390                                 "when simplifying conditional to constant"),
8391                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8392         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8393         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8394         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8395                                   TYPE_SIGN (TREE_TYPE (@1)))
8396                         != (op == MINUS_EXPR);
8397         constant_boolean_node (less == ovf_high, type);
8398       }
8399       (if (single_use (@3))
8400        (with
8401         {
8402           fold_overflow_warning (("assuming signed overflow does not occur "
8403                                   "when changing X +- C1 cmp C2 to "
8404                                   "X cmp C2 -+ C1"),
8405                                  WARN_STRICT_OVERFLOW_COMPARISON);
8406         }
8407         (cmp @0 { res; })))))))))
8409 /* Canonicalizations of BIT_FIELD_REFs.  */
8411 (simplify
8412  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8413  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8415 (simplify
8416  (BIT_FIELD_REF (view_convert @0) @1 @2)
8417  (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8418       || type_has_mode_precision_p (TREE_TYPE (@0)))
8419   (BIT_FIELD_REF @0 @1 @2)))
8421 (simplify
8422  (BIT_FIELD_REF @0 @1 integer_zerop)
8423  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8424   (view_convert @0)))
8426 (simplify
8427  (BIT_FIELD_REF @0 @1 @2)
8428  (switch
8429   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8430        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8431    (switch
8432     (if (integer_zerop (@2))
8433      (view_convert (realpart @0)))
8434     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8435      (view_convert (imagpart @0)))))
8436   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8437        && INTEGRAL_TYPE_P (type)
8438        /* On GIMPLE this should only apply to register arguments.  */
8439        && (! GIMPLE || is_gimple_reg (@0))
8440        /* A bit-field-ref that referenced the full argument can be stripped.  */
8441        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8442             && integer_zerop (@2))
8443            /* Low-parts can be reduced to integral conversions.
8444               ???  The following doesn't work for PDP endian.  */
8445            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8446                /* But only do this after vectorization.  */
8447                && canonicalize_math_after_vectorization_p ()
8448                /* Don't even think about BITS_BIG_ENDIAN.  */
8449                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8450                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8451                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8452                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8453                                             - TYPE_PRECISION (type))
8454                                          : 0)) == 0)))
8455    (convert @0))))
8457 /* Simplify vector extracts.  */
8459 (simplify
8460  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8461  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8462       && tree_fits_uhwi_p (TYPE_SIZE (type))
8463       && ((tree_to_uhwi (TYPE_SIZE (type))
8464            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8465           || (VECTOR_TYPE_P (type)
8466               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8467                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8468   (with
8469    {
8470      tree ctor = (TREE_CODE (@0) == SSA_NAME
8471                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8472      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8473      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8474      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8475      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8476    }
8477    (if (n != 0
8478         && (idx % width) == 0
8479         && (n % width) == 0
8480         && known_le ((idx + n) / width,
8481                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8482     (with
8483      {
8484        idx = idx / width;
8485        n = n / width;
8486        /* Constructor elements can be subvectors.  */
8487        poly_uint64 k = 1;
8488        if (CONSTRUCTOR_NELTS (ctor) != 0)
8489          {
8490            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8491            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8492              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8493          }
8494        unsigned HOST_WIDE_INT elt, count, const_k;
8495      }
8496      (switch
8497       /* We keep an exact subset of the constructor elements.  */
8498       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8499        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8500         { build_zero_cst (type); }
8501         (if (count == 1)
8502          (if (elt < CONSTRUCTOR_NELTS (ctor))
8503           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8504           { build_zero_cst (type); })
8505          /* We don't want to emit new CTORs unless the old one goes away.
8506             ???  Eventually allow this if the CTOR ends up constant or
8507             uniform.  */
8508          (if (single_use (@0))
8509           (with
8510             {
8511               vec<constructor_elt, va_gc> *vals;
8512               vec_alloc (vals, count);
8513               bool constant_p = true;
8514               tree res;
8515               for (unsigned i = 0;
8516                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8517                 {
8518                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8519                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8520                   if (!CONSTANT_CLASS_P (e))
8521                     constant_p = false;
8522                 }
8523               tree evtype = (types_match (TREE_TYPE (type),
8524                                           TREE_TYPE (TREE_TYPE (ctor)))
8525                              ? type
8526                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8527                                                   count * k));
8528               /* We used to build a CTOR in the non-constant case here
8529                  but that's not a GIMPLE value.  We'd have to expose this
8530                  operation somehow so the code generation can properly
8531                  split it out to a separate stmt.  */
8532               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8533                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8534             }
8535             (if (res)
8536              (view_convert { res; })))))))
8537       /* The bitfield references a single constructor element.  */
8538       (if (k.is_constant (&const_k)
8539            && idx + n <= (idx / const_k + 1) * const_k)
8540        (switch
8541         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8542          { build_zero_cst (type); })
8543         (if (n == const_k)
8544          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8545         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8546                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8548 /* Simplify a bit extraction from a bit insertion for the cases with
8549    the inserted element fully covering the extraction or the insertion
8550    not touching the extraction.  */
8551 (simplify
8552  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8553  (with
8554   {
8555     unsigned HOST_WIDE_INT isize;
8556     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8557       isize = TYPE_PRECISION (TREE_TYPE (@1));
8558     else
8559       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8560   }
8561   (switch
8562    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8563          || type_has_mode_precision_p (TREE_TYPE (@1)))
8564         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8565         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8566                       wi::to_wide (@ipos) + isize))
8567     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8568                                                  wi::to_wide (@rpos)
8569                                                  - wi::to_wide (@ipos)); }))
8570    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8571         && compare_tree_int (@rsize, isize) == 0)
8572     (convert @1))
8573    (if (wi::geu_p (wi::to_wide (@ipos),
8574                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8575         || wi::geu_p (wi::to_wide (@rpos),
8576                       wi::to_wide (@ipos) + isize))
8577     (BIT_FIELD_REF @0 @rsize @rpos)))))
8579 /* Simplify vector inserts of other vector extracts to a permute.  */
8580 (simplify
8581  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8582  (if (VECTOR_TYPE_P (type)
8583       && (VECTOR_MODE_P (TYPE_MODE (type))
8584           || optimize_vectors_before_lowering_p ())
8585       && types_match (@0, @1)
8586       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8587       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8588   (with
8589    {
8590      unsigned HOST_WIDE_INT elsz
8591        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8592      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8593      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8594      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8595      vec_perm_builder builder;
8596      builder.new_vector (nunits, nunits, 1);
8597      for (unsigned i = 0; i < nunits; ++i)
8598        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8599      vec_perm_indices sel (builder, 2, nunits);
8600    }
8601    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8602         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8603     (vec_perm @0 @1 { vec_perm_indices_to_tree
8604                         (build_vector_type (ssizetype, nunits), sel); })))))
8606 (if (canonicalize_math_after_vectorization_p ())
8607  (for fmas (FMA)
8608   (simplify
8609    (fmas:c (negate @0) @1 @2)
8610    (IFN_FNMA @0 @1 @2))
8611   (simplify
8612    (fmas @0 @1 (negate @2))
8613    (IFN_FMS @0 @1 @2))
8614   (simplify
8615    (fmas:c (negate @0) @1 (negate @2))
8616    (IFN_FNMS @0 @1 @2))
8617   (simplify
8618    (negate (fmas@3 @0 @1 @2))
8619    (if (single_use (@3))
8620     (IFN_FNMS @0 @1 @2))))
8622  (simplify
8623   (IFN_FMS:c (negate @0) @1 @2)
8624   (IFN_FNMS @0 @1 @2))
8625  (simplify
8626   (IFN_FMS @0 @1 (negate @2))
8627   (IFN_FMA @0 @1 @2))
8628  (simplify
8629   (IFN_FMS:c (negate @0) @1 (negate @2))
8630   (IFN_FNMA @0 @1 @2))
8631  (simplify
8632   (negate (IFN_FMS@3 @0 @1 @2))
8633    (if (single_use (@3))
8634     (IFN_FNMA @0 @1 @2)))
8636  (simplify
8637   (IFN_FNMA:c (negate @0) @1 @2)
8638   (IFN_FMA @0 @1 @2))
8639  (simplify
8640   (IFN_FNMA @0 @1 (negate @2))
8641   (IFN_FNMS @0 @1 @2))
8642  (simplify
8643   (IFN_FNMA:c (negate @0) @1 (negate @2))
8644   (IFN_FMS @0 @1 @2))
8645  (simplify
8646   (negate (IFN_FNMA@3 @0 @1 @2))
8647   (if (single_use (@3))
8648    (IFN_FMS @0 @1 @2)))
8650  (simplify
8651   (IFN_FNMS:c (negate @0) @1 @2)
8652   (IFN_FMS @0 @1 @2))
8653  (simplify
8654   (IFN_FNMS @0 @1 (negate @2))
8655   (IFN_FNMA @0 @1 @2))
8656  (simplify
8657   (IFN_FNMS:c (negate @0) @1 (negate @2))
8658   (IFN_FMA @0 @1 @2))
8659  (simplify
8660   (negate (IFN_FNMS@3 @0 @1 @2))
8661   (if (single_use (@3))
8662    (IFN_FMA @0 @1 @2))))
8664 /* CLZ simplifications.  */
8665 (for clz (CLZ)
8666  (for op (eq ne)
8667       cmp (lt ge)
8668   (simplify
8669    (op (clz:s@2 @0) INTEGER_CST@1)
8670    (if (integer_zerop (@1) && single_use (@2))
8671     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8672     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8673      (cmp (convert:stype @0) { build_zero_cst (stype); }))
8674     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8675     (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8676      (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8677 (for op (eq ne)
8678      cmp (lt ge)
8679  (simplify
8680   (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8681   (if (integer_zerop (@1) && single_use (@2))
8682    /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8683    (with { tree type0 = TREE_TYPE (@0);
8684            tree stype = signed_type_for (TREE_TYPE (@0));
8685            /* Punt if clz(0) == 0.  */
8686            if (integer_zerop (@3))
8687              stype = NULL_TREE;
8688          }
8689     (if (stype)
8690      (cmp (convert:stype @0) { build_zero_cst (stype); })))
8691    /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8692    (with { bool ok = true;
8693            tree type0 = TREE_TYPE (@0);
8694            /* Punt if clz(0) == prec - 1.  */
8695            if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8696              ok = false;
8697          }
8698     (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8699      (op @0 { build_one_cst (type0); }))))))
8701 /* CTZ simplifications.  */
8702 (for ctz (CTZ)
8703  (for op (ge gt le lt)
8704       cmp (eq eq ne ne)
8705   (simplify
8706    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8707    (op (ctz:s @0) INTEGER_CST@1)
8708     (with { bool ok = true;
8709             HOST_WIDE_INT val = 0;
8710             if (!tree_fits_shwi_p (@1))
8711               ok = false;
8712             else
8713               {
8714                 val = tree_to_shwi (@1);
8715                 /* Canonicalize to >= or <.  */
8716                 if (op == GT_EXPR || op == LE_EXPR)
8717                   {
8718                     if (val == HOST_WIDE_INT_MAX)
8719                       ok = false;
8720                     else
8721                       val++;
8722                   }
8723               }
8724             tree type0 = TREE_TYPE (@0);
8725             int prec = TYPE_PRECISION (type0);
8726           }
8727      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8728       (if (val <= 0)
8729        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8730        (if (val >= prec)
8731         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8732         (cmp (bit_and @0 { wide_int_to_tree (type0,
8733                                              wi::mask (val, false, prec)); })
8734              { build_zero_cst (type0); })))))))
8735  (for op (eq ne)
8736   (simplify
8737    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8738    (op (ctz:s @0) INTEGER_CST@1)
8739     (with { tree type0 = TREE_TYPE (@0);
8740             int prec = TYPE_PRECISION (type0);
8741           }
8742      (if (prec <= MAX_FIXED_MODE_SIZE)
8743       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8744        { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8745        (op (bit_and @0 { wide_int_to_tree (type0,
8746                                            wi::mask (tree_to_uhwi (@1) + 1,
8747                                                      false, prec)); })
8748            { wide_int_to_tree (type0,
8749                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8750                                                  false, prec)); })))))))
8751 (for op (ge gt le lt)
8752      cmp (eq eq ne ne)
8753  (simplify
8754   /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8755   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8756    (with { bool ok = true;
8757            HOST_WIDE_INT val = 0;
8758            if (!tree_fits_shwi_p (@1))
8759              ok = false;
8760            else
8761              {
8762                val = tree_to_shwi (@1);
8763                /* Canonicalize to >= or <.  */
8764                if (op == GT_EXPR || op == LE_EXPR)
8765                  {
8766                    if (val == HOST_WIDE_INT_MAX)
8767                      ok = false;
8768                    else
8769                      val++;
8770                  }
8771              }
8772            HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8773            tree type0 = TREE_TYPE (@0);
8774            int prec = TYPE_PRECISION (type0);
8775            if (prec > MAX_FIXED_MODE_SIZE)
8776              ok = false;
8777           }
8778      (if (val <= 0)
8779       (if (ok && zero_val >= val)
8780        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8781       (if (val >= prec)
8782        (if (ok && zero_val < val)
8783         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8784        (if (ok && (zero_val < 0 || zero_val >= prec))
8785         (cmp (bit_and @0 { wide_int_to_tree (type0,
8786                                              wi::mask (val, false, prec)); })
8787              { build_zero_cst (type0); })))))))
8788 (for op (eq ne)
8789  (simplify
8790   /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8791   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8792    (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8793            tree type0 = TREE_TYPE (@0);
8794            int prec = TYPE_PRECISION (type0);
8795          }
8796     (if (prec <= MAX_FIXED_MODE_SIZE)
8797      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8798       (if (zero_val != wi::to_widest (@1))
8799        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8800       (if (zero_val < 0 || zero_val >= prec)
8801        (op (bit_and @0 { wide_int_to_tree (type0,
8802                                            wi::mask (tree_to_uhwi (@1) + 1,
8803                                                      false, prec)); })
8804            { wide_int_to_tree (type0,
8805                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8806                                                  false, prec)); })))))))
8808 #if GIMPLE
8809 /* ctz(ext(X)) == ctz(X).  Valid just for the UB at zero cases though.  */
8810 (simplify
8811   (CTZ (convert@1 @0))
8812   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8813        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8814        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8815    (with { combined_fn cfn = CFN_LAST;
8816            tree type0 = TREE_TYPE (@0);
8817            if (TREE_CODE (type0) == BITINT_TYPE)
8818              {
8819                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8820                  cfn = CFN_CTZ;
8821                else
8822                  type0
8823                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8824                                                      1);
8825              }
8826            type0 = unsigned_type_for (type0);
8827            if (cfn == CFN_LAST
8828                && direct_internal_fn_supported_p (IFN_CTZ, type0,
8829                                                   OPTIMIZE_FOR_BOTH))
8830              cfn = CFN_CTZ;
8831            if (cfn == CFN_LAST
8832                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8833                && !direct_internal_fn_supported_p (IFN_CTZ,
8834                                                    TREE_TYPE (@1),
8835                                                    OPTIMIZE_FOR_BOTH))
8836              {
8837                if (TYPE_PRECISION (type0)
8838                    == TYPE_PRECISION (unsigned_type_node))
8839                  cfn = CFN_BUILT_IN_CTZ;
8840                else if (TYPE_PRECISION (type0)
8841                         == TYPE_PRECISION (long_long_unsigned_type_node))
8842                  cfn = CFN_BUILT_IN_CTZLL;
8843              } }
8844     (if (cfn == CFN_CTZ)
8845      (IFN_CTZ (convert:type0 @0))
8846      (if (cfn == CFN_BUILT_IN_CTZ)
8847       (BUILT_IN_CTZ (convert:type0 @0))
8848       (if (cfn == CFN_BUILT_IN_CTZLL)
8849        (BUILT_IN_CTZLL (convert:type0 @0))))))))
8850 #endif
8852 /* POPCOUNT simplifications.  */
8853 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8854 (simplify
8855   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8856   (if (INTEGRAL_TYPE_P (type)
8857        && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8858                         widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8859            == 0))
8860    (with { tree utype = TREE_TYPE (@0);
8861            if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8862              utype = TREE_TYPE (@1); }
8863     (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8865 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8866 (for popcount (POPCOUNT)
8867   (for cmp (le eq ne gt)
8868        rep (eq eq ne ne)
8869     (simplify
8870       (cmp (popcount @0) integer_zerop)
8871       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8873 /* popcount(bswap(x)) is popcount(x).  */
8874 (for popcount (POPCOUNT)
8875   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8876               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8877     (simplify
8878       (popcount (convert?@0 (bswap:s@1 @2)))
8879       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8880            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8881         (with { tree type0 = TREE_TYPE (@0);
8882                 tree type1 = TREE_TYPE (@1);
8883                 unsigned int prec0 = TYPE_PRECISION (type0);
8884                 unsigned int prec1 = TYPE_PRECISION (type1); }
8885           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8886             (popcount (convert:type0 (convert:type1 @2)))))))))
8888 /* popcount(rotate(X Y)) is popcount(X).  */
8889 (for popcount (POPCOUNT)
8890   (for rot (lrotate rrotate)
8891     (simplify
8892       (popcount (convert?@0 (rot:s@1 @2 @3)))
8893       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8894            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8895            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8896         (with { tree type0 = TREE_TYPE (@0);
8897                 tree type1 = TREE_TYPE (@1);
8898                 unsigned int prec0 = TYPE_PRECISION (type0);
8899                 unsigned int prec1 = TYPE_PRECISION (type1); }
8900           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8901             (popcount (convert:type0 @2))))))))
8903 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8904 (simplify
8905   (bit_and (POPCOUNT @0) integer_onep)
8906   (PARITY @0))
8908 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8909 (simplify
8910   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8911   (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8913 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8914 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8915 (for popcount (POPCOUNT)
8916   (for log1 (bit_and bit_ior)
8917        log2 (bit_ior bit_and)
8918     (simplify
8919       (minus (plus:s (popcount:s @0) (popcount:s @1))
8920              (popcount:s (log1:cs @0 @1)))
8921       (popcount (log2 @0 @1)))
8922     (simplify
8923       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8924               (popcount:s @1))
8925       (popcount (log2 @0 @1)))))
8927 #if GIMPLE
8928 /* popcount(zext(X)) == popcount(X).  */
8929 (simplify
8930   (POPCOUNT (convert@1 @0))
8931   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8932        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8933        && TYPE_UNSIGNED (TREE_TYPE (@0))
8934        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8935    (with { combined_fn cfn = CFN_LAST;
8936            tree type0 = TREE_TYPE (@0);
8937            if (TREE_CODE (type0) == BITINT_TYPE)
8938              {
8939                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8940                  cfn = CFN_POPCOUNT;
8941                else
8942                  type0
8943                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8944                                                      1);
8945              }
8946            if (cfn == CFN_LAST
8947                && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8948                                                   OPTIMIZE_FOR_BOTH))
8949              cfn = CFN_POPCOUNT;
8950            if (cfn == CFN_LAST
8951                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8952                && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8953                                                    TREE_TYPE (@1),
8954                                                    OPTIMIZE_FOR_BOTH))
8955              {
8956                if (TYPE_PRECISION (type0)
8957                    == TYPE_PRECISION (unsigned_type_node))
8958                  cfn = CFN_BUILT_IN_POPCOUNT;
8959                else if (TYPE_PRECISION (type0)
8960                         == TYPE_PRECISION (long_long_unsigned_type_node))
8961                  cfn = CFN_BUILT_IN_POPCOUNTLL;
8962              } }
8963     (if (cfn == CFN_POPCOUNT)
8964      (IFN_POPCOUNT (convert:type0 @0))
8965      (if (cfn == CFN_BUILT_IN_POPCOUNT)
8966       (BUILT_IN_POPCOUNT (convert:type0 @0))
8967       (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8968        (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8969 #endif
8971 /* PARITY simplifications.  */
8972 /* parity(~X) is parity(X).  */
8973 (simplify
8974   (PARITY (bit_not @0))
8975   (PARITY @0))
8977 /* parity(bswap(x)) is parity(x).  */
8978 (for parity (PARITY)
8979   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8980               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8981     (simplify
8982       (parity (convert?@0 (bswap:s@1 @2)))
8983       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8984            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8985            && TYPE_PRECISION (TREE_TYPE (@0))
8986               >= TYPE_PRECISION (TREE_TYPE (@1)))
8987         (with { tree type0 = TREE_TYPE (@0);
8988                 tree type1 = TREE_TYPE (@1); }
8989           (parity (convert:type0 (convert:type1 @2))))))))
8991 /* parity(rotate(X Y)) is parity(X).  */
8992 (for parity (PARITY)
8993   (for rot (lrotate rrotate)
8994     (simplify
8995       (parity (convert?@0 (rot:s@1 @2 @3)))
8996       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8997            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8998            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8999            && TYPE_PRECISION (TREE_TYPE (@0))
9000               >= TYPE_PRECISION (TREE_TYPE (@1)))
9001         (with { tree type0 = TREE_TYPE (@0); }
9002           (parity (convert:type0 @2)))))))
9004 /* parity(X)^parity(Y) is parity(X^Y).  */
9005 (simplify
9006   (bit_xor (PARITY:s @0) (PARITY:s @1))
9007   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
9008    (PARITY (bit_xor @0 @1))
9009    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9010         && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9011     (with { tree utype = TREE_TYPE (@0);
9012             if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
9013               utype = TREE_TYPE (@1); }
9014      (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
9016 #if GIMPLE
9017 /* parity(zext(X)) == parity(X).  */
9018 /* parity(sext(X)) == parity(X) if the difference in precision is even.  */
9019 (simplify
9020   (PARITY (convert@1 @0))
9021   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9022        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9023        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
9024        && (TYPE_UNSIGNED (TREE_TYPE (@0))
9025            || ((TYPE_PRECISION (TREE_TYPE (@1))
9026                 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
9027    (with { combined_fn cfn = CFN_LAST;
9028            tree type0 = TREE_TYPE (@0);
9029            if (TREE_CODE (type0) == BITINT_TYPE)
9030              {
9031                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9032                  cfn = CFN_PARITY;
9033                else
9034                  type0
9035                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9036                                                      1);
9037              }
9038            type0 = unsigned_type_for (type0);
9039            if (cfn == CFN_LAST
9040                && direct_internal_fn_supported_p (IFN_PARITY, type0,
9041                                                   OPTIMIZE_FOR_BOTH))
9042              cfn = CFN_PARITY;
9043            if (cfn == CFN_LAST
9044                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9045                && !direct_internal_fn_supported_p (IFN_PARITY,
9046                                                    TREE_TYPE (@1),
9047                                                    OPTIMIZE_FOR_BOTH))
9048              {
9049                if (TYPE_PRECISION (type0)
9050                    == TYPE_PRECISION (unsigned_type_node))
9051                  cfn = CFN_BUILT_IN_PARITY;
9052                else if (TYPE_PRECISION (type0)
9053                         == TYPE_PRECISION (long_long_unsigned_type_node))
9054                  cfn = CFN_BUILT_IN_PARITYLL;
9055              } }
9056     (if (cfn == CFN_PARITY)
9057      (IFN_PARITY (convert:type0 @0))
9058      (if (cfn == CFN_BUILT_IN_PARITY)
9059       (BUILT_IN_PARITY (convert:type0 @0))
9060       (if (cfn == CFN_BUILT_IN_PARITYLL)
9061        (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9062 #endif
9064 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9065 (for func (POPCOUNT BSWAP FFS PARITY)
9066  (simplify
9067   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9068   @3))
9070 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9071    where CST is precision-1. */
9072 (for func (CLRSB)
9073  (simplify
9074   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9075   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9076    @4)))
9078 #if GIMPLE
9079 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9080 (for func (CLZ)
9081  (simplify
9082   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9083   (with { int val;
9084           internal_fn ifn = IFN_LAST;
9085           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9086             {
9087               if (tree_fits_shwi_p (@2))
9088                 {
9089                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9090                   if ((int) valw == valw)
9091                     {
9092                       val = valw;
9093                       ifn = IFN_CLZ;
9094                     }
9095                 }
9096             }
9097           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9098                                                    OPTIMIZE_FOR_BOTH)
9099                    && CLZ_DEFINED_VALUE_AT_ZERO
9100                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9101             ifn = IFN_CLZ;
9102         }
9103    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9104     (IFN_CLZ @3 @2)))))
9105 (simplify
9106  (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9107   (with { int val;
9108           internal_fn ifn = IFN_LAST;
9109           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9110             ifn = IFN_CLZ;
9111           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9112                                                    OPTIMIZE_FOR_BOTH))
9113             ifn = IFN_CLZ;
9114         }
9115    (if (ifn == IFN_CLZ)
9116     (IFN_CLZ @3 @2))))
9118 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9119 (for func (CTZ)
9120  (simplify
9121   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9122   (with { int val;
9123           internal_fn ifn = IFN_LAST;
9124           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9125             {
9126               if (tree_fits_shwi_p (@2))
9127                 {
9128                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9129                   if ((int) valw == valw)
9130                     {
9131                       val = valw;
9132                       ifn = IFN_CTZ;
9133                     }
9134                 }
9135             }
9136           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9137                                                    OPTIMIZE_FOR_BOTH)
9138                    && CTZ_DEFINED_VALUE_AT_ZERO
9139                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9140             ifn = IFN_CTZ;
9141         }
9142    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9143     (IFN_CTZ @3 @2)))))
9144 (simplify
9145  (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9146   (with { int val;
9147           internal_fn ifn = IFN_LAST;
9148           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9149             ifn = IFN_CTZ;
9150           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9151                                                    OPTIMIZE_FOR_BOTH))
9152             ifn = IFN_CTZ;
9153         }
9154    (if (ifn == IFN_CTZ)
9155     (IFN_CTZ @3 @2))))
9156 #endif
9158 /* Common POPCOUNT/PARITY simplifications.  */
9159 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
9160 (for pfun (POPCOUNT PARITY)
9161   (simplify
9162     (pfun @0)
9163     (if (INTEGRAL_TYPE_P (type))
9164      (with { wide_int nz = tree_nonzero_bits (@0); }
9165        (switch
9166          (if (nz == 1)
9167            (convert @0))
9168          (if (wi::popcount (nz) == 1)
9169            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9170              (convert (rshift:utype (convert:utype @0)
9171                                     { build_int_cst (integer_type_node,
9172                                                      wi::ctz (nz)); })))))))))
9174 #if GIMPLE
9175 /* 64- and 32-bits branchless implementations of popcount are detected:
9177    int popcount64c (uint64_t x)
9178    {
9179      x -= (x >> 1) & 0x5555555555555555ULL;
9180      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9181      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9182      return (x * 0x0101010101010101ULL) >> 56;
9183    }
9185    int popcount32c (uint32_t x)
9186    {
9187      x -= (x >> 1) & 0x55555555;
9188      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9189      x = (x + (x >> 4)) & 0x0f0f0f0f;
9190      return (x * 0x01010101) >> 24;
9191    }  */
9192 (simplify
9193  (rshift
9194   (mult
9195    (bit_and
9196     (plus:c
9197      (rshift @8 INTEGER_CST@5)
9198       (plus:c@8
9199        (bit_and @6 INTEGER_CST@7)
9200         (bit_and
9201          (rshift
9202           (minus@6 @0
9203            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9204           INTEGER_CST@10)
9205          INTEGER_CST@9)))
9206     INTEGER_CST@3)
9207    INTEGER_CST@2)
9208   INTEGER_CST@1)
9209   /* Check constants and optab.  */
9210   (with { unsigned prec = TYPE_PRECISION (type);
9211           int shift = (64 - prec) & 63;
9212           unsigned HOST_WIDE_INT c1
9213             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9214           unsigned HOST_WIDE_INT c2
9215             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9216           unsigned HOST_WIDE_INT c3
9217             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9218           unsigned HOST_WIDE_INT c4
9219             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9220    }
9221    (if (prec >= 16
9222         && prec <= 64
9223         && pow2p_hwi (prec)
9224         && TYPE_UNSIGNED (type)
9225         && integer_onep (@4)
9226         && wi::to_widest (@10) == 2
9227         && wi::to_widest (@5) == 4
9228         && wi::to_widest (@1) == prec - 8
9229         && tree_to_uhwi (@2) == c1
9230         && tree_to_uhwi (@3) == c2
9231         && tree_to_uhwi (@9) == c3
9232         && tree_to_uhwi (@7) == c3
9233         && tree_to_uhwi (@11) == c4)
9234     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9235                                          OPTIMIZE_FOR_BOTH))
9236      (convert (IFN_POPCOUNT:type @0))
9237      /* Try to do popcount in two halves.  PREC must be at least
9238         five bits for this to work without extension before adding.  */
9239      (with {
9240        tree half_type = NULL_TREE;
9241        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9242        int half_prec = 8;
9243        if (m.exists ()
9244            && m.require () != TYPE_MODE (type))
9245          {
9246            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9247            half_type = build_nonstandard_integer_type (half_prec, 1);
9248          }
9249        gcc_assert (half_prec > 2);
9250       }
9251       (if (half_type != NULL_TREE
9252            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9253                                               OPTIMIZE_FOR_BOTH))
9254        (convert (plus
9255          (IFN_POPCOUNT:half_type (convert @0))
9256          (IFN_POPCOUNT:half_type (convert (rshift @0
9257             { build_int_cst (integer_type_node, half_prec); } )))))))))))
9259 /* __builtin_ffs needs to deal on many targets with the possible zero
9260    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
9261    should lead to better code.  */
9262 (simplify
9263  (FFS tree_expr_nonzero_p@0)
9264  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9265       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9266                                          OPTIMIZE_FOR_SPEED))
9267   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9268    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9269 #endif
9271 (for ffs (FFS)
9272  /* __builtin_ffs (X) == 0 -> X == 0.
9273     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
9274  (for cmp (eq ne)
9275   (simplify
9276    (cmp (ffs@2 @0) INTEGER_CST@1)
9277     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9278      (switch
9279       (if (integer_zerop (@1))
9280        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9281       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9282        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9283       (if (single_use (@2))
9284        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9285                                             wi::mask (tree_to_uhwi (@1),
9286                                                       false, prec)); })
9287             { wide_int_to_tree (TREE_TYPE (@0),
9288                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9289                                                   false, prec)); }))))))
9291  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
9292  (for cmp (gt le)
9293       cmp2 (ne eq)
9294       cmp3 (eq ne)
9295       bit_op (bit_and bit_ior)
9296   (simplify
9297    (cmp (ffs@2 @0) INTEGER_CST@1)
9298     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9299      (switch
9300       (if (integer_zerop (@1))
9301        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9302       (if (tree_int_cst_sgn (@1) < 0)
9303        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9304       (if (wi::to_widest (@1) >= prec)
9305        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9306       (if (wi::to_widest (@1) == prec - 1)
9307        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9308                                     wi::shifted_mask (prec - 1, 1,
9309                                                       false, prec)); }))
9310       (if (single_use (@2))
9311        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9312                (cmp3 (bit_and @0
9313                               { wide_int_to_tree (TREE_TYPE (@0),
9314                                                   wi::mask (tree_to_uhwi (@1),
9315                                                   false, prec)); })
9316                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
9318 #if GIMPLE
9319 /* ffs(ext(X)) == ffs(X).  */
9320 (simplify
9321   (FFS (convert@1 @0))
9322   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9323        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9324        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9325    (with { combined_fn cfn = CFN_LAST;
9326            tree type0 = TREE_TYPE (@0);
9327            if (TREE_CODE (type0) == BITINT_TYPE)
9328              {
9329                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9330                  cfn = CFN_FFS;
9331                else
9332                  type0
9333                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9334                                                      0);
9335              }
9336            type0 = signed_type_for (type0);
9337            if (cfn == CFN_LAST
9338                && direct_internal_fn_supported_p (IFN_FFS, type0,
9339                                                   OPTIMIZE_FOR_BOTH))
9340              cfn = CFN_FFS;
9341            if (cfn == CFN_LAST
9342                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9343                && !direct_internal_fn_supported_p (IFN_FFS,
9344                                                    TREE_TYPE (@1),
9345                                                    OPTIMIZE_FOR_BOTH))
9346              {
9347                if (TYPE_PRECISION (type0)
9348                    == TYPE_PRECISION (integer_type_node))
9349                  cfn = CFN_BUILT_IN_FFS;
9350                else if (TYPE_PRECISION (type0)
9351                         == TYPE_PRECISION (long_long_integer_type_node))
9352                  cfn = CFN_BUILT_IN_FFSLL;
9353              } }
9354     (if (cfn == CFN_FFS)
9355      (IFN_FFS (convert:type0 @0))
9356      (if (cfn == CFN_BUILT_IN_FFS)
9357       (BUILT_IN_FFS (convert:type0 @0))
9358       (if (cfn == CFN_BUILT_IN_FFSLL)
9359        (BUILT_IN_FFSLL (convert:type0 @0))))))))
9360 #endif
9362 #if GIMPLE
9364 /* Simplify:
9365      a = op a1
9366      r = cond ? a : b
9367      --> r = .COND_FN (cond, a, b)
9368 and,
9369     a = op a1
9370     r = cond ? b : a
9371     --> r = .COND_FN (~cond, b, a).  */
9373 (for uncond_op (UNCOND_UNARY)
9374      cond_op (COND_UNARY)
9375  (simplify
9376   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9377    (with { tree op_type = TREE_TYPE (@3); }
9378     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9379         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9380      (cond_op @0 (view_convert @1) @2))))
9381  (simplify
9382   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9383    (with { tree op_type = TREE_TYPE (@3); }
9384     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9385         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9386      (cond_op (bit_not @0) (view_convert @2) @1)))))
9388 (for uncond_op (UNCOND_UNARY)
9389      cond_op (COND_LEN_UNARY)
9390  (simplify
9391   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9392    (with { tree op_type = TREE_TYPE (@3); }
9393     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9394         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9395      (cond_op @0 (view_convert @1) @2 @4 @5))))
9396  (simplify
9397   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9398    (with { tree op_type = TREE_TYPE (@3); }
9399     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9400         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9401      (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9403 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
9404 (simplify
9405  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9406  (if (canonicalize_math_after_vectorization_p ()
9407       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9408       && is_truth_type_for (type, TREE_TYPE (@0)))
9409  (if (integer_all_onesp (@1) && integer_zerop (@2))
9410   (IFN_COND_NOT @0 @3 @3))
9411   (if (integer_all_onesp (@2) && integer_zerop (@1))
9412    (IFN_COND_NOT (bit_not @0) @3 @3))))
9414 /* Simplify:
9416      a = a1 op a2
9417      r = c ? a : b;
9419    to:
9421      r = c ? a1 op a2 : b;
9423    if the target can do it in one go.  This makes the operation conditional
9424    on c, so could drop potentially-trapping arithmetic, but that's a valid
9425    simplification if the result of the operation isn't needed.
9427    Avoid speculatively generating a stand-alone vector comparison
9428    on targets that might not support them.  Any target implementing
9429    conditional internal functions must support the same comparisons
9430    inside and outside a VEC_COND_EXPR.  */
9432 (for uncond_op (UNCOND_BINARY)
9433      cond_op (COND_BINARY)
9434  (simplify
9435   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9436   (with { tree op_type = TREE_TYPE (@4); }
9437    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9438         && is_truth_type_for (op_type, TREE_TYPE (@0))
9439         && single_use (@4))
9440     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9441  (simplify
9442   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9443   (with { tree op_type = TREE_TYPE (@4); }
9444    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9445         && is_truth_type_for (op_type, TREE_TYPE (@0))
9446         && single_use (@4))
9447     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9449 (for uncond_op (UNCOND_BINARY)
9450      cond_op (COND_LEN_BINARY)
9451  (simplify
9452   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9453   (with { tree op_type = TREE_TYPE (@4); }
9454    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9455         && is_truth_type_for (op_type, TREE_TYPE (@0))
9456         && single_use (@4))
9457     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9458  (simplify
9459   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9460   (with { tree op_type = TREE_TYPE (@4); }
9461    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9462         && is_truth_type_for (op_type, TREE_TYPE (@0))
9463         && single_use (@4))
9464     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9466 /* Same for ternary operations.  */
9467 (for uncond_op (UNCOND_TERNARY)
9468      cond_op (COND_TERNARY)
9469  (simplify
9470   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9471   (with { tree op_type = TREE_TYPE (@5); }
9472    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9473         && is_truth_type_for (op_type, TREE_TYPE (@0))
9474         && single_use (@5))
9475     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9476  (simplify
9477   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9478   (with { tree op_type = TREE_TYPE (@5); }
9479    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9480         && is_truth_type_for (op_type, TREE_TYPE (@0))
9481         && single_use (@5))
9482     (view_convert (cond_op (bit_not @0) @2 @3 @4
9483                   (view_convert:op_type @1)))))))
9485 (for uncond_op (UNCOND_TERNARY)
9486      cond_op (COND_LEN_TERNARY)
9487  (simplify
9488   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9489   (with { tree op_type = TREE_TYPE (@5); }
9490    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9491         && is_truth_type_for (op_type, TREE_TYPE (@0))
9492         && single_use (@5))
9493     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9494  (simplify
9495   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9496   (with { tree op_type = TREE_TYPE (@5); }
9497    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9498         && is_truth_type_for (op_type, TREE_TYPE (@0))
9499         && single_use (@5))
9500     (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9501 #endif
9503 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9504    "else" value of an IFN_COND_*.  */
9505 (for cond_op (COND_BINARY)
9506  (simplify
9507   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9508   (with { tree op_type = TREE_TYPE (@3); }
9509    (if (element_precision (type) == element_precision (op_type))
9510     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9511  (simplify
9512   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9513   (with { tree op_type = TREE_TYPE (@5); }
9514    (if (inverse_conditions_p (@0, @2)
9515         && element_precision (type) == element_precision (op_type))
9516     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9518 /* Same for ternary operations.  */
9519 (for cond_op (COND_TERNARY)
9520  (simplify
9521   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9522   (with { tree op_type = TREE_TYPE (@4); }
9523    (if (element_precision (type) == element_precision (op_type))
9524     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9525  (simplify
9526   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9527   (with { tree op_type = TREE_TYPE (@6); }
9528    (if (inverse_conditions_p (@0, @2)
9529         && element_precision (type) == element_precision (op_type))
9530     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9532 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9533    "else" value of an IFN_COND_LEN_*.  */
9534 (for cond_len_op (COND_LEN_BINARY)
9535  (simplify
9536   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9537   (with { tree op_type = TREE_TYPE (@3); }
9538    (if (element_precision (type) == element_precision (op_type))
9539     (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9540  (simplify
9541   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9542   (with { tree op_type = TREE_TYPE (@5); }
9543    (if (inverse_conditions_p (@0, @2)
9544         && element_precision (type) == element_precision (op_type))
9545     (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9547 /* Same for ternary operations.  */
9548 (for cond_len_op (COND_LEN_TERNARY)
9549  (simplify
9550   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9551   (with { tree op_type = TREE_TYPE (@4); }
9552    (if (element_precision (type) == element_precision (op_type))
9553     (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9554  (simplify
9555   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9556   (with { tree op_type = TREE_TYPE (@6); }
9557    (if (inverse_conditions_p (@0, @2)
9558         && element_precision (type) == element_precision (op_type))
9559     (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9561 /* Detect simplication for a conditional reduction where
9563    a = mask1 ? b : 0
9564    c = mask2 ? d + a : d
9566    is turned into
9568    c = mask1 && mask2 ? d + b : d.  */
9569 (simplify
9570   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9571    (if (ANY_INTEGRAL_TYPE_P (type)
9572         || (FLOAT_TYPE_P (type)
9573             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9574    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9576 /* Detect simplication for a conditional length reduction where
9578    a = mask ? b : 0
9579    c = i < len + bias ? d + a : d
9581    is turned into
9583    c = mask && i < len + bias ? d + b : d.  */
9584 (simplify
9585   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9586    (if (ANY_INTEGRAL_TYPE_P (type)
9587         || (FLOAT_TYPE_P (type)
9588             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9589     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9591 /* Detect simplification for vector condition folding where
9593   c = mask1 ? (masked_op mask2 a b) : b
9595   into
9597   c = masked_op (mask1 & mask2) a b
9599   where the operation can be partially applied to one operand. */
9601 (for cond_op (COND_BINARY)
9602  (simplify
9603   (vec_cond @0
9604    (cond_op:s @1 @2 @3 @4) @3)
9605   (cond_op (bit_and @1 @0) @2 @3 @4)))
9607 /* And same for ternary expressions.  */
9609 (for cond_op (COND_TERNARY)
9610  (simplify
9611   (vec_cond @0
9612    (cond_op:s @1 @2 @3 @4 @5) @4)
9613   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9615 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9616    expressions like:
9618    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9619    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9621    If pointers are known not to wrap, B checks whether @1 bytes starting
9622    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9623    bytes.  A is more efficiently tested as:
9625    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9627    The equivalent expression for B is given by replacing @1 with @1 - 1:
9629    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9631    @0 and @2 can be swapped in both expressions without changing the result.
9633    The folds rely on sizetype's being unsigned (which is always true)
9634    and on its being the same width as the pointer (which we have to check).
9636    The fold replaces two pointer_plus expressions, two comparisons and
9637    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9638    the best case it's a saving of two operations.  The A fold retains one
9639    of the original pointer_pluses, so is a win even if both pointer_pluses
9640    are used elsewhere.  The B fold is a wash if both pointer_pluses are
9641    used elsewhere, since all we end up doing is replacing a comparison with
9642    a pointer_plus.  We do still apply the fold under those circumstances
9643    though, in case applying it to other conditions eventually makes one of the
9644    pointer_pluses dead.  */
9645 (for ior (truth_orif truth_or bit_ior)
9646  (for cmp (le lt)
9647   (simplify
9648    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9649         (cmp:cs (pointer_plus@4 @2 @1) @0))
9650    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9651         && TYPE_OVERFLOW_WRAPS (sizetype)
9652         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9653     /* Calculate the rhs constant.  */
9654     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9655             offset_int rhs = off * 2; }
9656      /* Always fails for negative values.  */
9657      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9658       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9659          pick a canonical order.  This increases the chances of using the
9660          same pointer_plus in multiple checks.  */
9661       (with { bool swap_p = tree_swap_operands_p (@0, @2);
9662               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9663        (if (cmp == LT_EXPR)
9664         (gt (convert:sizetype
9665              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9666                                      { swap_p ? @0 : @2; }))
9667             { rhs_tree; })
9668         (gt (convert:sizetype
9669              (pointer_diff:ssizetype
9670               (pointer_plus { swap_p ? @2 : @0; }
9671                             { wide_int_to_tree (sizetype, off); })
9672               { swap_p ? @0 : @2; }))
9673             { rhs_tree; })))))))))
9675 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9676    element of @1.  */
9677 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9678  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9679   (with { int i = single_nonzero_element (@1); }
9680    (if (i >= 0)
9681     (with { tree elt = vector_cst_elt (@1, i);
9682             tree elt_type = TREE_TYPE (elt);
9683             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9684             tree size = bitsize_int (elt_bits);
9685             tree pos = bitsize_int (elt_bits * i); }
9686      (view_convert
9687       (bit_and:elt_type
9688        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9689        { elt; })))))))
9691 /* Fold reduction of a single nonzero element constructor.  */
9692 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9693   (simplify (reduc (CONSTRUCTOR@0))
9694     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9695                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9696             tree elt = ctor_single_nonzero_element (ctor); }
9697       (if (elt
9698            && !HONOR_SNANS (type)
9699            && !HONOR_SIGNED_ZEROS (type))
9700         { elt; }))))
9702 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
9703 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9704             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9705      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9706   (simplify (reduc (op @0 VECTOR_CST@1))
9707     (op (reduc:type @0) (reduc:type @1))))
9709 /* Simplify vector floating point operations of alternating sub/add pairs
9710    into using an fneg of a wider element type followed by a normal add.
9711    under IEEE 754 the fneg of the wider type will negate every even entry
9712    and when doing an add we get a sub of the even and add of every odd
9713    elements.  */
9714 (for plusminus (plus minus)
9715      minusplus (minus plus)
9716  (simplify
9717   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9718    (if (!VECTOR_INTEGER_TYPE_P (type)
9719         && !FLOAT_WORDS_BIG_ENDIAN
9720         /* plus is commutative, while minus is not, so :c can't be used.
9721            Do equality comparisons by hand and at the end pick the operands
9722            from the minus.  */
9723         && (operand_equal_p (@0, @2, 0)
9724             ? operand_equal_p (@1, @3, 0)
9725             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9726    (with
9727     {
9728       /* Build a vector of integers from the tree mask.  */
9729       vec_perm_builder builder;
9730     }
9731     (if (tree_to_vec_perm_builder (&builder, @4))
9732      (with
9733       {
9734         /* Create a vec_perm_indices for the integer vector.  */
9735         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9736         vec_perm_indices sel (builder, 2, nelts);
9737         machine_mode vec_mode = TYPE_MODE (type);
9738         machine_mode wide_mode;
9739         scalar_mode wide_elt_mode;
9740         poly_uint64 wide_nunits;
9741         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9742       }
9743       (if (VECTOR_MODE_P (vec_mode)
9744            && sel.series_p (0, 2, 0, 2)
9745            && sel.series_p (1, 2, nelts + 1, 2)
9746            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9747            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9748            && related_vector_mode (vec_mode, wide_elt_mode,
9749                                    wide_nunits).exists (&wide_mode))
9750        (with
9751         {
9752           tree stype
9753             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9754                                               TYPE_UNSIGNED (type));
9755           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9757           /* The format has to be a non-extended ieee format.  */
9758           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9759           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9760         }
9761         (if (TYPE_MODE (stype) != BLKmode
9762              && VECTOR_TYPE_P (ntype)
9763              && fmt_old != NULL
9764              && fmt_new != NULL)
9765          (with
9766           {
9767             /* If the target doesn't support v1xx vectors, try using
9768                scalar mode xx instead.  */
9769             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9770                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9771               ntype = stype;
9772           }
9773           (if (fmt_new->signbit_rw
9774                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9775                && fmt_new->signbit_rw == fmt_new->signbit_ro
9776                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9777                                                  TYPE_MODE (type), ALL_REGS)
9778                && ((optimize_vectors_before_lowering_p ()
9779                     && VECTOR_TYPE_P (ntype))
9780                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9781            (if (plusminus == PLUS_EXPR)
9782             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9783             (minus @0 (view_convert:type
9784                         (negate (view_convert:ntype @1))))))))))))))))
9786 (simplify
9787  (vec_perm @0 @1 VECTOR_CST@2)
9788  (with
9789   {
9790     tree op0 = @0, op1 = @1, op2 = @2;
9791     machine_mode result_mode = TYPE_MODE (type);
9792     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9794     /* Build a vector of integers from the tree mask.  */
9795     vec_perm_builder builder;
9796   }
9797   (if (tree_to_vec_perm_builder (&builder, op2))
9798    (with
9799     {
9800       /* Create a vec_perm_indices for the integer vector.  */
9801       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9802       bool single_arg = (op0 == op1);
9803       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9804     }
9805     (if (sel.series_p (0, 1, 0, 1))
9806      { op0; }
9807      (if (sel.series_p (0, 1, nelts, 1))
9808       { op1; }
9809       (with
9810        {
9811          if (!single_arg)
9812            {
9813              if (sel.all_from_input_p (0))
9814                op1 = op0;
9815              else if (sel.all_from_input_p (1))
9816                {
9817                  op0 = op1;
9818                  sel.rotate_inputs (1);
9819                }
9820              else if (known_ge (poly_uint64 (sel[0]), nelts))
9821                {
9822                  std::swap (op0, op1);
9823                  sel.rotate_inputs (1);
9824                }
9825            }
9826          gassign *def;
9827          tree cop0 = op0, cop1 = op1;
9828          if (TREE_CODE (op0) == SSA_NAME
9829              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9830              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9831            cop0 = gimple_assign_rhs1 (def);
9832          if (TREE_CODE (op1) == SSA_NAME
9833              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9834              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9835            cop1 = gimple_assign_rhs1 (def);
9836          tree t;
9837        }
9838        (if ((TREE_CODE (cop0) == VECTOR_CST
9839              || TREE_CODE (cop0) == CONSTRUCTOR)
9840             && (TREE_CODE (cop1) == VECTOR_CST
9841                 || TREE_CODE (cop1) == CONSTRUCTOR)
9842             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9843         { t; }
9844         (with
9845          {
9846            bool changed = (op0 == op1 && !single_arg);
9847            tree ins = NULL_TREE;
9848            unsigned at = 0;
9850            /* See if the permutation is performing a single element
9851               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9852               in that case.  But only if the vector mode is supported,
9853               otherwise this is invalid GIMPLE.  */
9854            if (op_mode != BLKmode
9855                && (TREE_CODE (cop0) == VECTOR_CST
9856                    || TREE_CODE (cop0) == CONSTRUCTOR
9857                    || TREE_CODE (cop1) == VECTOR_CST
9858                    || TREE_CODE (cop1) == CONSTRUCTOR))
9859              {
9860                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9861                if (insert_first_p)
9862                  {
9863                    /* After canonicalizing the first elt to come from the
9864                       first vector we only can insert the first elt from
9865                       the first vector.  */
9866                    at = 0;
9867                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9868                      op0 = op1;
9869                  }
9870                /* The above can fail for two-element vectors which always
9871                   appear to insert the first element, so try inserting
9872                   into the second lane as well.  For more than two
9873                   elements that's wasted time.  */
9874                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9875                  {
9876                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9877                    for (at = 0; at < encoded_nelts; ++at)
9878                      if (maybe_ne (sel[at], at))
9879                        break;
9880                    if (at < encoded_nelts
9881                        && (known_eq (at + 1, nelts)
9882                            || sel.series_p (at + 1, 1, at + 1, 1)))
9883                      {
9884                        if (known_lt (poly_uint64 (sel[at]), nelts))
9885                          ins = fold_read_from_vector (cop0, sel[at]);
9886                        else
9887                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9888                      }
9889                  }
9890              }
9892            /* Generate a canonical form of the selector.  */
9893            if (!ins && sel.encoding () != builder)
9894              {
9895                /* Some targets are deficient and fail to expand a single
9896                   argument permutation while still allowing an equivalent
9897                   2-argument version.  */
9898                tree oldop2 = op2;
9899                if (sel.ninputs () == 2
9900                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9901                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9902                else
9903                  {
9904                    vec_perm_indices sel2 (builder, 2, nelts);
9905                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9906                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9907                    else
9908                      /* Not directly supported with either encoding,
9909                         so use the preferred form.  */
9910                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9911                  }
9912                if (!operand_equal_p (op2, oldop2, 0))
9913                  changed = true;
9914              }
9915          }
9916          (if (ins)
9917           (bit_insert { op0; } { ins; }
9918            { bitsize_int (at * vector_element_bits (type)); })
9919           (if (changed)
9920            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9922 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9924 (match vec_same_elem_p
9925  (vec_duplicate @0))
9927 (match vec_same_elem_p
9928  CONSTRUCTOR@0
9929  (if (TREE_CODE (@0) == SSA_NAME
9930       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9932 (match vec_same_elem_p
9933  @0
9934  (if (uniform_vector_p (@0))))
9937 (simplify
9938  (vec_perm vec_same_elem_p@0 @0 @1)
9939  (if (types_match (type, TREE_TYPE (@0)))
9940   @0
9941   (with
9942    {
9943      tree elem = uniform_vector_p (@0);
9944    }
9945    (if (elem)
9946     { build_vector_from_val (type, elem); }))))
9948 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9949 (simplify
9950  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9951  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9952   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9953 (simplify
9954  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9955  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9956   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9959 /* Merge
9960      c = VEC_PERM_EXPR <a, b, VCST0>;
9961      d = VEC_PERM_EXPR <c, c, VCST1>;
9962    to
9963      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9965 (simplify
9966  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9967  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9968   (with
9969    {
9970      machine_mode result_mode = TYPE_MODE (type);
9971      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9972      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9973      vec_perm_builder builder0;
9974      vec_perm_builder builder1;
9975      vec_perm_builder builder2 (nelts, nelts, 1);
9976    }
9977    (if (tree_to_vec_perm_builder (&builder0, @3)
9978         && tree_to_vec_perm_builder (&builder1, @4))
9979     (with
9980      {
9981        vec_perm_indices sel0 (builder0, 2, nelts);
9982        vec_perm_indices sel1 (builder1, 1, nelts);
9984        for (int i = 0; i < nelts; i++)
9985          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9987        vec_perm_indices sel2 (builder2, 2, nelts);
9989        tree op0 = NULL_TREE;
9990        /* If the new VEC_PERM_EXPR can't be handled but both
9991           original VEC_PERM_EXPRs can, punt.
9992           If one or both of the original VEC_PERM_EXPRs can't be
9993           handled and the new one can't be either, don't increase
9994           number of VEC_PERM_EXPRs that can't be handled.  */
9995        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9996            || (single_use (@0)
9997                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9998                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9999                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10000          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10001      }
10002      (if (op0)
10003       (vec_perm @1 @2 { op0; })))))))
10005 /* Merge
10006      c = VEC_PERM_EXPR <a, b, VCST0>;
10007      d = VEC_PERM_EXPR <x, c, VCST1>;
10008    to
10009      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
10010    when all elements from a or b are replaced by the later
10011    permutation.  */
10013 (simplify
10014  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
10015  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10016   (with
10017    {
10018      machine_mode result_mode = TYPE_MODE (type);
10019      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10020      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10021      vec_perm_builder builder0;
10022      vec_perm_builder builder1;
10023      vec_perm_builder builder2 (nelts, nelts, 2);
10024    }
10025    (if (tree_to_vec_perm_builder (&builder0, @3)
10026         && tree_to_vec_perm_builder (&builder1, @4))
10027     (with
10028      {
10029        vec_perm_indices sel0 (builder0, 2, nelts);
10030        vec_perm_indices sel1 (builder1, 2, nelts);
10031        bool use_1 = false, use_2 = false;
10033        for (int i = 0; i < nelts; i++)
10034          {
10035            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10036              builder2.quick_push (sel1[i]);
10037            else
10038              {
10039                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10040                                     .to_constant ()];
10041                if (known_lt (j, sel0.nelts_per_input ()))
10042                  use_1 = true;
10043                else
10044                  {
10045                    use_2 = true;
10046                    j -= sel0.nelts_per_input ();
10047                  }
10048                builder2.quick_push (j + sel1.nelts_per_input ());
10049              }
10050          }
10051      }
10052      (if (use_1 ^ use_2)
10053       (with
10054        {
10055          vec_perm_indices sel2 (builder2, 2, nelts);
10056          tree op0 = NULL_TREE;
10057          /* If the new VEC_PERM_EXPR can't be handled but both
10058             original VEC_PERM_EXPRs can, punt.
10059             If one or both of the original VEC_PERM_EXPRs can't be
10060             handled and the new one can't be either, don't increase
10061             number of VEC_PERM_EXPRs that can't be handled.  */
10062          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10063              || (single_use (@0)
10064                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10065                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10066                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10067            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10068        }
10069        (if (op0)
10070         (switch
10071          (if (use_1)
10072           (vec_perm @5 @1 { op0; }))
10073          (if (use_2)
10074           (vec_perm @5 @2 { op0; })))))))))))
10076 /* And the case with swapped outer permute sources.  */
10078 (simplify
10079  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10080  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10081   (with
10082    {
10083      machine_mode result_mode = TYPE_MODE (type);
10084      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10085      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10086      vec_perm_builder builder0;
10087      vec_perm_builder builder1;
10088      vec_perm_builder builder2 (nelts, nelts, 2);
10089    }
10090    (if (tree_to_vec_perm_builder (&builder0, @3)
10091         && tree_to_vec_perm_builder (&builder1, @4))
10092     (with
10093      {
10094        vec_perm_indices sel0 (builder0, 2, nelts);
10095        vec_perm_indices sel1 (builder1, 2, nelts);
10096        bool use_1 = false, use_2 = false;
10098        for (int i = 0; i < nelts; i++)
10099          {
10100            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10101              builder2.quick_push (sel1[i]);
10102            else
10103              {
10104                poly_uint64 j = sel0[sel1[i].to_constant ()];
10105                if (known_lt (j, sel0.nelts_per_input ()))
10106                  use_1 = true;
10107                else
10108                  {
10109                    use_2 = true;
10110                    j -= sel0.nelts_per_input ();
10111                  }
10112                builder2.quick_push (j);
10113              }
10114          }
10115      }
10116      (if (use_1 ^ use_2)
10117       (with
10118        {
10119          vec_perm_indices sel2 (builder2, 2, nelts);
10120          tree op0 = NULL_TREE;
10121          /* If the new VEC_PERM_EXPR can't be handled but both
10122             original VEC_PERM_EXPRs can, punt.
10123             If one or both of the original VEC_PERM_EXPRs can't be
10124             handled and the new one can't be either, don't increase
10125             number of VEC_PERM_EXPRs that can't be handled.  */
10126          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10127              || (single_use (@0)
10128                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10129                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10130                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10131            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10132        }
10133        (if (op0)
10134         (switch
10135          (if (use_1)
10136           (vec_perm @1 @5 { op0; }))
10137          (if (use_2)
10138           (vec_perm @2 @5 { op0; })))))))))))
10141 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10142    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10143    constant which when multiplied by a power of 2 contains a unique value
10144    in the top 5 or 6 bits.  This is then indexed into a table which maps it
10145    to the number of trailing zeroes.  */
10146 (match (ctz_table_index @1 @2 @3)
10147   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10149 (match (cond_expr_convert_p @0 @2 @3 @6)
10150  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10151   (if (INTEGRAL_TYPE_P (type)
10152        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10153        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10154        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10155        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10156        && TYPE_PRECISION (TREE_TYPE (@0))
10157           == TYPE_PRECISION (TREE_TYPE (@2))
10158        && TYPE_PRECISION (TREE_TYPE (@0))
10159           == TYPE_PRECISION (TREE_TYPE (@3))
10160        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10161           signess when convert is truncation, but not ok for extension since
10162           it's sign_extend vs zero_extend.  */
10163        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10164            || (TYPE_UNSIGNED (TREE_TYPE (@2))
10165                == TYPE_UNSIGNED (TREE_TYPE (@3))))
10166        && single_use (@4)
10167        && single_use (@5))))
10169 (for bit_op (bit_and bit_ior bit_xor)
10170  (match (bitwise_induction_p @0 @2 @3)
10171   (bit_op:c
10172    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10173    @3)))
10175 (match (bitwise_induction_p @0 @2 @3)
10176  (bit_not
10177   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10179 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
10180    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
10181 (simplify
10182   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10183   (with { auto i = wi::neg (wi::to_wide (@2)); }
10184   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
10185     (if (wi::popcount (i) == 1
10186          && (wi::to_wide (@1)) == (i - 1))
10187       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10188         (bit_and @0 @1)
10189       (cond (le @0 @1) @0 (bit_and @0 @1))))))
10191 /* -x & 1 -> x & 1.  */
10192 (simplify 
10193  (bit_and (negate @0) integer_onep@1)
10194  (if (!TYPE_OVERFLOW_SANITIZED (type))
10195   (bit_and @0 @1)))
10197 /* `-a` is just `a` if the type is 1bit wide or when converting
10198    to a 1bit type; similar to the above transformation of `(-x)&1`.
10199    This is used mostly with the transformation of
10200    `a ? ~b : b` into `(-a)^b`.
10201    It also can show up with bitfields.  */
10202 (simplify
10203  (convert? (negate @0))
10204  (if (INTEGRAL_TYPE_P (type)
10205       && TYPE_PRECISION (type) == 1
10206       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10207   (convert @0)))
10209 /* Optimize
10210    c1 = VEC_PERM_EXPR (a, a, mask)
10211    c2 = VEC_PERM_EXPR (b, b, mask)
10212    c3 = c1 op c2
10213    -->
10214    c = a op b
10215    c3 = VEC_PERM_EXPR (c, c, mask)
10216    For all integer non-div operations.  */
10217 (for op (plus minus mult bit_and bit_ior bit_xor
10218          lshift rshift)
10219  (simplify
10220   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10221    (if (VECTOR_INTEGER_TYPE_P (type))
10222     (vec_perm (op@3 @0 @1) @3 @2))))
10224 /* Similar for float arithmetic when permutation constant covers
10225    all vector elements.  */
10226 (for op (plus minus mult)
10227  (simplify
10228   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10229    (if (VECTOR_FLOAT_TYPE_P (type)
10230         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10231     (with
10232      {
10233        tree perm_cst = @2;
10234        vec_perm_builder builder;
10235        bool full_perm_p = false;
10236        if (tree_to_vec_perm_builder (&builder, perm_cst))
10237          {
10238            unsigned HOST_WIDE_INT nelts;
10240            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10241            /* Create a vec_perm_indices for the VECTOR_CST.  */
10242            vec_perm_indices sel (builder, 1, nelts);
10244            /* Check if perm indices covers all vector elements.  */
10245            if (sel.encoding ().encoded_full_vector_p ())
10246              {
10247                auto_sbitmap seen (nelts);
10248                bitmap_clear (seen);
10250                unsigned HOST_WIDE_INT count = 0, i;
10252                for (i = 0; i < nelts; i++)
10253                  {
10254                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10255                     break;
10256                    count++;
10257                  }
10258                full_perm_p = count == nelts;
10259              }
10260          }
10261       }
10262       (if (full_perm_p)
10263         (vec_perm (op@3 @0 @1) @3 @2))))))