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