Prefer open-coding vector integer division
[official-gcc.git] / gcc / match.pd
blobf08571ef28c6bc95a78f906631341ac92d6b975e
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2    This file is consumed by genmatch which produces gimple-match.c
3    and generic-match.c from it.
5    Copyright (C) 2014-2018 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    CONSTANT_CLASS_P
33    tree_expr_nonnegative_p
34    tree_expr_nonzero_p
35    integer_valued_real_p
36    integer_pow2p
37    HONOR_NANS)
39 /* Operator lists.  */
40 (define_operator_list tcc_comparison
41   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison
43   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
44 (define_operator_list inverted_tcc_comparison_with_nans
45   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
46 (define_operator_list swapped_tcc_comparison
47   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
48 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
49 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
51 #include "cfn-operators.pd"
53 /* Define operand lists for math rounding functions {,i,l,ll}FN,
54    where the versions prefixed with "i" return an int, those prefixed with
55    "l" return a long and those prefixed with "ll" return a long long.
57    Also define operand lists:
59      X<FN>F for all float functions, in the order i, l, ll
60      X<FN> for all double functions, in the same order
61      X<FN>L for all long double functions, in the same order.  */
62 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
63   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
64                                  BUILT_IN_L##FN##F \
65                                  BUILT_IN_LL##FN##F) \
66   (define_operator_list X##FN BUILT_IN_I##FN \
67                               BUILT_IN_L##FN \
68                               BUILT_IN_LL##FN) \
69   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
70                                  BUILT_IN_L##FN##L \
71                                  BUILT_IN_LL##FN##L)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
78 /* Binary operations and their associated IFN_COND_* function.  */
79 (define_operator_list UNCOND_BINARY
80   plus minus
81   min max
82   bit_and bit_ior bit_xor)
83 (define_operator_list COND_BINARY
84   IFN_COND_ADD IFN_COND_SUB
85   IFN_COND_MIN IFN_COND_MAX
86   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
87     
88 /* As opposed to convert?, this still creates a single pattern, so
89    it is not a suitable replacement for convert? in all cases.  */
90 (match (nop_convert @0)
91  (convert @0)
92  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
93 (match (nop_convert @0)
94  (view_convert @0)
95  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
96       && known_eq (TYPE_VECTOR_SUBPARTS (type),
97                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
98       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
99 /* This one has to be last, or it shadows the others.  */
100 (match (nop_convert @0)
101  @0) 
103 /* Simplifications of operations with one constant operand and
104    simplifications to constants or single values.  */
106 (for op (plus pointer_plus minus bit_ior bit_xor)
107   (simplify
108     (op @0 integer_zerop)
109     (non_lvalue @0)))
111 /* 0 +p index -> (type)index */
112 (simplify
113  (pointer_plus integer_zerop @1)
114  (non_lvalue (convert @1)))
116 /* ptr - 0 -> (type)ptr */
117 (simplify
118  (pointer_diff @0 integer_zerop)
119  (convert @0))
121 /* See if ARG1 is zero and X + ARG1 reduces to X.
122    Likewise if the operands are reversed.  */
123 (simplify
124  (plus:c @0 real_zerop@1)
125  (if (fold_real_zero_addition_p (type, @1, 0))
126   (non_lvalue @0)))
128 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
129 (simplify
130  (minus @0 real_zerop@1)
131  (if (fold_real_zero_addition_p (type, @1, 1))
132   (non_lvalue @0)))
134 /* Simplify x - x.
135    This is unsafe for certain floats even in non-IEEE formats.
136    In IEEE, it is unsafe because it does wrong for NaNs.
137    Also note that operand_equal_p is always false if an operand
138    is volatile.  */
139 (simplify
140  (minus @0 @0)
141  (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
142   { build_zero_cst (type); }))
143 (simplify
144  (pointer_diff @@0 @0)
145  { build_zero_cst (type); })
147 (simplify
148  (mult @0 integer_zerop@1)
149  @1)
151 /* Maybe fold x * 0 to 0.  The expressions aren't the same
152    when x is NaN, since x * 0 is also NaN.  Nor are they the
153    same in modes with signed zeros, since multiplying a
154    negative value by 0 gives -0, not +0.  */
155 (simplify
156  (mult @0 real_zerop@1)
157  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
158   @1))
160 /* In IEEE floating point, x*1 is not equivalent to x for snans.
161    Likewise for complex arithmetic with signed zeros.  */
162 (simplify
163  (mult @0 real_onep)
164  (if (!HONOR_SNANS (type)
165       && (!HONOR_SIGNED_ZEROS (type)
166           || !COMPLEX_FLOAT_TYPE_P (type)))
167   (non_lvalue @0)))
169 /* Transform x * -1.0 into -x.  */
170 (simplify
171  (mult @0 real_minus_onep)
172   (if (!HONOR_SNANS (type)
173        && (!HONOR_SIGNED_ZEROS (type)
174            || !COMPLEX_FLOAT_TYPE_P (type)))
175    (negate @0)))
177 (for cmp (gt ge lt le)
178      outp (convert convert negate negate)
179      outn (negate negate convert convert)
180  /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
181  /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
182  /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
183  /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
184  (simplify
185   (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
186   (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
187        && types_match (type, TREE_TYPE (@0)))
188    (switch
189     (if (types_match (type, float_type_node))
190      (BUILT_IN_COPYSIGNF @1 (outp @0)))
191     (if (types_match (type, double_type_node))
192      (BUILT_IN_COPYSIGN @1 (outp @0)))
193     (if (types_match (type, long_double_type_node))
194      (BUILT_IN_COPYSIGNL @1 (outp @0))))))
195  /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
196  /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
197  /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
198  /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
199  (simplify
200   (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
201   (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
202        && types_match (type, TREE_TYPE (@0)))
203    (switch
204     (if (types_match (type, float_type_node))
205      (BUILT_IN_COPYSIGNF @1 (outn @0)))
206     (if (types_match (type, double_type_node))
207      (BUILT_IN_COPYSIGN @1 (outn @0)))
208     (if (types_match (type, long_double_type_node))
209      (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
211 /* Transform X * copysign (1.0, X) into abs(X). */
212 (simplify
213  (mult:c @0 (COPYSIGN_ALL real_onep @0))
214  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
215   (abs @0)))
217 /* Transform X * copysign (1.0, -X) into -abs(X). */
218 (simplify
219  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
220  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
221   (negate (abs @0))))
223 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
224 (simplify
225  (COPYSIGN_ALL REAL_CST@0 @1)
226  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
227   (COPYSIGN_ALL (negate @0) @1)))
229 /* X * 1, X / 1 -> X.  */
230 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
231   (simplify
232     (op @0 integer_onep)
233     (non_lvalue @0)))
235 /* (A / (1 << B)) -> (A >> B).
236    Only for unsigned A.  For signed A, this would not preserve rounding
237    toward zero.
238    For example: (-1 / ( 1 << B)) !=  -1 >> B.  */
239 (simplify
240  (trunc_div @0 (lshift integer_onep@1 @2))
241  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
242       && (!VECTOR_TYPE_P (type)
243           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
244           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
245   (rshift @0 @2)))
247 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
248    undefined behavior in constexpr evaluation, and assuming that the division
249    traps enables better optimizations than these anyway.  */
250 (for div (trunc_div ceil_div floor_div round_div exact_div)
251  /* 0 / X is always zero.  */
252  (simplify
253   (div integer_zerop@0 @1)
254   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
255   (if (!integer_zerop (@1))
256    @0))
257   /* X / -1 is -X.  */
258  (simplify
259    (div @0 integer_minus_onep@1)
260    (if (!TYPE_UNSIGNED (type))
261     (negate @0)))
262  /* X / X is one.  */
263  (simplify
264   (div @0 @0)
265   /* But not for 0 / 0 so that we can get the proper warnings and errors.
266      And not for _Fract types where we can't build 1.  */
267   (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
268    { build_one_cst (type); }))
269  /* X / abs (X) is X < 0 ? -1 : 1.  */ 
270  (simplify
271    (div:C @0 (abs @0))
272    (if (INTEGRAL_TYPE_P (type)
273         && TYPE_OVERFLOW_UNDEFINED (type))
274     (cond (lt @0 { build_zero_cst (type); })
275           { build_minus_one_cst (type); } { build_one_cst (type); })))
276  /* X / -X is -1.  */
277  (simplify
278    (div:C @0 (negate @0))
279    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
280         && TYPE_OVERFLOW_UNDEFINED (type))
281     { build_minus_one_cst (type); })))
283 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
284    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
285 (simplify
286  (floor_div @0 @1)
287  (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
288       && TYPE_UNSIGNED (type))
289   (trunc_div @0 @1)))
291 /* Combine two successive divisions.  Note that combining ceil_div
292    and floor_div is trickier and combining round_div even more so.  */
293 (for div (trunc_div exact_div)
294  (simplify
295   (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
296   (with {
297     bool overflow_p;
298     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
299                             TYPE_SIGN (type), &overflow_p);
300    }
301    (if (!overflow_p)
302     (div @0 { wide_int_to_tree (type, mul); })
303     (if (TYPE_UNSIGNED (type)
304          || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
305      { build_zero_cst (type); })))))
307 /* Combine successive multiplications.  Similar to above, but handling
308    overflow is different.  */
309 (simplify
310  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
311  (with {
312    bool overflow_p;
313    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
314                            TYPE_SIGN (type), &overflow_p);
315   }
316   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
317      otherwise undefined overflow implies that @0 must be zero.  */
318   (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
319    (mult @0 { wide_int_to_tree (type, mul); }))))
321 /* Optimize A / A to 1.0 if we don't care about
322    NaNs or Infinities.  */
323 (simplify
324  (rdiv @0 @0)
325  (if (FLOAT_TYPE_P (type)
326       && ! HONOR_NANS (type)
327       && ! HONOR_INFINITIES (type))
328   { build_one_cst (type); }))
330 /* Optimize -A / A to -1.0 if we don't care about
331    NaNs or Infinities.  */
332 (simplify
333  (rdiv:C @0 (negate @0))
334  (if (FLOAT_TYPE_P (type)
335       && ! HONOR_NANS (type)
336       && ! HONOR_INFINITIES (type))
337   { build_minus_one_cst (type); }))
339 /* PR71078: x / abs(x) -> copysign (1.0, x) */
340 (simplify
341  (rdiv:C (convert? @0) (convert? (abs @0)))
342   (if (SCALAR_FLOAT_TYPE_P (type)
343        && ! HONOR_NANS (type)
344        && ! HONOR_INFINITIES (type))
345    (switch
346     (if (types_match (type, float_type_node))
347      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
348     (if (types_match (type, double_type_node))
349      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
350     (if (types_match (type, long_double_type_node))
351      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
353 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
354 (simplify
355  (rdiv @0 real_onep)
356  (if (!HONOR_SNANS (type))
357   (non_lvalue @0)))
359 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
360 (simplify
361  (rdiv @0 real_minus_onep)
362  (if (!HONOR_SNANS (type))
363   (negate @0)))
365 (if (flag_reciprocal_math)
366  /* Convert (A/B)/C to A/(B*C). */
367  (simplify
368   (rdiv (rdiv:s @0 @1) @2)
369   (rdiv @0 (mult @1 @2)))
371  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
372  (simplify
373   (rdiv @0 (mult:s @1 REAL_CST@2))
374   (with
375    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
376    (if (tem)
377     (rdiv (mult @0 { tem; } ) @1))))
379  /* Convert A/(B/C) to (A/B)*C  */
380  (simplify
381   (rdiv @0 (rdiv:s @1 @2))
382    (mult (rdiv @0 @1) @2)))
384 /* Simplify x / (- y) to -x / y.  */
385 (simplify
386  (rdiv @0 (negate @1))
387  (rdiv (negate @0) @1))
389 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
390 (for div (trunc_div ceil_div floor_div round_div exact_div)
391  (simplify
392   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
393   (if (integer_pow2p (@2)
394        && tree_int_cst_sgn (@2) > 0
395        && tree_nop_conversion_p (type, TREE_TYPE (@0))
396        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
397    (rshift (convert @0)
398            { build_int_cst (integer_type_node,
399                             wi::exact_log2 (wi::to_wide (@2))); }))))
401 /* If ARG1 is a constant, we can convert this to a multiply by the
402    reciprocal.  This does not have the same rounding properties,
403    so only do this if -freciprocal-math.  We can actually
404    always safely do it if ARG1 is a power of two, but it's hard to
405    tell if it is or not in a portable manner.  */
406 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
407  (simplify
408   (rdiv @0 cst@1)
409   (if (optimize)
410    (if (flag_reciprocal_math
411         && !real_zerop (@1))
412     (with
413      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
414      (if (tem)
415       (mult @0 { tem; } )))
416     (if (cst != COMPLEX_CST)
417      (with { tree inverse = exact_inverse (type, @1); }
418       (if (inverse)
419        (mult @0 { inverse; } ))))))))
421 (for mod (ceil_mod floor_mod round_mod trunc_mod)
422  /* 0 % X is always zero.  */
423  (simplify
424   (mod integer_zerop@0 @1)
425   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
426   (if (!integer_zerop (@1))
427    @0))
428  /* X % 1 is always zero.  */
429  (simplify
430   (mod @0 integer_onep)
431   { build_zero_cst (type); })
432  /* X % -1 is zero.  */
433  (simplify
434   (mod @0 integer_minus_onep@1)
435   (if (!TYPE_UNSIGNED (type))
436    { build_zero_cst (type); }))
437  /* X % X is zero.  */
438  (simplify
439   (mod @0 @0)
440   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
441   (if (!integer_zerop (@0))
442    { build_zero_cst (type); }))
443  /* (X % Y) % Y is just X % Y.  */
444  (simplify
445   (mod (mod@2 @0 @1) @1)
446   @2)
447  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
448  (simplify
449   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
450   (if (ANY_INTEGRAL_TYPE_P (type)
451        && TYPE_OVERFLOW_UNDEFINED (type)
452        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
453                              TYPE_SIGN (type)))
454    { build_zero_cst (type); })))
456 /* X % -C is the same as X % C.  */
457 (simplify
458  (trunc_mod @0 INTEGER_CST@1)
459   (if (TYPE_SIGN (type) == SIGNED
460        && !TREE_OVERFLOW (@1)
461        && wi::neg_p (wi::to_wide (@1))
462        && !TYPE_OVERFLOW_TRAPS (type)
463        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
464        && !sign_bit_p (@1, @1))
465    (trunc_mod @0 (negate @1))))
467 /* X % -Y is the same as X % Y.  */
468 (simplify
469  (trunc_mod @0 (convert? (negate @1)))
470  (if (INTEGRAL_TYPE_P (type)
471       && !TYPE_UNSIGNED (type)
472       && !TYPE_OVERFLOW_TRAPS (type)
473       && tree_nop_conversion_p (type, TREE_TYPE (@1))
474       /* Avoid this transformation if X might be INT_MIN or
475          Y might be -1, because we would then change valid
476          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
477       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
478           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
479                                                         (TREE_TYPE (@1))))))
480   (trunc_mod @0 (convert @1))))
482 /* X - (X / Y) * Y is the same as X % Y.  */
483 (simplify
484  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
485  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
486   (convert (trunc_mod @0 @1))))
488 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
489    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
490    Also optimize A % (C << N)  where C is a power of 2,
491    to A & ((C << N) - 1).  */
492 (match (power_of_two_cand @1)
493  INTEGER_CST@1)
494 (match (power_of_two_cand @1)
495  (lshift INTEGER_CST@1 @2))
496 (for mod (trunc_mod floor_mod)
497  (simplify
498   (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
499   (if ((TYPE_UNSIGNED (type)
500         || tree_expr_nonnegative_p (@0))
501         && tree_nop_conversion_p (type, TREE_TYPE (@3))
502         && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
503    (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
505 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
506 (simplify
507  (trunc_div (mult @0 integer_pow2p@1) @1)
508  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
509   (bit_and @0 { wide_int_to_tree
510                 (type, wi::mask (TYPE_PRECISION (type)
511                                  - wi::exact_log2 (wi::to_wide (@1)),
512                                  false, TYPE_PRECISION (type))); })))
514 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
515 (simplify
516  (mult (trunc_div @0 integer_pow2p@1) @1)
517  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
518   (bit_and @0 (negate @1))))
520 /* Simplify (t * 2) / 2) -> t.  */
521 (for div (trunc_div ceil_div floor_div round_div exact_div)
522  (simplify
523   (div (mult:c @0 @1) @1)
524   (if (ANY_INTEGRAL_TYPE_P (type)
525        && TYPE_OVERFLOW_UNDEFINED (type))
526    @0)))
528 (for op (negate abs)
529  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
530  (for coss (COS COSH)
531   (simplify
532    (coss (op @0))
533     (coss @0)))
534  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
535  (for pows (POW)
536   (simplify
537    (pows (op @0) REAL_CST@1)
538    (with { HOST_WIDE_INT n; }
539     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
540      (pows @0 @1)))))
541  /* Likewise for powi.  */
542  (for pows (POWI)
543   (simplify
544    (pows (op @0) INTEGER_CST@1)
545    (if ((wi::to_wide (@1) & 1) == 0)
546     (pows @0 @1))))
547  /* Strip negate and abs from both operands of hypot.  */
548  (for hypots (HYPOT)
549   (simplify
550    (hypots (op @0) @1)
551    (hypots @0 @1))
552   (simplify
553    (hypots @0 (op @1))
554    (hypots @0 @1)))
555  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
556  (for copysigns (COPYSIGN_ALL)
557   (simplify
558    (copysigns (op @0) @1)
559    (copysigns @0 @1))))
561 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
562 (simplify
563  (mult (abs@1 @0) @1)
564  (mult @0 @0))
566 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
567 (for coss (COS COSH)
568      copysigns (COPYSIGN)
569  (simplify
570   (coss (copysigns @0 @1))
571    (coss @0)))
573 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
574 (for pows (POW)
575      copysigns (COPYSIGN)
576  (simplify
577   (pows (copysigns @0 @2) REAL_CST@1)
578   (with { HOST_WIDE_INT n; }
579    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
580     (pows @0 @1)))))
581 /* Likewise for powi.  */
582 (for pows (POWI)
583      copysigns (COPYSIGN)
584  (simplify
585   (pows (copysigns @0 @2) INTEGER_CST@1)
586   (if ((wi::to_wide (@1) & 1) == 0)
587    (pows @0 @1))))
589 (for hypots (HYPOT)
590      copysigns (COPYSIGN)
591  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
592  (simplify
593   (hypots (copysigns @0 @1) @2)
594   (hypots @0 @2))
595  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
596  (simplify
597   (hypots @0 (copysigns @1 @2))
598   (hypots @0 @1)))
600 /* copysign(x, CST) -> [-]abs (x).  */
601 (for copysigns (COPYSIGN_ALL)
602  (simplify
603   (copysigns @0 REAL_CST@1)
604   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
605    (negate (abs @0))
606    (abs @0))))
608 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
609 (for copysigns (COPYSIGN_ALL)
610  (simplify
611   (copysigns (copysigns @0 @1) @2)
612   (copysigns @0 @2)))
614 /* copysign(x,y)*copysign(x,y) -> x*x.  */
615 (for copysigns (COPYSIGN_ALL)
616  (simplify
617   (mult (copysigns@2 @0 @1) @2)
618   (mult @0 @0)))
620 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
621 (for ccoss (CCOS CCOSH)
622  (simplify
623   (ccoss (negate @0))
624    (ccoss @0)))
626 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
627 (for ops (conj negate)
628  (for cabss (CABS)
629   (simplify
630    (cabss (ops @0))
631    (cabss @0))))
633 /* Fold (a * (1 << b)) into (a << b)  */
634 (simplify
635  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
636   (if (! FLOAT_TYPE_P (type)
637        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
638    (lshift @0 @2)))
640 /* Fold (1 << (C - x)) where C = precision(type) - 1
641    into ((1 << C) >> x). */
642 (simplify
643  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
644   (if (INTEGRAL_TYPE_P (type)
645        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
646        && single_use (@1))
647    (if (TYPE_UNSIGNED (type))
648      (rshift (lshift @0 @2) @3)
649    (with
650     { tree utype = unsigned_type_for (type); }
651     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
653 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
654 (simplify
655  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
656   (if (flag_associative_math
657        && single_use (@3))
658    (with
659     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
660     (if (tem)
661      (rdiv { tem; } @1)))))
663 /* Simplify ~X & X as zero.  */
664 (simplify
665  (bit_and:c (convert? @0) (convert? (bit_not @0)))
666   { build_zero_cst (type); })
668 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
669 (simplify
670   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
671   (if (TYPE_UNSIGNED (type))
672     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
674 (for bitop (bit_and bit_ior)
675      cmp (eq ne)
676  /* PR35691: Transform
677     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
678     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
679  (simplify
680   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
681    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
682         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
683         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
684     (cmp (bit_ior @0 (convert @1)) @2)))
685  /* Transform:
686     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
687     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
688  (simplify
689   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
690    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
691         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
692         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
693     (cmp (bit_and @0 (convert @1)) @2))))
695 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
696 (simplify
697  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
698   (minus (bit_xor @0 @1) @1))
699 (simplify
700  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
701  (if (~wi::to_wide (@2) == wi::to_wide (@1))
702   (minus (bit_xor @0 @1) @1)))
704 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
705 (simplify
706  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
707   (minus @1 (bit_xor @0 @1)))
709 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
710 (for op (bit_ior bit_xor plus)
711  (simplify
712   (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
713    (bit_xor @0 @1))
714  (simplify
715   (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
716   (if (~wi::to_wide (@2) == wi::to_wide (@1))
717    (bit_xor @0 @1))))
719 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
720 (simplify
721   (bit_ior:c (bit_xor:c @0 @1) @0)
722   (bit_ior @0 @1))
724 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
725 (simplify
726  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
727  @2)
729 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
730 (simplify
731  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
732  (bit_not (bit_and @0 @1)))
734 /* (a | b) & ~(a ^ b)  -->  a & b  */
735 (simplify
736  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
737  (bit_and @0 @1))
739 /* a | ~(a ^ b)  -->  a | ~b  */
740 (simplify
741  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
742  (bit_ior @0 (bit_not @1)))
744 /* (a | b) | (a &^ b)  -->  a | b  */
745 (for op (bit_and bit_xor)
746  (simplify
747   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
748   @2))
750 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
751 (simplify
752  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
753  @2)
755 /* ~(~a & b)  -->  a | ~b  */
756 (simplify
757  (bit_not (bit_and:cs (bit_not @0) @1))
758  (bit_ior @0 (bit_not @1)))
760 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
761 #if GIMPLE
762 (simplify
763  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
764  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
765       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
766   (bit_xor @0 @1)))
767 #endif
769 /* X % Y is smaller than Y.  */
770 (for cmp (lt ge)
771  (simplify
772   (cmp (trunc_mod @0 @1) @1)
773   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
774    { constant_boolean_node (cmp == LT_EXPR, type); })))
775 (for cmp (gt le)
776  (simplify
777   (cmp @1 (trunc_mod @0 @1))
778   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
779    { constant_boolean_node (cmp == GT_EXPR, type); })))
781 /* x | ~0 -> ~0  */
782 (simplify
783  (bit_ior @0 integer_all_onesp@1)
784  @1)
786 /* x | 0 -> x  */
787 (simplify
788  (bit_ior @0 integer_zerop)
789  @0)
791 /* x & 0 -> 0  */
792 (simplify
793  (bit_and @0 integer_zerop@1)
794  @1)
796 /* ~x | x -> -1 */
797 /* ~x ^ x -> -1 */
798 /* ~x + x -> -1 */
799 (for op (bit_ior bit_xor plus)
800  (simplify
801   (op:c (convert? @0) (convert? (bit_not @0)))
802   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
804 /* x ^ x -> 0 */
805 (simplify
806   (bit_xor @0 @0)
807   { build_zero_cst (type); })
809 /* Canonicalize X ^ ~0 to ~X.  */
810 (simplify
811   (bit_xor @0 integer_all_onesp@1)
812   (bit_not @0))
814 /* x & ~0 -> x  */
815 (simplify
816  (bit_and @0 integer_all_onesp)
817   (non_lvalue @0))
819 /* x & x -> x,  x | x -> x  */
820 (for bitop (bit_and bit_ior)
821  (simplify
822   (bitop @0 @0)
823   (non_lvalue @0)))
825 /* x & C -> x if we know that x & ~C == 0.  */
826 #if GIMPLE
827 (simplify
828  (bit_and SSA_NAME@0 INTEGER_CST@1)
829  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
830       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
831   @0))
832 #endif
834 /* x + (x & 1) -> (x + 1) & ~1 */
835 (simplify
836  (plus:c @0 (bit_and:s @0 integer_onep@1))
837  (bit_and (plus @0 @1) (bit_not @1)))
839 /* x & ~(x & y) -> x & ~y */
840 /* x | ~(x | y) -> x | ~y  */
841 (for bitop (bit_and bit_ior)
842  (simplify
843   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
844   (bitop @0 (bit_not @1))))
846 /* (x | y) & ~x -> y & ~x */
847 /* (x & y) | ~x -> y | ~x */
848 (for bitop (bit_and bit_ior)
849      rbitop (bit_ior bit_and)
850  (simplify
851   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
852   (bitop @1 @2)))
854 /* (x & y) ^ (x | y) -> x ^ y */
855 (simplify
856  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
857  (bit_xor @0 @1))
859 /* (x ^ y) ^ (x | y) -> x & y */
860 (simplify
861  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
862  (bit_and @0 @1))
864 /* (x & y) + (x ^ y) -> x | y */
865 /* (x & y) | (x ^ y) -> x | y */
866 /* (x & y) ^ (x ^ y) -> x | y */
867 (for op (plus bit_ior bit_xor)
868  (simplify
869   (op:c (bit_and @0 @1) (bit_xor @0 @1))
870   (bit_ior @0 @1)))
872 /* (x & y) + (x | y) -> x + y */
873 (simplify
874  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
875  (plus @0 @1))
877 /* (x + y) - (x | y) -> x & y */
878 (simplify
879  (minus (plus @0 @1) (bit_ior @0 @1))
880  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
881       && !TYPE_SATURATING (type))
882   (bit_and @0 @1)))
884 /* (x + y) - (x & y) -> x | y */
885 (simplify
886  (minus (plus @0 @1) (bit_and @0 @1))
887  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
888       && !TYPE_SATURATING (type))
889   (bit_ior @0 @1)))
891 /* (x | y) - (x ^ y) -> x & y */
892 (simplify
893  (minus (bit_ior @0 @1) (bit_xor @0 @1))
894  (bit_and @0 @1))
896 /* (x | y) - (x & y) -> x ^ y */
897 (simplify
898  (minus (bit_ior @0 @1) (bit_and @0 @1))
899  (bit_xor @0 @1))
901 /* (x | y) & ~(x & y) -> x ^ y */
902 (simplify
903  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
904  (bit_xor @0 @1))
906 /* (x | y) & (~x ^ y) -> x & y */
907 (simplify
908  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
909  (bit_and @0 @1))
911 /* ~x & ~y -> ~(x | y)
912    ~x | ~y -> ~(x & y) */
913 (for op (bit_and bit_ior)
914      rop (bit_ior bit_and)
915  (simplify
916   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
917   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
918        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
919    (bit_not (rop (convert @0) (convert @1))))))
921 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
922    with a constant, and the two constants have no bits in common,
923    we should treat this as a BIT_IOR_EXPR since this may produce more
924    simplifications.  */
925 (for op (bit_xor plus)
926  (simplify
927   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
928       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
929   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
930        && tree_nop_conversion_p (type, TREE_TYPE (@2))
931        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
932    (bit_ior (convert @4) (convert @5)))))
934 /* (X | Y) ^ X -> Y & ~ X*/
935 (simplify
936  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
937  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
938   (convert (bit_and @1 (bit_not @0)))))
940 /* Convert ~X ^ ~Y to X ^ Y.  */
941 (simplify
942  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
943  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
944       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
945   (bit_xor (convert @0) (convert @1))))
947 /* Convert ~X ^ C to X ^ ~C.  */
948 (simplify
949  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
950  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
951   (bit_xor (convert @0) (bit_not @1))))
953 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
954 (for opo (bit_and bit_xor)
955      opi (bit_xor bit_and)
956  (simplify
957   (opo:c (opi:c @0 @1) @1) 
958   (bit_and (bit_not @0) @1)))
960 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
961    operands are another bit-wise operation with a common input.  If so,
962    distribute the bit operations to save an operation and possibly two if
963    constants are involved.  For example, convert
964      (A | B) & (A | C) into A | (B & C)
965    Further simplification will occur if B and C are constants.  */
966 (for op (bit_and bit_ior bit_xor)
967      rop (bit_ior bit_and bit_and)
968  (simplify
969   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
970   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
971        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
972    (rop (convert @0) (op (convert @1) (convert @2))))))
974 /* Some simple reassociation for bit operations, also handled in reassoc.  */
975 /* (X & Y) & Y -> X & Y
976    (X | Y) | Y -> X | Y  */
977 (for op (bit_and bit_ior)
978  (simplify
979   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
980   @2))
981 /* (X ^ Y) ^ Y -> X  */
982 (simplify
983  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
984  (convert @0))
985 /* (X & Y) & (X & Z) -> (X & Y) & Z
986    (X | Y) | (X | Z) -> (X | Y) | Z  */
987 (for op (bit_and bit_ior)
988  (simplify
989   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
990   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
991        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
992    (if (single_use (@5) && single_use (@6))
993     (op @3 (convert @2))
994     (if (single_use (@3) && single_use (@4))
995      (op (convert @1) @5))))))
996 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
997 (simplify
998  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
999  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1000       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1001   (bit_xor (convert @1) (convert @2))))
1003 (simplify
1004  (abs (abs@1 @0))
1005  @1)
1006 (simplify
1007  (abs (negate @0))
1008  (abs @0))
1009 (simplify
1010  (abs tree_expr_nonnegative_p@0)
1011  @0)
1013 /* A few cases of fold-const.c negate_expr_p predicate.  */
1014 (match negate_expr_p
1015  INTEGER_CST
1016  (if ((INTEGRAL_TYPE_P (type)
1017        && TYPE_UNSIGNED (type))
1018       || (!TYPE_OVERFLOW_SANITIZED (type)
1019           && may_negate_without_overflow_p (t)))))
1020 (match negate_expr_p
1021  FIXED_CST)
1022 (match negate_expr_p
1023  (negate @0)
1024  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1025 (match negate_expr_p
1026  REAL_CST
1027  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1028 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1029    ways.  */
1030 (match negate_expr_p
1031  VECTOR_CST
1032  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1033 (match negate_expr_p
1034  (minus @0 @1)
1035  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1036       || (FLOAT_TYPE_P (type)
1037           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1038           && !HONOR_SIGNED_ZEROS (type)))))
1040 /* (-A) * (-B) -> A * B  */
1041 (simplify
1042  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1043   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1044        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1045    (mult (convert @0) (convert (negate @1)))))
1047 /* -(A + B) -> (-B) - A.  */
1048 (simplify
1049  (negate (plus:c @0 negate_expr_p@1))
1050  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1051       && !HONOR_SIGNED_ZEROS (element_mode (type)))
1052   (minus (negate @1) @0)))
1054 /* -(A - B) -> B - A.  */
1055 (simplify
1056  (negate (minus @0 @1))
1057  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1058       || (FLOAT_TYPE_P (type)
1059           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1060           && !HONOR_SIGNED_ZEROS (type)))
1061   (minus @1 @0)))
1062 (simplify
1063  (negate (pointer_diff @0 @1))
1064  (if (TYPE_OVERFLOW_UNDEFINED (type))
1065   (pointer_diff @1 @0)))
1067 /* A - B -> A + (-B) if B is easily negatable.  */
1068 (simplify
1069  (minus @0 negate_expr_p@1)
1070  (if (!FIXED_POINT_TYPE_P (type))
1071  (plus @0 (negate @1))))
1073 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1074    when profitable.
1075    For bitwise binary operations apply operand conversions to the
1076    binary operation result instead of to the operands.  This allows
1077    to combine successive conversions and bitwise binary operations.
1078    We combine the above two cases by using a conditional convert.  */
1079 (for bitop (bit_and bit_ior bit_xor)
1080  (simplify
1081   (bitop (convert @0) (convert? @1))
1082   (if (((TREE_CODE (@1) == INTEGER_CST
1083          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1084          && int_fits_type_p (@1, TREE_TYPE (@0)))
1085         || types_match (@0, @1))
1086        /* ???  This transform conflicts with fold-const.c doing
1087           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1088           constants (if x has signed type, the sign bit cannot be set
1089           in c).  This folds extension into the BIT_AND_EXPR.
1090           Restrict it to GIMPLE to avoid endless recursions.  */
1091        && (bitop != BIT_AND_EXPR || GIMPLE)
1092        && (/* That's a good idea if the conversion widens the operand, thus
1093               after hoisting the conversion the operation will be narrower.  */
1094            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1095            /* It's also a good idea if the conversion is to a non-integer
1096               mode.  */
1097            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1098            /* Or if the precision of TO is not the same as the precision
1099               of its mode.  */
1100            || !type_has_mode_precision_p (type)))
1101    (convert (bitop @0 (convert @1))))))
1103 (for bitop (bit_and bit_ior)
1104      rbitop (bit_ior bit_and)
1105   /* (x | y) & x -> x */
1106   /* (x & y) | x -> x */
1107  (simplify
1108   (bitop:c (rbitop:c @0 @1) @0)
1109   @0)
1110  /* (~x | y) & x -> x & y */
1111  /* (~x & y) | x -> x | y */
1112  (simplify
1113   (bitop:c (rbitop:c (bit_not @0) @1) @0)
1114   (bitop @0 @1)))
1116 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1117 (simplify
1118   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1119   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1121 /* Combine successive equal operations with constants.  */
1122 (for bitop (bit_and bit_ior bit_xor)
1123  (simplify
1124   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1125   (if (!CONSTANT_CLASS_P (@0))
1126    /* This is the canonical form regardless of whether (bitop @1 @2) can be
1127       folded to a constant.  */
1128    (bitop @0 (bitop @1 @2))
1129    /* In this case we have three constants and (bitop @0 @1) doesn't fold
1130       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
1131       the values involved are such that the operation can't be decided at
1132       compile time.  Try folding one of @0 or @1 with @2 to see whether
1133       that combination can be decided at compile time.
1135       Keep the existing form if both folds fail, to avoid endless
1136       oscillation.  */
1137    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1138     (if (cst1)
1139      (bitop @1 { cst1; })
1140      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1141       (if (cst2)
1142        (bitop @0 { cst2; }))))))))
1144 /* Try simple folding for X op !X, and X op X with the help
1145    of the truth_valued_p and logical_inverted_value predicates.  */
1146 (match truth_valued_p
1147  @0
1148  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1149 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1150  (match truth_valued_p
1151   (op @0 @1)))
1152 (match truth_valued_p
1153   (truth_not @0))
1155 (match (logical_inverted_value @0)
1156  (truth_not @0))
1157 (match (logical_inverted_value @0)
1158  (bit_not truth_valued_p@0))
1159 (match (logical_inverted_value @0)
1160  (eq @0 integer_zerop))
1161 (match (logical_inverted_value @0)
1162  (ne truth_valued_p@0 integer_truep))
1163 (match (logical_inverted_value @0)
1164  (bit_xor truth_valued_p@0 integer_truep))
1166 /* X & !X -> 0.  */
1167 (simplify
1168  (bit_and:c @0 (logical_inverted_value @0))
1169  { build_zero_cst (type); })
1170 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
1171 (for op (bit_ior bit_xor)
1172  (simplify
1173   (op:c truth_valued_p@0 (logical_inverted_value @0))
1174   { constant_boolean_node (true, type); }))
1175 /* X ==/!= !X is false/true.  */
1176 (for op (eq ne)
1177  (simplify
1178   (op:c truth_valued_p@0 (logical_inverted_value @0))
1179   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1181 /* ~~x -> x */
1182 (simplify
1183   (bit_not (bit_not @0))
1184   @0)
1186 /* Convert ~ (-A) to A - 1.  */
1187 (simplify
1188  (bit_not (convert? (negate @0)))
1189  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1190       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1191   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1193 /* Convert - (~A) to A + 1.  */
1194 (simplify
1195  (negate (nop_convert (bit_not @0)))
1196  (plus (view_convert @0) { build_each_one_cst (type); }))
1198 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
1199 (simplify
1200  (bit_not (convert? (minus @0 integer_each_onep)))
1201  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1202       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1203   (convert (negate @0))))
1204 (simplify
1205  (bit_not (convert? (plus @0 integer_all_onesp)))
1206  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1207       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1208   (convert (negate @0))))
1210 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
1211 (simplify
1212  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1213  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1214   (convert (bit_xor @0 (bit_not @1)))))
1215 (simplify
1216  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1217  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1218   (convert (bit_xor @0 @1))))
1220 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
1221 (simplify
1222  (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1223  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1224   (bit_not (bit_xor (view_convert @0) @1))))
1226 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1227 (simplify
1228  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1229  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1231 /* Fold A - (A & B) into ~B & A.  */
1232 (simplify
1233  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1234  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1235       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1236   (convert (bit_and (bit_not @1) @0))))
1238 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
1239 (for cmp (gt lt ge le)
1240 (simplify
1241  (mult (convert (cmp @0 @1)) @2)
1242   (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1244 /* For integral types with undefined overflow and C != 0 fold
1245    x * C EQ/NE y * C into x EQ/NE y.  */
1246 (for cmp (eq ne)
1247  (simplify
1248   (cmp (mult:c @0 @1) (mult:c @2 @1))
1249   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1250        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1251        && tree_expr_nonzero_p (@1))
1252    (cmp @0 @2))))
1254 /* For integral types with wrapping overflow and C odd fold
1255    x * C EQ/NE y * C into x EQ/NE y.  */
1256 (for cmp (eq ne)
1257  (simplify
1258   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1259   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1260        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1261        && (TREE_INT_CST_LOW (@1) & 1) != 0)
1262    (cmp @0 @2))))
1264 /* For integral types with undefined overflow and C != 0 fold
1265    x * C RELOP y * C into:
1267    x RELOP y for nonnegative C
1268    y RELOP x for negative C  */
1269 (for cmp (lt gt le ge)
1270  (simplify
1271   (cmp (mult:c @0 @1) (mult:c @2 @1))
1272   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1273        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1274    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1275     (cmp @0 @2)
1276    (if (TREE_CODE (@1) == INTEGER_CST
1277         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1278     (cmp @2 @0))))))
1280 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
1281 (for cmp (le gt)
1282      icmp (gt le)
1283  (simplify
1284   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1285    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1286         && TYPE_UNSIGNED (TREE_TYPE (@0))
1287         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1288         && (wi::to_wide (@2)
1289             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1290     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1291      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1293 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
1294 (for cmp (simple_comparison)
1295  (simplify
1296   (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1297   (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1298    (cmp @0 @1))))
1300 /* X / C1 op C2 into a simple range test.  */
1301 (for cmp (simple_comparison)
1302  (simplify
1303   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1304   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1305        && integer_nonzerop (@1)
1306        && !TREE_OVERFLOW (@1)
1307        && !TREE_OVERFLOW (@2))
1308    (with { tree lo, hi; bool neg_overflow;
1309            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1310                                                    &neg_overflow); }
1311     (switch
1312      (if (code == LT_EXPR || code == GE_EXPR)
1313        (if (TREE_OVERFLOW (lo))
1314         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1315         (if (code == LT_EXPR)
1316          (lt @0 { lo; })
1317          (ge @0 { lo; }))))
1318      (if (code == LE_EXPR || code == GT_EXPR)
1319        (if (TREE_OVERFLOW (hi))
1320         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1321         (if (code == LE_EXPR)
1322          (le @0 { hi; })
1323          (gt @0 { hi; }))))
1324      (if (!lo && !hi)
1325       { build_int_cst (type, code == NE_EXPR); })
1326      (if (code == EQ_EXPR && !hi)
1327       (ge @0 { lo; }))
1328      (if (code == EQ_EXPR && !lo)
1329       (le @0 { hi; }))
1330      (if (code == NE_EXPR && !hi)
1331       (lt @0 { lo; }))
1332      (if (code == NE_EXPR && !lo)
1333       (gt @0 { hi; }))
1334      (if (GENERIC)
1335       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1336                            lo, hi); })
1337      (with
1338       {
1339         tree etype = range_check_type (TREE_TYPE (@0));
1340         if (etype)
1341           {
1342             if (! TYPE_UNSIGNED (etype))
1343               etype = unsigned_type_for (etype);
1344             hi = fold_convert (etype, hi);
1345             lo = fold_convert (etype, lo);
1346             hi = const_binop (MINUS_EXPR, etype, hi, lo);
1347           }
1348       }
1349       (if (etype && hi && !TREE_OVERFLOW (hi))
1350        (if (code == EQ_EXPR)
1351         (le (minus (convert:etype @0) { lo; }) { hi; })
1352         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1354 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
1355 (for op (lt le ge gt)
1356  (simplify
1357   (op (plus:c @0 @2) (plus:c @1 @2))
1358   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1359        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1360    (op @0 @1))))
1361 /* For equality and subtraction, this is also true with wrapping overflow.  */
1362 (for op (eq ne minus)
1363  (simplify
1364   (op (plus:c @0 @2) (plus:c @1 @2))
1365   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1366        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1367            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1368    (op @0 @1))))
1370 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
1371 (for op (lt le ge gt)
1372  (simplify
1373   (op (minus @0 @2) (minus @1 @2))
1374   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1375        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1376    (op @0 @1))))
1377 /* For equality and subtraction, this is also true with wrapping overflow.  */
1378 (for op (eq ne minus)
1379  (simplify
1380   (op (minus @0 @2) (minus @1 @2))
1381   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1382        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1383            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1384    (op @0 @1))))
1385 /* And for pointers...  */
1386 (for op (simple_comparison)
1387  (simplify
1388   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1389   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1390    (op @0 @1))))
1391 (simplify
1392  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1393  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1394       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1395   (pointer_diff @0 @1)))
1397 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
1398 (for op (lt le ge gt)
1399  (simplify
1400   (op (minus @2 @0) (minus @2 @1))
1401   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1402        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1403    (op @1 @0))))
1404 /* For equality and subtraction, this is also true with wrapping overflow.  */
1405 (for op (eq ne minus)
1406  (simplify
1407   (op (minus @2 @0) (minus @2 @1))
1408   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1409        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1410            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1411    (op @1 @0))))
1412 /* And for pointers...  */
1413 (for op (simple_comparison)
1414  (simplify
1415   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1416   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1417    (op @1 @0))))
1418 (simplify
1419  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1420  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1421       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1422   (pointer_diff @1 @0)))
1424 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
1425 (for op (lt le gt ge)
1426  (simplify
1427   (op:c (plus:c@2 @0 @1) @1)
1428   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1429        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1430        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1431    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1432 /* For equality, this is also true with wrapping overflow.  */
1433 (for op (eq ne)
1434  (simplify
1435   (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1436   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1437        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1438            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1439        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1440        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1441        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1442    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1443  (simplify
1444   (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1445   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1446        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1447        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1448    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1450 /* X - Y < X is the same as Y > 0 when there is no overflow.
1451    For equality, this is also true with wrapping overflow.  */
1452 (for op (simple_comparison)
1453  (simplify
1454   (op:c @0 (minus@2 @0 @1))
1455   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1456        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1457            || ((op == EQ_EXPR || op == NE_EXPR)
1458                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1459        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1460    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1462 /* Transform:
1463  * (X / Y) == 0 -> X < Y if X, Y are unsigned.
1464  * (X / Y) != 0 -> X >= Y, if X, Y are unsigned.
1465  */
1466 (for cmp (eq ne)
1467      ocmp (lt ge)
1468  (simplify
1469   (cmp (trunc_div @0 @1) integer_zerop)
1470   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1471        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1472    (ocmp @0 @1))))
1474 /* X == C - X can never be true if C is odd.  */
1475 (for cmp (eq ne)
1476  (simplify
1477   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1478   (if (TREE_INT_CST_LOW (@1) & 1)
1479    { constant_boolean_node (cmp == NE_EXPR, type); })))
1481 /* Arguments on which one can call get_nonzero_bits to get the bits
1482    possibly set.  */
1483 (match with_possible_nonzero_bits
1484  INTEGER_CST@0)
1485 (match with_possible_nonzero_bits
1486  SSA_NAME@0
1487  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1488 /* Slightly extended version, do not make it recursive to keep it cheap.  */
1489 (match (with_possible_nonzero_bits2 @0)
1490  with_possible_nonzero_bits@0)
1491 (match (with_possible_nonzero_bits2 @0)
1492  (bit_and:c with_possible_nonzero_bits@0 @2))
1494 /* Same for bits that are known to be set, but we do not have
1495    an equivalent to get_nonzero_bits yet.  */
1496 (match (with_certain_nonzero_bits2 @0)
1497  INTEGER_CST@0)
1498 (match (with_certain_nonzero_bits2 @0)
1499  (bit_ior @1 INTEGER_CST@0))
1501 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
1502 (for cmp (eq ne)
1503  (simplify
1504   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1505   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1506    { constant_boolean_node (cmp == NE_EXPR, type); })))
1508 /* ((X inner_op C0) outer_op C1)
1509    With X being a tree where value_range has reasoned certain bits to always be
1510    zero throughout its computed value range,
1511    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1512    where zero_mask has 1's for all bits that are sure to be 0 in
1513    and 0's otherwise.
1514    if (inner_op == '^') C0 &= ~C1;
1515    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1516    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1518 (for inner_op (bit_ior bit_xor)
1519      outer_op (bit_xor bit_ior)
1520 (simplify
1521  (outer_op
1522   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1523  (with
1524   {
1525     bool fail = false;
1526     wide_int zero_mask_not;
1527     wide_int C0;
1528     wide_int cst_emit;
1530     if (TREE_CODE (@2) == SSA_NAME)
1531       zero_mask_not = get_nonzero_bits (@2);
1532     else
1533       fail = true;
1535     if (inner_op == BIT_XOR_EXPR)
1536       {
1537         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1538         cst_emit = C0 | wi::to_wide (@1);
1539       }
1540     else
1541       {
1542         C0 = wi::to_wide (@0);
1543         cst_emit = C0 ^ wi::to_wide (@1);
1544       }
1545   }
1546   (if (!fail && (C0 & zero_mask_not) == 0)
1547    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1548    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1549     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1551 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
1552 (simplify
1553   (pointer_plus (pointer_plus:s @0 @1) @3)
1554   (pointer_plus @0 (plus @1 @3)))
1556 /* Pattern match
1557      tem1 = (long) ptr1;
1558      tem2 = (long) ptr2;
1559      tem3 = tem2 - tem1;
1560      tem4 = (unsigned long) tem3;
1561      tem5 = ptr1 + tem4;
1562    and produce
1563      tem5 = ptr2;  */
1564 (simplify
1565   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1566   /* Conditionally look through a sign-changing conversion.  */
1567   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1568        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1569             || (GENERIC && type == TREE_TYPE (@1))))
1570    @1))
1571 (simplify
1572   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1573   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1574    (convert @1)))
1576 /* Pattern match
1577      tem = (sizetype) ptr;
1578      tem = tem & algn;
1579      tem = -tem;
1580      ... = ptr p+ tem;
1581    and produce the simpler and easier to analyze with respect to alignment
1582      ... = ptr & ~algn;  */
1583 (simplify
1584   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1585   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1586    (bit_and @0 { algn; })))
1588 /* Try folding difference of addresses.  */
1589 (simplify
1590  (minus (convert ADDR_EXPR@0) (convert @1))
1591  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1592   (with { poly_int64 diff; }
1593    (if (ptr_difference_const (@0, @1, &diff))
1594     { build_int_cst_type (type, diff); }))))
1595 (simplify
1596  (minus (convert @0) (convert ADDR_EXPR@1))
1597  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1598   (with { poly_int64 diff; }
1599    (if (ptr_difference_const (@0, @1, &diff))
1600     { build_int_cst_type (type, diff); }))))
1601 (simplify
1602  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert?@3 @1))
1603  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1604       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1605   (with { poly_int64 diff; }
1606    (if (ptr_difference_const (@0, @1, &diff))
1607     { build_int_cst_type (type, diff); }))))
1608 (simplify
1609  (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1))
1610  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1611       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1612   (with { poly_int64 diff; }
1613    (if (ptr_difference_const (@0, @1, &diff))
1614     { build_int_cst_type (type, diff); }))))
1616 /* If arg0 is derived from the address of an object or function, we may
1617    be able to fold this expression using the object or function's
1618    alignment.  */
1619 (simplify
1620  (bit_and (convert? @0) INTEGER_CST@1)
1621  (if (POINTER_TYPE_P (TREE_TYPE (@0))
1622       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1623   (with
1624    {
1625      unsigned int align;
1626      unsigned HOST_WIDE_INT bitpos;
1627      get_pointer_alignment_1 (@0, &align, &bitpos);
1628    }
1629    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1630     { wide_int_to_tree (type, (wi::to_wide (@1)
1631                                & (bitpos / BITS_PER_UNIT))); }))))
1634 /* We can't reassociate at all for saturating types.  */
1635 (if (!TYPE_SATURATING (type))
1637  /* Contract negates.  */
1638  /* A + (-B) -> A - B */
1639  (simplify
1640   (plus:c @0 (convert? (negate @1)))
1641   /* Apply STRIP_NOPS on the negate.  */
1642   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1643        && !TYPE_OVERFLOW_SANITIZED (type))
1644    (with
1645     {
1646      tree t1 = type;
1647      if (INTEGRAL_TYPE_P (type)
1648          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1649        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1650     }
1651     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1652  /* A - (-B) -> A + B */
1653  (simplify
1654   (minus @0 (convert? (negate @1)))
1655   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1656        && !TYPE_OVERFLOW_SANITIZED (type))
1657    (with
1658     {
1659      tree t1 = type;
1660      if (INTEGRAL_TYPE_P (type)
1661          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1662        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1663     }
1664     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1665  /* -(T)(-A) -> (T)A
1666     Sign-extension is ok except for INT_MIN, which thankfully cannot
1667     happen without overflow.  */
1668  (simplify
1669   (negate (convert (negate @1)))
1670   (if (INTEGRAL_TYPE_P (type)
1671        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1672            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1673                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1674        && !TYPE_OVERFLOW_SANITIZED (type)
1675        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1676    (convert @1)))
1677  (simplify
1678   (negate (convert negate_expr_p@1))
1679   (if (SCALAR_FLOAT_TYPE_P (type)
1680        && ((DECIMAL_FLOAT_TYPE_P (type)
1681             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1682             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1683            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1684    (convert (negate @1))))
1685  (simplify
1686   (negate (nop_convert (negate @1)))
1687   (if (!TYPE_OVERFLOW_SANITIZED (type)
1688        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1689    (view_convert @1)))
1691  /* We can't reassociate floating-point unless -fassociative-math
1692     or fixed-point plus or minus because of saturation to +-Inf.  */
1693  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1694       && !FIXED_POINT_TYPE_P (type))
1696   /* Match patterns that allow contracting a plus-minus pair
1697      irrespective of overflow issues.  */
1698   /* (A +- B) - A       ->  +- B */
1699   /* (A +- B) -+ B      ->  A */
1700   /* A - (A +- B)       -> -+ B */
1701   /* A +- (B -+ A)      ->  +- B */
1702   (simplify
1703     (minus (plus:c @0 @1) @0)
1704     @1)
1705   (simplify
1706     (minus (minus @0 @1) @0)
1707     (negate @1))
1708   (simplify
1709     (plus:c (minus @0 @1) @1)
1710     @0)
1711   (simplify
1712    (minus @0 (plus:c @0 @1))
1713    (negate @1))
1714   (simplify
1715    (minus @0 (minus @0 @1))
1716    @1)
1717   /* (A +- B) + (C - A)   -> C +- B */
1718   /* (A +  B) - (A - C)   -> B + C */
1719   /* More cases are handled with comparisons.  */
1720   (simplify
1721    (plus:c (plus:c @0 @1) (minus @2 @0))
1722    (plus @2 @1))
1723   (simplify
1724    (plus:c (minus @0 @1) (minus @2 @0))
1725    (minus @2 @1))
1726   (simplify
1727    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1728    (if (TYPE_OVERFLOW_UNDEFINED (type)
1729         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1730     (pointer_diff @2 @1)))
1731   (simplify
1732    (minus (plus:c @0 @1) (minus @0 @2))
1733    (plus @1 @2))
1735   /* (A +- CST1) +- CST2 -> A + CST3
1736      Use view_convert because it is safe for vectors and equivalent for
1737      scalars.  */
1738   (for outer_op (plus minus)
1739    (for inner_op (plus minus)
1740         neg_inner_op (minus plus)
1741     (simplify
1742      (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1743                CONSTANT_CLASS_P@2)
1744      /* If one of the types wraps, use that one.  */
1745      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1746       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1747          forever if something doesn't simplify into a constant.  */
1748       (if (!CONSTANT_CLASS_P (@0))
1749        (if (outer_op == PLUS_EXPR)
1750         (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1751         (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1752       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1753            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1754        (if (outer_op == PLUS_EXPR)
1755         (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1756         (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1757        /* If the constant operation overflows we cannot do the transform
1758           directly as we would introduce undefined overflow, for example
1759           with (a - 1) + INT_MIN.  */
1760        (if (types_match (type, @0))
1761         (with { tree cst = const_binop (outer_op == inner_op
1762                                         ? PLUS_EXPR : MINUS_EXPR,
1763                                         type, @1, @2); }
1764          (if (cst && !TREE_OVERFLOW (cst))
1765           (inner_op @0 { cst; } )
1766           /* X+INT_MAX+1 is X-INT_MIN.  */
1767           (if (INTEGRAL_TYPE_P (type) && cst
1768                && wi::to_wide (cst) == wi::min_value (type))
1769            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1770            /* Last resort, use some unsigned type.  */
1771            (with { tree utype = unsigned_type_for (type); }
1772             (view_convert (inner_op
1773                            (view_convert:utype @0)
1774                            (view_convert:utype
1775                             { drop_tree_overflow (cst); })))))))))))))
1777   /* (CST1 - A) +- CST2 -> CST3 - A  */
1778   (for outer_op (plus minus)
1779    (simplify
1780     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1781     (with { tree cst = const_binop (outer_op, type, @1, @2); }
1782      (if (cst && !TREE_OVERFLOW (cst))
1783       (minus { cst; } @0)))))
1785   /* CST1 - (CST2 - A) -> CST3 + A  */
1786   (simplify
1787    (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1788    (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1789     (if (cst && !TREE_OVERFLOW (cst))
1790      (plus { cst; } @0))))
1792   /* ~A + A -> -1 */
1793   (simplify
1794    (plus:c (bit_not @0) @0)
1795    (if (!TYPE_OVERFLOW_TRAPS (type))
1796     { build_all_ones_cst (type); }))
1798   /* ~A + 1 -> -A */
1799   (simplify
1800    (plus (convert? (bit_not @0)) integer_each_onep)
1801    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1802     (negate (convert @0))))
1804   /* -A - 1 -> ~A */
1805   (simplify
1806    (minus (convert? (negate @0)) integer_each_onep)
1807    (if (!TYPE_OVERFLOW_TRAPS (type)
1808         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1809     (bit_not (convert @0))))
1811   /* -1 - A -> ~A */
1812   (simplify
1813    (minus integer_all_onesp @0)
1814    (bit_not @0))
1816   /* (T)(P + A) - (T)P -> (T) A */
1817   (simplify
1818    (minus (convert (plus:c @@0 @1))
1819     (convert? @0))
1820    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1821         /* For integer types, if A has a smaller type
1822            than T the result depends on the possible
1823            overflow in P + A.
1824            E.g. T=size_t, A=(unsigned)429497295, P>0.
1825            However, if an overflow in P + A would cause
1826            undefined behavior, we can assume that there
1827            is no overflow.  */
1828         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1829             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1830     (convert @1)))
1831   (simplify
1832    (minus (convert (pointer_plus @@0 @1))
1833     (convert @0))
1834    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1835         /* For pointer types, if the conversion of A to the
1836            final type requires a sign- or zero-extension,
1837            then we have to punt - it is not defined which
1838            one is correct.  */
1839         || (POINTER_TYPE_P (TREE_TYPE (@0))
1840             && TREE_CODE (@1) == INTEGER_CST
1841             && tree_int_cst_sign_bit (@1) == 0))
1842     (convert @1)))
1843    (simplify
1844     (pointer_diff (pointer_plus @@0 @1) @0)
1845     /* The second argument of pointer_plus must be interpreted as signed, and
1846        thus sign-extended if necessary.  */
1847     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1848      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1849         second arg is unsigned even when we need to consider it as signed,
1850         we don't want to diagnose overflow here.  */
1851      (convert (view_convert:stype @1))))
1853   /* (T)P - (T)(P + A) -> -(T) A */
1854   (simplify
1855    (minus (convert? @0)
1856     (convert (plus:c @@0 @1)))
1857    (if (INTEGRAL_TYPE_P (type)
1858         && TYPE_OVERFLOW_UNDEFINED (type)
1859         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1860     (with { tree utype = unsigned_type_for (type); }
1861      (convert (negate (convert:utype @1))))
1862     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1863          /* For integer types, if A has a smaller type
1864             than T the result depends on the possible
1865             overflow in P + A.
1866             E.g. T=size_t, A=(unsigned)429497295, P>0.
1867             However, if an overflow in P + A would cause
1868             undefined behavior, we can assume that there
1869             is no overflow.  */
1870          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1871              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1872      (negate (convert @1)))))
1873   (simplify
1874    (minus (convert @0)
1875     (convert (pointer_plus @@0 @1)))
1876    (if (INTEGRAL_TYPE_P (type)
1877         && TYPE_OVERFLOW_UNDEFINED (type)
1878         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1879     (with { tree utype = unsigned_type_for (type); }
1880      (convert (negate (convert:utype @1))))
1881     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1882          /* For pointer types, if the conversion of A to the
1883             final type requires a sign- or zero-extension,
1884             then we have to punt - it is not defined which
1885             one is correct.  */
1886          || (POINTER_TYPE_P (TREE_TYPE (@0))
1887              && TREE_CODE (@1) == INTEGER_CST
1888              && tree_int_cst_sign_bit (@1) == 0))
1889      (negate (convert @1)))))
1890    (simplify
1891     (pointer_diff @0 (pointer_plus @@0 @1))
1892     /* The second argument of pointer_plus must be interpreted as signed, and
1893        thus sign-extended if necessary.  */
1894     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1895      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1896         second arg is unsigned even when we need to consider it as signed,
1897         we don't want to diagnose overflow here.  */
1898      (negate (convert (view_convert:stype @1)))))
1900   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1901   (simplify
1902    (minus (convert (plus:c @@0 @1))
1903     (convert (plus:c @0 @2)))
1904    (if (INTEGRAL_TYPE_P (type)
1905         && TYPE_OVERFLOW_UNDEFINED (type)
1906         && element_precision (type) <= element_precision (TREE_TYPE (@1))
1907         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
1908     (with { tree utype = unsigned_type_for (type); }
1909      (convert (minus (convert:utype @1) (convert:utype @2))))
1910     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
1911           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
1912          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
1913              /* For integer types, if A has a smaller type
1914                 than T the result depends on the possible
1915                 overflow in P + A.
1916                 E.g. T=size_t, A=(unsigned)429497295, P>0.
1917                 However, if an overflow in P + A would cause
1918                 undefined behavior, we can assume that there
1919                 is no overflow.  */
1920              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1921                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1922                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
1923                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
1924      (minus (convert @1) (convert @2)))))
1925   (simplify
1926    (minus (convert (pointer_plus @@0 @1))
1927     (convert (pointer_plus @0 @2)))
1928    (if (INTEGRAL_TYPE_P (type)
1929         && TYPE_OVERFLOW_UNDEFINED (type)
1930         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1931     (with { tree utype = unsigned_type_for (type); }
1932      (convert (minus (convert:utype @1) (convert:utype @2))))
1933     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1934          /* For pointer types, if the conversion of A to the
1935             final type requires a sign- or zero-extension,
1936             then we have to punt - it is not defined which
1937             one is correct.  */
1938          || (POINTER_TYPE_P (TREE_TYPE (@0))
1939              && TREE_CODE (@1) == INTEGER_CST
1940              && tree_int_cst_sign_bit (@1) == 0
1941              && TREE_CODE (@2) == INTEGER_CST
1942              && tree_int_cst_sign_bit (@2) == 0))
1943      (minus (convert @1) (convert @2)))))
1944    (simplify
1945     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
1946     /* The second argument of pointer_plus must be interpreted as signed, and
1947        thus sign-extended if necessary.  */
1948     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1949      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1950         second arg is unsigned even when we need to consider it as signed,
1951         we don't want to diagnose overflow here.  */
1952      (minus (convert (view_convert:stype @1))
1953             (convert (view_convert:stype @2)))))))
1955 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
1956     Modeled after fold_plusminus_mult_expr.  */
1957 (if (!TYPE_SATURATING (type)
1958      && (!FLOAT_TYPE_P (type) || flag_associative_math))
1959  (for plusminus (plus minus)
1960   (simplify
1961    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
1962    (if ((!ANY_INTEGRAL_TYPE_P (type)
1963          || TYPE_OVERFLOW_WRAPS (type)
1964          || (INTEGRAL_TYPE_P (type)
1965              && tree_expr_nonzero_p (@0)
1966              && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1967         /* If @1 +- @2 is constant require a hard single-use on either
1968            original operand (but not on both).  */
1969         && (single_use (@3) || single_use (@4)))
1970     (mult (plusminus @1 @2) @0)))
1971   /* We cannot generate constant 1 for fract.  */
1972   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
1973    (simplify
1974     (plusminus @0 (mult:c@3 @0 @2))
1975     (if ((!ANY_INTEGRAL_TYPE_P (type)
1976           || TYPE_OVERFLOW_WRAPS (type)
1977           || (INTEGRAL_TYPE_P (type)
1978               && tree_expr_nonzero_p (@0)
1979               && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1980          && single_use (@3))
1981      (mult (plusminus { build_one_cst (type); } @2) @0)))
1982    (simplify
1983     (plusminus (mult:c@3 @0 @2) @0)
1984     (if ((!ANY_INTEGRAL_TYPE_P (type)
1985           || TYPE_OVERFLOW_WRAPS (type)
1986           || (INTEGRAL_TYPE_P (type)
1987               && tree_expr_nonzero_p (@0)
1988               && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1989          && single_use (@3))
1990      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
1992 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
1994 (for minmax (min max FMIN_ALL FMAX_ALL)
1995  (simplify
1996   (minmax @0 @0)
1997   @0))
1998 /* min(max(x,y),y) -> y.  */
1999 (simplify
2000  (min:c (max:c @0 @1) @1)
2001  @1)
2002 /* max(min(x,y),y) -> y.  */
2003 (simplify
2004  (max:c (min:c @0 @1) @1)
2005  @1)
2006 /* max(a,-a) -> abs(a).  */
2007 (simplify
2008  (max:c @0 (negate @0))
2009  (if (TREE_CODE (type) != COMPLEX_TYPE
2010       && (! ANY_INTEGRAL_TYPE_P (type)
2011           || TYPE_OVERFLOW_UNDEFINED (type)))
2012   (abs @0)))
2013 /* min(a,-a) -> -abs(a).  */
2014 (simplify
2015  (min:c @0 (negate @0))
2016  (if (TREE_CODE (type) != COMPLEX_TYPE
2017       && (! ANY_INTEGRAL_TYPE_P (type)
2018           || TYPE_OVERFLOW_UNDEFINED (type)))
2019   (negate (abs @0))))
2020 (simplify
2021  (min @0 @1)
2022  (switch
2023   (if (INTEGRAL_TYPE_P (type)
2024        && TYPE_MIN_VALUE (type)
2025        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2026    @1)
2027   (if (INTEGRAL_TYPE_P (type)
2028        && TYPE_MAX_VALUE (type)
2029        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2030    @0)))
2031 (simplify
2032  (max @0 @1)
2033  (switch
2034   (if (INTEGRAL_TYPE_P (type)
2035        && TYPE_MAX_VALUE (type)
2036        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2037    @1)
2038   (if (INTEGRAL_TYPE_P (type)
2039        && TYPE_MIN_VALUE (type)
2040        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2041    @0)))
2043 /* max (a, a + CST) -> a + CST where CST is positive.  */
2044 /* max (a, a + CST) -> a where CST is negative.  */
2045 (simplify
2046  (max:c @0 (plus@2 @0 INTEGER_CST@1))
2047   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2048    (if (tree_int_cst_sgn (@1) > 0)
2049     @2
2050     @0)))
2052 /* min (a, a + CST) -> a where CST is positive.  */
2053 /* min (a, a + CST) -> a + CST where CST is negative. */
2054 (simplify
2055  (min:c @0 (plus@2 @0 INTEGER_CST@1))
2056   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2057    (if (tree_int_cst_sgn (@1) > 0)
2058     @0
2059     @2)))
2061 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2062    and the outer convert demotes the expression back to x's type.  */
2063 (for minmax (min max)
2064  (simplify
2065   (convert (minmax@0 (convert @1) INTEGER_CST@2))
2066   (if (INTEGRAL_TYPE_P (type)
2067        && types_match (@1, type) && int_fits_type_p (@2, type)
2068        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2069        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2070    (minmax @1 (convert @2)))))
2072 (for minmax (FMIN_ALL FMAX_ALL)
2073  /* If either argument is NaN, return the other one.  Avoid the
2074     transformation if we get (and honor) a signalling NaN.  */
2075  (simplify
2076   (minmax:c @0 REAL_CST@1)
2077   (if (real_isnan (TREE_REAL_CST_PTR (@1))
2078        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2079    @0)))
2080 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
2081    functions to return the numeric arg if the other one is NaN.
2082    MIN and MAX don't honor that, so only transform if -ffinite-math-only
2083    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
2084    worry about it either.  */
2085 (if (flag_finite_math_only)
2086  (simplify
2087   (FMIN_ALL @0 @1)
2088   (min @0 @1))
2089  (simplify
2090   (FMAX_ALL @0 @1)
2091   (max @0 @1)))
2092 /* min (-A, -B) -> -max (A, B)  */
2093 (for minmax (min max FMIN_ALL FMAX_ALL)
2094      maxmin (max min FMAX_ALL FMIN_ALL)
2095  (simplify
2096   (minmax (negate:s@2 @0) (negate:s@3 @1))
2097   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2098        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2099            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2100    (negate (maxmin @0 @1)))))
2101 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2102    MAX (~X, ~Y) -> ~MIN (X, Y)  */
2103 (for minmax (min max)
2104  maxmin (max min)
2105  (simplify
2106   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2107   (bit_not (maxmin @0 @1))))
2109 /* MIN (X, Y) == X -> X <= Y  */
2110 (for minmax (min min max max)
2111      cmp    (eq  ne  eq  ne )
2112      out    (le  gt  ge  lt )
2113  (simplify
2114   (cmp:c (minmax:c @0 @1) @0)
2115   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2116    (out @0 @1))))
2117 /* MIN (X, 5) == 0 -> X == 0
2118    MIN (X, 5) == 7 -> false  */
2119 (for cmp (eq ne)
2120  (simplify
2121   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2122   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2123                  TYPE_SIGN (TREE_TYPE (@0))))
2124    { constant_boolean_node (cmp == NE_EXPR, type); }
2125    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2126                   TYPE_SIGN (TREE_TYPE (@0))))
2127     (cmp @0 @2)))))
2128 (for cmp (eq ne)
2129  (simplify
2130   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2131   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2132                  TYPE_SIGN (TREE_TYPE (@0))))
2133    { constant_boolean_node (cmp == NE_EXPR, type); }
2134    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2135                   TYPE_SIGN (TREE_TYPE (@0))))
2136     (cmp @0 @2)))))
2137 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
2138 (for minmax (min     min     max     max     min     min     max     max    )
2139      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
2140      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2141  (simplify
2142   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2143   (comb (cmp @0 @2) (cmp @1 @2))))
2145 /* Simplifications of shift and rotates.  */
2147 (for rotate (lrotate rrotate)
2148  (simplify
2149   (rotate integer_all_onesp@0 @1)
2150   @0))
2152 /* Optimize -1 >> x for arithmetic right shifts.  */
2153 (simplify
2154  (rshift integer_all_onesp@0 @1)
2155  (if (!TYPE_UNSIGNED (type)
2156       && tree_expr_nonnegative_p (@1))
2157   @0))
2159 /* Optimize (x >> c) << c into x & (-1<<c).  */
2160 (simplify
2161  (lshift (rshift @0 INTEGER_CST@1) @1)
2162  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2163   (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2165 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2166    types.  */
2167 (simplify
2168  (rshift (lshift @0 INTEGER_CST@1) @1)
2169  (if (TYPE_UNSIGNED (type)
2170       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2171   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2173 (for shiftrotate (lrotate rrotate lshift rshift)
2174  (simplify
2175   (shiftrotate @0 integer_zerop)
2176   (non_lvalue @0))
2177  (simplify
2178   (shiftrotate integer_zerop@0 @1)
2179   @0)
2180  /* Prefer vector1 << scalar to vector1 << vector2
2181     if vector2 is uniform.  */
2182  (for vec (VECTOR_CST CONSTRUCTOR)
2183   (simplify
2184    (shiftrotate @0 vec@1)
2185    (with { tree tem = uniform_vector_p (@1); }
2186     (if (tem)
2187      (shiftrotate @0 { tem; }))))))
2189 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2190    Y is 0.  Similarly for X >> Y.  */
2191 #if GIMPLE
2192 (for shift (lshift rshift)
2193  (simplify
2194   (shift @0 SSA_NAME@1)
2195    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2196     (with {
2197       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2198       int prec = TYPE_PRECISION (TREE_TYPE (@1));
2199      }
2200      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2201       @0)))))
2202 #endif
2204 /* Rewrite an LROTATE_EXPR by a constant into an
2205    RROTATE_EXPR by a new constant.  */
2206 (simplify
2207  (lrotate @0 INTEGER_CST@1)
2208  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2209                             build_int_cst (TREE_TYPE (@1),
2210                                            element_precision (type)), @1); }))
2212 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
2213 (for op (lrotate rrotate rshift lshift)
2214  (simplify
2215   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2216   (with { unsigned int prec = element_precision (type); }
2217    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2218         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2219         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2220         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2221     (with { unsigned int low = (tree_to_uhwi (@1)
2222                                 + tree_to_uhwi (@2)); }
2223      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2224         being well defined.  */
2225      (if (low >= prec)
2226       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2227        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2228        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2229         { build_zero_cst (type); }
2230         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2231       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2234 /* ((1 << A) & 1) != 0 -> A == 0
2235    ((1 << A) & 1) == 0 -> A != 0 */
2236 (for cmp (ne eq)
2237      icmp (eq ne)
2238  (simplify
2239   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2240   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2242 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2243    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2244    if CST2 != 0.  */
2245 (for cmp (ne eq)
2246  (simplify
2247   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2248   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2249    (if (cand < 0
2250         || (!integer_zerop (@2)
2251             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2252     { constant_boolean_node (cmp == NE_EXPR, type); }
2253     (if (!integer_zerop (@2)
2254          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2255      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2257 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2258         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2259    if the new mask might be further optimized.  */
2260 (for shift (lshift rshift)
2261  (simplify
2262   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2263            INTEGER_CST@2)
2264    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2265         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2266         && tree_fits_uhwi_p (@1)
2267         && tree_to_uhwi (@1) > 0
2268         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2269     (with
2270      {
2271        unsigned int shiftc = tree_to_uhwi (@1);
2272        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2273        unsigned HOST_WIDE_INT newmask, zerobits = 0;
2274        tree shift_type = TREE_TYPE (@3);
2275        unsigned int prec;
2277        if (shift == LSHIFT_EXPR)
2278          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2279        else if (shift == RSHIFT_EXPR
2280                 && type_has_mode_precision_p (shift_type))
2281          {
2282            prec = TYPE_PRECISION (TREE_TYPE (@3));
2283            tree arg00 = @0;
2284            /* See if more bits can be proven as zero because of
2285               zero extension.  */
2286            if (@3 != @0
2287                && TYPE_UNSIGNED (TREE_TYPE (@0)))
2288              {
2289                tree inner_type = TREE_TYPE (@0);
2290                if (type_has_mode_precision_p (inner_type)
2291                    && TYPE_PRECISION (inner_type) < prec)
2292                  {
2293                    prec = TYPE_PRECISION (inner_type);
2294                    /* See if we can shorten the right shift.  */
2295                    if (shiftc < prec)
2296                      shift_type = inner_type;
2297                    /* Otherwise X >> C1 is all zeros, so we'll optimize
2298                       it into (X, 0) later on by making sure zerobits
2299                       is all ones.  */
2300                  }
2301              }
2302            zerobits = HOST_WIDE_INT_M1U;
2303            if (shiftc < prec)
2304              {
2305                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2306                zerobits <<= prec - shiftc;
2307              }
2308            /* For arithmetic shift if sign bit could be set, zerobits
2309               can contain actually sign bits, so no transformation is
2310               possible, unless MASK masks them all away.  In that
2311               case the shift needs to be converted into logical shift.  */
2312            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2313                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2314              {
2315                if ((mask & zerobits) == 0)
2316                  shift_type = unsigned_type_for (TREE_TYPE (@3));
2317                else
2318                  zerobits = 0;
2319              }
2320          }
2321      }
2322      /* ((X << 16) & 0xff00) is (X, 0).  */
2323      (if ((mask & zerobits) == mask)
2324       { build_int_cst (type, 0); }
2325       (with { newmask = mask | zerobits; }
2326        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2327         (with
2328          {
2329            /* Only do the transformation if NEWMASK is some integer
2330               mode's mask.  */
2331            for (prec = BITS_PER_UNIT;
2332                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2333              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2334                break;
2335          }
2336          (if (prec < HOST_BITS_PER_WIDE_INT
2337               || newmask == HOST_WIDE_INT_M1U)
2338           (with
2339            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2340            (if (!tree_int_cst_equal (newmaskt, @2))
2341             (if (shift_type != TREE_TYPE (@3))
2342              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2343              (bit_and @4 { newmaskt; })))))))))))))
2345 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2346    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
2347 (for shift (lshift rshift)
2348  (for bit_op (bit_and bit_xor bit_ior)
2349   (simplify
2350    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2351    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2352     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2353      (bit_op (shift (convert @0) @1) { mask; }))))))
2355 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
2356 (simplify
2357  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2358   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2359        && (element_precision (TREE_TYPE (@0))
2360            <= element_precision (TREE_TYPE (@1))
2361            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2362    (with
2363     { tree shift_type = TREE_TYPE (@0); }
2364      (convert (rshift (convert:shift_type @1) @2)))))
2366 /* ~(~X >>r Y) -> X >>r Y
2367    ~(~X <<r Y) -> X <<r Y */
2368 (for rotate (lrotate rrotate)
2369  (simplify
2370   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2371    (if ((element_precision (TREE_TYPE (@0))
2372          <= element_precision (TREE_TYPE (@1))
2373          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2374         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2375             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2376     (with
2377      { tree rotate_type = TREE_TYPE (@0); }
2378       (convert (rotate (convert:rotate_type @1) @2))))))
2380 /* Simplifications of conversions.  */
2382 /* Basic strip-useless-type-conversions / strip_nops.  */
2383 (for cvt (convert view_convert float fix_trunc)
2384  (simplify
2385   (cvt @0)
2386   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2387        || (GENERIC && type == TREE_TYPE (@0)))
2388    @0)))
2390 /* Contract view-conversions.  */
2391 (simplify
2392   (view_convert (view_convert @0))
2393   (view_convert @0))
2395 /* For integral conversions with the same precision or pointer
2396    conversions use a NOP_EXPR instead.  */
2397 (simplify
2398   (view_convert @0)
2399   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2400        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2401        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2402    (convert @0)))
2404 /* Strip inner integral conversions that do not change precision or size, or
2405    zero-extend while keeping the same size (for bool-to-char).  */
2406 (simplify
2407   (view_convert (convert@0 @1))
2408   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2409        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2410        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2411        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2412            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2413                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2414    (view_convert @1)))
2416 /* Re-association barriers around constants and other re-association
2417    barriers can be removed.  */
2418 (simplify
2419  (paren CONSTANT_CLASS_P@0)
2420  @0)
2421 (simplify
2422  (paren (paren@1 @0))
2423  @1)
2425 /* Handle cases of two conversions in a row.  */
2426 (for ocvt (convert float fix_trunc)
2427  (for icvt (convert float)
2428   (simplify
2429    (ocvt (icvt@1 @0))
2430    (with
2431     {
2432       tree inside_type = TREE_TYPE (@0);
2433       tree inter_type = TREE_TYPE (@1);
2434       int inside_int = INTEGRAL_TYPE_P (inside_type);
2435       int inside_ptr = POINTER_TYPE_P (inside_type);
2436       int inside_float = FLOAT_TYPE_P (inside_type);
2437       int inside_vec = VECTOR_TYPE_P (inside_type);
2438       unsigned int inside_prec = TYPE_PRECISION (inside_type);
2439       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2440       int inter_int = INTEGRAL_TYPE_P (inter_type);
2441       int inter_ptr = POINTER_TYPE_P (inter_type);
2442       int inter_float = FLOAT_TYPE_P (inter_type);
2443       int inter_vec = VECTOR_TYPE_P (inter_type);
2444       unsigned int inter_prec = TYPE_PRECISION (inter_type);
2445       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2446       int final_int = INTEGRAL_TYPE_P (type);
2447       int final_ptr = POINTER_TYPE_P (type);
2448       int final_float = FLOAT_TYPE_P (type);
2449       int final_vec = VECTOR_TYPE_P (type);
2450       unsigned int final_prec = TYPE_PRECISION (type);
2451       int final_unsignedp = TYPE_UNSIGNED (type);
2452     }
2453    (switch
2454     /* In addition to the cases of two conversions in a row
2455        handled below, if we are converting something to its own
2456        type via an object of identical or wider precision, neither
2457        conversion is needed.  */
2458     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2459           || (GENERIC
2460               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2461          && (((inter_int || inter_ptr) && final_int)
2462              || (inter_float && final_float))
2463          && inter_prec >= final_prec)
2464      (ocvt @0))
2466     /* Likewise, if the intermediate and initial types are either both
2467        float or both integer, we don't need the middle conversion if the
2468        former is wider than the latter and doesn't change the signedness
2469        (for integers).  Avoid this if the final type is a pointer since
2470        then we sometimes need the middle conversion.  */
2471     (if (((inter_int && inside_int) || (inter_float && inside_float))
2472          && (final_int || final_float)
2473          && inter_prec >= inside_prec
2474          && (inter_float || inter_unsignedp == inside_unsignedp))
2475      (ocvt @0))
2477     /* If we have a sign-extension of a zero-extended value, we can
2478        replace that by a single zero-extension.  Likewise if the
2479        final conversion does not change precision we can drop the
2480        intermediate conversion.  */
2481     (if (inside_int && inter_int && final_int
2482          && ((inside_prec < inter_prec && inter_prec < final_prec
2483               && inside_unsignedp && !inter_unsignedp)
2484              || final_prec == inter_prec))
2485      (ocvt @0))
2487     /* Two conversions in a row are not needed unless:
2488         - some conversion is floating-point (overstrict for now), or
2489         - some conversion is a vector (overstrict for now), or
2490         - the intermediate type is narrower than both initial and
2491           final, or
2492         - the intermediate type and innermost type differ in signedness,
2493           and the outermost type is wider than the intermediate, or
2494         - the initial type is a pointer type and the precisions of the
2495           intermediate and final types differ, or
2496         - the final type is a pointer type and the precisions of the
2497           initial and intermediate types differ.  */
2498     (if (! inside_float && ! inter_float && ! final_float
2499          && ! inside_vec && ! inter_vec && ! final_vec
2500          && (inter_prec >= inside_prec || inter_prec >= final_prec)
2501          && ! (inside_int && inter_int
2502                && inter_unsignedp != inside_unsignedp
2503                && inter_prec < final_prec)
2504          && ((inter_unsignedp && inter_prec > inside_prec)
2505              == (final_unsignedp && final_prec > inter_prec))
2506          && ! (inside_ptr && inter_prec != final_prec)
2507          && ! (final_ptr && inside_prec != inter_prec))
2508      (ocvt @0))
2510     /* A truncation to an unsigned type (a zero-extension) should be
2511        canonicalized as bitwise and of a mask.  */
2512     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
2513          && final_int && inter_int && inside_int
2514          && final_prec == inside_prec
2515          && final_prec > inter_prec
2516          && inter_unsignedp)
2517      (convert (bit_and @0 { wide_int_to_tree
2518                               (inside_type,
2519                                wi::mask (inter_prec, false,
2520                                          TYPE_PRECISION (inside_type))); })))
2522     /* If we are converting an integer to a floating-point that can
2523        represent it exactly and back to an integer, we can skip the
2524        floating-point conversion.  */
2525     (if (GIMPLE /* PR66211 */
2526          && inside_int && inter_float && final_int &&
2527          (unsigned) significand_size (TYPE_MODE (inter_type))
2528          >= inside_prec - !inside_unsignedp)
2529      (convert @0)))))))
2531 /* If we have a narrowing conversion to an integral type that is fed by a
2532    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2533    masks off bits outside the final type (and nothing else).  */
2534 (simplify
2535   (convert (bit_and @0 INTEGER_CST@1))
2536   (if (INTEGRAL_TYPE_P (type)
2537        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2538        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2539        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2540                                                     TYPE_PRECISION (type)), 0))
2541    (convert @0)))
2544 /* (X /[ex] A) * A -> X.  */
2545 (simplify
2546   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2547   (convert @0))
2549 /* Canonicalization of binary operations.  */
2551 /* Convert X + -C into X - C.  */
2552 (simplify
2553  (plus @0 REAL_CST@1)
2554  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2555   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2556    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2557     (minus @0 { tem; })))))
2559 /* Convert x+x into x*2.  */
2560 (simplify
2561  (plus @0 @0)
2562  (if (SCALAR_FLOAT_TYPE_P (type))
2563   (mult @0 { build_real (type, dconst2); })
2564   (if (INTEGRAL_TYPE_P (type))
2565    (mult @0 { build_int_cst (type, 2); }))))
2567 /* 0 - X  ->  -X.  */
2568 (simplify
2569  (minus integer_zerop @1)
2570  (negate @1))
2571 (simplify
2572  (pointer_diff integer_zerop @1)
2573  (negate (convert @1)))
2575 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
2576    ARG0 is zero and X + ARG0 reduces to X, since that would mean
2577    (-ARG1 + ARG0) reduces to -ARG1.  */
2578 (simplify
2579  (minus real_zerop@0 @1)
2580  (if (fold_real_zero_addition_p (type, @0, 0))
2581   (negate @1)))
2583 /* Transform x * -1 into -x.  */
2584 (simplify
2585  (mult @0 integer_minus_onep)
2586  (negate @0))
2588 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
2589    signed overflow for CST != 0 && CST != -1.  */
2590 (simplify
2591  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2592  (if (TREE_CODE (@2) != INTEGER_CST
2593       && single_use (@3)
2594       && !integer_zerop (@1) && !integer_minus_onep (@1))
2595   (mult (mult @0 @2) @1)))
2597 /* True if we can easily extract the real and imaginary parts of a complex
2598    number.  */
2599 (match compositional_complex
2600  (convert? (complex @0 @1)))
2602 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
2603 (simplify
2604  (complex (realpart @0) (imagpart @0))
2605  @0)
2606 (simplify
2607  (realpart (complex @0 @1))
2608  @0)
2609 (simplify
2610  (imagpart (complex @0 @1))
2611  @1)
2613 /* Sometimes we only care about half of a complex expression.  */
2614 (simplify
2615  (realpart (convert?:s (conj:s @0)))
2616  (convert (realpart @0)))
2617 (simplify
2618  (imagpart (convert?:s (conj:s @0)))
2619  (convert (negate (imagpart @0))))
2620 (for part (realpart imagpart)
2621  (for op (plus minus)
2622   (simplify
2623    (part (convert?:s@2 (op:s @0 @1)))
2624    (convert (op (part @0) (part @1))))))
2625 (simplify
2626  (realpart (convert?:s (CEXPI:s @0)))
2627  (convert (COS @0)))
2628 (simplify
2629  (imagpart (convert?:s (CEXPI:s @0)))
2630  (convert (SIN @0)))
2632 /* conj(conj(x)) -> x  */
2633 (simplify
2634  (conj (convert? (conj @0)))
2635  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2636   (convert @0)))
2638 /* conj({x,y}) -> {x,-y}  */
2639 (simplify
2640  (conj (convert?:s (complex:s @0 @1)))
2641  (with { tree itype = TREE_TYPE (type); }
2642   (complex (convert:itype @0) (negate (convert:itype @1)))))
2644 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
2645 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2646  (simplify
2647   (bswap (bswap @0))
2648   @0)
2649  (simplify
2650   (bswap (bit_not (bswap @0)))
2651   (bit_not @0))
2652  (for bitop (bit_xor bit_ior bit_and)
2653   (simplify
2654    (bswap (bitop:c (bswap @0) @1))
2655    (bitop @0 (bswap @1)))))
2658 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
2660 /* Simplify constant conditions.
2661    Only optimize constant conditions when the selected branch
2662    has the same type as the COND_EXPR.  This avoids optimizing
2663    away "c ? x : throw", where the throw has a void type.
2664    Note that we cannot throw away the fold-const.c variant nor
2665    this one as we depend on doing this transform before possibly
2666    A ? B : B -> B triggers and the fold-const.c one can optimize
2667    0 ? A : B to B even if A has side-effects.  Something
2668    genmatch cannot handle.  */
2669 (simplify
2670  (cond INTEGER_CST@0 @1 @2)
2671  (if (integer_zerop (@0))
2672   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2673    @2)
2674   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2675    @1)))
2676 (simplify
2677  (vec_cond VECTOR_CST@0 @1 @2)
2678  (if (integer_all_onesp (@0))
2679   @1
2680   (if (integer_zerop (@0))
2681    @2)))
2683 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
2684    be extended.  */
2685 /* This pattern implements two kinds simplification:
2687    Case 1)
2688    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2689      1) Conversions are type widening from smaller type.
2690      2) Const c1 equals to c2 after canonicalizing comparison.
2691      3) Comparison has tree code LT, LE, GT or GE.
2692    This specific pattern is needed when (cmp (convert x) c) may not
2693    be simplified by comparison patterns because of multiple uses of
2694    x.  It also makes sense here because simplifying across multiple
2695    referred var is always benefitial for complicated cases.
2697    Case 2)
2698    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
2699 (for cmp (lt le gt ge eq)
2700  (simplify
2701   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2702   (with
2703    {
2704      tree from_type = TREE_TYPE (@1);
2705      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2706      enum tree_code code = ERROR_MARK;
2708      if (INTEGRAL_TYPE_P (from_type)
2709          && int_fits_type_p (@2, from_type)
2710          && (types_match (c1_type, from_type)
2711              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2712                  && (TYPE_UNSIGNED (from_type)
2713                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2714          && (types_match (c2_type, from_type)
2715              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2716                  && (TYPE_UNSIGNED (from_type)
2717                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2718        {
2719          if (cmp != EQ_EXPR)
2720            {
2721              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2722                {
2723                  /* X <= Y - 1 equals to X < Y.  */
2724                  if (cmp == LE_EXPR)
2725                    code = LT_EXPR;
2726                  /* X > Y - 1 equals to X >= Y.  */
2727                  if (cmp == GT_EXPR)
2728                    code = GE_EXPR;
2729                }
2730              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2731                {
2732                  /* X < Y + 1 equals to X <= Y.  */
2733                  if (cmp == LT_EXPR)
2734                    code = LE_EXPR;
2735                  /* X >= Y + 1 equals to X > Y.  */
2736                  if (cmp == GE_EXPR)
2737                    code = GT_EXPR;
2738                }
2739              if (code != ERROR_MARK
2740                  || wi::to_widest (@2) == wi::to_widest (@3))
2741                {
2742                  if (cmp == LT_EXPR || cmp == LE_EXPR)
2743                    code = MIN_EXPR;
2744                  if (cmp == GT_EXPR || cmp == GE_EXPR)
2745                    code = MAX_EXPR;
2746                }
2747            }
2748          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
2749          else if (int_fits_type_p (@3, from_type))
2750            code = EQ_EXPR;
2751        }
2752    }
2753    (if (code == MAX_EXPR)
2754     (convert (max @1 (convert @2)))
2755     (if (code == MIN_EXPR)
2756      (convert (min @1 (convert @2)))
2757      (if (code == EQ_EXPR)
2758       (convert (cond (eq @1 (convert @3))
2759                      (convert:from_type @3) (convert:from_type @2)))))))))
2761 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2763      1) OP is PLUS or MINUS.
2764      2) CMP is LT, LE, GT or GE.
2765      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2767    This pattern also handles special cases like:
2769      A) Operand x is a unsigned to signed type conversion and c1 is
2770         integer zero.  In this case,
2771           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
2772           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
2773      B) Const c1 may not equal to (C3 op' C2).  In this case we also
2774         check equality for (c1+1) and (c1-1) by adjusting comparison
2775         code.
2777    TODO: Though signed type is handled by this pattern, it cannot be
2778    simplified at the moment because C standard requires additional
2779    type promotion.  In order to match&simplify it here, the IR needs
2780    to be cleaned up by other optimizers, i.e, VRP.  */
2781 (for op (plus minus)
2782  (for cmp (lt le gt ge)
2783   (simplify
2784    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2785    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2786     (if (types_match (from_type, to_type)
2787          /* Check if it is special case A).  */
2788          || (TYPE_UNSIGNED (from_type)
2789              && !TYPE_UNSIGNED (to_type)
2790              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2791              && integer_zerop (@1)
2792              && (cmp == LT_EXPR || cmp == GE_EXPR)))
2793      (with
2794       {
2795         bool overflow = false;
2796         enum tree_code code, cmp_code = cmp;
2797         wide_int real_c1;
2798         wide_int c1 = wi::to_wide (@1);
2799         wide_int c2 = wi::to_wide (@2);
2800         wide_int c3 = wi::to_wide (@3);
2801         signop sgn = TYPE_SIGN (from_type);
2803         /* Handle special case A), given x of unsigned type:
2804             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
2805             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
2806         if (!types_match (from_type, to_type))
2807           {
2808             if (cmp_code == LT_EXPR)
2809               cmp_code = GT_EXPR;
2810             if (cmp_code == GE_EXPR)
2811               cmp_code = LE_EXPR;
2812             c1 = wi::max_value (to_type);
2813           }
2814         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
2815            compute (c3 op' c2) and check if it equals to c1 with op' being
2816            the inverted operator of op.  Make sure overflow doesn't happen
2817            if it is undefined.  */
2818         if (op == PLUS_EXPR)
2819           real_c1 = wi::sub (c3, c2, sgn, &overflow);
2820         else
2821           real_c1 = wi::add (c3, c2, sgn, &overflow);
2823         code = cmp_code;
2824         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2825           {
2826             /* Check if c1 equals to real_c1.  Boundary condition is handled
2827                by adjusting comparison operation if necessary.  */
2828             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2829                 && !overflow)
2830               {
2831                 /* X <= Y - 1 equals to X < Y.  */
2832                 if (cmp_code == LE_EXPR)
2833                   code = LT_EXPR;
2834                 /* X > Y - 1 equals to X >= Y.  */
2835                 if (cmp_code == GT_EXPR)
2836                   code = GE_EXPR;
2837               }
2838             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2839                 && !overflow)
2840               {
2841                 /* X < Y + 1 equals to X <= Y.  */
2842                 if (cmp_code == LT_EXPR)
2843                   code = LE_EXPR;
2844                 /* X >= Y + 1 equals to X > Y.  */
2845                 if (cmp_code == GE_EXPR)
2846                   code = GT_EXPR;
2847               }
2848             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2849               {
2850                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2851                   code = MIN_EXPR;
2852                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2853                   code = MAX_EXPR;
2854               }
2855           }
2856       }
2857       (if (code == MAX_EXPR)
2858        (op (max @X { wide_int_to_tree (from_type, real_c1); })
2859            { wide_int_to_tree (from_type, c2); })
2860        (if (code == MIN_EXPR)
2861         (op (min @X { wide_int_to_tree (from_type, real_c1); })
2862             { wide_int_to_tree (from_type, c2); })))))))))
2864 (for cnd (cond vec_cond)
2865  /* A ? B : (A ? X : C) -> A ? B : C.  */
2866  (simplify
2867   (cnd @0 (cnd @0 @1 @2) @3)
2868   (cnd @0 @1 @3))
2869  (simplify
2870   (cnd @0 @1 (cnd @0 @2 @3))
2871   (cnd @0 @1 @3))
2872  /* A ? B : (!A ? C : X) -> A ? B : C.  */
2873  /* ???  This matches embedded conditions open-coded because genmatch
2874     would generate matching code for conditions in separate stmts only.
2875     The following is still important to merge then and else arm cases
2876     from if-conversion.  */
2877  (simplify
2878   (cnd @0 @1 (cnd @2 @3 @4))
2879   (if (COMPARISON_CLASS_P (@0)
2880        && COMPARISON_CLASS_P (@2)
2881        && invert_tree_comparison
2882            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2883        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2884        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2885    (cnd @0 @1 @3)))
2886  (simplify
2887   (cnd @0 (cnd @1 @2 @3) @4)
2888   (if (COMPARISON_CLASS_P (@0)
2889        && COMPARISON_CLASS_P (@1)
2890        && invert_tree_comparison
2891            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2892        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2893        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2894    (cnd @0 @3 @4)))
2896  /* A ? B : B -> B.  */
2897  (simplify
2898   (cnd @0 @1 @1)
2899   @1)
2901  /* !A ? B : C -> A ? C : B.  */
2902  (simplify
2903   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2904   (cnd @0 @2 @1)))
2906 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2907    return all -1 or all 0 results.  */
2908 /* ??? We could instead convert all instances of the vec_cond to negate,
2909    but that isn't necessarily a win on its own.  */
2910 (simplify
2911  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2912  (if (VECTOR_TYPE_P (type)
2913       && known_eq (TYPE_VECTOR_SUBPARTS (type),
2914                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2915       && (TYPE_MODE (TREE_TYPE (type))
2916           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2917   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2919 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
2920 (simplify
2921  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2922  (if (VECTOR_TYPE_P (type)
2923       && known_eq (TYPE_VECTOR_SUBPARTS (type),
2924                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2925       && (TYPE_MODE (TREE_TYPE (type))
2926           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2927   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2930 /* Simplifications of comparisons.  */
2932 /* See if we can reduce the magnitude of a constant involved in a
2933    comparison by changing the comparison code.  This is a canonicalization
2934    formerly done by maybe_canonicalize_comparison_1.  */
2935 (for cmp  (le gt)
2936      acmp (lt ge)
2937  (simplify
2938   (cmp @0 INTEGER_CST@1)
2939   (if (tree_int_cst_sgn (@1) == -1)
2940    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2941 (for cmp  (ge lt)
2942      acmp (gt le)
2943  (simplify
2944   (cmp @0 INTEGER_CST@1)
2945   (if (tree_int_cst_sgn (@1) == 1)
2946    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2949 /* We can simplify a logical negation of a comparison to the
2950    inverted comparison.  As we cannot compute an expression
2951    operator using invert_tree_comparison we have to simulate
2952    that with expression code iteration.  */
2953 (for cmp (tcc_comparison)
2954      icmp (inverted_tcc_comparison)
2955      ncmp (inverted_tcc_comparison_with_nans)
2956  /* Ideally we'd like to combine the following two patterns
2957     and handle some more cases by using
2958       (logical_inverted_value (cmp @0 @1))
2959     here but for that genmatch would need to "inline" that.
2960     For now implement what forward_propagate_comparison did.  */
2961  (simplify
2962   (bit_not (cmp @0 @1))
2963   (if (VECTOR_TYPE_P (type)
2964        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2965    /* Comparison inversion may be impossible for trapping math,
2966       invert_tree_comparison will tell us.  But we can't use
2967       a computed operator in the replacement tree thus we have
2968       to play the trick below.  */
2969    (with { enum tree_code ic = invert_tree_comparison
2970              (cmp, HONOR_NANS (@0)); }
2971     (if (ic == icmp)
2972      (icmp @0 @1)
2973      (if (ic == ncmp)
2974       (ncmp @0 @1))))))
2975  (simplify
2976   (bit_xor (cmp @0 @1) integer_truep)
2977   (with { enum tree_code ic = invert_tree_comparison
2978             (cmp, HONOR_NANS (@0)); }
2979    (if (ic == icmp)
2980     (icmp @0 @1)
2981     (if (ic == ncmp)
2982      (ncmp @0 @1))))))
2984 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2985    ??? The transformation is valid for the other operators if overflow
2986    is undefined for the type, but performing it here badly interacts
2987    with the transformation in fold_cond_expr_with_comparison which
2988    attempts to synthetize ABS_EXPR.  */
2989 (for cmp (eq ne)
2990  (for sub (minus pointer_diff)
2991   (simplify
2992    (cmp (sub@2 @0 @1) integer_zerop)
2993    (if (single_use (@2))
2994     (cmp @0 @1)))))
2996 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2997    signed arithmetic case.  That form is created by the compiler
2998    often enough for folding it to be of value.  One example is in
2999    computing loop trip counts after Operator Strength Reduction.  */
3000 (for cmp (simple_comparison)
3001      scmp (swapped_simple_comparison)
3002  (simplify
3003   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3004   /* Handle unfolded multiplication by zero.  */
3005   (if (integer_zerop (@1))
3006    (cmp @1 @2)
3007    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3008         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3009         && single_use (@3))
3010     /* If @1 is negative we swap the sense of the comparison.  */
3011     (if (tree_int_cst_sgn (@1) < 0)
3012      (scmp @0 @2)
3013      (cmp @0 @2))))))
3015 /* Simplify comparison of something with itself.  For IEEE
3016    floating-point, we can only do some of these simplifications.  */
3017 (for cmp (eq ge le)
3018  (simplify
3019   (cmp @0 @0)
3020   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3021        || ! HONOR_NANS (@0))
3022    { constant_boolean_node (true, type); }
3023    (if (cmp != EQ_EXPR)
3024     (eq @0 @0)))))
3025 (for cmp (ne gt lt)
3026  (simplify
3027   (cmp @0 @0)
3028   (if (cmp != NE_EXPR
3029        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3030        || ! HONOR_NANS (@0))
3031    { constant_boolean_node (false, type); })))
3032 (for cmp (unle unge uneq)
3033  (simplify
3034   (cmp @0 @0)
3035   { constant_boolean_node (true, type); }))
3036 (for cmp (unlt ungt)
3037  (simplify
3038   (cmp @0 @0)
3039   (unordered @0 @0)))
3040 (simplify
3041  (ltgt @0 @0)
3042  (if (!flag_trapping_math)
3043   { constant_boolean_node (false, type); }))
3045 /* Fold ~X op ~Y as Y op X.  */
3046 (for cmp (simple_comparison)
3047  (simplify
3048   (cmp (bit_not@2 @0) (bit_not@3 @1))
3049   (if (single_use (@2) && single_use (@3))
3050    (cmp @1 @0))))
3052 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
3053 (for cmp (simple_comparison)
3054      scmp (swapped_simple_comparison)
3055  (simplify
3056   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3057   (if (single_use (@2)
3058        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3059    (scmp @0 (bit_not @1)))))
3061 (for cmp (simple_comparison)
3062  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
3063  (simplify
3064   (cmp (convert@2 @0) (convert? @1))
3065   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3066        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3067            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3068        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3069            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3070    (with
3071     {
3072       tree type1 = TREE_TYPE (@1);
3073       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3074         {
3075           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3076           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3077               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3078             type1 = float_type_node;
3079           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3080               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3081             type1 = double_type_node;
3082         }
3083       tree newtype
3084         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3085            ? TREE_TYPE (@0) : type1); 
3086     }
3087     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3088      (cmp (convert:newtype @0) (convert:newtype @1))))))
3090  (simplify
3091   (cmp @0 REAL_CST@1)
3092   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
3093   (switch
3094    /* a CMP (-0) -> a CMP 0  */
3095    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3096     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3097    /* x != NaN is always true, other ops are always false.  */
3098    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3099         && ! HONOR_SNANS (@1))
3100     { constant_boolean_node (cmp == NE_EXPR, type); })
3101    /* Fold comparisons against infinity.  */
3102    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3103         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3104     (with
3105      {
3106        REAL_VALUE_TYPE max;
3107        enum tree_code code = cmp;
3108        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3109        if (neg)
3110          code = swap_tree_comparison (code);
3111      }
3112      (switch
3113       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
3114       (if (code == GT_EXPR
3115            && !(HONOR_NANS (@0) && flag_trapping_math))
3116        { constant_boolean_node (false, type); })
3117       (if (code == LE_EXPR)
3118        /* x <= +Inf is always true, if we don't care about NaNs.  */
3119        (if (! HONOR_NANS (@0))
3120         { constant_boolean_node (true, type); }
3121         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3122            an "invalid" exception.  */
3123         (if (!flag_trapping_math)
3124          (eq @0 @0))))
3125       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3126          for == this introduces an exception for x a NaN.  */
3127       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3128            || code == GE_EXPR)
3129        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3130         (if (neg)
3131          (lt @0 { build_real (TREE_TYPE (@0), max); })
3132          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3133       /* x < +Inf is always equal to x <= DBL_MAX.  */
3134       (if (code == LT_EXPR)
3135        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3136         (if (neg)
3137          (ge @0 { build_real (TREE_TYPE (@0), max); })
3138          (le @0 { build_real (TREE_TYPE (@0), max); }))))
3139       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3140          an exception for x a NaN so use an unordered comparison.  */
3141       (if (code == NE_EXPR)
3142        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3143         (if (! HONOR_NANS (@0))
3144          (if (neg)
3145           (ge @0 { build_real (TREE_TYPE (@0), max); })
3146           (le @0 { build_real (TREE_TYPE (@0), max); }))
3147          (if (neg)
3148           (unge @0 { build_real (TREE_TYPE (@0), max); })
3149           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3151  /* If this is a comparison of a real constant with a PLUS_EXPR
3152     or a MINUS_EXPR of a real constant, we can convert it into a
3153     comparison with a revised real constant as long as no overflow
3154     occurs when unsafe_math_optimizations are enabled.  */
3155  (if (flag_unsafe_math_optimizations)
3156   (for op (plus minus)
3157    (simplify
3158     (cmp (op @0 REAL_CST@1) REAL_CST@2)
3159     (with
3160      {
3161        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3162                                TREE_TYPE (@1), @2, @1);
3163      }
3164      (if (tem && !TREE_OVERFLOW (tem))
3165       (cmp @0 { tem; }))))))
3167  /* Likewise, we can simplify a comparison of a real constant with
3168     a MINUS_EXPR whose first operand is also a real constant, i.e.
3169     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
3170     floating-point types only if -fassociative-math is set.  */
3171  (if (flag_associative_math)
3172   (simplify
3173    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3174    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3175     (if (tem && !TREE_OVERFLOW (tem))
3176      (cmp { tem; } @1)))))
3178  /* Fold comparisons against built-in math functions.  */
3179  (if (flag_unsafe_math_optimizations
3180       && ! flag_errno_math)
3181   (for sq (SQRT)
3182    (simplify
3183     (cmp (sq @0) REAL_CST@1)
3184     (switch
3185      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3186       (switch
3187        /* sqrt(x) < y is always false, if y is negative.  */
3188        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3189         { constant_boolean_node (false, type); })
3190        /* sqrt(x) > y is always true, if y is negative and we
3191           don't care about NaNs, i.e. negative values of x.  */
3192        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3193         { constant_boolean_node (true, type); })
3194        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
3195        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3196      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3197       (switch
3198        /* sqrt(x) < 0 is always false.  */
3199        (if (cmp == LT_EXPR)
3200         { constant_boolean_node (false, type); })
3201        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
3202        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3203         { constant_boolean_node (true, type); })
3204        /* sqrt(x) <= 0 -> x == 0.  */
3205        (if (cmp == LE_EXPR)
3206         (eq @0 @1))
3207        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
3208           == or !=.  In the last case:
3210             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3212           if x is negative or NaN.  Due to -funsafe-math-optimizations,
3213           the results for other x follow from natural arithmetic.  */
3214        (cmp @0 @1)))
3215      (if (cmp == GT_EXPR || cmp == GE_EXPR)
3216       (with
3217        {
3218          REAL_VALUE_TYPE c2;
3219          real_arithmetic (&c2, MULT_EXPR,
3220                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3221          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3222        }
3223        (if (REAL_VALUE_ISINF (c2))
3224         /* sqrt(x) > y is x == +Inf, when y is very large.  */
3225         (if (HONOR_INFINITIES (@0))
3226          (eq @0 { build_real (TREE_TYPE (@0), c2); })
3227          { constant_boolean_node (false, type); })
3228         /* sqrt(x) > c is the same as x > c*c.  */
3229         (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3230      (if (cmp == LT_EXPR || cmp == LE_EXPR)
3231       (with
3232        {
3233          REAL_VALUE_TYPE c2;
3234          real_arithmetic (&c2, MULT_EXPR,
3235                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3236          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3237        }
3238        (if (REAL_VALUE_ISINF (c2))
3239         (switch
3240          /* sqrt(x) < y is always true, when y is a very large
3241             value and we don't care about NaNs or Infinities.  */
3242          (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3243           { constant_boolean_node (true, type); })
3244          /* sqrt(x) < y is x != +Inf when y is very large and we
3245             don't care about NaNs.  */
3246          (if (! HONOR_NANS (@0))
3247           (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3248          /* sqrt(x) < y is x >= 0 when y is very large and we
3249             don't care about Infinities.  */
3250          (if (! HONOR_INFINITIES (@0))
3251           (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3252          /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
3253          (if (GENERIC)
3254           (truth_andif
3255            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3256            (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3257         /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
3258         (if (! HONOR_NANS (@0))
3259          (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3260          /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
3261          (if (GENERIC)
3262           (truth_andif
3263            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3264            (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3265    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
3266    (simplify
3267     (cmp (sq @0) (sq @1))
3268       (if (! HONOR_NANS (@0))
3269         (cmp @0 @1))))))
3271 /* Optimize various special cases of (FTYPE) N CMP CST.  */
3272 (for cmp  (lt le eq ne ge gt)
3273      icmp (le le eq ne ge ge)
3274  (simplify
3275   (cmp (float @0) REAL_CST@1)
3276    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3277         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3278     (with
3279      {
3280        tree itype = TREE_TYPE (@0);
3281        signop isign = TYPE_SIGN (itype);
3282        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3283        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3284        /* Be careful to preserve any potential exceptions due to
3285           NaNs.  qNaNs are ok in == or != context.
3286           TODO: relax under -fno-trapping-math or
3287           -fno-signaling-nans.  */
3288        bool exception_p
3289          = real_isnan (cst) && (cst->signalling
3290                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3291        /* INT?_MIN is power-of-two so it takes
3292           only one mantissa bit.  */
3293        bool signed_p = isign == SIGNED;
3294        bool itype_fits_ftype_p
3295          = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3296      }
3297      /* TODO: allow non-fitting itype and SNaNs when
3298         -fno-trapping-math.  */
3299      (if (itype_fits_ftype_p && ! exception_p)
3300       (with
3301        {
3302          REAL_VALUE_TYPE imin, imax;
3303          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3304          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3306          REAL_VALUE_TYPE icst;
3307          if (cmp == GT_EXPR || cmp == GE_EXPR)
3308            real_ceil (&icst, fmt, cst);
3309          else if (cmp == LT_EXPR || cmp == LE_EXPR)
3310            real_floor (&icst, fmt, cst);
3311          else
3312            real_trunc (&icst, fmt, cst);
3314          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3316          bool overflow_p = false;
3317          wide_int icst_val
3318            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3319        }
3320        (switch
3321         /* Optimize cases when CST is outside of ITYPE's range.  */
3322         (if (real_compare (LT_EXPR, cst, &imin))
3323          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3324                                   type); })
3325         (if (real_compare (GT_EXPR, cst, &imax))
3326          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3327                                   type); })
3328         /* Remove cast if CST is an integer representable by ITYPE.  */
3329         (if (cst_int_p)
3330          (cmp @0 { gcc_assert (!overflow_p);
3331                    wide_int_to_tree (itype, icst_val); })
3332         )
3333         /* When CST is fractional, optimize
3334             (FTYPE) N == CST -> 0
3335             (FTYPE) N != CST -> 1.  */
3336         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3337          { constant_boolean_node (cmp == NE_EXPR, type); }) 
3338         /* Otherwise replace with sensible integer constant.  */
3339         (with
3340          {
3341            gcc_checking_assert (!overflow_p);
3342          }
3343          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3345 /* Fold A /[ex] B CMP C to A CMP B * C.  */
3346 (for cmp (eq ne)
3347  (simplify
3348   (cmp (exact_div @0 @1) INTEGER_CST@2)
3349   (if (!integer_zerop (@1))
3350    (if (wi::to_wide (@2) == 0)
3351     (cmp @0 @2)
3352     (if (TREE_CODE (@1) == INTEGER_CST)
3353      (with
3354       {
3355         bool ovf;
3356         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3357                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3358       }
3359       (if (ovf)
3360        { constant_boolean_node (cmp == NE_EXPR, type); }
3361        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3362 (for cmp (lt le gt ge)
3363  (simplify
3364   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3365   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3366    (with
3367     {
3368       bool ovf;
3369       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3370                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3371     }
3372     (if (ovf)
3373      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3374                                         TYPE_SIGN (TREE_TYPE (@2)))
3375                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3376      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3378 /* Unordered tests if either argument is a NaN.  */
3379 (simplify
3380  (bit_ior (unordered @0 @0) (unordered @1 @1))
3381  (if (types_match (@0, @1))
3382   (unordered @0 @1)))
3383 (simplify
3384  (bit_and (ordered @0 @0) (ordered @1 @1))
3385  (if (types_match (@0, @1))
3386   (ordered @0 @1)))
3387 (simplify
3388  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3389  @2)
3390 (simplify
3391  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3392  @2)
3394 /* Simple range test simplifications.  */
3395 /* A < B || A >= B -> true.  */
3396 (for test1 (lt le le le ne ge)
3397      test2 (ge gt ge ne eq ne)
3398  (simplify
3399   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3400   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3401        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3402    { constant_boolean_node (true, type); })))
3403 /* A < B && A >= B -> false.  */
3404 (for test1 (lt lt lt le ne eq)
3405      test2 (ge gt eq gt eq gt)
3406  (simplify
3407   (bit_and:c (test1 @0 @1) (test2 @0 @1))
3408   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3409        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3410    { constant_boolean_node (false, type); })))
3412 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3413    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
3415    Note that comparisons
3416      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
3417      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
3418    will be canonicalized to above so there's no need to
3419    consider them here.
3420  */
3422 (for cmp (le gt)
3423      eqcmp (eq ne)
3424  (simplify
3425   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3426   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3427    (with
3428     {
3429      tree ty = TREE_TYPE (@0);
3430      unsigned prec = TYPE_PRECISION (ty);
3431      wide_int mask = wi::to_wide (@2, prec);
3432      wide_int rhs = wi::to_wide (@3, prec);
3433      signop sgn = TYPE_SIGN (ty);
3434     }
3435     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3436          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3437       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3438              { build_zero_cst (ty); }))))))
3440 /* -A CMP -B -> B CMP A.  */
3441 (for cmp (tcc_comparison)
3442      scmp (swapped_tcc_comparison)
3443  (simplify
3444   (cmp (negate @0) (negate @1))
3445   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3446        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3447            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3448    (scmp @0 @1)))
3449  (simplify
3450   (cmp (negate @0) CONSTANT_CLASS_P@1)
3451   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3452        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3453            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3454    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3455     (if (tem && !TREE_OVERFLOW (tem))
3456      (scmp @0 { tem; }))))))
3458 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
3459 (for op (eq ne)
3460  (simplify
3461   (op (abs @0) zerop@1)
3462   (op @0 @1)))
3464 /* From fold_sign_changed_comparison and fold_widened_comparison.
3465    FIXME: the lack of symmetry is disturbing.  */
3466 (for cmp (simple_comparison)
3467  (simplify
3468   (cmp (convert@0 @00) (convert?@1 @10))
3469   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3470        /* Disable this optimization if we're casting a function pointer
3471           type on targets that require function pointer canonicalization.  */
3472        && !(targetm.have_canonicalize_funcptr_for_compare ()
3473             && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3474             && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3475        && single_use (@0))
3476    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3477         && (TREE_CODE (@10) == INTEGER_CST
3478             || @1 != @10)
3479         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3480             || cmp == NE_EXPR
3481             || cmp == EQ_EXPR)
3482         && !POINTER_TYPE_P (TREE_TYPE (@00)))
3483     /* ???  The special-casing of INTEGER_CST conversion was in the original
3484        code and here to avoid a spurious overflow flag on the resulting
3485        constant which fold_convert produces.  */
3486     (if (TREE_CODE (@1) == INTEGER_CST)
3487      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3488                                 TREE_OVERFLOW (@1)); })
3489      (cmp @00 (convert @1)))
3491     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3492      /* If possible, express the comparison in the shorter mode.  */
3493      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3494            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3495            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3496                && TYPE_UNSIGNED (TREE_TYPE (@00))))
3497           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3498               || ((TYPE_PRECISION (TREE_TYPE (@00))
3499                    >= TYPE_PRECISION (TREE_TYPE (@10)))
3500                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
3501                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
3502               || (TREE_CODE (@10) == INTEGER_CST
3503                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3504                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
3505       (cmp @00 (convert @10))
3506       (if (TREE_CODE (@10) == INTEGER_CST
3507            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3508            && !int_fits_type_p (@10, TREE_TYPE (@00)))
3509        (with
3510         {
3511           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3512           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3513           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3514           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3515         }
3516         (if (above || below)
3517          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3518           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3519           (if (cmp == LT_EXPR || cmp == LE_EXPR)
3520            { constant_boolean_node (above ? true : false, type); }
3521            (if (cmp == GT_EXPR || cmp == GE_EXPR)
3522             { constant_boolean_node (above ? false : true, type); }))))))))))))
3524 (for cmp (eq ne)
3525  /* A local variable can never be pointed to by
3526     the default SSA name of an incoming parameter.
3527     SSA names are canonicalized to 2nd place.  */
3528  (simplify
3529   (cmp addr@0 SSA_NAME@1)
3530   (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3531        && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3532    (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3533     (if (TREE_CODE (base) == VAR_DECL
3534          && auto_var_in_fn_p (base, current_function_decl))
3535      (if (cmp == NE_EXPR)
3536       { constant_boolean_node (true, type); }
3537       { constant_boolean_node (false, type); }))))))
3539 /* Equality compare simplifications from fold_binary  */
3540 (for cmp (eq ne)
3542  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3543     Similarly for NE_EXPR.  */
3544  (simplify
3545   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3546   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3547        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3548    { constant_boolean_node (cmp == NE_EXPR, type); }))
3550  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
3551  (simplify
3552   (cmp (bit_xor @0 @1) integer_zerop)
3553   (cmp @0 @1))
3555  /* (X ^ Y) == Y becomes X == 0.
3556     Likewise (X ^ Y) == X becomes Y == 0.  */
3557  (simplify
3558   (cmp:c (bit_xor:c @0 @1) @0)
3559   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3561  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
3562  (simplify
3563   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3564   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3565    (cmp @0 (bit_xor @1 (convert @2)))))
3567  (simplify
3568   (cmp (convert? addr@0) integer_zerop)
3569   (if (tree_single_nonzero_warnv_p (@0, NULL))
3570    { constant_boolean_node (cmp == NE_EXPR, type); })))
3572 /* If we have (A & C) == C where C is a power of 2, convert this into
3573    (A & C) != 0.  Similarly for NE_EXPR.  */
3574 (for cmp (eq ne)
3575      icmp (ne eq)
3576  (simplify
3577   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3578   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3580 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3581    convert this into a shift followed by ANDing with D.  */
3582 (simplify
3583  (cond
3584   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3585   INTEGER_CST@2 integer_zerop)
3586  (if (integer_pow2p (@2))
3587   (with {
3588      int shift = (wi::exact_log2 (wi::to_wide (@2))
3589                   - wi::exact_log2 (wi::to_wide (@1)));
3590    }
3591    (if (shift > 0)
3592     (bit_and
3593      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3594     (bit_and
3595      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3596      @2)))))
3598 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3599    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
3600 (for cmp (eq ne)
3601      ncmp (ge lt)
3602  (simplify
3603   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3604   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3605        && type_has_mode_precision_p (TREE_TYPE (@0))
3606        && element_precision (@2) >= element_precision (@0)
3607        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3608    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3609     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3611 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3612    this into a right shift or sign extension followed by ANDing with C.  */
3613 (simplify
3614  (cond
3615   (lt @0 integer_zerop)
3616   INTEGER_CST@1 integer_zerop)
3617  (if (integer_pow2p (@1)
3618       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3619   (with {
3620     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3621    }
3622    (if (shift >= 0)
3623     (bit_and
3624      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3625      @1)
3626     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3627        sign extension followed by AND with C will achieve the effect.  */
3628     (bit_and (convert @0) @1)))))
3630 /* When the addresses are not directly of decls compare base and offset.
3631    This implements some remaining parts of fold_comparison address
3632    comparisons but still no complete part of it.  Still it is good
3633    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
3634 (for cmp (simple_comparison)
3635  (simplify
3636   (cmp (convert1?@2 addr@0) (convert2? addr@1))
3637   (with
3638    {
3639      poly_int64 off0, off1;
3640      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3641      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3642      if (base0 && TREE_CODE (base0) == MEM_REF)
3643        {
3644          off0 += mem_ref_offset (base0).force_shwi ();
3645          base0 = TREE_OPERAND (base0, 0);
3646        }
3647      if (base1 && TREE_CODE (base1) == MEM_REF)
3648        {
3649          off1 += mem_ref_offset (base1).force_shwi ();
3650          base1 = TREE_OPERAND (base1, 0);
3651        }
3652    }
3653    (if (base0 && base1)
3654     (with
3655      {
3656        int equal = 2;
3657        /* Punt in GENERIC on variables with value expressions;
3658           the value expressions might point to fields/elements
3659           of other vars etc.  */
3660        if (GENERIC
3661            && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3662                || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3663          ;
3664        else if (decl_in_symtab_p (base0)
3665                 && decl_in_symtab_p (base1))
3666          equal = symtab_node::get_create (base0)
3667                    ->equal_address_to (symtab_node::get_create (base1));
3668        else if ((DECL_P (base0)
3669                  || TREE_CODE (base0) == SSA_NAME
3670                  || TREE_CODE (base0) == STRING_CST)
3671                 && (DECL_P (base1)
3672                     || TREE_CODE (base1) == SSA_NAME
3673                     || TREE_CODE (base1) == STRING_CST))
3674          equal = (base0 == base1);
3675      }
3676      (if (equal == 1
3677           && (cmp == EQ_EXPR || cmp == NE_EXPR
3678               /* If the offsets are equal we can ignore overflow.  */
3679               || known_eq (off0, off1)
3680               || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3681                  /* Or if we compare using pointers to decls or strings.  */
3682               || (POINTER_TYPE_P (TREE_TYPE (@2))
3683                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3684       (switch
3685        (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3686         { constant_boolean_node (known_eq (off0, off1), type); })
3687        (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3688         { constant_boolean_node (known_ne (off0, off1), type); })
3689        (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3690         { constant_boolean_node (known_lt (off0, off1), type); })
3691        (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3692         { constant_boolean_node (known_le (off0, off1), type); })
3693        (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3694         { constant_boolean_node (known_ge (off0, off1), type); })
3695        (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3696         { constant_boolean_node (known_gt (off0, off1), type); }))
3697       (if (equal == 0
3698            && DECL_P (base0) && DECL_P (base1)
3699            /* If we compare this as integers require equal offset.  */
3700            && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3701                || known_eq (off0, off1)))
3702        (switch
3703         (if (cmp == EQ_EXPR)
3704          { constant_boolean_node (false, type); })
3705         (if (cmp == NE_EXPR)
3706          { constant_boolean_node (true, type); })))))))))
3708 /* Simplify pointer equality compares using PTA.  */
3709 (for neeq (ne eq)
3710  (simplify
3711   (neeq @0 @1)
3712   (if (POINTER_TYPE_P (TREE_TYPE (@0))
3713        && ptrs_compare_unequal (@0, @1))
3714    { constant_boolean_node (neeq != EQ_EXPR, type); })))
3716 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3717    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3718    Disable the transform if either operand is pointer to function.
3719    This broke pr22051-2.c for arm where function pointer
3720    canonicalizaion is not wanted.  */
3722 (for cmp (ne eq)
3723  (simplify
3724   (cmp (convert @0) INTEGER_CST@1)
3725   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3726          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3727          && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3728         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3729             && POINTER_TYPE_P (TREE_TYPE (@1))
3730             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3731        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
3732    (cmp @0 (convert @1)))))
3734 /* Non-equality compare simplifications from fold_binary  */
3735 (for cmp (lt gt le ge)
3736  /* Comparisons with the highest or lowest possible integer of
3737     the specified precision will have known values.  */
3738  (simplify
3739   (cmp (convert?@2 @0) INTEGER_CST@1)
3740   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3741        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3742    (with
3743     {
3744       tree arg1_type = TREE_TYPE (@1);
3745       unsigned int prec = TYPE_PRECISION (arg1_type);
3746       wide_int max = wi::max_value (arg1_type);
3747       wide_int signed_max = wi::max_value (prec, SIGNED);
3748       wide_int min = wi::min_value (arg1_type);
3749     }
3750     (switch
3751      (if (wi::to_wide (@1) == max)
3752       (switch
3753        (if (cmp == GT_EXPR)
3754         { constant_boolean_node (false, type); })
3755        (if (cmp == GE_EXPR)
3756         (eq @2 @1))
3757        (if (cmp == LE_EXPR)
3758         { constant_boolean_node (true, type); })
3759        (if (cmp == LT_EXPR)
3760         (ne @2 @1))))
3761      (if (wi::to_wide (@1) == min)
3762       (switch
3763        (if (cmp == LT_EXPR)
3764         { constant_boolean_node (false, type); })
3765        (if (cmp == LE_EXPR)
3766         (eq @2 @1))
3767        (if (cmp == GE_EXPR)
3768         { constant_boolean_node (true, type); })
3769        (if (cmp == GT_EXPR)
3770         (ne @2 @1))))
3771      (if (wi::to_wide (@1) == max - 1)
3772       (switch
3773        (if (cmp == GT_EXPR)
3774         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3775        (if (cmp == LE_EXPR)
3776         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3777      (if (wi::to_wide (@1) == min + 1)
3778       (switch
3779        (if (cmp == GE_EXPR)
3780         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3781        (if (cmp == LT_EXPR)
3782         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3783      (if (wi::to_wide (@1) == signed_max
3784           && TYPE_UNSIGNED (arg1_type)
3785           /* We will flip the signedness of the comparison operator
3786              associated with the mode of @1, so the sign bit is
3787              specified by this mode.  Check that @1 is the signed
3788              max associated with this sign bit.  */
3789           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3790           /* signed_type does not work on pointer types.  */
3791           && INTEGRAL_TYPE_P (arg1_type))
3792       /* The following case also applies to X < signed_max+1
3793          and X >= signed_max+1 because previous transformations.  */
3794       (if (cmp == LE_EXPR || cmp == GT_EXPR)
3795        (with { tree st = signed_type_for (arg1_type); }
3796         (if (cmp == LE_EXPR)
3797          (ge (convert:st @0) { build_zero_cst (st); })
3798          (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3800 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3801  /* If the second operand is NaN, the result is constant.  */
3802  (simplify
3803   (cmp @0 REAL_CST@1)
3804   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3805        && (cmp != LTGT_EXPR || ! flag_trapping_math))
3806    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3807                             ? false : true, type); })))
3809 /* bool_var != 0 becomes bool_var.  */
3810 (simplify
3811  (ne @0 integer_zerop)
3812  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3813       && types_match (type, TREE_TYPE (@0)))
3814   (non_lvalue @0)))
3815 /* bool_var == 1 becomes bool_var.  */
3816 (simplify
3817  (eq @0 integer_onep)
3818  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3819       && types_match (type, TREE_TYPE (@0)))
3820   (non_lvalue @0)))
3821 /* Do not handle
3822    bool_var == 0 becomes !bool_var or
3823    bool_var != 1 becomes !bool_var
3824    here because that only is good in assignment context as long
3825    as we require a tcc_comparison in GIMPLE_CONDs where we'd
3826    replace if (x == 0) with tem = ~x; if (tem != 0) which is
3827    clearly less optimal and which we'll transform again in forwprop.  */
3829 /* When one argument is a constant, overflow detection can be simplified.
3830    Currently restricted to single use so as not to interfere too much with
3831    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3832    A + CST CMP A  ->  A CMP' CST' */
3833 (for cmp (lt le ge gt)
3834      out (gt gt le le)
3835  (simplify
3836   (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3837   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3838        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3839        && wi::to_wide (@1) != 0
3840        && single_use (@2))
3841    (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3842     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3843                                 wi::max_value (prec, UNSIGNED)
3844                                 - wi::to_wide (@1)); })))))
3846 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3847    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3848    expects the long form, so we restrict the transformation for now.  */
3849 (for cmp (gt le)
3850  (simplify
3851   (cmp:c (minus@2 @0 @1) @0)
3852   (if (single_use (@2)
3853        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3854        && TYPE_UNSIGNED (TREE_TYPE (@0))
3855        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3856    (cmp @1 @0))))
3858 /* Testing for overflow is unnecessary if we already know the result.  */
3859 /* A - B > A  */
3860 (for cmp (gt le)
3861      out (ne eq)
3862  (simplify
3863   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3864   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3865        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3866    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3867 /* A + B < A  */
3868 (for cmp (lt ge)
3869      out (ne eq)
3870  (simplify
3871   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3872   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3873        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3874    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3876 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3877    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
3878 (for cmp (lt ge)
3879      out (ne eq)
3880  (simplify
3881   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3882   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3883    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3884     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3886 /* Simplification of math builtins.  These rules must all be optimizations
3887    as well as IL simplifications.  If there is a possibility that the new
3888    form could be a pessimization, the rule should go in the canonicalization
3889    section that follows this one.
3891    Rules can generally go in this section if they satisfy one of
3892    the following:
3894    - the rule describes an identity
3896    - the rule replaces calls with something as simple as addition or
3897      multiplication
3899    - the rule contains unary calls only and simplifies the surrounding
3900      arithmetic.  (The idea here is to exclude non-unary calls in which
3901      one operand is constant and in which the call is known to be cheap
3902      when the operand has that value.)  */
3904 (if (flag_unsafe_math_optimizations)
3905  /* Simplify sqrt(x) * sqrt(x) -> x.  */
3906  (simplify
3907   (mult (SQRT_ALL@1 @0) @1)
3908   (if (!HONOR_SNANS (type))
3909    @0))
3911  (for op (plus minus)
3912   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
3913   (simplify
3914    (op (rdiv @0 @1)
3915        (rdiv @2 @1))
3916    (rdiv (op @0 @2) @1)))
3918  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
3919  (for root (SQRT CBRT)
3920   (simplify
3921    (mult (root:s @0) (root:s @1))
3922     (root (mult @0 @1))))
3924  /* Simplify expN(x) * expN(y) -> expN(x+y). */
3925  (for exps (EXP EXP2 EXP10 POW10)
3926   (simplify
3927    (mult (exps:s @0) (exps:s @1))
3928     (exps (plus @0 @1))))
3930  /* Simplify a/root(b/c) into a*root(c/b).  */
3931  (for root (SQRT CBRT)
3932   (simplify
3933    (rdiv @0 (root:s (rdiv:s @1 @2)))
3934     (mult @0 (root (rdiv @2 @1)))))
3936  /* Simplify x/expN(y) into x*expN(-y).  */
3937  (for exps (EXP EXP2 EXP10 POW10)
3938   (simplify
3939    (rdiv @0 (exps:s @1))
3940     (mult @0 (exps (negate @1)))))
3942  (for logs (LOG LOG2 LOG10 LOG10)
3943       exps (EXP EXP2 EXP10 POW10)
3944   /* logN(expN(x)) -> x.  */
3945   (simplify
3946    (logs (exps @0))
3947    @0)
3948   /* expN(logN(x)) -> x.  */
3949   (simplify
3950    (exps (logs @0))
3951    @0))
3953  /* Optimize logN(func()) for various exponential functions.  We
3954     want to determine the value "x" and the power "exponent" in
3955     order to transform logN(x**exponent) into exponent*logN(x).  */
3956  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
3957       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
3958   (simplify
3959    (logs (exps @0))
3960    (if (SCALAR_FLOAT_TYPE_P (type))
3961     (with {
3962       tree x;
3963       switch (exps)
3964         {
3965         CASE_CFN_EXP:
3966           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
3967           x = build_real_truncate (type, dconst_e ());
3968           break;
3969         CASE_CFN_EXP2:
3970           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
3971           x = build_real (type, dconst2);
3972           break;
3973         CASE_CFN_EXP10:
3974         CASE_CFN_POW10:
3975           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
3976           {
3977             REAL_VALUE_TYPE dconst10;
3978             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3979             x = build_real (type, dconst10);
3980           }
3981           break;
3982         default:
3983           gcc_unreachable ();
3984         }
3985       }
3986      (mult (logs { x; }) @0)))))
3988  (for logs (LOG LOG
3989             LOG2 LOG2
3990             LOG10 LOG10)
3991       exps (SQRT CBRT)
3992   (simplify
3993    (logs (exps @0))
3994    (if (SCALAR_FLOAT_TYPE_P (type))
3995     (with {
3996       tree x;
3997       switch (exps)
3998         {
3999         CASE_CFN_SQRT:
4000           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
4001           x = build_real (type, dconsthalf);
4002           break;
4003         CASE_CFN_CBRT:
4004           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
4005           x = build_real_truncate (type, dconst_third ());
4006           break;
4007         default:
4008           gcc_unreachable ();
4009         }
4010       }
4011      (mult { x; } (logs @0))))))
4013  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
4014  (for logs (LOG LOG2 LOG10)
4015       pows (POW)
4016   (simplify
4017    (logs (pows @0 @1))
4018    (mult @1 (logs @0))))
4020  /* pow(C,x) -> exp(log(C)*x) if C > 0,
4021     or if C is a positive power of 2,
4022     pow(C,x) -> exp2(log2(C)*x).  */
4023 #if GIMPLE
4024  (for pows (POW)
4025       exps (EXP)
4026       logs (LOG)
4027       exp2s (EXP2)
4028       log2s (LOG2)
4029   (simplify
4030    (pows REAL_CST@0 @1)
4031    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4032         && real_isfinite (TREE_REAL_CST_PTR (@0))
4033         /* As libmvec doesn't have a vectorized exp2, defer optimizing
4034            the use_exp2 case until after vectorization.  It seems actually
4035            beneficial for all constants to postpone this until later,
4036            because exp(log(C)*x), while faster, will have worse precision
4037            and if x folds into a constant too, that is unnecessary
4038            pessimization.  */
4039         && canonicalize_math_after_vectorization_p ())
4040     (with {
4041        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4042        bool use_exp2 = false;
4043        if (targetm.libc_has_function (function_c99_misc)
4044            && value->cl == rvc_normal)
4045          {
4046            REAL_VALUE_TYPE frac_rvt = *value;
4047            SET_REAL_EXP (&frac_rvt, 1);
4048            if (real_equal (&frac_rvt, &dconst1))
4049              use_exp2 = true;
4050          }
4051      }
4052      (if (!use_exp2)
4053       (if (optimize_pow_to_exp (@0, @1))
4054        (exps (mult (logs @0) @1)))
4055       (exp2s (mult (log2s @0) @1)))))))
4056 #endif
4058  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
4059  (for pows (POW)
4060       exps (EXP EXP2 EXP10 POW10)
4061       logs (LOG LOG2 LOG10 LOG10)
4062   (simplify
4063    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4064    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4065         && real_isfinite (TREE_REAL_CST_PTR (@0)))
4066     (exps (plus (mult (logs @0) @1) @2)))))
4068  (for sqrts (SQRT)
4069       cbrts (CBRT)
4070       pows (POW)
4071       exps (EXP EXP2 EXP10 POW10)
4072   /* sqrt(expN(x)) -> expN(x*0.5).  */
4073   (simplify
4074    (sqrts (exps @0))
4075    (exps (mult @0 { build_real (type, dconsthalf); })))
4076   /* cbrt(expN(x)) -> expN(x/3).  */
4077   (simplify
4078    (cbrts (exps @0))
4079    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4080   /* pow(expN(x), y) -> expN(x*y).  */
4081   (simplify
4082    (pows (exps @0) @1)
4083    (exps (mult @0 @1))))
4085  /* tan(atan(x)) -> x.  */
4086  (for tans (TAN)
4087       atans (ATAN)
4088   (simplify
4089    (tans (atans @0))
4090    @0)))
4092 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
4093 (simplify
4094  (CABS (complex:C @0 real_zerop@1))
4095  (abs @0))
4097 /* trunc(trunc(x)) -> trunc(x), etc.  */
4098 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4099  (simplify
4100   (fns (fns @0))
4101   (fns @0)))
4102 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
4103 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4104  (simplify
4105   (fns integer_valued_real_p@0)
4106   @0))
4108 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
4109 (simplify
4110  (HYPOT:c @0 real_zerop@1)
4111  (abs @0))
4113 /* pow(1,x) -> 1.  */
4114 (simplify
4115  (POW real_onep@0 @1)
4116  @0)
4118 (simplify
4119  /* copysign(x,x) -> x.  */
4120  (COPYSIGN_ALL @0 @0)
4121  @0)
4123 (simplify
4124  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
4125  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4126  (abs @0))
4128 (for scale (LDEXP SCALBN SCALBLN)
4129  /* ldexp(0, x) -> 0.  */
4130  (simplify
4131   (scale real_zerop@0 @1)
4132   @0)
4133  /* ldexp(x, 0) -> x.  */
4134  (simplify
4135   (scale @0 integer_zerop@1)
4136   @0)
4137  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
4138  (simplify
4139   (scale REAL_CST@0 @1)
4140   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4141    @0)))
4143 /* Canonicalization of sequences of math builtins.  These rules represent
4144    IL simplifications but are not necessarily optimizations.
4146    The sincos pass is responsible for picking "optimal" implementations
4147    of math builtins, which may be more complicated and can sometimes go
4148    the other way, e.g. converting pow into a sequence of sqrts.
4149    We only want to do these canonicalizations before the pass has run.  */
4151 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4152  /* Simplify tan(x) * cos(x) -> sin(x). */
4153  (simplify
4154   (mult:c (TAN:s @0) (COS:s @0))
4155    (SIN @0))
4157  /* Simplify x * pow(x,c) -> pow(x,c+1). */
4158  (simplify
4159   (mult:c @0 (POW:s @0 REAL_CST@1))
4160   (if (!TREE_OVERFLOW (@1))
4161    (POW @0 (plus @1 { build_one_cst (type); }))))
4163  /* Simplify sin(x) / cos(x) -> tan(x). */
4164  (simplify
4165   (rdiv (SIN:s @0) (COS:s @0))
4166    (TAN @0))
4168  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4169  (simplify
4170   (rdiv (COS:s @0) (SIN:s @0))
4171    (rdiv { build_one_cst (type); } (TAN @0)))
4173  /* Simplify sin(x) / tan(x) -> cos(x). */
4174  (simplify
4175   (rdiv (SIN:s @0) (TAN:s @0))
4176   (if (! HONOR_NANS (@0)
4177        && ! HONOR_INFINITIES (@0))
4178    (COS @0)))
4180  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4181  (simplify
4182   (rdiv (TAN:s @0) (SIN:s @0))
4183   (if (! HONOR_NANS (@0)
4184        && ! HONOR_INFINITIES (@0))
4185    (rdiv { build_one_cst (type); } (COS @0))))
4187  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4188  (simplify
4189   (mult (POW:s @0 @1) (POW:s @0 @2))
4190    (POW @0 (plus @1 @2)))
4192  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4193  (simplify
4194   (mult (POW:s @0 @1) (POW:s @2 @1))
4195    (POW (mult @0 @2) @1))
4197  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4198  (simplify
4199   (mult (POWI:s @0 @1) (POWI:s @2 @1))
4200    (POWI (mult @0 @2) @1))
4202  /* Simplify pow(x,c) / x -> pow(x,c-1). */
4203  (simplify
4204   (rdiv (POW:s @0 REAL_CST@1) @0)
4205   (if (!TREE_OVERFLOW (@1))
4206    (POW @0 (minus @1 { build_one_cst (type); }))))
4208  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4209  (simplify
4210   (rdiv @0 (POW:s @1 @2))
4211    (mult @0 (POW @1 (negate @2))))
4213  (for sqrts (SQRT)
4214       cbrts (CBRT)
4215       pows (POW)
4216   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
4217   (simplify
4218    (sqrts (sqrts @0))
4219    (pows @0 { build_real (type, dconst_quarter ()); }))
4220   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
4221   (simplify
4222    (sqrts (cbrts @0))
4223    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4224   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
4225   (simplify
4226    (cbrts (sqrts @0))
4227    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4228   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
4229   (simplify
4230    (cbrts (cbrts tree_expr_nonnegative_p@0))
4231    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4232   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
4233   (simplify
4234    (sqrts (pows @0 @1))
4235    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4236   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
4237   (simplify
4238    (cbrts (pows tree_expr_nonnegative_p@0 @1))
4239    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4240   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
4241   (simplify
4242    (pows (sqrts @0) @1)
4243    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4244   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
4245   (simplify
4246    (pows (cbrts tree_expr_nonnegative_p@0) @1)
4247    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4248   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
4249   (simplify
4250    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4251    (pows @0 (mult @1 @2))))
4253  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
4254  (simplify
4255   (CABS (complex @0 @0))
4256   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4258  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
4259  (simplify
4260   (HYPOT @0 @0)
4261   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4263  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
4264  (for cexps (CEXP)
4265       exps (EXP)
4266       cexpis (CEXPI)
4267   (simplify
4268    (cexps compositional_complex@0)
4269    (if (targetm.libc_has_function (function_c99_math_complex))
4270     (complex
4271      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4272      (mult @1 (imagpart @2)))))))
4274 (if (canonicalize_math_p ())
4275  /* floor(x) -> trunc(x) if x is nonnegative.  */
4276  (for floors (FLOOR_ALL)
4277       truncs (TRUNC_ALL)
4278   (simplify
4279    (floors tree_expr_nonnegative_p@0)
4280    (truncs @0))))
4282 (match double_value_p
4283  @0
4284  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4285 (for froms (BUILT_IN_TRUNCL
4286             BUILT_IN_FLOORL
4287             BUILT_IN_CEILL
4288             BUILT_IN_ROUNDL
4289             BUILT_IN_NEARBYINTL
4290             BUILT_IN_RINTL)
4291      tos (BUILT_IN_TRUNC
4292           BUILT_IN_FLOOR
4293           BUILT_IN_CEIL
4294           BUILT_IN_ROUND
4295           BUILT_IN_NEARBYINT
4296           BUILT_IN_RINT)
4297  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
4298  (if (optimize && canonicalize_math_p ())
4299   (simplify
4300    (froms (convert double_value_p@0))
4301    (convert (tos @0)))))
4303 (match float_value_p
4304  @0
4305  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4306 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4307             BUILT_IN_FLOORL BUILT_IN_FLOOR
4308             BUILT_IN_CEILL BUILT_IN_CEIL
4309             BUILT_IN_ROUNDL BUILT_IN_ROUND
4310             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4311             BUILT_IN_RINTL BUILT_IN_RINT)
4312      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4313           BUILT_IN_FLOORF BUILT_IN_FLOORF
4314           BUILT_IN_CEILF BUILT_IN_CEILF
4315           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4316           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4317           BUILT_IN_RINTF BUILT_IN_RINTF)
4318  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4319     if x is a float.  */
4320  (if (optimize && canonicalize_math_p ()
4321       && targetm.libc_has_function (function_c99_misc))
4322   (simplify
4323    (froms (convert float_value_p@0))
4324    (convert (tos @0)))))
4326 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4327      tos (XFLOOR XCEIL XROUND XRINT)
4328  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
4329  (if (optimize && canonicalize_math_p ())
4330   (simplify
4331    (froms (convert double_value_p@0))
4332    (tos @0))))
4334 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4335             XFLOOR XCEIL XROUND XRINT)
4336      tos (XFLOORF XCEILF XROUNDF XRINTF)
4337  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4338     if x is a float.  */
4339  (if (optimize && canonicalize_math_p ())
4340   (simplify
4341    (froms (convert float_value_p@0))
4342    (tos @0))))
4344 (if (canonicalize_math_p ())
4345  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
4346  (for floors (IFLOOR LFLOOR LLFLOOR)
4347   (simplify
4348    (floors tree_expr_nonnegative_p@0)
4349    (fix_trunc @0))))
4351 (if (canonicalize_math_p ())
4352  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
4353  (for fns (IFLOOR LFLOOR LLFLOOR
4354            ICEIL LCEIL LLCEIL
4355            IROUND LROUND LLROUND)
4356   (simplify
4357    (fns integer_valued_real_p@0)
4358    (fix_trunc @0)))
4359  (if (!flag_errno_math)
4360   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
4361   (for rints (IRINT LRINT LLRINT)
4362    (simplify
4363     (rints integer_valued_real_p@0)
4364     (fix_trunc @0)))))
4366 (if (canonicalize_math_p ())
4367  (for ifn (IFLOOR ICEIL IROUND IRINT)
4368       lfn (LFLOOR LCEIL LROUND LRINT)
4369       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4370   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4371      sizeof (int) == sizeof (long).  */
4372   (if (TYPE_PRECISION (integer_type_node)
4373        == TYPE_PRECISION (long_integer_type_node))
4374    (simplify
4375     (ifn @0)
4376     (lfn:long_integer_type_node @0)))
4377   /* Canonicalize llround (x) to lround (x) on LP64 targets where
4378      sizeof (long long) == sizeof (long).  */
4379   (if (TYPE_PRECISION (long_long_integer_type_node)
4380        == TYPE_PRECISION (long_integer_type_node))
4381    (simplify
4382     (llfn @0)
4383     (lfn:long_integer_type_node @0)))))
4385 /* cproj(x) -> x if we're ignoring infinities.  */
4386 (simplify
4387  (CPROJ @0)
4388  (if (!HONOR_INFINITIES (type))
4389    @0))
4391 /* If the real part is inf and the imag part is known to be
4392    nonnegative, return (inf + 0i).  */
4393 (simplify
4394  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4395  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4396   { build_complex_inf (type, false); }))
4398 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
4399 (simplify
4400  (CPROJ (complex @0 REAL_CST@1))
4401  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4402   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4404 (for pows (POW)
4405      sqrts (SQRT)
4406      cbrts (CBRT)
4407  (simplify
4408   (pows @0 REAL_CST@1)
4409   (with {
4410     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4411     REAL_VALUE_TYPE tmp;
4412    }
4413    (switch
4414     /* pow(x,0) -> 1.  */
4415     (if (real_equal (value, &dconst0))
4416      { build_real (type, dconst1); })
4417     /* pow(x,1) -> x.  */
4418     (if (real_equal (value, &dconst1))
4419      @0)
4420     /* pow(x,-1) -> 1/x.  */
4421     (if (real_equal (value, &dconstm1))
4422      (rdiv { build_real (type, dconst1); } @0))
4423     /* pow(x,0.5) -> sqrt(x).  */
4424     (if (flag_unsafe_math_optimizations
4425          && canonicalize_math_p ()
4426          && real_equal (value, &dconsthalf))
4427      (sqrts @0))
4428     /* pow(x,1/3) -> cbrt(x).  */
4429     (if (flag_unsafe_math_optimizations
4430          && canonicalize_math_p ()
4431          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4432              real_equal (value, &tmp)))
4433      (cbrts @0))))))
4435 /* powi(1,x) -> 1.  */
4436 (simplify
4437  (POWI real_onep@0 @1)
4438  @0)
4440 (simplify
4441  (POWI @0 INTEGER_CST@1)
4442  (switch
4443   /* powi(x,0) -> 1.  */
4444   (if (wi::to_wide (@1) == 0)
4445    { build_real (type, dconst1); })
4446   /* powi(x,1) -> x.  */
4447   (if (wi::to_wide (@1) == 1)
4448    @0)
4449   /* powi(x,-1) -> 1/x.  */
4450   (if (wi::to_wide (@1) == -1)
4451    (rdiv { build_real (type, dconst1); } @0))))
4453 /* Narrowing of arithmetic and logical operations. 
4455    These are conceptually similar to the transformations performed for
4456    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
4457    term we want to move all that code out of the front-ends into here.  */
4459 /* If we have a narrowing conversion of an arithmetic operation where
4460    both operands are widening conversions from the same type as the outer
4461    narrowing conversion.  Then convert the innermost operands to a suitable
4462    unsigned type (to avoid introducing undefined behavior), perform the
4463    operation and convert the result to the desired type.  */
4464 (for op (plus minus)
4465   (simplify
4466     (convert (op:s (convert@2 @0) (convert?@3 @1)))
4467     (if (INTEGRAL_TYPE_P (type)
4468          /* We check for type compatibility between @0 and @1 below,
4469             so there's no need to check that @1/@3 are integral types.  */
4470          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4471          && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4472          /* The precision of the type of each operand must match the
4473             precision of the mode of each operand, similarly for the
4474             result.  */
4475          && type_has_mode_precision_p (TREE_TYPE (@0))
4476          && type_has_mode_precision_p (TREE_TYPE (@1))
4477          && type_has_mode_precision_p (type)
4478          /* The inner conversion must be a widening conversion.  */
4479          && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4480          && types_match (@0, type)
4481          && (types_match (@0, @1)
4482              /* Or the second operand is const integer or converted const
4483                 integer from valueize.  */
4484              || TREE_CODE (@1) == INTEGER_CST))
4485       (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4486         (op @0 (convert @1))
4487         (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4488          (convert (op (convert:utype @0)
4489                       (convert:utype @1))))))))
4491 /* This is another case of narrowing, specifically when there's an outer
4492    BIT_AND_EXPR which masks off bits outside the type of the innermost
4493    operands.   Like the previous case we have to convert the operands
4494    to unsigned types to avoid introducing undefined behavior for the
4495    arithmetic operation.  */
4496 (for op (minus plus)
4497  (simplify
4498   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4499   (if (INTEGRAL_TYPE_P (type)
4500        /* We check for type compatibility between @0 and @1 below,
4501           so there's no need to check that @1/@3 are integral types.  */
4502        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4503        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4504        /* The precision of the type of each operand must match the
4505           precision of the mode of each operand, similarly for the
4506           result.  */
4507        && type_has_mode_precision_p (TREE_TYPE (@0))
4508        && type_has_mode_precision_p (TREE_TYPE (@1))
4509        && type_has_mode_precision_p (type)
4510        /* The inner conversion must be a widening conversion.  */
4511        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4512        && types_match (@0, @1)
4513        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4514            <= TYPE_PRECISION (TREE_TYPE (@0)))
4515        && (wi::to_wide (@4)
4516            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4517                        true, TYPE_PRECISION (type))) == 0)
4518    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4519     (with { tree ntype = TREE_TYPE (@0); }
4520      (convert (bit_and (op @0 @1) (convert:ntype @4))))
4521     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4522      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4523                (convert:utype @4))))))))
4525 /* Transform (@0 < @1 and @0 < @2) to use min, 
4526    (@0 > @1 and @0 > @2) to use max */
4527 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4528      op    (lt      le      gt      ge      lt      le      gt      ge     )
4529      ext   (min     min     max     max     max     max     min     min    )
4530  (simplify
4531   (logic (op:cs @0 @1) (op:cs @0 @2))
4532   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4533        && TREE_CODE (@0) != INTEGER_CST)
4534    (op @0 (ext @1 @2)))))
4536 (simplify
4537  /* signbit(x) -> 0 if x is nonnegative.  */
4538  (SIGNBIT tree_expr_nonnegative_p@0)
4539  { integer_zero_node; })
4541 (simplify
4542  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
4543  (SIGNBIT @0)
4544  (if (!HONOR_SIGNED_ZEROS (@0))
4545   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4547 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
4548 (for cmp (eq ne)
4549  (for op (plus minus)
4550       rop (minus plus)
4551   (simplify
4552    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4553    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4554         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4555         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4556         && !TYPE_SATURATING (TREE_TYPE (@0)))
4557     (with { tree res = int_const_binop (rop, @2, @1); }
4558      (if (TREE_OVERFLOW (res)
4559           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4560       { constant_boolean_node (cmp == NE_EXPR, type); }
4561       (if (single_use (@3))
4562        (cmp @0 { TREE_OVERFLOW (res)
4563                  ? drop_tree_overflow (res) : res; }))))))))
4564 (for cmp (lt le gt ge)
4565  (for op (plus minus)
4566       rop (minus plus)
4567   (simplify
4568    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4569    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4570         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4571     (with { tree res = int_const_binop (rop, @2, @1); }
4572      (if (TREE_OVERFLOW (res))
4573       {
4574         fold_overflow_warning (("assuming signed overflow does not occur "
4575                                 "when simplifying conditional to constant"),
4576                                WARN_STRICT_OVERFLOW_CONDITIONAL);
4577         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4578         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
4579         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4580                                   TYPE_SIGN (TREE_TYPE (@1)))
4581                         != (op == MINUS_EXPR);
4582         constant_boolean_node (less == ovf_high, type);
4583       }
4584       (if (single_use (@3))
4585        (with
4586         {
4587           fold_overflow_warning (("assuming signed overflow does not occur "
4588                                   "when changing X +- C1 cmp C2 to "
4589                                   "X cmp C2 -+ C1"),
4590                                  WARN_STRICT_OVERFLOW_COMPARISON);
4591         }
4592         (cmp @0 { res; })))))))))
4594 /* Canonicalizations of BIT_FIELD_REFs.  */
4596 (simplify
4597  (BIT_FIELD_REF @0 @1 @2)
4598  (switch
4599   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4600        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4601    (switch
4602     (if (integer_zerop (@2))
4603      (view_convert (realpart @0)))
4604     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4605      (view_convert (imagpart @0)))))
4606   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4607        && INTEGRAL_TYPE_P (type)
4608        /* On GIMPLE this should only apply to register arguments.  */
4609        && (! GIMPLE || is_gimple_reg (@0))
4610        /* A bit-field-ref that referenced the full argument can be stripped.  */
4611        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4612             && integer_zerop (@2))
4613            /* Low-parts can be reduced to integral conversions.
4614               ???  The following doesn't work for PDP endian.  */
4615            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4616                /* Don't even think about BITS_BIG_ENDIAN.  */
4617                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4618                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4619                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4620                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
4621                                             - TYPE_PRECISION (type))
4622                                          : 0)) == 0)))
4623    (convert @0))))
4625 /* Simplify vector extracts.  */
4627 (simplify
4628  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4629  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4630       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4631           || (VECTOR_TYPE_P (type)
4632               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4633   (with
4634    {
4635      tree ctor = (TREE_CODE (@0) == SSA_NAME
4636                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4637      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4638      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4639      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4640      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4641    }
4642    (if (n != 0
4643         && (idx % width) == 0
4644         && (n % width) == 0
4645         && known_le ((idx + n) / width,
4646                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
4647     (with
4648      {
4649        idx = idx / width;
4650        n = n / width;
4651        /* Constructor elements can be subvectors.  */
4652        poly_uint64 k = 1;
4653        if (CONSTRUCTOR_NELTS (ctor) != 0)
4654          {
4655            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4656            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4657              k = TYPE_VECTOR_SUBPARTS (cons_elem);
4658          }
4659        unsigned HOST_WIDE_INT elt, count, const_k;
4660      }
4661      (switch
4662       /* We keep an exact subset of the constructor elements.  */
4663       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
4664        (if (CONSTRUCTOR_NELTS (ctor) == 0)
4665         { build_constructor (type, NULL); }
4666         (if (count == 1)
4667          (if (elt < CONSTRUCTOR_NELTS (ctor))
4668           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
4669           { build_zero_cst (type); })
4670          {
4671            vec<constructor_elt, va_gc> *vals;
4672            vec_alloc (vals, count);
4673            for (unsigned i = 0;
4674                 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4675              CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4676                                      CONSTRUCTOR_ELT (ctor, elt + i)->value);
4677            build_constructor (type, vals);
4678          })))
4679       /* The bitfield references a single constructor element.  */
4680       (if (k.is_constant (&const_k)
4681            && idx + n <= (idx / const_k + 1) * const_k)
4682        (switch
4683         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4684          { build_zero_cst (type); })
4685         (if (n == const_k)
4686          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
4687         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4688                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
4690 /* Simplify a bit extraction from a bit insertion for the cases with
4691    the inserted element fully covering the extraction or the insertion
4692    not touching the extraction.  */
4693 (simplify
4694  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4695  (with
4696   {
4697     unsigned HOST_WIDE_INT isize;
4698     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4699       isize = TYPE_PRECISION (TREE_TYPE (@1));
4700     else
4701       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4702   }
4703   (switch
4704    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4705         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4706                       wi::to_wide (@ipos) + isize))
4707     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4708                                                  wi::to_wide (@rpos)
4709                                                  - wi::to_wide (@ipos)); }))
4710    (if (wi::geu_p (wi::to_wide (@ipos),
4711                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
4712         || wi::geu_p (wi::to_wide (@rpos),
4713                       wi::to_wide (@ipos) + isize))
4714     (BIT_FIELD_REF @0 @rsize @rpos)))))
4716 (if (canonicalize_math_after_vectorization_p ())
4717  (for fmas (FMA)
4718   (simplify
4719    (fmas:c (negate @0) @1 @2)
4720    (IFN_FNMA @0 @1 @2))
4721   (simplify
4722    (fmas @0 @1 (negate @2))
4723    (IFN_FMS @0 @1 @2))
4724   (simplify
4725    (fmas:c (negate @0) @1 (negate @2))
4726    (IFN_FNMS @0 @1 @2))
4727   (simplify
4728    (negate (fmas@3 @0 @1 @2))
4729    (if (single_use (@3))
4730     (IFN_FNMS @0 @1 @2))))
4732  (simplify
4733   (IFN_FMS:c (negate @0) @1 @2)
4734   (IFN_FNMS @0 @1 @2))
4735  (simplify
4736   (IFN_FMS @0 @1 (negate @2))
4737   (IFN_FMA @0 @1 @2))
4738  (simplify
4739   (IFN_FMS:c (negate @0) @1 (negate @2))
4740   (IFN_FNMA @0 @1 @2))
4741  (simplify
4742   (negate (IFN_FMS@3 @0 @1 @2))
4743    (if (single_use (@3))
4744     (IFN_FNMA @0 @1 @2)))
4746  (simplify
4747   (IFN_FNMA:c (negate @0) @1 @2)
4748   (IFN_FMA @0 @1 @2))
4749  (simplify
4750   (IFN_FNMA @0 @1 (negate @2))
4751   (IFN_FNMS @0 @1 @2))
4752  (simplify
4753   (IFN_FNMA:c (negate @0) @1 (negate @2))
4754   (IFN_FMS @0 @1 @2))
4755  (simplify
4756   (negate (IFN_FNMA@3 @0 @1 @2))
4757   (if (single_use (@3))
4758    (IFN_FMS @0 @1 @2)))
4760  (simplify
4761   (IFN_FNMS:c (negate @0) @1 @2)
4762   (IFN_FMS @0 @1 @2))
4763  (simplify
4764   (IFN_FNMS @0 @1 (negate @2))
4765   (IFN_FNMA @0 @1 @2))
4766  (simplify
4767   (IFN_FNMS:c (negate @0) @1 (negate @2))
4768   (IFN_FMA @0 @1 @2))
4769  (simplify
4770   (negate (IFN_FNMS@3 @0 @1 @2))
4771   (if (single_use (@3))
4772    (IFN_FMA @0 @1 @2))))
4774 /* POPCOUNT simplifications.  */
4775 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
4776                BUILT_IN_POPCOUNTIMAX)
4777   /* popcount(X&1) is nop_expr(X&1).  */
4778   (simplify
4779     (popcount @0)
4780     (if (tree_nonzero_bits (@0) == 1)
4781       (convert @0)))
4782   /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
4783   (simplify
4784     (plus (popcount:s @0) (popcount:s @1))
4785     (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
4786       (popcount (bit_ior @0 @1))))
4787   /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
4788   (for cmp (le eq ne gt)
4789        rep (eq eq ne ne)
4790     (simplify
4791       (cmp (popcount @0) integer_zerop)
4792       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4794 /* Simplify:
4796      a = a1 op a2
4797      r = c ? a : b;
4799    to:
4801      r = c ? a1 op a2 : b;
4803    if the target can do it in one go.  This makes the operation conditional
4804    on c, so could drop potentially-trapping arithmetic, but that's a valid
4805    simplification if the result of the operation isn't needed.  */
4806 (for uncond_op (UNCOND_BINARY)
4807      cond_op (COND_BINARY)
4808  (simplify
4809   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
4810   (with { tree op_type = TREE_TYPE (@4); }
4811    (if (element_precision (type) == element_precision (op_type))
4812     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
4813  (simplify
4814   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
4815   (with { tree op_type = TREE_TYPE (@4); }
4816    (if (element_precision (type) == element_precision (op_type))
4817     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))