PR rtl-optimization/82913
[official-gcc.git] / gcc / match.pd
blob4d56847d6889923938625beb579b7bbb0cbbad91
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 FMAX)
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 FMAX)
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   (FMAX @0 @1)
1903   (max @0 @1)))
1904 /* min (-A, -B) -> -max (A, B)  */
1905 (for minmax (min max FMIN FMAX)
1906      maxmin (max min FMAX FMIN)
1907  (simplify
1908   (minmax (negate:s@2 @0) (negate:s@3 @1))
1909   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1910        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1911            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1912    (negate (maxmin @0 @1)))))
1913 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1914    MAX (~X, ~Y) -> ~MIN (X, Y)  */
1915 (for minmax (min max)
1916  maxmin (max min)
1917  (simplify
1918   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1919   (bit_not (maxmin @0 @1))))
1921 /* MIN (X, Y) == X -> X <= Y  */
1922 (for minmax (min min max max)
1923      cmp    (eq  ne  eq  ne )
1924      out    (le  gt  ge  lt )
1925  (simplify
1926   (cmp:c (minmax:c @0 @1) @0)
1927   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1928    (out @0 @1))))
1929 /* MIN (X, 5) == 0 -> X == 0
1930    MIN (X, 5) == 7 -> false  */
1931 (for cmp (eq ne)
1932  (simplify
1933   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1934   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1935                  TYPE_SIGN (TREE_TYPE (@0))))
1936    { constant_boolean_node (cmp == NE_EXPR, type); }
1937    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1938                   TYPE_SIGN (TREE_TYPE (@0))))
1939     (cmp @0 @2)))))
1940 (for cmp (eq ne)
1941  (simplify
1942   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1943   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1944                  TYPE_SIGN (TREE_TYPE (@0))))
1945    { constant_boolean_node (cmp == NE_EXPR, type); }
1946    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1947                   TYPE_SIGN (TREE_TYPE (@0))))
1948     (cmp @0 @2)))))
1949 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
1950 (for minmax (min     min     max     max     min     min     max     max    )
1951      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
1952      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1953  (simplify
1954   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1955   (comb (cmp @0 @2) (cmp @1 @2))))
1957 /* Simplifications of shift and rotates.  */
1959 (for rotate (lrotate rrotate)
1960  (simplify
1961   (rotate integer_all_onesp@0 @1)
1962   @0))
1964 /* Optimize -1 >> x for arithmetic right shifts.  */
1965 (simplify
1966  (rshift integer_all_onesp@0 @1)
1967  (if (!TYPE_UNSIGNED (type)
1968       && tree_expr_nonnegative_p (@1))
1969   @0))
1971 /* Optimize (x >> c) << c into x & (-1<<c).  */
1972 (simplify
1973  (lshift (rshift @0 INTEGER_CST@1) @1)
1974  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
1975   (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1977 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1978    types.  */
1979 (simplify
1980  (rshift (lshift @0 INTEGER_CST@1) @1)
1981  (if (TYPE_UNSIGNED (type)
1982       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
1983   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1985 (for shiftrotate (lrotate rrotate lshift rshift)
1986  (simplify
1987   (shiftrotate @0 integer_zerop)
1988   (non_lvalue @0))
1989  (simplify
1990   (shiftrotate integer_zerop@0 @1)
1991   @0)
1992  /* Prefer vector1 << scalar to vector1 << vector2
1993     if vector2 is uniform.  */
1994  (for vec (VECTOR_CST CONSTRUCTOR)
1995   (simplify
1996    (shiftrotate @0 vec@1)
1997    (with { tree tem = uniform_vector_p (@1); }
1998     (if (tem)
1999      (shiftrotate @0 { tem; }))))))
2001 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2002    Y is 0.  Similarly for X >> Y.  */
2003 #if GIMPLE
2004 (for shift (lshift rshift)
2005  (simplify
2006   (shift @0 SSA_NAME@1)
2007    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2008     (with {
2009       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2010       int prec = TYPE_PRECISION (TREE_TYPE (@1));
2011      }
2012      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2013       @0)))))
2014 #endif
2016 /* Rewrite an LROTATE_EXPR by a constant into an
2017    RROTATE_EXPR by a new constant.  */
2018 (simplify
2019  (lrotate @0 INTEGER_CST@1)
2020  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2021                             build_int_cst (TREE_TYPE (@1),
2022                                            element_precision (type)), @1); }))
2024 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
2025 (for op (lrotate rrotate rshift lshift)
2026  (simplify
2027   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2028   (with { unsigned int prec = element_precision (type); }
2029    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2030         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2031         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2032         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2033     (with { unsigned int low = (tree_to_uhwi (@1)
2034                                 + tree_to_uhwi (@2)); }
2035      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2036         being well defined.  */
2037      (if (low >= prec)
2038       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2039        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2040        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2041         { build_zero_cst (type); }
2042         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2043       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2046 /* ((1 << A) & 1) != 0 -> A == 0
2047    ((1 << A) & 1) == 0 -> A != 0 */
2048 (for cmp (ne eq)
2049      icmp (eq ne)
2050  (simplify
2051   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2052   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2054 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2055    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2056    if CST2 != 0.  */
2057 (for cmp (ne eq)
2058  (simplify
2059   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2060   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2061    (if (cand < 0
2062         || (!integer_zerop (@2)
2063             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2064     { constant_boolean_node (cmp == NE_EXPR, type); }
2065     (if (!integer_zerop (@2)
2066          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2067      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2069 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2070         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2071    if the new mask might be further optimized.  */
2072 (for shift (lshift rshift)
2073  (simplify
2074   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2075            INTEGER_CST@2)
2076    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2077         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2078         && tree_fits_uhwi_p (@1)
2079         && tree_to_uhwi (@1) > 0
2080         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2081     (with
2082      {
2083        unsigned int shiftc = tree_to_uhwi (@1);
2084        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2085        unsigned HOST_WIDE_INT newmask, zerobits = 0;
2086        tree shift_type = TREE_TYPE (@3);
2087        unsigned int prec;
2089        if (shift == LSHIFT_EXPR)
2090          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2091        else if (shift == RSHIFT_EXPR
2092                 && type_has_mode_precision_p (shift_type))
2093          {
2094            prec = TYPE_PRECISION (TREE_TYPE (@3));
2095            tree arg00 = @0;
2096            /* See if more bits can be proven as zero because of
2097               zero extension.  */
2098            if (@3 != @0
2099                && TYPE_UNSIGNED (TREE_TYPE (@0)))
2100              {
2101                tree inner_type = TREE_TYPE (@0);
2102                if (type_has_mode_precision_p (inner_type)
2103                    && TYPE_PRECISION (inner_type) < prec)
2104                  {
2105                    prec = TYPE_PRECISION (inner_type);
2106                    /* See if we can shorten the right shift.  */
2107                    if (shiftc < prec)
2108                      shift_type = inner_type;
2109                    /* Otherwise X >> C1 is all zeros, so we'll optimize
2110                       it into (X, 0) later on by making sure zerobits
2111                       is all ones.  */
2112                  }
2113              }
2114            zerobits = HOST_WIDE_INT_M1U;
2115            if (shiftc < prec)
2116              {
2117                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2118                zerobits <<= prec - shiftc;
2119              }
2120            /* For arithmetic shift if sign bit could be set, zerobits
2121               can contain actually sign bits, so no transformation is
2122               possible, unless MASK masks them all away.  In that
2123               case the shift needs to be converted into logical shift.  */
2124            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2125                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2126              {
2127                if ((mask & zerobits) == 0)
2128                  shift_type = unsigned_type_for (TREE_TYPE (@3));
2129                else
2130                  zerobits = 0;
2131              }
2132          }
2133      }
2134      /* ((X << 16) & 0xff00) is (X, 0).  */
2135      (if ((mask & zerobits) == mask)
2136       { build_int_cst (type, 0); }
2137       (with { newmask = mask | zerobits; }
2138        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2139         (with
2140          {
2141            /* Only do the transformation if NEWMASK is some integer
2142               mode's mask.  */
2143            for (prec = BITS_PER_UNIT;
2144                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2145              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2146                break;
2147          }
2148          (if (prec < HOST_BITS_PER_WIDE_INT
2149               || newmask == HOST_WIDE_INT_M1U)
2150           (with
2151            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2152            (if (!tree_int_cst_equal (newmaskt, @2))
2153             (if (shift_type != TREE_TYPE (@3))
2154              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2155              (bit_and @4 { newmaskt; })))))))))))))
2157 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2158    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
2159 (for shift (lshift rshift)
2160  (for bit_op (bit_and bit_xor bit_ior)
2161   (simplify
2162    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2163    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2164     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2165      (bit_op (shift (convert @0) @1) { mask; }))))))
2167 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
2168 (simplify
2169  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2170   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2171        && (element_precision (TREE_TYPE (@0))
2172            <= element_precision (TREE_TYPE (@1))
2173            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2174    (with
2175     { tree shift_type = TREE_TYPE (@0); }
2176      (convert (rshift (convert:shift_type @1) @2)))))
2178 /* ~(~X >>r Y) -> X >>r Y
2179    ~(~X <<r Y) -> X <<r Y */
2180 (for rotate (lrotate rrotate)
2181  (simplify
2182   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2183    (if ((element_precision (TREE_TYPE (@0))
2184          <= element_precision (TREE_TYPE (@1))
2185          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2186         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2187             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2188     (with
2189      { tree rotate_type = TREE_TYPE (@0); }
2190       (convert (rotate (convert:rotate_type @1) @2))))))
2192 /* Simplifications of conversions.  */
2194 /* Basic strip-useless-type-conversions / strip_nops.  */
2195 (for cvt (convert view_convert float fix_trunc)
2196  (simplify
2197   (cvt @0)
2198   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2199        || (GENERIC && type == TREE_TYPE (@0)))
2200    @0)))
2202 /* Contract view-conversions.  */
2203 (simplify
2204   (view_convert (view_convert @0))
2205   (view_convert @0))
2207 /* For integral conversions with the same precision or pointer
2208    conversions use a NOP_EXPR instead.  */
2209 (simplify
2210   (view_convert @0)
2211   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2212        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2213        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2214    (convert @0)))
2216 /* Strip inner integral conversions that do not change precision or size, or
2217    zero-extend while keeping the same size (for bool-to-char).  */
2218 (simplify
2219   (view_convert (convert@0 @1))
2220   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2221        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2222        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2223        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2224            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2225                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2226    (view_convert @1)))
2228 /* Re-association barriers around constants and other re-association
2229    barriers can be removed.  */
2230 (simplify
2231  (paren CONSTANT_CLASS_P@0)
2232  @0)
2233 (simplify
2234  (paren (paren@1 @0))
2235  @1)
2237 /* Handle cases of two conversions in a row.  */
2238 (for ocvt (convert float fix_trunc)
2239  (for icvt (convert float)
2240   (simplify
2241    (ocvt (icvt@1 @0))
2242    (with
2243     {
2244       tree inside_type = TREE_TYPE (@0);
2245       tree inter_type = TREE_TYPE (@1);
2246       int inside_int = INTEGRAL_TYPE_P (inside_type);
2247       int inside_ptr = POINTER_TYPE_P (inside_type);
2248       int inside_float = FLOAT_TYPE_P (inside_type);
2249       int inside_vec = VECTOR_TYPE_P (inside_type);
2250       unsigned int inside_prec = TYPE_PRECISION (inside_type);
2251       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2252       int inter_int = INTEGRAL_TYPE_P (inter_type);
2253       int inter_ptr = POINTER_TYPE_P (inter_type);
2254       int inter_float = FLOAT_TYPE_P (inter_type);
2255       int inter_vec = VECTOR_TYPE_P (inter_type);
2256       unsigned int inter_prec = TYPE_PRECISION (inter_type);
2257       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2258       int final_int = INTEGRAL_TYPE_P (type);
2259       int final_ptr = POINTER_TYPE_P (type);
2260       int final_float = FLOAT_TYPE_P (type);
2261       int final_vec = VECTOR_TYPE_P (type);
2262       unsigned int final_prec = TYPE_PRECISION (type);
2263       int final_unsignedp = TYPE_UNSIGNED (type);
2264     }
2265    (switch
2266     /* In addition to the cases of two conversions in a row
2267        handled below, if we are converting something to its own
2268        type via an object of identical or wider precision, neither
2269        conversion is needed.  */
2270     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2271           || (GENERIC
2272               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2273          && (((inter_int || inter_ptr) && final_int)
2274              || (inter_float && final_float))
2275          && inter_prec >= final_prec)
2276      (ocvt @0))
2278     /* Likewise, if the intermediate and initial types are either both
2279        float or both integer, we don't need the middle conversion if the
2280        former is wider than the latter and doesn't change the signedness
2281        (for integers).  Avoid this if the final type is a pointer since
2282        then we sometimes need the middle conversion.  */
2283     (if (((inter_int && inside_int) || (inter_float && inside_float))
2284          && (final_int || final_float)
2285          && inter_prec >= inside_prec
2286          && (inter_float || inter_unsignedp == inside_unsignedp))
2287      (ocvt @0))
2289     /* If we have a sign-extension of a zero-extended value, we can
2290        replace that by a single zero-extension.  Likewise if the
2291        final conversion does not change precision we can drop the
2292        intermediate conversion.  */
2293     (if (inside_int && inter_int && final_int
2294          && ((inside_prec < inter_prec && inter_prec < final_prec
2295               && inside_unsignedp && !inter_unsignedp)
2296              || final_prec == inter_prec))
2297      (ocvt @0))
2299     /* Two conversions in a row are not needed unless:
2300         - some conversion is floating-point (overstrict for now), or
2301         - some conversion is a vector (overstrict for now), or
2302         - the intermediate type is narrower than both initial and
2303           final, or
2304         - the intermediate type and innermost type differ in signedness,
2305           and the outermost type is wider than the intermediate, or
2306         - the initial type is a pointer type and the precisions of the
2307           intermediate and final types differ, or
2308         - the final type is a pointer type and the precisions of the
2309           initial and intermediate types differ.  */
2310     (if (! inside_float && ! inter_float && ! final_float
2311          && ! inside_vec && ! inter_vec && ! final_vec
2312          && (inter_prec >= inside_prec || inter_prec >= final_prec)
2313          && ! (inside_int && inter_int
2314                && inter_unsignedp != inside_unsignedp
2315                && inter_prec < final_prec)
2316          && ((inter_unsignedp && inter_prec > inside_prec)
2317              == (final_unsignedp && final_prec > inter_prec))
2318          && ! (inside_ptr && inter_prec != final_prec)
2319          && ! (final_ptr && inside_prec != inter_prec))
2320      (ocvt @0))
2322     /* A truncation to an unsigned type (a zero-extension) should be
2323        canonicalized as bitwise and of a mask.  */
2324     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
2325          && final_int && inter_int && inside_int
2326          && final_prec == inside_prec
2327          && final_prec > inter_prec
2328          && inter_unsignedp)
2329      (convert (bit_and @0 { wide_int_to_tree
2330                               (inside_type,
2331                                wi::mask (inter_prec, false,
2332                                          TYPE_PRECISION (inside_type))); })))
2334     /* If we are converting an integer to a floating-point that can
2335        represent it exactly and back to an integer, we can skip the
2336        floating-point conversion.  */
2337     (if (GIMPLE /* PR66211 */
2338          && inside_int && inter_float && final_int &&
2339          (unsigned) significand_size (TYPE_MODE (inter_type))
2340          >= inside_prec - !inside_unsignedp)
2341      (convert @0)))))))
2343 /* If we have a narrowing conversion to an integral type that is fed by a
2344    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2345    masks off bits outside the final type (and nothing else).  */
2346 (simplify
2347   (convert (bit_and @0 INTEGER_CST@1))
2348   (if (INTEGRAL_TYPE_P (type)
2349        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2350        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2351        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2352                                                     TYPE_PRECISION (type)), 0))
2353    (convert @0)))
2356 /* (X /[ex] A) * A -> X.  */
2357 (simplify
2358   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2359   (convert @0))
2361 /* Canonicalization of binary operations.  */
2363 /* Convert X + -C into X - C.  */
2364 (simplify
2365  (plus @0 REAL_CST@1)
2366  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2367   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2368    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2369     (minus @0 { tem; })))))
2371 /* Convert x+x into x*2.  */
2372 (simplify
2373  (plus @0 @0)
2374  (if (SCALAR_FLOAT_TYPE_P (type))
2375   (mult @0 { build_real (type, dconst2); })
2376   (if (INTEGRAL_TYPE_P (type))
2377    (mult @0 { build_int_cst (type, 2); }))))
2379 (simplify
2380  (minus integer_zerop @1)
2381  (negate @1))
2383 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
2384    ARG0 is zero and X + ARG0 reduces to X, since that would mean
2385    (-ARG1 + ARG0) reduces to -ARG1.  */
2386 (simplify
2387  (minus real_zerop@0 @1)
2388  (if (fold_real_zero_addition_p (type, @0, 0))
2389   (negate @1)))
2391 /* Transform x * -1 into -x.  */
2392 (simplify
2393  (mult @0 integer_minus_onep)
2394  (negate @0))
2396 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
2397    signed overflow for CST != 0 && CST != -1.  */
2398 (simplify
2399  (mult:c (mult:s @0 INTEGER_CST@1) @2)
2400  (if (TREE_CODE (@2) != INTEGER_CST
2401       && !integer_zerop (@1) && !integer_minus_onep (@1))
2402   (mult (mult @0 @2) @1)))
2404 /* True if we can easily extract the real and imaginary parts of a complex
2405    number.  */
2406 (match compositional_complex
2407  (convert? (complex @0 @1)))
2409 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
2410 (simplify
2411  (complex (realpart @0) (imagpart @0))
2412  @0)
2413 (simplify
2414  (realpart (complex @0 @1))
2415  @0)
2416 (simplify
2417  (imagpart (complex @0 @1))
2418  @1)
2420 /* Sometimes we only care about half of a complex expression.  */
2421 (simplify
2422  (realpart (convert?:s (conj:s @0)))
2423  (convert (realpart @0)))
2424 (simplify
2425  (imagpart (convert?:s (conj:s @0)))
2426  (convert (negate (imagpart @0))))
2427 (for part (realpart imagpart)
2428  (for op (plus minus)
2429   (simplify
2430    (part (convert?:s@2 (op:s @0 @1)))
2431    (convert (op (part @0) (part @1))))))
2432 (simplify
2433  (realpart (convert?:s (CEXPI:s @0)))
2434  (convert (COS @0)))
2435 (simplify
2436  (imagpart (convert?:s (CEXPI:s @0)))
2437  (convert (SIN @0)))
2439 /* conj(conj(x)) -> x  */
2440 (simplify
2441  (conj (convert? (conj @0)))
2442  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2443   (convert @0)))
2445 /* conj({x,y}) -> {x,-y}  */
2446 (simplify
2447  (conj (convert?:s (complex:s @0 @1)))
2448  (with { tree itype = TREE_TYPE (type); }
2449   (complex (convert:itype @0) (negate (convert:itype @1)))))
2451 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
2452 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2453  (simplify
2454   (bswap (bswap @0))
2455   @0)
2456  (simplify
2457   (bswap (bit_not (bswap @0)))
2458   (bit_not @0))
2459  (for bitop (bit_xor bit_ior bit_and)
2460   (simplify
2461    (bswap (bitop:c (bswap @0) @1))
2462    (bitop @0 (bswap @1)))))
2465 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
2467 /* Simplify constant conditions.
2468    Only optimize constant conditions when the selected branch
2469    has the same type as the COND_EXPR.  This avoids optimizing
2470    away "c ? x : throw", where the throw has a void type.
2471    Note that we cannot throw away the fold-const.c variant nor
2472    this one as we depend on doing this transform before possibly
2473    A ? B : B -> B triggers and the fold-const.c one can optimize
2474    0 ? A : B to B even if A has side-effects.  Something
2475    genmatch cannot handle.  */
2476 (simplify
2477  (cond INTEGER_CST@0 @1 @2)
2478  (if (integer_zerop (@0))
2479   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2480    @2)
2481   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2482    @1)))
2483 (simplify
2484  (vec_cond VECTOR_CST@0 @1 @2)
2485  (if (integer_all_onesp (@0))
2486   @1
2487   (if (integer_zerop (@0))
2488    @2)))
2490 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
2491    be extended.  */
2492 /* This pattern implements two kinds simplification:
2494    Case 1)
2495    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2496      1) Conversions are type widening from smaller type.
2497      2) Const c1 equals to c2 after canonicalizing comparison.
2498      3) Comparison has tree code LT, LE, GT or GE.
2499    This specific pattern is needed when (cmp (convert x) c) may not
2500    be simplified by comparison patterns because of multiple uses of
2501    x.  It also makes sense here because simplifying across multiple
2502    referred var is always benefitial for complicated cases.
2504    Case 2)
2505    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
2506 (for cmp (lt le gt ge eq)
2507  (simplify
2508   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2509   (with
2510    {
2511      tree from_type = TREE_TYPE (@1);
2512      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2513      enum tree_code code = ERROR_MARK;
2515      if (INTEGRAL_TYPE_P (from_type)
2516          && int_fits_type_p (@2, from_type)
2517          && (types_match (c1_type, from_type)
2518              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2519                  && (TYPE_UNSIGNED (from_type)
2520                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2521          && (types_match (c2_type, from_type)
2522              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2523                  && (TYPE_UNSIGNED (from_type)
2524                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2525        {
2526          if (cmp != EQ_EXPR)
2527            {
2528              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2529                {
2530                  /* X <= Y - 1 equals to X < Y.  */
2531                  if (cmp == LE_EXPR)
2532                    code = LT_EXPR;
2533                  /* X > Y - 1 equals to X >= Y.  */
2534                  if (cmp == GT_EXPR)
2535                    code = GE_EXPR;
2536                }
2537              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2538                {
2539                  /* X < Y + 1 equals to X <= Y.  */
2540                  if (cmp == LT_EXPR)
2541                    code = LE_EXPR;
2542                  /* X >= Y + 1 equals to X > Y.  */
2543                  if (cmp == GE_EXPR)
2544                    code = GT_EXPR;
2545                }
2546              if (code != ERROR_MARK
2547                  || wi::to_widest (@2) == wi::to_widest (@3))
2548                {
2549                  if (cmp == LT_EXPR || cmp == LE_EXPR)
2550                    code = MIN_EXPR;
2551                  if (cmp == GT_EXPR || cmp == GE_EXPR)
2552                    code = MAX_EXPR;
2553                }
2554            }
2555          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
2556          else if (int_fits_type_p (@3, from_type))
2557            code = EQ_EXPR;
2558        }
2559    }
2560    (if (code == MAX_EXPR)
2561     (convert (max @1 (convert @2)))
2562     (if (code == MIN_EXPR)
2563      (convert (min @1 (convert @2)))
2564      (if (code == EQ_EXPR)
2565       (convert (cond (eq @1 (convert @3))
2566                      (convert:from_type @3) (convert:from_type @2)))))))))
2568 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2570      1) OP is PLUS or MINUS.
2571      2) CMP is LT, LE, GT or GE.
2572      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2574    This pattern also handles special cases like:
2576      A) Operand x is a unsigned to signed type conversion and c1 is
2577         integer zero.  In this case,
2578           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
2579           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
2580      B) Const c1 may not equal to (C3 op' C2).  In this case we also
2581         check equality for (c1+1) and (c1-1) by adjusting comparison
2582         code.
2584    TODO: Though signed type is handled by this pattern, it cannot be
2585    simplified at the moment because C standard requires additional
2586    type promotion.  In order to match&simplify it here, the IR needs
2587    to be cleaned up by other optimizers, i.e, VRP.  */
2588 (for op (plus minus)
2589  (for cmp (lt le gt ge)
2590   (simplify
2591    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2592    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2593     (if (types_match (from_type, to_type)
2594          /* Check if it is special case A).  */
2595          || (TYPE_UNSIGNED (from_type)
2596              && !TYPE_UNSIGNED (to_type)
2597              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2598              && integer_zerop (@1)
2599              && (cmp == LT_EXPR || cmp == GE_EXPR)))
2600      (with
2601       {
2602         bool overflow = false;
2603         enum tree_code code, cmp_code = cmp;
2604         wide_int real_c1;
2605         wide_int c1 = wi::to_wide (@1);
2606         wide_int c2 = wi::to_wide (@2);
2607         wide_int c3 = wi::to_wide (@3);
2608         signop sgn = TYPE_SIGN (from_type);
2610         /* Handle special case A), given x of unsigned type:
2611             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
2612             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
2613         if (!types_match (from_type, to_type))
2614           {
2615             if (cmp_code == LT_EXPR)
2616               cmp_code = GT_EXPR;
2617             if (cmp_code == GE_EXPR)
2618               cmp_code = LE_EXPR;
2619             c1 = wi::max_value (to_type);
2620           }
2621         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
2622            compute (c3 op' c2) and check if it equals to c1 with op' being
2623            the inverted operator of op.  Make sure overflow doesn't happen
2624            if it is undefined.  */
2625         if (op == PLUS_EXPR)
2626           real_c1 = wi::sub (c3, c2, sgn, &overflow);
2627         else
2628           real_c1 = wi::add (c3, c2, sgn, &overflow);
2630         code = cmp_code;
2631         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2632           {
2633             /* Check if c1 equals to real_c1.  Boundary condition is handled
2634                by adjusting comparison operation if necessary.  */
2635             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2636                 && !overflow)
2637               {
2638                 /* X <= Y - 1 equals to X < Y.  */
2639                 if (cmp_code == LE_EXPR)
2640                   code = LT_EXPR;
2641                 /* X > Y - 1 equals to X >= Y.  */
2642                 if (cmp_code == GT_EXPR)
2643                   code = GE_EXPR;
2644               }
2645             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2646                 && !overflow)
2647               {
2648                 /* X < Y + 1 equals to X <= Y.  */
2649                 if (cmp_code == LT_EXPR)
2650                   code = LE_EXPR;
2651                 /* X >= Y + 1 equals to X > Y.  */
2652                 if (cmp_code == GE_EXPR)
2653                   code = GT_EXPR;
2654               }
2655             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2656               {
2657                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2658                   code = MIN_EXPR;
2659                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2660                   code = MAX_EXPR;
2661               }
2662           }
2663       }
2664       (if (code == MAX_EXPR)
2665        (op (max @X { wide_int_to_tree (from_type, real_c1); })
2666            { wide_int_to_tree (from_type, c2); })
2667        (if (code == MIN_EXPR)
2668         (op (min @X { wide_int_to_tree (from_type, real_c1); })
2669             { wide_int_to_tree (from_type, c2); })))))))))
2671 (for cnd (cond vec_cond)
2672  /* A ? B : (A ? X : C) -> A ? B : C.  */
2673  (simplify
2674   (cnd @0 (cnd @0 @1 @2) @3)
2675   (cnd @0 @1 @3))
2676  (simplify
2677   (cnd @0 @1 (cnd @0 @2 @3))
2678   (cnd @0 @1 @3))
2679  /* A ? B : (!A ? C : X) -> A ? B : C.  */
2680  /* ???  This matches embedded conditions open-coded because genmatch
2681     would generate matching code for conditions in separate stmts only.
2682     The following is still important to merge then and else arm cases
2683     from if-conversion.  */
2684  (simplify
2685   (cnd @0 @1 (cnd @2 @3 @4))
2686   (if (COMPARISON_CLASS_P (@0)
2687        && COMPARISON_CLASS_P (@2)
2688        && invert_tree_comparison
2689            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2690        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2691        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2692    (cnd @0 @1 @3)))
2693  (simplify
2694   (cnd @0 (cnd @1 @2 @3) @4)
2695   (if (COMPARISON_CLASS_P (@0)
2696        && COMPARISON_CLASS_P (@1)
2697        && invert_tree_comparison
2698            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2699        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2700        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2701    (cnd @0 @3 @4)))
2703  /* A ? B : B -> B.  */
2704  (simplify
2705   (cnd @0 @1 @1)
2706   @1)
2708  /* !A ? B : C -> A ? C : B.  */
2709  (simplify
2710   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2711   (cnd @0 @2 @1)))
2713 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2714    return all -1 or all 0 results.  */
2715 /* ??? We could instead convert all instances of the vec_cond to negate,
2716    but that isn't necessarily a win on its own.  */
2717 (simplify
2718  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2719  (if (VECTOR_TYPE_P (type)
2720       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2721       && (TYPE_MODE (TREE_TYPE (type))
2722           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2723   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2725 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
2726 (simplify
2727  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2728  (if (VECTOR_TYPE_P (type)
2729       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2730       && (TYPE_MODE (TREE_TYPE (type))
2731           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2732   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2735 /* Simplifications of comparisons.  */
2737 /* See if we can reduce the magnitude of a constant involved in a
2738    comparison by changing the comparison code.  This is a canonicalization
2739    formerly done by maybe_canonicalize_comparison_1.  */
2740 (for cmp  (le gt)
2741      acmp (lt ge)
2742  (simplify
2743   (cmp @0 INTEGER_CST@1)
2744   (if (tree_int_cst_sgn (@1) == -1)
2745    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2746 (for cmp  (ge lt)
2747      acmp (gt le)
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); }))))
2754 /* We can simplify a logical negation of a comparison to the
2755    inverted comparison.  As we cannot compute an expression
2756    operator using invert_tree_comparison we have to simulate
2757    that with expression code iteration.  */
2758 (for cmp (tcc_comparison)
2759      icmp (inverted_tcc_comparison)
2760      ncmp (inverted_tcc_comparison_with_nans)
2761  /* Ideally we'd like to combine the following two patterns
2762     and handle some more cases by using
2763       (logical_inverted_value (cmp @0 @1))
2764     here but for that genmatch would need to "inline" that.
2765     For now implement what forward_propagate_comparison did.  */
2766  (simplify
2767   (bit_not (cmp @0 @1))
2768   (if (VECTOR_TYPE_P (type)
2769        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2770    /* Comparison inversion may be impossible for trapping math,
2771       invert_tree_comparison will tell us.  But we can't use
2772       a computed operator in the replacement tree thus we have
2773       to play the trick below.  */
2774    (with { enum tree_code ic = invert_tree_comparison
2775              (cmp, HONOR_NANS (@0)); }
2776     (if (ic == icmp)
2777      (icmp @0 @1)
2778      (if (ic == ncmp)
2779       (ncmp @0 @1))))))
2780  (simplify
2781   (bit_xor (cmp @0 @1) integer_truep)
2782   (with { enum tree_code ic = invert_tree_comparison
2783             (cmp, HONOR_NANS (@0)); }
2784    (if (ic == icmp)
2785     (icmp @0 @1)
2786     (if (ic == ncmp)
2787      (ncmp @0 @1))))))
2789 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2790    ??? The transformation is valid for the other operators if overflow
2791    is undefined for the type, but performing it here badly interacts
2792    with the transformation in fold_cond_expr_with_comparison which
2793    attempts to synthetize ABS_EXPR.  */
2794 (for cmp (eq ne)
2795  (simplify
2796   (cmp (minus@2 @0 @1) integer_zerop)
2797   (if (single_use (@2))
2798    (cmp @0 @1))))
2800 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2801    signed arithmetic case.  That form is created by the compiler
2802    often enough for folding it to be of value.  One example is in
2803    computing loop trip counts after Operator Strength Reduction.  */
2804 (for cmp (simple_comparison)
2805      scmp (swapped_simple_comparison)
2806  (simplify
2807   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2808   /* Handle unfolded multiplication by zero.  */
2809   (if (integer_zerop (@1))
2810    (cmp @1 @2)
2811    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2812         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2813         && single_use (@3))
2814     /* If @1 is negative we swap the sense of the comparison.  */
2815     (if (tree_int_cst_sgn (@1) < 0)
2816      (scmp @0 @2)
2817      (cmp @0 @2))))))
2819 /* Simplify comparison of something with itself.  For IEEE
2820    floating-point, we can only do some of these simplifications.  */
2821 (for cmp (eq ge le)
2822  (simplify
2823   (cmp @0 @0)
2824   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2825        || ! HONOR_NANS (@0))
2826    { constant_boolean_node (true, type); }
2827    (if (cmp != EQ_EXPR)
2828     (eq @0 @0)))))
2829 (for cmp (ne gt lt)
2830  (simplify
2831   (cmp @0 @0)
2832   (if (cmp != NE_EXPR
2833        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2834        || ! HONOR_NANS (@0))
2835    { constant_boolean_node (false, type); })))
2836 (for cmp (unle unge uneq)
2837  (simplify
2838   (cmp @0 @0)
2839   { constant_boolean_node (true, type); }))
2840 (for cmp (unlt ungt)
2841  (simplify
2842   (cmp @0 @0)
2843   (unordered @0 @0)))
2844 (simplify
2845  (ltgt @0 @0)
2846  (if (!flag_trapping_math)
2847   { constant_boolean_node (false, type); }))
2849 /* Fold ~X op ~Y as Y op X.  */
2850 (for cmp (simple_comparison)
2851  (simplify
2852   (cmp (bit_not@2 @0) (bit_not@3 @1))
2853   (if (single_use (@2) && single_use (@3))
2854    (cmp @1 @0))))
2856 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
2857 (for cmp (simple_comparison)
2858      scmp (swapped_simple_comparison)
2859  (simplify
2860   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2861   (if (single_use (@2)
2862        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2863    (scmp @0 (bit_not @1)))))
2865 (for cmp (simple_comparison)
2866  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
2867  (simplify
2868   (cmp (convert@2 @0) (convert? @1))
2869   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2870        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2871            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2872        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2873            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2874    (with
2875     {
2876       tree type1 = TREE_TYPE (@1);
2877       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2878         {
2879           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2880           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2881               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2882             type1 = float_type_node;
2883           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2884               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2885             type1 = double_type_node;
2886         }
2887       tree newtype
2888         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2889            ? TREE_TYPE (@0) : type1); 
2890     }
2891     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2892      (cmp (convert:newtype @0) (convert:newtype @1))))))
2894  (simplify
2895   (cmp @0 REAL_CST@1)
2896   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
2897   (switch
2898    /* a CMP (-0) -> a CMP 0  */
2899    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2900     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2901    /* x != NaN is always true, other ops are always false.  */
2902    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2903         && ! HONOR_SNANS (@1))
2904     { constant_boolean_node (cmp == NE_EXPR, type); })
2905    /* Fold comparisons against infinity.  */
2906    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2907         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2908     (with
2909      {
2910        REAL_VALUE_TYPE max;
2911        enum tree_code code = cmp;
2912        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2913        if (neg)
2914          code = swap_tree_comparison (code);
2915      }
2916      (switch
2917       /* x > +Inf is always false, if with ignore sNANs.  */
2918       (if (code == GT_EXPR
2919            && ! HONOR_SNANS (@0))
2920        { constant_boolean_node (false, type); })
2921       (if (code == LE_EXPR)
2922        /* x <= +Inf is always true, if we don't case about NaNs.  */
2923        (if (! HONOR_NANS (@0))
2924         { constant_boolean_node (true, type); }
2925         /* x <= +Inf is the same as x == x, i.e. !isnan(x).  */
2926         (eq @0 @0)))
2927       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
2928       (if (code == EQ_EXPR || code == GE_EXPR)
2929        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2930         (if (neg)
2931          (lt @0 { build_real (TREE_TYPE (@0), max); })
2932          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2933       /* x < +Inf is always equal to x <= DBL_MAX.  */
2934       (if (code == LT_EXPR)
2935        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2936         (if (neg)
2937          (ge @0 { build_real (TREE_TYPE (@0), max); })
2938          (le @0 { build_real (TREE_TYPE (@0), max); }))))
2939       /* x != +Inf is always equal to !(x > DBL_MAX).  */
2940       (if (code == NE_EXPR)
2941        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2942         (if (! HONOR_NANS (@0))
2943          (if (neg)
2944           (ge @0 { build_real (TREE_TYPE (@0), max); })
2945           (le @0 { build_real (TREE_TYPE (@0), max); }))
2946          (if (neg)
2947           (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2948            { build_one_cst (type); })
2949           (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2950            { build_one_cst (type); }))))))))))
2952  /* If this is a comparison of a real constant with a PLUS_EXPR
2953     or a MINUS_EXPR of a real constant, we can convert it into a
2954     comparison with a revised real constant as long as no overflow
2955     occurs when unsafe_math_optimizations are enabled.  */
2956  (if (flag_unsafe_math_optimizations)
2957   (for op (plus minus)
2958    (simplify
2959     (cmp (op @0 REAL_CST@1) REAL_CST@2)
2960     (with
2961      {
2962        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2963                                TREE_TYPE (@1), @2, @1);
2964      }
2965      (if (tem && !TREE_OVERFLOW (tem))
2966       (cmp @0 { tem; }))))))
2968  /* Likewise, we can simplify a comparison of a real constant with
2969     a MINUS_EXPR whose first operand is also a real constant, i.e.
2970     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
2971     floating-point types only if -fassociative-math is set.  */
2972  (if (flag_associative_math)
2973   (simplify
2974    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2975    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2976     (if (tem && !TREE_OVERFLOW (tem))
2977      (cmp { tem; } @1)))))
2979  /* Fold comparisons against built-in math functions.  */
2980  (if (flag_unsafe_math_optimizations
2981       && ! flag_errno_math)
2982   (for sq (SQRT)
2983    (simplify
2984     (cmp (sq @0) REAL_CST@1)
2985     (switch
2986      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2987       (switch
2988        /* sqrt(x) < y is always false, if y is negative.  */
2989        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2990         { constant_boolean_node (false, type); })
2991        /* sqrt(x) > y is always true, if y is negative and we
2992           don't care about NaNs, i.e. negative values of x.  */
2993        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2994         { constant_boolean_node (true, type); })
2995        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
2996        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2997      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2998       (switch
2999        /* sqrt(x) < 0 is always false.  */
3000        (if (cmp == LT_EXPR)
3001         { constant_boolean_node (false, type); })
3002        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
3003        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3004         { constant_boolean_node (true, type); })
3005        /* sqrt(x) <= 0 -> x == 0.  */
3006        (if (cmp == LE_EXPR)
3007         (eq @0 @1))
3008        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
3009           == or !=.  In the last case:
3011             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3013           if x is negative or NaN.  Due to -funsafe-math-optimizations,
3014           the results for other x follow from natural arithmetic.  */
3015        (cmp @0 @1)))
3016      (if (cmp == GT_EXPR || cmp == GE_EXPR)
3017       (with
3018        {
3019          REAL_VALUE_TYPE c2;
3020          real_arithmetic (&c2, MULT_EXPR,
3021                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3022          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3023        }
3024        (if (REAL_VALUE_ISINF (c2))
3025         /* sqrt(x) > y is x == +Inf, when y is very large.  */
3026         (if (HONOR_INFINITIES (@0))
3027          (eq @0 { build_real (TREE_TYPE (@0), c2); })
3028          { constant_boolean_node (false, type); })
3029         /* sqrt(x) > c is the same as x > c*c.  */
3030         (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3031      (if (cmp == LT_EXPR || cmp == LE_EXPR)
3032       (with
3033        {
3034          REAL_VALUE_TYPE c2;
3035          real_arithmetic (&c2, MULT_EXPR,
3036                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3037          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3038        }
3039        (if (REAL_VALUE_ISINF (c2))
3040         (switch
3041          /* sqrt(x) < y is always true, when y is a very large
3042             value and we don't care about NaNs or Infinities.  */
3043          (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3044           { constant_boolean_node (true, type); })
3045          /* sqrt(x) < y is x != +Inf when y is very large and we
3046             don't care about NaNs.  */
3047          (if (! HONOR_NANS (@0))
3048           (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3049          /* sqrt(x) < y is x >= 0 when y is very large and we
3050             don't care about Infinities.  */
3051          (if (! HONOR_INFINITIES (@0))
3052           (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3053          /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
3054          (if (GENERIC)
3055           (truth_andif
3056            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3057            (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3058         /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
3059         (if (! HONOR_NANS (@0))
3060          (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3061          /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
3062          (if (GENERIC)
3063           (truth_andif
3064            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3065            (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3066    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
3067    (simplify
3068     (cmp (sq @0) (sq @1))
3069       (if (! HONOR_NANS (@0))
3070         (cmp @0 @1))))))
3072 /* Optimize various special cases of (FTYPE) N CMP CST.  */
3073 (for cmp  (lt le eq ne ge gt)
3074      icmp (le le eq ne ge ge)
3075  (simplify
3076   (cmp (float @0) REAL_CST@1)
3077    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3078         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3079     (with
3080      {
3081        tree itype = TREE_TYPE (@0);
3082        signop isign = TYPE_SIGN (itype);
3083        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3084        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3085        /* Be careful to preserve any potential exceptions due to
3086           NaNs.  qNaNs are ok in == or != context.
3087           TODO: relax under -fno-trapping-math or
3088           -fno-signaling-nans.  */
3089        bool exception_p
3090          = real_isnan (cst) && (cst->signalling
3091                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3092        /* INT?_MIN is power-of-two so it takes
3093           only one mantissa bit.  */
3094        bool signed_p = isign == SIGNED;
3095        bool itype_fits_ftype_p
3096          = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3097      }
3098      /* TODO: allow non-fitting itype and SNaNs when
3099         -fno-trapping-math.  */
3100      (if (itype_fits_ftype_p && ! exception_p)
3101       (with
3102        {
3103          REAL_VALUE_TYPE imin, imax;
3104          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3105          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3107          REAL_VALUE_TYPE icst;
3108          if (cmp == GT_EXPR || cmp == GE_EXPR)
3109            real_ceil (&icst, fmt, cst);
3110          else if (cmp == LT_EXPR || cmp == LE_EXPR)
3111            real_floor (&icst, fmt, cst);
3112          else
3113            real_trunc (&icst, fmt, cst);
3115          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3117          bool overflow_p = false;
3118          wide_int icst_val
3119            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3120        }
3121        (switch
3122         /* Optimize cases when CST is outside of ITYPE's range.  */
3123         (if (real_compare (LT_EXPR, cst, &imin))
3124          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3125                                   type); })
3126         (if (real_compare (GT_EXPR, cst, &imax))
3127          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3128                                   type); })
3129         /* Remove cast if CST is an integer representable by ITYPE.  */
3130         (if (cst_int_p)
3131          (cmp @0 { gcc_assert (!overflow_p);
3132                    wide_int_to_tree (itype, icst_val); })
3133         )
3134         /* When CST is fractional, optimize
3135             (FTYPE) N == CST -> 0
3136             (FTYPE) N != CST -> 1.  */
3137         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3138          { constant_boolean_node (cmp == NE_EXPR, type); }) 
3139         /* Otherwise replace with sensible integer constant.  */
3140         (with
3141          {
3142            gcc_checking_assert (!overflow_p);
3143          }
3144          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3146 /* Fold A /[ex] B CMP C to A CMP B * C.  */
3147 (for cmp (eq ne)
3148  (simplify
3149   (cmp (exact_div @0 @1) INTEGER_CST@2)
3150   (if (!integer_zerop (@1))
3151    (if (wi::to_wide (@2) == 0)
3152     (cmp @0 @2)
3153     (if (TREE_CODE (@1) == INTEGER_CST)
3154      (with
3155       {
3156         bool ovf;
3157         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3158                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3159       }
3160       (if (ovf)
3161        { constant_boolean_node (cmp == NE_EXPR, type); }
3162        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3163 (for cmp (lt le gt ge)
3164  (simplify
3165   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3166   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3167    (with
3168     {
3169       bool ovf;
3170       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3171                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3172     }
3173     (if (ovf)
3174      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3175                                         TYPE_SIGN (TREE_TYPE (@2)))
3176                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3177      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3179 /* Unordered tests if either argument is a NaN.  */
3180 (simplify
3181  (bit_ior (unordered @0 @0) (unordered @1 @1))
3182  (if (types_match (@0, @1))
3183   (unordered @0 @1)))
3184 (simplify
3185  (bit_and (ordered @0 @0) (ordered @1 @1))
3186  (if (types_match (@0, @1))
3187   (ordered @0 @1)))
3188 (simplify
3189  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3190  @2)
3191 (simplify
3192  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3193  @2)
3195 /* Simple range test simplifications.  */
3196 /* A < B || A >= B -> true.  */
3197 (for test1 (lt le le le ne ge)
3198      test2 (ge gt ge ne eq ne)
3199  (simplify
3200   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3201   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3202        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3203    { constant_boolean_node (true, type); })))
3204 /* A < B && A >= B -> false.  */
3205 (for test1 (lt lt lt le ne eq)
3206      test2 (ge gt eq gt eq gt)
3207  (simplify
3208   (bit_and:c (test1 @0 @1) (test2 @0 @1))
3209   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3210        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3211    { constant_boolean_node (false, type); })))
3213 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3214    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
3216    Note that comparisons
3217      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
3218      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
3219    will be canonicalized to above so there's no need to
3220    consider them here.
3221  */
3223 (for cmp (le gt)
3224      eqcmp (eq ne)
3225  (simplify
3226   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3227   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3228    (with
3229     {
3230      tree ty = TREE_TYPE (@0);
3231      unsigned prec = TYPE_PRECISION (ty);
3232      wide_int mask = wi::to_wide (@2, prec);
3233      wide_int rhs = wi::to_wide (@3, prec);
3234      signop sgn = TYPE_SIGN (ty);
3235     }
3236     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3237          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3238       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3239              { build_zero_cst (ty); }))))))
3241 /* -A CMP -B -> B CMP A.  */
3242 (for cmp (tcc_comparison)
3243      scmp (swapped_tcc_comparison)
3244  (simplify
3245   (cmp (negate @0) (negate @1))
3246   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3247        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3248            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3249    (scmp @0 @1)))
3250  (simplify
3251   (cmp (negate @0) CONSTANT_CLASS_P@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    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3256     (if (tem && !TREE_OVERFLOW (tem))
3257      (scmp @0 { tem; }))))))
3259 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
3260 (for op (eq ne)
3261  (simplify
3262   (op (abs @0) zerop@1)
3263   (op @0 @1)))
3265 /* From fold_sign_changed_comparison and fold_widened_comparison.
3266    FIXME: the lack of symmetry is disturbing.  */
3267 (for cmp (simple_comparison)
3268  (simplify
3269   (cmp (convert@0 @00) (convert?@1 @10))
3270   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3271        /* Disable this optimization if we're casting a function pointer
3272           type on targets that require function pointer canonicalization.  */
3273        && !(targetm.have_canonicalize_funcptr_for_compare ()
3274             && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3275             && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3276        && single_use (@0))
3277    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3278         && (TREE_CODE (@10) == INTEGER_CST
3279             || @1 != @10)
3280         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3281             || cmp == NE_EXPR
3282             || cmp == EQ_EXPR)
3283         && !POINTER_TYPE_P (TREE_TYPE (@00)))
3284     /* ???  The special-casing of INTEGER_CST conversion was in the original
3285        code and here to avoid a spurious overflow flag on the resulting
3286        constant which fold_convert produces.  */
3287     (if (TREE_CODE (@1) == INTEGER_CST)
3288      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3289                                 TREE_OVERFLOW (@1)); })
3290      (cmp @00 (convert @1)))
3292     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3293      /* If possible, express the comparison in the shorter mode.  */
3294      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3295            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3296            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3297                && TYPE_UNSIGNED (TREE_TYPE (@00))))
3298           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3299               || ((TYPE_PRECISION (TREE_TYPE (@00))
3300                    >= TYPE_PRECISION (TREE_TYPE (@10)))
3301                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
3302                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
3303               || (TREE_CODE (@10) == INTEGER_CST
3304                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3305                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
3306       (cmp @00 (convert @10))
3307       (if (TREE_CODE (@10) == INTEGER_CST
3308            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3309            && !int_fits_type_p (@10, TREE_TYPE (@00)))
3310        (with
3311         {
3312           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3313           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3314           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3315           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3316         }
3317         (if (above || below)
3318          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3319           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3320           (if (cmp == LT_EXPR || cmp == LE_EXPR)
3321            { constant_boolean_node (above ? true : false, type); }
3322            (if (cmp == GT_EXPR || cmp == GE_EXPR)
3323             { constant_boolean_node (above ? false : true, type); }))))))))))))
3325 (for cmp (eq ne)
3326  /* A local variable can never be pointed to by
3327     the default SSA name of an incoming parameter.
3328     SSA names are canonicalized to 2nd place.  */
3329  (simplify
3330   (cmp addr@0 SSA_NAME@1)
3331   (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3332        && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3333    (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3334     (if (TREE_CODE (base) == VAR_DECL
3335          && auto_var_in_fn_p (base, current_function_decl))
3336      (if (cmp == NE_EXPR)
3337       { constant_boolean_node (true, type); }
3338       { constant_boolean_node (false, type); }))))))
3340 /* Equality compare simplifications from fold_binary  */
3341 (for cmp (eq ne)
3343  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3344     Similarly for NE_EXPR.  */
3345  (simplify
3346   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3347   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3348        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3349    { constant_boolean_node (cmp == NE_EXPR, type); }))
3351  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
3352  (simplify
3353   (cmp (bit_xor @0 @1) integer_zerop)
3354   (cmp @0 @1))
3356  /* (X ^ Y) == Y becomes X == 0.
3357     Likewise (X ^ Y) == X becomes Y == 0.  */
3358  (simplify
3359   (cmp:c (bit_xor:c @0 @1) @0)
3360   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3362  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
3363  (simplify
3364   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3365   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3366    (cmp @0 (bit_xor @1 (convert @2)))))
3368  (simplify
3369   (cmp (convert? addr@0) integer_zerop)
3370   (if (tree_single_nonzero_warnv_p (@0, NULL))
3371    { constant_boolean_node (cmp == NE_EXPR, type); })))
3373 /* If we have (A & C) == C where C is a power of 2, convert this into
3374    (A & C) != 0.  Similarly for NE_EXPR.  */
3375 (for cmp (eq ne)
3376      icmp (ne eq)
3377  (simplify
3378   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3379   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3381 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3382    convert this into a shift followed by ANDing with D.  */
3383 (simplify
3384  (cond
3385   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3386   integer_pow2p@2 integer_zerop)
3387  (with {
3388     int shift = (wi::exact_log2 (wi::to_wide (@2))
3389                  - wi::exact_log2 (wi::to_wide (@1)));
3390   }
3391   (if (shift > 0)
3392    (bit_and
3393     (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3394    (bit_and
3395     (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3397 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3398    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
3399 (for cmp (eq ne)
3400      ncmp (ge lt)
3401  (simplify
3402   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3403   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3404        && type_has_mode_precision_p (TREE_TYPE (@0))
3405        && element_precision (@2) >= element_precision (@0)
3406        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3407    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3408     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3410 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3411    this into a right shift or sign extension followed by ANDing with C.  */
3412 (simplify
3413  (cond
3414   (lt @0 integer_zerop)
3415   integer_pow2p@1 integer_zerop)
3416  (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3417   (with {
3418     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3419    }
3420    (if (shift >= 0)
3421     (bit_and
3422      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3423      @1)
3424     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3425        sign extension followed by AND with C will achieve the effect.  */
3426     (bit_and (convert @0) @1)))))
3428 /* When the addresses are not directly of decls compare base and offset.
3429    This implements some remaining parts of fold_comparison address
3430    comparisons but still no complete part of it.  Still it is good
3431    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
3432 (for cmp (simple_comparison)
3433  (simplify
3434   (cmp (convert1?@2 addr@0) (convert2? addr@1))
3435   (with
3436    {
3437      HOST_WIDE_INT off0, off1;
3438      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3439      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3440      if (base0 && TREE_CODE (base0) == MEM_REF)
3441        {
3442          off0 += mem_ref_offset (base0).to_short_addr ();
3443          base0 = TREE_OPERAND (base0, 0);
3444        }
3445      if (base1 && TREE_CODE (base1) == MEM_REF)
3446        {
3447          off1 += mem_ref_offset (base1).to_short_addr ();
3448          base1 = TREE_OPERAND (base1, 0);
3449        }
3450    }
3451    (if (base0 && base1)
3452     (with
3453      {
3454        int equal = 2;
3455        /* Punt in GENERIC on variables with value expressions;
3456           the value expressions might point to fields/elements
3457           of other vars etc.  */
3458        if (GENERIC
3459            && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3460                || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3461          ;
3462        else if (decl_in_symtab_p (base0)
3463                 && decl_in_symtab_p (base1))
3464          equal = symtab_node::get_create (base0)
3465                    ->equal_address_to (symtab_node::get_create (base1));
3466        else if ((DECL_P (base0)
3467                  || TREE_CODE (base0) == SSA_NAME
3468                  || TREE_CODE (base0) == STRING_CST)
3469                 && (DECL_P (base1)
3470                     || TREE_CODE (base1) == SSA_NAME
3471                     || TREE_CODE (base1) == STRING_CST))
3472          equal = (base0 == base1);
3473      }
3474      (if (equal == 1)
3475       (switch
3476        (if (cmp == EQ_EXPR)
3477         { constant_boolean_node (off0 == off1, type); })
3478        (if (cmp == NE_EXPR)
3479         { constant_boolean_node (off0 != off1, type); })
3480        (if (cmp == LT_EXPR)
3481         { constant_boolean_node (off0 < off1, type); })
3482        (if (cmp == LE_EXPR)
3483         { constant_boolean_node (off0 <= off1, type); })
3484        (if (cmp == GE_EXPR)
3485         { constant_boolean_node (off0 >= off1, type); })
3486        (if (cmp == GT_EXPR)
3487         { constant_boolean_node (off0 > off1, type); }))
3488       (if (equal == 0
3489            && DECL_P (base0) && DECL_P (base1)
3490            /* If we compare this as integers require equal offset.  */
3491            && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3492                || off0 == off1))
3493        (switch
3494         (if (cmp == EQ_EXPR)
3495          { constant_boolean_node (false, type); })
3496         (if (cmp == NE_EXPR)
3497          { constant_boolean_node (true, type); })))))))))
3499 /* Simplify pointer equality compares using PTA.  */
3500 (for neeq (ne eq)
3501  (simplify
3502   (neeq @0 @1)
3503   (if (POINTER_TYPE_P (TREE_TYPE (@0))
3504        && ptrs_compare_unequal (@0, @1))
3505    { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3507 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3508    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3509    Disable the transform if either operand is pointer to function.
3510    This broke pr22051-2.c for arm where function pointer
3511    canonicalizaion is not wanted.  */
3513 (for cmp (ne eq)
3514  (simplify
3515   (cmp (convert @0) INTEGER_CST@1)
3516   (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3517         && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3518       || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3519           && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3520    (cmp @0 (convert @1)))))
3522 /* Non-equality compare simplifications from fold_binary  */
3523 (for cmp (lt gt le ge)
3524  /* Comparisons with the highest or lowest possible integer of
3525     the specified precision will have known values.  */
3526  (simplify
3527   (cmp (convert?@2 @0) INTEGER_CST@1)
3528   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3529        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3530    (with
3531     {
3532       tree arg1_type = TREE_TYPE (@1);
3533       unsigned int prec = TYPE_PRECISION (arg1_type);
3534       wide_int max = wi::max_value (arg1_type);
3535       wide_int signed_max = wi::max_value (prec, SIGNED);
3536       wide_int min = wi::min_value (arg1_type);
3537     }
3538     (switch
3539      (if (wi::to_wide (@1) == max)
3540       (switch
3541        (if (cmp == GT_EXPR)
3542         { constant_boolean_node (false, type); })
3543        (if (cmp == GE_EXPR)
3544         (eq @2 @1))
3545        (if (cmp == LE_EXPR)
3546         { constant_boolean_node (true, type); })
3547        (if (cmp == LT_EXPR)
3548         (ne @2 @1))))
3549      (if (wi::to_wide (@1) == min)
3550       (switch
3551        (if (cmp == LT_EXPR)
3552         { constant_boolean_node (false, type); })
3553        (if (cmp == LE_EXPR)
3554         (eq @2 @1))
3555        (if (cmp == GE_EXPR)
3556         { constant_boolean_node (true, type); })
3557        (if (cmp == GT_EXPR)
3558         (ne @2 @1))))
3559      (if (wi::to_wide (@1) == max - 1)
3560       (switch
3561        (if (cmp == GT_EXPR)
3562         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3563        (if (cmp == LE_EXPR)
3564         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3565      (if (wi::to_wide (@1) == min + 1)
3566       (switch
3567        (if (cmp == GE_EXPR)
3568         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3569        (if (cmp == LT_EXPR)
3570         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3571      (if (wi::to_wide (@1) == signed_max
3572           && TYPE_UNSIGNED (arg1_type)
3573           /* We will flip the signedness of the comparison operator
3574              associated with the mode of @1, so the sign bit is
3575              specified by this mode.  Check that @1 is the signed
3576              max associated with this sign bit.  */
3577           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3578           /* signed_type does not work on pointer types.  */
3579           && INTEGRAL_TYPE_P (arg1_type))
3580       /* The following case also applies to X < signed_max+1
3581          and X >= signed_max+1 because previous transformations.  */
3582       (if (cmp == LE_EXPR || cmp == GT_EXPR)
3583        (with { tree st = signed_type_for (arg1_type); }
3584         (if (cmp == LE_EXPR)
3585          (ge (convert:st @0) { build_zero_cst (st); })
3586          (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3588 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3589  /* If the second operand is NaN, the result is constant.  */
3590  (simplify
3591   (cmp @0 REAL_CST@1)
3592   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3593        && (cmp != LTGT_EXPR || ! flag_trapping_math))
3594    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3595                             ? false : true, type); })))
3597 /* bool_var != 0 becomes bool_var.  */
3598 (simplify
3599  (ne @0 integer_zerop)
3600  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3601       && types_match (type, TREE_TYPE (@0)))
3602   (non_lvalue @0)))
3603 /* bool_var == 1 becomes bool_var.  */
3604 (simplify
3605  (eq @0 integer_onep)
3606  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3607       && types_match (type, TREE_TYPE (@0)))
3608   (non_lvalue @0)))
3609 /* Do not handle
3610    bool_var == 0 becomes !bool_var or
3611    bool_var != 1 becomes !bool_var
3612    here because that only is good in assignment context as long
3613    as we require a tcc_comparison in GIMPLE_CONDs where we'd
3614    replace if (x == 0) with tem = ~x; if (tem != 0) which is
3615    clearly less optimal and which we'll transform again in forwprop.  */
3617 /* When one argument is a constant, overflow detection can be simplified.
3618    Currently restricted to single use so as not to interfere too much with
3619    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3620    A + CST CMP A  ->  A CMP' CST' */
3621 (for cmp (lt le ge gt)
3622      out (gt gt le le)
3623  (simplify
3624   (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3625   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3626        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3627        && wi::to_wide (@1) != 0
3628        && single_use (@2))
3629    (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3630     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3631                                 wi::max_value (prec, UNSIGNED)
3632                                 - wi::to_wide (@1)); })))))
3634 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3635    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3636    expects the long form, so we restrict the transformation for now.  */
3637 (for cmp (gt le)
3638  (simplify
3639   (cmp:c (minus@2 @0 @1) @0)
3640   (if (single_use (@2)
3641        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3642        && TYPE_UNSIGNED (TREE_TYPE (@0))
3643        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3644    (cmp @1 @0))))
3646 /* Testing for overflow is unnecessary if we already know the result.  */
3647 /* A - B > A  */
3648 (for cmp (gt le)
3649      out (ne eq)
3650  (simplify
3651   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3652   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3653        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3654    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3655 /* A + B < A  */
3656 (for cmp (lt ge)
3657      out (ne eq)
3658  (simplify
3659   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3660   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3661        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3662    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3664 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3665    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
3666 (for cmp (lt ge)
3667      out (ne eq)
3668  (simplify
3669   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3670   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3671    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3672     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3674 /* Simplification of math builtins.  These rules must all be optimizations
3675    as well as IL simplifications.  If there is a possibility that the new
3676    form could be a pessimization, the rule should go in the canonicalization
3677    section that follows this one.
3679    Rules can generally go in this section if they satisfy one of
3680    the following:
3682    - the rule describes an identity
3684    - the rule replaces calls with something as simple as addition or
3685      multiplication
3687    - the rule contains unary calls only and simplifies the surrounding
3688      arithmetic.  (The idea here is to exclude non-unary calls in which
3689      one operand is constant and in which the call is known to be cheap
3690      when the operand has that value.)  */
3692 (if (flag_unsafe_math_optimizations)
3693  /* Simplify sqrt(x) * sqrt(x) -> x.  */
3694  (simplify
3695   (mult (SQRT@1 @0) @1)
3696   (if (!HONOR_SNANS (type))
3697    @0))
3699  (for op (plus minus)
3700   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
3701   (simplify
3702    (op (rdiv @0 @1)
3703        (rdiv @2 @1))
3704    (rdiv (op @0 @2) @1)))
3706  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
3707  (for root (SQRT CBRT)
3708   (simplify
3709    (mult (root:s @0) (root:s @1))
3710     (root (mult @0 @1))))
3712  /* Simplify expN(x) * expN(y) -> expN(x+y). */
3713  (for exps (EXP EXP2 EXP10 POW10)
3714   (simplify
3715    (mult (exps:s @0) (exps:s @1))
3716     (exps (plus @0 @1))))
3718  /* Simplify a/root(b/c) into a*root(c/b).  */
3719  (for root (SQRT CBRT)
3720   (simplify
3721    (rdiv @0 (root:s (rdiv:s @1 @2)))
3722     (mult @0 (root (rdiv @2 @1)))))
3724  /* Simplify x/expN(y) into x*expN(-y).  */
3725  (for exps (EXP EXP2 EXP10 POW10)
3726   (simplify
3727    (rdiv @0 (exps:s @1))
3728     (mult @0 (exps (negate @1)))))
3730  (for logs (LOG LOG2 LOG10 LOG10)
3731       exps (EXP EXP2 EXP10 POW10)
3732   /* logN(expN(x)) -> x.  */
3733   (simplify
3734    (logs (exps @0))
3735    @0)
3736   /* expN(logN(x)) -> x.  */
3737   (simplify
3738    (exps (logs @0))
3739    @0))
3741  /* Optimize logN(func()) for various exponential functions.  We
3742     want to determine the value "x" and the power "exponent" in
3743     order to transform logN(x**exponent) into exponent*logN(x).  */
3744  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
3745       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
3746   (simplify
3747    (logs (exps @0))
3748    (if (SCALAR_FLOAT_TYPE_P (type))
3749     (with {
3750       tree x;
3751       switch (exps)
3752         {
3753         CASE_CFN_EXP:
3754           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
3755           x = build_real_truncate (type, dconst_e ());
3756           break;
3757         CASE_CFN_EXP2:
3758           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
3759           x = build_real (type, dconst2);
3760           break;
3761         CASE_CFN_EXP10:
3762         CASE_CFN_POW10:
3763           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
3764           {
3765             REAL_VALUE_TYPE dconst10;
3766             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3767             x = build_real (type, dconst10);
3768           }
3769           break;
3770         default:
3771           gcc_unreachable ();
3772         }
3773       }
3774      (mult (logs { x; }) @0)))))
3776  (for logs (LOG LOG
3777             LOG2 LOG2
3778             LOG10 LOG10)
3779       exps (SQRT CBRT)
3780   (simplify
3781    (logs (exps @0))
3782    (if (SCALAR_FLOAT_TYPE_P (type))
3783     (with {
3784       tree x;
3785       switch (exps)
3786         {
3787         CASE_CFN_SQRT:
3788           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
3789           x = build_real (type, dconsthalf);
3790           break;
3791         CASE_CFN_CBRT:
3792           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
3793           x = build_real_truncate (type, dconst_third ());
3794           break;
3795         default:
3796           gcc_unreachable ();
3797         }
3798       }
3799      (mult { x; } (logs @0))))))
3801  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
3802  (for logs (LOG LOG2 LOG10)
3803       pows (POW)
3804   (simplify
3805    (logs (pows @0 @1))
3806    (mult @1 (logs @0))))
3808  /* pow(C,x) -> exp(log(C)*x) if C > 0.  */
3809  (for pows (POW)
3810       exps (EXP)
3811       logs (LOG)
3812   (simplify
3813    (pows REAL_CST@0 @1)
3814     (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
3815          && real_isfinite (TREE_REAL_CST_PTR (@0)))
3816      (exps (mult (logs @0) @1)))))
3818  (for sqrts (SQRT)
3819       cbrts (CBRT)
3820       pows (POW)
3821       exps (EXP EXP2 EXP10 POW10)
3822   /* sqrt(expN(x)) -> expN(x*0.5).  */
3823   (simplify
3824    (sqrts (exps @0))
3825    (exps (mult @0 { build_real (type, dconsthalf); })))
3826   /* cbrt(expN(x)) -> expN(x/3).  */
3827   (simplify
3828    (cbrts (exps @0))
3829    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3830   /* pow(expN(x), y) -> expN(x*y).  */
3831   (simplify
3832    (pows (exps @0) @1)
3833    (exps (mult @0 @1))))
3835  /* tan(atan(x)) -> x.  */
3836  (for tans (TAN)
3837       atans (ATAN)
3838   (simplify
3839    (tans (atans @0))
3840    @0)))
3842 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
3843 (simplify
3844  (CABS (complex:C @0 real_zerop@1))
3845  (abs @0))
3847 /* trunc(trunc(x)) -> trunc(x), etc.  */
3848 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3849  (simplify
3850   (fns (fns @0))
3851   (fns @0)))
3852 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
3853 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3854  (simplify
3855   (fns integer_valued_real_p@0)
3856   @0))
3858 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
3859 (simplify
3860  (HYPOT:c @0 real_zerop@1)
3861  (abs @0))
3863 /* pow(1,x) -> 1.  */
3864 (simplify
3865  (POW real_onep@0 @1)
3866  @0)
3868 (simplify
3869  /* copysign(x,x) -> x.  */
3870  (COPYSIGN @0 @0)
3871  @0)
3873 (simplify
3874  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
3875  (COPYSIGN @0 tree_expr_nonnegative_p@1)
3876  (abs @0))
3878 (for scale (LDEXP SCALBN SCALBLN)
3879  /* ldexp(0, x) -> 0.  */
3880  (simplify
3881   (scale real_zerop@0 @1)
3882   @0)
3883  /* ldexp(x, 0) -> x.  */
3884  (simplify
3885   (scale @0 integer_zerop@1)
3886   @0)
3887  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
3888  (simplify
3889   (scale REAL_CST@0 @1)
3890   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3891    @0)))
3893 /* Canonicalization of sequences of math builtins.  These rules represent
3894    IL simplifications but are not necessarily optimizations.
3896    The sincos pass is responsible for picking "optimal" implementations
3897    of math builtins, which may be more complicated and can sometimes go
3898    the other way, e.g. converting pow into a sequence of sqrts.
3899    We only want to do these canonicalizations before the pass has run.  */
3901 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3902  /* Simplify tan(x) * cos(x) -> sin(x). */
3903  (simplify
3904   (mult:c (TAN:s @0) (COS:s @0))
3905    (SIN @0))
3907  /* Simplify x * pow(x,c) -> pow(x,c+1). */
3908  (simplify
3909   (mult:c @0 (POW:s @0 REAL_CST@1))
3910   (if (!TREE_OVERFLOW (@1))
3911    (POW @0 (plus @1 { build_one_cst (type); }))))
3913  /* Simplify sin(x) / cos(x) -> tan(x). */
3914  (simplify
3915   (rdiv (SIN:s @0) (COS:s @0))
3916    (TAN @0))
3918  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3919  (simplify
3920   (rdiv (COS:s @0) (SIN:s @0))
3921    (rdiv { build_one_cst (type); } (TAN @0)))
3923  /* Simplify sin(x) / tan(x) -> cos(x). */
3924  (simplify
3925   (rdiv (SIN:s @0) (TAN:s @0))
3926   (if (! HONOR_NANS (@0)
3927        && ! HONOR_INFINITIES (@0))
3928    (COS @0)))
3930  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3931  (simplify
3932   (rdiv (TAN:s @0) (SIN:s @0))
3933   (if (! HONOR_NANS (@0)
3934        && ! HONOR_INFINITIES (@0))
3935    (rdiv { build_one_cst (type); } (COS @0))))
3937  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3938  (simplify
3939   (mult (POW:s @0 @1) (POW:s @0 @2))
3940    (POW @0 (plus @1 @2)))
3942  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3943  (simplify
3944   (mult (POW:s @0 @1) (POW:s @2 @1))
3945    (POW (mult @0 @2) @1))
3947  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3948  (simplify
3949   (mult (POWI:s @0 @1) (POWI:s @2 @1))
3950    (POWI (mult @0 @2) @1))
3952  /* Simplify pow(x,c) / x -> pow(x,c-1). */
3953  (simplify
3954   (rdiv (POW:s @0 REAL_CST@1) @0)
3955   (if (!TREE_OVERFLOW (@1))
3956    (POW @0 (minus @1 { build_one_cst (type); }))))
3958  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3959  (simplify
3960   (rdiv @0 (POW:s @1 @2))
3961    (mult @0 (POW @1 (negate @2))))
3963  (for sqrts (SQRT)
3964       cbrts (CBRT)
3965       pows (POW)
3966   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
3967   (simplify
3968    (sqrts (sqrts @0))
3969    (pows @0 { build_real (type, dconst_quarter ()); }))
3970   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
3971   (simplify
3972    (sqrts (cbrts @0))
3973    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3974   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
3975   (simplify
3976    (cbrts (sqrts @0))
3977    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3978   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
3979   (simplify
3980    (cbrts (cbrts tree_expr_nonnegative_p@0))
3981    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3982   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
3983   (simplify
3984    (sqrts (pows @0 @1))
3985    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3986   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
3987   (simplify
3988    (cbrts (pows tree_expr_nonnegative_p@0 @1))
3989    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3990   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
3991   (simplify
3992    (pows (sqrts @0) @1)
3993    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3994   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
3995   (simplify
3996    (pows (cbrts tree_expr_nonnegative_p@0) @1)
3997    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3998   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
3999   (simplify
4000    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4001    (pows @0 (mult @1 @2))))
4003  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
4004  (simplify
4005   (CABS (complex @0 @0))
4006   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4008  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
4009  (simplify
4010   (HYPOT @0 @0)
4011   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4013  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
4014  (for cexps (CEXP)
4015       exps (EXP)
4016       cexpis (CEXPI)
4017   (simplify
4018    (cexps compositional_complex@0)
4019    (if (targetm.libc_has_function (function_c99_math_complex))
4020     (complex
4021      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4022      (mult @1 (imagpart @2)))))))
4024 (if (canonicalize_math_p ())
4025  /* floor(x) -> trunc(x) if x is nonnegative.  */
4026  (for floors (FLOOR)
4027       truncs (TRUNC)
4028   (simplify
4029    (floors tree_expr_nonnegative_p@0)
4030    (truncs @0))))
4032 (match double_value_p
4033  @0
4034  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4035 (for froms (BUILT_IN_TRUNCL
4036             BUILT_IN_FLOORL
4037             BUILT_IN_CEILL
4038             BUILT_IN_ROUNDL
4039             BUILT_IN_NEARBYINTL
4040             BUILT_IN_RINTL)
4041      tos (BUILT_IN_TRUNC
4042           BUILT_IN_FLOOR
4043           BUILT_IN_CEIL
4044           BUILT_IN_ROUND
4045           BUILT_IN_NEARBYINT
4046           BUILT_IN_RINT)
4047  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
4048  (if (optimize && canonicalize_math_p ())
4049   (simplify
4050    (froms (convert double_value_p@0))
4051    (convert (tos @0)))))
4053 (match float_value_p
4054  @0
4055  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4056 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4057             BUILT_IN_FLOORL BUILT_IN_FLOOR
4058             BUILT_IN_CEILL BUILT_IN_CEIL
4059             BUILT_IN_ROUNDL BUILT_IN_ROUND
4060             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4061             BUILT_IN_RINTL BUILT_IN_RINT)
4062      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4063           BUILT_IN_FLOORF BUILT_IN_FLOORF
4064           BUILT_IN_CEILF BUILT_IN_CEILF
4065           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4066           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4067           BUILT_IN_RINTF BUILT_IN_RINTF)
4068  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4069     if x is a float.  */
4070  (if (optimize && canonicalize_math_p ()
4071       && targetm.libc_has_function (function_c99_misc))
4072   (simplify
4073    (froms (convert float_value_p@0))
4074    (convert (tos @0)))))
4076 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4077      tos (XFLOOR XCEIL XROUND XRINT)
4078  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
4079  (if (optimize && canonicalize_math_p ())
4080   (simplify
4081    (froms (convert double_value_p@0))
4082    (tos @0))))
4084 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4085             XFLOOR XCEIL XROUND XRINT)
4086      tos (XFLOORF XCEILF XROUNDF XRINTF)
4087  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4088     if x is a float.  */
4089  (if (optimize && canonicalize_math_p ())
4090   (simplify
4091    (froms (convert float_value_p@0))
4092    (tos @0))))
4094 (if (canonicalize_math_p ())
4095  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
4096  (for floors (IFLOOR LFLOOR LLFLOOR)
4097   (simplify
4098    (floors tree_expr_nonnegative_p@0)
4099    (fix_trunc @0))))
4101 (if (canonicalize_math_p ())
4102  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
4103  (for fns (IFLOOR LFLOOR LLFLOOR
4104            ICEIL LCEIL LLCEIL
4105            IROUND LROUND LLROUND)
4106   (simplify
4107    (fns integer_valued_real_p@0)
4108    (fix_trunc @0)))
4109  (if (!flag_errno_math)
4110   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
4111   (for rints (IRINT LRINT LLRINT)
4112    (simplify
4113     (rints integer_valued_real_p@0)
4114     (fix_trunc @0)))))
4116 (if (canonicalize_math_p ())
4117  (for ifn (IFLOOR ICEIL IROUND IRINT)
4118       lfn (LFLOOR LCEIL LROUND LRINT)
4119       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4120   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4121      sizeof (int) == sizeof (long).  */
4122   (if (TYPE_PRECISION (integer_type_node)
4123        == TYPE_PRECISION (long_integer_type_node))
4124    (simplify
4125     (ifn @0)
4126     (lfn:long_integer_type_node @0)))
4127   /* Canonicalize llround (x) to lround (x) on LP64 targets where
4128      sizeof (long long) == sizeof (long).  */
4129   (if (TYPE_PRECISION (long_long_integer_type_node)
4130        == TYPE_PRECISION (long_integer_type_node))
4131    (simplify
4132     (llfn @0)
4133     (lfn:long_integer_type_node @0)))))
4135 /* cproj(x) -> x if we're ignoring infinities.  */
4136 (simplify
4137  (CPROJ @0)
4138  (if (!HONOR_INFINITIES (type))
4139    @0))
4141 /* If the real part is inf and the imag part is known to be
4142    nonnegative, return (inf + 0i).  */
4143 (simplify
4144  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4145  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4146   { build_complex_inf (type, false); }))
4148 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
4149 (simplify
4150  (CPROJ (complex @0 REAL_CST@1))
4151  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4152   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4154 (for pows (POW)
4155      sqrts (SQRT)
4156      cbrts (CBRT)
4157  (simplify
4158   (pows @0 REAL_CST@1)
4159   (with {
4160     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4161     REAL_VALUE_TYPE tmp;
4162    }
4163    (switch
4164     /* pow(x,0) -> 1.  */
4165     (if (real_equal (value, &dconst0))
4166      { build_real (type, dconst1); })
4167     /* pow(x,1) -> x.  */
4168     (if (real_equal (value, &dconst1))
4169      @0)
4170     /* pow(x,-1) -> 1/x.  */
4171     (if (real_equal (value, &dconstm1))
4172      (rdiv { build_real (type, dconst1); } @0))
4173     /* pow(x,0.5) -> sqrt(x).  */
4174     (if (flag_unsafe_math_optimizations
4175          && canonicalize_math_p ()
4176          && real_equal (value, &dconsthalf))
4177      (sqrts @0))
4178     /* pow(x,1/3) -> cbrt(x).  */
4179     (if (flag_unsafe_math_optimizations
4180          && canonicalize_math_p ()
4181          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4182              real_equal (value, &tmp)))
4183      (cbrts @0))))))
4185 /* powi(1,x) -> 1.  */
4186 (simplify
4187  (POWI real_onep@0 @1)
4188  @0)
4190 (simplify
4191  (POWI @0 INTEGER_CST@1)
4192  (switch
4193   /* powi(x,0) -> 1.  */
4194   (if (wi::to_wide (@1) == 0)
4195    { build_real (type, dconst1); })
4196   /* powi(x,1) -> x.  */
4197   (if (wi::to_wide (@1) == 1)
4198    @0)
4199   /* powi(x,-1) -> 1/x.  */
4200   (if (wi::to_wide (@1) == -1)
4201    (rdiv { build_real (type, dconst1); } @0))))
4203 /* Narrowing of arithmetic and logical operations. 
4205    These are conceptually similar to the transformations performed for
4206    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
4207    term we want to move all that code out of the front-ends into here.  */
4209 /* If we have a narrowing conversion of an arithmetic operation where
4210    both operands are widening conversions from the same type as the outer
4211    narrowing conversion.  Then convert the innermost operands to a suitable
4212    unsigned type (to avoid introducing undefined behavior), perform the
4213    operation and convert the result to the desired type.  */
4214 (for op (plus minus)
4215   (simplify
4216     (convert (op:s (convert@2 @0) (convert?@3 @1)))
4217     (if (INTEGRAL_TYPE_P (type)
4218          /* We check for type compatibility between @0 and @1 below,
4219             so there's no need to check that @1/@3 are integral types.  */
4220          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4221          && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4222          /* The precision of the type of each operand must match the
4223             precision of the mode of each operand, similarly for the
4224             result.  */
4225          && type_has_mode_precision_p (TREE_TYPE (@0))
4226          && type_has_mode_precision_p (TREE_TYPE (@1))
4227          && type_has_mode_precision_p (type)
4228          /* The inner conversion must be a widening conversion.  */
4229          && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4230          && types_match (@0, type)
4231          && (types_match (@0, @1)
4232              /* Or the second operand is const integer or converted const
4233                 integer from valueize.  */
4234              || TREE_CODE (@1) == INTEGER_CST))
4235       (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4236         (op @0 (convert @1))
4237         (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4238          (convert (op (convert:utype @0)
4239                       (convert:utype @1))))))))
4241 /* This is another case of narrowing, specifically when there's an outer
4242    BIT_AND_EXPR which masks off bits outside the type of the innermost
4243    operands.   Like the previous case we have to convert the operands
4244    to unsigned types to avoid introducing undefined behavior for the
4245    arithmetic operation.  */
4246 (for op (minus plus)
4247  (simplify
4248   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4249   (if (INTEGRAL_TYPE_P (type)
4250        /* We check for type compatibility between @0 and @1 below,
4251           so there's no need to check that @1/@3 are integral types.  */
4252        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4253        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4254        /* The precision of the type of each operand must match the
4255           precision of the mode of each operand, similarly for the
4256           result.  */
4257        && type_has_mode_precision_p (TREE_TYPE (@0))
4258        && type_has_mode_precision_p (TREE_TYPE (@1))
4259        && type_has_mode_precision_p (type)
4260        /* The inner conversion must be a widening conversion.  */
4261        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4262        && types_match (@0, @1)
4263        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4264            <= TYPE_PRECISION (TREE_TYPE (@0)))
4265        && (wi::to_wide (@4)
4266            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4267                        true, TYPE_PRECISION (type))) == 0)
4268    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4269     (with { tree ntype = TREE_TYPE (@0); }
4270      (convert (bit_and (op @0 @1) (convert:ntype @4))))
4271     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4272      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4273                (convert:utype @4))))))))
4275 /* Transform (@0 < @1 and @0 < @2) to use min, 
4276    (@0 > @1 and @0 > @2) to use max */
4277 (for op (lt le gt ge)
4278      ext (min min max max)
4279  (simplify
4280   (bit_and (op:cs @0 @1) (op:cs @0 @2))
4281   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4282        && TREE_CODE (@0) != INTEGER_CST)
4283    (op @0 (ext @1 @2)))))
4285 (simplify
4286  /* signbit(x) -> 0 if x is nonnegative.  */
4287  (SIGNBIT tree_expr_nonnegative_p@0)
4288  { integer_zero_node; })
4290 (simplify
4291  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
4292  (SIGNBIT @0)
4293  (if (!HONOR_SIGNED_ZEROS (@0))
4294   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4296 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
4297 (for cmp (eq ne)
4298  (for op (plus minus)
4299       rop (minus plus)
4300   (simplify
4301    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4302    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4303         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4304         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4305         && !TYPE_SATURATING (TREE_TYPE (@0)))
4306     (with { tree res = int_const_binop (rop, @2, @1); }
4307      (if (TREE_OVERFLOW (res)
4308           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4309       { constant_boolean_node (cmp == NE_EXPR, type); }
4310       (if (single_use (@3))
4311        (cmp @0 { res; }))))))))
4312 (for cmp (lt le gt ge)
4313  (for op (plus minus)
4314       rop (minus plus)
4315   (simplify
4316    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4317    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4318         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4319     (with { tree res = int_const_binop (rop, @2, @1); }
4320      (if (TREE_OVERFLOW (res))
4321       {
4322         fold_overflow_warning (("assuming signed overflow does not occur "
4323                                 "when simplifying conditional to constant"),
4324                                WARN_STRICT_OVERFLOW_CONDITIONAL);
4325         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4326         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
4327         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4328                                   TYPE_SIGN (TREE_TYPE (@1)))
4329                         != (op == MINUS_EXPR);
4330         constant_boolean_node (less == ovf_high, type);
4331       }
4332       (if (single_use (@3))
4333        (with
4334         {
4335           fold_overflow_warning (("assuming signed overflow does not occur "
4336                                   "when changing X +- C1 cmp C2 to "
4337                                   "X cmp C2 -+ C1"),
4338                                  WARN_STRICT_OVERFLOW_COMPARISON);
4339         }
4340         (cmp @0 { res; })))))))))
4342 /* Canonicalizations of BIT_FIELD_REFs.  */
4344 (simplify
4345  (BIT_FIELD_REF @0 @1 @2)
4346  (switch
4347   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4348        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4349    (switch
4350     (if (integer_zerop (@2))
4351      (view_convert (realpart @0)))
4352     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4353      (view_convert (imagpart @0)))))
4354   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4355        && INTEGRAL_TYPE_P (type)
4356        /* On GIMPLE this should only apply to register arguments.  */
4357        && (! GIMPLE || is_gimple_reg (@0))
4358        /* A bit-field-ref that referenced the full argument can be stripped.  */
4359        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4360             && integer_zerop (@2))
4361            /* Low-parts can be reduced to integral conversions.
4362               ???  The following doesn't work for PDP endian.  */
4363            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4364                /* Don't even think about BITS_BIG_ENDIAN.  */
4365                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4366                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4367                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4368                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
4369                                             - TYPE_PRECISION (type))
4370                                          : 0)) == 0)))
4371    (convert @0))))
4373 /* Simplify vector extracts.  */
4375 (simplify
4376  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4377  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4378       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4379           || (VECTOR_TYPE_P (type)
4380               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4381   (with
4382    {
4383      tree ctor = (TREE_CODE (@0) == SSA_NAME
4384                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4385      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4386      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4387      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4388      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4389    }
4390    (if (n != 0
4391         && (idx % width) == 0
4392         && (n % width) == 0
4393         && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4394     (with
4395      {
4396        idx = idx / width;
4397        n = n / width;
4398        /* Constructor elements can be subvectors.  */
4399        unsigned HOST_WIDE_INT k = 1;
4400        if (CONSTRUCTOR_NELTS (ctor) != 0)
4401          {
4402            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4403            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4404              k = TYPE_VECTOR_SUBPARTS (cons_elem);
4405          }
4406      }
4407      (switch
4408       /* We keep an exact subset of the constructor elements.  */
4409       (if ((idx % k) == 0 && (n % k) == 0)
4410        (if (CONSTRUCTOR_NELTS (ctor) == 0)
4411         { build_constructor (type, NULL); }
4412         (with
4413          {
4414            idx /= k;
4415            n /= k;
4416          }
4417          (if (n == 1)
4418           (if (idx < CONSTRUCTOR_NELTS (ctor))
4419            { CONSTRUCTOR_ELT (ctor, idx)->value; }
4420            { build_zero_cst (type); })
4421           {
4422             vec<constructor_elt, va_gc> *vals;
4423             vec_alloc (vals, n);
4424             for (unsigned i = 0;
4425                  i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4426               CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4427                                       CONSTRUCTOR_ELT (ctor, idx + i)->value);
4428             build_constructor (type, vals);
4429           }))))
4430       /* The bitfield references a single constructor element.  */
4431       (if (idx + n <= (idx / k + 1) * k)
4432        (switch
4433         (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4434          { build_zero_cst (type); })
4435         (if (n == k)
4436          { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4437         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4438                        @1 { bitsize_int ((idx % k) * width); })))))))))
4440 /* Simplify a bit extraction from a bit insertion for the cases with
4441    the inserted element fully covering the extraction or the insertion
4442    not touching the extraction.  */
4443 (simplify
4444  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4445  (with
4446   {
4447     unsigned HOST_WIDE_INT isize;
4448     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4449       isize = TYPE_PRECISION (TREE_TYPE (@1));
4450     else
4451       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4452   }
4453   (switch
4454    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4455         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4456                       wi::to_wide (@ipos) + isize))
4457     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4458                                                  wi::to_wide (@rpos)
4459                                                  - wi::to_wide (@ipos)); }))
4460    (if (wi::geu_p (wi::to_wide (@ipos),
4461                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
4462         || wi::geu_p (wi::to_wide (@rpos),
4463                       wi::to_wide (@ipos) + isize))
4464     (BIT_FIELD_REF @0 @rsize @rpos)))))