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