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