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