hppa: Fix LO_SUM DLTIND14R address support in PRINT_OPERAND_ADDRESS
[official-gcc.git] / gcc / match.pd
blob15a1e7350d4a896758204b00638b53e84045cb13
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  (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
458   (abs @0)))
460 /* X * 1, X / 1 -> X.  */
461 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
462   (simplify
463     (op @0 integer_onep)
464     (non_lvalue @0)))
466 /* (A / (1 << B)) -> (A >> B).
467    Only for unsigned A.  For signed A, this would not preserve rounding
468    toward zero.
469    For example: (-1 / ( 1 << B)) !=  -1 >> B.
470    Also handle widening conversions, like:
471    (A / (unsigned long long) (1U << B)) -> (A >> B)
472    or
473    (A / (unsigned long long) (1 << B)) -> (A >> B).
474    If the left shift is signed, it can be done only if the upper bits
475    of A starting from shift's type sign bit are zero, as
476    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
477    so it is valid only if A >> 31 is zero.  */
478 (simplify
479  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
480  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
481       && (!VECTOR_TYPE_P (type)
482           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
483           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
484       && (useless_type_conversion_p (type, TREE_TYPE (@1))
485           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
486               && (TYPE_UNSIGNED (TREE_TYPE (@1))
487                   || (element_precision (type)
488                       == element_precision (TREE_TYPE (@1)))
489                   || (INTEGRAL_TYPE_P (type)
490                       && (tree_nonzero_bits (@0)
491                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
492                                       true,
493                                       element_precision (type))) == 0)))))
494    (if (!VECTOR_TYPE_P (type)
495         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
496         && element_precision (TREE_TYPE (@3)) < element_precision (type))
497     (convert (rshift @3 @2))
498     (rshift @0 @2))))
500 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
501    undefined behavior in constexpr evaluation, and assuming that the division
502    traps enables better optimizations than these anyway.  */
503 (for div (trunc_div ceil_div floor_div round_div exact_div)
504  /* 0 / X is always zero.  */
505  (simplify
506   (div integer_zerop@0 @1)
507   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
508   (if (!integer_zerop (@1))
509    @0))
510  /* X / -1 is -X.  */
511  (simplify
512   (div @0 integer_minus_onep@1)
513   (if (!TYPE_UNSIGNED (type))
514    (negate @0)))
515  /* X / bool_range_Y is X.  */ 
516  (simplify
517   (div @0 SSA_NAME@1)
518   (if (INTEGRAL_TYPE_P (type)
519        && ssa_name_has_boolean_range (@1)
520        && !flag_non_call_exceptions)
521    @0))
522  /* X / X is one.  */
523  (simplify
524   (div @0 @0)
525   /* But not for 0 / 0 so that we can get the proper warnings and errors.
526      And not for _Fract types where we can't build 1.  */
527   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
528        && !integer_zerop (@0)
529        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
530    { build_one_cst (type); }))
531  /* X / abs (X) is X < 0 ? -1 : 1.  */
532  (simplify
533    (div:C @0 (abs @0))
534    (if (INTEGRAL_TYPE_P (type)
535         && TYPE_OVERFLOW_UNDEFINED (type)
536         && !integer_zerop (@0)
537         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
538     (cond (lt @0 { build_zero_cst (type); })
539           { build_minus_one_cst (type); } { build_one_cst (type); })))
540  /* X / -X is -1.  */
541  (simplify
542    (div:C @0 (negate @0))
543    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
544         && TYPE_OVERFLOW_UNDEFINED (type)
545         && !integer_zerop (@0)
546         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
547     { build_minus_one_cst (type); })))
549 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
550    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
551    for MOD instead of DIV.  */
552 (for floor_divmod (floor_div floor_mod)
553      trunc_divmod (trunc_div trunc_mod)
554  (simplify
555   (floor_divmod @0 @1)
556   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
557        && TYPE_UNSIGNED (type))
558    (trunc_divmod @0 @1))))
560 /* 1 / X -> X == 1 for unsigned integer X.
561    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
562    But not for 1 / 0 so that we can get proper warnings and errors,
563    and not for 1-bit integers as they are edge cases better handled
564    elsewhere.  Delay the conversion of the signed division until late
565    because `1 / X` is simplier to handle than the resulting COND_EXPR.  */
566 (simplify
567  (trunc_div integer_onep@0 @1)
568  (if (INTEGRAL_TYPE_P (type)
569       && TYPE_PRECISION (type) > 1
570       && !integer_zerop (@1)
571       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
572   (if (TYPE_UNSIGNED (type))
573    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
574    (if (!canonicalize_math_p ())
575     (with { tree utype = unsigned_type_for (type); }
576      (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
577                 { build_int_cst (utype, 2); })
578       @1 { build_zero_cst (type); }))))))
580 /* Combine two successive divisions.  Note that combining ceil_div
581    and floor_div is trickier and combining round_div even more so.  */
582 (for div (trunc_div exact_div)
583  (simplify
584   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
585   (with {
586     wi::overflow_type overflow;
587     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
588                             TYPE_SIGN (type), &overflow);
589    }
590    (if (div == EXACT_DIV_EXPR
591         || optimize_successive_divisions_p (@2, @3))
592     (if (!overflow)
593      (div @0 { wide_int_to_tree (type, mul); })
594      (if (TYPE_UNSIGNED (type)
595           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
596       { build_zero_cst (type); }))))))
598 /* Combine successive multiplications.  Similar to above, but handling
599    overflow is different.  */
600 (simplify
601  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
602  (with {
603    wi::overflow_type overflow;
604    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
605                            TYPE_SIGN (type), &overflow);
606   }
607   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
608      otherwise undefined overflow implies that @0 must be zero.  */
609   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
610    (mult @0 { wide_int_to_tree (type, mul); }))))
612 /* Similar to above, but there could be an extra add/sub between
613    successive multuiplications.  */
614 (simplify
615  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
616  (with {
617    bool overflowed = true;
618    wi::overflow_type ovf1, ovf2;
619    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
620                            TYPE_SIGN (type), &ovf1);
621    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
622                            TYPE_SIGN (type), &ovf2);
623   if (TYPE_OVERFLOW_UNDEFINED (type))
624     {
625 #if GIMPLE
626       value_range vr0;
627       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
628           && get_global_range_query ()->range_of_expr (vr0, @4)
629           && !vr0.varying_p () && !vr0.undefined_p ())
630         {
631           wide_int wmin0 = vr0.lower_bound ();
632           wide_int wmax0 = vr0.upper_bound ();
633           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
634           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
635           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
636             {
637               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
638               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
639               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
640                 overflowed = false;
641             }
642         }
643 #endif
644     }
645   else
646    overflowed = false;
648   /* Skip folding on overflow.  */
649   (if (!overflowed)
650    (plus (mult @0 { wide_int_to_tree (type, mul); })
651          { wide_int_to_tree (type, add); }))))
653 /* Similar to above, but a multiplication between successive additions.  */
654 (simplify
655  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
656  (with {
657    bool overflowed = true;
658    wi::overflow_type ovf1;
659    wi::overflow_type ovf2;
660    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
661                            TYPE_SIGN (type), &ovf1);
662    wide_int add = wi::add (mul, wi::to_wide (@3),
663                            TYPE_SIGN (type), &ovf2);
664   if (TYPE_OVERFLOW_UNDEFINED (type))
665     {
666 #if GIMPLE
667       value_range vr0;
668       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
669           && get_global_range_query ()->range_of_expr (vr0, @0)
670           && !vr0.varying_p () && !vr0.undefined_p ())
671         {
672           wide_int wmin0 = vr0.lower_bound ();
673           wide_int wmax0 = vr0.upper_bound ();
674           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
675           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
676           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
677             {
678               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
679               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
680               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
681                 overflowed = false;
682             }
683         }
684 #endif
685     }
686   else
687    overflowed = false;
689   /* Skip folding on overflow.  */
690   (if (!overflowed)
691    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
693 /* Optimize A / A to 1.0 if we don't care about
694    NaNs or Infinities.  */
695 (simplify
696  (rdiv @0 @0)
697  (if (FLOAT_TYPE_P (type)
698       && ! HONOR_NANS (type)
699       && ! HONOR_INFINITIES (type))
700   { build_one_cst (type); }))
702 /* Optimize -A / A to -1.0 if we don't care about
703    NaNs or Infinities.  */
704 (simplify
705  (rdiv:C @0 (negate @0))
706  (if (FLOAT_TYPE_P (type)
707       && ! HONOR_NANS (type)
708       && ! HONOR_INFINITIES (type))
709   { build_minus_one_cst (type); }))
711 /* PR71078: x / abs(x) -> copysign (1.0, x) */
712 (simplify
713  (rdiv:C (convert? @0) (convert? (abs @0)))
714   (if (SCALAR_FLOAT_TYPE_P (type)
715        && ! HONOR_NANS (type)
716        && ! HONOR_INFINITIES (type))
717    (switch
718     (if (types_match (type, float_type_node))
719      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
720     (if (types_match (type, double_type_node))
721      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
722     (if (types_match (type, long_double_type_node))
723      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
725 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
726 (simplify
727  (rdiv @0 real_onep)
728  (if (!tree_expr_maybe_signaling_nan_p (@0))
729   (non_lvalue @0)))
731 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
732 (simplify
733  (rdiv @0 real_minus_onep)
734  (if (!tree_expr_maybe_signaling_nan_p (@0))
735   (negate @0)))
737 (if (flag_reciprocal_math)
738  /* Convert (A/B)/C to A/(B*C). */
739  (simplify
740   (rdiv (rdiv:s @0 @1) @2)
741   (rdiv @0 (mult @1 @2)))
743  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
744  (simplify
745   (rdiv @0 (mult:s @1 REAL_CST@2))
746   (with
747    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
748    (if (tem)
749     (rdiv (mult @0 { tem; } ) @1))))
751  /* Convert A/(B/C) to (A/B)*C  */
752  (simplify
753   (rdiv @0 (rdiv:s @1 @2))
754    (mult (rdiv @0 @1) @2)))
756 /* Simplify x / (- y) to -x / y.  */
757 (simplify
758  (rdiv @0 (negate @1))
759  (rdiv (negate @0) @1))
761 (if (flag_unsafe_math_optimizations)
762  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
763     Since C / x may underflow to zero, do this only for unsafe math.  */
764  (for op (lt le gt ge)
765       neg_op (gt ge lt le)
766   (simplify
767    (op (rdiv REAL_CST@0 @1) real_zerop@2)
768    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
769     (switch
770      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
771       (op @1 @2))
772      /* For C < 0, use the inverted operator.  */
773      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
774       (neg_op @1 @2)))))))
776 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
777 (for div (trunc_div ceil_div floor_div round_div exact_div)
778  (simplify
779   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
780   (if (integer_pow2p (@2)
781        && tree_int_cst_sgn (@2) > 0
782        && tree_nop_conversion_p (type, TREE_TYPE (@0))
783        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
784    (rshift (convert @0)
785            { build_int_cst (integer_type_node,
786                             wi::exact_log2 (wi::to_wide (@2))); }))))
788 /* If ARG1 is a constant, we can convert this to a multiply by the
789    reciprocal.  This does not have the same rounding properties,
790    so only do this if -freciprocal-math.  We can actually
791    always safely do it if ARG1 is a power of two, but it's hard to
792    tell if it is or not in a portable manner.  */
793 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
794  (simplify
795   (rdiv @0 cst@1)
796   (if (optimize)
797    (if (flag_reciprocal_math
798         && !real_zerop (@1))
799     (with
800      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
801      (if (tem)
802       (mult @0 { tem; } )))
803     (if (cst != COMPLEX_CST)
804      (with { tree inverse = exact_inverse (type, @1); }
805       (if (inverse)
806        (mult @0 { inverse; } ))))))))
808 (for mod (ceil_mod floor_mod round_mod trunc_mod)
809  /* 0 % X is always zero.  */
810  (simplify
811   (mod integer_zerop@0 @1)
812   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
813   (if (!integer_zerop (@1))
814    @0))
815  /* X % 1 is always zero.  */
816  (simplify
817   (mod @0 integer_onep)
818   { build_zero_cst (type); })
819  /* X % -1 is zero.  */
820  (simplify
821   (mod @0 integer_minus_onep@1)
822   (if (!TYPE_UNSIGNED (type))
823    { build_zero_cst (type); }))
824  /* X % X is zero.  */
825  (simplify
826   (mod @0 @0)
827   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
828   (if (!integer_zerop (@0))
829    { build_zero_cst (type); }))
830  /* (X % Y) % Y is just X % Y.  */
831  (simplify
832   (mod (mod@2 @0 @1) @1)
833   @2)
834  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
835  (simplify
836   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
837   (if (ANY_INTEGRAL_TYPE_P (type)
838        && TYPE_OVERFLOW_UNDEFINED (type)
839        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
840                              TYPE_SIGN (type)))
841    { build_zero_cst (type); }))
842  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
843     modulo and comparison, since it is simpler and equivalent.  */
844  (for cmp (eq ne)
845   (simplify
846    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
847    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
848     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
849      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
851 /* X % -C is the same as X % C.  */
852 (simplify
853  (trunc_mod @0 INTEGER_CST@1)
854   (if (TYPE_SIGN (type) == SIGNED
855        && !TREE_OVERFLOW (@1)
856        && wi::neg_p (wi::to_wide (@1))
857        && !TYPE_OVERFLOW_TRAPS (type)
858        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
859        && !sign_bit_p (@1, @1))
860    (trunc_mod @0 (negate @1))))
862 /* X % -Y is the same as X % Y.  */
863 (simplify
864  (trunc_mod @0 (convert? (negate @1)))
865  (if (INTEGRAL_TYPE_P (type)
866       && !TYPE_UNSIGNED (type)
867       && !TYPE_OVERFLOW_TRAPS (type)
868       && tree_nop_conversion_p (type, TREE_TYPE (@1))
869       /* Avoid this transformation if X might be INT_MIN or
870          Y might be -1, because we would then change valid
871          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
872       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
873           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
874                                                         (TREE_TYPE (@1))))))
875   (trunc_mod @0 (convert @1))))
877 /* X - (X / Y) * Y is the same as X % Y.  */
878 (simplify
879  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
880  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
881   (convert (trunc_mod @0 @1))))
883 /* x * (1 + y / x) - y -> x - y % x */
884 (simplify
885  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
886  (if (INTEGRAL_TYPE_P (type))
887   (minus @0 (trunc_mod @1 @0))))
889 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
890    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
891    Also optimize A % (C << N)  where C is a power of 2,
892    to A & ((C << N) - 1).
893    Also optimize "A shift (B % C)", if C is a power of 2, to
894    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
895    and assume (B % C) is nonnegative as shifts negative values would
896    be UB.  */
897 (match (power_of_two_cand @1)
898  INTEGER_CST@1)
899 (match (power_of_two_cand @1)
900  (lshift INTEGER_CST@1 @2))
901 (for mod (trunc_mod floor_mod)
902  (for shift (lshift rshift)
903   (simplify
904    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
905    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
906     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
907                                                       1); }))))))
908  (simplify
909   (mod @0 (convert? (power_of_two_cand@1 @2)))
910   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
911        /* Allow any integral conversions of the divisor, except
912           conversion from narrower signed to wider unsigned type
913           where if @1 would be negative power of two, the divisor
914           would not be a power of two.  */
915        && INTEGRAL_TYPE_P (type)
916        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
917        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
918            || TYPE_UNSIGNED (TREE_TYPE (@1))
919            || !TYPE_UNSIGNED (type))
920        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
921    (with { tree utype = TREE_TYPE (@1);
922            if (!TYPE_OVERFLOW_WRAPS (utype))
923              utype = unsigned_type_for (utype); }
924     (bit_and @0 (convert (minus (convert:utype @1)
925                                 { build_one_cst (utype); })))))))
927 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
928 (simplify
929  (trunc_div (mult @0 integer_pow2p@1) @1)
930  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
931   (bit_and @0 { wide_int_to_tree
932                 (type, wi::mask (TYPE_PRECISION (type)
933                                  - wi::exact_log2 (wi::to_wide (@1)),
934                                  false, TYPE_PRECISION (type))); })))
936 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
937 (simplify
938  (mult (trunc_div @0 integer_pow2p@1) @1)
939  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
940   (bit_and @0 (negate @1))))
942 (for div (trunc_div ceil_div floor_div round_div exact_div)
943  /* Simplify (t * u) / u -> t.  */
944  (simplify
945   (div (mult:c @0 @1) @1)
946   (if (ANY_INTEGRAL_TYPE_P (type))
947    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
948     @0
949 #if GIMPLE
950     (with {value_range vr0, vr1;}
951      (if (INTEGRAL_TYPE_P (type)
952           && get_range_query (cfun)->range_of_expr (vr0, @0)
953           && get_range_query (cfun)->range_of_expr (vr1, @1)
954           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
955       @0))
956 #endif
957    )))
958 #if GIMPLE
959  /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v.  */
960  (simplify
961   (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
962   (if (INTEGRAL_TYPE_P (type)
963        && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
964    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
965     (mult @0 (div! @1 @2))
966     (with {value_range vr0, vr1;}
967      (if (get_range_query (cfun)->range_of_expr (vr0, @0)
968           && get_range_query (cfun)->range_of_expr (vr1, @1)
969           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
970       (mult @0 (div! @1 @2))))
971    )))
972 #endif
973  /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v.  */
974  (simplify
975   (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
976   (if (INTEGRAL_TYPE_P (type)
977        && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
978    (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
979     (div @1 @2)
980 #if GIMPLE
981     (with {value_range vr0, vr1, vr2;}
982      (if (get_range_query (cfun)->range_of_expr (vr0, @0)
983           && get_range_query (cfun)->range_of_expr (vr1, @1)
984           && get_range_query (cfun)->range_of_expr (vr2, @2)
985           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
986           && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
987       (div @1 @2)))
988 #endif
989    ))))
991 #if GIMPLE
992 (for div (trunc_div exact_div)
993  /* Simplify (X + M*N) / N -> X / N + M.  */
994  (simplify
995   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
996   (with {value_range vr0, vr1, vr2, vr3, vr4;}
997   (if (INTEGRAL_TYPE_P (type)
998        && get_range_query (cfun)->range_of_expr (vr1, @1)
999        && get_range_query (cfun)->range_of_expr (vr2, @2)
1000        /* "N*M" doesn't overflow.  */
1001        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1002        && get_range_query (cfun)->range_of_expr (vr0, @0)
1003        && get_range_query (cfun)->range_of_expr (vr3, @3)
1004        /* "X+(N*M)" doesn't overflow.  */
1005        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
1006        && get_range_query (cfun)->range_of_expr (vr4, @4)
1007        && !vr4.undefined_p ()
1008        /* "X+N*M" is not with opposite sign as "X".  */
1009        && (TYPE_UNSIGNED (type)
1010            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1011            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1012   (plus (div @0 @2) @1))))
1014  /* Simplify (X - M*N) / N -> X / N - M.  */
1015  (simplify
1016   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1017   (with {value_range vr0, vr1, vr2, vr3, vr4;}
1018   (if (INTEGRAL_TYPE_P (type)
1019        && get_range_query (cfun)->range_of_expr (vr1, @1)
1020        && get_range_query (cfun)->range_of_expr (vr2, @2)
1021        /* "N * M" doesn't overflow.  */
1022        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1023        && get_range_query (cfun)->range_of_expr (vr0, @0)
1024        && get_range_query (cfun)->range_of_expr (vr3, @3)
1025        /* "X - (N*M)" doesn't overflow.  */
1026        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1027        && get_range_query (cfun)->range_of_expr (vr4, @4)
1028        && !vr4.undefined_p ()
1029        /* "X-N*M" is not with opposite sign as "X".  */
1030        && (TYPE_UNSIGNED (type)
1031            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1032            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1033   (minus (div @0 @2) @1)))))
1035 /* Simplify
1036    (X + C) / N -> X / N + C / N where C is multiple of N.
1037    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
1038 (for op (trunc_div exact_div rshift)
1039  (simplify
1040   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1041    (with
1042     {
1043       wide_int c = wi::to_wide (@1);
1044       wide_int n = wi::to_wide (@2);
1045       bool shift = op == RSHIFT_EXPR;
1046 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1047                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
1048 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1049                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1050       value_range vr0, vr1, vr3;
1051     }
1052     (if (INTEGRAL_TYPE_P (type)
1053          && get_range_query (cfun)->range_of_expr (vr0, @0))
1054      (if (exact_mod (c)
1055           && get_range_query (cfun)->range_of_expr (vr1, @1)
1056           /* "X+C" doesn't overflow.  */
1057           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1058           && get_range_query (cfun)->range_of_expr (vr3, @3)
1059           && !vr3.undefined_p ()
1060           /* "X+C" and "X" are not of opposite sign.  */
1061           && (TYPE_UNSIGNED (type)
1062               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1063               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1064        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1065        (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1066             && exact_mod (-c)
1067             /* unsigned "X-(-C)" doesn't underflow.  */
1068             && wi::geu_p (vr0.lower_bound (), -c))
1069          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1070 #undef plus_op1
1071 #undef exact_mod
1072 #endif
1074 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1075    if var is smaller in precision.
1076    This is always safe for both doing the negative in signed or unsigned
1077    as the value for undefined will not show up.
1078    Note the outer cast cannot be a boolean type as the only valid values
1079    are 0,-1/1 (depending on the signedness of the boolean) and the negative
1080    is there to get the correct value.  */
1081 (simplify
1082  (convert (negate:s@1 (convert:s @0)))
1083  (if (INTEGRAL_TYPE_P (type)
1084       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1085       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1086       && TREE_CODE (type) != BOOLEAN_TYPE)
1087     (negate (convert @0))))
1089 (for op (negate abs)
1090  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1091  (for coss (COS COSH)
1092   (simplify
1093    (coss (op @0))
1094     (coss @0)))
1095  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1096  (for pows (POW)
1097   (simplify
1098    (pows (op @0) REAL_CST@1)
1099    (with { HOST_WIDE_INT n; }
1100     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1101      (pows @0 @1)))))
1102  /* Likewise for powi.  */
1103  (for pows (POWI)
1104   (simplify
1105    (pows (op @0) INTEGER_CST@1)
1106    (if ((wi::to_wide (@1) & 1) == 0)
1107     (pows @0 @1))))
1108  /* Strip negate and abs from both operands of hypot.  */
1109  (for hypots (HYPOT)
1110   (simplify
1111    (hypots (op @0) @1)
1112    (hypots @0 @1))
1113   (simplify
1114    (hypots @0 (op @1))
1115    (hypots @0 @1)))
1116  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1117  (for copysigns (COPYSIGN_ALL)
1118   (simplify
1119    (copysigns (op @0) @1)
1120    (copysigns @0 @1))))
1122 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1123 (simplify
1124  (mult (abs@1 @0) @1)
1125  (mult @0 @0))
1127 /* Convert absu(x)*absu(x) -> x*x.  */
1128 (simplify
1129  (mult (absu@1 @0) @1)
1130  (mult (convert@2 @0) @2))
1132 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1133 (for coss (COS COSH)
1134  (for copysigns (COPYSIGN)
1135   (simplify
1136    (coss (copysigns @0 @1))
1137     (coss @0))))
1139 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1140 (for pows (POW)
1141  (for copysigns (COPYSIGN)
1142   (simplify
1143    (pows (copysigns @0 @2) REAL_CST@1)
1144    (with { HOST_WIDE_INT n; }
1145     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1146      (pows @0 @1))))))
1147 /* Likewise for powi.  */
1148 (for pows (POWI)
1149  (for copysigns (COPYSIGN)
1150   (simplify
1151    (pows (copysigns @0 @2) INTEGER_CST@1)
1152    (if ((wi::to_wide (@1) & 1) == 0)
1153     (pows @0 @1)))))
1155 (for hypots (HYPOT)
1156  (for copysigns (COPYSIGN)
1157   /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1158   (simplify
1159    (hypots (copysigns @0 @1) @2)
1160    (hypots @0 @2))
1161   /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1162   (simplify
1163    (hypots @0 (copysigns @1 @2))
1164    (hypots @0 @1))))
1166 /* copysign(x, CST) -> abs (x).  If the target does not
1167    support the copysign optab then canonicalize
1168    copysign(x, -CST) -> fneg (abs (x)).   */
1169 (for copysigns (COPYSIGN_ALL)
1170  (simplify
1171   (copysigns @0 REAL_CST@1)
1172   (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1173    (abs @0)
1174 #if GIMPLE
1175    (if (!direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1176                                          OPTIMIZE_FOR_BOTH))
1177     (negate (abs @0)))
1178 #endif
1179    )))
1181 #if GIMPLE
1182 /* Transform fneg (fabs (X)) -> copysign (X, -1) as the canonical
1183    representation if the target supports the copysign optab.  */
1184 (simplify
1185  (negate (abs @0))
1186  (if (direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1187                                       OPTIMIZE_FOR_BOTH))
1188    (IFN_COPYSIGN @0 { build_minus_one_cst (type); })))
1189 #endif
1190 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1191 (for copysigns (COPYSIGN_ALL)
1192  (simplify
1193   (copysigns (copysigns @0 @1) @2)
1194   (copysigns @0 @2)))
1196 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1197 (for copysigns (COPYSIGN_ALL)
1198  (simplify
1199   (mult (copysigns@2 @0 @1) @2)
1200   (mult @0 @0)))
1202 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1203 (for ccoss (CCOS CCOSH)
1204  (simplify
1205   (ccoss (negate @0))
1206    (ccoss @0)))
1208 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1209 (for ops (conj negate)
1210  (for cabss (CABS)
1211   (simplify
1212    (cabss (ops @0))
1213    (cabss @0))))
1215 /* Fold (a * (1 << b)) into (a << b)  */
1216 (simplify
1217  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1218   (if (! FLOAT_TYPE_P (type)
1219        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1220    (lshift @0 @2)))
1222 /* Fold a * !a into 0.  */
1223 (simplify
1224  (mult:c @0 (convert? (eq @0 integer_zerop)))
1225   { build_zero_cst (type); })
1226 (simplify
1227  (mult:c @0 (vec_cond (eq @0 integer_zerop) @1 integer_zerop))
1228   { build_zero_cst (type); })
1229 (simplify
1230  (mult:c @0 (vec_cond (ne @0 integer_zerop) integer_zerop @1))
1231   { build_zero_cst (type); })
1233 /* Shifts by precision or greater result in zero.  */
1234 (for shift (lshift rshift)
1235  (simplify
1236   (shift @0 uniform_integer_cst_p@1)
1237   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1238        /* Leave arithmetic right shifts of possibly negative values alone.  */
1239        && (TYPE_UNSIGNED (type)
1240            || shift == LSHIFT_EXPR
1241            || tree_expr_nonnegative_p (@0))
1242        /* Use a signed compare to leave negative shift counts alone.  */
1243        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1244                      element_precision (type)))
1245    { build_zero_cst (type); })))
1247 /* Shifts by constants distribute over several binary operations,
1248    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1249 (for op (plus minus)
1250   (simplify
1251     (op (lshift:s @0 @1) (lshift:s @2 @1))
1252     (if (INTEGRAL_TYPE_P (type)
1253          && TYPE_OVERFLOW_WRAPS (type)
1254          && !TYPE_SATURATING (type))
1255       (lshift (op @0 @2) @1))))
1257 (for op (bit_and bit_ior bit_xor)
1258   (simplify
1259     (op (lshift:s @0 @1) (lshift:s @2 @1))
1260     (if (INTEGRAL_TYPE_P (type))
1261       (lshift (op @0 @2) @1)))
1262   (simplify
1263     (op (rshift:s @0 @1) (rshift:s @2 @1))
1264     (if (INTEGRAL_TYPE_P (type))
1265       (rshift (op @0 @2) @1))))
1267 /* Fold (1 << (C - x)) where C = precision(type) - 1
1268    into ((1 << C) >> x). */
1269 (simplify
1270  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1271   (if (INTEGRAL_TYPE_P (type)
1272        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1273        && single_use (@1))
1274    (if (TYPE_UNSIGNED (type))
1275      (rshift (lshift @0 @2) @3)
1276    (with
1277     { tree utype = unsigned_type_for (type); }
1278     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1280 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1281 (simplify
1282  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1283  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1284       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1285   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1286    (bit_and (convert @0)
1287             { wide_int_to_tree (type,
1288                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1290 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1291 (for cst (INTEGER_CST VECTOR_CST)
1292  (simplify
1293   (rshift (negate:s @0) cst@1)
1294    (if (!TYPE_UNSIGNED (type)
1295         && TYPE_OVERFLOW_UNDEFINED (type))
1296     (with { tree stype = TREE_TYPE (@1);
1297             tree bt = truth_type_for (type);
1298             tree zeros = build_zero_cst (type);
1299             tree cst = NULL_TREE; }
1300      (switch
1301       /* Handle scalar case.  */
1302       (if (INTEGRAL_TYPE_P (type)
1303            /* If we apply the rule to the scalar type before vectorization
1304               we will enforce the result of the comparison being a bool
1305               which will require an extra AND on the result that will be
1306               indistinguishable from when the user did actually want 0
1307               or 1 as the result so it can't be removed.  */
1308            && canonicalize_math_after_vectorization_p ()
1309            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1310        (negate (convert (gt @0 { zeros; }))))
1311       /* Handle vector case.  */
1312       (if (VECTOR_INTEGER_TYPE_P (type)
1313            /* First check whether the target has the same mode for vector
1314               comparison results as it's operands do.  */
1315            && TYPE_MODE (bt) == TYPE_MODE (type)
1316            /* Then check to see if the target is able to expand the comparison
1317               with the given type later on, otherwise we may ICE.  */
1318            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1319            && (cst = uniform_integer_cst_p (@1)) != NULL
1320            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1321        (view_convert (gt:bt @0 { zeros; }))))))))
1323 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1324 (simplify
1325  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1326   (if (flag_associative_math
1327        && single_use (@3))
1328    (with
1329     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1330     (if (tem)
1331      (rdiv { tem; } @1)))))
1333 /* Simplify ~X & X as zero.  */
1334 (simplify
1335  (bit_and (convert? @0) (convert? @1))
1336  (with { bool wascmp; }
1337   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1338        && bitwise_inverted_equal_p (@0, @1, wascmp))
1339    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1341 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1342 (simplify
1343   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1344   (if (TYPE_UNSIGNED (type))
1345     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1347 (for bitop (bit_and bit_ior)
1348      cmp (eq ne)
1349  /* PR35691: Transform
1350     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1351     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1352  (simplify
1353   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1354    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1355         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1356         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1357     (cmp (bit_ior @0 (convert @1)) @2)))
1358  /* Transform:
1359     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1360     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1361  (simplify
1362   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1363    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1364         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1365         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1366     (cmp (bit_and @0 (convert @1)) @2))))
1368 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1369 (simplify
1370  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1371   (minus (bit_xor @0 @1) @1))
1372 (simplify
1373  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1374  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1375   (minus (bit_xor @0 @1) @1)))
1377 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1378 (simplify
1379  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1380   (minus @1 (bit_xor @0 @1)))
1382 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1383 (for op (bit_ior bit_xor plus)
1384  (simplify
1385   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1386   (with { bool wascmp0, wascmp1; }
1387    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1388         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1389         && ((!wascmp0 && !wascmp1)
1390             || element_precision (type) == 1))
1391    (bit_xor @0 @1)))))
1393 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1394 (simplify
1395   (bit_ior:c (bit_xor:c @0 @1) @0)
1396   (bit_ior @0 @1))
1398 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1399 (simplify
1400  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1401  @2)
1403 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1404 (simplify
1405  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1406  (bit_not (bit_and @0 @1)))
1408 /* (~a & b) ^ a  -->   (a | b)   */
1409 (simplify
1410  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1411  (bit_ior @0 @1))
1413 /* (a | b) & ~(a ^ b)  -->  a & b  */
1414 (simplify
1415  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1416  (bit_and @0 @1))
1418 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1419 (simplify
1420  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1421  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1422       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1423   (bit_and @0 @1)))
1425 /* a | ~(a ^ b)  -->  a | ~b  */
1426 (simplify
1427  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1428  (bit_ior @0 (bit_not @1)))
1430 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1431 (simplify
1432  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1433  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1434       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1435   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1437 /* a | ((~a) ^ b)  -->  a | (~b) (alt version of the above 2) */
1438 (simplify
1439  (bit_ior:c @0 (bit_xor:cs @1 @2))
1440  (with { bool wascmp; }
1441  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1442       && (!wascmp || element_precision (type) == 1))
1443   (bit_ior @0 (bit_not @2)))))
1445 /* a & ~(a ^ b)  -->  a & b  */
1446 (simplify
1447  (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1448  (bit_and @0 @1))
1450 /* a & (a == b)  -->  a & b (boolean version of the above). */
1451 (simplify
1452  (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1453  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1454       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1455   (bit_and @0 @1)))
1457 /* a & ((~a) ^ b)  -->  a & b (alt version of the above 2) */
1458 (simplify
1459  (bit_and:c @0 (bit_xor:c @1 @2))
1460  (with { bool wascmp; }
1461  (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1462       && (!wascmp || element_precision (type) == 1))
1463   (bit_and @0 @2))))
1465 /* (a | b) | (a &^ b)  -->  a | b  */
1466 (for op (bit_and bit_xor)
1467  (simplify
1468   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1469   @2))
1471 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1472 (simplify
1473  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1474  @2)
1476 /* (a & b) | (a == b)  -->  a == b  */
1477 (simplify
1478  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1479  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1480       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1481   @2))
1483 /* ~(~a & b)  -->  a | ~b  */
1484 (simplify
1485  (bit_not (bit_and:cs (bit_not @0) @1))
1486  (bit_ior @0 (bit_not @1)))
1488 /* ~(~a | b) --> a & ~b */
1489 (simplify
1490  (bit_not (bit_ior:cs (bit_not @0) @1))
1491  (bit_and @0 (bit_not @1)))
1493 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1494 (simplify
1495  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1496  (bit_and @3 (bit_not @2)))
1498 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1499 (simplify
1500  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1501  (bit_ior @3 @2))
1503 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1504 (simplify
1505  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1506   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1508 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1509 (simplify
1510  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1511   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1513 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1514 (simplify
1515  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1516  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1517       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1518   (bit_xor @0 @1)))
1520 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1521    ((A & N) + B) & M -> (A + B) & M
1522    Similarly if (N & M) == 0,
1523    ((A | N) + B) & M -> (A + B) & M
1524    and for - instead of + (or unary - instead of +)
1525    and/or ^ instead of |.
1526    If B is constant and (B & M) == 0, fold into A & M.  */
1527 (for op (plus minus)
1528  (for bitop (bit_and bit_ior bit_xor)
1529   (simplify
1530    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1531     (with
1532      { tree pmop[2];
1533        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1534                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1535                                        NULL_TREE, pmop); }
1536      (if (utype)
1537       (convert (bit_and (op (convert:utype { pmop[0]; })
1538                             (convert:utype { pmop[1]; }))
1539                         (convert:utype @2))))))
1540   (simplify
1541    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1542     (with
1543      { tree pmop[2];
1544        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1545                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1546                                        @4, pmop); }
1547      (if (utype)
1548       (convert (bit_and (op (convert:utype { pmop[0]; })
1549                             (convert:utype { pmop[1]; }))
1550                         (convert:utype @2)))))))
1551  (simplify
1552   (bit_and (op:s @0 @1) INTEGER_CST@2)
1553    (with
1554     { tree pmop[2];
1555       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1556                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1557                                       NULL_TREE, NULL_TREE, pmop); }
1558     (if (utype)
1559      (convert (bit_and (op (convert:utype { pmop[0]; })
1560                            (convert:utype { pmop[1]; }))
1561                        (convert:utype @2)))))))
1562 (for bitop (bit_and bit_ior bit_xor)
1563  (simplify
1564   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1565    (with
1566     { tree pmop[2];
1567       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1568                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1569                                       NULL_TREE, NULL_TREE, pmop); }
1570     (if (utype)
1571      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1572                        (convert:utype @1)))))))
1574 /* X % Y is smaller than Y.  */
1575 (for cmp (lt ge)
1576  (simplify
1577   (cmp:c (trunc_mod @0 @1) @1)
1578   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1579    { constant_boolean_node (cmp == LT_EXPR, type); })))
1581 /* x | ~0 -> ~0  */
1582 (simplify
1583  (bit_ior @0 integer_all_onesp@1)
1584  @1)
1586 /* x | 0 -> x  */
1587 (simplify
1588  (bit_ior @0 integer_zerop)
1589  @0)
1591 /* x & 0 -> 0  */
1592 (simplify
1593  (bit_and @0 integer_zerop@1)
1594  @1)
1596 /* ~x | x -> -1 */
1597 /* ~x ^ x -> -1 */
1598 (for op (bit_ior bit_xor)
1599  (simplify
1600   (op (convert? @0) (convert? @1))
1601   (with { bool wascmp; }
1602    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1603         && bitwise_inverted_equal_p (@0, @1, wascmp))
1604     (convert
1605      { wascmp
1606         ? constant_boolean_node (true, type)
1607         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1609 /* x ^ x -> 0 */
1610 (simplify
1611   (bit_xor @0 @0)
1612   { build_zero_cst (type); })
1614 /* Canonicalize X ^ ~0 to ~X.  */
1615 (simplify
1616   (bit_xor @0 integer_all_onesp@1)
1617   (bit_not @0))
1619 /* x & ~0 -> x  */
1620 (simplify
1621  (bit_and @0 integer_all_onesp)
1622   (non_lvalue @0))
1624 /* x & x -> x,  x | x -> x  */
1625 (for bitop (bit_and bit_ior)
1626  (simplify
1627   (bitop @0 @0)
1628   (non_lvalue @0)))
1630 /* x & C -> x if we know that x & ~C == 0.  */
1631 #if GIMPLE
1632 (simplify
1633  (bit_and 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   @0))
1638 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0   */
1639 (simplify
1640  (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1641  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1642       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1643   @0))
1645 /* x | C -> C if we know that x & ~C == 0.  */
1646 (simplify
1647  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1648  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1649       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1650   @1))
1651 #endif
1653 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1654 (simplify
1655  (bit_not (minus (bit_not @0) @1))
1656  (plus @0 @1))
1657 (simplify
1658  (bit_not (plus:c (bit_not @0) @1))
1659  (minus @0 @1))
1660 /* (~X - ~Y) -> Y - X.  */
1661 (simplify
1662  (minus (bit_not @0) (bit_not @1))
1663   (if (!TYPE_OVERFLOW_SANITIZED (type))
1664    (with { tree utype = unsigned_type_for (type); }
1665     (convert (minus (convert:utype @1) (convert:utype @0))))))
1667 /* ~(X - Y) -> ~X + Y.  */
1668 (simplify
1669  (bit_not (minus:s @0 @1))
1670  (plus (bit_not @0) @1))
1671 (simplify
1672  (bit_not (plus:s @0 INTEGER_CST@1))
1673  (if ((INTEGRAL_TYPE_P (type)
1674        && TYPE_UNSIGNED (type))
1675       || (!TYPE_OVERFLOW_SANITIZED (type)
1676           && may_negate_without_overflow_p (@1)))
1677   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1679 #if GIMPLE
1680 /* ~X + Y -> (Y - X) - 1.  */
1681 (simplify
1682  (plus:c (bit_not @0) @1)
1683   (if (ANY_INTEGRAL_TYPE_P (type)
1684        && TYPE_OVERFLOW_WRAPS (type)
1685        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1686        && !integer_all_onesp (@1))
1687    (plus (minus @1 @0) { build_minus_one_cst (type); })
1688    (if (INTEGRAL_TYPE_P (type)
1689         && TREE_CODE (@1) == INTEGER_CST
1690         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1691                                               SIGNED))
1692     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1693 #endif
1695 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1696 (simplify
1697  (bit_not (rshift:s @0 @1))
1698   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1699    (rshift (bit_not! @0) @1)
1700    /* For logical right shifts, this is possible only if @0 doesn't
1701       have MSB set and the logical right shift is changed into
1702       arithmetic shift.  */
1703    (if (INTEGRAL_TYPE_P (type)
1704         && !wi::neg_p (tree_nonzero_bits (@0)))
1705     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1706      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1708 /* x + (x & 1) -> (x + 1) & ~1 */
1709 (simplify
1710  (plus:c @0 (bit_and:s @0 integer_onep@1))
1711  (bit_and (plus @0 @1) (bit_not @1)))
1713 /* x & ~(x & y) -> x & ~y */
1714 /* x | ~(x | y) -> x | ~y  */
1715 (for bitop (bit_and bit_ior)
1716  (simplify
1717   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1718   (bitop @0 (bit_not @1))))
1720 /* (~x & y) | ~(x | y) -> ~x */
1721 (simplify
1722  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1723  @2)
1725 /* (x | y) ^ (x | ~y) -> ~x */
1726 (simplify
1727  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1728  (bit_not @0))
1730 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1731 (simplify
1732  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1733  (bit_not (bit_xor @0 @1)))
1735 /* (~x | y) ^ (x ^ y) -> x | ~y */
1736 (simplify
1737  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1738  (bit_ior @0 (bit_not @1)))
1740 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1741 (simplify
1742  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1743  (bit_not (bit_and @0 @1)))
1745 /* (x & y) ^ (x | y) -> x ^ y */
1746 (simplify
1747  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1748  (bit_xor @0 @1))
1750 /* (x ^ y) ^ (x | y) -> x & y */
1751 (simplify
1752  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1753  (bit_and @0 @1))
1755 /* (x & y) + (x ^ y) -> x | y */
1756 /* (x & y) | (x ^ y) -> x | y */
1757 /* (x & y) ^ (x ^ y) -> x | y */
1758 (for op (plus bit_ior bit_xor)
1759  (simplify
1760   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1761   (bit_ior @0 @1)))
1763 /* (x & y) + (x | y) -> x + y */
1764 (simplify
1765  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1766  (plus @0 @1))
1768 /* (x + y) - (x | y) -> x & y */
1769 (simplify
1770  (minus (plus @0 @1) (bit_ior @0 @1))
1771  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1772       && !TYPE_SATURATING (type))
1773   (bit_and @0 @1)))
1775 /* (x + y) - (x & y) -> x | y */
1776 (simplify
1777  (minus (plus @0 @1) (bit_and @0 @1))
1778  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1779       && !TYPE_SATURATING (type))
1780   (bit_ior @0 @1)))
1782 /* (x | y) - y -> (x & ~y) */
1783 (simplify
1784  (minus (bit_ior:cs @0 @1) @1)
1785  (bit_and @0 (bit_not @1)))
1787 /* (x | y) - (x ^ y) -> x & y */
1788 (simplify
1789  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1790  (bit_and @0 @1))
1792 /* (x | y) - (x & y) -> x ^ y */
1793 (simplify
1794  (minus (bit_ior @0 @1) (bit_and @0 @1))
1795  (bit_xor @0 @1))
1797 /* (x | y) & ~(x & y) -> x ^ y */
1798 (simplify
1799  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1800  (bit_xor @0 @1))
1802 /* (x | y) & (~x ^ y) -> x & y */
1803 (simplify
1804  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1805  (with { bool wascmp; }
1806   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1807        && (!wascmp || element_precision (type) == 1))
1808    (bit_and @0 @1))))
1810 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1811 (simplify
1812  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1813  (bit_not (bit_xor @0 @1)))
1815 /* (~x | y) ^ (x | ~y) -> x ^ y */
1816 (simplify
1817  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1818  (bit_xor @0 @1))
1820 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1821 (simplify
1822  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1823                               (nop_convert2? (bit_ior @0 @1))))
1824        integer_all_onesp)
1825  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1826       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1827       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1828       && !TYPE_SATURATING (TREE_TYPE (@2)))
1829  (bit_not (convert (bit_xor @0 @1)))))
1830 (simplify
1831  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1832                                integer_all_onesp))
1833        (nop_convert3? (bit_ior @0 @1)))
1834  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1835       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1836       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1837       && !TYPE_SATURATING (TREE_TYPE (@2)))
1838  (bit_not (convert (bit_xor @0 @1)))))
1839 (simplify
1840  (minus (nop_convert1? (bit_and @0 @1))
1841        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1842                                integer_onep)))
1843  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1844       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1845       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1846       && !TYPE_SATURATING (TREE_TYPE (@2)))
1847  (bit_not (convert (bit_xor @0 @1)))))
1849 /* ~x & ~y -> ~(x | y)
1850    ~x | ~y -> ~(x & y) */
1851 (for op (bit_and bit_ior)
1852      rop (bit_ior bit_and)
1853  (simplify
1854   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1855   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1856        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1857    (bit_not (rop (convert @0) (convert @1))))))
1859 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1860    with a constant, and the two constants have no bits in common,
1861    we should treat this as a BIT_IOR_EXPR since this may produce more
1862    simplifications.  */
1863 (for op (bit_xor plus)
1864  (simplify
1865   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1866       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1867   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1868        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1869        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1870    (bit_ior (convert @4) (convert @5)))))
1872 /* (X | Y) ^ X -> Y & ~ X*/
1873 (simplify
1874  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1875  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1876   (convert (bit_and @1 (bit_not @0)))))
1878 /* (~X | Y) ^ X -> ~(X & Y).  */
1879 (simplify
1880  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1881  (if (bitwise_equal_p (@0, @2))
1882   (convert (bit_not (bit_and @0 (convert @1))))))
1884 /* Convert ~X ^ ~Y to X ^ Y.  */
1885 (simplify
1886  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1887  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1888       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1889   (bit_xor (convert @0) (convert @1))))
1891 /* Convert ~X ^ C to X ^ ~C.  */
1892 (simplify
1893  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1894  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1895   (bit_xor (convert @0) (bit_not @1))))
1897 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1898 (for opo (bit_and bit_xor)
1899      opi (bit_xor bit_and)
1900  (simplify
1901   (opo:c (opi:cs @0 @1) @1)
1902   (bit_and (bit_not @0) @1)))
1904 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1905    operands are another bit-wise operation with a common input.  If so,
1906    distribute the bit operations to save an operation and possibly two if
1907    constants are involved.  For example, convert
1908      (A | B) & (A | C) into A | (B & C)
1909    Further simplification will occur if B and C are constants.  */
1910 (for op (bit_and bit_ior bit_xor)
1911      rop (bit_ior bit_and bit_and)
1912  (simplify
1913   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1914   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1915        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1916    (rop (convert @0) (op (convert @1) (convert @2))))))
1918 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1919 /* (X & Y) & Y -> X & Y
1920    (X | Y) | Y -> X | Y  */
1921 (for op (bit_and bit_ior)
1922  (simplify
1923   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1924   @2))
1925 /* (X ^ Y) ^ Y -> X  */
1926 (simplify
1927  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1928  (convert @0))
1930 /* (X & ~Y) & Y -> 0 */
1931 (simplify
1932  (bit_and:c (bit_and @0 @1) @2)
1933  (with { bool wascmp; }
1934   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1935        || bitwise_inverted_equal_p (@1, @2, wascmp))
1936    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1937 /* (X | ~Y) | Y -> -1 */
1938 (simplify
1939  (bit_ior:c (bit_ior @0 @1) @2)
1940  (with { bool wascmp; }
1941   (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1942         || bitwise_inverted_equal_p (@1, @2, wascmp))
1943        && (!wascmp || element_precision (type) == 1))
1944    { build_all_ones_cst (TREE_TYPE (@0)); })))
1946 /* (X & Y) & (X & Z) -> (X & Y) & Z
1947    (X | Y) | (X | Z) -> (X | Y) | Z  */
1948 (for op (bit_and bit_ior)
1949  (simplify
1950   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1951   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1952        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1953    (if (single_use (@5) && single_use (@6))
1954     (op @3 (convert @2))
1955     (if (single_use (@3) && single_use (@4))
1956      (op (convert @1) @5))))))
1957 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1958 (simplify
1959  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1960  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1961       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1962   (bit_xor (convert @1) (convert @2))))
1964 /* Convert abs (abs (X)) into abs (X).
1965    also absu (absu (X)) into absu (X).  */
1966 (simplify
1967  (abs (abs@1 @0))
1968  @1)
1970 (simplify
1971  (absu (convert@2 (absu@1 @0)))
1972  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1973   @1))
1975 /* Convert abs[u] (-X) -> abs[u] (X).  */
1976 (simplify
1977  (abs (negate @0))
1978  (abs @0))
1980 (simplify
1981  (absu (negate @0))
1982  (absu @0))
1984 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1985 (simplify
1986  (abs tree_expr_nonnegative_p@0)
1987  @0)
1989 (simplify
1990  (absu tree_expr_nonnegative_p@0)
1991  (convert @0))
1993 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1994 (simplify
1995  (mult:c (nop_convert1?
1996           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1997                     integer_onep))
1998          (nop_convert3? @0))
1999  (if (INTEGRAL_TYPE_P (type)
2000       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2001       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
2002   (if (TYPE_UNSIGNED (type))
2003    (absu @0)
2004    (abs @0)
2005   )
2009 /* A few cases of fold-const.cc negate_expr_p predicate.  */
2010 (match negate_expr_p
2011  INTEGER_CST
2012  (if ((INTEGRAL_TYPE_P (type)
2013        && TYPE_UNSIGNED (type))
2014       || (!TYPE_OVERFLOW_SANITIZED (type)
2015           && may_negate_without_overflow_p (t)))))
2016 (match negate_expr_p
2017  FIXED_CST)
2018 (match negate_expr_p
2019  (negate @0)
2020  (if (!TYPE_OVERFLOW_SANITIZED (type))))
2021 (match negate_expr_p
2022  REAL_CST
2023  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
2024 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
2025    ways.  */
2026 (match negate_expr_p
2027  VECTOR_CST
2028  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2029 (match negate_expr_p
2030  (minus @0 @1)
2031  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2032       || (FLOAT_TYPE_P (type)
2033           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2034           && !HONOR_SIGNED_ZEROS (type)))))
2036 /* (-A) * (-B) -> A * B  */
2037 (simplify
2038  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2039   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2040        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2041    (mult (convert @0) (convert (negate @1)))))
2043 /* -(A + B) -> (-B) - A.  */
2044 (simplify
2045  (negate (plus:c @0 negate_expr_p@1))
2046  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2047       && !HONOR_SIGNED_ZEROS (type))
2048   (minus (negate @1) @0)))
2050 /* -(A - B) -> B - A.  */
2051 (simplify
2052  (negate (minus @0 @1))
2053  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2054       || (FLOAT_TYPE_P (type)
2055           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2056           && !HONOR_SIGNED_ZEROS (type)))
2057   (minus @1 @0)))
2058 (simplify
2059  (negate (pointer_diff @0 @1))
2060  (if (TYPE_OVERFLOW_UNDEFINED (type))
2061   (pointer_diff @1 @0)))
2063 /* A - B -> A + (-B) if B is easily negatable.  */
2064 (simplify
2065  (minus @0 negate_expr_p@1)
2066  (if (!FIXED_POINT_TYPE_P (type))
2067  (plus @0 (negate @1))))
2069 /* 1 - a is a ^ 1 if a had a bool range. */
2070 /* This is only enabled for gimple as sometimes
2071    cfun is not set for the function which contains
2072    the SSA_NAME (e.g. while IPA passes are happening,
2073    fold might be called).  */
2074 (simplify
2075  (minus integer_onep@0 SSA_NAME@1)
2076   (if (INTEGRAL_TYPE_P (type)
2077        && ssa_name_has_boolean_range (@1))
2078    (bit_xor @1 @0)))
2080 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
2081 (simplify
2082  (negate (mult:c@0 @1 negate_expr_p@2))
2083  (if (! TYPE_UNSIGNED (type)
2084       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2085       && single_use (@0))
2086   (mult @1 (negate @2))))
2088 (simplify
2089  (negate (rdiv@0 @1 negate_expr_p@2))
2090  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2091       && single_use (@0))
2092   (rdiv @1 (negate @2))))
2094 (simplify
2095  (negate (rdiv@0 negate_expr_p@1 @2))
2096  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2097       && single_use (@0))
2098   (rdiv (negate @1) @2)))
2100 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
2101 (simplify
2102  (negate (convert? (rshift @0 INTEGER_CST@1)))
2103  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2104       && wi::to_wide (@1) == element_precision (type) - 1)
2105   (with { tree stype = TREE_TYPE (@0);
2106           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2107                                              : unsigned_type_for (stype); }
2108    (if (VECTOR_TYPE_P (type))
2109     (view_convert (rshift (view_convert:ntype @0) @1))
2110     (convert (rshift (convert:ntype @0) @1))))))
2112 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2113    when profitable.
2114    For bitwise binary operations apply operand conversions to the
2115    binary operation result instead of to the operands.  This allows
2116    to combine successive conversions and bitwise binary operations.
2117    We combine the above two cases by using a conditional convert.  */
2118 (for bitop (bit_and bit_ior bit_xor)
2119  (simplify
2120   (bitop (convert@2 @0) (convert?@3 @1))
2121   (if (((TREE_CODE (@1) == INTEGER_CST
2122          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2123          && (int_fits_type_p (@1, TREE_TYPE (@0))
2124              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2125         || types_match (@0, @1))
2126        && !POINTER_TYPE_P (TREE_TYPE (@0))
2127        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2128        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2129        /* ???  This transform conflicts with fold-const.cc doing
2130           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2131           constants (if x has signed type, the sign bit cannot be set
2132           in c).  This folds extension into the BIT_AND_EXPR.
2133           Restrict it to GIMPLE to avoid endless recursions.  */
2134        && (bitop != BIT_AND_EXPR || GIMPLE)
2135        && (/* That's a good idea if the conversion widens the operand, thus
2136               after hoisting the conversion the operation will be narrower.
2137               It is also a good if the conversion is a nop as moves the
2138               conversion to one side; allowing for combining of the conversions.  */
2139            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2140            /* The conversion check for being a nop can only be done at the gimple
2141               level as fold_binary has some re-association code which can conflict
2142               with this if there is a "constant" which is not a full INTEGER_CST.  */
2143            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2144            /* It's also a good idea if the conversion is to a non-integer
2145               mode.  */
2146            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2147            /* Or if the precision of TO is not the same as the precision
2148               of its mode.  */
2149            || !type_has_mode_precision_p (type)
2150            /* In GIMPLE, getting rid of 2 conversions for one new results
2151               in smaller IL.  */
2152            || (GIMPLE
2153                && TREE_CODE (@1) != INTEGER_CST
2154                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2155                && single_use (@2)
2156                && single_use (@3))))
2157    (convert (bitop @0 (convert @1)))))
2158  /* In GIMPLE, getting rid of 2 conversions for one new results
2159     in smaller IL.  */
2160  (simplify
2161   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2162   (if (GIMPLE
2163        && TREE_CODE (@1) != INTEGER_CST
2164        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2165        && types_match (type, @0)
2166        && !POINTER_TYPE_P (TREE_TYPE (@0))
2167        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2168    (bitop @0 (convert @1)))))
2170 (for bitop (bit_and bit_ior)
2171      rbitop (bit_ior bit_and)
2172   /* (x | y) & x -> x */
2173   /* (x & y) | x -> x */
2174  (simplify
2175   (bitop:c (rbitop:c @0 @1) @0)
2176   @0)
2177  /* (~x | y) & x -> x & y */
2178  /* (~x & y) | x -> x | y */
2179  (simplify
2180   (bitop:c (rbitop:c @2 @1) @0)
2181   (with { bool wascmp; }
2182    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2183         && (!wascmp || element_precision (type) == 1))
2184     (bitop @0 @1))))
2185   /* (x | y) & (x & z) -> (x & z) */
2186   /* (x & y) | (x | z) -> (x | z) */
2187  (simplify
2188   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2189   @3)
2190  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2191  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2192  (simplify
2193   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2194   (bitop @0 @3))
2195  /* x & ~(y | x) -> 0 */
2196  /* x | ~(y & x) -> -1 */
2197  (simplify
2198   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2199   (if (bitop == BIT_AND_EXPR)
2200    { build_zero_cst (type); }
2201    { build_minus_one_cst (type); })))
2203 /* ((x | y) & z) | x -> (z & y) | x
2204    ((x ^ y) & z) | x -> (z & y) | x  */
2205 (for op (bit_ior bit_xor)
2206  (simplify
2207   (bit_ior:c (nop_convert1?:s
2208                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2209   (if (bitwise_equal_p (@0, @3))
2210    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2212 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2213 (simplify
2214   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2215   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2217 /* Combine successive equal operations with constants.  */
2218 (for bitop (bit_and bit_ior bit_xor)
2219  (simplify
2220   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2221   (if (!CONSTANT_CLASS_P (@0))
2222    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2223       folded to a constant.  */
2224    (bitop @0 (bitop! @1 @2))
2225    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2226       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2227       the values involved are such that the operation can't be decided at
2228       compile time.  Try folding one of @0 or @1 with @2 to see whether
2229       that combination can be decided at compile time.
2231       Keep the existing form if both folds fail, to avoid endless
2232       oscillation.  */
2233    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2234     (if (cst1)
2235      (bitop @1 { cst1; })
2236      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2237       (if (cst2)
2238        (bitop @0 { cst2; }))))))))
2240 /* Try simple folding for X op !X, and X op X with the help
2241    of the truth_valued_p and logical_inverted_value predicates.  */
2242 (match truth_valued_p
2243  @0
2244  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2245 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2246  (match truth_valued_p
2247   (op @0 @1)))
2248 (match truth_valued_p
2249   (truth_not @0))
2251 (match (logical_inverted_value @0)
2252  (truth_not @0))
2253 (match (logical_inverted_value @0)
2254  (bit_not truth_valued_p@0))
2255 (match (logical_inverted_value @0)
2256  (eq @0 integer_zerop))
2257 (match (logical_inverted_value @0)
2258  (ne truth_valued_p@0 integer_truep))
2259 (match (logical_inverted_value @0)
2260  (bit_xor truth_valued_p@0 integer_truep))
2262 /* X & !X -> 0.  */
2263 (simplify
2264  (bit_and:c @0 (logical_inverted_value @0))
2265  { build_zero_cst (type); })
2266 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2267 (for op (bit_ior bit_xor)
2268  (simplify
2269   (op:c truth_valued_p@0 (logical_inverted_value @0))
2270   { constant_boolean_node (true, type); }))
2271 /* X ==/!= !X is false/true.  */
2272 (for op (eq ne)
2273  (simplify
2274   (op:c truth_valued_p@0 (logical_inverted_value @0))
2275   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2277 /* ~~x -> x */
2278 (simplify
2279   (bit_not (bit_not @0))
2280   @0)
2282 /* zero_one_valued_p will match when a value is known to be either
2283    0 or 1 including constants 0 or 1.
2284    Signed 1-bits includes -1 so they cannot match here. */
2285 (match zero_one_valued_p
2286  @0
2287  (if (INTEGRAL_TYPE_P (type)
2288       && (TYPE_UNSIGNED (type)
2289           || TYPE_PRECISION (type) > 1)
2290       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2291 (match zero_one_valued_p
2292  truth_valued_p@0
2293  (if (INTEGRAL_TYPE_P (type)
2294       && (TYPE_UNSIGNED (type)
2295           || TYPE_PRECISION (type) > 1))))
2297 /* (a&1) is always [0,1] too. This is useful again when
2298    the range is not known. */
2299 /* Note this can't be recursive due to VN handling of equivalents,
2300    VN and would cause an infinite recursion. */
2301 (match zero_one_valued_p
2302  (bit_and:c@0 @1 integer_onep)
2303  (if (INTEGRAL_TYPE_P (type))))
2305 /* A conversion from an zero_one_valued_p is still a [0,1].
2306    This is useful when the range of a variable is not known */
2307 /* Note this matches can't be recursive because of the way VN handles
2308    nop conversions being equivalent and then recursive between them. */
2309 (match zero_one_valued_p
2310  (convert@0 @1)
2311  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2312       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2313           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2314       && INTEGRAL_TYPE_P (type)
2315       && (TYPE_UNSIGNED (type)
2316           || TYPE_PRECISION (type) > 1)
2317       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2319 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2320 (simplify
2321  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2322  (if (INTEGRAL_TYPE_P (type))
2323   (bit_and @0 @1)))
2325 (for cmp (tcc_comparison)
2326      icmp (inverted_tcc_comparison)
2327  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2328  (simplify
2329   (bit_ior
2330    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2331    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2332     (if (INTEGRAL_TYPE_P (type)
2333          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2334          /* The scalar version has to be canonicalized after vectorization
2335             because it makes unconditional loads conditional ones, which
2336             means we lose vectorization because the loads may trap.  */
2337          && canonicalize_math_after_vectorization_p ())
2338      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2340  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2341     canonicalized further and we recognize the conditional form:
2342     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2343  (simplify
2344   (bit_ior
2345    (cond (cmp@0  @01 @02) @3 zerop)
2346    (cond (icmp@4 @01 @02) @5 zerop))
2347     (if (INTEGRAL_TYPE_P (type)
2348          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2349          /* The scalar version has to be canonicalized after vectorization
2350             because it makes unconditional loads conditional ones, which
2351             means we lose vectorization because the loads may trap.  */
2352          && canonicalize_math_after_vectorization_p ())
2353     (cond @0 @3 @5)))
2355  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2356     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2357  (simplify
2358   (bit_ior
2359    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2360    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2361     (if (integer_zerop (@5)
2362          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2363      (switch
2364       (if (integer_onep (@4))
2365        (bit_and (vec_cond @0 @2 @3) @4))
2366         (if (integer_minus_onep (@4))
2367          (vec_cond @0 @2 @3)))
2368     (if (integer_zerop (@4)
2369          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2370      (switch
2371       (if (integer_onep (@5))
2372        (bit_and (vec_cond @0 @3 @2) @5))
2373       (if (integer_minus_onep (@5))
2374        (vec_cond @0 @3 @2))))))
2376  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2377     into a < b ? d : c.  */
2378  (simplify
2379   (bit_ior
2380    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2381    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2382   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2383    (vec_cond @0 @2 @3))))
2385 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2386 (simplify
2387  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2388  (if (INTEGRAL_TYPE_P (type)
2389       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2390       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2391       /* Sign extending of the neg or a truncation of the neg
2392          is needed. */
2393       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2394           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2395   (mult (convert @0) @1)))
2397 /* Narrow integer multiplication by a zero_one_valued_p operand.
2398    Multiplication by [0,1] is guaranteed not to overflow.  */
2399 (simplify
2400  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2401  (if (INTEGRAL_TYPE_P (type)
2402       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2403       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2404   (mult (convert @1) (convert @2))))
2406 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2407    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2408    as some targets (such as x86's SSE) may return zero for larger C.  */
2409 (simplify
2410   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2411   (if (tree_fits_shwi_p (@1)
2412        && tree_to_shwi (@1) > 0
2413        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2414     (convert @0)))
2416 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2417    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2418    as some targets (such as x86's SSE) may return zero for larger C.  */
2419 (simplify
2420   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2421   (if (tree_fits_shwi_p (@1)
2422        && tree_to_shwi (@1) > 0
2423        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2424     (eq @0 @2)))
2426 /* Convert ~ (-A) to A - 1.  */
2427 (simplify
2428  (bit_not (convert? (negate @0)))
2429  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2430       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2431   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2433 /* Convert - (~A) to A + 1.  */
2434 (simplify
2435  (negate (nop_convert? (bit_not @0)))
2436  (plus (view_convert @0) { build_each_one_cst (type); }))
2438 /* (a & b) ^ (a == b) -> !(a | b) */
2439 /* (a & b) == (a ^ b) -> !(a | b) */
2440 (for first_op (bit_xor eq)
2441      second_op (eq bit_xor)
2442  (simplify
2443   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2444     (bit_not (bit_ior @0 @1))))
2446 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2447 (simplify
2448  (bit_not (convert? (minus @0 integer_each_onep)))
2449  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2450       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2451   (convert (negate @0))))
2452 (simplify
2453  (bit_not (convert? (plus @0 integer_all_onesp)))
2454  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2455       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2456   (convert (negate @0))))
2458 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2459 (simplify
2460  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2461  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2462   (convert (bit_xor @0 (bit_not @1)))))
2463 (simplify
2464  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2465  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2466   (convert (bit_xor @0 @1))))
2468 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2469 (simplify
2470  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2471  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2472   (bit_not (bit_xor (view_convert @0) @1))))
2474 /* ~(a ^ b) is a == b for truth valued a and b.  */
2475 (simplify
2476  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2477  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2478       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2479   (convert (eq @0 @1))))
2481 /* (~a) == b is a ^ b for truth valued a and b.  */
2482 (simplify
2483  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2484  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2485       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2486   (convert (bit_xor @0 @1))))
2488 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2489 (simplify
2490  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2491  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2493 /* Fold A - (A & B) into ~B & A.  */
2494 (simplify
2495  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2496  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2497       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2498   (convert (bit_and (bit_not @1) @0))))
2500 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2501 (if (!canonicalize_math_p ())
2502  (for cmp (tcc_comparison)
2503   (simplify
2504    (mult:c (convert (cmp@0 @1 @2)) @3)
2505    (if (INTEGRAL_TYPE_P (type)
2506         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2507      (cond @0 @3 { build_zero_cst (type); })))
2508 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2509   (simplify
2510    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2511    (if (INTEGRAL_TYPE_P (type)
2512         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2513      (cond @0 @3 { build_zero_cst (type); })))
2517 /* For integral types with undefined overflow and C != 0 fold
2518    x * C EQ/NE y * C into x EQ/NE y.  */
2519 (for cmp (eq ne)
2520  (simplify
2521   (cmp (mult:c @0 @1) (mult:c @2 @1))
2522   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2523        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2524        && tree_expr_nonzero_p (@1))
2525    (cmp @0 @2))))
2527 /* For integral types with wrapping overflow and C odd fold
2528    x * C EQ/NE y * C into x EQ/NE y.  */
2529 (for cmp (eq ne)
2530  (simplify
2531   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2532   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2533        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2534        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2535    (cmp @0 @2))))
2537 /* For integral types with undefined overflow and C != 0 fold
2538    x * C RELOP y * C into:
2540    x RELOP y for nonnegative C
2541    y RELOP x for negative C  */
2542 (for cmp (lt gt le ge)
2543  (simplify
2544   (cmp (mult:c @0 @1) (mult:c @2 @1))
2545   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2546        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2547    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2548     (cmp @0 @2)
2549    (if (TREE_CODE (@1) == INTEGER_CST
2550         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2551     (cmp @2 @0))))))
2553 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2554 (for cmp (le gt)
2555      icmp (gt le)
2556  (simplify
2557   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2558    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2559         && TYPE_UNSIGNED (TREE_TYPE (@0))
2560         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2561         && (wi::to_wide (@2)
2562             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2563     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2564      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2566 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2567 (for cmp (simple_comparison)
2568  (simplify
2569   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2570   (if (element_precision (@3) >= element_precision (@0)
2571        && types_match (@0, @1))
2572    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2573     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2574      (cmp @1 @0)
2575      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2576       (with
2577        {
2578         tree utype = unsigned_type_for (TREE_TYPE (@0));
2579        }
2580        (cmp (convert:utype @1) (convert:utype @0)))))
2581     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2582      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2583       (cmp @0 @1)
2584       (with
2585        {
2586         tree utype = unsigned_type_for (TREE_TYPE (@0));
2587        }
2588        (cmp (convert:utype @0) (convert:utype @1)))))))))
2590 /* X / C1 op C2 into a simple range test.  */
2591 (for cmp (simple_comparison)
2592  (simplify
2593   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2594   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2595        && integer_nonzerop (@1)
2596        && !TREE_OVERFLOW (@1)
2597        && !TREE_OVERFLOW (@2))
2598    (with { tree lo, hi; bool neg_overflow;
2599            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2600                                                    &neg_overflow); }
2601     (switch
2602      (if (code == LT_EXPR || code == GE_EXPR)
2603        (if (TREE_OVERFLOW (lo))
2604         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2605         (if (code == LT_EXPR)
2606          (lt @0 { lo; })
2607          (ge @0 { lo; }))))
2608      (if (code == LE_EXPR || code == GT_EXPR)
2609        (if (TREE_OVERFLOW (hi))
2610         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2611         (if (code == LE_EXPR)
2612          (le @0 { hi; })
2613          (gt @0 { hi; }))))
2614      (if (!lo && !hi)
2615       { build_int_cst (type, code == NE_EXPR); })
2616      (if (code == EQ_EXPR && !hi)
2617       (ge @0 { lo; }))
2618      (if (code == EQ_EXPR && !lo)
2619       (le @0 { hi; }))
2620      (if (code == NE_EXPR && !hi)
2621       (lt @0 { lo; }))
2622      (if (code == NE_EXPR && !lo)
2623       (gt @0 { hi; }))
2624      (if (GENERIC)
2625       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2626                            lo, hi); })
2627      (with
2628       {
2629         tree etype = range_check_type (TREE_TYPE (@0));
2630         if (etype)
2631           {
2632             hi = fold_convert (etype, hi);
2633             lo = fold_convert (etype, lo);
2634             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2635           }
2636       }
2637       (if (etype && hi && !TREE_OVERFLOW (hi))
2638        (if (code == EQ_EXPR)
2639         (le (minus (convert:etype @0) { lo; }) { hi; })
2640         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2642 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2643 (for op (lt le ge gt)
2644  (simplify
2645   (op (plus:c @0 @2) (plus:c @1 @2))
2646   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2647        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2648    (op @0 @1))))
2650 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2651    when C is an unsigned integer constant with only the MSB set, and X and
2652    Y have types of equal or lower integer conversion rank than C's.  */
2653 (for op (lt le ge gt)
2654  (simplify
2655   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2656   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2657        && TYPE_UNSIGNED (TREE_TYPE (@0))
2658        && wi::only_sign_bit_p (wi::to_wide (@0)))
2659    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2660     (op (convert:stype @1) (convert:stype @2))))))
2662 /* For equality and subtraction, this is also true with wrapping overflow.  */
2663 (for op (eq ne minus)
2664  (simplify
2665   (op (plus:c @0 @2) (plus:c @1 @2))
2666   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2667        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2668            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2669    (op @0 @1))))
2670 /* And similar for pointers.  */
2671 (for op (eq ne)
2672  (simplify
2673   (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2674   (op @1 @2)))
2675 (simplify
2676  (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2677  (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2678   (convert (minus @1 @2))))
2680 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2681 (for op (lt le ge gt)
2682  (simplify
2683   (op (minus @0 @2) (minus @1 @2))
2684   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2685        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2686    (op @0 @1))))
2687 /* For equality and subtraction, this is also true with wrapping overflow.  */
2688 (for op (eq ne minus)
2689  (simplify
2690   (op (minus @0 @2) (minus @1 @2))
2691   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2692        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2693            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2694    (op @0 @1))))
2695 /* And for pointers...  */
2696 (for op (simple_comparison)
2697  (simplify
2698   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2699   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2700    (op @0 @1))))
2701 (simplify
2702  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2703  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2704       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2705   (pointer_diff @0 @1)))
2707 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2708 (for op (lt le ge gt)
2709  (simplify
2710   (op (minus @2 @0) (minus @2 @1))
2711   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2712        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2713    (op @1 @0))))
2714 /* For equality and subtraction, this is also true with wrapping overflow.  */
2715 (for op (eq ne minus)
2716  (simplify
2717   (op (minus @2 @0) (minus @2 @1))
2718   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2719        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2720            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2721    (op @1 @0))))
2722 /* And for pointers...  */
2723 (for op (simple_comparison)
2724  (simplify
2725   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2726   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2727    (op @1 @0))))
2728 (simplify
2729  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2730  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2731       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2732   (pointer_diff @1 @0)))
2734 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2735 (for op (lt le gt ge)
2736  (simplify
2737   (op:c (plus:c@2 @0 @1) @1)
2738   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2739        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2740        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2741        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2742    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2743 /* For equality, this is also true with wrapping overflow.  */
2744 (for op (eq ne)
2745  (simplify
2746   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2747   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2748        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2749            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2750        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2751        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2752        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2753    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2754  (simplify
2755   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2756   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2757        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2758        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2759    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2761 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2762 (for neeq (ne eq)
2763  (simplify
2764   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2765    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2766     (if (ptr_difference_const (@0, @2, &diff))
2767      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2768  (simplify
2769   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2770    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2771     (if (ptr_difference_const (@0, @2, &diff))
2772      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2774 /* X - Y < X is the same as Y > 0 when there is no overflow.
2775    For equality, this is also true with wrapping overflow.  */
2776 (for op (simple_comparison)
2777  (simplify
2778   (op:c @0 (minus@2 @0 @1))
2779   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2780        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2781            || ((op == EQ_EXPR || op == NE_EXPR)
2782                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2783        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2784    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2786 /* Transform:
2787    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2788    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2789 (for cmp (eq ne)
2790      ocmp (lt ge)
2791  (simplify
2792   (cmp (trunc_div @0 @1) integer_zerop)
2793   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2794        /* Complex ==/!= is allowed, but not </>=.  */
2795        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2796        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2797    (ocmp @0 @1))))
2799 /* X == C - X can never be true if C is odd.  */
2800 (for cmp (eq ne)
2801  (simplify
2802   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2803   (if (TREE_INT_CST_LOW (@1) & 1)
2804    { constant_boolean_node (cmp == NE_EXPR, type); })))
2807    U & N <= U  -> true
2808    U & N >  U  -> false
2809    U needs to be non-negative.
2811    U | N <  U  -> false
2812    U | N >= U  -> true
2813    U and N needs to be non-negative
2815    U | N <  U  -> true
2816    U | N >= U  -> false
2817    U needs to be non-negative and N needs to be a negative constant.
2818    */
2819 (for cmp   (lt      ge      le      gt     )
2820      bitop (bit_ior bit_ior bit_and bit_and)
2821  (simplify
2822   (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2823   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2824    (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2825     { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2826     /* The sign is opposite now so the comparison is swapped around. */
2827     (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2828      { constant_boolean_node (cmp == LT_EXPR, type); })))))
2830 /* Arguments on which one can call get_nonzero_bits to get the bits
2831    possibly set.  */
2832 (match with_possible_nonzero_bits
2833  INTEGER_CST@0)
2834 (match with_possible_nonzero_bits
2835  SSA_NAME@0
2836  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2837 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2838 (match (with_possible_nonzero_bits2 @0)
2839  with_possible_nonzero_bits@0)
2840 (match (with_possible_nonzero_bits2 @0)
2841  (bit_and:c with_possible_nonzero_bits@0 @2))
2843 /* Same for bits that are known to be set, but we do not have
2844    an equivalent to get_nonzero_bits yet.  */
2845 (match (with_certain_nonzero_bits2 @0)
2846  INTEGER_CST@0)
2847 (match (with_certain_nonzero_bits2 @0)
2848  (bit_ior @1 INTEGER_CST@0))
2850 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2851 (for cmp (eq ne)
2852  (simplify
2853   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2854   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2855    { constant_boolean_node (cmp == NE_EXPR, type); })))
2857 /* ((X inner_op C0) outer_op C1)
2858    With X being a tree where value_range has reasoned certain bits to always be
2859    zero throughout its computed value range,
2860    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2861    where zero_mask has 1's for all bits that are sure to be 0 in
2862    and 0's otherwise.
2863    if (inner_op == '^') C0 &= ~C1;
2864    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2865    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2867 (for inner_op (bit_ior bit_xor)
2868      outer_op (bit_xor bit_ior)
2869 (simplify
2870  (outer_op
2871   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2872  (with
2873   {
2874     bool fail = false;
2875     wide_int zero_mask_not;
2876     wide_int C0;
2877     wide_int cst_emit;
2879     if (TREE_CODE (@2) == SSA_NAME)
2880       zero_mask_not = get_nonzero_bits (@2);
2881     else
2882       fail = true;
2884     if (inner_op == BIT_XOR_EXPR)
2885       {
2886         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2887         cst_emit = C0 | wi::to_wide (@1);
2888       }
2889     else
2890       {
2891         C0 = wi::to_wide (@0);
2892         cst_emit = C0 ^ wi::to_wide (@1);
2893       }
2894   }
2895   (if (!fail && (C0 & zero_mask_not) == 0)
2896    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2897    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2898     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2900 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2901 (simplify
2902   (pointer_plus (pointer_plus:s @0 @1) @3)
2903   (pointer_plus @0 (plus @1 @3)))
2904 #if GENERIC
2905 (simplify
2906   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2907   (convert:type (pointer_plus @0 (plus @1 @3))))
2908 #endif
2910 /* Pattern match
2911      tem1 = (long) ptr1;
2912      tem2 = (long) ptr2;
2913      tem3 = tem2 - tem1;
2914      tem4 = (unsigned long) tem3;
2915      tem5 = ptr1 + tem4;
2916    and produce
2917      tem5 = ptr2;  */
2918 (simplify
2919   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2920   /* Conditionally look through a sign-changing conversion.  */
2921   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2922        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2923             || (GENERIC && type == TREE_TYPE (@1))))
2924    @1))
2925 (simplify
2926   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2927   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2928    (convert @1)))
2930 /* Pattern match
2931      tem = (sizetype) ptr;
2932      tem = tem & algn;
2933      tem = -tem;
2934      ... = ptr p+ tem;
2935    and produce the simpler and easier to analyze with respect to alignment
2936      ... = ptr & ~algn;  */
2937 (simplify
2938   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2939   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2940    (bit_and @0 { algn; })))
2942 /* Try folding difference of addresses.  */
2943 (simplify
2944  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2945  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2946   (with { poly_int64 diff; }
2947    (if (ptr_difference_const (@0, @1, &diff))
2948     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2949 (simplify
2950  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2951  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2952   (with { poly_int64 diff; }
2953    (if (ptr_difference_const (@0, @1, &diff))
2954     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2955 (simplify
2956  (minus (convert ADDR_EXPR@0) (convert @1))
2957  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2958   (with { poly_int64 diff; }
2959    (if (ptr_difference_const (@0, @1, &diff))
2960     { build_int_cst_type (type, diff); }))))
2961 (simplify
2962  (minus (convert @0) (convert ADDR_EXPR@1))
2963  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2964   (with { poly_int64 diff; }
2965    (if (ptr_difference_const (@0, @1, &diff))
2966     { build_int_cst_type (type, diff); }))))
2967 (simplify
2968  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2969  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2970       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2971   (with { poly_int64 diff; }
2972    (if (ptr_difference_const (@0, @1, &diff))
2973     { build_int_cst_type (type, diff); }))))
2974 (simplify
2975  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2976  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2977       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2978   (with { poly_int64 diff; }
2979    (if (ptr_difference_const (@0, @1, &diff))
2980     { build_int_cst_type (type, diff); }))))
2982 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2983 (simplify
2984  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2985  (with { poly_int64 diff; }
2986    (if (ptr_difference_const (@0, @2, &diff))
2987     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2988 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2989 (simplify
2990  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2991  (with { poly_int64 diff; }
2992    (if (ptr_difference_const (@0, @2, &diff))
2993     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2994 (simplify
2995  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2996  (with { poly_int64 diff; }
2997    (if (ptr_difference_const (@0, @1, &diff))
2998     (minus { build_int_cst_type (type, diff); } (convert @2)))))
3000 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
3001 (simplify
3002  (convert (pointer_diff @0 INTEGER_CST@1))
3003  (if (POINTER_TYPE_P (type))
3004   { build_fold_addr_expr_with_type
3005       (build2 (MEM_REF, char_type_node, @0,
3006                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
3007                type); }))
3009 /* If arg0 is derived from the address of an object or function, we may
3010    be able to fold this expression using the object or function's
3011    alignment.  */
3012 (simplify
3013  (bit_and (convert? @0) INTEGER_CST@1)
3014  (if (POINTER_TYPE_P (TREE_TYPE (@0))
3015       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3016   (with
3017    {
3018      unsigned int align;
3019      unsigned HOST_WIDE_INT bitpos;
3020      get_pointer_alignment_1 (@0, &align, &bitpos);
3021    }
3022    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
3023     { wide_int_to_tree (type, (wi::to_wide (@1)
3024                                & (bitpos / BITS_PER_UNIT))); }))))
3026 (match min_value
3027  uniform_integer_cst_p
3028  (with {
3029    tree int_cst = uniform_integer_cst_p (t);
3030    tree inner_type = TREE_TYPE (int_cst);
3031   }
3032   (if ((INTEGRAL_TYPE_P (inner_type)
3033         || POINTER_TYPE_P (inner_type))
3034        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3036 (match max_value
3037  uniform_integer_cst_p
3038  (with {
3039    tree int_cst = uniform_integer_cst_p (t);
3040    tree itype = TREE_TYPE (int_cst);
3041   }
3042  (if ((INTEGRAL_TYPE_P (itype)
3043        || POINTER_TYPE_P (itype))
3044       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3046 /* x >  y  &&  x != XXX_MIN  -->  x > y
3047    x >  y  &&  x == XXX_MIN  -->  false . */
3048 (for eqne (eq ne)
3049  (simplify
3050   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3051    (switch
3052     (if (eqne == EQ_EXPR)
3053      { constant_boolean_node (false, type); })
3054     (if (eqne == NE_EXPR)
3055      @2)
3056     )))
3058 /* x <  y  &&  x != XXX_MAX  -->  x < y
3059    x <  y  &&  x == XXX_MAX  -->  false.  */
3060 (for eqne (eq ne)
3061  (simplify
3062   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3063    (switch
3064     (if (eqne == EQ_EXPR)
3065      { constant_boolean_node (false, type); })
3066     (if (eqne == NE_EXPR)
3067      @2)
3068     )))
3070 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
3071 (simplify
3072  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3073   @2)
3075 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
3076 (simplify
3077  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3078   @2)
3080 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
3081 (simplify
3082  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3083   @2)
3085 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
3086 (simplify
3087  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3088   { constant_boolean_node (true, type); })
3090 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
3091 (simplify
3092  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3093   @2)
3095 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
3096 (simplify
3097  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3098   @2)
3100 /* x >=  y  ||  x != XXX_MAX   -->  true
3101    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
3102 (for eqne (eq ne)
3103  (simplify
3104   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3105    (switch
3106     (if (eqne == EQ_EXPR)
3107      @2)
3108     (if (eqne == NE_EXPR)
3109      { constant_boolean_node (true, type); }))))
3111 /* y == XXX_MIN || x < y --> x <= y - 1 */
3112 (simplify
3113  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3114   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3115        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3116   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3118 /* y != XXX_MIN && x >= y --> x > y - 1 */
3119 (simplify
3120  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3121   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3122        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3123   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3125 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3126    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3127 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3128    Similarly for (X != Y).  */
3130 (for code1 (eq ne)
3131  (for code2 (eq ne lt gt le ge)
3132   (simplify
3133    (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3134    (if ((TREE_CODE (@1) == INTEGER_CST
3135          && TREE_CODE (@2) == INTEGER_CST)
3136         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3137              || POINTER_TYPE_P (TREE_TYPE (@1)))
3138             && bitwise_equal_p (@1, @2)))
3139     (with
3140      {
3141       bool one_before = false;
3142       bool one_after = false;
3143       int cmp = 0;
3144       bool allbits = true;
3145       if (TREE_CODE (@1) == INTEGER_CST
3146           && TREE_CODE (@2) == INTEGER_CST)
3147         {
3148           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3149           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3150           auto t2 = wi::to_wide (@2);
3151           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3152           if (cmp < 0
3153               && t1 == t2 - 1)
3154             one_before = true;
3155           if (cmp > 0
3156               && t1 == t2 + 1)
3157             one_after = true;
3158         }
3159       bool val;
3160       switch (code2)
3161          {
3162         case EQ_EXPR: val = (cmp == 0); break;
3163         case NE_EXPR: val = (cmp != 0); break;
3164         case LT_EXPR: val = (cmp < 0); break;
3165         case GT_EXPR: val = (cmp > 0); break;
3166         case LE_EXPR: val = (cmp <= 0); break;
3167         case GE_EXPR: val = (cmp >= 0); break;
3168         default: gcc_unreachable ();
3169         }
3170      }
3171      (switch
3172       (if (code1 == EQ_EXPR && val) @3)
3173       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3174       (if (code1 == NE_EXPR && !val && allbits) @4)
3175       (if (code1 == NE_EXPR
3176            && code2 == GE_EXPR
3177            && cmp == 0
3178            && allbits)
3179        (gt @c0 (convert @1)))
3180       (if (code1 == NE_EXPR
3181            && code2 == LE_EXPR
3182            && cmp == 0
3183            && allbits)
3184        (lt @c0 (convert @1)))
3185       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3186       (if (code1 == NE_EXPR
3187            && code2 == GT_EXPR
3188            && one_after
3189            && allbits)
3190        (gt @c0 (convert @1)))
3191       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3192       (if (code1 == NE_EXPR
3193            && code2 == LT_EXPR
3194            && one_before
3195            && allbits)
3196        (lt @c0 (convert @1)))
3197      )
3198     )
3199    )
3200   )
3204 /* Convert (X OP1 CST1) && (X OP2 CST2).
3205    Convert (X OP1 Y) && (X OP2 Y).  */
3207 (for code1 (lt le gt ge)
3208  (for code2 (lt le gt ge)
3209   (simplify
3210   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3211   (if ((TREE_CODE (@1) == INTEGER_CST
3212         && TREE_CODE (@2) == INTEGER_CST)
3213        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3214             || POINTER_TYPE_P (TREE_TYPE (@1)))
3215            && operand_equal_p (@1, @2)))
3216    (with
3217     {
3218      int cmp = 0;
3219      if (TREE_CODE (@1) == INTEGER_CST
3220          && TREE_CODE (@2) == INTEGER_CST)
3221        cmp = tree_int_cst_compare (@1, @2);
3222     }
3223     (switch
3224      /* Choose the more restrictive of two < or <= comparisons.  */
3225      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3226           && (code2 == LT_EXPR || code2 == LE_EXPR))
3227       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3228        @3
3229        @4))
3230      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3231      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3232           && (code2 == GT_EXPR || code2 == GE_EXPR))
3233       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3234        @3
3235        @4))
3236      /* Check for singleton ranges.  */
3237      (if (cmp == 0
3238           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3239             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3240       (eq @0 @1))
3241      /* Check for disjoint ranges.  */
3242      (if (cmp <= 0
3243           && (code1 == LT_EXPR || code1 == LE_EXPR)
3244           && (code2 == GT_EXPR || code2 == GE_EXPR))
3245       { constant_boolean_node (false, type); })
3246      (if (cmp >= 0
3247           && (code1 == GT_EXPR || code1 == GE_EXPR)
3248           && (code2 == LT_EXPR || code2 == LE_EXPR))
3249       { constant_boolean_node (false, type); })
3250      ))))))
3252 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3253    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3254 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3255    Similarly for (X != Y).  */
3257 (for code1 (eq ne)
3258  (for code2 (eq ne lt gt le ge)
3259   (simplify
3260    (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3261    (if ((TREE_CODE (@1) == INTEGER_CST
3262          && TREE_CODE (@2) == INTEGER_CST)
3263         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3264             || POINTER_TYPE_P (TREE_TYPE (@1)))
3265             && bitwise_equal_p (@1, @2)))
3266     (with
3267      {
3268       bool one_before = false;
3269       bool one_after = false;
3270       int cmp = 0;
3271       bool allbits = true;
3272       if (TREE_CODE (@1) == INTEGER_CST
3273           && TREE_CODE (@2) == INTEGER_CST)
3274         {
3275           allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3276           auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3277           auto t2 = wi::to_wide (@2);
3278           cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3279           if (cmp < 0
3280               && t1 == t2 - 1)
3281             one_before = true;
3282           if (cmp > 0
3283               && t1 == t2 + 1)
3284             one_after = true;
3285         }
3286       bool val;
3287       switch (code2)
3288         {
3289         case EQ_EXPR: val = (cmp == 0); break;
3290         case NE_EXPR: val = (cmp != 0); break;
3291         case LT_EXPR: val = (cmp < 0); break;
3292         case GT_EXPR: val = (cmp > 0); break;
3293         case LE_EXPR: val = (cmp <= 0); break;
3294         case GE_EXPR: val = (cmp >= 0); break;
3295         default: gcc_unreachable ();
3296         }
3297      }
3298      (switch
3299       (if (code1 == EQ_EXPR && val) @4)
3300       (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3301       (if (code1 == NE_EXPR && !val && allbits) @3)
3302       (if (code1 == EQ_EXPR
3303            && code2 == GT_EXPR
3304            && cmp == 0
3305            && allbits)
3306        (ge @c0 @2))
3307       (if (code1 == EQ_EXPR
3308            && code2 == LT_EXPR
3309            && cmp == 0
3310            && allbits)
3311        (le @c0 @2))
3312       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3313       (if (code1 == EQ_EXPR
3314            && code2 == GE_EXPR
3315            && one_before
3316            && allbits)
3317        (ge @c0 (convert @1)))
3318       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3319       (if (code1 == EQ_EXPR
3320            && code2 == LE_EXPR
3321            && one_after
3322            && allbits)
3323        (le @c0 (convert @1)))
3324      )
3325     )
3326    )
3327   )
3331 /* Convert (X OP1 CST1) || (X OP2 CST2).
3332    Convert (X OP1 Y)    || (X OP2 Y).  */
3334 (for code1 (lt le gt ge)
3335  (for code2 (lt le gt ge)
3336   (simplify
3337   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3338   (if ((TREE_CODE (@1) == INTEGER_CST
3339         && TREE_CODE (@2) == INTEGER_CST)
3340        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3341             || POINTER_TYPE_P (TREE_TYPE (@1)))
3342            && operand_equal_p (@1, @2)))
3343    (with
3344     {
3345      int cmp = 0;
3346      if (TREE_CODE (@1) == INTEGER_CST
3347          && TREE_CODE (@2) == INTEGER_CST)
3348        cmp = tree_int_cst_compare (@1, @2);
3349     }
3350     (switch
3351      /* Choose the more restrictive of two < or <= comparisons.  */
3352      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3353           && (code2 == LT_EXPR || code2 == LE_EXPR))
3354       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3355        @4
3356        @3))
3357      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3358      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3359           && (code2 == GT_EXPR || code2 == GE_EXPR))
3360       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3361        @4
3362        @3))
3363      /* Check for singleton ranges.  */
3364      (if (cmp == 0
3365           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3366               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3367       (ne @0 @2))
3368      /* Check for disjoint ranges.  */
3369      (if (cmp >= 0
3370           && (code1 == LT_EXPR || code1 == LE_EXPR)
3371           && (code2 == GT_EXPR || code2 == GE_EXPR))
3372       { constant_boolean_node (true, type); })
3373      (if (cmp <= 0
3374           && (code1 == GT_EXPR || code1 == GE_EXPR)
3375           && (code2 == LT_EXPR || code2 == LE_EXPR))
3376       { constant_boolean_node (true, type); })
3377      ))))))
3379 /* Optimize (a CMP b) ^ (a CMP b)  */
3380 /* Optimize (a CMP b) != (a CMP b)  */
3381 (for op (bit_xor ne)
3382  (for cmp1 (lt lt lt le le le)
3383       cmp2 (gt eq ne ge eq ne)
3384       rcmp (ne le gt ne lt ge)
3385   (simplify
3386    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3387    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3388     (rcmp @0 @1)))))
3390 /* Optimize (a CMP b) == (a CMP b)  */
3391 (for cmp1 (lt lt lt le le le)
3392      cmp2 (gt eq ne ge eq ne)
3393      rcmp (eq gt le eq ge lt)
3394  (simplify
3395   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3396   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3397     (rcmp @0 @1))))
3399 /* (type)([0,1]@a != 0) -> (type)a
3400    (type)([0,1]@a == 1) -> (type)a
3401    (type)([0,1]@a == 0) -> a ^ 1
3402    (type)([0,1]@a != 1) -> a ^ 1.  */
3403 (for eqne (eq ne)
3404  (simplify
3405   (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3406   (if ((integer_zerop (@1) || integer_onep (@1)))
3407    (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3408     (convert @0)
3409     /* Only do this if the types match as (type)(a == 0) is
3410        canonical form normally, while `a ^ 1` is canonical when
3411        there is no type change. */
3412     (if (types_match (type, TREE_TYPE (@0)))
3413      (bit_xor @0 { build_one_cst (type); } ))))))
3415 /* We can't reassociate at all for saturating types.  */
3416 (if (!TYPE_SATURATING (type))
3418  /* Contract negates.  */
3419  /* A + (-B) -> A - B */
3420  (simplify
3421   (plus:c @0 (convert? (negate @1)))
3422   /* Apply STRIP_NOPS on the negate.  */
3423   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3424        && !TYPE_OVERFLOW_SANITIZED (type))
3425    (with
3426     {
3427      tree t1 = type;
3428      if (INTEGRAL_TYPE_P (type)
3429          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3430        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3431     }
3432     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3433  /* A - (-B) -> A + B */
3434  (simplify
3435   (minus @0 (convert? (negate @1)))
3436   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3437        && !TYPE_OVERFLOW_SANITIZED (type))
3438    (with
3439     {
3440      tree t1 = type;
3441      if (INTEGRAL_TYPE_P (type)
3442          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3443        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3444     }
3445     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3446  /* -(T)(-A) -> (T)A
3447     Sign-extension is ok except for INT_MIN, which thankfully cannot
3448     happen without overflow.  */
3449  (simplify
3450   (negate (convert (negate @1)))
3451   (if (INTEGRAL_TYPE_P (type)
3452        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3453            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3454                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3455        && !TYPE_OVERFLOW_SANITIZED (type)
3456        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3457    (convert @1)))
3458  (simplify
3459   (negate (convert negate_expr_p@1))
3460   (if (SCALAR_FLOAT_TYPE_P (type)
3461        && ((DECIMAL_FLOAT_TYPE_P (type)
3462             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3463             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3464            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3465    (convert (negate @1))))
3466  (simplify
3467   (negate (nop_convert? (negate @1)))
3468   (if (!TYPE_OVERFLOW_SANITIZED (type)
3469        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3470    (view_convert @1)))
3472  /* We can't reassociate floating-point unless -fassociative-math
3473     or fixed-point plus or minus because of saturation to +-Inf.  */
3474  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3475       && !FIXED_POINT_TYPE_P (type))
3477   /* Match patterns that allow contracting a plus-minus pair
3478      irrespective of overflow issues.  */
3479   /* (A +- B) - A       ->  +- B */
3480   /* (A +- B) -+ B      ->  A */
3481   /* A - (A +- B)       -> -+ B */
3482   /* A +- (B -+ A)      ->  +- B */
3483   (simplify
3484    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3485    (view_convert @1))
3486   (simplify
3487    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3488    (if (!ANY_INTEGRAL_TYPE_P (type)
3489         || TYPE_OVERFLOW_WRAPS (type))
3490    (negate (view_convert @1))
3491    (view_convert (negate @1))))
3492   (simplify
3493    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3494    (view_convert @0))
3495   (simplify
3496    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3497     (if (!ANY_INTEGRAL_TYPE_P (type)
3498          || TYPE_OVERFLOW_WRAPS (type))
3499      (negate (view_convert @1))
3500      (view_convert (negate @1))))
3501   (simplify
3502    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3503    (view_convert @1))
3504   /* (A +- B) + (C - A)   -> C +- B */
3505   /* (A +  B) - (A - C)   -> B + C */
3506   /* More cases are handled with comparisons.  */
3507   (simplify
3508    (plus:c (plus:c @0 @1) (minus @2 @0))
3509    (plus @2 @1))
3510   (simplify
3511    (plus:c (minus @0 @1) (minus @2 @0))
3512    (minus @2 @1))
3513   (simplify
3514    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3515    (if (TYPE_OVERFLOW_UNDEFINED (type)
3516         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3517     (pointer_diff @2 @1)))
3518   (simplify
3519    (minus (plus:c @0 @1) (minus @0 @2))
3520    (plus @1 @2))
3522   /* (A +- CST1) +- CST2 -> A + CST3
3523      Use view_convert because it is safe for vectors and equivalent for
3524      scalars.  */
3525   (for outer_op (plus minus)
3526    (for inner_op (plus minus)
3527         neg_inner_op (minus plus)
3528     (simplify
3529      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3530                CONSTANT_CLASS_P@2)
3531      /* If one of the types wraps, use that one.  */
3532      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3533       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3534          forever if something doesn't simplify into a constant.  */
3535       (if (!CONSTANT_CLASS_P (@0))
3536        (if (outer_op == PLUS_EXPR)
3537         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3538         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3539       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3540            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3541        (if (outer_op == PLUS_EXPR)
3542         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3543         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3544        /* If the constant operation overflows we cannot do the transform
3545           directly as we would introduce undefined overflow, for example
3546           with (a - 1) + INT_MIN.  */
3547        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3548         (with { tree cst = const_binop (outer_op == inner_op
3549                                         ? PLUS_EXPR : MINUS_EXPR,
3550                                         type, @1, @2); }
3551          (if (cst)
3552           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3553            (inner_op @0 { cst; } )
3554            /* X+INT_MAX+1 is X-INT_MIN.  */
3555            (if (INTEGRAL_TYPE_P (type)
3556                 && wi::to_wide (cst) == wi::min_value (type))
3557             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3558             /* Last resort, use some unsigned type.  */
3559             (with { tree utype = unsigned_type_for (type); }
3560              (if (utype)
3561               (view_convert (inner_op
3562                              (view_convert:utype @0)
3563                              (view_convert:utype
3564                               { TREE_OVERFLOW (cst)
3565                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3567   /* (CST1 - A) +- CST2 -> CST3 - A  */
3568   (for outer_op (plus minus)
3569    (simplify
3570     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3571     /* If one of the types wraps, use that one.  */
3572     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3573      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3574         forever if something doesn't simplify into a constant.  */
3575      (if (!CONSTANT_CLASS_P (@0))
3576       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3577      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3578           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3579       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3580       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3581        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3582         (if (cst && !TREE_OVERFLOW (cst))
3583          (minus { cst; } @0))))))))
3585   /* CST1 - (CST2 - A) -> CST3 + A
3586      Use view_convert because it is safe for vectors and equivalent for
3587      scalars.  */
3588   (simplify
3589    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3590    /* If one of the types wraps, use that one.  */
3591    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3592     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3593       forever if something doesn't simplify into a constant.  */
3594     (if (!CONSTANT_CLASS_P (@0))
3595      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3596     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3597          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3598      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3599      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3600       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3601        (if (cst && !TREE_OVERFLOW (cst))
3602         (plus { cst; } @0)))))))
3604 /* ((T)(A)) + CST -> (T)(A + CST)  */
3605 #if GIMPLE
3606   (simplify
3607    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3608     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3609          && TREE_CODE (type) == INTEGER_TYPE
3610          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3611          && int_fits_type_p (@1, TREE_TYPE (@0)))
3612      /* Perform binary operation inside the cast if the constant fits
3613         and (A + CST)'s range does not overflow.  */
3614      (with
3615       {
3616         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3617                           max_ovf = wi::OVF_OVERFLOW;
3618         tree inner_type = TREE_TYPE (@0);
3620         wide_int w1
3621           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3622                             TYPE_SIGN (inner_type));
3624         value_range vr;
3625         if (get_global_range_query ()->range_of_expr (vr, @0)
3626             && !vr.varying_p () && !vr.undefined_p ())
3627           {
3628             wide_int wmin0 = vr.lower_bound ();
3629             wide_int wmax0 = vr.upper_bound ();
3630             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3631             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3632           }
3633       }
3634      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3635       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3636      )))
3637 #endif
3639 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3640 #if GIMPLE
3641   (for op (plus minus)
3642    (simplify
3643     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3644      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3645           && TREE_CODE (type) == INTEGER_TYPE
3646           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3647           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3648           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3649           && TYPE_OVERFLOW_WRAPS (type))
3650        (plus (convert @0) (op @2 (convert @1))))))
3651 #endif
3653 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3654    to a simple value.  */
3655   (for op (plus minus)
3656    (simplify
3657     (op (convert @0) (convert @1))
3658      (if (INTEGRAL_TYPE_P (type)
3659           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3660           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3661           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3662           && !TYPE_OVERFLOW_TRAPS (type)
3663           && !TYPE_OVERFLOW_SANITIZED (type))
3664       (convert (op! @0 @1)))))
3666   /* ~A + A -> -1 */
3667   (simplify
3668    (plus:c (convert? (bit_not @0)) (convert? @0))
3669    (if (!TYPE_OVERFLOW_TRAPS (type))
3670     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3672   /* ~A + 1 -> -A */
3673   (simplify
3674    (plus (convert? (bit_not @0)) integer_each_onep)
3675    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3676     (negate (convert @0))))
3678   /* -A - 1 -> ~A */
3679   (simplify
3680    (minus (convert? (negate @0)) integer_each_onep)
3681    (if (!TYPE_OVERFLOW_TRAPS (type)
3682         && TREE_CODE (type) != COMPLEX_TYPE
3683         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3684     (bit_not (convert @0))))
3686   /* -1 - A -> ~A */
3687   (simplify
3688    (minus integer_all_onesp @0)
3689    (if (TREE_CODE (type) != COMPLEX_TYPE)
3690     (bit_not @0)))
3692   /* (T)(P + A) - (T)P -> (T) A */
3693   (simplify
3694    (minus (convert (plus:c @@0 @1))
3695     (convert? @0))
3696    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3697         /* For integer types, if A has a smaller type
3698            than T the result depends on the possible
3699            overflow in P + A.
3700            E.g. T=size_t, A=(unsigned)429497295, P>0.
3701            However, if an overflow in P + A would cause
3702            undefined behavior, we can assume that there
3703            is no overflow.  */
3704         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3705             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3706     (convert @1)))
3707   (simplify
3708    (minus (convert (pointer_plus @@0 @1))
3709     (convert @0))
3710    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3711         /* For pointer types, if the conversion of A to the
3712            final type requires a sign- or zero-extension,
3713            then we have to punt - it is not defined which
3714            one is correct.  */
3715         || (POINTER_TYPE_P (TREE_TYPE (@0))
3716             && TREE_CODE (@1) == INTEGER_CST
3717             && tree_int_cst_sign_bit (@1) == 0))
3718     (convert @1)))
3719    (simplify
3720     (pointer_diff (pointer_plus @@0 @1) @0)
3721     /* The second argument of pointer_plus must be interpreted as signed, and
3722        thus sign-extended if necessary.  */
3723     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3724      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3725         second arg is unsigned even when we need to consider it as signed,
3726         we don't want to diagnose overflow here.  */
3727      (convert (view_convert:stype @1))))
3729   /* (T)P - (T)(P + A) -> -(T) A */
3730   (simplify
3731    (minus (convert? @0)
3732     (convert (plus:c @@0 @1)))
3733    (if (INTEGRAL_TYPE_P (type)
3734         && TYPE_OVERFLOW_UNDEFINED (type)
3735         /* For integer literals, using an intermediate unsigned type to avoid
3736            an overflow at run time is counter-productive because it introduces
3737            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3738            the result, which may be problematic in GENERIC for some front-ends:
3739              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3740            so we use the direct path for them.  */
3741         && TREE_CODE (@1) != INTEGER_CST
3742         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3743     (with { tree utype = unsigned_type_for (type); }
3744      (convert (negate (convert:utype @1))))
3745     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3746          /* For integer types, if A has a smaller type
3747             than T the result depends on the possible
3748             overflow in P + A.
3749             E.g. T=size_t, A=(unsigned)429497295, P>0.
3750             However, if an overflow in P + A would cause
3751             undefined behavior, we can assume that there
3752             is no overflow.  */
3753          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3754              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3755      (negate (convert @1)))))
3756   (simplify
3757    (minus (convert @0)
3758     (convert (pointer_plus @@0 @1)))
3759    (if (INTEGRAL_TYPE_P (type)
3760         && TYPE_OVERFLOW_UNDEFINED (type)
3761         /* See above the rationale for this condition.  */
3762         && TREE_CODE (@1) != INTEGER_CST
3763         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3764     (with { tree utype = unsigned_type_for (type); }
3765      (convert (negate (convert:utype @1))))
3766     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3767          /* For pointer types, if the conversion of A to the
3768             final type requires a sign- or zero-extension,
3769             then we have to punt - it is not defined which
3770             one is correct.  */
3771          || (POINTER_TYPE_P (TREE_TYPE (@0))
3772              && TREE_CODE (@1) == INTEGER_CST
3773              && tree_int_cst_sign_bit (@1) == 0))
3774      (negate (convert @1)))))
3775    (simplify
3776     (pointer_diff @0 (pointer_plus @@0 @1))
3777     /* The second argument of pointer_plus must be interpreted as signed, and
3778        thus sign-extended if necessary.  */
3779     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3780      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3781         second arg is unsigned even when we need to consider it as signed,
3782         we don't want to diagnose overflow here.  */
3783      (negate (convert (view_convert:stype @1)))))
3785   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3786   (simplify
3787    (minus (convert (plus:c @@0 @1))
3788     (convert (plus:c @0 @2)))
3789    (if (INTEGRAL_TYPE_P (type)
3790         && TYPE_OVERFLOW_UNDEFINED (type)
3791         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3792         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3793     (with { tree utype = unsigned_type_for (type); }
3794      (convert (minus (convert:utype @1) (convert:utype @2))))
3795     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3796           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3797          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3798              /* For integer types, if A has a smaller type
3799                 than T the result depends on the possible
3800                 overflow in P + A.
3801                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3802                 However, if an overflow in P + A would cause
3803                 undefined behavior, we can assume that there
3804                 is no overflow.  */
3805              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3806                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3807                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3808                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3809      (minus (convert @1) (convert @2)))))
3810   (simplify
3811    (minus (convert (pointer_plus @@0 @1))
3812     (convert (pointer_plus @0 @2)))
3813    (if (INTEGRAL_TYPE_P (type)
3814         && TYPE_OVERFLOW_UNDEFINED (type)
3815         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3816     (with { tree utype = unsigned_type_for (type); }
3817      (convert (minus (convert:utype @1) (convert:utype @2))))
3818     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3819          /* For pointer types, if the conversion of A to the
3820             final type requires a sign- or zero-extension,
3821             then we have to punt - it is not defined which
3822             one is correct.  */
3823          || (POINTER_TYPE_P (TREE_TYPE (@0))
3824              && TREE_CODE (@1) == INTEGER_CST
3825              && tree_int_cst_sign_bit (@1) == 0
3826              && TREE_CODE (@2) == INTEGER_CST
3827              && tree_int_cst_sign_bit (@2) == 0))
3828      (minus (convert @1) (convert @2)))))
3829    (simplify
3830     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3831      (pointer_diff @0 @1))
3832    (simplify
3833     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3834     /* The second argument of pointer_plus must be interpreted as signed, and
3835        thus sign-extended if necessary.  */
3836     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3837      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3838         second arg is unsigned even when we need to consider it as signed,
3839         we don't want to diagnose overflow here.  */
3840      (minus (convert (view_convert:stype @1))
3841             (convert (view_convert:stype @2)))))))
3843 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3844     Modeled after fold_plusminus_mult_expr.  */
3845 (if (!TYPE_SATURATING (type)
3846      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3847  (for plusminus (plus minus)
3848   (simplify
3849    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3850    (if (!ANY_INTEGRAL_TYPE_P (type)
3851         || TYPE_OVERFLOW_WRAPS (type)
3852         || (INTEGRAL_TYPE_P (type)
3853             && tree_expr_nonzero_p (@0)
3854             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3855     (if (single_use (@3) || single_use (@4))
3856      /* If @1 +- @2 is constant require a hard single-use on either
3857         original operand (but not on both).  */
3858      (mult (plusminus @1 @2) @0)
3859      (mult! (plusminus @1 @2) @0)
3860   )))
3861   /* We cannot generate constant 1 for fract.  */
3862   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3863    (simplify
3864     (plusminus @0 (mult:c@3 @0 @2))
3865     (if ((!ANY_INTEGRAL_TYPE_P (type)
3866           || TYPE_OVERFLOW_WRAPS (type)
3867           /* For @0 + @0*@2 this transformation would introduce UB
3868              (where there was none before) for @0 in [-1,0] and @2 max.
3869              For @0 - @0*@2 this transformation would introduce UB
3870              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3871           || (INTEGRAL_TYPE_P (type)
3872               && ((tree_expr_nonzero_p (@0)
3873                    && expr_not_equal_to (@0,
3874                                 wi::minus_one (TYPE_PRECISION (type))))
3875                   || (plusminus == PLUS_EXPR
3876                       ? expr_not_equal_to (@2,
3877                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3878                       /* Let's ignore the @0 -1 and @2 min case.  */
3879                       : (expr_not_equal_to (@2,
3880                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3881                          && expr_not_equal_to (@2,
3882                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3883                                 + 1))))))
3884          && single_use (@3))
3885      (mult (plusminus { build_one_cst (type); } @2) @0)))
3886    (simplify
3887     (plusminus (mult:c@3 @0 @2) @0)
3888     (if ((!ANY_INTEGRAL_TYPE_P (type)
3889           || TYPE_OVERFLOW_WRAPS (type)
3890           /* For @0*@2 + @0 this transformation would introduce UB
3891              (where there was none before) for @0 in [-1,0] and @2 max.
3892              For @0*@2 - @0 this transformation would introduce UB
3893              for @0 0 and @2 min.  */
3894           || (INTEGRAL_TYPE_P (type)
3895               && ((tree_expr_nonzero_p (@0)
3896                    && (plusminus == MINUS_EXPR
3897                        || expr_not_equal_to (@0,
3898                                 wi::minus_one (TYPE_PRECISION (type)))))
3899                   || expr_not_equal_to (@2,
3900                         (plusminus == PLUS_EXPR
3901                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3902                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3903          && single_use (@3))
3904      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3906 #if GIMPLE
3907 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3908    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3909 (simplify
3910  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3911   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3912        && tree_fits_uhwi_p (@1)
3913        && tree_to_uhwi (@1) < element_precision (type)
3914        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3915            || optab_handler (smul_optab,
3916                              TYPE_MODE (type)) != CODE_FOR_nothing))
3917    (with { tree t = type;
3918            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3919            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3920                                              element_precision (type));
3921            w += 1;
3922            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3923                                         : t, w);
3924            cst = build_uniform_cst (t, cst); }
3925     (convert (mult (convert:t @0) { cst; })))))
3926 (simplify
3927  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3928   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3929        && tree_fits_uhwi_p (@1)
3930        && tree_to_uhwi (@1) < element_precision (type)
3931        && tree_fits_uhwi_p (@2)
3932        && tree_to_uhwi (@2) < element_precision (type)
3933        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3934            || optab_handler (smul_optab,
3935                              TYPE_MODE (type)) != CODE_FOR_nothing))
3936    (with { tree t = type;
3937            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3938            unsigned int prec = element_precision (type);
3939            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3940            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3941            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3942                                         : t, w);
3943            cst = build_uniform_cst (t, cst); }
3944     (convert (mult (convert:t @0) { cst; })))))
3945 #endif
3947 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3948    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3949    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3950 (for op (bit_ior bit_xor)
3951  (simplify
3952   (op (mult:s@0 @1 INTEGER_CST@2)
3953       (mult:s@3 @1 INTEGER_CST@4))
3954   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3955        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3956    (mult @1
3957          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3958  (simplify
3959   (op:c (mult:s@0 @1 INTEGER_CST@2)
3960         (lshift:s@3 @1 INTEGER_CST@4))
3961   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3962        && tree_int_cst_sgn (@4) > 0
3963        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3964    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3965            wide_int c = wi::add (wi::to_wide (@2),
3966                                  wi::lshift (wone, wi::to_wide (@4))); }
3967     (mult @1 { wide_int_to_tree (type, c); }))))
3968  (simplify
3969   (op:c (mult:s@0 @1 INTEGER_CST@2)
3970         @1)
3971   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3972        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3973    (mult @1
3974          { wide_int_to_tree (type,
3975                              wi::add (wi::to_wide (@2), 1)); })))
3976  (simplify
3977   (op (lshift:s@0 @1 INTEGER_CST@2)
3978       (lshift:s@3 @1 INTEGER_CST@4))
3979   (if (INTEGRAL_TYPE_P (type)
3980        && tree_int_cst_sgn (@2) > 0
3981        && tree_int_cst_sgn (@4) > 0
3982        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3983    (with { tree t = type;
3984            if (!TYPE_OVERFLOW_WRAPS (t))
3985              t = unsigned_type_for (t);
3986            wide_int wone = wi::one (TYPE_PRECISION (t));
3987            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3988                                  wi::lshift (wone, wi::to_wide (@4))); }
3989     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3990  (simplify
3991   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3992         @1)
3993   (if (INTEGRAL_TYPE_P (type)
3994        && tree_int_cst_sgn (@2) > 0
3995        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3996    (with { tree t = type;
3997            if (!TYPE_OVERFLOW_WRAPS (t))
3998              t = unsigned_type_for (t);
3999            wide_int wone = wi::one (TYPE_PRECISION (t));
4000            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
4001     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
4003 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
4005 (for minmax (min max)
4006  (simplify
4007   (minmax @0 @0)
4008   @0)
4009 /* max(max(x,y),x) -> max(x,y)  */
4010  (simplify
4011   (minmax:c (minmax:c@2 @0 @1) @0)
4012   @2))
4013 /* For fmin() and fmax(), skip folding when both are sNaN.  */
4014 (for minmax (FMIN_ALL FMAX_ALL)
4015  (simplify
4016   (minmax @0 @0)
4017   (if (!tree_expr_maybe_signaling_nan_p (@0))
4018     @0)))
4019 /* min(max(x,y),y) -> y.  */
4020 (simplify
4021  (min:c (max:c @0 @1) @1)
4022  @1)
4023 /* max(min(x,y),y) -> y.  */
4024 (simplify
4025  (max:c (min:c @0 @1) @1)
4026  @1)
4027 /* max(a,-a) -> abs(a).  */
4028 (simplify
4029  (max:c @0 (negate @0))
4030  (if (TREE_CODE (type) != COMPLEX_TYPE
4031       && (! ANY_INTEGRAL_TYPE_P (type)
4032           || TYPE_OVERFLOW_UNDEFINED (type)))
4033   (abs @0)))
4034 /* min(a,-a) -> -abs(a).  */
4035 (simplify
4036  (min:c @0 (negate @0))
4037  (if (TREE_CODE (type) != COMPLEX_TYPE
4038       && (! ANY_INTEGRAL_TYPE_P (type)
4039           || TYPE_OVERFLOW_UNDEFINED (type)))
4040   (negate (abs @0))))
4041 (simplify
4042  (min @0 @1)
4043  (switch
4044   (if (INTEGRAL_TYPE_P (type)
4045        && TYPE_MIN_VALUE (type)
4046        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4047    @1)
4048   (if (INTEGRAL_TYPE_P (type)
4049        && TYPE_MAX_VALUE (type)
4050        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4051    @0)))
4052 (simplify
4053  (max @0 @1)
4054  (switch
4055   (if (INTEGRAL_TYPE_P (type)
4056        && TYPE_MAX_VALUE (type)
4057        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4058    @1)
4059   (if (INTEGRAL_TYPE_P (type)
4060        && TYPE_MIN_VALUE (type)
4061        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4062    @0)))
4064 /* max (a, a + CST) -> a + CST where CST is positive.  */
4065 /* max (a, a + CST) -> a where CST is negative.  */
4066 (simplify
4067  (max:c @0 (plus@2 @0 INTEGER_CST@1))
4068   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4069    (if (tree_int_cst_sgn (@1) > 0)
4070     @2
4071     @0)))
4073 /* min (a, a + CST) -> a where CST is positive.  */
4074 /* min (a, a + CST) -> a + CST where CST is negative. */
4075 (simplify
4076  (min:c @0 (plus@2 @0 INTEGER_CST@1))
4077   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4078    (if (tree_int_cst_sgn (@1) > 0)
4079     @0
4080     @2)))
4082 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4083    the addresses are known to be less, equal or greater.  */
4084 (for minmax (min max)
4085      cmp (lt gt)
4086  (simplify
4087   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4088   (with
4089    {
4090      poly_int64 off0, off1;
4091      tree base0, base1;
4092      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4093                                   off0, off1, GENERIC);
4094    }
4095    (if (equal == 1)
4096     (if (minmax == MIN_EXPR)
4097      (if (known_le (off0, off1))
4098       @2
4099       (if (known_gt (off0, off1))
4100        @3))
4101      (if (known_ge (off0, off1))
4102       @2
4103       (if (known_lt (off0, off1))
4104        @3)))))))
4106 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4107    and the outer convert demotes the expression back to x's type.  */
4108 (for minmax (min max)
4109  (simplify
4110   (convert (minmax@0 (convert @1) INTEGER_CST@2))
4111   (if (INTEGRAL_TYPE_P (type)
4112        && types_match (@1, type) && int_fits_type_p (@2, type)
4113        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4114        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4115    (minmax @1 (convert @2)))))
4117 (for minmax (FMIN_ALL FMAX_ALL)
4118  /* If either argument is NaN and other one is not sNaN, return the other
4119     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
4120  (simplify
4121   (minmax:c @0 REAL_CST@1)
4122    (if (real_isnan (TREE_REAL_CST_PTR (@1))
4123        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4124        && !tree_expr_maybe_signaling_nan_p (@0))
4125    @0)))
4126 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
4127    functions to return the numeric arg if the other one is NaN.
4128    MIN and MAX don't honor that, so only transform if -ffinite-math-only
4129    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
4130    worry about it either.  */
4131 (if (flag_finite_math_only)
4132  (simplify
4133   (FMIN_ALL @0 @1)
4134   (min @0 @1))
4135  (simplify
4136   (FMAX_ALL @0 @1)
4137   (max @0 @1)))
4138 /* min (-A, -B) -> -max (A, B)  */
4139 (for minmax (min max FMIN_ALL FMAX_ALL)
4140      maxmin (max min FMAX_ALL FMIN_ALL)
4141  (simplify
4142   (minmax (negate:s@2 @0) (negate:s@3 @1))
4143   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4144        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4145            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4146    (negate (maxmin @0 @1)))))
4147 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4148    MAX (~X, ~Y) -> ~MIN (X, Y)  */
4149 (for minmax (min max)
4150  maxmin (max min)
4151  (simplify
4152   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4153   (bit_not (maxmin @0 @1)))
4154 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4155 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4156  (simplify
4157   (bit_not (minmax:cs (bit_not @0) @1))
4158   (maxmin @0 (bit_not @1))))
4160 /* MIN (X, Y) == X -> X <= Y  */
4161 /* MIN (X, Y) < X -> X > Y  */
4162 /* MIN (X, Y) >= X -> X <= Y  */
4163 (for minmax (min min min min max max max max)
4164      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
4165      out    (le  gt  gt  le  ge  lt  lt  ge )
4166  (simplify
4167   (cmp:c (minmax:c @0 @1) @0)
4168   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4169    (out @0 @1))))
4170 /* MIN (X, 5) == 0 -> X == 0
4171    MIN (X, 5) == 7 -> false  */
4172 (for cmp (eq ne)
4173  (simplify
4174   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4175   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4176                  TYPE_SIGN (TREE_TYPE (@0))))
4177    { constant_boolean_node (cmp == NE_EXPR, type); }
4178    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4179                   TYPE_SIGN (TREE_TYPE (@0))))
4180     (cmp @0 @2)))))
4181 (for cmp (eq ne)
4182  (simplify
4183   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4184   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4185                  TYPE_SIGN (TREE_TYPE (@0))))
4186    { constant_boolean_node (cmp == NE_EXPR, type); }
4187    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4188                   TYPE_SIGN (TREE_TYPE (@0))))
4189     (cmp @0 @2)))))
4191 /* X <= MAX(X, Y) -> true
4192    X > MAX(X, Y) -> false 
4193    X >= MIN(X, Y) -> true
4194    X < MIN(X, Y) -> false */
4195 (for minmax (min     min     max     max     )
4196      cmp    (ge      lt      le      gt      )
4197  (simplify
4198   (cmp:c @0 (minmax:c @0 @1))
4199   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4201 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4202 (for minmax (min     min     max     max     min     min     max     max    )
4203      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4204      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4205  (simplify
4206   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4207   (comb (cmp @0 @2) (cmp @1 @2))))
4209 /* Undo fancy ways of writing max/min or other ?: expressions, like
4210    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4211    People normally use ?: and that is what we actually try to optimize.  */
4212 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4213 (simplify
4214  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4215  (if (INTEGRAL_TYPE_P (type)
4216       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4217   (cond (convert:boolean_type_node @2) @1 @0)))
4218 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4219 (simplify
4220  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4221  (if (INTEGRAL_TYPE_P (type)
4222       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4223   (cond (convert:boolean_type_node @2) @1 @0)))
4224 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4225 (simplify
4226  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4227  (if (INTEGRAL_TYPE_P (type)
4228       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4229   (cond (convert:boolean_type_node @2) @1 @0)))
4231 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4232 (simplify
4233  (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4234  (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
4235   (max @2 @1)))
4237 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4238 (for op (bit_xor bit_ior plus)
4239  (simplify
4240   (cond (eq zero_one_valued_p@0
4241             integer_zerop)
4242         @1
4243         (op:c @2 @1))
4244   (if (INTEGRAL_TYPE_P (type)
4245        && TYPE_PRECISION (type) > 1
4246        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4247        (op (mult (convert:type @0) @2) @1))))
4249 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4250 (for op (bit_xor bit_ior plus)
4251  (simplify
4252   (cond (ne zero_one_valued_p@0
4253             integer_zerop)
4254        (op:c @2 @1)
4255         @1)
4256   (if (INTEGRAL_TYPE_P (type)
4257        && TYPE_PRECISION (type) > 1
4258        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4259        (op (mult (convert:type @0) @2) @1))))
4261 /* ?: Value replacement. */
4262 /* a == 0 ? b : b + a  -> b + a */
4263 (for op (plus bit_ior bit_xor)
4264  (simplify
4265   (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4266    @2))
4267 /* a == 0 ? b : b - a  -> b - a */
4268 /* a == 0 ? b : b ptr+ a  -> b ptr+ a */
4269 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4270 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4271  (simplify
4272   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4273    @2))
4275 /* a == 1 ? b : b / a  -> b / a */
4276 (for op (trunc_div ceil_div floor_div round_div exact_div)
4277  (simplify
4278   (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4279    @2))
4281 /* a == 1 ? b : a * b -> a * b */
4282 (for op (mult)
4283  (simplify
4284   (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4285    @2))
4287 /* a == -1 ? b : a & b -> a & b */
4288 (for op (bit_and)
4289  (simplify
4290   (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4291    @2))
4293 /* Simplifications of shift and rotates.  */
4295 (for rotate (lrotate rrotate)
4296  (simplify
4297   (rotate integer_all_onesp@0 @1)
4298   @0))
4300 /* Optimize -1 >> x for arithmetic right shifts.  */
4301 (simplify
4302  (rshift integer_all_onesp@0 @1)
4303  (if (!TYPE_UNSIGNED (type))
4304   @0))
4306 /* Optimize (x >> c) << c into x & (-1<<c).  */
4307 (simplify
4308  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4309  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4310   /* It doesn't matter if the right shift is arithmetic or logical.  */
4311   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4313 (simplify
4314  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4315  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4316       /* Allow intermediate conversion to integral type with whatever sign, as
4317          long as the low TYPE_PRECISION (type)
4318          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4319       && INTEGRAL_TYPE_P (type)
4320       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4321       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4322       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4323       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4324           || wi::geu_p (wi::to_wide (@1),
4325                         TYPE_PRECISION (type)
4326                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4327   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4329 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4330    unsigned x OR truncate into the precision(type) - c lowest bits
4331    of signed x (if they have mode precision or a precision of 1).  */
4332 (simplify
4333  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4334  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4335   (if (TYPE_UNSIGNED (type))
4336    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4337    (if (INTEGRAL_TYPE_P (type))
4338     (with {
4339       int width = element_precision (type) - tree_to_uhwi (@1);
4340       tree stype = NULL_TREE;
4341       if (width <= MAX_FIXED_MODE_SIZE)
4342         stype = build_nonstandard_integer_type (width, 0);
4343      }
4344      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4345       (convert (convert:stype @0))))))))
4347 /* Optimize x >> x into 0 */
4348 (simplify
4349  (rshift @0 @0)
4350   { build_zero_cst (type); })
4352 (for shiftrotate (lrotate rrotate lshift rshift)
4353  (simplify
4354   (shiftrotate @0 integer_zerop)
4355   (non_lvalue @0))
4356  (simplify
4357   (shiftrotate integer_zerop@0 @1)
4358   @0)
4359  /* Prefer vector1 << scalar to vector1 << vector2
4360     if vector2 is uniform.  */
4361  (for vec (VECTOR_CST CONSTRUCTOR)
4362   (simplify
4363    (shiftrotate @0 vec@1)
4364    (with { tree tem = uniform_vector_p (@1); }
4365     (if (tem)
4366      (shiftrotate @0 { tem; }))))))
4368 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4369    Y is 0.  Similarly for X >> Y.  */
4370 #if GIMPLE
4371 (for shift (lshift rshift)
4372  (simplify
4373   (shift @0 SSA_NAME@1)
4374    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4375     (with {
4376       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4377       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4378      }
4379      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4380       @0)))))
4381 #endif
4383 /* Rewrite an LROTATE_EXPR by a constant into an
4384    RROTATE_EXPR by a new constant.  */
4385 (simplify
4386  (lrotate @0 INTEGER_CST@1)
4387  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4388                             build_int_cst (TREE_TYPE (@1),
4389                                            element_precision (type)), @1); }))
4391 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4392 (for op (lrotate rrotate rshift lshift)
4393  (simplify
4394   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4395   (with { unsigned int prec = element_precision (type); }
4396    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4397         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4398         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4399         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4400     (with { unsigned int low = (tree_to_uhwi (@1)
4401                                 + tree_to_uhwi (@2)); }
4402      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4403         being well defined.  */
4404      (if (low >= prec)
4405       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4406        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4407        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4408         { build_zero_cst (type); }
4409         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4410       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4413 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4414 (simplify
4415  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4416   (if ((wi::to_wide (@1) & 1) != 0)
4417    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4418    { build_zero_cst (type); }))
4420 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4421    either to false if D is smaller (unsigned comparison) than C, or to
4422    x == log2 (D) - log2 (C).  Similarly for right shifts.
4423    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4424 (for cmp (ne eq)
4425      icmp (eq ne)
4426  (simplify
4427   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4428    (with { int c1 = wi::clz (wi::to_wide (@1));
4429            int c2 = wi::clz (wi::to_wide (@2)); }
4430     (if (c1 < c2)
4431      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4432      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4433  (simplify
4434   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4435    (if (tree_int_cst_sgn (@1) > 0)
4436     (with { int c1 = wi::clz (wi::to_wide (@1));
4437             int c2 = wi::clz (wi::to_wide (@2)); }
4438      (if (c1 > c2)
4439       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4440       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4441  /* `(1 >> X) != 0` -> `X == 0` */
4442  /* `(1 >> X) == 0` -> `X != 0` */
4443  (simplify
4444   (cmp (rshift integer_onep@1 @0) integer_zerop)
4445    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4446     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4448 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4449    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4450    if CST2 != 0.  */
4451 (for cmp (ne eq)
4452  (simplify
4453   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4454   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4455    (if (cand < 0
4456         || (!integer_zerop (@2)
4457             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4458     { constant_boolean_node (cmp == NE_EXPR, type); }
4459     (if (!integer_zerop (@2)
4460          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4461      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4463 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4464         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4465 (for cmp (ne eq)
4466  (simplify
4467   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4468   (if (tree_fits_shwi_p (@1)
4469        && tree_to_shwi (@1) > 0
4470        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4471     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4472       { constant_boolean_node (cmp == NE_EXPR, type); }
4473       (with { wide_int c1 = wi::to_wide (@1);
4474               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4475               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4476         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4477              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4478  (simplify
4479   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4480   (if (tree_fits_shwi_p (@1)
4481        && tree_to_shwi (@1) > 0
4482        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4483     (with { tree t0 = TREE_TYPE (@0);
4484             unsigned int prec = TYPE_PRECISION (t0);
4485             wide_int c1 = wi::to_wide (@1);
4486             wide_int c2 = wi::to_wide (@2);
4487             wide_int c3 = wi::to_wide (@3);
4488             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4489       (if ((c2 & c3) != c3)
4490         { constant_boolean_node (cmp == NE_EXPR, type); }
4491         (if (TYPE_UNSIGNED (t0))
4492           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4493             { constant_boolean_node (cmp == NE_EXPR, type); }
4494             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4495                  { wide_int_to_tree (t0, c3 << c1); }))
4496           (with { wide_int smask = wi::arshift (sb, c1); }
4497             (switch
4498               (if ((c2 & smask) == 0)
4499                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4500                      { wide_int_to_tree (t0, c3 << c1); }))
4501               (if ((c3 & smask) == 0)
4502                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4503                      { wide_int_to_tree (t0, c3 << c1); }))
4504               (if ((c2 & smask) != (c3 & smask))
4505                 { constant_boolean_node (cmp == NE_EXPR, type); })
4506               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4507                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4509 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4510         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4511    if the new mask might be further optimized.  */
4512 (for shift (lshift rshift)
4513  (simplify
4514   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4515            INTEGER_CST@2)
4516    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4517         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4518         && tree_fits_uhwi_p (@1)
4519         && tree_to_uhwi (@1) > 0
4520         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4521     (with
4522      {
4523        unsigned int shiftc = tree_to_uhwi (@1);
4524        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4525        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4526        tree shift_type = TREE_TYPE (@3);
4527        unsigned int prec;
4529        if (shift == LSHIFT_EXPR)
4530          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4531        else if (shift == RSHIFT_EXPR
4532                 && type_has_mode_precision_p (shift_type))
4533          {
4534            prec = TYPE_PRECISION (TREE_TYPE (@3));
4535            tree arg00 = @0;
4536            /* See if more bits can be proven as zero because of
4537               zero extension.  */
4538            if (@3 != @0
4539                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4540              {
4541                tree inner_type = TREE_TYPE (@0);
4542                if (type_has_mode_precision_p (inner_type)
4543                    && TYPE_PRECISION (inner_type) < prec)
4544                  {
4545                    prec = TYPE_PRECISION (inner_type);
4546                    /* See if we can shorten the right shift.  */
4547                    if (shiftc < prec)
4548                      shift_type = inner_type;
4549                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4550                       it into (X, 0) later on by making sure zerobits
4551                       is all ones.  */
4552                  }
4553              }
4554            zerobits = HOST_WIDE_INT_M1U;
4555            if (shiftc < prec)
4556              {
4557                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4558                zerobits <<= prec - shiftc;
4559              }
4560            /* For arithmetic shift if sign bit could be set, zerobits
4561               can contain actually sign bits, so no transformation is
4562               possible, unless MASK masks them all away.  In that
4563               case the shift needs to be converted into logical shift.  */
4564            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4565                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4566              {
4567                if ((mask & zerobits) == 0)
4568                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4569                else
4570                  zerobits = 0;
4571              }
4572          }
4573      }
4574      /* ((X << 16) & 0xff00) is (X, 0).  */
4575      (if ((mask & zerobits) == mask)
4576       { build_int_cst (type, 0); }
4577       (with { newmask = mask | zerobits; }
4578        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4579         (with
4580          {
4581            /* Only do the transformation if NEWMASK is some integer
4582               mode's mask.  */
4583            for (prec = BITS_PER_UNIT;
4584                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4585              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4586                break;
4587          }
4588          (if (prec < HOST_BITS_PER_WIDE_INT
4589               || newmask == HOST_WIDE_INT_M1U)
4590           (with
4591            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4592            (if (!tree_int_cst_equal (newmaskt, @2))
4593             (if (shift_type != TREE_TYPE (@3))
4594              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4595              (bit_and @4 { newmaskt; })))))))))))))
4597 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4598 (for cmp (ne eq)
4599        icmp (eq ne)
4600  (simplify
4601   (cmp
4602    (bit_and
4603     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4604   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4605    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4606                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4608 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4609    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4610 (for shift (lshift rshift)
4611  (for bit_op (bit_and bit_xor bit_ior)
4612   (simplify
4613    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4614    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4615     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4616      (if (mask)
4617       (bit_op (shift (convert @0) @1) { mask; })))))))
4619 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4620 (simplify
4621  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4622   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4623        && (element_precision (TREE_TYPE (@0))
4624            <= element_precision (TREE_TYPE (@1))
4625            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4626    (with
4627     { tree shift_type = TREE_TYPE (@0); }
4628      (convert (rshift (convert:shift_type @1) @2)))))
4630 /* ~(~X >>r Y) -> X >>r Y
4631    ~(~X <<r Y) -> X <<r Y */
4632 (for rotate (lrotate rrotate)
4633  (simplify
4634   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4635    (if ((element_precision (TREE_TYPE (@0))
4636          <= element_precision (TREE_TYPE (@1))
4637          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4638         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4639             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4640     (with
4641      { tree rotate_type = TREE_TYPE (@0); }
4642       (convert (rotate (convert:rotate_type @1) @2))))))
4644 (for cmp (eq ne)
4645  (for rotate (lrotate rrotate)
4646       invrot (rrotate lrotate)
4647   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4648   (simplify
4649    (cmp (rotate @1 @0) (rotate @2 @0))
4650    (cmp @1 @2))
4651   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4652   (simplify
4653    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4654    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4655   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4656   (simplify
4657    (cmp (rotate @0 @1) INTEGER_CST@2)
4658     (if (integer_zerop (@2) || integer_all_onesp (@2))
4659      (cmp @0 @2)))))
4661 /* Narrow a lshift by constant.  */
4662 (simplify
4663  (convert (lshift:s@0 @1 INTEGER_CST@2))
4664  (if (INTEGRAL_TYPE_P (type)
4665       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4666       && !integer_zerop (@2)
4667       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4668   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4669        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4670    (lshift (convert @1) @2)
4671    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4672     { build_zero_cst (type); }))))
4674 /* Simplifications of conversions.  */
4676 /* Basic strip-useless-type-conversions / strip_nops.  */
4677 (for cvt (convert view_convert float fix_trunc)
4678  (simplify
4679   (cvt @0)
4680   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4681        || (GENERIC && type == TREE_TYPE (@0)))
4682    @0)))
4684 /* Contract view-conversions.  */
4685 (simplify
4686   (view_convert (view_convert @0))
4687   (view_convert @0))
4689 /* For integral conversions with the same precision or pointer
4690    conversions use a NOP_EXPR instead.  */
4691 (simplify
4692   (view_convert @0)
4693   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4694        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4695        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4696    (convert @0)))
4698 /* Strip inner integral conversions that do not change precision or size, or
4699    zero-extend while keeping the same size (for bool-to-char).  */
4700 (simplify
4701   (view_convert (convert@0 @1))
4702   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4703        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4704        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4705        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4706            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4707                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4708    (view_convert @1)))
4710 /* Simplify a view-converted empty or single-element constructor.  */
4711 (simplify
4712   (view_convert CONSTRUCTOR@0)
4713   (with
4714    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4715                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4716    (switch
4717     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4718      { build_zero_cst (type); })
4719     (if (CONSTRUCTOR_NELTS (ctor) == 1
4720          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4721          && operand_equal_p (TYPE_SIZE (type),
4722                              TYPE_SIZE (TREE_TYPE
4723                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4724      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4726 /* Re-association barriers around constants and other re-association
4727    barriers can be removed.  */
4728 (simplify
4729  (paren CONSTANT_CLASS_P@0)
4730  @0)
4731 (simplify
4732  (paren (paren@1 @0))
4733  @1)
4735 /* Handle cases of two conversions in a row.  */
4736 (for ocvt (convert float fix_trunc)
4737  (for icvt (convert float)
4738   (simplify
4739    (ocvt (icvt@1 @0))
4740    (with
4741     {
4742       tree inside_type = TREE_TYPE (@0);
4743       tree inter_type = TREE_TYPE (@1);
4744       int inside_int = INTEGRAL_TYPE_P (inside_type);
4745       int inside_ptr = POINTER_TYPE_P (inside_type);
4746       int inside_float = FLOAT_TYPE_P (inside_type);
4747       int inside_vec = VECTOR_TYPE_P (inside_type);
4748       unsigned int inside_prec = element_precision (inside_type);
4749       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4750       int inter_int = INTEGRAL_TYPE_P (inter_type);
4751       int inter_ptr = POINTER_TYPE_P (inter_type);
4752       int inter_float = FLOAT_TYPE_P (inter_type);
4753       int inter_vec = VECTOR_TYPE_P (inter_type);
4754       unsigned int inter_prec = element_precision (inter_type);
4755       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4756       int final_int = INTEGRAL_TYPE_P (type);
4757       int final_ptr = POINTER_TYPE_P (type);
4758       int final_float = FLOAT_TYPE_P (type);
4759       int final_vec = VECTOR_TYPE_P (type);
4760       unsigned int final_prec = element_precision (type);
4761       int final_unsignedp = TYPE_UNSIGNED (type);
4762     }
4763    (switch
4764     /* In addition to the cases of two conversions in a row
4765        handled below, if we are converting something to its own
4766        type via an object of identical or wider precision, neither
4767        conversion is needed.  */
4768     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4769           || (GENERIC
4770               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4771          && (((inter_int || inter_ptr) && final_int)
4772              || (inter_float && final_float))
4773          && inter_prec >= final_prec)
4774      (ocvt @0))
4776     /* Likewise, if the intermediate and initial types are either both
4777        float or both integer, we don't need the middle conversion if the
4778        former is wider than the latter and doesn't change the signedness
4779        (for integers).  Avoid this if the final type is a pointer since
4780        then we sometimes need the middle conversion.  */
4781     (if (((inter_int && inside_int) || (inter_float && inside_float))
4782          && (final_int || final_float)
4783          && inter_prec >= inside_prec
4784          && (inter_float || inter_unsignedp == inside_unsignedp))
4785      (ocvt @0))
4787     /* If we have a sign-extension of a zero-extended value, we can
4788        replace that by a single zero-extension.  Likewise if the
4789        final conversion does not change precision we can drop the
4790        intermediate conversion.  Similarly truncation of a sign-extension
4791        can be replaced by a single sign-extension.  */
4792     (if (inside_int && inter_int && final_int
4793          && ((inside_prec < inter_prec && inter_prec < final_prec
4794               && inside_unsignedp && !inter_unsignedp)
4795              || final_prec == inter_prec
4796              || (inside_prec < inter_prec && inter_prec > final_prec
4797                  && !inside_unsignedp && inter_unsignedp)))
4798      (ocvt @0))
4800     /* Two conversions in a row are not needed unless:
4801         - some conversion is floating-point (overstrict for now), or
4802         - some conversion is a vector (overstrict for now), or
4803         - the intermediate type is narrower than both initial and
4804           final, or
4805         - the intermediate type and innermost type differ in signedness,
4806           and the outermost type is wider than the intermediate, or
4807         - the initial type is a pointer type and the precisions of the
4808           intermediate and final types differ, or
4809         - the final type is a pointer type and the precisions of the
4810           initial and intermediate types differ.  */
4811     (if (! inside_float && ! inter_float && ! final_float
4812          && ! inside_vec && ! inter_vec && ! final_vec
4813          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4814          && ! (inside_int && inter_int
4815                && inter_unsignedp != inside_unsignedp
4816                && inter_prec < final_prec)
4817          && ((inter_unsignedp && inter_prec > inside_prec)
4818              == (final_unsignedp && final_prec > inter_prec))
4819          && ! (inside_ptr && inter_prec != final_prec)
4820          && ! (final_ptr && inside_prec != inter_prec))
4821      (ocvt @0))
4823    /* `(outer:M)(inter:N) a:O`
4824       can be converted to `(outer:M) a`
4825       if M <= O && N >= O. No matter what signedness of the casts,
4826       as the final is either a truncation from the original or just
4827       a sign change of the type. */
4828    (if (inside_int && inter_int && final_int
4829         && final_prec <= inside_prec
4830         && inter_prec >= inside_prec)
4831     (convert @0))
4833     /* A truncation to an unsigned type (a zero-extension) should be
4834        canonicalized as bitwise and of a mask.  */
4835     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4836          && final_int && inter_int && inside_int
4837          && final_prec == inside_prec
4838          && final_prec > inter_prec
4839          && inter_unsignedp)
4840      (convert (bit_and @0 { wide_int_to_tree
4841                               (inside_type,
4842                                wi::mask (inter_prec, false,
4843                                          TYPE_PRECISION (inside_type))); })))
4845     /* If we are converting an integer to a floating-point that can
4846        represent it exactly and back to an integer, we can skip the
4847        floating-point conversion.  */
4848     (if (GIMPLE /* PR66211 */
4849          && inside_int && inter_float && final_int &&
4850          (unsigned) significand_size (TYPE_MODE (inter_type))
4851          >= inside_prec - !inside_unsignedp)
4852      (convert @0)))))))
4854 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4855    float_type.  Only do the transformation if we do not need to preserve
4856    trapping behaviour, so require !flag_trapping_math. */
4857 #if GIMPLE
4858 (simplify
4859    (float (fix_trunc @0))
4860    (if (!flag_trapping_math
4861         && !HONOR_SIGNED_ZEROS (type)
4862         && types_match (type, TREE_TYPE (@0))
4863         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4864                                           OPTIMIZE_FOR_BOTH))
4865       (IFN_TRUNC @0)))
4866 #endif
4868 /* If we have a narrowing conversion to an integral type that is fed by a
4869    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4870    masks off bits outside the final type (and nothing else).  */
4871 (simplify
4872   (convert (bit_and @0 INTEGER_CST@1))
4873   (if (INTEGRAL_TYPE_P (type)
4874        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4875        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4876        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4877                                                     TYPE_PRECISION (type)), 0))
4878    (convert @0)))
4881 /* (X /[ex] A) * A -> X.  */
4882 (simplify
4883   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4884   (convert @0))
4886 /* Simplify (A / B) * B + (A % B) -> A.  */
4887 (for div (trunc_div ceil_div floor_div round_div)
4888      mod (trunc_mod ceil_mod floor_mod round_mod)
4889   (simplify
4890    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4891    @0))
4893 /* x / y * y == x -> x % y == 0.  */
4894 (simplify
4895   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4896   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4897     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4899 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4900 (for op (plus minus)
4901  (simplify
4902   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4903   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4904        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4905    (with
4906      {
4907        wi::overflow_type overflow;
4908        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4909                                TYPE_SIGN (type), &overflow);
4910      }
4911      (if (types_match (type, TREE_TYPE (@2))
4912          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4913       (op @0 { wide_int_to_tree (type, mul); })
4914       (with { tree utype = unsigned_type_for (type); }
4915        (convert (op (convert:utype @0)
4916                     (mult (convert:utype @1) (convert:utype @2))))))))))
4918 /* Canonicalization of binary operations.  */
4920 /* Convert X + -C into X - C.  */
4921 (simplify
4922  (plus @0 REAL_CST@1)
4923  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4924   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4925    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4926     (minus @0 { tem; })))))
4928 /* Convert x+x into x*2.  */
4929 (simplify
4930  (plus @0 @0)
4931  (if (SCALAR_FLOAT_TYPE_P (type))
4932   (mult @0 { build_real (type, dconst2); })
4933   (if (INTEGRAL_TYPE_P (type))
4934    (mult @0 { build_int_cst (type, 2); }))))
4936 /* 0 - X  ->  -X.  */
4937 (simplify
4938  (minus integer_zerop @1)
4939  (negate @1))
4940 (simplify
4941  (pointer_diff integer_zerop @1)
4942  (negate (convert @1)))
4944 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4945    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4946    (-ARG1 + ARG0) reduces to -ARG1.  */
4947 (simplify
4948  (minus real_zerop@0 @1)
4949  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4950   (negate @1)))
4952 /* Transform x * -1 into -x.  */
4953 (simplify
4954  (mult @0 integer_minus_onep)
4955  (negate @0))
4957 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4958    signed overflow for CST != 0 && CST != -1.  */
4959 (simplify
4960  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4961  (if (TREE_CODE (@2) != INTEGER_CST
4962       && single_use (@3)
4963       && !integer_zerop (@1) && !integer_minus_onep (@1))
4964   (mult (mult @0 @2) @1)))
4966 /* True if we can easily extract the real and imaginary parts of a complex
4967    number.  */
4968 (match compositional_complex
4969  (convert? (complex @0 @1)))
4971 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4972 (simplify
4973  (complex (realpart @0) (imagpart @0))
4974  @0)
4975 (simplify
4976  (realpart (complex @0 @1))
4977  @0)
4978 (simplify
4979  (imagpart (complex @0 @1))
4980  @1)
4982 /* Sometimes we only care about half of a complex expression.  */
4983 (simplify
4984  (realpart (convert?:s (conj:s @0)))
4985  (convert (realpart @0)))
4986 (simplify
4987  (imagpart (convert?:s (conj:s @0)))
4988  (convert (negate (imagpart @0))))
4989 (for part (realpart imagpart)
4990  (for op (plus minus)
4991   (simplify
4992    (part (convert?:s@2 (op:s @0 @1)))
4993    (convert (op (part @0) (part @1))))))
4994 (simplify
4995  (realpart (convert?:s (CEXPI:s @0)))
4996  (convert (COS @0)))
4997 (simplify
4998  (imagpart (convert?:s (CEXPI:s @0)))
4999  (convert (SIN @0)))
5001 /* conj(conj(x)) -> x  */
5002 (simplify
5003  (conj (convert? (conj @0)))
5004  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
5005   (convert @0)))
5007 /* conj({x,y}) -> {x,-y}  */
5008 (simplify
5009  (conj (convert?:s (complex:s @0 @1)))
5010  (with { tree itype = TREE_TYPE (type); }
5011   (complex (convert:itype @0) (negate (convert:itype @1)))))
5013 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
5014 (for bswap (BSWAP)
5015  (simplify
5016   (bswap (bswap @0))
5017   @0)
5018  (simplify
5019   (bswap (bit_not (bswap @0)))
5020   (bit_not @0))
5021  (for bitop (bit_xor bit_ior bit_and)
5022   (simplify
5023    (bswap (bitop:c (bswap @0) @1))
5024    (bitop @0 (bswap @1))))
5025  (for cmp (eq ne)
5026   (simplify
5027    (cmp (bswap@2 @0) (bswap @1))
5028    (with { tree ctype = TREE_TYPE (@2); }
5029     (cmp (convert:ctype @0) (convert:ctype @1))))
5030   (simplify
5031    (cmp (bswap @0) INTEGER_CST@1)
5032    (with { tree ctype = TREE_TYPE (@1); }
5033     (cmp (convert:ctype @0) (bswap! @1)))))
5034  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
5035  (simplify
5036   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5037            INTEGER_CST@3)
5038    (if (BITS_PER_UNIT == 8
5039         && tree_fits_uhwi_p (@2)
5040         && tree_fits_uhwi_p (@3))
5041     (with
5042      {
5043       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5044       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5045       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5046       unsigned HOST_WIDE_INT lo = bits & 7;
5047       unsigned HOST_WIDE_INT hi = bits - lo;
5048      }
5049      (if (bits < prec
5050           && mask < (256u>>lo)
5051           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5052       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5053        (if (ns == 0)
5054         (bit_and (convert @1) @3)
5055         (with
5056          {
5057           tree utype = unsigned_type_for (TREE_TYPE (@1));
5058           tree nst = build_int_cst (integer_type_node, ns);
5059          }
5060          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5061  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
5062  (simplify
5063   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5064    (if (BITS_PER_UNIT == 8
5065         && CHAR_TYPE_SIZE == 8
5066         && tree_fits_uhwi_p (@1))
5067     (with
5068      {
5069       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5070       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5071       /* If the bswap was extended before the original shift, this
5072          byte (shift) has the sign of the extension, not the sign of
5073          the original shift.  */
5074       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5075      }
5076      /* Special case: logical right shift of sign-extended bswap.
5077         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5078      (if (TYPE_PRECISION (type) > prec
5079           && !TYPE_UNSIGNED (TREE_TYPE (@2))
5080           && TYPE_UNSIGNED (type)
5081           && bits < prec && bits + 8 >= prec)
5082       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5083        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5084       (if (bits + 8 == prec)
5085        (if (TYPE_UNSIGNED (st))
5086         (convert (convert:unsigned_char_type_node @0))
5087         (convert (convert:signed_char_type_node @0)))
5088        (if (bits < prec && bits + 8 > prec)
5089         (with 
5090          {
5091           tree nst = build_int_cst (integer_type_node, bits & 7);
5092           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5093                                        : signed_char_type_node;
5094          }
5095          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5096  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
5097  (simplify
5098   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5099    (if (BITS_PER_UNIT == 8
5100         && tree_fits_uhwi_p (@1)
5101         && tree_to_uhwi (@1) < 256)
5102     (with
5103      {
5104       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5105       tree utype = unsigned_type_for (TREE_TYPE (@0));
5106       tree nst = build_int_cst (integer_type_node, prec - 8);
5107      }
5108      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5111 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
5113 /* Simplify constant conditions.
5114    Only optimize constant conditions when the selected branch
5115    has the same type as the COND_EXPR.  This avoids optimizing
5116    away "c ? x : throw", where the throw has a void type.
5117    Note that we cannot throw away the fold-const.cc variant nor
5118    this one as we depend on doing this transform before possibly
5119    A ? B : B -> B triggers and the fold-const.cc one can optimize
5120    0 ? A : B to B even if A has side-effects.  Something
5121    genmatch cannot handle.  */
5122 (simplify
5123  (cond INTEGER_CST@0 @1 @2)
5124  (if (integer_zerop (@0))
5125   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5126    @2)
5127   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5128    @1)))
5129 (simplify
5130  (vec_cond VECTOR_CST@0 @1 @2)
5131  (if (integer_all_onesp (@0))
5132   @1
5133   (if (integer_zerop (@0))
5134    @2)))
5136 /* Sink unary operations to branches, but only if we do fold both.  */
5137 (for op (negate bit_not abs absu)
5138  (simplify
5139   (op (vec_cond:s @0 @1 @2))
5140   (vec_cond @0 (op! @1) (op! @2))))
5142 /* Sink unary conversions to branches, but only if we do fold both
5143    and the target's truth type is the same as we already have.  */
5144 (simplify
5145  (convert (vec_cond:s @0 @1 @2))
5146  (if (VECTOR_TYPE_P (type)
5147       && types_match (TREE_TYPE (@0), truth_type_for (type)))
5148   (vec_cond @0 (convert! @1) (convert! @2))))
5150 /* Likewise for view_convert of nop_conversions. */
5151 (simplify
5152  (view_convert (vec_cond:s @0 @1 @2))
5153  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5154       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5155                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5156       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5157   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5159 /* Sink binary operation to branches, but only if we can fold it.  */
5160 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5161          lshift rshift rdiv trunc_div ceil_div floor_div round_div
5162          trunc_mod ceil_mod floor_mod round_mod min max)
5163 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
5164  (simplify
5165   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5166   (if (TREE_CODE_CLASS (op) != tcc_comparison
5167        || types_match (type, TREE_TYPE (@1))
5168        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5169        || (optimize_vectors_before_lowering_p ()
5170            /* The following is optimistic on the side of non-support, we are
5171               missing the legacy vcond{,u,eq} cases.  Do this only when
5172               lowering will be able to fixup..  */
5173            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5174                                        TREE_TYPE (@0), ERROR_MARK)))
5175    (vec_cond @0 (op! @1 @3) (op! @2 @4))))
5177 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
5178  (simplify
5179   (op (vec_cond:s @0 @1 @2) @3)
5180   (if (TREE_CODE_CLASS (op) != tcc_comparison
5181        || types_match (type, TREE_TYPE (@1))
5182        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5183        || (optimize_vectors_before_lowering_p ()
5184            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5185                                        TREE_TYPE (@0), ERROR_MARK)))
5186    (vec_cond @0 (op! @1 @3) (op! @2 @3))))
5187  (simplify
5188   (op @3 (vec_cond:s @0 @1 @2))
5189   (if (TREE_CODE_CLASS (op) != tcc_comparison
5190        || types_match (type, TREE_TYPE (@1))
5191        || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5192        || (optimize_vectors_before_lowering_p ()
5193            && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5194                                        TREE_TYPE (@0), ERROR_MARK)))
5195    (vec_cond @0 (op! @3 @1) (op! @3 @2)))))
5197 #if GIMPLE
5198 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5199  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5200            INTEGER_CST@1)
5201  (with {
5202          int ibit = tree_log2 (@0);
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 (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5211           INTEGER_CST@1)
5212  (with {
5213          int ibit = tree_log2 (@0);
5214          int ibit2 = tree_log2 (@1);
5215        }
5216   (if (ibit == ibit2
5217       && ibit >= 0
5218       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5220 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5221  (bit_and:c
5222   (convert1?@4
5223    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5224   (convert2? @0))
5225  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5227 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5228  (bit_and:c
5229   (convert1?@4
5230    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5231   (convert2? @0))
5232  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5234 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5235  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5236             INTEGER_CST@1)
5237  (with {
5238          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5239                                               TYPE_PRECISION(type)));
5240          int ibit2 = tree_log2 (@1);
5241        }
5242   (if (ibit == ibit2
5243       && ibit >= 0
5244       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5246 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5247  (bit_and@4
5248   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5249   INTEGER_CST@1)
5250  (with {
5251          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5252                                               TYPE_PRECISION(type)));
5253          int ibit2 = tree_log2 (@1);
5254        }
5255   (if (ibit == ibit2
5256       && ibit >= 0
5257       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5259 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5260  (bit_and:c
5261   (convert1?@3
5262    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5263   (convert2? @0))
5264  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5266 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5267  (bit_and:c
5268   (convert1?@3
5269    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5270   (convert2? @0))
5271   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5273 #endif
5275 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5276    Currently disabled after pass lvec because ARM understands
5277    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5278 #if GIMPLE
5279 /* These can only be done in gimple as fold likes to convert:
5280    (CMP) & N into (CMP) ? N : 0
5281    and we try to match the same pattern again and again. */
5282 (simplify
5283  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5284  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5285   (vec_cond (bit_and @0 @3) @1 @2)))
5286 (simplify
5287  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5288  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5289   (vec_cond (bit_ior @0 @3) @1 @2)))
5290 (simplify
5291  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5292  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5293   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5294 (simplify
5295  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5296  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5297   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5299 /*  ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5300     (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5301     component type of the outer vec_cond is greater equal the inner one.  */
5302 (for cmp (simple_comparison)
5303  (simplify
5304   (vec_cond
5305     (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5306                                     integer_all_onesp
5307                                     integer_zerop))
5308           integer_zerop) @2 @3)
5309   (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5310        && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5311        && !TYPE_UNSIGNED (TREE_TYPE (@5))
5312        && VECTOR_TYPE_P (TREE_TYPE (@6))
5313        && VECTOR_TYPE_P (type)
5314        && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5315                            TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5316        && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5317    (with { tree vtype = TREE_TYPE (@6);}
5318      (view_convert:type
5319        (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5321 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5322 (simplify
5323  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5324  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5325   (vec_cond (bit_and @0 @1) @2 @3)))
5326 (simplify
5327  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5328  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5329   (vec_cond (bit_ior @0 @1) @2 @3)))
5330 (simplify
5331  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5332  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5333   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5334 (simplify
5335  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5336  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5337   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5338 #endif
5340 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5341    types are compatible.  */
5342 (simplify
5343  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5344  (if (VECTOR_BOOLEAN_TYPE_P (type)
5345       && types_match (type, TREE_TYPE (@0)))
5346   (if (integer_zerop (@1) && integer_all_onesp (@2))
5347    (bit_not @0)
5348    (if (integer_all_onesp (@1) && integer_zerop (@2))
5349     @0))))
5351 /* A few simplifications of "a ? CST1 : CST2". */
5352 /* NOTE: Only do this on gimple as the if-chain-to-switch
5353    optimization depends on the gimple to have if statements in it. */
5354 #if GIMPLE
5355 (simplify
5356  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5357  (switch
5358   (if (integer_zerop (@2))
5359    (switch
5360     /* a ? 1 : 0 -> a if 0 and 1 are integral types.  */
5361     (if (integer_onep (@1))
5362      (convert (convert:boolean_type_node @0)))
5363     /* a ? -1 : 0 -> -a.  */
5364     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5365      (if (TYPE_PRECISION (type) == 1)
5366       /* For signed 1-bit precision just cast bool to the type.  */
5367       (convert (convert:boolean_type_node @0))
5368       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5369        (with {
5370           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5371                                                       TYPE_UNSIGNED (type));
5372         }
5373         (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5374        (negate (convert:type (convert:boolean_type_node @0))))))
5375     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5376     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5377      (with {
5378        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5379       }
5380       (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5381   (if (integer_zerop (@1))
5382    (switch
5383     /* a ? 0 : 1 -> !a.  */
5384     (if (integer_onep (@2))
5385      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5386     /* a ? 0 : -1 -> -(!a).  */
5387     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5388      (if (TYPE_PRECISION (type) == 1)
5389       /* For signed 1-bit precision just cast bool to the type.  */
5390       (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5391       (if (TREE_CODE (type) == BOOLEAN_TYPE)
5392        (with {
5393           tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5394                                                       TYPE_UNSIGNED (type));
5395         }
5396         (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5397                                                 { boolean_true_node; })))))
5398        (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5399                                       { boolean_true_node; }))))))
5400     /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5401     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5402      (with {
5403        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5404       }
5405       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5406                                 { boolean_true_node; })) { shift; })))))))
5408 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5409    for unsigned types. */
5410 (simplify
5411  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5412  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5413       && bitwise_equal_p (@0, @2))
5414   (convert (eq @0 @1))
5418 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5419    for unsigned types. */
5420 (simplify
5421  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5422  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5423       && bitwise_equal_p (@0, @2))
5424   (convert (eq @0 @1))
5428 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5429    on the first bit of the CST.  */
5430 (simplify
5431  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5432  (if ((wi::to_wide (@1) & 1) != 0)
5433   @2
5434   { build_zero_cst (type); }))
5436 /* Optimize
5437    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5438    x_5 == cstN ? cst4 : cst3
5439    # op is == or != and N is 1 or 2
5440    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5441    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5442    of cst3 and cst4 is smaller.
5443    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5444 (for eqne (ne eq)
5445  (simplify
5446   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5447   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5448        && INTEGRAL_TYPE_P (type)
5449        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5450            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5451    (with {
5452      value_range r;
5453      get_range_query (cfun)->range_of_expr (r, @0);
5454      if (r.undefined_p ())
5455        r.set_varying (TREE_TYPE (@0));
5457      wide_int min = r.lower_bound ();
5458      wide_int max = r.upper_bound ();
5459     }
5460     (if (min + 1 == max
5461          && (wi::to_wide (@1) == min
5462              || wi::to_wide (@1) == max))
5463      (with {
5464        tree arg0 = @2, arg1 = @3;
5465        tree type1;
5466        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5467          std::swap (arg0, arg1);
5468        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5469          type1 = TREE_TYPE (@0);
5470        else
5471          type1 = type;
5472        auto prec = TYPE_PRECISION (type1);
5473        auto unsign = TYPE_UNSIGNED (type1);
5474        if (TREE_CODE (type1) == BOOLEAN_TYPE)
5475         type1 = build_nonstandard_integer_type (prec, unsign);
5476        min = wide_int::from (min, prec,
5477                              TYPE_SIGN (TREE_TYPE (@0)));
5478        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5479                                     TYPE_SIGN (type));
5480        enum tree_code code;
5481        wi::overflow_type ovf;
5482        if (tree_int_cst_lt (arg0, arg1))
5483          {
5484            code = PLUS_EXPR;
5485            a -= min;
5486            if (!unsign)
5487              {
5488                /* lhs is known to be in range [min, min+1] and we want to add a
5489                   to it.  Check if that operation can overflow for those 2 values
5490                   and if yes, force unsigned type.  */
5491                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5492                if (ovf)
5493                  type1 = unsigned_type_for (type1);
5494              }
5495          }
5496        else
5497          {
5498            code = MINUS_EXPR;
5499            a += min;
5500            if (!unsign)
5501              {
5502                /* lhs is known to be in range [min, min+1] and we want to subtract
5503                   it from a.  Check if that operation can overflow for those 2
5504                   values and if yes, force unsigned type.  */
5505                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5506                if (ovf)
5507                 type1 = unsigned_type_for (type1);
5508              }
5509          }
5510        tree arg = wide_int_to_tree (type1, a);
5511       }
5512       (if (code == PLUS_EXPR)
5513        (convert (plus (convert:type1 @0) { arg; }))
5514        (convert (minus { arg; } (convert:type1 @0))))))))))
5515 #endif
5517 (simplify
5518  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5519  (if (INTEGRAL_TYPE_P (type)
5520       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5521   (cond @1 (convert @2) (convert @3))))
5523 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5524    be extended.  */
5525 /* This pattern implements two kinds simplification:
5527    Case 1)
5528    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5529      1) Conversions are type widening from smaller type.
5530      2) Const c1 equals to c2 after canonicalizing comparison.
5531      3) Comparison has tree code LT, LE, GT or GE.
5532    This specific pattern is needed when (cmp (convert x) c) may not
5533    be simplified by comparison patterns because of multiple uses of
5534    x.  It also makes sense here because simplifying across multiple
5535    referred var is always benefitial for complicated cases.
5537    Case 2)
5538    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5539 (for cmp (lt le gt ge eq ne)
5540  (simplify
5541   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5542   (with
5543    {
5544      tree from_type = TREE_TYPE (@1);
5545      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5546      enum tree_code code = ERROR_MARK;
5548      if (INTEGRAL_TYPE_P (from_type)
5549          && int_fits_type_p (@2, from_type)
5550          && (types_match (c1_type, from_type)
5551              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5552                  && (TYPE_UNSIGNED (from_type)
5553                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5554          && (types_match (c2_type, from_type)
5555              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5556                  && (TYPE_UNSIGNED (from_type)
5557                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5558        {
5559          if (cmp != EQ_EXPR)
5560            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5561          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5562          else if (int_fits_type_p (@3, from_type))
5563            code = EQ_EXPR;
5564        }
5565    }
5566    (if (code == MAX_EXPR)
5567     (convert (max @1 (convert @2)))
5568     (if (code == MIN_EXPR)
5569      (convert (min @1 (convert @2)))
5570      (if (code == EQ_EXPR)
5571       (convert (cond (eq @1 (convert @3))
5572                      (convert:from_type @3) (convert:from_type @2)))))))))
5574 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5576      1) OP is PLUS or MINUS.
5577      2) CMP is LT, LE, GT or GE.
5578      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5580    This pattern also handles special cases like:
5582      A) Operand x is a unsigned to signed type conversion and c1 is
5583         integer zero.  In this case,
5584           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5585           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5586      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5587         check equality for (c1+1) and (c1-1) by adjusting comparison
5588         code.
5590    TODO: Though signed type is handled by this pattern, it cannot be
5591    simplified at the moment because C standard requires additional
5592    type promotion.  In order to match&simplify it here, the IR needs
5593    to be cleaned up by other optimizers, i.e, VRP.  */
5594 (for op (plus minus)
5595  (for cmp (lt le gt ge)
5596   (simplify
5597    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5598    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5599     (if (types_match (from_type, to_type)
5600          /* Check if it is special case A).  */
5601          || (TYPE_UNSIGNED (from_type)
5602              && !TYPE_UNSIGNED (to_type)
5603              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5604              && integer_zerop (@1)
5605              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5606      (with
5607       {
5608         wi::overflow_type overflow = wi::OVF_NONE;
5609         enum tree_code code, cmp_code = cmp;
5610         wide_int real_c1;
5611         wide_int c1 = wi::to_wide (@1);
5612         wide_int c2 = wi::to_wide (@2);
5613         wide_int c3 = wi::to_wide (@3);
5614         signop sgn = TYPE_SIGN (from_type);
5616         /* Handle special case A), given x of unsigned type:
5617             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5618             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5619         if (!types_match (from_type, to_type))
5620           {
5621             if (cmp_code == LT_EXPR)
5622               cmp_code = GT_EXPR;
5623             if (cmp_code == GE_EXPR)
5624               cmp_code = LE_EXPR;
5625             c1 = wi::max_value (to_type);
5626           }
5627         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5628            compute (c3 op' c2) and check if it equals to c1 with op' being
5629            the inverted operator of op.  Make sure overflow doesn't happen
5630            if it is undefined.  */
5631         if (op == PLUS_EXPR)
5632           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5633         else
5634           real_c1 = wi::add (c3, c2, sgn, &overflow);
5636         code = cmp_code;
5637         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5638           {
5639             /* Check if c1 equals to real_c1.  Boundary condition is handled
5640                by adjusting comparison operation if necessary.  */
5641             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5642                 && !overflow)
5643               {
5644                 /* X <= Y - 1 equals to X < Y.  */
5645                 if (cmp_code == LE_EXPR)
5646                   code = LT_EXPR;
5647                 /* X > Y - 1 equals to X >= Y.  */
5648                 if (cmp_code == GT_EXPR)
5649                   code = GE_EXPR;
5650               }
5651             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5652                 && !overflow)
5653               {
5654                 /* X < Y + 1 equals to X <= Y.  */
5655                 if (cmp_code == LT_EXPR)
5656                   code = LE_EXPR;
5657                 /* X >= Y + 1 equals to X > Y.  */
5658                 if (cmp_code == GE_EXPR)
5659                   code = GT_EXPR;
5660               }
5661             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5662               {
5663                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5664                   code = MIN_EXPR;
5665                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5666                   code = MAX_EXPR;
5667               }
5668           }
5669       }
5670       (if (code == MAX_EXPR)
5671        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5672            { wide_int_to_tree (from_type, c2); })
5673        (if (code == MIN_EXPR)
5674         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5675             { wide_int_to_tree (from_type, c2); })))))))))
5677 #if GIMPLE
5678 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5679    in fold_cond_expr_with_comparison for GENERIC folding with
5680    some extra constraints.  */
5681 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5682  (simplify
5683   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5684         (convert3? @0) (convert4? @1))
5685   (if (!HONOR_SIGNED_ZEROS (type)
5686        && (/* Allow widening conversions of the compare operands as data.  */
5687            (INTEGRAL_TYPE_P (type)
5688             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5689             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5690             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5691             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5692            /* Or sign conversions for the comparison.  */
5693            || (types_match (type, TREE_TYPE (@0))
5694                && types_match (type, TREE_TYPE (@1)))))
5695    (switch
5696     (if (cmp == EQ_EXPR)
5697      (if (VECTOR_TYPE_P (type))
5698       (view_convert @c1)
5699       (convert @c1)))
5700     (if (cmp == NE_EXPR)
5701      (if (VECTOR_TYPE_P (type))
5702       (view_convert @c0)
5703       (convert @c0)))
5704     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5705      (if (!HONOR_NANS (type))
5706       (if (VECTOR_TYPE_P (type))
5707        (view_convert (min @c0 @c1))
5708        (convert (min @c0 @c1)))))
5709     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5710      (if (!HONOR_NANS (type))
5711       (if (VECTOR_TYPE_P (type))
5712        (view_convert (max @c0 @c1))
5713        (convert (max @c0 @c1)))))
5714     (if (cmp == UNEQ_EXPR)
5715      (if (!HONOR_NANS (type))
5716       (if (VECTOR_TYPE_P (type))
5717        (view_convert @c1)
5718        (convert @c1))))
5719     (if (cmp == LTGT_EXPR)
5720      (if (!HONOR_NANS (type))
5721       (if (VECTOR_TYPE_P (type))
5722        (view_convert @c0)
5723        (convert @c0))))))))
5725 /* This is for VEC_COND_EXPR
5726    Optimize A < B ? A : B to MIN (A, B)
5727             A > B ? A : B to MAX (A, B).  */
5728 (for cmp (lt le ungt unge gt ge unlt unle)
5729      minmax (min min min min max max max max)
5730      MINMAX (MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR)
5731  (simplify
5732   (vec_cond (cmp @0 @1) @0 @1)
5733    (if (VECTOR_INTEGER_TYPE_P (type)
5734        && target_supports_op_p (type, MINMAX, optab_vector))
5735     (minmax @0 @1))))
5737 (for cmp (lt le ungt unge gt ge unlt unle)
5738      minmax (max max max max min min min min)
5739      MINMAX (MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR)
5740  (simplify
5741   (vec_cond (cmp @0 @1) @1 @0)
5742    (if (VECTOR_INTEGER_TYPE_P (type)
5743        && target_supports_op_p (type, MINMAX, optab_vector))
5744     (minmax @0 @1))))
5745 #endif
5747 (for cnd (cond vec_cond)
5748  /* (a != b) ? (a - b) : 0 -> (a - b) */
5749  (simplify
5750   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5751   @2)
5752  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5753  (simplify
5754   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5755   @2)
5756  /* (a != b) ? (a & b) : a -> (a & b) */
5757  /* (a != b) ? (a | b) : a -> (a | b) */
5758  /* (a != b) ? min(a,b) : a -> min(a,b) */
5759  /* (a != b) ? max(a,b) : a -> max(a,b) */
5760  (for op (bit_and bit_ior min max)
5761   (simplify
5762    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5763    @2))
5764  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5765  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5766  (for op (mult plus)
5767   (simplify
5768    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5769    (if (ANY_INTEGRAL_TYPE_P (type))
5770     @2)))
5771  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5772  (simplify
5773   (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5774   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5775    @2))
5778 /* These was part of minmax phiopt.  */
5779 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5780    to minmax<min/max<a, b>, c> */
5781 (for minmax (min max)
5782  (for cmp (lt le gt ge ne)
5783   (simplify
5784    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5785    (with
5786     {
5787       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5788     }
5789     (if (code == MIN_EXPR)
5790      (minmax (min @1 @2) @4)
5791      (if (code == MAX_EXPR)
5792       (minmax (max @1 @2) @4)))))))
5794 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5795 (for cmp    (gt  ge  lt  le)
5796      minmax (min min max max)
5797  (simplify
5798   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5799    (with
5800     {
5801       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5802     }
5803     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5804          && code == MIN_EXPR
5805          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5806      (min @2 @4)
5807      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5808           && code == MAX_EXPR
5809           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5810       (max @2 @4))))))
5812 #if GIMPLE
5813 /* These patterns should be after min/max detection as simplifications
5814    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5815    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5816    Even without those, reaching min/max/and/ior faster is better.  */
5817 (simplify
5818  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5819  (switch
5820   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5821   (if (integer_zerop (@2))
5822    (bit_and (convert @0) @1))
5823   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5824   (if (integer_zerop (@1))
5825    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5826   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5827   (if (integer_onep (@1))
5828    (bit_ior (convert @0) @2))
5829   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5830   (if (integer_onep (@2))
5831    (bit_ior (bit_xor (convert @0) @2) @1))
5834 #endif
5836 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5837 (simplify
5838  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5839  (if (!TYPE_SATURATING (type)
5840       && (TYPE_OVERFLOW_WRAPS (type)
5841           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5842       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5843   @3))
5845 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5846 (simplify
5847  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5848  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5849   @3))
5851 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5852 (for op (abs absu)
5853  (simplify
5854   (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5855   (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5856    (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5857     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5858      (convert (absu:utype @0)))
5859     @3))))
5861 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5862    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5863 (simplify
5864  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5865  (if (TYPE_UNSIGNED (type))
5866   (cond (ge @0 @1) (negate @0) @2)))
5868 (for cnd (cond vec_cond)
5869  /* A ? B : (A ? X : C) -> A ? B : C.  */
5870  (simplify
5871   (cnd @0 (cnd @0 @1 @2) @3)
5872   (cnd @0 @1 @3))
5873  (simplify
5874   (cnd @0 @1 (cnd @0 @2 @3))
5875   (cnd @0 @1 @3))
5876  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5877  /* ???  This matches embedded conditions open-coded because genmatch
5878     would generate matching code for conditions in separate stmts only.
5879     The following is still important to merge then and else arm cases
5880     from if-conversion.  */
5881  (simplify
5882   (cnd @0 @1 (cnd @2 @3 @4))
5883   (if (inverse_conditions_p (@0, @2))
5884    (cnd @0 @1 @3)))
5885  (simplify
5886   (cnd @0 (cnd @1 @2 @3) @4)
5887   (if (inverse_conditions_p (@0, @1))
5888    (cnd @0 @3 @4)))
5890  /* A ? B : B -> B.  */
5891  (simplify
5892   (cnd @0 @1 @1)
5893   @1)
5895  /* !A ? B : C -> A ? C : B.  */
5896  (simplify
5897   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5898   (cnd @0 @2 @1)))
5900 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5902    None of these transformations work for modes with signed
5903    zeros.  If A is +/-0, the first two transformations will
5904    change the sign of the result (from +0 to -0, or vice
5905    versa).  The last four will fix the sign of the result,
5906    even though the original expressions could be positive or
5907    negative, depending on the sign of A.
5909    Note that all these transformations are correct if A is
5910    NaN, since the two alternatives (A and -A) are also NaNs.  */
5912 (for cnd (cond vec_cond)
5913  /* A == 0 ? A : -A    same as -A */
5914  (for cmp (eq uneq)
5915   (simplify
5916    (cnd (cmp @0 zerop) @2 (negate@1 @2))
5917     (if (!HONOR_SIGNED_ZEROS (type)
5918          && bitwise_equal_p (@0, @2))
5919      @1))
5920   (simplify
5921    (cnd (cmp @0 zerop) zerop (negate@1 @2))
5922     (if (!HONOR_SIGNED_ZEROS (type)
5923          && bitwise_equal_p (@0, @2))
5924      @1))
5926  /* A != 0 ? A : -A    same as A */
5927  (for cmp (ne ltgt)
5928   (simplify
5929    (cnd (cmp @0 zerop) @1 (negate @1))
5930     (if (!HONOR_SIGNED_ZEROS (type)
5931          && bitwise_equal_p (@0, @1))
5932      @1))
5933   (simplify
5934    (cnd (cmp @0 zerop) @1 integer_zerop)
5935     (if (!HONOR_SIGNED_ZEROS (type)
5936          && bitwise_equal_p (@0, @1))
5937      @1))
5939  /* A >=/> 0 ? A : -A    same as abs (A) */
5940  (for cmp (ge gt)
5941   (simplify
5942    (cnd (cmp @0 zerop) @1 (negate @1))
5943     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5944          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5945          && bitwise_equal_p (@0, @1))
5946      (if (TYPE_UNSIGNED (type))
5947       (absu:type @0)
5948       (abs @0)))))
5949  /* A <=/< 0 ? A : -A    same as -abs (A) */
5950  (for cmp (le lt)
5951   (simplify
5952    (cnd (cmp @0 zerop) @1 (negate @1))
5953     (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5954          && !TYPE_UNSIGNED (TREE_TYPE(@0))
5955          && bitwise_equal_p (@0, @1))
5956      (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5957            && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5958           || TYPE_UNSIGNED (type))
5959       (with {
5960         tree utype = unsigned_type_for (TREE_TYPE(@0));
5961        }
5962        (convert (negate (absu:utype @0))))
5963        (negate (abs @0)))))
5966  /* (A - B) == 0 ? (A - B) : (B - A)    same as (B - A) */
5967  (for cmp (eq uneq)
5968   (simplify
5969    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5970    (if (!HONOR_SIGNED_ZEROS (type))
5971     @3))
5972   (simplify
5973    (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5974    @3)
5976  /* (A - B) != 0 ? (A - B) : (B - A)    same as (A - B) */
5977  (for cmp (ne ltgt)
5978   (simplify
5979    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5980    (if (!HONOR_SIGNED_ZEROS (type))
5981     @0))
5982   (simplify
5983    (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5984    @0)
5986  /* (A - B) >=/> 0 ? (A - B) : (B - A)    same as abs (A - B) */
5987  (for cmp (ge gt)
5988   (simplify
5989    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5990    (if (!HONOR_SIGNED_ZEROS (type)
5991         && !TYPE_UNSIGNED (type))
5992     (abs @0))))
5993  /* (A - B) <=/< 0 ? (A - B) : (B - A)    same as -abs (A - B) */
5994  (for cmp (le lt)
5995   (simplify
5996    (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5997    (if (!HONOR_SIGNED_ZEROS (type)
5998         && !TYPE_UNSIGNED (type))
5999     (if (ANY_INTEGRAL_TYPE_P (type)
6000          && !TYPE_OVERFLOW_WRAPS (type))
6001      (with {
6002         tree utype = unsigned_type_for (type);
6003       }
6004       (convert (negate (absu:utype @0))))
6005       (negate (abs @0)))))
6009 /* -(type)!A -> (type)A - 1.  */
6010 (simplify
6011  (negate (convert?:s (logical_inverted_value:s @0)))
6012  (if (INTEGRAL_TYPE_P (type)
6013       && TREE_CODE (type) != BOOLEAN_TYPE
6014       && TYPE_PRECISION (type) > 1
6015       && TREE_CODE (@0) == SSA_NAME
6016       && ssa_name_has_boolean_range (@0))
6017   (plus (convert:type @0) { build_all_ones_cst (type); })))
6019 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
6020    return all -1 or all 0 results.  */
6021 /* ??? We could instead convert all instances of the vec_cond to negate,
6022    but that isn't necessarily a win on its own.  */
6023 (simplify
6024  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6025  (if (VECTOR_TYPE_P (type)
6026       && known_eq (TYPE_VECTOR_SUBPARTS (type),
6027                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6028       && (TYPE_MODE (TREE_TYPE (type))
6029           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6030   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6032 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
6033 (simplify
6034  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6035  (if (VECTOR_TYPE_P (type)
6036       && known_eq (TYPE_VECTOR_SUBPARTS (type),
6037                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6038       && (TYPE_MODE (TREE_TYPE (type))
6039           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6040   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6043 /* Simplifications of comparisons.  */
6045 /* See if we can reduce the magnitude of a constant involved in a
6046    comparison by changing the comparison code.  This is a canonicalization
6047    formerly done by maybe_canonicalize_comparison_1.  */
6048 (for cmp  (le gt)
6049      acmp (lt ge)
6050  (simplify
6051   (cmp @0 uniform_integer_cst_p@1)
6052   (with { tree cst = uniform_integer_cst_p (@1); }
6053    (if (tree_int_cst_sgn (cst) == -1)
6054      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6055                                    wide_int_to_tree (TREE_TYPE (cst),
6056                                                      wi::to_wide (cst)
6057                                                      + 1)); })))))
6058 (for cmp  (ge lt)
6059      acmp (gt le)
6060  (simplify
6061   (cmp @0 uniform_integer_cst_p@1)
6062   (with { tree cst = uniform_integer_cst_p (@1); }
6063    (if (tree_int_cst_sgn (cst) == 1)
6064     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6065                                   wide_int_to_tree (TREE_TYPE (cst),
6066                                   wi::to_wide (cst) - 1)); })))))
6068 /* We can simplify a logical negation of a comparison to the
6069    inverted comparison.  As we cannot compute an expression
6070    operator using invert_tree_comparison we have to simulate
6071    that with expression code iteration.  */
6072 (for cmp (tcc_comparison)
6073      icmp (inverted_tcc_comparison)
6074      ncmp (inverted_tcc_comparison_with_nans)
6075  /* Ideally we'd like to combine the following two patterns
6076     and handle some more cases by using
6077       (logical_inverted_value (cmp @0 @1))
6078     here but for that genmatch would need to "inline" that.
6079     For now implement what forward_propagate_comparison did.  */
6080  (simplify
6081   (bit_not (cmp @0 @1))
6082   (if (VECTOR_TYPE_P (type)
6083        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6084    /* Comparison inversion may be impossible for trapping math,
6085       invert_tree_comparison will tell us.  But we can't use
6086       a computed operator in the replacement tree thus we have
6087       to play the trick below.  */
6088    (with { enum tree_code ic = invert_tree_comparison
6089              (cmp, HONOR_NANS (@0)); }
6090     (if (ic == icmp)
6091      (icmp @0 @1)
6092      (if (ic == ncmp)
6093       (ncmp @0 @1))))))
6094  (simplify
6095   (bit_xor (cmp @0 @1) integer_truep)
6096   (with { enum tree_code ic = invert_tree_comparison
6097             (cmp, HONOR_NANS (@0)); }
6098    (if (ic == icmp)
6099     (icmp @0 @1)
6100     (if (ic == ncmp)
6101      (ncmp @0 @1)))))
6102  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
6103  (simplify
6104   (ne (cmp@2 @0 @1) integer_zerop)
6105   (if (types_match (type, TREE_TYPE (@2)))
6106    (cmp @0 @1)))
6107  (simplify
6108   (eq (cmp@2 @0 @1) integer_truep)
6109   (if (types_match (type, TREE_TYPE (@2)))
6110    (cmp @0 @1)))
6111  (simplify
6112   (ne (cmp@2 @0 @1) integer_truep)
6113   (if (types_match (type, TREE_TYPE (@2)))
6114    (with { enum tree_code ic = invert_tree_comparison
6115              (cmp, HONOR_NANS (@0)); }
6116     (if (ic == icmp)
6117      (icmp @0 @1)
6118      (if (ic == ncmp)
6119       (ncmp @0 @1))))))
6120  (simplify
6121   (eq (cmp@2 @0 @1) integer_zerop)
6122   (if (types_match (type, TREE_TYPE (@2)))
6123    (with { enum tree_code ic = invert_tree_comparison
6124              (cmp, HONOR_NANS (@0)); }
6125     (if (ic == icmp)
6126      (icmp @0 @1)
6127      (if (ic == ncmp)
6128       (ncmp @0 @1)))))))
6130 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6131    ??? The transformation is valid for the other operators if overflow
6132    is undefined for the type, but performing it here badly interacts
6133    with the transformation in fold_cond_expr_with_comparison which
6134    attempts to synthetize ABS_EXPR.  */
6135 (for cmp (eq ne)
6136  (for sub (minus pointer_diff)
6137   (simplify
6138    (cmp (sub@2 @0 @1) integer_zerop)
6139    (if (single_use (@2))
6140     (cmp @0 @1)))))
6142 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6143    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
6144 (for cmp (lt ge)
6145  (simplify
6146   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6147    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6148         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6149         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6150     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6151 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6152    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
6153 (simplify
6154  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6155   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6156        && !TYPE_UNSIGNED (TREE_TYPE (@0))
6157        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6158    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6160 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6161    signed arithmetic case.  That form is created by the compiler
6162    often enough for folding it to be of value.  One example is in
6163    computing loop trip counts after Operator Strength Reduction.  */
6164 (for cmp (simple_comparison)
6165      scmp (swapped_simple_comparison)
6166  (simplify
6167   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6168   /* Handle unfolded multiplication by zero.  */
6169   (if (integer_zerop (@1))
6170    (cmp @1 @2)
6171    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6172         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6173         && single_use (@3))
6174     /* If @1 is negative we swap the sense of the comparison.  */
6175     (if (tree_int_cst_sgn (@1) < 0)
6176      (scmp @0 @2)
6177      (cmp @0 @2))))))
6179 /* For integral types with undefined overflow fold
6180    x * C1 == C2 into x == C2 / C1 or false.
6181    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6182    Z / 2^n Z.  */
6183 (for cmp (eq ne)
6184  (simplify
6185   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6186   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6187        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6188        && wi::to_wide (@1) != 0)
6189    (with { widest_int quot; }
6190     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6191                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
6192      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6193      { constant_boolean_node (cmp == NE_EXPR, type); }))
6194    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6195         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6196         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6197     (cmp @0
6198      {
6199        tree itype = TREE_TYPE (@0);
6200        int p = TYPE_PRECISION (itype);
6201        wide_int m = wi::one (p + 1) << p;
6202        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6203        wide_int i = wide_int::from (wi::mod_inv (a, m),
6204                                     p, TYPE_SIGN (itype));
6205        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6206      })))))
6208 /* Simplify comparison of something with itself.  For IEEE
6209    floating-point, we can only do some of these simplifications.  */
6210 (for cmp (eq ge le)
6211  (simplify
6212   (cmp @0 @0)
6213   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6214        || ! tree_expr_maybe_nan_p (@0))
6215    { constant_boolean_node (true, type); }
6216    (if (cmp != EQ_EXPR
6217         /* With -ftrapping-math conversion to EQ loses an exception.  */
6218         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6219             || ! flag_trapping_math))
6220     (eq @0 @0)))))
6221 (for cmp (ne gt lt)
6222  (simplify
6223   (cmp @0 @0)
6224   (if (cmp != NE_EXPR
6225        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6226        || ! tree_expr_maybe_nan_p (@0))
6227    { constant_boolean_node (false, type); })))
6228 (for cmp (unle unge uneq)
6229  (simplify
6230   (cmp @0 @0)
6231   { constant_boolean_node (true, type); }))
6232 (for cmp (unlt ungt)
6233  (simplify
6234   (cmp @0 @0)
6235   (unordered @0 @0)))
6236 (simplify
6237  (ltgt @0 @0)
6238  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6239   { constant_boolean_node (false, type); }))
6241 /* x == ~x -> false */
6242 /* x != ~x -> true */
6243 (for cmp (eq ne)
6244  (simplify
6245   (cmp:c @0 (bit_not @0))
6246   { constant_boolean_node (cmp == NE_EXPR, type); }))
6248 /* Fold ~X op ~Y as Y op X.  */
6249 (for cmp (simple_comparison)
6250  (simplify
6251   (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6252   (if (single_use (@2) && single_use (@3))
6253    (with { tree otype = TREE_TYPE (@4); }
6254     (cmp (convert:otype @1) (convert:otype @0))))))
6256 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
6257 (for cmp (simple_comparison)
6258      scmp (swapped_simple_comparison)
6259  (simplify
6260   (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6261   (if (single_use (@2)
6262        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6263    (with { tree otype = TREE_TYPE (@1); }
6264     (scmp (convert:otype @0) (bit_not @1))))))
6266 (for cmp (simple_comparison)
6267  (simplify
6268   (cmp @0 REAL_CST@1)
6269   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
6270   (switch
6271    /* a CMP (-0) -> a CMP 0  */
6272    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6273     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6274    /* (-0) CMP b -> 0 CMP b.  */
6275    (if (TREE_CODE (@0) == REAL_CST
6276         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6277     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6278    /* x != NaN is always true, other ops are always false.  */
6279    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6280         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6281         && !tree_expr_signaling_nan_p (@1)
6282         && !tree_expr_maybe_signaling_nan_p (@0))
6283     { constant_boolean_node (cmp == NE_EXPR, type); })
6284    /* NaN != y is always true, other ops are always false.  */
6285    (if (TREE_CODE (@0) == REAL_CST
6286         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6287         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6288         && !tree_expr_signaling_nan_p (@0)
6289         && !tree_expr_signaling_nan_p (@1))
6290     { constant_boolean_node (cmp == NE_EXPR, type); })
6291    /* Fold comparisons against infinity.  */
6292    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6293         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6294     (with
6295      {
6296        REAL_VALUE_TYPE max;
6297        enum tree_code code = cmp;
6298        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6299        if (neg)
6300          code = swap_tree_comparison (code);
6301      }
6302      (switch
6303       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
6304       (if (code == GT_EXPR
6305            && !(HONOR_NANS (@0) && flag_trapping_math))
6306        { constant_boolean_node (false, type); })
6307       (if (code == LE_EXPR)
6308        /* x <= +Inf is always true, if we don't care about NaNs.  */
6309        (if (! HONOR_NANS (@0))
6310         { constant_boolean_node (true, type); }
6311         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6312            an "invalid" exception.  */
6313         (if (!flag_trapping_math)
6314          (eq @0 @0))))
6315       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6316          for == this introduces an exception for x a NaN.  */
6317       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6318            || code == GE_EXPR)
6319        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6320         (if (neg)
6321          (lt @0 { build_real (TREE_TYPE (@0), max); })
6322          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6323       /* x < +Inf is always equal to x <= DBL_MAX.  */
6324       (if (code == LT_EXPR)
6325        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6326         (if (neg)
6327          (ge @0 { build_real (TREE_TYPE (@0), max); })
6328          (le @0 { build_real (TREE_TYPE (@0), max); }))))
6329       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6330          an exception for x a NaN so use an unordered comparison.  */
6331       (if (code == NE_EXPR)
6332        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6333         (if (! HONOR_NANS (@0))
6334          (if (neg)
6335           (ge @0 { build_real (TREE_TYPE (@0), max); })
6336           (le @0 { build_real (TREE_TYPE (@0), max); }))
6337          (if (neg)
6338           (unge @0 { build_real (TREE_TYPE (@0), max); })
6339           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6341  /* If this is a comparison of a real constant with a PLUS_EXPR
6342     or a MINUS_EXPR of a real constant, we can convert it into a
6343     comparison with a revised real constant as long as no overflow
6344     occurs when unsafe_math_optimizations are enabled.  */
6345  (if (flag_unsafe_math_optimizations)
6346   (for op (plus minus)
6347    (simplify
6348     (cmp (op @0 REAL_CST@1) REAL_CST@2)
6349     (with
6350      {
6351        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6352                                TREE_TYPE (@1), @2, @1);
6353      }
6354      (if (tem && !TREE_OVERFLOW (tem))
6355       (cmp @0 { tem; }))))))
6357  /* Likewise, we can simplify a comparison of a real constant with
6358     a MINUS_EXPR whose first operand is also a real constant, i.e.
6359     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6360     floating-point types only if -fassociative-math is set.  */
6361  (if (flag_associative_math)
6362   (simplify
6363    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6364    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6365     (if (tem && !TREE_OVERFLOW (tem))
6366      (cmp { tem; } @1)))))
6368  /* Fold comparisons against built-in math functions.  */
6369  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6370   (for sq (SQRT)
6371    (simplify
6372     (cmp (sq @0) REAL_CST@1)
6373     (switch
6374      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6375       (switch
6376        /* sqrt(x) < y is always false, if y is negative.  */
6377        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6378         { constant_boolean_node (false, type); })
6379        /* sqrt(x) > y is always true, if y is negative and we
6380           don't care about NaNs, i.e. negative values of x.  */
6381        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6382         { constant_boolean_node (true, type); })
6383        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6384        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6385      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6386       (switch
6387        /* sqrt(x) < 0 is always false.  */
6388        (if (cmp == LT_EXPR)
6389         { constant_boolean_node (false, type); })
6390        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6391        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6392         { constant_boolean_node (true, type); })
6393        /* sqrt(x) <= 0 -> x == 0.  */
6394        (if (cmp == LE_EXPR)
6395         (eq @0 @1))
6396        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6397           == or !=.  In the last case:
6399             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6401           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6402           the results for other x follow from natural arithmetic.  */
6403        (cmp @0 @1)))
6404      (if ((cmp == LT_EXPR
6405            || cmp == LE_EXPR
6406            || cmp == GT_EXPR
6407            || cmp == GE_EXPR)
6408           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6409           /* Give up for -frounding-math.  */
6410           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6411       (with
6412        {
6413          REAL_VALUE_TYPE c2;
6414          enum tree_code ncmp = cmp;
6415          const real_format *fmt
6416            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6417          real_arithmetic (&c2, MULT_EXPR,
6418                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6419          real_convert (&c2, fmt, &c2);
6420          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6421             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6422          if (!REAL_VALUE_ISINF (c2))
6423            {
6424              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6425                                         build_real (TREE_TYPE (@0), c2));
6426              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6427                ncmp = ERROR_MARK;
6428              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6429                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6430                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6431              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6432                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6433                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6434              else
6435                {
6436                  /* With rounding to even, sqrt of up to 3 different values
6437                     gives the same normal result, so in some cases c2 needs
6438                     to be adjusted.  */
6439                  REAL_VALUE_TYPE c2alt, tow;
6440                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6441                    tow = dconst0;
6442                  else
6443                    tow = dconstinf;
6444                  real_nextafter (&c2alt, fmt, &c2, &tow);
6445                  real_convert (&c2alt, fmt, &c2alt);
6446                  if (REAL_VALUE_ISINF (c2alt))
6447                    ncmp = ERROR_MARK;
6448                  else
6449                    {
6450                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6451                                            build_real (TREE_TYPE (@0), c2alt));
6452                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6453                        ncmp = ERROR_MARK;
6454                      else if (real_equal (&TREE_REAL_CST (c3),
6455                                           &TREE_REAL_CST (@1)))
6456                        c2 = c2alt;
6457                    }
6458                }
6459            }
6460        }
6461        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6462         (if (REAL_VALUE_ISINF (c2))
6463          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6464          (if (HONOR_INFINITIES (@0))
6465           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6466           { constant_boolean_node (false, type); })
6467          /* sqrt(x) > c is the same as x > c*c.  */
6468          (if (ncmp != ERROR_MARK)
6469           (if (ncmp == GE_EXPR)
6470            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6471            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6472         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6473         (if (REAL_VALUE_ISINF (c2))
6474          (switch
6475           /* sqrt(x) < y is always true, when y is a very large
6476              value and we don't care about NaNs or Infinities.  */
6477           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6478            { constant_boolean_node (true, type); })
6479           /* sqrt(x) < y is x != +Inf when y is very large and we
6480              don't care about NaNs.  */
6481           (if (! HONOR_NANS (@0))
6482            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6483           /* sqrt(x) < y is x >= 0 when y is very large and we
6484              don't care about Infinities.  */
6485           (if (! HONOR_INFINITIES (@0))
6486            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6487           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6488           (if (GENERIC)
6489            (truth_andif
6490             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6491             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6492          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6493          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6494           (if (ncmp == LT_EXPR)
6495            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6496            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6497           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6498           (if (ncmp != ERROR_MARK && GENERIC)
6499            (if (ncmp == LT_EXPR)
6500             (truth_andif
6501              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6502              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6503             (truth_andif
6504              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6505              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6506    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6507    (simplify
6508     (cmp (sq @0) (sq @1))
6509       (if (! HONOR_NANS (@0))
6510         (cmp @0 @1))))))
6512 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6513 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6514      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6515  (simplify
6516   (cmp (float@0 @1) (float @2))
6517    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6518         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6519     (with
6520      {
6521        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6522        tree type1 = TREE_TYPE (@1);
6523        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6524        tree type2 = TREE_TYPE (@2);
6525        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6526      }
6527      (if (fmt.can_represent_integral_type_p (type1)
6528           && fmt.can_represent_integral_type_p (type2))
6529       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6530        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6531        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6532             && type1_signed_p >= type2_signed_p)
6533         (icmp @1 (convert @2))
6534         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6535              && type1_signed_p <= type2_signed_p)
6536          (icmp (convert:type2 @1) @2)
6537          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6538               && type1_signed_p == type2_signed_p)
6539           (icmp @1 @2))))))))))
6541 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6542 (for cmp  (lt le eq ne ge gt)
6543      icmp (le le eq ne ge ge)
6544  (simplify
6545   (cmp (float @0) REAL_CST@1)
6546    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6547         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6548     (with
6549      {
6550        tree itype = TREE_TYPE (@0);
6551        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6552        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6553        /* Be careful to preserve any potential exceptions due to
6554           NaNs.  qNaNs are ok in == or != context.
6555           TODO: relax under -fno-trapping-math or
6556           -fno-signaling-nans.  */
6557        bool exception_p
6558          = real_isnan (cst) && (cst->signalling
6559                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6560      }
6561      /* TODO: allow non-fitting itype and SNaNs when
6562         -fno-trapping-math.  */
6563      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6564       (with
6565        {
6566          signop isign = TYPE_SIGN (itype);
6567          REAL_VALUE_TYPE imin, imax;
6568          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6569          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6571          REAL_VALUE_TYPE icst;
6572          if (cmp == GT_EXPR || cmp == GE_EXPR)
6573            real_ceil (&icst, fmt, cst);
6574          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6575            real_floor (&icst, fmt, cst);
6576          else
6577            real_trunc (&icst, fmt, cst);
6579          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6581          bool overflow_p = false;
6582          wide_int icst_val
6583            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6584        }
6585        (switch
6586         /* Optimize cases when CST is outside of ITYPE's range.  */
6587         (if (real_compare (LT_EXPR, cst, &imin))
6588          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6589                                   type); })
6590         (if (real_compare (GT_EXPR, cst, &imax))
6591          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6592                                   type); })
6593         /* Remove cast if CST is an integer representable by ITYPE.  */
6594         (if (cst_int_p)
6595          (cmp @0 { gcc_assert (!overflow_p);
6596                    wide_int_to_tree (itype, icst_val); })
6597         )
6598         /* When CST is fractional, optimize
6599             (FTYPE) N == CST -> 0
6600             (FTYPE) N != CST -> 1.  */
6601         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6602          { constant_boolean_node (cmp == NE_EXPR, type); })
6603         /* Otherwise replace with sensible integer constant.  */
6604         (with
6605          {
6606            gcc_checking_assert (!overflow_p);
6607          }
6608          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6610 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6611 (for cmp (eq ne)
6612  (simplify
6613   (cmp (exact_div @0 @1) INTEGER_CST@2)
6614   (if (!integer_zerop (@1))
6615    (if (wi::to_wide (@2) == 0)
6616     (cmp @0 @2)
6617     (if (TREE_CODE (@1) == INTEGER_CST)
6618      (with
6619       {
6620         wi::overflow_type ovf;
6621         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6622                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6623       }
6624       (if (ovf)
6625        { constant_boolean_node (cmp == NE_EXPR, type); }
6626        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6627 (for cmp (lt le gt ge)
6628  (simplify
6629   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6630   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6631    (with
6632     {
6633       wi::overflow_type ovf;
6634       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6635                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6636     }
6637     (if (ovf)
6638      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6639                                         TYPE_SIGN (TREE_TYPE (@2)))
6640                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6641      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6643 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6645    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6646    For large C (more than min/B+2^size), this is also true, with the
6647    multiplication computed modulo 2^size.
6648    For intermediate C, this just tests the sign of A.  */
6649 (for cmp  (lt le gt ge)
6650      cmp2 (ge ge lt lt)
6651  (simplify
6652   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6653   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6654        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6655        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6656    (with
6657     {
6658       tree utype = TREE_TYPE (@2);
6659       wide_int denom = wi::to_wide (@1);
6660       wide_int right = wi::to_wide (@2);
6661       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6662       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6663       bool small = wi::leu_p (right, smax);
6664       bool large = wi::geu_p (right, smin);
6665     }
6666     (if (small || large)
6667      (cmp (convert:utype @0) (mult @2 (convert @1)))
6668      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6670 /* Unordered tests if either argument is a NaN.  */
6671 (simplify
6672  (bit_ior (unordered @0 @0) (unordered @1 @1))
6673  (if (types_match (@0, @1))
6674   (unordered @0 @1)))
6675 (simplify
6676  (bit_and (ordered @0 @0) (ordered @1 @1))
6677  (if (types_match (@0, @1))
6678   (ordered @0 @1)))
6679 (simplify
6680  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6681  @2)
6682 (simplify
6683  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6684  @2)
6686 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6687    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6689    Note that comparisons
6690      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6691      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6692    will be canonicalized to above so there's no need to
6693    consider them here.
6694  */
6696 (for cmp (le gt)
6697      eqcmp (eq ne)
6698  (simplify
6699   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6700   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6701    (with
6702     {
6703      tree ty = TREE_TYPE (@0);
6704      unsigned prec = TYPE_PRECISION (ty);
6705      wide_int mask = wi::to_wide (@2, prec);
6706      wide_int rhs = wi::to_wide (@3, prec);
6707      signop sgn = TYPE_SIGN (ty);
6708     }
6709     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6710          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6711       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6712              { build_zero_cst (ty); }))))))
6714 /* -A CMP -B -> B CMP A.  */
6715 (for cmp (tcc_comparison)
6716      scmp (swapped_tcc_comparison)
6717  (simplify
6718   (cmp (negate @0) (negate @1))
6719   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6720        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6721            && (cmp == EQ_EXPR
6722                || cmp == NE_EXPR
6723                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6724    (scmp @0 @1)))
6725  (simplify
6726   (cmp (negate @0) CONSTANT_CLASS_P@1)
6727   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6728        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6729            && (cmp == EQ_EXPR
6730                || cmp == NE_EXPR
6731                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6732    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6733     (if (tem && !TREE_OVERFLOW (tem))
6734      (scmp @0 { tem; }))))))
6736 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6737 (for op (abs absu)
6738  (for eqne (eq ne)
6739   (simplify
6740    (eqne (op @0) zerop@1)
6741    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6743 /* From fold_sign_changed_comparison and fold_widened_comparison.
6744    FIXME: the lack of symmetry is disturbing.  */
6745 (for cmp (simple_comparison)
6746  (simplify
6747   (cmp (convert@0 @00) (convert?@1 @10))
6748   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6749        /* Disable this optimization if we're casting a function pointer
6750           type on targets that require function pointer canonicalization.  */
6751        && !(targetm.have_canonicalize_funcptr_for_compare ()
6752             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6753                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6754                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6755                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6756        && single_use (@0))
6757    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6758         && (TREE_CODE (@10) == INTEGER_CST
6759             || @1 != @10)
6760         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6761             || cmp == NE_EXPR
6762             || cmp == EQ_EXPR)
6763         && !POINTER_TYPE_P (TREE_TYPE (@00))
6764         /* (int)bool:32 != (int)uint is not the same as
6765            bool:32 != (bool:32)uint since boolean types only have two valid
6766            values independent of their precision.  */
6767         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6768             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6769     /* ???  The special-casing of INTEGER_CST conversion was in the original
6770        code and here to avoid a spurious overflow flag on the resulting
6771        constant which fold_convert produces.  */
6772     (if (TREE_CODE (@1) == INTEGER_CST)
6773      (cmp @00 { force_fit_type (TREE_TYPE (@00),
6774                                 wide_int::from (wi::to_wide (@1),
6775                                                 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6776                                                      TYPE_PRECISION (TREE_TYPE (@00))),
6777                                                 TYPE_SIGN (TREE_TYPE (@1))),
6778                                 0, TREE_OVERFLOW (@1)); })
6779      (cmp @00 (convert @1)))
6781     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6782      /* If possible, express the comparison in the shorter mode.  */
6783      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6784            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6785            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6786                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6787           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6788               || ((TYPE_PRECISION (TREE_TYPE (@00))
6789                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6790                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6791                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6792               || (TREE_CODE (@1) == INTEGER_CST
6793                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6794                   && int_fits_type_p (@1, TREE_TYPE (@00)))))
6795       (cmp @00 (convert @10))
6796       (if (TREE_CODE (@1) == INTEGER_CST
6797            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6798            && !int_fits_type_p (@1, TREE_TYPE (@00)))
6799        (with
6800         {
6801           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6802           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6803           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @1));
6804           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @1, min));
6805         }
6806         (if (above || below)
6807          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6808           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6809           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6810            { constant_boolean_node (above ? true : false, type); }
6811            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6812             { constant_boolean_node (above ? false : true, type); })))))))))
6813    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6814    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6815         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6816             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6817         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6818             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6819     (with
6820      {
6821        tree type1 = TREE_TYPE (@10);
6822        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6823          {
6824            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6825            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6826                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6827              type1 = float_type_node;
6828            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6829                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6830              type1 = double_type_node;
6831          }
6832        tree newtype
6833          = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6834             ? TREE_TYPE (@00) : type1);
6835      }
6836      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype)
6837           && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
6838       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6841 (for cmp (eq ne)
6842  (simplify
6843   /* SSA names are canonicalized to 2nd place.  */
6844   (cmp addr@0 SSA_NAME@1)
6845   (with
6846    {
6847      poly_int64 off; tree base;
6848      tree addr = (TREE_CODE (@0) == SSA_NAME
6849                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6850    }
6851    /* A local variable can never be pointed to by
6852       the default SSA name of an incoming parameter.  */
6853    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6854         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6855         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6856         && TREE_CODE (base) == VAR_DECL
6857         && auto_var_in_fn_p (base, current_function_decl))
6858     (if (cmp == NE_EXPR)
6859      { constant_boolean_node (true, type); }
6860      { constant_boolean_node (false, type); })
6861     /* If the address is based on @1 decide using the offset.  */
6862     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6863          && TREE_CODE (base) == MEM_REF
6864          && TREE_OPERAND (base, 0) == @1)
6865      (with { off += mem_ref_offset (base).force_shwi (); }
6866       (if (known_ne (off, 0))
6867        { constant_boolean_node (cmp == NE_EXPR, type); }
6868        (if (known_eq (off, 0))
6869         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6871 /* Equality compare simplifications from fold_binary  */
6872 (for cmp (eq ne)
6874  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6875     Similarly for NE_EXPR.  */
6876  (simplify
6877   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6878   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6879        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6880    { constant_boolean_node (cmp == NE_EXPR, type); }))
6882  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6883  (simplify
6884   (cmp (bit_xor @0 @1) integer_zerop)
6885   (cmp @0 @1))
6887  /* (X ^ Y) == Y becomes X == 0.
6888     Likewise (X ^ Y) == X becomes Y == 0.  */
6889  (simplify
6890   (cmp:c (bit_xor:c @0 @1) @0)
6891   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6893  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6894  (simplify
6895   (cmp:c (bit_and:c @0 @1) @0)
6896   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6897  (simplify
6898   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6899   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6900        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6901        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6902        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6903        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6904        && !wi::neg_p (wi::to_wide (@1)))
6905    (cmp (bit_and @0 (convert (bit_not @1)))
6906         { build_zero_cst (TREE_TYPE (@0)); })))
6908  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6909  (simplify
6910   (cmp:c (bit_ior:c @0 @1) @1)
6911   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6913  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6914  (simplify
6915   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6916   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6917    (cmp @0 (bit_xor @1 (convert @2)))))
6919  (simplify
6920   (cmp (nop_convert? @0) integer_zerop)
6921   (if (tree_expr_nonzero_p (@0))
6922    { constant_boolean_node (cmp == NE_EXPR, type); }))
6924  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6925  (simplify
6926   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6927   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6929 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6930    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6931    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6932    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6933 (for cmp (eq ne)
6934      ncmp (ge lt)
6935  (for sgncmp (ge lt)
6936   (simplify
6937    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6938    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6939         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6940         && types_match (@0, @1))
6941     (ncmp (bit_xor @0 @1) @2)))))
6942 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6943    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6944 (for cmp (eq ne)
6945      ncmp (lt ge)
6946  (simplify
6947   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6948    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6949         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6950         && types_match (@0, @1))
6951     (ncmp (bit_xor @0 @1) @2))))
6953 /* If we have (A & C) == C where C is a power of 2, convert this into
6954    (A & C) != 0.  Similarly for NE_EXPR.  */
6955 (for cmp (eq ne)
6956      icmp (ne eq)
6957  (simplify
6958   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6959   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6961 #if GIMPLE
6962 /* From fold_binary_op_with_conditional_arg handle the case of
6963    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6964    compares simplify.  */
6965 (for cmp (simple_comparison)
6966  (simplify
6967   (cmp:c (cond @0 @1 @2) @3)
6968   /* Do not move possibly trapping operations into the conditional as this
6969      pessimizes code and causes gimplification issues when applied late.  */
6970   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6971        || !operation_could_trap_p (cmp, true, false, @3))
6972    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6973 #endif
6975 (for cmp (ge lt)
6976 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6977 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6978  (simplify
6979   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6980    (if (INTEGRAL_TYPE_P (type)
6981         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6982         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6983         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6984     (with
6985      {
6986        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6987      }
6988     (if (cmp == LT_EXPR)
6989      (bit_xor (convert (rshift @0 {shifter;})) @1)
6990      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6991 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6992 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6993  (simplify
6994   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6995    (if (INTEGRAL_TYPE_P (type)
6996         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6997         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6998         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6999     (with
7000      {
7001        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
7002      }
7003     (if (cmp == GE_EXPR)
7004      (bit_xor (convert (rshift @0 {shifter;})) @1)
7005      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
7007 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
7008    convert this into a shift followed by ANDing with D.  */
7009 (simplify
7010  (cond
7011   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
7012   INTEGER_CST@2 integer_zerop)
7013  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
7014   (with {
7015      int shift = (wi::exact_log2 (wi::to_wide (@2))
7016                   - wi::exact_log2 (wi::to_wide (@1)));
7017    }
7018    (if (shift > 0)
7019     (bit_and
7020      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
7021     (bit_and
7022      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
7023      @2)))))
7025 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7026    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
7027 (for cmp (eq ne)
7028      ncmp (ge lt)
7029  (simplify
7030   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
7031   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7032        && type_has_mode_precision_p (TREE_TYPE (@0))
7033        && element_precision (@2) >= element_precision (@0)
7034        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
7035    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
7036     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
7038 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
7039    this into a right shift or sign extension followed by ANDing with C.  */
7040 (simplify
7041  (cond
7042   (lt @0 integer_zerop)
7043   INTEGER_CST@1 integer_zerop)
7044  (if (integer_pow2p (@1)
7045       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
7046   (with {
7047     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
7048    }
7049    (if (shift >= 0)
7050     (bit_and
7051      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
7052      @1)
7053     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
7054        sign extension followed by AND with C will achieve the effect.  */
7055     (bit_and (convert @0) @1)))))
7057 /* When the addresses are not directly of decls compare base and offset.
7058    This implements some remaining parts of fold_comparison address
7059    comparisons but still no complete part of it.  Still it is good
7060    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
7061 (for cmp (simple_comparison)
7062  (simplify
7063   (cmp (convert1?@2 addr@0) (convert2? addr@1))
7064   (with
7065    {
7066      poly_int64 off0, off1;
7067      tree base0, base1;
7068      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
7069                                   off0, off1, GENERIC);
7070    }
7071    (if (equal == 1)
7072     (switch
7073      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7074       { constant_boolean_node (known_eq (off0, off1), type); })
7075      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7076       { constant_boolean_node (known_ne (off0, off1), type); })
7077      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7078       { constant_boolean_node (known_lt (off0, off1), type); })
7079      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7080       { constant_boolean_node (known_le (off0, off1), type); })
7081      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7082       { constant_boolean_node (known_ge (off0, off1), type); })
7083      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7084       { constant_boolean_node (known_gt (off0, off1), type); }))
7085     (if (equal == 0)
7086      (switch
7087       (if (cmp == EQ_EXPR)
7088        { constant_boolean_node (false, type); })
7089       (if (cmp == NE_EXPR)
7090        { constant_boolean_node (true, type); })))))))
7092 #if GIMPLE
7093 /* a?~t:t -> (-(a))^t */
7094 (simplify
7095  (cond @0 @1 @2)
7096  (with { bool wascmp; }
7097   (if (INTEGRAL_TYPE_P (type)
7098        && bitwise_inverted_equal_p (@1, @2, wascmp)
7099        && (!wascmp || TYPE_PRECISION (type) == 1))
7100    (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7101         || TYPE_PRECISION (type) == 1)
7102     (bit_xor (convert:type @0) @2)
7103     (bit_xor (negate (convert:type @0)) @2)))))
7104 #endif
7106 /* Simplify pointer equality compares using PTA.  */
7107 (for neeq (ne eq)
7108  (simplify
7109   (neeq @0 @1)
7110   (if (POINTER_TYPE_P (TREE_TYPE (@0))
7111        && ptrs_compare_unequal (@0, @1))
7112    { constant_boolean_node (neeq != EQ_EXPR, type); })))
7114 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7115    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7116    Disable the transform if either operand is pointer to function.
7117    This broke pr22051-2.c for arm where function pointer
7118    canonicalizaion is not wanted.  */
7120 (for cmp (ne eq)
7121  (simplify
7122   (cmp (convert @0) INTEGER_CST@1)
7123   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7124          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7125          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7126          /* Don't perform this optimization in GENERIC if @0 has reference
7127             type when sanitizing.  See PR101210.  */
7128          && !(GENERIC
7129               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7130               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7131         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7132             && POINTER_TYPE_P (TREE_TYPE (@1))
7133             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7134        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7135    (cmp @0 (convert @1)))))
7137 /* Non-equality compare simplifications from fold_binary  */
7138 (for cmp (lt gt le ge)
7139  /* Comparisons with the highest or lowest possible integer of
7140     the specified precision will have known values.  */
7141  (simplify
7142   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7143   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7144         || POINTER_TYPE_P (TREE_TYPE (@1))
7145         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7146        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7147    (with
7148     {
7149       tree cst = uniform_integer_cst_p (@1);
7150       tree arg1_type = TREE_TYPE (cst);
7151       unsigned int prec = TYPE_PRECISION (arg1_type);
7152       wide_int max = wi::max_value (arg1_type);
7153       wide_int signed_max = wi::max_value (prec, SIGNED);
7154       wide_int min = wi::min_value (arg1_type);
7155     }
7156     (switch
7157      (if (wi::to_wide (cst) == max)
7158       (switch
7159        (if (cmp == GT_EXPR)
7160         { constant_boolean_node (false, type); })
7161        (if (cmp == GE_EXPR)
7162         (eq @2 @1))
7163        (if (cmp == LE_EXPR)
7164         { constant_boolean_node (true, type); })
7165        (if (cmp == LT_EXPR)
7166         (ne @2 @1))))
7167      (if (wi::to_wide (cst) == min)
7168       (switch
7169        (if (cmp == LT_EXPR)
7170         { constant_boolean_node (false, type); })
7171        (if (cmp == LE_EXPR)
7172         (eq @2 @1))
7173        (if (cmp == GE_EXPR)
7174         { constant_boolean_node (true, type); })
7175        (if (cmp == GT_EXPR)
7176         (ne @2 @1))))
7177      (if (wi::to_wide (cst) == max - 1)
7178       (switch
7179        (if (cmp == GT_EXPR)
7180         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7181                                     wide_int_to_tree (TREE_TYPE (cst),
7182                                                       wi::to_wide (cst)
7183                                                       + 1)); }))
7184        (if (cmp == LE_EXPR)
7185         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7186                                     wide_int_to_tree (TREE_TYPE (cst),
7187                                                       wi::to_wide (cst)
7188                                                       + 1)); }))))
7189      (if (wi::to_wide (cst) == min + 1)
7190       (switch
7191        (if (cmp == GE_EXPR)
7192         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7193                                     wide_int_to_tree (TREE_TYPE (cst),
7194                                                       wi::to_wide (cst)
7195                                                       - 1)); }))
7196        (if (cmp == LT_EXPR)
7197         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7198                                     wide_int_to_tree (TREE_TYPE (cst),
7199                                                       wi::to_wide (cst)
7200                                                       - 1)); }))))
7201      (if (wi::to_wide (cst) == signed_max
7202           && TYPE_UNSIGNED (arg1_type)
7203           && TYPE_MODE (arg1_type) != BLKmode
7204           /* We will flip the signedness of the comparison operator
7205              associated with the mode of @1, so the sign bit is
7206              specified by this mode.  Check that @1 is the signed
7207              max associated with this sign bit.  */
7208           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7209           /* signed_type does not work on pointer types.  */
7210           && INTEGRAL_TYPE_P (arg1_type))
7211       /* The following case also applies to X < signed_max+1
7212          and X >= signed_max+1 because previous transformations.  */
7213       (if (cmp == LE_EXPR || cmp == GT_EXPR)
7214        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7215         (switch
7216          (if (cst == @1 && cmp == LE_EXPR)
7217           (ge (convert:st @0) { build_zero_cst (st); }))
7218          (if (cst == @1 && cmp == GT_EXPR)
7219           (lt (convert:st @0) { build_zero_cst (st); }))
7220          (if (cmp == LE_EXPR)
7221           (ge (view_convert:st @0) { build_zero_cst (st); }))
7222          (if (cmp == GT_EXPR)
7223           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7225 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
7226 (simplify
7227  (lt:c @0 (convert (ne @0 integer_zerop)))
7228  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7229   { constant_boolean_node (false, type); }))
7231 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7232 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7233 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7234 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7235 (for outer (ne eq)
7236  (for inner (ne eq)
7237   (simplify
7238    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7239    (with {
7240      bool cst1 = integer_onep (@1);
7241      bool cst0 = integer_zerop (@1);
7242      bool innereq = inner == EQ_EXPR;
7243      bool outereq = outer == EQ_EXPR;
7244     }
7245     (switch
7246      (if (innereq ? cst0 : cst1)
7247       { constant_boolean_node (!outereq, type); })
7248      (if (innereq ? cst1 : cst0)
7249       (with {
7250         tree utype = unsigned_type_for (TREE_TYPE (@0));
7251         tree ucst1 = build_one_cst (utype);
7252        }
7253        (if (!outereq)
7254         (gt (convert:utype @0) { ucst1; })
7255         (le (convert:utype @0) { ucst1; })
7256        )
7257       )
7258      )
7259      (with {
7260        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7261       }
7262       (if (outereq)
7263        (eq @0 { value; })
7264        (ne @0 { value; })
7265       )
7266      )
7267     )
7268    )
7269   )
7273 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7274  /* If the second operand is NaN, the result is constant.  */
7275  (simplify
7276   (cmp @0 REAL_CST@1)
7277   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7278        && (cmp != LTGT_EXPR || ! flag_trapping_math))
7279    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7280                             ? false : true, type); })))
7282 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
7283 (simplify
7284   (unordered @0 @1)
7285   (switch
7286     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7287         { constant_boolean_node (true, type); })
7288     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7289         { constant_boolean_node (false, type); })))
7291 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
7292 (simplify
7293   (ordered @0 @1)
7294   (switch
7295     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7296         { constant_boolean_node (false, type); })
7297     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7298         { constant_boolean_node (true, type); })))
7300 /* bool_var != 0 becomes bool_var.  */
7301 (simplify
7302  (ne @0 integer_zerop)
7303  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7304       && types_match (type, TREE_TYPE (@0)))
7305   (non_lvalue @0)))
7306 /* bool_var == 1 becomes bool_var.  */
7307 (simplify
7308  (eq @0 integer_onep)
7309  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7310       && types_match (type, TREE_TYPE (@0)))
7311   (non_lvalue @0)))
7312 /* Do not handle
7313    bool_var == 0 becomes !bool_var or
7314    bool_var != 1 becomes !bool_var
7315    here because that only is good in assignment context as long
7316    as we require a tcc_comparison in GIMPLE_CONDs where we'd
7317    replace if (x == 0) with tem = ~x; if (tem != 0) which is
7318    clearly less optimal and which we'll transform again in forwprop.  */
7320 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7321    where ~Y + 1 == pow2 and Z = ~Y.  */
7322 (for cst (VECTOR_CST INTEGER_CST)
7323  (for cmp (eq ne)
7324       icmp (le gt)
7325   (simplify
7326    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7327     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7328      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7329       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7330                          ? optab_vector : optab_default;
7331               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7332        (if (target_supports_op_p (utype, icmp, optab)
7333             || (optimize_vectors_before_lowering_p ()
7334                 && (!target_supports_op_p (type, cmp, optab)
7335                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7336         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7337          (icmp @0 { csts; })
7338          (icmp (view_convert:utype @0) { csts; })))))))))
7340 /* When one argument is a constant, overflow detection can be simplified.
7341    Currently restricted to single use so as not to interfere too much with
7342    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7343    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
7344 (for cmp (lt le ge gt)
7345      out (gt gt le le)
7346  (simplify
7347   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7348   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7349        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7350        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7351        && wi::to_wide (@1) != 0
7352        && single_use (@2))
7353    (with {
7354      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7355      signop sign = TYPE_SIGN (TREE_TYPE (@0));
7356     }
7357     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7358                                 wi::max_value (prec, sign)
7359                                 - wi::to_wide (@1)); })))))
7361 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7362    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7363    expects the long form, so we restrict the transformation for now.  */
7364 (for cmp (gt le)
7365  (simplify
7366   (cmp:c (minus@2 @0 @1) @0)
7367   (if (single_use (@2)
7368        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7369        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7370    (cmp @1 @0))))
7372 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7373 (for cmp (ge lt)
7374  (simplify
7375   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7376    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7377         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7378     (cmp @1 @0))))
7380 /* Testing for overflow is unnecessary if we already know the result.  */
7381 /* A - B > A  */
7382 (for cmp (gt le)
7383      out (ne eq)
7384  (simplify
7385   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7386   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7387        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7388    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7389 /* A + B < A  */
7390 (for cmp (lt ge)
7391      out (ne eq)
7392  (simplify
7393   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7394   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7395        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7396    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7398 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7399    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7400 (for cmp (lt ge)
7401      out (ne eq)
7402  (simplify
7403   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7404   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7405    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7406     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7408 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7409    is at least twice as wide as type of A and B, simplify to
7410    __builtin_mul_overflow (A, B, <unused>).  */
7411 (for cmp (eq ne)
7412  (simplify
7413   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7414        integer_zerop)
7415   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7416        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7417        && TYPE_UNSIGNED (TREE_TYPE (@0))
7418        && (TYPE_PRECISION (TREE_TYPE (@3))
7419            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7420        && tree_fits_uhwi_p (@2)
7421        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7422        && types_match (@0, @1)
7423        && type_has_mode_precision_p (TREE_TYPE (@0))
7424        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7425            != CODE_FOR_nothing))
7426    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7427     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7429 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7430 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7431  (simplify
7432   (ovf (convert@2 @0) @1)
7433   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7434        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7435        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7436        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7437    (ovf @0 @1)))
7438  (simplify
7439   (ovf @1 (convert@2 @0))
7440   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7441        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7442        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7443        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7444    (ovf @1 @0))))
7446 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7447    are unsigned to x > (umax / cst).  Similarly for signed type, but
7448    in that case it needs to be outside of a range.  */
7449 (simplify
7450  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7451   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7452        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7453        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7454        && int_fits_type_p (@1, TREE_TYPE (@0)))
7455    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7456     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7457     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7458      (if (integer_minus_onep (@1))
7459       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7460       (with
7461        {
7462          tree div = fold_convert (TREE_TYPE (@0), @1);
7463          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7464                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7465          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7466                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7467          tree etype = range_check_type (TREE_TYPE (@0));
7468          if (etype)
7469            {
7470              if (wi::neg_p (wi::to_wide (div)))
7471                std::swap (lo, hi);
7472              lo = fold_convert (etype, lo);
7473              hi = fold_convert (etype, hi);
7474              hi = int_const_binop (MINUS_EXPR, hi, lo);
7475            }
7476        }
7477        (if (etype)
7478         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7480 /* Simplification of math builtins.  These rules must all be optimizations
7481    as well as IL simplifications.  If there is a possibility that the new
7482    form could be a pessimization, the rule should go in the canonicalization
7483    section that follows this one.
7485    Rules can generally go in this section if they satisfy one of
7486    the following:
7488    - the rule describes an identity
7490    - the rule replaces calls with something as simple as addition or
7491      multiplication
7493    - the rule contains unary calls only and simplifies the surrounding
7494      arithmetic.  (The idea here is to exclude non-unary calls in which
7495      one operand is constant and in which the call is known to be cheap
7496      when the operand has that value.)  */
7498 (if (flag_unsafe_math_optimizations)
7499  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7500  (simplify
7501   (mult (SQRT_ALL@1 @0) @1)
7502   (if (!tree_expr_maybe_signaling_nan_p (@0))
7503    @0))
7505  (for op (plus minus)
7506   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7507   (simplify
7508    (op (rdiv @0 @1)
7509        (rdiv @2 @1))
7510    (rdiv (op @0 @2) @1)))
7512  (for cmp (lt le gt ge)
7513       neg_cmp (gt ge lt le)
7514   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7515   (simplify
7516    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7517    (with
7518     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7519     (if (tem
7520          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7521               || (real_zerop (tem) && !real_zerop (@1))))
7522      (switch
7523       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7524        (cmp @0 { tem; }))
7525       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7526        (neg_cmp @0 { tem; })))))))
7528  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7529  (for root (SQRT CBRT)
7530   (simplify
7531    (mult (root:s @0) (root:s @1))
7532     (root (mult @0 @1))))
7534  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7535  (for exps (EXP EXP2 EXP10 POW10)
7536   (simplify
7537    (mult (exps:s @0) (exps:s @1))
7538     (exps (plus @0 @1))))
7540  /* Simplify a/root(b/c) into a*root(c/b).  */
7541  (for root (SQRT CBRT)
7542   (simplify
7543    (rdiv @0 (root:s (rdiv:s @1 @2)))
7544     (mult @0 (root (rdiv @2 @1)))))
7546  /* Simplify x/expN(y) into x*expN(-y).  */
7547  (for exps (EXP EXP2 EXP10 POW10)
7548   (simplify
7549    (rdiv @0 (exps:s @1))
7550     (mult @0 (exps (negate @1)))))
7552  (for logs (LOG LOG2 LOG10 LOG10)
7553       exps (EXP EXP2 EXP10 POW10)
7554   /* logN(expN(x)) -> x.  */
7555   (simplify
7556    (logs (exps @0))
7557    @0)
7558   /* expN(logN(x)) -> x.  */
7559   (simplify
7560    (exps (logs @0))
7561    @0))
7563  /* Optimize logN(func()) for various exponential functions.  We
7564     want to determine the value "x" and the power "exponent" in
7565     order to transform logN(x**exponent) into exponent*logN(x).  */
7566  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7567       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7568   (simplify
7569    (logs (exps @0))
7570    (if (SCALAR_FLOAT_TYPE_P (type))
7571     (with {
7572       tree x;
7573       switch (exps)
7574         {
7575         CASE_CFN_EXP:
7576           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7577           x = build_real_truncate (type, dconst_e ());
7578           break;
7579         CASE_CFN_EXP2:
7580           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7581           x = build_real (type, dconst2);
7582           break;
7583         CASE_CFN_EXP10:
7584         CASE_CFN_POW10:
7585           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7586           {
7587             REAL_VALUE_TYPE dconst10;
7588             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7589             x = build_real (type, dconst10);
7590           }
7591           break;
7592         default:
7593           gcc_unreachable ();
7594         }
7595       }
7596      (mult (logs { x; }) @0)))))
7598  (for logs (LOG LOG
7599             LOG2 LOG2
7600             LOG10 LOG10)
7601       exps (SQRT CBRT)
7602   (simplify
7603    (logs (exps @0))
7604    (if (SCALAR_FLOAT_TYPE_P (type))
7605     (with {
7606       tree x;
7607       switch (exps)
7608         {
7609         CASE_CFN_SQRT:
7610           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7611           x = build_real (type, dconsthalf);
7612           break;
7613         CASE_CFN_CBRT:
7614           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7615           x = build_real_truncate (type, dconst_third ());
7616           break;
7617         default:
7618           gcc_unreachable ();
7619         }
7620       }
7621      (mult { x; } (logs @0))))))
7623  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7624  (for logs (LOG LOG2 LOG10)
7625       pows (POW)
7626   (simplify
7627    (logs (pows @0 @1))
7628    (mult @1 (logs @0))))
7630  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7631     or if C is a positive power of 2,
7632     pow(C,x) -> exp2(log2(C)*x).  */
7633 #if GIMPLE
7634  (for pows (POW)
7635       exps (EXP)
7636       logs (LOG)
7637       exp2s (EXP2)
7638       log2s (LOG2)
7639   (simplify
7640    (pows REAL_CST@0 @1)
7641    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7642         && real_isfinite (TREE_REAL_CST_PTR (@0))
7643         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7644            the use_exp2 case until after vectorization.  It seems actually
7645            beneficial for all constants to postpone this until later,
7646            because exp(log(C)*x), while faster, will have worse precision
7647            and if x folds into a constant too, that is unnecessary
7648            pessimization.  */
7649         && canonicalize_math_after_vectorization_p ())
7650     (with {
7651        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7652        bool use_exp2 = false;
7653        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7654            && value->cl == rvc_normal)
7655          {
7656            REAL_VALUE_TYPE frac_rvt = *value;
7657            SET_REAL_EXP (&frac_rvt, 1);
7658            if (real_equal (&frac_rvt, &dconst1))
7659              use_exp2 = true;
7660          }
7661      }
7662      (if (!use_exp2)
7663       (if (optimize_pow_to_exp (@0, @1))
7664        (exps (mult (logs @0) @1)))
7665       (exp2s (mult (log2s @0) @1)))))))
7666 #endif
7668  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7669  (for pows (POW)
7670       exps (EXP EXP2 EXP10 POW10)
7671       logs (LOG LOG2 LOG10 LOG10)
7672   (simplify
7673    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7674    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7675         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7676     (exps (plus (mult (logs @0) @1) @2)))))
7678  (for sqrts (SQRT)
7679       cbrts (CBRT)
7680       pows (POW)
7681       exps (EXP EXP2 EXP10 POW10)
7682   /* sqrt(expN(x)) -> expN(x*0.5).  */
7683   (simplify
7684    (sqrts (exps @0))
7685    (exps (mult @0 { build_real (type, dconsthalf); })))
7686   /* cbrt(expN(x)) -> expN(x/3).  */
7687   (simplify
7688    (cbrts (exps @0))
7689    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7690   /* pow(expN(x), y) -> expN(x*y).  */
7691   (simplify
7692    (pows (exps @0) @1)
7693    (exps (mult @0 @1))))
7695  /* tan(atan(x)) -> x.  */
7696  (for tans (TAN)
7697       atans (ATAN)
7698   (simplify
7699    (tans (atans @0))
7700    @0)))
7702  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7703  (for sins (SIN)
7704       atans (ATAN)
7705       sqrts (SQRT)
7706       copysigns (COPYSIGN)
7707   (simplify
7708    (sins (atans:s @0))
7709    (with
7710      {
7711       REAL_VALUE_TYPE r_cst;
7712       build_sinatan_real (&r_cst, type);
7713       tree t_cst = build_real (type, r_cst);
7714       tree t_one = build_one_cst (type);
7715      }
7716     (if (SCALAR_FLOAT_TYPE_P (type))
7717      (cond (lt (abs @0) { t_cst; })
7718       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7719       (copysigns { t_one; } @0))))))
7721 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7722  (for coss (COS)
7723       atans (ATAN)
7724       sqrts (SQRT)
7725       copysigns (COPYSIGN)
7726   (simplify
7727    (coss (atans:s @0))
7728    (with
7729      {
7730       REAL_VALUE_TYPE r_cst;
7731       build_sinatan_real (&r_cst, type);
7732       tree t_cst = build_real (type, r_cst);
7733       tree t_one = build_one_cst (type);
7734       tree t_zero = build_zero_cst (type);
7735      }
7736     (if (SCALAR_FLOAT_TYPE_P (type))
7737      (cond (lt (abs @0) { t_cst; })
7738       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7739       (copysigns { t_zero; } @0))))))
7741  (if (!flag_errno_math)
7742   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7743   (for sinhs (SINH)
7744        atanhs (ATANH)
7745        sqrts (SQRT)
7746    (simplify
7747     (sinhs (atanhs:s @0))
7748     (with { tree t_one = build_one_cst (type); }
7749     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7751   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7752   (for coshs (COSH)
7753        atanhs (ATANH)
7754        sqrts (SQRT)
7755    (simplify
7756     (coshs (atanhs:s @0))
7757     (with { tree t_one = build_one_cst (type); }
7758     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7760 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7761 (simplify
7762  (CABS (complex:C @0 real_zerop@1))
7763  (abs @0))
7765 /* trunc(trunc(x)) -> trunc(x), etc.  */
7766 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7767  (simplify
7768   (fns (fns @0))
7769   (fns @0)))
7770 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7771 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7772  (simplify
7773   (fns integer_valued_real_p@0)
7774   @0))
7776 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7777 (simplify
7778  (HYPOT:c @0 real_zerop@1)
7779  (abs @0))
7781 /* pow(1,x) -> 1.  */
7782 (simplify
7783  (POW real_onep@0 @1)
7784  @0)
7786 (simplify
7787  /* copysign(x,x) -> x.  */
7788  (COPYSIGN_ALL @0 @0)
7789  @0)
7791 (simplify
7792  /* copysign(x,-x) -> -x.  */
7793  (COPYSIGN_ALL @0 (negate@1 @0))
7794  @1)
7796 (simplify
7797  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7798  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7799  (abs @0))
7801 (simplify
7802  /* fabs (copysign(x, y)) -> fabs (x).  */
7803  (abs (COPYSIGN_ALL @0 @1))
7804  (abs @0))
7806 (for scale (LDEXP SCALBN SCALBLN)
7807  /* ldexp(0, x) -> 0.  */
7808  (simplify
7809   (scale real_zerop@0 @1)
7810   @0)
7811  /* ldexp(x, 0) -> x.  */
7812  (simplify
7813   (scale @0 integer_zerop@1)
7814   @0)
7815  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7816  (simplify
7817   (scale REAL_CST@0 @1)
7818   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7819    @0)))
7821 /* Canonicalization of sequences of math builtins.  These rules represent
7822    IL simplifications but are not necessarily optimizations.
7824    The sincos pass is responsible for picking "optimal" implementations
7825    of math builtins, which may be more complicated and can sometimes go
7826    the other way, e.g. converting pow into a sequence of sqrts.
7827    We only want to do these canonicalizations before the pass has run.  */
7829 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7830  /* Simplify tan(x) * cos(x) -> sin(x). */
7831  (simplify
7832   (mult:c (TAN:s @0) (COS:s @0))
7833    (SIN @0))
7835  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7836  (simplify
7837   (mult:c @0 (POW:s @0 REAL_CST@1))
7838   (if (!TREE_OVERFLOW (@1))
7839    (POW @0 (plus @1 { build_one_cst (type); }))))
7841  /* Simplify sin(x) / cos(x) -> tan(x). */
7842  (simplify
7843   (rdiv (SIN:s @0) (COS:s @0))
7844    (TAN @0))
7846  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7847  (simplify
7848   (rdiv (SINH:s @0) (COSH:s @0))
7849    (TANH @0))
7851  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7852  (simplify
7853    (rdiv (TANH:s @0) (SINH:s @0))
7854    (rdiv {build_one_cst (type);} (COSH @0)))
7856  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7857  (simplify
7858   (rdiv (COS:s @0) (SIN:s @0))
7859    (rdiv { build_one_cst (type); } (TAN @0)))
7861  /* Simplify sin(x) / tan(x) -> cos(x). */
7862  (simplify
7863   (rdiv (SIN:s @0) (TAN:s @0))
7864   (if (! HONOR_NANS (@0)
7865        && ! HONOR_INFINITIES (@0))
7866    (COS @0)))
7868  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7869  (simplify
7870   (rdiv (TAN:s @0) (SIN:s @0))
7871   (if (! HONOR_NANS (@0)
7872        && ! HONOR_INFINITIES (@0))
7873    (rdiv { build_one_cst (type); } (COS @0))))
7875  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7876  (simplify
7877   (mult (POW:s @0 @1) (POW:s @0 @2))
7878    (POW @0 (plus @1 @2)))
7880  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7881  (simplify
7882   (mult (POW:s @0 @1) (POW:s @2 @1))
7883    (POW (mult @0 @2) @1))
7885  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7886  (simplify
7887   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7888    (POWI (mult @0 @2) @1))
7890  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7891  (simplify
7892   (rdiv (POW:s @0 REAL_CST@1) @0)
7893   (if (!TREE_OVERFLOW (@1))
7894    (POW @0 (minus @1 { build_one_cst (type); }))))
7896  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7897  (simplify
7898   (rdiv @0 (POW:s @1 @2))
7899    (mult @0 (POW @1 (negate @2))))
7901  (for sqrts (SQRT)
7902       cbrts (CBRT)
7903       pows (POW)
7904   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7905   (simplify
7906    (sqrts (sqrts @0))
7907    (pows @0 { build_real (type, dconst_quarter ()); }))
7908   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7909   (simplify
7910    (sqrts (cbrts @0))
7911    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7912   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7913   (simplify
7914    (cbrts (sqrts @0))
7915    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7916   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7917   (simplify
7918    (cbrts (cbrts tree_expr_nonnegative_p@0))
7919    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7920   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7921   (simplify
7922    (sqrts (pows @0 @1))
7923    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7924   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7925   (simplify
7926    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7927    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7928   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7929   (simplify
7930    (pows (sqrts @0) @1)
7931    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7932   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7933   (simplify
7934    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7935    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7936   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7937   (simplify
7938    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7939    (pows @0 (mult @1 @2))))
7941  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7942  (simplify
7943   (CABS (complex @0 @0))
7944   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7946  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7947  (simplify
7948   (HYPOT @0 @0)
7949   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7951  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7952  (for cexps (CEXP)
7953       exps (EXP)
7954       cexpis (CEXPI)
7955   (simplify
7956    (cexps compositional_complex@0)
7957    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7958     (complex
7959      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7960      (mult @1 (imagpart @2)))))))
7962 (if (canonicalize_math_p ())
7963  /* floor(x) -> trunc(x) if x is nonnegative.  */
7964  (for floors (FLOOR_ALL)
7965       truncs (TRUNC_ALL)
7966   (simplify
7967    (floors tree_expr_nonnegative_p@0)
7968    (truncs @0))))
7970 (match double_value_p
7971  @0
7972  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7973 (for froms (BUILT_IN_TRUNCL
7974             BUILT_IN_FLOORL
7975             BUILT_IN_CEILL
7976             BUILT_IN_ROUNDL
7977             BUILT_IN_NEARBYINTL
7978             BUILT_IN_RINTL)
7979      tos (BUILT_IN_TRUNC
7980           BUILT_IN_FLOOR
7981           BUILT_IN_CEIL
7982           BUILT_IN_ROUND
7983           BUILT_IN_NEARBYINT
7984           BUILT_IN_RINT)
7985  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7986  (if (optimize && canonicalize_math_p ())
7987   (simplify
7988    (froms (convert double_value_p@0))
7989    (convert (tos @0)))))
7991 (match float_value_p
7992  @0
7993  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7994 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7995             BUILT_IN_FLOORL BUILT_IN_FLOOR
7996             BUILT_IN_CEILL BUILT_IN_CEIL
7997             BUILT_IN_ROUNDL BUILT_IN_ROUND
7998             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7999             BUILT_IN_RINTL BUILT_IN_RINT)
8000      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
8001           BUILT_IN_FLOORF BUILT_IN_FLOORF
8002           BUILT_IN_CEILF BUILT_IN_CEILF
8003           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
8004           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
8005           BUILT_IN_RINTF BUILT_IN_RINTF)
8006  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
8007     if x is a float.  */
8008  (if (optimize && canonicalize_math_p ()
8009       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
8010   (simplify
8011    (froms (convert float_value_p@0))
8012    (convert (tos @0)))))
8014 #if GIMPLE
8015 (match float16_value_p
8016  @0
8017  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
8018 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
8019             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
8020             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
8021             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
8022             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
8023             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
8024             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
8025             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
8026      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
8027           IFN_FLOOR IFN_FLOOR IFN_FLOOR
8028           IFN_CEIL IFN_CEIL IFN_CEIL
8029           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
8030           IFN_ROUND IFN_ROUND IFN_ROUND
8031           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
8032           IFN_RINT IFN_RINT IFN_RINT
8033           IFN_SQRT IFN_SQRT IFN_SQRT)
8034  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
8035     if x is a _Float16.  */
8036  (simplify
8037    (convert (froms (convert float16_value_p@0)))
8038      (if (optimize
8039           && types_match (type, TREE_TYPE (@0))
8040           && direct_internal_fn_supported_p (as_internal_fn (tos),
8041                                              type, OPTIMIZE_FOR_BOTH))
8042        (tos @0))))
8044 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
8045    x,y is float value, similar for _Float16/double.  */
8046 (for copysigns (COPYSIGN_ALL)
8047  (simplify
8048   (convert (copysigns (convert@2 @0) (convert @1)))
8049    (if (optimize
8050        && !HONOR_SNANS (@2)
8051        && types_match (type, TREE_TYPE (@0))
8052        && types_match (type, TREE_TYPE (@1))
8053        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
8054        && direct_internal_fn_supported_p (IFN_COPYSIGN,
8055                                           type, OPTIMIZE_FOR_BOTH))
8056     (IFN_COPYSIGN @0 @1))))
8058 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
8059      tos (IFN_FMA IFN_FMA IFN_FMA)
8060  (simplify
8061   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
8062    (if (flag_unsafe_math_optimizations
8063        && optimize
8064        && FLOAT_TYPE_P (type)
8065        && FLOAT_TYPE_P (TREE_TYPE (@3))
8066        && types_match (type, TREE_TYPE (@0))
8067        && types_match (type, TREE_TYPE (@1))
8068        && types_match (type, TREE_TYPE (@2))
8069        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
8070        && direct_internal_fn_supported_p (as_internal_fn (tos),
8071                                           type, OPTIMIZE_FOR_BOTH))
8072     (tos @0 @1 @2))))
8074 (for maxmin (max min)
8075  (simplify
8076   (convert (maxmin (convert@2 @0) (convert @1)))
8077    (if (optimize
8078        && FLOAT_TYPE_P (type)
8079        && FLOAT_TYPE_P (TREE_TYPE (@2))
8080        && types_match (type, TREE_TYPE (@0))
8081        && types_match (type, TREE_TYPE (@1))
8082        && element_precision (type) < element_precision (TREE_TYPE (@2)))
8083     (maxmin @0 @1))))
8084 #endif
8086 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8087      tos (XFLOOR XCEIL XROUND XRINT)
8088  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
8089  (if (optimize && canonicalize_math_p ())
8090   (simplify
8091    (froms (convert double_value_p@0))
8092    (tos @0))))
8094 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8095             XFLOOR XCEIL XROUND XRINT)
8096      tos (XFLOORF XCEILF XROUNDF XRINTF)
8097  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8098     if x is a float.  */
8099  (if (optimize && canonicalize_math_p ())
8100   (simplify
8101    (froms (convert float_value_p@0))
8102    (tos @0))))
8104 (if (canonicalize_math_p ())
8105  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
8106  (for floors (IFLOOR LFLOOR LLFLOOR)
8107   (simplify
8108    (floors tree_expr_nonnegative_p@0)
8109    (fix_trunc @0))))
8111 (if (canonicalize_math_p ())
8112  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
8113  (for fns (IFLOOR LFLOOR LLFLOOR
8114            ICEIL LCEIL LLCEIL
8115            IROUND LROUND LLROUND)
8116   (simplify
8117    (fns integer_valued_real_p@0)
8118    (fix_trunc @0)))
8119  (if (!flag_errno_math)
8120   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
8121   (for rints (IRINT LRINT LLRINT)
8122    (simplify
8123     (rints integer_valued_real_p@0)
8124     (fix_trunc @0)))))
8126 (if (canonicalize_math_p ())
8127  (for ifn (IFLOOR ICEIL IROUND IRINT)
8128       lfn (LFLOOR LCEIL LROUND LRINT)
8129       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8130   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8131      sizeof (int) == sizeof (long).  */
8132   (if (TYPE_PRECISION (integer_type_node)
8133        == TYPE_PRECISION (long_integer_type_node))
8134    (simplify
8135     (ifn @0)
8136     (lfn:long_integer_type_node @0)))
8137   /* Canonicalize llround (x) to lround (x) on LP64 targets where
8138      sizeof (long long) == sizeof (long).  */
8139   (if (TYPE_PRECISION (long_long_integer_type_node)
8140        == TYPE_PRECISION (long_integer_type_node))
8141    (simplify
8142     (llfn @0)
8143     (lfn:long_integer_type_node @0)))))
8145 /* cproj(x) -> x if we're ignoring infinities.  */
8146 (simplify
8147  (CPROJ @0)
8148  (if (!HONOR_INFINITIES (type))
8149    @0))
8151 /* If the real part is inf and the imag part is known to be
8152    nonnegative, return (inf + 0i).  */
8153 (simplify
8154  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8155  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8156   { build_complex_inf (type, false); }))
8158 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
8159 (simplify
8160  (CPROJ (complex @0 REAL_CST@1))
8161  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8162   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8164 (for pows (POW)
8165      sqrts (SQRT)
8166      cbrts (CBRT)
8167  (simplify
8168   (pows @0 REAL_CST@1)
8169   (with {
8170     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8171     REAL_VALUE_TYPE tmp;
8172    }
8173    (switch
8174     /* pow(x,0) -> 1.  */
8175     (if (real_equal (value, &dconst0))
8176      { build_real (type, dconst1); })
8177     /* pow(x,1) -> x.  */
8178     (if (real_equal (value, &dconst1))
8179      @0)
8180     /* pow(x,-1) -> 1/x.  */
8181     (if (real_equal (value, &dconstm1))
8182      (rdiv { build_real (type, dconst1); } @0))
8183     /* pow(x,0.5) -> sqrt(x).  */
8184     (if (flag_unsafe_math_optimizations
8185          && canonicalize_math_p ()
8186          && real_equal (value, &dconsthalf))
8187      (sqrts @0))
8188     /* pow(x,1/3) -> cbrt(x).  */
8189     (if (flag_unsafe_math_optimizations
8190          && canonicalize_math_p ()
8191          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8192              real_equal (value, &tmp)))
8193      (cbrts @0))))))
8195 /* powi(1,x) -> 1.  */
8196 (simplify
8197  (POWI real_onep@0 @1)
8198  @0)
8200 (simplify
8201  (POWI @0 INTEGER_CST@1)
8202  (switch
8203   /* powi(x,0) -> 1.  */
8204   (if (wi::to_wide (@1) == 0)
8205    { build_real (type, dconst1); })
8206   /* powi(x,1) -> x.  */
8207   (if (wi::to_wide (@1) == 1)
8208    @0)
8209   /* powi(x,-1) -> 1/x.  */
8210   (if (wi::to_wide (@1) == -1)
8211    (rdiv { build_real (type, dconst1); } @0))))
8213 /* Narrowing of arithmetic and logical operations.
8215    These are conceptually similar to the transformations performed for
8216    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
8217    term we want to move all that code out of the front-ends into here.  */
8219 /* Convert (outertype)((innertype0)a+(innertype1)b)
8220    into ((newtype)a+(newtype)b) where newtype
8221    is the widest mode from all of these.  */
8222 (for op (plus minus mult rdiv)
8223  (simplify
8224    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8225    /* If we have a narrowing conversion of an arithmetic operation where
8226       both operands are widening conversions from the same type as the outer
8227       narrowing conversion.  Then convert the innermost operands to a
8228       suitable unsigned type (to avoid introducing undefined behavior),
8229       perform the operation and convert the result to the desired type.  */
8230    (if (INTEGRAL_TYPE_P (type)
8231         && op != MULT_EXPR
8232         && op != RDIV_EXPR
8233         /* We check for type compatibility between @0 and @1 below,
8234            so there's no need to check that @2/@4 are integral types.  */
8235         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8236         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8237         /* The precision of the type of each operand must match the
8238            precision of the mode of each operand, similarly for the
8239            result.  */
8240         && type_has_mode_precision_p (TREE_TYPE (@1))
8241         && type_has_mode_precision_p (TREE_TYPE (@2))
8242         && type_has_mode_precision_p (type)
8243         /* The inner conversion must be a widening conversion.  */
8244         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8245         && types_match (@1, type)
8246         && (types_match (@1, @2)
8247             /* Or the second operand is const integer or converted const
8248                integer from valueize.  */
8249             || poly_int_tree_p (@4)))
8250      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8251        (op @1 (convert @2))
8252        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8253         (convert (op (convert:utype @1)
8254                      (convert:utype @2)))))
8255      (if (FLOAT_TYPE_P (type)
8256           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8257                == DECIMAL_FLOAT_TYPE_P (type))
8258       (with { tree arg0 = strip_float_extensions (@1);
8259               tree arg1 = strip_float_extensions (@2);
8260               tree itype = TREE_TYPE (@0);
8261               tree ty1 = TREE_TYPE (arg0);
8262               tree ty2 = TREE_TYPE (arg1);
8263               enum tree_code code = TREE_CODE (itype); }
8264         (if (FLOAT_TYPE_P (ty1)
8265              && FLOAT_TYPE_P (ty2))
8266          (with { tree newtype = type;
8267                  if (TYPE_MODE (ty1) == SDmode
8268                      || TYPE_MODE (ty2) == SDmode
8269                      || TYPE_MODE (type) == SDmode)
8270                    newtype = dfloat32_type_node;
8271                  if (TYPE_MODE (ty1) == DDmode
8272                      || TYPE_MODE (ty2) == DDmode
8273                      || TYPE_MODE (type) == DDmode)
8274                    newtype = dfloat64_type_node;
8275                  if (TYPE_MODE (ty1) == TDmode
8276                      || TYPE_MODE (ty2) == TDmode
8277                      || TYPE_MODE (type) == TDmode)
8278                    newtype = dfloat128_type_node; }
8279           (if ((newtype == dfloat32_type_node
8280                 || newtype == dfloat64_type_node
8281                 || newtype == dfloat128_type_node)
8282               && newtype == type
8283               && types_match (newtype, type))
8284             (op (convert:newtype @1) (convert:newtype @2))
8285             (with { if (element_precision (ty1) > element_precision (newtype))
8286                       newtype = ty1;
8287                     if (element_precision (ty2) > element_precision (newtype))
8288                       newtype = ty2; }
8289                /* Sometimes this transformation is safe (cannot
8290                   change results through affecting double rounding
8291                   cases) and sometimes it is not.  If NEWTYPE is
8292                   wider than TYPE, e.g. (float)((long double)double
8293                   + (long double)double) converted to
8294                   (float)(double + double), the transformation is
8295                   unsafe regardless of the details of the types
8296                   involved; double rounding can arise if the result
8297                   of NEWTYPE arithmetic is a NEWTYPE value half way
8298                   between two representable TYPE values but the
8299                   exact value is sufficiently different (in the
8300                   right direction) for this difference to be
8301                   visible in ITYPE arithmetic.  If NEWTYPE is the
8302                   same as TYPE, however, the transformation may be
8303                   safe depending on the types involved: it is safe
8304                   if the ITYPE has strictly more than twice as many
8305                   mantissa bits as TYPE, can represent infinities
8306                   and NaNs if the TYPE can, and has sufficient
8307                   exponent range for the product or ratio of two
8308                   values representable in the TYPE to be within the
8309                   range of normal values of ITYPE.  */
8310               (if (element_precision (newtype) < element_precision (itype)
8311                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8312                        || target_supports_op_p (newtype, op, optab_default))
8313                    && (flag_unsafe_math_optimizations
8314                        || (element_precision (newtype) == element_precision (type)
8315                            && real_can_shorten_arithmetic (element_mode (itype),
8316                                                            element_mode (type))
8317                            && !excess_precision_type (newtype)))
8318                    && !types_match (itype, newtype))
8319                  (convert:type (op (convert:newtype @1)
8320                                    (convert:newtype @2)))
8321          )))) )
8322    ))
8325 /* This is another case of narrowing, specifically when there's an outer
8326    BIT_AND_EXPR which masks off bits outside the type of the innermost
8327    operands.   Like the previous case we have to convert the operands
8328    to unsigned types to avoid introducing undefined behavior for the
8329    arithmetic operation.  */
8330 (for op (minus plus)
8331  (simplify
8332   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8333   (if (INTEGRAL_TYPE_P (type)
8334        /* We check for type compatibility between @0 and @1 below,
8335           so there's no need to check that @1/@3 are integral types.  */
8336        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8337        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8338        /* The precision of the type of each operand must match the
8339           precision of the mode of each operand, similarly for the
8340           result.  */
8341        && type_has_mode_precision_p (TREE_TYPE (@0))
8342        && type_has_mode_precision_p (TREE_TYPE (@1))
8343        && type_has_mode_precision_p (type)
8344        /* The inner conversion must be a widening conversion.  */
8345        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8346        && types_match (@0, @1)
8347        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8348            <= TYPE_PRECISION (TREE_TYPE (@0)))
8349        && (wi::to_wide (@4)
8350            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8351                        true, TYPE_PRECISION (type))) == 0)
8352    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8353     (with { tree ntype = TREE_TYPE (@0); }
8354      (convert (bit_and (op @0 @1) (convert:ntype @4))))
8355     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8356      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8357                (convert:utype @4))))))))
8359 /* Transform (@0 < @1 and @0 < @2) to use min,
8360    (@0 > @1 and @0 > @2) to use max */
8361 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8362      op    (lt      le      gt      ge      lt      le      gt      ge     )
8363      ext   (min     min     max     max     max     max     min     min    )
8364  (simplify
8365   (logic (op:cs @0 @1) (op:cs @0 @2))
8366   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8367        && TREE_CODE (@0) != INTEGER_CST)
8368    (op @0 (ext @1 @2)))))
8370 /* Max<bool0, bool1> -> bool0 | bool1
8371    Min<bool0, bool1> -> bool0 & bool1 */
8372 (for op    (max     min)
8373      logic (bit_ior bit_and)
8374  (simplify
8375   (op zero_one_valued_p@0 zero_one_valued_p@1)
8376   (logic @0 @1)))
8378 /* signbit(x) != 0 ? -x : x -> abs(x)
8379    signbit(x) == 0 ? -x : x -> -abs(x) */
8380 (for sign (SIGNBIT)
8381  (for neeq (ne eq)
8382   (simplify
8383    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8384     (if (neeq == NE_EXPR)
8385      (abs @0)
8386      (negate (abs @0))))))
8388 (simplify
8389  /* signbit(x) -> 0 if x is nonnegative.  */
8390  (SIGNBIT tree_expr_nonnegative_p@0)
8391  { integer_zero_node; })
8393 (simplify
8394  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8395  (SIGNBIT @0)
8396  (if (!HONOR_SIGNED_ZEROS (@0))
8397   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8399 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8400 (for cmp (eq ne)
8401  (for op (plus minus)
8402       rop (minus plus)
8403   (simplify
8404    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8405    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8406         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8407         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8408         && !TYPE_SATURATING (TREE_TYPE (@0)))
8409     (with { tree res = int_const_binop (rop, @2, @1); }
8410      (if (TREE_OVERFLOW (res)
8411           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8412       { constant_boolean_node (cmp == NE_EXPR, type); }
8413       (if (single_use (@3))
8414        (cmp @0 { TREE_OVERFLOW (res)
8415                  ? drop_tree_overflow (res) : res; }))))))))
8416 (for cmp (lt le gt ge)
8417  (for op (plus minus)
8418       rop (minus plus)
8419   (simplify
8420    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8421    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8422         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8423     (with { tree res = int_const_binop (rop, @2, @1); }
8424      (if (TREE_OVERFLOW (res))
8425       {
8426         fold_overflow_warning (("assuming signed overflow does not occur "
8427                                 "when simplifying conditional to constant"),
8428                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8429         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8430         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8431         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8432                                   TYPE_SIGN (TREE_TYPE (@1)))
8433                         != (op == MINUS_EXPR);
8434         constant_boolean_node (less == ovf_high, type);
8435       }
8436       (if (single_use (@3))
8437        (with
8438         {
8439           fold_overflow_warning (("assuming signed overflow does not occur "
8440                                   "when changing X +- C1 cmp C2 to "
8441                                   "X cmp C2 -+ C1"),
8442                                  WARN_STRICT_OVERFLOW_COMPARISON);
8443         }
8444         (cmp @0 { res; })))))))))
8446 /* Canonicalizations of BIT_FIELD_REFs.  */
8448 (simplify
8449  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8450  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8452 (simplify
8453  (BIT_FIELD_REF (view_convert @0) @1 @2)
8454  (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8455       || type_has_mode_precision_p (TREE_TYPE (@0)))
8456   (BIT_FIELD_REF @0 @1 @2)))
8458 (simplify
8459  (BIT_FIELD_REF @0 @1 integer_zerop)
8460  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8461   (view_convert @0)))
8463 (simplify
8464  (BIT_FIELD_REF @0 @1 @2)
8465  (switch
8466   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8467        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8468    (switch
8469     (if (integer_zerop (@2))
8470      (view_convert (realpart @0)))
8471     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8472      (view_convert (imagpart @0)))))
8473   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8474        && INTEGRAL_TYPE_P (type)
8475        /* On GIMPLE this should only apply to register arguments.  */
8476        && (! GIMPLE || is_gimple_reg (@0))
8477        /* A bit-field-ref that referenced the full argument can be stripped.  */
8478        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8479             && integer_zerop (@2))
8480            /* Low-parts can be reduced to integral conversions.
8481               ???  The following doesn't work for PDP endian.  */
8482            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8483                /* But only do this after vectorization.  */
8484                && canonicalize_math_after_vectorization_p ()
8485                /* Don't even think about BITS_BIG_ENDIAN.  */
8486                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8487                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8488                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8489                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8490                                             - TYPE_PRECISION (type))
8491                                          : 0)) == 0)))
8492    (convert @0))))
8494 /* Simplify vector extracts.  */
8496 (simplify
8497  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8498  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8499       && tree_fits_uhwi_p (TYPE_SIZE (type))
8500       && ((tree_to_uhwi (TYPE_SIZE (type))
8501            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8502           || (VECTOR_TYPE_P (type)
8503               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8504                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8505   (with
8506    {
8507      tree ctor = (TREE_CODE (@0) == SSA_NAME
8508                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8509      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8510      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8511      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8512      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8513    }
8514    (if (n != 0
8515         && (idx % width) == 0
8516         && (n % width) == 0
8517         && known_le ((idx + n) / width,
8518                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8519     (with
8520      {
8521        idx = idx / width;
8522        n = n / width;
8523        /* Constructor elements can be subvectors.  */
8524        poly_uint64 k = 1;
8525        if (CONSTRUCTOR_NELTS (ctor) != 0)
8526          {
8527            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8528            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8529              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8530          }
8531        unsigned HOST_WIDE_INT elt, count, const_k;
8532      }
8533      (switch
8534       /* We keep an exact subset of the constructor elements.  */
8535       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8536        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8537         { build_zero_cst (type); }
8538         (if (count == 1)
8539          (if (elt < CONSTRUCTOR_NELTS (ctor))
8540           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8541           { build_zero_cst (type); })
8542          /* We don't want to emit new CTORs unless the old one goes away.
8543             ???  Eventually allow this if the CTOR ends up constant or
8544             uniform.  */
8545          (if (single_use (@0))
8546           (with
8547             {
8548               vec<constructor_elt, va_gc> *vals;
8549               vec_alloc (vals, count);
8550               bool constant_p = true;
8551               tree res;
8552               for (unsigned i = 0;
8553                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8554                 {
8555                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8556                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8557                   if (!CONSTANT_CLASS_P (e))
8558                     constant_p = false;
8559                 }
8560               tree evtype = (types_match (TREE_TYPE (type),
8561                                           TREE_TYPE (TREE_TYPE (ctor)))
8562                              ? type
8563                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8564                                                   count * k));
8565               /* We used to build a CTOR in the non-constant case here
8566                  but that's not a GIMPLE value.  We'd have to expose this
8567                  operation somehow so the code generation can properly
8568                  split it out to a separate stmt.  */
8569               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8570                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8571             }
8572             (if (res)
8573              (view_convert { res; })))))))
8574       /* The bitfield references a single constructor element.  */
8575       (if (k.is_constant (&const_k)
8576            && idx + n <= (idx / const_k + 1) * const_k)
8577        (switch
8578         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8579          { build_zero_cst (type); })
8580         (if (n == const_k)
8581          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8582         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8583                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8585 /* Simplify a bit extraction from a bit insertion for the cases with
8586    the inserted element fully covering the extraction or the insertion
8587    not touching the extraction.  */
8588 (simplify
8589  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8590  (with
8591   {
8592     unsigned HOST_WIDE_INT isize;
8593     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8594       isize = TYPE_PRECISION (TREE_TYPE (@1));
8595     else
8596       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8597   }
8598   (switch
8599    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8600          || type_has_mode_precision_p (TREE_TYPE (@1)))
8601         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8602         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8603                       wi::to_wide (@ipos) + isize))
8604     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8605                                                  wi::to_wide (@rpos)
8606                                                  - wi::to_wide (@ipos)); }))
8607    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8608         && compare_tree_int (@rsize, isize) == 0)
8609     (convert @1))
8610    (if (wi::geu_p (wi::to_wide (@ipos),
8611                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8612         || wi::geu_p (wi::to_wide (@rpos),
8613                       wi::to_wide (@ipos) + isize))
8614     (BIT_FIELD_REF @0 @rsize @rpos)))))
8616 /* Simplify vector inserts of other vector extracts to a permute.  */
8617 (simplify
8618  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8619  (if (VECTOR_TYPE_P (type)
8620       && (VECTOR_MODE_P (TYPE_MODE (type))
8621           || optimize_vectors_before_lowering_p ())
8622       && types_match (@0, @1)
8623       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8624       && TYPE_VECTOR_SUBPARTS (type).is_constant ()
8625       && multiple_p (wi::to_poly_offset (@rpos),
8626                      wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (type)))))
8627   (with
8628    {
8629      unsigned HOST_WIDE_INT elsz
8630        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8631      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8632      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8633      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8634      vec_perm_builder builder;
8635      builder.new_vector (nunits, nunits, 1);
8636      for (unsigned i = 0; i < nunits; ++i)
8637        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8638      vec_perm_indices sel (builder, 2, nunits);
8639    }
8640    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8641         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8642     (vec_perm @0 @1 { vec_perm_indices_to_tree
8643                         (build_vector_type (ssizetype, nunits), sel); })))))
8645 (if (canonicalize_math_after_vectorization_p ())
8646  (for fmas (FMA)
8647   (simplify
8648    (fmas:c (negate @0) @1 @2)
8649    (IFN_FNMA @0 @1 @2))
8650   (simplify
8651    (fmas @0 @1 (negate @2))
8652    (IFN_FMS @0 @1 @2))
8653   (simplify
8654    (fmas:c (negate @0) @1 (negate @2))
8655    (IFN_FNMS @0 @1 @2))
8656   (simplify
8657    (negate (fmas@3 @0 @1 @2))
8658    (if (single_use (@3))
8659     (IFN_FNMS @0 @1 @2))))
8661  (simplify
8662   (IFN_FMS:c (negate @0) @1 @2)
8663   (IFN_FNMS @0 @1 @2))
8664  (simplify
8665   (IFN_FMS @0 @1 (negate @2))
8666   (IFN_FMA @0 @1 @2))
8667  (simplify
8668   (IFN_FMS:c (negate @0) @1 (negate @2))
8669   (IFN_FNMA @0 @1 @2))
8670  (simplify
8671   (negate (IFN_FMS@3 @0 @1 @2))
8672    (if (single_use (@3))
8673     (IFN_FNMA @0 @1 @2)))
8675  (simplify
8676   (IFN_FNMA:c (negate @0) @1 @2)
8677   (IFN_FMA @0 @1 @2))
8678  (simplify
8679   (IFN_FNMA @0 @1 (negate @2))
8680   (IFN_FNMS @0 @1 @2))
8681  (simplify
8682   (IFN_FNMA:c (negate @0) @1 (negate @2))
8683   (IFN_FMS @0 @1 @2))
8684  (simplify
8685   (negate (IFN_FNMA@3 @0 @1 @2))
8686   (if (single_use (@3))
8687    (IFN_FMS @0 @1 @2)))
8689  (simplify
8690   (IFN_FNMS:c (negate @0) @1 @2)
8691   (IFN_FMS @0 @1 @2))
8692  (simplify
8693   (IFN_FNMS @0 @1 (negate @2))
8694   (IFN_FNMA @0 @1 @2))
8695  (simplify
8696   (IFN_FNMS:c (negate @0) @1 (negate @2))
8697   (IFN_FMA @0 @1 @2))
8698  (simplify
8699   (negate (IFN_FNMS@3 @0 @1 @2))
8700   (if (single_use (@3))
8701    (IFN_FMA @0 @1 @2))))
8703 /* CLZ simplifications.  */
8704 (for clz (CLZ)
8705  (for op (eq ne)
8706       cmp (lt ge)
8707   (simplify
8708    (op (clz:s@2 @0) INTEGER_CST@1)
8709    (if (integer_zerop (@1) && single_use (@2))
8710     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8711     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8712      (cmp (convert:stype @0) { build_zero_cst (stype); }))
8713     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8714     (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8715      (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8716 (for op (eq ne)
8717      cmp (lt ge)
8718  (simplify
8719   (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8720   (if (integer_zerop (@1) && single_use (@2))
8721    /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8722    (with { tree type0 = TREE_TYPE (@0);
8723            tree stype = signed_type_for (TREE_TYPE (@0));
8724            /* Punt if clz(0) == 0.  */
8725            if (integer_zerop (@3))
8726              stype = NULL_TREE;
8727          }
8728     (if (stype)
8729      (cmp (convert:stype @0) { build_zero_cst (stype); })))
8730    /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8731    (with { bool ok = true;
8732            tree type0 = TREE_TYPE (@0);
8733            /* Punt if clz(0) == prec - 1.  */
8734            if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8735              ok = false;
8736          }
8737     (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8738      (op @0 { build_one_cst (type0); }))))))
8740 /* CTZ simplifications.  */
8741 (for ctz (CTZ)
8742  (for op (ge gt le lt)
8743       cmp (eq eq ne ne)
8744   (simplify
8745    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8746    (op (ctz:s @0) INTEGER_CST@1)
8747     (with { bool ok = true;
8748             HOST_WIDE_INT val = 0;
8749             if (!tree_fits_shwi_p (@1))
8750               ok = false;
8751             else
8752               {
8753                 val = tree_to_shwi (@1);
8754                 /* Canonicalize to >= or <.  */
8755                 if (op == GT_EXPR || op == LE_EXPR)
8756                   {
8757                     if (val == HOST_WIDE_INT_MAX)
8758                       ok = false;
8759                     else
8760                       val++;
8761                   }
8762               }
8763             tree type0 = TREE_TYPE (@0);
8764             int prec = TYPE_PRECISION (type0);
8765           }
8766      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8767       (if (val <= 0)
8768        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8769        (if (val >= prec)
8770         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8771         (cmp (bit_and @0 { wide_int_to_tree (type0,
8772                                              wi::mask (val, false, prec)); })
8773              { build_zero_cst (type0); })))))))
8774  (for op (eq ne)
8775   (simplify
8776    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8777    (op (ctz:s @0) INTEGER_CST@1)
8778     (with { tree type0 = TREE_TYPE (@0);
8779             int prec = TYPE_PRECISION (type0);
8780           }
8781      (if (prec <= MAX_FIXED_MODE_SIZE)
8782       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8783        { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8784        (op (bit_and @0 { wide_int_to_tree (type0,
8785                                            wi::mask (tree_to_uhwi (@1) + 1,
8786                                                      false, prec)); })
8787            { wide_int_to_tree (type0,
8788                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8789                                                  false, prec)); })))))))
8790 (for op (ge gt le lt)
8791      cmp (eq eq ne ne)
8792  (simplify
8793   /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8794   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8795    (with { bool ok = true;
8796            HOST_WIDE_INT val = 0;
8797            if (!tree_fits_shwi_p (@1))
8798              ok = false;
8799            else
8800              {
8801                val = tree_to_shwi (@1);
8802                /* Canonicalize to >= or <.  */
8803                if (op == GT_EXPR || op == LE_EXPR)
8804                  {
8805                    if (val == HOST_WIDE_INT_MAX)
8806                      ok = false;
8807                    else
8808                      val++;
8809                  }
8810              }
8811            HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8812            tree type0 = TREE_TYPE (@0);
8813            int prec = TYPE_PRECISION (type0);
8814            if (prec > MAX_FIXED_MODE_SIZE)
8815              ok = false;
8816           }
8817      (if (val <= 0)
8818       (if (ok && zero_val >= val)
8819        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8820       (if (val >= prec)
8821        (if (ok && zero_val < val)
8822         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8823        (if (ok && (zero_val < 0 || zero_val >= prec))
8824         (cmp (bit_and @0 { wide_int_to_tree (type0,
8825                                              wi::mask (val, false, prec)); })
8826              { build_zero_cst (type0); })))))))
8827 (for op (eq ne)
8828  (simplify
8829   /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8830   (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8831    (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8832            tree type0 = TREE_TYPE (@0);
8833            int prec = TYPE_PRECISION (type0);
8834          }
8835     (if (prec <= MAX_FIXED_MODE_SIZE)
8836      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8837       (if (zero_val != wi::to_widest (@1))
8838        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8839       (if (zero_val < 0 || zero_val >= prec)
8840        (op (bit_and @0 { wide_int_to_tree (type0,
8841                                            wi::mask (tree_to_uhwi (@1) + 1,
8842                                                      false, prec)); })
8843            { wide_int_to_tree (type0,
8844                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8845                                                  false, prec)); })))))))
8847 #if GIMPLE
8848 /* ctz(ext(X)) == ctz(X).  Valid just for the UB at zero cases though.  */
8849 (simplify
8850   (CTZ (convert@1 @0))
8851   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8852        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8853        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8854    (with { combined_fn cfn = CFN_LAST;
8855            tree type0 = TREE_TYPE (@0);
8856            if (TREE_CODE (type0) == BITINT_TYPE)
8857              {
8858                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8859                  cfn = CFN_CTZ;
8860                else
8861                  type0
8862                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8863                                                      1);
8864              }
8865            type0 = unsigned_type_for (type0);
8866            if (cfn == CFN_LAST
8867                && direct_internal_fn_supported_p (IFN_CTZ, type0,
8868                                                   OPTIMIZE_FOR_BOTH))
8869              cfn = CFN_CTZ;
8870            if (cfn == CFN_LAST
8871                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8872                && !direct_internal_fn_supported_p (IFN_CTZ,
8873                                                    TREE_TYPE (@1),
8874                                                    OPTIMIZE_FOR_BOTH))
8875              {
8876                if (TYPE_PRECISION (type0)
8877                    == TYPE_PRECISION (unsigned_type_node))
8878                  cfn = CFN_BUILT_IN_CTZ;
8879                else if (TYPE_PRECISION (type0)
8880                         == TYPE_PRECISION (long_long_unsigned_type_node))
8881                  cfn = CFN_BUILT_IN_CTZLL;
8882              } }
8883     (if (cfn == CFN_CTZ)
8884      (IFN_CTZ (convert:type0 @0))
8885      (if (cfn == CFN_BUILT_IN_CTZ)
8886       (BUILT_IN_CTZ (convert:type0 @0))
8887       (if (cfn == CFN_BUILT_IN_CTZLL)
8888        (BUILT_IN_CTZLL (convert:type0 @0))))))))
8889 #endif
8891 /* POPCOUNT simplifications.  */
8892 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8893 (simplify
8894   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8895   (if (INTEGRAL_TYPE_P (type)
8896        && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8897                         widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8898            == 0))
8899    (with { tree utype = TREE_TYPE (@0);
8900            if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8901              utype = TREE_TYPE (@1); }
8902     (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8904 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8905 (for popcount (POPCOUNT)
8906   (for cmp (le eq ne gt)
8907        rep (eq eq ne ne)
8908     (simplify
8909       (cmp (popcount @0) integer_zerop)
8910       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8912 /* popcount(bswap(x)) is popcount(x).  */
8913 (for popcount (POPCOUNT)
8914   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8915               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8916     (simplify
8917       (popcount (convert?@0 (bswap:s@1 @2)))
8918       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8919            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8920         (with { tree type0 = TREE_TYPE (@0);
8921                 tree type1 = TREE_TYPE (@1);
8922                 unsigned int prec0 = TYPE_PRECISION (type0);
8923                 unsigned int prec1 = TYPE_PRECISION (type1); }
8924           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8925             (popcount (convert:type0 (convert:type1 @2)))))))))
8927 /* popcount(rotate(X Y)) is popcount(X).  */
8928 (for popcount (POPCOUNT)
8929   (for rot (lrotate rrotate)
8930     (simplify
8931       (popcount (convert?@0 (rot:s@1 @2 @3)))
8932       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8933            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8934            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8935         (with { tree type0 = TREE_TYPE (@0);
8936                 tree type1 = TREE_TYPE (@1);
8937                 unsigned int prec0 = TYPE_PRECISION (type0);
8938                 unsigned int prec1 = TYPE_PRECISION (type1); }
8939           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8940             (popcount (convert:type0 @2))))))))
8942 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8943 (simplify
8944   (bit_and (POPCOUNT @0) integer_onep)
8945   (PARITY @0))
8947 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8948 (simplify
8949   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8950   (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8952 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8953 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8954 (for popcount (POPCOUNT)
8955   (for log1 (bit_and bit_ior)
8956        log2 (bit_ior bit_and)
8957     (simplify
8958       (minus (plus:s (popcount:s @0) (popcount:s @1))
8959              (popcount:s (log1:cs @0 @1)))
8960       (popcount (log2 @0 @1)))
8961     (simplify
8962       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8963               (popcount:s @1))
8964       (popcount (log2 @0 @1)))))
8966 #if GIMPLE
8967 /* popcount(zext(X)) == popcount(X).  */
8968 (simplify
8969   (POPCOUNT (convert@1 @0))
8970   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8971        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8972        && TYPE_UNSIGNED (TREE_TYPE (@0))
8973        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8974    (with { combined_fn cfn = CFN_LAST;
8975            tree type0 = TREE_TYPE (@0);
8976            if (TREE_CODE (type0) == BITINT_TYPE)
8977              {
8978                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8979                  cfn = CFN_POPCOUNT;
8980                else
8981                  type0
8982                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8983                                                      1);
8984              }
8985            if (cfn == CFN_LAST
8986                && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8987                                                   OPTIMIZE_FOR_BOTH))
8988              cfn = CFN_POPCOUNT;
8989            if (cfn == CFN_LAST
8990                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8991                && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8992                                                    TREE_TYPE (@1),
8993                                                    OPTIMIZE_FOR_BOTH))
8994              {
8995                if (TYPE_PRECISION (type0)
8996                    == TYPE_PRECISION (unsigned_type_node))
8997                  cfn = CFN_BUILT_IN_POPCOUNT;
8998                else if (TYPE_PRECISION (type0)
8999                         == TYPE_PRECISION (long_long_unsigned_type_node))
9000                  cfn = CFN_BUILT_IN_POPCOUNTLL;
9001              } }
9002     (if (cfn == CFN_POPCOUNT)
9003      (IFN_POPCOUNT (convert:type0 @0))
9004      (if (cfn == CFN_BUILT_IN_POPCOUNT)
9005       (BUILT_IN_POPCOUNT (convert:type0 @0))
9006       (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
9007        (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
9008 #endif
9010 /* PARITY simplifications.  */
9011 /* parity(~X) is parity(X).  */
9012 (simplify
9013   (PARITY (bit_not @0))
9014   (PARITY @0))
9016 /* parity(bswap(x)) is parity(x).  */
9017 (for parity (PARITY)
9018   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
9019               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
9020     (simplify
9021       (parity (convert?@0 (bswap:s@1 @2)))
9022       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9023            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9024            && TYPE_PRECISION (TREE_TYPE (@0))
9025               >= TYPE_PRECISION (TREE_TYPE (@1)))
9026         (with { tree type0 = TREE_TYPE (@0);
9027                 tree type1 = TREE_TYPE (@1); }
9028           (parity (convert:type0 (convert:type1 @2))))))))
9030 /* parity(rotate(X Y)) is parity(X).  */
9031 (for parity (PARITY)
9032   (for rot (lrotate rrotate)
9033     (simplify
9034       (parity (convert?@0 (rot:s@1 @2 @3)))
9035       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9036            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9037            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
9038            && TYPE_PRECISION (TREE_TYPE (@0))
9039               >= TYPE_PRECISION (TREE_TYPE (@1)))
9040         (with { tree type0 = TREE_TYPE (@0); }
9041           (parity (convert:type0 @2)))))))
9043 /* parity(X)^parity(Y) is parity(X^Y).  */
9044 (simplify
9045   (bit_xor (PARITY:s @0) (PARITY:s @1))
9046   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
9047    (PARITY (bit_xor @0 @1))
9048    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9049         && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9050     (with { tree utype = TREE_TYPE (@0);
9051             if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
9052               utype = TREE_TYPE (@1); }
9053      (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
9055 #if GIMPLE
9056 /* parity(zext(X)) == parity(X).  */
9057 /* parity(sext(X)) == parity(X) if the difference in precision is even.  */
9058 (simplify
9059   (PARITY (convert@1 @0))
9060   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9061        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9062        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
9063        && (TYPE_UNSIGNED (TREE_TYPE (@0))
9064            || ((TYPE_PRECISION (TREE_TYPE (@1))
9065                 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
9066    (with { combined_fn cfn = CFN_LAST;
9067            tree type0 = TREE_TYPE (@0);
9068            if (TREE_CODE (type0) == BITINT_TYPE)
9069              {
9070                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9071                  cfn = CFN_PARITY;
9072                else
9073                  type0
9074                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9075                                                      1);
9076              }
9077            type0 = unsigned_type_for (type0);
9078            if (cfn == CFN_LAST
9079                && direct_internal_fn_supported_p (IFN_PARITY, type0,
9080                                                   OPTIMIZE_FOR_BOTH))
9081              cfn = CFN_PARITY;
9082            if (cfn == CFN_LAST
9083                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9084                && !direct_internal_fn_supported_p (IFN_PARITY,
9085                                                    TREE_TYPE (@1),
9086                                                    OPTIMIZE_FOR_BOTH))
9087              {
9088                if (TYPE_PRECISION (type0)
9089                    == TYPE_PRECISION (unsigned_type_node))
9090                  cfn = CFN_BUILT_IN_PARITY;
9091                else if (TYPE_PRECISION (type0)
9092                         == TYPE_PRECISION (long_long_unsigned_type_node))
9093                  cfn = CFN_BUILT_IN_PARITYLL;
9094              } }
9095     (if (cfn == CFN_PARITY)
9096      (IFN_PARITY (convert:type0 @0))
9097      (if (cfn == CFN_BUILT_IN_PARITY)
9098       (BUILT_IN_PARITY (convert:type0 @0))
9099       (if (cfn == CFN_BUILT_IN_PARITYLL)
9100        (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9101 #endif
9103 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9104 (for func (POPCOUNT BSWAP FFS PARITY)
9105  (simplify
9106   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9107   @3))
9109 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9110    where CST is precision-1. */
9111 (for func (CLRSB)
9112  (simplify
9113   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9114   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9115    @4)))
9117 #if GIMPLE
9118 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9119 (for func (CLZ)
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_CLZ;
9133                     }
9134                 }
9135             }
9136           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9137                                                    OPTIMIZE_FOR_BOTH)
9138                    && CLZ_DEFINED_VALUE_AT_ZERO
9139                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9140             ifn = IFN_CLZ;
9141         }
9142    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9143     (IFN_CLZ @3 @2)))))
9144 (simplify
9145  (cond (ne @0 integer_zerop@1) (IFN_CLZ (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_CLZ;
9150           else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9151                                                    OPTIMIZE_FOR_BOTH))
9152             ifn = IFN_CLZ;
9153         }
9154    (if (ifn == IFN_CLZ)
9155     (IFN_CLZ @3 @2))))
9157 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9158 (for func (CTZ)
9159  (simplify
9160   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9161   (with { int val;
9162           internal_fn ifn = IFN_LAST;
9163           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9164             {
9165               if (tree_fits_shwi_p (@2))
9166                 {
9167                   HOST_WIDE_INT valw = tree_to_shwi (@2);
9168                   if ((int) valw == valw)
9169                     {
9170                       val = valw;
9171                       ifn = IFN_CTZ;
9172                     }
9173                 }
9174             }
9175           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9176                                                    OPTIMIZE_FOR_BOTH)
9177                    && CTZ_DEFINED_VALUE_AT_ZERO
9178                         (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9179             ifn = IFN_CTZ;
9180         }
9181    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9182     (IFN_CTZ @3 @2)))))
9183 (simplify
9184  (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9185   (with { int val;
9186           internal_fn ifn = IFN_LAST;
9187           if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9188             ifn = IFN_CTZ;
9189           else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9190                                                    OPTIMIZE_FOR_BOTH))
9191             ifn = IFN_CTZ;
9192         }
9193    (if (ifn == IFN_CTZ)
9194     (IFN_CTZ @3 @2))))
9195 #endif
9197 /* Common POPCOUNT/PARITY simplifications.  */
9198 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
9199 (for pfun (POPCOUNT PARITY)
9200   (simplify
9201     (pfun @0)
9202     (if (INTEGRAL_TYPE_P (type))
9203      (with { wide_int nz = tree_nonzero_bits (@0); }
9204        (switch
9205          (if (nz == 1)
9206            (convert @0))
9207          (if (wi::popcount (nz) == 1)
9208            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9209              (convert (rshift:utype (convert:utype @0)
9210                                     { build_int_cst (integer_type_node,
9211                                                      wi::ctz (nz)); })))))))))
9213 #if GIMPLE
9214 /* 64- and 32-bits branchless implementations of popcount are detected:
9216    int popcount64c (uint64_t x)
9217    {
9218      x -= (x >> 1) & 0x5555555555555555ULL;
9219      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9220      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9221      return (x * 0x0101010101010101ULL) >> 56;
9222    }
9224    int popcount32c (uint32_t x)
9225    {
9226      x -= (x >> 1) & 0x55555555;
9227      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9228      x = (x + (x >> 4)) & 0x0f0f0f0f;
9229      return (x * 0x01010101) >> 24;
9230    }  */
9231 (simplify
9232  (rshift
9233   (mult
9234    (bit_and
9235     (plus:c
9236      (rshift @8 INTEGER_CST@5)
9237       (plus:c@8
9238        (bit_and @6 INTEGER_CST@7)
9239         (bit_and
9240          (rshift
9241           (minus@6 @0
9242            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9243           INTEGER_CST@10)
9244          INTEGER_CST@9)))
9245     INTEGER_CST@3)
9246    INTEGER_CST@2)
9247   INTEGER_CST@1)
9248   /* Check constants and optab.  */
9249   (with { unsigned prec = TYPE_PRECISION (type);
9250           int shift = (64 - prec) & 63;
9251           unsigned HOST_WIDE_INT c1
9252             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9253           unsigned HOST_WIDE_INT c2
9254             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9255           unsigned HOST_WIDE_INT c3
9256             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9257           unsigned HOST_WIDE_INT c4
9258             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9259    }
9260    (if (prec >= 16
9261         && prec <= 64
9262         && pow2p_hwi (prec)
9263         && TYPE_UNSIGNED (type)
9264         && integer_onep (@4)
9265         && wi::to_widest (@10) == 2
9266         && wi::to_widest (@5) == 4
9267         && wi::to_widest (@1) == prec - 8
9268         && tree_to_uhwi (@2) == c1
9269         && tree_to_uhwi (@3) == c2
9270         && tree_to_uhwi (@9) == c3
9271         && tree_to_uhwi (@7) == c3
9272         && tree_to_uhwi (@11) == c4)
9273     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9274                                          OPTIMIZE_FOR_BOTH))
9275      (convert (IFN_POPCOUNT:type @0))
9276      /* Try to do popcount in two halves.  PREC must be at least
9277         five bits for this to work without extension before adding.  */
9278      (with {
9279        tree half_type = NULL_TREE;
9280        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9281        int half_prec = 8;
9282        if (m.exists ()
9283            && m.require () != TYPE_MODE (type))
9284          {
9285            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9286            half_type = build_nonstandard_integer_type (half_prec, 1);
9287          }
9288        gcc_assert (half_prec > 2);
9289       }
9290       (if (half_type != NULL_TREE
9291            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9292                                               OPTIMIZE_FOR_BOTH))
9293        (convert (plus
9294          (IFN_POPCOUNT:half_type (convert @0))
9295          (IFN_POPCOUNT:half_type (convert (rshift @0
9296             { build_int_cst (integer_type_node, half_prec); } )))))))))))
9298 /* __builtin_ffs needs to deal on many targets with the possible zero
9299    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
9300    should lead to better code.  */
9301 (simplify
9302  (FFS tree_expr_nonzero_p@0)
9303  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9304       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9305                                          OPTIMIZE_FOR_SPEED))
9306   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9307    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9308 #endif
9310 (for ffs (FFS)
9311  /* __builtin_ffs (X) == 0 -> X == 0.
9312     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
9313  (for cmp (eq ne)
9314   (simplify
9315    (cmp (ffs@2 @0) INTEGER_CST@1)
9316     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9317      (switch
9318       (if (integer_zerop (@1))
9319        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9320       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9321        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9322       (if (single_use (@2))
9323        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9324                                             wi::mask (tree_to_uhwi (@1),
9325                                                       false, prec)); })
9326             { wide_int_to_tree (TREE_TYPE (@0),
9327                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9328                                                   false, prec)); }))))))
9330  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
9331  (for cmp (gt le)
9332       cmp2 (ne eq)
9333       cmp3 (eq ne)
9334       bit_op (bit_and bit_ior)
9335   (simplify
9336    (cmp (ffs@2 @0) INTEGER_CST@1)
9337     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9338      (switch
9339       (if (integer_zerop (@1))
9340        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9341       (if (tree_int_cst_sgn (@1) < 0)
9342        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9343       (if (wi::to_widest (@1) >= prec)
9344        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9345       (if (wi::to_widest (@1) == prec - 1)
9346        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9347                                     wi::shifted_mask (prec - 1, 1,
9348                                                       false, prec)); }))
9349       (if (single_use (@2))
9350        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9351                (cmp3 (bit_and @0
9352                               { wide_int_to_tree (TREE_TYPE (@0),
9353                                                   wi::mask (tree_to_uhwi (@1),
9354                                                   false, prec)); })
9355                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
9357 #if GIMPLE
9358 /* ffs(ext(X)) == ffs(X).  */
9359 (simplify
9360   (FFS (convert@1 @0))
9361   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9362        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9363        && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9364    (with { combined_fn cfn = CFN_LAST;
9365            tree type0 = TREE_TYPE (@0);
9366            if (TREE_CODE (type0) == BITINT_TYPE)
9367              {
9368                if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9369                  cfn = CFN_FFS;
9370                else
9371                  type0
9372                    = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9373                                                      0);
9374              }
9375            type0 = signed_type_for (type0);
9376            if (cfn == CFN_LAST
9377                && direct_internal_fn_supported_p (IFN_FFS, type0,
9378                                                   OPTIMIZE_FOR_BOTH))
9379              cfn = CFN_FFS;
9380            if (cfn == CFN_LAST
9381                && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9382                && !direct_internal_fn_supported_p (IFN_FFS,
9383                                                    TREE_TYPE (@1),
9384                                                    OPTIMIZE_FOR_BOTH))
9385              {
9386                if (TYPE_PRECISION (type0)
9387                    == TYPE_PRECISION (integer_type_node))
9388                  cfn = CFN_BUILT_IN_FFS;
9389                else if (TYPE_PRECISION (type0)
9390                         == TYPE_PRECISION (long_long_integer_type_node))
9391                  cfn = CFN_BUILT_IN_FFSLL;
9392              } }
9393     (if (cfn == CFN_FFS)
9394      (IFN_FFS (convert:type0 @0))
9395      (if (cfn == CFN_BUILT_IN_FFS)
9396       (BUILT_IN_FFS (convert:type0 @0))
9397       (if (cfn == CFN_BUILT_IN_FFSLL)
9398        (BUILT_IN_FFSLL (convert:type0 @0))))))))
9399 #endif
9401 #if GIMPLE
9403 /* Simplify:
9404      a = op a1
9405      r = cond ? a : b
9406      --> r = .COND_FN (cond, a, b)
9407 and,
9408     a = op a1
9409     r = cond ? b : a
9410     --> r = .COND_FN (~cond, b, a).  */
9412 (for uncond_op (UNCOND_UNARY)
9413      cond_op (COND_UNARY)
9414  (simplify
9415   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9416    (with { tree op_type = TREE_TYPE (@3); }
9417     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9418         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9419      (cond_op @0 (view_convert @1) @2))))
9420  (simplify
9421   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9422    (with { tree op_type = TREE_TYPE (@3); }
9423     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9424         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9425      (cond_op (bit_not @0) (view_convert @2) @1)))))
9427 (for uncond_op (UNCOND_UNARY)
9428      cond_op (COND_LEN_UNARY)
9429  (simplify
9430   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9431    (with { tree op_type = TREE_TYPE (@3); }
9432     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9433         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9434      (cond_op @0 (view_convert @1) @2 @4 @5))))
9435  (simplify
9436   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9437    (with { tree op_type = TREE_TYPE (@3); }
9438     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9439         && is_truth_type_for (op_type, TREE_TYPE (@0)))
9440      (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9442 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
9443 (simplify
9444  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9445  (if (canonicalize_math_after_vectorization_p ()
9446       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9447       && is_truth_type_for (type, TREE_TYPE (@0)))
9448  (if (integer_all_onesp (@1) && integer_zerop (@2))
9449   (IFN_COND_NOT @0 @3 @3))
9450   (if (integer_all_onesp (@2) && integer_zerop (@1))
9451    (IFN_COND_NOT (bit_not @0) @3 @3))))
9453 /* Simplify:
9455      a = a1 op a2
9456      r = c ? a : b;
9458    to:
9460      r = c ? a1 op a2 : b;
9462    if the target can do it in one go.  This makes the operation conditional
9463    on c, so could drop potentially-trapping arithmetic, but that's a valid
9464    simplification if the result of the operation isn't needed.
9466    Avoid speculatively generating a stand-alone vector comparison
9467    on targets that might not support them.  Any target implementing
9468    conditional internal functions must support the same comparisons
9469    inside and outside a VEC_COND_EXPR.  */
9471 (for uncond_op (UNCOND_BINARY)
9472      cond_op (COND_BINARY)
9473  (simplify
9474   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9475   (with { tree op_type = TREE_TYPE (@4); }
9476    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9477         && is_truth_type_for (op_type, TREE_TYPE (@0))
9478         && single_use (@4))
9479     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9480  (simplify
9481   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9482   (with { tree op_type = TREE_TYPE (@4); }
9483    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9484         && is_truth_type_for (op_type, TREE_TYPE (@0))
9485         && single_use (@4))
9486     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9488 (for uncond_op (UNCOND_BINARY)
9489      cond_op (COND_LEN_BINARY)
9490  (simplify
9491   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9492   (with { tree op_type = TREE_TYPE (@4); }
9493    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9494         && is_truth_type_for (op_type, TREE_TYPE (@0))
9495         && single_use (@4))
9496     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9497  (simplify
9498   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9499   (with { tree op_type = TREE_TYPE (@4); }
9500    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9501         && is_truth_type_for (op_type, TREE_TYPE (@0))
9502         && single_use (@4))
9503     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9505 /* Same for ternary operations.  */
9506 (for uncond_op (UNCOND_TERNARY)
9507      cond_op (COND_TERNARY)
9508  (simplify
9509   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9510   (with { tree op_type = TREE_TYPE (@5); }
9511    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9512         && is_truth_type_for (op_type, TREE_TYPE (@0))
9513         && single_use (@5))
9514     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9515  (simplify
9516   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9517   (with { tree op_type = TREE_TYPE (@5); }
9518    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9519         && is_truth_type_for (op_type, TREE_TYPE (@0))
9520         && single_use (@5))
9521     (view_convert (cond_op (bit_not @0) @2 @3 @4
9522                   (view_convert:op_type @1)))))))
9524 (for uncond_op (UNCOND_TERNARY)
9525      cond_op (COND_LEN_TERNARY)
9526  (simplify
9527   (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9528   (with { tree op_type = TREE_TYPE (@5); }
9529    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9530         && is_truth_type_for (op_type, TREE_TYPE (@0))
9531         && single_use (@5))
9532     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9533  (simplify
9534   (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9535   (with { tree op_type = TREE_TYPE (@5); }
9536    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9537         && is_truth_type_for (op_type, TREE_TYPE (@0))
9538         && single_use (@5))
9539     (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9540 #endif
9542 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9543    "else" value of an IFN_COND_*.  */
9544 (for cond_op (COND_BINARY)
9545  (simplify
9546   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9547   (with { tree op_type = TREE_TYPE (@3); }
9548    (if (element_precision (type) == element_precision (op_type))
9549     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9550  (simplify
9551   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9552   (with { tree op_type = TREE_TYPE (@5); }
9553    (if (inverse_conditions_p (@0, @2)
9554         && element_precision (type) == element_precision (op_type))
9555     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9557 /* Same for ternary operations.  */
9558 (for cond_op (COND_TERNARY)
9559  (simplify
9560   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9561   (with { tree op_type = TREE_TYPE (@4); }
9562    (if (element_precision (type) == element_precision (op_type))
9563     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9564  (simplify
9565   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9566   (with { tree op_type = TREE_TYPE (@6); }
9567    (if (inverse_conditions_p (@0, @2)
9568         && element_precision (type) == element_precision (op_type))
9569     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9571 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9572    "else" value of an IFN_COND_LEN_*.  */
9573 (for cond_len_op (COND_LEN_BINARY)
9574  (simplify
9575   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9576   (with { tree op_type = TREE_TYPE (@3); }
9577    (if (element_precision (type) == element_precision (op_type))
9578     (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9579  (simplify
9580   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9581   (with { tree op_type = TREE_TYPE (@5); }
9582    (if (inverse_conditions_p (@0, @2)
9583         && element_precision (type) == element_precision (op_type))
9584     (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9586 /* Same for ternary operations.  */
9587 (for cond_len_op (COND_LEN_TERNARY)
9588  (simplify
9589   (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9590   (with { tree op_type = TREE_TYPE (@4); }
9591    (if (element_precision (type) == element_precision (op_type))
9592     (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9593  (simplify
9594   (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9595   (with { tree op_type = TREE_TYPE (@6); }
9596    (if (inverse_conditions_p (@0, @2)
9597         && element_precision (type) == element_precision (op_type))
9598     (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9600 /* Detect simplication for a conditional reduction where
9602    a = mask1 ? b : 0
9603    c = mask2 ? d + a : d
9605    is turned into
9607    c = mask1 && mask2 ? d + b : d.  */
9608 (simplify
9609   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9610    (if (ANY_INTEGRAL_TYPE_P (type)
9611         || (FLOAT_TYPE_P (type)
9612             && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9613    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9615 /* Detect simplication for a conditional length reduction where
9617    a = mask ? b : 0
9618    c = i < len + bias ? d + a : d
9620    is turned into
9622    c = mask && i < len + bias ? d + b : d.  */
9623 (simplify
9624   (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9625    (if (ANY_INTEGRAL_TYPE_P (type)
9626         || (FLOAT_TYPE_P (type)
9627             && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9628     (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9630 /* Detect simplification for vector condition folding where
9632   c = mask1 ? (masked_op mask2 a b els) : els
9634   into
9636   c = masked_op (mask1 & mask2) a b els
9638   where the operation can be partially applied to one operand. */
9640 (for cond_op (COND_BINARY)
9641  (simplify
9642   (vec_cond @0
9643    (cond_op:s @1 @2 @3 @4) @4)
9644   (cond_op (bit_and @1 @0) @2 @3 @4)))
9646 /* And same for ternary expressions.  */
9648 (for cond_op (COND_TERNARY)
9649  (simplify
9650   (vec_cond @0
9651    (cond_op:s @1 @2 @3 @4 @5) @5)
9652   (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9654 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9655    expressions like:
9657    A: (@0 + @1 < @2) | (@2 + @1 < @0)
9658    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9660    If pointers are known not to wrap, B checks whether @1 bytes starting
9661    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9662    bytes.  A is more efficiently tested as:
9664    A: (sizetype) (@0 + @1 - @2) > @1 * 2
9666    The equivalent expression for B is given by replacing @1 with @1 - 1:
9668    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9670    @0 and @2 can be swapped in both expressions without changing the result.
9672    The folds rely on sizetype's being unsigned (which is always true)
9673    and on its being the same width as the pointer (which we have to check).
9675    The fold replaces two pointer_plus expressions, two comparisons and
9676    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9677    the best case it's a saving of two operations.  The A fold retains one
9678    of the original pointer_pluses, so is a win even if both pointer_pluses
9679    are used elsewhere.  The B fold is a wash if both pointer_pluses are
9680    used elsewhere, since all we end up doing is replacing a comparison with
9681    a pointer_plus.  We do still apply the fold under those circumstances
9682    though, in case applying it to other conditions eventually makes one of the
9683    pointer_pluses dead.  */
9684 (for ior (truth_orif truth_or bit_ior)
9685  (for cmp (le lt)
9686   (simplify
9687    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9688         (cmp:cs (pointer_plus@4 @2 @1) @0))
9689    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9690         && TYPE_OVERFLOW_WRAPS (sizetype)
9691         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9692     /* Calculate the rhs constant.  */
9693     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9694             offset_int rhs = off * 2; }
9695      /* Always fails for negative values.  */
9696      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9697       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9698          pick a canonical order.  This increases the chances of using the
9699          same pointer_plus in multiple checks.  */
9700       (with { bool swap_p = tree_swap_operands_p (@0, @2);
9701               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9702        (if (cmp == LT_EXPR)
9703         (gt (convert:sizetype
9704              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9705                                      { swap_p ? @0 : @2; }))
9706             { rhs_tree; })
9707         (gt (convert:sizetype
9708              (pointer_diff:ssizetype
9709               (pointer_plus { swap_p ? @2 : @0; }
9710                             { wide_int_to_tree (sizetype, off); })
9711               { swap_p ? @0 : @2; }))
9712             { rhs_tree; })))))))))
9714 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9715    element of @1.  */
9716 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9717  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9718   (with { int i = single_nonzero_element (@1); }
9719    (if (i >= 0)
9720     (with { tree elt = vector_cst_elt (@1, i);
9721             tree elt_type = TREE_TYPE (elt);
9722             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9723             tree size = bitsize_int (elt_bits);
9724             tree pos = bitsize_int (elt_bits * i); }
9725      (view_convert
9726       (bit_and:elt_type
9727        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9728        { elt; })))))))
9730 /* Fold reduction of a single nonzero element constructor.  */
9731 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9732   (simplify (reduc (CONSTRUCTOR@0))
9733     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9734                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9735             tree elt = ctor_single_nonzero_element (ctor); }
9736       (if (elt
9737            && !HONOR_SNANS (type)
9738            && !HONOR_SIGNED_ZEROS (type))
9739         { elt; }))))
9741 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
9742 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9743             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9744      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9745   (simplify (reduc (op @0 VECTOR_CST@1))
9746     (op (reduc:type @0) (reduc:type @1))))
9748 /* Simplify vector floating point operations of alternating sub/add pairs
9749    into using an fneg of a wider element type followed by a normal add.
9750    under IEEE 754 the fneg of the wider type will negate every even entry
9751    and when doing an add we get a sub of the even and add of every odd
9752    elements.  */
9753 (for plusminus (plus minus)
9754      minusplus (minus plus)
9755  (simplify
9756   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9757    (if (!VECTOR_INTEGER_TYPE_P (type)
9758         && !FLOAT_WORDS_BIG_ENDIAN
9759         /* plus is commutative, while minus is not, so :c can't be used.
9760            Do equality comparisons by hand and at the end pick the operands
9761            from the minus.  */
9762         && (operand_equal_p (@0, @2, 0)
9763             ? operand_equal_p (@1, @3, 0)
9764             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9765    (with
9766     {
9767       /* Build a vector of integers from the tree mask.  */
9768       vec_perm_builder builder;
9769     }
9770     (if (tree_to_vec_perm_builder (&builder, @4))
9771      (with
9772       {
9773         /* Create a vec_perm_indices for the integer vector.  */
9774         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9775         vec_perm_indices sel (builder, 2, nelts);
9776         machine_mode vec_mode = TYPE_MODE (type);
9777         machine_mode wide_mode;
9778         scalar_mode wide_elt_mode;
9779         poly_uint64 wide_nunits;
9780         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9781       }
9782       (if (VECTOR_MODE_P (vec_mode)
9783            && sel.series_p (0, 2, 0, 2)
9784            && sel.series_p (1, 2, nelts + 1, 2)
9785            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9786            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9787            && related_vector_mode (vec_mode, wide_elt_mode,
9788                                    wide_nunits).exists (&wide_mode))
9789        (with
9790         {
9791           tree stype
9792             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9793                                               TYPE_UNSIGNED (type));
9794           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9796           /* The format has to be a non-extended ieee format.  */
9797           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9798           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9799         }
9800         (if (TYPE_MODE (stype) != BLKmode
9801              && VECTOR_TYPE_P (ntype)
9802              && fmt_old != NULL
9803              && fmt_new != NULL)
9804          (with
9805           {
9806             /* If the target doesn't support v1xx vectors, try using
9807                scalar mode xx instead.  */
9808             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9809                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9810               ntype = stype;
9811           }
9812           (if (fmt_new->signbit_rw
9813                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9814                && fmt_new->signbit_rw == fmt_new->signbit_ro
9815                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9816                                                  TYPE_MODE (type), ALL_REGS)
9817                && ((optimize_vectors_before_lowering_p ()
9818                     && VECTOR_TYPE_P (ntype))
9819                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9820            (if (plusminus == PLUS_EXPR)
9821             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9822             (minus @0 (view_convert:type
9823                         (negate (view_convert:ntype @1))))))))))))))))
9825 (simplify
9826  (vec_perm @0 @1 VECTOR_CST@2)
9827  (with
9828   {
9829     tree op0 = @0, op1 = @1, op2 = @2;
9830     machine_mode result_mode = TYPE_MODE (type);
9831     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9833     /* Build a vector of integers from the tree mask.  */
9834     vec_perm_builder builder;
9835   }
9836   (if (tree_to_vec_perm_builder (&builder, op2))
9837    (with
9838     {
9839       /* Create a vec_perm_indices for the integer vector.  */
9840       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9841       bool single_arg = (op0 == op1);
9842       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9843     }
9844     (if (sel.series_p (0, 1, 0, 1))
9845      { op0; }
9846      (if (sel.series_p (0, 1, nelts, 1))
9847       { op1; }
9848       (with
9849        {
9850          if (!single_arg)
9851            {
9852              if (sel.all_from_input_p (0))
9853                op1 = op0;
9854              else if (sel.all_from_input_p (1))
9855                {
9856                  op0 = op1;
9857                  sel.rotate_inputs (1);
9858                }
9859              else if (known_ge (poly_uint64 (sel[0]), nelts))
9860                {
9861                  std::swap (op0, op1);
9862                  sel.rotate_inputs (1);
9863                }
9864            }
9865          gassign *def;
9866          tree cop0 = op0, cop1 = op1;
9867          if (TREE_CODE (op0) == SSA_NAME
9868              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9869              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9870            cop0 = gimple_assign_rhs1 (def);
9871          if (TREE_CODE (op1) == SSA_NAME
9872              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9873              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9874            cop1 = gimple_assign_rhs1 (def);
9875          tree t;
9876        }
9877        (if ((TREE_CODE (cop0) == VECTOR_CST
9878              || TREE_CODE (cop0) == CONSTRUCTOR)
9879             && (TREE_CODE (cop1) == VECTOR_CST
9880                 || TREE_CODE (cop1) == CONSTRUCTOR)
9881             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9882         { t; }
9883         (with
9884          {
9885            bool changed = (op0 == op1 && !single_arg);
9886            tree ins = NULL_TREE;
9887            unsigned at = 0;
9889            /* See if the permutation is performing a single element
9890               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9891               in that case.  But only if the vector mode is supported,
9892               otherwise this is invalid GIMPLE.  */
9893            if (op_mode != BLKmode
9894                && (TREE_CODE (cop0) == VECTOR_CST
9895                    || TREE_CODE (cop0) == CONSTRUCTOR
9896                    || TREE_CODE (cop1) == VECTOR_CST
9897                    || TREE_CODE (cop1) == CONSTRUCTOR))
9898              {
9899                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9900                if (insert_first_p)
9901                  {
9902                    /* After canonicalizing the first elt to come from the
9903                       first vector we only can insert the first elt from
9904                       the first vector.  */
9905                    at = 0;
9906                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9907                      op0 = op1;
9908                  }
9909                /* The above can fail for two-element vectors which always
9910                   appear to insert the first element, so try inserting
9911                   into the second lane as well.  For more than two
9912                   elements that's wasted time.  */
9913                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9914                  {
9915                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9916                    for (at = 0; at < encoded_nelts; ++at)
9917                      if (maybe_ne (sel[at], at))
9918                        break;
9919                    if (at < encoded_nelts
9920                        && (known_eq (at + 1, nelts)
9921                            || sel.series_p (at + 1, 1, at + 1, 1)))
9922                      {
9923                        if (known_lt (poly_uint64 (sel[at]), nelts))
9924                          ins = fold_read_from_vector (cop0, sel[at]);
9925                        else
9926                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9927                      }
9928                  }
9929              }
9931            /* Generate a canonical form of the selector.  */
9932            if (!ins && sel.encoding () != builder)
9933              {
9934                /* Some targets are deficient and fail to expand a single
9935                   argument permutation while still allowing an equivalent
9936                   2-argument version.  */
9937                tree oldop2 = op2;
9938                if (sel.ninputs () == 2
9939                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9940                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9941                else
9942                  {
9943                    vec_perm_indices sel2 (builder, 2, nelts);
9944                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9945                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9946                    else
9947                      /* Not directly supported with either encoding,
9948                         so use the preferred form.  */
9949                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9950                  }
9951                if (!operand_equal_p (op2, oldop2, 0))
9952                  changed = true;
9953              }
9954          }
9955          (if (ins)
9956           (bit_insert { op0; } { ins; }
9957            { bitsize_int (at * vector_element_bits (type)); })
9958           (if (changed)
9959            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9961 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9963 (match vec_same_elem_p
9964  (vec_duplicate @0))
9966 (match vec_same_elem_p
9967  CONSTRUCTOR@0
9968  (if (TREE_CODE (@0) == SSA_NAME
9969       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9971 (match vec_same_elem_p
9972  @0
9973  (if (uniform_vector_p (@0))))
9976 (simplify
9977  (vec_perm vec_same_elem_p@0 @0 @1)
9978  (if (types_match (type, TREE_TYPE (@0)))
9979   @0
9980   (with
9981    {
9982      tree elem = uniform_vector_p (@0);
9983    }
9984    (if (elem)
9985     { build_vector_from_val (type, elem); }))))
9987 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9988 (simplify
9989  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9990  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9991   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9992 (simplify
9993  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9994  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9995   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9998 /* Merge
9999      c = VEC_PERM_EXPR <a, b, VCST0>;
10000      d = VEC_PERM_EXPR <c, c, VCST1>;
10001    to
10002      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
10004 (simplify
10005  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
10006  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10007   (with
10008    {
10009      machine_mode result_mode = TYPE_MODE (type);
10010      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10011      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10012      vec_perm_builder builder0;
10013      vec_perm_builder builder1;
10014      vec_perm_builder builder2 (nelts, nelts, 1);
10015    }
10016    (if (tree_to_vec_perm_builder (&builder0, @3)
10017         && tree_to_vec_perm_builder (&builder1, @4))
10018     (with
10019      {
10020        vec_perm_indices sel0 (builder0, 2, nelts);
10021        vec_perm_indices sel1 (builder1, 1, nelts);
10023        for (int i = 0; i < nelts; i++)
10024          builder2.quick_push (sel0[sel1[i].to_constant ()]);
10026        vec_perm_indices sel2 (builder2, 2, nelts);
10028        tree op0 = NULL_TREE;
10029        /* If the new VEC_PERM_EXPR can't be handled but both
10030           original VEC_PERM_EXPRs can, punt.
10031           If one or both of the original VEC_PERM_EXPRs can't be
10032           handled and the new one can't be either, don't increase
10033           number of VEC_PERM_EXPRs that can't be handled.  */
10034        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10035            || (single_use (@0)
10036                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10037                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10038                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10039          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10040      }
10041      (if (op0)
10042       (vec_perm @1 @2 { op0; })))))))
10044 /* Merge
10045      c = VEC_PERM_EXPR <a, b, VCST0>;
10046      d = VEC_PERM_EXPR <x, c, VCST1>;
10047    to
10048      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
10049    when all elements from a or b are replaced by the later
10050    permutation.  */
10052 (simplify
10053  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
10054  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10055   (with
10056    {
10057      machine_mode result_mode = TYPE_MODE (type);
10058      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10059      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10060      vec_perm_builder builder0;
10061      vec_perm_builder builder1;
10062      vec_perm_builder builder2 (nelts, nelts, 2);
10063    }
10064    (if (tree_to_vec_perm_builder (&builder0, @3)
10065         && tree_to_vec_perm_builder (&builder1, @4))
10066     (with
10067      {
10068        vec_perm_indices sel0 (builder0, 2, nelts);
10069        vec_perm_indices sel1 (builder1, 2, nelts);
10070        bool use_1 = false, use_2 = false;
10072        for (int i = 0; i < nelts; i++)
10073          {
10074            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10075              builder2.quick_push (sel1[i]);
10076            else
10077              {
10078                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10079                                     .to_constant ()];
10080                if (known_lt (j, sel0.nelts_per_input ()))
10081                  use_1 = true;
10082                else
10083                  {
10084                    use_2 = true;
10085                    j -= sel0.nelts_per_input ();
10086                  }
10087                builder2.quick_push (j + sel1.nelts_per_input ());
10088              }
10089          }
10090      }
10091      (if (use_1 ^ use_2)
10092       (with
10093        {
10094          vec_perm_indices sel2 (builder2, 2, nelts);
10095          tree op0 = NULL_TREE;
10096          /* If the new VEC_PERM_EXPR can't be handled but both
10097             original VEC_PERM_EXPRs can, punt.
10098             If one or both of the original VEC_PERM_EXPRs can't be
10099             handled and the new one can't be either, don't increase
10100             number of VEC_PERM_EXPRs that can't be handled.  */
10101          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10102              || (single_use (@0)
10103                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10104                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10105                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10106            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10107        }
10108        (if (op0)
10109         (switch
10110          (if (use_1)
10111           (vec_perm @5 @1 { op0; }))
10112          (if (use_2)
10113           (vec_perm @5 @2 { op0; })))))))))))
10115 /* And the case with swapped outer permute sources.  */
10117 (simplify
10118  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10119  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10120   (with
10121    {
10122      machine_mode result_mode = TYPE_MODE (type);
10123      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10124      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10125      vec_perm_builder builder0;
10126      vec_perm_builder builder1;
10127      vec_perm_builder builder2 (nelts, nelts, 2);
10128    }
10129    (if (tree_to_vec_perm_builder (&builder0, @3)
10130         && tree_to_vec_perm_builder (&builder1, @4))
10131     (with
10132      {
10133        vec_perm_indices sel0 (builder0, 2, nelts);
10134        vec_perm_indices sel1 (builder1, 2, nelts);
10135        bool use_1 = false, use_2 = false;
10137        for (int i = 0; i < nelts; i++)
10138          {
10139            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10140              builder2.quick_push (sel1[i]);
10141            else
10142              {
10143                poly_uint64 j = sel0[sel1[i].to_constant ()];
10144                if (known_lt (j, sel0.nelts_per_input ()))
10145                  use_1 = true;
10146                else
10147                  {
10148                    use_2 = true;
10149                    j -= sel0.nelts_per_input ();
10150                  }
10151                builder2.quick_push (j);
10152              }
10153          }
10154      }
10155      (if (use_1 ^ use_2)
10156       (with
10157        {
10158          vec_perm_indices sel2 (builder2, 2, nelts);
10159          tree op0 = NULL_TREE;
10160          /* If the new VEC_PERM_EXPR can't be handled but both
10161             original VEC_PERM_EXPRs can, punt.
10162             If one or both of the original VEC_PERM_EXPRs can't be
10163             handled and the new one can't be either, don't increase
10164             number of VEC_PERM_EXPRs that can't be handled.  */
10165          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10166              || (single_use (@0)
10167                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10168                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10169                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10170            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10171        }
10172        (if (op0)
10173         (switch
10174          (if (use_1)
10175           (vec_perm @1 @5 { op0; }))
10176          (if (use_2)
10177           (vec_perm @2 @5 { op0; })))))))))))
10180 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10181    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10182    constant which when multiplied by a power of 2 contains a unique value
10183    in the top 5 or 6 bits.  This is then indexed into a table which maps it
10184    to the number of trailing zeroes.  */
10185 (match (ctz_table_index @1 @2 @3)
10186   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10188 (match (cond_expr_convert_p @0 @2 @3 @6)
10189  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10190   (if (INTEGRAL_TYPE_P (type)
10191        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10192        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10193        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10194        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10195        && TYPE_PRECISION (TREE_TYPE (@0))
10196           == TYPE_PRECISION (TREE_TYPE (@2))
10197        && TYPE_PRECISION (TREE_TYPE (@0))
10198           == TYPE_PRECISION (TREE_TYPE (@3))
10199        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10200           signess when convert is truncation, but not ok for extension since
10201           it's sign_extend vs zero_extend.  */
10202        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10203            || (TYPE_UNSIGNED (TREE_TYPE (@2))
10204                == TYPE_UNSIGNED (TREE_TYPE (@3))))
10205        && single_use (@4)
10206        && single_use (@5))))
10208 (for bit_op (bit_and bit_ior bit_xor)
10209  (match (bitwise_induction_p @0 @2 @3)
10210   (bit_op:c
10211    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10212    @3)))
10214 (match (bitwise_induction_p @0 @2 @3)
10215  (bit_not
10216   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10218 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
10219    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
10220 (simplify
10221   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10222   (with { auto i = wi::neg (wi::to_wide (@2)); }
10223   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
10224     (if (wi::popcount (i) == 1
10225          && (wi::to_wide (@1)) == (i - 1))
10226       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10227         (bit_and @0 @1)
10228       (cond (le @0 @1) @0 (bit_and @0 @1))))))
10230 /* -x & 1 -> x & 1.  */
10231 (simplify 
10232  (bit_and (negate @0) integer_onep@1)
10233  (if (!TYPE_OVERFLOW_SANITIZED (type))
10234   (bit_and @0 @1)))
10236 /* `-a` is just `a` if the type is 1bit wide or when converting
10237    to a 1bit type; similar to the above transformation of `(-x)&1`.
10238    This is used mostly with the transformation of
10239    `a ? ~b : b` into `(-a)^b`.
10240    It also can show up with bitfields.  */
10241 (simplify
10242  (convert? (negate @0))
10243  (if (INTEGRAL_TYPE_P (type)
10244       && TYPE_PRECISION (type) == 1
10245       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10246   (convert @0)))
10248 /* Optimize
10249    c1 = VEC_PERM_EXPR (a, a, mask)
10250    c2 = VEC_PERM_EXPR (b, b, mask)
10251    c3 = c1 op c2
10252    -->
10253    c = a op b
10254    c3 = VEC_PERM_EXPR (c, c, mask)
10255    For all integer non-div operations.  */
10256 (for op (plus minus mult bit_and bit_ior bit_xor
10257          lshift rshift)
10258  (simplify
10259   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10260    (if (VECTOR_INTEGER_TYPE_P (type))
10261     (vec_perm (op@3 @0 @1) @3 @2))))
10263 /* Similar for float arithmetic when permutation constant covers
10264    all vector elements.  */
10265 (for op (plus minus mult)
10266  (simplify
10267   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10268    (if (VECTOR_FLOAT_TYPE_P (type)
10269         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10270     (with
10271      {
10272        tree perm_cst = @2;
10273        vec_perm_builder builder;
10274        bool full_perm_p = false;
10275        if (tree_to_vec_perm_builder (&builder, perm_cst))
10276          {
10277            unsigned HOST_WIDE_INT nelts;
10279            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10280            /* Create a vec_perm_indices for the VECTOR_CST.  */
10281            vec_perm_indices sel (builder, 1, nelts);
10283            /* Check if perm indices covers all vector elements.  */
10284            if (sel.encoding ().encoded_full_vector_p ())
10285              {
10286                auto_sbitmap seen (nelts);
10287                bitmap_clear (seen);
10289                unsigned HOST_WIDE_INT count = 0, i;
10291                for (i = 0; i < nelts; i++)
10292                  {
10293                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10294                     break;
10295                    count++;
10296                  }
10297                full_perm_p = count == nelts;
10298              }
10299          }
10300       }
10301       (if (full_perm_p)
10302         (vec_perm (op@3 @0 @1) @3 @2))))))